2006-06-15 16:02:20 +00:00
|
|
|
#include "types.h"
|
2006-08-12 04:33:50 +00:00
|
|
|
#include "stat.h"
|
2006-06-15 16:02:20 +00:00
|
|
|
#include "param.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "x86.h"
|
|
|
|
#include "traps.h"
|
|
|
|
#include "syscall.h"
|
2006-07-12 01:48:35 +00:00
|
|
|
#include "spinlock.h"
|
2006-07-21 13:18:04 +00:00
|
|
|
#include "buf.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "fsvar.h"
|
2006-07-27 21:10:00 +00:00
|
|
|
#include "elf.h"
|
2006-09-06 18:40:28 +00:00
|
|
|
#include "file.h"
|
2006-08-10 02:07:10 +00:00
|
|
|
#include "fcntl.h"
|
2006-06-15 16:02:20 +00:00
|
|
|
|
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
|
|
|
{
|
2006-09-07 14:13:26 +00:00
|
|
|
char *cp, *ep;
|
|
|
|
|
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;
|
|
|
|
for(cp = *pp; cp < ep; cp++)
|
|
|
|
if(*cp == 0)
|
|
|
|
return cp - *pp;
|
|
|
|
return -1;
|
2006-06-26 15:11:19 +00:00
|
|
|
}
|
|
|
|
|
2006-09-07 14:13:26 +00:00
|
|
|
// Fetch the argno'th word-sized system call argument as an integer.
|
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
|
|
|
{
|
2006-09-07 14:13:26 +00:00
|
|
|
struct proc *p = curproc[cpu()];
|
2006-06-26 15:11:19 +00:00
|
|
|
|
2006-09-07 14:13:26 +00:00
|
|
|
return fetchint(p, p->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;
|
|
|
|
struct proc *p = curproc[cpu()];
|
|
|
|
|
|
|
|
if(argint(argno, &i) < 0)
|
|
|
|
return -1;
|
|
|
|
if((uint)i >= p->sz || (uint)i+size >= p->sz)
|
|
|
|
return -1;
|
|
|
|
*pp = p->mem + i;
|
|
|
|
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;
|
2006-09-07 14:13:26 +00:00
|
|
|
return fetchstr(curproc[cpu()], 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);
|
|
|
|
extern int sys_open(void);
|
|
|
|
extern int sys_pipe(void);
|
|
|
|
extern int sys_read(void);
|
|
|
|
extern int sys_sbrk(void);
|
|
|
|
extern int sys_unlink(void);
|
|
|
|
extern int sys_wait(void);
|
|
|
|
extern int sys_write(void);
|
2006-07-27 21:10:00 +00:00
|
|
|
|
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
|
|
|
{
|
2006-06-22 20:47:23 +00:00
|
|
|
struct proc *cp = curproc[cpu()];
|
2006-07-17 01:36:39 +00:00
|
|
|
int num = cp->tf->eax;
|
2006-06-26 20:31:52 +00:00
|
|
|
int ret = -1;
|
2006-06-15 16:02:20 +00:00
|
|
|
|
|
|
|
switch(num){
|
|
|
|
case SYS_fork:
|
2006-06-26 20:31:52 +00:00
|
|
|
ret = sys_fork();
|
2006-06-15 16:02:20 +00:00
|
|
|
break;
|
|
|
|
case SYS_exit:
|
2006-06-26 20:31:52 +00:00
|
|
|
ret = sys_exit();
|
2006-06-15 16:02:20 +00:00
|
|
|
break;
|
2006-06-15 19:58:01 +00:00
|
|
|
case SYS_wait:
|
2006-06-26 20:31:52 +00:00
|
|
|
ret = sys_wait();
|
2006-06-15 19:58:01 +00:00
|
|
|
break;
|
2006-06-27 14:35:53 +00:00
|
|
|
case SYS_pipe:
|
|
|
|
ret = sys_pipe();
|
|
|
|
break;
|
|
|
|
case SYS_write:
|
|
|
|
ret = sys_write();
|
|
|
|
break;
|
|
|
|
case SYS_read:
|
|
|
|
ret = sys_read();
|
|
|
|
break;
|
2006-07-01 21:26:01 +00:00
|
|
|
case SYS_close:
|
|
|
|
ret = sys_close();
|
|
|
|
break;
|
2006-07-11 17:39:45 +00:00
|
|
|
case SYS_kill:
|
|
|
|
ret = sys_kill();
|
|
|
|
break;
|
2006-07-27 21:10:00 +00:00
|
|
|
case SYS_exec:
|
|
|
|
ret = sys_exec();
|
|
|
|
break;
|
2006-07-29 09:35:02 +00:00
|
|
|
case SYS_open:
|
|
|
|
ret = sys_open();
|
|
|
|
break;
|
2006-08-08 18:07:37 +00:00
|
|
|
case SYS_mknod:
|
|
|
|
ret = sys_mknod();
|
|
|
|
break;
|
2006-08-10 01:28:57 +00:00
|
|
|
case SYS_unlink:
|
|
|
|
ret = sys_unlink();
|
|
|
|
break;
|
2006-08-12 04:33:50 +00:00
|
|
|
case SYS_fstat:
|
|
|
|
ret = sys_fstat();
|
|
|
|
break;
|
2006-08-13 02:12:44 +00:00
|
|
|
case SYS_link:
|
|
|
|
ret = sys_link();
|
|
|
|
break;
|
2006-08-14 03:00:13 +00:00
|
|
|
case SYS_mkdir:
|
|
|
|
ret = sys_mkdir();
|
|
|
|
break;
|
2006-08-19 23:41:34 +00:00
|
|
|
case SYS_chdir:
|
|
|
|
ret = sys_chdir();
|
|
|
|
break;
|
2006-08-20 03:08:54 +00:00
|
|
|
case SYS_dup:
|
|
|
|
ret = sys_dup();
|
|
|
|
break;
|
2006-08-23 01:09:24 +00:00
|
|
|
case SYS_getpid:
|
|
|
|
ret = sys_getpid();
|
|
|
|
break;
|
|
|
|
case SYS_sbrk:
|
|
|
|
ret = sys_sbrk();
|
|
|
|
break;
|
2006-06-15 16:02:20 +00:00
|
|
|
default:
|
|
|
|
cprintf("unknown sys call %d\n", num);
|
2006-09-07 13:07:52 +00:00
|
|
|
// Maybe kill the process?
|
2006-06-15 16:02:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-07-17 01:36:39 +00:00
|
|
|
cp->tf->eax = ret;
|
2006-06-15 16:02:20 +00:00
|
|
|
}
|