traps_32.c 21 KB

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