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
|
#include <machine/syscall.h>
#include <string.h>
#include <errno.h>
#include "pk.h"
#include "pcr.h"
#include "file.h"
#include "frontend.h"
typedef sysret_t (*syscall_t)(long,long,long,long,long);
void sys_exit(int code)
{
frontend_syscall(SYS_exit,code,0,0,0);
panic("exit didn't exit!");
}
sysret_t sys_read(int fd, char* buf, size_t n)
{
sysret_t r = {-1,EBADF};
file_t* f = file_get(fd);
if(!f)
return r;
return file_read(f,buf,n);
}
sysret_t sys_write(int fd, const char* buf, size_t n)
{
sysret_t r = {-1,EBADF};
file_t* f = file_get(fd);
if(!f)
return r;
return file_write(f,buf,n);
}
sysret_t sys_open(const char* name, size_t len, int flags, int mode)
{
sysret_t ret = file_open(name, len, flags, mode);
if(ret.result == -1)
return ret;
if((ret.result = file_dup((file_t*)ret.result)) == -1)
ret.err = ENOMEM;
return ret;
}
sysret_t sys_close(int fd)
{
return (sysret_t){fd_close(fd),EBADF};
}
sysret_t sys_fstat(int fd, void* st)
{
sysret_t r = {-1,EBADF};
file_t* f = file_get(fd);
if(!f)
return r;
return file_stat(f,st);
}
sysret_t sys_lseek(int fd, size_t ptr, int dir)
{
sysret_t r = {-1,EBADF};
file_t* f = file_get(fd);
if(!f)
return r;
return file_lseek(f,ptr,dir);
}
sysret_t sys_stat(const char* name, size_t len, void* st)
{
return frontend_syscall(SYS_stat,(long)name,len,(long)st,0);
}
sysret_t sys_lstat(const char* name, size_t len, void* st)
{
return frontend_syscall(SYS_lstat,(long)name,len,(long)st,0);
}
sysret_t sys_link(const char* old_name, size_t old_len,
const char* new_name, size_t new_len)
{
return frontend_syscall(SYS_link,(long)old_name,old_len,
(long)new_name,new_len);
}
sysret_t sys_unlink(const char* name, size_t len)
{
return frontend_syscall(SYS_unlink,(long)name,len,0,0);
}
sysret_t sys_brk(size_t pos)
{
if(pos / (1024 * 1024) >= mem_mb)
return (sysret_t){-1, ENOMEM};
return (sysret_t){0,0};
}
sysret_t syscall(long a0, long a1, long a2, long a3, long n)
{
const static void* syscall_table[] = {
[SYS_exit] = sys_exit,
[SYS_read] = sys_read,
[SYS_write] = sys_write,
[SYS_open] = sys_open,
[SYS_close] = sys_close,
[SYS_fstat] = sys_fstat,
[SYS_lseek] = sys_lseek,
[SYS_stat] = sys_stat,
[SYS_lstat] = sys_lstat,
[SYS_link] = sys_link,
[SYS_unlink] = sys_unlink,
[SYS_brk] = sys_brk,
};
if(n >= ARRAY_SIZE(syscall_table) || !syscall_table[n])
panic("bad syscall #%ld!",n);
return ((syscall_t)syscall_table[n])(a0, a1, a2, a3, n);
}
|