traps_32.c 20 KB

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