traps_32.c 18 KB

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