syscall.c 3.2 KB
Newer Older
rtm's avatar
rtm committed
1
#include "types.h"
rsc's avatar
rsc committed
2
#include "defs.h"
rtm's avatar
rtm committed
3
#include "param.h"
Frans Kaashoek's avatar
Frans Kaashoek committed
4
#include "memlayout.h"
rtm's avatar
rtm committed
5
6
7
8
9
#include "mmu.h"
#include "proc.h"
#include "x86.h"
#include "syscall.h"

rsc's avatar
rsc committed
10
11
12
13
14
// User code makes a system call with INT T_SYSCALL.
// System call number in %eax.
// Arguments on the stack, from the user call to the C
// library system call function. The saved user %esp points
// to a saved program counter, and then the first argument.
rtm's avatar
rtm committed
15

16
// Fetch the int at addr from the current process.
rtm's avatar
rtm committed
17
int
18
fetchint(uint addr, int *ip)
rtm's avatar
rtm committed
19
{
20
  if(addr >= proc->sz || addr+4 > proc->sz)
rsc's avatar
   
rsc committed
21
    return -1;
22
  *ip = *(int*)(addr);
23
24
25
  return 0;
}

26
// Fetch the nul-terminated string at addr from the current process.
rsc's avatar
rsc committed
27
28
// Doesn't actually copy the string - just sets *pp to point at it.
// Returns length of string, not including nul.
29
int
30
fetchstr(uint addr, char **pp)
31
{
rsc's avatar
rsc committed
32
  char *s, *ep;
rsc's avatar
rsc committed
33

34
  if(addr >= proc->sz)
35
    return -1;
Russ Cox's avatar
Russ Cox committed
36
  *pp = (char*)addr;
37
  ep = (char*)proc->sz;
rsc's avatar
rsc committed
38
39
40
  for(s = *pp; s < ep; s++)
    if(*s == 0)
      return s - *pp;
rsc's avatar
rsc committed
41
  return -1;
rtm's avatar
rtm committed
42
43
}

rsc's avatar
rsc committed
44
// Fetch the nth 32-bit system call argument.
rtm's avatar
rtm committed
45
int
rsc's avatar
rsc committed
46
argint(int n, int *ip)
rtm's avatar
rtm committed
47
{
48
  return fetchint(proc->tf->esp + 4 + 4*n, ip);
rtm's avatar
rtm committed
49
50
}

rsc's avatar
rsc committed
51
// Fetch the nth word-sized system call argument as a pointer
Frans Kaashoek's avatar
Frans Kaashoek committed
52
// to a block of memory of size bytes.  Check that the pointer
rsc's avatar
rsc committed
53
// lies within the process address space.
rtm's avatar
rtm committed
54
int
rsc's avatar
rsc committed
55
argptr(int n, char **pp, int size)
rtm's avatar
rtm committed
56
{
rsc's avatar
rsc committed
57
  int i;
58

rsc's avatar
rsc committed
59
  if(argint(n, &i) < 0)
rsc's avatar
rsc committed
60
    return -1;
Russ Cox's avatar
Russ Cox committed
61
  if((uint)i >= proc->sz || (uint)i+size > proc->sz)
rsc's avatar
rsc committed
62
    return -1;
Russ Cox's avatar
Russ Cox committed
63
  *pp = (char*)i;
rsc's avatar
rsc committed
64
  return 0;
rtm's avatar
rtm committed
65
66
}

rsc's avatar
rsc committed
67
68
69
70
// Fetch the nth word-sized system call argument as a string pointer.
// Check that the pointer is valid and the string is nul-terminated.
// (There is no shared writable memory, so the string can't change
// between this check and being used by the kernel.)
rtm's avatar
rtm committed
71
int
rsc's avatar
rsc committed
72
argstr(int n, char **pp)
rtm's avatar
rtm committed
73
{
rsc's avatar
rsc committed
74
  int addr;
rsc's avatar
rsc committed
75
  if(argint(n, &addr) < 0)
rsc's avatar
   
rsc committed
76
    return -1;
77
  return fetchstr(addr, pp);
rtm's avatar
rtm committed
78
79
}

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
extern int sys_chdir(void);
extern int sys_close(void);
extern int sys_dup(void);
extern int sys_exec(void);
extern int sys_exit(void);
extern int sys_fork(void);
extern int sys_fstat(void);
extern int sys_getpid(void);
extern int sys_kill(void);
extern int sys_link(void);
extern int sys_mkdir(void);
extern int sys_mknod(void);
extern int sys_open(void);
extern int sys_pipe(void);
extern int sys_read(void);
extern int sys_sbrk(void);
rsc's avatar
rsc committed
96
extern int sys_sleep(void);
97
98
99
extern int sys_unlink(void);
extern int sys_wait(void);
extern int sys_write(void);
100
extern int sys_uptime(void);
rtm's avatar
rtm committed
101

rsc's avatar
rsc committed
102
103
static int (*syscalls[])(void) = {
[SYS_fork]    sys_fork,
Frans Kaashoek's avatar
Frans Kaashoek committed
104
105
[SYS_exit]    sys_exit,
[SYS_wait]    sys_wait,
rsc's avatar
rsc committed
106
107
[SYS_pipe]    sys_pipe,
[SYS_read]    sys_read,
Frans Kaashoek's avatar
Frans Kaashoek committed
108
109
110
111
112
113
[SYS_kill]    sys_kill,
[SYS_exec]    sys_exec,
[SYS_fstat]   sys_fstat,
[SYS_chdir]   sys_chdir,
[SYS_dup]     sys_dup,
[SYS_getpid]  sys_getpid,
rsc's avatar
rsc committed
114
[SYS_sbrk]    sys_sbrk,
rsc's avatar
rsc committed
115
[SYS_sleep]   sys_sleep,
116
[SYS_uptime]  sys_uptime,
Frans Kaashoek's avatar
Frans Kaashoek committed
117
118
119
120
121
122
123
[SYS_open]    sys_open,
[SYS_write]   sys_write,
[SYS_mknod]   sys_mknod,
[SYS_unlink]  sys_unlink,
[SYS_link]    sys_link,
[SYS_mkdir]   sys_mkdir,
[SYS_close]   sys_close,
rsc's avatar
rsc committed
124
125
};

rtm's avatar
rtm committed
126
void
rsc's avatar
rsc committed
127
syscall(void)
rtm's avatar
rtm committed
128
{
rsc's avatar
nits    
rsc committed
129
  int num;
Frans Kaashoek's avatar
Frans Kaashoek committed
130

Russ Cox's avatar
Russ Cox committed
131
  num = proc->tf->eax;
132
  if(num > 0 && num < NELEM(syscalls) && syscalls[num]) {
Russ Cox's avatar
Russ Cox committed
133
    proc->tf->eax = syscalls[num]();
Frans Kaashoek's avatar
Frans Kaashoek committed
134
  } else {
rsc's avatar
rsc committed
135
    cprintf("%d %s: unknown sys call %d\n",
Russ Cox's avatar
Russ Cox committed
136
137
            proc->pid, proc->name, num);
    proc->tf->eax = -1;
rtm's avatar
rtm committed
138
139
  }
}