// The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #include #include #include #include #include #define log printf #define vlog vprintf #define packed __attribute__ ((__packed__)) #define static_assert _Static_assert #define alignof _Alignof typedef unsigned int bool; typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; typedef unsigned long ulong; typedef struct reg_t reg_t; typedef struct ctx_t ctx_t; typedef struct instr_t instr_t; typedef struct acc_t acc_t; typedef struct arch_t arch_t; enum { INV, RAX, RBX, RCX, RDX, RDI, RSI, RBP, RSP, R8, R9, R10, R11, R12, R13, R14, R15, K0, K1, K2, K3, K4, K5, K6, K7, CR0, CR1, CR2, CR3, CR4, CR5, CR6, CR7, RIP, FLG, NREGS }; enum { GPR = 1 << 0, // General CTL = 1 << 1, // Control SEG = 1 << 2, // Segment RES = 1 << 8, // Reserved for insternal use SYS = 1 << 9, // Reserved for supervisor mode }; // FLG register enum { CF = 1 << 0, // Carry flag PF = 1 << 1, // Parity flag AC = 1 << 2, // Auxiliary flag ZF = 1 << 3, // Zero flag OV = 1 << 4, // Overflow flag DF = 1 << 5, // Direction flag SF = 1 << 6, // Sign flag UF = 1 << 16, // User-mode flag IF = 1 << 17, // Interrupts enable flag }; struct reg_t { char *name; char *desc; ulong val; ulong flags; }; // A_REG is implicit // A_MEM denotes a memory access // A_OFF is A_MEM but a 16-bit offset is expected immediatly next enum { A_REG = 0, A_MEM8 = 0x7001, A_MEM16 = 0x7002, A_MEM32 = 0x7004, A_MEM64 = 0x7008, A_OFF8 = 0x7701, A_OFF16 = 0x7702, A_OFF32 = 0x7704, A_OFF64 = 0x7708, A_IMM16 = 0x7772, A_IMM32 = 0x7774, A_IMM64 = 0x7778 }; #define A_IS_MEM(x) ((x) >= A_MEM8 && (x) <= A_MEM64) #define A_IS_OFF(x) ((x) >= A_OFF8 && (x) <= A_OFF64) #define A_IS_IMM(x) ((x) >= A_IMM16) #define A_IS_8(x) ((x) & 0xF == 1) #define A_IS_16(x) ((x) & 0xF == 2) #define A_IS_32(x) ((x) & 0xF == 3) #define A_IS_64(x) ((x) & 0xF == 4) struct acc_t { bool mem; uint len; uint type; ulong val; short off; }; enum { NOPREF, PREF_REP=0x8000, PREF_LOCK, NPREFS }; #define ISPREF(x) ((x) & 0x8000 && (x)