2006-06-15 18:02:20 +02:00
|
|
|
#include "types.h"
|
2007-08-28 01:26:33 +02:00
|
|
|
#include "defs.h"
|
2006-06-15 18:02:20 +02:00
|
|
|
#include "param.h"
|
2011-07-29 13:31:27 +02:00
|
|
|
#include "memlayout.h"
|
2006-06-15 18:02:20 +02:00
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "x86.h"
|
|
|
|
#include "syscall.h"
|
|
|
|
|
2006-09-06 19:50:20 +02: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 18:02:20 +02:00
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
// Fetch the int at addr from process p.
|
2006-06-26 17:11:19 +02:00
|
|
|
int
|
2006-07-17 03:52:13 +02:00
|
|
|
fetchint(struct proc *p, uint addr, int *ip)
|
2006-06-26 17:11:19 +02:00
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
if(addr >= p->sz || addr+4 > p->sz)
|
2006-07-15 19:13:56 +02:00
|
|
|
return -1;
|
2010-07-02 20:51:53 +02:00
|
|
|
*ip = *(int*)(addr);
|
2006-07-16 17:38:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-07 16:13:26 +02: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 17:38:00 +02:00
|
|
|
int
|
2006-09-07 16:13:26 +02:00
|
|
|
fetchstr(struct proc *p, uint addr, char **pp)
|
2006-07-16 17:38:00 +02:00
|
|
|
{
|
2007-08-09 19:32:40 +02:00
|
|
|
char *s, *ep;
|
2006-09-07 16:13:26 +02:00
|
|
|
|
2006-07-16 17:38:00 +02:00
|
|
|
if(addr >= p->sz)
|
|
|
|
return -1;
|
2011-01-11 19:01:13 +01:00
|
|
|
*pp = (char*)addr;
|
|
|
|
ep = (char*)p->sz;
|
2007-08-09 19:32:40 +02:00
|
|
|
for(s = *pp; s < ep; s++)
|
|
|
|
if(*s == 0)
|
|
|
|
return s - *pp;
|
2006-09-07 16:13:26 +02:00
|
|
|
return -1;
|
2006-06-26 17:11:19 +02:00
|
|
|
}
|
|
|
|
|
2007-08-28 01:53:50 +02:00
|
|
|
// Fetch the nth 32-bit system call argument.
|
2006-06-26 17:11:19 +02:00
|
|
|
int
|
2007-08-28 01:53:17 +02:00
|
|
|
argint(int n, int *ip)
|
2006-06-26 17:11:19 +02:00
|
|
|
{
|
2011-01-11 19:01:13 +01:00
|
|
|
return fetchint(proc, proc->tf->esp + 4 + 4*n, ip);
|
2006-06-26 17:11:19 +02:00
|
|
|
}
|
|
|
|
|
2006-09-07 16:13:26 +02: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 23:10:00 +02:00
|
|
|
int
|
2007-08-28 01:53:17 +02:00
|
|
|
argptr(int n, char **pp, int size)
|
2006-07-27 23:10:00 +02:00
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
int i;
|
|
|
|
|
2007-08-28 01:53:17 +02:00
|
|
|
if(argint(n, &i) < 0)
|
2006-09-07 16:13:26 +02:00
|
|
|
return -1;
|
2011-02-28 15:32:20 +01:00
|
|
|
if((uint)i >= proc->sz || (uint)i+size > proc->sz)
|
2006-09-07 16:13:26 +02:00
|
|
|
return -1;
|
2011-01-11 19:01:13 +01:00
|
|
|
*pp = (char*)i;
|
2006-09-07 16:13:26 +02:00
|
|
|
return 0;
|
2006-07-27 23:10:00 +02:00
|
|
|
}
|
|
|
|
|
2006-09-07 16:13:26 +02: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 16:35:53 +02:00
|
|
|
int
|
2007-08-28 01:53:17 +02:00
|
|
|
argstr(int n, char **pp)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
int addr;
|
2007-08-28 01:53:17 +02:00
|
|
|
if(argint(n, &addr) < 0)
|
2006-07-15 19:13:56 +02:00
|
|
|
return -1;
|
2009-08-31 08:02:08 +02:00
|
|
|
return fetchstr(proc, addr, pp);
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
2006-09-06 20:19:11 +02: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);
|
|
|
|
extern int sys_open(void);
|
|
|
|
extern int sys_pipe(void);
|
|
|
|
extern int sys_read(void);
|
|
|
|
extern int sys_sbrk(void);
|
2007-08-27 15:34:35 +02:00
|
|
|
extern int sys_sleep(void);
|
2006-09-06 20:19:11 +02:00
|
|
|
extern int sys_unlink(void);
|
|
|
|
extern int sys_wait(void);
|
|
|
|
extern int sys_write(void);
|
2010-08-11 20:34:45 +02:00
|
|
|
extern int sys_uptime(void);
|
2006-07-27 23:10:00 +02:00
|
|
|
|
2007-08-08 11:41:21 +02:00
|
|
|
static int (*syscalls[])(void) = {
|
|
|
|
[SYS_fork] sys_fork,
|
2011-07-28 02:35:46 +02:00
|
|
|
[SYS_exit] sys_exit,
|
|
|
|
[SYS_wait] sys_wait,
|
2007-08-08 11:41:21 +02:00
|
|
|
[SYS_pipe] sys_pipe,
|
|
|
|
[SYS_read] sys_read,
|
2011-07-28 02:35:46 +02:00
|
|
|
[SYS_kill] sys_kill,
|
|
|
|
[SYS_exec] sys_exec,
|
|
|
|
[SYS_fstat] sys_fstat,
|
|
|
|
[SYS_chdir] sys_chdir,
|
|
|
|
[SYS_dup] sys_dup,
|
|
|
|
[SYS_getpid] sys_getpid,
|
2007-08-08 11:41:21 +02:00
|
|
|
[SYS_sbrk] sys_sbrk,
|
2007-08-27 15:34:35 +02:00
|
|
|
[SYS_sleep] sys_sleep,
|
2010-08-11 20:34:45 +02:00
|
|
|
[SYS_uptime] sys_uptime,
|
2011-07-28 02:35:46 +02:00
|
|
|
[SYS_open] sys_open,
|
|
|
|
[SYS_write] sys_write,
|
|
|
|
[SYS_mknod] sys_mknod,
|
|
|
|
[SYS_unlink] sys_unlink,
|
|
|
|
[SYS_link] sys_link,
|
|
|
|
[SYS_mkdir] sys_mkdir,
|
|
|
|
[SYS_close] sys_close,
|
2007-08-08 11:41:21 +02:00
|
|
|
};
|
|
|
|
|
2006-06-15 18:02:20 +02:00
|
|
|
void
|
2006-07-15 19:17:00 +02:00
|
|
|
syscall(void)
|
2006-06-15 18:02:20 +02:00
|
|
|
{
|
2007-08-24 22:28:08 +02:00
|
|
|
int num;
|
2011-07-28 02:35:46 +02:00
|
|
|
|
2009-08-31 08:02:08 +02:00
|
|
|
num = proc->tf->eax;
|
2011-07-28 02:35:46 +02:00
|
|
|
if(num >= 0 && num < SYS_open && syscalls[num]) {
|
|
|
|
proc->tf->eax = syscalls[num]();
|
|
|
|
} else if (num >= SYS_open && num < NELEM(syscalls) && syscalls[num]) {
|
2009-08-31 08:02:08 +02:00
|
|
|
proc->tf->eax = syscalls[num]();
|
2011-07-28 02:35:46 +02:00
|
|
|
} else {
|
2007-08-08 11:41:21 +02:00
|
|
|
cprintf("%d %s: unknown sys call %d\n",
|
2009-08-31 08:02:08 +02:00
|
|
|
proc->pid, proc->name, num);
|
|
|
|
proc->tf->eax = -1;
|
2006-06-15 18:02:20 +02:00
|
|
|
}
|
|
|
|
}
|