traps.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Modified by Cort Dougan (cort@cs.nmt.edu)
  10. * and Paul Mackerras (paulus@cs.anu.edu.au)
  11. */
  12. /*
  13. * This file handles the architecture-dependent parts of hardware exceptions
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/sched.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/stddef.h>
  20. #include <linux/unistd.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/slab.h>
  23. #include <linux/user.h>
  24. #include <linux/a.out.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/config.h>
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/prctl.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/system.h>
  33. #include <asm/io.h>
  34. #include <asm/reg.h>
  35. #include <asm/xmon.h>
  36. #include <asm/pmc.h>
  37. #ifdef CONFIG_XMON
  38. extern int xmon_bpt(struct pt_regs *regs);
  39. extern int xmon_sstep(struct pt_regs *regs);
  40. extern int xmon_iabr_match(struct pt_regs *regs);
  41. extern int xmon_dabr_match(struct pt_regs *regs);
  42. int (*debugger)(struct pt_regs *regs) = xmon;
  43. int (*debugger_bpt)(struct pt_regs *regs) = xmon_bpt;
  44. int (*debugger_sstep)(struct pt_regs *regs) = xmon_sstep;
  45. int (*debugger_iabr_match)(struct pt_regs *regs) = xmon_iabr_match;
  46. int (*debugger_dabr_match)(struct pt_regs *regs) = xmon_dabr_match;
  47. void (*debugger_fault_handler)(struct pt_regs *regs);
  48. #else
  49. #ifdef CONFIG_KGDB
  50. int (*debugger)(struct pt_regs *regs);
  51. int (*debugger_bpt)(struct pt_regs *regs);
  52. int (*debugger_sstep)(struct pt_regs *regs);
  53. int (*debugger_iabr_match)(struct pt_regs *regs);
  54. int (*debugger_dabr_match)(struct pt_regs *regs);
  55. void (*debugger_fault_handler)(struct pt_regs *regs);
  56. #else
  57. #define debugger(regs) do { } while (0)
  58. #define debugger_bpt(regs) 0
  59. #define debugger_sstep(regs) 0
  60. #define debugger_iabr_match(regs) 0
  61. #define debugger_dabr_match(regs) 0
  62. #define debugger_fault_handler ((void (*)(struct pt_regs *))0)
  63. #endif
  64. #endif
  65. /*
  66. * Trap & Exception support
  67. */
  68. DEFINE_SPINLOCK(die_lock);
  69. int die(const char * str, struct pt_regs * fp, long err)
  70. {
  71. static int die_counter;
  72. int nl = 0;
  73. console_verbose();
  74. spin_lock_irq(&die_lock);
  75. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  76. #ifdef CONFIG_PREEMPT
  77. printk("PREEMPT ");
  78. nl = 1;
  79. #endif
  80. #ifdef CONFIG_SMP
  81. printk("SMP NR_CPUS=%d ", NR_CPUS);
  82. nl = 1;
  83. #endif
  84. if (nl)
  85. printk("\n");
  86. show_regs(fp);
  87. spin_unlock_irq(&die_lock);
  88. /* do_exit() should take care of panic'ing from an interrupt
  89. * context so we don't handle it here
  90. */
  91. do_exit(err);
  92. }
  93. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  94. {
  95. siginfo_t info;
  96. if (!user_mode(regs)) {
  97. debugger(regs);
  98. die("Exception in kernel mode", regs, signr);
  99. }
  100. info.si_signo = signr;
  101. info.si_errno = 0;
  102. info.si_code = code;
  103. info.si_addr = (void __user *) addr;
  104. force_sig_info(signr, &info, current);
  105. /*
  106. * Init gets no signals that it doesn't have a handler for.
  107. * That's all very well, but if it has caused a synchronous
  108. * exception and we ignore the resulting signal, it will just
  109. * generate the same exception over and over again and we get
  110. * nowhere. Better to kill it and let the kernel panic.
  111. */
  112. if (current->pid == 1) {
  113. __sighandler_t handler;
  114. spin_lock_irq(&current->sighand->siglock);
  115. handler = current->sighand->action[signr-1].sa.sa_handler;
  116. spin_unlock_irq(&current->sighand->siglock);
  117. if (handler == SIG_DFL) {
  118. /* init has generated a synchronous exception
  119. and it doesn't have a handler for the signal */
  120. printk(KERN_CRIT "init has generated signal %d "
  121. "but has no handler for it\n", signr);
  122. do_exit(signr);
  123. }
  124. }
  125. }
  126. /*
  127. * I/O accesses can cause machine checks on powermacs.
  128. * Check if the NIP corresponds to the address of a sync
  129. * instruction for which there is an entry in the exception
  130. * table.
  131. * Note that the 601 only takes a machine check on TEA
  132. * (transfer error ack) signal assertion, and does not
  133. * set any of the top 16 bits of SRR1.
  134. * -- paulus.
  135. */
  136. static inline int check_io_access(struct pt_regs *regs)
  137. {
  138. #if defined CONFIG_8xx
  139. unsigned long msr = regs->msr;
  140. const struct exception_table_entry *entry;
  141. unsigned int *nip = (unsigned int *)regs->nip;
  142. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  143. && (entry = search_exception_tables(regs->nip)) != NULL) {
  144. /*
  145. * Check that it's a sync instruction, or somewhere
  146. * in the twi; isync; nop sequence that inb/inw/inl uses.
  147. * As the address is in the exception table
  148. * we should be able to read the instr there.
  149. * For the debug message, we look at the preceding
  150. * load or store.
  151. */
  152. if (*nip == 0x60000000) /* nop */
  153. nip -= 2;
  154. else if (*nip == 0x4c00012c) /* isync */
  155. --nip;
  156. /* eieio from I/O string functions */
  157. else if ((*nip) == 0x7c0006ac || *(nip+1) == 0x7c0006ac)
  158. nip += 2;
  159. if (*nip == 0x7c0004ac || (*nip >> 26) == 3 ||
  160. (*(nip+1) >> 26) == 3) {
  161. /* sync or twi */
  162. unsigned int rb;
  163. --nip;
  164. rb = (*nip >> 11) & 0x1f;
  165. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  166. (*nip & 0x100)? "OUT to": "IN from",
  167. regs->gpr[rb] - _IO_BASE, nip);
  168. regs->msr |= MSR_RI;
  169. regs->nip = entry->fixup;
  170. return 1;
  171. }
  172. }
  173. #endif /* CONFIG_8xx */
  174. return 0;
  175. }
  176. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  177. /* On 4xx, the reason for the machine check or program exception
  178. is in the ESR. */
  179. #define get_reason(regs) ((regs)->dsisr)
  180. #ifndef CONFIG_FSL_BOOKE
  181. #define get_mc_reason(regs) ((regs)->dsisr)
  182. #else
  183. #define get_mc_reason(regs) (mfspr(SPRN_MCSR))
  184. #endif
  185. #define REASON_FP ESR_FP
  186. #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
  187. #define REASON_PRIVILEGED ESR_PPR
  188. #define REASON_TRAP ESR_PTR
  189. /* single-step stuff */
  190. #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
  191. #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
  192. #else
  193. /* On non-4xx, the reason for the machine check or program
  194. exception is in the MSR. */
  195. #define get_reason(regs) ((regs)->msr)
  196. #define get_mc_reason(regs) ((regs)->msr)
  197. #define REASON_FP 0x100000
  198. #define REASON_ILLEGAL 0x80000
  199. #define REASON_PRIVILEGED 0x40000
  200. #define REASON_TRAP 0x20000
  201. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  202. #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
  203. #endif
  204. /*
  205. * This is "fall-back" implementation for configurations
  206. * which don't provide platform-specific machine check info
  207. */
  208. void __attribute__ ((weak))
  209. platform_machine_check(struct pt_regs *regs)
  210. {
  211. }
  212. void machine_check_exception(struct pt_regs *regs)
  213. {
  214. unsigned long reason = get_mc_reason(regs);
  215. if (user_mode(regs)) {
  216. regs->msr |= MSR_RI;
  217. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  218. return;
  219. }
  220. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  221. /* the qspan pci read routines can cause machine checks -- Cort */
  222. bad_page_fault(regs, regs->dar, SIGBUS);
  223. return;
  224. #endif
  225. if (debugger_fault_handler) {
  226. debugger_fault_handler(regs);
  227. regs->msr |= MSR_RI;
  228. return;
  229. }
  230. if (check_io_access(regs))
  231. return;
  232. #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
  233. if (reason & ESR_IMCP) {
  234. printk("Instruction");
  235. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  236. } else
  237. printk("Data");
  238. printk(" machine check in kernel mode.\n");
  239. #elif defined(CONFIG_440A)
  240. printk("Machine check in kernel mode.\n");
  241. if (reason & ESR_IMCP){
  242. printk("Instruction Synchronous Machine Check exception\n");
  243. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  244. }
  245. else {
  246. u32 mcsr = mfspr(SPRN_MCSR);
  247. if (mcsr & MCSR_IB)
  248. printk("Instruction Read PLB Error\n");
  249. if (mcsr & MCSR_DRB)
  250. printk("Data Read PLB Error\n");
  251. if (mcsr & MCSR_DWB)
  252. printk("Data Write PLB Error\n");
  253. if (mcsr & MCSR_TLBP)
  254. printk("TLB Parity Error\n");
  255. if (mcsr & MCSR_ICP){
  256. flush_instruction_cache();
  257. printk("I-Cache Parity Error\n");
  258. }
  259. if (mcsr & MCSR_DCSP)
  260. printk("D-Cache Search Parity Error\n");
  261. if (mcsr & MCSR_DCFP)
  262. printk("D-Cache Flush Parity Error\n");
  263. if (mcsr & MCSR_IMPE)
  264. printk("Machine Check exception is imprecise\n");
  265. /* Clear MCSR */
  266. mtspr(SPRN_MCSR, mcsr);
  267. }
  268. #elif defined (CONFIG_E500)
  269. printk("Machine check in kernel mode.\n");
  270. printk("Caused by (from MCSR=%lx): ", reason);
  271. if (reason & MCSR_MCP)
  272. printk("Machine Check Signal\n");
  273. if (reason & MCSR_ICPERR)
  274. printk("Instruction Cache Parity Error\n");
  275. if (reason & MCSR_DCP_PERR)
  276. printk("Data Cache Push Parity Error\n");
  277. if (reason & MCSR_DCPERR)
  278. printk("Data Cache Parity Error\n");
  279. if (reason & MCSR_GL_CI)
  280. printk("Guarded Load or Cache-Inhibited stwcx.\n");
  281. if (reason & MCSR_BUS_IAERR)
  282. printk("Bus - Instruction Address Error\n");
  283. if (reason & MCSR_BUS_RAERR)
  284. printk("Bus - Read Address Error\n");
  285. if (reason & MCSR_BUS_WAERR)
  286. printk("Bus - Write Address Error\n");
  287. if (reason & MCSR_BUS_IBERR)
  288. printk("Bus - Instruction Data Error\n");
  289. if (reason & MCSR_BUS_RBERR)
  290. printk("Bus - Read Data Bus Error\n");
  291. if (reason & MCSR_BUS_WBERR)
  292. printk("Bus - Read Data Bus Error\n");
  293. if (reason & MCSR_BUS_IPERR)
  294. printk("Bus - Instruction Parity Error\n");
  295. if (reason & MCSR_BUS_RPERR)
  296. printk("Bus - Read Parity Error\n");
  297. #elif defined (CONFIG_E200)
  298. printk("Machine check in kernel mode.\n");
  299. printk("Caused by (from MCSR=%lx): ", reason);
  300. if (reason & MCSR_MCP)
  301. printk("Machine Check Signal\n");
  302. if (reason & MCSR_CP_PERR)
  303. printk("Cache Push Parity Error\n");
  304. if (reason & MCSR_CPERR)
  305. printk("Cache Parity Error\n");
  306. if (reason & MCSR_EXCP_ERR)
  307. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  308. if (reason & MCSR_BUS_IRERR)
  309. printk("Bus - Read Bus Error on instruction fetch\n");
  310. if (reason & MCSR_BUS_DRERR)
  311. printk("Bus - Read Bus Error on data load\n");
  312. if (reason & MCSR_BUS_WRERR)
  313. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  314. #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
  315. printk("Machine check in kernel mode.\n");
  316. printk("Caused by (from SRR1=%lx): ", reason);
  317. switch (reason & 0x601F0000) {
  318. case 0x80000:
  319. printk("Machine check signal\n");
  320. break;
  321. case 0: /* for 601 */
  322. case 0x40000:
  323. case 0x140000: /* 7450 MSS error and TEA */
  324. printk("Transfer error ack signal\n");
  325. break;
  326. case 0x20000:
  327. printk("Data parity error signal\n");
  328. break;
  329. case 0x10000:
  330. printk("Address parity error signal\n");
  331. break;
  332. case 0x20000000:
  333. printk("L1 Data Cache error\n");
  334. break;
  335. case 0x40000000:
  336. printk("L1 Instruction Cache error\n");
  337. break;
  338. case 0x00100000:
  339. printk("L2 data cache parity error\n");
  340. break;
  341. default:
  342. printk("Unknown values in msr\n");
  343. }
  344. #endif /* CONFIG_4xx */
  345. /*
  346. * Optional platform-provided routine to print out
  347. * additional info, e.g. bus error registers.
  348. */
  349. platform_machine_check(regs);
  350. debugger(regs);
  351. die("machine check", regs, SIGBUS);
  352. }
  353. void SMIException(struct pt_regs *regs)
  354. {
  355. debugger(regs);
  356. #if !(defined(CONFIG_XMON) || defined(CONFIG_KGDB))
  357. show_regs(regs);
  358. panic("System Management Interrupt");
  359. #endif
  360. }
  361. void unknown_exception(struct pt_regs *regs)
  362. {
  363. printk("Bad trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  364. regs->nip, regs->msr, regs->trap, print_tainted());
  365. _exception(SIGTRAP, regs, 0, 0);
  366. }
  367. void instruction_breakpoint_exception(struct pt_regs *regs)
  368. {
  369. if (debugger_iabr_match(regs))
  370. return;
  371. _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
  372. }
  373. void RunModeException(struct pt_regs *regs)
  374. {
  375. _exception(SIGTRAP, regs, 0, 0);
  376. }
  377. /* Illegal instruction emulation support. Originally written to
  378. * provide the PVR to user applications using the mfspr rd, PVR.
  379. * Return non-zero if we can't emulate, or -EFAULT if the associated
  380. * memory access caused an access fault. Return zero on success.
  381. *
  382. * There are a couple of ways to do this, either "decode" the instruction
  383. * or directly match lots of bits. In this case, matching lots of
  384. * bits is faster and easier.
  385. *
  386. */
  387. #define INST_MFSPR_PVR 0x7c1f42a6
  388. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  389. #define INST_DCBA 0x7c0005ec
  390. #define INST_DCBA_MASK 0x7c0007fe
  391. #define INST_MCRXR 0x7c000400
  392. #define INST_MCRXR_MASK 0x7c0007fe
  393. #define INST_STRING 0x7c00042a
  394. #define INST_STRING_MASK 0x7c0007fe
  395. #define INST_STRING_GEN_MASK 0x7c00067e
  396. #define INST_LSWI 0x7c0004aa
  397. #define INST_LSWX 0x7c00042a
  398. #define INST_STSWI 0x7c0005aa
  399. #define INST_STSWX 0x7c00052a
  400. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  401. {
  402. u8 rT = (instword >> 21) & 0x1f;
  403. u8 rA = (instword >> 16) & 0x1f;
  404. u8 NB_RB = (instword >> 11) & 0x1f;
  405. u32 num_bytes;
  406. unsigned long EA;
  407. int pos = 0;
  408. /* Early out if we are an invalid form of lswx */
  409. if ((instword & INST_STRING_MASK) == INST_LSWX)
  410. if ((rT == rA) || (rT == NB_RB))
  411. return -EINVAL;
  412. EA = (rA == 0) ? 0 : regs->gpr[rA];
  413. switch (instword & INST_STRING_MASK) {
  414. case INST_LSWX:
  415. case INST_STSWX:
  416. EA += NB_RB;
  417. num_bytes = regs->xer & 0x7f;
  418. break;
  419. case INST_LSWI:
  420. case INST_STSWI:
  421. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  422. break;
  423. default:
  424. return -EINVAL;
  425. }
  426. while (num_bytes != 0)
  427. {
  428. u8 val;
  429. u32 shift = 8 * (3 - (pos & 0x3));
  430. switch ((instword & INST_STRING_MASK)) {
  431. case INST_LSWX:
  432. case INST_LSWI:
  433. if (get_user(val, (u8 __user *)EA))
  434. return -EFAULT;
  435. /* first time updating this reg,
  436. * zero it out */
  437. if (pos == 0)
  438. regs->gpr[rT] = 0;
  439. regs->gpr[rT] |= val << shift;
  440. break;
  441. case INST_STSWI:
  442. case INST_STSWX:
  443. val = regs->gpr[rT] >> shift;
  444. if (put_user(val, (u8 __user *)EA))
  445. return -EFAULT;
  446. break;
  447. }
  448. /* move EA to next address */
  449. EA += 1;
  450. num_bytes--;
  451. /* manage our position within the register */
  452. if (++pos == 4) {
  453. pos = 0;
  454. if (++rT == 32)
  455. rT = 0;
  456. }
  457. }
  458. return 0;
  459. }
  460. static int emulate_instruction(struct pt_regs *regs)
  461. {
  462. u32 instword;
  463. u32 rd;
  464. if (!user_mode(regs))
  465. return -EINVAL;
  466. CHECK_FULL_REGS(regs);
  467. if (get_user(instword, (u32 __user *)(regs->nip)))
  468. return -EFAULT;
  469. /* Emulate the mfspr rD, PVR.
  470. */
  471. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  472. rd = (instword >> 21) & 0x1f;
  473. regs->gpr[rd] = mfspr(SPRN_PVR);
  474. return 0;
  475. }
  476. /* Emulating the dcba insn is just a no-op. */
  477. if ((instword & INST_DCBA_MASK) == INST_DCBA)
  478. return 0;
  479. /* Emulate the mcrxr insn. */
  480. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  481. int shift = (instword >> 21) & 0x1c;
  482. unsigned long msk = 0xf0000000UL >> shift;
  483. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  484. regs->xer &= ~0xf0000000UL;
  485. return 0;
  486. }
  487. /* Emulate load/store string insn. */
  488. if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
  489. return emulate_string_inst(regs, instword);
  490. return -EINVAL;
  491. }
  492. /*
  493. * After we have successfully emulated an instruction, we have to
  494. * check if the instruction was being single-stepped, and if so,
  495. * pretend we got a single-step exception. This was pointed out
  496. * by Kumar Gala. -- paulus
  497. */
  498. static void emulate_single_step(struct pt_regs *regs)
  499. {
  500. if (single_stepping(regs)) {
  501. clear_single_step(regs);
  502. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  503. }
  504. }
  505. /*
  506. * Look through the list of trap instructions that are used for BUG(),
  507. * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
  508. * that the exception was caused by a trap instruction of some kind.
  509. * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
  510. * otherwise.
  511. */
  512. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  513. #ifndef CONFIG_MODULES
  514. #define module_find_bug(x) NULL
  515. #endif
  516. struct bug_entry *find_bug(unsigned long bugaddr)
  517. {
  518. struct bug_entry *bug;
  519. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  520. if (bugaddr == bug->bug_addr)
  521. return bug;
  522. return module_find_bug(bugaddr);
  523. }
  524. int check_bug_trap(struct pt_regs *regs)
  525. {
  526. struct bug_entry *bug;
  527. unsigned long addr;
  528. if (regs->msr & MSR_PR)
  529. return 0; /* not in kernel */
  530. addr = regs->nip; /* address of trap instruction */
  531. if (addr < PAGE_OFFSET)
  532. return 0;
  533. bug = find_bug(regs->nip);
  534. if (bug == NULL)
  535. return 0;
  536. if (bug->line & BUG_WARNING_TRAP) {
  537. /* this is a WARN_ON rather than BUG/BUG_ON */
  538. #ifdef CONFIG_XMON
  539. xmon_printf(KERN_ERR "Badness in %s at %s:%ld\n",
  540. bug->function, bug->file,
  541. bug->line & ~BUG_WARNING_TRAP);
  542. #endif /* CONFIG_XMON */
  543. printk(KERN_ERR "Badness in %s at %s:%ld\n",
  544. bug->function, bug->file,
  545. bug->line & ~BUG_WARNING_TRAP);
  546. dump_stack();
  547. return 1;
  548. }
  549. #ifdef CONFIG_XMON
  550. xmon_printf(KERN_CRIT "kernel BUG in %s at %s:%ld!\n",
  551. bug->function, bug->file, bug->line);
  552. xmon(regs);
  553. #endif /* CONFIG_XMON */
  554. printk(KERN_CRIT "kernel BUG in %s at %s:%ld!\n",
  555. bug->function, bug->file, bug->line);
  556. return 0;
  557. }
  558. void program_check_exception(struct pt_regs *regs)
  559. {
  560. unsigned int reason = get_reason(regs);
  561. extern int do_mathemu(struct pt_regs *regs);
  562. #ifdef CONFIG_MATH_EMULATION
  563. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  564. * but there seems to be a hardware bug on the 405GP (RevD)
  565. * that means ESR is sometimes set incorrectly - either to
  566. * ESR_DST (!?) or 0. In the process of chasing this with the
  567. * hardware people - not sure if it can happen on any illegal
  568. * instruction or only on FP instructions, whether there is a
  569. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  570. if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
  571. emulate_single_step(regs);
  572. return;
  573. }
  574. #endif /* CONFIG_MATH_EMULATION */
  575. if (reason & REASON_FP) {
  576. /* IEEE FP exception */
  577. int code = 0;
  578. u32 fpscr;
  579. /* We must make sure the FP state is consistent with
  580. * our MSR_FP in regs
  581. */
  582. preempt_disable();
  583. if (regs->msr & MSR_FP)
  584. giveup_fpu(current);
  585. preempt_enable();
  586. fpscr = current->thread.fpscr.val;
  587. fpscr &= fpscr << 22; /* mask summary bits with enables */
  588. if (fpscr & FPSCR_VX)
  589. code = FPE_FLTINV;
  590. else if (fpscr & FPSCR_OX)
  591. code = FPE_FLTOVF;
  592. else if (fpscr & FPSCR_UX)
  593. code = FPE_FLTUND;
  594. else if (fpscr & FPSCR_ZX)
  595. code = FPE_FLTDIV;
  596. else if (fpscr & FPSCR_XX)
  597. code = FPE_FLTRES;
  598. _exception(SIGFPE, regs, code, regs->nip);
  599. return;
  600. }
  601. if (reason & REASON_TRAP) {
  602. /* trap exception */
  603. if (debugger_bpt(regs))
  604. return;
  605. if (check_bug_trap(regs)) {
  606. regs->nip += 4;
  607. return;
  608. }
  609. _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
  610. return;
  611. }
  612. /* Try to emulate it if we should. */
  613. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  614. switch (emulate_instruction(regs)) {
  615. case 0:
  616. regs->nip += 4;
  617. emulate_single_step(regs);
  618. return;
  619. case -EFAULT:
  620. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  621. return;
  622. }
  623. }
  624. if (reason & REASON_PRIVILEGED)
  625. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  626. else
  627. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  628. }
  629. void single_step_exception(struct pt_regs *regs)
  630. {
  631. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  632. if (debugger_sstep(regs))
  633. return;
  634. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  635. }
  636. void alignment_exception(struct pt_regs *regs)
  637. {
  638. int fixed;
  639. fixed = fix_alignment(regs);
  640. if (fixed == 1) {
  641. regs->nip += 4; /* skip over emulated instruction */
  642. emulate_single_step(regs);
  643. return;
  644. }
  645. if (fixed == -EFAULT) {
  646. /* fixed == -EFAULT means the operand address was bad */
  647. if (user_mode(regs))
  648. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);
  649. else
  650. bad_page_fault(regs, regs->dar, SIGSEGV);
  651. return;
  652. }
  653. _exception(SIGBUS, regs, BUS_ADRALN, regs->dar);
  654. }
  655. void StackOverflow(struct pt_regs *regs)
  656. {
  657. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  658. current, regs->gpr[1]);
  659. debugger(regs);
  660. show_regs(regs);
  661. panic("kernel stack overflow");
  662. }
  663. void nonrecoverable_exception(struct pt_regs *regs)
  664. {
  665. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  666. regs->nip, regs->msr);
  667. debugger(regs);
  668. die("nonrecoverable exception", regs, SIGKILL);
  669. }
  670. void trace_syscall(struct pt_regs *regs)
  671. {
  672. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  673. current, current->pid, regs->nip, regs->link, regs->gpr[0],
  674. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  675. }
  676. #ifdef CONFIG_8xx
  677. void SoftwareEmulation(struct pt_regs *regs)
  678. {
  679. extern int do_mathemu(struct pt_regs *);
  680. extern int Soft_emulate_8xx(struct pt_regs *);
  681. int errcode;
  682. CHECK_FULL_REGS(regs);
  683. if (!user_mode(regs)) {
  684. debugger(regs);
  685. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  686. }
  687. #ifdef CONFIG_MATH_EMULATION
  688. errcode = do_mathemu(regs);
  689. #else
  690. errcode = Soft_emulate_8xx(regs);
  691. #endif
  692. if (errcode) {
  693. if (errcode > 0)
  694. _exception(SIGFPE, regs, 0, 0);
  695. else if (errcode == -EFAULT)
  696. _exception(SIGSEGV, regs, 0, 0);
  697. else
  698. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  699. } else
  700. emulate_single_step(regs);
  701. }
  702. #endif /* CONFIG_8xx */
  703. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  704. void DebugException(struct pt_regs *regs, unsigned long debug_status)
  705. {
  706. if (debug_status & DBSR_IC) { /* instruction completion */
  707. regs->msr &= ~MSR_DE;
  708. if (user_mode(regs)) {
  709. current->thread.dbcr0 &= ~DBCR0_IC;
  710. } else {
  711. /* Disable instruction completion */
  712. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  713. /* Clear the instruction completion event */
  714. mtspr(SPRN_DBSR, DBSR_IC);
  715. if (debugger_sstep(regs))
  716. return;
  717. }
  718. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  719. }
  720. }
  721. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  722. #if !defined(CONFIG_TAU_INT)
  723. void TAUException(struct pt_regs *regs)
  724. {
  725. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  726. regs->nip, regs->msr, regs->trap, print_tainted());
  727. }
  728. #endif /* CONFIG_INT_TAU */
  729. /*
  730. * FP unavailable trap from kernel - print a message, but let
  731. * the task use FP in the kernel until it returns to user mode.
  732. */
  733. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  734. {
  735. regs->msr |= MSR_FP;
  736. printk(KERN_ERR "floating point used in kernel (task=%p, pc=%lx)\n",
  737. current, regs->nip);
  738. }
  739. void altivec_unavailable_exception(struct pt_regs *regs)
  740. {
  741. static int kernel_altivec_count;
  742. #ifndef CONFIG_ALTIVEC
  743. if (user_mode(regs)) {
  744. /* A user program has executed an altivec instruction,
  745. but this kernel doesn't support altivec. */
  746. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  747. return;
  748. }
  749. #endif
  750. /* The kernel has executed an altivec instruction without
  751. first enabling altivec. Whinge but let it do it. */
  752. if (++kernel_altivec_count < 10)
  753. printk(KERN_ERR "AltiVec used in kernel (task=%p, pc=%lx)\n",
  754. current, regs->nip);
  755. regs->msr |= MSR_VEC;
  756. }
  757. #ifdef CONFIG_ALTIVEC
  758. void altivec_assist_exception(struct pt_regs *regs)
  759. {
  760. int err;
  761. preempt_disable();
  762. if (regs->msr & MSR_VEC)
  763. giveup_altivec(current);
  764. preempt_enable();
  765. if (!user_mode(regs)) {
  766. printk(KERN_ERR "altivec assist exception in kernel mode"
  767. " at %lx\n", regs->nip);
  768. debugger(regs);
  769. die("altivec assist exception", regs, SIGFPE);
  770. return;
  771. }
  772. err = emulate_altivec(regs);
  773. if (err == 0) {
  774. regs->nip += 4; /* skip emulated instruction */
  775. emulate_single_step(regs);
  776. return;
  777. }
  778. if (err == -EFAULT) {
  779. /* got an error reading the instruction */
  780. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  781. } else {
  782. /* didn't recognize the instruction */
  783. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  784. printk(KERN_ERR "unrecognized altivec instruction "
  785. "in %s at %lx\n", current->comm, regs->nip);
  786. current->thread.vscr.u[3] |= 0x10000;
  787. }
  788. }
  789. #endif /* CONFIG_ALTIVEC */
  790. #ifdef CONFIG_E500
  791. void performance_monitor_exception(struct pt_regs *regs)
  792. {
  793. perf_irq(regs);
  794. }
  795. #endif
  796. #ifdef CONFIG_FSL_BOOKE
  797. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  798. unsigned long error_code)
  799. {
  800. /* We treat cache locking instructions from the user
  801. * as priv ops, in the future we could try to do
  802. * something smarter
  803. */
  804. if (error_code & (ESR_DLK|ESR_ILK))
  805. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  806. return;
  807. }
  808. #endif /* CONFIG_FSL_BOOKE */
  809. #ifdef CONFIG_SPE
  810. void SPEFloatingPointException(struct pt_regs *regs)
  811. {
  812. unsigned long spefscr;
  813. int fpexc_mode;
  814. int code = 0;
  815. spefscr = current->thread.spefscr;
  816. fpexc_mode = current->thread.fpexc_mode;
  817. /* Hardware does not neccessarily set sticky
  818. * underflow/overflow/invalid flags */
  819. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  820. code = FPE_FLTOVF;
  821. spefscr |= SPEFSCR_FOVFS;
  822. }
  823. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  824. code = FPE_FLTUND;
  825. spefscr |= SPEFSCR_FUNFS;
  826. }
  827. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  828. code = FPE_FLTDIV;
  829. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  830. code = FPE_FLTINV;
  831. spefscr |= SPEFSCR_FINVS;
  832. }
  833. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  834. code = FPE_FLTRES;
  835. current->thread.spefscr = spefscr;
  836. _exception(SIGFPE, regs, code, regs->nip);
  837. return;
  838. }
  839. #endif
  840. #ifdef CONFIG_BOOKE_WDT
  841. /*
  842. * Default handler for a Watchdog exception,
  843. * spins until a reboot occurs
  844. */
  845. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  846. {
  847. /* Generic WatchdogHandler, implement your own */
  848. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  849. return;
  850. }
  851. void WatchdogException(struct pt_regs *regs)
  852. {
  853. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  854. WatchdogHandler(regs);
  855. }
  856. #endif
  857. void __init trap_init(void)
  858. {
  859. }