traps_32.c 24 KB

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