-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.c
289 lines (240 loc) · 6.11 KB
/
vm.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include "vm.h"
void clear_memory(Memory* memory) {
free(memory->data);
free(memory);
}
void clear_sectors(VM* vm) {
if (vm->num_sectors > 0) {
free(vm->memory_map);
}
}
VM* init_vm() {
FILE *fp;
fp = fopen("code.omg", "r");
fseek(fp, 0L, SEEK_END);
uint64_t fsize = ftell(fp);
fseek(fp, 0L, SEEK_SET);
Memory *memory = malloc(sizeof(Memory));
memory->length = fsize/sizeof(uint64_t);
memory->data = malloc(fsize);
printf("File size: %" PRIu64 "\n", fsize);
fread(memory->data, sizeof(uint64_t), fsize/sizeof(uint64_t), fp);
Memory *external_memory = malloc(sizeof(Memory));
// number of instructions is constant
Memory *gas_map = malloc(sizeof(Memory));
gas_map->data = malloc(sizeof(uint64_t) * I_INVALID);
gas_map->length = I_INVALID;
Sector* sector_pt;
VM *vm = malloc(sizeof(VM));
vm->mode = M_ROOT;
vm->state = S_NRM,
vm->ip = 0;
vm->oldip = 0;
vm->interrupt = 0;
vm->memory = memory;
vm->external_memory = external_memory;
vm->gas = 0;
vm->gas_map = gas_map;
vm->num_sectors = 0;
vm->memory_map = sector_pt;
return vm;
}
uint64_t mmap_len(VM* vm) {
uint64_t total = 0;
for (int i = 0; i<vm->num_sectors;i++) {
Sector sector = vm->memory_map[i];
total += sector.length;
}
return total;
}
uint64_t get_offset(VM* vm, uint64_t virtual_address) {
for (int i = 0; i<vm->num_sectors;i++) {
Sector sector = vm->memory_map[i];
if (virtual_address < sector.length) {
return sector.start + virtual_address;
} else {
virtual_address -= sector.length;
}
}
// TODO what to do on fail?
}
void print_memory(VM* vm) {
printf("[");
if (vm->mode == M_ROOT) {
for (int i=0;i<vm->memory->length;i++) {
printf("%" PRIu64, vm->memory->data[i]);
if (i < vm->memory->length-1) {
printf(",");
}
}
} else {
for (int i=0;i<mmap_len(vm);i++) {
printf("%" PRIu64, vm->memory->data[get_offset(vm, i)]);
if (i < mmap_len(vm)-1) {
printf(",");
}
}
}
printf("]\n");
}
uint64_t mmap(VM* vm, uint64_t offset) {
if (vm->mode == M_ROOT) {
return offset;
} else {
return get_offset(vm, offset);
}
}
uint64_t gmem(VM* vm, uint64_t offset) {
//TODO check for memory access exception
if (offset < vm->memory->length) {
return vm->memory->data[mmap(vm, offset)];
} else {
}
}
uint64_t smem(VM* vm, uint64_t offset, uint64_t value) {
if (offset < vm->memory->length) {
vm->memory->data[mmap(vm, offset)] = value;
} else {
}
}
uint8_t valid_instruction(uint8_t mode, uint64_t instr) {
if (mode == M_ROOT) {
return 1;
} else if (mode == M_CHILD && instr <= 6) {
return 1;
} else {
return 0;
}
}
uint64_t args(VM* vm, uint64_t arg) {
return gmem(vm, vm->ip+1+arg);
}
void build_gmap(VM* vm, uint64_t offset) {
for (int i=0;i<I_INVALID;i++) {
vm->gas_map->data[i] = gmem(vm, offset+i);
}
}
void build_mmap(VM* vm, uint64_t offset) {
clear_sectors(vm);
vm->num_sectors = gmem(vm, offset);
printf("Creating %lu sectors at offset %lu\n", vm->num_sectors, offset);
vm->memory_map = malloc(sizeof(Sector) * vm->num_sectors);
for (int i=0;i<vm->num_sectors;i++) {
vm->memory_map[i].start = gmem(vm, offset+1+2*i);
vm->memory_map[i].length = gmem(vm, offset+1+2*i+1);
}
}
void run(VM* vm, uint8_t debug) {
printf("Running\n");
while (1) {
if (debug) {
sleep(1);
printf("IP: %" PRIu64 "\n", vm->ip);
print_memory(vm);
}
uint8_t instr = gmem(vm, vm->ip);
if (instr >= I_INVALID || !valid_instruction(vm->mode, instr)) {
vm->mode = M_ROOT;
vm->state = S_IIE;
vm->oldip = vm->ip;
vm->ip = 0;
continue;
}
uint64_t instrcost = vm->gas_map->data[instr];
if (debug) {
//printf("INSTR: %" PRIu8 "\n", instr);
printf("INSTR: %s\n", istrings[instr]);
}
if (vm->mode == M_CHILD && (uint64_t)(vm->gas) - instrcost < 0) {
vm->mode = M_ROOT;
vm->state = S_OOG;
vm->ip = 0;
continue;
}
if (vm->mode == M_CHILD) {
vm->gas -= instrcost;
}
uint8_t jump = 0;
//uint64_t* args =
switch(instr) {
case I_ADD:
smem(vm, args(vm, 0), gmem(vm, args(vm, 0)) + gmem(vm, args(vm, 1)));
break;
case I_ADDI:
smem(vm, args(vm, 0), gmem(vm, args(vm, 0)) + args(vm, 1));
break;
case I_MUL:
smem(vm, args(vm, 0), gmem(vm, args(vm, 0)) * gmem(vm, args(vm, 1)));
break;
case I_MULI:
smem(vm, args(vm, 0), gmem(vm, args(vm, 0)) * args(vm, 1));
break;
case I_JMP:
vm->ip = args(vm, 0);
jump = 1;
break;
case I_JLE:
if (gmem(vm, args(vm, 1)) < gmem(vm, args(vm, 2))) {
vm->ip = args(vm, 0);
jump = 1;
}
break;
case I_RUN:
build_mmap(vm, args(vm, 0));
vm->ip = gmem(vm, args(vm, 1));
vm->mode = M_CHILD;
jump = 1;
break;
case I_INT:
vm->mode = M_ROOT;
vm->oldip = vm->ip;
vm->ip = 0;
jump = 1;
break;
case I_LMT:
build_gmap(vm, args(vm, 0));
vm->gas = args(vm, 1);
break;
case I_JIT:
if (vm->interrupt) {
vm->interrupt = 0;
vm->ip = args(vm, 0);
jump = 1;
}
break;
case I_REM:
if (args(vm, 1) < vm->external_memory->length) {
smem(vm, args(vm, 0), vm->external_memory->data[args(vm, 1)]);
}
break;
case I_MLN:
if (vm->mode == M_ROOT) {
smem(vm, args(vm, 0), vm->memory->length);
} else {
smem(vm, args(vm, 0), mmap_len(vm));
}
break;
case I_SET:
smem(vm, args(vm, 0), args(vm, 1));
break;
case I_OLDIP:
smem(vm, args(vm, 0), vm->oldip);
break;
}
if (!jump) {
// TODO check for out of bounds here?!
vm->ip += 1 + INSTRUCTION_ARGS[instr];
}
}
}
int main() {
printf("Hello, World!\n");
VM* vm = init_vm();
run(vm, 1);
return 0;
}