comparison src/proc.c @ 0:83c23a36980d

Init
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Fri, 26 May 2017 23:11:05 +0900
parents
children bf2f70fa8852
comparison
equal deleted inserted replaced
-1:000000000000 0:83c23a36980d
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;
199 *np->tf = *proc->tf;
200
201 // Clear r0 so that fork returns 0 in the child.
202 np->tf->r0 = 0;
203
204 for(i = 0; i < NOFILE; i++) {
205 if(proc->ofile[i]) {
206 np->ofile[i] = filedup(proc->ofile[i]);
207 }
208 }
209
210 np->cwd = idup(proc->cwd);
211
212 pid = np->pid;
213 np->state = RUNNABLE;
214 safestrcpy(np->name, proc->name, sizeof(proc->name));
215
216 return pid;
217 }
218
219 // Exit the current process. Does not return.
220 // An exited process remains in the zombie state
221 // until its parent calls wait() to find out it exited.
222 void exit(void)
223 {
224 struct proc *p;
225 int fd;
226
227 if(proc == initproc) {
228 panic("init exiting");
229 }
230
231 // Close all open files.
232 for(fd = 0; fd < NOFILE; fd++){
233 if(proc->ofile[fd]){
234 fileclose(proc->ofile[fd]);
235 proc->ofile[fd] = 0;
236 }
237 }
238
239 iput(proc->cwd);
240 proc->cwd = 0;
241
242 acquire(&ptable.lock);
243
244 // Parent might be sleeping in wait().
245 wakeup1(proc->parent);
246
247 // Pass abandoned children to init.
248 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
249 if(p->parent == proc){
250 p->parent = initproc;
251
252 if(p->state == ZOMBIE) {
253 wakeup1(initproc);
254 }
255 }
256 }
257
258 // Jump into the scheduler, never to return.
259 proc->state = ZOMBIE;
260 sched();
261
262 panic("zombie exit");
263 }
264
265 // Wait for a child process to exit and return its pid.
266 // Return -1 if this process has no children.
267 int wait(void)
268 {
269 struct proc *p;
270 int havekids, pid;
271
272 acquire(&ptable.lock);
273
274 for(;;){
275 // Scan through table looking for zombie children.
276 havekids = 0;
277
278 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
279 if(p->parent != proc) {
280 continue;
281 }
282
283 havekids = 1;
284
285 if(p->state == ZOMBIE){
286 // Found one.
287 pid = p->pid;
288 free_page(p->kstack);
289 p->kstack = 0;
290 freevm(p->pgdir);
291 p->state = UNUSED;
292 p->pid = 0;
293 p->parent = 0;
294 p->name[0] = 0;
295 p->killed = 0;
296 release(&ptable.lock);
297
298 return pid;
299 }
300 }
301
302 // No point waiting if we don't have any children.
303 if(!havekids || proc->killed){
304 release(&ptable.lock);
305 return -1;
306 }
307
308 // Wait for children to exit. (See wakeup1 call in proc_exit.)
309 sleep(proc, &ptable.lock); //DOC: wait-sleep
310 }
311 }
312
313 //PAGEBREAK: 42
314 // Per-CPU process scheduler.
315 // Each CPU calls scheduler() after setting itself up.
316 // Scheduler never returns. It loops, doing:
317 // - choose a process to run
318 // - swtch to start running that process
319 // - eventually that process transfers control
320 // via swtch back to the scheduler.
321 void scheduler(void)
322 {
323 struct proc *p;
324
325 for(;;){
326 // Enable interrupts on this processor.
327 sti();
328
329 // Loop over process table looking for process to run.
330 acquire(&ptable.lock);
331
332 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
333 if(p->state != RUNNABLE) {
334 continue;
335 }
336
337 // Switch to chosen process. It is the process's job
338 // to release ptable.lock and then reacquire it
339 // before jumping back to us.
340 proc = p;
341 switchuvm(p);
342
343 p->state = RUNNING;
344
345 swtch(&cpu->scheduler, proc->context);
346 // Process is done running for now.
347 // It should have changed its p->state before coming back.
348 proc = 0;
349 }
350
351 release(&ptable.lock);
352 }
353 }
354
355 // Enter scheduler. Must hold only ptable.lock
356 // and have changed proc->state.
357 void sched(void)
358 {
359 int intena;
360
361 //show_callstk ("sched");
362
363 if(!holding(&ptable.lock)) {
364 panic("sched ptable.lock");
365 }
366
367 if(cpu->ncli != 1) {
368 panic("sched locks");
369 }
370
371 if(proc->state == RUNNING) {
372 panic("sched running");
373 }
374
375 if(int_enabled ()) {
376 panic("sched interruptible");
377 }
378
379 intena = cpu->intena;
380 swtch(&proc->context, cpu->scheduler);
381 cpu->intena = intena;
382 }
383
384 // Give up the CPU for one scheduling round.
385 void yield(void)
386 {
387 acquire(&ptable.lock); //DOC: yieldlock
388 proc->state = RUNNABLE;
389 sched();
390 release(&ptable.lock);
391 }
392
393 // A fork child's very first scheduling by scheduler()
394 // will swtch here. "Return" to user space.
395 void forkret(void)
396 {
397 static int first = 1;
398
399 // Still holding ptable.lock from scheduler.
400 release(&ptable.lock);
401
402 if (first) {
403 // Some initialization functions must be run in the context
404 // of a regular process (e.g., they call sleep), and thus cannot
405 // be run from main().
406 first = 0;
407 initlog();
408 }
409
410 // Return to "caller", actually trapret (see allocproc).
411 }
412
413 // Atomically release lock and sleep on chan.
414 // Reacquires lock when awakened.
415 void sleep(void *chan, struct spinlock *lk)
416 {
417 //show_callstk("sleep");
418
419 if(proc == 0) {
420 panic("sleep");
421 }
422
423 if(lk == 0) {
424 panic("sleep without lk");
425 }
426
427 // Must acquire ptable.lock in order to change p->state and then call
428 // sched. Once we hold ptable.lock, we can be guaranteed that we won't
429 // miss any wakeup (wakeup runs with ptable.lock locked), so it's okay
430 // to release lk.
431 if(lk != &ptable.lock){ //DOC: sleeplock0
432 acquire(&ptable.lock); //DOC: sleeplock1
433 release(lk);
434 }
435
436 // Go to sleep.
437 proc->chan = chan;
438 proc->state = SLEEPING;
439 sched();
440
441 // Tidy up.
442 proc->chan = 0;
443
444 // Reacquire original lock.
445 if(lk != &ptable.lock){ //DOC: sleeplock2
446 release(&ptable.lock);
447 acquire(lk);
448 }
449 }
450
451 //PAGEBREAK!
452 // Wake up all processes sleeping on chan. The ptable lock must be held.
453 static void wakeup1(void *chan)
454 {
455 struct proc *p;
456
457 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++) {
458 if(p->state == SLEEPING && p->chan == chan) {
459 p->state = RUNNABLE;
460 }
461 }
462 }
463
464 // Wake up all processes sleeping on chan.
465 void wakeup(void *chan)
466 {
467 acquire(&ptable.lock);
468 wakeup1(chan);
469 release(&ptable.lock);
470 }
471
472 // Kill the process with the given pid. Process won't exit until it returns
473 // to user space (see trap in trap.c).
474 int kill(int pid)
475 {
476 struct proc *p;
477
478 acquire(&ptable.lock);
479
480 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
481 if(p->pid == pid){
482 p->killed = 1;
483
484 // Wake process from sleep if necessary.
485 if(p->state == SLEEPING) {
486 p->state = RUNNABLE;
487 }
488
489 release(&ptable.lock);
490 return 0;
491 }
492 }
493
494 release(&ptable.lock);
495 return -1;
496 }
497
498 //PAGEBREAK: 36
499 // Print a process listing to console. For debugging. Runs when user
500 // types ^P on console. No lock to avoid wedging a stuck machine further.
501 void procdump(void)
502 {
503 static char *states[] = {
504 [UNUSED] "unused",
505 [EMBRYO] "embryo",
506 [SLEEPING] "sleep ",
507 [RUNNABLE] "runble",
508 [RUNNING] "run ",
509 [ZOMBIE] "zombie"
510 };
511
512 struct proc *p;
513 char *state;
514
515 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
516 if(p->state == UNUSED) {
517 continue;
518 }
519
520 if(p->state >= 0 && p->state < NELEM(states) && states[p->state]) {
521 state = states[p->state];
522 } else {
523 state = "???";
524 }
525
526 cprintf("%d %s %d:%s %d\n", p->pid, state, p->pid, p->name, p->parent->pid);
527 }
528
529 show_callstk("procdump: \n");
530 }
531
532