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