aboutsummaryrefslogtreecommitdiff
path: root/pk/syscall.c
blob: 4180eb2bafe88dd9719bd7da25355fdedd0a1782 (plain)
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
// See LICENSE for license details.

#include "syscall.h"
#include "pk.h"
#include "pcr.h"
#include "file.h"
#include "frontend.h"
#include "vm.h"
#include <string.h>
#include <errno.h>

typedef sysret_t (*syscall_t)(long, long, long, long, long, long, long);

void sys_exit(int code)
{
  if (current.t0)
    printk("%ld cycles\n", rdcycle() - current.t0);

  frontend_syscall(SYS_exit, code, 0, 0, 0);
  while (1);
}

sysret_t sys_read(int fd, char* buf, size_t n)
{
  sysret_t r = {-1,EBADF};
  file_t* f = file_get(fd);

  if (f)
  {
    r = file_read(f, buf, n);
    file_decref(f);
  }

  return r;
}

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)
  {
    r = file_write(f, buf, n);
    file_decref(f);
  }

  return r;
}

sysret_t sys_open(const char* name, int flags, int mode)
{
  sysret_t ret = file_open(name, 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)
{
  int ret = fd_close(fd);
  return (sysret_t){ret, ret & EBADF};
}

sysret_t sys_fstat(int fd, void* st)
{
  sysret_t r = {-1,EBADF};
  file_t* f = file_get(fd);

  if (f)
  {
    r = file_stat(f, st);
    file_decref(f);
  }

  return r;
}

sysret_t sys_lseek(int fd, size_t ptr, int dir)
{
  sysret_t r = {-1,EBADF};
  file_t* f = file_get(fd);

  if (f)
  {
    r = file_lseek(f, ptr, dir);
    file_decref(f);
  }

  return r;
}

sysret_t sys_stat(const char* name, void* st)
{
  size_t name_size = strlen(name)+1;
  populate_mapping(st, sizeof(struct stat), PROT_WRITE);
  return frontend_syscall(SYS_stat, (uintptr_t)name, name_size, (uintptr_t)st, 0);
}

sysret_t sys_lstat(const char* name, void* st)
{
  size_t name_size = strlen(name)+1;
  populate_mapping(st, sizeof(struct stat), PROT_WRITE);
  return frontend_syscall(SYS_lstat, (uintptr_t)name, name_size, (uintptr_t)st, 0);
}

sysret_t sys_link(const char* old_name, const char* new_name)
{
  size_t old_size = strlen(old_name)+1;
  size_t new_size = strlen(new_name)+1;
  return frontend_syscall(SYS_link, (uintptr_t)old_name, old_size,
                                    (uintptr_t)new_name, new_size);
}

sysret_t sys_unlink(const char* name, size_t len)
{
  size_t name_size = strlen(name)+1;
  return frontend_syscall(SYS_unlink, (uintptr_t)name, name_size, 0, 0);
}

sysret_t sys_brk(size_t pos)
{
  return do_brk(pos);
}

sysret_t sys_uname(void* buf)
{
  const int sz = 65;
  strcpy(buf + 0*sz, "Proxy Kernel");
  strcpy(buf + 1*sz, "");
  strcpy(buf + 2*sz, "3.4.5");
  strcpy(buf + 3*sz, "");
  strcpy(buf + 4*sz, "");
  strcpy(buf + 5*sz, "");
  return (sysret_t){0,0};
}

sysret_t sys_getuid()
{
  return (sysret_t){0,0};
}

sysret_t sys_mmap(uintptr_t addr, size_t length, int prot, int flags, int fd, off_t offset)
{
  return do_mmap(addr, length, prot, flags, fd, offset);
}

sysret_t sys_munmap(uintptr_t addr, size_t length)
{
  return do_munmap(addr, length);
}

sysret_t sys_mremap(uintptr_t addr, size_t old_size, size_t new_size, int flags)
{
  return do_mremap(addr, old_size, new_size, flags);
}

sysret_t sys_rt_sigaction(int sig, const void* act, void* oact, size_t sssz)
{
  if (oact)
  {
    size_t sz = current.elf64 ? 6*sizeof(uint64_t) : 8*sizeof(uint32_t);
    populate_mapping(oact, sz, PROT_WRITE);
    memset(oact, 0, sz);
  }

  return (sysret_t){0, 0};
}

sysret_t sys_time(long* loc)
{
  uintptr_t t = rdcycle(), hz = 1000000000;
  if (loc)
  {
    populate_mapping(loc, sizeof(long), PROT_WRITE);
    loc[0] = t/hz;
  }
  return (sysret_t){t, 0};
}

sysret_t sys_gettimeofday(long* loc)
{
  populate_mapping(loc, 2*sizeof(long), PROT_WRITE);

  uintptr_t t = rdcycle(), hz = 1000000000;
  loc[0] = t/hz;
  loc[1] = (t % hz) / (hz / 1000000);
  
  return (sysret_t){0, 0};
}

sysret_t sys_writev(int fd, const void* iov, int cnt)
{
  long get(int i) { return current.elf64 ? ((long*)iov)[i] : ((int*)iov)[i]; }
  populate_mapping(iov, cnt*2*(current.elf64 ? 8 : 4), PROT_READ);

  ssize_t ret = 0;
  for (int i = 0; i < cnt; i++)
  {
    sysret_t r = sys_write(fd, (void*)get(2*i), get(2*i+1));
    if (r.result < 0)
      return r;
    ret += r.result;
  }
  return (sysret_t){ret, 0};
}

sysret_t syscall(long a0, long a1, long a2, long a3, long a4, long a5, long n)
{
  const static void* syscall_table[] = {
    [SYS_exit] = sys_exit,
    [SYS_exit_group] = 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,
    [SYS_uname] = sys_uname,
    [SYS_getuid] = sys_getuid,
    [SYS_geteuid] = sys_getuid,
    [SYS_getgid] = sys_getuid,
    [SYS_getegid] = sys_getuid,
    [SYS_mmap] = sys_mmap,
    [SYS_munmap] = sys_munmap,
    [SYS_mremap] = sys_mremap,
    [SYS_rt_sigaction] = sys_rt_sigaction,
    [SYS_time] = sys_time,
    [SYS_gettimeofday] = sys_gettimeofday,
    [SYS_writev] = sys_writev,
  };

  if(n >= ARRAY_SIZE(syscall_table) || !syscall_table[n])
    panic("bad syscall #%ld!",n);

  sysret_t r = ((syscall_t)syscall_table[n])(a0, a1, a2, a3, a4, a5, n);
  return r;
}