thread.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  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. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Core kernel-level thread system.
  31. */
  32. #define THREADINLINE
  33. #include <types.h>
  34. #include <kern/errno.h>
  35. #include <lib.h>
  36. #include <array.h>
  37. #include <cpu.h>
  38. #include <spl.h>
  39. #include <spinlock.h>
  40. #include <wchan.h>
  41. #include <thread.h>
  42. #include <threadlist.h>
  43. #include <threadprivate.h>
  44. #include <proc.h>
  45. #include <current.h>
  46. #include <synch.h>
  47. #include <addrspace.h>
  48. #include <mainbus.h>
  49. #include <vnode.h>
  50. #include "opt-synchprobs.h"
  51. /* Magic number used as a guard value on kernel thread stacks. */
  52. #define THREAD_STACK_MAGIC 0xbaadf00d
  53. /* Wait channel. */
  54. struct wchan
  55. {
  56. const char * wc_name; /* name for this channel */
  57. struct threadlist wc_threads; /* list of waiting threads */
  58. struct spinlock wc_lock; /* lock for mutual exclusion */
  59. };
  60. /* Master array of CPUs. */
  61. DECLARRAY(cpu);
  62. DEFARRAY(cpu, /*no inline*/ );
  63. static struct cpuarray allcpus;
  64. /* Used to wait for secondary CPUs to come online. */
  65. static struct semaphore *cpu_startup_sem;
  66. ////////////////////////////////////////////////////////////
  67. /*
  68. * Stick a magic number on the bottom end of the stack. This will
  69. * (sometimes) catch kernel stack overflows. Use thread_checkstack()
  70. * to test this.
  71. */
  72. static
  73. void
  74. thread_checkstack_init(struct thread * thread)
  75. {
  76. ((uint32_t *)thread->t_stack)[0] = THREAD_STACK_MAGIC;
  77. ((uint32_t *)thread->t_stack)[1] = THREAD_STACK_MAGIC;
  78. ((uint32_t *)thread->t_stack)[2] = THREAD_STACK_MAGIC;
  79. ((uint32_t *)thread->t_stack)[3] = THREAD_STACK_MAGIC;
  80. }
  81. /*
  82. * Check the magic number we put on the bottom end of the stack in
  83. * thread_checkstack_init. If these assertions go off, it most likely
  84. * means you overflowed your stack at some point, which can cause all
  85. * kinds of mysterious other things to happen.
  86. *
  87. * Note that when ->t_stack is NULL, which is the case if the stack
  88. * cannot be freed (which in turn is the case if the stack is the boot
  89. * stack, and the thread is the boot thread) this doesn't do anything.
  90. */
  91. static void thread_checkstack(struct thread * thread)
  92. {
  93. if (thread->t_stack != NULL) {
  94. KASSERT(((uint32_t*)thread->t_stack)[0] == THREAD_STACK_MAGIC);
  95. KASSERT(((uint32_t*)thread->t_stack)[1] == THREAD_STACK_MAGIC);
  96. KASSERT(((uint32_t*)thread->t_stack)[2] == THREAD_STACK_MAGIC);
  97. KASSERT(((uint32_t*)thread->t_stack)[3] == THREAD_STACK_MAGIC);
  98. }
  99. }
  100. /*
  101. * Create a thread. This is used both to create a first thread
  102. * for each CPU and to create subsequent forked threads.
  103. */
  104. static struct thread * thread_create(const char * name)
  105. {
  106. struct thread * thread;
  107. DEBUGASSERT(name != NULL);
  108. thread = kmalloc(sizeof(*thread));
  109. if (thread == NULL)
  110. {
  111. return NULL;
  112. }
  113. thread->t_name = kstrdup(name);
  114. if (thread->t_name == NULL)
  115. {
  116. kfree(thread);
  117. return NULL;
  118. }
  119. thread->t_wchan_name = "NEW";
  120. thread->t_state = S_READY;
  121. /* Thread subsystem fields */
  122. thread_machdep_init(&thread->t_machdep);
  123. threadlistnode_init(&thread->t_listnode, thread);
  124. thread->t_stack = NULL;
  125. thread->t_context = NULL;
  126. thread->t_cpu = NULL;
  127. thread->t_proc = NULL;
  128. /* Interrupt state fields */
  129. thread->t_in_interrupt = false;
  130. thread->t_curspl = IPL_HIGH;
  131. thread->t_iplhigh_count = 1; /* corresponding to t_curspl */
  132. /* If you add to struct thread, be sure to initialize here */
  133. return thread;
  134. }
  135. /*
  136. * Create a CPU structure. This is used for the bootup CPU and
  137. * also for secondary CPUs.
  138. *
  139. * The hardware number (the number assigned by firmware or system
  140. * board config or whatnot) is tracked separately because it is not
  141. * necessarily anything sane or meaningful.
  142. */
  143. struct cpu * cpu_create(unsigned hardware_number)
  144. {
  145. struct cpu * c;
  146. int result;
  147. char namebuf[16];
  148. c = kmalloc(sizeof(*c));
  149. if (c == NULL)
  150. {
  151. panic("cpu_create: Out of memory\n");
  152. }
  153. c->c_self = c;
  154. c->c_hardware_number = hardware_number;
  155. c->c_curthread = NULL;
  156. threadlist_init(&c->c_zombies);
  157. c->c_hardclocks = 0;
  158. c->c_isidle = false;
  159. threadlist_init(&c->c_runqueue);
  160. spinlock_init(&c->c_runqueue_lock);
  161. c->c_ipi_pending = 0;
  162. c->c_numshootdown = 0;
  163. spinlock_init(&c->c_ipi_lock);
  164. result = cpuarray_add(&allcpus, c, &c->c_number);
  165. if (result != 0)
  166. {
  167. panic("cpu_create: array_add: %s\n", strerror(result));
  168. }
  169. snprintf(namebuf, sizeof(namebuf), "<boot #%d>", c->c_number);
  170. c->c_curthread = thread_create(namebuf);
  171. if (c->c_curthread == NULL)
  172. {
  173. panic("cpu_create: thread_create failed\n");
  174. }
  175. result = proc_addthread(kproc, c->c_curthread);
  176. if (result)
  177. {
  178. panic("cpu_create: proc_addthread:: %s\n", strerror(result));
  179. }
  180. if (c->c_number == 0)
  181. {
  182. /*
  183. * Leave c->c_curthread->t_stack NULL for the boot
  184. * cpu. This means we're using the boot stack, which
  185. * can't be freed. (Exercise: what would it take to
  186. * make it possible to free the boot stack?)
  187. */
  188. /*c->c_curthread->t_stack = ... */
  189. }
  190. else
  191. {
  192. c->c_curthread->t_stack = kmalloc(STACK_SIZE);
  193. if (c->c_curthread->t_stack == NULL)
  194. {
  195. panic("cpu_create: couldn't allocate stack");
  196. }
  197. thread_checkstack_init(c->c_curthread);
  198. }
  199. c->c_curthread->t_cpu = c;
  200. cpu_machdep_init(c);
  201. return c;
  202. }
  203. /*
  204. * Destroy a thread.
  205. *
  206. * This function cannot be called in the victim thread's own context.
  207. * Nor can it be called on a running thread.
  208. *
  209. * (Freeing the stack you're actually using to run is ... inadvisable.)
  210. */
  211. static void thread_destroy(struct thread * thread)
  212. {
  213. KASSERT(thread != curthread);
  214. KASSERT(thread->t_state != S_RUN);
  215. /*
  216. * If you add things to struct thread, be sure to clean them up
  217. * either here or in thread_exit(). (And not both...)
  218. */
  219. /* Thread subsystem fields */
  220. KASSERT(thread->t_proc == NULL);
  221. if (thread->t_stack != NULL)
  222. {
  223. kfree(thread->t_stack);
  224. }
  225. threadlistnode_cleanup(&thread->t_listnode);
  226. thread_machdep_cleanup(&thread->t_machdep);
  227. /* sheer paranoia */
  228. thread->t_wchan_name = "DESTROYED";
  229. kfree(thread->t_name);
  230. kfree(thread);
  231. }
  232. /*
  233. * Clean up zombies. (Zombies are threads that have exited but still
  234. * need to have thread_destroy called on them.)
  235. *
  236. * The list of zombies is per-cpu.
  237. */
  238. static void exorcise(void)
  239. {
  240. struct thread * z;
  241. while ((z = threadlist_remhead(&curcpu->c_zombies)) != NULL)
  242. {
  243. KASSERT(z != curthread);
  244. KASSERT(z->t_state == S_ZOMBIE);
  245. thread_destroy(z);
  246. }
  247. }
  248. /*
  249. * On panic, stop the thread system (as much as is reasonably
  250. * possible) to make sure we don't end up letting any other threads
  251. * run.
  252. */
  253. void thread_panic(void)
  254. {
  255. /*
  256. * Kill off other CPUs.
  257. *
  258. * We could wait for them to stop, except that they might not.
  259. */
  260. ipi_broadcast(IPI_PANIC);
  261. /*
  262. * Drop runnable threads on the floor.
  263. *
  264. * Don't try to get the run queue lock; we might not be able
  265. * to. Instead, blat the list structure by hand, and take the
  266. * risk that it might not be quite atomic.
  267. */
  268. curcpu->c_runqueue.tl_count = 0;
  269. curcpu->c_runqueue.tl_head.tln_next = NULL;
  270. curcpu->c_runqueue.tl_tail.tln_prev = NULL;
  271. /*
  272. * Ideally, we want to make sure sleeping threads don't wake
  273. * up and start running. However, there's no good way to track
  274. * down all the wchans floating around the system. Another
  275. * alternative would be to set a global flag to make the wchan
  276. * wakeup operations do nothing; but that would mean we
  277. * ourselves couldn't sleep to wait for an I/O completion
  278. * interrupt, and we'd like to be able to do that if the
  279. * system isn't that badly hosed.
  280. *
  281. * So, do nothing else here.
  282. *
  283. * This may prove inadequate in practice and further steps
  284. * might be needed. It may also be necessary to go through and
  285. * forcibly unlock all locks or the like...
  286. */
  287. }
  288. /*
  289. * At system shutdown, ask the other CPUs to switch off.
  290. */
  291. void thread_shutdown(void)
  292. {
  293. /*
  294. * Stop the other CPUs.
  295. *
  296. * We should probably wait for them to stop and shut them off
  297. * on the system board.
  298. */
  299. ipi_broadcast(IPI_OFFLINE);
  300. }
  301. /*
  302. * Thread system initialization.
  303. */
  304. void thread_bootstrap(void)
  305. {
  306. struct cpu * bootcpu;
  307. struct thread * bootthread;
  308. cpuarray_init(&allcpus);
  309. /*
  310. * Create the cpu structure for the bootup CPU, the one we're
  311. * currently running on. Assume the hardware number is 0; that
  312. * might be updated later by mainbus-type code. This also
  313. * creates a thread structure for the first thread, the one
  314. * that's already implicitly running when the kernel is
  315. * started from the bootloader.
  316. */
  317. bootcpu = cpu_create(0);
  318. bootthread = bootcpu->c_curthread;
  319. /*
  320. * Initializing curcpu and curthread is machine-dependent
  321. * because either of curcpu and curthread might be defined in
  322. * terms of the other.
  323. */
  324. INIT_CURCPU(bootcpu, bootthread);
  325. /*
  326. * Now make sure both t_cpu and c_curthread are set. This
  327. * might be partially redundant with INIT_CURCPU depending on
  328. * how things are defined.
  329. */
  330. curthread->t_cpu = curcpu;
  331. curcpu->c_curthread = curthread;
  332. /* cpu_create() should have set t_proc. */
  333. KASSERT(curthread->t_proc != NULL);
  334. /* Done */
  335. }
  336. /*
  337. * New CPUs come here once MD initialization is finished. curthread
  338. * and curcpu should already be initialized.
  339. *
  340. * Other than clearing thread_start_cpus() to continue, we don't need
  341. * to do anything. The startup thread can just exit; we only need it
  342. * to be able to get into thread_switch() properly.
  343. */
  344. void cpu_hatch(unsigned software_number)
  345. {
  346. KASSERT(curcpu != NULL);
  347. KASSERT(curthread != NULL);
  348. KASSERT(curcpu->c_number == software_number);
  349. spl0();
  350. kprintf("cpu%u: %s\n", software_number, cpu_identify());
  351. V(cpu_startup_sem);
  352. thread_exit();
  353. }
  354. /*
  355. * Start up secondary cpus. Called from boot().
  356. */
  357. void thread_start_cpus(void)
  358. {
  359. unsigned i;
  360. kprintf("cpu0: %s\n", cpu_identify());
  361. cpu_startup_sem = sem_create("cpu_hatch", 0);
  362. mainbus_start_cpus();
  363. for (i=0; i<cpuarray_num(&allcpus) - 1; i++)
  364. {
  365. P(cpu_startup_sem);
  366. }
  367. sem_destroy(cpu_startup_sem);
  368. cpu_startup_sem = NULL;
  369. }
  370. /*
  371. * Make a thread runnable.
  372. *
  373. * targetcpu might be curcpu; it might not be, too.
  374. */
  375. static void thread_make_runnable(struct thread * target, bool already_have_lock)
  376. {
  377. struct cpu * targetcpu;
  378. bool isidle;
  379. /* Lock the run queue of the target thread's cpu. */
  380. targetcpu = target->t_cpu;
  381. if (already_have_lock)
  382. {
  383. /* The target thread's cpu should be already locked. */
  384. KASSERT(spinlock_do_i_hold(&targetcpu->c_runqueue_lock));
  385. }
  386. else
  387. {
  388. spinlock_acquire(&targetcpu->c_runqueue_lock);
  389. }
  390. isidle = targetcpu->c_isidle;
  391. threadlist_addtail(&targetcpu->c_runqueue, target);
  392. if (isidle)
  393. {
  394. /*
  395. * Other processor is idle; send interrupt to make
  396. * sure it unidles.
  397. */
  398. ipi_send(targetcpu, IPI_UNIDLE);
  399. }
  400. if (!already_have_lock)
  401. {
  402. spinlock_release(&targetcpu->c_runqueue_lock);
  403. }
  404. }
  405. /*
  406. * Create a new thread based on an existing one.
  407. *
  408. * The new thread has name NAME, and starts executing in function
  409. * ENTRYPOINT. DATA1 and DATA2 are passed to ENTRYPOINT.
  410. *
  411. * The new thread is created in the process P. If P is null, the
  412. * process is inherited from the caller. It will start on the same CPU
  413. * as the caller, unless the scheduler intervenes first.
  414. */
  415. int
  416. thread_fork(const char *name,
  417. struct proc *proc,
  418. void (*entrypoint)(void *data1, unsigned long data2),
  419. void *data1, unsigned long data2)
  420. {
  421. struct thread *newthread;
  422. int result;
  423. #ifdef UW
  424. DEBUG(DB_THREADS,"Forking thread: %s\n",name);
  425. #endif // UW
  426. newthread = thread_create(name);
  427. if (newthread == NULL) {
  428. return ENOMEM;
  429. }
  430. /* Allocate a stack */
  431. newthread->t_stack = kmalloc(STACK_SIZE);
  432. if (newthread->t_stack == NULL) {
  433. thread_destroy(newthread);
  434. return ENOMEM;
  435. }
  436. thread_checkstack_init(newthread);
  437. /*
  438. * Now we clone various fields from the parent thread.
  439. */
  440. /* Thread subsystem fields */
  441. newthread->t_cpu = curthread->t_cpu;
  442. /* Attach the new thread to its process */
  443. if (proc == NULL) {
  444. proc = curthread->t_proc;
  445. }
  446. result = proc_addthread(proc, newthread);
  447. if (result) {
  448. /* thread_destroy will clean up the stack */
  449. thread_destroy(newthread);
  450. return result;
  451. }
  452. /*
  453. * Because new threads come out holding the cpu runqueue lock
  454. * (see notes at bottom of thread_switch), we need to account
  455. * for the spllower() that will be done releasing it.
  456. */
  457. newthread->t_iplhigh_count++;
  458. /* Set up the switchframe so entrypoint() gets called */
  459. switchframe_init(newthread, entrypoint, data1, data2);
  460. /* Lock the current cpu's run queue and make the new thread runnable */
  461. thread_make_runnable(newthread, false);
  462. return 0;
  463. }
  464. /*
  465. * High level, machine-independent context switch code.
  466. *
  467. * The current thread is queued appropriately and its state is changed
  468. * to NEWSTATE; another thread to run is selected and switched to.
  469. *
  470. * If NEWSTATE is S_SLEEP, the thread is queued on the wait channel
  471. * WC. Otherwise WC should be NULL.
  472. */
  473. static
  474. void
  475. thread_switch(threadstate_t newstate, struct wchan *wc)
  476. {
  477. struct thread *cur, *next;
  478. int spl;
  479. DEBUGASSERT(curcpu->c_curthread == curthread);
  480. DEBUGASSERT(curthread->t_cpu == curcpu->c_self);
  481. /* Explicitly disable interrupts on this processor */
  482. spl = splhigh();
  483. cur = curthread;
  484. /*
  485. * If we're idle, return without doing anything. This happens
  486. * when the timer interrupt interrupts the idle loop.
  487. */
  488. if (curcpu->c_isidle) {
  489. splx(spl);
  490. return;
  491. }
  492. /* Check the stack guard band. */
  493. thread_checkstack(cur);
  494. /* Lock the run queue. */
  495. spinlock_acquire(&curcpu->c_runqueue_lock);
  496. /* Micro-optimization: if nothing to do, just return */
  497. if (newstate == S_READY && threadlist_isempty(&curcpu->c_runqueue)) {
  498. spinlock_release(&curcpu->c_runqueue_lock);
  499. splx(spl);
  500. return;
  501. }
  502. /* Put the thread in the right place. */
  503. switch (newstate) {
  504. case S_RUN:
  505. panic("Illegal S_RUN in thread_switch\n");
  506. case S_READY:
  507. thread_make_runnable(cur, true /*have lock*/);
  508. break;
  509. case S_SLEEP:
  510. cur->t_wchan_name = wc->wc_name;
  511. /*
  512. * Add the thread to the list in the wait channel, and
  513. * unlock same. To avoid a race with someone else
  514. * calling wchan_wake*, we must keep the wchan locked
  515. * from the point the caller of wchan_sleep locked it
  516. * until the thread is on the list.
  517. *
  518. * (We could for symmetry relock the channel before
  519. * returning from wchan_sleep, but we don't, for two
  520. * reasons. One is that the caller is unlikely to need
  521. * or want it locked and if it does can lock it itself
  522. * without racing. Exercise: what's the other?)
  523. */
  524. threadlist_addtail(&wc->wc_threads, cur);
  525. wchan_unlock(wc);
  526. break;
  527. case S_ZOMBIE:
  528. cur->t_wchan_name = "ZOMBIE";
  529. threadlist_addtail(&curcpu->c_zombies, cur);
  530. break;
  531. }
  532. cur->t_state = newstate;
  533. /*
  534. * Get the next thread. While there isn't one, call md_idle().
  535. * curcpu->c_isidle must be true when md_idle is
  536. * called. Unlock the runqueue while idling too, to make sure
  537. * things can be added to it.
  538. *
  539. * Note that we don't need to unlock the runqueue atomically
  540. * with idling; becoming unidle requires receiving an
  541. * interrupt (either a hardware interrupt or an interprocessor
  542. * interrupt from another cpu posting a wakeup) and idling
  543. * *is* atomic with respect to re-enabling interrupts.
  544. *
  545. * Note that c_isidle becomes true briefly even if we don't go
  546. * idle. However, because one is supposed to hold the runqueue
  547. * lock to look at it, this should not be visible or matter.
  548. */
  549. /* The current cpu is now idle. */
  550. curcpu->c_isidle = true;
  551. do {
  552. next = threadlist_remhead(&curcpu->c_runqueue);
  553. if (next == NULL) {
  554. spinlock_release(&curcpu->c_runqueue_lock);
  555. cpu_idle();
  556. spinlock_acquire(&curcpu->c_runqueue_lock);
  557. }
  558. } while (next == NULL);
  559. curcpu->c_isidle = false;
  560. /*
  561. * Note that curcpu->c_curthread may be the same variable as
  562. * curthread and it may not be, depending on how curthread and
  563. * curcpu are defined by the MD code. We'll assign both and
  564. * assume the compiler will optimize one away if they're the
  565. * same.
  566. */
  567. curcpu->c_curthread = next;
  568. curthread = next;
  569. /* do the switch (in assembler in switch.S) */
  570. switchframe_switch(&cur->t_context, &next->t_context);
  571. /*
  572. * When we get to this point we are either running in the next
  573. * thread, or have come back to the same thread again,
  574. * depending on how you look at it. That is,
  575. * switchframe_switch returns immediately in another thread
  576. * context, which in general will be executing here with a
  577. * different stack and different values in the local
  578. * variables. (Although new threads go to thread_startup
  579. * instead.) But, later on when the processor, or some
  580. * processor, comes back to the previous thread, it's also
  581. * executing here with the *same* value in the local
  582. * variables.
  583. *
  584. * The upshot, however, is as follows:
  585. *
  586. * - The thread now currently running is "cur", not "next",
  587. * because when we return from switchrame_switch on the
  588. * same stack, we're back to the thread that
  589. * switchframe_switch call switched away from, which is
  590. * "cur".
  591. *
  592. * - "cur" is _not_ the thread that just *called*
  593. * switchframe_switch.
  594. *
  595. * - If newstate is S_ZOMB we never get back here in that
  596. * context at all.
  597. *
  598. * - If the thread just chosen to run ("next") was a new
  599. * thread, we don't get to this code again until
  600. * *another* context switch happens, because when new
  601. * threads return from switchframe_switch they teleport
  602. * to thread_startup.
  603. *
  604. * - At this point the thread whose stack we're now on may
  605. * have been migrated to another cpu since it last ran.
  606. *
  607. * The above is inherently confusing and will probably take a
  608. * while to get used to.
  609. *
  610. * However, the important part is that code placed here, after
  611. * the call to switchframe_switch, does not necessarily run on
  612. * every context switch. Thus any such code must be either
  613. * skippable on some switches or also called from
  614. * thread_startup.
  615. */
  616. /* Clear the wait channel and set the thread state. */
  617. cur->t_wchan_name = NULL;
  618. cur->t_state = S_RUN;
  619. /* Unlock the run queue. */
  620. spinlock_release(&curcpu->c_runqueue_lock);
  621. /* Activate our address space in the MMU. */
  622. as_activate();
  623. /* Clean up dead threads. */
  624. exorcise();
  625. /* Turn interrupts back on. */
  626. splx(spl);
  627. }
  628. /*
  629. * This function is where new threads start running. The arguments
  630. * ENTRYPOINT, DATA1, and DATA2 are passed through from thread_fork.
  631. *
  632. * Because new code comes here from inside the middle of
  633. * thread_switch, the beginning part of this function must match the
  634. * tail of thread_switch.
  635. */
  636. void
  637. thread_startup(void (*entrypoint)(void *data1, unsigned long data2),
  638. void *data1, unsigned long data2)
  639. {
  640. struct thread *cur;
  641. cur = curthread;
  642. /* Clear the wait channel and set the thread state. */
  643. cur->t_wchan_name = NULL;
  644. cur->t_state = S_RUN;
  645. /* Release the runqueue lock acquired in thread_switch. */
  646. spinlock_release(&curcpu->c_runqueue_lock);
  647. /* Activate our address space in the MMU. */
  648. as_activate();
  649. /* Clean up dead threads. */
  650. exorcise();
  651. /* Enable interrupts. */
  652. spl0();
  653. #if OPT_SYNCHPROBS
  654. /* Yield a random number of times to get a good mix of threads. */
  655. {
  656. int i, n;
  657. n = random()%161 + random()%161;
  658. for (i=0; i<n; i++) {
  659. thread_yield();
  660. }
  661. }
  662. #endif
  663. /* Call the function. */
  664. entrypoint(data1, data2);
  665. /* Done. */
  666. thread_exit();
  667. }
  668. /*
  669. * Cause the current thread to exit.
  670. *
  671. * The parts of the thread structure we don't actually need to run
  672. * should be cleaned up right away. The rest has to wait until
  673. * thread_destroy is called from exorcise().
  674. *
  675. * Does not return.
  676. */
  677. void
  678. thread_exit(void)
  679. {
  680. struct thread *cur;
  681. cur = curthread;
  682. #ifdef UW
  683. /* threads for user processes should have detached from their process
  684. in sys__exit */
  685. KASSERT(curproc == kproc || curproc == NULL || curproc->exitcode != -1);
  686. /* kernel threads don't go through sys__exit, so we detach them from kproc here */
  687. if (curproc == kproc) {
  688. proc_remthread(cur);
  689. }
  690. #else // UW
  691. proc_remthread(cur);
  692. #endif // UW
  693. /* Make sure we *are* detached (move this only if you're sure!) */
  694. KASSERT(cur->t_proc == NULL);
  695. /* Check the stack guard band. */
  696. thread_checkstack(cur);
  697. /* Interrupts off on this processor */
  698. splhigh();
  699. thread_switch(S_ZOMBIE, NULL);
  700. panic("The zombie walks!\n");
  701. }
  702. /*
  703. * Yield the cpu to another process, but stay runnable.
  704. */
  705. void
  706. thread_yield(void)
  707. {
  708. thread_switch(S_READY, NULL);
  709. }
  710. ////////////////////////////////////////////////////////////
  711. /*
  712. * Scheduler.
  713. *
  714. * This is called periodically from hardclock(). It should reshuffle
  715. * the current CPU's run queue by job priority.
  716. */
  717. void
  718. schedule(void)
  719. {
  720. /*
  721. * You can write this. If we do nothing, threads will run in
  722. * round-robin fashion.
  723. */
  724. }
  725. /*
  726. * Thread migration.
  727. *
  728. * This is also called periodically from hardclock(). If the current
  729. * CPU is busy and other CPUs are idle, or less busy, it should move
  730. * threads across to those other other CPUs.
  731. *
  732. * Migrating threads isn't free because of cache affinity; a thread's
  733. * working cache set will end up having to be moved to the other CPU,
  734. * which is fairly slow. The tradeoff between this performance loss
  735. * and the performance loss due to underutilization of some CPUs is
  736. * something that needs to be tuned and probably is workload-specific.
  737. *
  738. * For here and now, because we know we're running on System/161 and
  739. * System/161 does not (yet) model such cache effects, we'll be very
  740. * aggressive.
  741. */
  742. void
  743. thread_consider_migration(void)
  744. {
  745. unsigned my_count, total_count, one_share, to_send;
  746. unsigned i, numcpus;
  747. struct cpu *c;
  748. struct threadlist victims;
  749. struct thread *t;
  750. my_count = total_count = 0;
  751. numcpus = cpuarray_num(&allcpus);
  752. for (i=0; i<numcpus; i++) {
  753. c = cpuarray_get(&allcpus, i);
  754. spinlock_acquire(&c->c_runqueue_lock);
  755. total_count += c->c_runqueue.tl_count;
  756. if (c == curcpu->c_self) {
  757. my_count = c->c_runqueue.tl_count;
  758. }
  759. spinlock_release(&c->c_runqueue_lock);
  760. }
  761. one_share = DIVROUNDUP(total_count, numcpus);
  762. if (my_count < one_share) {
  763. return;
  764. }
  765. to_send = my_count - one_share;
  766. threadlist_init(&victims);
  767. spinlock_acquire(&curcpu->c_runqueue_lock);
  768. for (i=0; i<to_send; i++) {
  769. t = threadlist_remtail(&curcpu->c_runqueue);
  770. threadlist_addhead(&victims, t);
  771. }
  772. spinlock_release(&curcpu->c_runqueue_lock);
  773. for (i=0; i < numcpus && to_send > 0; i++) {
  774. c = cpuarray_get(&allcpus, i);
  775. if (c == curcpu->c_self) {
  776. continue;
  777. }
  778. spinlock_acquire(&c->c_runqueue_lock);
  779. while (c->c_runqueue.tl_count < one_share && to_send > 0) {
  780. t = threadlist_remhead(&victims);
  781. /*
  782. * Ordinarily, curthread will not appear on
  783. * the run queue. However, it can under the
  784. * following circumstances:
  785. * - it went to sleep;
  786. * - the processor became idle, so it
  787. * remained curthread;
  788. * - it was reawakened, so it was put on the
  789. * run queue;
  790. * - and the processor hasn't fully unidled
  791. * yet, so all these things are still true.
  792. *
  793. * If the timer interrupt happens at (almost)
  794. * exactly the proper moment, we can come here
  795. * while things are in this state and see
  796. * curthread. However, *migrating* curthread
  797. * can cause bad things to happen (Exercise:
  798. * Why? And what?) so shuffle it to the end of
  799. * the list and decrement to_send in order to
  800. * skip it. Then it goes back on our own run
  801. * queue below.
  802. */
  803. if (t == curthread) {
  804. threadlist_addtail(&victims, t);
  805. to_send--;
  806. continue;
  807. }
  808. t->t_cpu = c;
  809. threadlist_addtail(&c->c_runqueue, t);
  810. DEBUG(DB_THREADS,
  811. "Migrated thread %s: cpu %u -> %u",
  812. t->t_name, curcpu->c_number, c->c_number);
  813. to_send--;
  814. if (c->c_isidle) {
  815. /*
  816. * Other processor is idle; send
  817. * interrupt to make sure it unidles.
  818. */
  819. ipi_send(c, IPI_UNIDLE);
  820. }
  821. }
  822. spinlock_release(&c->c_runqueue_lock);
  823. }
  824. /*
  825. * Because the code above isn't atomic, the thread counts may have
  826. * changed while we were working and we may end up with leftovers.
  827. * Don't panic; just put them back on our own run queue.
  828. */
  829. if (!threadlist_isempty(&victims)) {
  830. spinlock_acquire(&curcpu->c_runqueue_lock);
  831. while ((t = threadlist_remhead(&victims)) != NULL) {
  832. threadlist_addtail(&curcpu->c_runqueue, t);
  833. }
  834. spinlock_release(&curcpu->c_runqueue_lock);
  835. }
  836. KASSERT(threadlist_isempty(&victims));
  837. threadlist_cleanup(&victims);
  838. }
  839. ////////////////////////////////////////////////////////////
  840. /*
  841. * Wait channel functions
  842. */
  843. /*
  844. * Create a wait channel. NAME is a symbolic string name for it.
  845. * This is what's displayed by ps -alx in Unix.
  846. *
  847. * NAME should generally be a string constant. If it isn't, alternate
  848. * arrangements should be made to free it after the wait channel is
  849. * destroyed.
  850. */
  851. struct wchan *
  852. wchan_create(const char *name)
  853. {
  854. struct wchan *wc;
  855. wc = kmalloc(sizeof(*wc));
  856. if (wc == NULL) {
  857. return NULL;
  858. }
  859. spinlock_init(&wc->wc_lock);
  860. threadlist_init(&wc->wc_threads);
  861. wc->wc_name = name;
  862. return wc;
  863. }
  864. /*
  865. * Destroy a wait channel. Must be empty and unlocked.
  866. * (The corresponding cleanup functions require this.)
  867. */
  868. void
  869. wchan_destroy(struct wchan *wc)
  870. {
  871. spinlock_cleanup(&wc->wc_lock);
  872. threadlist_cleanup(&wc->wc_threads);
  873. kfree(wc);
  874. }
  875. /*
  876. * Lock and unlock a wait channel, respectively.
  877. */
  878. void
  879. wchan_lock(struct wchan *wc)
  880. {
  881. spinlock_acquire(&wc->wc_lock);
  882. }
  883. void
  884. wchan_unlock(struct wchan *wc)
  885. {
  886. spinlock_release(&wc->wc_lock);
  887. }
  888. /*
  889. * Yield the cpu to another process, and go to sleep, on the specified
  890. * wait channel WC. Calling wakeup on the channel will make the thread
  891. * runnable again. The channel must be locked, and will be *unlocked*
  892. * upon return.
  893. */
  894. void
  895. wchan_sleep(struct wchan *wc)
  896. {
  897. /* may not sleep in an interrupt handler */
  898. KASSERT(!curthread->t_in_interrupt);
  899. thread_switch(S_SLEEP, wc);
  900. }
  901. /*
  902. * Wake up one thread sleeping on a wait channel.
  903. */
  904. void
  905. wchan_wakeone(struct wchan *wc)
  906. {
  907. struct thread *target;
  908. /* Lock the channel and grab a thread from it */
  909. spinlock_acquire(&wc->wc_lock);
  910. target = threadlist_remhead(&wc->wc_threads);
  911. /*
  912. * Nobody else can wake up this thread now, so we don't need
  913. * to hang onto the lock.
  914. */
  915. spinlock_release(&wc->wc_lock);
  916. if (target == NULL) {
  917. /* Nobody was sleeping. */
  918. return;
  919. }
  920. thread_make_runnable(target, false);
  921. }
  922. /*
  923. * Wake up all threads sleeping on a wait channel.
  924. */
  925. void
  926. wchan_wakeall(struct wchan *wc)
  927. {
  928. struct thread *target;
  929. struct threadlist list;
  930. threadlist_init(&list);
  931. /*
  932. * Lock the channel and grab all the threads, moving them to a
  933. * private list.
  934. */
  935. spinlock_acquire(&wc->wc_lock);
  936. while ((target = threadlist_remhead(&wc->wc_threads)) != NULL) {
  937. threadlist_addtail(&list, target);
  938. }
  939. /*
  940. * Nobody else can wake up these threads now, so we don't need
  941. * to hang onto the lock.
  942. */
  943. spinlock_release(&wc->wc_lock);
  944. /*
  945. * We could conceivably sort by cpu first to cause fewer lock
  946. * ops and fewer IPIs, but for now at least don't bother. Just
  947. * make each thread runnable.
  948. */
  949. while ((target = threadlist_remhead(&list)) != NULL) {
  950. thread_make_runnable(target, false);
  951. }
  952. threadlist_cleanup(&list);
  953. }
  954. /*
  955. * Return nonzero if there are no threads sleeping on the channel.
  956. * This is meant to be used only for diagnostic purposes.
  957. */
  958. bool
  959. wchan_isempty(struct wchan *wc)
  960. {
  961. bool ret;
  962. spinlock_acquire(&wc->wc_lock);
  963. ret = threadlist_isempty(&wc->wc_threads);
  964. spinlock_release(&wc->wc_lock);
  965. return ret;
  966. }
  967. ////////////////////////////////////////////////////////////
  968. /*
  969. * Machine-independent IPI handling
  970. */
  971. /*
  972. * Send an IPI (inter-processor interrupt) to the specified CPU.
  973. */
  974. void
  975. ipi_send(struct cpu *target, int code)
  976. {
  977. KASSERT(code >= 0 && code < 32);
  978. spinlock_acquire(&target->c_ipi_lock);
  979. target->c_ipi_pending |= (uint32_t)1 << code;
  980. mainbus_send_ipi(target);
  981. spinlock_release(&target->c_ipi_lock);
  982. }
  983. void
  984. ipi_broadcast(int code)
  985. {
  986. unsigned i;
  987. struct cpu *c;
  988. for (i=0; i < cpuarray_num(&allcpus); i++) {
  989. c = cpuarray_get(&allcpus, i);
  990. if (c != curcpu->c_self) {
  991. ipi_send(c, code);
  992. }
  993. }
  994. }
  995. void
  996. ipi_tlbshootdown(struct cpu *target, const struct tlbshootdown *mapping)
  997. {
  998. int n;
  999. spinlock_acquire(&target->c_ipi_lock);
  1000. n = target->c_numshootdown;
  1001. if (n == TLBSHOOTDOWN_MAX) {
  1002. target->c_numshootdown = TLBSHOOTDOWN_ALL;
  1003. }
  1004. else {
  1005. target->c_shootdown[n] = *mapping;
  1006. target->c_numshootdown = n+1;
  1007. }
  1008. target->c_ipi_pending |= (uint32_t)1 << IPI_TLBSHOOTDOWN;
  1009. mainbus_send_ipi(target);
  1010. spinlock_release(&target->c_ipi_lock);
  1011. }
  1012. void
  1013. interprocessor_interrupt(void)
  1014. {
  1015. uint32_t bits;
  1016. int i;
  1017. spinlock_acquire(&curcpu->c_ipi_lock);
  1018. bits = curcpu->c_ipi_pending;
  1019. if (bits & (1U << IPI_PANIC)) {
  1020. /* panic on another cpu - just stop dead */
  1021. cpu_halt();
  1022. }
  1023. if (bits & (1U << IPI_OFFLINE)) {
  1024. /* offline request */
  1025. spinlock_acquire(&curcpu->c_runqueue_lock);
  1026. if (!curcpu->c_isidle) {
  1027. kprintf("cpu%d: offline: warning: not idle\n",
  1028. curcpu->c_number);
  1029. }
  1030. spinlock_release(&curcpu->c_runqueue_lock);
  1031. kprintf("cpu%d: offline.\n", curcpu->c_number);
  1032. cpu_halt();
  1033. }
  1034. if (bits & (1U << IPI_UNIDLE)) {
  1035. /*
  1036. * The cpu has already unidled itself to take the
  1037. * interrupt; don't need to do anything else.
  1038. */
  1039. }
  1040. if (bits & (1U << IPI_TLBSHOOTDOWN)) {
  1041. if (curcpu->c_numshootdown == TLBSHOOTDOWN_ALL) {
  1042. vm_tlbshootdown_all();
  1043. }
  1044. else {
  1045. for (i=0; i<curcpu->c_numshootdown; i++) {
  1046. vm_tlbshootdown(&curcpu->c_shootdown[i]);
  1047. }
  1048. }
  1049. curcpu->c_numshootdown = 0;
  1050. }
  1051. curcpu->c_ipi_pending = 0;
  1052. spinlock_release(&curcpu->c_ipi_lock);
  1053. }