traps_32.c 24 KB

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