traps_32.c 21 KB

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