thread.c 30 KB

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