syscall.c 3.21 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
4
5
6
7
8
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "x86.h"
#include "syscall.h"

rsc's avatar
rsc committed
9
10
11
12
13
// 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
14

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

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

33
34
  if(addr >= p->sz)
    return -1;
Frans Kaashoek's avatar
nits    
Frans Kaashoek committed
35
36
  *pp = (char *) addr;
  ep = (char *) p->sz;
rsc's avatar
rsc committed
37
38
39
  for(s = *pp; s < ep; s++)
    if(*s == 0)
      return s - *pp;
rsc's avatar
rsc committed
40
  return -1;
rtm's avatar
rtm committed
41
42
}

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

rsc's avatar
rsc committed
51
52
53
// Fetch the nth word-sized system call argument as a pointer
// to a block of memory of size n bytes.  Check that the pointer
// 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
58
  int i;
  
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;
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;
Russ Cox's avatar
Russ Cox committed
77
  return fetchstr(proc, 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
static int (*syscalls[])(void) = {
[SYS_chdir]   sys_chdir,
[SYS_close]   sys_close,
[SYS_dup]     sys_dup,
[SYS_exec]    sys_exec,
[SYS_exit]    sys_exit,
[SYS_fork]    sys_fork,
[SYS_fstat]   sys_fstat,
[SYS_getpid]  sys_getpid,
[SYS_kill]    sys_kill,
[SYS_link]    sys_link,
[SYS_mkdir]   sys_mkdir,
[SYS_mknod]   sys_mknod,
[SYS_open]    sys_open,
[SYS_pipe]    sys_pipe,
[SYS_read]    sys_read,
[SYS_sbrk]    sys_sbrk,
rsc's avatar
rsc committed
119
[SYS_sleep]   sys_sleep,
rsc's avatar
rsc committed
120
121
122
[SYS_unlink]  sys_unlink,
[SYS_wait]    sys_wait,
[SYS_write]   sys_write,
123
[SYS_uptime]  sys_uptime,
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
130
  int num;
  
Russ Cox's avatar
Russ Cox committed
131
  num = proc->tf->eax;
rsc's avatar
rsc committed
132
  if(num >= 0 && num < NELEM(syscalls) && syscalls[num])
Russ Cox's avatar
Russ Cox committed
133
    proc->tf->eax = syscalls[num]();
rsc's avatar
rsc 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
  }
}