traps_32.c 21 KB

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