trap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. #include <types.h>
  30. #include <signal.h>
  31. #include <lib.h>
  32. #include <mips/specialreg.h>
  33. #include <mips/trapframe.h>
  34. #include <cpu.h>
  35. #include <spl.h>
  36. #include <thread.h>
  37. #include <current.h>
  38. #include <vm.h>
  39. #include <mainbus.h>
  40. #include <syscall.h>
  41. /* in exception.S */
  42. extern void asm_usermode(struct trapframe *tf);
  43. /* called only from assembler, so not declared in a header */
  44. void mips_trap(struct trapframe *tf);
  45. /* Names for trap codes */
  46. #define NTRAPCODES 13
  47. static const char *const trapcodenames[NTRAPCODES] = {
  48. "Interrupt",
  49. "TLB modify trap",
  50. "TLB miss on load",
  51. "TLB miss on store",
  52. "Address error on load",
  53. "Address error on store",
  54. "Bus error on code",
  55. "Bus error on data",
  56. "System call",
  57. "Break instruction",
  58. "Illegal instruction",
  59. "Coprocessor unusable",
  60. "Arithmetic overflow",
  61. };
  62. /*
  63. * Function called when user-level code hits a fatal fault.
  64. */
  65. static
  66. void
  67. kill_curthread(vaddr_t epc, unsigned code, vaddr_t vaddr)
  68. {
  69. int sig = 0;
  70. KASSERT(code < NTRAPCODES);
  71. switch (code) {
  72. case EX_IRQ:
  73. case EX_IBE:
  74. case EX_DBE:
  75. case EX_SYS:
  76. /* should not be seen */
  77. KASSERT(0);
  78. sig = SIGABRT;
  79. break;
  80. case EX_MOD:
  81. case EX_TLBL:
  82. case EX_TLBS:
  83. sig = SIGSEGV;
  84. break;
  85. case EX_ADEL:
  86. case EX_ADES:
  87. sig = SIGBUS;
  88. break;
  89. case EX_BP:
  90. sig = SIGTRAP;
  91. break;
  92. case EX_RI:
  93. sig = SIGILL;
  94. break;
  95. case EX_CPU:
  96. sig = SIGSEGV;
  97. break;
  98. case EX_OVF:
  99. sig = SIGFPE;
  100. break;
  101. }
  102. /*
  103. * You will probably want to change this.
  104. */
  105. kprintf("Fatal user mode trap %u sig %d (%s, epc 0x%x, vaddr 0x%x)\n",
  106. code, sig, trapcodenames[code], epc, vaddr);
  107. panic("I don't know how to handle this\n");
  108. }
  109. /*
  110. * General trap (exception) handling function for mips.
  111. * This is called by the assembly-language exception handler once
  112. * the trapframe has been set up.
  113. */
  114. void
  115. mips_trap(struct trapframe *tf)
  116. {
  117. uint32_t code;
  118. bool isutlb, iskern;
  119. int spl;
  120. /* The trap frame is supposed to be 37 registers long. */
  121. KASSERT(sizeof(struct trapframe)==(37*4));
  122. /*
  123. * Extract the exception code info from the register fields.
  124. */
  125. code = (tf->tf_cause & CCA_CODE) >> CCA_CODESHIFT;
  126. isutlb = (tf->tf_cause & CCA_UTLB) != 0;
  127. iskern = (tf->tf_status & CST_KUp) == 0;
  128. KASSERT(code < NTRAPCODES);
  129. /* Make sure we haven't run off our stack */
  130. if (curthread != NULL && curthread->t_stack != NULL) {
  131. KASSERT((vaddr_t)tf > (vaddr_t)curthread->t_stack);
  132. KASSERT((vaddr_t)tf < (vaddr_t)(curthread->t_stack
  133. + STACK_SIZE));
  134. }
  135. /* Interrupt? Call the interrupt handler and return. */
  136. if (code == EX_IRQ) {
  137. int old_in;
  138. bool doadjust;
  139. old_in = curthread->t_in_interrupt;
  140. curthread->t_in_interrupt = 1;
  141. /*
  142. * The processor has turned interrupts off; if the
  143. * currently recorded interrupt state is interrupts on
  144. * (spl of 0), adjust the recorded state to match, and
  145. * restore after processing the interrupt.
  146. *
  147. * How can we get an interrupt if the recorded state
  148. * is interrupts off? Well, as things currently stand
  149. * when the CPU finishes idling it flips interrupts on
  150. * and off to allow things to happen, but leaves
  151. * curspl high while doing so.
  152. *
  153. * While we're here, assert that the interrupt
  154. * handling code hasn't leaked a spinlock or an
  155. * splhigh().
  156. */
  157. if (curthread->t_curspl == 0) {
  158. KASSERT(curthread->t_curspl == 0);
  159. KASSERT(curthread->t_iplhigh_count == 0);
  160. curthread->t_curspl = IPL_HIGH;
  161. curthread->t_iplhigh_count++;
  162. doadjust = true;
  163. }
  164. else {
  165. doadjust = false;
  166. }
  167. mainbus_interrupt(tf);
  168. if (doadjust) {
  169. KASSERT(curthread->t_curspl == IPL_HIGH);
  170. KASSERT(curthread->t_iplhigh_count == 1);
  171. curthread->t_iplhigh_count--;
  172. curthread->t_curspl = 0;
  173. }
  174. curthread->t_in_interrupt = old_in;
  175. goto done2;
  176. }
  177. /*
  178. * The processor turned interrupts off when it took the trap.
  179. *
  180. * While we're in the kernel, and not actually handling an
  181. * interrupt, restore the interrupt state to where it was in
  182. * the previous context, which may be low (interrupts on).
  183. *
  184. * Do this by forcing splhigh(), which may do a redundant
  185. * cpu_irqoff() but forces the stored MI interrupt state into
  186. * sync, then restoring the previous state.
  187. */
  188. spl = splhigh();
  189. splx(spl);
  190. /* Syscall? Call the syscall handler and return. */
  191. if (code == EX_SYS) {
  192. /* Interrupts should have been on while in user mode. */
  193. KASSERT(curthread->t_curspl == 0);
  194. KASSERT(curthread->t_iplhigh_count == 0);
  195. DEBUG(DB_SYSCALL, "syscall: #%d, args %x %x %x %x\n",
  196. tf->tf_v0, tf->tf_a0, tf->tf_a1, tf->tf_a2, tf->tf_a3);
  197. syscall(tf);
  198. goto done;
  199. }
  200. /*
  201. * Ok, it wasn't any of the really easy cases.
  202. * Call vm_fault on the TLB exceptions.
  203. * Panic on the bus error exceptions.
  204. */
  205. switch (code) {
  206. case EX_MOD:
  207. if (vm_fault(VM_FAULT_READONLY, tf->tf_vaddr)==0) {
  208. goto done;
  209. }
  210. break;
  211. case EX_TLBL:
  212. if (vm_fault(VM_FAULT_READ, tf->tf_vaddr)==0) {
  213. goto done;
  214. }
  215. break;
  216. case EX_TLBS:
  217. if (vm_fault(VM_FAULT_WRITE, tf->tf_vaddr)==0) {
  218. goto done;
  219. }
  220. break;
  221. case EX_IBE:
  222. case EX_DBE:
  223. /*
  224. * This means you loaded invalid TLB entries, or
  225. * touched invalid parts of the direct-mapped
  226. * segments. These are serious kernel errors, so
  227. * panic.
  228. *
  229. * The MIPS won't even tell you what invalid address
  230. * caused the bus error.
  231. */
  232. panic("Bus error exception, PC=0x%x\n", tf->tf_epc);
  233. break;
  234. }
  235. /*
  236. * If we get to this point, it's a fatal fault - either it's
  237. * one of the other exceptions, like illegal instruction, or
  238. * it was a page fault we couldn't handle.
  239. */
  240. if (!iskern) {
  241. /*
  242. * Fatal fault in user mode.
  243. * Kill the current user process.
  244. */
  245. kill_curthread(tf->tf_epc, code, tf->tf_vaddr);
  246. goto done;
  247. }
  248. /*
  249. * Fatal fault in kernel mode.
  250. *
  251. * If pcb_badfaultfunc is set, we do not panic; badfaultfunc is
  252. * set by copyin/copyout and related functions to signify that
  253. * the addresses they're accessing are userlevel-supplied and
  254. * not trustable. What we actually want to do is resume
  255. * execution at the function pointed to by badfaultfunc. That's
  256. * going to be "copyfail" (see copyinout.c), which longjmps
  257. * back to copyin/copyout or wherever and returns EFAULT.
  258. *
  259. * Note that we do not just *call* this function, because that
  260. * won't necessarily do anything. We want the control flow
  261. * that is currently executing in copyin (or whichever), and
  262. * is stopped while we process the exception, to *teleport* to
  263. * copyfail.
  264. *
  265. * This is accomplished by changing tf->tf_epc and returning
  266. * from the exception handler.
  267. */
  268. if (curthread != NULL &&
  269. curthread->t_machdep.tm_badfaultfunc != NULL) {
  270. tf->tf_epc = (vaddr_t) curthread->t_machdep.tm_badfaultfunc;
  271. goto done;
  272. }
  273. /*
  274. * Really fatal kernel-mode fault.
  275. */
  276. kprintf("panic: Fatal exception %u (%s) in kernel mode\n", code,
  277. trapcodenames[code]);
  278. kprintf("panic: EPC 0x%x, exception vaddr 0x%x\n",
  279. tf->tf_epc, tf->tf_vaddr);
  280. panic("I can't handle this... I think I'll just die now...\n");
  281. done:
  282. /*
  283. * Turn interrupts off on the processor, without affecting the
  284. * stored interrupt state.
  285. */
  286. cpu_irqoff();
  287. done2:
  288. /*
  289. * The boot thread can get here (e.g. on interrupt return) but
  290. * since it doesn't go to userlevel, it can't be returning to
  291. * userlevel, so there's no need to set cputhreads[] and
  292. * cpustacks[]. Just return.
  293. */
  294. if (curthread->t_stack == NULL) {
  295. return;
  296. }
  297. cputhreads[curcpu->c_number] = (vaddr_t)curthread;
  298. cpustacks[curcpu->c_number] = (vaddr_t)curthread->t_stack + STACK_SIZE;
  299. /*
  300. * This assertion will fail if either
  301. * (1) curthread->t_stack is corrupted, or
  302. * (2) the trap frame is somehow on the wrong kernel stack.
  303. *
  304. * If cpustacks[] is corrupted, the next trap back to the
  305. * kernel will (most likely) hang the system, so it's better
  306. * to find out now.
  307. */
  308. KASSERT(SAME_STACK(cpustacks[curcpu->c_number]-1, (vaddr_t)tf));
  309. }
  310. /*
  311. * Function for entering user mode.
  312. *
  313. * This should not be used by threads returning from traps - they
  314. * should just return from mips_trap(). It should be used by threads
  315. * entering user mode for the first time - whether the child thread in
  316. * a fork(), or into a brand-new address space after exec(), or when
  317. * starting the first userlevel program.
  318. *
  319. * It works by jumping into the exception return code.
  320. *
  321. * mips_usermode is common code for this. It cannot usefully be called
  322. * outside the mips port, but should be called from one of the
  323. * following places:
  324. * - enter_new_process, for use by exec and equivalent.
  325. * - enter_forked_process, in syscall.c, for use by fork.
  326. */
  327. void
  328. mips_usermode(struct trapframe *tf)
  329. {
  330. /*
  331. * Interrupts should be off within the kernel while entering
  332. * user mode. However, while in user mode, interrupts should
  333. * be on. To interact properly with the spl-handling logic
  334. * above, we explicitly call spl0() and then call cpu_irqoff().
  335. */
  336. spl0();
  337. cpu_irqoff();
  338. cputhreads[curcpu->c_number] = (vaddr_t)curthread;
  339. cpustacks[curcpu->c_number] = (vaddr_t)curthread->t_stack + STACK_SIZE;
  340. /*
  341. * This assertion will fail if either
  342. * (1) cpustacks[] is corrupted, or
  343. * (2) the trap frame is not on our own kernel stack, or
  344. * (3) the boot thread tries to enter user mode.
  345. *
  346. * If cpustacks[] is corrupted, the next trap back to the
  347. * kernel will (most likely) hang the system, so it's better
  348. * to find out now.
  349. *
  350. * It's necessary for the trap frame used here to be on the
  351. * current thread's own stack. It cannot correctly be on
  352. * either another thread's stack or in the kernel heap.
  353. * (Exercise: why?)
  354. */
  355. KASSERT(SAME_STACK(cpustacks[curcpu->c_number]-1, (vaddr_t)tf));
  356. /*
  357. * This actually does it. See exception.S.
  358. */
  359. asm_usermode(tf);
  360. }
  361. /*
  362. * enter_new_process: go to user mode after loading an executable.
  363. *
  364. * Performs the necessary initialization so that the user program will
  365. * get the arguments supplied in argc/argv (note that argv must be a
  366. * user-level address), and begin executing at the specified entry
  367. * point. The stack pointer is initialized from the stackptr
  368. * argument. Note that passing argc/argv may use additional stack
  369. * space on some other platforms (but not on mips).
  370. *
  371. * Works by creating an ersatz trapframe.
  372. */
  373. void
  374. enter_new_process(int argc, userptr_t argv, vaddr_t stack, vaddr_t entry)
  375. {
  376. struct trapframe tf;
  377. bzero(&tf, sizeof(tf));
  378. tf.tf_status = CST_IRQMASK | CST_IEp | CST_KUp;
  379. tf.tf_epc = entry;
  380. tf.tf_a0 = argc;
  381. tf.tf_a1 = (vaddr_t)argv;
  382. tf.tf_sp = stack;
  383. mips_usermode(&tf);
  384. }