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