Add REP-prefix table

This commit is contained in:
Alexis Engelke
2019-11-02 19:01:23 +01:00
parent 7682541a00
commit 194a7d6831
3 changed files with 22 additions and 5 deletions

View File

@@ -38,6 +38,7 @@ typedef enum DecodeMode DecodeMode;
#define ENTRY_TABLE72 4 #define ENTRY_TABLE72 4
#define ENTRY_TABLE_PREFIX 5 #define ENTRY_TABLE_PREFIX 5
#define ENTRY_TABLE_VEX 6 #define ENTRY_TABLE_VEX 6
#define ENTRY_TABLE_PREFIX_REP 7
#define ENTRY_MASK 7 #define ENTRY_MASK 7
#define ENTRY_UNPACK(table,kind,entry) do { \ #define ENTRY_UNPACK(table,kind,entry) do { \
@@ -388,6 +389,13 @@ fd_decode(const uint8_t* buffer, size_t len_sz, int mode_int, uintptr_t address,
prefixes &= ~(PREFIX_OPSZ | PREFIX_REPNZ | PREFIX_REP); prefixes &= ~(PREFIX_OPSZ | PREFIX_REPNZ | PREFIX_REP);
ENTRY_UNPACK(table, kind, table[index]); ENTRY_UNPACK(table, kind, table[index]);
} }
else if (kind == ENTRY_TABLE_PREFIX_REP)
{
// Discard 66h mandatory prefix
uint8_t index = mandatory_prefix != 1 ? mandatory_prefix : 0;
prefixes &= ~(PREFIX_REPNZ | PREFIX_REP);
ENTRY_UNPACK(table, kind, table[index]);
}
// For VEX prefix, we have to distinguish between VEX.W and VEX.L which may // For VEX prefix, we have to distinguish between VEX.W and VEX.L which may
// be part of the opcode. // be part of the opcode.

View File

@@ -422,8 +422,10 @@ F3.0fb8 RM GP GP - - POPCNT
0fba/6 MI GP IMM8 - - BTR_IMM IMM_8 LOCK 0fba/6 MI GP IMM8 - - BTR_IMM IMM_8 LOCK
0fba/7 MI GP IMM8 - - BTC_IMM IMM_8 LOCK 0fba/7 MI GP IMM8 - - BTC_IMM IMM_8 LOCK
0fbb MR GP GP - - BTC LOCK 0fbb MR GP GP - - BTC LOCK
0fbc RM GP GP - - BSF_TZCNT RNP.0fbc RM GP GP - - BSF
0fbd RM GP GP - - BSR_LZCNT RF3.0fbc RM GP GP - - TZCNT
RNP.0fbd RM GP GP - - BSR
RF3.0fbd RM GP GP - - LZCNT
0fbe RM GP GP8 - - MOVSX 0fbe RM GP GP8 - - MOVSX
0fbf RM GP GP16 - - MOVSX 0fbf RM GP GP16 - - MOVSX
0fc0 MR GP GP - - XADD SIZE_8 LOCK 0fc0 MR GP GP - - XADD SIZE_8 LOCK
@@ -433,8 +435,10 @@ NP.0fc3 MR GP GP - - MOVNTI
0fc8+ O GP - - - BSWAP 0fc8+ O GP - - - BSWAP
0fff RM GP GP - - UD0 0fff RM GP GP - - UD0
# #
0f38f0 RM GP GP - - MOVBE_CRC32 RNP.0f38f0 RM GP GP - - MOVBE MUSTMEM
0f38f1 MR GP GP - - MOVBE_CRC32 RF2.0f38f0 RM GP GP8 - - CRC32
RNP.0f38f1 MR GP GP - - MOVBE MUSTMEM
RF2.0f38f1 MR GP GP - - CRC32
# #
# SSE # SSE
NP.0f10 RM XMM XMM - - SSE_MOVUPS NP.0f10 RM XMM XMM - - SSE_MOVUPS

View File

@@ -139,6 +139,7 @@ class EntryKind(Enum):
TABLE72 = 4 TABLE72 = 4
TABLE_PREFIX = 5 TABLE_PREFIX = 5
TABLE_VEX = 6 TABLE_VEX = 6
TABLE_PREFIX_REP = 7
class TrieEntry(namedtuple("TrieEntry", "kind,items,payload")): class TrieEntry(namedtuple("TrieEntry", "kind,items,payload")):
__slots__ = () __slots__ = ()
@@ -148,6 +149,7 @@ class TrieEntry(namedtuple("TrieEntry", "kind,items,payload")):
EntryKind.TABLE72: 72, EntryKind.TABLE72: 72,
EntryKind.TABLE_PREFIX: 8, EntryKind.TABLE_PREFIX: 8,
EntryKind.TABLE_VEX: 4, EntryKind.TABLE_VEX: 4,
EntryKind.TABLE_PREFIX_REP: 4,
} }
@classmethod @classmethod
def table(cls, kind): def table(cls, kind):
@@ -170,7 +172,7 @@ class TrieEntry(namedtuple("TrieEntry", "kind,items,payload")):
return TrieEntry(self.kind, tuple(mapped_items), self.payload) return TrieEntry(self.kind, tuple(mapped_items), self.payload)
import re import re
opcode_regex = re.compile(r"^(?P<prefixes>(?P<vex>VEX\.)?(?P<legacy>NP|66|F2|F3)\.(?P<rexw>W[01]\.)?(?P<vexl>L[01]\.)?)?(?P<opcode>(?:[0-9a-f]{2})+)(?P<modrm>//?[0-7]|//[c-f][0-9a-f])?(?P<extended>\+)?$") opcode_regex = re.compile(r"^(?:(?P<prefixes>(?P<vex>VEX\.)?(?P<legacy>NP|66|F2|F3)\.(?P<rexw>W[01]\.)?(?P<vexl>L[01]\.)?)|R(?P<repprefix>NP|F2|F3).)?(?P<opcode>(?:[0-9a-f]{2})+)(?P<modrm>//?[0-7]|//[c-f][0-9a-f])?(?P<extended>\+)?$")
def parse_opcode(opcode_string): def parse_opcode(opcode_string):
""" """
@@ -213,6 +215,9 @@ def parse_opcode(opcode_string):
entries = list(map(sum, product(rexw, vexl))) entries = list(map(sum, product(rexw, vexl)))
opcode.append((EntryKind.TABLE_VEX, entries)) opcode.append((EntryKind.TABLE_VEX, entries))
elif match.group("repprefix"):
rep = {"NP": 0, "F3": 2, "F2": 3}[match.group("repprefix")]
opcode.append((EntryKind.TABLE_PREFIX_REP, [rep]))
kinds, values = zip(*opcode) kinds, values = zip(*opcode)
return [tuple(zip(kinds, prod)) for prod in product(*values)] return [tuple(zip(kinds, prod)) for prod in product(*values)]