-
Notifications
You must be signed in to change notification settings - Fork 6
/
vm.c
422 lines (386 loc) · 10.5 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
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
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define PROGRAM_LENGTH 65536*2
#define PC 61
#define dprintf if (verbose > VERBOSE_NONE) printf
#define VERBOSE_NONE 0
#define VERBOSE_DEBUG 1
#define VERBOSE_TRACE 2
int _argc;
const char** _argv;
int verbose = 0;
int dump = 0;
uint8_t flag;
uint8_t program[PROGRAM_LENGTH];
uint32_t registers[64];
void debug(const char* msg) {
dprintf("DEBUG %s\n", msg);
}
void invalid(const char* what) {
printf("ERROR Invalid %s\n", what);
printf("PC = %08x\n", registers[PC]);
if (dump) {
printf("Dumped memory to /tmp/memory.bin\n");
int fd = open("/tmp/memory.bin", O_CREAT | O_TRUNC | O_WRONLY, 0777);
write(fd, &program[0], sizeof(program));
close(fd);
}
exit(1);
}
uint8_t* memory(int register_index) {
uint32_t address = registers[register_index];
if (address >= PROGRAM_LENGTH) {
invalid("memory access");
}
return &program[address];
}
void write16(void* location, uint32_t value) {
uint8_t* bytes = (uint8_t*)location;
bytes[0] = value & 0xff;
bytes[1] = (value >> 8) & 0xff;
}
void write32(void* location, uint32_t value) {
uint8_t* bytes = (uint8_t*)location;
bytes[0] = value & 0xff;
bytes[1] = (value >> 8) & 0xff;
bytes[2] = (value >> 16) & 0xff;
bytes[3] = (value >> 24) & 0xff;
}
uint32_t read16(void* location) {
uint8_t* bytes = (uint8_t*)location;
return bytes[0] | bytes[1] << 8;
}
uint32_t read32(void* location) {
uint8_t* bytes = (uint8_t*)location;
return bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24;
}
uint32_t readpc8() {
uint8_t value = program[registers[PC]];
registers[PC]++;
return value;
}
uint32_t readpc32() {
uint32_t value = read32(&program[registers[PC]]);
registers[PC] += 4;
return value;
}
int map_open_flags(uint32_t flags) {
int out_flags = 0;
switch (flags & 3) {
case 0:
out_flags |= O_RDONLY;
break;
case 1:
out_flags |= O_WRONLY;
break;
case 2:
out_flags |= O_RDWR;
break;
}
if (flags & 0x200) {
out_flags |= O_CREAT;
}
if (flags & 0x400) {
out_flags |= O_TRUNC;
}
return out_flags;
}
int sc(uint32_t syscall,
uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
if (syscall == 0) {
debug("open");
dprintf("open: %s %08x (%08x)\n", (const char*)&program[arg1], arg2, map_open_flags(arg2));
int r = open((const char*)&program[arg1], map_open_flags(arg2), 0777);
if (r < 0) {
perror("open");
}
return r;
} else if (syscall == 1) {
debug("read");
return read(arg1, &program[arg2], arg3);
} else if (syscall == 2) {
debug("write");
return write(arg1, &program[arg2], arg3);
} else if (syscall == 3) {
debug("lseek");
int whence = 0;
if (arg3 == 0) {
whence = SEEK_SET;
} else if (arg3 == 1) {
whence = SEEK_CUR;
} else if (arg3 == 2) {
whence = SEEK_END;
} else {
invalid("seek");
}
return (uint32_t)(int32_t)lseek(arg1, (off_t)(int32_t)arg2, whence);
} else if (syscall == 4) {
debug("close");
return close(arg1);
} else if (syscall == 5) {
debug("getargv");
uint32_t needed = _argc * 4; // includes trailing zero
for (int i = 1; i < _argc; i++) {
needed += strlen(_argv[i]) + 1;
}
uint8_t* address_write = &program[arg1];
uint32_t string_write = arg1 + _argc * 4;
if (needed <= arg2) {
for (int i = 1; i < _argc; i++) {
write32(address_write, string_write);
strcpy((char*)&program[string_write], _argv[i]);
string_write += strlen(_argv[i]) + 1;
address_write += 4;
}
write32(address_write, 0);
} else {
dprintf("Buffer not large enough\n");
}
return needed;
} else if (syscall == 6) {
debug("getmemsize");
return PROGRAM_LENGTH;
} else if (syscall == 7) {
debug("exit");
if (dump) {
printf("Dumped memory to /tmp/memory.bin\n");
int fd = open("/tmp/memory.bin", O_CREAT | O_TRUNC | O_WRONLY, 0777);
write(fd, &program[0], sizeof(program));
close(fd);
}
exit(arg1);
return 0;
} else if (syscall == 8) {
debug("openat");
dprintf("%x %s %08x\n", arg1, (const char*)&program[arg2], arg3);
int r = openat(arg1 == 0xffffff38 ? AT_FDCWD : (int)arg1, (const char*)&program[arg2], map_open_flags(arg3), 0777);
if (r < 0) {
perror("openat");
}
return r;
} else {
printf("%x\n", syscall);
invalid("syscall");
}
return 0;
}
int char_to_register(uint8_t reg) {
if (reg >= '0' && reg <= '9') {
return reg - '0';
}
if (reg >= 'A' && reg <= 'Z') {
return reg - 'A' + 10;
}
if (reg >= 'a' && reg <= 'z') {
return reg - 'a' + 36;
}
if (reg == ' ') {
return 0;
}
invalid("register");
}
uint8_t hexchar(const char hex) {
if (hex >= '0' && hex <= '9') {
return hex - '0';
}
if (hex >= 'a' && hex <= 'f') {
return hex - 'a' + 10;
}
invalid("hex");
return 0;
}
uint16_t readpchex() {
return hexchar(readpc8()) << 12 | hexchar(readpc8()) << 8 | hexchar(readpc8()) << 4 | hexchar(readpc8()) << 0;
}
uint32_t rhs(uint8_t op2, uint8_t op4) {
if (op2 == ' ') {
return registers[char_to_register(op4)];
}
if (op2 == '!') {
// Sign-extended
return (int8_t)op4;
}
debug("invalid load");
invalid("load");
}
int main(int argc, const char** argv) {
int fd;
while (argc > 1) {
if (strcmp(argv[1], "-v") == 0) {
verbose++;
} else if (strcmp(argv[1], "-d") == 0) {
dump++;
} else {
break;
}
argv++;
argc--;
}
if (argc <= 1) {
printf("USAGE: vm [-v [-v]] program [arguments...]\n");
exit(1);
}
_argc = argc;
_argv = argv;
flag = 0;
memset(program, 0, PROGRAM_LENGTH);
memset(registers, 0, sizeof(registers));
fd = open(argv[1], O_RDONLY);
read(fd, program, PROGRAM_LENGTH);
close(fd);
while (1) {
dprintf("PC = %08x\n", registers[PC]);
if (verbose >= VERBOSE_TRACE) {
for (int i = 0; i < sizeof(registers) / sizeof(registers[0]); i += 8) {
dprintf("%08x %08x %08x %08x %08x %08x %08x %08x\n", registers[i], registers[i+1], registers[i+2], registers[i+3],
registers[i+4], registers[i+5], registers[i+6], registers[i+7]);
}
}
int pc = registers[PC];
uint8_t op1 = program[pc+0];
uint8_t op2 = program[pc+1];
uint8_t op3 = program[pc+2];
uint8_t op4 = program[pc+3];
registers[PC] += 4;
int trace = op1 == '?' && op2 == '?' && op3 == '?';
if (trace) {
if (op4 == '?') {
printf("\n");
for (int i = 0; i < sizeof(registers) / sizeof(registers[0]); i += 8) {
printf("%08x %08x %08x %08x %08x %08x %08x %08x\n", registers[i], registers[i+1], registers[i+2], registers[i+3],
registers[i+4], registers[i+5], registers[i+6], registers[i+7]);
}
} else {
int address = registers[char_to_register(op4)];
printf("%08x:", address);
for (int i = 0; i < 16; i++) {
printf(" %02x", program[address + i]);
}
printf(" ");
for (int i = 0; i < 16; i++) {
printf("%c", (program[address + i] >= 0x20 && program[address + i] < 0x7f) ? program[address + i] : '.');
}
printf("\n");
}
continue;
}
dprintf("%c%c%c%c\n", op1, op2, op3, op4);
// Only if flag is set
if (op2 == '?') {
if (flag) {
op2 = ' ';
} else {
// Skip
continue;
}
}
// Only if flag is not set
if (op2 == '^') {
if (!flag) {
op2 = ' ';
} else {
// Skip
continue;
}
}
if (op1 == '=') {
if (op2 == '#') {
// 16-bit hex literal
debug("hex literal");
registers[char_to_register(op3)] = readpchex();
} else if (op2 == '$') {
// 32-bit binary literal
debug("binary literal");
registers[char_to_register(op3)] = readpc32();
} else if (op2 == '[') {
// 8-bit indirect load
debug("8-bit indirect load");
registers[char_to_register(op3)] = *memory(char_to_register(op4));
} else if (op2 == '{') {
// 16-bit indirect load
debug("16-bit indirect load");
registers[char_to_register(op3)] = read16(memory(char_to_register(op4)));
} else if (op2 == '(') {
// 32-bit indirect load
debug("32-bit indirect load");
registers[char_to_register(op3)] = read32(memory(char_to_register(op4)));
} else {
registers[char_to_register(op3)] = rhs(op2, op4);
}
} else if (op1 == '[' && op2 == '=') {
// 8-bit indirect store
debug("8-bit indirect store");
*memory(char_to_register(op3)) = registers[char_to_register(op4)];
} else if (op1 == '{' && op2 == '=') {
write16(memory(char_to_register(op3)), registers[char_to_register(op4)]);
} else if (op1 == '(' && op2 == '=') {
write32(memory(char_to_register(op3)), registers[char_to_register(op4)]);
} else if (op1 == '+') {
registers[char_to_register(op3)] += rhs(op2, op4);
} else if (op1 == '-') {
registers[char_to_register(op3)] -= rhs(op2, op4);
} else if (op1 == '*') {
registers[char_to_register(op3)] *= rhs(op2, op4);
} else if (op1 == '/') {
registers[char_to_register(op3)] /= rhs(op2, op4);
} else if (op1 == '%') {
registers[char_to_register(op3)] %= rhs(op2, op4);
} else if (op1 == '&') {
registers[char_to_register(op3)] &= rhs(op2, op4);
} else if (op1 == '|') {
registers[char_to_register(op3)] |= rhs(op2, op4);
} else if (op1 == '^') {
registers[char_to_register(op3)] ^= rhs(op2, op4);
} else if (op1 == '>') {
registers[char_to_register(op3)] >>= rhs(op2, op4);
} else if (op1 == '<') {
registers[char_to_register(op3)] <<= rhs(op2, op4);
} else if (op1 == '?') {
if (op2 == '=') {
debug("equal?");
flag = registers[char_to_register(op3)] == registers[char_to_register(op4)];
} else if (op2 == '>') {
debug("gt?");
flag = registers[char_to_register(op3)] > registers[char_to_register(op4)];
} else if (op2 == '<') {
debug("lt?");
flag = registers[char_to_register(op3)] < registers[char_to_register(op4)];
} else if (op2 == '!') {
debug("ne?");
flag = registers[char_to_register(op3)] != registers[char_to_register(op4)];
} else {
invalid("compare");
}
} else if (op1 == 'S') {
// Syscall
debug("syscall");
if (op2 == ' ') {
registers[char_to_register(op3)] = sc(registers[char_to_register(op3)], registers[char_to_register(op4)], 0, 0, 0, 0);
} else if (op2 == '+') {
int a = readpc8();
int b = readpc8();
int c = readpc8();
int d = readpc8();
registers[char_to_register(op3)] = sc(registers[char_to_register(op3)], registers[char_to_register(op4)],
registers[char_to_register(a)], registers[char_to_register(b)],
registers[char_to_register(c)], registers[char_to_register(d)]);
} else {
invalid("syscall");
}
} else if (op1 == 'J') {
debug("jump");
if (op2 == ' ') {
registers[PC] = registers[char_to_register(op3)];
} else {
invalid("jump");
}
} else {
invalid("opcode");
}
}
return 0;
}