traps_32.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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) {
  394. if (user_mode(regs) && (se_usermode & 1) && printk_ratelimit())
  395. pr_notice("Fixing up unaligned userspace access "
  396. "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
  397. current->comm, task_pid_nr(current),
  398. (void *)regs->pc, instruction);
  399. else if (se_kernmode_warn && printk_ratelimit())
  400. pr_notice("Fixing up unaligned kernel access "
  401. "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
  402. current->comm, task_pid_nr(current),
  403. (void *)regs->pc, instruction);
  404. }
  405. ret = -EFAULT;
  406. switch (instruction&0xF000) {
  407. case 0x0000:
  408. if (instruction==0x000B) {
  409. /* rts */
  410. ret = handle_delayslot(regs, instruction, ma);
  411. if (ret==0)
  412. regs->pc = regs->pr;
  413. }
  414. else if ((instruction&0x00FF)==0x0023) {
  415. /* braf @Rm */
  416. ret = handle_delayslot(regs, instruction, ma);
  417. if (ret==0)
  418. regs->pc += rm + 4;
  419. }
  420. else if ((instruction&0x00FF)==0x0003) {
  421. /* bsrf @Rm */
  422. ret = handle_delayslot(regs, instruction, ma);
  423. if (ret==0) {
  424. regs->pr = regs->pc + 4;
  425. regs->pc += rm + 4;
  426. }
  427. }
  428. else {
  429. /* mov.[bwl] to/from memory via r0+rn */
  430. goto simple;
  431. }
  432. break;
  433. case 0x1000: /* mov.l Rm,@(disp,Rn) */
  434. goto simple;
  435. case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
  436. goto simple;
  437. case 0x4000:
  438. if ((instruction&0x00FF)==0x002B) {
  439. /* jmp @Rm */
  440. ret = handle_delayslot(regs, instruction, ma);
  441. if (ret==0)
  442. regs->pc = rm;
  443. }
  444. else if ((instruction&0x00FF)==0x000B) {
  445. /* jsr @Rm */
  446. ret = handle_delayslot(regs, instruction, ma);
  447. if (ret==0) {
  448. regs->pr = regs->pc + 4;
  449. regs->pc = rm;
  450. }
  451. }
  452. else {
  453. /* mov.[bwl] to/from memory via r0+rn */
  454. goto simple;
  455. }
  456. break;
  457. case 0x5000: /* mov.l @(disp,Rm),Rn */
  458. goto simple;
  459. case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
  460. goto simple;
  461. case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
  462. switch (instruction&0x0F00) {
  463. case 0x0100: /* mov.w R0,@(disp,Rm) */
  464. goto simple;
  465. case 0x0500: /* mov.w @(disp,Rm),R0 */
  466. goto simple;
  467. case 0x0B00: /* bf lab - no delayslot*/
  468. break;
  469. case 0x0F00: /* bf/s lab */
  470. ret = handle_delayslot(regs, instruction, ma);
  471. if (ret==0) {
  472. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  473. if ((regs->sr & 0x00000001) != 0)
  474. regs->pc += 4; /* next after slot */
  475. else
  476. #endif
  477. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  478. }
  479. break;
  480. case 0x0900: /* bt lab - no delayslot */
  481. break;
  482. case 0x0D00: /* bt/s lab */
  483. ret = handle_delayslot(regs, instruction, ma);
  484. if (ret==0) {
  485. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
  486. if ((regs->sr & 0x00000001) == 0)
  487. regs->pc += 4; /* next after slot */
  488. else
  489. #endif
  490. regs->pc += SH_PC_8BIT_OFFSET(instruction);
  491. }
  492. break;
  493. }
  494. break;
  495. case 0xA000: /* bra label */
  496. ret = handle_delayslot(regs, instruction, ma);
  497. if (ret==0)
  498. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  499. break;
  500. case 0xB000: /* bsr label */
  501. ret = handle_delayslot(regs, instruction, ma);
  502. if (ret==0) {
  503. regs->pr = regs->pc + 4;
  504. regs->pc += SH_PC_12BIT_OFFSET(instruction);
  505. }
  506. break;
  507. }
  508. return ret;
  509. /* handle non-delay-slot instruction */
  510. simple:
  511. ret = handle_unaligned_ins(instruction, regs, ma);
  512. if (ret==0)
  513. regs->pc += instruction_size(instruction);
  514. return ret;
  515. }
  516. /*
  517. * Handle various address error exceptions:
  518. * - instruction address error:
  519. * misaligned PC
  520. * PC >= 0x80000000 in user mode
  521. * - data address error (read and write)
  522. * misaligned data access
  523. * access to >= 0x80000000 is user mode
  524. * Unfortuntaly we can't distinguish between instruction address error
  525. * and data address errors caused by read accesses.
  526. */
  527. asmlinkage void do_address_error(struct pt_regs *regs,
  528. unsigned long writeaccess,
  529. unsigned long address)
  530. {
  531. unsigned long error_code = 0;
  532. mm_segment_t oldfs;
  533. siginfo_t info;
  534. insn_size_t instruction;
  535. int tmp;
  536. /* Intentional ifdef */
  537. #ifdef CONFIG_CPU_HAS_SR_RB
  538. error_code = lookup_exception_vector();
  539. #endif
  540. oldfs = get_fs();
  541. if (user_mode(regs)) {
  542. int si_code = BUS_ADRERR;
  543. local_irq_enable();
  544. se_user += 1;
  545. set_fs(USER_DS);
  546. if (copy_from_user(&instruction, (insn_size_t *)(regs->pc & ~1),
  547. sizeof(instruction))) {
  548. set_fs(oldfs);
  549. goto uspace_segv;
  550. }
  551. set_fs(oldfs);
  552. /* shout about userspace fixups */
  553. if (se_usermode & 1)
  554. printk(KERN_NOTICE "Unaligned userspace access "
  555. "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
  556. current->comm, current->pid, (void *)regs->pc,
  557. instruction);
  558. if (se_usermode & 2)
  559. goto fixup;
  560. if (se_usermode & 4)
  561. goto uspace_segv;
  562. else {
  563. /* ignore */
  564. regs->pc += instruction_size(instruction);
  565. return;
  566. }
  567. fixup:
  568. /* bad PC is not something we can fix */
  569. if (regs->pc & 1) {
  570. si_code = BUS_ADRALN;
  571. goto uspace_segv;
  572. }
  573. set_fs(USER_DS);
  574. tmp = handle_unaligned_access(instruction, regs,
  575. &user_mem_access, 0);
  576. set_fs(oldfs);
  577. if (tmp==0)
  578. return; /* sorted */
  579. uspace_segv:
  580. printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
  581. "access (PC %lx PR %lx)\n", current->comm, regs->pc,
  582. regs->pr);
  583. info.si_signo = SIGBUS;
  584. info.si_errno = 0;
  585. info.si_code = si_code;
  586. info.si_addr = (void __user *)address;
  587. force_sig_info(SIGBUS, &info, current);
  588. } else {
  589. se_sys += 1;
  590. if (regs->pc & 1)
  591. die("unaligned program counter", regs, error_code);
  592. set_fs(KERNEL_DS);
  593. if (copy_from_user(&instruction, (void __user *)(regs->pc),
  594. sizeof(instruction))) {
  595. /* Argh. Fault on the instruction itself.
  596. This should never happen non-SMP
  597. */
  598. set_fs(oldfs);
  599. die("insn faulting in do_address_error", regs, 0);
  600. }
  601. if (se_kernmode_warn)
  602. printk(KERN_NOTICE "Unaligned kernel access "
  603. "on behalf of \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
  604. current->comm, current->pid, (void *)regs->pc,
  605. instruction);
  606. handle_unaligned_access(instruction, regs,
  607. &user_mem_access, 0);
  608. set_fs(oldfs);
  609. }
  610. }
  611. #ifdef CONFIG_SH_DSP
  612. /*
  613. * SH-DSP support gerg@snapgear.com.
  614. */
  615. int is_dsp_inst(struct pt_regs *regs)
  616. {
  617. unsigned short inst = 0;
  618. /*
  619. * Safe guard if DSP mode is already enabled or we're lacking
  620. * the DSP altogether.
  621. */
  622. if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
  623. return 0;
  624. get_user(inst, ((unsigned short *) regs->pc));
  625. inst &= 0xf000;
  626. /* Check for any type of DSP or support instruction */
  627. if ((inst == 0xf000) || (inst == 0x4000))
  628. return 1;
  629. return 0;
  630. }
  631. #else
  632. #define is_dsp_inst(regs) (0)
  633. #endif /* CONFIG_SH_DSP */
  634. #ifdef CONFIG_CPU_SH2A
  635. asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
  636. unsigned long r6, unsigned long r7,
  637. struct pt_regs __regs)
  638. {
  639. siginfo_t info;
  640. switch (r4) {
  641. case TRAP_DIVZERO_ERROR:
  642. info.si_code = FPE_INTDIV;
  643. break;
  644. case TRAP_DIVOVF_ERROR:
  645. info.si_code = FPE_INTOVF;
  646. break;
  647. }
  648. force_sig_info(SIGFPE, &info, current);
  649. }
  650. #endif
  651. asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
  652. unsigned long r6, unsigned long r7,
  653. struct pt_regs __regs)
  654. {
  655. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  656. unsigned long error_code;
  657. struct task_struct *tsk = current;
  658. #ifdef CONFIG_SH_FPU_EMU
  659. unsigned short inst = 0;
  660. int err;
  661. get_user(inst, (unsigned short*)regs->pc);
  662. err = do_fpu_inst(inst, regs);
  663. if (!err) {
  664. regs->pc += instruction_size(inst);
  665. return;
  666. }
  667. /* not a FPU inst. */
  668. #endif
  669. #ifdef CONFIG_SH_DSP
  670. /* Check if it's a DSP instruction */
  671. if (is_dsp_inst(regs)) {
  672. /* Enable DSP mode, and restart instruction. */
  673. regs->sr |= SR_DSP;
  674. /* Save DSP mode */
  675. tsk->thread.dsp_status.status |= SR_DSP;
  676. return;
  677. }
  678. #endif
  679. error_code = lookup_exception_vector();
  680. local_irq_enable();
  681. force_sig(SIGILL, tsk);
  682. die_if_no_fixup("reserved instruction", regs, error_code);
  683. }
  684. #ifdef CONFIG_SH_FPU_EMU
  685. static int emulate_branch(unsigned short inst, struct pt_regs *regs)
  686. {
  687. /*
  688. * bfs: 8fxx: PC+=d*2+4;
  689. * bts: 8dxx: PC+=d*2+4;
  690. * bra: axxx: PC+=D*2+4;
  691. * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
  692. * braf:0x23: PC+=Rn*2+4;
  693. * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
  694. * jmp: 4x2b: PC=Rn;
  695. * jsr: 4x0b: PC=Rn after PR=PC+4;
  696. * rts: 000b: PC=PR;
  697. */
  698. if (((inst & 0xf000) == 0xb000) || /* bsr */
  699. ((inst & 0xf0ff) == 0x0003) || /* bsrf */
  700. ((inst & 0xf0ff) == 0x400b)) /* jsr */
  701. regs->pr = regs->pc + 4;
  702. if ((inst & 0xfd00) == 0x8d00) { /* bfs, bts */
  703. regs->pc += SH_PC_8BIT_OFFSET(inst);
  704. return 0;
  705. }
  706. if ((inst & 0xe000) == 0xa000) { /* bra, bsr */
  707. regs->pc += SH_PC_12BIT_OFFSET(inst);
  708. return 0;
  709. }
  710. if ((inst & 0xf0df) == 0x0003) { /* braf, bsrf */
  711. regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
  712. return 0;
  713. }
  714. if ((inst & 0xf0df) == 0x400b) { /* jmp, jsr */
  715. regs->pc = regs->regs[(inst & 0x0f00) >> 8];
  716. return 0;
  717. }
  718. if ((inst & 0xffff) == 0x000b) { /* rts */
  719. regs->pc = regs->pr;
  720. return 0;
  721. }
  722. return 1;
  723. }
  724. #endif
  725. asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
  726. unsigned long r6, unsigned long r7,
  727. struct pt_regs __regs)
  728. {
  729. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  730. unsigned long inst;
  731. struct task_struct *tsk = current;
  732. if (kprobe_handle_illslot(regs->pc) == 0)
  733. return;
  734. #ifdef CONFIG_SH_FPU_EMU
  735. get_user(inst, (unsigned short *)regs->pc + 1);
  736. if (!do_fpu_inst(inst, regs)) {
  737. get_user(inst, (unsigned short *)regs->pc);
  738. if (!emulate_branch(inst, regs))
  739. return;
  740. /* fault in branch.*/
  741. }
  742. /* not a FPU inst. */
  743. #endif
  744. inst = lookup_exception_vector();
  745. local_irq_enable();
  746. force_sig(SIGILL, tsk);
  747. die_if_no_fixup("illegal slot instruction", regs, inst);
  748. }
  749. asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
  750. unsigned long r6, unsigned long r7,
  751. struct pt_regs __regs)
  752. {
  753. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  754. long ex;
  755. ex = lookup_exception_vector();
  756. die_if_kernel("exception", regs, ex);
  757. }
  758. #if defined(CONFIG_SH_STANDARD_BIOS)
  759. void *gdb_vbr_vector;
  760. static inline void __init gdb_vbr_init(void)
  761. {
  762. register unsigned long vbr;
  763. /*
  764. * Read the old value of the VBR register to initialise
  765. * the vector through which debug and BIOS traps are
  766. * delegated by the Linux trap handler.
  767. */
  768. asm volatile("stc vbr, %0" : "=r" (vbr));
  769. gdb_vbr_vector = (void *)(vbr + 0x100);
  770. printk("Setting GDB trap vector to 0x%08lx\n",
  771. (unsigned long)gdb_vbr_vector);
  772. }
  773. #endif
  774. void __cpuinit per_cpu_trap_init(void)
  775. {
  776. extern void *vbr_base;
  777. #ifdef CONFIG_SH_STANDARD_BIOS
  778. if (raw_smp_processor_id() == 0)
  779. gdb_vbr_init();
  780. #endif
  781. /* NOTE: The VBR value should be at P1
  782. (or P2, virtural "fixed" address space).
  783. It's definitely should not in physical address. */
  784. asm volatile("ldc %0, vbr"
  785. : /* no output */
  786. : "r" (&vbr_base)
  787. : "memory");
  788. }
  789. void *set_exception_table_vec(unsigned int vec, void *handler)
  790. {
  791. extern void *exception_handling_table[];
  792. void *old_handler;
  793. old_handler = exception_handling_table[vec];
  794. exception_handling_table[vec] = handler;
  795. return old_handler;
  796. }
  797. void __init trap_init(void)
  798. {
  799. set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
  800. set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
  801. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
  802. defined(CONFIG_SH_FPU_EMU)
  803. /*
  804. * For SH-4 lacking an FPU, treat floating point instructions as
  805. * reserved. They'll be handled in the math-emu case, or faulted on
  806. * otherwise.
  807. */
  808. set_exception_table_evt(0x800, do_reserved_inst);
  809. set_exception_table_evt(0x820, do_illegal_slot_inst);
  810. #elif defined(CONFIG_SH_FPU)
  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. #ifdef CONFIG_CPU_SH2
  815. set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
  816. #endif
  817. #ifdef CONFIG_CPU_SH2A
  818. set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
  819. set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
  820. #ifdef CONFIG_SH_FPU
  821. set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
  822. #endif
  823. #endif
  824. #ifdef TRAP_UBC
  825. set_exception_table_vec(TRAP_UBC, break_point_trap);
  826. #endif
  827. /* Setup VBR for boot cpu */
  828. per_cpu_trap_init();
  829. }
  830. void show_stack(struct task_struct *tsk, unsigned long *sp)
  831. {
  832. unsigned long stack;
  833. if (!tsk)
  834. tsk = current;
  835. if (tsk == current)
  836. sp = (unsigned long *)current_stack_pointer;
  837. else
  838. sp = (unsigned long *)tsk->thread.sp;
  839. stack = (unsigned long)sp;
  840. dump_mem("Stack: ", stack, THREAD_SIZE +
  841. (unsigned long)task_stack_page(tsk));
  842. show_trace(tsk, sp, NULL);
  843. }
  844. void dump_stack(void)
  845. {
  846. show_stack(NULL, NULL);
  847. }
  848. EXPORT_SYMBOL(dump_stack);
  849. #ifdef CONFIG_PROC_FS
  850. /*
  851. * This needs to be done after sysctl_init, otherwise sys/ will be
  852. * overwritten. Actually, this shouldn't be in sys/ at all since
  853. * it isn't a sysctl, and it doesn't contain sysctl information.
  854. * We now locate it in /proc/cpu/alignment instead.
  855. */
  856. static int __init alignment_init(void)
  857. {
  858. struct proc_dir_entry *dir, *res;
  859. dir = proc_mkdir("cpu", NULL);
  860. if (!dir)
  861. return -ENOMEM;
  862. res = proc_create_data("alignment", S_IWUSR | S_IRUGO, dir,
  863. &alignment_proc_fops, &se_usermode);
  864. if (!res)
  865. return -ENOMEM;
  866. res = proc_create_data("kernel_alignment", S_IWUSR | S_IRUGO, dir,
  867. &alignment_proc_fops, &se_kernmode_warn);
  868. if (!res)
  869. return -ENOMEM;
  870. return 0;
  871. }
  872. fs_initcall(alignment_init);
  873. #endif