traps_32.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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/sysfs.h>
  29. #include <asm/system.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/fpu.h>
  32. #include <asm/kprobes.h>
  33. #ifdef CONFIG_CPU_SH2
  34. # define TRAP_RESERVED_INST 4
  35. # define TRAP_ILLEGAL_SLOT_INST 6
  36. # define TRAP_ADDRESS_ERROR 9
  37. # ifdef CONFIG_CPU_SH2A
  38. # define TRAP_UBC 12
  39. # define TRAP_FPU_ERROR 13
  40. # define TRAP_DIVZERO_ERROR 17
  41. # define TRAP_DIVOVF_ERROR 18
  42. # endif
  43. #else
  44. #define TRAP_RESERVED_INST 12
  45. #define TRAP_ILLEGAL_SLOT_INST 13
  46. #endif
  47. static unsigned long se_user;
  48. static unsigned long se_sys;
  49. static unsigned long se_half;
  50. static unsigned long se_word;
  51. static unsigned long se_dword;
  52. static unsigned long se_multi;
  53. /* bitfield: 1: warn 2: fixup 4: signal -> combinations 2|4 && 1|2|4 are not
  54. valid! */
  55. static int se_usermode = 3;
  56. /* 0: no warning 1: print a warning message, disabled by default */
  57. static int se_kernmode_warn;
  58. #ifdef CONFIG_PROC_FS
  59. static const char *se_usermode_action[] = {
  60. "ignored",
  61. "warn",
  62. "fixup",
  63. "fixup+warn",
  64. "signal",
  65. "signal+warn"
  66. };
  67. static int
  68. proc_alignment_read(char *page, char **start, off_t off, int count, int *eof,
  69. void *data)
  70. {
  71. char *p = page;
  72. int len;
  73. p += sprintf(p, "User:\t\t%lu\n", se_user);
  74. p += sprintf(p, "System:\t\t%lu\n", se_sys);
  75. p += sprintf(p, "Half:\t\t%lu\n", se_half);
  76. p += sprintf(p, "Word:\t\t%lu\n", se_word);
  77. p += sprintf(p, "DWord:\t\t%lu\n", se_dword);
  78. p += sprintf(p, "Multi:\t\t%lu\n", se_multi);
  79. p += sprintf(p, "User faults:\t%i (%s)\n", se_usermode,
  80. se_usermode_action[se_usermode]);
  81. p += sprintf(p, "Kernel faults:\t%i (fixup%s)\n", se_kernmode_warn,
  82. se_kernmode_warn ? "+warn" : "");
  83. len = (p - page) - off;
  84. if (len < 0)
  85. len = 0;
  86. *eof = (len <= count) ? 1 : 0;
  87. *start = page + off;
  88. return len;
  89. }
  90. static int proc_alignment_write(struct file *file, const char __user *buffer,
  91. unsigned long count, void *data)
  92. {
  93. char mode;
  94. if (count > 0) {
  95. if (get_user(mode, buffer))
  96. return -EFAULT;
  97. if (mode >= '0' && mode <= '5')
  98. se_usermode = mode - '0';
  99. }
  100. return count;
  101. }
  102. static int proc_alignment_kern_write(struct file *file, const char __user *buffer,
  103. unsigned long count, void *data)
  104. {
  105. char mode;
  106. if (count > 0) {
  107. if (get_user(mode, buffer))
  108. return -EFAULT;
  109. if (mode >= '0' && mode <= '1')
  110. se_kernmode_warn = mode - '0';
  111. }
  112. return count;
  113. }
  114. #endif
  115. static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
  116. {
  117. unsigned long p;
  118. int i;
  119. printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
  120. for (p = bottom & ~31; p < top; ) {
  121. printk("%04lx: ", p & 0xffff);
  122. for (i = 0; i < 8; i++, p += 4) {
  123. unsigned int val;
  124. if (p < bottom || p >= top)
  125. printk(" ");
  126. else {
  127. if (__get_user(val, (unsigned int __user *)p)) {
  128. printk("\n");
  129. return;
  130. }
  131. printk("%08x ", val);
  132. }
  133. }
  134. printk("\n");
  135. }
  136. }
  137. static DEFINE_SPINLOCK(die_lock);
  138. void die(const char * str, struct pt_regs * regs, long err)
  139. {
  140. static int die_counter;
  141. oops_enter();
  142. spin_lock_irq(&die_lock);
  143. console_verbose();
  144. bust_spinlocks(1);
  145. printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
  146. sysfs_printk_last_file();
  147. print_modules();
  148. show_regs(regs);
  149. printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
  150. task_pid_nr(current), task_stack_page(current) + 1);
  151. if (!user_mode(regs) || in_interrupt())
  152. dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
  153. (unsigned long)task_stack_page(current));
  154. notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);
  155. bust_spinlocks(0);
  156. add_taint(TAINT_DIE);
  157. spin_unlock_irq(&die_lock);
  158. oops_exit();
  159. if (kexec_should_crash(current))
  160. crash_kexec(regs);
  161. if (in_interrupt())
  162. panic("Fatal exception in interrupt");
  163. if (panic_on_oops)
  164. panic("Fatal exception");
  165. do_exit(SIGSEGV);
  166. }
  167. static inline void die_if_kernel(const char *str, struct pt_regs *regs,
  168. long err)
  169. {
  170. if (!user_mode(regs))
  171. die(str, regs, err);
  172. }
  173. /*
  174. * try and fix up kernelspace address errors
  175. * - userspace errors just cause EFAULT to be returned, resulting in SEGV
  176. * - kernel/userspace interfaces cause a jump to an appropriate handler
  177. * - other kernel errors are bad
  178. */
  179. static void die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
  180. {
  181. if (!user_mode(regs)) {
  182. const struct exception_table_entry *fixup;
  183. fixup = search_exception_tables(regs->pc);
  184. if (fixup) {
  185. regs->pc = fixup->fixup;
  186. return;
  187. }
  188. die(str, regs, err);
  189. }
  190. }
  191. static inline void sign_extend(unsigned int count, unsigned char *dst)
  192. {
  193. #ifdef __LITTLE_ENDIAN__
  194. if ((count == 1) && dst[0] & 0x80) {
  195. dst[1] = 0xff;
  196. dst[2] = 0xff;
  197. dst[3] = 0xff;
  198. }
  199. if ((count == 2) && dst[1] & 0x80) {
  200. dst[2] = 0xff;
  201. dst[3] = 0xff;
  202. }
  203. #else
  204. if ((count == 1) && dst[3] & 0x80) {
  205. dst[2] = 0xff;
  206. dst[1] = 0xff;
  207. dst[0] = 0xff;
  208. }
  209. if ((count == 2) && dst[2] & 0x80) {
  210. dst[1] = 0xff;
  211. dst[0] = 0xff;
  212. }
  213. #endif
  214. }
  215. static struct mem_access user_mem_access = {
  216. copy_from_user,
  217. copy_to_user,
  218. };
  219. /*
  220. * handle an instruction that does an unaligned memory access by emulating the
  221. * desired behaviour
  222. * - note that PC _may not_ point to the faulting instruction
  223. * (if that instruction is in a branch delay slot)
  224. * - return 0 if emulation okay, -EFAULT on existential error
  225. */
  226. static int handle_unaligned_ins(insn_size_t instruction, struct pt_regs *regs,
  227. struct mem_access *ma)
  228. {
  229. int ret, index, count;
  230. unsigned long *rm, *rn;
  231. unsigned char *src, *dst;
  232. unsigned char __user *srcu, *dstu;
  233. index = (instruction>>8)&15; /* 0x0F00 */
  234. rn = &regs->regs[index];
  235. index = (instruction>>4)&15; /* 0x00F0 */
  236. rm = &regs->regs[index];
  237. count = 1<<(instruction&3);
  238. switch (count) {
  239. case 1: se_half += 1; break;
  240. case 2: se_word += 1; break;
  241. case 4: se_dword += 1; break;
  242. case 8: se_multi += 1; break; /* ??? */
  243. }
  244. ret = -EFAULT;
  245. switch (instruction>>12) {
  246. case 0: /* mov.[bwl] to/from memory via r0+rn */
  247. if (instruction & 8) {
  248. /* from memory */
  249. srcu = (unsigned char __user *)*rm;
  250. srcu += regs->regs[0];
  251. dst = (unsigned char *)rn;
  252. *(unsigned long *)dst = 0;
  253. #if !defined(__LITTLE_ENDIAN__)
  254. dst += 4-count;
  255. #endif
  256. if (ma->from(dst, srcu, count))
  257. goto fetch_fault;
  258. sign_extend(count, dst);
  259. } else {
  260. /* to memory */
  261. src = (unsigned char *)rm;
  262. #if !defined(__LITTLE_ENDIAN__)
  263. src += 4-count;
  264. #endif
  265. dstu = (unsigned char __user *)*rn;
  266. dstu += regs->regs[0];
  267. if (ma->to(dstu, src, count))
  268. goto fetch_fault;
  269. }
  270. ret = 0;
  271. break;
  272. case 1: /* mov.l Rm,@(disp,Rn) */
  273. src = (unsigned char*) rm;
  274. dstu = (unsigned char __user *)*rn;
  275. dstu += (instruction&0x000F)<<2;
  276. if (ma->to(dstu, src, 4))
  277. goto fetch_fault;
  278. ret = 0;
  279. break;
  280. case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
  281. if (instruction & 4)
  282. *rn -= count;
  283. src = (unsigned char*) rm;
  284. dstu = (unsigned char __user *)*rn;
  285. #if !defined(__LITTLE_ENDIAN__)
  286. src += 4-count;
  287. #endif
  288. if (ma->to(dstu, src, count))
  289. goto fetch_fault;
  290. ret = 0;
  291. break;
  292. case 5: /* mov.l @(disp,Rm),Rn */
  293. srcu = (unsigned char __user *)*rm;
  294. srcu += (instruction & 0x000F) << 2;
  295. dst = (unsigned char *)rn;
  296. *(unsigned long *)dst = 0;
  297. if (ma->from(dst, srcu, 4))
  298. goto fetch_fault;
  299. ret = 0;
  300. break;
  301. case 6: /* mov.[bwl] from memory, possibly with post-increment */
  302. srcu = (unsigned char __user *)*rm;
  303. if (instruction & 4)
  304. *rm += count;
  305. dst = (unsigned char*) rn;
  306. *(unsigned long*)dst = 0;
  307. #if !defined(__LITTLE_ENDIAN__)
  308. dst += 4-count;
  309. #endif
  310. if (ma->from(dst, srcu, count))
  311. goto fetch_fault;
  312. sign_extend(count, dst);
  313. ret = 0;
  314. break;
  315. case 8:
  316. switch ((instruction&0xFF00)>>8) {
  317. case 0x81: /* mov.w R0,@(disp,Rn) */
  318. src = (unsigned char *) &regs->regs[0];
  319. #if !defined(__LITTLE_ENDIAN__)
  320. src += 2;
  321. #endif
  322. dstu = (unsigned char __user *)*rm; /* called Rn in the spec */
  323. dstu += (instruction & 0x000F) << 1;
  324. if (ma->to(dstu, src, 2))
  325. goto fetch_fault;
  326. ret = 0;
  327. break;
  328. case 0x85: /* mov.w @(disp,Rm),R0 */
  329. srcu = (unsigned char __user *)*rm;
  330. srcu += (instruction & 0x000F) << 1;
  331. dst = (unsigned char *) &regs->regs[0];
  332. *(unsigned long *)dst = 0;
  333. #if !defined(__LITTLE_ENDIAN__)
  334. dst += 2;
  335. #endif
  336. if (ma->from(dst, srcu, 2))
  337. goto fetch_fault;
  338. sign_extend(2, dst);
  339. ret = 0;
  340. break;
  341. }
  342. break;
  343. }
  344. return ret;
  345. fetch_fault:
  346. /* Argh. Address not only misaligned but also non-existent.
  347. * Raise an EFAULT and see if it's trapped
  348. */
  349. die_if_no_fixup("Fault in unaligned fixup", regs, 0);
  350. return -EFAULT;
  351. }
  352. /*
  353. * emulate the instruction in the delay slot
  354. * - fetches the instruction from PC+2
  355. */
  356. static inline int handle_delayslot(struct pt_regs *regs,
  357. insn_size_t old_instruction,
  358. struct mem_access *ma)
  359. {
  360. insn_size_t instruction;
  361. void __user *addr = (void __user *)(regs->pc +
  362. instruction_size(old_instruction));
  363. if (copy_from_user(&instruction, addr, sizeof(instruction))) {
  364. /* the instruction-fetch faulted */
  365. if (user_mode(regs))
  366. return -EFAULT;
  367. /* kernel */
  368. die("delay-slot-insn faulting in handle_unaligned_delayslot",
  369. regs, 0);
  370. }
  371. return handle_unaligned_ins(instruction, regs, ma);
  372. }
  373. /*
  374. * handle an instruction that does an unaligned memory access
  375. * - have to be careful of branch delay-slot instructions that fault
  376. * SH3:
  377. * - if the branch would be taken PC points to the branch
  378. * - if the branch would not be taken, PC points to delay-slot
  379. * SH4:
  380. * - PC always points to delayed branch
  381. * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
  382. */
  383. /* Macros to determine offset from current PC for branch instructions */
  384. /* Explicit type coercion is used to force sign extension where needed */
  385. #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
  386. #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
  387. int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
  388. struct mem_access *ma, int expected)
  389. {
  390. u_int rm;
  391. int ret, index;
  392. /*
  393. * XXX: We can't handle mixed 16/32-bit instructions yet
  394. */
  395. if (instruction_size(instruction) != 2)
  396. return -EINVAL;
  397. index = (instruction>>8)&15; /* 0x0F00 */
  398. rm = regs->regs[index];
  399. /* shout about fixups */
  400. if (!expected && printk_ratelimit())
  401. printk(KERN_NOTICE "Fixing up unaligned %s access "
  402. "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
  403. user_mode(regs) ? "userspace" : "kernel",
  404. current->comm, task_pid_nr(current),
  405. (void *)regs->pc, instruction);
  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. #if defined(CONFIG_SH_STANDARD_BIOS)
  760. void *gdb_vbr_vector;
  761. static inline void __init gdb_vbr_init(void)
  762. {
  763. register unsigned long vbr;
  764. /*
  765. * Read the old value of the VBR register to initialise
  766. * the vector through which debug and BIOS traps are
  767. * delegated by the Linux trap handler.
  768. */
  769. asm volatile("stc vbr, %0" : "=r" (vbr));
  770. gdb_vbr_vector = (void *)(vbr + 0x100);
  771. printk("Setting GDB trap vector to 0x%08lx\n",
  772. (unsigned long)gdb_vbr_vector);
  773. }
  774. #endif
  775. void __cpuinit per_cpu_trap_init(void)
  776. {
  777. extern void *vbr_base;
  778. #ifdef CONFIG_SH_STANDARD_BIOS
  779. if (raw_smp_processor_id() == 0)
  780. gdb_vbr_init();
  781. #endif
  782. /* NOTE: The VBR value should be at P1
  783. (or P2, virtural "fixed" address space).
  784. It's definitely should not in physical address. */
  785. asm volatile("ldc %0, vbr"
  786. : /* no output */
  787. : "r" (&vbr_base)
  788. : "memory");
  789. }
  790. void *set_exception_table_vec(unsigned int vec, void *handler)
  791. {
  792. extern void *exception_handling_table[];
  793. void *old_handler;
  794. old_handler = exception_handling_table[vec];
  795. exception_handling_table[vec] = handler;
  796. return old_handler;
  797. }
  798. void __init trap_init(void)
  799. {
  800. set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
  801. set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
  802. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
  803. defined(CONFIG_SH_FPU_EMU)
  804. /*
  805. * For SH-4 lacking an FPU, treat floating point instructions as
  806. * reserved. They'll be handled in the math-emu case, or faulted on
  807. * otherwise.
  808. */
  809. set_exception_table_evt(0x800, do_reserved_inst);
  810. set_exception_table_evt(0x820, do_illegal_slot_inst);
  811. #elif defined(CONFIG_SH_FPU)
  812. #ifdef CONFIG_CPU_SUBTYPE_SHX3
  813. set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
  814. set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
  815. #else
  816. set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
  817. set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
  818. #endif
  819. #endif
  820. #ifdef CONFIG_CPU_SH2
  821. set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
  822. #endif
  823. #ifdef CONFIG_CPU_SH2A
  824. set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
  825. set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
  826. #ifdef CONFIG_SH_FPU
  827. set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
  828. #endif
  829. #endif
  830. #ifdef TRAP_UBC
  831. set_exception_table_vec(TRAP_UBC, break_point_trap);
  832. #endif
  833. /* Setup VBR for boot cpu */
  834. per_cpu_trap_init();
  835. }
  836. void show_stack(struct task_struct *tsk, unsigned long *sp)
  837. {
  838. unsigned long stack;
  839. if (!tsk)
  840. tsk = current;
  841. if (tsk == current)
  842. sp = (unsigned long *)current_stack_pointer;
  843. else
  844. sp = (unsigned long *)tsk->thread.sp;
  845. stack = (unsigned long)sp;
  846. dump_mem("Stack: ", stack, THREAD_SIZE +
  847. (unsigned long)task_stack_page(tsk));
  848. show_trace(tsk, sp, NULL);
  849. }
  850. void dump_stack(void)
  851. {
  852. show_stack(NULL, NULL);
  853. }
  854. EXPORT_SYMBOL(dump_stack);
  855. #ifdef CONFIG_PROC_FS
  856. /*
  857. * This needs to be done after sysctl_init, otherwise sys/ will be
  858. * overwritten. Actually, this shouldn't be in sys/ at all since
  859. * it isn't a sysctl, and it doesn't contain sysctl information.
  860. * We now locate it in /proc/cpu/alignment instead.
  861. */
  862. static int __init alignment_init(void)
  863. {
  864. struct proc_dir_entry *dir, *res;
  865. dir = proc_mkdir("cpu", NULL);
  866. if (!dir)
  867. return -ENOMEM;
  868. res = create_proc_entry("alignment", S_IWUSR | S_IRUGO, dir);
  869. if (!res)
  870. return -ENOMEM;
  871. res->read_proc = proc_alignment_read;
  872. res->write_proc = proc_alignment_write;
  873. res = create_proc_entry("kernel_alignment", S_IWUSR | S_IRUGO, dir);
  874. if (!res)
  875. return -ENOMEM;
  876. res->read_proc = proc_alignment_read;
  877. res->write_proc = proc_alignment_kern_write;
  878. return 0;
  879. }
  880. fs_initcall(alignment_init);
  881. #endif