traps_32.c 21 KB

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