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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
#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, 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.
*
* Return value? Error indication? Errno?
*/
/*
* 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);
}
void
sys_fork()
{
newproc();
}
void
sys_exit()
{
struct proc *p;
struct proc *cp = curproc[cpu()];
cp->state = ZOMBIE;
// wake up parent
for(p = proc; p < &proc[NPROC]; p++)
if(p->pid == cp->ppid)
wakeup(p);
// abandon children
for(p = proc; p < &proc[NPROC]; p++)
if(p->ppid == cp->pid)
p->pid = 1;
swtch();
}
void
sys_wait()
{
struct proc *p;
struct proc *cp = curproc[cpu()];
int any;
cprintf("waid pid %d ppid %d\n", cp->pid, cp->ppid);
while(1){
any = 0;
for(p = proc; p < &proc[NPROC]; p++){
if(p->state == ZOMBIE && p->ppid == cp->pid){
kfree(p->mem, p->sz);
kfree(p->kstack, KSTACKSIZE);
p->state = UNUSED;
cprintf("%x collected %x\n", cp, p);
return;
}
if(p->state != UNUSED && p->ppid == cp->pid)
any = 1;
}
if(any == 0){
cprintf("%x nothing to wait for\n", cp);
return;
}
sleep(cp);
}
}
void
sys_cons_putc()
{
int c;
fetcharg(0, &c);
cons_putc(c & 0xff);
}
void
syscall()
{
struct proc *cp = curproc[cpu()];
int num = cp->tf->tf_regs.reg_eax;
cprintf("%x sys %d\n", cp, num);
switch(num){
case SYS_fork:
sys_fork();
break;
case SYS_exit:
sys_exit();
break;
case SYS_wait:
sys_wait();
break;
case SYS_cons_putc:
sys_cons_putc();
break;
default:
cprintf("unknown sys call %d\n", num);
// XXX fault
break;
}
}
|