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. sysfs_printk_last_file();
  80. print_modules();
  81. show_regs(regs);
  82. printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
  83. task_pid_nr(current), task_stack_page(current) + 1);
  84. if (!user_mode(regs) || in_interrupt())
  85. dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
  86. (unsigned long)task_stack_page(current));
  87. notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);
  88. bust_spinlocks(0);
  89. add_taint(TAINT_DIE);
  90. spin_unlock_irq(&die_lock);
  91. oops_exit();
  92. if (kexec_should_crash(current))
  93. crash_kexec(regs);
  94. if (in_interrupt())
  95. panic("Fatal exception in interrupt");
  96. if (panic_on_oops)
  97. panic("Fatal exception");
  98. do_exit(SIGSEGV);
  99. }
  100. static inline void die_if_kernel(const char *str, struct pt_regs *regs,
  101. long err)
  102. {
  103. if (!user_mode(regs))
  104. die(str, regs, err);
  105. }
  106. /*
  107. * try and fix up kernelspace address errors
  108. * - userspace errors just cause EFAULT to be returned, resulting in SEGV
  109. * - kernel/userspace interfaces cause a jump to an appropriate handler
  110. * - other kernel errors are bad
  111. */
  112. static void die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
  113. {
  114. if (!user_mode(regs)) {
  115. const struct exception_table_entry *fixup;
  116. fixup = search_exception_tables(regs->pc);
  117. if (fixup) {
  118. regs->pc = fixup->fixup;
  119. return;
  120. }
  121. die(str, regs, err);
  122. }
  123. }
  124. static inline void sign_extend(unsigned int count, unsigned char *dst)
  125. {
  126. #ifdef __LITTLE_ENDIAN__
  127. if ((count == 1) && dst[0] & 0x80) {
  128. dst[1] = 0xff;
  129. dst[2] = 0xff;
  130. dst[3] = 0xff;
  131. }
  132. if ((count == 2) && dst[1] & 0x80) {
  133. dst[2] = 0xff;
  134. dst[3] = 0xff;
  135. }
  136. #else
  137. if ((count == 1) && dst[3] & 0x80) {
  138. dst[2] = 0xff;
  139. dst[1] = 0xff;
  140. dst[0] = 0xff;
  141. }
  142. if ((count == 2) && dst[2] & 0x80) {
  143. dst[1] = 0xff;
  144. dst[0] = 0xff;
  145. }
  146. #endif
  147. }
  148. static struct mem_access user_mem_access = {
  149. copy_from_user,
  150. copy_to_user,
  151. };
  152. /*
  153. * handle an instruction that does an unaligned memory access by emulating the
  154. * desired behaviour
  155. * - note that PC _may not_ point to the faulting instruction
  156. * (if that instruction is in a branch delay slot)
  157. * - return 0 if emulation okay, -EFAULT on existential error
  158. */
  159. static int handle_unaligned_ins(insn_size_t instruction, struct pt_regs *regs,
  160. struct mem_access *ma)
  161. {
  162. int ret, index, count;
  163. unsigned long *rm, *rn;
  164. unsigned char *src, *dst;
  165. unsigned char __user *srcu, *dstu;
  166. index = (instruction>>8)&15; /* 0x0F00 */
  167. rn = &regs->regs[index];
  168. index = (instruction>>4)&15; /* 0x00F0 */
  169. rm = &regs->regs[index];
  170. count = 1<<(instruction&3);
  171. switch (count) {
  172. case 1: inc_unaligned_byte_access(); break;
  173. case 2: inc_unaligned_word_access(); break;
  174. case 4: inc_unaligned_dword_access(); break;
  175. case 8: inc_unaligned_multi_access(); break;
  176. }
  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. die_if_no_fixup("Fault in unaligned fixup", regs, 0);
  283. return -EFAULT;
  284. }
  285. /*
  286. * emulate the instruction in the delay slot
  287. * - fetches the instruction from PC+2
  288. */
  289. static inline int handle_delayslot(struct pt_regs *regs,
  290. insn_size_t old_instruction,
  291. struct mem_access *ma)
  292. {
  293. insn_size_t instruction;
  294. void __user *addr = (void __user *)(regs->pc +
  295. instruction_size(old_instruction));
  296. if (copy_from_user(&instruction, addr, sizeof(instruction))) {
  297. /* the instruction-fetch faulted */
  298. if (user_mode(regs))
  299. return -EFAULT;
  300. /* kernel */
  301. die("delay-slot-insn faulting in handle_unaligned_delayslot",
  302. regs, 0);
  303. }
  304. return handle_unaligned_ins(instruction, regs, ma);
  305. }
  306. /*
  307. * handle an instruction that does an unaligned memory access
  308. * - have to be careful of branch delay-slot instructions that fault
  309. * SH3:
  310. * - if the branch would be taken PC points to the branch
  311. * - if the branch would not be taken, PC points to delay-slot
  312. * SH4:
  313. * - PC always points to delayed branch
  314. * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
  315. */
  316. /* Macros to determine offset from current PC for branch instructions */
  317. /* Explicit type coercion is used to force sign extension where needed */
  318. #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
  319. #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
  320. int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
  321. struct mem_access *ma, int expected,
  322. unsigned long address)
  323. {
  324. u_int rm;
  325. int ret, index;
  326. /*
  327. * XXX: We can't handle mixed 16/32-bit instructions yet
  328. */
  329. if (instruction_size(instruction) != 2)
  330. return -EINVAL;
  331. index = (instruction>>8)&15; /* 0x0F00 */
  332. rm = regs->regs[index];
  333. /*
  334. * Log the unexpected fixups, and then pass them on to perf.
  335. *
  336. * We intentionally don't report the expected cases to perf as
  337. * otherwise the trapped I/O case will skew the results too much
  338. * to be useful.
  339. */
  340. if (!expected) {
  341. unaligned_fixups_notify(current, instruction, regs);
  342. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, 0,
  343. regs, address);
  344. }
  345. ret = -EFAULT;
  346. switch (instruction&0xF000) {
  347. case 0x0000:
  348. if (instruction==0x000B) {
  349. /* rts */
  350. ret = handle_delayslot(regs, instruction, ma);
  351. if (ret==0)
  352. regs->pc = regs->pr;
  353. }
  354. else if ((instruction&0x00FF)==0x0023) {
  355. /* braf @Rm */
  356. ret = handle_delayslot(regs, instruction, ma);
  357. if (ret==0)
  358. regs->pc += rm + 4;
  359. }
  360. else if ((instruction&0x00FF)==0x0003) {
  361. /* bsrf @Rm */
  362. ret = handle_delayslot(regs, instruction, ma);
  363. if (ret==0) {
  364. regs->pr = regs->pc + 4;
  365. regs->pc += rm + 4;
  366. }
  367. }
  368. else {
  369. /* mov.[bwl] to/from memory via r0+rn */
  370. goto simple;
  371. }
  372. break;
  373. case 0x1000: /* mov.l Rm,@(disp,Rn) */
  374. goto simple;
  375. case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
  376. goto simple;
  377. case 0x4000:
  378. if ((instruction&0x00FF)==0x002B) {
  379. /* jmp @Rm */
  380. ret = handle_delayslot(regs, instruction, ma);
  381. if (ret==0)
  382. regs->pc = rm;
  383. }
  384. else if ((instruction&0x00FF)==0x000B) {
  385. /* jsr @Rm */
  386. ret = handle_delayslot(regs, instruction, ma);
  387. if (ret==0) {
  388. regs->pr = regs->pc + 4;
  389. regs->pc = rm;
  390. }
  391. }
  392. else {
  393. /* mov.[bwl] to/from memory via r0+rn */
  394. goto simple;
  395. }
  396. break;
  397. case 0x5000: /* mov.l @(disp,Rm),Rn */
  398. goto simple;
  399. case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
  400. goto simple;
  401. case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
  402. switch (instruction&0x0F00) {
  403. case 0x0100: /* mov.w R0,@(disp,Rm) */
  404. goto simple;
  405. case 0x0500: /* mov.w @(disp,Rm),R0 */
  406. goto simple;
  407. case 0x0B00: /* bf lab - no delayslot*/
  408. break;
  409. case 0x0F00: /* bf/s lab */
  410. ret = handle_delayslot(regs, instruction, ma);
  411. if (ret==0) {
  412. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  413. if ((regs->sr & 0x00000001) != 0)
  414. regs->pc += 4; /* next after slot */
  415. else
  416. #endif
  417. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  418. }
  419. break;
  420. case 0x0900: /* bt lab - no delayslot */
  421. break;
  422. case 0x0D00: /* bt/s lab */
  423. ret = handle_delayslot(regs, instruction, ma);
  424. if (ret==0) {
  425. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  426. if ((regs->sr & 0x00000001) == 0)
  427. regs->pc += 4; /* next after slot */
  428. else
  429. #endif
  430. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  431. }
  432. break;
  433. }
  434. break;
  435. case 0xA000: /* bra label */
  436. ret = handle_delayslot(regs, instruction, ma);
  437. if (ret==0)
  438. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  439. break;
  440. case 0xB000: /* bsr label */
  441. ret = handle_delayslot(regs, instruction, ma);
  442. if (ret==0) {
  443. regs->pr = regs->pc + 4;
  444. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  445. }
  446. break;
  447. }
  448. return ret;
  449. /* handle non-delay-slot instruction */
  450. simple:
  451. ret = handle_unaligned_ins(instruction, regs, ma);
  452. if (ret==0)
  453. regs->pc += instruction_size(instruction);
  454. return ret;
  455. }
  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. insn_size_t instruction;
  475. int tmp;
  476. /* Intentional ifdef */
  477. #ifdef CONFIG_CPU_HAS_SR_RB
  478. error_code = lookup_exception_vector();
  479. #endif
  480. oldfs = get_fs();
  481. if (user_mode(regs)) {
  482. int si_code = BUS_ADRERR;
  483. unsigned int user_action;
  484. local_irq_enable();
  485. inc_unaligned_user_access();
  486. set_fs(USER_DS);
  487. if (copy_from_user(&instruction, (insn_size_t *)(regs->pc & ~1),
  488. sizeof(instruction))) {
  489. set_fs(oldfs);
  490. goto uspace_segv;
  491. }
  492. set_fs(oldfs);
  493. /* shout about userspace fixups */
  494. unaligned_fixups_notify(current, instruction, regs);
  495. user_action = unaligned_user_action();
  496. if (user_action & UM_FIXUP)
  497. goto fixup;
  498. if (user_action & UM_SIGNAL)
  499. goto uspace_segv;
  500. else {
  501. /* ignore */
  502. regs->pc += instruction_size(instruction);
  503. return;
  504. }
  505. fixup:
  506. /* bad PC is not something we can fix */
  507. if (regs->pc & 1) {
  508. si_code = BUS_ADRALN;
  509. goto uspace_segv;
  510. }
  511. set_fs(USER_DS);
  512. tmp = handle_unaligned_access(instruction, regs,
  513. &user_mem_access, 0,
  514. address);
  515. set_fs(oldfs);
  516. if (tmp == 0)
  517. return; /* sorted */
  518. uspace_segv:
  519. printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
  520. "access (PC %lx PR %lx)\n", current->comm, regs->pc,
  521. regs->pr);
  522. info.si_signo = SIGBUS;
  523. info.si_errno = 0;
  524. info.si_code = si_code;
  525. info.si_addr = (void __user *)address;
  526. force_sig_info(SIGBUS, &info, current);
  527. } else {
  528. inc_unaligned_kernel_access();
  529. if (regs->pc & 1)
  530. die("unaligned program counter", regs, error_code);
  531. set_fs(KERNEL_DS);
  532. if (copy_from_user(&instruction, (void __user *)(regs->pc),
  533. sizeof(instruction))) {
  534. /* Argh. Fault on the instruction itself.
  535. This should never happen non-SMP
  536. */
  537. set_fs(oldfs);
  538. die("insn faulting in do_address_error", regs, 0);
  539. }
  540. unaligned_fixups_notify(current, instruction, regs);
  541. handle_unaligned_access(instruction, regs, &user_mem_access,
  542. 0, address);
  543. set_fs(oldfs);
  544. }
  545. }
  546. #ifdef CONFIG_SH_DSP
  547. /*
  548. * SH-DSP support gerg@snapgear.com.
  549. */
  550. int is_dsp_inst(struct pt_regs *regs)
  551. {
  552. unsigned short inst = 0;
  553. /*
  554. * Safe guard if DSP mode is already enabled or we're lacking
  555. * the DSP altogether.
  556. */
  557. if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
  558. return 0;
  559. get_user(inst, ((unsigned short *) regs->pc));
  560. inst &= 0xf000;
  561. /* Check for any type of DSP or support instruction */
  562. if ((inst == 0xf000) || (inst == 0x4000))
  563. return 1;
  564. return 0;
  565. }
  566. #else
  567. #define is_dsp_inst(regs) (0)
  568. #endif /* CONFIG_SH_DSP */
  569. #ifdef CONFIG_CPU_SH2A
  570. asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
  571. unsigned long r6, unsigned long r7,
  572. struct pt_regs __regs)
  573. {
  574. siginfo_t info;
  575. switch (r4) {
  576. case TRAP_DIVZERO_ERROR:
  577. info.si_code = FPE_INTDIV;
  578. break;
  579. case TRAP_DIVOVF_ERROR:
  580. info.si_code = FPE_INTOVF;
  581. break;
  582. }
  583. force_sig_info(SIGFPE, &info, current);
  584. }
  585. #endif
  586. asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
  587. unsigned long r6, unsigned long r7,
  588. struct pt_regs __regs)
  589. {
  590. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  591. unsigned long error_code;
  592. struct task_struct *tsk = current;
  593. #ifdef CONFIG_SH_FPU_EMU
  594. unsigned short inst = 0;
  595. int err;
  596. get_user(inst, (unsigned short*)regs->pc);
  597. err = do_fpu_inst(inst, regs);
  598. if (!err) {
  599. regs->pc += instruction_size(inst);
  600. return;
  601. }
  602. /* not a FPU inst. */
  603. #endif
  604. #ifdef CONFIG_SH_DSP
  605. /* Check if it's a DSP instruction */
  606. if (is_dsp_inst(regs)) {
  607. /* Enable DSP mode, and restart instruction. */
  608. regs->sr |= SR_DSP;
  609. /* Save DSP mode */
  610. tsk->thread.dsp_status.status |= SR_DSP;
  611. return;
  612. }
  613. #endif
  614. error_code = lookup_exception_vector();
  615. local_irq_enable();
  616. force_sig(SIGILL, tsk);
  617. die_if_no_fixup("reserved instruction", regs, error_code);
  618. }
  619. #ifdef CONFIG_SH_FPU_EMU
  620. static int emulate_branch(unsigned short inst, struct pt_regs *regs)
  621. {
  622. /*
  623. * bfs: 8fxx: PC+=d*2+4;
  624. * bts: 8dxx: PC+=d*2+4;
  625. * bra: axxx: PC+=D*2+4;
  626. * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
  627. * braf:0x23: PC+=Rn*2+4;
  628. * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
  629. * jmp: 4x2b: PC=Rn;
  630. * jsr: 4x0b: PC=Rn after PR=PC+4;
  631. * rts: 000b: PC=PR;
  632. */
  633. if (((inst & 0xf000) == 0xb000) || /* bsr */
  634. ((inst & 0xf0ff) == 0x0003) || /* bsrf */
  635. ((inst & 0xf0ff) == 0x400b)) /* jsr */
  636. regs->pr = regs->pc + 4;
  637. if ((inst & 0xfd00) == 0x8d00) { /* bfs, bts */
  638. regs->pc += SH_PC_8BIT_OFFSET(inst);
  639. return 0;
  640. }
  641. if ((inst & 0xe000) == 0xa000) { /* bra, bsr */
  642. regs->pc += SH_PC_12BIT_OFFSET(inst);
  643. return 0;
  644. }
  645. if ((inst & 0xf0df) == 0x0003) { /* braf, bsrf */
  646. regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
  647. return 0;
  648. }
  649. if ((inst & 0xf0df) == 0x400b) { /* jmp, jsr */
  650. regs->pc = regs->regs[(inst & 0x0f00) >> 8];
  651. return 0;
  652. }
  653. if ((inst & 0xffff) == 0x000b) { /* rts */
  654. regs->pc = regs->pr;
  655. return 0;
  656. }
  657. return 1;
  658. }
  659. #endif
  660. asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
  661. unsigned long r6, unsigned long r7,
  662. struct pt_regs __regs)
  663. {
  664. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  665. unsigned long inst;
  666. struct task_struct *tsk = current;
  667. if (kprobe_handle_illslot(regs->pc) == 0)
  668. return;
  669. #ifdef CONFIG_SH_FPU_EMU
  670. get_user(inst, (unsigned short *)regs->pc + 1);
  671. if (!do_fpu_inst(inst, regs)) {
  672. get_user(inst, (unsigned short *)regs->pc);
  673. if (!emulate_branch(inst, regs))
  674. return;
  675. /* fault in branch.*/
  676. }
  677. /* not a FPU inst. */
  678. #endif
  679. inst = lookup_exception_vector();
  680. local_irq_enable();
  681. force_sig(SIGILL, tsk);
  682. die_if_no_fixup("illegal slot instruction", regs, inst);
  683. }
  684. asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
  685. unsigned long r6, unsigned long r7,
  686. struct pt_regs __regs)
  687. {
  688. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  689. long ex;
  690. ex = lookup_exception_vector();
  691. die_if_kernel("exception", regs, ex);
  692. }
  693. void __cpuinit per_cpu_trap_init(void)
  694. {
  695. extern void *vbr_base;
  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. /* disable exception blocking now when the vbr has been setup */
  704. clear_bl_bit();
  705. }
  706. void *set_exception_table_vec(unsigned int vec, void *handler)
  707. {
  708. extern void *exception_handling_table[];
  709. void *old_handler;
  710. old_handler = exception_handling_table[vec];
  711. exception_handling_table[vec] = handler;
  712. return old_handler;
  713. }
  714. void __init trap_init(void)
  715. {
  716. set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
  717. set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
  718. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
  719. defined(CONFIG_SH_FPU_EMU)
  720. /*
  721. * For SH-4 lacking an FPU, treat floating point instructions as
  722. * reserved. They'll be handled in the math-emu case, or faulted on
  723. * otherwise.
  724. */
  725. set_exception_table_evt(0x800, do_reserved_inst);
  726. set_exception_table_evt(0x820, do_illegal_slot_inst);
  727. #elif defined(CONFIG_SH_FPU)
  728. set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
  729. set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
  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. #ifdef TRAP_UBC
  742. set_exception_table_vec(TRAP_UBC, breakpoint_trap_handler);
  743. #endif
  744. }
  745. void show_stack(struct task_struct *tsk, unsigned long *sp)
  746. {
  747. unsigned long stack;
  748. if (!tsk)
  749. tsk = current;
  750. if (tsk == current)
  751. sp = (unsigned long *)current_stack_pointer;
  752. else
  753. sp = (unsigned long *)tsk->thread.sp;
  754. stack = (unsigned long)sp;
  755. dump_mem("Stack: ", stack, THREAD_SIZE +
  756. (unsigned long)task_stack_page(tsk));
  757. show_trace(tsk, sp, NULL);
  758. }
  759. void dump_stack(void)
  760. {
  761. show_stack(NULL, NULL);
  762. }
  763. EXPORT_SYMBOL(dump_stack);