traps_32.c 20 KB

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