traps.c 25 KB

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