traps_32.c 18 KB

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