Initial import

This commit is contained in:
Justine Tunney 2020-06-15 07:18:57 -07:00
commit c91b3c5006
14915 changed files with 590219 additions and 0 deletions

16
third_party/xed/private.h vendored Normal file
View file

@ -0,0 +1,16 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_XED_PRIVATE_H_
#define COSMOPOLITAN_THIRD_PARTY_XED_PRIVATE_H_
#include "third_party/xed/x86.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
typedef int xed_int_t;
typedef unsigned int xed_uint_t;
typedef unsigned int xed_uint_t;
typedef unsigned char xed_bits_t;
typedef intptr_t xed_addr_t;
typedef bool32 xed_bool_t;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_XED_PRIVATE_H_ */

586
third_party/xed/x86.h vendored Normal file
View file

@ -0,0 +1,586 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_XED_X86_H_
#define COSMOPOLITAN_THIRD_PARTY_XED_X86_H_
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/* ▓▓▓▓▓▓▓▓▓▓▓▓▓ ▄▄▄▄
cosmopolitan § virtual machine » byte code language
*/
#define XED_MAX_INSTRUCTION_BYTES 15
#define XED_MODE_REAL 0
#define XED_MODE_LEGACY 1
#define XED_MODE_LONG 2
#define XED_HINT_NTAKEN 1
#define XED_HINT_TAKEN 2
#define XED_HINT_ALTER 3
#define xed_modrm_mod(M) (((M)&0xff) >> 6)
#define xed_modrm_reg(M) (((M) >> 3) & 7)
#define xed_modrm_rm(M) ((M)&7)
#define xed_sib_base(M) ((M)&7)
#define xed_sib_index(M) (((M) >> 3) & 7)
#define xed_sib_scale(M) (((M)&0xff) >> 6)
#define xed_get_modrm_reg_field(M) (((M)&0x38) >> 3)
enum XedMachineMode {
XED_MACHINE_MODE_INVALID,
XED_MACHINE_MODE_LONG_64,
XED_MACHINE_MODE_LONG_COMPAT_32,
XED_MACHINE_MODE_LONG_COMPAT_16,
XED_MACHINE_MODE_LEGACY_32,
XED_MACHINE_MODE_LEGACY_16,
XED_MACHINE_MODE_REAL,
XED_MACHINE_MODE_UNREAL,
XED_MACHINE_MODE_LAST
};
enum XedError {
XED_ERROR_NONE,
XED_ERROR_BUFFER_TOO_SHORT,
XED_ERROR_GENERAL_ERROR,
XED_ERROR_INVALID_FOR_CHIP,
XED_ERROR_BAD_REGISTER,
XED_ERROR_BAD_LOCK_PREFIX,
XED_ERROR_BAD_REP_PREFIX,
XED_ERROR_BAD_LEGACY_PREFIX,
XED_ERROR_BAD_REX_PREFIX,
XED_ERROR_BAD_EVEX_UBIT,
XED_ERROR_BAD_MAP,
XED_ERROR_BAD_EVEX_V_PRIME,
XED_ERROR_BAD_EVEX_Z_NO_MASKING,
XED_ERROR_NO_OUTPUT_POINTER,
XED_ERROR_NO_AGEN_CALL_BACK_REGISTERED,
XED_ERROR_BAD_MEMOP_INDEX,
XED_ERROR_CALLBACK_PROBLEM,
XED_ERROR_GATHER_REGS,
XED_ERROR_INSTR_TOO_LONG,
XED_ERROR_INVALID_MODE,
XED_ERROR_BAD_EVEX_LL,
XED_ERROR_UNIMPLEMENTED,
XED_ERROR_LAST
};
enum XedAddressWidth {
XED_ADDRESS_WIDTH_INVALID = 0,
XED_ADDRESS_WIDTH_16b = 2,
XED_ADDRESS_WIDTH_32b = 4,
XED_ADDRESS_WIDTH_64b = 8,
XED_ADDRESS_WIDTH_LAST
};
enum XedChip {
XED_CHIP_INVALID = 1,
XED_CHIP_I86 = 2,
XED_CHIP_I86FP = 3,
XED_CHIP_I186 = 4,
XED_CHIP_I186FP = 5,
XED_CHIP_I286REAL = 6,
XED_CHIP_I286 = 7,
XED_CHIP_I2186FP = 8,
XED_CHIP_I386REAL = 9,
XED_CHIP_I386 = 10,
XED_CHIP_I386FP = 11,
XED_CHIP_I486REAL = 12,
XED_CHIP_I486 = 13,
XED_CHIP_PENTIUMREAL = 14,
XED_CHIP_PENTIUM = 15,
XED_CHIP_QUARK = 16,
XED_CHIP_PENTIUMMMXREAL = 17,
XED_CHIP_PENTIUMMMX = 18,
XED_CHIP_ALLREAL = 19,
XED_CHIP_PENTIUMPRO = 20,
XED_CHIP_PENTIUM2 = 21,
XED_CHIP_PENTIUM3 = 22,
XED_CHIP_PENTIUM4 = 23,
XED_CHIP_P4PRESCOTT = 24,
XED_CHIP_P4PRESCOTT_NOLAHF = 25,
XED_CHIP_P4PRESCOTT_VTX = 26,
XED_CHIP_CORE2 = 27,
XED_CHIP_PENRYN = 28,
XED_CHIP_PENRYN_E = 29,
XED_CHIP_NEHALEM = 30,
XED_CHIP_WESTMERE = 31,
XED_CHIP_BONNELL = 32,
XED_CHIP_SALTWELL = 33,
XED_CHIP_SILVERMONT = 34,
XED_CHIP_AMD = 35,
XED_CHIP_GOLDMONT = 36,
XED_CHIP_GOLDMONT_PLUS = 37,
XED_CHIP_TREMONT = 38,
XED_CHIP_SANDYBRIDGE = 39,
XED_CHIP_IVYBRIDGE = 40,
XED_CHIP_HASWELL = 41,
XED_CHIP_BROADWELL = 42,
XED_CHIP_SKYLAKE = 43,
XED_CHIP_SKYLAKE_SERVER = 44,
XED_CHIP_CASCADE_LAKE = 45,
XED_CHIP_KNL = 46,
XED_CHIP_KNM = 47,
XED_CHIP_CANNONLAKE = 48,
XED_CHIP_ICELAKE = 49,
XED_CHIP_ICELAKE_SERVER = 50,
XED_CHIP_FUTURE = 51,
XED_CHIP_ALL = 52,
XED_CHIP_LAST
};
enum XedIsaSet {
XED_ISA_SET_INVALID,
XED_ISA_SET_3DNOW,
XED_ISA_SET_ADOX_ADCX,
XED_ISA_SET_AES,
XED_ISA_SET_AMD,
XED_ISA_SET_AVX,
XED_ISA_SET_AVX2,
XED_ISA_SET_AVX2GATHER,
XED_ISA_SET_AVX512BW_128,
XED_ISA_SET_AVX512BW_128N,
XED_ISA_SET_AVX512BW_256,
XED_ISA_SET_AVX512BW_512,
XED_ISA_SET_AVX512BW_KOP,
XED_ISA_SET_AVX512CD_128,
XED_ISA_SET_AVX512CD_256,
XED_ISA_SET_AVX512CD_512,
XED_ISA_SET_AVX512DQ_128,
XED_ISA_SET_AVX512DQ_128N,
XED_ISA_SET_AVX512DQ_256,
XED_ISA_SET_AVX512DQ_512,
XED_ISA_SET_AVX512DQ_KOP,
XED_ISA_SET_AVX512DQ_SCALAR,
XED_ISA_SET_AVX512ER_512,
XED_ISA_SET_AVX512ER_SCALAR,
XED_ISA_SET_AVX512F_128,
XED_ISA_SET_AVX512F_128N,
XED_ISA_SET_AVX512F_256,
XED_ISA_SET_AVX512F_512,
XED_ISA_SET_AVX512F_KOP,
XED_ISA_SET_AVX512F_SCALAR,
XED_ISA_SET_AVX512PF_512,
XED_ISA_SET_AVX512_4FMAPS_512,
XED_ISA_SET_AVX512_4FMAPS_SCALAR,
XED_ISA_SET_AVX512_4VNNIW_512,
XED_ISA_SET_AVX512_BITALG_128,
XED_ISA_SET_AVX512_BITALG_256,
XED_ISA_SET_AVX512_BITALG_512,
XED_ISA_SET_AVX512_GFNI_128,
XED_ISA_SET_AVX512_GFNI_256,
XED_ISA_SET_AVX512_GFNI_512,
XED_ISA_SET_AVX512_IFMA_128,
XED_ISA_SET_AVX512_IFMA_256,
XED_ISA_SET_AVX512_IFMA_512,
XED_ISA_SET_AVX512_VAES_128,
XED_ISA_SET_AVX512_VAES_256,
XED_ISA_SET_AVX512_VAES_512,
XED_ISA_SET_AVX512_VBMI2_128,
XED_ISA_SET_AVX512_VBMI2_256,
XED_ISA_SET_AVX512_VBMI2_512,
XED_ISA_SET_AVX512_VBMI_128,
XED_ISA_SET_AVX512_VBMI_256,
XED_ISA_SET_AVX512_VBMI_512,
XED_ISA_SET_AVX512_VNNI_128,
XED_ISA_SET_AVX512_VNNI_256,
XED_ISA_SET_AVX512_VNNI_512,
XED_ISA_SET_AVX512_VPCLMULQDQ_128,
XED_ISA_SET_AVX512_VPCLMULQDQ_256,
XED_ISA_SET_AVX512_VPCLMULQDQ_512,
XED_ISA_SET_AVX512_VPOPCNTDQ_128,
XED_ISA_SET_AVX512_VPOPCNTDQ_256,
XED_ISA_SET_AVX512_VPOPCNTDQ_512,
XED_ISA_SET_AVXAES,
XED_ISA_SET_AVX_GFNI,
XED_ISA_SET_BMI1,
XED_ISA_SET_BMI2,
XED_ISA_SET_CET,
XED_ISA_SET_CLDEMOTE,
XED_ISA_SET_CLFLUSHOPT,
XED_ISA_SET_CLFSH,
XED_ISA_SET_CLWB,
XED_ISA_SET_CLZERO,
XED_ISA_SET_CMOV,
XED_ISA_SET_CMPXCHG16B,
XED_ISA_SET_F16C,
XED_ISA_SET_FAT_NOP,
XED_ISA_SET_FCMOV,
XED_ISA_SET_FMA,
XED_ISA_SET_FMA4,
XED_ISA_SET_FXSAVE,
XED_ISA_SET_FXSAVE64,
XED_ISA_SET_GFNI,
XED_ISA_SET_I186,
XED_ISA_SET_I286PROTECTED,
XED_ISA_SET_I286REAL,
XED_ISA_SET_I386,
XED_ISA_SET_I486,
XED_ISA_SET_I486REAL,
XED_ISA_SET_I86,
XED_ISA_SET_INVPCID,
XED_ISA_SET_LAHF,
XED_ISA_SET_LONGMODE,
XED_ISA_SET_LZCNT,
XED_ISA_SET_MONITOR,
XED_ISA_SET_MONITORX,
XED_ISA_SET_MOVBE,
XED_ISA_SET_MOVDIR,
XED_ISA_SET_MPX,
XED_ISA_SET_PAUSE,
XED_ISA_SET_PCLMULQDQ,
XED_ISA_SET_PCONFIG,
XED_ISA_SET_PENTIUMMMX,
XED_ISA_SET_PENTIUMREAL,
XED_ISA_SET_PKU,
XED_ISA_SET_POPCNT,
XED_ISA_SET_PPRO,
XED_ISA_SET_PREFETCHW,
XED_ISA_SET_PREFETCHWT1,
XED_ISA_SET_PREFETCH_NOP,
XED_ISA_SET_PT,
XED_ISA_SET_RDPID,
XED_ISA_SET_RDPMC,
XED_ISA_SET_RDRAND,
XED_ISA_SET_RDSEED,
XED_ISA_SET_RDTSCP,
XED_ISA_SET_RDWRFSGS,
XED_ISA_SET_RTM,
XED_ISA_SET_SGX,
XED_ISA_SET_SGX_ENCLV,
XED_ISA_SET_SHA,
XED_ISA_SET_SMAP,
XED_ISA_SET_SMX,
XED_ISA_SET_SSE,
XED_ISA_SET_SSE2,
XED_ISA_SET_SSE2MMX,
XED_ISA_SET_SSE3,
XED_ISA_SET_SSE3X87,
XED_ISA_SET_SSE4,
XED_ISA_SET_SSE42,
XED_ISA_SET_SSE4A,
XED_ISA_SET_SSEMXCSR,
XED_ISA_SET_SSE_PREFETCH,
XED_ISA_SET_SSSE3,
XED_ISA_SET_SSSE3MMX,
XED_ISA_SET_SVM,
XED_ISA_SET_TBM,
XED_ISA_SET_VAES,
XED_ISA_SET_VMFUNC,
XED_ISA_SET_VPCLMULQDQ,
XED_ISA_SET_VTX,
XED_ISA_SET_WAITPKG,
XED_ISA_SET_WBNOINVD,
XED_ISA_SET_X87,
XED_ISA_SET_XOP,
XED_ISA_SET_XSAVE,
XED_ISA_SET_XSAVEC,
XED_ISA_SET_XSAVEOPT,
XED_ISA_SET_XSAVES,
XED_ISA_SET_LAST
};
enum XedIldMap {
XED_ILD_MAP0,
XED_ILD_MAP1,
XED_ILD_MAP2,
XED_ILD_MAP3,
XED_ILD_MAP4,
XED_ILD_MAP5,
XED_ILD_MAP6,
XED_ILD_MAPAMD,
XED_ILD_MAP_XOP8,
XED_ILD_MAP_XOP9,
XED_ILD_MAP_XOPA,
XED_ILD_MAP_LAST,
XED_ILD_MAP_INVALID
};
struct XedChipFeatures {
uint64_t f[3];
};
struct XedOperands {
uint8_t imm_width;
uint8_t map; /* enum XedIldMap */
uint8_t error; /* enum XedError */
uint8_t mode;
uint8_t rexw;
uint8_t osz;
uint8_t max_bytes;
uint8_t nominal_opcode;
uint8_t out_of_bytes;
uint8_t disp_width;
int64_t disp;
uint64_t uimm0;
enum XedChip chip;
uint8_t amd3dnow;
uint8_t asz;
uint8_t bcrc;
uint8_t cldemote;
uint8_t has_sib;
uint8_t ild_f2;
uint8_t ild_f3;
uint8_t lock;
uint8_t modep5;
uint8_t modep55c;
uint8_t mode_first_prefix;
uint8_t prefix66;
uint8_t realmode;
uint8_t rex;
uint8_t rexb;
uint8_t rexr;
uint8_t rexrr;
uint8_t rexx;
uint8_t ubit;
uint8_t vexdest3;
uint8_t vexdest4;
uint8_t wbnoinvd;
uint8_t zeroing;
uint8_t first_f2f3;
uint8_t has_modrm;
uint8_t last_f2f3;
uint8_t llrc;
uint8_t mod;
uint8_t rep;
uint8_t sibscale;
uint8_t vex_prefix;
uint8_t vl;
uint8_t hint;
uint8_t mask;
uint8_t reg;
uint8_t rm;
uint8_t seg_ovd;
uint8_t sibbase;
uint8_t sibindex;
uint8_t srm;
uint8_t vexdest210;
uint8_t vexvalid;
uint8_t esrc;
uint8_t ild_seg;
uint8_t imm1_bytes;
uint8_t modrm_byte;
uint8_t nprefixes;
uint8_t nrexes;
uint8_t nseg_prefixes;
uint8_t pos_disp;
uint8_t pos_imm;
uint8_t pos_imm1;
uint8_t pos_modrm;
uint8_t pos_nominal_opcode;
uint8_t pos_sib;
uint8_t uimm1;
};
struct XedInst {
uint8_t noperands;
uint8_t cpl;
uint8_t flag_complex;
uint8_t exceptions;
uint16_t flag_info_index;
uint16_t iform_enum;
uint16_t operand_base;
uint16_t attributes;
};
struct XedEncoderIforms {
unsigned x_MEMDISPv;
unsigned x_SIBBASE_ENCODE_SIB1;
unsigned x_VEX_MAP_ENC;
unsigned x_SIB_NT;
unsigned x_UIMM8_1;
unsigned x_SIBBASE_ENCODE;
unsigned x_VEX_ESCVL_ENC;
unsigned x_PREFIX_ENC;
unsigned x_VEXED_REX;
unsigned x_REMOVE_SEGMENT;
unsigned x_VSIB_ENC;
unsigned x_EVEX_REXB_ENC;
unsigned x_MODRM_RM_ENCODE_EA64_SIB0;
unsigned x_VEX_REXXB_ENC;
unsigned x_EVEX_REXRR_ENC;
unsigned x_AVX512_EVEX_BYTE3_ENC;
unsigned x_EVEX_REXW_VVVV_ENC;
unsigned x_VEX_REG_ENC;
unsigned x_SIMM8;
unsigned x_XOP_MAP_ENC;
unsigned x_MODRM_RM_ENCODE_EA32_SIB0;
unsigned x_UIMM8;
unsigned x_MODRM_RM_ENCODE_EA16_SIB0;
unsigned x_XOP_REXXB_ENC;
unsigned x_EVEX_MAP_ENC;
unsigned x_MEMDISP8;
unsigned x_MODRM_RM_ENCODE;
unsigned x_REX_PREFIX_ENC;
unsigned x_UIMM16;
unsigned x_VEX_TYPE_ENC;
unsigned x_EVEX_UPP_ENC;
unsigned x_VEX_REXR_ENC;
unsigned x_BRDISP32;
unsigned x_MEMDISP32;
unsigned x_MEMDISP16;
unsigned x_SIBINDEX_ENCODE;
unsigned x_SE_IMM8;
unsigned x_UIMM32;
unsigned x_SIMMz;
unsigned x_UIMMv;
unsigned x_EVEX_62_REXR_ENC;
unsigned x_DISP_NT;
unsigned x_MODRM_MOD_ENCODE;
unsigned x_MEMDISP;
unsigned x_VSIB_ENC_BASE;
unsigned x_BRDISP8;
unsigned x_BRDISPz;
unsigned x_EVEX_REXX_ENC;
unsigned x_XOP_TYPE_ENC;
};
struct XedEncoderVars {
struct XedEncoderIforms iforms;
unsigned short iform_index;
unsigned ilen;
unsigned olen;
unsigned bit_offset;
};
struct XedDecodedInst {
struct XedOperands operands;
unsigned char decoded_length;
uint8_t *bytes;
};
union XedAvxC4Payload1 {
struct {
unsigned map : 5;
unsigned b_inv : 1;
unsigned x_inv : 1;
unsigned r_inv : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvxC4Payload2 {
struct {
unsigned pp : 2;
unsigned l : 1;
unsigned vvv210 : 3;
unsigned v3 : 1;
unsigned w : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvxC5Payload {
struct {
unsigned pp : 2;
unsigned l : 1;
unsigned vvv210 : 3;
unsigned v3 : 1;
unsigned r_inv : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvx512Payload1 {
struct {
unsigned map : 4;
unsigned rr_inv : 1;
unsigned b_inv : 1;
unsigned x_inv : 1;
unsigned r_inv : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvx512Payload2 {
struct {
unsigned pp : 2;
unsigned ubit : 1;
unsigned vexdest210 : 3;
unsigned vexdest3 : 1;
unsigned rexw : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvx512Payload3 {
struct {
unsigned mask : 3;
unsigned vexdest4p : 1;
unsigned bcrc : 1;
unsigned llrc : 2;
unsigned z : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
forceinline unsigned char xed_decoded_inst_get_byte(
const struct XedDecodedInst *p, long byte_index) {
return p->bytes[byte_index];
}
forceinline void xed_operands_set_mode(struct XedOperands *p,
enum XedMachineMode mmode) {
p->realmode = 0;
switch (mmode) {
case XED_MACHINE_MODE_LONG_64:
p->mode = 2;
return;
case XED_MACHINE_MODE_LEGACY_32:
case XED_MACHINE_MODE_LONG_COMPAT_32:
p->mode = 1;
break;
case XED_MACHINE_MODE_REAL:
p->realmode = 1;
p->mode = 0;
break;
case XED_MACHINE_MODE_UNREAL:
p->realmode = 1;
p->mode = 1;
break;
case XED_MACHINE_MODE_LEGACY_16:
case XED_MACHINE_MODE_LONG_COMPAT_16:
p->mode = 0;
break;
default:
abort();
}
}
forceinline struct XedDecodedInst *xed_decoded_inst_zero_set_mode(
struct XedDecodedInst *p, enum XedMachineMode mmode) {
memset(p, 0, sizeof(*p));
xed_operands_set_mode(&p->operands, mmode);
return p;
}
extern const uint64_t xed_chip_features[XED_CHIP_LAST][3] hidden;
enum XedError xed_instruction_length_decode(struct XedDecodedInst *,
const unsigned char *, size_t);
bool xed_isa_set_is_valid_for_chip(enum XedIsaSet, enum XedChip);
bool xed_test_chip_features(struct XedChipFeatures *, enum XedIsaSet);
void xed_get_chip_features(struct XedChipFeatures *, enum XedChip);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_XED_X86_H_ */

93
third_party/xed/x86features.c vendored Normal file
View file

@ -0,0 +1,93 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2018 Intel Corporation
Copyright 2019 Justine Alexandra Roberts Tunney
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 │
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "third_party/xed/x86.h"
asm(".ident\t\"\\n\\n\
Xed (Apache 2.0)\\n\
Copyright 2018 Intel Corporation\\n\
Copyright 2019 Justine Alexandra Roberts Tunney\\n\
Modifications: Trimmed down to 3kb [2019-03-22 jart]\"");
asm(".include \"libc/disclaimer.inc\"");
/**
* Mapping of enum XedChip -> bitset<enum XedIsaSet>.
*
* See related APIs, e.g. xed_isa_set_is_valid_for_chip().
*
* This information can be reproduced by building Xed and running the C
* preprocessor on xed-chip-features-table.c (see xed-chips.txt) which
* turns several thousand lines of non-evolving code into fifty. For
* example, 0x2800000ul was calculated as: 1UL<<(XED_ISA_SET_I86-64) |
* 1UL<<(XED_ISA_SET_LAHF-64).
*/
const uint64_t xed_chip_features[XED_CHIP_LAST][3] /* clang-format off */
_Section(".text") = {
{0, 0, 0, },
{0, 0x02800000, 0, }, /*I86*/
{0, 0x02800000, 0x02000}, /*I86FP*/
{0, 0x02820000, 0, }, /*I186*/
{0, 0x02820000, 0x02000}, /*I186FP*/
{0, 0x028a0000, 0x02000}, /*I286REAL*/
{0, 0x028e0000, 0x02000}, /*I286*/
{0, 0x028e0000, 0x02000}, /*I2186FP*/
{0, 0x028a0000, 0x02000}, /*I386REAL*/
{0, 0x029e0000, 0x02000}, /*I386*/
{0, 0x029e0000, 0x02000}, /*I386FP*/
{0, 0x02ca0000, 0x02000}, /*I486REAL*/
{0, 0x02fe0000, 0x02000}, /*I486*/
{0, 0x2002ca0000, 0x02000}, /*PENTIUMREAL*/
{0, 0x2002fe0000, 0x02000}, /*PENTIUM*/
{0, 0x2002fe0000, 0x02000}, /*QUARK*/
{0, 0x402002ca0000, 0x02000}, /*PEN..MMXREAL*/
{0, 0x403002fe0000, 0x02000}, /*PENTIUMMMX*/
{0, 0x402002ca0000, 0x02000}, /*ALLREAL*/
{0, 0x492002fe0c80, 0x02000}, /*PENTIUMPRO*/
{0, 0x493002fe4c80, 0x02000}, /*PENTIUM2*/
{0, 0x200493002fe4c80, 0x02006}, /*PENTIUM3*/
{0, 0xe00493202fe4c90, 0x02006}, /*PENTIUM4*/
{0, 0x3e00493216fecd90, 0x02006}, /*P4PRESCOTT*/
{0, 0x3e00493214fecd90, 0x02000}, /*P4PR..NOLAHF*/
{0, 0x3e00493216fecd90, 0x02406}, /*P4PRESC..VTX*/
{0, 0x3f00493216fecd90, 0x0241e}, /*CORE2*/
{0, 0x7f00493216fecd90, 0x0241e}, /*PENRYN*/
{0, 0x7f00493216fecd90, 0x0a41e}, /*PENRYN_E*/
{0, 0xff0249b216fecd90, 0x0241e}, /*NEHALEM*/
{8, 0xff0249b616fecd90, 0x0241e}, /*WESTMERE*/
{0, 0x3e00493256fecd90, 0x0241e}, /*BONNELL*/
{0, 0x3e00493256fecd90, 0x0241e}, /*SALTWELL*/
{8, 0xff02cbb656fecd90, 0x0241e}, /*SILVERMONT*/
{18, 0x00000a0020002040, 0x04061}, /*AMD*/
{8, 0xffc7cbb756fecd98, 0x7a41e}, /*GOLDMONT*/
{8, 0xffd7fbb756fecd98, 0x7a41e}, /*GOLDMONTPLUS*/
{8, 0xfff7fbb7d6ffcdbc, 0x7ac1e}, /*TREMONT*/
{0x2000000000000028, 0xff0249b616fecd90, 0x2a41e}, /*SANDYBRIDGE*/
{0x2000000000000028, 0xff06c9b616fecf90, 0x2a41e}, /*IVYBRIDGE*/
{0xa0000000000000e8, 0xff0ec9b65ffedf91, 0x2a51e}, /*HASWELL*/
{0xa0000000000000ec, 0xff8fcbb65ffedf91, 0x2a51e}, /*BROADWELL*/
{0xa0000000000000ec, 0xff9fcbb75ffedf99, 0x7a51e}, /*SKYLAKE*/
{0xa00000003f3fffec, 0xff9fcbf75ffedfb9, 0x7a51e}, /*SKYL..SERVER*/
{0xa07000003f3fffec, 0xff9fcbf75ffedfb9, 0x7a51e}, /*CASCADE_LAKE*/
{0xa00000007ac080ec, 0xff07cdb65efedf91, 0x2a41e}, /*KNL*/
{0xb0000003fac080ec, 0xff07cdb65efedf91, 0x2a51e}, /*KNM*/
{0xa00e07003f3fffec, 0xffdfcbf75ffedf99, 0x7a51e}, /*CANNONLAKE*/
{0xfffffffc3f3fffec, 0xffdfebf75fffdfb9, 0x7a79e}, /*ICELAKE*/
{0xfffffffc3f3fffec, 0xffffebff5fffdfb9, 0x7b79e}, /*ICEL..SERVER*/
{0xfffffffc3f3fffec, 0xffdffbf75fffdfbb, 0x7a79e}, /*FUTURE*/
{0xfffffffffffffffe, 0xffffffffffffffff, 0x7ffff} /*ALL*/
} /* clang-format on */;

1249
third_party/xed/x86ild.greg.c vendored Normal file

File diff suppressed because it is too large Load diff

56
third_party/xed/x86isa.c vendored Normal file
View file

@ -0,0 +1,56 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2018 Intel Corporation
Copyright 2019 Justine Alexandra Roberts Tunney
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 │
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "third_party/xed/x86.h"
asm(".ident\t\"\\n\\n\
Xed (Apache 2.0)\\n\
Copyright 2018 Intel Corporation\\n\
Copyright 2019 Justine Alexandra Roberts Tunney\\n\
Modifications: Trimmed down to 3kb [2019-03-22 jart]\"");
asm(".include \"libc/disclaimer.inc\"");
bool xed_isa_set_is_valid_for_chip(enum XedIsaSet isa_set, enum XedChip chip) {
unsigned n, r;
n = isa_set / 64;
r = isa_set - (64 * n);
return !!(xed_chip_features[chip][n] & (1ul << r));
}
bool xed_test_chip_features(struct XedChipFeatures *p, enum XedIsaSet isa_set) {
unsigned n, r;
n = isa_set / 64;
r = isa_set - (64 * n);
return !!(p->f[n] & (1ul << r));
}
void xed_get_chip_features(struct XedChipFeatures *p, enum XedChip chip) {
if (p) {
if (chip < XED_CHIP_LAST) {
p->f[0] = xed_chip_features[chip][0];
p->f[1] = xed_chip_features[chip][1];
p->f[2] = xed_chip_features[chip][2];
p->f[3] = 0;
} else {
p->f[0] = 0;
p->f[1] = 0;
p->f[2] = 0;
p->f[3] = 0;
}
}
}

336
third_party/xed/x86tab.S vendored Normal file
View file

@ -0,0 +1,336 @@
/*-*- mode:asm; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 sw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify │
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License. │
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of │
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software │
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/macros.h"
/ Phash tables for instruction length decoding.
.initbss 300,_init_x86tab
xed_prefix_table_bit:
.zero 32
.endobj xed_prefix_table_bit,globl,hidden
xed_has_modrm_2d:
.zero 512
.endobj xed_has_modrm_2d,globl,hidden
xed_has_disp_regular:
.zero 96
.endobj xed_has_disp_regular,globl,hidden
xed_has_sib_table:
.zero 96
.endobj xed_has_sib_table,globl,hidden
xed_disp_bits_2d:
.zero 512
.endobj xed_disp_bits_2d,globl,hidden
xed_imm_bits_2d:
.zero 512
.endobj xed_imm_bits_2d,globl,hidden
.previous
.initro 300,_init_x86tab # 536 bytes (30%)
xed_prefix_table_bit.rodata:
.byte 4,0x00 # 0003
.byte 4,0x40 # 0407
.byte 2,0xff # 0809
.byte 2,0x00 # 0a0b
.byte 1,0xf0 # 0c0c
.byte 17,0x00 # 0d1d
.byte 1,0x0d # 1e1e
.byte 1,0x00 # 1f1f
.endobj xed_prefix_table_bit.rodata
xed_has_modrm_2d.rodata:
.byte 4,0x01 # 0003
.byte 4,0x00 # 0407
.byte 4,0x01 # 080b
.byte 3,0x00 # 0c0e
.byte 1,0x03 # 0f0f
.byte 4,0x01 # 1013
.byte 4,0x00 # 1417
.byte 4,0x01 # 181b
.byte 4,0x00 # 1c1f
.byte 4,0x01 # 2023 #
.byte 2,0x00 # 2425 $%
.byte 1,0x03 # 2626 &
.byte 1,0x00 # 2727 '
.byte 4,0x01 # 282b (+
.byte 2,0x00 # 2c2d ,
.byte 1,0x03 # 2e2e .
.byte 1,0x00 # 2f2f /
.byte 4,0x01 # 3033 03
.byte 2,0x00 # 3435 45
.byte 1,0x03 # 3636 6
.byte 1,0x00 # 3737 7
.byte 4,0x01 # 383b 8;
.byte 2,0x00 # 3c3d <=
.byte 1,0x03 # 3e3e >
.byte 35,0x00 # 3f61 ?a
.byte 2,0x01 # 6263 bc
.byte 4,0x03 # 6467 dg
.byte 1,0x00 # 6868 h
.byte 1,0x01 # 6969 i
.byte 1,0x00 # 6a6a j
.byte 1,0x01 # 6b6b k
.byte 20,0x00 # 6c7f l
.byte 16,0x01 # 808f ÇÅ
.byte 48,0x00 # 90bf É
.byte 2,0x01 # c0c1
.byte 2,0x00 # c2c3
.byte 4,0x01 # c4c7
.byte 8,0x00 # c8cf
.byte 4,0x01 # d0d3
.byte 4,0x00 # d4d7
.byte 8,0x01 # d8df
.byte 16,0x00 # e0ef α
.byte 1,0x03 # f0f0
.byte 1,0x00 # f1f1 ±
.byte 2,0x03 # f2f3
.byte 2,0x00 # f4f5
.byte 2,0x01 # f6f7 ÷
.byte 6,0x00 # f8fd °²
.byte 6,0x01 # fe103
.byte 1,0x03 # 104104
.byte 5,0x00 # 105109
.byte 1,0x03 # 10a10a
.byte 1,0x00 # 10b10b
.byte 1,0x03 # 10c10c
.byte 1,0x01 # 10d10d
.byte 1,0x00 # 10e10e
.byte 1,0x03 # 10f10f
.byte 16,0x01 # 11011f
.byte 4,0x02 # 120123
.byte 4,0x03 # 124127
.byte 8,0x01 # 12812f
.byte 6,0x00 # 130135
.byte 1,0x03 # 136136
.byte 1,0x00 # 137137
.byte 8,0x03 # 13813f
.byte 55,0x01 # 140176
.byte 1,0x00 # 177177
.byte 8,0x01 # 17817f
.byte 16,0x00 # 18018f
.byte 16,0x01 # 19019f
.byte 3,0x00 # 1a01a2
.byte 3,0x01 # 1a31a5
.byte 2,0x03 # 1a61a7
.byte 3,0x00 # 1a81aa
.byte 29,0x01 # 1ab1c7
.byte 8,0x00 # 1c81cf
.byte 48,0x01 # 1d01ff
.endobj xed_has_modrm_2d.rodata
xed_has_disp_regular.rodata:
.byte 6,0x00 # 0005
.byte 1,0x02 # 0606
.byte 1,0x00 # 0707
.byte 8,0x01 # 080f
.byte 8,0x02 # 1017
.byte 13,0x00 # 1824 $
.byte 1,0x04 # 2525 %
.byte 2,0x00 # 2627 &'
.byte 8,0x01 # 282f (/
.byte 8,0x04 # 3037 07
.byte 13,0x00 # 3844 8D
.byte 1,0x04 # 4545 E
.byte 2,0x00 # 4647 FG
.byte 8,0x01 # 484f HO
.byte 8,0x04 # 5057 PW
.byte 8,0x00 # 585f X_
.endobj xed_has_disp_regular.rodata
xed_has_sib_table.rodata:
.byte 36,0x00 # 0023 #
.byte 1,0x01 # 2424 $
.byte 7,0x00 # 252b %+
.byte 1,0x01 # 2c2c ,
.byte 7,0x00 # 2d33 3
.byte 1,0x01 # 3434 4
.byte 15,0x00 # 3543 5C
.byte 1,0x01 # 4444 D
.byte 7,0x00 # 454b EK
.byte 1,0x01 # 4c4c L
.byte 7,0x00 # 4d53 MS
.byte 1,0x01 # 5454 T
.byte 11,0x00 # 555f U_
.endobj xed_has_sib_table.rodata
xed_disp_bits_2d.rodata:
.byte 15,0x04 # 000e
.byte 1,0x00 # 0f0f
.byte 22,0x04 # 1025 %
.byte 1,0x00 # 2626 &
.byte 7,0x04 # 272d '
.byte 1,0x00 # 2e2e .
.byte 7,0x04 # 2f35 /5
.byte 1,0x00 # 3636 6
.byte 7,0x04 # 373d 7=
.byte 1,0x00 # 3e3e >
.byte 37,0x04 # 3f63 ?c
.byte 4,0x00 # 6467 dg
.byte 8,0x04 # 686f ho
.byte 16,0x01 # 707f p
.byte 26,0x04 # 8099 ÇÖ
.byte 1,0x02 # 9a9a Ü
.byte 5,0x04 # 9b9f ¢ƒ
.byte 4,0x05 # a0a3 áú
.byte 35,0x04 # a4c6 ñ
.byte 1,0x06 # c7c7
.byte 24,0x04 # c8df
.byte 4,0x01 # e0e3 απ
.byte 4,0x04 # e4e7 Στ
.byte 2,0x03 # e8e9 ΦΘ
.byte 1,0x02 # eaea Ω
.byte 1,0x01 # ebeb δ
.byte 4,0x04 # ecef
.byte 1,0x00 # f0f0
.byte 1,0x04 # f1f1 ±
.byte 2,0x00 # f2f3
.byte 16,0x04 # f4103
.byte 1,0x00 # 104104
.byte 5,0x04 # 105109
.byte 1,0x00 # 10a10a
.byte 1,0x04 # 10b10b
.byte 1,0x00 # 10c10c
.byte 2,0x04 # 10d10e
.byte 1,0x00 # 10f10f
.byte 20,0x04 # 110123
.byte 4,0x00 # 124127
.byte 14,0x04 # 128135
.byte 1,0x00 # 136136
.byte 1,0x04 # 137137
.byte 8,0x00 # 13813f
.byte 64,0x04 # 14017f
.byte 16,0x03 # 18018f
.byte 22,0x04 # 1901a5
.byte 2,0x00 # 1a61a7
.byte 88,0x04 # 1a81ff
.endobj xed_disp_bits_2d.rodata
xed_imm_bits_2d.rodata:
.byte 4,0x01 # 0003
.byte 1,0x05 # 0404
.byte 1,0x07 # 0505
.byte 6,0x01 # 060b
.byte 1,0x09 # 0c0c
.byte 1,0x07 # 0d0d
.byte 1,0x01 # 0e0e
.byte 1,0x00 # 0f0f
.byte 4,0x01 # 1013
.byte 1,0x05 # 1414
.byte 1,0x07 # 1515 §
.byte 6,0x01 # 161b
.byte 1,0x05 # 1c1c
.byte 1,0x07 # 1d1d
.byte 6,0x01 # 1e23 #
.byte 1,0x05 # 2424 $
.byte 1,0x07 # 2525 %
.byte 1,0x00 # 2626 &
.byte 5,0x01 # 272b '+
.byte 1,0x05 # 2c2c ,
.byte 1,0x07 # 2d2d
.byte 1,0x00 # 2e2e .
.byte 5,0x01 # 2f33 /3
.byte 1,0x09 # 3434 4
.byte 1,0x07 # 3535 5
.byte 1,0x00 # 3636 6
.byte 5,0x01 # 373b 7;
.byte 1,0x05 # 3c3c <
.byte 1,0x07 # 3d3d =
.byte 1,0x00 # 3e3e >
.byte 37,0x01 # 3f63 ?c
.byte 4,0x00 # 6467 dg
.byte 1,0x06 # 6868 h
.byte 1,0x07 # 6969 i
.byte 2,0x05 # 6a6b jk
.byte 20,0x01 # 6c7f l
.byte 1,0x05 # 8080 Ç
.byte 1,0x07 # 8181 ü
.byte 2,0x05 # 8283 éâ
.byte 22,0x01 # 8499 äÖ
.byte 1,0x08 # 9a9a Ü
.byte 13,0x01 # 9ba7 ¢º
.byte 1,0x05 # a8a8 ¿
.byte 1,0x07 # a9a9
.byte 6,0x01 # aaaf ¬»
.byte 8,0x09 # b0b7
.byte 8,0x0a # b8bf
.byte 2,0x09 # c0c1
.byte 1,0x08 # c2c2
.byte 3,0x01 # c3c5
.byte 1,0x09 # c6c6
.byte 1,0x02 # c7c7
.byte 1,0x0b # c8c8
.byte 1,0x01 # c9c9
.byte 1,0x08 # caca
.byte 2,0x01 # cbcc
.byte 1,0x09 # cdcd
.byte 6,0x01 # ced3
.byte 2,0x09 # d4d5
.byte 14,0x01 # d6e3 π
.byte 4,0x09 # e4e7 Στ
.byte 2,0x01 # e8e9 ΦΘ
.byte 1,0x08 # eaea Ω
.byte 5,0x01 # ebef δ
.byte 1,0x00 # f0f0
.byte 1,0x01 # f1f1 ±
.byte 2,0x00 # f2f3
.byte 2,0x01 # f4f5
.byte 1,0x03 # f6f6 ÷
.byte 1,0x04 # f7f7
.byte 12,0x01 # f8103
.byte 1,0x00 # 104104
.byte 5,0x01 # 105109
.byte 1,0x00 # 10a10a
.byte 1,0x01 # 10b10b
.byte 1,0x00 # 10c10c
.byte 2,0x01 # 10d10e
.byte 1,0x00 # 10f10f
.byte 20,0x01 # 110123
.byte 4,0x00 # 124127
.byte 14,0x01 # 128135
.byte 1,0x00 # 136136
.byte 1,0x01 # 137137
.byte 8,0x00 # 13813f
.byte 48,0x01 # 14016f
.byte 4,0x09 # 170173
.byte 4,0x01 # 174177
.byte 1,0x0c # 178178
.byte 43,0x01 # 1791a3
.byte 1,0x09 # 1a41a4
.byte 1,0x01 # 1a51a5
.byte 2,0x00 # 1a61a7
.byte 4,0x01 # 1a81ab
.byte 1,0x09 # 1ac1ac
.byte 13,0x01 # 1ad1b9
.byte 1,0x09 # 1ba1ba
.byte 7,0x01 # 1bb1c1
.byte 1,0x09 # 1c21c2
.byte 1,0x01 # 1c31c3
.byte 3,0x09 # 1c41c6
.byte 57,0x01 # 1c71ff
.endobj xed_imm_bits_2d.rodata
.byte 0,0 # terminator
.byte 0,0,0,0,0,0 # padding
.previous
.init.start 300,_init_x86tab
call rldecode
lodsl
lodsw
.init.end 300,_init_x86tab

64
third_party/xed/xed.mk vendored Normal file
View file

@ -0,0 +1,64 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
#
# SYNOPSIS
#
# Cosmopolitan x86 Instruction Decoding
#
# DESCRIPTION
#
# See test/libc/x86ild_test.c for more information.
PKGS += THIRD_PARTY_XED
THIRD_PARTY_XED_ARTIFACTS += THIRD_PARTY_XED_A
THIRD_PARTY_XED = $(THIRD_PARTY_XED_A_DEPS) $(THIRD_PARTY_XED_A)
THIRD_PARTY_XED_A = o/$(MODE)/third_party/xed/xed.a
THIRD_PARTY_XED_A_FILES := $(wildcard third_party/xed/*)
THIRD_PARTY_XED_A_HDRS = $(filter %.h,$(THIRD_PARTY_XED_A_FILES))
THIRD_PARTY_XED_A_SRCS_S = $(filter %.S,$(THIRD_PARTY_XED_A_FILES))
THIRD_PARTY_XED_A_SRCS_C = $(filter %.c,$(THIRD_PARTY_XED_A_FILES))
THIRD_PARTY_XED_A_SRCS = \
$(THIRD_PARTY_XED_A_SRCS_S) \
$(THIRD_PARTY_XED_A_SRCS_C)
THIRD_PARTY_XED_A_OBJS = \
$(THIRD_PARTY_XED_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(THIRD_PARTY_XED_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(THIRD_PARTY_XED_A_SRCS_C:%.c=o/$(MODE)/%.o)
THIRD_PARTY_XED_A_CHECKS = \
$(THIRD_PARTY_XED_A).pkg \
$(THIRD_PARTY_XED_A_HDRS:%=o/$(MODE)/%.ok)
THIRD_PARTY_XED_A_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_STUBS \
LIBC_STR
THIRD_PARTY_XED_A_DEPS := \
$(call uniq,$(foreach x,$(THIRD_PARTY_XED_A_DIRECTDEPS),$($(x))))
o//third_party/xed/x86ild.greg.o: \
OVERRIDE_CFLAGS += \
-Os
$(THIRD_PARTY_XED_A): \
third_party/xed/ \
$(THIRD_PARTY_XED_A).pkg \
$(THIRD_PARTY_XED_A_OBJS)
$(THIRD_PARTY_XED_A).pkg: \
$(THIRD_PARTY_XED_A_OBJS) \
$(foreach x,$(THIRD_PARTY_XED_A_DIRECTDEPS),$($(x)_A).pkg)
HIRD_PARTY_XED_LIBS = $(foreach x,$(THIRD_PARTY_XED_ARTIFACTS),$($(x)))
THIRD_PARTY_XED_SRCS = $(foreach x,$(THIRD_PARTY_XED_ARTIFACTS),$($(x)_SRCS))
THIRD_PARTY_XED_HDRS = $(foreach x,$(THIRD_PARTY_XED_ARTIFACTS),$($(x)_HDRS))
THIRD_PARTY_XED_CHECKS = $(foreach x,$(THIRD_PARTY_XED_ARTIFACTS),$($(x)_CHECKS))
THIRD_PARTY_XED_OBJS = $(foreach x,$(THIRD_PARTY_XED_ARTIFACTS),$($(x)_OBJS))
$(THIRD_PARTY_XED_OBJS): $(BUILD_FILES) third_party/xed/xed.mk
.PHONY: o/$(MODE)/third_party/xed
o/$(MODE)/third_party/xed: $(THIRD_PARTY_XED_CHECKS)