annotate src/proc.cbc @ 241:1696f9c2ad0d

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