18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci#ifndef _ASM_X86_INAT_H
38c2ecf20Sopenharmony_ci#define _ASM_X86_INAT_H
48c2ecf20Sopenharmony_ci/*
58c2ecf20Sopenharmony_ci * x86 instruction attributes
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Written by Masami Hiramatsu <mhiramat@redhat.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci#include "inat_types.h" /* __ignore_sync_check__ */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * Internal bits. Don't use bitmasks directly, because these bits are
138c2ecf20Sopenharmony_ci * unstable. You should use checking functions.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define INAT_OPCODE_TABLE_SIZE 256
178c2ecf20Sopenharmony_ci#define INAT_GROUP_TABLE_SIZE 8
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* Legacy last prefixes */
208c2ecf20Sopenharmony_ci#define INAT_PFX_OPNDSZ	1	/* 0x66 */ /* LPFX1 */
218c2ecf20Sopenharmony_ci#define INAT_PFX_REPE	2	/* 0xF3 */ /* LPFX2 */
228c2ecf20Sopenharmony_ci#define INAT_PFX_REPNE	3	/* 0xF2 */ /* LPFX3 */
238c2ecf20Sopenharmony_ci/* Other Legacy prefixes */
248c2ecf20Sopenharmony_ci#define INAT_PFX_LOCK	4	/* 0xF0 */
258c2ecf20Sopenharmony_ci#define INAT_PFX_CS	5	/* 0x2E */
268c2ecf20Sopenharmony_ci#define INAT_PFX_DS	6	/* 0x3E */
278c2ecf20Sopenharmony_ci#define INAT_PFX_ES	7	/* 0x26 */
288c2ecf20Sopenharmony_ci#define INAT_PFX_FS	8	/* 0x64 */
298c2ecf20Sopenharmony_ci#define INAT_PFX_GS	9	/* 0x65 */
308c2ecf20Sopenharmony_ci#define INAT_PFX_SS	10	/* 0x36 */
318c2ecf20Sopenharmony_ci#define INAT_PFX_ADDRSZ	11	/* 0x67 */
328c2ecf20Sopenharmony_ci/* x86-64 REX prefix */
338c2ecf20Sopenharmony_ci#define INAT_PFX_REX	12	/* 0x4X */
348c2ecf20Sopenharmony_ci/* AVX VEX prefixes */
358c2ecf20Sopenharmony_ci#define INAT_PFX_VEX2	13	/* 2-bytes VEX prefix */
368c2ecf20Sopenharmony_ci#define INAT_PFX_VEX3	14	/* 3-bytes VEX prefix */
378c2ecf20Sopenharmony_ci#define INAT_PFX_EVEX	15	/* EVEX prefix */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define INAT_LSTPFX_MAX	3
408c2ecf20Sopenharmony_ci#define INAT_LGCPFX_MAX	11
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* Immediate size */
438c2ecf20Sopenharmony_ci#define INAT_IMM_BYTE		1
448c2ecf20Sopenharmony_ci#define INAT_IMM_WORD		2
458c2ecf20Sopenharmony_ci#define INAT_IMM_DWORD		3
468c2ecf20Sopenharmony_ci#define INAT_IMM_QWORD		4
478c2ecf20Sopenharmony_ci#define INAT_IMM_PTR		5
488c2ecf20Sopenharmony_ci#define INAT_IMM_VWORD32	6
498c2ecf20Sopenharmony_ci#define INAT_IMM_VWORD		7
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* Legacy prefix */
528c2ecf20Sopenharmony_ci#define INAT_PFX_OFFS	0
538c2ecf20Sopenharmony_ci#define INAT_PFX_BITS	4
548c2ecf20Sopenharmony_ci#define INAT_PFX_MAX    ((1 << INAT_PFX_BITS) - 1)
558c2ecf20Sopenharmony_ci#define INAT_PFX_MASK	(INAT_PFX_MAX << INAT_PFX_OFFS)
568c2ecf20Sopenharmony_ci/* Escape opcodes */
578c2ecf20Sopenharmony_ci#define INAT_ESC_OFFS	(INAT_PFX_OFFS + INAT_PFX_BITS)
588c2ecf20Sopenharmony_ci#define INAT_ESC_BITS	2
598c2ecf20Sopenharmony_ci#define INAT_ESC_MAX	((1 << INAT_ESC_BITS) - 1)
608c2ecf20Sopenharmony_ci#define INAT_ESC_MASK	(INAT_ESC_MAX << INAT_ESC_OFFS)
618c2ecf20Sopenharmony_ci/* Group opcodes (1-16) */
628c2ecf20Sopenharmony_ci#define INAT_GRP_OFFS	(INAT_ESC_OFFS + INAT_ESC_BITS)
638c2ecf20Sopenharmony_ci#define INAT_GRP_BITS	5
648c2ecf20Sopenharmony_ci#define INAT_GRP_MAX	((1 << INAT_GRP_BITS) - 1)
658c2ecf20Sopenharmony_ci#define INAT_GRP_MASK	(INAT_GRP_MAX << INAT_GRP_OFFS)
668c2ecf20Sopenharmony_ci/* Immediates */
678c2ecf20Sopenharmony_ci#define INAT_IMM_OFFS	(INAT_GRP_OFFS + INAT_GRP_BITS)
688c2ecf20Sopenharmony_ci#define INAT_IMM_BITS	3
698c2ecf20Sopenharmony_ci#define INAT_IMM_MASK	(((1 << INAT_IMM_BITS) - 1) << INAT_IMM_OFFS)
708c2ecf20Sopenharmony_ci/* Flags */
718c2ecf20Sopenharmony_ci#define INAT_FLAG_OFFS	(INAT_IMM_OFFS + INAT_IMM_BITS)
728c2ecf20Sopenharmony_ci#define INAT_MODRM	(1 << (INAT_FLAG_OFFS))
738c2ecf20Sopenharmony_ci#define INAT_FORCE64	(1 << (INAT_FLAG_OFFS + 1))
748c2ecf20Sopenharmony_ci#define INAT_SCNDIMM	(1 << (INAT_FLAG_OFFS + 2))
758c2ecf20Sopenharmony_ci#define INAT_MOFFSET	(1 << (INAT_FLAG_OFFS + 3))
768c2ecf20Sopenharmony_ci#define INAT_VARIANT	(1 << (INAT_FLAG_OFFS + 4))
778c2ecf20Sopenharmony_ci#define INAT_VEXOK	(1 << (INAT_FLAG_OFFS + 5))
788c2ecf20Sopenharmony_ci#define INAT_VEXONLY	(1 << (INAT_FLAG_OFFS + 6))
798c2ecf20Sopenharmony_ci#define INAT_EVEXONLY	(1 << (INAT_FLAG_OFFS + 7))
808c2ecf20Sopenharmony_ci/* Attribute making macros for attribute tables */
818c2ecf20Sopenharmony_ci#define INAT_MAKE_PREFIX(pfx)	(pfx << INAT_PFX_OFFS)
828c2ecf20Sopenharmony_ci#define INAT_MAKE_ESCAPE(esc)	(esc << INAT_ESC_OFFS)
838c2ecf20Sopenharmony_ci#define INAT_MAKE_GROUP(grp)	((grp << INAT_GRP_OFFS) | INAT_MODRM)
848c2ecf20Sopenharmony_ci#define INAT_MAKE_IMM(imm)	(imm << INAT_IMM_OFFS)
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* Identifiers for segment registers */
878c2ecf20Sopenharmony_ci#define INAT_SEG_REG_IGNORE	0
888c2ecf20Sopenharmony_ci#define INAT_SEG_REG_DEFAULT	1
898c2ecf20Sopenharmony_ci#define INAT_SEG_REG_CS		2
908c2ecf20Sopenharmony_ci#define INAT_SEG_REG_SS		3
918c2ecf20Sopenharmony_ci#define INAT_SEG_REG_DS		4
928c2ecf20Sopenharmony_ci#define INAT_SEG_REG_ES		5
938c2ecf20Sopenharmony_ci#define INAT_SEG_REG_FS		6
948c2ecf20Sopenharmony_ci#define INAT_SEG_REG_GS		7
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/* Attribute search APIs */
978c2ecf20Sopenharmony_ciextern insn_attr_t inat_get_opcode_attribute(insn_byte_t opcode);
988c2ecf20Sopenharmony_ciextern int inat_get_last_prefix_id(insn_byte_t last_pfx);
998c2ecf20Sopenharmony_ciextern insn_attr_t inat_get_escape_attribute(insn_byte_t opcode,
1008c2ecf20Sopenharmony_ci					     int lpfx_id,
1018c2ecf20Sopenharmony_ci					     insn_attr_t esc_attr);
1028c2ecf20Sopenharmony_ciextern insn_attr_t inat_get_group_attribute(insn_byte_t modrm,
1038c2ecf20Sopenharmony_ci					    int lpfx_id,
1048c2ecf20Sopenharmony_ci					    insn_attr_t esc_attr);
1058c2ecf20Sopenharmony_ciextern insn_attr_t inat_get_avx_attribute(insn_byte_t opcode,
1068c2ecf20Sopenharmony_ci					  insn_byte_t vex_m,
1078c2ecf20Sopenharmony_ci					  insn_byte_t vex_pp);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* Attribute checking functions */
1108c2ecf20Sopenharmony_cistatic inline int inat_is_legacy_prefix(insn_attr_t attr)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	attr &= INAT_PFX_MASK;
1138c2ecf20Sopenharmony_ci	return attr && attr <= INAT_LGCPFX_MAX;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic inline int inat_is_address_size_prefix(insn_attr_t attr)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	return (attr & INAT_PFX_MASK) == INAT_PFX_ADDRSZ;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic inline int inat_is_operand_size_prefix(insn_attr_t attr)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	return (attr & INAT_PFX_MASK) == INAT_PFX_OPNDSZ;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic inline int inat_is_rex_prefix(insn_attr_t attr)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	return (attr & INAT_PFX_MASK) == INAT_PFX_REX;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic inline int inat_last_prefix_id(insn_attr_t attr)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	if ((attr & INAT_PFX_MASK) > INAT_LSTPFX_MAX)
1348c2ecf20Sopenharmony_ci		return 0;
1358c2ecf20Sopenharmony_ci	else
1368c2ecf20Sopenharmony_ci		return attr & INAT_PFX_MASK;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic inline int inat_is_vex_prefix(insn_attr_t attr)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	attr &= INAT_PFX_MASK;
1428c2ecf20Sopenharmony_ci	return attr == INAT_PFX_VEX2 || attr == INAT_PFX_VEX3 ||
1438c2ecf20Sopenharmony_ci	       attr == INAT_PFX_EVEX;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic inline int inat_is_evex_prefix(insn_attr_t attr)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	return (attr & INAT_PFX_MASK) == INAT_PFX_EVEX;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic inline int inat_is_vex3_prefix(insn_attr_t attr)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	return (attr & INAT_PFX_MASK) == INAT_PFX_VEX3;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic inline int inat_is_escape(insn_attr_t attr)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	return attr & INAT_ESC_MASK;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic inline int inat_escape_id(insn_attr_t attr)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	return (attr & INAT_ESC_MASK) >> INAT_ESC_OFFS;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic inline int inat_is_group(insn_attr_t attr)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	return attr & INAT_GRP_MASK;
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic inline int inat_group_id(insn_attr_t attr)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	return (attr & INAT_GRP_MASK) >> INAT_GRP_OFFS;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic inline int inat_group_common_attribute(insn_attr_t attr)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	return attr & ~INAT_GRP_MASK;
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic inline int inat_has_immediate(insn_attr_t attr)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	return attr & INAT_IMM_MASK;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic inline int inat_immediate_size(insn_attr_t attr)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	return (attr & INAT_IMM_MASK) >> INAT_IMM_OFFS;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic inline int inat_has_modrm(insn_attr_t attr)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	return attr & INAT_MODRM;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic inline int inat_is_force64(insn_attr_t attr)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	return attr & INAT_FORCE64;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic inline int inat_has_second_immediate(insn_attr_t attr)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	return attr & INAT_SCNDIMM;
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic inline int inat_has_moffset(insn_attr_t attr)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	return attr & INAT_MOFFSET;
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic inline int inat_has_variant(insn_attr_t attr)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	return attr & INAT_VARIANT;
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic inline int inat_accept_vex(insn_attr_t attr)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	return attr & INAT_VEXOK;
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic inline int inat_must_vex(insn_attr_t attr)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	return attr & (INAT_VEXONLY | INAT_EVEXONLY);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic inline int inat_must_evex(insn_attr_t attr)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	return attr & INAT_EVEXONLY;
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci#endif
231