traps_32.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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 - 2010 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/hardirq.h>
  17. #include <linux/init.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/module.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/io.h>
  22. #include <linux/bug.h>
  23. #include <linux/debug_locks.h>
  24. #include <linux/kdebug.h>
  25. #include <linux/kexec.h>
  26. #include <linux/limits.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/perf_event.h>
  30. #include <asm/system.h>
  31. #include <asm/alignment.h>
  32. #include <asm/fpu.h>
  33. #include <asm/kprobes.h>
  34. #ifdef CONFIG_CPU_SH2
  35. # define TRAP_RESERVED_INST 4
  36. # define TRAP_ILLEGAL_SLOT_INST 6
  37. # define TRAP_ADDRESS_ERROR 9
  38. # ifdef CONFIG_CPU_SH2A
  39. # define TRAP_UBC 12
  40. # define TRAP_FPU_ERROR 13
  41. # define TRAP_DIVZERO_ERROR 17
  42. # define TRAP_DIVOVF_ERROR 18
  43. # endif
  44. #else
  45. #define TRAP_RESERVED_INST 12
  46. #define TRAP_ILLEGAL_SLOT_INST 13
  47. #endif
  48. static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
  49. {
  50. unsigned long p;
  51. int i;
  52. printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
  53. for (p = bottom & ~31; p < top; ) {
  54. printk("%04lx: ", p & 0xffff);
  55. for (i = 0; i < 8; i++, p += 4) {
  56. unsigned int val;
  57. if (p < bottom || p >= top)
  58. printk(" ");
  59. else {
  60. if (__get_user(val, (unsigned int __user *)p)) {
  61. printk("\n");
  62. return;
  63. }
  64. printk("%08x ", val);
  65. }
  66. }
  67. printk("\n");
  68. }
  69. }
  70. static DEFINE_SPINLOCK(die_lock);
  71. void die(const char * str, struct pt_regs * regs, long err)
  72. {
  73. static int die_counter;
  74. oops_enter();
  75. spin_lock_irq(&die_lock);
  76. console_verbose();
  77. bust_spinlocks(1);
  78. printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
  79. print_modules();
  80. show_regs(regs);
  81. printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
  82. task_pid_nr(current), task_stack_page(current) + 1);
  83. if (!user_mode(regs) || in_interrupt())
  84. dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
  85. (unsigned long)task_stack_page(current));
  86. notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);
  87. bust_spinlocks(0);
  88. add_taint(TAINT_DIE);
  89. spin_unlock_irq(&die_lock);
  90. oops_exit();
  91. if (kexec_should_crash(current))
  92. crash_kexec(regs);
  93. if (in_interrupt())
  94. panic("Fatal exception in interrupt");
  95. if (panic_on_oops)
  96. panic("Fatal exception");
  97. do_exit(SIGSEGV);
  98. }
  99. static inline void die_if_kernel(const char *str, struct pt_regs *regs,
  100. long err)
  101. {
  102. if (!user_mode(regs))
  103. die(str, regs, err);
  104. }
  105. /*
  106. * try and fix up kernelspace address errors
  107. * - userspace errors just cause EFAULT to be returned, resulting in SEGV
  108. * - kernel/userspace interfaces cause a jump to an appropriate handler
  109. * - other kernel errors are bad
  110. */
  111. static void die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
  112. {
  113. if (!user_mode(regs)) {
  114. const struct exception_table_entry *fixup;
  115. fixup = search_exception_tables(regs->pc);
  116. if (fixup) {
  117. regs->pc = fixup->fixup;
  118. return;
  119. }
  120. die(str, regs, err);
  121. }
  122. }
  123. static inline void sign_extend(unsigned int count, unsigned char *dst)
  124. {
  125. #ifdef __LITTLE_ENDIAN__
  126. if ((count == 1) && dst[0] & 0x80) {
  127. dst[1] = 0xff;
  128. dst[2] = 0xff;
  129. dst[3] = 0xff;
  130. }
  131. if ((count == 2) && dst[1] & 0x80) {
  132. dst[2] = 0xff;
  133. dst[3] = 0xff;
  134. }
  135. #else
  136. if ((count == 1) && dst[3] & 0x80) {
  137. dst[2] = 0xff;
  138. dst[1] = 0xff;
  139. dst[0] = 0xff;
  140. }
  141. if ((count == 2) && dst[2] & 0x80) {
  142. dst[1] = 0xff;
  143. dst[0] = 0xff;
  144. }
  145. #endif
  146. }
  147. static struct mem_access user_mem_access = {
  148. copy_from_user,
  149. copy_to_user,
  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(insn_size_t instruction, struct pt_regs *regs,
  159. struct mem_access *ma)
  160. {
  161. int ret, index, count;
  162. unsigned long *rm, *rn;
  163. unsigned char *src, *dst;
  164. unsigned char __user *srcu, *dstu;
  165. index = (instruction>>8)&15; /* 0x0F00 */
  166. rn = &regs->regs[index];
  167. index = (instruction>>4)&15; /* 0x00F0 */
  168. rm = &regs->regs[index];
  169. count = 1<<(instruction&3);
  170. switch (count) {
  171. case 1: inc_unaligned_byte_access(); break;
  172. case 2: inc_unaligned_word_access(); break;
  173. case 4: inc_unaligned_dword_access(); break;
  174. case 8: inc_unaligned_multi_access(); break;
  175. }
  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. die_if_no_fixup("Fault in unaligned fixup", regs, 0);
  282. return -EFAULT;
  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. insn_size_t old_instruction,
  290. struct mem_access *ma)
  291. {
  292. insn_size_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. int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
  320. struct mem_access *ma, int expected,
  321. unsigned long address)
  322. {
  323. u_int rm;
  324. int ret, index;
  325. /*
  326. * XXX: We can't handle mixed 16/32-bit instructions yet
  327. */
  328. if (instruction_size(instruction) != 2)
  329. return -EINVAL;
  330. index = (instruction>>8)&15; /* 0x0F00 */
  331. rm = regs->regs[index];
  332. /*
  333. * Log the unexpected fixups, and then pass them on to perf.
  334. *
  335. * We intentionally don't report the expected cases to perf as
  336. * otherwise the trapped I/O case will skew the results too much
  337. * to be useful.
  338. */
  339. if (!expected) {
  340. unaligned_fixups_notify(current, instruction, regs);
  341. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, 0,
  342. regs, address);
  343. }
  344. ret = -EFAULT;
  345. switch (instruction&0xF000) {
  346. case 0x0000:
  347. if (instruction==0x000B) {
  348. /* rts */
  349. ret = handle_delayslot(regs, instruction, ma);
  350. if (ret==0)
  351. regs->pc = regs->pr;
  352. }
  353. else if ((instruction&0x00FF)==0x0023) {
  354. /* braf @Rm */
  355. ret = handle_delayslot(regs, instruction, ma);
  356. if (ret==0)
  357. regs->pc += rm + 4;
  358. }
  359. else if ((instruction&0x00FF)==0x0003) {
  360. /* bsrf @Rm */
  361. ret = handle_delayslot(regs, instruction, ma);
  362. if (ret==0) {
  363. regs->pr = regs->pc + 4;
  364. regs->pc += rm + 4;
  365. }
  366. }
  367. else {
  368. /* mov.[bwl] to/from memory via r0+rn */
  369. goto simple;
  370. }
  371. break;
  372. case 0x1000: /* mov.l Rm,@(disp,Rn) */
  373. goto simple;
  374. case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
  375. goto simple;
  376. case 0x4000:
  377. if ((instruction&0x00FF)==0x002B) {
  378. /* jmp @Rm */
  379. ret = handle_delayslot(regs, instruction, ma);
  380. if (ret==0)
  381. regs->pc = rm;
  382. }
  383. else if ((instruction&0x00FF)==0x000B) {
  384. /* jsr @Rm */
  385. ret = handle_delayslot(regs, instruction, ma);
  386. if (ret==0) {
  387. regs->pr = regs->pc + 4;
  388. regs->pc = rm;
  389. }
  390. }
  391. else {
  392. /* mov.[bwl] to/from memory via r0+rn */
  393. goto simple;
  394. }
  395. break;
  396. case 0x5000: /* mov.l @(disp,Rm),Rn */
  397. goto simple;
  398. case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
  399. goto simple;
  400. case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
  401. switch (instruction&0x0F00) {
  402. case 0x0100: /* mov.w R0,@(disp,Rm) */
  403. goto simple;
  404. case 0x0500: /* mov.w @(disp,Rm),R0 */
  405. goto simple;
  406. case 0x0B00: /* bf lab - no delayslot*/
  407. break;
  408. case 0x0F00: /* bf/s lab */
  409. ret = handle_delayslot(regs, instruction, ma);
  410. if (ret==0) {
  411. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  412. if ((regs->sr & 0x00000001) != 0)
  413. regs->pc += 4; /* next after slot */
  414. else
  415. #endif
  416. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  417. }
  418. break;
  419. case 0x0900: /* bt lab - no delayslot */
  420. break;
  421. case 0x0D00: /* bt/s lab */
  422. ret = handle_delayslot(regs, instruction, ma);
  423. if (ret==0) {
  424. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  425. if ((regs->sr & 0x00000001) == 0)
  426. regs->pc += 4; /* next after slot */
  427. else
  428. #endif
  429. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  430. }
  431. break;
  432. }
  433. break;
  434. case 0xA000: /* bra label */
  435. ret = handle_delayslot(regs, instruction, ma);
  436. if (ret==0)
  437. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  438. break;
  439. case 0xB000: /* bsr label */
  440. ret = handle_delayslot(regs, instruction, ma);
  441. if (ret==0) {
  442. regs->pr = regs->pc + 4;
  443. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  444. }
  445. break;
  446. }
  447. return ret;
  448. /* handle non-delay-slot instruction */
  449. simple:
  450. ret = handle_unaligned_ins(instruction, regs, ma);
  451. if (ret==0)
  452. regs->pc += instruction_size(instruction);
  453. return ret;
  454. }
  455. /*
  456. * Handle various address error exceptions:
  457. * - instruction address error:
  458. * misaligned PC
  459. * PC >= 0x80000000 in user mode
  460. * - data address error (read and write)
  461. * misaligned data access
  462. * access to >= 0x80000000 is user mode
  463. * Unfortuntaly we can't distinguish between instruction address error
  464. * and data address errors caused by read accesses.
  465. */
  466. asmlinkage void do_address_error(struct pt_regs *regs,
  467. unsigned long writeaccess,
  468. unsigned long address)
  469. {
  470. unsigned long error_code = 0;
  471. mm_segment_t oldfs;
  472. siginfo_t info;
  473. insn_size_t instruction;
  474. int tmp;
  475. /* Intentional ifdef */
  476. #ifdef CONFIG_CPU_HAS_SR_RB
  477. error_code = lookup_exception_vector();
  478. #endif
  479. oldfs = get_fs();
  480. if (user_mode(regs)) {
  481. int si_code = BUS_ADRERR;
  482. unsigned int user_action;
  483. local_irq_enable();
  484. inc_unaligned_user_access();
  485. set_fs(USER_DS);
  486. if (copy_from_user(&instruction, (insn_size_t *)(regs->pc & ~1),
  487. sizeof(instruction))) {
  488. set_fs(oldfs);
  489. goto uspace_segv;
  490. }
  491. set_fs(oldfs);
  492. /* shout about userspace fixups */
  493. unaligned_fixups_notify(current, instruction, regs);
  494. user_action = unaligned_user_action();
  495. if (user_action & UM_FIXUP)
  496. goto fixup;
  497. if (user_action & UM_SIGNAL)
  498. goto uspace_segv;
  499. else {
  500. /* ignore */
  501. regs->pc += instruction_size(instruction);
  502. return;
  503. }
  504. fixup:
  505. /* bad PC is not something we can fix */
  506. if (regs->pc & 1) {
  507. si_code = BUS_ADRALN;
  508. goto uspace_segv;
  509. }
  510. set_fs(USER_DS);
  511. tmp = handle_unaligned_access(instruction, regs,
  512. &user_mem_access, 0,
  513. address);
  514. set_fs(oldfs);
  515. if (tmp == 0)
  516. return; /* sorted */
  517. uspace_segv:
  518. printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
  519. "access (PC %lx PR %lx)\n", current->comm, regs->pc,
  520. regs->pr);
  521. info.si_signo = SIGBUS;
  522. info.si_errno = 0;
  523. info.si_code = si_code;
  524. info.si_addr = (void __user *)address;
  525. force_sig_info(SIGBUS, &info, current);
  526. } else {
  527. inc_unaligned_kernel_access();
  528. if (regs->pc & 1)
  529. die("unaligned program counter", regs, error_code);
  530. set_fs(KERNEL_DS);
  531. if (copy_from_user(&instruction, (void __user *)(regs->pc),
  532. sizeof(instruction))) {
  533. /* Argh. Fault on the instruction itself.
  534. This should never happen non-SMP
  535. */
  536. set_fs(oldfs);
  537. die("insn faulting in do_address_error", regs, 0);
  538. }
  539. unaligned_fixups_notify(current, instruction, regs);
  540. handle_unaligned_access(instruction, regs, &user_mem_access,
  541. 0, address);
  542. set_fs(oldfs);
  543. }
  544. }
  545. #ifdef CONFIG_SH_DSP
  546. /*
  547. * SH-DSP support gerg@snapgear.com.
  548. */
  549. int is_dsp_inst(struct pt_regs *regs)
  550. {
  551. unsigned short inst = 0;
  552. /*
  553. * Safe guard if DSP mode is already enabled or we're lacking
  554. * the DSP altogether.
  555. */
  556. if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
  557. return 0;
  558. get_user(inst, ((unsigned short *) regs->pc));
  559. inst &= 0xf000;
  560. /* Check for any type of DSP or support instruction */
  561. if ((inst == 0xf000) || (inst == 0x4000))
  562. return 1;
  563. return 0;
  564. }
  565. #else
  566. #define is_dsp_inst(regs) (0)
  567. #endif /* CONFIG_SH_DSP */
  568. #ifdef CONFIG_CPU_SH2A
  569. asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
  570. unsigned long r6, unsigned long r7,
  571. struct pt_regs __regs)
  572. {
  573. siginfo_t info;
  574. switch (r4) {
  575. case TRAP_DIVZERO_ERROR:
  576. info.si_code = FPE_INTDIV;
  577. break;
  578. case TRAP_DIVOVF_ERROR:
  579. info.si_code = FPE_INTOVF;
  580. break;
  581. }
  582. force_sig_info(SIGFPE, &info, current);
  583. }
  584. #endif
  585. asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
  586. unsigned long r6, unsigned long r7,
  587. struct pt_regs __regs)
  588. {
  589. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  590. unsigned long error_code;
  591. struct task_struct *tsk = current;
  592. #ifdef CONFIG_SH_FPU_EMU
  593. unsigned short inst = 0;
  594. int err;
  595. get_user(inst, (unsigned short*)regs->pc);
  596. err = do_fpu_inst(inst, regs);
  597. if (!err) {
  598. regs->pc += instruction_size(inst);
  599. return;
  600. }
  601. /* not a FPU inst. */
  602. #endif
  603. #ifdef CONFIG_SH_DSP
  604. /* Check if it's a DSP instruction */
  605. if (is_dsp_inst(regs)) {
  606. /* Enable DSP mode, and restart instruction. */
  607. regs->sr |= SR_DSP;
  608. /* Save DSP mode */
  609. tsk->thread.dsp_status.status |= SR_DSP;
  610. return;
  611. }
  612. #endif
  613. error_code = lookup_exception_vector();
  614. local_irq_enable();
  615. force_sig(SIGILL, tsk);
  616. die_if_no_fixup("reserved instruction", regs, error_code);
  617. }
  618. #ifdef CONFIG_SH_FPU_EMU
  619. static int emulate_branch(unsigned short inst, struct pt_regs *regs)
  620. {
  621. /*
  622. * bfs: 8fxx: PC+=d*2+4;
  623. * bts: 8dxx: PC+=d*2+4;
  624. * bra: axxx: PC+=D*2+4;
  625. * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
  626. * braf:0x23: PC+=Rn*2+4;
  627. * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
  628. * jmp: 4x2b: PC=Rn;
  629. * jsr: 4x0b: PC=Rn after PR=PC+4;
  630. * rts: 000b: PC=PR;
  631. */
  632. if (((inst & 0xf000) == 0xb000) || /* bsr */
  633. ((inst & 0xf0ff) == 0x0003) || /* bsrf */
  634. ((inst & 0xf0ff) == 0x400b)) /* jsr */
  635. regs->pr = regs->pc + 4;
  636. if ((inst & 0xfd00) == 0x8d00) { /* bfs, bts */
  637. regs->pc += SH_PC_8BIT_OFFSET(inst);
  638. return 0;
  639. }
  640. if ((inst & 0xe000) == 0xa000) { /* bra, bsr */
  641. regs->pc += SH_PC_12BIT_OFFSET(inst);
  642. return 0;
  643. }
  644. if ((inst & 0xf0df) == 0x0003) { /* braf, bsrf */
  645. regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
  646. return 0;
  647. }
  648. if ((inst & 0xf0df) == 0x400b) { /* jmp, jsr */
  649. regs->pc = regs->regs[(inst & 0x0f00) >> 8];
  650. return 0;
  651. }
  652. if ((inst & 0xffff) == 0x000b) { /* rts */
  653. regs->pc = regs->pr;
  654. return 0;
  655. }
  656. return 1;
  657. }
  658. #endif
  659. asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
  660. unsigned long r6, unsigned long r7,
  661. struct pt_regs __regs)
  662. {
  663. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  664. unsigned long inst;
  665. struct task_struct *tsk = current;
  666. if (kprobe_handle_illslot(regs->pc) == 0)
  667. return;
  668. #ifdef CONFIG_SH_FPU_EMU
  669. get_user(inst, (unsigned short *)regs->pc + 1);
  670. if (!do_fpu_inst(inst, regs)) {
  671. get_user(inst, (unsigned short *)regs->pc);
  672. if (!emulate_branch(inst, regs))
  673. return;
  674. /* fault in branch.*/
  675. }
  676. /* not a FPU inst. */
  677. #endif
  678. inst = lookup_exception_vector();
  679. local_irq_enable();
  680. force_sig(SIGILL, tsk);
  681. die_if_no_fixup("illegal slot instruction", regs, inst);
  682. }
  683. asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
  684. unsigned long r6, unsigned long r7,
  685. struct pt_regs __regs)
  686. {
  687. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  688. long ex;
  689. ex = lookup_exception_vector();
  690. die_if_kernel("exception", regs, ex);
  691. }
  692. void __cpuinit per_cpu_trap_init(void)
  693. {
  694. extern void *vbr_base;
  695. /* NOTE: The VBR value should be at P1
  696. (or P2, virtural "fixed" address space).
  697. It's definitely should not in physical address. */
  698. asm volatile("ldc %0, vbr"
  699. : /* no output */
  700. : "r" (&vbr_base)
  701. : "memory");
  702. /* disable exception blocking now when the vbr has been setup */
  703. clear_bl_bit();
  704. }
  705. void *set_exception_table_vec(unsigned int vec, void *handler)
  706. {
  707. extern void *exception_handling_table[];
  708. void *old_handler;
  709. old_handler = exception_handling_table[vec];
  710. exception_handling_table[vec] = handler;
  711. return old_handler;
  712. }
  713. void __init trap_init(void)
  714. {
  715. set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
  716. set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
  717. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
  718. defined(CONFIG_SH_FPU_EMU)
  719. /*
  720. * For SH-4 lacking an FPU, treat floating point instructions as
  721. * reserved. They'll be handled in the math-emu case, or faulted on
  722. * otherwise.
  723. */
  724. set_exception_table_evt(0x800, do_reserved_inst);
  725. set_exception_table_evt(0x820, do_illegal_slot_inst);
  726. #elif defined(CONFIG_SH_FPU)
  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. #ifdef CONFIG_CPU_SH2
  731. set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
  732. #endif
  733. #ifdef CONFIG_CPU_SH2A
  734. set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
  735. set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
  736. #ifdef CONFIG_SH_FPU
  737. set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
  738. #endif
  739. #endif
  740. #ifdef TRAP_UBC
  741. set_exception_table_vec(TRAP_UBC, breakpoint_trap_handler);
  742. #endif
  743. }
  744. void show_stack(struct task_struct *tsk, unsigned long *sp)
  745. {
  746. unsigned long stack;
  747. if (!tsk)
  748. tsk = current;
  749. if (tsk == current)
  750. sp = (unsigned long *)current_stack_pointer;
  751. else
  752. sp = (unsigned long *)tsk->thread.sp;
  753. stack = (unsigned long)sp;
  754. dump_mem("Stack: ", stack, THREAD_SIZE +
  755. (unsigned long)task_stack_page(tsk));
  756. show_trace(tsk, sp, NULL);
  757. }
  758. void dump_stack(void)
  759. {
  760. show_stack(NULL, NULL);
  761. }
  762. EXPORT_SYMBOL(dump_stack);