traps.c 18 KB

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