Newer
Older
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "cpu/exec.h"
#include "cpu/rtl.h"
/* shared by all helper functions */
DecodeInfo decoding;
rtlreg_t t0, t1, t2, t3, at;
void decoding_set_jmp(bool is_jmp) {
decoding.is_jmp = is_jmp;
}
#define make_DopHelper(name) void concat(decode_op_, name) (vaddr_t *eip, Operand *op, bool load_val)
/* Refer to Appendix A in i386 manual for the explanations of these abbreviations */
/* Ib, Iv */
static inline make_DopHelper(I) {
/* eip here is pointing to the immediate */
op->type = OP_TYPE_IMM;
op->imm = instr_fetch(eip, op->width);
rtl_li(&op->val, op->imm);
#ifdef DEBUG
snprintf(op->str, OP_STR_SIZE, "$0x%x", op->imm);
#endif
}
/* I386 manual does not contain this abbreviation, but it is different from
* the one above from the view of implementation. So we use another helper
* function to decode it.
*/
/* sign immediate */
static inline make_DopHelper(SI) {
assert(op->width == 1 || op->width == 4);
op->type = OP_TYPE_IMM;
/* TODO: Use instr_fetch() to read `op->width' bytes of memory
* pointed by `eip'. Interpret the result as a signed immediate,
* and assign it to op->simm.
*
op->simm = ???
*/
TODO();
rtl_li(&op->val, op->simm);
#ifdef DEBUG
snprintf(op->str, OP_STR_SIZE, "$0x%x", op->simm);
#endif
}
/* I386 manual does not contain this abbreviation.
* It is convenient to merge them into a single helper function.
*/
/* AL/eAX */
static inline make_DopHelper(a) {
op->type = OP_TYPE_REG;
op->reg = R_EAX;
if (load_val) {
rtl_lr(&op->val, R_EAX, op->width);
}
#ifdef DEBUG
snprintf(op->str, OP_STR_SIZE, "%%%s", reg_name(R_EAX, op->width));
#endif
}
/* This helper function is use to decode register encoded in the opcode. */
/* XX: AL, AH, BL, BH, CL, CH, DL, DH
* eXX: eAX, eCX, eDX, eBX, eSP, eBP, eSI, eDI
*/
static inline make_DopHelper(r) {
op->type = OP_TYPE_REG;
op->reg = decoding.opcode & 0x7;
if (load_val) {
rtl_lr(&op->val, op->reg, op->width);
}
#ifdef DEBUG
snprintf(op->str, OP_STR_SIZE, "%%%s", reg_name(op->reg, op->width));
#endif
}
/* I386 manual does not contain this abbreviation.
* We decode everything of modR/M byte by one time.
*/
/* Eb, Ew, Ev
* Gb, Gv
* Cd,
* M
* Rd
* Sw
*/
static inline void decode_op_rm(vaddr_t *eip, Operand *rm, bool load_rm_val, Operand *reg, bool load_reg_val) {
read_ModR_M(eip, rm, load_rm_val, reg, load_reg_val);
}
/* Ob, Ov */
static inline make_DopHelper(O) {
op->type = OP_TYPE_MEM;
rtl_li(&op->addr, instr_fetch(eip, 4));
if (load_val) {
rtl_lm(&op->val, &op->addr, op->width);
}
#ifdef DEBUG
snprintf(op->str, OP_STR_SIZE, "0x%x", op->addr);
#endif
}
/* Eb <- Gb
* Ev <- Gv
*/
make_DHelper(G2E) {
decode_op_rm(eip, id_dest, true, id_src, true);
}
make_DHelper(mov_G2E) {
decode_op_rm(eip, id_dest, false, id_src, true);
}
/* Gb <- Eb
* Gv <- Ev
*/
make_DHelper(E2G) {
decode_op_rm(eip, id_src, true, id_dest, true);
}
make_DHelper(mov_E2G) {
decode_op_rm(eip, id_src, true, id_dest, false);
}
make_DHelper(lea_M2G) {
decode_op_rm(eip, id_src, false, id_dest, false);
}
/* AL <- Ib
* eAX <- Iv
*/
make_DHelper(I2a) {
decode_op_a(eip, id_dest, true);
decode_op_I(eip, id_src, true);
}
/* Gv <- EvIb
* Gv <- EvIv
* use for imul */
make_DHelper(I_E2G) {
decode_op_rm(eip, id_src2, true, id_dest, false);
decode_op_I(eip, id_src, true);
}
/* Eb <- Ib
* Ev <- Iv
*/
make_DHelper(I2E) {
decode_op_rm(eip, id_dest, true, NULL, false);
decode_op_I(eip, id_src, true);
}
make_DHelper(mov_I2E) {
decode_op_rm(eip, id_dest, false, NULL, false);
decode_op_I(eip, id_src, true);
}
/* XX <- Ib
* eXX <- Iv
*/
make_DHelper(I2r) {
decode_op_r(eip, id_dest, true);
decode_op_I(eip, id_src, true);
}
make_DHelper(mov_I2r) {
decode_op_r(eip, id_dest, false);
decode_op_I(eip, id_src, true);
}
/* used by unary operations */
make_DHelper(I) {
decode_op_I(eip, id_dest, true);
}
make_DHelper(r) {
decode_op_r(eip, id_dest, true);
}
make_DHelper(E) {
decode_op_rm(eip, id_dest, true, NULL, false);
}
make_DHelper(setcc_E) {
decode_op_rm(eip, id_dest, false, NULL, false);
}
make_DHelper(gp7_E) {
decode_op_rm(eip, id_dest, false, NULL, false);
}
/* used by test in group3 */
make_DHelper(test_I) {
decode_op_I(eip, id_src, true);
}
make_DHelper(SI2E) {
assert(id_dest->width == 2 || id_dest->width == 4);
decode_op_rm(eip, id_dest, true, NULL, false);
id_src->width = 1;
decode_op_SI(eip, id_src, true);
if (id_dest->width == 2) {
id_src->val &= 0xffff;
}
}
make_DHelper(SI_E2G) {
assert(id_dest->width == 2 || id_dest->width == 4);
decode_op_rm(eip, id_src2, true, id_dest, false);
id_src->width = 1;
decode_op_SI(eip, id_src, true);
if (id_dest->width == 2) {
id_src->val &= 0xffff;
}
}
make_DHelper(gp2_1_E) {
decode_op_rm(eip, id_dest, true, NULL, false);
id_src->type = OP_TYPE_IMM;
id_src->imm = 1;
rtl_li(&id_src->val, 1);
#ifdef DEBUG
sprintf(id_src->str, "$1");
#endif
}
make_DHelper(gp2_cl2E) {
decode_op_rm(eip, id_dest, true, NULL, false);
id_src->type = OP_TYPE_REG;
id_src->reg = R_CL;
rtl_lr(&id_src->val, R_CL, 1);
#ifdef DEBUG
sprintf(id_src->str, "%%cl");
#endif
}
make_DHelper(gp2_Ib2E) {
decode_op_rm(eip, id_dest, true, NULL, false);
id_src->width = 1;
decode_op_I(eip, id_src, true);
}
/* Ev <- GvIb
* use for shld/shrd */
make_DHelper(Ib_G2E) {
decode_op_rm(eip, id_dest, true, id_src2, true);
id_src->width = 1;
decode_op_I(eip, id_src, true);
}
/* Ev <- GvCL
* use for shld/shrd */
make_DHelper(cl_G2E) {
decode_op_rm(eip, id_dest, true, id_src2, true);
id_src->type = OP_TYPE_REG;
id_src->reg = R_CL;
rtl_lr(&id_src->val, R_CL, 1);
#ifdef DEBUG
sprintf(id_src->str, "%%cl");
#endif
}
make_DHelper(O2a) {
decode_op_O(eip, id_src, true);
decode_op_a(eip, id_dest, false);
}
make_DHelper(a2O) {
decode_op_a(eip, id_src, true);
decode_op_O(eip, id_dest, false);
}
make_DHelper(J) {
decode_op_SI(eip, id_dest, false);
// the target address can be computed in the decode stage
decoding.jmp_eip = id_dest->simm + *eip;
}
make_DHelper(push_SI) {
decode_op_SI(eip, id_dest, true);
}
make_DHelper(in_I2a) {
id_src->width = 1;
decode_op_I(eip, id_src, true);
decode_op_a(eip, id_dest, false);
}
make_DHelper(in_dx2a) {
id_src->type = OP_TYPE_REG;
id_src->reg = R_DX;
rtl_lr(&id_src->val, R_DX, 2);
#ifdef DEBUG
sprintf(id_src->str, "(%%dx)");
#endif
decode_op_a(eip, id_dest, false);
}
make_DHelper(out_a2I) {
decode_op_a(eip, id_src, true);
id_dest->width = 1;
decode_op_I(eip, id_dest, true);
}
make_DHelper(out_a2dx) {
decode_op_a(eip, id_src, true);
id_dest->type = OP_TYPE_REG;
id_dest->reg = R_DX;
rtl_lr(&id_dest->val, R_DX, 2);
#ifdef DEBUG
sprintf(id_dest->str, "(%%dx)");
#endif
}
void operand_write(Operand *op, rtlreg_t* src) {
if (op->type == OP_TYPE_REG) { rtl_sr(op->reg, src, op->width); }
else if (op->type == OP_TYPE_MEM) { rtl_sm(&op->addr, src, op->width); }
else { assert(0); }
}