| 1 |
/* asm-common-i386.h - Common macros for i386 assembly |
| 2 |
* |
| 3 |
* Copyright (C) 2023 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_I386_H |
| 22 |
#define GCRY_ASM_COMMON_I386_H |
| 23 |
|
| 24 |
#include <config.h> |
| 25 |
|
| 26 |
#ifdef HAVE_COMPATIBLE_GCC_I386_PLATFORM_AS |
| 27 |
# define ELF(...) __VA_ARGS__ |
| 28 |
#else |
| 29 |
# define ELF(...) /*_*/ |
| 30 |
#endif |
| 31 |
|
| 32 |
#ifdef HAVE_COMPATIBLE_GCC_WIN32_PLATFORM_AS |
| 33 |
# define SECTION_RODATA .section .rdata |
| 34 |
#else |
| 35 |
# define SECTION_RODATA .section .rodata |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef HAVE_COMPATIBLE_GCC_WIN32_PLATFORM_AS |
| 39 |
# define SYM_NAME(name) _##name |
| 40 |
#else |
| 41 |
# define SYM_NAME(name) name |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifdef HAVE_COMPATIBLE_GCC_WIN32_PLATFORM_AS |
| 45 |
# define DECL_GET_PC_THUNK(reg) |
| 46 |
# define GET_DATA_POINTER(name, reg) leal name, %reg |
| 47 |
#else |
| 48 |
# define DECL_GET_PC_THUNK(reg) \ |
| 49 |
.type __gcry_get_pc_thunk_##reg, @function; \ |
| 50 |
.align 16; \ |
| 51 |
__gcry_get_pc_thunk_##reg:; \ |
| 52 |
CFI_STARTPROC(); \ |
| 53 |
movl (%esp), %reg; \ |
| 54 |
ret_spec_stop; \ |
| 55 |
CFI_ENDPROC() |
| 56 |
# define GET_DATA_POINTER(name, reg) \ |
| 57 |
call __gcry_get_pc_thunk_##reg; \ |
| 58 |
addl $_GLOBAL_OFFSET_TABLE_, %reg; \ |
| 59 |
movl name##@GOT(%reg), %reg; |
| 60 |
#endif |
| 61 |
|
| 62 |
#ifdef __CET__ |
| 63 |
#define ENDBRANCH endbr32 |
| 64 |
#else |
| 65 |
#define ENDBRANCH /*_*/ |
| 66 |
#endif |
| 67 |
|
| 68 |
#ifdef HAVE_GCC_ASM_CFI_DIRECTIVES |
| 69 |
/* CFI directives to emit DWARF stack unwinding information. */ |
| 70 |
# define CFI_STARTPROC() .cfi_startproc; ENDBRANCH |
| 71 |
# define CFI_ENDPROC() .cfi_endproc |
| 72 |
# define CFI_REMEMBER_STATE() .cfi_remember_state |
| 73 |
# define CFI_RESTORE_STATE() .cfi_restore_state |
| 74 |
# define CFI_ADJUST_CFA_OFFSET(off) .cfi_adjust_cfa_offset off |
| 75 |
# define CFI_REL_OFFSET(reg,off) .cfi_rel_offset reg, off |
| 76 |
# define CFI_DEF_CFA_REGISTER(reg) .cfi_def_cfa_register reg |
| 77 |
# define CFI_REGISTER(ro,rn) .cfi_register ro, rn |
| 78 |
# define CFI_RESTORE(reg) .cfi_restore reg |
| 79 |
|
| 80 |
# define CFI_PUSH(reg) \ |
| 81 |
CFI_ADJUST_CFA_OFFSET(4); CFI_REL_OFFSET(reg, 0) |
| 82 |
# define CFI_POP(reg) \ |
| 83 |
CFI_ADJUST_CFA_OFFSET(-4); CFI_RESTORE(reg) |
| 84 |
# define CFI_POP_TMP_REG() \ |
| 85 |
CFI_ADJUST_CFA_OFFSET(-4); |
| 86 |
# define CFI_LEAVE() \ |
| 87 |
CFI_ADJUST_CFA_OFFSET(-4); CFI_DEF_CFA_REGISTER(%esp) |
| 88 |
|
| 89 |
/* CFA expressions are used for pointing CFA and registers to |
| 90 |
* %rsp relative offsets. */ |
| 91 |
# define DW_REGNO_eax 0 |
| 92 |
# define DW_REGNO_edx 1 |
| 93 |
# define DW_REGNO_ecx 2 |
| 94 |
# define DW_REGNO_ebx 3 |
| 95 |
# define DW_REGNO_esi 4 |
| 96 |
# define DW_REGNO_edi 5 |
| 97 |
# define DW_REGNO_ebp 6 |
| 98 |
# define DW_REGNO_esp 7 |
| 99 |
|
| 100 |
# define DW_REGNO(reg) DW_REGNO_ ## reg |
| 101 |
|
| 102 |
/* Fixed length encoding used for integers for now. */ |
| 103 |
# define DW_SLEB128_7BIT(value) \ |
| 104 |
0x00|((value) & 0x7f) |
| 105 |
# define DW_SLEB128_28BIT(value) \ |
| 106 |
0x80|((value)&0x7f), \ |
| 107 |
0x80|(((value)>>7)&0x7f), \ |
| 108 |
0x80|(((value)>>14)&0x7f), \ |
| 109 |
0x00|(((value)>>21)&0x7f) |
| 110 |
|
| 111 |
# define CFI_CFA_ON_STACK(esp_offs,cfa_depth) \ |
| 112 |
.cfi_escape \ |
| 113 |
0x0f, /* DW_CFA_def_cfa_expression */ \ |
| 114 |
DW_SLEB128_7BIT(11), /* length */ \ |
| 115 |
0x77, /* DW_OP_breg7, rsp + constant */ \ |
| 116 |
DW_SLEB128_28BIT(esp_offs), \ |
| 117 |
0x06, /* DW_OP_deref */ \ |
| 118 |
0x23, /* DW_OP_plus_constu */ \ |
| 119 |
DW_SLEB128_28BIT((cfa_depth)+4) |
| 120 |
|
| 121 |
# define CFI_REG_ON_STACK(reg,esp_offs) \ |
| 122 |
.cfi_escape \ |
| 123 |
0x10, /* DW_CFA_expression */ \ |
| 124 |
DW_SLEB128_7BIT(DW_REGNO(reg)), \ |
| 125 |
DW_SLEB128_7BIT(5), /* length */ \ |
| 126 |
0x77, /* DW_OP_breg7, rsp + constant */ \ |
| 127 |
DW_SLEB128_28BIT(esp_offs) |
| 128 |
|
| 129 |
#else |
| 130 |
# define CFI_STARTPROC() ENDBRANCH |
| 131 |
# define CFI_ENDPROC() |
| 132 |
# define CFI_REMEMBER_STATE() |
| 133 |
# define CFI_RESTORE_STATE() |
| 134 |
# define CFI_ADJUST_CFA_OFFSET(off) |
| 135 |
# define CFI_REL_OFFSET(reg,off) |
| 136 |
# define CFI_DEF_CFA_REGISTER(reg) |
| 137 |
# define CFI_REGISTER(ro,rn) |
| 138 |
# define CFI_RESTORE(reg) |
| 139 |
|
| 140 |
# define CFI_PUSH(reg) |
| 141 |
# define CFI_POP(reg) |
| 142 |
# define CFI_POP_TMP_REG() |
| 143 |
# define CFI_LEAVE() |
| 144 |
|
| 145 |
# define CFI_CFA_ON_STACK(rsp_offs,cfa_depth) |
| 146 |
# define CFI_REG_ON_STACK(reg,rsp_offs) |
| 147 |
#endif |
| 148 |
|
| 149 |
/* 'ret' instruction replacement for straight-line speculation mitigation. */ |
| 150 |
#define ret_spec_stop \ |
| 151 |
ret; int3; |
| 152 |
|
| 153 |
/* This prevents speculative execution on old AVX512 CPUs, to prevent |
| 154 |
* speculative execution to AVX512 code. The vpopcntb instruction is |
| 155 |
* available on newer CPUs that do not suffer from significant frequency |
| 156 |
* drop when 512-bit vectors are utilized. */ |
| 157 |
#define spec_stop_avx512 \ |
| 158 |
vpxord %ymm7, %ymm7, %ymm7; \ |
| 159 |
vpopcntb %xmm7, %xmm7; /* Supported only by newer AVX512 CPUs. */ \ |
| 160 |
vpxord %ymm7, %ymm7, %ymm7; |
| 161 |
|
| 162 |
#define spec_stop_avx512_intel_syntax \ |
| 163 |
vpxord ymm7, ymm7, ymm7; \ |
| 164 |
vpopcntb xmm7, xmm7; /* Supported only by newer AVX512 CPUs. */ \ |
| 165 |
vpxord ymm7, ymm7, ymm7; |
| 166 |
|
| 167 |
#ifdef __CET__ |
| 168 |
/* Generate CET property for all assembly files including this header. */ |
| 169 |
ELF(.section .note.gnu.property,"a") |
| 170 |
ELF(.align 4) |
| 171 |
ELF(.long 1f - 0f) |
| 172 |
ELF(.long 4f - 1f) |
| 173 |
ELF(.long 5) |
| 174 |
ELF(0:) |
| 175 |
ELF(.byte 0x47, 0x4e, 0x55, 0) /* string "GNU" */ |
| 176 |
ELF(1:) |
| 177 |
ELF(.align 4) |
| 178 |
ELF(.long 0xc0000002) |
| 179 |
ELF(.long 3f - 2f) |
| 180 |
ELF(2:) |
| 181 |
ELF(.long 0x3) |
| 182 |
ELF(3:) |
| 183 |
ELF(.align 4) |
| 184 |
ELF(4:) |
| 185 |
#endif |
| 186 |
|
| 187 |
#endif /* GCRY_ASM_COMMON_AMD64_H */ |