traps.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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@samba.org)
  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/delay.h>
  30. #include <linux/kprobes.h>
  31. #include <linux/kexec.h>
  32. #include <linux/backlight.h>
  33. #include <linux/bug.h>
  34. #include <linux/kdebug.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/system.h>
  38. #include <asm/io.h>
  39. #include <asm/machdep.h>
  40. #include <asm/rtas.h>
  41. #include <asm/pmc.h>
  42. #ifdef CONFIG_PPC32
  43. #include <asm/reg.h>
  44. #endif
  45. #ifdef CONFIG_PMAC_BACKLIGHT
  46. #include <asm/backlight.h>
  47. #endif
  48. #ifdef CONFIG_PPC64
  49. #include <asm/firmware.h>
  50. #include <asm/processor.h>
  51. #endif
  52. #include <asm/kexec.h>
  53. #ifdef CONFIG_DEBUGGER
  54. int (*__debugger)(struct pt_regs *regs);
  55. int (*__debugger_ipi)(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. int (*__debugger_fault_handler)(struct pt_regs *regs);
  61. EXPORT_SYMBOL(__debugger);
  62. EXPORT_SYMBOL(__debugger_ipi);
  63. EXPORT_SYMBOL(__debugger_bpt);
  64. EXPORT_SYMBOL(__debugger_sstep);
  65. EXPORT_SYMBOL(__debugger_iabr_match);
  66. EXPORT_SYMBOL(__debugger_dabr_match);
  67. EXPORT_SYMBOL(__debugger_fault_handler);
  68. #endif
  69. /*
  70. * Trap & Exception support
  71. */
  72. #ifdef CONFIG_PMAC_BACKLIGHT
  73. static void pmac_backlight_unblank(void)
  74. {
  75. mutex_lock(&pmac_backlight_mutex);
  76. if (pmac_backlight) {
  77. struct backlight_properties *props;
  78. props = &pmac_backlight->props;
  79. props->brightness = props->max_brightness;
  80. props->power = FB_BLANK_UNBLANK;
  81. backlight_update_status(pmac_backlight);
  82. }
  83. mutex_unlock(&pmac_backlight_mutex);
  84. }
  85. #else
  86. static inline void pmac_backlight_unblank(void) { }
  87. #endif
  88. int die(const char *str, struct pt_regs *regs, long err)
  89. {
  90. static struct {
  91. spinlock_t lock;
  92. u32 lock_owner;
  93. int lock_owner_depth;
  94. } die = {
  95. .lock = __SPIN_LOCK_UNLOCKED(die.lock),
  96. .lock_owner = -1,
  97. .lock_owner_depth = 0
  98. };
  99. static int die_counter;
  100. unsigned long flags;
  101. if (debugger(regs))
  102. return 1;
  103. oops_enter();
  104. if (die.lock_owner != raw_smp_processor_id()) {
  105. console_verbose();
  106. spin_lock_irqsave(&die.lock, flags);
  107. die.lock_owner = smp_processor_id();
  108. die.lock_owner_depth = 0;
  109. bust_spinlocks(1);
  110. if (machine_is(powermac))
  111. pmac_backlight_unblank();
  112. } else {
  113. local_save_flags(flags);
  114. }
  115. if (++die.lock_owner_depth < 3) {
  116. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  117. #ifdef CONFIG_PREEMPT
  118. printk("PREEMPT ");
  119. #endif
  120. #ifdef CONFIG_SMP
  121. printk("SMP NR_CPUS=%d ", NR_CPUS);
  122. #endif
  123. #ifdef CONFIG_DEBUG_PAGEALLOC
  124. printk("DEBUG_PAGEALLOC ");
  125. #endif
  126. #ifdef CONFIG_NUMA
  127. printk("NUMA ");
  128. #endif
  129. printk("%s\n", ppc_md.name ? ppc_md.name : "");
  130. print_modules();
  131. show_regs(regs);
  132. } else {
  133. printk("Recursive die() failure, output suppressed\n");
  134. }
  135. bust_spinlocks(0);
  136. die.lock_owner = -1;
  137. add_taint(TAINT_DIE);
  138. spin_unlock_irqrestore(&die.lock, flags);
  139. if (kexec_should_crash(current) ||
  140. kexec_sr_activated(smp_processor_id()))
  141. crash_kexec(regs);
  142. crash_kexec_secondary(regs);
  143. if (in_interrupt())
  144. panic("Fatal exception in interrupt");
  145. if (panic_on_oops)
  146. panic("Fatal exception");
  147. oops_exit();
  148. do_exit(err);
  149. return 0;
  150. }
  151. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  152. {
  153. siginfo_t info;
  154. const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
  155. "at %08lx nip %08lx lr %08lx code %x\n";
  156. const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
  157. "at %016lx nip %016lx lr %016lx code %x\n";
  158. if (!user_mode(regs)) {
  159. if (die("Exception in kernel mode", regs, signr))
  160. return;
  161. } else if (show_unhandled_signals &&
  162. unhandled_signal(current, signr) &&
  163. printk_ratelimit()) {
  164. printk(regs->msr & MSR_SF ? fmt64 : fmt32,
  165. current->comm, current->pid, signr,
  166. addr, regs->nip, regs->link, code);
  167. }
  168. memset(&info, 0, sizeof(info));
  169. info.si_signo = signr;
  170. info.si_code = code;
  171. info.si_addr = (void __user *) addr;
  172. force_sig_info(signr, &info, current);
  173. /*
  174. * Init gets no signals that it doesn't have a handler for.
  175. * That's all very well, but if it has caused a synchronous
  176. * exception and we ignore the resulting signal, it will just
  177. * generate the same exception over and over again and we get
  178. * nowhere. Better to kill it and let the kernel panic.
  179. */
  180. if (is_init(current)) {
  181. __sighandler_t handler;
  182. spin_lock_irq(&current->sighand->siglock);
  183. handler = current->sighand->action[signr-1].sa.sa_handler;
  184. spin_unlock_irq(&current->sighand->siglock);
  185. if (handler == SIG_DFL) {
  186. /* init has generated a synchronous exception
  187. and it doesn't have a handler for the signal */
  188. printk(KERN_CRIT "init has generated signal %d "
  189. "but has no handler for it\n", signr);
  190. do_exit(signr);
  191. }
  192. }
  193. }
  194. #ifdef CONFIG_PPC64
  195. void system_reset_exception(struct pt_regs *regs)
  196. {
  197. /* See if any machine dependent calls */
  198. if (ppc_md.system_reset_exception) {
  199. if (ppc_md.system_reset_exception(regs))
  200. return;
  201. }
  202. #ifdef CONFIG_KEXEC
  203. cpu_set(smp_processor_id(), cpus_in_sr);
  204. #endif
  205. die("System Reset", regs, SIGABRT);
  206. /*
  207. * Some CPUs when released from the debugger will execute this path.
  208. * These CPUs entered the debugger via a soft-reset. If the CPU was
  209. * hung before entering the debugger it will return to the hung
  210. * state when exiting this function. This causes a problem in
  211. * kdump since the hung CPU(s) will not respond to the IPI sent
  212. * from kdump. To prevent the problem we call crash_kexec_secondary()
  213. * here. If a kdump had not been initiated or we exit the debugger
  214. * with the "exit and recover" command (x) crash_kexec_secondary()
  215. * will return after 5ms and the CPU returns to its previous state.
  216. */
  217. crash_kexec_secondary(regs);
  218. /* Must die if the interrupt is not recoverable */
  219. if (!(regs->msr & MSR_RI))
  220. panic("Unrecoverable System Reset");
  221. /* What should we do here? We could issue a shutdown or hard reset. */
  222. }
  223. #endif
  224. /*
  225. * I/O accesses can cause machine checks on powermacs.
  226. * Check if the NIP corresponds to the address of a sync
  227. * instruction for which there is an entry in the exception
  228. * table.
  229. * Note that the 601 only takes a machine check on TEA
  230. * (transfer error ack) signal assertion, and does not
  231. * set any of the top 16 bits of SRR1.
  232. * -- paulus.
  233. */
  234. static inline int check_io_access(struct pt_regs *regs)
  235. {
  236. #ifdef CONFIG_PPC32
  237. unsigned long msr = regs->msr;
  238. const struct exception_table_entry *entry;
  239. unsigned int *nip = (unsigned int *)regs->nip;
  240. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  241. && (entry = search_exception_tables(regs->nip)) != NULL) {
  242. /*
  243. * Check that it's a sync instruction, or somewhere
  244. * in the twi; isync; nop sequence that inb/inw/inl uses.
  245. * As the address is in the exception table
  246. * we should be able to read the instr there.
  247. * For the debug message, we look at the preceding
  248. * load or store.
  249. */
  250. if (*nip == 0x60000000) /* nop */
  251. nip -= 2;
  252. else if (*nip == 0x4c00012c) /* isync */
  253. --nip;
  254. if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
  255. /* sync or twi */
  256. unsigned int rb;
  257. --nip;
  258. rb = (*nip >> 11) & 0x1f;
  259. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  260. (*nip & 0x100)? "OUT to": "IN from",
  261. regs->gpr[rb] - _IO_BASE, nip);
  262. regs->msr |= MSR_RI;
  263. regs->nip = entry->fixup;
  264. return 1;
  265. }
  266. }
  267. #endif /* CONFIG_PPC32 */
  268. return 0;
  269. }
  270. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  271. /* On 4xx, the reason for the machine check or program exception
  272. is in the ESR. */
  273. #define get_reason(regs) ((regs)->dsisr)
  274. #ifndef CONFIG_FSL_BOOKE
  275. #define get_mc_reason(regs) ((regs)->dsisr)
  276. #else
  277. #define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
  278. #endif
  279. #define REASON_FP ESR_FP
  280. #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
  281. #define REASON_PRIVILEGED ESR_PPR
  282. #define REASON_TRAP ESR_PTR
  283. /* single-step stuff */
  284. #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
  285. #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
  286. #else
  287. /* On non-4xx, the reason for the machine check or program
  288. exception is in the MSR. */
  289. #define get_reason(regs) ((regs)->msr)
  290. #define get_mc_reason(regs) ((regs)->msr)
  291. #define REASON_FP 0x100000
  292. #define REASON_ILLEGAL 0x80000
  293. #define REASON_PRIVILEGED 0x40000
  294. #define REASON_TRAP 0x20000
  295. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  296. #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
  297. #endif
  298. static int generic_machine_check_exception(struct pt_regs *regs)
  299. {
  300. unsigned long reason = get_mc_reason(regs);
  301. #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
  302. if (reason & ESR_IMCP) {
  303. printk("Instruction");
  304. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  305. } else
  306. printk("Data");
  307. printk(" machine check in kernel mode.\n");
  308. #elif defined(CONFIG_440A)
  309. printk("Machine check in kernel mode.\n");
  310. if (reason & ESR_IMCP){
  311. printk("Instruction Synchronous Machine Check exception\n");
  312. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  313. }
  314. else {
  315. u32 mcsr = mfspr(SPRN_MCSR);
  316. if (mcsr & MCSR_IB)
  317. printk("Instruction Read PLB Error\n");
  318. if (mcsr & MCSR_DRB)
  319. printk("Data Read PLB Error\n");
  320. if (mcsr & MCSR_DWB)
  321. printk("Data Write PLB Error\n");
  322. if (mcsr & MCSR_TLBP)
  323. printk("TLB Parity Error\n");
  324. if (mcsr & MCSR_ICP){
  325. flush_instruction_cache();
  326. printk("I-Cache Parity Error\n");
  327. }
  328. if (mcsr & MCSR_DCSP)
  329. printk("D-Cache Search Parity Error\n");
  330. if (mcsr & MCSR_DCFP)
  331. printk("D-Cache Flush Parity Error\n");
  332. if (mcsr & MCSR_IMPE)
  333. printk("Machine Check exception is imprecise\n");
  334. /* Clear MCSR */
  335. mtspr(SPRN_MCSR, mcsr);
  336. }
  337. #elif defined (CONFIG_E500)
  338. printk("Machine check in kernel mode.\n");
  339. printk("Caused by (from MCSR=%lx): ", reason);
  340. if (reason & MCSR_MCP)
  341. printk("Machine Check Signal\n");
  342. if (reason & MCSR_ICPERR)
  343. printk("Instruction Cache Parity Error\n");
  344. if (reason & MCSR_DCP_PERR)
  345. printk("Data Cache Push Parity Error\n");
  346. if (reason & MCSR_DCPERR)
  347. printk("Data Cache Parity Error\n");
  348. if (reason & MCSR_BUS_IAERR)
  349. printk("Bus - Instruction Address Error\n");
  350. if (reason & MCSR_BUS_RAERR)
  351. printk("Bus - Read Address Error\n");
  352. if (reason & MCSR_BUS_WAERR)
  353. printk("Bus - Write Address Error\n");
  354. if (reason & MCSR_BUS_IBERR)
  355. printk("Bus - Instruction Data Error\n");
  356. if (reason & MCSR_BUS_RBERR)
  357. printk("Bus - Read Data Bus Error\n");
  358. if (reason & MCSR_BUS_WBERR)
  359. printk("Bus - Read Data Bus Error\n");
  360. if (reason & MCSR_BUS_IPERR)
  361. printk("Bus - Instruction Parity Error\n");
  362. if (reason & MCSR_BUS_RPERR)
  363. printk("Bus - Read Parity Error\n");
  364. #elif defined (CONFIG_E200)
  365. printk("Machine check in kernel mode.\n");
  366. printk("Caused by (from MCSR=%lx): ", reason);
  367. if (reason & MCSR_MCP)
  368. printk("Machine Check Signal\n");
  369. if (reason & MCSR_CP_PERR)
  370. printk("Cache Push Parity Error\n");
  371. if (reason & MCSR_CPERR)
  372. printk("Cache Parity Error\n");
  373. if (reason & MCSR_EXCP_ERR)
  374. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  375. if (reason & MCSR_BUS_IRERR)
  376. printk("Bus - Read Bus Error on instruction fetch\n");
  377. if (reason & MCSR_BUS_DRERR)
  378. printk("Bus - Read Bus Error on data load\n");
  379. if (reason & MCSR_BUS_WRERR)
  380. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  381. #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
  382. printk("Machine check in kernel mode.\n");
  383. printk("Caused by (from SRR1=%lx): ", reason);
  384. switch (reason & 0x601F0000) {
  385. case 0x80000:
  386. printk("Machine check signal\n");
  387. break;
  388. case 0: /* for 601 */
  389. case 0x40000:
  390. case 0x140000: /* 7450 MSS error and TEA */
  391. printk("Transfer error ack signal\n");
  392. break;
  393. case 0x20000:
  394. printk("Data parity error signal\n");
  395. break;
  396. case 0x10000:
  397. printk("Address parity error signal\n");
  398. break;
  399. case 0x20000000:
  400. printk("L1 Data Cache error\n");
  401. break;
  402. case 0x40000000:
  403. printk("L1 Instruction Cache error\n");
  404. break;
  405. case 0x00100000:
  406. printk("L2 data cache parity error\n");
  407. break;
  408. default:
  409. printk("Unknown values in msr\n");
  410. }
  411. #endif /* CONFIG_4xx */
  412. return 0;
  413. }
  414. void machine_check_exception(struct pt_regs *regs)
  415. {
  416. int recover = 0;
  417. /* See if any machine dependent calls */
  418. if (ppc_md.machine_check_exception)
  419. recover = ppc_md.machine_check_exception(regs);
  420. else
  421. recover = generic_machine_check_exception(regs);
  422. if (recover)
  423. return;
  424. if (user_mode(regs)) {
  425. regs->msr |= MSR_RI;
  426. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  427. return;
  428. }
  429. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  430. /* the qspan pci read routines can cause machine checks -- Cort */
  431. bad_page_fault(regs, regs->dar, SIGBUS);
  432. return;
  433. #endif
  434. if (debugger_fault_handler(regs)) {
  435. regs->msr |= MSR_RI;
  436. return;
  437. }
  438. if (check_io_access(regs))
  439. return;
  440. if (debugger_fault_handler(regs))
  441. return;
  442. die("Machine check", regs, SIGBUS);
  443. /* Must die if the interrupt is not recoverable */
  444. if (!(regs->msr & MSR_RI))
  445. panic("Unrecoverable Machine check");
  446. }
  447. void SMIException(struct pt_regs *regs)
  448. {
  449. die("System Management Interrupt", regs, SIGABRT);
  450. }
  451. void unknown_exception(struct pt_regs *regs)
  452. {
  453. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  454. regs->nip, regs->msr, regs->trap);
  455. _exception(SIGTRAP, regs, 0, 0);
  456. }
  457. void instruction_breakpoint_exception(struct pt_regs *regs)
  458. {
  459. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  460. 5, SIGTRAP) == NOTIFY_STOP)
  461. return;
  462. if (debugger_iabr_match(regs))
  463. return;
  464. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  465. }
  466. void RunModeException(struct pt_regs *regs)
  467. {
  468. _exception(SIGTRAP, regs, 0, 0);
  469. }
  470. void __kprobes single_step_exception(struct pt_regs *regs)
  471. {
  472. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  473. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  474. 5, SIGTRAP) == NOTIFY_STOP)
  475. return;
  476. if (debugger_sstep(regs))
  477. return;
  478. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  479. }
  480. /*
  481. * After we have successfully emulated an instruction, we have to
  482. * check if the instruction was being single-stepped, and if so,
  483. * pretend we got a single-step exception. This was pointed out
  484. * by Kumar Gala. -- paulus
  485. */
  486. static void emulate_single_step(struct pt_regs *regs)
  487. {
  488. if (single_stepping(regs)) {
  489. clear_single_step(regs);
  490. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  491. }
  492. }
  493. static inline int __parse_fpscr(unsigned long fpscr)
  494. {
  495. int ret = 0;
  496. /* Invalid operation */
  497. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  498. ret = FPE_FLTINV;
  499. /* Overflow */
  500. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  501. ret = FPE_FLTOVF;
  502. /* Underflow */
  503. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  504. ret = FPE_FLTUND;
  505. /* Divide by zero */
  506. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  507. ret = FPE_FLTDIV;
  508. /* Inexact result */
  509. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  510. ret = FPE_FLTRES;
  511. return ret;
  512. }
  513. static void parse_fpe(struct pt_regs *regs)
  514. {
  515. int code = 0;
  516. flush_fp_to_thread(current);
  517. code = __parse_fpscr(current->thread.fpscr.val);
  518. _exception(SIGFPE, regs, code, regs->nip);
  519. }
  520. /*
  521. * Illegal instruction emulation support. Originally written to
  522. * provide the PVR to user applications using the mfspr rd, PVR.
  523. * Return non-zero if we can't emulate, or -EFAULT if the associated
  524. * memory access caused an access fault. Return zero on success.
  525. *
  526. * There are a couple of ways to do this, either "decode" the instruction
  527. * or directly match lots of bits. In this case, matching lots of
  528. * bits is faster and easier.
  529. *
  530. */
  531. #define INST_MFSPR_PVR 0x7c1f42a6
  532. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  533. #define INST_DCBA 0x7c0005ec
  534. #define INST_DCBA_MASK 0xfc0007fe
  535. #define INST_MCRXR 0x7c000400
  536. #define INST_MCRXR_MASK 0xfc0007fe
  537. #define INST_STRING 0x7c00042a
  538. #define INST_STRING_MASK 0xfc0007fe
  539. #define INST_STRING_GEN_MASK 0xfc00067e
  540. #define INST_LSWI 0x7c0004aa
  541. #define INST_LSWX 0x7c00042a
  542. #define INST_STSWI 0x7c0005aa
  543. #define INST_STSWX 0x7c00052a
  544. #define INST_POPCNTB 0x7c0000f4
  545. #define INST_POPCNTB_MASK 0xfc0007fe
  546. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  547. {
  548. u8 rT = (instword >> 21) & 0x1f;
  549. u8 rA = (instword >> 16) & 0x1f;
  550. u8 NB_RB = (instword >> 11) & 0x1f;
  551. u32 num_bytes;
  552. unsigned long EA;
  553. int pos = 0;
  554. /* Early out if we are an invalid form of lswx */
  555. if ((instword & INST_STRING_MASK) == INST_LSWX)
  556. if ((rT == rA) || (rT == NB_RB))
  557. return -EINVAL;
  558. EA = (rA == 0) ? 0 : regs->gpr[rA];
  559. switch (instword & INST_STRING_MASK) {
  560. case INST_LSWX:
  561. case INST_STSWX:
  562. EA += NB_RB;
  563. num_bytes = regs->xer & 0x7f;
  564. break;
  565. case INST_LSWI:
  566. case INST_STSWI:
  567. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  568. break;
  569. default:
  570. return -EINVAL;
  571. }
  572. while (num_bytes != 0)
  573. {
  574. u8 val;
  575. u32 shift = 8 * (3 - (pos & 0x3));
  576. switch ((instword & INST_STRING_MASK)) {
  577. case INST_LSWX:
  578. case INST_LSWI:
  579. if (get_user(val, (u8 __user *)EA))
  580. return -EFAULT;
  581. /* first time updating this reg,
  582. * zero it out */
  583. if (pos == 0)
  584. regs->gpr[rT] = 0;
  585. regs->gpr[rT] |= val << shift;
  586. break;
  587. case INST_STSWI:
  588. case INST_STSWX:
  589. val = regs->gpr[rT] >> shift;
  590. if (put_user(val, (u8 __user *)EA))
  591. return -EFAULT;
  592. break;
  593. }
  594. /* move EA to next address */
  595. EA += 1;
  596. num_bytes--;
  597. /* manage our position within the register */
  598. if (++pos == 4) {
  599. pos = 0;
  600. if (++rT == 32)
  601. rT = 0;
  602. }
  603. }
  604. return 0;
  605. }
  606. static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
  607. {
  608. u32 ra,rs;
  609. unsigned long tmp;
  610. ra = (instword >> 16) & 0x1f;
  611. rs = (instword >> 21) & 0x1f;
  612. tmp = regs->gpr[rs];
  613. tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
  614. tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
  615. tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
  616. regs->gpr[ra] = tmp;
  617. return 0;
  618. }
  619. static int emulate_instruction(struct pt_regs *regs)
  620. {
  621. u32 instword;
  622. u32 rd;
  623. if (!user_mode(regs) || (regs->msr & MSR_LE))
  624. return -EINVAL;
  625. CHECK_FULL_REGS(regs);
  626. if (get_user(instword, (u32 __user *)(regs->nip)))
  627. return -EFAULT;
  628. /* Emulate the mfspr rD, PVR. */
  629. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  630. rd = (instword >> 21) & 0x1f;
  631. regs->gpr[rd] = mfspr(SPRN_PVR);
  632. return 0;
  633. }
  634. /* Emulating the dcba insn is just a no-op. */
  635. if ((instword & INST_DCBA_MASK) == INST_DCBA)
  636. return 0;
  637. /* Emulate the mcrxr insn. */
  638. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  639. int shift = (instword >> 21) & 0x1c;
  640. unsigned long msk = 0xf0000000UL >> shift;
  641. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  642. regs->xer &= ~0xf0000000UL;
  643. return 0;
  644. }
  645. /* Emulate load/store string insn. */
  646. if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
  647. return emulate_string_inst(regs, instword);
  648. /* Emulate the popcntb (Population Count Bytes) instruction. */
  649. if ((instword & INST_POPCNTB_MASK) == INST_POPCNTB) {
  650. return emulate_popcntb_inst(regs, instword);
  651. }
  652. return -EINVAL;
  653. }
  654. int is_valid_bugaddr(unsigned long addr)
  655. {
  656. return is_kernel_addr(addr);
  657. }
  658. void __kprobes program_check_exception(struct pt_regs *regs)
  659. {
  660. unsigned int reason = get_reason(regs);
  661. extern int do_mathemu(struct pt_regs *regs);
  662. /* We can now get here via a FP Unavailable exception if the core
  663. * has no FPU, in that case the reason flags will be 0 */
  664. if (reason & REASON_FP) {
  665. /* IEEE FP exception */
  666. parse_fpe(regs);
  667. return;
  668. }
  669. if (reason & REASON_TRAP) {
  670. /* trap exception */
  671. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  672. == NOTIFY_STOP)
  673. return;
  674. if (debugger_bpt(regs))
  675. return;
  676. if (!(regs->msr & MSR_PR) && /* not user-mode */
  677. report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
  678. regs->nip += 4;
  679. return;
  680. }
  681. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  682. return;
  683. }
  684. local_irq_enable();
  685. #ifdef CONFIG_MATH_EMULATION
  686. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  687. * but there seems to be a hardware bug on the 405GP (RevD)
  688. * that means ESR is sometimes set incorrectly - either to
  689. * ESR_DST (!?) or 0. In the process of chasing this with the
  690. * hardware people - not sure if it can happen on any illegal
  691. * instruction or only on FP instructions, whether there is a
  692. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  693. switch (do_mathemu(regs)) {
  694. case 0:
  695. emulate_single_step(regs);
  696. return;
  697. case 1: {
  698. int code = 0;
  699. code = __parse_fpscr(current->thread.fpscr.val);
  700. _exception(SIGFPE, regs, code, regs->nip);
  701. return;
  702. }
  703. case -EFAULT:
  704. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  705. return;
  706. }
  707. /* fall through on any other errors */
  708. #endif /* CONFIG_MATH_EMULATION */
  709. /* Try to emulate it if we should. */
  710. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  711. switch (emulate_instruction(regs)) {
  712. case 0:
  713. regs->nip += 4;
  714. emulate_single_step(regs);
  715. return;
  716. case -EFAULT:
  717. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  718. return;
  719. }
  720. }
  721. if (reason & REASON_PRIVILEGED)
  722. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  723. else
  724. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  725. }
  726. void alignment_exception(struct pt_regs *regs)
  727. {
  728. int sig, code, fixed = 0;
  729. /* we don't implement logging of alignment exceptions */
  730. if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
  731. fixed = fix_alignment(regs);
  732. if (fixed == 1) {
  733. regs->nip += 4; /* skip over emulated instruction */
  734. emulate_single_step(regs);
  735. return;
  736. }
  737. /* Operand address was bad */
  738. if (fixed == -EFAULT) {
  739. sig = SIGSEGV;
  740. code = SEGV_ACCERR;
  741. } else {
  742. sig = SIGBUS;
  743. code = BUS_ADRALN;
  744. }
  745. if (user_mode(regs))
  746. _exception(sig, regs, code, regs->dar);
  747. else
  748. bad_page_fault(regs, regs->dar, sig);
  749. }
  750. void StackOverflow(struct pt_regs *regs)
  751. {
  752. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  753. current, regs->gpr[1]);
  754. debugger(regs);
  755. show_regs(regs);
  756. panic("kernel stack overflow");
  757. }
  758. void nonrecoverable_exception(struct pt_regs *regs)
  759. {
  760. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  761. regs->nip, regs->msr);
  762. debugger(regs);
  763. die("nonrecoverable exception", regs, SIGKILL);
  764. }
  765. void trace_syscall(struct pt_regs *regs)
  766. {
  767. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  768. current, current->pid, regs->nip, regs->link, regs->gpr[0],
  769. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  770. }
  771. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  772. {
  773. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  774. "%lx at %lx\n", regs->trap, regs->nip);
  775. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  776. }
  777. void altivec_unavailable_exception(struct pt_regs *regs)
  778. {
  779. if (user_mode(regs)) {
  780. /* A user program has executed an altivec instruction,
  781. but this kernel doesn't support altivec. */
  782. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  783. return;
  784. }
  785. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  786. "%lx at %lx\n", regs->trap, regs->nip);
  787. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  788. }
  789. void performance_monitor_exception(struct pt_regs *regs)
  790. {
  791. perf_irq(regs);
  792. }
  793. #ifdef CONFIG_8xx
  794. void SoftwareEmulation(struct pt_regs *regs)
  795. {
  796. extern int do_mathemu(struct pt_regs *);
  797. extern int Soft_emulate_8xx(struct pt_regs *);
  798. #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
  799. int errcode;
  800. #endif
  801. CHECK_FULL_REGS(regs);
  802. if (!user_mode(regs)) {
  803. debugger(regs);
  804. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  805. }
  806. #ifdef CONFIG_MATH_EMULATION
  807. errcode = do_mathemu(regs);
  808. switch (errcode) {
  809. case 0:
  810. emulate_single_step(regs);
  811. return;
  812. case 1: {
  813. int code = 0;
  814. code = __parse_fpscr(current->thread.fpscr.val);
  815. _exception(SIGFPE, regs, code, regs->nip);
  816. return;
  817. }
  818. case -EFAULT:
  819. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  820. return;
  821. default:
  822. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  823. return;
  824. }
  825. #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
  826. errcode = Soft_emulate_8xx(regs);
  827. switch (errcode) {
  828. case 0:
  829. emulate_single_step(regs);
  830. return;
  831. case 1:
  832. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  833. return;
  834. case -EFAULT:
  835. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  836. return;
  837. }
  838. #else
  839. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  840. #endif
  841. }
  842. #endif /* CONFIG_8xx */
  843. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  844. void DebugException(struct pt_regs *regs, unsigned long debug_status)
  845. {
  846. if (debug_status & DBSR_IC) { /* instruction completion */
  847. regs->msr &= ~MSR_DE;
  848. if (user_mode(regs)) {
  849. current->thread.dbcr0 &= ~DBCR0_IC;
  850. } else {
  851. /* Disable instruction completion */
  852. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  853. /* Clear the instruction completion event */
  854. mtspr(SPRN_DBSR, DBSR_IC);
  855. if (debugger_sstep(regs))
  856. return;
  857. }
  858. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  859. }
  860. }
  861. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  862. #if !defined(CONFIG_TAU_INT)
  863. void TAUException(struct pt_regs *regs)
  864. {
  865. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  866. regs->nip, regs->msr, regs->trap, print_tainted());
  867. }
  868. #endif /* CONFIG_INT_TAU */
  869. #ifdef CONFIG_ALTIVEC
  870. void altivec_assist_exception(struct pt_regs *regs)
  871. {
  872. int err;
  873. if (!user_mode(regs)) {
  874. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  875. " at %lx\n", regs->nip);
  876. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  877. }
  878. flush_altivec_to_thread(current);
  879. err = emulate_altivec(regs);
  880. if (err == 0) {
  881. regs->nip += 4; /* skip emulated instruction */
  882. emulate_single_step(regs);
  883. return;
  884. }
  885. if (err == -EFAULT) {
  886. /* got an error reading the instruction */
  887. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  888. } else {
  889. /* didn't recognize the instruction */
  890. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  891. if (printk_ratelimit())
  892. printk(KERN_ERR "Unrecognized altivec instruction "
  893. "in %s at %lx\n", current->comm, regs->nip);
  894. current->thread.vscr.u[3] |= 0x10000;
  895. }
  896. }
  897. #endif /* CONFIG_ALTIVEC */
  898. #ifdef CONFIG_FSL_BOOKE
  899. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  900. unsigned long error_code)
  901. {
  902. /* We treat cache locking instructions from the user
  903. * as priv ops, in the future we could try to do
  904. * something smarter
  905. */
  906. if (error_code & (ESR_DLK|ESR_ILK))
  907. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  908. return;
  909. }
  910. #endif /* CONFIG_FSL_BOOKE */
  911. #ifdef CONFIG_SPE
  912. void SPEFloatingPointException(struct pt_regs *regs)
  913. {
  914. unsigned long spefscr;
  915. int fpexc_mode;
  916. int code = 0;
  917. spefscr = current->thread.spefscr;
  918. fpexc_mode = current->thread.fpexc_mode;
  919. /* Hardware does not neccessarily set sticky
  920. * underflow/overflow/invalid flags */
  921. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  922. code = FPE_FLTOVF;
  923. spefscr |= SPEFSCR_FOVFS;
  924. }
  925. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  926. code = FPE_FLTUND;
  927. spefscr |= SPEFSCR_FUNFS;
  928. }
  929. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  930. code = FPE_FLTDIV;
  931. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  932. code = FPE_FLTINV;
  933. spefscr |= SPEFSCR_FINVS;
  934. }
  935. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  936. code = FPE_FLTRES;
  937. current->thread.spefscr = spefscr;
  938. _exception(SIGFPE, regs, code, regs->nip);
  939. return;
  940. }
  941. #endif
  942. /*
  943. * We enter here if we get an unrecoverable exception, that is, one
  944. * that happened at a point where the RI (recoverable interrupt) bit
  945. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  946. * we therefore lost state by taking this exception.
  947. */
  948. void unrecoverable_exception(struct pt_regs *regs)
  949. {
  950. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  951. regs->trap, regs->nip);
  952. die("Unrecoverable exception", regs, SIGABRT);
  953. }
  954. #ifdef CONFIG_BOOKE_WDT
  955. /*
  956. * Default handler for a Watchdog exception,
  957. * spins until a reboot occurs
  958. */
  959. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  960. {
  961. /* Generic WatchdogHandler, implement your own */
  962. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  963. return;
  964. }
  965. void WatchdogException(struct pt_regs *regs)
  966. {
  967. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  968. WatchdogHandler(regs);
  969. }
  970. #endif
  971. /*
  972. * We enter here if we discover during exception entry that we are
  973. * running in supervisor mode with a userspace value in the stack pointer.
  974. */
  975. void kernel_bad_stack(struct pt_regs *regs)
  976. {
  977. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  978. regs->gpr[1], regs->nip);
  979. die("Bad kernel stack pointer", regs, SIGABRT);
  980. }
  981. void __init trap_init(void)
  982. {
  983. }