traps_32.c 23 KB

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