traps_32.c 21 KB

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