traps.c 20 KB

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