traps_32.c 21 KB

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