-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvmas.h
88 lines (68 loc) · 2.06 KB
/
vmas.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
#ifndef VMAS_H
#define VMAS_H
#include <stdint.h>
#include "debug.h"
#define DEFAULT_STACKSIZE 0x100
#define DEFAULT_CODESIZE 0x300
#define DEFAULT_DATASIZE 0x100
#define MAX_CODESIZE 0xFFFF
#define MAX_DATASIZE 0xFFFF
class VMAddrSpace {
private:
uint32_t stacksize, codesize, datasize;
uint8_t *stack, *code, *data;
bool allocate(void);
public:
VMAddrSpace();
VMAddrSpace(uint32_t ss, uint16_t cs, uint16_t ds);
~VMAddrSpace();
uint8_t *getStack();
uint8_t *getCode();
uint8_t *getData();
uint32_t getStacksize();
uint32_t getCodesize();
uint32_t getDatasize();
bool insStack(uint8_t *buf, uint32_t size);
bool insCode(uint8_t *buf, uint32_t size);
bool insData(uint8_t *buf, uint32_t size);
template<typename src_t, typename dst_t>
bool getArgs(uint32_t idx, src_t *src, dst_t *dst, uint8_t flag_byte_op = 0) {
if (sizeof(*src) == sizeof(*dst)) {
if (idx + 1 >= codesize) {
DBG_ERROR(("Argument out of code segment bounds.\n"));
return false;
}
if (!flag_byte_op) {
// both regs
*dst = code[idx + 1] >> 4;
*src = code[idx + 1] & 0b00001111;
} else {
*dst = code[idx + 1];
*src = code[idx + 1 + sizeof(*dst)];
}
} else {
/*
* OP DST SRC
* DST = IP + 1
* SRC = IP + 1 + SIZE(DST)
*/
if (idx + sizeof(*dst) >= codesize) {
DBG_ERROR(("Argument out of code segment bounds.\n"));
return false;
}
*dst = *((dst_t *) &code[idx + 1]);
*src = *((src_t *) &code[idx + 1 + sizeof(*dst)]);
}
return true;
}
template<typename T>
bool getArgs(uint32_t ip, T *arg) {
if (ip + 1 >= codesize) {
DBG_ERROR(("Argument out of code segment bounds.\n"));
return false;
}
*arg = *((T *) &code[ip + 1]);
return true;
}
};
#endif