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.
|
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 1 if addr was OK, 0 if illegal.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
fetchint(struct proc *p, unsigned addr, int *ip)
|
|
|
|
{
|
|
|
|
*ip = 0;
|
|
|
|
|
|
|
|
if(addr > p->sz - 4)
|
|
|
|
return 0;
|
|
|
|
memcpy(ip, p->mem + addr, 4);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fetcharg(int argno, int *ip)
|
|
|
|
{
|
|
|
|
unsigned esp;
|
|
|
|
|
|
|
|
esp = (unsigned) curproc[cpu()]->tf->tf_esp;
|
|
|
|
return fetchint(curproc[cpu()], esp + 8 + 4*argno, ip);
|
|
|
|
}
|
|
|
|
|
2006-06-26 20:31:52 +00:00
|
|
|
int
|
2006-06-15 16:02:20 +00:00
|
|
|
sys_fork()
|
|
|
|
{
|
2006-06-26 20:31:52 +00:00
|
|
|
struct proc *np;
|
|
|
|
|
|
|
|
np = newproc();
|
|
|
|
return np->pid;
|
2006-06-15 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2006-06-26 20:31:52 +00:00
|
|
|
int
|
2006-06-15 16:02:20 +00:00
|
|
|
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-26 20:31:52 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-06-15 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2006-06-26 20:31:52 +00:00
|
|
|
int
|
2006-06-15 19:58:01 +00:00
|
|
|
sys_wait()
|
|
|
|
{
|
|
|
|
struct proc *p;
|
2006-06-22 20:47:23 +00:00
|
|
|
struct proc *cp = curproc[cpu()];
|
2006-06-26 20:31:52 +00:00
|
|
|
int any, pid;
|
2006-06-15 19:58:01 +00:00
|
|
|
|
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);
|
2006-06-26 20:31:52 +00:00
|
|
|
pid = p->pid;
|
2006-06-15 19:58:01 +00:00
|
|
|
p->state = UNUSED;
|
2006-06-22 20:47:23 +00:00
|
|
|
cprintf("%x collected %x\n", cp, p);
|
2006-06-26 20:31:52 +00:00
|
|
|
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){
|
2006-06-22 20:47:23 +00:00
|
|
|
cprintf("%x nothing to wait for\n", cp);
|
2006-06-26 20:31:52 +00:00
|
|
|
return -1;
|
2006-06-15 19:58:01 +00:00
|
|
|
}
|
2006-06-22 20:47:23 +00:00
|
|
|
sleep(cp);
|
2006-06-15 19:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-26 20:31:52 +00:00
|
|
|
int
|
2006-06-26 15:11:19 +00:00
|
|
|
sys_cons_putc()
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
fetcharg(0, &c);
|
|
|
|
cons_putc(c & 0xff);
|
2006-06-26 20:31:52 +00:00
|
|
|
return 0;
|
2006-06-26 15:11:19 +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-26 20:31:52 +00:00
|
|
|
int ret = -1;
|
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:
|
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-26 15:11:19 +00:00
|
|
|
case SYS_cons_putc:
|
2006-06-26 20:31:52 +00:00
|
|
|
ret = sys_cons_putc();
|
2006-06-26 15:11:19 +00:00
|
|
|
break;
|
2006-06-15 16:02:20 +00:00
|
|
|
default:
|
|
|
|
cprintf("unknown sys call %d\n", num);
|
|
|
|
// XXX fault
|
|
|
|
break;
|
|
|
|
}
|
2006-06-26 20:31:52 +00:00
|
|
|
cp->tf->tf_regs.reg_eax = ret;
|
2006-06-15 16:02:20 +00:00
|
|
|
}
|