traps_32.c 24 KB

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