xv6/syscall.c

98 lines
1.6 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"
/*
* User code makes a system call with INT T_SYSCALL.
* System call number in %eax.
* Arguments on the stack.
*
* Return value? Error indication? Errno?
*/
void
sys_fork()
{
newproc();
}
void
sys_exit()
{
2006-06-15 19:58:01 +00:00
struct proc *p;
2006-06-22 20:47:23 +00:00
struct proc *cp = curproc[cpu()];
2006-06-15 19:58:01 +00:00
2006-06-22 20:47:23 +00:00
cp->state = ZOMBIE;
2006-06-15 19:58:01 +00:00
// wake up parent
for(p = proc; p < &proc[NPROC]; p++)
2006-06-22 20:47:23 +00:00
if(p->pid == cp->ppid)
2006-06-15 19:58:01 +00:00
wakeup(p);
// abandon children
for(p = proc; p < &proc[NPROC]; p++)
2006-06-22 20:47:23 +00:00
if(p->ppid == cp->pid)
2006-06-15 19:58:01 +00:00
p->pid = 1;
2006-06-15 16:02:20 +00:00
swtch();
}
2006-06-15 19:58:01 +00:00
void
sys_wait()
{
struct proc *p;
2006-06-22 20:47:23 +00:00
struct proc *cp = curproc[cpu()];
2006-06-15 19:58:01 +00:00
int any;
2006-06-22 20:47:23 +00:00
cprintf("waid pid %d ppid %d\n", cp->pid, cp->ppid);
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);
p->state = UNUSED;
2006-06-22 20:47:23 +00:00
cprintf("%x collected %x\n", cp, p);
2006-06-15 19:58:01 +00:00
return;
}
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){
2006-06-22 20:47:23 +00:00
cprintf("%x nothing to wait for\n", cp);
2006-06-15 19:58:01 +00:00
return;
}
2006-06-22 20:47:23 +00:00
sleep(cp);
2006-06-15 19:58:01 +00:00
}
}
2006-06-15 16:02:20 +00:00
void
syscall()
{
2006-06-22 20:47:23 +00:00
struct proc *cp = curproc[cpu()];
int num = cp->tf->tf_regs.reg_eax;
2006-06-15 16:02:20 +00:00
2006-06-22 20:47:23 +00:00
cprintf("%x sys %d\n", cp, num);
2006-06-15 16:02:20 +00:00
switch(num){
case SYS_fork:
sys_fork();
break;
case SYS_exit:
sys_exit();
break;
2006-06-15 19:58:01 +00:00
case SYS_wait:
sys_wait();
break;
2006-06-15 16:02:20 +00:00
default:
cprintf("unknown sys call %d\n", num);
// XXX fault
break;
}
}