| 1 |
/* asm-common-amd64.h - Common macros for AMD64 assembly |
| 2 |
* |
| 3 |
* Copyright (C) 2018 Jussi Kivilinna <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of Libgcrypt. |
| 6 |
* |
| 7 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as |
| 9 |
* published by the Free Software Foundation; either version 2.1 of |
| 10 |
* the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifndef GCRY_ASM_COMMON_AMD64_H |
| 22 |
#define GCRY_ASM_COMMON_AMD64_H |
| 23 |
|
| 24 |
#include <config.h> |
| 25 |
|
| 26 |
#ifdef HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS |
| 27 |
# define ELF(...) __VA_ARGS__ |
| 28 |
#else |
| 29 |
# define ELF(...) /*_*/ |
| 30 |
#endif |
| 31 |
|
| 32 |
#ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS |
| 33 |
# define SECTION_RODATA .section .rdata |
| 34 |
#else |
| 35 |
# define SECTION_RODATA .section .rodata |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef __PIC__ |
| 39 |
# define rRIP (%rip) |
| 40 |
#else |
| 41 |
# define rRIP |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifdef __PIC__ |
| 45 |
# define RIP %rip |
| 46 |
#else |
| 47 |
# define RIP |
| 48 |
#endif |
| 49 |
|
| 50 |
#ifdef __PIC__ |
| 51 |
# define ADD_RIP +rip |
| 52 |
#else |
| 53 |
# define ADD_RIP |
| 54 |
#endif |
| 55 |
|
| 56 |
#ifdef __PIC__ |
| 57 |
# define AT_PLT ELF(@PLT) |
| 58 |
#else |
| 59 |
# define AT_PLT |
| 60 |
#endif |
| 61 |
|
| 62 |
#if defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS) || !defined(__PIC__) |
| 63 |
# define GET_EXTERN_POINTER(name, reg) movabsq $name, reg |
| 64 |
#else |
| 65 |
# ifdef __code_model_large__ |
| 66 |
# define GET_EXTERN_POINTER(name, reg) \ |
| 67 |
pushq %r15; \ |
| 68 |
CFI_PUSH(%r15); \ |
| 69 |
pushq %r14; \ |
| 70 |
CFI_PUSH(%r14); \ |
| 71 |
1: leaq 1b(%rip), reg; \ |
| 72 |
movabsq $_GLOBAL_OFFSET_TABLE_-1b, %r14; \ |
| 73 |
movabsq $name@GOT, %r15; \ |
| 74 |
addq %r14, reg; \ |
| 75 |
popq %r14; \ |
| 76 |
CFI_POP(%r14); \ |
| 77 |
movq (reg, %r15), reg; \ |
| 78 |
popq %r15; \ |
| 79 |
CFI_POP(%r15); |
| 80 |
# else |
| 81 |
# define GET_EXTERN_POINTER(name, reg) movq name@GOTPCREL(%rip), reg |
| 82 |
# endif |
| 83 |
#endif |
| 84 |
|
| 85 |
#ifdef __CET__ |
| 86 |
#define ENDBRANCH endbr64 |
| 87 |
#else |
| 88 |
#define ENDBRANCH /*_*/ |
| 89 |
#endif |
| 90 |
|
| 91 |
#ifdef HAVE_GCC_ASM_CFI_DIRECTIVES |
| 92 |
/* CFI directives to emit DWARF stack unwinding information. */ |
| 93 |
# define CFI_STARTPROC() .cfi_startproc; ENDBRANCH |
| 94 |
# define CFI_ENDPROC() .cfi_endproc |
| 95 |
# define CFI_REMEMBER_STATE() .cfi_remember_state |
| 96 |
# define CFI_RESTORE_STATE() .cfi_restore_state |
| 97 |
# define CFI_ADJUST_CFA_OFFSET(off) .cfi_adjust_cfa_offset off |
| 98 |
# define CFI_REL_OFFSET(reg,off) .cfi_rel_offset reg, off |
| 99 |
# define CFI_DEF_CFA_REGISTER(reg) .cfi_def_cfa_register reg |
| 100 |
# define CFI_REGISTER(ro,rn) .cfi_register ro, rn |
| 101 |
# define CFI_RESTORE(reg) .cfi_restore reg |
| 102 |
|
| 103 |
# define CFI_PUSH(reg) \ |
| 104 |
CFI_ADJUST_CFA_OFFSET(8); CFI_REL_OFFSET(reg, 0) |
| 105 |
# define CFI_POP(reg) \ |
| 106 |
CFI_ADJUST_CFA_OFFSET(-8); CFI_RESTORE(reg) |
| 107 |
# define CFI_POP_TMP_REG() \ |
| 108 |
CFI_ADJUST_CFA_OFFSET(-8); |
| 109 |
# define CFI_LEAVE() \ |
| 110 |
CFI_ADJUST_CFA_OFFSET(-8); CFI_DEF_CFA_REGISTER(%rsp) |
| 111 |
|
| 112 |
/* CFA expressions are used for pointing CFA and registers to |
| 113 |
* %rsp relative offsets. */ |
| 114 |
# define DW_REGNO_rax 0 |
| 115 |
# define DW_REGNO_rdx 1 |
| 116 |
# define DW_REGNO_rcx 2 |
| 117 |
# define DW_REGNO_rbx 3 |
| 118 |
# define DW_REGNO_rsi 4 |
| 119 |
# define DW_REGNO_rdi 5 |
| 120 |
# define DW_REGNO_rbp 6 |
| 121 |
# define DW_REGNO_rsp 7 |
| 122 |
# define DW_REGNO_r8 8 |
| 123 |
# define DW_REGNO_r9 9 |
| 124 |
# define DW_REGNO_r10 10 |
| 125 |
# define DW_REGNO_r11 11 |
| 126 |
# define DW_REGNO_r12 12 |
| 127 |
# define DW_REGNO_r13 13 |
| 128 |
# define DW_REGNO_r14 14 |
| 129 |
# define DW_REGNO_r15 15 |
| 130 |
|
| 131 |
# define DW_REGNO(reg) DW_REGNO_ ## reg |
| 132 |
|
| 133 |
/* Fixed length encoding used for integers for now. */ |
| 134 |
# define DW_SLEB128_7BIT(value) \ |
| 135 |
0x00|((value) & 0x7f) |
| 136 |
# define DW_SLEB128_28BIT(value) \ |
| 137 |
0x80|((value)&0x7f), \ |
| 138 |
0x80|(((value)>>7)&0x7f), \ |
| 139 |
0x80|(((value)>>14)&0x7f), \ |
| 140 |
0x00|(((value)>>21)&0x7f) |
| 141 |
|
| 142 |
# define CFI_CFA_ON_STACK(rsp_offs,cfa_depth) \ |
| 143 |
.cfi_escape \ |
| 144 |
0x0f, /* DW_CFA_def_cfa_expression */ \ |
| 145 |
DW_SLEB128_7BIT(11), /* length */ \ |
| 146 |
0x77, /* DW_OP_breg7, rsp + constant */ \ |
| 147 |
DW_SLEB128_28BIT(rsp_offs), \ |
| 148 |
0x06, /* DW_OP_deref */ \ |
| 149 |
0x23, /* DW_OP_plus_constu */ \ |
| 150 |
DW_SLEB128_28BIT((cfa_depth)+8) |
| 151 |
|
| 152 |
# define CFI_REG_ON_STACK(reg,rsp_offs) \ |
| 153 |
.cfi_escape \ |
| 154 |
0x10, /* DW_CFA_expression */ \ |
| 155 |
DW_SLEB128_7BIT(DW_REGNO(reg)), \ |
| 156 |
DW_SLEB128_7BIT(5), /* length */ \ |
| 157 |
0x77, /* DW_OP_breg7, rsp + constant */ \ |
| 158 |
DW_SLEB128_28BIT(rsp_offs) |
| 159 |
|
| 160 |
#else |
| 161 |
# define CFI_STARTPROC() ENDBRANCH |
| 162 |
# define CFI_ENDPROC() |
| 163 |
# define CFI_REMEMBER_STATE() |
| 164 |
# define CFI_RESTORE_STATE() |
| 165 |
# define CFI_ADJUST_CFA_OFFSET(off) |
| 166 |
# define CFI_REL_OFFSET(reg,off) |
| 167 |
# define CFI_DEF_CFA_REGISTER(reg) |
| 168 |
# define CFI_REGISTER(ro,rn) |
| 169 |
# define CFI_RESTORE(reg) |
| 170 |
|
| 171 |
# define CFI_PUSH(reg) |
| 172 |
# define CFI_POP(reg) |
| 173 |
# define CFI_POP_TMP_REG() |
| 174 |
# define CFI_LEAVE() |
| 175 |
|
| 176 |
# define CFI_CFA_ON_STACK(rsp_offs,cfa_depth) |
| 177 |
# define CFI_REG_ON_STACK(reg,rsp_offs) |
| 178 |
#endif |
| 179 |
|
| 180 |
#ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS |
| 181 |
# define ENTER_SYSV_FUNC_PARAMS_0_4 \ |
| 182 |
pushq %rdi; \ |
| 183 |
CFI_PUSH(%rdi); \ |
| 184 |
pushq %rsi; \ |
| 185 |
CFI_PUSH(%rsi); \ |
| 186 |
movq %rcx, %rdi; \ |
| 187 |
movq %rdx, %rsi; \ |
| 188 |
movq %r8, %rdx; \ |
| 189 |
movq %r9, %rcx; \ |
| 190 |
|
| 191 |
# define ENTER_SYSV_FUNC_PARAMS_5 \ |
| 192 |
ENTER_SYSV_FUNC_PARAMS_0_4; \ |
| 193 |
movq 0x38(%rsp), %r8; |
| 194 |
|
| 195 |
# define ENTER_SYSV_FUNC_PARAMS_6 \ |
| 196 |
ENTER_SYSV_FUNC_PARAMS_5; \ |
| 197 |
movq 0x40(%rsp), %r9; |
| 198 |
|
| 199 |
# define EXIT_SYSV_FUNC \ |
| 200 |
popq %rsi; \ |
| 201 |
CFI_POP(%rsi); \ |
| 202 |
popq %rdi; \ |
| 203 |
CFI_POP(%rdi); |
| 204 |
#else |
| 205 |
# define ENTER_SYSV_FUNC_PARAMS_0_4 |
| 206 |
# define ENTER_SYSV_FUNC_PARAMS_5 |
| 207 |
# define ENTER_SYSV_FUNC_PARAMS_6 |
| 208 |
# define EXIT_SYSV_FUNC |
| 209 |
#endif |
| 210 |
|
| 211 |
/* 'ret' instruction replacement for straight-line speculation mitigation. */ |
| 212 |
#define ret_spec_stop \ |
| 213 |
ret; int3; |
| 214 |
|
| 215 |
/* This prevents speculative execution on old AVX512 CPUs, to prevent |
| 216 |
* speculative execution to AVX512 code. The vpopcntb instruction is |
| 217 |
* available on newer CPUs that do not suffer from significant frequency |
| 218 |
* drop when 512-bit vectors are utilized. */ |
| 219 |
#define spec_stop_avx512 \ |
| 220 |
vpxord %ymm16, %ymm16, %ymm16; \ |
| 221 |
vpopcntb %xmm16, %xmm16; /* Supported only by newer AVX512 CPUs. */ \ |
| 222 |
vpxord %ymm16, %ymm16, %ymm16; |
| 223 |
|
| 224 |
#define spec_stop_avx512_intel_syntax \ |
| 225 |
vpxord ymm16, ymm16, ymm16; \ |
| 226 |
vpopcntb xmm16, xmm16; /* Supported only by newer AVX512 CPUs. */ \ |
| 227 |
vpxord ymm16, ymm16, ymm16; |
| 228 |
|
| 229 |
#ifdef __CET__ |
| 230 |
/* Generate CET property for all assembly files including this header. */ |
| 231 |
ELF(.section .note.gnu.property,"a") |
| 232 |
ELF(.align 8) |
| 233 |
ELF(.long 1f - 0f) |
| 234 |
ELF(.long 4f - 1f) |
| 235 |
ELF(.long 5) |
| 236 |
ELF(0:) |
| 237 |
ELF(.byte 0x47, 0x4e, 0x55, 0) /* string "GNU" */ |
| 238 |
ELF(1:) |
| 239 |
ELF(.align 8) |
| 240 |
ELF(.long 0xc0000002) |
| 241 |
ELF(.long 3f - 2f) |
| 242 |
ELF(2:) |
| 243 |
ELF(.long 0x3) |
| 244 |
ELF(3:) |
| 245 |
ELF(.align 8) |
| 246 |
ELF(4:) |
| 247 |
#endif |
| 248 |
|
| 249 |
#endif /* GCRY_ASM_COMMON_AMD64_H */ |