xv6/syscall.c

315 lines
5.3 KiB
C
Raw Normal View History

2006-06-15 16:02:20 +00:00
#include "types.h"
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "x86.h"
#include "traps.h"
#include "syscall.h"
#include "spinlock.h"
2006-06-15 16:02:20 +00:00
/*
* User code makes a system call with INT T_SYSCALL.
* System call number in %eax.
2006-06-26 15:11:19 +00:00
* Arguments on the stack, from the user call to the C
* library system call function. The saved user %esp points
* to a saved frame pointer, a program counter, and then
* the first argument.
2006-06-15 16:02:20 +00:00
*
* Return value? Error indication? Errno?
*/
2006-06-26 15:11:19 +00:00
/*
* fetch 32 bits from a user-supplied pointer.
* returns 0 if addr was OK, -1 if illegal.
2006-06-26 15:11:19 +00:00
*/
int
fetchint(struct proc *p, unsigned addr, int *ip)
{
*ip = 0;
if(addr > p->sz - 4)
return -1;
2006-06-26 15:11:19 +00:00
memcpy(ip, p->mem + addr, 4);
return 0;
2006-06-26 15:11:19 +00:00
}
int
fetcharg(int argno, int *ip)
{
unsigned esp;
esp = (unsigned) curproc[cpu()]->tf->tf_esp;
2006-07-11 01:07:40 +00:00
return fetchint(curproc[cpu()], esp + 4 + 4*argno, ip);
2006-06-26 15:11:19 +00:00
}
2006-06-27 14:35:53 +00:00
int
putint(struct proc *p, unsigned addr, int ip)
{
if(addr > p->sz - 4)
return -1;
2006-06-27 14:35:53 +00:00
memcpy(p->mem + addr, &ip, 4);
return 0;
2006-06-27 14:35:53 +00:00
}
int
2006-07-15 17:17:00 +00:00
sys_pipe(void)
2006-06-27 14:35:53 +00:00
{
struct fd *rfd = 0, *wfd = 0;
int f1 = -1, f2 = -1;
struct proc *p = curproc[cpu()];
unsigned fdp;
if(pipe_alloc(&rfd, &wfd) < 0)
goto oops;
if((f1 = fd_ualloc()) < 0)
goto oops;
p->fds[f1] = rfd;
if((f2 = fd_ualloc()) < 0)
goto oops;
p->fds[f2] = wfd;
if(fetcharg(0, &fdp) < 0)
goto oops;
if(putint(p, fdp, f1) < 0)
goto oops;
if(putint(p, fdp+4, f2) < 0)
goto oops;
return 0;
oops:
cprintf("sys_pipe failed\n");
if(rfd)
fd_close(rfd);
if(wfd)
fd_close(wfd);
if(f1 >= 0)
p->fds[f1] = 0;
if(f2 >= 0)
p->fds[f2] = 0;
return -1;
}
int
2006-07-15 17:17:00 +00:00
sys_write(void)
2006-06-27 14:35:53 +00:00
{
int fd, n, ret;
2006-06-27 14:35:53 +00:00
unsigned addr;
struct proc *p = curproc[cpu()];
if(fetcharg(0, &fd) < 0 || fetcharg(1, &addr) < 0 || fetcharg(2, &n) < 0)
return -1;
if(fd < 0 || fd >= NOFILE)
return -1;
if(p->fds[fd] == 0)
return -1;
if(addr + n > p->sz)
return -1;
ret = fd_write(p->fds[fd], p->mem + addr, n);
return ret;
2006-06-27 14:35:53 +00:00
}
int
2006-07-15 17:17:00 +00:00
sys_read(void)
2006-06-27 14:35:53 +00:00
{
int fd, n, ret;
2006-06-27 14:35:53 +00:00
unsigned addr;
struct proc *p = curproc[cpu()];
if(fetcharg(0, &fd) < 0 || fetcharg(1, &addr) < 0 || fetcharg(2, &n) < 0)
return -1;
if(fd < 0 || fd >= NOFILE)
return -1;
if(p->fds[fd] == 0)
return -1;
if(addr + n > p->sz)
return -1;
ret = fd_read(p->fds[fd], p->mem + addr, n);
return ret;
}
int
2006-07-15 17:17:00 +00:00
sys_close(void)
{
int fd;
struct proc *p = curproc[cpu()];
if(fetcharg(0, &fd) < 0)
return -1;
if(fd < 0 || fd >= NOFILE)
return -1;
if(p->fds[fd] == 0)
return -1;
fd_close(p->fds[fd]);
p->fds[fd] = 0;
return 0;
2006-06-27 14:35:53 +00:00
}
int
2006-07-15 17:17:00 +00:00
sys_fork(void)
2006-06-15 16:02:20 +00:00
{
struct proc *np;
np = newproc();
if(np){
np->state = RUNNABLE;
return np->pid;
} else {
return -1;
}
2006-06-15 16:02:20 +00:00
}
int
2006-07-15 17:17:00 +00:00
sys_exit(void)
2006-06-15 16:02:20 +00:00
{
proc_exit();
return 0;
2006-06-15 16:02:20 +00:00
}
int
2006-07-15 17:17:00 +00:00
sys_wait(void)
2006-06-15 19:58:01 +00:00
{
struct proc *p;
2006-06-22 20:47:23 +00:00
struct proc *cp = curproc[cpu()];
int any, pid;
2006-06-15 19:58:01 +00:00
acquire(&proc_table_lock);
2006-06-15 19:58:01 +00:00
while(1){
any = 0;
for(p = proc; p < &proc[NPROC]; p++){
2006-06-22 20:47:23 +00:00
if(p->state == ZOMBIE && p->ppid == cp->pid){
2006-06-15 19:58:01 +00:00
kfree(p->mem, p->sz);
kfree(p->kstack, KSTACKSIZE);
pid = p->pid;
2006-06-15 19:58:01 +00:00
p->state = UNUSED;
release(&proc_table_lock);
return pid;
2006-06-15 19:58:01 +00:00
}
2006-06-22 20:47:23 +00:00
if(p->state != UNUSED && p->ppid == cp->pid)
2006-06-15 19:58:01 +00:00
any = 1;
}
if(any == 0){
release(&proc_table_lock);
return -1;
2006-06-15 19:58:01 +00:00
}
sleep(cp, &proc_table_lock);
2006-06-15 19:58:01 +00:00
}
}
int
2006-07-15 17:17:00 +00:00
sys_cons_putc(void)
2006-06-26 15:11:19 +00:00
{
int c;
fetcharg(0, &c);
cons_putc(c & 0xff);
return 0;
2006-06-26 15:11:19 +00:00
}
int
sys_block(void)
{
2006-07-10 13:08:37 +00:00
char buf[512];
int i, j;
2006-07-10 19:06:48 +00:00
void *c;
cprintf("%d: call sys_block\n", cpu());
2006-07-10 13:08:37 +00:00
for (i = 0; i < 100; i++) {
2006-07-10 19:06:48 +00:00
if ((c = ide_start_read(i, buf, 1)) == 0) {
2006-07-10 13:08:37 +00:00
panic("couldn't start read\n");
}
cprintf("call sleep\n");
sleep (c, 0);
2006-07-10 19:06:48 +00:00
if (ide_finish_read(c)) {
2006-07-10 13:08:37 +00:00
panic("couldn't do read\n");
}
cprintf("sector %d: ", i);
for (j = 0; j < 2; j++)
cprintf("%x ", buf[j] & 0xff);
cprintf("\n");
}
return 0;
}
int
2006-07-15 17:17:00 +00:00
sys_kill(void)
{
int pid;
struct proc *p;
fetcharg(0, &pid);
acquire(&proc_table_lock);
for(p = proc; p < &proc[NPROC]; p++){
if(p->pid == pid && p->state != UNUSED){
p->killed = 1;
if(p->state == WAITING)
p->state = RUNNABLE;
release(&proc_table_lock);
return 0;
}
}
release(&proc_table_lock);
return -1;
}
int
2006-07-15 17:17:00 +00:00
sys_panic(void)
{
struct proc *p = curproc[cpu()];
unsigned int addr;
fetcharg(0, &addr);
panic(p->mem + addr);
return 0;
}
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()];
int num = cp->tf->tf_regs.reg_eax;
int ret = -1;
2006-06-15 16:02:20 +00:00
2006-06-27 14:35:53 +00:00
//cprintf("%x sys %d\n", cp, num);
2006-06-15 16:02:20 +00:00
switch(num){
case SYS_fork:
ret = sys_fork();
2006-06-15 16:02:20 +00:00
break;
case SYS_exit:
ret = sys_exit();
2006-06-15 16:02:20 +00:00
break;
2006-06-15 19:58:01 +00:00
case SYS_wait:
ret = sys_wait();
2006-06-15 19:58:01 +00:00
break;
2006-06-26 15:11:19 +00:00
case SYS_cons_putc:
ret = sys_cons_putc();
2006-06-26 15:11:19 +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;
case SYS_close:
ret = sys_close();
break;
case SYS_block:
ret = sys_block();
break;
case SYS_kill:
ret = sys_kill();
break;
case SYS_panic:
ret = sys_panic();
break;
2006-06-15 16:02:20 +00:00
default:
cprintf("unknown sys call %d\n", num);
// XXX fault
break;
}
cp->tf->tf_regs.reg_eax = ret;
2006-06-15 16:02:20 +00:00
}