traps.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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. #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
  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_global_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. #if defined(CONFIG_4xx)
  299. int machine_check_4xx(struct pt_regs *regs)
  300. {
  301. unsigned long reason = get_mc_reason(regs);
  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. return 0;
  309. }
  310. int machine_check_440A(struct pt_regs *regs)
  311. {
  312. unsigned long reason = get_mc_reason(regs);
  313. printk("Machine check in kernel mode.\n");
  314. if (reason & ESR_IMCP){
  315. printk("Instruction Synchronous Machine Check exception\n");
  316. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  317. }
  318. else {
  319. u32 mcsr = mfspr(SPRN_MCSR);
  320. if (mcsr & MCSR_IB)
  321. printk("Instruction Read PLB Error\n");
  322. if (mcsr & MCSR_DRB)
  323. printk("Data Read PLB Error\n");
  324. if (mcsr & MCSR_DWB)
  325. printk("Data Write PLB Error\n");
  326. if (mcsr & MCSR_TLBP)
  327. printk("TLB Parity Error\n");
  328. if (mcsr & MCSR_ICP){
  329. flush_instruction_cache();
  330. printk("I-Cache Parity Error\n");
  331. }
  332. if (mcsr & MCSR_DCSP)
  333. printk("D-Cache Search Parity Error\n");
  334. if (mcsr & MCSR_DCFP)
  335. printk("D-Cache Flush Parity Error\n");
  336. if (mcsr & MCSR_IMPE)
  337. printk("Machine Check exception is imprecise\n");
  338. /* Clear MCSR */
  339. mtspr(SPRN_MCSR, mcsr);
  340. }
  341. return 0;
  342. }
  343. #elif defined(CONFIG_E500)
  344. int machine_check_e500(struct pt_regs *regs)
  345. {
  346. unsigned long reason = get_mc_reason(regs);
  347. printk("Machine check in kernel mode.\n");
  348. printk("Caused by (from MCSR=%lx): ", reason);
  349. if (reason & MCSR_MCP)
  350. printk("Machine Check Signal\n");
  351. if (reason & MCSR_ICPERR)
  352. printk("Instruction Cache Parity Error\n");
  353. if (reason & MCSR_DCP_PERR)
  354. printk("Data Cache Push Parity Error\n");
  355. if (reason & MCSR_DCPERR)
  356. printk("Data Cache Parity Error\n");
  357. if (reason & MCSR_BUS_IAERR)
  358. printk("Bus - Instruction Address Error\n");
  359. if (reason & MCSR_BUS_RAERR)
  360. printk("Bus - Read Address Error\n");
  361. if (reason & MCSR_BUS_WAERR)
  362. printk("Bus - Write Address Error\n");
  363. if (reason & MCSR_BUS_IBERR)
  364. printk("Bus - Instruction Data Error\n");
  365. if (reason & MCSR_BUS_RBERR)
  366. printk("Bus - Read Data Bus Error\n");
  367. if (reason & MCSR_BUS_WBERR)
  368. printk("Bus - Read Data Bus Error\n");
  369. if (reason & MCSR_BUS_IPERR)
  370. printk("Bus - Instruction Parity Error\n");
  371. if (reason & MCSR_BUS_RPERR)
  372. printk("Bus - Read Parity Error\n");
  373. return 0;
  374. }
  375. #elif defined(CONFIG_E200)
  376. int machine_check_e200(struct pt_regs *regs)
  377. {
  378. unsigned long reason = get_mc_reason(regs);
  379. printk("Machine check in kernel mode.\n");
  380. printk("Caused by (from MCSR=%lx): ", reason);
  381. if (reason & MCSR_MCP)
  382. printk("Machine Check Signal\n");
  383. if (reason & MCSR_CP_PERR)
  384. printk("Cache Push Parity Error\n");
  385. if (reason & MCSR_CPERR)
  386. printk("Cache Parity Error\n");
  387. if (reason & MCSR_EXCP_ERR)
  388. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  389. if (reason & MCSR_BUS_IRERR)
  390. printk("Bus - Read Bus Error on instruction fetch\n");
  391. if (reason & MCSR_BUS_DRERR)
  392. printk("Bus - Read Bus Error on data load\n");
  393. if (reason & MCSR_BUS_WRERR)
  394. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  395. return 0;
  396. }
  397. #else
  398. int machine_check_generic(struct pt_regs *regs)
  399. {
  400. unsigned long reason = get_mc_reason(regs);
  401. printk("Machine check in kernel mode.\n");
  402. printk("Caused by (from SRR1=%lx): ", reason);
  403. switch (reason & 0x601F0000) {
  404. case 0x80000:
  405. printk("Machine check signal\n");
  406. break;
  407. case 0: /* for 601 */
  408. case 0x40000:
  409. case 0x140000: /* 7450 MSS error and TEA */
  410. printk("Transfer error ack signal\n");
  411. break;
  412. case 0x20000:
  413. printk("Data parity error signal\n");
  414. break;
  415. case 0x10000:
  416. printk("Address parity error signal\n");
  417. break;
  418. case 0x20000000:
  419. printk("L1 Data Cache error\n");
  420. break;
  421. case 0x40000000:
  422. printk("L1 Instruction Cache error\n");
  423. break;
  424. case 0x00100000:
  425. printk("L2 data cache parity error\n");
  426. break;
  427. default:
  428. printk("Unknown values in msr\n");
  429. }
  430. return 0;
  431. }
  432. #endif /* everything else */
  433. void machine_check_exception(struct pt_regs *regs)
  434. {
  435. int recover = 0;
  436. /* See if any machine dependent calls. In theory, we would want
  437. * to call the CPU first, and call the ppc_md. one if the CPU
  438. * one returns a positive number. However there is existing code
  439. * that assumes the board gets a first chance, so let's keep it
  440. * that way for now and fix things later. --BenH.
  441. */
  442. if (ppc_md.machine_check_exception)
  443. recover = ppc_md.machine_check_exception(regs);
  444. else if (cur_cpu_spec->machine_check)
  445. recover = cur_cpu_spec->machine_check(regs);
  446. if (recover > 0)
  447. return;
  448. if (user_mode(regs)) {
  449. regs->msr |= MSR_RI;
  450. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  451. return;
  452. }
  453. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  454. /* the qspan pci read routines can cause machine checks -- Cort
  455. *
  456. * yuck !!! that totally needs to go away ! There are better ways
  457. * to deal with that than having a wart in the mcheck handler.
  458. * -- BenH
  459. */
  460. bad_page_fault(regs, regs->dar, SIGBUS);
  461. return;
  462. #endif
  463. if (debugger_fault_handler(regs)) {
  464. regs->msr |= MSR_RI;
  465. return;
  466. }
  467. if (check_io_access(regs))
  468. return;
  469. if (debugger_fault_handler(regs))
  470. return;
  471. die("Machine check", regs, SIGBUS);
  472. /* Must die if the interrupt is not recoverable */
  473. if (!(regs->msr & MSR_RI))
  474. panic("Unrecoverable Machine check");
  475. }
  476. void SMIException(struct pt_regs *regs)
  477. {
  478. die("System Management Interrupt", regs, SIGABRT);
  479. }
  480. void unknown_exception(struct pt_regs *regs)
  481. {
  482. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  483. regs->nip, regs->msr, regs->trap);
  484. _exception(SIGTRAP, regs, 0, 0);
  485. }
  486. void instruction_breakpoint_exception(struct pt_regs *regs)
  487. {
  488. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  489. 5, SIGTRAP) == NOTIFY_STOP)
  490. return;
  491. if (debugger_iabr_match(regs))
  492. return;
  493. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  494. }
  495. void RunModeException(struct pt_regs *regs)
  496. {
  497. _exception(SIGTRAP, regs, 0, 0);
  498. }
  499. void __kprobes single_step_exception(struct pt_regs *regs)
  500. {
  501. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  502. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  503. 5, SIGTRAP) == NOTIFY_STOP)
  504. return;
  505. if (debugger_sstep(regs))
  506. return;
  507. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  508. }
  509. /*
  510. * After we have successfully emulated an instruction, we have to
  511. * check if the instruction was being single-stepped, and if so,
  512. * pretend we got a single-step exception. This was pointed out
  513. * by Kumar Gala. -- paulus
  514. */
  515. static void emulate_single_step(struct pt_regs *regs)
  516. {
  517. if (single_stepping(regs)) {
  518. clear_single_step(regs);
  519. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  520. }
  521. }
  522. static inline int __parse_fpscr(unsigned long fpscr)
  523. {
  524. int ret = 0;
  525. /* Invalid operation */
  526. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  527. ret = FPE_FLTINV;
  528. /* Overflow */
  529. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  530. ret = FPE_FLTOVF;
  531. /* Underflow */
  532. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  533. ret = FPE_FLTUND;
  534. /* Divide by zero */
  535. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  536. ret = FPE_FLTDIV;
  537. /* Inexact result */
  538. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  539. ret = FPE_FLTRES;
  540. return ret;
  541. }
  542. static void parse_fpe(struct pt_regs *regs)
  543. {
  544. int code = 0;
  545. flush_fp_to_thread(current);
  546. code = __parse_fpscr(current->thread.fpscr.val);
  547. _exception(SIGFPE, regs, code, regs->nip);
  548. }
  549. /*
  550. * Illegal instruction emulation support. Originally written to
  551. * provide the PVR to user applications using the mfspr rd, PVR.
  552. * Return non-zero if we can't emulate, or -EFAULT if the associated
  553. * memory access caused an access fault. Return zero on success.
  554. *
  555. * There are a couple of ways to do this, either "decode" the instruction
  556. * or directly match lots of bits. In this case, matching lots of
  557. * bits is faster and easier.
  558. *
  559. */
  560. #define INST_MFSPR_PVR 0x7c1f42a6
  561. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  562. #define INST_DCBA 0x7c0005ec
  563. #define INST_DCBA_MASK 0xfc0007fe
  564. #define INST_MCRXR 0x7c000400
  565. #define INST_MCRXR_MASK 0xfc0007fe
  566. #define INST_STRING 0x7c00042a
  567. #define INST_STRING_MASK 0xfc0007fe
  568. #define INST_STRING_GEN_MASK 0xfc00067e
  569. #define INST_LSWI 0x7c0004aa
  570. #define INST_LSWX 0x7c00042a
  571. #define INST_STSWI 0x7c0005aa
  572. #define INST_STSWX 0x7c00052a
  573. #define INST_POPCNTB 0x7c0000f4
  574. #define INST_POPCNTB_MASK 0xfc0007fe
  575. #define INST_ISEL 0x7c00001e
  576. #define INST_ISEL_MASK 0xfc00003e
  577. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  578. {
  579. u8 rT = (instword >> 21) & 0x1f;
  580. u8 rA = (instword >> 16) & 0x1f;
  581. u8 NB_RB = (instword >> 11) & 0x1f;
  582. u32 num_bytes;
  583. unsigned long EA;
  584. int pos = 0;
  585. /* Early out if we are an invalid form of lswx */
  586. if ((instword & INST_STRING_MASK) == INST_LSWX)
  587. if ((rT == rA) || (rT == NB_RB))
  588. return -EINVAL;
  589. EA = (rA == 0) ? 0 : regs->gpr[rA];
  590. switch (instword & INST_STRING_MASK) {
  591. case INST_LSWX:
  592. case INST_STSWX:
  593. EA += NB_RB;
  594. num_bytes = regs->xer & 0x7f;
  595. break;
  596. case INST_LSWI:
  597. case INST_STSWI:
  598. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  599. break;
  600. default:
  601. return -EINVAL;
  602. }
  603. while (num_bytes != 0)
  604. {
  605. u8 val;
  606. u32 shift = 8 * (3 - (pos & 0x3));
  607. switch ((instword & INST_STRING_MASK)) {
  608. case INST_LSWX:
  609. case INST_LSWI:
  610. if (get_user(val, (u8 __user *)EA))
  611. return -EFAULT;
  612. /* first time updating this reg,
  613. * zero it out */
  614. if (pos == 0)
  615. regs->gpr[rT] = 0;
  616. regs->gpr[rT] |= val << shift;
  617. break;
  618. case INST_STSWI:
  619. case INST_STSWX:
  620. val = regs->gpr[rT] >> shift;
  621. if (put_user(val, (u8 __user *)EA))
  622. return -EFAULT;
  623. break;
  624. }
  625. /* move EA to next address */
  626. EA += 1;
  627. num_bytes--;
  628. /* manage our position within the register */
  629. if (++pos == 4) {
  630. pos = 0;
  631. if (++rT == 32)
  632. rT = 0;
  633. }
  634. }
  635. return 0;
  636. }
  637. static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
  638. {
  639. u32 ra,rs;
  640. unsigned long tmp;
  641. ra = (instword >> 16) & 0x1f;
  642. rs = (instword >> 21) & 0x1f;
  643. tmp = regs->gpr[rs];
  644. tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
  645. tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
  646. tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
  647. regs->gpr[ra] = tmp;
  648. return 0;
  649. }
  650. static int emulate_isel(struct pt_regs *regs, u32 instword)
  651. {
  652. u8 rT = (instword >> 21) & 0x1f;
  653. u8 rA = (instword >> 16) & 0x1f;
  654. u8 rB = (instword >> 11) & 0x1f;
  655. u8 BC = (instword >> 6) & 0x1f;
  656. u8 bit;
  657. unsigned long tmp;
  658. tmp = (rA == 0) ? 0 : regs->gpr[rA];
  659. bit = (regs->ccr >> (31 - BC)) & 0x1;
  660. regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
  661. return 0;
  662. }
  663. static int emulate_instruction(struct pt_regs *regs)
  664. {
  665. u32 instword;
  666. u32 rd;
  667. if (!user_mode(regs) || (regs->msr & MSR_LE))
  668. return -EINVAL;
  669. CHECK_FULL_REGS(regs);
  670. if (get_user(instword, (u32 __user *)(regs->nip)))
  671. return -EFAULT;
  672. /* Emulate the mfspr rD, PVR. */
  673. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  674. rd = (instword >> 21) & 0x1f;
  675. regs->gpr[rd] = mfspr(SPRN_PVR);
  676. return 0;
  677. }
  678. /* Emulating the dcba insn is just a no-op. */
  679. if ((instword & INST_DCBA_MASK) == INST_DCBA)
  680. return 0;
  681. /* Emulate the mcrxr insn. */
  682. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  683. int shift = (instword >> 21) & 0x1c;
  684. unsigned long msk = 0xf0000000UL >> shift;
  685. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  686. regs->xer &= ~0xf0000000UL;
  687. return 0;
  688. }
  689. /* Emulate load/store string insn. */
  690. if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
  691. return emulate_string_inst(regs, instword);
  692. /* Emulate the popcntb (Population Count Bytes) instruction. */
  693. if ((instword & INST_POPCNTB_MASK) == INST_POPCNTB) {
  694. return emulate_popcntb_inst(regs, instword);
  695. }
  696. /* Emulate isel (Integer Select) instruction */
  697. if ((instword & INST_ISEL_MASK) == INST_ISEL) {
  698. return emulate_isel(regs, instword);
  699. }
  700. return -EINVAL;
  701. }
  702. int is_valid_bugaddr(unsigned long addr)
  703. {
  704. return is_kernel_addr(addr);
  705. }
  706. void __kprobes program_check_exception(struct pt_regs *regs)
  707. {
  708. unsigned int reason = get_reason(regs);
  709. extern int do_mathemu(struct pt_regs *regs);
  710. /* We can now get here via a FP Unavailable exception if the core
  711. * has no FPU, in that case the reason flags will be 0 */
  712. if (reason & REASON_FP) {
  713. /* IEEE FP exception */
  714. parse_fpe(regs);
  715. return;
  716. }
  717. if (reason & REASON_TRAP) {
  718. /* trap exception */
  719. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  720. == NOTIFY_STOP)
  721. return;
  722. if (debugger_bpt(regs))
  723. return;
  724. if (!(regs->msr & MSR_PR) && /* not user-mode */
  725. report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
  726. regs->nip += 4;
  727. return;
  728. }
  729. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  730. return;
  731. }
  732. local_irq_enable();
  733. #ifdef CONFIG_MATH_EMULATION
  734. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  735. * but there seems to be a hardware bug on the 405GP (RevD)
  736. * that means ESR is sometimes set incorrectly - either to
  737. * ESR_DST (!?) or 0. In the process of chasing this with the
  738. * hardware people - not sure if it can happen on any illegal
  739. * instruction or only on FP instructions, whether there is a
  740. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  741. switch (do_mathemu(regs)) {
  742. case 0:
  743. emulate_single_step(regs);
  744. return;
  745. case 1: {
  746. int code = 0;
  747. code = __parse_fpscr(current->thread.fpscr.val);
  748. _exception(SIGFPE, regs, code, regs->nip);
  749. return;
  750. }
  751. case -EFAULT:
  752. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  753. return;
  754. }
  755. /* fall through on any other errors */
  756. #endif /* CONFIG_MATH_EMULATION */
  757. /* Try to emulate it if we should. */
  758. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  759. switch (emulate_instruction(regs)) {
  760. case 0:
  761. regs->nip += 4;
  762. emulate_single_step(regs);
  763. return;
  764. case -EFAULT:
  765. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  766. return;
  767. }
  768. }
  769. if (reason & REASON_PRIVILEGED)
  770. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  771. else
  772. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  773. }
  774. void alignment_exception(struct pt_regs *regs)
  775. {
  776. int sig, code, fixed = 0;
  777. /* we don't implement logging of alignment exceptions */
  778. if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
  779. fixed = fix_alignment(regs);
  780. if (fixed == 1) {
  781. regs->nip += 4; /* skip over emulated instruction */
  782. emulate_single_step(regs);
  783. return;
  784. }
  785. /* Operand address was bad */
  786. if (fixed == -EFAULT) {
  787. sig = SIGSEGV;
  788. code = SEGV_ACCERR;
  789. } else {
  790. sig = SIGBUS;
  791. code = BUS_ADRALN;
  792. }
  793. if (user_mode(regs))
  794. _exception(sig, regs, code, regs->dar);
  795. else
  796. bad_page_fault(regs, regs->dar, sig);
  797. }
  798. void StackOverflow(struct pt_regs *regs)
  799. {
  800. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  801. current, regs->gpr[1]);
  802. debugger(regs);
  803. show_regs(regs);
  804. panic("kernel stack overflow");
  805. }
  806. void nonrecoverable_exception(struct pt_regs *regs)
  807. {
  808. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  809. regs->nip, regs->msr);
  810. debugger(regs);
  811. die("nonrecoverable exception", regs, SIGKILL);
  812. }
  813. void trace_syscall(struct pt_regs *regs)
  814. {
  815. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  816. current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
  817. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  818. }
  819. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  820. {
  821. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  822. "%lx at %lx\n", regs->trap, regs->nip);
  823. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  824. }
  825. void altivec_unavailable_exception(struct pt_regs *regs)
  826. {
  827. if (user_mode(regs)) {
  828. /* A user program has executed an altivec instruction,
  829. but this kernel doesn't support altivec. */
  830. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  831. return;
  832. }
  833. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  834. "%lx at %lx\n", regs->trap, regs->nip);
  835. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  836. }
  837. void vsx_unavailable_exception(struct pt_regs *regs)
  838. {
  839. if (user_mode(regs)) {
  840. /* A user program has executed an vsx instruction,
  841. but this kernel doesn't support vsx. */
  842. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  843. return;
  844. }
  845. printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
  846. "%lx at %lx\n", regs->trap, regs->nip);
  847. die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
  848. }
  849. void performance_monitor_exception(struct pt_regs *regs)
  850. {
  851. perf_irq(regs);
  852. }
  853. #ifdef CONFIG_8xx
  854. void SoftwareEmulation(struct pt_regs *regs)
  855. {
  856. extern int do_mathemu(struct pt_regs *);
  857. extern int Soft_emulate_8xx(struct pt_regs *);
  858. #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
  859. int errcode;
  860. #endif
  861. CHECK_FULL_REGS(regs);
  862. if (!user_mode(regs)) {
  863. debugger(regs);
  864. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  865. }
  866. #ifdef CONFIG_MATH_EMULATION
  867. errcode = do_mathemu(regs);
  868. switch (errcode) {
  869. case 0:
  870. emulate_single_step(regs);
  871. return;
  872. case 1: {
  873. int code = 0;
  874. code = __parse_fpscr(current->thread.fpscr.val);
  875. _exception(SIGFPE, regs, code, regs->nip);
  876. return;
  877. }
  878. case -EFAULT:
  879. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  880. return;
  881. default:
  882. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  883. return;
  884. }
  885. #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
  886. errcode = Soft_emulate_8xx(regs);
  887. switch (errcode) {
  888. case 0:
  889. emulate_single_step(regs);
  890. return;
  891. case 1:
  892. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  893. return;
  894. case -EFAULT:
  895. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  896. return;
  897. }
  898. #else
  899. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  900. #endif
  901. }
  902. #endif /* CONFIG_8xx */
  903. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  904. void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
  905. {
  906. if (debug_status & DBSR_IC) { /* instruction completion */
  907. regs->msr &= ~MSR_DE;
  908. /* Disable instruction completion */
  909. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  910. /* Clear the instruction completion event */
  911. mtspr(SPRN_DBSR, DBSR_IC);
  912. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  913. 5, SIGTRAP) == NOTIFY_STOP) {
  914. return;
  915. }
  916. if (debugger_sstep(regs))
  917. return;
  918. if (user_mode(regs)) {
  919. current->thread.dbcr0 &= ~DBCR0_IC;
  920. }
  921. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  922. }
  923. }
  924. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  925. #if !defined(CONFIG_TAU_INT)
  926. void TAUException(struct pt_regs *regs)
  927. {
  928. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  929. regs->nip, regs->msr, regs->trap, print_tainted());
  930. }
  931. #endif /* CONFIG_INT_TAU */
  932. #ifdef CONFIG_ALTIVEC
  933. void altivec_assist_exception(struct pt_regs *regs)
  934. {
  935. int err;
  936. if (!user_mode(regs)) {
  937. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  938. " at %lx\n", regs->nip);
  939. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  940. }
  941. flush_altivec_to_thread(current);
  942. err = emulate_altivec(regs);
  943. if (err == 0) {
  944. regs->nip += 4; /* skip emulated instruction */
  945. emulate_single_step(regs);
  946. return;
  947. }
  948. if (err == -EFAULT) {
  949. /* got an error reading the instruction */
  950. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  951. } else {
  952. /* didn't recognize the instruction */
  953. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  954. if (printk_ratelimit())
  955. printk(KERN_ERR "Unrecognized altivec instruction "
  956. "in %s at %lx\n", current->comm, regs->nip);
  957. current->thread.vscr.u[3] |= 0x10000;
  958. }
  959. }
  960. #endif /* CONFIG_ALTIVEC */
  961. #ifdef CONFIG_VSX
  962. void vsx_assist_exception(struct pt_regs *regs)
  963. {
  964. if (!user_mode(regs)) {
  965. printk(KERN_EMERG "VSX assist exception in kernel mode"
  966. " at %lx\n", regs->nip);
  967. die("Kernel VSX assist exception", regs, SIGILL);
  968. }
  969. flush_vsx_to_thread(current);
  970. printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
  971. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  972. }
  973. #endif /* CONFIG_VSX */
  974. #ifdef CONFIG_FSL_BOOKE
  975. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  976. unsigned long error_code)
  977. {
  978. /* We treat cache locking instructions from the user
  979. * as priv ops, in the future we could try to do
  980. * something smarter
  981. */
  982. if (error_code & (ESR_DLK|ESR_ILK))
  983. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  984. return;
  985. }
  986. #endif /* CONFIG_FSL_BOOKE */
  987. #ifdef CONFIG_SPE
  988. void SPEFloatingPointException(struct pt_regs *regs)
  989. {
  990. unsigned long spefscr;
  991. int fpexc_mode;
  992. int code = 0;
  993. spefscr = current->thread.spefscr;
  994. fpexc_mode = current->thread.fpexc_mode;
  995. /* Hardware does not neccessarily set sticky
  996. * underflow/overflow/invalid flags */
  997. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  998. code = FPE_FLTOVF;
  999. spefscr |= SPEFSCR_FOVFS;
  1000. }
  1001. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  1002. code = FPE_FLTUND;
  1003. spefscr |= SPEFSCR_FUNFS;
  1004. }
  1005. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  1006. code = FPE_FLTDIV;
  1007. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  1008. code = FPE_FLTINV;
  1009. spefscr |= SPEFSCR_FINVS;
  1010. }
  1011. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  1012. code = FPE_FLTRES;
  1013. current->thread.spefscr = spefscr;
  1014. _exception(SIGFPE, regs, code, regs->nip);
  1015. return;
  1016. }
  1017. #endif
  1018. /*
  1019. * We enter here if we get an unrecoverable exception, that is, one
  1020. * that happened at a point where the RI (recoverable interrupt) bit
  1021. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  1022. * we therefore lost state by taking this exception.
  1023. */
  1024. void unrecoverable_exception(struct pt_regs *regs)
  1025. {
  1026. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  1027. regs->trap, regs->nip);
  1028. die("Unrecoverable exception", regs, SIGABRT);
  1029. }
  1030. #ifdef CONFIG_BOOKE_WDT
  1031. /*
  1032. * Default handler for a Watchdog exception,
  1033. * spins until a reboot occurs
  1034. */
  1035. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  1036. {
  1037. /* Generic WatchdogHandler, implement your own */
  1038. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  1039. return;
  1040. }
  1041. void WatchdogException(struct pt_regs *regs)
  1042. {
  1043. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  1044. WatchdogHandler(regs);
  1045. }
  1046. #endif
  1047. /*
  1048. * We enter here if we discover during exception entry that we are
  1049. * running in supervisor mode with a userspace value in the stack pointer.
  1050. */
  1051. void kernel_bad_stack(struct pt_regs *regs)
  1052. {
  1053. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  1054. regs->gpr[1], regs->nip);
  1055. die("Bad kernel stack pointer", regs, SIGABRT);
  1056. }
  1057. void __init trap_init(void)
  1058. {
  1059. }