-
Notifications
You must be signed in to change notification settings - Fork 9
/
emulator_functions.c
592 lines (515 loc) · 14.1 KB
/
emulator_functions.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#include <stdint.h>
#include "emulator_functions.h"
#include "gdt.h"
#include "paging.h"
#include "io.h"
#include "lapic.h"
#include "ioapic.h"
#include "util.h"
/* Register Operations */
void set_register8(Emulator *emu, int index, uint8_t value)
{
if (index < 4)
{
/* Resets lowest byte keeping the rest same. */
uint32_t r = emu->registers[index] & 0xffffff00;
emu->registers[index] = r | (uint32_t)value;
}
else
{
/* Resets 2nd byte keeping the rest same. */
uint32_t r = emu->registers[index - 4] & 0xffff00ff;
emu->registers[index - 4] = r | ((int32_t)value << 8);
}
}
uint8_t get_register8(Emulator *emu, int index)
{
if (index < 4)
{
return emu->registers[index] & 0xff;
}
else
{
/* AH - DH: Offset 8 bit higher. */
return (emu->registers[index - 4] >> 8) & 0xff;
}
}
void set_register16(Emulator *emu, int reg_index, uint16_t value)
{
/* Resets lowest byte keeping the rest same. */
uint32_t r = emu->registers[reg_index] & 0xffff0000;
emu->registers[reg_index] = r | (uint32_t)value;
}
uint16_t get_register16(Emulator *emu, int reg_index)
{
return emu->registers[reg_index] & 0xffff;
}
void set_register32(Emulator *emu, int reg_index, uint32_t value)
{
emu->registers[reg_index] = value;
}
uint32_t get_register32(Emulator *emu, int reg_index)
{
return emu->registers[reg_index];
}
/* Segment Register Operations */
void set_seg_register16(Emulator *emu, int reg_index, uint16_t value)
{
emu->segment_registers[reg_index] = value;
}
uint16_t get_seg_register16(Emulator *emu, int reg_index)
{
return emu->segment_registers[reg_index];
}
/* Control Register Operations */
void set_ctrl_register32(Emulator *emu, int reg_index, uint32_t value)
{
emu->control_registers[reg_index] = value;
}
uint32_t get_ctrl_register32(Emulator *emu, int reg_index)
{
return emu->control_registers[reg_index];
}
/* Physical Memory Operations */
uint32_t get_physical_address(Emulator *emu, int seg_index, uint32_t offset, uint8_t write)
{
uint8_t exec = 0;
if (seg_index == CS)
exec = 1;
uint16_t seg_val = get_seg_register16(emu, seg_index);
/* Protected mode enabled. */
if (emu->is_pe)
{
uint32_t linear = get_linear_addr(emu, seg_val, offset, write, exec);
check_paging(emu);
/* Paging enabled. */
if (emu->is_pg)
return get_phys_addr(emu, linear, write, exec);
else
return linear;
}
/* Real mode */
uint32_t p_base = ((uint32_t)seg_val) << 4;
return p_base + offset;
}
void _set_memory8(Emulator *emu, uint32_t p_address, uint8_t value)
{
emu->memory[p_address] = value & 0xFF;
}
void _set_memory16(Emulator *emu, uint32_t p_address, uint16_t value)
{
int i;
for (i = 0; i < 2; i++)
{
/* passes right-most 8 bits first (little endian) */
_set_memory8(emu, p_address + i, value >> (i * 8));
}
}
void _set_memory32(Emulator *emu, uint32_t p_address, uint32_t value)
{
int i;
for (i = 0; i < 4; i++)
{
/* passes right-most 8 bits first (little endian) */
_set_memory8(emu, p_address + i, value >> (i * 8));
}
}
static uint8_t _get_memory8(Emulator *emu, uint32_t p_address)
{
return emu->memory[p_address];
}
static uint16_t _get_memory16(Emulator *emu, uint32_t p_address)
{
int i;
uint16_t mem_read = 0;
for (i = 0; i < 2; i++)
{
/* little endian: first to far right */
mem_read |= _get_memory8(emu, p_address + i) << (8 * i);
}
return mem_read;
}
uint32_t _get_memory32(Emulator *emu, uint32_t p_address)
{
int i;
uint32_t mem_read = 0;
for (i = 0; i < 4; i++)
{
mem_read |= _get_memory8(emu, p_address + i) << (8 * i);
}
return mem_read;
}
/* Source Instruction Operations */
/* Retrieves code from memory by offset from EIP. */
uint8_t get_code8(Emulator *emu, int index)
{
uint32_t p_addr = get_physical_address(emu, CS, emu->eip, 0);
return emu->memory[p_addr + index];
}
int8_t get_sign_code8(Emulator *emu, int index)
{
return (int8_t)get_code8(emu, index);
}
uint16_t get_code16(Emulator *emu, int index)
{
int i;
uint32_t code = 0;
/* i386 uses little endian (lower bytes to right). */
for (i = 0; i < 2; i++)
{
/* Shift 8 bits to left per byte read */
code |= get_code8(emu, index + i) << (i * 8);
}
return code;
}
int16_t get_sign_code16(Emulator *emu, int index)
{
return (int16_t)get_code16(emu, index);
}
uint32_t get_code32(Emulator *emu, int index)
{
int i;
uint32_t code = 0;
for (i = 0; i < 4; i++)
{
code |= get_code8(emu, index + i) << (i * 8);
}
return code;
}
int32_t get_sign_code32(Emulator *emu, int index)
{
return (int32_t)get_code32(emu, index);
}
/* Memory Operations with Segment Registers */
void set_memory8(Emulator *emu, int seg_index, uint32_t address, uint32_t value)
{
uint32_t p_address = get_physical_address(emu, seg_index, address, 1);
_set_memory8(emu, p_address, value);
}
void set_memory16(Emulator *emu, int seg_index, uint32_t address, uint16_t value)
{
uint32_t p_address = get_physical_address(emu, seg_index, address, 1);
_set_memory16(emu, p_address, value);
}
void set_memory32(Emulator *emu, int seg_index, uint32_t address, uint32_t value)
{
uint32_t p_address = get_physical_address(emu, seg_index, address, 1);
/* mmio */
if (p_address >= LAPIC_DEFAULT_BASE)
{
lapic_write_reg(emu->lapic, p_address, value);
return;
}
if (p_address >= IOAPIC_DEFAULT_BASE)
{
ioapic_write_reg(p_address, value);
return;
}
_set_memory32(emu, p_address, value);
}
uint8_t get_memory8(Emulator *emu, int seg_index, uint32_t address)
{
uint32_t p_address = get_physical_address(emu, seg_index, address, 0);
return _get_memory8(emu, p_address);
}
uint16_t get_memory16(Emulator *emu, int seg_index, uint32_t address)
{
uint32_t p_address = get_physical_address(emu, seg_index, address, 0);
return _get_memory16(emu, p_address);
}
uint32_t get_memory32(Emulator *emu, int seg_index, uint32_t address)
{
uint32_t p_address = get_physical_address(emu, seg_index, address, 0);
/* mmio */
if (p_address >= LAPIC_DEFAULT_BASE)
{
return lapic_read_reg(emu->lapic, p_address);
}
if (p_address >= IOAPIC_DEFAULT_BASE)
{
return ioapic_read_reg(p_address);
}
return _get_memory32(emu, p_address);
}
/* Stack Operations */
void push16(Emulator *emu, uint16_t value)
{
/* New offset would be ESP value - 2 bytes. */
uint32_t offset = get_register32(emu, ESP) - 2;
set_register32(emu, ESP, offset);
uint32_t p_address = get_physical_address(emu, SS, offset, 1);
_set_memory16(emu, p_address, value);
}
void push32(Emulator *emu, uint32_t value)
{
/* New offset would be ESP value - 4 bytes. */
uint32_t offset = get_register32(emu, ESP) - 4;
/* Updates ESP with the new address. */
set_register32(emu, ESP, offset);
uint32_t p_address = get_physical_address(emu, SS, offset, 1);
_set_memory32(emu, p_address, value);
}
uint16_t pop16(Emulator *emu)
{
uint32_t offset = get_register32(emu, ESP);
uint32_t p_address = get_physical_address(emu, SS, offset, 0);
uint16_t value = _get_memory16(emu, p_address);
set_register32(emu, ESP, offset + 2);
return value;
}
uint32_t pop32(Emulator *emu)
{
uint32_t offset = get_register32(emu, ESP);
uint32_t p_address = get_physical_address(emu, SS, offset, 0);
uint32_t value = _get_memory32(emu, p_address);
set_register32(emu, ESP, offset + 4);
return value;
}
/* Segment Register to Memory */
void push_segment_register(Emulator *emu, int reg_index)
{
if (emu->is_pe)
{
uint16_t value = get_seg_register16(emu, reg_index);
push32(emu, (uint32_t)value);
}
else
{
uint16_t value = get_seg_register16(emu, reg_index);
push16(emu, value);
}
}
void pop_segment_register(Emulator *emu, int reg_index)
{
if (emu->is_pe)
{
uint16_t value = (uint16_t)pop32(emu);
set_seg_register16(emu, reg_index, value);
}
else
{
uint16_t value = pop16(emu);
set_seg_register16(emu, reg_index, value);
}
}
/* Eflag Operations */
void set_carry_flag(Emulator *emu, int is_carry)
{
/* Higher 32 bits are pass as int */
if (is_carry)
{
emu->eflags |= CARRY_FLAG;
}
else
{
/* Deflag: AND ~(NOT): NOT 0001 = 1110 AND 1001 -> 1000 */
emu->eflags &= ~CARRY_FLAG;
}
}
void set_zero_flag(Emulator *emu, int is_zero)
{
if (is_zero)
{
emu->eflags |= ZERO_FLAG;
}
else
{
emu->eflags &= ~ZERO_FLAG;
}
}
void set_sign_flag(Emulator *emu, int is_sign)
{
if (is_sign)
{
emu->eflags |= SIGN_FLAG;
}
else
{
emu->eflags &= ~SIGN_FLAG;
}
}
void set_int_flag(Emulator *emu, int is_enabled)
{
if (is_enabled)
{
emu->eflags |= INT_ENABLE_FLAG;
}
else
{
emu->eflags &= ~INT_ENABLE_FLAG;
}
}
void set_direction_flag(Emulator *emu, int is_down)
{
if (is_down)
{
emu->eflags |= DIRECTION_FLAG;
}
else
{
emu->eflags &= ~DIRECTION_FLAG;
}
}
void set_overflow_flag(Emulator *emu, int is_overflow)
{
if (is_overflow)
{
emu->eflags |= OVERFLOW_FLAG;
}
else
{
emu->eflags &= ~OVERFLOW_FLAG;
}
}
int32_t is_carry(Emulator *emu)
{
return (emu->eflags & CARRY_FLAG) != 0;
}
int32_t is_zero(Emulator *emu)
{
return (emu->eflags & ZERO_FLAG) != 0;
}
int32_t is_sign(Emulator *emu)
{
return (emu->eflags & SIGN_FLAG) != 0;
}
int32_t is_int_enabled(Emulator *emu)
{
return (emu->eflags & INT_ENABLE_FLAG) != 0;
}
int32_t is_direction_down(Emulator *emu)
{
return (emu->eflags & DIRECTION_FLAG) != 0;
}
int32_t is_overflow(Emulator *emu)
{
return (emu->eflags & OVERFLOW_FLAG) != 0;
}
void update_eflags_add(Emulator *emu, uint32_t value1, uint32_t value2, uint64_t result)
{
/* sign bits */
int sign1 = value1 >> 31;
int sign2 = value2 >> 31;
int signr = (result >> 31) & 1;
/* 1111 1111 (255) + 1111 1111 (255) = 1 1111 1110 (510)*/
set_carry_flag(emu, result >> 32);
/*
* For assumed signed numbers.
* Overflow: Most significant bit (sign bit) is changed.
* Happens only with +n + +n or +n - -n.
* 0111 1111 (127) + 0111 1111 (127) = 1111 1110 (254)
*/
set_overflow_flag(emu, sign1 == sign2 && sign1 != signr);
set_sign_flag(emu, signr);
}
void update_eflags_add_8bit(Emulator *emu, uint8_t value1, uint8_t value2, uint16_t result)
{
int sign1 = value1 >> 7;
int sign2 = value2 >> 7;
int signr = (result >> 7) & 1;
set_carry_flag(emu, result >> 8);
set_overflow_flag(emu, sign1 == sign2 && sign1 != signr);
set_sign_flag(emu, signr);
}
void update_eflags_add_16bit(Emulator *emu, uint8_t value1, uint8_t value2, uint16_t result)
{
int sign1 = value1 >> 15;
int sign2 = value2 >> 15;
int signr = (result >> 15) & 1;
set_carry_flag(emu, result >> 16);
set_overflow_flag(emu, sign1 == sign2 && sign1 != signr);
set_sign_flag(emu, signr);
}
/*
* 1000 (-8) - 0001 (1) = 10111 (-9) -> carry
* 1000 (-8) - 0010 (2) = 10110 (-10) -> carry
* Overflow might happen only when 2 values have different signs in subtraction.
* AND overflow changes the sign of value 1 in result. Ex: 4 bit case:
* 0111 (7) - 1111 (-1) = 1000 (-8) *wrong (sign1 != sign2 && sign1 != signr)* -> overflow
* 0111 (7) - 1000 (-8) = 1111 (-1) *wrong (sign1 != sign2 && sign1 != signr)* -> overflow
*/
void update_eflags_sub(Emulator *emu, uint32_t value1, uint32_t value2, uint64_t result)
{
/* sign bits */
int sign1 = value1 >> 31;
int sign2 = value2 >> 31;
int signr = (result >> 31) & 1;
set_carry_flag(emu, result >> 32); // higher 32 bits
set_zero_flag(emu, result == 0);
set_sign_flag(emu, signr);
set_overflow_flag(emu, sign1 != sign2 && sign1 != signr);
}
void update_eflags_sub_8bit(Emulator *emu, uint8_t value1, uint8_t value2, uint16_t result)
{
int sign1 = value1 >> 7;
int sign2 = value2 >> 7;
int signr = (result >> 7) & 1;
set_carry_flag(emu, result >> 8); // higher 32 bits
set_zero_flag(emu, result == 0);
set_sign_flag(emu, signr);
set_overflow_flag(emu, sign1 != sign2 && sign1 != signr);
}
void update_eflags_sub_16bit(Emulator *emu, uint16_t value1, uint16_t value2, uint32_t result)
{
int sign1 = value1 >> 15;
int sign2 = value2 >> 15;
int signr = (result >> 15) & 1;
set_carry_flag(emu, result >> 16); // higher 32 bits
set_zero_flag(emu, result == 0);
set_sign_flag(emu, signr);
set_overflow_flag(emu, sign1 != sign2 && sign1 != signr);
}
void update_eflags_logical_ops(Emulator *emu, uint32_t result)
{
int signr = (result >> 31) & 1;
set_carry_flag(emu, 0);
set_zero_flag(emu, result == 0);
set_sign_flag(emu, signr);
set_overflow_flag(emu, 0);
}
void update_eflags_logical_ops_8bit(Emulator *emu, uint8_t result)
{
int signr = (result >> 7) & 1;
set_carry_flag(emu, 0);
set_zero_flag(emu, result == 0);
set_sign_flag(emu, signr);
set_overflow_flag(emu, 0);
}
void update_eflags_logical_ops_16bit(Emulator *emu, uint8_t result)
{
int signr = (result >> 15) & 1;
set_carry_flag(emu, 0);
set_zero_flag(emu, result == 0);
set_sign_flag(emu, signr);
set_overflow_flag(emu, 0);
}
/*
* imul & mul both updates OF and CF when a significant bit gets into upper half.
*/
void update_eflags_mul(Emulator *emu, uint64_t upper_half_result)
{
if (upper_half_result == 0)
{
set_overflow_flag(emu, 0);
set_carry_flag(emu, 0);
}
else
{
set_overflow_flag(emu, 1);
set_carry_flag(emu, 1);
}
}
void set_tr(Emulator *emu, uint16_t val)
{
emu->tr = val;
}
void set_gdtr(Emulator *emu, uint16_t limit, uint32_t base)
{
emu->gdtr.limit = limit;
emu->gdtr.base = base;
}
void set_idtr(Emulator *emu, uint16_t limit, uint32_t base)
{
emu->idtr.limit = limit;
emu->idtr.base = base;
}