1 /* 2 * Decode table flags, mostly based on Intel SDM. 3 * 4 * Copyright (c) 2022 Red Hat, Inc. 5 * 6 * Author: Paolo Bonzini <pbonzini@redhat.com> 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 typedef enum X86OpType { 23 X86_TYPE_None, 24 25 X86_TYPE_A, /* Implicit */ 26 X86_TYPE_B, /* VEX.vvvv selects a GPR */ 27 X86_TYPE_C, /* REG in the modrm byte selects a control register */ 28 X86_TYPE_D, /* REG in the modrm byte selects a debug register */ 29 X86_TYPE_E, /* ALU modrm operand */ 30 X86_TYPE_F, /* EFLAGS/RFLAGS */ 31 X86_TYPE_G, /* REG in the modrm byte selects a GPR */ 32 X86_TYPE_H, /* For AVX, VEX.vvvv selects an XMM/YMM register */ 33 X86_TYPE_I, /* Immediate */ 34 X86_TYPE_J, /* Relative offset for a jump */ 35 X86_TYPE_L, /* The upper 4 bits of the immediate select a 128-bit register */ 36 X86_TYPE_M, /* modrm byte selects a memory operand */ 37 X86_TYPE_N, /* R/M in the modrm byte selects an MMX register */ 38 X86_TYPE_O, /* Absolute address encoded in the instruction */ 39 X86_TYPE_P, /* reg in the modrm byte selects an MMX register */ 40 X86_TYPE_Q, /* MMX modrm operand */ 41 X86_TYPE_R, /* R/M in the modrm byte selects a register */ 42 X86_TYPE_S, /* reg selects a segment register */ 43 X86_TYPE_U, /* R/M in the modrm byte selects an XMM/YMM register */ 44 X86_TYPE_V, /* reg in the modrm byte selects an XMM/YMM register */ 45 X86_TYPE_W, /* XMM/YMM modrm operand */ 46 X86_TYPE_X, /* string source */ 47 X86_TYPE_Y, /* string destination */ 48 49 /* Custom */ 50 X86_TYPE_WM, /* modrm byte selects an XMM/YMM memory operand */ 51 X86_TYPE_I_unsigned, /* Immediate, zero-extended */ 52 X86_TYPE_2op, /* 2-operand RMW instruction */ 53 X86_TYPE_LoBits, /* encoded in bits 0-2 of the operand + REX.B */ 54 X86_TYPE_0, /* Hard-coded GPRs (RAX..RDI) */ 55 X86_TYPE_1, 56 X86_TYPE_2, 57 X86_TYPE_3, 58 X86_TYPE_4, 59 X86_TYPE_5, 60 X86_TYPE_6, 61 X86_TYPE_7, 62 X86_TYPE_ES, /* Hard-coded segment registers */ 63 X86_TYPE_CS, 64 X86_TYPE_SS, 65 X86_TYPE_DS, 66 X86_TYPE_FS, 67 X86_TYPE_GS, 68 } X86OpType; 69 70 typedef enum X86OpSize { 71 X86_SIZE_None, 72 73 X86_SIZE_a, /* BOUND operand */ 74 X86_SIZE_b, /* byte */ 75 X86_SIZE_d, /* 32-bit */ 76 X86_SIZE_dq, /* SSE/AVX 128-bit */ 77 X86_SIZE_p, /* Far pointer */ 78 X86_SIZE_pd, /* SSE/AVX packed double precision */ 79 X86_SIZE_pi, /* MMX */ 80 X86_SIZE_ps, /* SSE/AVX packed single precision */ 81 X86_SIZE_q, /* 64-bit */ 82 X86_SIZE_qq, /* AVX 256-bit */ 83 X86_SIZE_s, /* Descriptor */ 84 X86_SIZE_sd, /* SSE/AVX scalar double precision */ 85 X86_SIZE_ss, /* SSE/AVX scalar single precision */ 86 X86_SIZE_si, /* 32-bit GPR */ 87 X86_SIZE_v, /* 16/32/64-bit, based on operand size */ 88 X86_SIZE_w, /* 16-bit */ 89 X86_SIZE_x, /* 128/256-bit, based on operand size */ 90 X86_SIZE_y, /* 32/64-bit, based on operand size */ 91 X86_SIZE_z, /* 16-bit for 16-bit operand size, else 32-bit */ 92 93 /* Custom */ 94 X86_SIZE_d64, 95 X86_SIZE_f64, 96 X86_SIZE_xh, /* SSE/AVX packed half register */ 97 } X86OpSize; 98 99 typedef enum X86CPUIDFeature { 100 X86_FEAT_None, 101 X86_FEAT_3DNOW, 102 X86_FEAT_ADX, 103 X86_FEAT_AES, 104 X86_FEAT_AVX, 105 X86_FEAT_AVX2, 106 X86_FEAT_BMI1, 107 X86_FEAT_BMI2, 108 X86_FEAT_CMPCCXADD, 109 X86_FEAT_F16C, 110 X86_FEAT_FMA, 111 X86_FEAT_MOVBE, 112 X86_FEAT_PCLMULQDQ, 113 X86_FEAT_SHA_NI, 114 X86_FEAT_SSE, 115 X86_FEAT_SSE2, 116 X86_FEAT_SSE3, 117 X86_FEAT_SSSE3, 118 X86_FEAT_SSE41, 119 X86_FEAT_SSE42, 120 X86_FEAT_SSE4A, 121 } X86CPUIDFeature; 122 123 /* Execution flags */ 124 125 typedef enum X86OpUnit { 126 X86_OP_SKIP, /* not valid or managed by emission function */ 127 X86_OP_SEG, /* segment selector */ 128 X86_OP_CR, /* control register */ 129 X86_OP_DR, /* debug register */ 130 X86_OP_INT, /* loaded into/stored from s->T0/T1 */ 131 X86_OP_IMM, /* immediate */ 132 X86_OP_SSE, /* address in either s->ptrX or s->A0 depending on has_ea */ 133 X86_OP_MMX, /* address in either s->ptrX or s->A0 depending on has_ea */ 134 } X86OpUnit; 135 136 typedef enum X86InsnCheck { 137 /* Illegal or exclusive to 64-bit mode */ 138 X86_CHECK_i64 = 1, 139 X86_CHECK_o64 = 2, 140 141 /* Fault outside protected mode */ 142 X86_CHECK_prot = 4, 143 144 /* Privileged instruction checks */ 145 X86_CHECK_cpl0 = 8, 146 X86_CHECK_vm86_iopl = 16, 147 X86_CHECK_cpl_iopl = 32, 148 X86_CHECK_iopl = X86_CHECK_cpl_iopl | X86_CHECK_vm86_iopl, 149 150 /* Fault if VEX.L=1 */ 151 X86_CHECK_VEX128 = 64, 152 153 /* Fault if VEX.W=1 */ 154 X86_CHECK_W0 = 128, 155 156 /* Fault if VEX.W=0 */ 157 X86_CHECK_W1 = 256, 158 } X86InsnCheck; 159 160 typedef enum X86InsnSpecial { 161 X86_SPECIAL_None, 162 163 /* Accepts LOCK prefix; LOCKed operations do not load or writeback operand 0 */ 164 X86_SPECIAL_HasLock, 165 166 /* Always locked if it has a memory operand (XCHG) */ 167 X86_SPECIAL_Locked, 168 169 /* Do not apply segment base to effective address */ 170 X86_SPECIAL_NoSeg, 171 /* 172 * Rd/Mb or Rd/Mw in the manual: register operand 0 is treated as 32 bits 173 * (and writeback zero-extends it to 64 bits if applicable). PREFIX_DATA 174 * does not trigger 16-bit writeback and, as a side effect, high-byte 175 * registers are never used. 176 */ 177 X86_SPECIAL_Op0_Rd, 178 179 /* 180 * Ry/Mb in the manual (PINSRB). However, the high bits are never used by 181 * the instruction in either the register or memory cases; the *real* effect 182 * of this modifier is that high-byte registers are never used, even without 183 * a REX prefix. Therefore, PINSRW does not need it despite having Ry/Mw. 184 */ 185 X86_SPECIAL_Op2_Ry, 186 187 /* 188 * Register operand 2 is extended to full width, while a memory operand 189 * is doubled in size if VEX.L=1. 190 */ 191 X86_SPECIAL_AVXExtMov, 192 193 /* 194 * MMX instruction exists with no prefix; if there is no prefix, V/H/W/U operands 195 * become P/P/Q/N, and size "x" becomes "q". 196 */ 197 X86_SPECIAL_MMX, 198 199 /* When loaded into s->T0, register operand 1 is zero/sign extended. */ 200 X86_SPECIAL_SExtT0, 201 X86_SPECIAL_ZExtT0, 202 } X86InsnSpecial; 203 204 /* 205 * Special cases for instructions that operate on XMM/YMM registers. Intel 206 * retconned all of them to have VEX exception classes other than 0 and 13, so 207 * all these only matter for instructions that have a VEX exception class. 208 * Based on tables in the "AVX and SSE Instruction Exception Specification" 209 * section of the manual. 210 */ 211 typedef enum X86VEXSpecial { 212 /* Legacy SSE instructions that allow unaligned operands */ 213 X86_VEX_SSEUnaligned, 214 215 /* 216 * Used for instructions that distinguish the XMM operand type with an 217 * instruction prefix; legacy SSE encodings will allow unaligned operands 218 * for scalar operands only (identified by a REP prefix). In this case, 219 * the decoding table uses "x" for the vector operands instead of specifying 220 * pd/ps/sd/ss individually. 221 */ 222 X86_VEX_REPScalar, 223 224 /* 225 * VEX instructions that only support 256-bit operands with AVX2 (Table 2-17 226 * column 3). Columns 2 and 4 (instructions limited to 256- and 127-bit 227 * operands respectively) are implicit in the presence of dq and qq 228 * operands, and thus handled by decode_op_size. 229 */ 230 X86_VEX_AVX2_256, 231 } X86VEXSpecial; 232 233 234 typedef struct X86OpEntry X86OpEntry; 235 typedef struct X86DecodedInsn X86DecodedInsn; 236 237 /* Decode function for multibyte opcodes. */ 238 typedef void (*X86DecodeFunc)(DisasContext *s, CPUX86State *env, X86OpEntry *entry, uint8_t *b); 239 240 /* Code generation function. */ 241 typedef void (*X86GenFunc)(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode); 242 243 struct X86OpEntry { 244 /* Based on the is_decode flags. */ 245 union { 246 X86GenFunc gen; 247 X86DecodeFunc decode; 248 }; 249 /* op0 is always written, op1 and op2 are always read. */ 250 X86OpType op0:8; 251 X86OpSize s0:8; 252 X86OpType op1:8; 253 X86OpSize s1:8; 254 X86OpType op2:8; 255 X86OpSize s2:8; 256 /* Must be I and b respectively if present. */ 257 X86OpType op3:8; 258 X86OpSize s3:8; 259 260 X86InsnSpecial special:8; 261 X86CPUIDFeature cpuid:8; 262 unsigned vex_class:8; 263 X86VEXSpecial vex_special:8; 264 unsigned valid_prefix:16; 265 unsigned check:16; 266 unsigned intercept:8; 267 bool is_decode:1; 268 }; 269 270 typedef struct X86DecodedOp { 271 int8_t n; 272 MemOp ot; /* For b/c/d/p/s/q/v/w/y/z */ 273 X86OpUnit unit; 274 bool has_ea; 275 int offset; /* For MMX and SSE */ 276 277 union { 278 target_ulong imm; 279 /* 280 * This field is used internally by macros OP0_PTR/OP1_PTR/OP2_PTR, 281 * do not access directly! 282 */ 283 TCGv_ptr v_ptr; 284 }; 285 } X86DecodedOp; 286 287 struct X86DecodedInsn { 288 X86OpEntry e; 289 X86DecodedOp op[3]; 290 /* 291 * Rightmost immediate, for convenience since most instructions have 292 * one (and also for 4-operand instructions). 293 */ 294 target_ulong immediate; 295 AddressParts mem; 296 297 TCGv cc_dst, cc_src, cc_src2; 298 TCGv_i32 cc_op_dynamic; 299 int8_t cc_op; 300 301 uint8_t b; 302 }; 303 304