syscall.c 3.13 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
22
23
24
  *ip = *(int*)(p->mem + addr);
  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;
rsc's avatar
rsc committed
35
36
  *pp = p->mem + addr;
  ep = p->mem + 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
{
rsc's avatar
rsc committed
47
  return fetchint(cp, cp->tf->esp + 4 + 4*n, ip);
rtm's avatar
rtm committed
48
49
}

rsc's avatar
rsc committed
50
51
52
// 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
53
int
rsc's avatar
rsc committed
54
argptr(int n, char **pp, int size)
rtm's avatar
rtm committed
55
{
rsc's avatar
rsc committed
56
57
  int i;
  
rsc's avatar
rsc committed
58
  if(argint(n, &i) < 0)
rsc's avatar
rsc committed
59
    return -1;
rsc's avatar
rsc committed
60
  if((uint)i >= cp->sz || (uint)i+size >= cp->sz)
rsc's avatar
rsc committed
61
    return -1;
rsc's avatar
rsc committed
62
  *pp = cp->mem + i;
rsc's avatar
rsc committed
63
  return 0;
rtm's avatar
rtm committed
64
65
}

rsc's avatar
rsc committed
66
67
68
69
// 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
70
int
rsc's avatar
rsc committed
71
argstr(int n, char **pp)
rtm's avatar
rtm committed
72
{
rsc's avatar
rsc committed
73
  int addr;
rsc's avatar
rsc committed
74
  if(argint(n, &addr) < 0)
rsc's avatar
   
rsc committed
75
    return -1;
rsc's avatar
rsc committed
76
  return fetchstr(cp, addr, pp);
rtm's avatar
rtm committed
77
78
}

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
95
extern int sys_sleep(void);
96
97
98
extern int sys_unlink(void);
extern int sys_wait(void);
extern int sys_write(void);
rtm's avatar
rtm committed
99

rsc's avatar
rsc committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
117
[SYS_sleep]   sys_sleep,
rsc's avatar
rsc committed
118
119
120
121
122
[SYS_unlink]  sys_unlink,
[SYS_wait]    sys_wait,
[SYS_write]   sys_write,
};

rtm's avatar
rtm committed
123
void
rsc's avatar
rsc committed
124
syscall(void)
rtm's avatar
rtm committed
125
{
rsc's avatar
nits    
rsc committed
126
127
128
  int num;
  
  num = cp->tf->eax;
rsc's avatar
rsc committed
129
130
131
132
133
134
  if(num >= 0 && num < NELEM(syscalls) && syscalls[num])
    cp->tf->eax = syscalls[num]();
  else {
    cprintf("%d %s: unknown sys call %d\n",
            cp->pid, cp->name, num);
    cp->tf->eax = -1;
rtm's avatar
rtm committed
135
136
  }
}