traps.c 20 KB

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