0
|
1 #include "types.h"
|
|
2 #include "defs.h"
|
|
3 #include "param.h"
|
|
4 #include "memlayout.h"
|
|
5 #include "mmu.h"
|
|
6 #include "arm.h"
|
|
7 #include "proc.h"
|
|
8 #include "spinlock.h"
|
|
9
|
|
10 //
|
|
11 // Process initialization:
|
|
12 // process initialize is somewhat tricky.
|
|
13 // 1. We need to fake the kernel stack of a new process as if the process
|
|
14 // has been interrupt (a trapframe on the stack), this would allow us
|
|
15 // to "return" to the correct user instruction.
|
|
16 // 2. We also need to fake the kernel execution for this new process. When
|
|
17 // swtch switches to this (new) process, it will switch to its stack,
|
|
18 // and reload registers with the saved context. We use forkret as the
|
|
19 // return address (in lr register). (In x86, it will be the return address
|
|
20 // pushed on the stack by the process.)
|
|
21 //
|
|
22 // The design of context switch in xv6 is interesting: after initialization,
|
|
23 // each CPU executes in the scheduler() function. The context switch is not
|
|
24 // between two processes, but instead, between the scheduler. Think of scheduler
|
|
25 // as the idle process.
|
|
26 //
|
|
27 struct {
|
|
28 struct spinlock lock;
|
|
29 struct proc proc[NPROC];
|
|
30 } ptable;
|
|
31
|
|
32 static struct proc *initproc;
|
|
33 struct proc *proc;
|
|
34
|
|
35 int nextpid = 1;
|
|
36 extern void forkret(void);
|
|
37 extern void trapret(void);
|
|
38
|
|
39 static void wakeup1(void *chan);
|
|
40
|
|
41 void pinit(void)
|
|
42 {
|
|
43 initlock(&ptable.lock, "ptable");
|
|
44 }
|
|
45
|
|
46 //PAGEBREAK: 32
|
|
47 // Look in the process table for an UNUSED proc.
|
|
48 // If found, change state to EMBRYO and initialize
|
|
49 // state required to run in the kernel.
|
|
50 // Otherwise return 0.
|
|
51 static struct proc* allocproc(void)
|
|
52 {
|
|
53 struct proc *p;
|
|
54 char *sp;
|
|
55
|
|
56 acquire(&ptable.lock);
|
|
57
|
|
58 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++) {
|
|
59 if(p->state == UNUSED) {
|
|
60 goto found;
|
|
61 }
|
|
62
|
|
63 }
|
|
64
|
|
65 release(&ptable.lock);
|
|
66 return 0;
|
|
67
|
|
68 found:
|
|
69 p->state = EMBRYO;
|
|
70 p->pid = nextpid++;
|
|
71 release(&ptable.lock);
|
|
72
|
|
73 // Allocate kernel stack.
|
|
74 if((p->kstack = alloc_page ()) == 0){
|
|
75 p->state = UNUSED;
|
|
76 return 0;
|
|
77 }
|
|
78
|
|
79 sp = p->kstack + KSTACKSIZE;
|
|
80
|
|
81 // Leave room for trap frame.
|
|
82 sp -= sizeof (*p->tf);
|
|
83 p->tf = (struct trapframe*)sp;
|
|
84
|
|
85 // Set up new context to start executing at forkret,
|
|
86 // which returns to trapret.
|
|
87 sp -= 4;
|
|
88 *(uint*)sp = (uint)trapret;
|
|
89
|
|
90 sp -= 4;
|
|
91 *(uint*)sp = (uint)p->kstack + KSTACKSIZE;
|
|
92
|
|
93 sp -= sizeof (*p->context);
|
|
94 p->context = (struct context*)sp;
|
|
95 memset(p->context, 0, sizeof(*p->context));
|
|
96
|
|
97 // skip the push {fp, lr} instruction in the prologue of forkret.
|
|
98 // This is different from x86, in which the harderware pushes return
|
|
99 // address before executing the callee. In ARM, return address is
|
|
100 // loaded into the lr register, and push to the stack by the callee
|
|
101 // (if and when necessary). We need to skip that instruction and let
|
|
102 // it use our implementation.
|
|
103 p->context->lr = (uint)forkret+4;
|
|
104
|
|
105 return p;
|
|
106 }
|
|
107
|
|
108 void error_init ()
|
|
109 {
|
|
110 panic ("failed to craft first process\n");
|
|
111 }
|
|
112
|
|
113
|
|
114 //PAGEBREAK: 32
|
|
115 // hand-craft the first user process. We link initcode.S into the kernel
|
|
116 // as a binary, the linker will generate __binary_initcode_start/_size
|
|
117 void userinit(void)
|
|
118 {
|
|
119 struct proc *p;
|
|
120 extern char _binary_initcode_start[], _binary_initcode_size[];
|
|
121
|
|
122 p = allocproc();
|
|
123 initproc = p;
|
|
124
|
|
125 if((p->pgdir = kpt_alloc()) == NULL) {
|
|
126 panic("userinit: out of memory?");
|
|
127 }
|
|
128
|
|
129 inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
|
|
130
|
|
131 p->sz = PTE_SZ;
|
|
132
|
|
133 // craft the trapframe as if
|
|
134 memset(p->tf, 0, sizeof(*p->tf));
|
|
135
|
|
136 p->tf->r14_svc = (uint)error_init;
|
|
137 p->tf->spsr = spsr_usr ();
|
|
138 p->tf->sp_usr = PTE_SZ; // set the user stack
|
|
139 p->tf->lr_usr = 0;
|
|
140
|
|
141 // set the user pc. The actual pc loaded into r15_usr is in
|
|
142 // p->tf, the trapframe.
|
|
143 p->tf->pc = 0; // beginning of initcode.S
|
|
144
|
|
145 safestrcpy(p->name, "initcode", sizeof(p->name));
|
|
146 p->cwd = namei("/");
|
|
147
|
|
148 p->state = RUNNABLE;
|
|
149 }
|
|
150
|
|
151 // Grow current process's memory by n bytes.
|
|
152 // Return 0 on success, -1 on failure.
|
|
153 int growproc(int n)
|
|
154 {
|
|
155 uint sz;
|
|
156
|
|
157 sz = proc->sz;
|
|
158
|
|
159 if(n > 0){
|
|
160 if((sz = allocuvm(proc->pgdir, sz, sz + n)) == 0) {
|
|
161 return -1;
|
|
162 }
|
|
163
|
|
164 } else if(n < 0){
|
|
165 if((sz = deallocuvm(proc->pgdir, sz, sz + n)) == 0) {
|
|
166 return -1;
|
|
167 }
|
|
168 }
|
|
169
|
|
170 proc->sz = sz;
|
|
171 switchuvm(proc);
|
|
172
|
|
173 return 0;
|
|
174 }
|
|
175
|
|
176 // Create a new process copying p as the parent.
|
|
177 // Sets up stack to return as if from system call.
|
|
178 // Caller must set state of returned proc to RUNNABLE.
|
|
179 int fork(void)
|
|
180 {
|
|
181 int i, pid;
|
|
182 struct proc *np;
|
|
183
|
|
184 // Allocate process.
|
|
185 if((np = allocproc()) == 0) {
|
|
186 return -1;
|
|
187 }
|
|
188
|
|
189 // Copy process state from p.
|
|
190 if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
|
|
191 free_page(np->kstack);
|
|
192 np->kstack = 0;
|
|
193 np->state = UNUSED;
|
|
194 return -1;
|
|
195 }
|
|
196
|
|
197 np->sz = proc->sz;
|
|
198 np->parent = proc;
|
19
|
199 // *np->tf = *proc->tf; // This generate memcpy4 which is not in libgcc.a
|
|
200 memmove(np->tf, proc->tf, sizeof(*np->tf));
|
0
|
201
|
|
202 // Clear r0 so that fork returns 0 in the child.
|
|
203 np->tf->r0 = 0;
|
|
204
|
|
205 for(i = 0; i < NOFILE; i++) {
|
|
206 if(proc->ofile[i]) {
|
|
207 np->ofile[i] = filedup(proc->ofile[i]);
|
|
208 }
|
|
209 }
|
|
210
|
|
211 np->cwd = idup(proc->cwd);
|
|
212
|
|
213 pid = np->pid;
|
|
214 np->state = RUNNABLE;
|
|
215 safestrcpy(np->name, proc->name, sizeof(proc->name));
|
|
216
|
|
217 return pid;
|
|
218 }
|
|
219
|
|
220 // Exit the current process. Does not return.
|
|
221 // An exited process remains in the zombie state
|
|
222 // until its parent calls wait() to find out it exited.
|
|
223 void exit(void)
|
|
224 {
|
|
225 struct proc *p;
|
|
226 int fd;
|
|
227
|
|
228 if(proc == initproc) {
|
|
229 panic("init exiting");
|
|
230 }
|
|
231
|
|
232 // Close all open files.
|
|
233 for(fd = 0; fd < NOFILE; fd++){
|
|
234 if(proc->ofile[fd]){
|
|
235 fileclose(proc->ofile[fd]);
|
|
236 proc->ofile[fd] = 0;
|
|
237 }
|
|
238 }
|
|
239
|
|
240 iput(proc->cwd);
|
|
241 proc->cwd = 0;
|
|
242
|
|
243 acquire(&ptable.lock);
|
|
244
|
|
245 // Parent might be sleeping in wait().
|
|
246 wakeup1(proc->parent);
|
|
247
|
|
248 // Pass abandoned children to init.
|
|
249 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
|
|
250 if(p->parent == proc){
|
|
251 p->parent = initproc;
|
|
252
|
|
253 if(p->state == ZOMBIE) {
|
|
254 wakeup1(initproc);
|
|
255 }
|
|
256 }
|
|
257 }
|
|
258
|
|
259 // Jump into the scheduler, never to return.
|
|
260 proc->state = ZOMBIE;
|
|
261 sched();
|
|
262
|
|
263 panic("zombie exit");
|
|
264 }
|
|
265
|
|
266 // Wait for a child process to exit and return its pid.
|
|
267 // Return -1 if this process has no children.
|
|
268 int wait(void)
|
|
269 {
|
|
270 struct proc *p;
|
|
271 int havekids, pid;
|
|
272
|
|
273 acquire(&ptable.lock);
|
|
274
|
|
275 for(;;){
|
|
276 // Scan through table looking for zombie children.
|
|
277 havekids = 0;
|
|
278
|
|
279 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
|
|
280 if(p->parent != proc) {
|
|
281 continue;
|
|
282 }
|
|
283
|
|
284 havekids = 1;
|
|
285
|
|
286 if(p->state == ZOMBIE){
|
|
287 // Found one.
|
|
288 pid = p->pid;
|
|
289 free_page(p->kstack);
|
|
290 p->kstack = 0;
|
|
291 freevm(p->pgdir);
|
|
292 p->state = UNUSED;
|
|
293 p->pid = 0;
|
|
294 p->parent = 0;
|
|
295 p->name[0] = 0;
|
|
296 p->killed = 0;
|
|
297 release(&ptable.lock);
|
|
298
|
|
299 return pid;
|
|
300 }
|
|
301 }
|
|
302
|
|
303 // No point waiting if we don't have any children.
|
|
304 if(!havekids || proc->killed){
|
|
305 release(&ptable.lock);
|
|
306 return -1;
|
|
307 }
|
|
308
|
|
309 // Wait for children to exit. (See wakeup1 call in proc_exit.)
|
|
310 sleep(proc, &ptable.lock); //DOC: wait-sleep
|
|
311 }
|
|
312 }
|
|
313
|
|
314 //PAGEBREAK: 42
|
|
315 // Per-CPU process scheduler.
|
|
316 // Each CPU calls scheduler() after setting itself up.
|
|
317 // Scheduler never returns. It loops, doing:
|
|
318 // - choose a process to run
|
|
319 // - swtch to start running that process
|
|
320 // - eventually that process transfers control
|
|
321 // via swtch back to the scheduler.
|
|
322 void scheduler(void)
|
|
323 {
|
|
324 struct proc *p;
|
|
325
|
|
326 for(;;){
|
|
327 // Enable interrupts on this processor.
|
|
328 sti();
|
|
329
|
|
330 // Loop over process table looking for process to run.
|
|
331 acquire(&ptable.lock);
|
|
332
|
|
333 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
|
|
334 if(p->state != RUNNABLE) {
|
|
335 continue;
|
|
336 }
|
|
337
|
|
338 // Switch to chosen process. It is the process's job
|
|
339 // to release ptable.lock and then reacquire it
|
|
340 // before jumping back to us.
|
|
341 proc = p;
|
|
342 switchuvm(p);
|
|
343
|
|
344 p->state = RUNNING;
|
|
345
|
|
346 swtch(&cpu->scheduler, proc->context);
|
|
347 // Process is done running for now.
|
|
348 // It should have changed its p->state before coming back.
|
|
349 proc = 0;
|
|
350 }
|
|
351
|
|
352 release(&ptable.lock);
|
|
353 }
|
|
354 }
|
|
355
|
|
356 // Enter scheduler. Must hold only ptable.lock
|
|
357 // and have changed proc->state.
|
|
358 void sched(void)
|
|
359 {
|
|
360 int intena;
|
|
361
|
|
362 //show_callstk ("sched");
|
|
363
|
|
364 if(!holding(&ptable.lock)) {
|
|
365 panic("sched ptable.lock");
|
|
366 }
|
|
367
|
|
368 if(cpu->ncli != 1) {
|
|
369 panic("sched locks");
|
|
370 }
|
|
371
|
|
372 if(proc->state == RUNNING) {
|
|
373 panic("sched running");
|
|
374 }
|
|
375
|
|
376 if(int_enabled ()) {
|
|
377 panic("sched interruptible");
|
|
378 }
|
|
379
|
|
380 intena = cpu->intena;
|
|
381 swtch(&proc->context, cpu->scheduler);
|
|
382 cpu->intena = intena;
|
|
383 }
|
|
384
|
|
385 // Give up the CPU for one scheduling round.
|
|
386 void yield(void)
|
|
387 {
|
|
388 acquire(&ptable.lock); //DOC: yieldlock
|
|
389 proc->state = RUNNABLE;
|
|
390 sched();
|
|
391 release(&ptable.lock);
|
|
392 }
|
|
393
|
|
394 // A fork child's very first scheduling by scheduler()
|
|
395 // will swtch here. "Return" to user space.
|
|
396 void forkret(void)
|
|
397 {
|
|
398 static int first = 1;
|
|
399
|
|
400 // Still holding ptable.lock from scheduler.
|
|
401 release(&ptable.lock);
|
|
402
|
|
403 if (first) {
|
|
404 // Some initialization functions must be run in the context
|
|
405 // of a regular process (e.g., they call sleep), and thus cannot
|
|
406 // be run from main().
|
|
407 first = 0;
|
|
408 initlog();
|
|
409 }
|
|
410
|
|
411 // Return to "caller", actually trapret (see allocproc).
|
|
412 }
|
|
413
|
|
414 // Atomically release lock and sleep on chan.
|
|
415 // Reacquires lock when awakened.
|
|
416 void sleep(void *chan, struct spinlock *lk)
|
|
417 {
|
|
418 //show_callstk("sleep");
|
|
419
|
|
420 if(proc == 0) {
|
|
421 panic("sleep");
|
|
422 }
|
|
423
|
|
424 if(lk == 0) {
|
|
425 panic("sleep without lk");
|
|
426 }
|
|
427
|
|
428 // Must acquire ptable.lock in order to change p->state and then call
|
|
429 // sched. Once we hold ptable.lock, we can be guaranteed that we won't
|
|
430 // miss any wakeup (wakeup runs with ptable.lock locked), so it's okay
|
|
431 // to release lk.
|
|
432 if(lk != &ptable.lock){ //DOC: sleeplock0
|
|
433 acquire(&ptable.lock); //DOC: sleeplock1
|
|
434 release(lk);
|
|
435 }
|
|
436
|
|
437 // Go to sleep.
|
|
438 proc->chan = chan;
|
|
439 proc->state = SLEEPING;
|
|
440 sched();
|
|
441
|
|
442 // Tidy up.
|
|
443 proc->chan = 0;
|
|
444
|
|
445 // Reacquire original lock.
|
|
446 if(lk != &ptable.lock){ //DOC: sleeplock2
|
|
447 release(&ptable.lock);
|
|
448 acquire(lk);
|
|
449 }
|
|
450 }
|
|
451
|
|
452 //PAGEBREAK!
|
|
453 // Wake up all processes sleeping on chan. The ptable lock must be held.
|
|
454 static void wakeup1(void *chan)
|
|
455 {
|
|
456 struct proc *p;
|
|
457
|
|
458 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++) {
|
|
459 if(p->state == SLEEPING && p->chan == chan) {
|
|
460 p->state = RUNNABLE;
|
|
461 }
|
|
462 }
|
|
463 }
|
|
464
|
|
465 // Wake up all processes sleeping on chan.
|
|
466 void wakeup(void *chan)
|
|
467 {
|
|
468 acquire(&ptable.lock);
|
|
469 wakeup1(chan);
|
|
470 release(&ptable.lock);
|
|
471 }
|
|
472
|
|
473 // Kill the process with the given pid. Process won't exit until it returns
|
|
474 // to user space (see trap in trap.c).
|
|
475 int kill(int pid)
|
|
476 {
|
|
477 struct proc *p;
|
|
478
|
|
479 acquire(&ptable.lock);
|
|
480
|
|
481 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
|
|
482 if(p->pid == pid){
|
|
483 p->killed = 1;
|
|
484
|
|
485 // Wake process from sleep if necessary.
|
|
486 if(p->state == SLEEPING) {
|
|
487 p->state = RUNNABLE;
|
|
488 }
|
|
489
|
|
490 release(&ptable.lock);
|
|
491 return 0;
|
|
492 }
|
|
493 }
|
|
494
|
|
495 release(&ptable.lock);
|
|
496 return -1;
|
|
497 }
|
|
498
|
|
499 //PAGEBREAK: 36
|
|
500 // Print a process listing to console. For debugging. Runs when user
|
|
501 // types ^P on console. No lock to avoid wedging a stuck machine further.
|
|
502 void procdump(void)
|
|
503 {
|
|
504 static char *states[] = {
|
|
505 [UNUSED] "unused",
|
|
506 [EMBRYO] "embryo",
|
|
507 [SLEEPING] "sleep ",
|
|
508 [RUNNABLE] "runble",
|
|
509 [RUNNING] "run ",
|
|
510 [ZOMBIE] "zombie"
|
|
511 };
|
|
512
|
|
513 struct proc *p;
|
|
514 char *state;
|
|
515
|
|
516 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
|
|
517 if(p->state == UNUSED) {
|
|
518 continue;
|
|
519 }
|
|
520
|
|
521 if(p->state >= 0 && p->state < NELEM(states) && states[p->state]) {
|
|
522 state = states[p->state];
|
|
523 } else {
|
|
524 state = "???";
|
|
525 }
|
|
526
|
|
527 cprintf("%d %s %d:%s %d\n", p->pid, state, p->pid, p->name, p->parent->pid);
|
|
528 }
|
|
529
|
|
530 show_callstk("procdump: \n");
|
|
531 }
|
|
532
|
|
533
|