traps_32.c 21 KB

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