traps_32.c 24 KB

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