summaryrefslogtreecommitdiff
path: root/syscall.c
blob: 1968c8afaeedb3de1833c90d457d6b07ef716538 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#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()
{
  struct proc *p;

  curproc->state = ZOMBIE;

  // wake up parent
  for(p = proc; p < &proc[NPROC]; p++)
    if(p->pid == curproc->ppid)
      wakeup(p);

  // abandon children
  for(p = proc; p < &proc[NPROC]; p++)
    if(p->ppid == curproc->pid)
      p->pid = 1;

  swtch();
}

void
sys_wait()
{
  struct proc *p;
  int any;

  cprintf("waid pid %d ppid %d\n", curproc->pid, curproc->ppid);

  while(1){
    any = 0;
    for(p = proc; p < &proc[NPROC]; p++){
      if(p->state == ZOMBIE && p->ppid == curproc->pid){
        kfree(p->mem, p->sz);
        kfree(p->kstack, KSTACKSIZE);
        p->state = UNUSED;
        cprintf("%x collected %x\n", curproc, p);
        return;
      }
      if(p->state != UNUSED && p->ppid == curproc->pid)
        any = 1;
    }
    if(any == 0){
      cprintf("%x nothing to wait for\n", curproc);
      return;
    }
    sleep(curproc);
  }
}

void
syscall()
{
  int num = curproc->tf->tf_regs.reg_eax;

  cprintf("%x sys %d\n", curproc, num);
  switch(num){
  case SYS_fork:
    sys_fork();
    break;
  case SYS_exit:
    sys_exit();
    break;
  case SYS_wait:
    sys_wait();
    break;
  default:
    cprintf("unknown sys call %d\n", num);
    // XXX fault
    break;
  }
}