traps.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*
  2. * 'traps.c' handles hardware traps and faults after we have saved some
  3. * state in 'entry.S'.
  4. *
  5. * SuperH version: Copyright (C) 1999 Niibe Yutaka
  6. * Copyright (C) 2000 Philipp Rumpf
  7. * Copyright (C) 2000 David Howells
  8. * Copyright (C) 2002 - 2007 Paul Mundt
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/init.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/module.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/io.h>
  21. #include <linux/bug.h>
  22. #include <linux/debug_locks.h>
  23. #include <linux/limits.h>
  24. #include <asm/system.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/kdebug.h>
  27. #ifdef CONFIG_SH_KGDB
  28. #include <asm/kgdb.h>
  29. #define CHK_REMOTE_DEBUG(regs) \
  30. { \
  31. if (kgdb_debug_hook && !user_mode(regs))\
  32. (*kgdb_debug_hook)(regs); \
  33. }
  34. #else
  35. #define CHK_REMOTE_DEBUG(regs)
  36. #endif
  37. #ifdef CONFIG_CPU_SH2
  38. # define TRAP_RESERVED_INST 4
  39. # define TRAP_ILLEGAL_SLOT_INST 6
  40. # define TRAP_ADDRESS_ERROR 9
  41. # ifdef CONFIG_CPU_SH2A
  42. # define TRAP_DIVZERO_ERROR 17
  43. # define TRAP_DIVOVF_ERROR 18
  44. # endif
  45. #else
  46. #define TRAP_RESERVED_INST 12
  47. #define TRAP_ILLEGAL_SLOT_INST 13
  48. #endif
  49. static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
  50. {
  51. unsigned long p;
  52. int i;
  53. printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
  54. for (p = bottom & ~31; p < top; ) {
  55. printk("%04lx: ", p & 0xffff);
  56. for (i = 0; i < 8; i++, p += 4) {
  57. unsigned int val;
  58. if (p < bottom || p >= top)
  59. printk(" ");
  60. else {
  61. if (__get_user(val, (unsigned int __user *)p)) {
  62. printk("\n");
  63. return;
  64. }
  65. printk("%08x ", val);
  66. }
  67. }
  68. printk("\n");
  69. }
  70. }
  71. ATOMIC_NOTIFIER_HEAD(shdie_chain);
  72. int register_die_notifier(struct notifier_block *nb)
  73. {
  74. return atomic_notifier_chain_register(&shdie_chain, nb);
  75. }
  76. EXPORT_SYMBOL(register_die_notifier);
  77. int unregister_die_notifier(struct notifier_block *nb)
  78. {
  79. return atomic_notifier_chain_unregister(&shdie_chain, nb);
  80. }
  81. EXPORT_SYMBOL(unregister_die_notifier);
  82. static DEFINE_SPINLOCK(die_lock);
  83. void die(const char * str, struct pt_regs * regs, long err)
  84. {
  85. static int die_counter;
  86. console_verbose();
  87. spin_lock_irq(&die_lock);
  88. bust_spinlocks(1);
  89. printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
  90. CHK_REMOTE_DEBUG(regs);
  91. print_modules();
  92. show_regs(regs);
  93. printk("Process: %s (pid: %d, stack limit = %p)\n",
  94. current->comm, current->pid, task_stack_page(current) + 1);
  95. if (!user_mode(regs) || in_interrupt())
  96. dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
  97. (unsigned long)task_stack_page(current));
  98. bust_spinlocks(0);
  99. spin_unlock_irq(&die_lock);
  100. do_exit(SIGSEGV);
  101. }
  102. static inline void die_if_kernel(const char *str, struct pt_regs *regs,
  103. long err)
  104. {
  105. if (!user_mode(regs))
  106. die(str, regs, err);
  107. }
  108. /*
  109. * try and fix up kernelspace address errors
  110. * - userspace errors just cause EFAULT to be returned, resulting in SEGV
  111. * - kernel/userspace interfaces cause a jump to an appropriate handler
  112. * - other kernel errors are bad
  113. * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
  114. */
  115. static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
  116. {
  117. if (!user_mode(regs)) {
  118. const struct exception_table_entry *fixup;
  119. fixup = search_exception_tables(regs->pc);
  120. if (fixup) {
  121. regs->pc = fixup->fixup;
  122. return 0;
  123. }
  124. die(str, regs, err);
  125. }
  126. return -EFAULT;
  127. }
  128. /*
  129. * handle an instruction that does an unaligned memory access by emulating the
  130. * desired behaviour
  131. * - note that PC _may not_ point to the faulting instruction
  132. * (if that instruction is in a branch delay slot)
  133. * - return 0 if emulation okay, -EFAULT on existential error
  134. */
  135. static int handle_unaligned_ins(u16 instruction, struct pt_regs *regs)
  136. {
  137. int ret, index, count;
  138. unsigned long *rm, *rn;
  139. unsigned char *src, *dst;
  140. index = (instruction>>8)&15; /* 0x0F00 */
  141. rn = &regs->regs[index];
  142. index = (instruction>>4)&15; /* 0x00F0 */
  143. rm = &regs->regs[index];
  144. count = 1<<(instruction&3);
  145. ret = -EFAULT;
  146. switch (instruction>>12) {
  147. case 0: /* mov.[bwl] to/from memory via r0+rn */
  148. if (instruction & 8) {
  149. /* from memory */
  150. src = (unsigned char*) *rm;
  151. src += regs->regs[0];
  152. dst = (unsigned char*) rn;
  153. *(unsigned long*)dst = 0;
  154. #ifdef __LITTLE_ENDIAN__
  155. if (copy_from_user(dst, src, count))
  156. goto fetch_fault;
  157. if ((count == 2) && dst[1] & 0x80) {
  158. dst[2] = 0xff;
  159. dst[3] = 0xff;
  160. }
  161. #else
  162. dst += 4-count;
  163. if (__copy_user(dst, src, count))
  164. goto fetch_fault;
  165. if ((count == 2) && dst[2] & 0x80) {
  166. dst[0] = 0xff;
  167. dst[1] = 0xff;
  168. }
  169. #endif
  170. } else {
  171. /* to memory */
  172. src = (unsigned char*) rm;
  173. #if !defined(__LITTLE_ENDIAN__)
  174. src += 4-count;
  175. #endif
  176. dst = (unsigned char*) *rn;
  177. dst += regs->regs[0];
  178. if (copy_to_user(dst, src, count))
  179. goto fetch_fault;
  180. }
  181. ret = 0;
  182. break;
  183. case 1: /* mov.l Rm,@(disp,Rn) */
  184. src = (unsigned char*) rm;
  185. dst = (unsigned char*) *rn;
  186. dst += (instruction&0x000F)<<2;
  187. if (copy_to_user(dst,src,4))
  188. goto fetch_fault;
  189. ret = 0;
  190. break;
  191. case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
  192. if (instruction & 4)
  193. *rn -= count;
  194. src = (unsigned char*) rm;
  195. dst = (unsigned char*) *rn;
  196. #if !defined(__LITTLE_ENDIAN__)
  197. src += 4-count;
  198. #endif
  199. if (copy_to_user(dst, src, count))
  200. goto fetch_fault;
  201. ret = 0;
  202. break;
  203. case 5: /* mov.l @(disp,Rm),Rn */
  204. src = (unsigned char*) *rm;
  205. src += (instruction&0x000F)<<2;
  206. dst = (unsigned char*) rn;
  207. *(unsigned long*)dst = 0;
  208. if (copy_from_user(dst,src,4))
  209. goto fetch_fault;
  210. ret = 0;
  211. break;
  212. case 6: /* mov.[bwl] from memory, possibly with post-increment */
  213. src = (unsigned char*) *rm;
  214. if (instruction & 4)
  215. *rm += count;
  216. dst = (unsigned char*) rn;
  217. *(unsigned long*)dst = 0;
  218. #ifdef __LITTLE_ENDIAN__
  219. if (copy_from_user(dst, src, count))
  220. goto fetch_fault;
  221. if ((count == 2) && dst[1] & 0x80) {
  222. dst[2] = 0xff;
  223. dst[3] = 0xff;
  224. }
  225. #else
  226. dst += 4-count;
  227. if (copy_from_user(dst, src, count))
  228. goto fetch_fault;
  229. if ((count == 2) && dst[2] & 0x80) {
  230. dst[0] = 0xff;
  231. dst[1] = 0xff;
  232. }
  233. #endif
  234. ret = 0;
  235. break;
  236. case 8:
  237. switch ((instruction&0xFF00)>>8) {
  238. case 0x81: /* mov.w R0,@(disp,Rn) */
  239. src = (unsigned char*) &regs->regs[0];
  240. #if !defined(__LITTLE_ENDIAN__)
  241. src += 2;
  242. #endif
  243. dst = (unsigned char*) *rm; /* called Rn in the spec */
  244. dst += (instruction&0x000F)<<1;
  245. if (copy_to_user(dst, src, 2))
  246. goto fetch_fault;
  247. ret = 0;
  248. break;
  249. case 0x85: /* mov.w @(disp,Rm),R0 */
  250. src = (unsigned char*) *rm;
  251. src += (instruction&0x000F)<<1;
  252. dst = (unsigned char*) &regs->regs[0];
  253. *(unsigned long*)dst = 0;
  254. #if !defined(__LITTLE_ENDIAN__)
  255. dst += 2;
  256. #endif
  257. if (copy_from_user(dst, src, 2))
  258. goto fetch_fault;
  259. #ifdef __LITTLE_ENDIAN__
  260. if (dst[1] & 0x80) {
  261. dst[2] = 0xff;
  262. dst[3] = 0xff;
  263. }
  264. #else
  265. if (dst[2] & 0x80) {
  266. dst[0] = 0xff;
  267. dst[1] = 0xff;
  268. }
  269. #endif
  270. ret = 0;
  271. break;
  272. }
  273. break;
  274. }
  275. return ret;
  276. fetch_fault:
  277. /* Argh. Address not only misaligned but also non-existent.
  278. * Raise an EFAULT and see if it's trapped
  279. */
  280. return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
  281. }
  282. /*
  283. * emulate the instruction in the delay slot
  284. * - fetches the instruction from PC+2
  285. */
  286. static inline int handle_unaligned_delayslot(struct pt_regs *regs)
  287. {
  288. u16 instruction;
  289. if (copy_from_user(&instruction, (u16 *)(regs->pc+2), 2)) {
  290. /* the instruction-fetch faulted */
  291. if (user_mode(regs))
  292. return -EFAULT;
  293. /* kernel */
  294. die("delay-slot-insn faulting in handle_unaligned_delayslot",
  295. regs, 0);
  296. }
  297. return handle_unaligned_ins(instruction,regs);
  298. }
  299. /*
  300. * handle an instruction that does an unaligned memory access
  301. * - have to be careful of branch delay-slot instructions that fault
  302. * SH3:
  303. * - if the branch would be taken PC points to the branch
  304. * - if the branch would not be taken, PC points to delay-slot
  305. * SH4:
  306. * - PC always points to delayed branch
  307. * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
  308. */
  309. /* Macros to determine offset from current PC for branch instructions */
  310. /* Explicit type coercion is used to force sign extension where needed */
  311. #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
  312. #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
  313. /*
  314. * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
  315. * opcodes..
  316. */
  317. #ifndef CONFIG_CPU_SH2A
  318. static int handle_unaligned_notify_count = 10;
  319. static int handle_unaligned_access(u16 instruction, struct pt_regs *regs)
  320. {
  321. u_int rm;
  322. int ret, index;
  323. index = (instruction>>8)&15; /* 0x0F00 */
  324. rm = regs->regs[index];
  325. /* shout about the first ten userspace fixups */
  326. if (user_mode(regs) && handle_unaligned_notify_count>0) {
  327. handle_unaligned_notify_count--;
  328. printk(KERN_NOTICE "Fixing up unaligned userspace access "
  329. "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
  330. current->comm,current->pid,(u16*)regs->pc,instruction);
  331. }
  332. ret = -EFAULT;
  333. switch (instruction&0xF000) {
  334. case 0x0000:
  335. if (instruction==0x000B) {
  336. /* rts */
  337. ret = handle_unaligned_delayslot(regs);
  338. if (ret==0)
  339. regs->pc = regs->pr;
  340. }
  341. else if ((instruction&0x00FF)==0x0023) {
  342. /* braf @Rm */
  343. ret = handle_unaligned_delayslot(regs);
  344. if (ret==0)
  345. regs->pc += rm + 4;
  346. }
  347. else if ((instruction&0x00FF)==0x0003) {
  348. /* bsrf @Rm */
  349. ret = handle_unaligned_delayslot(regs);
  350. if (ret==0) {
  351. regs->pr = regs->pc + 4;
  352. regs->pc += rm + 4;
  353. }
  354. }
  355. else {
  356. /* mov.[bwl] to/from memory via r0+rn */
  357. goto simple;
  358. }
  359. break;
  360. case 0x1000: /* mov.l Rm,@(disp,Rn) */
  361. goto simple;
  362. case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
  363. goto simple;
  364. case 0x4000:
  365. if ((instruction&0x00FF)==0x002B) {
  366. /* jmp @Rm */
  367. ret = handle_unaligned_delayslot(regs);
  368. if (ret==0)
  369. regs->pc = rm;
  370. }
  371. else if ((instruction&0x00FF)==0x000B) {
  372. /* jsr @Rm */
  373. ret = handle_unaligned_delayslot(regs);
  374. if (ret==0) {
  375. regs->pr = regs->pc + 4;
  376. regs->pc = rm;
  377. }
  378. }
  379. else {
  380. /* mov.[bwl] to/from memory via r0+rn */
  381. goto simple;
  382. }
  383. break;
  384. case 0x5000: /* mov.l @(disp,Rm),Rn */
  385. goto simple;
  386. case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
  387. goto simple;
  388. case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
  389. switch (instruction&0x0F00) {
  390. case 0x0100: /* mov.w R0,@(disp,Rm) */
  391. goto simple;
  392. case 0x0500: /* mov.w @(disp,Rm),R0 */
  393. goto simple;
  394. case 0x0B00: /* bf lab - no delayslot*/
  395. break;
  396. case 0x0F00: /* bf/s lab */
  397. ret = handle_unaligned_delayslot(regs);
  398. if (ret==0) {
  399. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  400. if ((regs->sr & 0x00000001) != 0)
  401. regs->pc += 4; /* next after slot */
  402. else
  403. #endif
  404. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  405. }
  406. break;
  407. case 0x0900: /* bt lab - no delayslot */
  408. break;
  409. case 0x0D00: /* bt/s lab */
  410. ret = handle_unaligned_delayslot(regs);
  411. if (ret==0) {
  412. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  413. if ((regs->sr & 0x00000001) == 0)
  414. regs->pc += 4; /* next after slot */
  415. else
  416. #endif
  417. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  418. }
  419. break;
  420. }
  421. break;
  422. case 0xA000: /* bra label */
  423. ret = handle_unaligned_delayslot(regs);
  424. if (ret==0)
  425. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  426. break;
  427. case 0xB000: /* bsr label */
  428. ret = handle_unaligned_delayslot(regs);
  429. if (ret==0) {
  430. regs->pr = regs->pc + 4;
  431. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  432. }
  433. break;
  434. }
  435. return ret;
  436. /* handle non-delay-slot instruction */
  437. simple:
  438. ret = handle_unaligned_ins(instruction,regs);
  439. if (ret==0)
  440. regs->pc += 2;
  441. return ret;
  442. }
  443. #endif /* CONFIG_CPU_SH2A */
  444. #ifdef CONFIG_CPU_HAS_SR_RB
  445. #define lookup_exception_vector(x) \
  446. __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
  447. #else
  448. #define lookup_exception_vector(x) \
  449. __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
  450. #endif
  451. /*
  452. * Handle various address error exceptions:
  453. * - instruction address error:
  454. * misaligned PC
  455. * PC >= 0x80000000 in user mode
  456. * - data address error (read and write)
  457. * misaligned data access
  458. * access to >= 0x80000000 is user mode
  459. * Unfortuntaly we can't distinguish between instruction address error
  460. * and data address errors caused by read acceses.
  461. */
  462. asmlinkage void do_address_error(struct pt_regs *regs,
  463. unsigned long writeaccess,
  464. unsigned long address)
  465. {
  466. unsigned long error_code = 0;
  467. mm_segment_t oldfs;
  468. siginfo_t info;
  469. #ifndef CONFIG_CPU_SH2A
  470. u16 instruction;
  471. int tmp;
  472. #endif
  473. /* Intentional ifdef */
  474. #ifdef CONFIG_CPU_HAS_SR_RB
  475. lookup_exception_vector(error_code);
  476. #endif
  477. oldfs = get_fs();
  478. if (user_mode(regs)) {
  479. int si_code = BUS_ADRERR;
  480. local_irq_enable();
  481. /* bad PC is not something we can fix */
  482. if (regs->pc & 1) {
  483. si_code = BUS_ADRALN;
  484. goto uspace_segv;
  485. }
  486. #ifndef CONFIG_CPU_SH2A
  487. set_fs(USER_DS);
  488. if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
  489. /* Argh. Fault on the instruction itself.
  490. This should never happen non-SMP
  491. */
  492. set_fs(oldfs);
  493. goto uspace_segv;
  494. }
  495. tmp = handle_unaligned_access(instruction, regs);
  496. set_fs(oldfs);
  497. if (tmp==0)
  498. return; /* sorted */
  499. #endif
  500. uspace_segv:
  501. printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
  502. "access (PC %lx PR %lx)\n", current->comm, regs->pc,
  503. regs->pr);
  504. info.si_signo = SIGBUS;
  505. info.si_errno = 0;
  506. info.si_code = si_code;
  507. info.si_addr = (void *) address;
  508. force_sig_info(SIGBUS, &info, current);
  509. } else {
  510. if (regs->pc & 1)
  511. die("unaligned program counter", regs, error_code);
  512. #ifndef CONFIG_CPU_SH2A
  513. set_fs(KERNEL_DS);
  514. if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
  515. /* Argh. Fault on the instruction itself.
  516. This should never happen non-SMP
  517. */
  518. set_fs(oldfs);
  519. die("insn faulting in do_address_error", regs, 0);
  520. }
  521. handle_unaligned_access(instruction, regs);
  522. set_fs(oldfs);
  523. #else
  524. printk(KERN_NOTICE "Killing process \"%s\" due to unaligned "
  525. "access\n", current->comm);
  526. force_sig(SIGSEGV, current);
  527. #endif
  528. }
  529. }
  530. #ifdef CONFIG_SH_DSP
  531. /*
  532. * SH-DSP support gerg@snapgear.com.
  533. */
  534. int is_dsp_inst(struct pt_regs *regs)
  535. {
  536. unsigned short inst;
  537. /*
  538. * Safe guard if DSP mode is already enabled or we're lacking
  539. * the DSP altogether.
  540. */
  541. if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
  542. return 0;
  543. get_user(inst, ((unsigned short *) regs->pc));
  544. inst &= 0xf000;
  545. /* Check for any type of DSP or support instruction */
  546. if ((inst == 0xf000) || (inst == 0x4000))
  547. return 1;
  548. return 0;
  549. }
  550. #else
  551. #define is_dsp_inst(regs) (0)
  552. #endif /* CONFIG_SH_DSP */
  553. #ifdef CONFIG_CPU_SH2A
  554. asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
  555. unsigned long r6, unsigned long r7,
  556. struct pt_regs __regs)
  557. {
  558. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  559. siginfo_t info;
  560. switch (r4) {
  561. case TRAP_DIVZERO_ERROR:
  562. info.si_code = FPE_INTDIV;
  563. break;
  564. case TRAP_DIVOVF_ERROR:
  565. info.si_code = FPE_INTOVF;
  566. break;
  567. }
  568. force_sig_info(SIGFPE, &info, current);
  569. }
  570. #endif
  571. /* arch/sh/kernel/cpu/sh4/fpu.c */
  572. extern int do_fpu_inst(unsigned short, struct pt_regs *);
  573. extern asmlinkage void do_fpu_state_restore(unsigned long r4, unsigned long r5,
  574. unsigned long r6, unsigned long r7, struct pt_regs __regs);
  575. asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
  576. unsigned long r6, unsigned long r7,
  577. struct pt_regs __regs)
  578. {
  579. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  580. unsigned long error_code;
  581. struct task_struct *tsk = current;
  582. #ifdef CONFIG_SH_FPU_EMU
  583. unsigned short inst = 0;
  584. int err;
  585. get_user(inst, (unsigned short*)regs->pc);
  586. err = do_fpu_inst(inst, regs);
  587. if (!err) {
  588. regs->pc += 2;
  589. return;
  590. }
  591. /* not a FPU inst. */
  592. #endif
  593. #ifdef CONFIG_SH_DSP
  594. /* Check if it's a DSP instruction */
  595. if (is_dsp_inst(regs)) {
  596. /* Enable DSP mode, and restart instruction. */
  597. regs->sr |= SR_DSP;
  598. return;
  599. }
  600. #endif
  601. lookup_exception_vector(error_code);
  602. local_irq_enable();
  603. CHK_REMOTE_DEBUG(regs);
  604. force_sig(SIGILL, tsk);
  605. die_if_no_fixup("reserved instruction", regs, error_code);
  606. }
  607. #ifdef CONFIG_SH_FPU_EMU
  608. static int emulate_branch(unsigned short inst, struct pt_regs* regs)
  609. {
  610. /*
  611. * bfs: 8fxx: PC+=d*2+4;
  612. * bts: 8dxx: PC+=d*2+4;
  613. * bra: axxx: PC+=D*2+4;
  614. * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
  615. * braf:0x23: PC+=Rn*2+4;
  616. * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
  617. * jmp: 4x2b: PC=Rn;
  618. * jsr: 4x0b: PC=Rn after PR=PC+4;
  619. * rts: 000b: PC=PR;
  620. */
  621. if ((inst & 0xfd00) == 0x8d00) {
  622. regs->pc += SH_PC_8BIT_OFFSET(inst);
  623. return 0;
  624. }
  625. if ((inst & 0xe000) == 0xa000) {
  626. regs->pc += SH_PC_12BIT_OFFSET(inst);
  627. return 0;
  628. }
  629. if ((inst & 0xf0df) == 0x0003) {
  630. regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
  631. return 0;
  632. }
  633. if ((inst & 0xf0df) == 0x400b) {
  634. regs->pc = regs->regs[(inst & 0x0f00) >> 8];
  635. return 0;
  636. }
  637. if ((inst & 0xffff) == 0x000b) {
  638. regs->pc = regs->pr;
  639. return 0;
  640. }
  641. return 1;
  642. }
  643. #endif
  644. asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
  645. unsigned long r6, unsigned long r7,
  646. struct pt_regs __regs)
  647. {
  648. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  649. unsigned long error_code;
  650. struct task_struct *tsk = current;
  651. #ifdef CONFIG_SH_FPU_EMU
  652. unsigned short inst = 0;
  653. get_user(inst, (unsigned short *)regs->pc + 1);
  654. if (!do_fpu_inst(inst, regs)) {
  655. get_user(inst, (unsigned short *)regs->pc);
  656. if (!emulate_branch(inst, regs))
  657. return;
  658. /* fault in branch.*/
  659. }
  660. /* not a FPU inst. */
  661. #endif
  662. lookup_exception_vector(error_code);
  663. local_irq_enable();
  664. CHK_REMOTE_DEBUG(regs);
  665. force_sig(SIGILL, tsk);
  666. die_if_no_fixup("illegal slot instruction", regs, error_code);
  667. }
  668. asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
  669. unsigned long r6, unsigned long r7,
  670. struct pt_regs __regs)
  671. {
  672. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  673. long ex;
  674. lookup_exception_vector(ex);
  675. die_if_kernel("exception", regs, ex);
  676. }
  677. #if defined(CONFIG_SH_STANDARD_BIOS)
  678. void *gdb_vbr_vector;
  679. static inline void __init gdb_vbr_init(void)
  680. {
  681. register unsigned long vbr;
  682. /*
  683. * Read the old value of the VBR register to initialise
  684. * the vector through which debug and BIOS traps are
  685. * delegated by the Linux trap handler.
  686. */
  687. asm volatile("stc vbr, %0" : "=r" (vbr));
  688. gdb_vbr_vector = (void *)(vbr + 0x100);
  689. printk("Setting GDB trap vector to 0x%08lx\n",
  690. (unsigned long)gdb_vbr_vector);
  691. }
  692. #endif
  693. void __init per_cpu_trap_init(void)
  694. {
  695. extern void *vbr_base;
  696. #ifdef CONFIG_SH_STANDARD_BIOS
  697. gdb_vbr_init();
  698. #endif
  699. /* NOTE: The VBR value should be at P1
  700. (or P2, virtural "fixed" address space).
  701. It's definitely should not in physical address. */
  702. asm volatile("ldc %0, vbr"
  703. : /* no output */
  704. : "r" (&vbr_base)
  705. : "memory");
  706. }
  707. void *set_exception_table_vec(unsigned int vec, void *handler)
  708. {
  709. extern void *exception_handling_table[];
  710. void *old_handler;
  711. old_handler = exception_handling_table[vec];
  712. exception_handling_table[vec] = handler;
  713. return old_handler;
  714. }
  715. extern asmlinkage void address_error_handler(unsigned long r4, unsigned long r5,
  716. unsigned long r6, unsigned long r7,
  717. struct pt_regs __regs);
  718. void __init trap_init(void)
  719. {
  720. set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
  721. set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
  722. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
  723. defined(CONFIG_SH_FPU_EMU)
  724. /*
  725. * For SH-4 lacking an FPU, treat floating point instructions as
  726. * reserved. They'll be handled in the math-emu case, or faulted on
  727. * otherwise.
  728. */
  729. set_exception_table_evt(0x800, do_reserved_inst);
  730. set_exception_table_evt(0x820, do_illegal_slot_inst);
  731. #elif defined(CONFIG_SH_FPU)
  732. set_exception_table_evt(0x800, do_fpu_state_restore);
  733. set_exception_table_evt(0x820, do_fpu_state_restore);
  734. #endif
  735. #ifdef CONFIG_CPU_SH2
  736. set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_handler);
  737. #endif
  738. #ifdef CONFIG_CPU_SH2A
  739. set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
  740. set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
  741. #endif
  742. /* Setup VBR for boot cpu */
  743. per_cpu_trap_init();
  744. }
  745. #ifdef CONFIG_BUG
  746. void handle_BUG(struct pt_regs *regs)
  747. {
  748. enum bug_trap_type tt;
  749. tt = report_bug(regs->pc);
  750. if (tt == BUG_TRAP_TYPE_WARN) {
  751. regs->pc += 2;
  752. return;
  753. }
  754. die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
  755. }
  756. int is_valid_bugaddr(unsigned long addr)
  757. {
  758. return addr >= PAGE_OFFSET;
  759. }
  760. #endif
  761. void show_trace(struct task_struct *tsk, unsigned long *sp,
  762. struct pt_regs *regs)
  763. {
  764. unsigned long addr;
  765. if (regs && user_mode(regs))
  766. return;
  767. printk("\nCall trace: ");
  768. #ifdef CONFIG_KALLSYMS
  769. printk("\n");
  770. #endif
  771. while (!kstack_end(sp)) {
  772. addr = *sp++;
  773. if (kernel_text_address(addr))
  774. print_ip_sym(addr);
  775. }
  776. printk("\n");
  777. if (!tsk)
  778. tsk = current;
  779. debug_show_held_locks(tsk);
  780. }
  781. void show_stack(struct task_struct *tsk, unsigned long *sp)
  782. {
  783. unsigned long stack;
  784. if (!tsk)
  785. tsk = current;
  786. if (tsk == current)
  787. sp = (unsigned long *)current_stack_pointer;
  788. else
  789. sp = (unsigned long *)tsk->thread.sp;
  790. stack = (unsigned long)sp;
  791. dump_mem("Stack: ", stack, THREAD_SIZE +
  792. (unsigned long)task_stack_page(tsk));
  793. show_trace(tsk, sp, NULL);
  794. }
  795. void dump_stack(void)
  796. {
  797. show_stack(NULL, NULL);
  798. }
  799. EXPORT_SYMBOL(dump_stack);