traps.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 <linux/debug_locks.h>
  22. #include <asm/system.h>
  23. #include <asm/uaccess.h>
  24. #ifdef CONFIG_SH_KGDB
  25. #include <asm/kgdb.h>
  26. #define CHK_REMOTE_DEBUG(regs) \
  27. { \
  28. if (kgdb_debug_hook && !user_mode(regs))\
  29. (*kgdb_debug_hook)(regs); \
  30. }
  31. #else
  32. #define CHK_REMOTE_DEBUG(regs)
  33. #endif
  34. #ifdef CONFIG_CPU_SH2
  35. # define TRAP_RESERVED_INST 4
  36. # define TRAP_ILLEGAL_SLOT_INST 6
  37. # define TRAP_ADDRESS_ERROR 9
  38. # ifdef CONFIG_CPU_SH2A
  39. # define TRAP_DIVZERO_ERROR 17
  40. # define TRAP_DIVOVF_ERROR 18
  41. # endif
  42. #else
  43. #define TRAP_RESERVED_INST 12
  44. #define TRAP_ILLEGAL_SLOT_INST 13
  45. #endif
  46. static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
  47. {
  48. unsigned long p;
  49. int i;
  50. printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
  51. for (p = bottom & ~31; p < top; ) {
  52. printk("%04lx: ", p & 0xffff);
  53. for (i = 0; i < 8; i++, p += 4) {
  54. unsigned int val;
  55. if (p < bottom || p >= top)
  56. printk(" ");
  57. else {
  58. if (__get_user(val, (unsigned int __user *)p)) {
  59. printk("\n");
  60. return;
  61. }
  62. printk("%08x ", val);
  63. }
  64. }
  65. printk("\n");
  66. }
  67. }
  68. DEFINE_SPINLOCK(die_lock);
  69. void die(const char * str, struct pt_regs * regs, long err)
  70. {
  71. static int die_counter;
  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. CHK_REMOTE_DEBUG(regs);
  77. print_modules();
  78. show_regs(regs);
  79. printk("Process: %s (pid: %d, stack limit = %p)\n",
  80. current->comm, current->pid, task_stack_page(current) + 1);
  81. if (!user_mode(regs) || in_interrupt())
  82. dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
  83. (unsigned long)task_stack_page(current));
  84. bust_spinlocks(0);
  85. spin_unlock_irq(&die_lock);
  86. do_exit(SIGSEGV);
  87. }
  88. static inline void die_if_kernel(const char *str, struct pt_regs *regs,
  89. long err)
  90. {
  91. if (!user_mode(regs))
  92. die(str, regs, err);
  93. }
  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",
  281. regs, 0);
  282. }
  283. return handle_unaligned_ins(instruction,regs);
  284. }
  285. /*
  286. * handle an instruction that does an unaligned memory access
  287. * - have to be careful of branch delay-slot instructions that fault
  288. * SH3:
  289. * - if the branch would be taken PC points to the branch
  290. * - if the branch would not be taken, PC points to delay-slot
  291. * SH4:
  292. * - PC always points to delayed branch
  293. * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
  294. */
  295. /* Macros to determine offset from current PC for branch instructions */
  296. /* Explicit type coercion is used to force sign extension where needed */
  297. #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
  298. #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
  299. /*
  300. * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
  301. * opcodes..
  302. */
  303. #ifndef CONFIG_CPU_SH2A
  304. static int handle_unaligned_notify_count = 10;
  305. static int handle_unaligned_access(u16 instruction, struct pt_regs *regs)
  306. {
  307. u_int rm;
  308. int ret, index;
  309. index = (instruction>>8)&15; /* 0x0F00 */
  310. rm = regs->regs[index];
  311. /* shout about the first ten userspace fixups */
  312. if (user_mode(regs) && handle_unaligned_notify_count>0) {
  313. handle_unaligned_notify_count--;
  314. printk(KERN_NOTICE "Fixing up unaligned userspace access "
  315. "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
  316. current->comm,current->pid,(u16*)regs->pc,instruction);
  317. }
  318. ret = -EFAULT;
  319. switch (instruction&0xF000) {
  320. case 0x0000:
  321. if (instruction==0x000B) {
  322. /* rts */
  323. ret = handle_unaligned_delayslot(regs);
  324. if (ret==0)
  325. regs->pc = regs->pr;
  326. }
  327. else if ((instruction&0x00FF)==0x0023) {
  328. /* braf @Rm */
  329. ret = handle_unaligned_delayslot(regs);
  330. if (ret==0)
  331. regs->pc += rm + 4;
  332. }
  333. else if ((instruction&0x00FF)==0x0003) {
  334. /* bsrf @Rm */
  335. ret = handle_unaligned_delayslot(regs);
  336. if (ret==0) {
  337. regs->pr = regs->pc + 4;
  338. regs->pc += rm + 4;
  339. }
  340. }
  341. else {
  342. /* mov.[bwl] to/from memory via r0+rn */
  343. goto simple;
  344. }
  345. break;
  346. case 0x1000: /* mov.l Rm,@(disp,Rn) */
  347. goto simple;
  348. case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
  349. goto simple;
  350. case 0x4000:
  351. if ((instruction&0x00FF)==0x002B) {
  352. /* jmp @Rm */
  353. ret = handle_unaligned_delayslot(regs);
  354. if (ret==0)
  355. regs->pc = rm;
  356. }
  357. else if ((instruction&0x00FF)==0x000B) {
  358. /* jsr @Rm */
  359. ret = handle_unaligned_delayslot(regs);
  360. if (ret==0) {
  361. regs->pr = regs->pc + 4;
  362. regs->pc = rm;
  363. }
  364. }
  365. else {
  366. /* mov.[bwl] to/from memory via r0+rn */
  367. goto simple;
  368. }
  369. break;
  370. case 0x5000: /* mov.l @(disp,Rm),Rn */
  371. goto simple;
  372. case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
  373. goto simple;
  374. case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
  375. switch (instruction&0x0F00) {
  376. case 0x0100: /* mov.w R0,@(disp,Rm) */
  377. goto simple;
  378. case 0x0500: /* mov.w @(disp,Rm),R0 */
  379. goto simple;
  380. case 0x0B00: /* bf lab - no delayslot*/
  381. break;
  382. case 0x0F00: /* bf/s lab */
  383. ret = handle_unaligned_delayslot(regs);
  384. if (ret==0) {
  385. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  386. if ((regs->sr & 0x00000001) != 0)
  387. regs->pc += 4; /* next after slot */
  388. else
  389. #endif
  390. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  391. }
  392. break;
  393. case 0x0900: /* bt lab - no delayslot */
  394. break;
  395. case 0x0D00: /* bt/s lab */
  396. ret = handle_unaligned_delayslot(regs);
  397. if (ret==0) {
  398. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  399. if ((regs->sr & 0x00000001) == 0)
  400. regs->pc += 4; /* next after slot */
  401. else
  402. #endif
  403. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  404. }
  405. break;
  406. }
  407. break;
  408. case 0xA000: /* bra label */
  409. ret = handle_unaligned_delayslot(regs);
  410. if (ret==0)
  411. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  412. break;
  413. case 0xB000: /* bsr label */
  414. ret = handle_unaligned_delayslot(regs);
  415. if (ret==0) {
  416. regs->pr = regs->pc + 4;
  417. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  418. }
  419. break;
  420. }
  421. return ret;
  422. /* handle non-delay-slot instruction */
  423. simple:
  424. ret = handle_unaligned_ins(instruction,regs);
  425. if (ret==0)
  426. regs->pc += 2;
  427. return ret;
  428. }
  429. #endif /* CONFIG_CPU_SH2A */
  430. #ifdef CONFIG_CPU_HAS_SR_RB
  431. #define lookup_exception_vector(x) \
  432. __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
  433. #else
  434. #define lookup_exception_vector(x) \
  435. __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
  436. #endif
  437. /*
  438. * Handle various address error exceptions:
  439. * - instruction address error:
  440. * misaligned PC
  441. * PC >= 0x80000000 in user mode
  442. * - data address error (read and write)
  443. * misaligned data access
  444. * access to >= 0x80000000 is user mode
  445. * Unfortuntaly we can't distinguish between instruction address error
  446. * and data address errors caused by read acceses.
  447. */
  448. asmlinkage void do_address_error(struct pt_regs *regs,
  449. unsigned long writeaccess,
  450. unsigned long address)
  451. {
  452. unsigned long error_code = 0;
  453. mm_segment_t oldfs;
  454. siginfo_t info;
  455. #ifndef CONFIG_CPU_SH2A
  456. u16 instruction;
  457. int tmp;
  458. #endif
  459. /* Intentional ifdef */
  460. #ifdef CONFIG_CPU_HAS_SR_RB
  461. lookup_exception_vector(error_code);
  462. #endif
  463. oldfs = get_fs();
  464. if (user_mode(regs)) {
  465. int si_code = BUS_ADRERR;
  466. local_irq_enable();
  467. /* bad PC is not something we can fix */
  468. if (regs->pc & 1) {
  469. si_code = BUS_ADRALN;
  470. goto uspace_segv;
  471. }
  472. #ifndef CONFIG_CPU_SH2A
  473. set_fs(USER_DS);
  474. if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
  475. /* Argh. Fault on the instruction itself.
  476. This should never happen non-SMP
  477. */
  478. set_fs(oldfs);
  479. goto uspace_segv;
  480. }
  481. tmp = handle_unaligned_access(instruction, regs);
  482. set_fs(oldfs);
  483. if (tmp==0)
  484. return; /* sorted */
  485. #endif
  486. uspace_segv:
  487. printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
  488. "access (PC %lx PR %lx)\n", current->comm, regs->pc,
  489. regs->pr);
  490. info.si_signo = SIGBUS;
  491. info.si_errno = 0;
  492. info.si_code = si_code;
  493. info.si_addr = (void *) address;
  494. force_sig_info(SIGBUS, &info, current);
  495. } else {
  496. if (regs->pc & 1)
  497. die("unaligned program counter", regs, error_code);
  498. #ifndef CONFIG_CPU_SH2A
  499. set_fs(KERNEL_DS);
  500. if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
  501. /* Argh. Fault on the instruction itself.
  502. This should never happen non-SMP
  503. */
  504. set_fs(oldfs);
  505. die("insn faulting in do_address_error", regs, 0);
  506. }
  507. handle_unaligned_access(instruction, regs);
  508. set_fs(oldfs);
  509. #else
  510. printk(KERN_NOTICE "Killing process \"%s\" due to unaligned "
  511. "access\n", current->comm);
  512. force_sig(SIGSEGV, current);
  513. #endif
  514. }
  515. }
  516. #ifdef CONFIG_SH_DSP
  517. /*
  518. * SH-DSP support gerg@snapgear.com.
  519. */
  520. int is_dsp_inst(struct pt_regs *regs)
  521. {
  522. unsigned short inst;
  523. /*
  524. * Safe guard if DSP mode is already enabled or we're lacking
  525. * the DSP altogether.
  526. */
  527. if (!(cpu_data->flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
  528. return 0;
  529. get_user(inst, ((unsigned short *) regs->pc));
  530. inst &= 0xf000;
  531. /* Check for any type of DSP or support instruction */
  532. if ((inst == 0xf000) || (inst == 0x4000))
  533. return 1;
  534. return 0;
  535. }
  536. #else
  537. #define is_dsp_inst(regs) (0)
  538. #endif /* CONFIG_SH_DSP */
  539. #ifdef CONFIG_CPU_SH2A
  540. asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
  541. unsigned long r6, unsigned long r7,
  542. struct pt_regs __regs)
  543. {
  544. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  545. siginfo_t info;
  546. switch (r4) {
  547. case TRAP_DIVZERO_ERROR:
  548. info.si_code = FPE_INTDIV;
  549. break;
  550. case TRAP_DIVOVF_ERROR:
  551. info.si_code = FPE_INTOVF;
  552. break;
  553. }
  554. force_sig_info(SIGFPE, &info, current);
  555. }
  556. #endif
  557. /* arch/sh/kernel/cpu/sh4/fpu.c */
  558. extern int do_fpu_inst(unsigned short, struct pt_regs *);
  559. extern asmlinkage void do_fpu_state_restore(unsigned long r4, unsigned long r5,
  560. unsigned long r6, unsigned long r7, struct pt_regs __regs);
  561. asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
  562. unsigned long r6, unsigned long r7,
  563. struct pt_regs __regs)
  564. {
  565. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  566. unsigned long error_code;
  567. struct task_struct *tsk = current;
  568. #ifdef CONFIG_SH_FPU_EMU
  569. unsigned short inst = 0;
  570. int err;
  571. get_user(inst, (unsigned short*)regs->pc);
  572. err = do_fpu_inst(inst, regs);
  573. if (!err) {
  574. regs->pc += 2;
  575. return;
  576. }
  577. /* not a FPU inst. */
  578. #endif
  579. #ifdef CONFIG_SH_DSP
  580. /* Check if it's a DSP instruction */
  581. if (is_dsp_inst(regs)) {
  582. /* Enable DSP mode, and restart instruction. */
  583. regs->sr |= SR_DSP;
  584. return;
  585. }
  586. #endif
  587. lookup_exception_vector(error_code);
  588. local_irq_enable();
  589. CHK_REMOTE_DEBUG(regs);
  590. force_sig(SIGILL, tsk);
  591. die_if_no_fixup("reserved instruction", regs, error_code);
  592. }
  593. #ifdef CONFIG_SH_FPU_EMU
  594. static int emulate_branch(unsigned short inst, struct pt_regs* regs)
  595. {
  596. /*
  597. * bfs: 8fxx: PC+=d*2+4;
  598. * bts: 8dxx: PC+=d*2+4;
  599. * bra: axxx: PC+=D*2+4;
  600. * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
  601. * braf:0x23: PC+=Rn*2+4;
  602. * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
  603. * jmp: 4x2b: PC=Rn;
  604. * jsr: 4x0b: PC=Rn after PR=PC+4;
  605. * rts: 000b: PC=PR;
  606. */
  607. if ((inst & 0xfd00) == 0x8d00) {
  608. regs->pc += SH_PC_8BIT_OFFSET(inst);
  609. return 0;
  610. }
  611. if ((inst & 0xe000) == 0xa000) {
  612. regs->pc += SH_PC_12BIT_OFFSET(inst);
  613. return 0;
  614. }
  615. if ((inst & 0xf0df) == 0x0003) {
  616. regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
  617. return 0;
  618. }
  619. if ((inst & 0xf0df) == 0x400b) {
  620. regs->pc = regs->regs[(inst & 0x0f00) >> 8];
  621. return 0;
  622. }
  623. if ((inst & 0xffff) == 0x000b) {
  624. regs->pc = regs->pr;
  625. return 0;
  626. }
  627. return 1;
  628. }
  629. #endif
  630. asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
  631. unsigned long r6, unsigned long r7,
  632. struct pt_regs __regs)
  633. {
  634. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  635. unsigned long error_code;
  636. struct task_struct *tsk = current;
  637. #ifdef CONFIG_SH_FPU_EMU
  638. unsigned short inst = 0;
  639. get_user(inst, (unsigned short *)regs->pc + 1);
  640. if (!do_fpu_inst(inst, regs)) {
  641. get_user(inst, (unsigned short *)regs->pc);
  642. if (!emulate_branch(inst, regs))
  643. return;
  644. /* fault in branch.*/
  645. }
  646. /* not a FPU inst. */
  647. #endif
  648. lookup_exception_vector(error_code);
  649. local_irq_enable();
  650. CHK_REMOTE_DEBUG(regs);
  651. force_sig(SIGILL, tsk);
  652. die_if_no_fixup("illegal slot instruction", regs, error_code);
  653. }
  654. asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
  655. unsigned long r6, unsigned long r7,
  656. struct pt_regs __regs)
  657. {
  658. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  659. long ex;
  660. lookup_exception_vector(ex);
  661. die_if_kernel("exception", regs, ex);
  662. }
  663. #if defined(CONFIG_SH_STANDARD_BIOS)
  664. void *gdb_vbr_vector;
  665. static inline void __init gdb_vbr_init(void)
  666. {
  667. register unsigned long vbr;
  668. /*
  669. * Read the old value of the VBR register to initialise
  670. * the vector through which debug and BIOS traps are
  671. * delegated by the Linux trap handler.
  672. */
  673. asm volatile("stc vbr, %0" : "=r" (vbr));
  674. gdb_vbr_vector = (void *)(vbr + 0x100);
  675. printk("Setting GDB trap vector to 0x%08lx\n",
  676. (unsigned long)gdb_vbr_vector);
  677. }
  678. #endif
  679. void __init per_cpu_trap_init(void)
  680. {
  681. extern void *vbr_base;
  682. #ifdef CONFIG_SH_STANDARD_BIOS
  683. gdb_vbr_init();
  684. #endif
  685. /* NOTE: The VBR value should be at P1
  686. (or P2, virtural "fixed" address space).
  687. It's definitely should not in physical address. */
  688. asm volatile("ldc %0, vbr"
  689. : /* no output */
  690. : "r" (&vbr_base)
  691. : "memory");
  692. }
  693. void *set_exception_table_vec(unsigned int vec, void *handler)
  694. {
  695. extern void *exception_handling_table[];
  696. void *old_handler;
  697. old_handler = exception_handling_table[vec];
  698. exception_handling_table[vec] = handler;
  699. return old_handler;
  700. }
  701. extern asmlinkage void address_error_handler(unsigned long r4, unsigned long r5,
  702. unsigned long r6, unsigned long r7,
  703. struct pt_regs __regs);
  704. void __init trap_init(void)
  705. {
  706. set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
  707. set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
  708. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
  709. defined(CONFIG_SH_FPU_EMU)
  710. /*
  711. * For SH-4 lacking an FPU, treat floating point instructions as
  712. * reserved. They'll be handled in the math-emu case, or faulted on
  713. * otherwise.
  714. */
  715. set_exception_table_evt(0x800, do_reserved_inst);
  716. set_exception_table_evt(0x820, do_illegal_slot_inst);
  717. #elif defined(CONFIG_SH_FPU)
  718. set_exception_table_evt(0x800, do_fpu_state_restore);
  719. set_exception_table_evt(0x820, do_fpu_state_restore);
  720. #endif
  721. #ifdef CONFIG_CPU_SH2
  722. set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_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. #endif
  728. /* Setup VBR for boot cpu */
  729. per_cpu_trap_init();
  730. }
  731. void show_trace(struct task_struct *tsk, unsigned long *sp,
  732. struct pt_regs *regs)
  733. {
  734. unsigned long addr;
  735. if (regs && user_mode(regs))
  736. return;
  737. printk("\nCall trace: ");
  738. #ifdef CONFIG_KALLSYMS
  739. printk("\n");
  740. #endif
  741. while (!kstack_end(sp)) {
  742. addr = *sp++;
  743. if (kernel_text_address(addr))
  744. print_ip_sym(addr);
  745. }
  746. printk("\n");
  747. if (!tsk)
  748. tsk = current;
  749. debug_show_held_locks(tsk);
  750. }
  751. void show_stack(struct task_struct *tsk, unsigned long *sp)
  752. {
  753. unsigned long stack;
  754. if (!tsk)
  755. tsk = current;
  756. if (tsk == current)
  757. sp = (unsigned long *)current_stack_pointer;
  758. else
  759. sp = (unsigned long *)tsk->thread.sp;
  760. stack = (unsigned long)sp;
  761. dump_mem("Stack: ", stack, THREAD_SIZE +
  762. (unsigned long)task_stack_page(tsk));
  763. show_trace(tsk, sp, NULL);
  764. }
  765. void dump_stack(void)
  766. {
  767. show_stack(NULL, NULL);
  768. }
  769. EXPORT_SYMBOL(dump_stack);