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