traps_32.c 21 KB

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