traps_32.c 24 KB

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