traps_32.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * 'traps.c' handles hardware traps and faults after we have saved some
  4. * state in 'entry.S'.
  5. *
  6. * SuperH version: Copyright (C) 1999 Niibe Yutaka
  7. * Copyright (C) 2000 Philipp Rumpf
  8. * Copyright (C) 2000 David Howells
  9. * Copyright (C) 2002 - 2010 Paul Mundt
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/init.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/io.h>
  18. #include <linux/bug.h>
  19. #include <linux/debug_locks.h>
  20. #include <linux/kdebug.h>
  21. #include <linux/limits.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/perf_event.h>
  25. #include <linux/sched/task_stack.h>
  26. #include <asm/alignment.h>
  27. #include <asm/fpu.h>
  28. #include <asm/kprobes.h>
  29. #include <asm/setup.h>
  30. #include <asm/traps.h>
  31. #include <asm/bl_bit.h>
  32. #ifdef CONFIG_CPU_SH2
  33. # define TRAP_RESERVED_INST 4
  34. # define TRAP_ILLEGAL_SLOT_INST 6
  35. # define TRAP_ADDRESS_ERROR 9
  36. # ifdef CONFIG_CPU_SH2A
  37. # define TRAP_UBC 12
  38. # define TRAP_FPU_ERROR 13
  39. # define TRAP_DIVZERO_ERROR 17
  40. # define TRAP_DIVOVF_ERROR 18
  41. # endif
  42. #else
  43. #define TRAP_RESERVED_INST 12
  44. #define TRAP_ILLEGAL_SLOT_INST 13
  45. #endif
  46. static inline void sign_extend(unsigned int count, unsigned char *dst)
  47. {
  48. #ifdef __LITTLE_ENDIAN__
  49. if ((count == 1) && dst[0] & 0x80) {
  50. dst[1] = 0xff;
  51. dst[2] = 0xff;
  52. dst[3] = 0xff;
  53. }
  54. if ((count == 2) && dst[1] & 0x80) {
  55. dst[2] = 0xff;
  56. dst[3] = 0xff;
  57. }
  58. #else
  59. if ((count == 1) && dst[3] & 0x80) {
  60. dst[2] = 0xff;
  61. dst[1] = 0xff;
  62. dst[0] = 0xff;
  63. }
  64. if ((count == 2) && dst[2] & 0x80) {
  65. dst[1] = 0xff;
  66. dst[0] = 0xff;
  67. }
  68. #endif
  69. }
  70. static struct mem_access user_mem_access = {
  71. copy_from_user,
  72. copy_to_user,
  73. };
  74. static unsigned long copy_from_kernel_wrapper(void *dst, const void __user *src,
  75. unsigned long cnt)
  76. {
  77. return copy_from_kernel_nofault(dst, (const void __force *)src, cnt);
  78. }
  79. static unsigned long copy_to_kernel_wrapper(void __user *dst, const void *src,
  80. unsigned long cnt)
  81. {
  82. return copy_to_kernel_nofault((void __force *)dst, src, cnt);
  83. }
  84. static struct mem_access kernel_mem_access = {
  85. copy_from_kernel_wrapper,
  86. copy_to_kernel_wrapper,
  87. };
  88. /*
  89. * handle an instruction that does an unaligned memory access by emulating the
  90. * desired behaviour
  91. * - note that PC _may not_ point to the faulting instruction
  92. * (if that instruction is in a branch delay slot)
  93. * - return 0 if emulation okay, -EFAULT on existential error
  94. */
  95. static int handle_unaligned_ins(insn_size_t instruction, struct pt_regs *regs,
  96. struct mem_access *ma)
  97. {
  98. int ret, index, count;
  99. unsigned long *rm, *rn;
  100. unsigned char *src, *dst;
  101. unsigned char __user *srcu, *dstu;
  102. index = (instruction>>8)&15; /* 0x0F00 */
  103. rn = &regs->regs[index];
  104. index = (instruction>>4)&15; /* 0x00F0 */
  105. rm = &regs->regs[index];
  106. count = 1<<(instruction&3);
  107. switch (count) {
  108. case 1: inc_unaligned_byte_access(); break;
  109. case 2: inc_unaligned_word_access(); break;
  110. case 4: inc_unaligned_dword_access(); break;
  111. case 8: inc_unaligned_multi_access(); break;
  112. }
  113. ret = -EFAULT;
  114. switch (instruction>>12) {
  115. case 0: /* mov.[bwl] to/from memory via r0+rn */
  116. if (instruction & 8) {
  117. /* from memory */
  118. srcu = (unsigned char __user *)*rm;
  119. srcu += regs->regs[0];
  120. dst = (unsigned char *)rn;
  121. *(unsigned long *)dst = 0;
  122. #if !defined(__LITTLE_ENDIAN__)
  123. dst += 4-count;
  124. #endif
  125. if (ma->from(dst, srcu, count))
  126. goto fetch_fault;
  127. sign_extend(count, dst);
  128. } else {
  129. /* to memory */
  130. src = (unsigned char *)rm;
  131. #if !defined(__LITTLE_ENDIAN__)
  132. src += 4-count;
  133. #endif
  134. dstu = (unsigned char __user *)*rn;
  135. dstu += regs->regs[0];
  136. if (ma->to(dstu, src, count))
  137. goto fetch_fault;
  138. }
  139. ret = 0;
  140. break;
  141. case 1: /* mov.l Rm,@(disp,Rn) */
  142. src = (unsigned char*) rm;
  143. dstu = (unsigned char __user *)*rn;
  144. dstu += (instruction&0x000F)<<2;
  145. if (ma->to(dstu, src, 4))
  146. goto fetch_fault;
  147. ret = 0;
  148. break;
  149. case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
  150. if (instruction & 4)
  151. *rn -= count;
  152. src = (unsigned char*) rm;
  153. dstu = (unsigned char __user *)*rn;
  154. #if !defined(__LITTLE_ENDIAN__)
  155. src += 4-count;
  156. #endif
  157. if (ma->to(dstu, src, count))
  158. goto fetch_fault;
  159. ret = 0;
  160. break;
  161. case 5: /* mov.l @(disp,Rm),Rn */
  162. srcu = (unsigned char __user *)*rm;
  163. srcu += (instruction & 0x000F) << 2;
  164. dst = (unsigned char *)rn;
  165. *(unsigned long *)dst = 0;
  166. if (ma->from(dst, srcu, 4))
  167. goto fetch_fault;
  168. ret = 0;
  169. break;
  170. case 6: /* mov.[bwl] from memory, possibly with post-increment */
  171. srcu = (unsigned char __user *)*rm;
  172. if (instruction & 4)
  173. *rm += count;
  174. dst = (unsigned char*) rn;
  175. *(unsigned long*)dst = 0;
  176. #if !defined(__LITTLE_ENDIAN__)
  177. dst += 4-count;
  178. #endif
  179. if (ma->from(dst, srcu, count))
  180. goto fetch_fault;
  181. sign_extend(count, dst);
  182. ret = 0;
  183. break;
  184. case 8:
  185. switch ((instruction&0xFF00)>>8) {
  186. case 0x81: /* mov.w R0,@(disp,Rn) */
  187. src = (unsigned char *) &regs->regs[0];
  188. #if !defined(__LITTLE_ENDIAN__)
  189. src += 2;
  190. #endif
  191. dstu = (unsigned char __user *)*rm; /* called Rn in the spec */
  192. dstu += (instruction & 0x000F) << 1;
  193. if (ma->to(dstu, src, 2))
  194. goto fetch_fault;
  195. ret = 0;
  196. break;
  197. case 0x85: /* mov.w @(disp,Rm),R0 */
  198. srcu = (unsigned char __user *)*rm;
  199. srcu += (instruction & 0x000F) << 1;
  200. dst = (unsigned char *) &regs->regs[0];
  201. *(unsigned long *)dst = 0;
  202. #if !defined(__LITTLE_ENDIAN__)
  203. dst += 2;
  204. #endif
  205. if (ma->from(dst, srcu, 2))
  206. goto fetch_fault;
  207. sign_extend(2, dst);
  208. ret = 0;
  209. break;
  210. }
  211. break;
  212. case 9: /* mov.w @(disp,PC),Rn */
  213. srcu = (unsigned char __user *)regs->pc;
  214. srcu += 4;
  215. srcu += (instruction & 0x00FF) << 1;
  216. dst = (unsigned char *)rn;
  217. *(unsigned long *)dst = 0;
  218. #if !defined(__LITTLE_ENDIAN__)
  219. dst += 2;
  220. #endif
  221. if (ma->from(dst, srcu, 2))
  222. goto fetch_fault;
  223. sign_extend(2, dst);
  224. ret = 0;
  225. break;
  226. case 0xd: /* mov.l @(disp,PC),Rn */
  227. srcu = (unsigned char __user *)(regs->pc & ~0x3);
  228. srcu += 4;
  229. srcu += (instruction & 0x00FF) << 2;
  230. dst = (unsigned char *)rn;
  231. *(unsigned long *)dst = 0;
  232. if (ma->from(dst, srcu, 4))
  233. goto fetch_fault;
  234. ret = 0;
  235. break;
  236. }
  237. return ret;
  238. fetch_fault:
  239. /* Argh. Address not only misaligned but also non-existent.
  240. * Raise an EFAULT and see if it's trapped
  241. */
  242. die_if_no_fixup("Fault in unaligned fixup", regs, 0);
  243. return -EFAULT;
  244. }
  245. /*
  246. * emulate the instruction in the delay slot
  247. * - fetches the instruction from PC+2
  248. */
  249. static inline int handle_delayslot(struct pt_regs *regs,
  250. insn_size_t old_instruction,
  251. struct mem_access *ma)
  252. {
  253. insn_size_t instruction;
  254. void __user *addr = (void __user *)(regs->pc +
  255. instruction_size(old_instruction));
  256. if (copy_from_user(&instruction, addr, sizeof(instruction))) {
  257. /* the instruction-fetch faulted */
  258. if (user_mode(regs))
  259. return -EFAULT;
  260. /* kernel */
  261. die("delay-slot-insn faulting in handle_unaligned_delayslot",
  262. regs, 0);
  263. }
  264. return handle_unaligned_ins(instruction, regs, ma);
  265. }
  266. /*
  267. * handle an instruction that does an unaligned memory access
  268. * - have to be careful of branch delay-slot instructions that fault
  269. * SH3:
  270. * - if the branch would be taken PC points to the branch
  271. * - if the branch would not be taken, PC points to delay-slot
  272. * SH4:
  273. * - PC always points to delayed branch
  274. * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
  275. */
  276. /* Macros to determine offset from current PC for branch instructions */
  277. /* Explicit type coercion is used to force sign extension where needed */
  278. #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
  279. #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
  280. int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
  281. struct mem_access *ma, int expected,
  282. unsigned long address)
  283. {
  284. u_int rm;
  285. int ret, index;
  286. /*
  287. * XXX: We can't handle mixed 16/32-bit instructions yet
  288. */
  289. if (instruction_size(instruction) != 2)
  290. return -EINVAL;
  291. index = (instruction>>8)&15; /* 0x0F00 */
  292. rm = regs->regs[index];
  293. /*
  294. * Log the unexpected fixups, and then pass them on to perf.
  295. *
  296. * We intentionally don't report the expected cases to perf as
  297. * otherwise the trapped I/O case will skew the results too much
  298. * to be useful.
  299. */
  300. if (!expected) {
  301. unaligned_fixups_notify(current, instruction, regs);
  302. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1,
  303. regs, address);
  304. }
  305. ret = -EFAULT;
  306. switch (instruction&0xF000) {
  307. case 0x0000:
  308. if (instruction==0x000B) {
  309. /* rts */
  310. ret = handle_delayslot(regs, instruction, ma);
  311. if (ret==0)
  312. regs->pc = regs->pr;
  313. }
  314. else if ((instruction&0x00FF)==0x0023) {
  315. /* braf @Rm */
  316. ret = handle_delayslot(regs, instruction, ma);
  317. if (ret==0)
  318. regs->pc += rm + 4;
  319. }
  320. else if ((instruction&0x00FF)==0x0003) {
  321. /* bsrf @Rm */
  322. ret = handle_delayslot(regs, instruction, ma);
  323. if (ret==0) {
  324. regs->pr = regs->pc + 4;
  325. regs->pc += rm + 4;
  326. }
  327. }
  328. else {
  329. /* mov.[bwl] to/from memory via r0+rn */
  330. goto simple;
  331. }
  332. break;
  333. case 0x1000: /* mov.l Rm,@(disp,Rn) */
  334. goto simple;
  335. case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
  336. goto simple;
  337. case 0x4000:
  338. if ((instruction&0x00FF)==0x002B) {
  339. /* jmp @Rm */
  340. ret = handle_delayslot(regs, instruction, ma);
  341. if (ret==0)
  342. regs->pc = rm;
  343. }
  344. else if ((instruction&0x00FF)==0x000B) {
  345. /* jsr @Rm */
  346. ret = handle_delayslot(regs, instruction, ma);
  347. if (ret==0) {
  348. regs->pr = regs->pc + 4;
  349. regs->pc = rm;
  350. }
  351. }
  352. else {
  353. /* mov.[bwl] to/from memory via r0+rn */
  354. goto simple;
  355. }
  356. break;
  357. case 0x5000: /* mov.l @(disp,Rm),Rn */
  358. goto simple;
  359. case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
  360. goto simple;
  361. case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
  362. switch (instruction&0x0F00) {
  363. case 0x0100: /* mov.w R0,@(disp,Rm) */
  364. goto simple;
  365. case 0x0500: /* mov.w @(disp,Rm),R0 */
  366. goto simple;
  367. case 0x0B00: /* bf lab - no delayslot*/
  368. ret = 0;
  369. break;
  370. case 0x0F00: /* bf/s lab */
  371. ret = handle_delayslot(regs, instruction, ma);
  372. if (ret==0) {
  373. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  374. if ((regs->sr & 0x00000001) != 0)
  375. regs->pc += 4; /* next after slot */
  376. else
  377. #endif
  378. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  379. }
  380. break;
  381. case 0x0900: /* bt lab - no delayslot */
  382. ret = 0;
  383. break;
  384. case 0x0D00: /* bt/s lab */
  385. ret = handle_delayslot(regs, instruction, ma);
  386. if (ret==0) {
  387. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  388. if ((regs->sr & 0x00000001) == 0)
  389. regs->pc += 4; /* next after slot */
  390. else
  391. #endif
  392. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  393. }
  394. break;
  395. }
  396. break;
  397. case 0x9000: /* mov.w @(disp,Rm),Rn */
  398. goto simple;
  399. case 0xA000: /* bra label */
  400. ret = handle_delayslot(regs, instruction, ma);
  401. if (ret==0)
  402. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  403. break;
  404. case 0xB000: /* bsr label */
  405. ret = handle_delayslot(regs, instruction, ma);
  406. if (ret==0) {
  407. regs->pr = regs->pc + 4;
  408. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  409. }
  410. break;
  411. case 0xD000: /* mov.l @(disp,Rm),Rn */
  412. goto simple;
  413. }
  414. return ret;
  415. /* handle non-delay-slot instruction */
  416. simple:
  417. ret = handle_unaligned_ins(instruction, regs, ma);
  418. if (ret==0)
  419. regs->pc += instruction_size(instruction);
  420. return ret;
  421. }
  422. /*
  423. * Handle various address error exceptions:
  424. * - instruction address error:
  425. * misaligned PC
  426. * PC >= 0x80000000 in user mode
  427. * - data address error (read and write)
  428. * misaligned data access
  429. * access to >= 0x80000000 is user mode
  430. * Unfortuntaly we can't distinguish between instruction address error
  431. * and data address errors caused by read accesses.
  432. */
  433. asmlinkage void do_address_error(struct pt_regs *regs,
  434. unsigned long writeaccess,
  435. unsigned long address)
  436. {
  437. unsigned long error_code = 0;
  438. insn_size_t instruction;
  439. int tmp;
  440. /* Intentional ifdef */
  441. #ifdef CONFIG_CPU_HAS_SR_RB
  442. error_code = lookup_exception_vector();
  443. #endif
  444. if (user_mode(regs)) {
  445. int si_code = BUS_ADRERR;
  446. unsigned int user_action;
  447. local_irq_enable();
  448. inc_unaligned_user_access();
  449. if (copy_from_user(&instruction, (insn_size_t __user *)(regs->pc & ~1),
  450. sizeof(instruction))) {
  451. goto uspace_segv;
  452. }
  453. /* shout about userspace fixups */
  454. unaligned_fixups_notify(current, instruction, regs);
  455. user_action = unaligned_user_action();
  456. if (user_action & UM_FIXUP)
  457. goto fixup;
  458. if (user_action & UM_SIGNAL)
  459. goto uspace_segv;
  460. else {
  461. /* ignore */
  462. regs->pc += instruction_size(instruction);
  463. return;
  464. }
  465. fixup:
  466. /* bad PC is not something we can fix */
  467. if (regs->pc & 1) {
  468. si_code = BUS_ADRALN;
  469. goto uspace_segv;
  470. }
  471. tmp = handle_unaligned_access(instruction, regs,
  472. &user_mem_access, 0,
  473. address);
  474. if (tmp == 0)
  475. return; /* sorted */
  476. uspace_segv:
  477. printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
  478. "access (PC %lx PR %lx)\n", current->comm, regs->pc,
  479. regs->pr);
  480. force_sig_fault(SIGBUS, si_code, (void __user *)address);
  481. } else {
  482. inc_unaligned_kernel_access();
  483. if (regs->pc & 1)
  484. die("unaligned program counter", regs, error_code);
  485. if (copy_from_kernel_nofault(&instruction, (void *)(regs->pc),
  486. sizeof(instruction))) {
  487. /* Argh. Fault on the instruction itself.
  488. This should never happen non-SMP
  489. */
  490. die("insn faulting in do_address_error", regs, 0);
  491. }
  492. unaligned_fixups_notify(current, instruction, regs);
  493. handle_unaligned_access(instruction, regs, &kernel_mem_access,
  494. 0, address);
  495. }
  496. }
  497. #ifdef CONFIG_SH_DSP
  498. /*
  499. * SH-DSP support gerg@snapgear.com.
  500. */
  501. static int is_dsp_inst(struct pt_regs *regs)
  502. {
  503. unsigned short inst = 0;
  504. /*
  505. * Safe guard if DSP mode is already enabled or we're lacking
  506. * the DSP altogether.
  507. */
  508. if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
  509. return 0;
  510. get_user(inst, ((unsigned short *) regs->pc));
  511. inst &= 0xf000;
  512. /* Check for any type of DSP or support instruction */
  513. if ((inst == 0xf000) || (inst == 0x4000))
  514. return 1;
  515. return 0;
  516. }
  517. #else
  518. static inline int is_dsp_inst(struct pt_regs *regs) { return 0; }
  519. #endif /* CONFIG_SH_DSP */
  520. #ifdef CONFIG_CPU_SH2A
  521. asmlinkage void do_divide_error(unsigned long r4)
  522. {
  523. int code;
  524. switch (r4) {
  525. case TRAP_DIVZERO_ERROR:
  526. code = FPE_INTDIV;
  527. break;
  528. case TRAP_DIVOVF_ERROR:
  529. code = FPE_INTOVF;
  530. break;
  531. default:
  532. /* Let gcc know unhandled cases don't make it past here */
  533. return;
  534. }
  535. force_sig_fault(SIGFPE, code, NULL);
  536. }
  537. #endif
  538. asmlinkage void do_reserved_inst(void)
  539. {
  540. struct pt_regs *regs = current_pt_regs();
  541. unsigned long error_code;
  542. #ifdef CONFIG_SH_FPU_EMU
  543. unsigned short inst = 0;
  544. int err;
  545. get_user(inst, (unsigned short __user *)regs->pc);
  546. err = do_fpu_inst(inst, regs);
  547. if (!err) {
  548. regs->pc += instruction_size(inst);
  549. return;
  550. }
  551. /* not a FPU inst. */
  552. #endif
  553. #ifdef CONFIG_SH_DSP
  554. /* Check if it's a DSP instruction */
  555. if (is_dsp_inst(regs)) {
  556. /* Enable DSP mode, and restart instruction. */
  557. regs->sr |= SR_DSP;
  558. /* Save DSP mode */
  559. current->thread.dsp_status.status |= SR_DSP;
  560. return;
  561. }
  562. #endif
  563. error_code = lookup_exception_vector();
  564. local_irq_enable();
  565. force_sig(SIGILL);
  566. die_if_no_fixup("reserved instruction", regs, error_code);
  567. }
  568. #ifdef CONFIG_SH_FPU_EMU
  569. static int emulate_branch(unsigned short inst, struct pt_regs *regs)
  570. {
  571. /*
  572. * bfs: 8fxx: PC+=d*2+4;
  573. * bts: 8dxx: PC+=d*2+4;
  574. * bra: axxx: PC+=D*2+4;
  575. * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
  576. * braf:0x23: PC+=Rn*2+4;
  577. * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
  578. * jmp: 4x2b: PC=Rn;
  579. * jsr: 4x0b: PC=Rn after PR=PC+4;
  580. * rts: 000b: PC=PR;
  581. */
  582. if (((inst & 0xf000) == 0xb000) || /* bsr */
  583. ((inst & 0xf0ff) == 0x0003) || /* bsrf */
  584. ((inst & 0xf0ff) == 0x400b)) /* jsr */
  585. regs->pr = regs->pc + 4;
  586. if ((inst & 0xfd00) == 0x8d00) { /* bfs, bts */
  587. regs->pc += SH_PC_8BIT_OFFSET(inst);
  588. return 0;
  589. }
  590. if ((inst & 0xe000) == 0xa000) { /* bra, bsr */
  591. regs->pc += SH_PC_12BIT_OFFSET(inst);
  592. return 0;
  593. }
  594. if ((inst & 0xf0df) == 0x0003) { /* braf, bsrf */
  595. regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
  596. return 0;
  597. }
  598. if ((inst & 0xf0df) == 0x400b) { /* jmp, jsr */
  599. regs->pc = regs->regs[(inst & 0x0f00) >> 8];
  600. return 0;
  601. }
  602. if ((inst & 0xffff) == 0x000b) { /* rts */
  603. regs->pc = regs->pr;
  604. return 0;
  605. }
  606. return 1;
  607. }
  608. #endif
  609. asmlinkage void do_illegal_slot_inst(void)
  610. {
  611. struct pt_regs *regs = current_pt_regs();
  612. unsigned long inst;
  613. if (kprobe_handle_illslot(regs->pc) == 0)
  614. return;
  615. #ifdef CONFIG_SH_FPU_EMU
  616. get_user(inst, (unsigned short __user *)regs->pc + 1);
  617. if (!do_fpu_inst(inst, regs)) {
  618. get_user(inst, (unsigned short __user *)regs->pc);
  619. if (!emulate_branch(inst, regs))
  620. return;
  621. /* fault in branch.*/
  622. }
  623. /* not a FPU inst. */
  624. #endif
  625. inst = lookup_exception_vector();
  626. local_irq_enable();
  627. force_sig(SIGILL);
  628. die_if_no_fixup("illegal slot instruction", regs, inst);
  629. }
  630. asmlinkage void do_exception_error(void)
  631. {
  632. long ex;
  633. ex = lookup_exception_vector();
  634. die_if_kernel("exception", current_pt_regs(), ex);
  635. }
  636. void per_cpu_trap_init(void)
  637. {
  638. extern void *vbr_base;
  639. /* NOTE: The VBR value should be at P1
  640. (or P2, virtural "fixed" address space).
  641. It's definitely should not in physical address. */
  642. asm volatile("ldc %0, vbr"
  643. : /* no output */
  644. : "r" (&vbr_base)
  645. : "memory");
  646. /* disable exception blocking now when the vbr has been setup */
  647. clear_bl_bit();
  648. }
  649. void *set_exception_table_vec(unsigned int vec, void *handler)
  650. {
  651. extern void *exception_handling_table[];
  652. void *old_handler;
  653. old_handler = exception_handling_table[vec];
  654. exception_handling_table[vec] = handler;
  655. return old_handler;
  656. }
  657. void __init trap_init(void)
  658. {
  659. set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
  660. set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
  661. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
  662. defined(CONFIG_SH_FPU_EMU)
  663. /*
  664. * For SH-4 lacking an FPU, treat floating point instructions as
  665. * reserved. They'll be handled in the math-emu case, or faulted on
  666. * otherwise.
  667. */
  668. set_exception_table_evt(0x800, do_reserved_inst);
  669. set_exception_table_evt(0x820, do_illegal_slot_inst);
  670. #elif defined(CONFIG_SH_FPU)
  671. set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
  672. set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
  673. #endif
  674. #ifdef CONFIG_CPU_SH2
  675. set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
  676. #endif
  677. #ifdef CONFIG_CPU_SH2A
  678. set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
  679. set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
  680. #ifdef CONFIG_SH_FPU
  681. set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
  682. #endif
  683. #endif
  684. #ifdef TRAP_UBC
  685. set_exception_table_vec(TRAP_UBC, breakpoint_trap_handler);
  686. #endif
  687. }