-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.c
316 lines (274 loc) · 6.85 KB
/
proc.c
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "common.h"
#include "proc.h"
#include "spinlock.h"
#include "mem_layout.h"
#include "mmu.h"
#include "param.h"
extern void forkret(void);
extern void trapret(void);
//extern void startinitcode;
char *sip = 0x0;
struct {
struct spinlock lock;
struct proc proc[64]; // max 64 processes
} ptable;
int nextpid = 0;
struct proc *initproc;
struct proc *cp; // current running process
struct cpu maincpu;
struct cpu *mcpu = &maincpu;
void initptable()
{
initlock(&ptable.lock, "ptable");
kprintf("\nInitializing process table. Loc: %x", &ptable.proc);
}
static struct proc* allocproc()
{
// find an empty ptable slot
struct proc *p;
char *sp;
for (p = ptable.proc ; p < &ptable.proc[64] ; p++ ){
if(p->state == UNUSED) {
goto found;
return 0;
}
}
found:
p->state = EMBRYO;
p->pid = nextpid++;
// allocate memory for kernel_stack
if ((p->kstack = kalloc()) == 0)
PANIC("ALLOCPROC: out of memory!");
sp = p->kstack + KSTACK_SIZE;
// leave room for the trapframe
sp -= sizeof(*p->tf);
p->tf = (struct trapframe *)sp;
// Set up new context to start executing at forkret,
// which returns to trapret.
sp -= 4;
*(uint32_t*)sp = (uint32_t)trapret;
sp -= sizeof(*p->context);
p->context = (struct context*)sp;
memset(p->context, 0, sizeof *p->context);
p->context->eip = (uint32_t)forkret;
return p;
}
// Set up first user process. Copied from xv6
void userinit(void)
{
struct proc *p;
extern char _binary_initcode_out_start[], _binary_initcode_out_size[];
p = allocproc();
initproc = p;
if((p->pgdir = setupkvm()) == 0)
PANIC("userinit: out of memory?");
inituvm(p->pgdir, _binary_initcode_out_start, (int)_binary_initcode_out_size);
//kprintf("userinit: initcode was linked at: %h", sip);
p->sz = PGSIZE;
memset(p->tf, 0, sizeof(*p->tf));
p->tf->cs = (SEG_UCODE << 3) | DPL_USER;
p->tf->ds = (SEG_UDATA << 3) | DPL_USER;
p->tf->es = p->tf->ds;
p->tf->ss = p->tf->ds;
p->tf->eflags = FL_IF;
p->tf->esp = PGSIZE;
p->tf->eip = 0; // beginning of initcode.S
safestrcpy(p->name, "initcode", sizeof(p->name));
//p->cwd = namei("/");
p->state = RUNNABLE;
p->cwd = namei("/");
}
// A fork child's very first scheduling by scheduler()
// will swtch here. "Return" to user space.
void forkret(void)
{
static int first = 1;
// Still holding ptable.lock from scheduler.
release(&ptable.lock);
if (first) {
// Some initialization functions must be run in the context
// of a regular process (e.g., they call sleep), and thus cannot
// be run from main().
first = 0;
//iinit(0);
//initlog(0);
}
// Return to "caller", actually trapret (see allocproc).
kprintf("forkret: returning to trapret");
}
// Scheduler never returns. It loops, doing:
// - choose a process to run
// - swtch to start running that process
// - eventually that process transfers control
// via swtch back to the scheduler.
void scheduler(void)
{
struct proc *p;
for(;;){ // endless Loop
// Enable interrupts on this processor.
sti();
//fb_write("scheduler: IRQ enabled.\n");
// Loop over process table looking for process to run.
acquire(&ptable.lock);
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->state != RUNNABLE)
continue;
// Switch to chosen process. It is the process's job
// to release ptable.lock and then reacquire it
// before jumping back to us.
cp = p;
//fb_write("scheduler: switching context.\n");
switchuvm(p);
p->state = RUNNING;
//kprintf("scheduler: switching to process: %d\n", p->pid);
swtch(&mcpu->scheduler, cp->context);
//fb_write("it doesnt get here...");
switchkvm();
// Process is done running for now.
// It should have changed its p->state before coming back.
cp = 0;
}
release(&ptable.lock);
}
}
void sched(void)
{
if(!holding(&ptable.lock))
PANIC("sched: not holding ptable lock");
swtch(&cp->context, mcpu->scheduler); // this makes it continue in scheduler above after swtch() there...
}
void yield(void)
{
//kprintf("%d: yielding...\n", cp->pid);
acquire(&ptable.lock);
cp->state = RUNNABLE;
sched();
release(&ptable.lock);
}
int fork()
{
struct proc *np;
int k, pid;
if((np = allocproc()) == 0)
PANIC("fork: cannot allocate processes");
if((np->pgdir = copyuvm(cp->pgdir, cp->sz)) == 0){
PANIC("fork: copyuvm failed");
}
np->sz = cp->sz;
np->parent = cp;
*np->tf = *cp->tf;
// this makes fork return 0 in the child, allowing code to check if its
// executing in the parent or child.
np->tf->eax = 0;
for(k = 0 ; k < NOFILE ; k++){
np->ofile[k] = cp->ofile[k];
}
np->cwd = cp->cwd;
safestrcpy(np->name, cp->name, sizeof(cp->name));
pid = np->pid;
np->state = RUNNABLE;
return pid;
}
void sleep(void *chan, struct spinlock *lk)
{
if(lk != &ptable.lock){
acquire(&ptable.lock);
release(lk);
}
cp->chan = chan;
cp->state = SLEEPING;
sched();
cp->chan = 0;
if(lk != &ptable.lock){
release(&ptable.lock);
acquire(lk);
}
}
void wakeup(void *chan)
{
struct proc *p;
acquire(&ptable.lock);
for (p = ptable.proc ; p < &ptable.proc[64] ; p++ ){
if(p->state == SLEEPING && p->chan == chan){
kprintf("wakeup: waking: %d", p->pid);
p->state = RUNNABLE;
}
}
release(&ptable.lock);
}
// Exit the current process. Does not return.
// An exited process remains in the zombie state
// until its parent calls wait() to find out it exited.
int exit(void)
{
struct proc *pp;
acquire(&ptable.lock);
cp->state = ZOMBIE;
release(&ptable.lock);
if((pp = cp->parent) == 0)
PANIC("exit: parent not found!");
// TODO: check if this process has children that are about to be orphaned
wakeup(pp);
acquire(&ptable.lock);
sched();
}
// Wait for a child process to exit and return its pid.
// Return -1 if this process has no children.
int wait(void)
{
struct proc *p;
int haschild, pid;
acquire(&ptable.lock);
for(;;){
haschild = 0;
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++ ){
if(p->parent == cp){
haschild = 1;
if(p->state == ZOMBIE){
pid = p->pid;
p->state = UNUSED;
//release(&ptable.lock);
return pid;
//TODO: proper clear ptable and clear memory!
}
}
}
if(haschild == 0){
release(&ptable.lock);
PANIC("wait: no child!");
return -1;
}
// wait for child to exit; sleep on parent
sleep(cp, &ptable.lock);
}
PANIC("wait: returning!")
}
int growproc(int sz)
{
int nsz;
nsz = cp->sz + sz;
allocuvm(cp->pgdir, cp->sz, nsz);
cp->sz = nsz;
switchuvm(cp);
}
void procdump(void)
{
struct proc *p;
char *state;
static char *states[] = {
[UNUSED] "unused ",
[EMBRYO] "embryo ",
[SLEEPING] "sleep ",
[RUNNABLE] "ready ",
[RUNNING] "running",
[ZOMBIE] "zombie "
};
kprintf("\n\n");
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->state == UNUSED)
continue;
state = states[p->state];
kprintf("%d: %s %s\n", p->pid, state, p->name);
}
kprintf("\n");
}