traps.c 24 KB

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