xref: /freebsd/contrib/processor-trace/libipt/internal/include/pt_ild.h (revision 85f87cf491bec6f90948a85b10f5523ea24db9e3)
174fe6c29SRuslan Bukin /*
2*85f87cf4SRuslan Bukin  * Copyright (c) 2013-2019, Intel Corporation
374fe6c29SRuslan Bukin  *
474fe6c29SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
574fe6c29SRuslan Bukin  * modification, are permitted provided that the following conditions are met:
674fe6c29SRuslan Bukin  *
774fe6c29SRuslan Bukin  *  * Redistributions of source code must retain the above copyright notice,
874fe6c29SRuslan Bukin  *    this list of conditions and the following disclaimer.
974fe6c29SRuslan Bukin  *  * Redistributions in binary form must reproduce the above copyright notice,
1074fe6c29SRuslan Bukin  *    this list of conditions and the following disclaimer in the documentation
1174fe6c29SRuslan Bukin  *    and/or other materials provided with the distribution.
1274fe6c29SRuslan Bukin  *  * Neither the name of Intel Corporation nor the names of its contributors
1374fe6c29SRuslan Bukin  *    may be used to endorse or promote products derived from this software
1474fe6c29SRuslan Bukin  *    without specific prior written permission.
1574fe6c29SRuslan Bukin  *
1674fe6c29SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1774fe6c29SRuslan Bukin  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1874fe6c29SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1974fe6c29SRuslan Bukin  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2074fe6c29SRuslan Bukin  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2174fe6c29SRuslan Bukin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2274fe6c29SRuslan Bukin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2374fe6c29SRuslan Bukin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2474fe6c29SRuslan Bukin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2574fe6c29SRuslan Bukin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2674fe6c29SRuslan Bukin  * POSSIBILITY OF SUCH DAMAGE.
2774fe6c29SRuslan Bukin  */
2874fe6c29SRuslan Bukin 
2974fe6c29SRuslan Bukin #if !defined(PT_ILD_H)
3074fe6c29SRuslan Bukin #define PT_ILD_H
3174fe6c29SRuslan Bukin 
3274fe6c29SRuslan Bukin #include "pt_insn.h"
3374fe6c29SRuslan Bukin 
3474fe6c29SRuslan Bukin #include "intel-pt.h"
3574fe6c29SRuslan Bukin 
3674fe6c29SRuslan Bukin 
3774fe6c29SRuslan Bukin typedef enum {
3874fe6c29SRuslan Bukin 	PTI_MAP_0,	/* 1-byte opcodes.           may have modrm */
3974fe6c29SRuslan Bukin 	PTI_MAP_1,	/* 2-byte opcodes (0x0f).    may have modrm */
4074fe6c29SRuslan Bukin 	PTI_MAP_2,	/* 3-byte opcodes (0x0f38).  has modrm */
4174fe6c29SRuslan Bukin 	PTI_MAP_3,	/* 3-byte opcodes (0x0f3a).  has modrm */
4274fe6c29SRuslan Bukin 	PTI_MAP_AMD3DNOW,	/* 3d-now opcodes (0x0f0f).  has modrm */
4374fe6c29SRuslan Bukin 	PTI_MAP_INVALID
4474fe6c29SRuslan Bukin } pti_map_enum_t;
4574fe6c29SRuslan Bukin 
4674fe6c29SRuslan Bukin struct pt_ild {
4774fe6c29SRuslan Bukin 	/* inputs */
4874fe6c29SRuslan Bukin 	uint8_t const *itext;
4974fe6c29SRuslan Bukin 	uint8_t max_bytes;	/*1..15 bytes  */
5074fe6c29SRuslan Bukin 	enum pt_exec_mode mode;
5174fe6c29SRuslan Bukin 
5274fe6c29SRuslan Bukin 	union {
5374fe6c29SRuslan Bukin 		struct {
5474fe6c29SRuslan Bukin 			uint32_t osz:1;
5574fe6c29SRuslan Bukin 			uint32_t asz:1;
5674fe6c29SRuslan Bukin 			uint32_t lock:1;
5774fe6c29SRuslan Bukin 			uint32_t f3:1;
5874fe6c29SRuslan Bukin 			uint32_t f2:1;
5974fe6c29SRuslan Bukin 			uint32_t last_f2f3:2;	/* 2 or 3 */
6074fe6c29SRuslan Bukin 			/* The vex bit is set for c4/c5 VEX and EVEX. */
6174fe6c29SRuslan Bukin 			uint32_t vex:1;
6274fe6c29SRuslan Bukin 			/* The REX.R and REX.W bits in REX, VEX, or EVEX. */
6374fe6c29SRuslan Bukin 			uint32_t rex_r:1;
6474fe6c29SRuslan Bukin 			uint32_t rex_w:1;
6574fe6c29SRuslan Bukin 		} s;
6674fe6c29SRuslan Bukin 		uint32_t i;
6774fe6c29SRuslan Bukin 	} u;
6874fe6c29SRuslan Bukin 	uint8_t imm1_bytes;	/* # of bytes in 1st immediate */
6974fe6c29SRuslan Bukin 	uint8_t imm2_bytes;	/* # of bytes in 2nd immediate */
7074fe6c29SRuslan Bukin 	uint8_t disp_bytes;	/* # of displacement bytes */
7174fe6c29SRuslan Bukin 	uint8_t modrm_byte;
7274fe6c29SRuslan Bukin 	/* 5b but valid values=  0,1,2,3 could be in bit union */
7374fe6c29SRuslan Bukin 	uint8_t map;
7474fe6c29SRuslan Bukin 	uint8_t rex;	/* 0b0100wrxb */
7574fe6c29SRuslan Bukin 	uint8_t nominal_opcode;
7674fe6c29SRuslan Bukin 	uint8_t disp_pos;
7774fe6c29SRuslan Bukin 	/* imm_pos can be derived from disp_pos + disp_bytes. */
7874fe6c29SRuslan Bukin };
7974fe6c29SRuslan Bukin 
pti_get_map(const struct pt_ild * ild)8074fe6c29SRuslan Bukin static inline pti_map_enum_t pti_get_map(const struct pt_ild *ild)
8174fe6c29SRuslan Bukin {
8274fe6c29SRuslan Bukin 	return (pti_map_enum_t) ild->map;
8374fe6c29SRuslan Bukin }
8474fe6c29SRuslan Bukin 
pti_get_modrm_mod(const struct pt_ild * ild)8574fe6c29SRuslan Bukin static inline uint8_t pti_get_modrm_mod(const struct pt_ild *ild)
8674fe6c29SRuslan Bukin {
8774fe6c29SRuslan Bukin 	return ild->modrm_byte >> 6;
8874fe6c29SRuslan Bukin }
8974fe6c29SRuslan Bukin 
pti_get_modrm_reg(const struct pt_ild * ild)9074fe6c29SRuslan Bukin static inline uint8_t pti_get_modrm_reg(const struct pt_ild *ild)
9174fe6c29SRuslan Bukin {
9274fe6c29SRuslan Bukin 	return (ild->modrm_byte >> 3) & 7;
9374fe6c29SRuslan Bukin }
9474fe6c29SRuslan Bukin 
pti_get_modrm_rm(const struct pt_ild * ild)9574fe6c29SRuslan Bukin static inline uint8_t pti_get_modrm_rm(const struct pt_ild *ild)
9674fe6c29SRuslan Bukin {
9774fe6c29SRuslan Bukin 	return ild->modrm_byte & 7;
9874fe6c29SRuslan Bukin }
9974fe6c29SRuslan Bukin 
10074fe6c29SRuslan Bukin /* all decoding is multithread safe. */
10174fe6c29SRuslan Bukin 
10274fe6c29SRuslan Bukin /* Decode one instruction.
10374fe6c29SRuslan Bukin  *
10474fe6c29SRuslan Bukin  * Input:
10574fe6c29SRuslan Bukin  *
10674fe6c29SRuslan Bukin  *   @insn->ip:      the virtual address of the instruction
10774fe6c29SRuslan Bukin  *   @insn->raw:     the memory at that virtual address
10874fe6c29SRuslan Bukin  *   @insn->size:    the maximal size of the instruction
10974fe6c29SRuslan Bukin  *   @insn->mode:    the execution mode
11074fe6c29SRuslan Bukin  *
11174fe6c29SRuslan Bukin  * Output:
11274fe6c29SRuslan Bukin  *
11374fe6c29SRuslan Bukin  *   @insn->size:    the actual size of the instruction
11474fe6c29SRuslan Bukin  *   @insn->iclass:  a coarse classification
11574fe6c29SRuslan Bukin  *
11674fe6c29SRuslan Bukin  *   @iext->iclass:  a finer grain classification
11774fe6c29SRuslan Bukin  *   @iext->variant: instruction class dependent information
11874fe6c29SRuslan Bukin  *
11974fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
12074fe6c29SRuslan Bukin  */
12174fe6c29SRuslan Bukin extern int pt_ild_decode(struct pt_insn *insn, struct pt_insn_ext *iext);
12274fe6c29SRuslan Bukin 
12374fe6c29SRuslan Bukin #endif /* PT_ILD_H */
124