Rev 430 | Rev 458 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 430 | Rev 452 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <proc/scheduler.h> |
29 | #include <proc/scheduler.h> |
30 | #include <proc/thread.h> |
30 | #include <proc/thread.h> |
31 | #include <proc/task.h> |
31 | #include <proc/task.h> |
32 | #include <mm/heap.h> |
32 | #include <mm/heap.h> |
33 | #include <mm/frame.h> |
33 | #include <mm/frame.h> |
34 | #include <mm/page.h> |
34 | #include <mm/page.h> |
35 | #include <mm/vm.h> |
35 | #include <mm/vm.h> |
36 | #include <arch/asm.h> |
36 | #include <arch/asm.h> |
37 | #include <arch/faddr.h> |
37 | #include <arch/faddr.h> |
38 | #include <arch/atomic.h> |
38 | #include <arch/atomic.h> |
39 | #include <synch/spinlock.h> |
39 | #include <synch/spinlock.h> |
40 | #include <config.h> |
40 | #include <config.h> |
41 | #include <context.h> |
41 | #include <context.h> |
42 | #include <func.h> |
42 | #include <func.h> |
43 | #include <arch.h> |
43 | #include <arch.h> |
44 | #include <list.h> |
44 | #include <list.h> |
45 | #include <panic.h> |
45 | #include <panic.h> |
46 | #include <typedefs.h> |
46 | #include <typedefs.h> |
47 | #include <cpu.h> |
47 | #include <cpu.h> |
48 | #include <print.h> |
48 | #include <print.h> |
49 | #include <debug.h> |
49 | #include <debug.h> |
50 | 50 | ||
51 | volatile count_t nrdy; |
51 | volatile count_t nrdy; |
52 | 52 | ||
53 | 53 | ||
54 | /** Take actions before new thread runs |
54 | /** Take actions before new thread runs |
55 | * |
55 | * |
56 | * Perform actions that need to be |
56 | * Perform actions that need to be |
57 | * taken before the newly selected |
57 | * taken before the newly selected |
58 | * tread is passed control. |
58 | * tread is passed control. |
59 | * |
59 | * |
60 | */ |
60 | */ |
61 | void before_thread_runs(void) |
61 | void before_thread_runs(void) |
62 | { |
62 | { |
63 | before_thread_runs_arch(); |
63 | before_thread_runs_arch(); |
64 | #ifdef FPU_LAZY |
64 | #ifdef FPU_LAZY |
65 | if(THREAD==CPU->fpu_owner) |
65 | if(THREAD==CPU->fpu_owner) |
66 | fpu_enable(); |
66 | fpu_enable(); |
67 | else |
67 | else |
68 | fpu_disable(); |
68 | fpu_disable(); |
69 | #else |
69 | #else |
70 | fpu_enable(); |
70 | fpu_enable(); |
71 | if (THREAD->fpu_context_exists) |
71 | if (THREAD->fpu_context_exists) |
72 | fpu_context_restore(&(THREAD->saved_fpu_context)); |
72 | fpu_context_restore(&(THREAD->saved_fpu_context)); |
73 | else { |
73 | else { |
74 | fpu_init(); |
74 | fpu_init(); |
75 | THREAD->fpu_context_exists=1; |
75 | THREAD->fpu_context_exists=1; |
76 | } |
76 | } |
77 | #endif |
77 | #endif |
78 | } |
78 | } |
79 | 79 | ||
80 | #ifdef FPU_LAZY |
80 | #ifdef FPU_LAZY |
81 | void scheduler_fpu_lazy_request(void) |
81 | void scheduler_fpu_lazy_request(void) |
82 | { |
82 | { |
83 | fpu_enable(); |
83 | fpu_enable(); |
84 | if (CPU->fpu_owner != NULL) { |
84 | if (CPU->fpu_owner != NULL) { |
85 | fpu_context_save(&CPU->fpu_owner->saved_fpu_context); |
85 | fpu_context_save(&CPU->fpu_owner->saved_fpu_context); |
86 | /* don't prevent migration */ |
86 | /* don't prevent migration */ |
87 | CPU->fpu_owner->fpu_context_engaged=0; |
87 | CPU->fpu_owner->fpu_context_engaged=0; |
88 | } |
88 | } |
89 | if (THREAD->fpu_context_exists) |
89 | if (THREAD->fpu_context_exists) |
90 | fpu_context_restore(&THREAD->saved_fpu_context); |
90 | fpu_context_restore(&THREAD->saved_fpu_context); |
91 | else { |
91 | else { |
92 | fpu_init(); |
92 | fpu_init(); |
93 | THREAD->fpu_context_exists=1; |
93 | THREAD->fpu_context_exists=1; |
94 | } |
94 | } |
95 | CPU->fpu_owner=THREAD; |
95 | CPU->fpu_owner=THREAD; |
96 | THREAD->fpu_context_engaged = 1; |
96 | THREAD->fpu_context_engaged = 1; |
97 | } |
97 | } |
98 | #endif |
98 | #endif |
99 | 99 | ||
100 | /** Initialize scheduler |
100 | /** Initialize scheduler |
101 | * |
101 | * |
102 | * Initialize kernel scheduler. |
102 | * Initialize kernel scheduler. |
103 | * |
103 | * |
104 | */ |
104 | */ |
105 | void scheduler_init(void) |
105 | void scheduler_init(void) |
106 | { |
106 | { |
107 | } |
107 | } |
108 | 108 | ||
109 | 109 | ||
110 | /** Get thread to be scheduled |
110 | /** Get thread to be scheduled |
111 | * |
111 | * |
112 | * Get the optimal thread to be scheduled |
112 | * Get the optimal thread to be scheduled |
113 | * according to thread accounting and scheduler |
113 | * according to thread accounting and scheduler |
114 | * policy. |
114 | * policy. |
115 | * |
115 | * |
116 | * @return Thread to be scheduled. |
116 | * @return Thread to be scheduled. |
117 | * |
117 | * |
118 | */ |
118 | */ |
119 | struct thread *find_best_thread(void) |
119 | static struct thread *find_best_thread(void) |
120 | { |
120 | { |
121 | thread_t *t; |
121 | thread_t *t; |
122 | runq_t *r; |
122 | runq_t *r; |
123 | int i, n; |
123 | int i, n; |
124 | 124 | ||
125 | ASSERT(CPU != NULL); |
125 | ASSERT(CPU != NULL); |
126 | 126 | ||
127 | loop: |
127 | loop: |
128 | interrupts_disable(); |
128 | interrupts_disable(); |
129 | 129 | ||
130 | spinlock_lock(&CPU->lock); |
130 | spinlock_lock(&CPU->lock); |
131 | n = CPU->nrdy; |
131 | n = CPU->nrdy; |
132 | spinlock_unlock(&CPU->lock); |
132 | spinlock_unlock(&CPU->lock); |
133 | 133 | ||
134 | interrupts_enable(); |
134 | interrupts_enable(); |
135 | 135 | ||
136 | if (n == 0) { |
136 | if (n == 0) { |
137 | #ifdef __SMP__ |
137 | #ifdef __SMP__ |
138 | /* |
138 | /* |
139 | * If the load balancing thread is not running, wake it up and |
139 | * If the load balancing thread is not running, wake it up and |
140 | * set CPU-private flag that the kcpulb has been started. |
140 | * set CPU-private flag that the kcpulb has been started. |
141 | */ |
141 | */ |
142 | if (test_and_set(&CPU->kcpulbstarted) == 0) { |
142 | if (test_and_set(&CPU->kcpulbstarted) == 0) { |
143 | waitq_wakeup(&CPU->kcpulb_wq, 0); |
143 | waitq_wakeup(&CPU->kcpulb_wq, 0); |
144 | goto loop; |
144 | goto loop; |
145 | } |
145 | } |
146 | #endif /* __SMP__ */ |
146 | #endif /* __SMP__ */ |
147 | 147 | ||
148 | /* |
148 | /* |
149 | * For there was nothing to run, the CPU goes to sleep |
149 | * For there was nothing to run, the CPU goes to sleep |
150 | * until a hardware interrupt or an IPI comes. |
150 | * until a hardware interrupt or an IPI comes. |
151 | * This improves energy saving and hyperthreading. |
151 | * This improves energy saving and hyperthreading. |
152 | * On the other hand, several hardware interrupts can be ignored. |
152 | * On the other hand, several hardware interrupts can be ignored. |
153 | */ |
153 | */ |
154 | cpu_sleep(); |
154 | cpu_sleep(); |
155 | goto loop; |
155 | goto loop; |
156 | } |
156 | } |
157 | 157 | ||
158 | interrupts_disable(); |
158 | interrupts_disable(); |
159 | 159 | ||
160 | i = 0; |
160 | i = 0; |
161 | retry: |
161 | retry: |
162 | for (; i<RQ_COUNT; i++) { |
162 | for (; i<RQ_COUNT; i++) { |
163 | r = &CPU->rq[i]; |
163 | r = &CPU->rq[i]; |
164 | spinlock_lock(&r->lock); |
164 | spinlock_lock(&r->lock); |
165 | if (r->n == 0) { |
165 | if (r->n == 0) { |
166 | /* |
166 | /* |
167 | * If this queue is empty, try a lower-priority queue. |
167 | * If this queue is empty, try a lower-priority queue. |
168 | */ |
168 | */ |
169 | spinlock_unlock(&r->lock); |
169 | spinlock_unlock(&r->lock); |
170 | continue; |
170 | continue; |
171 | } |
171 | } |
172 | 172 | ||
173 | /* avoid deadlock with relink_rq() */ |
173 | /* avoid deadlock with relink_rq() */ |
174 | if (!spinlock_trylock(&CPU->lock)) { |
174 | if (!spinlock_trylock(&CPU->lock)) { |
175 | /* |
175 | /* |
176 | * Unlock r and try again. |
176 | * Unlock r and try again. |
177 | */ |
177 | */ |
178 | spinlock_unlock(&r->lock); |
178 | spinlock_unlock(&r->lock); |
179 | goto retry; |
179 | goto retry; |
180 | } |
180 | } |
181 | CPU->nrdy--; |
181 | CPU->nrdy--; |
182 | spinlock_unlock(&CPU->lock); |
182 | spinlock_unlock(&CPU->lock); |
183 | 183 | ||
184 | atomic_dec((int *) &nrdy); |
184 | atomic_dec((int *) &nrdy); |
185 | r->n--; |
185 | r->n--; |
186 | 186 | ||
187 | /* |
187 | /* |
188 | * Take the first thread from the queue. |
188 | * Take the first thread from the queue. |
189 | */ |
189 | */ |
190 | t = list_get_instance(r->rq_head.next, thread_t, rq_link); |
190 | t = list_get_instance(r->rq_head.next, thread_t, rq_link); |
191 | list_remove(&t->rq_link); |
191 | list_remove(&t->rq_link); |
192 | 192 | ||
193 | spinlock_unlock(&r->lock); |
193 | spinlock_unlock(&r->lock); |
194 | 194 | ||
195 | spinlock_lock(&t->lock); |
195 | spinlock_lock(&t->lock); |
196 | t->cpu = CPU; |
196 | t->cpu = CPU; |
197 | 197 | ||
198 | t->ticks = us2ticks((i+1)*10000); |
198 | t->ticks = us2ticks((i+1)*10000); |
199 | t->priority = i; /* eventually correct rq index */ |
199 | t->priority = i; /* eventually correct rq index */ |
200 | 200 | ||
201 | /* |
201 | /* |
202 | * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge. |
202 | * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge. |
203 | */ |
203 | */ |
204 | t->flags &= ~X_STOLEN; |
204 | t->flags &= ~X_STOLEN; |
205 | spinlock_unlock(&t->lock); |
205 | spinlock_unlock(&t->lock); |
206 | 206 | ||
207 | return t; |
207 | return t; |
208 | } |
208 | } |
209 | goto loop; |
209 | goto loop; |
210 | 210 | ||
211 | } |
211 | } |
212 | 212 | ||
213 | 213 | ||
214 | /** Prevent rq starvation |
214 | /** Prevent rq starvation |
215 | * |
215 | * |
216 | * Prevent low priority threads from starving in rq's. |
216 | * Prevent low priority threads from starving in rq's. |
217 | * |
217 | * |
218 | * When the function decides to relink rq's, it reconnects |
218 | * When the function decides to relink rq's, it reconnects |
219 | * respective pointers so that in result threads with 'pri' |
219 | * respective pointers so that in result threads with 'pri' |
220 | * greater or equal 'start' are moved to a higher-priority queue. |
220 | * greater or equal 'start' are moved to a higher-priority queue. |
221 | * |
221 | * |
222 | * @param start Threshold priority. |
222 | * @param start Threshold priority. |
223 | * |
223 | * |
224 | */ |
224 | */ |
225 | void relink_rq(int start) |
225 | static void relink_rq(int start) |
226 | { |
226 | { |
227 | link_t head; |
227 | link_t head; |
228 | runq_t *r; |
228 | runq_t *r; |
229 | int i, n; |
229 | int i, n; |
230 | 230 | ||
231 | list_initialize(&head); |
231 | list_initialize(&head); |
232 | spinlock_lock(&CPU->lock); |
232 | spinlock_lock(&CPU->lock); |
233 | if (CPU->needs_relink > NEEDS_RELINK_MAX) { |
233 | if (CPU->needs_relink > NEEDS_RELINK_MAX) { |
234 | for (i = start; i<RQ_COUNT-1; i++) { |
234 | for (i = start; i<RQ_COUNT-1; i++) { |
235 | /* remember and empty rq[i + 1] */ |
235 | /* remember and empty rq[i + 1] */ |
236 | r = &CPU->rq[i + 1]; |
236 | r = &CPU->rq[i + 1]; |
237 | spinlock_lock(&r->lock); |
237 | spinlock_lock(&r->lock); |
238 | list_concat(&head, &r->rq_head); |
238 | list_concat(&head, &r->rq_head); |
239 | n = r->n; |
239 | n = r->n; |
240 | r->n = 0; |
240 | r->n = 0; |
241 | spinlock_unlock(&r->lock); |
241 | spinlock_unlock(&r->lock); |
242 | 242 | ||
243 | /* append rq[i + 1] to rq[i] */ |
243 | /* append rq[i + 1] to rq[i] */ |
244 | r = &CPU->rq[i]; |
244 | r = &CPU->rq[i]; |
245 | spinlock_lock(&r->lock); |
245 | spinlock_lock(&r->lock); |
246 | list_concat(&r->rq_head, &head); |
246 | list_concat(&r->rq_head, &head); |
247 | r->n += n; |
247 | r->n += n; |
248 | spinlock_unlock(&r->lock); |
248 | spinlock_unlock(&r->lock); |
249 | } |
249 | } |
250 | CPU->needs_relink = 0; |
250 | CPU->needs_relink = 0; |
251 | } |
251 | } |
252 | spinlock_unlock(&CPU->lock); |
252 | spinlock_unlock(&CPU->lock); |
253 | 253 | ||
254 | } |
254 | } |
255 | 255 | ||
256 | 256 | ||
257 | /** The scheduler |
- | |
258 | * |
- | |
259 | * The thread scheduling procedure. |
- | |
260 | * |
- | |
261 | */ |
- | |
262 | void scheduler(void) |
- | |
263 | { |
- | |
264 | volatile ipl_t ipl; |
- | |
265 | - | ||
266 | ASSERT(CPU != NULL); |
- | |
267 | - | ||
268 | ipl = interrupts_disable(); |
- | |
269 | - | ||
270 | if (haltstate) |
- | |
271 | halt(); |
- | |
272 | - | ||
273 | if (THREAD) { |
- | |
274 | spinlock_lock(&THREAD->lock); |
- | |
275 | #ifndef FPU_LAZY |
- | |
276 | fpu_context_save(&(THREAD->saved_fpu_context)); |
- | |
277 | #endif |
- | |
278 | if (!context_save(&THREAD->saved_context)) { |
- | |
279 | /* |
- | |
280 | * This is the place where threads leave scheduler(); |
- | |
281 | */ |
- | |
282 | before_thread_runs(); |
- | |
283 | spinlock_unlock(&THREAD->lock); |
- | |
284 | interrupts_restore(THREAD->saved_context.ipl); |
- | |
285 | return; |
- | |
286 | } |
- | |
287 | - | ||
288 | /* |
- | |
289 | * Interrupt priority level of preempted thread is recorded here |
- | |
290 | * to facilitate scheduler() invocations from interrupts_disable()'d |
- | |
291 | * code (e.g. waitq_sleep_timeout()). |
- | |
292 | */ |
- | |
293 | THREAD->saved_context.ipl = ipl; |
- | |
294 | } |
- | |
295 | - | ||
296 | /* |
- | |
297 | * Through the 'THE' structure, we keep track of THREAD, TASK, CPU |
- | |
298 | * and preemption counter. At this point THE could be coming either |
- | |
299 | * from THREAD's or CPU's stack. |
- | |
300 | */ |
- | |
301 | the_copy(THE, (the_t *) CPU->stack); |
- | |
302 | - | ||
303 | /* |
- | |
304 | * We may not keep the old stack. |
- | |
305 | * Reason: If we kept the old stack and got blocked, for instance, in |
- | |
306 | * find_best_thread(), the old thread could get rescheduled by another |
- | |
307 | * CPU and overwrite the part of its own stack that was also used by |
- | |
308 | * the scheduler on this CPU. |
- | |
309 | * |
- | |
310 | * Moreover, we have to bypass the compiler-generated POP sequence |
- | |
311 | * which is fooled by SP being set to the very top of the stack. |
- | |
312 | * Therefore the scheduler() function continues in |
- | |
313 | * scheduler_separated_stack(). |
- | |
314 | */ |
- | |
315 | context_save(&CPU->saved_context); |
- | |
316 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE); |
- | |
317 | context_restore(&CPU->saved_context); |
- | |
318 | /* not reached */ |
- | |
319 | } |
- | |
320 | - | ||
321 | - | ||
322 | /** Scheduler stack switch wrapper |
257 | /** Scheduler stack switch wrapper |
323 | * |
258 | * |
324 | * Second part of the scheduler() function |
259 | * Second part of the scheduler() function |
325 | * using new stack. Handling the actual context |
260 | * using new stack. Handling the actual context |
326 | * switch to a new thread. |
261 | * switch to a new thread. |
327 | * |
262 | * |
328 | */ |
263 | */ |
329 | void scheduler_separated_stack(void) |
264 | static void scheduler_separated_stack(void) |
330 | { |
265 | { |
331 | int priority; |
266 | int priority; |
332 | 267 | ||
333 | ASSERT(CPU != NULL); |
268 | ASSERT(CPU != NULL); |
334 | 269 | ||
335 | if (THREAD) { |
270 | if (THREAD) { |
336 | switch (THREAD->state) { |
271 | switch (THREAD->state) { |
337 | case Running: |
272 | case Running: |
338 | THREAD->state = Ready; |
273 | THREAD->state = Ready; |
339 | spinlock_unlock(&THREAD->lock); |
274 | spinlock_unlock(&THREAD->lock); |
340 | thread_ready(THREAD); |
275 | thread_ready(THREAD); |
341 | break; |
276 | break; |
342 | 277 | ||
343 | case Exiting: |
278 | case Exiting: |
344 | frame_free((__address) THREAD->kstack); |
279 | frame_free((__address) THREAD->kstack); |
345 | if (THREAD->ustack) { |
280 | if (THREAD->ustack) { |
346 | frame_free((__address) THREAD->ustack); |
281 | frame_free((__address) THREAD->ustack); |
347 | } |
282 | } |
348 | 283 | ||
349 | /* |
284 | /* |
350 | * Detach from the containing task. |
285 | * Detach from the containing task. |
351 | */ |
286 | */ |
352 | spinlock_lock(&TASK->lock); |
287 | spinlock_lock(&TASK->lock); |
353 | list_remove(&THREAD->th_link); |
288 | list_remove(&THREAD->th_link); |
354 | spinlock_unlock(&TASK->lock); |
289 | spinlock_unlock(&TASK->lock); |
355 | 290 | ||
356 | spinlock_unlock(&THREAD->lock); |
291 | spinlock_unlock(&THREAD->lock); |
357 | 292 | ||
358 | spinlock_lock(&threads_lock); |
293 | spinlock_lock(&threads_lock); |
359 | list_remove(&THREAD->threads_link); |
294 | list_remove(&THREAD->threads_link); |
360 | spinlock_unlock(&threads_lock); |
295 | spinlock_unlock(&threads_lock); |
361 | 296 | ||
362 | spinlock_lock(&CPU->lock); |
297 | spinlock_lock(&CPU->lock); |
363 | if(CPU->fpu_owner==THREAD) CPU->fpu_owner=NULL; |
298 | if(CPU->fpu_owner==THREAD) CPU->fpu_owner=NULL; |
364 | spinlock_unlock(&CPU->lock); |
299 | spinlock_unlock(&CPU->lock); |
365 | 300 | ||
366 | free(THREAD); |
301 | free(THREAD); |
367 | 302 | ||
368 | break; |
303 | break; |
369 | 304 | ||
370 | case Sleeping: |
305 | case Sleeping: |
371 | /* |
306 | /* |
372 | * Prefer the thread after it's woken up. |
307 | * Prefer the thread after it's woken up. |
373 | */ |
308 | */ |
374 | THREAD->priority = -1; |
309 | THREAD->priority = -1; |
375 | 310 | ||
376 | /* |
311 | /* |
377 | * We need to release wq->lock which we locked in waitq_sleep(). |
312 | * We need to release wq->lock which we locked in waitq_sleep(). |
378 | * Address of wq->lock is kept in THREAD->sleep_queue. |
313 | * Address of wq->lock is kept in THREAD->sleep_queue. |
379 | */ |
314 | */ |
380 | spinlock_unlock(&THREAD->sleep_queue->lock); |
315 | spinlock_unlock(&THREAD->sleep_queue->lock); |
381 | 316 | ||
382 | /* |
317 | /* |
383 | * Check for possible requests for out-of-context invocation. |
318 | * Check for possible requests for out-of-context invocation. |
384 | */ |
319 | */ |
385 | if (THREAD->call_me) { |
320 | if (THREAD->call_me) { |
386 | THREAD->call_me(THREAD->call_me_with); |
321 | THREAD->call_me(THREAD->call_me_with); |
387 | THREAD->call_me = NULL; |
322 | THREAD->call_me = NULL; |
388 | THREAD->call_me_with = NULL; |
323 | THREAD->call_me_with = NULL; |
389 | } |
324 | } |
390 | 325 | ||
391 | spinlock_unlock(&THREAD->lock); |
326 | spinlock_unlock(&THREAD->lock); |
392 | 327 | ||
393 | break; |
328 | break; |
394 | 329 | ||
395 | default: |
330 | default: |
396 | /* |
331 | /* |
397 | * Entering state is unexpected. |
332 | * Entering state is unexpected. |
398 | */ |
333 | */ |
399 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]); |
334 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]); |
400 | break; |
335 | break; |
401 | } |
336 | } |
402 | THREAD = NULL; |
337 | THREAD = NULL; |
403 | } |
338 | } |
404 | 339 | ||
405 | 340 | ||
406 | THREAD = find_best_thread(); |
341 | THREAD = find_best_thread(); |
407 | 342 | ||
408 | spinlock_lock(&THREAD->lock); |
343 | spinlock_lock(&THREAD->lock); |
409 | priority = THREAD->priority; |
344 | priority = THREAD->priority; |
410 | spinlock_unlock(&THREAD->lock); |
345 | spinlock_unlock(&THREAD->lock); |
411 | 346 | ||
412 | relink_rq(priority); |
347 | relink_rq(priority); |
413 | 348 | ||
414 | spinlock_lock(&THREAD->lock); |
349 | spinlock_lock(&THREAD->lock); |
415 | 350 | ||
416 | /* |
351 | /* |
417 | * If both the old and the new task are the same, lots of work is avoided. |
352 | * If both the old and the new task are the same, lots of work is avoided. |
418 | */ |
353 | */ |
419 | if (TASK != THREAD->task) { |
354 | if (TASK != THREAD->task) { |
420 | vm_t *m1 = NULL; |
355 | vm_t *m1 = NULL; |
421 | vm_t *m2; |
356 | vm_t *m2; |
422 | 357 | ||
423 | if (TASK) { |
358 | if (TASK) { |
424 | spinlock_lock(&TASK->lock); |
359 | spinlock_lock(&TASK->lock); |
425 | m1 = TASK->vm; |
360 | m1 = TASK->vm; |
426 | spinlock_unlock(&TASK->lock); |
361 | spinlock_unlock(&TASK->lock); |
427 | } |
362 | } |
428 | 363 | ||
429 | spinlock_lock(&THREAD->task->lock); |
364 | spinlock_lock(&THREAD->task->lock); |
430 | m2 = THREAD->task->vm; |
365 | m2 = THREAD->task->vm; |
431 | spinlock_unlock(&THREAD->task->lock); |
366 | spinlock_unlock(&THREAD->task->lock); |
432 | 367 | ||
433 | /* |
368 | /* |
434 | * Note that it is possible for two tasks to share one vm mapping. |
369 | * Note that it is possible for two tasks to share one vm mapping. |
435 | */ |
370 | */ |
436 | if (m1 != m2) { |
371 | if (m1 != m2) { |
437 | /* |
372 | /* |
438 | * Both tasks and vm mappings are different. |
373 | * Both tasks and vm mappings are different. |
439 | * Replace the old one with the new one. |
374 | * Replace the old one with the new one. |
440 | */ |
375 | */ |
441 | vm_install(m2); |
376 | vm_install(m2); |
442 | } |
377 | } |
443 | TASK = THREAD->task; |
378 | TASK = THREAD->task; |
444 | } |
379 | } |
445 | 380 | ||
446 | THREAD->state = Running; |
381 | THREAD->state = Running; |
447 | 382 | ||
448 | #ifdef SCHEDULER_VERBOSE |
383 | #ifdef SCHEDULER_VERBOSE |
449 | printf("cpu%d: tid %d (priority=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks, CPU->nrdy); |
384 | printf("cpu%d: tid %d (priority=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks, CPU->nrdy); |
450 | #endif |
385 | #endif |
451 | 386 | ||
452 | /* |
387 | /* |
453 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to thread's stack. |
388 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to thread's stack. |
454 | */ |
389 | */ |
455 | the_copy(THE, (the_t *) THREAD->kstack); |
390 | the_copy(THE, (the_t *) THREAD->kstack); |
456 | 391 | ||
457 | context_restore(&THREAD->saved_context); |
392 | context_restore(&THREAD->saved_context); |
458 | /* not reached */ |
393 | /* not reached */ |
459 | } |
394 | } |
460 | 395 | ||
- | 396 | ||
- | 397 | /** The scheduler |
|
- | 398 | * |
|
- | 399 | * The thread scheduling procedure. |
|
- | 400 | * |
|
- | 401 | */ |
|
- | 402 | void scheduler(void) |
|
- | 403 | { |
|
- | 404 | volatile ipl_t ipl; |
|
- | 405 | ||
- | 406 | ASSERT(CPU != NULL); |
|
- | 407 | ||
- | 408 | ipl = interrupts_disable(); |
|
- | 409 | ||
- | 410 | if (haltstate) |
|
- | 411 | halt(); |
|
- | 412 | ||
- | 413 | if (THREAD) { |
|
- | 414 | spinlock_lock(&THREAD->lock); |
|
- | 415 | #ifndef FPU_LAZY |
|
- | 416 | fpu_context_save(&(THREAD->saved_fpu_context)); |
|
- | 417 | #endif |
|
- | 418 | if (!context_save(&THREAD->saved_context)) { |
|
- | 419 | /* |
|
- | 420 | * This is the place where threads leave scheduler(); |
|
- | 421 | */ |
|
- | 422 | before_thread_runs(); |
|
- | 423 | spinlock_unlock(&THREAD->lock); |
|
- | 424 | interrupts_restore(THREAD->saved_context.ipl); |
|
- | 425 | return; |
|
- | 426 | } |
|
- | 427 | ||
- | 428 | /* |
|
- | 429 | * Interrupt priority level of preempted thread is recorded here |
|
- | 430 | * to facilitate scheduler() invocations from interrupts_disable()'d |
|
- | 431 | * code (e.g. waitq_sleep_timeout()). |
|
- | 432 | */ |
|
- | 433 | THREAD->saved_context.ipl = ipl; |
|
- | 434 | } |
|
- | 435 | ||
- | 436 | /* |
|
- | 437 | * Through the 'THE' structure, we keep track of THREAD, TASK, CPU |
|
- | 438 | * and preemption counter. At this point THE could be coming either |
|
- | 439 | * from THREAD's or CPU's stack. |
|
- | 440 | */ |
|
- | 441 | the_copy(THE, (the_t *) CPU->stack); |
|
- | 442 | ||
- | 443 | /* |
|
- | 444 | * We may not keep the old stack. |
|
- | 445 | * Reason: If we kept the old stack and got blocked, for instance, in |
|
- | 446 | * find_best_thread(), the old thread could get rescheduled by another |
|
- | 447 | * CPU and overwrite the part of its own stack that was also used by |
|
- | 448 | * the scheduler on this CPU. |
|
- | 449 | * |
|
- | 450 | * Moreover, we have to bypass the compiler-generated POP sequence |
|
- | 451 | * which is fooled by SP being set to the very top of the stack. |
|
- | 452 | * Therefore the scheduler() function continues in |
|
- | 453 | * scheduler_separated_stack(). |
|
- | 454 | */ |
|
- | 455 | context_save(&CPU->saved_context); |
|
- | 456 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE); |
|
- | 457 | context_restore(&CPU->saved_context); |
|
- | 458 | /* not reached */ |
|
- | 459 | } |
|
- | 460 | ||
- | 461 | ||
- | 462 | ||
- | 463 | ||
461 | 464 | ||
462 | #ifdef __SMP__ |
465 | #ifdef __SMP__ |
463 | /** Load balancing thread |
466 | /** Load balancing thread |
464 | * |
467 | * |
465 | * SMP load balancing thread, supervising thread supplies |
468 | * SMP load balancing thread, supervising thread supplies |
466 | * for the CPU it's wired to. |
469 | * for the CPU it's wired to. |
467 | * |
470 | * |
468 | * @param arg Generic thread argument (unused). |
471 | * @param arg Generic thread argument (unused). |
469 | * |
472 | * |
470 | */ |
473 | */ |
471 | void kcpulb(void *arg) |
474 | void kcpulb(void *arg) |
472 | { |
475 | { |
473 | thread_t *t; |
476 | thread_t *t; |
474 | int count, i, j, k = 0; |
477 | int count, i, j, k = 0; |
475 | ipl_t ipl; |
478 | ipl_t ipl; |
476 | 479 | ||
477 | loop: |
480 | loop: |
478 | /* |
481 | /* |
479 | * Sleep until there's some work to do. |
482 | * Sleep until there's some work to do. |
480 | */ |
483 | */ |
481 | waitq_sleep(&CPU->kcpulb_wq); |
484 | waitq_sleep(&CPU->kcpulb_wq); |
482 | 485 | ||
483 | not_satisfied: |
486 | not_satisfied: |
484 | /* |
487 | /* |
485 | * Calculate the number of threads that will be migrated/stolen from |
488 | * Calculate the number of threads that will be migrated/stolen from |
486 | * other CPU's. Note that situation can have changed between two |
489 | * other CPU's. Note that situation can have changed between two |
487 | * passes. Each time get the most up to date counts. |
490 | * passes. Each time get the most up to date counts. |
488 | */ |
491 | */ |
489 | ipl = interrupts_disable(); |
492 | ipl = interrupts_disable(); |
490 | spinlock_lock(&CPU->lock); |
493 | spinlock_lock(&CPU->lock); |
491 | count = nrdy / config.cpu_active; |
494 | count = nrdy / config.cpu_active; |
492 | count -= CPU->nrdy; |
495 | count -= CPU->nrdy; |
493 | spinlock_unlock(&CPU->lock); |
496 | spinlock_unlock(&CPU->lock); |
494 | interrupts_restore(ipl); |
497 | interrupts_restore(ipl); |
495 | 498 | ||
496 | if (count <= 0) |
499 | if (count <= 0) |
497 | goto satisfied; |
500 | goto satisfied; |
498 | 501 | ||
499 | /* |
502 | /* |
500 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last. |
503 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last. |
501 | */ |
504 | */ |
502 | for (j=RQ_COUNT-1; j >= 0; j--) { |
505 | for (j=RQ_COUNT-1; j >= 0; j--) { |
503 | for (i=0; i < config.cpu_active; i++) { |
506 | for (i=0; i < config.cpu_active; i++) { |
504 | link_t *l; |
507 | link_t *l; |
505 | runq_t *r; |
508 | runq_t *r; |
506 | cpu_t *cpu; |
509 | cpu_t *cpu; |
507 | 510 | ||
508 | cpu = &cpus[(i + k) % config.cpu_active]; |
511 | cpu = &cpus[(i + k) % config.cpu_active]; |
509 | 512 | ||
510 | /* |
513 | /* |
511 | * Not interested in ourselves. |
514 | * Not interested in ourselves. |
512 | * Doesn't require interrupt disabling for kcpulb is X_WIRED. |
515 | * Doesn't require interrupt disabling for kcpulb is X_WIRED. |
513 | */ |
516 | */ |
514 | if (CPU == cpu) |
517 | if (CPU == cpu) |
515 | continue; |
518 | continue; |
516 | 519 | ||
517 | restart: ipl = interrupts_disable(); |
520 | restart: ipl = interrupts_disable(); |
518 | r = &cpu->rq[j]; |
521 | r = &cpu->rq[j]; |
519 | spinlock_lock(&r->lock); |
522 | spinlock_lock(&r->lock); |
520 | if (r->n == 0) { |
523 | if (r->n == 0) { |
521 | spinlock_unlock(&r->lock); |
524 | spinlock_unlock(&r->lock); |
522 | interrupts_restore(ipl); |
525 | interrupts_restore(ipl); |
523 | continue; |
526 | continue; |
524 | } |
527 | } |
525 | 528 | ||
526 | t = NULL; |
529 | t = NULL; |
527 | l = r->rq_head.prev; /* search rq from the back */ |
530 | l = r->rq_head.prev; /* search rq from the back */ |
528 | while (l != &r->rq_head) { |
531 | while (l != &r->rq_head) { |
529 | t = list_get_instance(l, thread_t, rq_link); |
532 | t = list_get_instance(l, thread_t, rq_link); |
530 | /* |
533 | /* |
531 | * We don't want to steal CPU-wired threads neither threads already stolen. |
534 | * We don't want to steal CPU-wired threads neither threads already stolen. |
532 | * The latter prevents threads from migrating between CPU's without ever being run. |
535 | * The latter prevents threads from migrating between CPU's without ever being run. |
533 | * We don't want to steal threads whose FPU context is still in CPU. |
536 | * We don't want to steal threads whose FPU context is still in CPU. |
534 | */ |
537 | */ |
535 | spinlock_lock(&t->lock); |
538 | spinlock_lock(&t->lock); |
536 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) { |
539 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) { |
537 | 540 | ||
538 | /* |
541 | /* |
539 | * Remove t from r. |
542 | * Remove t from r. |
540 | */ |
543 | */ |
541 | 544 | ||
542 | spinlock_unlock(&t->lock); |
545 | spinlock_unlock(&t->lock); |
543 | 546 | ||
544 | /* |
547 | /* |
545 | * Here we have to avoid deadlock with relink_rq(), |
548 | * Here we have to avoid deadlock with relink_rq(), |
546 | * because it locks cpu and r in a different order than we do. |
549 | * because it locks cpu and r in a different order than we do. |
547 | */ |
550 | */ |
548 | if (!spinlock_trylock(&cpu->lock)) { |
551 | if (!spinlock_trylock(&cpu->lock)) { |
549 | /* Release all locks and try again. */ |
552 | /* Release all locks and try again. */ |
550 | spinlock_unlock(&r->lock); |
553 | spinlock_unlock(&r->lock); |
551 | interrupts_restore(ipl); |
554 | interrupts_restore(ipl); |
552 | goto restart; |
555 | goto restart; |
553 | } |
556 | } |
554 | cpu->nrdy--; |
557 | cpu->nrdy--; |
555 | spinlock_unlock(&cpu->lock); |
558 | spinlock_unlock(&cpu->lock); |
556 | 559 | ||
557 | atomic_dec((int *)&nrdy); |
560 | atomic_dec((int *)&nrdy); |
558 | 561 | ||
559 | r->n--; |
562 | r->n--; |
560 | list_remove(&t->rq_link); |
563 | list_remove(&t->rq_link); |
561 | 564 | ||
562 | break; |
565 | break; |
563 | } |
566 | } |
564 | spinlock_unlock(&t->lock); |
567 | spinlock_unlock(&t->lock); |
565 | l = l->prev; |
568 | l = l->prev; |
566 | t = NULL; |
569 | t = NULL; |
567 | } |
570 | } |
568 | spinlock_unlock(&r->lock); |
571 | spinlock_unlock(&r->lock); |
569 | 572 | ||
570 | if (t) { |
573 | if (t) { |
571 | /* |
574 | /* |
572 | * Ready t on local CPU |
575 | * Ready t on local CPU |
573 | */ |
576 | */ |
574 | spinlock_lock(&t->lock); |
577 | spinlock_lock(&t->lock); |
575 | #ifdef KCPULB_VERBOSE |
578 | #ifdef KCPULB_VERBOSE |
576 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, CPU->nrdy, nrdy / config.cpu_active); |
579 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, CPU->nrdy, nrdy / config.cpu_active); |
577 | #endif |
580 | #endif |
578 | t->flags |= X_STOLEN; |
581 | t->flags |= X_STOLEN; |
579 | spinlock_unlock(&t->lock); |
582 | spinlock_unlock(&t->lock); |
580 | 583 | ||
581 | thread_ready(t); |
584 | thread_ready(t); |
582 | 585 | ||
583 | interrupts_restore(ipl); |
586 | interrupts_restore(ipl); |
584 | 587 | ||
585 | if (--count == 0) |
588 | if (--count == 0) |
586 | goto satisfied; |
589 | goto satisfied; |
587 | 590 | ||
588 | /* |
591 | /* |
589 | * We are not satisfied yet, focus on another CPU next time. |
592 | * We are not satisfied yet, focus on another CPU next time. |
590 | */ |
593 | */ |
591 | k++; |
594 | k++; |
592 | 595 | ||
593 | continue; |
596 | continue; |
594 | } |
597 | } |
595 | interrupts_restore(ipl); |
598 | interrupts_restore(ipl); |
596 | } |
599 | } |
597 | } |
600 | } |
598 | 601 | ||
599 | if (CPU->nrdy) { |
602 | if (CPU->nrdy) { |
600 | /* |
603 | /* |
601 | * Be a little bit light-weight and let migrated threads run. |
604 | * Be a little bit light-weight and let migrated threads run. |
602 | */ |
605 | */ |
603 | scheduler(); |
606 | scheduler(); |
604 | } |
607 | } |
605 | else { |
608 | else { |
606 | /* |
609 | /* |
607 | * We failed to migrate a single thread. |
610 | * We failed to migrate a single thread. |
608 | * Something more sophisticated should be done. |
611 | * Something more sophisticated should be done. |
609 | */ |
612 | */ |
610 | scheduler(); |
613 | scheduler(); |
611 | } |
614 | } |
612 | 615 | ||
613 | goto not_satisfied; |
616 | goto not_satisfied; |
614 | 617 | ||
615 | satisfied: |
618 | satisfied: |
616 | /* |
619 | /* |
617 | * Tell find_best_thread() to wake us up later again. |
620 | * Tell find_best_thread() to wake us up later again. |
618 | */ |
621 | */ |
619 | CPU->kcpulbstarted = 0; |
622 | CPU->kcpulbstarted = 0; |
620 | goto loop; |
623 | goto loop; |
621 | } |
624 | } |
622 | 625 | ||
623 | #endif /* __SMP__ */ |
626 | #endif /* __SMP__ */ |
624 | 627 |