-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathebcvm.h
281 lines (254 loc) · 4.45 KB
/
ebcvm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* SPDX-License-Identifier: MIT */
#ifndef EBCVM_H_
#define EBCVM_H_
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARCH_BYTES 8 /* EBC supports 32-bit or 64-bit */
#define MAJOR_VERSION 0x0001
#define MINOR_VERSION 0x0000
#define AUTO_MEM_SIZE 0
#define MEM_SIZE 0x800000
#define STACK_SIZE 0x4000
#define HEAP_SIZE 0x4000000
#define STACK_MAGIC 0x0ebc0ebc0ebc0ebc
#define RET_MAGIC 0xffffffffffffffff
typedef enum reg {
FLAGS = 0,
IP,
RV2,
RV3,
RV4,
RV5,
RV6,
RV7,
R0,
R1,
R2,
R3,
R4,
R5,
R6,
R7,
} reg;
typedef struct regs {
uint64_t regs[16];
} regs;
typedef struct mem {
uint8_t *body;
size_t size;
} mem;
typedef enum mem_type {
MEM_HEAP = 0,
MEM_STACK,
MEM_TEXT,
MEM_DATA,
MEM_RODATA,
MEM_BSS,
MEM_EFI,
MEM_UNKNOWN,
} mem_type;
typedef struct memmap {
mem_type mem_type;
uint64_t addr;
size_t size;
} memmap;
typedef struct vm {
regs *regs;
mem *mem;
memmap *memmap;
size_t memmap_size;
uint32_t compiler_version;
} vm;
typedef enum opcode {
BREAK = 0x00,
JMP,
JMP8,
CALL,
CMPeq,
CMPlte,
CMPgte,
CMPulte,
CMPugte,
CMPIeq,
CMPIlte,
CMPIgte,
CMPIulte,
CMPIugte,
ADD,
SUB,
MUL,
DIV,
MOD,
AND,
OR,
XOR,
SHL,
SHR,
ASHR,
NEG,
NOT,
MULU,
DIVU,
MODU,
EXTNDB,
EXTNDD,
EXTNDW,
MOVbw,
MOVww,
MOVdw,
MOVqw,
MOVbd,
MOVwd,
MOVdd,
MOVqd,
MOVqq,
MOVI,
MOVIn,
MOVREL,
MOVnw,
MOVnd,
MOVsnw,
MOVsnd,
POP,
POPn,
PUSH,
PUSHn,
RET,
LOADSP,
STORESP,
NOP,
} opcode;
typedef struct inst {
size_t inst_len;
opcode opcode;
bool op2_indirect;
reg operand2;
bool op1_indirect;
reg operand1;
union {
/* BREAK */
struct {
uint8_t break_code;
};
/* CALL, JMP, and JMP8 */
struct {
bool is_jmp_imm;
bool is_jmp64;
bool is_cond;
bool is_cs; /* jump if Flags.C is set/clear */
bool is_native; /* call to EBC/native code */
bool is_rel;
uint64_t jmp_imm;
};
/* arithmetic ops, CMP, EXTND*,
* LOADSP, STORESP, POP, POPn, PUSH, PUSHn */
struct {
bool is_imm;
bool is_64op;
uint16_t imm;
};
/* MOV, MOVn, and MOVsn */
struct {
size_t op_len; /* size of the data move */
bool is_op1_idx;
bool is_op2_idx;
size_t idx_len;
uint64_t op1_idx;
uint64_t op2_idx;
};
/* MOVI, MOVIn, MOVREL, and CMPI */
struct {
bool is_opt_idx;
size_t imm_len;
size_t mov_len;
uint16_t opt_idx;
uint64_t imm_data;
};
};
} inst;
typedef enum except {
DIV0,
DEBUG,
STEP,
OPCODE,
STACK,
ALIGN,
ENCODE,
BADBREAK,
EXIT,
MEMORY,
UNDEF,
} except;
typedef struct dbg {
vm *_vm;
} dbg;
/* Debugger */
extern dbg *_dbg;
/* Debug mode */
extern bool FLAGS_debug;
/* Size of memory */
extern int FLAGS_mem;
/* Size of stack */
extern int FLAGS_stack;
/* Size of heap */
extern int FLAGS_heap;
/* Step exection */
extern bool FLAGS_step;
/* Relocate sections */
extern bool FLAGS_reloc;
/* vm.c */
vm *init_vm(void);
void fini_vm(vm *);
vm *step_inst(vm *);
void exec_vm(vm *);
size_t dump_inst(vm *);
void dump_vm(vm *);
void raise_except(except, const char *, const char *, int);
void raise_excall(uint64_t, vm *);
/* debug.c */
dbg *init_dbg(vm *);
void fini_dbg(dbg *);
void handle_except(dbg *, except, const char *, const char *, int);
/* load.c */
vm *load_exe(const char *, vm *);
/* decode.c */
inst *decode_op(uint8_t *);
/* exec.c */
vm *exec_op(vm *, inst *);
/* mem.c */
mem *init_mem(void);
mem *realloc_mem(mem *, size_t);
void fini_mem(mem *);
uint8_t read_mem8(mem *, size_t);
uint16_t read_mem16(mem *, size_t);
uint32_t read_mem32(mem *, size_t);
uint64_t read_mem64(mem *, size_t);
void write_mem8(mem *, size_t, uint8_t);
void write_mem16(mem *, size_t, uint16_t);
void write_mem32(mem *, size_t, uint32_t);
void write_mem64(mem *, size_t, uint64_t);
#if ARCH_BYTES == 4
uint32_t read_memn(mem *, size_t);
void write_memn(mem *, size_t, uint32_t);
#elif ARCH_BYTES == 8
uint64_t read_memn(mem *, size_t);
void write_memn(mem *, size_t, uint64_t);
#else
#error unsupported architecture
#endif
/* efi.c */
vm *load_efi(uint64_t, vm *);
void handle_excall(uint64_t, vm *);
/* disas.c */
char *disas_inst(inst *);
/* util.c */
#ifdef __GNUC__
__attribute__((noreturn))
#endif
void error(const char *, ...);
uint64_t uintn(uint64_t);
#endif /* EBCVM_H_ */