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.
17 fetchint(struct proc
*p
, uint addr
, int *ip
)
19 if(addr
>= p
->sz
|| addr
+4 > p
->sz
)
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.
29 fetchstr(struct proc
*p
, uint addr
, char **pp
)
37 for(s
= *pp
; s
< ep
; s
++)
43 // Fetch the nth 32-bit system call argument.
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.
54 argptr(int n
, char **pp
, int size
)
60 if((uint
)i
>= proc
->sz
|| (uint
)i
+size
> proc
->sz
)
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.)
71 argstr(int n
, char **pp
)
74 if(argint(n
, &addr
) < 0)
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
,
108 [SYS_fstat
] sys_fstat
,
109 [SYS_getpid
] sys_getpid
,
112 [SYS_mkdir
] sys_mkdir
,
113 [SYS_mknod
] sys_mknod
,
118 [SYS_sleep
] sys_sleep
,
119 [SYS_unlink
] sys_unlink
,
121 [SYS_write
] sys_write
,
122 [SYS_uptime
] sys_uptime
,
131 if(num
>= 0 && num
< NELEM(syscalls
) && syscalls
[num
])
132 proc
->tf
->eax
= syscalls
[num
]();
134 cprintf("%d %s: unknown sys call %d\n",
135 proc
->pid
, proc
->name
, num
);