1 /* $OpenBSD: asm.h,v 1.20 2021/09/04 22:15:33 bluhm Exp $ */ 2 /* $NetBSD: asm.h,v 1.2 2003/05/02 18:05:47 yamt Exp $ */ 3 4 /*- 5 * Copyright (c) 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * William Jolitz. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)asm.h 5.5 (Berkeley) 5/7/91 36 */ 37 38 #ifndef _MACHINE_ASM_H_ 39 #define _MACHINE_ASM_H_ 40 41 #ifdef __PIC__ 42 #define PIC_PLT(x) x@PLT 43 #define PIC_GOT(x) x@GOTPCREL(%rip) 44 #else 45 #define PIC_PLT(x) x 46 #define PIC_GOT(x) x 47 #endif 48 49 # define _C_LABEL(x) x 50 #define _ASM_LABEL(x) x 51 52 #define CVAROFF(x,y) (_C_LABEL(x)+y)(%rip) 53 54 #ifdef __STDC__ 55 # define __CONCAT(x,y) x ## y 56 # define __STRING(x) #x 57 #else 58 # define __CONCAT(x,y) x/**/y 59 # define __STRING(x) "x" 60 #endif 61 62 /* let kernels and others override entrypoint alignment */ 63 #ifndef _ALIGN_TEXT 64 #define _ALIGN_TEXT .align 16, 0x90 65 #endif 66 #define _ALIGN_TRAPS .align 16, 0xcc 67 68 #define _GENTRY(x) .globl x; .type x,@function; x: 69 #define _ENTRY(x) \ 70 .text; _ALIGN_TRAPS; _GENTRY(x) 71 #define _NENTRY(x) \ 72 .text; _ALIGN_TEXT; _GENTRY(x) 73 74 #ifdef _KERNEL 75 #define KUTEXT .section .kutext, "ax", @progbits 76 77 #define KUTEXT_PAGE_START .pushsection .kutext.page, "a", @progbits 78 #define KTEXT_PAGE_START .pushsection .ktext.page, "ax", @progbits 79 #define KUTEXT_PAGE_END .popsection 80 #define KTEXT_PAGE_END .popsection 81 82 #define IDTVEC(name) \ 83 KUTEXT; _ALIGN_TRAPS; IDTVEC_NOALIGN(name) 84 #define IDTVEC_NOALIGN(name) _GENTRY(X ## name) 85 #define GENTRY(x) _GENTRY(x) 86 #define KIDTVEC(name) \ 87 .text; _ALIGN_TRAPS; IDTVEC_NOALIGN(name) 88 #define KIDTVEC_FALLTHROUGH(name) \ 89 _ALIGN_TEXT; IDTVEC_NOALIGN(name) 90 #define KUENTRY(x) \ 91 KUTEXT; _ALIGN_TRAPS; _GENTRY(x) 92 93 /* Return stack refill, to prevent speculation attacks on natural returns */ 94 #define RET_STACK_REFILL_WITH_RCX \ 95 mov $8,%rcx ; \ 96 _ALIGN_TEXT ; \ 97 3: call 5f ; \ 98 4: pause ; \ 99 lfence ; \ 100 call 4b ; \ 101 _ALIGN_TRAPS ; \ 102 5: call 7f ; \ 103 6: pause ; \ 104 lfence ; \ 105 call 6b ; \ 106 _ALIGN_TRAPS ; \ 107 7: loop 3b ; \ 108 add $(16*8),%rsp 109 110 #endif /* _KERNEL */ 111 112 #ifdef __STDC__ 113 #define CPUVAR(off) %gs:CPU_INFO_ ## off 114 #else 115 #define CPUVAR(off) %gs:CPU_INFO_/**/off 116 #endif 117 118 119 #if defined(PROF) || defined(GPROF) 120 # define _PROF_PROLOGUE \ 121 pushq %rbp; leaq (%rsp),%rbp; call PIC_PLT(__mcount); popq %rbp 122 #else 123 # define _PROF_PROLOGUE 124 #endif 125 126 #if defined(_RET_PROTECTOR) 127 # define RETGUARD_SETUP_OFF(x, reg, off) \ 128 RETGUARD_SYMBOL(x); \ 129 movq (__retguard_ ## x)(%rip), %reg; \ 130 xorq off(%rsp), %reg 131 # define RETGUARD_SETUP(x, reg) \ 132 RETGUARD_SETUP_OFF(x, reg, 0) 133 # define RETGUARD_CHECK(x, reg) \ 134 xorq (%rsp), %reg; \ 135 cmpq (__retguard_ ## x)(%rip), %reg; \ 136 je 66f; \ 137 int3; int3; \ 138 .zero (0xf - ((. + 3 - x) & 0xf)), 0xcc; \ 139 66: 140 # define RETGUARD_PUSH(reg) \ 141 pushq %reg 142 # define RETGUARD_POP(reg) \ 143 popq %reg 144 # define RETGUARD_SYMBOL(x) \ 145 .ifndef __retguard_ ## x; \ 146 .hidden __retguard_ ## x; \ 147 .type __retguard_ ## x,@object; \ 148 .pushsection .openbsd.randomdata.retguard,"aw",@progbits; \ 149 .weak __retguard_ ## x; \ 150 .p2align 3; \ 151 __retguard_ ## x: ; \ 152 .quad 0; \ 153 .size __retguard_ ## x, 8; \ 154 .popsection; \ 155 .endif 156 #else 157 # define RETGUARD_SETUP_OFF(x, reg, off) 158 # define RETGUARD_SETUP(x, reg) 159 # define RETGUARD_CHECK(x, reg) 160 # define RETGUARD_PUSH(reg) 161 # define RETGUARD_POP(reg) 162 # define RETGUARD_SYMBOL(x) 163 #endif 164 165 #define ENTRY(y) _ENTRY(_C_LABEL(y)); _PROF_PROLOGUE 166 #define NENTRY(y) _NENTRY(_C_LABEL(y)) 167 #define ASENTRY(y) _NENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE 168 #define END(y) .size y, . - y 169 170 #define STRONG_ALIAS(alias,sym) \ 171 .global alias; \ 172 alias = sym 173 #define WEAK_ALIAS(alias,sym) \ 174 .weak alias; \ 175 alias = sym 176 177 /* XXXfvdl do not use stabs here */ 178 #ifdef __STDC__ 179 #define WARN_REFERENCES(sym,msg) \ 180 .stabs msg ## ,30,0,0,0 ; \ 181 .stabs __STRING(_C_LABEL(sym)) ## ,1,0,0,0 182 #else 183 #define WARN_REFERENCES(sym,msg) \ 184 .stabs msg,30,0,0,0 ; \ 185 .stabs __STRING(sym),1,0,0,0 186 #endif /* __STDC__ */ 187 188 /* generic retpoline ("return trampoline") generator */ 189 #define JMP_RETPOLINE(reg) \ 190 call 69f ; \ 191 68: pause ; \ 192 lfence ; \ 193 jmp 68b ; \ 194 _ALIGN_TRAPS ; \ 195 69: mov %reg,(%rsp) ; \ 196 ret ; \ 197 lfence 198 199 #endif /* !_MACHINE_ASM_H_ */ 200