This repository has been archived by the owner on Jan 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscall.cpp
93 lines (92 loc) · 2.29 KB
/
syscall.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
//
// Created by leewy on 2016/11/13.
//
#include "syscall.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <cassert>
#include <iostream>
#include "vmm.h"
extern unsigned long long sbrk_point;
extern VM memory;
extern long long ins_counter;
extern bool is_exit;
extern int exit_code;
using ULL = unsigned long long;
void sys_exit(int code) {
printf("Total instruction: %lld", ins_counter);
is_exit = true;
exit_code = code;
}
ssize_t sys_read(int fd, ULL buf, ssize_t n) {
char* p_buf = new char[n + 1];
auto rval = read(fd, p_buf, n);
memory.load(p_buf, buf, n);
delete[] p_buf;
return rval;
}
ssize_t sys_write(int fd, ULL buf, ssize_t n) {
char* p_buf = new char[n + 1];
memory.store(p_buf, buf, n);
auto rval = write(fd, p_buf, n);
delete[] p_buf;
return rval;
}
int sys_gettimeofday(ULL offset) {
timeval tv;
auto rval = gettimeofday(&tv, NULL);
memory.load((const char*)(&tv), offset, sizeof(timeval));
return rval;
}
ULL sys_brk(ULL ptr) {
if (ptr == 0)
return sbrk_point;
else
return ptr;
}
int sys_close(int fd) { return close(fd); }
int sys_fstat(int fd, ULL buf) {
struct stat st;
auto rval = fstat(fd, &st);
memory.load((const char*)(&st), buf, sizeof(struct stat));
return rval;
}
ssize_t sys_lseek(int fd, size_t ptr, int whence) {
return lseek(fd, ptr, whence);
}
ULL systemCall(int sys_code, ULL a0, ULL a1, ULL a2, ULL a3) {
ULL rval = 0;
switch (sys_code) {
case SYS_exit:
sys_exit(a0);
break;
case SYS_read:
rval = sys_read(a0, a1, a2);
break;
case SYS_write:
rval = sys_write(a0, a1, a2);
break;
case SYS_gettimeofday:
rval = sys_gettimeofday(a0);
break;
case SYS_brk:
rval = sys_brk(a0);
break;
case SYS_close:
rval = sys_close(a0);
break;
case SYS_fstat:
rval = sys_fstat(a0, a1);
break;
case SYS_lseek:
rval = sys_lseek(a0, a1, a2);
break;
default:
std::cerr << "Error: Systemcall error." << sys_code << std::endl;
assert(false);
}
return rval;
}