1 /* -*- mode: asm -*- */
3 * SPDX-License-Identifier: BSD-3-Clause
5 * Copyright (c) 1993 The Regents of the University of California.
8 * Copyright (c) 2018 The FreeBSD Foundation
11 * Portions of this software were developed by
12 * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from
13 * the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #ifndef _MACHINE_ASMACROS_H_
43 #define _MACHINE_ASMACROS_H_
45 #include <sys/cdefs.h>
47 /* XXX too much duplication in various asm*.h's. */
50 * CNAME is used to manage the relationship between symbol names in C
51 * and the equivalent assembly language names. CNAME is given a name as
52 * it would be used in a C program. It expands to the equivalent assembly
55 #define CNAME(csym) csym
57 #define ALIGN_DATA .p2align 3 /* 8 byte alignment, zero filled */
59 #define ALIGN_TEXT .p2align 4,0x90 /* 16-byte alignment, nop filled */
61 #define ALIGN_TEXT .p2align 4,0x90 /* 16-byte alignment, nop filled */
63 #define SUPERALIGN_TEXT .p2align 4,0x90 /* 16-byte alignment, nop filled */
65 #define GEN_ENTRY(name) ALIGN_TEXT; .globl CNAME(name); \
66 .type CNAME(name),@function; CNAME(name):
67 #define NON_GPROF_ENTRY(name) GEN_ENTRY(name)
68 #define NON_GPROF_RET .byte 0xc3 /* opcode for `ret' */
70 #define END(name) .size name, . - name
74 * __mcount is like [.]mcount except that doesn't require its caller to set
75 * up a frame pointer. It must be called before pushing anything onto the
76 * stack. gcc should eventually generate code to call __mcount in most
77 * cases. This would make -pg in combination with -fomit-frame-pointer
78 * useful. gcc has a configuration variable PROFILE_BEFORE_PROLOGUE to
79 * allow profiling before setting up the frame pointer, but this is
80 * inadequate for good handling of special cases, e.g., -fpic works best
81 * with profiling after the prologue.
83 * [.]mexitcount is a new function to support non-statistical profiling if an
84 * accurate clock is available. For C sources, calls to it are generated
85 * by the FreeBSD extension `-mprofiler-epilogue' to gcc. It is best to
86 * call [.]mexitcount at the end of a function like the MEXITCOUNT macro does,
87 * but gcc currently generates calls to it at the start of the epilogue to
88 * avoid problems with -fpic.
90 * [.]mcount and __mcount may clobber the call-used registers and %ef.
91 * [.]mexitcount may clobber %ecx and %ef.
93 * Cross-jumping makes non-statistical profiling timing more complicated.
94 * It is handled in many cases by calling [.]mexitcount before jumping. It
95 * is handled for conditional jumps using CROSSJUMP() and CROSSJUMP_LABEL().
96 * It is handled for some fault-handling jumps by not sharing the exit
99 * ALTENTRY() must be before a corresponding ENTRY() so that it can jump to
100 * the main entry point. Note that alt entries are counted twice. They
101 * have to be counted as ordinary entries for gprof to get the call times
102 * right for the ordinary entries.
104 * High local labels are used in macros to avoid clashes with local labels
107 * Ordinary `ret' is used instead of a macro `RET' because there are a lot
108 * of `ret's. 0xc3 is the opcode for `ret' (`#define ret ... ret' can't
109 * be used because this file is sometimes preprocessed in traditional mode).
110 * `ret' clobbers eflags but this doesn't matter.
112 #define ALTENTRY(name) GEN_ENTRY(name) ; MCOUNT ; MEXITCOUNT ; jmp 9f
113 #define CROSSJUMP(jtrue, label, jfalse) \
114 jfalse 8f; MEXITCOUNT; jmp __CONCAT(to,label); 8:
115 #define CROSSJUMPTARGET(label) \
116 ALIGN_TEXT; __CONCAT(to,label): ; MCOUNT; jmp label
117 #define ENTRY(name) GEN_ENTRY(name) ; 9: ; MCOUNT
118 #define FAKE_MCOUNT(caller) pushq caller ; call __mcount ; popq %rcx
119 #define MCOUNT call __mcount
120 #define MCOUNT_LABEL(name) GEN_ENTRY(name) ; nop ; ALIGN_TEXT
122 #define MEXITCOUNT call .mexitcount
123 #define ret MEXITCOUNT ; NON_GPROF_RET
130 * ALTENTRY() has to align because it is before a corresponding ENTRY().
131 * ENTRY() has to align to because there may be no ALTENTRY() before it.
132 * If there is a previous ALTENTRY() then the alignment code for ENTRY()
135 #define ALTENTRY(name) GEN_ENTRY(name)
136 #define CROSSJUMP(jtrue, label, jfalse) jtrue label
137 #define CROSSJUMPTARGET(label)
138 #define ENTRY(name) GEN_ENTRY(name)
139 #define FAKE_MCOUNT(caller)
141 #define MCOUNT_LABEL(name)
146 * Convenience for adding frame pointers to hand-coded ASM. Useful for
147 * DTrace, HWPMC, and KDB.
149 #define PUSH_FRAME_POINTER \
152 #define POP_FRAME_POINTER \
157 * Access per-CPU data.
159 #define PCPU(member) %gs:PC_ ## member
160 #define PCPU_ADDR(member, reg) \
161 movq %gs:PC_PRVSPACE, reg ; \
162 addq $PC_ ## member, reg
165 * Convenience macro for declaring interrupt entry points.
167 #define IDTVEC(name) ALIGN_TEXT; .globl __CONCAT(X,name); \
168 .type __CONCAT(X,name),@function; __CONCAT(X,name):
177 .macro MOVE_STACKS qw
180 movq .L.offset(%rsp),%rdx
181 movq %rdx,.L.offset(%rax)
182 .L.offset=.L.offset+8
186 .macro PTI_UUENTRY has_err
190 subq $PTI_SIZE - 8 * (1 - \has_err),%rax
191 MOVE_STACKS ((PTI_SIZE / 8) - 1 + \has_err)
197 .macro PTI_UENTRY has_err
208 .macro PTI_ENTRY name, contk, contu, has_err=0
211 .type X\name\()_pti,@function
213 /* %rax, %rdx, and possibly err are not yet pushed */
214 testb $SEL_RPL_MASK,PTI_CS-PTI_ERR-((1-\has_err)*8)(%rsp)
220 .macro PTI_INTRENTRY vec_name
222 .globl X\vec_name\()_pti
223 .type X\vec_name\()_pti,@function
225 testb $SEL_RPL_MASK,PTI_CS-3*8(%rsp) /* err, %rax, %rdx not pushed */
231 .macro INTR_PUSH_FRAME vec_name
234 .type X\vec_name,@function
236 testb $SEL_RPL_MASK,PTI_CS-3*8(%rsp) /* come from kernel? */
237 jz .L\vec_name\()_u /* Yes, dont swapgs again */
241 subq $TF_RIP,%rsp /* skip dummy tf_err and tf_trapno */
242 movq %rdi,TF_RDI(%rsp)
243 movq %rsi,TF_RSI(%rsp)
244 movq %rdx,TF_RDX(%rsp)
245 movq %rcx,TF_RCX(%rsp)
248 movq %rax,TF_RAX(%rsp)
249 movq %rbx,TF_RBX(%rsp)
250 movq %rbp,TF_RBP(%rsp)
251 movq %r10,TF_R10(%rsp)
252 movq %r11,TF_R11(%rsp)
253 movq %r12,TF_R12(%rsp)
254 movq %r13,TF_R13(%rsp)
255 movq %r14,TF_R14(%rsp)
256 movq %r15,TF_R15(%rsp)
258 movl $TF_HASSEGS,TF_FLAGS(%rsp)
260 andq $~(PSL_D|PSL_AC),(%rsp)
262 testb $SEL_RPL_MASK,TF_CS(%rsp) /* come from kernel ? */
263 jz 1f /* yes, leave PCB_FULL_IRET alone */
264 movq PCPU(CURPCB),%r8
265 andl $~PCB_FULL_IRET,PCB_FLAGS(%r8)
266 call handle_ibrs_entry
270 .macro INTR_HANDLER vec_name
272 PTI_INTRENTRY \vec_name
273 INTR_PUSH_FRAME \vec_name
277 movq TF_RDI(%rsp),%rdi
278 movq TF_RSI(%rsp),%rsi
279 movq TF_RDX(%rsp),%rdx
280 movq TF_RCX(%rsp),%rcx
283 movq TF_RAX(%rsp),%rax
284 movq TF_RBX(%rsp),%rbx
285 movq TF_RBP(%rsp),%rbp
286 movq TF_R10(%rsp),%r10
287 movq TF_R11(%rsp),%r11
288 movq TF_R12(%rsp),%r12
289 movq TF_R13(%rsp),%r13
290 movq TF_R14(%rsp),%r14
291 movq TF_R15(%rsp),%r15
297 #define ELFNOTE(name, type, desctype, descdata...) \
298 .pushsection .note.name ; \
300 .long 2f - 1f /* namesz */ ; \
301 .long 4f - 3f /* descsz */ ; \
305 3:desctype descdata ; \
308 #else /* !__STDC__, i.e. -traditional */
309 #define ELFNOTE(name, type, desctype, descdata) \
310 .pushsection .note.name ; \
312 .long 2f - 1f /* namesz */ ; \
313 .long 4f - 3f /* descsz */ ; \
317 3:desctype descdata ; \
320 #endif /* __STDC__ */
322 #endif /* !_MACHINE_ASMACROS_H_ */