traps.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. spin_unlock_irq(&die_lock);
  91. if (kexec_should_crash(current))
  92. crash_kexec(regs);
  93. if (in_interrupt())
  94. panic("Fatal exception in interrupt");
  95. if (panic_on_oops)
  96. panic("Fatal exception");
  97. oops_exit();
  98. do_exit(SIGSEGV);
  99. }
  100. static inline void die_if_kernel(const char *str, struct pt_regs *regs,
  101. long err)
  102. {
  103. if (!user_mode(regs))
  104. die(str, regs, err);
  105. }
  106. /*
  107. * try and fix up kernelspace address errors
  108. * - userspace errors just cause EFAULT to be returned, resulting in SEGV
  109. * - kernel/userspace interfaces cause a jump to an appropriate handler
  110. * - other kernel errors are bad
  111. * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
  112. */
  113. static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
  114. {
  115. if (!user_mode(regs)) {
  116. const struct exception_table_entry *fixup;
  117. fixup = search_exception_tables(regs->pc);
  118. if (fixup) {
  119. regs->pc = fixup->fixup;
  120. return 0;
  121. }
  122. die(str, regs, err);
  123. }
  124. return -EFAULT;
  125. }
  126. /*
  127. * handle an instruction that does an unaligned memory access by emulating the
  128. * desired behaviour
  129. * - note that PC _may not_ point to the faulting instruction
  130. * (if that instruction is in a branch delay slot)
  131. * - return 0 if emulation okay, -EFAULT on existential error
  132. */
  133. static int handle_unaligned_ins(u16 instruction, struct pt_regs *regs)
  134. {
  135. int ret, index, count;
  136. unsigned long *rm, *rn;
  137. unsigned char *src, *dst;
  138. index = (instruction>>8)&15; /* 0x0F00 */
  139. rn = &regs->regs[index];
  140. index = (instruction>>4)&15; /* 0x00F0 */
  141. rm = &regs->regs[index];
  142. count = 1<<(instruction&3);
  143. ret = -EFAULT;
  144. switch (instruction>>12) {
  145. case 0: /* mov.[bwl] to/from memory via r0+rn */
  146. if (instruction & 8) {
  147. /* from memory */
  148. src = (unsigned char*) *rm;
  149. src += regs->regs[0];
  150. dst = (unsigned char*) rn;
  151. *(unsigned long*)dst = 0;
  152. #ifdef __LITTLE_ENDIAN__
  153. if (copy_from_user(dst, src, count))
  154. goto fetch_fault;
  155. if ((count == 2) && dst[1] & 0x80) {
  156. dst[2] = 0xff;
  157. dst[3] = 0xff;
  158. }
  159. #else
  160. dst += 4-count;
  161. if (__copy_user(dst, src, count))
  162. goto fetch_fault;
  163. if ((count == 2) && dst[2] & 0x80) {
  164. dst[0] = 0xff;
  165. dst[1] = 0xff;
  166. }
  167. #endif
  168. } else {
  169. /* to memory */
  170. src = (unsigned char*) rm;
  171. #if !defined(__LITTLE_ENDIAN__)
  172. src += 4-count;
  173. #endif
  174. dst = (unsigned char*) *rn;
  175. dst += regs->regs[0];
  176. if (copy_to_user(dst, src, count))
  177. goto fetch_fault;
  178. }
  179. ret = 0;
  180. break;
  181. case 1: /* mov.l Rm,@(disp,Rn) */
  182. src = (unsigned char*) rm;
  183. dst = (unsigned char*) *rn;
  184. dst += (instruction&0x000F)<<2;
  185. if (copy_to_user(dst,src,4))
  186. goto fetch_fault;
  187. ret = 0;
  188. break;
  189. case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
  190. if (instruction & 4)
  191. *rn -= count;
  192. src = (unsigned char*) rm;
  193. dst = (unsigned char*) *rn;
  194. #if !defined(__LITTLE_ENDIAN__)
  195. src += 4-count;
  196. #endif
  197. if (copy_to_user(dst, src, count))
  198. goto fetch_fault;
  199. ret = 0;
  200. break;
  201. case 5: /* mov.l @(disp,Rm),Rn */
  202. src = (unsigned char*) *rm;
  203. src += (instruction&0x000F)<<2;
  204. dst = (unsigned char*) rn;
  205. *(unsigned long*)dst = 0;
  206. if (copy_from_user(dst,src,4))
  207. goto fetch_fault;
  208. ret = 0;
  209. break;
  210. case 6: /* mov.[bwl] from memory, possibly with post-increment */
  211. src = (unsigned char*) *rm;
  212. if (instruction & 4)
  213. *rm += count;
  214. dst = (unsigned char*) rn;
  215. *(unsigned long*)dst = 0;
  216. #ifdef __LITTLE_ENDIAN__
  217. if (copy_from_user(dst, src, count))
  218. goto fetch_fault;
  219. if ((count == 2) && dst[1] & 0x80) {
  220. dst[2] = 0xff;
  221. dst[3] = 0xff;
  222. }
  223. #else
  224. dst += 4-count;
  225. if (copy_from_user(dst, src, count))
  226. goto fetch_fault;
  227. if ((count == 2) && dst[2] & 0x80) {
  228. dst[0] = 0xff;
  229. dst[1] = 0xff;
  230. }
  231. #endif
  232. ret = 0;
  233. break;
  234. case 8:
  235. switch ((instruction&0xFF00)>>8) {
  236. case 0x81: /* mov.w R0,@(disp,Rn) */
  237. src = (unsigned char*) &regs->regs[0];
  238. #if !defined(__LITTLE_ENDIAN__)
  239. src += 2;
  240. #endif
  241. dst = (unsigned char*) *rm; /* called Rn in the spec */
  242. dst += (instruction&0x000F)<<1;
  243. if (copy_to_user(dst, src, 2))
  244. goto fetch_fault;
  245. ret = 0;
  246. break;
  247. case 0x85: /* mov.w @(disp,Rm),R0 */
  248. src = (unsigned char*) *rm;
  249. src += (instruction&0x000F)<<1;
  250. dst = (unsigned char*) &regs->regs[0];
  251. *(unsigned long*)dst = 0;
  252. #if !defined(__LITTLE_ENDIAN__)
  253. dst += 2;
  254. #endif
  255. if (copy_from_user(dst, src, 2))
  256. goto fetch_fault;
  257. #ifdef __LITTLE_ENDIAN__
  258. if (dst[1] & 0x80) {
  259. dst[2] = 0xff;
  260. dst[3] = 0xff;
  261. }
  262. #else
  263. if (dst[2] & 0x80) {
  264. dst[0] = 0xff;
  265. dst[1] = 0xff;
  266. }
  267. #endif
  268. ret = 0;
  269. break;
  270. }
  271. break;
  272. }
  273. return ret;
  274. fetch_fault:
  275. /* Argh. Address not only misaligned but also non-existent.
  276. * Raise an EFAULT and see if it's trapped
  277. */
  278. return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
  279. }
  280. /*
  281. * emulate the instruction in the delay slot
  282. * - fetches the instruction from PC+2
  283. */
  284. static inline int handle_unaligned_delayslot(struct pt_regs *regs)
  285. {
  286. u16 instruction;
  287. if (copy_from_user(&instruction, (u16 *)(regs->pc+2), 2)) {
  288. /* the instruction-fetch faulted */
  289. if (user_mode(regs))
  290. return -EFAULT;
  291. /* kernel */
  292. die("delay-slot-insn faulting in handle_unaligned_delayslot",
  293. regs, 0);
  294. }
  295. return handle_unaligned_ins(instruction,regs);
  296. }
  297. /*
  298. * handle an instruction that does an unaligned memory access
  299. * - have to be careful of branch delay-slot instructions that fault
  300. * SH3:
  301. * - if the branch would be taken PC points to the branch
  302. * - if the branch would not be taken, PC points to delay-slot
  303. * SH4:
  304. * - PC always points to delayed branch
  305. * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
  306. */
  307. /* Macros to determine offset from current PC for branch instructions */
  308. /* Explicit type coercion is used to force sign extension where needed */
  309. #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
  310. #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
  311. /*
  312. * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
  313. * opcodes..
  314. */
  315. #ifndef CONFIG_CPU_SH2A
  316. static int handle_unaligned_notify_count = 10;
  317. static int handle_unaligned_access(u16 instruction, struct pt_regs *regs)
  318. {
  319. u_int rm;
  320. int ret, index;
  321. index = (instruction>>8)&15; /* 0x0F00 */
  322. rm = regs->regs[index];
  323. /* shout about the first ten userspace fixups */
  324. if (user_mode(regs) && handle_unaligned_notify_count>0) {
  325. handle_unaligned_notify_count--;
  326. printk(KERN_NOTICE "Fixing up unaligned userspace access "
  327. "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
  328. current->comm,current->pid,(u16*)regs->pc,instruction);
  329. }
  330. ret = -EFAULT;
  331. switch (instruction&0xF000) {
  332. case 0x0000:
  333. if (instruction==0x000B) {
  334. /* rts */
  335. ret = handle_unaligned_delayslot(regs);
  336. if (ret==0)
  337. regs->pc = regs->pr;
  338. }
  339. else if ((instruction&0x00FF)==0x0023) {
  340. /* braf @Rm */
  341. ret = handle_unaligned_delayslot(regs);
  342. if (ret==0)
  343. regs->pc += rm + 4;
  344. }
  345. else if ((instruction&0x00FF)==0x0003) {
  346. /* bsrf @Rm */
  347. ret = handle_unaligned_delayslot(regs);
  348. if (ret==0) {
  349. regs->pr = regs->pc + 4;
  350. regs->pc += rm + 4;
  351. }
  352. }
  353. else {
  354. /* mov.[bwl] to/from memory via r0+rn */
  355. goto simple;
  356. }
  357. break;
  358. case 0x1000: /* mov.l Rm,@(disp,Rn) */
  359. goto simple;
  360. case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
  361. goto simple;
  362. case 0x4000:
  363. if ((instruction&0x00FF)==0x002B) {
  364. /* jmp @Rm */
  365. ret = handle_unaligned_delayslot(regs);
  366. if (ret==0)
  367. regs->pc = rm;
  368. }
  369. else if ((instruction&0x00FF)==0x000B) {
  370. /* jsr @Rm */
  371. ret = handle_unaligned_delayslot(regs);
  372. if (ret==0) {
  373. regs->pr = regs->pc + 4;
  374. regs->pc = rm;
  375. }
  376. }
  377. else {
  378. /* mov.[bwl] to/from memory via r0+rn */
  379. goto simple;
  380. }
  381. break;
  382. case 0x5000: /* mov.l @(disp,Rm),Rn */
  383. goto simple;
  384. case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
  385. goto simple;
  386. case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
  387. switch (instruction&0x0F00) {
  388. case 0x0100: /* mov.w R0,@(disp,Rm) */
  389. goto simple;
  390. case 0x0500: /* mov.w @(disp,Rm),R0 */
  391. goto simple;
  392. case 0x0B00: /* bf lab - no delayslot*/
  393. break;
  394. case 0x0F00: /* bf/s lab */
  395. ret = handle_unaligned_delayslot(regs);
  396. if (ret==0) {
  397. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  398. if ((regs->sr & 0x00000001) != 0)
  399. regs->pc += 4; /* next after slot */
  400. else
  401. #endif
  402. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  403. }
  404. break;
  405. case 0x0900: /* bt lab - no delayslot */
  406. break;
  407. case 0x0D00: /* bt/s lab */
  408. ret = handle_unaligned_delayslot(regs);
  409. if (ret==0) {
  410. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  411. if ((regs->sr & 0x00000001) == 0)
  412. regs->pc += 4; /* next after slot */
  413. else
  414. #endif
  415. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  416. }
  417. break;
  418. }
  419. break;
  420. case 0xA000: /* bra label */
  421. ret = handle_unaligned_delayslot(regs);
  422. if (ret==0)
  423. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  424. break;
  425. case 0xB000: /* bsr label */
  426. ret = handle_unaligned_delayslot(regs);
  427. if (ret==0) {
  428. regs->pr = regs->pc + 4;
  429. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  430. }
  431. break;
  432. }
  433. return ret;
  434. /* handle non-delay-slot instruction */
  435. simple:
  436. ret = handle_unaligned_ins(instruction,regs);
  437. if (ret==0)
  438. regs->pc += instruction_size(instruction);
  439. return ret;
  440. }
  441. #endif /* CONFIG_CPU_SH2A */
  442. #ifdef CONFIG_CPU_HAS_SR_RB
  443. #define lookup_exception_vector(x) \
  444. __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
  445. #else
  446. #define lookup_exception_vector(x) \
  447. __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
  448. #endif
  449. /*
  450. * Handle various address error exceptions:
  451. * - instruction address error:
  452. * misaligned PC
  453. * PC >= 0x80000000 in user mode
  454. * - data address error (read and write)
  455. * misaligned data access
  456. * access to >= 0x80000000 is user mode
  457. * Unfortuntaly we can't distinguish between instruction address error
  458. * and data address errors caused by read accesses.
  459. */
  460. asmlinkage void do_address_error(struct pt_regs *regs,
  461. unsigned long writeaccess,
  462. unsigned long address)
  463. {
  464. unsigned long error_code = 0;
  465. mm_segment_t oldfs;
  466. siginfo_t info;
  467. #ifndef CONFIG_CPU_SH2A
  468. u16 instruction;
  469. int tmp;
  470. #endif
  471. /* Intentional ifdef */
  472. #ifdef CONFIG_CPU_HAS_SR_RB
  473. lookup_exception_vector(error_code);
  474. #endif
  475. oldfs = get_fs();
  476. if (user_mode(regs)) {
  477. int si_code = BUS_ADRERR;
  478. local_irq_enable();
  479. /* bad PC is not something we can fix */
  480. if (regs->pc & 1) {
  481. si_code = BUS_ADRALN;
  482. goto uspace_segv;
  483. }
  484. #ifndef CONFIG_CPU_SH2A
  485. set_fs(USER_DS);
  486. if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
  487. /* Argh. Fault on the instruction itself.
  488. This should never happen non-SMP
  489. */
  490. set_fs(oldfs);
  491. goto uspace_segv;
  492. }
  493. tmp = handle_unaligned_access(instruction, regs);
  494. set_fs(oldfs);
  495. if (tmp==0)
  496. return; /* sorted */
  497. #endif
  498. uspace_segv:
  499. printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
  500. "access (PC %lx PR %lx)\n", current->comm, regs->pc,
  501. regs->pr);
  502. info.si_signo = SIGBUS;
  503. info.si_errno = 0;
  504. info.si_code = si_code;
  505. info.si_addr = (void *) address;
  506. force_sig_info(SIGBUS, &info, current);
  507. } else {
  508. if (regs->pc & 1)
  509. die("unaligned program counter", regs, error_code);
  510. #ifndef CONFIG_CPU_SH2A
  511. set_fs(KERNEL_DS);
  512. if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
  513. /* Argh. Fault on the instruction itself.
  514. This should never happen non-SMP
  515. */
  516. set_fs(oldfs);
  517. die("insn faulting in do_address_error", regs, 0);
  518. }
  519. handle_unaligned_access(instruction, regs);
  520. set_fs(oldfs);
  521. #else
  522. printk(KERN_NOTICE "Killing process \"%s\" due to unaligned "
  523. "access\n", current->comm);
  524. force_sig(SIGSEGV, current);
  525. #endif
  526. }
  527. }
  528. #ifdef CONFIG_SH_DSP
  529. /*
  530. * SH-DSP support gerg@snapgear.com.
  531. */
  532. int is_dsp_inst(struct pt_regs *regs)
  533. {
  534. unsigned short inst;
  535. /*
  536. * Safe guard if DSP mode is already enabled or we're lacking
  537. * the DSP altogether.
  538. */
  539. if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
  540. return 0;
  541. get_user(inst, ((unsigned short *) regs->pc));
  542. inst &= 0xf000;
  543. /* Check for any type of DSP or support instruction */
  544. if ((inst == 0xf000) || (inst == 0x4000))
  545. return 1;
  546. return 0;
  547. }
  548. #else
  549. #define is_dsp_inst(regs) (0)
  550. #endif /* CONFIG_SH_DSP */
  551. #ifdef CONFIG_CPU_SH2A
  552. asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
  553. unsigned long r6, unsigned long r7,
  554. struct pt_regs __regs)
  555. {
  556. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  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 __init per_cpu_trap_init(void)
  692. {
  693. extern void *vbr_base;
  694. #ifdef CONFIG_SH_STANDARD_BIOS
  695. gdb_vbr_init();
  696. #endif
  697. /* NOTE: The VBR value should be at P1
  698. (or P2, virtural "fixed" address space).
  699. It's definitely should not in physical address. */
  700. asm volatile("ldc %0, vbr"
  701. : /* no output */
  702. : "r" (&vbr_base)
  703. : "memory");
  704. }
  705. void *set_exception_table_vec(unsigned int vec, void *handler)
  706. {
  707. extern void *exception_handling_table[];
  708. void *old_handler;
  709. old_handler = exception_handling_table[vec];
  710. exception_handling_table[vec] = handler;
  711. return old_handler;
  712. }
  713. extern asmlinkage void address_error_handler(unsigned long r4, unsigned long r5,
  714. unsigned long r6, unsigned long r7,
  715. struct pt_regs __regs);
  716. void __init trap_init(void)
  717. {
  718. set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
  719. set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
  720. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
  721. defined(CONFIG_SH_FPU_EMU)
  722. /*
  723. * For SH-4 lacking an FPU, treat floating point instructions as
  724. * reserved. They'll be handled in the math-emu case, or faulted on
  725. * otherwise.
  726. */
  727. set_exception_table_evt(0x800, do_reserved_inst);
  728. set_exception_table_evt(0x820, do_illegal_slot_inst);
  729. #elif defined(CONFIG_SH_FPU)
  730. set_exception_table_evt(0x800, do_fpu_state_restore);
  731. set_exception_table_evt(0x820, do_fpu_state_restore);
  732. #endif
  733. #ifdef CONFIG_CPU_SH2
  734. set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_handler);
  735. #endif
  736. #ifdef CONFIG_CPU_SH2A
  737. set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
  738. set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
  739. #endif
  740. /* Setup VBR for boot cpu */
  741. per_cpu_trap_init();
  742. }
  743. #ifdef CONFIG_BUG
  744. void handle_BUG(struct pt_regs *regs)
  745. {
  746. enum bug_trap_type tt;
  747. tt = report_bug(regs->pc);
  748. if (tt == BUG_TRAP_TYPE_WARN) {
  749. regs->pc += 2;
  750. return;
  751. }
  752. die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
  753. }
  754. int is_valid_bugaddr(unsigned long addr)
  755. {
  756. return addr >= PAGE_OFFSET;
  757. }
  758. #endif
  759. void show_trace(struct task_struct *tsk, unsigned long *sp,
  760. struct pt_regs *regs)
  761. {
  762. unsigned long addr;
  763. if (regs && user_mode(regs))
  764. return;
  765. printk("\nCall trace: ");
  766. #ifdef CONFIG_KALLSYMS
  767. printk("\n");
  768. #endif
  769. while (!kstack_end(sp)) {
  770. addr = *sp++;
  771. if (kernel_text_address(addr))
  772. print_ip_sym(addr);
  773. }
  774. printk("\n");
  775. if (!tsk)
  776. tsk = current;
  777. debug_show_held_locks(tsk);
  778. }
  779. void show_stack(struct task_struct *tsk, unsigned long *sp)
  780. {
  781. unsigned long stack;
  782. if (!tsk)
  783. tsk = current;
  784. if (tsk == current)
  785. sp = (unsigned long *)current_stack_pointer;
  786. else
  787. sp = (unsigned long *)tsk->thread.sp;
  788. stack = (unsigned long)sp;
  789. dump_mem("Stack: ", stack, THREAD_SIZE +
  790. (unsigned long)task_stack_page(tsk));
  791. show_trace(tsk, sp, NULL);
  792. }
  793. void dump_stack(void)
  794. {
  795. show_stack(NULL, NULL);
  796. }
  797. EXPORT_SYMBOL(dump_stack);