2 * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 * ncopcode_desc.h - Descriptors to model opcode operands.
10 #ifndef NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_X86_DECODER_NCOPCODE_DESC_H_
11 #define NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_X86_DECODER_NCOPCODE_DESC_H_
15 #include "native_client/src/include/portability.h"
16 #include "native_client/src/shared/gio/gio.h"
17 #include "native_client/src/shared/utils/types.h"
18 #include "native_client/src/trusted/validator/x86/decoder/gen/ncopcode_prefix.h"
19 #include "native_client/src/trusted/validator/x86/decoder/gen/ncopcode_insts.h"
20 #include "native_client/src/trusted/validator/x86/decoder/gen/ncopcode_opcode_flags.h"
21 #include "native_client/src/trusted/validator/x86/decoder/gen/ncopcode_operand_kind.h"
22 #include "native_client/src/trusted/validator/x86/decoder/gen/ncopcode_operand_flag.h"
23 #include "native_client/src/trusted/validator/x86/x86_insts.h"
27 struct NaClDecodeTables;
29 /* Defines integer to represent sets of possible opcode (instruction) flags */
30 typedef uint64_t NaClIFlags;
32 /* Converts an NaClIFlagEnum to the corresponding bit in NaClIFlags. */
33 #define NACL_IFLAG(x) (((NaClIFlags) 1) << (x))
35 /* Models the empty set of opcode flags. */
36 #define NACL_EMPTY_IFLAGS ((NaClIFlags) 0)
38 /* Prints out the set of defined instruction flags. */
39 void NaClIFlagsPrint(struct Gio* out, NaClIFlags flags);
41 /* Defines integer to represent sets of possible operand flags. */
42 typedef uint32_t NaClOpFlags;
44 /* Converts an NaClOpFlag enum to the corresponding bit in NaClOpFlags. */
45 #define NACL_OPFLAG(x) (((NaClOpFlags) 1) << x)
47 /* Models the empty set of operand flags. */
48 #define NACL_EMPTY_OPFLAGS ((NaClOpFlags) 0)
50 /* Prints out the set of defined OPerand flags. */
51 void NaClOpFlagsPrint(struct Gio* out, NaClOpFlags flags);
53 /* Defines integer to represent sets of possible instruction disallow
56 typedef uint16_t NaClDisallowsFlags;
58 /* Converts a NaClDisallowsFlag to the corresponding bit
59 * in NaClDisallowsFlags.
61 #define NACL_DISALLOWS_FLAG(x) (((NaClDisallowsFlags) 1) << (x))
63 /* Models the empty set of instruction disallows flags. */
64 #define NACL_EMPTY_DISALLOWS_FLAGS ((NaClDisallowsFlags) 0)
66 /* Metadata about an instruction operand. */
67 typedef struct NaClOp {
68 /* The kind of the operand (i.e. kind of data modeled by the operand).*/
70 /* Flags defining additional facts about the operand. */
72 /* Printing format string for operand. */
73 const char* format_string;
76 /* Maxmimum number of opcode bytes per instruction. */
77 #define NACL_MAX_OPCODE_BYTES 3
79 /* Maximum number of opcode bytes used to model an instruction. Include
80 * opcodes in the modrm byte, and register values encoded in the opcode.
82 #define NACL_MAX_ALL_OPCODE_BYTES 4
84 /* Metadata about an instruction, defining a pattern. Note: Since the same
85 * sequence of opcode bytes may define more than one pattern (depending on
86 * other bytes in the parsed instruction), the patterns are
87 * modeled using a singly linked list.
89 typedef struct NaClInst {
90 /* Defines the origin of this instruction. */
91 NaClInstType insttype;
92 /* Flags defining additional facts about the instruction. */
94 /* The instruction that this instruction implements. */
96 /* Defines opcode extentions, which encodes values for OpcodeInModRm,
97 * OpcodePlusR, and OpcodeInModRmRm. Note: to fit the possible 9
98 * bits of information in 8 bits, we assume that OpcodeInModRm
99 * and OpcodePlusR do not happen in the same instruction.
102 /* The number of operands modeled for this instruction. */
103 uint8_t num_operands;
104 /* The corresponding models of the operands. */
105 uint16_t operands_offset;
106 /* Pointer to the next pattern to try and match for the
107 * given sequence of opcode bytes.
112 /* Returns the OpcodeInModRm value in the opcode_ext field. */
113 uint8_t NaClGetOpcodeInModRm(uint8_t opcode_ext);
115 /* Returns the OpcodeInModRmRm value in the opcode_ext field. */
116 uint8_t NaClGetOpcodeInModRmRm(uint8_t opcode_ext);
118 /* Returns the OpcodePlusR value in the opcode_ext field. */
119 uint8_t NaClGetOpcodePlusR(uint8_t opcode_ext);
121 /* Implements trie nodes for selecting instructions that must match
122 * a specific sequence of bytes. Used to handle NOP cases.
124 typedef struct NaClInstNode {
125 /* The matching byte for the trie node. */
126 const uint8_t matching_byte;
127 /* The matching modeled instruction, if byte matched. */
128 const uint16_t matching_inst;
129 /* Node to match remaining bytes if matching_byte matches. */
130 const struct NaClInstNode* success;
131 /* Node to try next if match_byte doesn't match. Note:
132 * The trie is generated in such a way that if the next input
133 * byte is > matching_byte, no node in the fail subtree will
134 * match the current input. That is, nodes in the trie are
135 * sorted by the sequence of matching bytes.
137 const struct NaClInstNode* fail;
140 /* Returns the number of logical operands an instruction has. That is,
141 * returns field num_operands unless the first operand is
142 * a special encoding that extends the opcode.
144 uint8_t NaClGetInstNumberOperands(const NaClInst* inst);
146 /* Returns the indexed logical operand for the instruction. That is,
147 * returns the index-th operand unless the first operand is
148 * a special encoding that extends the opcode. In the latter
149 * case, the (index+1)-th operand is returned.
151 const NaClOp* NaClGetInstOperand(const struct NaClDecodeTables* tables,
152 const NaClInst* inst,
155 /* Print out the given operand structure to the given file. */
156 void NaClOpPrint(struct Gio* f, const NaClOp* operand);
158 /* Returns a string defining bytes of the given prefix that are considered
159 * prefix bytes, independent of the opcode.
161 const char* OpcodePrefixBytes(NaClInstPrefix prefix);
163 /* Print out the given instruction to the given file. However, always
164 * print the value NULL for next_rule, even if the value is non-null. This
165 * function should be used to print out an individual opcode (instruction)
168 void NaClInstPrint(struct Gio* f,
169 const struct NaClDecodeTables* tables,
170 const NaClInst* inst);
174 #endif /* NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_X86_DECODER_NCOPCODE_DESC_H_ */