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