traps.c 21 KB

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