traps.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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", current->comm,
  85. task_pid_nr(current), 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, task_pid_nr(current),
  330. (u16 *)regs->pc, instruction);
  331. }
  332. ret = -EFAULT;
  333. switch (instruction&0xF000) {
  334. case 0x0000:
  335. if (instruction==0x000B) {
  336. /* rts */
  337. ret = handle_unaligned_delayslot(regs);
  338. if (ret==0)
  339. regs->pc = regs->pr;
  340. }
  341. else if ((instruction&0x00FF)==0x0023) {
  342. /* braf @Rm */
  343. ret = handle_unaligned_delayslot(regs);
  344. if (ret==0)
  345. regs->pc += rm + 4;
  346. }
  347. else if ((instruction&0x00FF)==0x0003) {
  348. /* bsrf @Rm */
  349. ret = handle_unaligned_delayslot(regs);
  350. if (ret==0) {
  351. regs->pr = regs->pc + 4;
  352. regs->pc += rm + 4;
  353. }
  354. }
  355. else {
  356. /* mov.[bwl] to/from memory via r0+rn */
  357. goto simple;
  358. }
  359. break;
  360. case 0x1000: /* mov.l Rm,@(disp,Rn) */
  361. goto simple;
  362. case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
  363. goto simple;
  364. case 0x4000:
  365. if ((instruction&0x00FF)==0x002B) {
  366. /* jmp @Rm */
  367. ret = handle_unaligned_delayslot(regs);
  368. if (ret==0)
  369. regs->pc = rm;
  370. }
  371. else if ((instruction&0x00FF)==0x000B) {
  372. /* jsr @Rm */
  373. ret = handle_unaligned_delayslot(regs);
  374. if (ret==0) {
  375. regs->pr = regs->pc + 4;
  376. regs->pc = rm;
  377. }
  378. }
  379. else {
  380. /* mov.[bwl] to/from memory via r0+rn */
  381. goto simple;
  382. }
  383. break;
  384. case 0x5000: /* mov.l @(disp,Rm),Rn */
  385. goto simple;
  386. case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
  387. goto simple;
  388. case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
  389. switch (instruction&0x0F00) {
  390. case 0x0100: /* mov.w R0,@(disp,Rm) */
  391. goto simple;
  392. case 0x0500: /* mov.w @(disp,Rm),R0 */
  393. goto simple;
  394. case 0x0B00: /* bf lab - no delayslot*/
  395. break;
  396. case 0x0F00: /* bf/s lab */
  397. ret = handle_unaligned_delayslot(regs);
  398. if (ret==0) {
  399. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  400. if ((regs->sr & 0x00000001) != 0)
  401. regs->pc += 4; /* next after slot */
  402. else
  403. #endif
  404. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  405. }
  406. break;
  407. case 0x0900: /* bt lab - no delayslot */
  408. break;
  409. case 0x0D00: /* bt/s lab */
  410. ret = handle_unaligned_delayslot(regs);
  411. if (ret==0) {
  412. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  413. if ((regs->sr & 0x00000001) == 0)
  414. regs->pc += 4; /* next after slot */
  415. else
  416. #endif
  417. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  418. }
  419. break;
  420. }
  421. break;
  422. case 0xA000: /* bra label */
  423. ret = handle_unaligned_delayslot(regs);
  424. if (ret==0)
  425. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  426. break;
  427. case 0xB000: /* bsr label */
  428. ret = handle_unaligned_delayslot(regs);
  429. if (ret==0) {
  430. regs->pr = regs->pc + 4;
  431. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  432. }
  433. break;
  434. }
  435. return ret;
  436. /* handle non-delay-slot instruction */
  437. simple:
  438. ret = handle_unaligned_ins(instruction,regs);
  439. if (ret==0)
  440. regs->pc += instruction_size(instruction);
  441. return ret;
  442. }
  443. #endif /* CONFIG_CPU_SH2A */
  444. #ifdef CONFIG_CPU_HAS_SR_RB
  445. #define lookup_exception_vector(x) \
  446. __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
  447. #else
  448. #define lookup_exception_vector(x) \
  449. __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
  450. #endif
  451. /*
  452. * Handle various address error exceptions:
  453. * - instruction address error:
  454. * misaligned PC
  455. * PC >= 0x80000000 in user mode
  456. * - data address error (read and write)
  457. * misaligned data access
  458. * access to >= 0x80000000 is user mode
  459. * Unfortuntaly we can't distinguish between instruction address error
  460. * and data address errors caused by read accesses.
  461. */
  462. asmlinkage void do_address_error(struct pt_regs *regs,
  463. unsigned long writeaccess,
  464. unsigned long address)
  465. {
  466. unsigned long error_code = 0;
  467. mm_segment_t oldfs;
  468. siginfo_t info;
  469. #ifndef CONFIG_CPU_SH2A
  470. u16 instruction;
  471. int tmp;
  472. #endif
  473. /* Intentional ifdef */
  474. #ifdef CONFIG_CPU_HAS_SR_RB
  475. lookup_exception_vector(error_code);
  476. #endif
  477. oldfs = get_fs();
  478. if (user_mode(regs)) {
  479. int si_code = BUS_ADRERR;
  480. local_irq_enable();
  481. /* bad PC is not something we can fix */
  482. if (regs->pc & 1) {
  483. si_code = BUS_ADRALN;
  484. goto uspace_segv;
  485. }
  486. #ifndef CONFIG_CPU_SH2A
  487. set_fs(USER_DS);
  488. if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
  489. /* Argh. Fault on the instruction itself.
  490. This should never happen non-SMP
  491. */
  492. set_fs(oldfs);
  493. goto uspace_segv;
  494. }
  495. tmp = handle_unaligned_access(instruction, regs);
  496. set_fs(oldfs);
  497. if (tmp==0)
  498. return; /* sorted */
  499. #endif
  500. uspace_segv:
  501. printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
  502. "access (PC %lx PR %lx)\n", current->comm, regs->pc,
  503. regs->pr);
  504. info.si_signo = SIGBUS;
  505. info.si_errno = 0;
  506. info.si_code = si_code;
  507. info.si_addr = (void __user *)address;
  508. force_sig_info(SIGBUS, &info, current);
  509. } else {
  510. if (regs->pc & 1)
  511. die("unaligned program counter", regs, error_code);
  512. #ifndef CONFIG_CPU_SH2A
  513. set_fs(KERNEL_DS);
  514. if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
  515. /* Argh. Fault on the instruction itself.
  516. This should never happen non-SMP
  517. */
  518. set_fs(oldfs);
  519. die("insn faulting in do_address_error", regs, 0);
  520. }
  521. handle_unaligned_access(instruction, regs);
  522. set_fs(oldfs);
  523. #else
  524. printk(KERN_NOTICE "Killing process \"%s\" due to unaligned "
  525. "access\n", current->comm);
  526. force_sig(SIGSEGV, current);
  527. #endif
  528. }
  529. }
  530. #ifdef CONFIG_SH_DSP
  531. /*
  532. * SH-DSP support gerg@snapgear.com.
  533. */
  534. int is_dsp_inst(struct pt_regs *regs)
  535. {
  536. unsigned short inst = 0;
  537. /*
  538. * Safe guard if DSP mode is already enabled or we're lacking
  539. * the DSP altogether.
  540. */
  541. if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
  542. return 0;
  543. get_user(inst, ((unsigned short *) regs->pc));
  544. inst &= 0xf000;
  545. /* Check for any type of DSP or support instruction */
  546. if ((inst == 0xf000) || (inst == 0x4000))
  547. return 1;
  548. return 0;
  549. }
  550. #else
  551. #define is_dsp_inst(regs) (0)
  552. #endif /* CONFIG_SH_DSP */
  553. #ifdef CONFIG_CPU_SH2A
  554. asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
  555. unsigned long r6, unsigned long r7,
  556. struct pt_regs __regs)
  557. {
  558. siginfo_t info;
  559. switch (r4) {
  560. case TRAP_DIVZERO_ERROR:
  561. info.si_code = FPE_INTDIV;
  562. break;
  563. case TRAP_DIVOVF_ERROR:
  564. info.si_code = FPE_INTOVF;
  565. break;
  566. }
  567. force_sig_info(SIGFPE, &info, current);
  568. }
  569. #endif
  570. /* arch/sh/kernel/cpu/sh4/fpu.c */
  571. extern int do_fpu_inst(unsigned short, struct pt_regs *);
  572. extern asmlinkage void do_fpu_state_restore(unsigned long r4, unsigned long r5,
  573. unsigned long r6, unsigned long r7, struct pt_regs __regs);
  574. asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
  575. unsigned long r6, unsigned long r7,
  576. struct pt_regs __regs)
  577. {
  578. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  579. unsigned long error_code;
  580. struct task_struct *tsk = current;
  581. #ifdef CONFIG_SH_FPU_EMU
  582. unsigned short inst = 0;
  583. int err;
  584. get_user(inst, (unsigned short*)regs->pc);
  585. err = do_fpu_inst(inst, regs);
  586. if (!err) {
  587. regs->pc += instruction_size(inst);
  588. return;
  589. }
  590. /* not a FPU inst. */
  591. #endif
  592. #ifdef CONFIG_SH_DSP
  593. /* Check if it's a DSP instruction */
  594. if (is_dsp_inst(regs)) {
  595. /* Enable DSP mode, and restart instruction. */
  596. regs->sr |= SR_DSP;
  597. return;
  598. }
  599. #endif
  600. lookup_exception_vector(error_code);
  601. local_irq_enable();
  602. CHK_REMOTE_DEBUG(regs);
  603. force_sig(SIGILL, tsk);
  604. die_if_no_fixup("reserved instruction", regs, error_code);
  605. }
  606. #ifdef CONFIG_SH_FPU_EMU
  607. static int emulate_branch(unsigned short inst, struct pt_regs* regs)
  608. {
  609. /*
  610. * bfs: 8fxx: PC+=d*2+4;
  611. * bts: 8dxx: PC+=d*2+4;
  612. * bra: axxx: PC+=D*2+4;
  613. * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
  614. * braf:0x23: PC+=Rn*2+4;
  615. * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
  616. * jmp: 4x2b: PC=Rn;
  617. * jsr: 4x0b: PC=Rn after PR=PC+4;
  618. * rts: 000b: PC=PR;
  619. */
  620. if ((inst & 0xfd00) == 0x8d00) {
  621. regs->pc += SH_PC_8BIT_OFFSET(inst);
  622. return 0;
  623. }
  624. if ((inst & 0xe000) == 0xa000) {
  625. regs->pc += SH_PC_12BIT_OFFSET(inst);
  626. return 0;
  627. }
  628. if ((inst & 0xf0df) == 0x0003) {
  629. regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
  630. return 0;
  631. }
  632. if ((inst & 0xf0df) == 0x400b) {
  633. regs->pc = regs->regs[(inst & 0x0f00) >> 8];
  634. return 0;
  635. }
  636. if ((inst & 0xffff) == 0x000b) {
  637. regs->pc = regs->pr;
  638. return 0;
  639. }
  640. return 1;
  641. }
  642. #endif
  643. asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
  644. unsigned long r6, unsigned long r7,
  645. struct pt_regs __regs)
  646. {
  647. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  648. unsigned long error_code;
  649. struct task_struct *tsk = current;
  650. #ifdef CONFIG_SH_FPU_EMU
  651. unsigned short inst = 0;
  652. get_user(inst, (unsigned short *)regs->pc + 1);
  653. if (!do_fpu_inst(inst, regs)) {
  654. get_user(inst, (unsigned short *)regs->pc);
  655. if (!emulate_branch(inst, regs))
  656. return;
  657. /* fault in branch.*/
  658. }
  659. /* not a FPU inst. */
  660. #endif
  661. lookup_exception_vector(error_code);
  662. local_irq_enable();
  663. CHK_REMOTE_DEBUG(regs);
  664. force_sig(SIGILL, tsk);
  665. die_if_no_fixup("illegal slot instruction", regs, error_code);
  666. }
  667. asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
  668. unsigned long r6, unsigned long r7,
  669. struct pt_regs __regs)
  670. {
  671. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  672. long ex;
  673. lookup_exception_vector(ex);
  674. die_if_kernel("exception", regs, ex);
  675. }
  676. #if defined(CONFIG_SH_STANDARD_BIOS)
  677. void *gdb_vbr_vector;
  678. static inline void __init gdb_vbr_init(void)
  679. {
  680. register unsigned long vbr;
  681. /*
  682. * Read the old value of the VBR register to initialise
  683. * the vector through which debug and BIOS traps are
  684. * delegated by the Linux trap handler.
  685. */
  686. asm volatile("stc vbr, %0" : "=r" (vbr));
  687. gdb_vbr_vector = (void *)(vbr + 0x100);
  688. printk("Setting GDB trap vector to 0x%08lx\n",
  689. (unsigned long)gdb_vbr_vector);
  690. }
  691. #endif
  692. void __cpuinit per_cpu_trap_init(void)
  693. {
  694. extern void *vbr_base;
  695. #ifdef CONFIG_SH_STANDARD_BIOS
  696. if (raw_smp_processor_id() == 0)
  697. gdb_vbr_init();
  698. #endif
  699. /* NOTE: The VBR value should be at P1
  700. (or P2, virtural "fixed" address space).
  701. It's definitely should not in physical address. */
  702. asm volatile("ldc %0, vbr"
  703. : /* no output */
  704. : "r" (&vbr_base)
  705. : "memory");
  706. }
  707. void *set_exception_table_vec(unsigned int vec, void *handler)
  708. {
  709. extern void *exception_handling_table[];
  710. void *old_handler;
  711. old_handler = exception_handling_table[vec];
  712. exception_handling_table[vec] = handler;
  713. return old_handler;
  714. }
  715. extern asmlinkage void address_error_handler(unsigned long r4, unsigned long r5,
  716. unsigned long r6, unsigned long r7,
  717. struct pt_regs __regs);
  718. void __init trap_init(void)
  719. {
  720. set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
  721. set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
  722. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
  723. defined(CONFIG_SH_FPU_EMU)
  724. /*
  725. * For SH-4 lacking an FPU, treat floating point instructions as
  726. * reserved. They'll be handled in the math-emu case, or faulted on
  727. * otherwise.
  728. */
  729. set_exception_table_evt(0x800, do_reserved_inst);
  730. set_exception_table_evt(0x820, do_illegal_slot_inst);
  731. #elif defined(CONFIG_SH_FPU)
  732. #ifdef CONFIG_CPU_SUBTYPE_SHX3
  733. set_exception_table_evt(0xd80, do_fpu_state_restore);
  734. set_exception_table_evt(0xda0, do_fpu_state_restore);
  735. #else
  736. set_exception_table_evt(0x800, do_fpu_state_restore);
  737. set_exception_table_evt(0x820, do_fpu_state_restore);
  738. #endif
  739. #endif
  740. #ifdef CONFIG_CPU_SH2
  741. set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_handler);
  742. #endif
  743. #ifdef CONFIG_CPU_SH2A
  744. set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
  745. set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
  746. #endif
  747. /* Setup VBR for boot cpu */
  748. per_cpu_trap_init();
  749. }
  750. #ifdef CONFIG_BUG
  751. void handle_BUG(struct pt_regs *regs)
  752. {
  753. enum bug_trap_type tt;
  754. tt = report_bug(regs->pc, regs);
  755. if (tt == BUG_TRAP_TYPE_WARN) {
  756. regs->pc += 2;
  757. return;
  758. }
  759. die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
  760. }
  761. int is_valid_bugaddr(unsigned long addr)
  762. {
  763. return addr >= PAGE_OFFSET;
  764. }
  765. #endif
  766. void show_trace(struct task_struct *tsk, unsigned long *sp,
  767. struct pt_regs *regs)
  768. {
  769. unsigned long addr;
  770. if (regs && user_mode(regs))
  771. return;
  772. printk("\nCall trace: ");
  773. #ifdef CONFIG_KALLSYMS
  774. printk("\n");
  775. #endif
  776. while (!kstack_end(sp)) {
  777. addr = *sp++;
  778. if (kernel_text_address(addr))
  779. print_ip_sym(addr);
  780. }
  781. printk("\n");
  782. if (!tsk)
  783. tsk = current;
  784. debug_show_held_locks(tsk);
  785. }
  786. void show_stack(struct task_struct *tsk, unsigned long *sp)
  787. {
  788. unsigned long stack;
  789. if (!tsk)
  790. tsk = current;
  791. if (tsk == current)
  792. sp = (unsigned long *)current_stack_pointer;
  793. else
  794. sp = (unsigned long *)tsk->thread.sp;
  795. stack = (unsigned long)sp;
  796. dump_mem("Stack: ", stack, THREAD_SIZE +
  797. (unsigned long)task_stack_page(tsk));
  798. show_trace(tsk, sp, NULL);
  799. }
  800. void dump_stack(void)
  801. {
  802. show_stack(NULL, NULL);
  803. }
  804. EXPORT_SYMBOL(dump_stack);