2006-06-15 16:02:20 +00:00
|
|
|
#include "types.h"
|
2007-08-27 23:26:33 +00:00
|
|
|
#include "defs.h"
|
2006-06-15 16:02:20 +00:00
|
|
|
#include "param.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "x86.h"
|
|
|
|
#include "syscall.h"
|
|
|
|
|
2006-09-06 17:50:20 +00:00
|
|
|
// 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.
|
2006-06-15 16:02:20 +00:00
|
|
|
|
2006-09-07 14:13:26 +00:00
|
|
|
// Fetch the int at addr from process p.
|
2006-06-26 15:11:19 +00:00
|
|
|
int
|
2006-07-17 01:52:13 +00:00
|
|
|
fetchint(struct proc *p, uint addr, int *ip)
|
2006-06-26 15:11:19 +00:00
|
|
|
{
|
2006-09-07 14:13:26 +00:00
|
|
|
if(addr >= p->sz || addr+4 > p->sz)
|
2006-07-15 17:13:56 +00:00
|
|
|
return -1;
|
2006-07-16 15:38:00 +00:00
|
|
|
*ip = *(int*)(p->mem + addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-07 14:13:26 +00:00
|
|
|
// 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.
|
2006-07-16 15:38:00 +00:00
|
|
|
int
|
2006-09-07 14:13:26 +00:00
|
|
|
fetchstr(struct proc *p, uint addr, char **pp)
|
2006-07-16 15:38:00 +00:00
|
|
|
{
|
2007-08-09 17:32:40 +00:00
|
|
|
char *s, *ep;
|
2006-09-07 14:13:26 +00:00
|
|
|
|
2006-07-16 15:38:00 +00:00
|
|
|
if(addr >= p->sz)
|
|
|
|
return -1;
|
2006-09-07 14:13:26 +00:00
|
|
|
*pp = p->mem + addr;
|
|
|
|
ep = p->mem + p->sz;
|
2007-08-09 17:32:40 +00:00
|
|
|
for(s = *pp; s < ep; s++)
|
|
|
|
if(*s == 0)
|
|
|
|
return s - *pp;
|
2006-09-07 14:13:26 +00:00
|
|
|
return -1;
|
2006-06-26 15:11:19 +00:00
|
|
|
}
|
|
|
|
|
2007-08-24 19:32:36 +00:00
|
|
|
// Fetch the argno'th 32-bit system call argument.
|
2006-06-26 15:11:19 +00:00
|
|
|
int
|
2006-09-07 14:13:26 +00:00
|
|
|
argint(int argno, int *ip)
|
2006-06-26 15:11:19 +00:00
|
|
|
{
|
2007-08-09 17:32:40 +00:00
|
|
|
return fetchint(cp, cp->tf->esp + 4 + 4*argno, ip);
|
2006-06-26 15:11:19 +00:00
|
|
|
}
|
|
|
|
|
2006-09-07 14:13:26 +00:00
|
|
|
// 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.
|
2006-07-27 21:10:00 +00:00
|
|
|
int
|
2006-09-07 14:13:26 +00:00
|
|
|
argptr(int argno, char **pp, int size)
|
2006-07-27 21:10:00 +00:00
|
|
|
{
|
2006-09-07 14:13:26 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if(argint(argno, &i) < 0)
|
|
|
|
return -1;
|
2007-08-09 17:32:40 +00:00
|
|
|
if((uint)i >= cp->sz || (uint)i+size >= cp->sz)
|
2006-09-07 14:13:26 +00:00
|
|
|
return -1;
|
2007-08-09 17:32:40 +00:00
|
|
|
*pp = cp->mem + i;
|
2006-09-07 14:13:26 +00:00
|
|
|
return 0;
|
2006-07-27 21:10:00 +00:00
|
|
|
}
|
|
|
|
|
2006-09-07 14:13:26 +00:00
|
|
|
// 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.)
|
2006-06-27 14:35:53 +00:00
|
|
|
int
|
2006-09-07 14:13:26 +00:00
|
|
|
argstr(int argno, char **pp)
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
2006-09-07 14:13:26 +00:00
|
|
|
int addr;
|
|
|
|
if(argint(argno, &addr) < 0)
|
2006-07-15 17:13:56 +00:00
|
|
|
return -1;
|
2007-08-10 16:37:27 +00:00
|
|
|
return fetchstr(cp, addr, pp);
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
|
|
|
|
2006-09-06 18:19:11 +00:00
|
|
|
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);
|
2007-08-14 18:42:34 +00:00
|
|
|
//PAGEBREAK: 0
|
2006-09-06 18:19:11 +00:00
|
|
|
extern int sys_open(void);
|
|
|
|
extern int sys_pipe(void);
|
|
|
|
extern int sys_read(void);
|
|
|
|
extern int sys_sbrk(void);
|
2007-08-27 13:34:35 +00:00
|
|
|
extern int sys_sleep(void);
|
2006-09-06 18:19:11 +00:00
|
|
|
extern int sys_unlink(void);
|
|
|
|
extern int sys_wait(void);
|
|
|
|
extern int sys_write(void);
|
2006-07-27 21:10:00 +00:00
|
|
|
|
2007-08-08 09:41:21 +00:00
|
|
|
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,
|
2007-08-27 13:34:35 +00:00
|
|
|
[SYS_sleep] sys_sleep,
|
2007-08-08 09:41:21 +00:00
|
|
|
[SYS_unlink] sys_unlink,
|
|
|
|
[SYS_wait] sys_wait,
|
|
|
|
[SYS_write] sys_write,
|
|
|
|
};
|
|
|
|
|
2006-06-15 16:02:20 +00:00
|
|
|
void
|
2006-07-15 17:17:00 +00:00
|
|
|
syscall(void)
|
2006-06-15 16:02:20 +00:00
|
|
|
{
|
2007-08-24 20:28:08 +00:00
|
|
|
int num;
|
|
|
|
|
|
|
|
num = cp->tf->eax;
|
2007-08-08 09:41:21 +00:00
|
|
|
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;
|
2006-06-15 16:02:20 +00:00
|
|
|
}
|
|
|
|
}
|