os161-1.99
 All Data Structures
thread.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Core kernel-level thread system.
00032  */
00033 
00034 #define THREADINLINE
00035 
00036 #include <types.h>
00037 #include <kern/errno.h>
00038 #include <lib.h>
00039 #include <array.h>
00040 #include <cpu.h>
00041 #include <spl.h>
00042 #include <spinlock.h>
00043 #include <wchan.h>
00044 #include <thread.h>
00045 #include <threadlist.h>
00046 #include <threadprivate.h>
00047 #include <proc.h>
00048 #include <current.h>
00049 #include <synch.h>
00050 #include <addrspace.h>
00051 #include <mainbus.h>
00052 #include <vnode.h>
00053 
00054 #include "opt-synchprobs.h"
00055 
00056 
00057 /* Magic number used as a guard value on kernel thread stacks. */
00058 #define THREAD_STACK_MAGIC 0xbaadf00d
00059 
00060 /* Wait channel. */
00061 struct wchan {
00062         const char *wc_name;            /* name for this channel */
00063         struct threadlist wc_threads;   /* list of waiting threads */
00064         struct spinlock wc_lock;        /* lock for mutual exclusion */
00065 };
00066 
00067 /* Master array of CPUs. */
00068 DECLARRAY(cpu);
00069 DEFARRAY(cpu, /*no inline*/ );
00070 static struct cpuarray allcpus;
00071 
00072 /* Used to wait for secondary CPUs to come online. */
00073 static struct semaphore *cpu_startup_sem;
00074 
00075 ////////////////////////////////////////////////////////////
00076 
00077 /*
00078  * Stick a magic number on the bottom end of the stack. This will
00079  * (sometimes) catch kernel stack overflows. Use thread_checkstack()
00080  * to test this.
00081  */
00082 static
00083 void
00084 thread_checkstack_init(struct thread *thread)
00085 {
00086         ((uint32_t *)thread->t_stack)[0] = THREAD_STACK_MAGIC;
00087         ((uint32_t *)thread->t_stack)[1] = THREAD_STACK_MAGIC;
00088         ((uint32_t *)thread->t_stack)[2] = THREAD_STACK_MAGIC;
00089         ((uint32_t *)thread->t_stack)[3] = THREAD_STACK_MAGIC;
00090 }
00091 
00092 /*
00093  * Check the magic number we put on the bottom end of the stack in
00094  * thread_checkstack_init. If these assertions go off, it most likely
00095  * means you overflowed your stack at some point, which can cause all
00096  * kinds of mysterious other things to happen.
00097  *
00098  * Note that when ->t_stack is NULL, which is the case if the stack
00099  * cannot be freed (which in turn is the case if the stack is the boot
00100  * stack, and the thread is the boot thread) this doesn't do anything.
00101  */
00102 static
00103 void
00104 thread_checkstack(struct thread *thread)
00105 {
00106         if (thread->t_stack != NULL) {
00107                 KASSERT(((uint32_t*)thread->t_stack)[0] == THREAD_STACK_MAGIC);
00108                 KASSERT(((uint32_t*)thread->t_stack)[1] == THREAD_STACK_MAGIC);
00109                 KASSERT(((uint32_t*)thread->t_stack)[2] == THREAD_STACK_MAGIC);
00110                 KASSERT(((uint32_t*)thread->t_stack)[3] == THREAD_STACK_MAGIC);
00111         }
00112 }
00113 
00114 /*
00115  * Create a thread. This is used both to create a first thread
00116  * for each CPU and to create subsequent forked threads.
00117  */
00118 static
00119 struct thread *
00120 thread_create(const char *name)
00121 {
00122         struct thread *thread;
00123 
00124         DEBUGASSERT(name != NULL);
00125 
00126         thread = kmalloc(sizeof(*thread));
00127         if (thread == NULL) {
00128                 return NULL;
00129         }
00130 
00131         thread->t_name = kstrdup(name);
00132         if (thread->t_name == NULL) {
00133                 kfree(thread);
00134                 return NULL;
00135         }
00136         thread->t_wchan_name = "NEW";
00137         thread->t_state = S_READY;
00138 
00139         /* Thread subsystem fields */
00140         thread_machdep_init(&thread->t_machdep);
00141         threadlistnode_init(&thread->t_listnode, thread);
00142         thread->t_stack = NULL;
00143         thread->t_context = NULL;
00144         thread->t_cpu = NULL;
00145         thread->t_proc = NULL;
00146 
00147         /* Interrupt state fields */
00148         thread->t_in_interrupt = false;
00149         thread->t_curspl = IPL_HIGH;
00150         thread->t_iplhigh_count = 1; /* corresponding to t_curspl */
00151 
00152         /* If you add to struct thread, be sure to initialize here */
00153 
00154         return thread;
00155 }
00156 
00157 /*
00158  * Create a CPU structure. This is used for the bootup CPU and
00159  * also for secondary CPUs.
00160  *
00161  * The hardware number (the number assigned by firmware or system
00162  * board config or whatnot) is tracked separately because it is not
00163  * necessarily anything sane or meaningful.
00164  */
00165 struct cpu *
00166 cpu_create(unsigned hardware_number)
00167 {
00168         struct cpu *c;
00169         int result;
00170         char namebuf[16];
00171 
00172         c = kmalloc(sizeof(*c));
00173         if (c == NULL) {
00174                 panic("cpu_create: Out of memory\n");
00175         }
00176         
00177         c->c_self = c;
00178         c->c_hardware_number = hardware_number;
00179 
00180         c->c_curthread = NULL;
00181         threadlist_init(&c->c_zombies);
00182         c->c_hardclocks = 0;
00183 
00184         c->c_isidle = false;
00185         threadlist_init(&c->c_runqueue);
00186         spinlock_init(&c->c_runqueue_lock);
00187 
00188         c->c_ipi_pending = 0;
00189         c->c_numshootdown = 0;
00190         spinlock_init(&c->c_ipi_lock);
00191 
00192         result = cpuarray_add(&allcpus, c, &c->c_number);
00193         if (result != 0) {
00194                 panic("cpu_create: array_add: %s\n", strerror(result));
00195         }
00196 
00197         snprintf(namebuf, sizeof(namebuf), "<boot #%d>", c->c_number);
00198         c->c_curthread = thread_create(namebuf);
00199         if (c->c_curthread == NULL) {
00200                 panic("cpu_create: thread_create failed\n");
00201         }
00202         result = proc_addthread(kproc, c->c_curthread);
00203         if (result) {
00204                 panic("cpu_create: proc_addthread:: %s\n", strerror(result));
00205         }
00206 
00207         if (c->c_number == 0) {
00208                 /*
00209                  * Leave c->c_curthread->t_stack NULL for the boot
00210                  * cpu. This means we're using the boot stack, which
00211                  * can't be freed. (Exercise: what would it take to
00212                  * make it possible to free the boot stack?)
00213                  */
00214                 /*c->c_curthread->t_stack = ... */
00215         }
00216         else {
00217                 c->c_curthread->t_stack = kmalloc(STACK_SIZE);
00218                 if (c->c_curthread->t_stack == NULL) {
00219                         panic("cpu_create: couldn't allocate stack");
00220                 }
00221                 thread_checkstack_init(c->c_curthread);
00222         }
00223         c->c_curthread->t_cpu = c;
00224 
00225         cpu_machdep_init(c);
00226 
00227         return c;
00228 }
00229 
00230 /*
00231  * Destroy a thread.
00232  *
00233  * This function cannot be called in the victim thread's own context.
00234  * Nor can it be called on a running thread.
00235  *
00236  * (Freeing the stack you're actually using to run is ... inadvisable.)
00237  */
00238 static
00239 void
00240 thread_destroy(struct thread *thread)
00241 {
00242         KASSERT(thread != curthread);
00243         KASSERT(thread->t_state != S_RUN);
00244 
00245         /*
00246          * If you add things to struct thread, be sure to clean them up
00247          * either here or in thread_exit(). (And not both...)
00248          */
00249 
00250         /* Thread subsystem fields */
00251         KASSERT(thread->t_proc == NULL);
00252         if (thread->t_stack != NULL) {
00253                 kfree(thread->t_stack);
00254         }
00255         threadlistnode_cleanup(&thread->t_listnode);
00256         thread_machdep_cleanup(&thread->t_machdep);
00257 
00258         /* sheer paranoia */
00259         thread->t_wchan_name = "DESTROYED";
00260 
00261         kfree(thread->t_name);
00262         kfree(thread);
00263 }
00264 
00265 /*
00266  * Clean up zombies. (Zombies are threads that have exited but still
00267  * need to have thread_destroy called on them.)
00268  *
00269  * The list of zombies is per-cpu.
00270  */
00271 static
00272 void
00273 exorcise(void)
00274 {
00275         struct thread *z;
00276 
00277         while ((z = threadlist_remhead(&curcpu->c_zombies)) != NULL) {
00278                 KASSERT(z != curthread);
00279                 KASSERT(z->t_state == S_ZOMBIE);
00280                 thread_destroy(z);
00281         }
00282 }
00283 
00284 /*
00285  * On panic, stop the thread system (as much as is reasonably
00286  * possible) to make sure we don't end up letting any other threads
00287  * run.
00288  */
00289 void
00290 thread_panic(void)
00291 {
00292         /*
00293          * Kill off other CPUs.
00294          *
00295          * We could wait for them to stop, except that they might not.
00296          */
00297         ipi_broadcast(IPI_PANIC);
00298 
00299         /*
00300          * Drop runnable threads on the floor.
00301          *
00302          * Don't try to get the run queue lock; we might not be able
00303          * to.  Instead, blat the list structure by hand, and take the
00304          * risk that it might not be quite atomic.
00305          */
00306         curcpu->c_runqueue.tl_count = 0;
00307         curcpu->c_runqueue.tl_head.tln_next = NULL;
00308         curcpu->c_runqueue.tl_tail.tln_prev = NULL;
00309 
00310         /*
00311          * Ideally, we want to make sure sleeping threads don't wake
00312          * up and start running. However, there's no good way to track
00313          * down all the wchans floating around the system. Another
00314          * alternative would be to set a global flag to make the wchan
00315          * wakeup operations do nothing; but that would mean we
00316          * ourselves couldn't sleep to wait for an I/O completion
00317          * interrupt, and we'd like to be able to do that if the
00318          * system isn't that badly hosed.
00319          *
00320          * So, do nothing else here.
00321          *
00322          * This may prove inadequate in practice and further steps
00323          * might be needed. It may also be necessary to go through and
00324          * forcibly unlock all locks or the like...
00325          */
00326 }
00327 
00328 /*
00329  * At system shutdown, ask the other CPUs to switch off.
00330  */
00331 void
00332 thread_shutdown(void)
00333 {
00334         /*
00335          * Stop the other CPUs.
00336          *
00337          * We should probably wait for them to stop and shut them off
00338          * on the system board.
00339          */
00340         ipi_broadcast(IPI_OFFLINE);
00341 }
00342 
00343 /*
00344  * Thread system initialization.
00345  */
00346 void
00347 thread_bootstrap(void)
00348 {
00349         struct cpu *bootcpu;
00350         struct thread *bootthread;
00351 
00352         cpuarray_init(&allcpus);
00353 
00354         /*
00355          * Create the cpu structure for the bootup CPU, the one we're
00356          * currently running on. Assume the hardware number is 0; that
00357          * might be updated later by mainbus-type code. This also
00358          * creates a thread structure for the first thread, the one
00359          * that's already implicitly running when the kernel is
00360          * started from the bootloader.
00361          */
00362         bootcpu = cpu_create(0);
00363         bootthread = bootcpu->c_curthread;
00364 
00365         /*
00366          * Initializing curcpu and curthread is machine-dependent
00367          * because either of curcpu and curthread might be defined in
00368          * terms of the other.
00369          */
00370         INIT_CURCPU(bootcpu, bootthread);
00371 
00372         /*
00373          * Now make sure both t_cpu and c_curthread are set. This
00374          * might be partially redundant with INIT_CURCPU depending on
00375          * how things are defined.
00376          */
00377         curthread->t_cpu = curcpu;
00378         curcpu->c_curthread = curthread;
00379 
00380         /* cpu_create() should have set t_proc. */
00381         KASSERT(curthread->t_proc != NULL);
00382 
00383         /* Done */
00384 }
00385 
00386 /*
00387  * New CPUs come here once MD initialization is finished. curthread
00388  * and curcpu should already be initialized.
00389  *
00390  * Other than clearing thread_start_cpus() to continue, we don't need
00391  * to do anything. The startup thread can just exit; we only need it
00392  * to be able to get into thread_switch() properly.
00393  */
00394 void
00395 cpu_hatch(unsigned software_number)
00396 {
00397         KASSERT(curcpu != NULL);
00398         KASSERT(curthread != NULL);
00399         KASSERT(curcpu->c_number == software_number);
00400 
00401         spl0();
00402 
00403         kprintf("cpu%u: %s\n", software_number, cpu_identify());
00404 
00405         V(cpu_startup_sem);
00406         thread_exit();
00407 }
00408 
00409 /*
00410  * Start up secondary cpus. Called from boot().
00411  */
00412 void
00413 thread_start_cpus(void)
00414 {
00415         unsigned i;
00416 
00417         kprintf("cpu0: %s\n", cpu_identify());
00418 
00419         cpu_startup_sem = sem_create("cpu_hatch", 0);
00420         mainbus_start_cpus();
00421         
00422         for (i=0; i<cpuarray_num(&allcpus) - 1; i++) {
00423                 P(cpu_startup_sem);
00424         }
00425         sem_destroy(cpu_startup_sem);
00426         cpu_startup_sem = NULL;
00427 }
00428 
00429 /*
00430  * Make a thread runnable.
00431  *
00432  * targetcpu might be curcpu; it might not be, too. 
00433  */
00434 static
00435 void
00436 thread_make_runnable(struct thread *target, bool already_have_lock)
00437 {
00438         struct cpu *targetcpu;
00439         bool isidle;
00440 
00441         /* Lock the run queue of the target thread's cpu. */
00442         targetcpu = target->t_cpu;
00443 
00444         if (already_have_lock) {
00445                 /* The target thread's cpu should be already locked. */
00446                 KASSERT(spinlock_do_i_hold(&targetcpu->c_runqueue_lock));
00447         }
00448         else {
00449                 spinlock_acquire(&targetcpu->c_runqueue_lock);
00450         }
00451 
00452         isidle = targetcpu->c_isidle;
00453         threadlist_addtail(&targetcpu->c_runqueue, target);
00454         if (isidle) {
00455                 /*
00456                  * Other processor is idle; send interrupt to make
00457                  * sure it unidles.
00458                  */
00459                 ipi_send(targetcpu, IPI_UNIDLE);
00460         }
00461 
00462         if (!already_have_lock) {
00463                 spinlock_release(&targetcpu->c_runqueue_lock);
00464         }
00465 }
00466 
00467 /*
00468  * Create a new thread based on an existing one.
00469  *
00470  * The new thread has name NAME, and starts executing in function
00471  * ENTRYPOINT. DATA1 and DATA2 are passed to ENTRYPOINT.
00472  *
00473  * The new thread is created in the process P. If P is null, the
00474  * process is inherited from the caller. It will start on the same CPU
00475  * as the caller, unless the scheduler intervenes first.
00476  */
00477 int
00478 thread_fork(const char *name,
00479             struct proc *proc,
00480             void (*entrypoint)(void *data1, unsigned long data2),
00481             void *data1, unsigned long data2)
00482 {
00483         struct thread *newthread;
00484         int result;
00485 
00486 #ifdef UW
00487         DEBUG(DB_THREADS,"Forking thread: %s\n",name);
00488 #endif // UW
00489 
00490         newthread = thread_create(name);
00491         if (newthread == NULL) {
00492                 return ENOMEM;
00493         }
00494 
00495         /* Allocate a stack */
00496         newthread->t_stack = kmalloc(STACK_SIZE);
00497         if (newthread->t_stack == NULL) {
00498                 thread_destroy(newthread);
00499                 return ENOMEM;
00500         }
00501         thread_checkstack_init(newthread);
00502 
00503         /*
00504          * Now we clone various fields from the parent thread.
00505          */
00506 
00507         /* Thread subsystem fields */
00508         newthread->t_cpu = curthread->t_cpu;
00509 
00510         /* Attach the new thread to its process */
00511         if (proc == NULL) {
00512                 proc = curthread->t_proc;
00513         }
00514         result = proc_addthread(proc, newthread);
00515         if (result) {
00516                 /* thread_destroy will clean up the stack */
00517                 thread_destroy(newthread);
00518                 return result;
00519         }
00520 
00521         /*
00522          * Because new threads come out holding the cpu runqueue lock
00523          * (see notes at bottom of thread_switch), we need to account
00524          * for the spllower() that will be done releasing it.
00525          */
00526         newthread->t_iplhigh_count++;
00527 
00528         /* Set up the switchframe so entrypoint() gets called */
00529         switchframe_init(newthread, entrypoint, data1, data2);
00530 
00531         /* Lock the current cpu's run queue and make the new thread runnable */
00532         thread_make_runnable(newthread, false);
00533 
00534         return 0;
00535 }
00536 
00537 /*
00538  * High level, machine-independent context switch code.
00539  *
00540  * The current thread is queued appropriately and its state is changed
00541  * to NEWSTATE; another thread to run is selected and switched to.
00542  *
00543  * If NEWSTATE is S_SLEEP, the thread is queued on the wait channel
00544  * WC. Otherwise WC should be NULL.
00545  */
00546 static
00547 void
00548 thread_switch(threadstate_t newstate, struct wchan *wc)
00549 {
00550         struct thread *cur, *next;
00551         int spl;
00552 
00553         DEBUGASSERT(curcpu->c_curthread == curthread);
00554         DEBUGASSERT(curthread->t_cpu == curcpu->c_self);
00555 
00556         /* Explicitly disable interrupts on this processor */
00557         spl = splhigh();
00558 
00559         cur = curthread;
00560 
00561         /*
00562          * If we're idle, return without doing anything. This happens
00563          * when the timer interrupt interrupts the idle loop.
00564          */
00565         if (curcpu->c_isidle) {
00566                 splx(spl);
00567                 return;
00568         }
00569 
00570         /* Check the stack guard band. */
00571         thread_checkstack(cur);
00572 
00573         /* Lock the run queue. */
00574         spinlock_acquire(&curcpu->c_runqueue_lock);
00575 
00576         /* Micro-optimization: if nothing to do, just return */
00577         if (newstate == S_READY && threadlist_isempty(&curcpu->c_runqueue)) {
00578                 spinlock_release(&curcpu->c_runqueue_lock);
00579                 splx(spl);
00580                 return;
00581         }
00582 
00583         /* Put the thread in the right place. */
00584         switch (newstate) {
00585             case S_RUN:
00586                 panic("Illegal S_RUN in thread_switch\n");
00587             case S_READY:
00588                 thread_make_runnable(cur, true /*have lock*/);
00589                 break;
00590             case S_SLEEP:
00591                 cur->t_wchan_name = wc->wc_name;
00592                 /*
00593                  * Add the thread to the list in the wait channel, and
00594                  * unlock same. To avoid a race with someone else
00595                  * calling wchan_wake*, we must keep the wchan locked
00596                  * from the point the caller of wchan_sleep locked it
00597                  * until the thread is on the list.
00598                  *
00599                  * (We could for symmetry relock the channel before
00600                  * returning from wchan_sleep, but we don't, for two
00601                  * reasons. One is that the caller is unlikely to need
00602                  * or want it locked and if it does can lock it itself
00603                  * without racing. Exercise: what's the other?)
00604                  */
00605                 threadlist_addtail(&wc->wc_threads, cur);
00606                 wchan_unlock(wc);
00607                 break;
00608             case S_ZOMBIE:
00609                 cur->t_wchan_name = "ZOMBIE";
00610                 threadlist_addtail(&curcpu->c_zombies, cur);
00611                 break;
00612         }
00613         cur->t_state = newstate;
00614 
00615         /*
00616          * Get the next thread. While there isn't one, call md_idle().
00617          * curcpu->c_isidle must be true when md_idle is
00618          * called. Unlock the runqueue while idling too, to make sure
00619          * things can be added to it.
00620          *
00621          * Note that we don't need to unlock the runqueue atomically
00622          * with idling; becoming unidle requires receiving an
00623          * interrupt (either a hardware interrupt or an interprocessor
00624          * interrupt from another cpu posting a wakeup) and idling
00625          * *is* atomic with respect to re-enabling interrupts.
00626          *
00627          * Note that c_isidle becomes true briefly even if we don't go
00628          * idle. However, because one is supposed to hold the runqueue
00629          * lock to look at it, this should not be visible or matter.
00630          */
00631 
00632         /* The current cpu is now idle. */
00633         curcpu->c_isidle = true;
00634         do {
00635                 next = threadlist_remhead(&curcpu->c_runqueue);
00636                 if (next == NULL) {
00637                         spinlock_release(&curcpu->c_runqueue_lock);
00638                         cpu_idle();
00639                         spinlock_acquire(&curcpu->c_runqueue_lock);
00640                 }
00641         } while (next == NULL);
00642         curcpu->c_isidle = false;
00643 
00644         /*
00645          * Note that curcpu->c_curthread may be the same variable as
00646          * curthread and it may not be, depending on how curthread and
00647          * curcpu are defined by the MD code. We'll assign both and
00648          * assume the compiler will optimize one away if they're the
00649          * same.
00650          */
00651         curcpu->c_curthread = next;
00652         curthread = next;
00653 
00654         /* do the switch (in assembler in switch.S) */
00655         switchframe_switch(&cur->t_context, &next->t_context);
00656 
00657         /*
00658          * When we get to this point we are either running in the next
00659          * thread, or have come back to the same thread again,
00660          * depending on how you look at it. That is,
00661          * switchframe_switch returns immediately in another thread
00662          * context, which in general will be executing here with a
00663          * different stack and different values in the local
00664          * variables. (Although new threads go to thread_startup
00665          * instead.) But, later on when the processor, or some
00666          * processor, comes back to the previous thread, it's also
00667          * executing here with the *same* value in the local
00668          * variables.
00669          *
00670          * The upshot, however, is as follows:
00671          *
00672          *    - The thread now currently running is "cur", not "next",
00673          *      because when we return from switchrame_switch on the
00674          *      same stack, we're back to the thread that
00675          *      switchframe_switch call switched away from, which is
00676          *      "cur".
00677          *
00678          *    - "cur" is _not_ the thread that just *called*
00679          *      switchframe_switch.
00680          *
00681          *    - If newstate is S_ZOMB we never get back here in that
00682          *      context at all.
00683          *
00684          *    - If the thread just chosen to run ("next") was a new
00685          *      thread, we don't get to this code again until
00686          *      *another* context switch happens, because when new
00687          *      threads return from switchframe_switch they teleport
00688          *      to thread_startup.
00689          *
00690          *    - At this point the thread whose stack we're now on may
00691          *      have been migrated to another cpu since it last ran.
00692          *
00693          * The above is inherently confusing and will probably take a
00694          * while to get used to.
00695          *
00696          * However, the important part is that code placed here, after
00697          * the call to switchframe_switch, does not necessarily run on
00698          * every context switch. Thus any such code must be either
00699          * skippable on some switches or also called from
00700          * thread_startup.
00701          */
00702 
00703 
00704         /* Clear the wait channel and set the thread state. */
00705         cur->t_wchan_name = NULL;
00706         cur->t_state = S_RUN;
00707 
00708         /* Unlock the run queue. */
00709         spinlock_release(&curcpu->c_runqueue_lock);
00710 
00711         /* Activate our address space in the MMU. */
00712         as_activate();
00713 
00714         /* Clean up dead threads. */
00715         exorcise();
00716 
00717         /* Turn interrupts back on. */
00718         splx(spl);
00719 }
00720 
00721 /*
00722  * This function is where new threads start running. The arguments
00723  * ENTRYPOINT, DATA1, and DATA2 are passed through from thread_fork.
00724  *
00725  * Because new code comes here from inside the middle of
00726  * thread_switch, the beginning part of this function must match the
00727  * tail of thread_switch.
00728  */
00729 void
00730 thread_startup(void (*entrypoint)(void *data1, unsigned long data2),
00731                void *data1, unsigned long data2)
00732 {
00733         struct thread *cur;
00734 
00735         cur = curthread;
00736 
00737         /* Clear the wait channel and set the thread state. */
00738         cur->t_wchan_name = NULL;
00739         cur->t_state = S_RUN;
00740 
00741         /* Release the runqueue lock acquired in thread_switch. */
00742         spinlock_release(&curcpu->c_runqueue_lock);
00743 
00744         /* Activate our address space in the MMU. */
00745         as_activate();
00746 
00747         /* Clean up dead threads. */
00748         exorcise();
00749 
00750         /* Enable interrupts. */
00751         spl0();
00752 
00753 #if OPT_SYNCHPROBS
00754         /* Yield a random number of times to get a good mix of threads. */
00755         {
00756                 int i, n;
00757                 n = random()%161 + random()%161;
00758                 for (i=0; i<n; i++) {
00759                         thread_yield();
00760                 }
00761         }
00762 #endif
00763 
00764         /* Call the function. */
00765         entrypoint(data1, data2);
00766 
00767         /* Done. */
00768         thread_exit();
00769 }
00770 
00771 /*
00772  * Cause the current thread to exit.
00773  *
00774  * The parts of the thread structure we don't actually need to run
00775  * should be cleaned up right away. The rest has to wait until
00776  * thread_destroy is called from exorcise().
00777  *
00778  * Does not return.
00779  */
00780 void
00781 thread_exit(void)
00782 {
00783         struct thread *cur;
00784 
00785         cur = curthread;
00786 
00787 #ifdef UW
00788         /* threads for user processes should have detached from their process
00789            in sys__exit */
00790         KASSERT(curproc == kproc || curproc == NULL);   
00791         /* kernel threads don't go through sys__exit, so we detach them from kproc here */
00792         if (curproc == kproc) {
00793           proc_remthread(cur);
00794         }
00795 #else // UW
00796         proc_remthread(cur);
00797 #endif // UW
00798 
00799         /* Make sure we *are* detached (move this only if you're sure!) */
00800         KASSERT(cur->t_proc == NULL);
00801 
00802         /* Check the stack guard band. */
00803         thread_checkstack(cur);
00804 
00805         /* Interrupts off on this processor */
00806         splhigh();
00807         thread_switch(S_ZOMBIE, NULL);
00808         panic("The zombie walks!\n");
00809 }
00810 
00811 /*
00812  * Yield the cpu to another process, but stay runnable.
00813  */
00814 void
00815 thread_yield(void)
00816 {
00817         thread_switch(S_READY, NULL);
00818 }
00819 
00820 ////////////////////////////////////////////////////////////
00821 
00822 /*
00823  * Scheduler.
00824  *
00825  * This is called periodically from hardclock(). It should reshuffle
00826  * the current CPU's run queue by job priority.
00827  */
00828 
00829 void
00830 schedule(void)
00831 {
00832         /*
00833          * You can write this. If we do nothing, threads will run in
00834          * round-robin fashion.
00835          */
00836 }
00837 
00838 /*
00839  * Thread migration.
00840  *
00841  * This is also called periodically from hardclock(). If the current
00842  * CPU is busy and other CPUs are idle, or less busy, it should move
00843  * threads across to those other other CPUs.
00844  *
00845  * Migrating threads isn't free because of cache affinity; a thread's
00846  * working cache set will end up having to be moved to the other CPU,
00847  * which is fairly slow. The tradeoff between this performance loss
00848  * and the performance loss due to underutilization of some CPUs is
00849  * something that needs to be tuned and probably is workload-specific.
00850  *
00851  * For here and now, because we know we're running on System/161 and
00852  * System/161 does not (yet) model such cache effects, we'll be very
00853  * aggressive.
00854  */
00855 void
00856 thread_consider_migration(void)
00857 {
00858         unsigned my_count, total_count, one_share, to_send;
00859         unsigned i, numcpus;
00860         struct cpu *c;
00861         struct threadlist victims;
00862         struct thread *t;
00863 
00864         my_count = total_count = 0;
00865         numcpus = cpuarray_num(&allcpus);
00866         for (i=0; i<numcpus; i++) {
00867                 c = cpuarray_get(&allcpus, i);
00868                 spinlock_acquire(&c->c_runqueue_lock);
00869                 total_count += c->c_runqueue.tl_count;
00870                 if (c == curcpu->c_self) {
00871                         my_count = c->c_runqueue.tl_count;
00872                 }
00873                 spinlock_release(&c->c_runqueue_lock);
00874         }
00875 
00876         one_share = DIVROUNDUP(total_count, numcpus);
00877         if (my_count < one_share) {
00878                 return;
00879         }
00880 
00881         to_send = my_count - one_share;
00882         threadlist_init(&victims);
00883         spinlock_acquire(&curcpu->c_runqueue_lock);
00884         for (i=0; i<to_send; i++) {
00885                 t = threadlist_remtail(&curcpu->c_runqueue);
00886                 threadlist_addhead(&victims, t);
00887         }
00888         spinlock_release(&curcpu->c_runqueue_lock);
00889 
00890         for (i=0; i < numcpus && to_send > 0; i++) {
00891                 c = cpuarray_get(&allcpus, i);
00892                 if (c == curcpu->c_self) {
00893                         continue;
00894                 }
00895                 spinlock_acquire(&c->c_runqueue_lock);
00896                 while (c->c_runqueue.tl_count < one_share && to_send > 0) {
00897                         t = threadlist_remhead(&victims);
00898                         /*
00899                          * Ordinarily, curthread will not appear on
00900                          * the run queue. However, it can under the
00901                          * following circumstances:
00902                          *   - it went to sleep;
00903                          *   - the processor became idle, so it
00904                          *     remained curthread;
00905                          *   - it was reawakened, so it was put on the
00906                          *     run queue;
00907                          *   - and the processor hasn't fully unidled
00908                          *     yet, so all these things are still true.
00909                          *
00910                          * If the timer interrupt happens at (almost)
00911                          * exactly the proper moment, we can come here
00912                          * while things are in this state and see
00913                          * curthread. However, *migrating* curthread
00914                          * can cause bad things to happen (Exercise:
00915                          * Why? And what?) so shuffle it to the end of
00916                          * the list and decrement to_send in order to
00917                          * skip it. Then it goes back on our own run
00918                          * queue below.
00919                          */
00920                         if (t == curthread) {
00921                                 threadlist_addtail(&victims, t);
00922                                 to_send--;
00923                                 continue;
00924                         }
00925 
00926                         t->t_cpu = c;
00927                         threadlist_addtail(&c->c_runqueue, t);
00928                         DEBUG(DB_THREADS,
00929                               "Migrated thread %s: cpu %u -> %u",
00930                               t->t_name, curcpu->c_number, c->c_number);
00931                         to_send--;
00932                         if (c->c_isidle) {
00933                                 /*
00934                                  * Other processor is idle; send
00935                                  * interrupt to make sure it unidles.
00936                                  */
00937                                 ipi_send(c, IPI_UNIDLE);
00938                         }
00939                 }
00940                 spinlock_release(&c->c_runqueue_lock);
00941         }
00942 
00943         /*
00944          * Because the code above isn't atomic, the thread counts may have
00945          * changed while we were working and we may end up with leftovers.
00946          * Don't panic; just put them back on our own run queue.
00947          */
00948         if (!threadlist_isempty(&victims)) {
00949                 spinlock_acquire(&curcpu->c_runqueue_lock);
00950                 while ((t = threadlist_remhead(&victims)) != NULL) {
00951                         threadlist_addtail(&curcpu->c_runqueue, t);
00952                 }
00953                 spinlock_release(&curcpu->c_runqueue_lock);
00954         }
00955 
00956         KASSERT(threadlist_isempty(&victims));
00957         threadlist_cleanup(&victims);
00958 }
00959 
00960 ////////////////////////////////////////////////////////////
00961 
00962 /*
00963  * Wait channel functions
00964  */
00965 
00966 /*
00967  * Create a wait channel. NAME is a symbolic string name for it.
00968  * This is what's displayed by ps -alx in Unix.
00969  *
00970  * NAME should generally be a string constant. If it isn't, alternate
00971  * arrangements should be made to free it after the wait channel is
00972  * destroyed.
00973  */
00974 struct wchan *
00975 wchan_create(const char *name)
00976 {
00977         struct wchan *wc;
00978 
00979         wc = kmalloc(sizeof(*wc));
00980         if (wc == NULL) {
00981                 return NULL;
00982         }
00983         spinlock_init(&wc->wc_lock);
00984         threadlist_init(&wc->wc_threads);
00985         wc->wc_name = name;
00986         return wc;
00987 }
00988 
00989 /*
00990  * Destroy a wait channel. Must be empty and unlocked.
00991  * (The corresponding cleanup functions require this.)
00992  */
00993 void
00994 wchan_destroy(struct wchan *wc)
00995 {
00996         spinlock_cleanup(&wc->wc_lock);
00997         threadlist_cleanup(&wc->wc_threads);
00998         kfree(wc);
00999 }
01000 
01001 /*
01002  * Lock and unlock a wait channel, respectively.
01003  */
01004 void
01005 wchan_lock(struct wchan *wc)
01006 {
01007         spinlock_acquire(&wc->wc_lock);
01008 }
01009 
01010 void
01011 wchan_unlock(struct wchan *wc)
01012 {
01013         spinlock_release(&wc->wc_lock);
01014 }
01015 
01016 /*
01017  * Yield the cpu to another process, and go to sleep, on the specified
01018  * wait channel WC. Calling wakeup on the channel will make the thread
01019  * runnable again. The channel must be locked, and will be *unlocked*
01020  * upon return.
01021  */
01022 void
01023 wchan_sleep(struct wchan *wc)
01024 {
01025         /* may not sleep in an interrupt handler */
01026         KASSERT(!curthread->t_in_interrupt);
01027 
01028         thread_switch(S_SLEEP, wc);
01029 }
01030 
01031 /*
01032  * Wake up one thread sleeping on a wait channel.
01033  */
01034 void
01035 wchan_wakeone(struct wchan *wc)
01036 {
01037         struct thread *target;
01038 
01039         /* Lock the channel and grab a thread from it */
01040         spinlock_acquire(&wc->wc_lock);
01041         target = threadlist_remhead(&wc->wc_threads);
01042         /*
01043          * Nobody else can wake up this thread now, so we don't need
01044          * to hang onto the lock.
01045          */
01046         spinlock_release(&wc->wc_lock);
01047 
01048         if (target == NULL) {
01049                 /* Nobody was sleeping. */
01050                 return;
01051         }
01052 
01053         thread_make_runnable(target, false);
01054 }
01055 
01056 /*
01057  * Wake up all threads sleeping on a wait channel.
01058  */
01059 void
01060 wchan_wakeall(struct wchan *wc)
01061 {
01062         struct thread *target;
01063         struct threadlist list;
01064 
01065         threadlist_init(&list);
01066 
01067         /*
01068          * Lock the channel and grab all the threads, moving them to a
01069          * private list.
01070          */
01071         spinlock_acquire(&wc->wc_lock);
01072         while ((target = threadlist_remhead(&wc->wc_threads)) != NULL) {
01073                 threadlist_addtail(&list, target);
01074         }
01075         /*
01076          * Nobody else can wake up these threads now, so we don't need
01077          * to hang onto the lock.
01078          */
01079         spinlock_release(&wc->wc_lock);
01080 
01081         /*
01082          * We could conceivably sort by cpu first to cause fewer lock
01083          * ops and fewer IPIs, but for now at least don't bother. Just
01084          * make each thread runnable.
01085          */
01086         while ((target = threadlist_remhead(&list)) != NULL) {
01087                 thread_make_runnable(target, false);
01088         }
01089 
01090         threadlist_cleanup(&list);
01091 }
01092 
01093 /*
01094  * Return nonzero if there are no threads sleeping on the channel.
01095  * This is meant to be used only for diagnostic purposes.
01096  */
01097 bool
01098 wchan_isempty(struct wchan *wc)
01099 {
01100         bool ret;
01101 
01102         spinlock_acquire(&wc->wc_lock);
01103         ret = threadlist_isempty(&wc->wc_threads);
01104         spinlock_release(&wc->wc_lock);
01105 
01106         return ret;
01107 }
01108 
01109 ////////////////////////////////////////////////////////////
01110 
01111 /*
01112  * Machine-independent IPI handling
01113  */
01114 
01115 /*
01116  * Send an IPI (inter-processor interrupt) to the specified CPU.
01117  */
01118 void
01119 ipi_send(struct cpu *target, int code)
01120 {
01121         KASSERT(code >= 0 && code < 32);
01122 
01123         spinlock_acquire(&target->c_ipi_lock);
01124         target->c_ipi_pending |= (uint32_t)1 << code;
01125         mainbus_send_ipi(target);
01126         spinlock_release(&target->c_ipi_lock);
01127 }
01128 
01129 void
01130 ipi_broadcast(int code)
01131 {
01132         unsigned i;
01133         struct cpu *c;
01134 
01135         for (i=0; i < cpuarray_num(&allcpus); i++) {
01136                 c = cpuarray_get(&allcpus, i);
01137                 if (c != curcpu->c_self) {
01138                         ipi_send(c, code);
01139                 }
01140         }
01141 }
01142 
01143 void
01144 ipi_tlbshootdown(struct cpu *target, const struct tlbshootdown *mapping)
01145 {
01146         int n;
01147 
01148         spinlock_acquire(&target->c_ipi_lock);
01149 
01150         n = target->c_numshootdown;
01151         if (n == TLBSHOOTDOWN_MAX) {
01152                 target->c_numshootdown = TLBSHOOTDOWN_ALL;
01153         }
01154         else {
01155                 target->c_shootdown[n] = *mapping;
01156                 target->c_numshootdown = n+1;
01157         }
01158 
01159         target->c_ipi_pending |= (uint32_t)1 << IPI_TLBSHOOTDOWN;
01160         mainbus_send_ipi(target);
01161 
01162         spinlock_release(&target->c_ipi_lock);
01163 }
01164 
01165 void
01166 interprocessor_interrupt(void)
01167 {
01168         uint32_t bits;
01169         int i;
01170 
01171         spinlock_acquire(&curcpu->c_ipi_lock);
01172         bits = curcpu->c_ipi_pending;
01173 
01174         if (bits & (1U << IPI_PANIC)) {
01175                 /* panic on another cpu - just stop dead */
01176                 cpu_halt();
01177         }
01178         if (bits & (1U << IPI_OFFLINE)) {
01179                 /* offline request */
01180                 spinlock_acquire(&curcpu->c_runqueue_lock);
01181                 if (!curcpu->c_isidle) {
01182                         kprintf("cpu%d: offline: warning: not idle\n",
01183                                 curcpu->c_number);
01184                 }
01185                 spinlock_release(&curcpu->c_runqueue_lock);
01186                 kprintf("cpu%d: offline.\n", curcpu->c_number);
01187                 cpu_halt();
01188         }
01189         if (bits & (1U << IPI_UNIDLE)) {
01190                 /*
01191                  * The cpu has already unidled itself to take the
01192                  * interrupt; don't need to do anything else.
01193                  */
01194         }
01195         if (bits & (1U << IPI_TLBSHOOTDOWN)) {
01196                 if (curcpu->c_numshootdown == TLBSHOOTDOWN_ALL) {
01197                         vm_tlbshootdown_all();
01198                 }
01199                 else {
01200                         for (i=0; i<curcpu->c_numshootdown; i++) {
01201                                 vm_tlbshootdown(&curcpu->c_shootdown[i]);
01202                         }
01203                 }
01204                 curcpu->c_numshootdown = 0;
01205         }
01206 
01207         curcpu->c_ipi_pending = 0;
01208         spinlock_release(&curcpu->c_ipi_lock);
01209 }
 All Data Structures