Rev 898 | Rev 907 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 898 | Rev 906 | ||
---|---|---|---|
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/frame.h> |
32 | #include <mm/frame.h> |
33 | #include <mm/page.h> |
33 | #include <mm/page.h> |
34 | #include <mm/as.h> |
34 | #include <mm/as.h> |
35 | #include <arch/asm.h> |
35 | #include <arch/asm.h> |
36 | #include <arch/faddr.h> |
36 | #include <arch/faddr.h> |
37 | #include <arch/atomic.h> |
37 | #include <arch/atomic.h> |
38 | #include <synch/spinlock.h> |
38 | #include <synch/spinlock.h> |
39 | #include <config.h> |
39 | #include <config.h> |
40 | #include <context.h> |
40 | #include <context.h> |
41 | #include <func.h> |
41 | #include <func.h> |
42 | #include <arch.h> |
42 | #include <arch.h> |
43 | #include <adt/list.h> |
43 | #include <adt/list.h> |
44 | #include <panic.h> |
44 | #include <panic.h> |
45 | #include <typedefs.h> |
45 | #include <typedefs.h> |
46 | #include <cpu.h> |
46 | #include <cpu.h> |
47 | #include <print.h> |
47 | #include <print.h> |
48 | #include <debug.h> |
48 | #include <debug.h> |
49 | 49 | ||
50 | static void scheduler_separated_stack(void); |
50 | static void scheduler_separated_stack(void); |
51 | 51 | ||
52 | atomic_t nrdy; /**< Number of ready threads in the system. */ |
52 | atomic_t nrdy; /**< Number of ready threads in the system. */ |
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 | * THREAD->lock is locked on entry |
60 | * THREAD->lock is locked on entry |
61 | * |
61 | * |
62 | */ |
62 | */ |
63 | void before_thread_runs(void) |
63 | void before_thread_runs(void) |
64 | { |
64 | { |
65 | before_thread_runs_arch(); |
65 | before_thread_runs_arch(); |
66 | #ifdef CONFIG_FPU_LAZY |
66 | #ifdef CONFIG_FPU_LAZY |
67 | if(THREAD==CPU->fpu_owner) |
67 | if(THREAD==CPU->fpu_owner) |
68 | fpu_enable(); |
68 | fpu_enable(); |
69 | else |
69 | else |
70 | fpu_disable(); |
70 | fpu_disable(); |
71 | #else |
71 | #else |
72 | fpu_enable(); |
72 | fpu_enable(); |
73 | if (THREAD->fpu_context_exists) |
73 | if (THREAD->fpu_context_exists) |
74 | fpu_context_restore(&(THREAD->saved_fpu_context)); |
74 | fpu_context_restore(THREAD->saved_fpu_context); |
75 | else { |
75 | else { |
76 | fpu_init(&(THREAD->saved_fpu_context)); |
76 | fpu_init(); |
77 | THREAD->fpu_context_exists=1; |
77 | THREAD->fpu_context_exists=1; |
78 | } |
78 | } |
79 | #endif |
79 | #endif |
80 | } |
80 | } |
81 | 81 | ||
82 | /** Take actions after THREAD had run. |
82 | /** Take actions after THREAD had run. |
83 | * |
83 | * |
84 | * Perform actions that need to be |
84 | * Perform actions that need to be |
85 | * taken after the running thread |
85 | * taken after the running thread |
86 | * had been preempted by the scheduler. |
86 | * had been preempted by the scheduler. |
87 | * |
87 | * |
88 | * THREAD->lock is locked on entry |
88 | * THREAD->lock is locked on entry |
89 | * |
89 | * |
90 | */ |
90 | */ |
91 | void after_thread_ran(void) |
91 | void after_thread_ran(void) |
92 | { |
92 | { |
93 | after_thread_ran_arch(); |
93 | after_thread_ran_arch(); |
94 | } |
94 | } |
95 | 95 | ||
96 | #ifdef CONFIG_FPU_LAZY |
96 | #ifdef CONFIG_FPU_LAZY |
97 | void scheduler_fpu_lazy_request(void) |
97 | void scheduler_fpu_lazy_request(void) |
98 | { |
98 | { |
99 | fpu_enable(); |
99 | fpu_enable(); |
100 | spinlock_lock(&CPU->lock); |
100 | spinlock_lock(&CPU->lock); |
101 | 101 | ||
102 | /* Save old context */ |
102 | /* Save old context */ |
103 | if (CPU->fpu_owner != NULL) { |
103 | if (CPU->fpu_owner != NULL) { |
104 | spinlock_lock(&CPU->fpu_owner->lock); |
104 | spinlock_lock(&CPU->fpu_owner->lock); |
105 | fpu_context_save(&CPU->fpu_owner->saved_fpu_context); |
105 | fpu_context_save(CPU->fpu_owner->saved_fpu_context); |
106 | /* don't prevent migration */ |
106 | /* don't prevent migration */ |
107 | CPU->fpu_owner->fpu_context_engaged=0; |
107 | CPU->fpu_owner->fpu_context_engaged=0; |
108 | spinlock_unlock(&CPU->fpu_owner->lock); |
108 | spinlock_unlock(&CPU->fpu_owner->lock); |
109 | } |
109 | } |
110 | 110 | ||
111 | spinlock_lock(&THREAD->lock); |
111 | spinlock_lock(&THREAD->lock); |
112 | if (THREAD->fpu_context_exists) { |
112 | if (THREAD->fpu_context_exists) { |
113 | fpu_context_restore(&THREAD->saved_fpu_context); |
113 | fpu_context_restore(THREAD->saved_fpu_context); |
114 | } else { |
114 | } else { |
- | 115 | /* Allocate FPU context */ |
|
115 | fpu_init(&(THREAD->saved_fpu_context)); |
116 | if (!THREAD->saved_fpu_context) { |
- | 117 | /* Might sleep */ |
|
- | 118 | spinlock_unlock(&THREAD->lock); |
|
- | 119 | THREAD->saved_fpu_context = slab_alloc(fpu_context_slab, |
|
- | 120 | 0); |
|
- | 121 | spinlock_lock(&THREAD->lock); |
|
- | 122 | } |
|
- | 123 | fpu_init(); |
|
116 | THREAD->fpu_context_exists=1; |
124 | THREAD->fpu_context_exists=1; |
117 | } |
125 | } |
118 | CPU->fpu_owner=THREAD; |
126 | CPU->fpu_owner=THREAD; |
119 | THREAD->fpu_context_engaged = 1; |
127 | THREAD->fpu_context_engaged = 1; |
120 | spinlock_unlock(&THREAD->lock); |
128 | spinlock_unlock(&THREAD->lock); |
121 | 129 | ||
122 | spinlock_unlock(&CPU->lock); |
130 | spinlock_unlock(&CPU->lock); |
123 | } |
131 | } |
124 | #endif |
132 | #endif |
125 | 133 | ||
126 | /** Initialize scheduler |
134 | /** Initialize scheduler |
127 | * |
135 | * |
128 | * Initialize kernel scheduler. |
136 | * Initialize kernel scheduler. |
129 | * |
137 | * |
130 | */ |
138 | */ |
131 | void scheduler_init(void) |
139 | void scheduler_init(void) |
132 | { |
140 | { |
133 | } |
141 | } |
134 | 142 | ||
135 | /** Get thread to be scheduled |
143 | /** Get thread to be scheduled |
136 | * |
144 | * |
137 | * Get the optimal thread to be scheduled |
145 | * Get the optimal thread to be scheduled |
138 | * according to thread accounting and scheduler |
146 | * according to thread accounting and scheduler |
139 | * policy. |
147 | * policy. |
140 | * |
148 | * |
141 | * @return Thread to be scheduled. |
149 | * @return Thread to be scheduled. |
142 | * |
150 | * |
143 | */ |
151 | */ |
144 | static thread_t *find_best_thread(void) |
152 | static thread_t *find_best_thread(void) |
145 | { |
153 | { |
146 | thread_t *t; |
154 | thread_t *t; |
147 | runq_t *r; |
155 | runq_t *r; |
148 | int i; |
156 | int i; |
149 | 157 | ||
150 | ASSERT(CPU != NULL); |
158 | ASSERT(CPU != NULL); |
151 | 159 | ||
152 | loop: |
160 | loop: |
153 | interrupts_enable(); |
161 | interrupts_enable(); |
154 | 162 | ||
155 | if (atomic_get(&CPU->nrdy) == 0) { |
163 | if (atomic_get(&CPU->nrdy) == 0) { |
156 | /* |
164 | /* |
157 | * For there was nothing to run, the CPU goes to sleep |
165 | * For there was nothing to run, the CPU goes to sleep |
158 | * until a hardware interrupt or an IPI comes. |
166 | * until a hardware interrupt or an IPI comes. |
159 | * This improves energy saving and hyperthreading. |
167 | * This improves energy saving and hyperthreading. |
160 | */ |
168 | */ |
161 | 169 | ||
162 | /* |
170 | /* |
163 | * An interrupt might occur right now and wake up a thread. |
171 | * An interrupt might occur right now and wake up a thread. |
164 | * In such case, the CPU will continue to go to sleep |
172 | * In such case, the CPU will continue to go to sleep |
165 | * even though there is a runnable thread. |
173 | * even though there is a runnable thread. |
166 | */ |
174 | */ |
167 | 175 | ||
168 | cpu_sleep(); |
176 | cpu_sleep(); |
169 | goto loop; |
177 | goto loop; |
170 | } |
178 | } |
171 | 179 | ||
172 | interrupts_disable(); |
180 | interrupts_disable(); |
173 | 181 | ||
174 | for (i = 0; i<RQ_COUNT; i++) { |
182 | for (i = 0; i<RQ_COUNT; i++) { |
175 | r = &CPU->rq[i]; |
183 | r = &CPU->rq[i]; |
176 | spinlock_lock(&r->lock); |
184 | spinlock_lock(&r->lock); |
177 | if (r->n == 0) { |
185 | if (r->n == 0) { |
178 | /* |
186 | /* |
179 | * If this queue is empty, try a lower-priority queue. |
187 | * If this queue is empty, try a lower-priority queue. |
180 | */ |
188 | */ |
181 | spinlock_unlock(&r->lock); |
189 | spinlock_unlock(&r->lock); |
182 | continue; |
190 | continue; |
183 | } |
191 | } |
184 | 192 | ||
185 | atomic_dec(&CPU->nrdy); |
193 | atomic_dec(&CPU->nrdy); |
186 | atomic_dec(&nrdy); |
194 | atomic_dec(&nrdy); |
187 | r->n--; |
195 | r->n--; |
188 | 196 | ||
189 | /* |
197 | /* |
190 | * Take the first thread from the queue. |
198 | * Take the first thread from the queue. |
191 | */ |
199 | */ |
192 | t = list_get_instance(r->rq_head.next, thread_t, rq_link); |
200 | t = list_get_instance(r->rq_head.next, thread_t, rq_link); |
193 | list_remove(&t->rq_link); |
201 | list_remove(&t->rq_link); |
194 | 202 | ||
195 | spinlock_unlock(&r->lock); |
203 | spinlock_unlock(&r->lock); |
196 | 204 | ||
197 | spinlock_lock(&t->lock); |
205 | spinlock_lock(&t->lock); |
198 | t->cpu = CPU; |
206 | t->cpu = CPU; |
199 | 207 | ||
200 | t->ticks = us2ticks((i+1)*10000); |
208 | t->ticks = us2ticks((i+1)*10000); |
201 | t->priority = i; /* correct rq index */ |
209 | t->priority = i; /* correct rq index */ |
202 | 210 | ||
203 | /* |
211 | /* |
204 | * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge. |
212 | * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge. |
205 | */ |
213 | */ |
206 | t->flags &= ~X_STOLEN; |
214 | t->flags &= ~X_STOLEN; |
207 | spinlock_unlock(&t->lock); |
215 | spinlock_unlock(&t->lock); |
208 | 216 | ||
209 | return t; |
217 | return t; |
210 | } |
218 | } |
211 | goto loop; |
219 | goto loop; |
212 | 220 | ||
213 | } |
221 | } |
214 | 222 | ||
215 | /** Prevent rq starvation |
223 | /** Prevent rq starvation |
216 | * |
224 | * |
217 | * Prevent low priority threads from starving in rq's. |
225 | * Prevent low priority threads from starving in rq's. |
218 | * |
226 | * |
219 | * When the function decides to relink rq's, it reconnects |
227 | * When the function decides to relink rq's, it reconnects |
220 | * respective pointers so that in result threads with 'pri' |
228 | * respective pointers so that in result threads with 'pri' |
221 | * greater or equal 'start' are moved to a higher-priority queue. |
229 | * greater or equal 'start' are moved to a higher-priority queue. |
222 | * |
230 | * |
223 | * @param start Threshold priority. |
231 | * @param start Threshold priority. |
224 | * |
232 | * |
225 | */ |
233 | */ |
226 | static void relink_rq(int start) |
234 | static void relink_rq(int start) |
227 | { |
235 | { |
228 | link_t head; |
236 | link_t head; |
229 | runq_t *r; |
237 | runq_t *r; |
230 | int i, n; |
238 | int i, n; |
231 | 239 | ||
232 | list_initialize(&head); |
240 | list_initialize(&head); |
233 | spinlock_lock(&CPU->lock); |
241 | spinlock_lock(&CPU->lock); |
234 | if (CPU->needs_relink > NEEDS_RELINK_MAX) { |
242 | if (CPU->needs_relink > NEEDS_RELINK_MAX) { |
235 | for (i = start; i<RQ_COUNT-1; i++) { |
243 | for (i = start; i<RQ_COUNT-1; i++) { |
236 | /* remember and empty rq[i + 1] */ |
244 | /* remember and empty rq[i + 1] */ |
237 | r = &CPU->rq[i + 1]; |
245 | r = &CPU->rq[i + 1]; |
238 | spinlock_lock(&r->lock); |
246 | spinlock_lock(&r->lock); |
239 | list_concat(&head, &r->rq_head); |
247 | list_concat(&head, &r->rq_head); |
240 | n = r->n; |
248 | n = r->n; |
241 | r->n = 0; |
249 | r->n = 0; |
242 | spinlock_unlock(&r->lock); |
250 | spinlock_unlock(&r->lock); |
243 | 251 | ||
244 | /* append rq[i + 1] to rq[i] */ |
252 | /* append rq[i + 1] to rq[i] */ |
245 | r = &CPU->rq[i]; |
253 | r = &CPU->rq[i]; |
246 | spinlock_lock(&r->lock); |
254 | spinlock_lock(&r->lock); |
247 | list_concat(&r->rq_head, &head); |
255 | list_concat(&r->rq_head, &head); |
248 | r->n += n; |
256 | r->n += n; |
249 | spinlock_unlock(&r->lock); |
257 | spinlock_unlock(&r->lock); |
250 | } |
258 | } |
251 | CPU->needs_relink = 0; |
259 | CPU->needs_relink = 0; |
252 | } |
260 | } |
253 | spinlock_unlock(&CPU->lock); |
261 | spinlock_unlock(&CPU->lock); |
254 | 262 | ||
255 | } |
263 | } |
256 | 264 | ||
257 | /** The scheduler |
265 | /** The scheduler |
258 | * |
266 | * |
259 | * The thread scheduling procedure. |
267 | * The thread scheduling procedure. |
260 | * Passes control directly to |
268 | * Passes control directly to |
261 | * scheduler_separated_stack(). |
269 | * scheduler_separated_stack(). |
262 | * |
270 | * |
263 | */ |
271 | */ |
264 | void scheduler(void) |
272 | void scheduler(void) |
265 | { |
273 | { |
266 | volatile ipl_t ipl; |
274 | volatile ipl_t ipl; |
267 | 275 | ||
268 | ASSERT(CPU != NULL); |
276 | ASSERT(CPU != NULL); |
269 | 277 | ||
270 | ipl = interrupts_disable(); |
278 | ipl = interrupts_disable(); |
271 | 279 | ||
272 | if (atomic_get(&haltstate)) |
280 | if (atomic_get(&haltstate)) |
273 | halt(); |
281 | halt(); |
274 | 282 | ||
275 | if (THREAD) { |
283 | if (THREAD) { |
276 | spinlock_lock(&THREAD->lock); |
284 | spinlock_lock(&THREAD->lock); |
277 | #ifndef CONFIG_FPU_LAZY |
285 | #ifndef CONFIG_FPU_LAZY |
278 | fpu_context_save(&(THREAD->saved_fpu_context)); |
286 | fpu_context_save(THREAD->saved_fpu_context); |
279 | #endif |
287 | #endif |
280 | if (!context_save(&THREAD->saved_context)) { |
288 | if (!context_save(&THREAD->saved_context)) { |
281 | /* |
289 | /* |
282 | * This is the place where threads leave scheduler(); |
290 | * This is the place where threads leave scheduler(); |
283 | */ |
291 | */ |
284 | spinlock_unlock(&THREAD->lock); |
292 | spinlock_unlock(&THREAD->lock); |
285 | interrupts_restore(THREAD->saved_context.ipl); |
293 | interrupts_restore(THREAD->saved_context.ipl); |
286 | return; |
294 | return; |
287 | } |
295 | } |
288 | 296 | ||
289 | /* |
297 | /* |
290 | * Interrupt priority level of preempted thread is recorded here |
298 | * Interrupt priority level of preempted thread is recorded here |
291 | * to facilitate scheduler() invocations from interrupts_disable()'d |
299 | * to facilitate scheduler() invocations from interrupts_disable()'d |
292 | * code (e.g. waitq_sleep_timeout()). |
300 | * code (e.g. waitq_sleep_timeout()). |
293 | */ |
301 | */ |
294 | THREAD->saved_context.ipl = ipl; |
302 | THREAD->saved_context.ipl = ipl; |
295 | } |
303 | } |
296 | 304 | ||
297 | /* |
305 | /* |
298 | * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, VM |
306 | * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, VM |
299 | * and preemption counter. At this point THE could be coming either |
307 | * and preemption counter. At this point THE could be coming either |
300 | * from THREAD's or CPU's stack. |
308 | * from THREAD's or CPU's stack. |
301 | */ |
309 | */ |
302 | the_copy(THE, (the_t *) CPU->stack); |
310 | the_copy(THE, (the_t *) CPU->stack); |
303 | 311 | ||
304 | /* |
312 | /* |
305 | * We may not keep the old stack. |
313 | * We may not keep the old stack. |
306 | * Reason: If we kept the old stack and got blocked, for instance, in |
314 | * Reason: If we kept the old stack and got blocked, for instance, in |
307 | * find_best_thread(), the old thread could get rescheduled by another |
315 | * find_best_thread(), the old thread could get rescheduled by another |
308 | * CPU and overwrite the part of its own stack that was also used by |
316 | * CPU and overwrite the part of its own stack that was also used by |
309 | * the scheduler on this CPU. |
317 | * the scheduler on this CPU. |
310 | * |
318 | * |
311 | * Moreover, we have to bypass the compiler-generated POP sequence |
319 | * Moreover, we have to bypass the compiler-generated POP sequence |
312 | * which is fooled by SP being set to the very top of the stack. |
320 | * which is fooled by SP being set to the very top of the stack. |
313 | * Therefore the scheduler() function continues in |
321 | * Therefore the scheduler() function continues in |
314 | * scheduler_separated_stack(). |
322 | * scheduler_separated_stack(). |
315 | */ |
323 | */ |
316 | context_save(&CPU->saved_context); |
324 | context_save(&CPU->saved_context); |
317 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE); |
325 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE); |
318 | context_restore(&CPU->saved_context); |
326 | context_restore(&CPU->saved_context); |
319 | /* not reached */ |
327 | /* not reached */ |
320 | } |
328 | } |
321 | 329 | ||
322 | /** Scheduler stack switch wrapper |
330 | /** Scheduler stack switch wrapper |
323 | * |
331 | * |
324 | * Second part of the scheduler() function |
332 | * Second part of the scheduler() function |
325 | * using new stack. Handling the actual context |
333 | * using new stack. Handling the actual context |
326 | * switch to a new thread. |
334 | * switch to a new thread. |
327 | * |
335 | * |
328 | * Assume THREAD->lock is held. |
336 | * Assume THREAD->lock is held. |
329 | */ |
337 | */ |
330 | void scheduler_separated_stack(void) |
338 | void scheduler_separated_stack(void) |
331 | { |
339 | { |
332 | int priority; |
340 | int priority; |
333 | 341 | ||
334 | ASSERT(CPU != NULL); |
342 | ASSERT(CPU != NULL); |
335 | 343 | ||
336 | if (THREAD) { |
344 | if (THREAD) { |
337 | /* must be run after the switch to scheduler stack */ |
345 | /* must be run after the switch to scheduler stack */ |
338 | after_thread_ran(); |
346 | after_thread_ran(); |
339 | 347 | ||
340 | switch (THREAD->state) { |
348 | switch (THREAD->state) { |
341 | case Running: |
349 | case Running: |
342 | THREAD->state = Ready; |
350 | THREAD->state = Ready; |
343 | spinlock_unlock(&THREAD->lock); |
351 | spinlock_unlock(&THREAD->lock); |
344 | thread_ready(THREAD); |
352 | thread_ready(THREAD); |
345 | break; |
353 | break; |
346 | 354 | ||
347 | case Exiting: |
355 | case Exiting: |
348 | thread_destroy(THREAD); |
356 | thread_destroy(THREAD); |
349 | break; |
357 | break; |
350 | 358 | ||
351 | case Sleeping: |
359 | case Sleeping: |
352 | /* |
360 | /* |
353 | * Prefer the thread after it's woken up. |
361 | * Prefer the thread after it's woken up. |
354 | */ |
362 | */ |
355 | THREAD->priority = -1; |
363 | THREAD->priority = -1; |
356 | 364 | ||
357 | /* |
365 | /* |
358 | * We need to release wq->lock which we locked in waitq_sleep(). |
366 | * We need to release wq->lock which we locked in waitq_sleep(). |
359 | * Address of wq->lock is kept in THREAD->sleep_queue. |
367 | * Address of wq->lock is kept in THREAD->sleep_queue. |
360 | */ |
368 | */ |
361 | spinlock_unlock(&THREAD->sleep_queue->lock); |
369 | spinlock_unlock(&THREAD->sleep_queue->lock); |
362 | 370 | ||
363 | /* |
371 | /* |
364 | * Check for possible requests for out-of-context invocation. |
372 | * Check for possible requests for out-of-context invocation. |
365 | */ |
373 | */ |
366 | if (THREAD->call_me) { |
374 | if (THREAD->call_me) { |
367 | THREAD->call_me(THREAD->call_me_with); |
375 | THREAD->call_me(THREAD->call_me_with); |
368 | THREAD->call_me = NULL; |
376 | THREAD->call_me = NULL; |
369 | THREAD->call_me_with = NULL; |
377 | THREAD->call_me_with = NULL; |
370 | } |
378 | } |
371 | 379 | ||
372 | spinlock_unlock(&THREAD->lock); |
380 | spinlock_unlock(&THREAD->lock); |
373 | 381 | ||
374 | break; |
382 | break; |
375 | 383 | ||
376 | default: |
384 | default: |
377 | /* |
385 | /* |
378 | * Entering state is unexpected. |
386 | * Entering state is unexpected. |
379 | */ |
387 | */ |
380 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]); |
388 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]); |
381 | break; |
389 | break; |
382 | } |
390 | } |
383 | 391 | ||
384 | THREAD = NULL; |
392 | THREAD = NULL; |
385 | } |
393 | } |
386 | 394 | ||
387 | THREAD = find_best_thread(); |
395 | THREAD = find_best_thread(); |
388 | 396 | ||
389 | spinlock_lock(&THREAD->lock); |
397 | spinlock_lock(&THREAD->lock); |
390 | priority = THREAD->priority; |
398 | priority = THREAD->priority; |
391 | spinlock_unlock(&THREAD->lock); |
399 | spinlock_unlock(&THREAD->lock); |
392 | 400 | ||
393 | relink_rq(priority); |
401 | relink_rq(priority); |
394 | 402 | ||
395 | spinlock_lock(&THREAD->lock); |
403 | spinlock_lock(&THREAD->lock); |
396 | 404 | ||
397 | /* |
405 | /* |
398 | * If both the old and the new task are the same, lots of work is avoided. |
406 | * If both the old and the new task are the same, lots of work is avoided. |
399 | */ |
407 | */ |
400 | if (TASK != THREAD->task) { |
408 | if (TASK != THREAD->task) { |
401 | as_t *as1 = NULL; |
409 | as_t *as1 = NULL; |
402 | as_t *as2; |
410 | as_t *as2; |
403 | 411 | ||
404 | if (TASK) { |
412 | if (TASK) { |
405 | spinlock_lock(&TASK->lock); |
413 | spinlock_lock(&TASK->lock); |
406 | as1 = TASK->as; |
414 | as1 = TASK->as; |
407 | spinlock_unlock(&TASK->lock); |
415 | spinlock_unlock(&TASK->lock); |
408 | } |
416 | } |
409 | 417 | ||
410 | spinlock_lock(&THREAD->task->lock); |
418 | spinlock_lock(&THREAD->task->lock); |
411 | as2 = THREAD->task->as; |
419 | as2 = THREAD->task->as; |
412 | spinlock_unlock(&THREAD->task->lock); |
420 | spinlock_unlock(&THREAD->task->lock); |
413 | 421 | ||
414 | /* |
422 | /* |
415 | * Note that it is possible for two tasks to share one address space. |
423 | * Note that it is possible for two tasks to share one address space. |
416 | */ |
424 | */ |
417 | if (as1 != as2) { |
425 | if (as1 != as2) { |
418 | /* |
426 | /* |
419 | * Both tasks and address spaces are different. |
427 | * Both tasks and address spaces are different. |
420 | * Replace the old one with the new one. |
428 | * Replace the old one with the new one. |
421 | */ |
429 | */ |
422 | as_switch(as1, as2); |
430 | as_switch(as1, as2); |
423 | } |
431 | } |
424 | TASK = THREAD->task; |
432 | TASK = THREAD->task; |
425 | } |
433 | } |
426 | 434 | ||
427 | THREAD->state = Running; |
435 | THREAD->state = Running; |
428 | 436 | ||
429 | #ifdef SCHEDULER_VERBOSE |
437 | #ifdef SCHEDULER_VERBOSE |
430 | printf("cpu%d: tid %d (priority=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks, atomic_get(&CPU->nrdy)); |
438 | printf("cpu%d: tid %d (priority=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks, atomic_get(&CPU->nrdy)); |
431 | #endif |
439 | #endif |
432 | 440 | ||
433 | /* |
441 | /* |
434 | * Some architectures provide late kernel PA2KA(identity) |
442 | * Some architectures provide late kernel PA2KA(identity) |
435 | * mapping in a page fault handler. However, the page fault |
443 | * mapping in a page fault handler. However, the page fault |
436 | * handler uses the kernel stack of the running thread and |
444 | * handler uses the kernel stack of the running thread and |
437 | * therefore cannot be used to map it. The kernel stack, if |
445 | * therefore cannot be used to map it. The kernel stack, if |
438 | * necessary, is to be mapped in before_thread_runs(). This |
446 | * necessary, is to be mapped in before_thread_runs(). This |
439 | * function must be executed before the switch to the new stack. |
447 | * function must be executed before the switch to the new stack. |
440 | */ |
448 | */ |
441 | before_thread_runs(); |
449 | before_thread_runs(); |
442 | 450 | ||
443 | /* |
451 | /* |
444 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to thread's stack. |
452 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to thread's stack. |
445 | */ |
453 | */ |
446 | the_copy(THE, (the_t *) THREAD->kstack); |
454 | the_copy(THE, (the_t *) THREAD->kstack); |
447 | 455 | ||
448 | context_restore(&THREAD->saved_context); |
456 | context_restore(&THREAD->saved_context); |
449 | /* not reached */ |
457 | /* not reached */ |
450 | } |
458 | } |
451 | 459 | ||
452 | #ifdef CONFIG_SMP |
460 | #ifdef CONFIG_SMP |
453 | /** Load balancing thread |
461 | /** Load balancing thread |
454 | * |
462 | * |
455 | * SMP load balancing thread, supervising thread supplies |
463 | * SMP load balancing thread, supervising thread supplies |
456 | * for the CPU it's wired to. |
464 | * for the CPU it's wired to. |
457 | * |
465 | * |
458 | * @param arg Generic thread argument (unused). |
466 | * @param arg Generic thread argument (unused). |
459 | * |
467 | * |
460 | */ |
468 | */ |
461 | void kcpulb(void *arg) |
469 | void kcpulb(void *arg) |
462 | { |
470 | { |
463 | thread_t *t; |
471 | thread_t *t; |
464 | int count, average, i, j, k = 0; |
472 | int count, average, i, j, k = 0; |
465 | ipl_t ipl; |
473 | ipl_t ipl; |
466 | 474 | ||
467 | loop: |
475 | loop: |
468 | /* |
476 | /* |
469 | * Work in 1s intervals. |
477 | * Work in 1s intervals. |
470 | */ |
478 | */ |
471 | thread_sleep(1); |
479 | thread_sleep(1); |
472 | 480 | ||
473 | not_satisfied: |
481 | not_satisfied: |
474 | /* |
482 | /* |
475 | * Calculate the number of threads that will be migrated/stolen from |
483 | * Calculate the number of threads that will be migrated/stolen from |
476 | * other CPU's. Note that situation can have changed between two |
484 | * other CPU's. Note that situation can have changed between two |
477 | * passes. Each time get the most up to date counts. |
485 | * passes. Each time get the most up to date counts. |
478 | */ |
486 | */ |
479 | average = atomic_get(&nrdy) / config.cpu_active + 1; |
487 | average = atomic_get(&nrdy) / config.cpu_active + 1; |
480 | count = average - atomic_get(&CPU->nrdy); |
488 | count = average - atomic_get(&CPU->nrdy); |
481 | 489 | ||
482 | if (count <= 0) |
490 | if (count <= 0) |
483 | goto satisfied; |
491 | goto satisfied; |
484 | 492 | ||
485 | /* |
493 | /* |
486 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last. |
494 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last. |
487 | */ |
495 | */ |
488 | for (j=RQ_COUNT-1; j >= 0; j--) { |
496 | for (j=RQ_COUNT-1; j >= 0; j--) { |
489 | for (i=0; i < config.cpu_active; i++) { |
497 | for (i=0; i < config.cpu_active; i++) { |
490 | link_t *l; |
498 | link_t *l; |
491 | runq_t *r; |
499 | runq_t *r; |
492 | cpu_t *cpu; |
500 | cpu_t *cpu; |
493 | 501 | ||
494 | cpu = &cpus[(i + k) % config.cpu_active]; |
502 | cpu = &cpus[(i + k) % config.cpu_active]; |
495 | 503 | ||
496 | /* |
504 | /* |
497 | * Not interested in ourselves. |
505 | * Not interested in ourselves. |
498 | * Doesn't require interrupt disabling for kcpulb is X_WIRED. |
506 | * Doesn't require interrupt disabling for kcpulb is X_WIRED. |
499 | */ |
507 | */ |
500 | if (CPU == cpu) |
508 | if (CPU == cpu) |
501 | continue; |
509 | continue; |
502 | if (atomic_get(&cpu->nrdy) <= average) |
510 | if (atomic_get(&cpu->nrdy) <= average) |
503 | continue; |
511 | continue; |
504 | 512 | ||
505 | ipl = interrupts_disable(); |
513 | ipl = interrupts_disable(); |
506 | r = &cpu->rq[j]; |
514 | r = &cpu->rq[j]; |
507 | spinlock_lock(&r->lock); |
515 | spinlock_lock(&r->lock); |
508 | if (r->n == 0) { |
516 | if (r->n == 0) { |
509 | spinlock_unlock(&r->lock); |
517 | spinlock_unlock(&r->lock); |
510 | interrupts_restore(ipl); |
518 | interrupts_restore(ipl); |
511 | continue; |
519 | continue; |
512 | } |
520 | } |
513 | 521 | ||
514 | t = NULL; |
522 | t = NULL; |
515 | l = r->rq_head.prev; /* search rq from the back */ |
523 | l = r->rq_head.prev; /* search rq from the back */ |
516 | while (l != &r->rq_head) { |
524 | while (l != &r->rq_head) { |
517 | t = list_get_instance(l, thread_t, rq_link); |
525 | t = list_get_instance(l, thread_t, rq_link); |
518 | /* |
526 | /* |
519 | * We don't want to steal CPU-wired threads neither threads already stolen. |
527 | * We don't want to steal CPU-wired threads neither threads already stolen. |
520 | * The latter prevents threads from migrating between CPU's without ever being run. |
528 | * The latter prevents threads from migrating between CPU's without ever being run. |
521 | * We don't want to steal threads whose FPU context is still in CPU. |
529 | * We don't want to steal threads whose FPU context is still in CPU. |
522 | */ |
530 | */ |
523 | spinlock_lock(&t->lock); |
531 | spinlock_lock(&t->lock); |
524 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) { |
532 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) { |
525 | /* |
533 | /* |
526 | * Remove t from r. |
534 | * Remove t from r. |
527 | */ |
535 | */ |
528 | spinlock_unlock(&t->lock); |
536 | spinlock_unlock(&t->lock); |
529 | 537 | ||
530 | atomic_dec(&cpu->nrdy); |
538 | atomic_dec(&cpu->nrdy); |
531 | atomic_dec(&nrdy); |
539 | atomic_dec(&nrdy); |
532 | 540 | ||
533 | r->n--; |
541 | r->n--; |
534 | list_remove(&t->rq_link); |
542 | list_remove(&t->rq_link); |
535 | 543 | ||
536 | break; |
544 | break; |
537 | } |
545 | } |
538 | spinlock_unlock(&t->lock); |
546 | spinlock_unlock(&t->lock); |
539 | l = l->prev; |
547 | l = l->prev; |
540 | t = NULL; |
548 | t = NULL; |
541 | } |
549 | } |
542 | spinlock_unlock(&r->lock); |
550 | spinlock_unlock(&r->lock); |
543 | 551 | ||
544 | if (t) { |
552 | if (t) { |
545 | /* |
553 | /* |
546 | * Ready t on local CPU |
554 | * Ready t on local CPU |
547 | */ |
555 | */ |
548 | spinlock_lock(&t->lock); |
556 | spinlock_lock(&t->lock); |
549 | #ifdef KCPULB_VERBOSE |
557 | #ifdef KCPULB_VERBOSE |
550 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, atomic_get(&CPU->nrdy), atomic_get(&nrdy) / config.cpu_active); |
558 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, atomic_get(&CPU->nrdy), atomic_get(&nrdy) / config.cpu_active); |
551 | #endif |
559 | #endif |
552 | t->flags |= X_STOLEN; |
560 | t->flags |= X_STOLEN; |
553 | spinlock_unlock(&t->lock); |
561 | spinlock_unlock(&t->lock); |
554 | 562 | ||
555 | thread_ready(t); |
563 | thread_ready(t); |
556 | 564 | ||
557 | interrupts_restore(ipl); |
565 | interrupts_restore(ipl); |
558 | 566 | ||
559 | if (--count == 0) |
567 | if (--count == 0) |
560 | goto satisfied; |
568 | goto satisfied; |
561 | 569 | ||
562 | /* |
570 | /* |
563 | * We are not satisfied yet, focus on another CPU next time. |
571 | * We are not satisfied yet, focus on another CPU next time. |
564 | */ |
572 | */ |
565 | k++; |
573 | k++; |
566 | 574 | ||
567 | continue; |
575 | continue; |
568 | } |
576 | } |
569 | interrupts_restore(ipl); |
577 | interrupts_restore(ipl); |
570 | } |
578 | } |
571 | } |
579 | } |
572 | 580 | ||
573 | if (atomic_get(&CPU->nrdy)) { |
581 | if (atomic_get(&CPU->nrdy)) { |
574 | /* |
582 | /* |
575 | * Be a little bit light-weight and let migrated threads run. |
583 | * Be a little bit light-weight and let migrated threads run. |
576 | */ |
584 | */ |
577 | scheduler(); |
585 | scheduler(); |
578 | } else { |
586 | } else { |
579 | /* |
587 | /* |
580 | * We failed to migrate a single thread. |
588 | * We failed to migrate a single thread. |
581 | * Give up this turn. |
589 | * Give up this turn. |
582 | */ |
590 | */ |
583 | goto loop; |
591 | goto loop; |
584 | } |
592 | } |
585 | 593 | ||
586 | goto not_satisfied; |
594 | goto not_satisfied; |
587 | 595 | ||
588 | satisfied: |
596 | satisfied: |
589 | goto loop; |
597 | goto loop; |
590 | } |
598 | } |
591 | 599 | ||
592 | #endif /* CONFIG_SMP */ |
600 | #endif /* CONFIG_SMP */ |
593 | 601 | ||
594 | 602 | ||
595 | /** Print information about threads & scheduler queues */ |
603 | /** Print information about threads & scheduler queues */ |
596 | void sched_print_list(void) |
604 | void sched_print_list(void) |
597 | { |
605 | { |
598 | ipl_t ipl; |
606 | ipl_t ipl; |
599 | int cpu,i; |
607 | int cpu,i; |
600 | runq_t *r; |
608 | runq_t *r; |
601 | thread_t *t; |
609 | thread_t *t; |
602 | link_t *cur; |
610 | link_t *cur; |
603 | 611 | ||
604 | /* We are going to mess with scheduler structures, |
612 | /* We are going to mess with scheduler structures, |
605 | * let's not be interrupted */ |
613 | * let's not be interrupted */ |
606 | ipl = interrupts_disable(); |
614 | ipl = interrupts_disable(); |
607 | printf("Scheduler dump:\n"); |
615 | printf("Scheduler dump:\n"); |
608 | for (cpu=0;cpu < config.cpu_count; cpu++) { |
616 | for (cpu=0;cpu < config.cpu_count; cpu++) { |
609 | 617 | ||
610 | if (!cpus[cpu].active) |
618 | if (!cpus[cpu].active) |
611 | continue; |
619 | continue; |
612 | 620 | ||
613 | spinlock_lock(&cpus[cpu].lock); |
621 | spinlock_lock(&cpus[cpu].lock); |
614 | printf("cpu%d: nrdy: %d, needs_relink: %d\n", |
622 | printf("cpu%d: nrdy: %d, needs_relink: %d\n", |
615 | cpus[cpu].id, atomic_get(&cpus[cpu].nrdy), cpus[cpu].needs_relink); |
623 | cpus[cpu].id, atomic_get(&cpus[cpu].nrdy), cpus[cpu].needs_relink); |
616 | 624 | ||
617 | for (i=0; i<RQ_COUNT; i++) { |
625 | for (i=0; i<RQ_COUNT; i++) { |
618 | r = &cpus[cpu].rq[i]; |
626 | r = &cpus[cpu].rq[i]; |
619 | spinlock_lock(&r->lock); |
627 | spinlock_lock(&r->lock); |
620 | if (!r->n) { |
628 | if (!r->n) { |
621 | spinlock_unlock(&r->lock); |
629 | spinlock_unlock(&r->lock); |
622 | continue; |
630 | continue; |
623 | } |
631 | } |
624 | printf("\trq[%d]: ", i); |
632 | printf("\trq[%d]: ", i); |
625 | for (cur=r->rq_head.next; cur!=&r->rq_head; cur=cur->next) { |
633 | for (cur=r->rq_head.next; cur!=&r->rq_head; cur=cur->next) { |
626 | t = list_get_instance(cur, thread_t, rq_link); |
634 | t = list_get_instance(cur, thread_t, rq_link); |
627 | printf("%d(%s) ", t->tid, |
635 | printf("%d(%s) ", t->tid, |
628 | thread_states[t->state]); |
636 | thread_states[t->state]); |
629 | } |
637 | } |
630 | printf("\n"); |
638 | printf("\n"); |
631 | spinlock_unlock(&r->lock); |
639 | spinlock_unlock(&r->lock); |
632 | } |
640 | } |
633 | spinlock_unlock(&cpus[cpu].lock); |
641 | spinlock_unlock(&cpus[cpu].lock); |
634 | } |
642 | } |
635 | 643 | ||
636 | interrupts_restore(ipl); |
644 | interrupts_restore(ipl); |
637 | } |
645 | } |
638 | 646 |