aboutsummaryrefslogtreecommitdiff
path: root/pk/syscall.c
blob: d303324b273aa7e3794ef0f854c998bacd2108c8 (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
#include <machine/syscall.h>
#include <string.h>
#include <errno.h>
#include "pk.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);
}

void handle_syscall(trapframe_t* tf)
{
  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,
  };

  syscall_t p;
  unsigned long n = tf->gpr[2];
  if(n >= sizeof(syscall_table)/sizeof(void*) || !syscall_table[n])
  {
    dump_tf(tf);
    panic("bad syscall #%ld!",n);
  }
  else
    p = (syscall_t)syscall_table[n];

  sysret_t ret = p(tf->gpr[4],tf->gpr[5],tf->gpr[6],tf->gpr[7],n);
  tf->gpr[2] = ret.result;
  tf->gpr[3] = ret.result == -1 ? ret.err : 0;

  //printk("syscall %d (%x,%x,%x,%x) from %x == %d\n",n,tf->gpr[4],tf->gpr[5],tf->gpr[6],tf->gpr[7],tf->gpr[31],tf->gpr[2]);

  advance_pc(tf);
  pop_tf(tf);
}