2021-01-03 00:16:07 +00:00
|
|
|
//
|
2021-01-15 23:16:01 +00:00
|
|
|
// Decoder.hpp
|
2021-01-03 00:16:07 +00:00
|
|
|
// Clock Signal
|
|
|
|
//
|
2021-01-17 03:09:19 +00:00
|
|
|
// Created by Thomas Harte on 01/01/21.
|
2021-01-03 00:16:07 +00:00
|
|
|
// Copyright © 2021 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2021-01-15 23:16:01 +00:00
|
|
|
#ifndef InstructionSets_x86_Decoder_hpp
|
|
|
|
#define InstructionSets_x86_Decoder_hpp
|
2021-01-03 00:16:07 +00:00
|
|
|
|
2021-01-15 23:16:01 +00:00
|
|
|
#include "Instruction.hpp"
|
|
|
|
|
2021-01-16 03:33:14 +00:00
|
|
|
#include <cstddef>
|
2021-01-15 23:16:01 +00:00
|
|
|
#include <utility>
|
2021-01-03 00:16:07 +00:00
|
|
|
|
2021-01-16 02:30:30 +00:00
|
|
|
namespace InstructionSet {
|
2021-01-03 00:16:07 +00:00
|
|
|
namespace x86 {
|
|
|
|
|
|
|
|
enum class Model {
|
|
|
|
i8086,
|
2022-02-09 22:51:48 +00:00
|
|
|
i80186,
|
|
|
|
i80286,
|
|
|
|
i80386,
|
2021-01-03 00:16:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Implements Intel x86 instruction decoding.
|
|
|
|
|
|
|
|
This is an experimental implementation; it has not yet undergone significant testing.
|
|
|
|
*/
|
2022-02-21 17:36:03 +00:00
|
|
|
template <Model model> class Decoder {
|
2021-01-03 00:16:07 +00:00
|
|
|
public:
|
2022-02-21 17:36:03 +00:00
|
|
|
using InstructionT = Instruction<model >= Model::i80386>;
|
|
|
|
|
2021-01-03 00:16:07 +00:00
|
|
|
/*!
|
2021-01-09 03:22:07 +00:00
|
|
|
@returns an @c Instruction plus a size; a positive size to indicate successful decoding; a
|
2021-01-03 02:19:45 +00:00
|
|
|
negative size specifies the [negatived] number of further bytes the caller should ideally
|
|
|
|
collect before calling again. The caller is free to call with fewer, but may not get a decoded
|
|
|
|
instruction in response, and the decoder may still not be able to complete decoding
|
|
|
|
even if given that number of bytes.
|
2021-01-03 00:16:07 +00:00
|
|
|
*/
|
2022-02-21 17:36:03 +00:00
|
|
|
std::pair<int, InstructionT> decode(const uint8_t *source, size_t length);
|
2021-01-03 00:16:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum class Phase {
|
2021-01-04 00:33:16 +00:00
|
|
|
/// Captures all prefixes and continues until an instruction byte is encountered.
|
2021-01-03 00:16:07 +00:00
|
|
|
Instruction,
|
2022-02-10 14:35:05 +00:00
|
|
|
/// Having encountered a 0x0f first instruction byte, waits for the next byte fully to determine the instruction.
|
|
|
|
InstructionPageF,
|
2021-01-06 02:25:12 +00:00
|
|
|
/// Receives a ModRegRM byte and either populates the source_ and dest_ fields appropriately
|
2021-01-04 00:33:16 +00:00
|
|
|
/// or completes decoding of the instruction, as per the instruction format.
|
2021-01-06 02:25:12 +00:00
|
|
|
ModRegRM,
|
2022-02-19 23:00:27 +00:00
|
|
|
/// Awaits n 80386+-style scale-index-base byte ('SIB'), indicating the form of indirect addressing.
|
|
|
|
ScaleIndexBase,
|
2021-01-06 02:25:12 +00:00
|
|
|
/// Waits for sufficiently many bytes to pass for the required displacement and operand to be captured.
|
|
|
|
/// Cf. displacement_size_ and operand_size_.
|
2022-01-31 13:14:33 +00:00
|
|
|
DisplacementOrOperand,
|
2021-01-04 00:33:16 +00:00
|
|
|
/// Forms and returns an Instruction, and resets parsing state.
|
2021-01-03 00:16:07 +00:00
|
|
|
ReadyToPost
|
|
|
|
} phase_ = Phase::Instruction;
|
|
|
|
|
2021-01-06 02:25:12 +00:00
|
|
|
/// During the ModRegRM phase, format dictates interpretation of the ModRegRM byte.
|
2021-01-04 00:33:16 +00:00
|
|
|
///
|
|
|
|
/// During the ReadyToPost phase, format determines how transiently-recorded fields
|
|
|
|
/// are packaged into an Instruction.
|
2021-01-06 02:25:12 +00:00
|
|
|
enum class ModRegRMFormat: uint8_t {
|
2021-01-06 02:34:35 +00:00
|
|
|
// Parse the ModRegRM for mode, register and register/memory fields
|
2021-01-06 02:25:12 +00:00
|
|
|
// and populate the source_ and destination_ fields appropriate.
|
2021-01-03 00:16:07 +00:00
|
|
|
MemReg_Reg,
|
|
|
|
Reg_MemReg,
|
2021-01-04 00:33:16 +00:00
|
|
|
|
2021-01-06 02:34:35 +00:00
|
|
|
// Parse for mode and register/memory fields, populating both
|
|
|
|
// source_ and destination_ fields with the result. Use the 'register'
|
|
|
|
// field to pick an operation from the TEST/NOT/NEG/MUL/IMUL/DIV/IDIV group.
|
|
|
|
MemRegTEST_to_IDIV,
|
|
|
|
|
2021-01-07 02:18:24 +00:00
|
|
|
// Parse for mode and register/memory fields, populating both
|
|
|
|
// source_ and destination_ fields with the result. Use the 'register'
|
|
|
|
// field to check for the POP operation.
|
|
|
|
MemRegPOP,
|
|
|
|
|
|
|
|
// Parse for mode and register/memory fields, populating both
|
|
|
|
// the destination_ field with the result and setting source_ to Immediate.
|
|
|
|
// Use the 'register' field to check for the MOV operation.
|
|
|
|
MemRegMOV,
|
|
|
|
|
2021-01-08 02:30:01 +00:00
|
|
|
// Parse for mode and register/memory fields, populating the
|
|
|
|
// destination_ field with the result. Use the 'register' field
|
|
|
|
// to pick an operation from the ROL/ROR/RCL/RCR/SAL/SHR/SAR group.
|
|
|
|
MemRegROL_to_SAR,
|
|
|
|
|
2021-01-09 03:50:59 +00:00
|
|
|
// Parse for mode and register/memory fields, populating the
|
|
|
|
// destination_ field with the result. Use the 'register' field
|
|
|
|
// to pick an operation from the ADD/OR/ADC/SBB/AND/SUB/XOR/CMP group and
|
|
|
|
// waits for an operand equal to the operation size.
|
|
|
|
MemRegADD_to_CMP,
|
|
|
|
|
2021-01-08 02:36:05 +00:00
|
|
|
// Parse for mode and register/memory fields, populating the
|
|
|
|
// source_ field with the result. Fills destination_ with a segment
|
|
|
|
// register based on the reg field.
|
|
|
|
SegReg,
|
|
|
|
|
2021-01-08 02:59:00 +00:00
|
|
|
// Parse for mode and register/memory fields, populating the
|
|
|
|
// source_ and destination_ fields with the result. Uses the
|
|
|
|
// 'register' field to pick INC or DEC.
|
|
|
|
MemRegINC_DEC,
|
|
|
|
|
|
|
|
// Parse for mode and register/memory fields, populating the
|
|
|
|
// source_ and destination_ fields with the result. Uses the
|
|
|
|
// 'register' field to pick from INC/DEC/CALL/JMP/PUSH, altering
|
|
|
|
// the source to ::Immediate and setting an operand size if necessary.
|
|
|
|
MemRegINC_to_PUSH,
|
2021-01-14 01:29:44 +00:00
|
|
|
|
|
|
|
// Parse for mode and register/memory fields, populating the
|
|
|
|
// source_ and destination_ fields with the result. Uses the
|
|
|
|
// 'register' field to pick from ADD/ADC/SBB/SUB/CMP, altering
|
|
|
|
// the source to ::Immediate and setting an appropriate operand size.
|
|
|
|
MemRegADC_to_CMP,
|
2022-02-10 22:13:50 +00:00
|
|
|
|
|
|
|
// Parse for mode and register/memory field, populating both source_
|
|
|
|
// and destination_ fields with the result. Uses the 'register' field
|
|
|
|
// to pick from SLDT/STR/LLDT/LTR/VERR/VERW.
|
|
|
|
MemRegSLDT_to_VERW,
|
|
|
|
|
|
|
|
// Parse for mode and register/memory field, populating both source_
|
|
|
|
// and destination_ fields with the result. Uses the 'register' field
|
|
|
|
// to pick from SGDT/LGDT/SMSW/LMSW.
|
|
|
|
MemRegSGDT_to_LMSW,
|
2021-01-06 02:25:12 +00:00
|
|
|
} modregrm_format_ = ModRegRMFormat::MemReg_Reg;
|
2021-01-03 02:11:19 +00:00
|
|
|
|
2021-01-04 00:33:16 +00:00
|
|
|
// Ephemeral decoding state.
|
2021-01-03 00:16:07 +00:00
|
|
|
Operation operation_ = Operation::Invalid;
|
2022-02-10 14:35:05 +00:00
|
|
|
uint16_t instr_ = 0x0000; // TODO: is this desired, versus loading more context into ModRegRMFormat?
|
2021-01-06 02:25:12 +00:00
|
|
|
int consumed_ = 0, operand_bytes_ = 0;
|
|
|
|
|
|
|
|
// Source and destination locations.
|
2021-01-03 00:16:07 +00:00
|
|
|
Source source_ = Source::None;
|
|
|
|
Source destination_ = Source::None;
|
2021-01-06 02:25:12 +00:00
|
|
|
|
2021-01-09 03:38:56 +00:00
|
|
|
// Immediate fields.
|
|
|
|
int16_t displacement_ = 0;
|
|
|
|
uint16_t operand_ = 0;
|
2021-01-11 03:55:25 +00:00
|
|
|
uint64_t inward_data_ = 0;
|
2021-01-09 03:38:56 +00:00
|
|
|
|
2022-02-19 23:00:27 +00:00
|
|
|
// Indirection style.
|
2022-02-20 22:54:53 +00:00
|
|
|
ScaleIndexBase sib_;
|
2022-02-19 23:00:27 +00:00
|
|
|
|
2021-01-06 02:25:12 +00:00
|
|
|
// Facts about the instruction.
|
|
|
|
int displacement_size_ = 0; // i.e. size of in-stream displacement, if any.
|
|
|
|
int operand_size_ = 0; // i.e. size of in-stream operand, if any.
|
|
|
|
int operation_size_ = 0; // i.e. size of data manipulated by the operation.
|
2021-01-03 22:28:29 +00:00
|
|
|
|
2021-01-04 00:33:16 +00:00
|
|
|
// Prefix capture fields.
|
2021-01-06 02:25:12 +00:00
|
|
|
Repetition repetition_ = Repetition::None;
|
2021-01-03 22:28:29 +00:00
|
|
|
bool lock_ = false;
|
|
|
|
Source segment_override_ = Source::None;
|
2021-01-04 00:33:16 +00:00
|
|
|
|
|
|
|
/// Resets size capture and all fields with default values.
|
2021-01-03 22:28:29 +00:00
|
|
|
void reset_parsing() {
|
|
|
|
consumed_ = operand_bytes_ = 0;
|
2021-01-06 02:25:12 +00:00
|
|
|
displacement_size_ = operand_size_ = 0;
|
2021-01-13 02:49:22 +00:00
|
|
|
displacement_ = operand_ = 0;
|
2021-01-03 22:28:29 +00:00
|
|
|
lock_ = false;
|
|
|
|
segment_override_ = Source::None;
|
2021-01-04 00:33:16 +00:00
|
|
|
repetition_ = Repetition::None;
|
2021-01-13 02:49:22 +00:00
|
|
|
phase_ = Phase::Instruction;
|
2021-01-14 02:51:18 +00:00
|
|
|
source_ = destination_ = Source::None;
|
2022-02-21 16:45:46 +00:00
|
|
|
sib_ = 0;
|
2021-01-03 22:28:29 +00:00
|
|
|
}
|
2021-01-03 00:16:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 23:16:01 +00:00
|
|
|
#endif /* InstructionSets_x86_Decoder_hpp */
|