traps.c 21 KB

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