added base src
[xv6-db.git] / syscall.c
blobf6550a1654f2247df8ec03236688c265a23f6894
1 #include "types.h"
2 #include "defs.h"
3 #include "param.h"
4 #include "mmu.h"
5 #include "proc.h"
6 #include "x86.h"
7 #include "syscall.h"
9 // User code makes a system call with INT T_SYSCALL.
10 // System call number in %eax.
11 // Arguments on the stack, from the user call to the C
12 // library system call function. The saved user %esp points
13 // to a saved program counter, and then the first argument.
15 // Fetch the int at addr from process p.
16 int
17 fetchint(struct proc *p, uint addr, int *ip)
19 if(addr >= p->sz || addr+4 > p->sz)
20 return -1;
21 *ip = *(int*)(addr);
22 return 0;
25 // Fetch the nul-terminated string at addr from process p.
26 // Doesn't actually copy the string - just sets *pp to point at it.
27 // Returns length of string, not including nul.
28 int
29 fetchstr(struct proc *p, uint addr, char **pp)
31 char *s, *ep;
33 if(addr >= p->sz)
34 return -1;
35 *pp = (char*)addr;
36 ep = (char*)p->sz;
37 for(s = *pp; s < ep; s++)
38 if(*s == 0)
39 return s - *pp;
40 return -1;
43 // Fetch the nth 32-bit system call argument.
44 int
45 argint(int n, int *ip)
47 return fetchint(proc, proc->tf->esp + 4 + 4*n, ip);
50 // Fetch the nth word-sized system call argument as a pointer
51 // to a block of memory of size n bytes. Check that the pointer
52 // lies within the process address space.
53 int
54 argptr(int n, char **pp, int size)
56 int i;
58 if(argint(n, &i) < 0)
59 return -1;
60 if((uint)i >= proc->sz || (uint)i+size > proc->sz)
61 return -1;
62 *pp = (char*)i;
63 return 0;
66 // Fetch the nth word-sized system call argument as a string pointer.
67 // Check that the pointer is valid and the string is nul-terminated.
68 // (There is no shared writable memory, so the string can't change
69 // between this check and being used by the kernel.)
70 int
71 argstr(int n, char **pp)
73 int addr;
74 if(argint(n, &addr) < 0)
75 return -1;
76 return fetchstr(proc, addr, pp);
79 extern int sys_chdir(void);
80 extern int sys_close(void);
81 extern int sys_dup(void);
82 extern int sys_exec(void);
83 extern int sys_exit(void);
84 extern int sys_fork(void);
85 extern int sys_fstat(void);
86 extern int sys_getpid(void);
87 extern int sys_kill(void);
88 extern int sys_link(void);
89 extern int sys_mkdir(void);
90 extern int sys_mknod(void);
91 extern int sys_open(void);
92 extern int sys_pipe(void);
93 extern int sys_read(void);
94 extern int sys_sbrk(void);
95 extern int sys_sleep(void);
96 extern int sys_unlink(void);
97 extern int sys_wait(void);
98 extern int sys_write(void);
99 extern int sys_uptime(void);
101 static int (*syscalls[])(void) = {
102 [SYS_chdir] sys_chdir,
103 [SYS_close] sys_close,
104 [SYS_dup] sys_dup,
105 [SYS_exec] sys_exec,
106 [SYS_exit] sys_exit,
107 [SYS_fork] sys_fork,
108 [SYS_fstat] sys_fstat,
109 [SYS_getpid] sys_getpid,
110 [SYS_kill] sys_kill,
111 [SYS_link] sys_link,
112 [SYS_mkdir] sys_mkdir,
113 [SYS_mknod] sys_mknod,
114 [SYS_open] sys_open,
115 [SYS_pipe] sys_pipe,
116 [SYS_read] sys_read,
117 [SYS_sbrk] sys_sbrk,
118 [SYS_sleep] sys_sleep,
119 [SYS_unlink] sys_unlink,
120 [SYS_wait] sys_wait,
121 [SYS_write] sys_write,
122 [SYS_uptime] sys_uptime,
125 void
126 syscall(void)
128 int num;
130 num = proc->tf->eax;
131 if(num >= 0 && num < NELEM(syscalls) && syscalls[num])
132 proc->tf->eax = syscalls[num]();
133 else {
134 cprintf("%d %s: unknown sys call %d\n",
135 proc->pid, proc->name, num);
136 proc->tf->eax = -1;