Lines Matching +full:mod +full:- +full:12 +full:b

1 // SPDX-License-Identifier: GPL-2.0-or-later
38 ((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr)
41 …({ t r = get_unaligned((t *)(insn)->next_byte); (insn)->next_byte += sizeof(t); leXX_to_cpu(t, r);…
44 ({ t r = get_unaligned((t *)(insn)->next_byte + n); leXX_to_cpu(t, r); })
55 * insn_init() - initialize struct insn
59 * @x86_64: !0 for 64-bit kernel or 64-bit app
71 insn->kaddr = kaddr; in insn_init()
72 insn->end_kaddr = kaddr + buf_len; in insn_init()
73 insn->next_byte = kaddr; in insn_init()
74 insn->x86_64 = x86_64; in insn_init()
75 insn->opnd_bytes = 4; in insn_init()
77 insn->addr_bytes = 8; in insn_init()
79 insn->addr_bytes = 4; in insn_init()
95 insn->emulate_prefix_size = len; in __insn_get_emulate_prefix()
96 insn->next_byte += len; in __insn_get_emulate_prefix()
113 * insn_get_prefixes - scan x86 instruction prefix bytes
116 * Populates the @insn->prefixes bitmap, and updates @insn->next_byte
117 * to point to the (first) opcode. No effect if @insn->prefixes.got
126 struct insn_field *prefixes = &insn->prefixes; in insn_get_prefixes()
128 insn_byte_t b, lb; in insn_get_prefixes() local
131 if (prefixes->got) in insn_get_prefixes()
138 b = peek_next(insn_byte_t, insn); in insn_get_prefixes()
139 attr = inat_get_opcode_attribute(b); in insn_get_prefixes()
143 if (prefixes->bytes[i] == b) in insn_get_prefixes()
148 prefixes->bytes[nb++] = b; in insn_get_prefixes()
151 if (insn->x86_64) in insn_get_prefixes()
152 insn->addr_bytes ^= 12; in insn_get_prefixes()
154 insn->addr_bytes ^= 6; in insn_get_prefixes()
157 insn->opnd_bytes ^= 6; in insn_get_prefixes()
160 prefixes->nbytes++; in insn_get_prefixes()
161 insn->next_byte++; in insn_get_prefixes()
162 lb = b; in insn_get_prefixes()
163 b = peek_next(insn_byte_t, insn); in insn_get_prefixes()
164 attr = inat_get_opcode_attribute(b); in insn_get_prefixes()
167 if (lb && lb != insn->prefixes.bytes[3]) { in insn_get_prefixes()
168 if (unlikely(insn->prefixes.bytes[3])) { in insn_get_prefixes()
170 b = insn->prefixes.bytes[3]; in insn_get_prefixes()
172 if (prefixes->bytes[i] == lb) in insn_get_prefixes()
173 insn_set_byte(prefixes, i, b); in insn_get_prefixes()
175 insn_set_byte(&insn->prefixes, 3, lb); in insn_get_prefixes()
179 if (insn->x86_64) { in insn_get_prefixes()
180 b = peek_next(insn_byte_t, insn); in insn_get_prefixes()
181 attr = inat_get_opcode_attribute(b); in insn_get_prefixes()
183 insn_field_set(&insn->rex_prefix, b, 1); in insn_get_prefixes()
184 insn->next_byte++; in insn_get_prefixes()
185 if (X86_REX_W(b)) in insn_get_prefixes()
187 insn->opnd_bytes = 8; in insn_get_prefixes()
189 insn_set_byte(&insn->rex_prefix, 0, b); in insn_get_prefixes()
190 b = peek_nbyte_next(insn_byte_t, insn, 1); in insn_get_prefixes()
191 insn_set_byte(&insn->rex_prefix, 1, b); in insn_get_prefixes()
192 insn->rex_prefix.nbytes = 2; in insn_get_prefixes()
193 insn->next_byte += 2; in insn_get_prefixes()
194 if (X86_REX_W(b)) in insn_get_prefixes()
196 insn->opnd_bytes = 8; in insn_get_prefixes()
197 insn->rex_prefix.got = 1; in insn_get_prefixes()
201 insn->rex_prefix.got = 1; in insn_get_prefixes()
204 b = peek_next(insn_byte_t, insn); in insn_get_prefixes()
205 attr = inat_get_opcode_attribute(b); in insn_get_prefixes()
208 if (!insn->x86_64) { in insn_get_prefixes()
210 * In 32-bits mode, if the [7:6] bits (mod bits of in insn_get_prefixes()
211 * ModRM) on the second byte are not 11b, it is in insn_get_prefixes()
217 insn_set_byte(&insn->vex_prefix, 0, b); in insn_get_prefixes()
218 insn_set_byte(&insn->vex_prefix, 1, b2); in insn_get_prefixes()
221 insn_set_byte(&insn->vex_prefix, 2, b2); in insn_get_prefixes()
223 insn_set_byte(&insn->vex_prefix, 3, b2); in insn_get_prefixes()
224 insn->vex_prefix.nbytes = 4; in insn_get_prefixes()
225 insn->next_byte += 4; in insn_get_prefixes()
226 if (insn->x86_64 && X86_VEX_W(b2)) in insn_get_prefixes()
228 insn->opnd_bytes = 8; in insn_get_prefixes()
231 insn_set_byte(&insn->vex_prefix, 2, b2); in insn_get_prefixes()
232 insn->vex_prefix.nbytes = 3; in insn_get_prefixes()
233 insn->next_byte += 3; in insn_get_prefixes()
234 if (insn->x86_64 && X86_VEX_W(b2)) in insn_get_prefixes()
236 insn->opnd_bytes = 8; in insn_get_prefixes()
239 * For VEX2, fake VEX3-like byte#2. in insn_get_prefixes()
243 insn_set_byte(&insn->vex_prefix, 2, b2 & 0x7f); in insn_get_prefixes()
244 insn->vex_prefix.nbytes = 2; in insn_get_prefixes()
245 insn->next_byte += 2; in insn_get_prefixes()
249 insn->vex_prefix.got = 1; in insn_get_prefixes()
251 prefixes->got = 1; in insn_get_prefixes()
256 return -ENODATA; in insn_get_prefixes()
260 * insn_get_opcode - collect opcode(s)
263 * Populates @insn->opcode, updates @insn->next_byte to point past the
264 * opcode byte(s), and set @insn->attr (except for groups).
266 * Sets @insn->opcode.value = opcode1. No effect if @insn->opcode.got
275 struct insn_field *opcode = &insn->opcode; in insn_get_opcode()
279 if (opcode->got) in insn_get_opcode()
289 opcode->nbytes = 1; in insn_get_opcode()
296 insn->attr = inat_get_avx_attribute(op, m, p); in insn_get_opcode()
298 if (inat_evex_scalable(insn->attr) && !insn_vex_w_bit(insn) && in insn_get_opcode()
300 insn->opnd_bytes = 2; in insn_get_opcode()
301 if ((inat_must_evex(insn->attr) && !insn_is_evex(insn)) || in insn_get_opcode()
302 (!inat_accept_vex(insn->attr) && in insn_get_opcode()
303 !inat_is_group(insn->attr))) { in insn_get_opcode()
305 insn->attr = 0; in insn_get_opcode()
306 return -EINVAL; in insn_get_opcode()
319 insn->attr = inat_get_escape_attribute(op, pfx_id, esc_attr); in insn_get_opcode()
321 insn->attr = inat_get_opcode_attribute(op); in insn_get_opcode()
326 insn->attr = inat_get_opcode_attribute(op); in insn_get_opcode()
327 while (inat_is_escape(insn->attr)) { in insn_get_opcode()
330 opcode->bytes[opcode->nbytes++] = op; in insn_get_opcode()
332 insn->attr = inat_get_escape_attribute(op, pfx_id, insn->attr); in insn_get_opcode()
335 if (inat_must_vex(insn->attr)) { in insn_get_opcode()
337 insn->attr = 0; in insn_get_opcode()
338 return -EINVAL; in insn_get_opcode()
341 opcode->got = 1; in insn_get_opcode()
345 return -ENODATA; in insn_get_opcode()
349 * insn_get_modrm - collect ModRM byte, if any
352 * Populates @insn->modrm and updates @insn->next_byte to point past the
354 * (prefixes and opcode(s)). No effect if @insn->modrm.got is already 1.
362 struct insn_field *modrm = &insn->modrm; in insn_get_modrm()
363 insn_byte_t pfx_id, mod; in insn_get_modrm() local
366 if (modrm->got) in insn_get_modrm()
373 if (inat_has_modrm(insn->attr)) { in insn_get_modrm()
374 mod = get_next(insn_byte_t, insn); in insn_get_modrm()
375 insn_field_set(modrm, mod, 1); in insn_get_modrm()
376 if (inat_is_group(insn->attr)) { in insn_get_modrm()
378 insn->attr = inat_get_group_attribute(mod, pfx_id, in insn_get_modrm()
379 insn->attr); in insn_get_modrm()
380 if (insn_is_avx(insn) && !inat_accept_vex(insn->attr)) { in insn_get_modrm()
382 insn->attr = 0; in insn_get_modrm()
383 return -EINVAL; in insn_get_modrm()
388 if (insn->x86_64 && inat_is_force64(insn->attr)) in insn_get_modrm()
389 insn->opnd_bytes = 8; in insn_get_modrm()
391 modrm->got = 1; in insn_get_modrm()
395 return -ENODATA; in insn_get_modrm()
400 * insn_rip_relative() - Does instruction use RIP-relative addressing mode?
404 * ModRM byte. No effect if @insn->x86_64 is 0.
408 struct insn_field *modrm = &insn->modrm; in insn_rip_relative()
411 if (!insn->x86_64) in insn_rip_relative()
418 * For rip-relative instructions, the mod field (top 2 bits) in insn_rip_relative()
421 return (modrm->nbytes && (modrm->bytes[0] & 0xc7) == 0x5); in insn_rip_relative()
425 * insn_get_sib() - Get the SIB byte of instruction
440 if (insn->sib.got) in insn_get_sib()
447 if (insn->modrm.nbytes) { in insn_get_sib()
448 modrm = insn->modrm.bytes[0]; in insn_get_sib()
449 if (insn->addr_bytes != 2 && in insn_get_sib()
451 insn_field_set(&insn->sib, in insn_get_sib()
455 insn->sib.got = 1; in insn_get_sib()
460 return -ENODATA; in insn_get_sib()
465 * insn_get_displacement() - Get the displacement of instruction
470 * Displacement value is sign-expanded.
478 insn_byte_t mod, rm, base; in insn_get_displacement() local
481 if (insn->displacement.got) in insn_get_displacement()
488 if (insn->modrm.nbytes) { in insn_get_displacement()
491 * mod = 00 - no displacement fields (exceptions below) in insn_get_displacement()
492 * mod = 01 - 1-byte displacement field in insn_get_displacement()
493 * mod = 10 - displacement field is 4 bytes, or 2 bytes if in insn_get_displacement()
494 * address size = 2 (0x67 prefix in 32-bit mode) in insn_get_displacement()
495 * mod = 11 - no memory operand in insn_get_displacement()
498 * mod = 00, r/m = 110 - displacement field is 2 bytes in insn_get_displacement()
501 * mod != 11, r/m = 100 - SIB byte exists in insn_get_displacement()
502 * mod = 00, SIB base = 101 - displacement field is 4 bytes in insn_get_displacement()
503 * mod = 00, r/m = 101 - rip-relative addressing, displacement in insn_get_displacement()
506 mod = X86_MODRM_MOD(insn->modrm.value); in insn_get_displacement()
507 rm = X86_MODRM_RM(insn->modrm.value); in insn_get_displacement()
508 base = X86_SIB_BASE(insn->sib.value); in insn_get_displacement()
509 if (mod == 3) in insn_get_displacement()
511 if (mod == 1) { in insn_get_displacement()
512 insn_field_set(&insn->displacement, in insn_get_displacement()
514 } else if (insn->addr_bytes == 2) { in insn_get_displacement()
515 if ((mod == 0 && rm == 6) || mod == 2) { in insn_get_displacement()
516 insn_field_set(&insn->displacement, in insn_get_displacement()
520 if ((mod == 0 && rm == 5) || mod == 2 || in insn_get_displacement()
521 (mod == 0 && base == 5)) { in insn_get_displacement()
522 insn_field_set(&insn->displacement, in insn_get_displacement()
528 insn->displacement.got = 1; in insn_get_displacement()
532 return -ENODATA; in insn_get_displacement()
538 switch (insn->addr_bytes) { in __get_moffset()
540 insn_field_set(&insn->moffset1, get_next(short, insn), 2); in __get_moffset()
543 insn_field_set(&insn->moffset1, get_next(int, insn), 4); in __get_moffset()
546 insn_field_set(&insn->moffset1, get_next(int, insn), 4); in __get_moffset()
547 insn_field_set(&insn->moffset2, get_next(int, insn), 4); in __get_moffset()
552 insn->moffset1.got = insn->moffset2.got = 1; in __get_moffset()
563 switch (insn->opnd_bytes) { in __get_immv32()
565 insn_field_set(&insn->immediate, get_next(short, insn), 2); in __get_immv32()
569 insn_field_set(&insn->immediate, get_next(int, insn), 4); in __get_immv32()
584 switch (insn->opnd_bytes) { in __get_immv()
586 insn_field_set(&insn->immediate1, get_next(short, insn), 2); in __get_immv()
589 insn_field_set(&insn->immediate1, get_next(int, insn), 4); in __get_immv()
590 insn->immediate1.nbytes = 4; in __get_immv()
593 insn_field_set(&insn->immediate1, get_next(int, insn), 4); in __get_immv()
594 insn_field_set(&insn->immediate2, get_next(int, insn), 4); in __get_immv()
599 insn->immediate1.got = insn->immediate2.got = 1; in __get_immv()
609 switch (insn->opnd_bytes) { in __get_immptr()
611 insn_field_set(&insn->immediate1, get_next(short, insn), 2); in __get_immptr()
614 insn_field_set(&insn->immediate1, get_next(int, insn), 4); in __get_immptr()
622 insn_field_set(&insn->immediate2, get_next(unsigned short, insn), 2); in __get_immptr()
623 insn->immediate1.got = insn->immediate2.got = 1; in __get_immptr()
631 * insn_get_immediate() - Get the immediate in an instruction
636 * Basically, most of immediates are sign-expanded. Unsigned-value can be
637 * computed by bit masking with ((1 << (nbytes * 8)) - 1)
647 if (insn->immediate.got) in insn_get_immediate()
654 if (inat_has_moffset(insn->attr)) { in insn_get_immediate()
660 if (!inat_has_immediate(insn->attr)) in insn_get_immediate()
664 switch (inat_immediate_size(insn->attr)) { in insn_get_immediate()
666 insn_field_set(&insn->immediate, get_next(signed char, insn), 1); in insn_get_immediate()
669 insn_field_set(&insn->immediate, get_next(short, insn), 2); in insn_get_immediate()
672 insn_field_set(&insn->immediate, get_next(int, insn), 4); in insn_get_immediate()
675 insn_field_set(&insn->immediate1, get_next(int, insn), 4); in insn_get_immediate()
676 insn_field_set(&insn->immediate2, get_next(int, insn), 4); in insn_get_immediate()
694 if (inat_has_second_immediate(insn->attr)) { in insn_get_immediate()
695 insn_field_set(&insn->immediate2, get_next(signed char, insn), 1); in insn_get_immediate()
698 insn->immediate.got = 1; in insn_get_immediate()
702 return -ENODATA; in insn_get_immediate()
706 * insn_get_length() - Get the length of instruction
713 * - 0 on success
714 * - < 0 on error
720 if (insn->length) in insn_get_length()
727 insn->length = (unsigned char)((unsigned long)insn->next_byte in insn_get_length()
728 - (unsigned long)insn->kaddr); in insn_get_length()
736 return insn->opcode.got && insn->modrm.got && insn->sib.got && in insn_complete()
737 insn->displacement.got && insn->immediate.got; in insn_complete()
741 * insn_decode() - Decode an x86 instruction
755 /* #define INSN_MODE_KERN -1 __ignore_sync_check__ mode is only valid in the kernel */ in insn_decode()
769 return -EINVAL; in insn_decode()