forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSyscallHandler.cpp
446 lines (417 loc) · 11.6 KB
/
SyscallHandler.cpp
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
// Copyright (c) 2011, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#include "Exceptions.h"
#include "Core.h"
#include "SyscallHandler.h"
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include "ScopedArray.h"
#include "Trace.h"
#ifndef _MSC_VER
#include <unistd.h>
#else
#include <io.h>
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
#define dup _dup
#define open _open
#define close _close
#define read _read
#define write _write
#define lseek _lseek
#endif
using namespace Register;
class SyscallHandlerImpl {
private:
const scoped_array<int> fds;
bool tracing;
unsigned doneSyscallsRequired;
char *getString(Thread &thread, uint32_t address);
void *getBuffer(Thread &thread, uint32_t address, uint32_t size);
int getNewFd();
bool isValidFd(int fd);
int convertOpenFlags(int flags);
int convertOpenMode(int mode);
bool convertLseekType(int whence, int &converted);
void doException(const Thread &state, uint32_t et, uint32_t ed);
public:
SyscallHandlerImpl();
void setDoneSyscallsRequired(unsigned count) { doneSyscallsRequired = count; }
SyscallHandler::SycallOutcome doSyscall(Thread &thread, int &retval);
void doException(const Thread &thread);
static SyscallHandlerImpl instance;
};
enum SyscallType {
OSCALL_EXIT = 0,
OSCALL_PRINTC = 1,
OSCALL_PRINTINT = 2,
OSCALL_OPEN = 3,
OSCALL_CLOSE = 4,
OSCALL_READ = 5,
OSCALL_WRITE = 6,
OSCALL_DONE = 7,
OSCALL_LSEEK = 8,
OSCALL_RENAME = 9,
OSCALL_TIME = 10,
OSCALL_REMOVE = 11,
OSCALL_SYSTEM = 12,
OSCALL_EXCEPTION = 13,
OSCALL_IS_SIMULATION = 99
};
enum LseekType {
XCORE_SEEK_CUR = 1,
XCORE_SEEK_END = 2,
XCORE_SEEK_SET = 0
};
enum OpenMode {
XCORE_S_IREAD = 0400,
XCORE_S_IWRITE = 0200,
XCORE_S_IEXEC = 0100
};
enum OpenFlags {
XCORE_O_RDONLY = 0x0001,
XCORE_O_WRONLY = 0x0002,
XCORE_O_RDWR = 0x0004,
XCORE_O_CREAT = 0x0100,
XCORE_O_TRUNC = 0x0200,
XCORE_O_APPEND = 0x0800,
XCORE_O_BINARY = 0x8000
};
const unsigned MAX_FDS = 512;
SyscallHandlerImpl::SyscallHandlerImpl() :
fds(new int[MAX_FDS]), tracing(false), doneSyscallsRequired(1)
{
// Duplicate the standard file descriptors.
fds[0] = dup(STDIN_FILENO);
fds[1] = dup(STDOUT_FILENO);
fds[2] = dup(STDERR_FILENO);
// The rest are initialised to -1.
for (unsigned i = 3; i < MAX_FDS; i++) {
fds[i] = - 1;
}
}
/// Returns a pointer to a string in memory at the given address.
/// Returns 0 if the address is invalid or the string is not null terminated.
char *SyscallHandlerImpl::getString(Thread &thread, uint32_t startAddress)
{
Core &core = thread.getParent();
if (!core.isValidAddress(startAddress))
return 0;
// Check the string is null terminated
uint32_t address = startAddress;
uint32_t end = core.getRamSize() + core.ram_base;
for (; address < end && core.loadByte(address); address++) {}
if (address >= end) {
return 0;
}
return (char *)&core.byte(address);
}
/// Returns a pointer to a buffer in memory of the given size.
/// Returns 0 if the buffer address is invalid.
void *SyscallHandlerImpl::
getBuffer(Thread &thread, uint32_t address, uint32_t size)
{
Core &core = thread.getParent();
if (!core.isValidAddress(address) || !core.isValidAddress(address + size))
return 0;
return (void *)&core.byte(address);
}
/// Either returns an unused number for a file descriptor or -1 if there are no
/// file descriptors available.
int SyscallHandlerImpl::getNewFd()
{
for (unsigned i = 0; i < MAX_FDS; i++) {
if (fds[i] == -1) {
return i;
}
}
return -1;
}
bool SyscallHandlerImpl::isValidFd(int fd)
{
return fd >= 0 && (unsigned)fd < MAX_FDS && fds[fd] != -1;
}
int SyscallHandlerImpl::convertOpenFlags(int flags)
{
int converted = 0;
if (flags & XCORE_O_RDONLY)
converted |= O_RDONLY;
if (flags & XCORE_O_WRONLY)
converted |= O_WRONLY;
if (flags & XCORE_O_RDWR)
converted |= O_RDWR;
if (flags & XCORE_O_CREAT)
converted |= O_CREAT;
if (flags & XCORE_O_TRUNC)
converted |= O_TRUNC;
if (flags & XCORE_O_APPEND)
converted |= O_APPEND;
#ifdef O_BINARY
if (flags & XCORE_O_BINARY)
converted |= O_BINARY;
#endif
return converted;
}
int SyscallHandlerImpl::convertOpenMode(int mode)
{
int converted = 0;
if (mode & XCORE_S_IREAD)
converted |= S_IREAD;
if (mode & XCORE_S_IWRITE)
converted |= S_IWRITE;
if (mode & XCORE_S_IEXEC)
converted |= S_IEXEC;
return converted;
}
bool SyscallHandlerImpl::convertLseekType(int whence, int &converted)
{
switch (whence) {
case XCORE_SEEK_CUR:
converted = SEEK_CUR;
return true;
case XCORE_SEEK_END:
converted = SEEK_END;
return true;
case XCORE_SEEK_SET:
converted = SEEK_SET;
return true;
default:
return false;
}
}
void SyscallHandlerImpl::doException(const Thread &thread, uint32_t et, uint32_t ed)
{
std::cout << "Unhandled exception: "
<< Exceptions::getExceptionName(et)
<< ", data: 0x" << std::hex << ed << std::dec << "\n";
std::cout << "Register state:\n";
thread.dump();
}
void SyscallHandlerImpl::doException(const Thread &thread)
{
doException(thread, thread.regs[ET], thread.regs[ED]);
}
#define TRACE_BEGIN() \
do { \
if (Tracer::get().getTracingEnabled()) { \
Tracer::get().syscall(thread); \
} \
} while(0)
#define TRACE_END() \
do { \
if (Tracer::get().getTracingEnabled()) { \
Tracer::get().traceEnd(); \
} \
} while(0)
#define TRACE(...) \
do { \
if (Tracer::get().getTracingEnabled()) { \
Tracer::get().syscall(thread, __VA_ARGS__); \
Tracer::get().traceEnd(); \
} \
} while(0)
#define TRACE_REG_WRITE(register, value) \
do { \
if (Tracer::getInstance().getTracingEnabled()) { \
Tracer::getInstance().regWrite(register, value); \
} \
} while(0)
#define STATS(...) void()
SyscallHandler::SycallOutcome SyscallHandlerImpl::
doSyscall(Thread &thread, int &retval)
{
switch (thread.regs[R0]) {
case OSCALL_EXIT:
TRACE("exit", thread.regs[R1]);
retval = thread.regs[R1];
return SyscallHandler::EXIT;
case OSCALL_DONE:
TRACE("done");
if (--doneSyscallsRequired == 0) {
retval = 0;
return SyscallHandler::EXIT;
}
return SyscallHandler::DESCHEDULE;
case OSCALL_EXCEPTION:
doException(thread, thread.regs[R1], thread.regs[R2]);
retval = 1;
return SyscallHandler::EXIT;
case OSCALL_OPEN:
{
uint32_t PathAddr = thread.regs[R1];
const char *path = getString(thread, PathAddr);
if (!path) {
// Invalid argument
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
int flags = convertOpenFlags(thread.regs[R2]);
int mode = convertOpenMode(thread.regs[R3]);
int fd = getNewFd();
if (fd == -1) {
// No free file descriptors
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
int hostfd = open(path, flags, mode);
if (hostfd == -1) {
// Call to open failed.
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
fds[fd] = hostfd;
thread.regs[R0] = fd;
return SyscallHandler::CONTINUE;
}
case OSCALL_CLOSE:
{
if (!isValidFd(thread.regs[R1])) {
// Invalid fd
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
int retval = close(fds[thread.regs[R1]]);
if (retval == 0) {
fds[thread.regs[R1]] = (uint32_t)-1;
}
thread.regs[R0] = retval;
return SyscallHandler::CONTINUE;
}
case OSCALL_READ:
{
if (!isValidFd(thread.regs[R1])) {
// Invalid fd
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
void *buf = getBuffer(thread, thread.regs[R2], thread.regs[R3]);
if (!buf) {
// Invalid buffer
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
thread.regs[R0] = read(fds[thread.regs[R1]], buf, thread.regs[R3]);
return SyscallHandler::CONTINUE;
}
case OSCALL_WRITE:
{
if (!isValidFd(thread.regs[R1])) {
// Invalid fd
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
const void *buf = getBuffer(thread, thread.regs[R2], thread.regs[R3]);
if (!buf) {
// Invalid buffer
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
thread.regs[R0] = write(fds[thread.regs[R1]], buf, thread.regs[R3]);
return SyscallHandler::CONTINUE;
}
case OSCALL_LSEEK:
{
if (!isValidFd(thread.regs[R1])) {
// Invalid fd
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
int whence;
if (! convertLseekType(thread.regs[R3], whence)) {
// Invalid seek type
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
thread.regs[R0] = lseek(fds[thread.regs[R1]], thread.regs[R2], whence);
return SyscallHandler::CONTINUE;
}
case OSCALL_RENAME:
{
uint32_t OldPathAddr = thread.regs[R1];
uint32_t NewPathAddr = thread.regs[R2];
const char *oldpath = getString(thread, OldPathAddr);
const char *newpath = getString(thread, NewPathAddr);
if (!oldpath || !newpath) {
// Invalid argument
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
thread.regs[R0] = std::rename(oldpath, newpath);
return SyscallHandler::CONTINUE;
}
case OSCALL_TIME:
{
uint32_t TimeAddr = thread.regs[R1];
uint32_t Time = (uint32_t)std::time(0);
Core &core = thread.getParent();
if (TimeAddr != 0) {
if (!core.isValidAddress(TimeAddr) || (TimeAddr & 3)) {
// Invalid address
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
core.storeWord(Time, TimeAddr);
core.invalidateWord(TimeAddr);
}
thread.regs[R0] = Time;
return SyscallHandler::CONTINUE;
}
case OSCALL_REMOVE:
{
uint32_t FileAddr = thread.regs[R1];
const char *file = getString(thread, FileAddr);
if (file == 0) {
// Invalid argument
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
thread.regs[R0] = std::remove(file);
return SyscallHandler::CONTINUE;
}
case OSCALL_SYSTEM:
{
uint32_t CmdAddr = thread.regs[R1];
const char *command = 0;
if (CmdAddr != 0) {
command = getString(thread, CmdAddr);
if (command == 0) {
// Invalid argument
thread.regs[R0] = (uint32_t)-1;
return SyscallHandler::CONTINUE;
}
}
thread.regs[R0] = std::system(command);
return SyscallHandler::CONTINUE;
}
case OSCALL_IS_SIMULATION:
thread.regs[R0] = 1;
return SyscallHandler::CONTINUE;
default:
std::cout << "Error: unknown system call number: " << thread.regs[R0] << "\n";
retval = 1;
return SyscallHandler::EXIT;
}
}
SyscallHandlerImpl SyscallHandlerImpl::instance;
void SyscallHandler::setDoneSyscallsRequired(unsigned number)
{
SyscallHandlerImpl::instance.setDoneSyscallsRequired(number);
}
SyscallHandler::SycallOutcome SyscallHandler::
doSyscall(Thread &thread, int &retval)
{
return SyscallHandlerImpl::instance.doSyscall(thread, retval);
}
void SyscallHandler::doException(const Thread &thread)
{
return SyscallHandlerImpl::instance.doException(thread);
}