-
Notifications
You must be signed in to change notification settings - Fork 9
/
instructions_C0.c
401 lines (353 loc) · 9.25 KB
/
instructions_C0.c
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include "instruction_defs.h"
#include "emulator_functions.h"
#include "modrm.h"
#include "io.h"
#include "shift.h"
#include "interrupt.h"
#include "util.h"
static uint8_t adjust_imm8_for_rm8(uint8_t val)
{
while (val > 8)
{
val -= 8;
}
return val;
}
static void rol_rm8_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm8(get_code8(emu, 0));
emu->eip += 1;
set_rm8(emu, modrm, rol8(get_rm8(emu, modrm), imm8_val));
}
static void ror_rm8_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm8(get_code8(emu, 0));
emu->eip += 1;
set_rm8(emu, modrm, ror8(get_rm8(emu, modrm), imm8_val));
}
static void rcl_rm8_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm8(get_code8(emu, 0));
emu->eip += 1;
ShiftRes8 res = rcl8(get_rm8(emu, modrm), imm8_val, (uint8_t)is_carry(emu));
set_rm8(emu, modrm, res.result);
set_carry_flag(emu, res.carry);
}
static void rcr_rm8_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm8(get_code8(emu, 0));
emu->eip += 1;
ShiftRes8 res = rcr8(get_rm8(emu, modrm), imm8_val, (uint8_t)is_carry(emu));
set_rm8(emu, modrm, res.result);
set_carry_flag(emu, res.carry);
}
static void shl_rm8_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm8(get_code8(emu, 0));
emu->eip += 1;
ShiftRes8 res = shl8(get_rm8(emu, modrm), imm8_val);
set_rm8(emu, modrm, res.result);
set_carry_flag(emu, res.carry);
}
static void shr_rm8_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm8(get_code8(emu, 0));
emu->eip += 1;
ShiftRes8 res = shr8(get_rm8(emu, modrm), imm8_val, (uint8_t)is_carry(emu));
set_rm8(emu, modrm, res.result);
set_carry_flag(emu, res.carry);
}
static void sar_rm8_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm8(get_code8(emu, 0));
emu->eip += 1;
ShiftRes8 res = sar8(get_rm8(emu, modrm), imm8_val, (uint8_t)is_carry(emu));
set_rm8(emu, modrm, res.result);
set_carry_flag(emu, res.carry);
}
void code_c0(Emulator *emu)
{
/* Proceed 1 byte for op code C0. */
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
switch (modrm.opcode)
{
case 0:
rol_rm8_imm8(emu, &modrm);
break;
case 1:
ror_rm8_imm8(emu, &modrm);
break;
case 2:
rcl_rm8_imm8(emu, &modrm);
break;
case 3:
rcr_rm8_imm8(emu, &modrm);
break;
case 4:
shl_rm8_imm8(emu, &modrm); /* shl or sal: technically the same. */
break;
case 5:
shr_rm8_imm8(emu, &modrm);
break;
case 6:
shr_rm8_imm8(emu, &modrm); /* shr or sar: undefined? */
break;
case 7:
sar_rm8_imm8(emu, &modrm);
break;
default:
printf("Not implemented: Op: C0 with ModR/M Op: %d\n", modrm.opcode);
panic_exit(emu);
}
}
static uint8_t adjust_imm8_for_rm32(uint8_t val)
{
while (val > 32)
{
val -= 32;
}
return val;
}
static void rol_rm32_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm32(get_code8(emu, 0));
emu->eip += 1;
set_rm32(emu, modrm, rol32(get_rm32(emu, modrm), imm8_val));
}
static void ror_rm32_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm32(get_code8(emu, 0));
emu->eip += 1;
set_rm32(emu, modrm, ror32(get_rm32(emu, modrm), imm8_val));
}
static void rcl_rm32_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm32(get_code8(emu, 0));
emu->eip += 1;
ShiftRes32 res = rcl32(get_rm32(emu, modrm), imm8_val, (uint8_t)is_carry(emu));
set_rm32(emu, modrm, res.result);
set_carry_flag(emu, res.carry);
}
static void rcr_rm32_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm32(get_code8(emu, 0));
emu->eip += 1;
ShiftRes32 res = rcr32(get_rm32(emu, modrm), imm8_val, (uint8_t)is_carry(emu));
set_rm32(emu, modrm, res.result);
set_carry_flag(emu, res.carry);
}
static void shl_rm32_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm32(get_code8(emu, 0));
emu->eip += 1;
ShiftRes32 res = shl32(get_rm32(emu, modrm), imm8_val);
set_rm32(emu, modrm, res.result);
set_carry_flag(emu, res.carry);
}
static void shr_rm32_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm32(get_code8(emu, 0));
emu->eip += 1;
ShiftRes32 res = shr32(get_rm32(emu, modrm), imm8_val, (uint8_t)is_carry(emu));
set_rm32(emu, modrm, res.result);
set_carry_flag(emu, res.carry);
}
static void sar_rm32_imm8(Emulator *emu, ModRM *modrm)
{
uint8_t imm8_val = adjust_imm8_for_rm32(get_code8(emu, 0));
emu->eip += 1;
ShiftRes32 res = sar32(get_rm32(emu, modrm), imm8_val, (uint8_t)is_carry(emu));
set_rm32(emu, modrm, res.result);
set_carry_flag(emu, res.carry);
}
void code_c01(Emulator *emu)
{
/* Proceed 1 byte for op code C1. */
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
switch (modrm.opcode)
{
case 0:
rol_rm32_imm8(emu, &modrm);
break;
case 1:
ror_rm32_imm8(emu, &modrm);
break;
case 2:
rcl_rm32_imm8(emu, &modrm);
break;
case 3:
rcr_rm32_imm8(emu, &modrm);
break;
case 4:
shl_rm32_imm8(emu, &modrm); /* shl or sal: technically the same. */
break;
case 5:
shr_rm32_imm8(emu, &modrm);
break;
case 6:
shr_rm32_imm8(emu, &modrm); /* shr or sar: undefined? */
break;
case 7:
sar_rm32_imm8(emu, &modrm);
break;
default:
printf("Not implemented: Op: C1 with ModR/M Op: %d\n", modrm.opcode);
panic_exit(emu);
}
}
/*
* ret: 1 byte
* Jumps to the address pushed by call (address after call instruction).
* 1 byte: op (C3)
*/
void ret(Emulator *emu)
{
emu->eip = pop32(emu);
}
void load_seg_r32(Emulator *emu, int seg_index, ModRM *modrm)
{
uint32_t address = calc_memory_address(emu, modrm);
uint16_t seg_val = get_memory16(emu, DS, address);
uint32_t offset = get_memory32(emu, DS, address + 2);
set_seg_register16(emu, seg_index, seg_val);
set_register32(emu, modrm->reg_index, offset);
}
/*
* les: 2|4 bytes
* Loads ES:r16/32 with far pointer from memory.
* 1 byte: op (C4)
* 1|5 bytes: ModRM
*/
void les(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
load_seg_r32(emu, ES, &modrm);
}
/*
* lds: 2|4 bytes
* Loads DS:r16/32 with far pointer from memory.
* 1 byte: op (C5)
* 1|5 bytes: ModRM
*/
void lds(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
load_seg_r32(emu, DS, &modrm);
}
/*
* mov rm8 imm8: 3/4 bytes
* Copies imm8 value to register or memory specified by ModR/M(rm8).
* 1 byte: op (C6)
* 1/2 byte: ModR/M
* 1 byte: imm8 value
*/
void mov_rm8_imm8(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
uint8_t value = get_code8(emu, 0);
emu->eip += 1;
set_rm8(emu, &modrm, value);
}
/*
* mov rm16 imm16: 5 bytes
* Copies imm value to register or memory specified by ModR/M (rm16).
* 1 byte: op (C7)
* 1 byte: ModR/M
* 2 bytes: value
*/
void mov_rm16_imm16(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
uint16_t value = get_code16(emu, 0);
emu->eip += 2;
set_rm32(emu, &modrm, value);
}
/*
* mov rm32 imm32: 5 bytes
* Copies imm value to register or memory specified by ModR/M (rm32).
* 1 byte: op (C7)
* 1 byte: ModR/M
* 4 bytes: value (32)
*/
void mov_rm32_imm32(Emulator *emu)
{
emu->eip += 1;
ModRM modrm = create_modrm();
parse_modrm(emu, &modrm);
uint32_t value = get_code32(emu, 0);
emu->eip += 4;
set_rm32(emu, &modrm, value);
}
/*
* leave: 1byte
* Set of mov esp, ebp & pop ebp.
* 1 byte: op (C9)
*/
void leave(Emulator *emu)
{
uint32_t ebp_val = get_register32(emu, EBP);
/* Update ESP with current EBP value. */
set_register32(emu, ESP, ebp_val);
/* Pop from stack and set it on EBP. */
set_register32(emu, EBP, pop32(emu));
emu->eip += 1;
}
/*
* ret far: 1 byte
* Jumps to the address pushed by call (EIP & CS).
* 1 byte: op (CB)
*/
void ret_far(Emulator *emu)
{
emu->eip = pop32(emu);
pop_segment_register(emu, CS);
}
/*
* int imm8: 2 bytes
* Software interrupt.
* 1 byte: op (CD)
* 1 byte: imm8
*/
void int_imm8(Emulator *emu)
{
uint8_t int_num = get_code8(emu, 1);
emu->eip += 2;
handle_interrupt(emu, int_num, 1);
}
/*
* iret: 1 byte
* Returns from interrupt.
* 1 byte: op (CF)
*/
void iret(Emulator *emu)
{
// printf("iret\n");
uint8_t cpl = get_seg_register16(emu, CS) & 3;
emu->eip = pop32(emu);
pop_segment_register(emu, CS);
/* By right each flag such as IOPL should be checked if CPL is 0 or not. */
emu->eflags = pop32(emu);
if ((get_seg_register16(emu, CS) & 3) > cpl)
{
uint32_t esp_val = pop32(emu);
pop_segment_register(emu, SS);
set_register32(emu, ESP, esp_val);
}
}