traps.c 21 KB

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