traps_64.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /*
  2. * arch/sh/kernel/traps_64.c
  3. *
  4. * Copyright (C) 2000, 2001 Paolo Alberelli
  5. * Copyright (C) 2003, 2004 Paul Mundt
  6. * Copyright (C) 2003, 2004 Richard Curnow
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/string.h>
  15. #include <linux/errno.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/timer.h>
  18. #include <linux/mm.h>
  19. #include <linux/smp.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/kallsyms.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/module.h>
  27. #include <linux/perf_event.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/io.h>
  30. #include <linux/atomic.h>
  31. #include <asm/processor.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/fpu.h>
  34. #undef DEBUG_EXCEPTION
  35. #ifdef DEBUG_EXCEPTION
  36. /* implemented in ../lib/dbg.c */
  37. extern void show_excp_regs(char *fname, int trapnr, int signr,
  38. struct pt_regs *regs);
  39. #else
  40. #define show_excp_regs(a, b, c, d)
  41. #endif
  42. static void do_unhandled_exception(int trapnr, int signr, char *str, char *fn_name,
  43. unsigned long error_code, struct pt_regs *regs, struct task_struct *tsk);
  44. #define DO_ERROR(trapnr, signr, str, name, tsk) \
  45. asmlinkage void do_##name(unsigned long error_code, struct pt_regs *regs) \
  46. { \
  47. do_unhandled_exception(trapnr, signr, str, __stringify(name), error_code, regs, current); \
  48. }
  49. static DEFINE_SPINLOCK(die_lock);
  50. void die(const char * str, struct pt_regs * regs, long err)
  51. {
  52. console_verbose();
  53. spin_lock_irq(&die_lock);
  54. printk("%s: %lx\n", str, (err & 0xffffff));
  55. show_regs(regs);
  56. spin_unlock_irq(&die_lock);
  57. do_exit(SIGSEGV);
  58. }
  59. static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
  60. {
  61. if (!user_mode(regs))
  62. die(str, regs, err);
  63. }
  64. static void die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
  65. {
  66. if (!user_mode(regs)) {
  67. const struct exception_table_entry *fixup;
  68. fixup = search_exception_tables(regs->pc);
  69. if (fixup) {
  70. regs->pc = fixup->fixup;
  71. return;
  72. }
  73. die(str, regs, err);
  74. }
  75. }
  76. DO_ERROR(13, SIGILL, "illegal slot instruction", illegal_slot_inst, current)
  77. DO_ERROR(87, SIGSEGV, "address error (exec)", address_error_exec, current)
  78. /* Implement misaligned load/store handling for kernel (and optionally for user
  79. mode too). Limitation : only SHmedia mode code is handled - there is no
  80. handling at all for misaligned accesses occurring in SHcompact code yet. */
  81. static int misaligned_fixup(struct pt_regs *regs);
  82. asmlinkage void do_address_error_load(unsigned long error_code, struct pt_regs *regs)
  83. {
  84. if (misaligned_fixup(regs) < 0) {
  85. do_unhandled_exception(7, SIGSEGV, "address error(load)",
  86. "do_address_error_load",
  87. error_code, regs, current);
  88. }
  89. return;
  90. }
  91. asmlinkage void do_address_error_store(unsigned long error_code, struct pt_regs *regs)
  92. {
  93. if (misaligned_fixup(regs) < 0) {
  94. do_unhandled_exception(8, SIGSEGV, "address error(store)",
  95. "do_address_error_store",
  96. error_code, regs, current);
  97. }
  98. return;
  99. }
  100. #if defined(CONFIG_SH64_ID2815_WORKAROUND)
  101. #define OPCODE_INVALID 0
  102. #define OPCODE_USER_VALID 1
  103. #define OPCODE_PRIV_VALID 2
  104. /* getcon/putcon - requires checking which control register is referenced. */
  105. #define OPCODE_CTRL_REG 3
  106. /* Table of valid opcodes for SHmedia mode.
  107. Form a 10-bit value by concatenating the major/minor opcodes i.e.
  108. opcode[31:26,20:16]. The 6 MSBs of this value index into the following
  109. array. The 4 LSBs select the bit-pair in the entry (bits 1:0 correspond to
  110. LSBs==4'b0000 etc). */
  111. static unsigned long shmedia_opcode_table[64] = {
  112. 0x55554044,0x54445055,0x15141514,0x14541414,0x00000000,0x10001000,0x01110055,0x04050015,
  113. 0x00000444,0xc0000000,0x44545515,0x40405555,0x55550015,0x10005555,0x55555505,0x04050000,
  114. 0x00000555,0x00000404,0x00040445,0x15151414,0x00000000,0x00000000,0x00000000,0x00000000,
  115. 0x00000055,0x40404444,0x00000404,0xc0009495,0x00000000,0x00000000,0x00000000,0x00000000,
  116. 0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
  117. 0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
  118. 0x80005050,0x04005055,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
  119. 0x81055554,0x00000404,0x55555555,0x55555555,0x00000000,0x00000000,0x00000000,0x00000000
  120. };
  121. void do_reserved_inst(unsigned long error_code, struct pt_regs *regs)
  122. {
  123. /* Workaround SH5-101 cut2 silicon defect #2815 :
  124. in some situations, inter-mode branches from SHcompact -> SHmedia
  125. which should take ITLBMISS or EXECPROT exceptions at the target
  126. falsely take RESINST at the target instead. */
  127. unsigned long opcode = 0x6ff4fff0; /* guaranteed reserved opcode */
  128. unsigned long pc, aligned_pc;
  129. int get_user_error;
  130. int trapnr = 12;
  131. int signr = SIGILL;
  132. char *exception_name = "reserved_instruction";
  133. pc = regs->pc;
  134. if ((pc & 3) == 1) {
  135. /* SHmedia : check for defect. This requires executable vmas
  136. to be readable too. */
  137. aligned_pc = pc & ~3;
  138. if (!access_ok(VERIFY_READ, aligned_pc, sizeof(unsigned long))) {
  139. get_user_error = -EFAULT;
  140. } else {
  141. get_user_error = __get_user(opcode, (unsigned long *)aligned_pc);
  142. }
  143. if (get_user_error >= 0) {
  144. unsigned long index, shift;
  145. unsigned long major, minor, combined;
  146. unsigned long reserved_field;
  147. reserved_field = opcode & 0xf; /* These bits are currently reserved as zero in all valid opcodes */
  148. major = (opcode >> 26) & 0x3f;
  149. minor = (opcode >> 16) & 0xf;
  150. combined = (major << 4) | minor;
  151. index = major;
  152. shift = minor << 1;
  153. if (reserved_field == 0) {
  154. int opcode_state = (shmedia_opcode_table[index] >> shift) & 0x3;
  155. switch (opcode_state) {
  156. case OPCODE_INVALID:
  157. /* Trap. */
  158. break;
  159. case OPCODE_USER_VALID:
  160. /* Restart the instruction : the branch to the instruction will now be from an RTE
  161. not from SHcompact so the silicon defect won't be triggered. */
  162. return;
  163. case OPCODE_PRIV_VALID:
  164. if (!user_mode(regs)) {
  165. /* Should only ever get here if a module has
  166. SHcompact code inside it. If so, the same fix up is needed. */
  167. return; /* same reason */
  168. }
  169. /* Otherwise, user mode trying to execute a privileged instruction -
  170. fall through to trap. */
  171. break;
  172. case OPCODE_CTRL_REG:
  173. /* If in privileged mode, return as above. */
  174. if (!user_mode(regs)) return;
  175. /* In user mode ... */
  176. if (combined == 0x9f) { /* GETCON */
  177. unsigned long regno = (opcode >> 20) & 0x3f;
  178. if (regno >= 62) {
  179. return;
  180. }
  181. /* Otherwise, reserved or privileged control register, => trap */
  182. } else if (combined == 0x1bf) { /* PUTCON */
  183. unsigned long regno = (opcode >> 4) & 0x3f;
  184. if (regno >= 62) {
  185. return;
  186. }
  187. /* Otherwise, reserved or privileged control register, => trap */
  188. } else {
  189. /* Trap */
  190. }
  191. break;
  192. default:
  193. /* Fall through to trap. */
  194. break;
  195. }
  196. }
  197. /* fall through to normal resinst processing */
  198. } else {
  199. /* Error trying to read opcode. This typically means a
  200. real fault, not a RESINST any more. So change the
  201. codes. */
  202. trapnr = 87;
  203. exception_name = "address error (exec)";
  204. signr = SIGSEGV;
  205. }
  206. }
  207. do_unhandled_exception(trapnr, signr, exception_name, "do_reserved_inst", error_code, regs, current);
  208. }
  209. #else /* CONFIG_SH64_ID2815_WORKAROUND */
  210. /* If the workaround isn't needed, this is just a straightforward reserved
  211. instruction */
  212. DO_ERROR(12, SIGILL, "reserved instruction", reserved_inst, current)
  213. #endif /* CONFIG_SH64_ID2815_WORKAROUND */
  214. /* Called with interrupts disabled */
  215. asmlinkage void do_exception_error(unsigned long ex, struct pt_regs *regs)
  216. {
  217. show_excp_regs(__func__, -1, -1, regs);
  218. die_if_kernel("exception", regs, ex);
  219. }
  220. int do_unknown_trapa(unsigned long scId, struct pt_regs *regs)
  221. {
  222. /* Syscall debug */
  223. printk("System call ID error: [0x1#args:8 #syscall:16 0x%lx]\n", scId);
  224. die_if_kernel("unknown trapa", regs, scId);
  225. return -ENOSYS;
  226. }
  227. void show_stack(struct task_struct *tsk, unsigned long *sp)
  228. {
  229. #ifdef CONFIG_KALLSYMS
  230. extern void sh64_unwind(struct pt_regs *regs);
  231. struct pt_regs *regs;
  232. regs = tsk ? tsk->thread.kregs : NULL;
  233. sh64_unwind(regs);
  234. #else
  235. printk(KERN_ERR "Can't backtrace on sh64 without CONFIG_KALLSYMS\n");
  236. #endif
  237. }
  238. void show_task(unsigned long *sp)
  239. {
  240. show_stack(NULL, sp);
  241. }
  242. void dump_stack(void)
  243. {
  244. show_task(NULL);
  245. }
  246. /* Needed by any user of WARN_ON in view of the defn in include/asm-sh/bug.h */
  247. EXPORT_SYMBOL(dump_stack);
  248. static void do_unhandled_exception(int trapnr, int signr, char *str, char *fn_name,
  249. unsigned long error_code, struct pt_regs *regs, struct task_struct *tsk)
  250. {
  251. show_excp_regs(fn_name, trapnr, signr, regs);
  252. tsk->thread.error_code = error_code;
  253. tsk->thread.trap_no = trapnr;
  254. if (user_mode(regs))
  255. force_sig(signr, tsk);
  256. die_if_no_fixup(str, regs, error_code);
  257. }
  258. static int read_opcode(unsigned long long pc, unsigned long *result_opcode, int from_user_mode)
  259. {
  260. int get_user_error;
  261. unsigned long aligned_pc;
  262. unsigned long opcode;
  263. if ((pc & 3) == 1) {
  264. /* SHmedia */
  265. aligned_pc = pc & ~3;
  266. if (from_user_mode) {
  267. if (!access_ok(VERIFY_READ, aligned_pc, sizeof(unsigned long))) {
  268. get_user_error = -EFAULT;
  269. } else {
  270. get_user_error = __get_user(opcode, (unsigned long *)aligned_pc);
  271. *result_opcode = opcode;
  272. }
  273. return get_user_error;
  274. } else {
  275. /* If the fault was in the kernel, we can either read
  276. * this directly, or if not, we fault.
  277. */
  278. *result_opcode = *(unsigned long *) aligned_pc;
  279. return 0;
  280. }
  281. } else if ((pc & 1) == 0) {
  282. /* SHcompact */
  283. /* TODO : provide handling for this. We don't really support
  284. user-mode SHcompact yet, and for a kernel fault, this would
  285. have to come from a module built for SHcompact. */
  286. return -EFAULT;
  287. } else {
  288. /* misaligned */
  289. return -EFAULT;
  290. }
  291. }
  292. static int address_is_sign_extended(__u64 a)
  293. {
  294. __u64 b;
  295. #if (NEFF == 32)
  296. b = (__u64)(__s64)(__s32)(a & 0xffffffffUL);
  297. return (b == a) ? 1 : 0;
  298. #else
  299. #error "Sign extend check only works for NEFF==32"
  300. #endif
  301. }
  302. static int generate_and_check_address(struct pt_regs *regs,
  303. __u32 opcode,
  304. int displacement_not_indexed,
  305. int width_shift,
  306. __u64 *address)
  307. {
  308. /* return -1 for fault, 0 for OK */
  309. __u64 base_address, addr;
  310. int basereg;
  311. basereg = (opcode >> 20) & 0x3f;
  312. base_address = regs->regs[basereg];
  313. if (displacement_not_indexed) {
  314. __s64 displacement;
  315. displacement = (opcode >> 10) & 0x3ff;
  316. displacement = ((displacement << 54) >> 54); /* sign extend */
  317. addr = (__u64)((__s64)base_address + (displacement << width_shift));
  318. } else {
  319. __u64 offset;
  320. int offsetreg;
  321. offsetreg = (opcode >> 10) & 0x3f;
  322. offset = regs->regs[offsetreg];
  323. addr = base_address + offset;
  324. }
  325. /* Check sign extended */
  326. if (!address_is_sign_extended(addr)) {
  327. return -1;
  328. }
  329. /* Check accessible. For misaligned access in the kernel, assume the
  330. address is always accessible (and if not, just fault when the
  331. load/store gets done.) */
  332. if (user_mode(regs)) {
  333. if (addr >= TASK_SIZE) {
  334. return -1;
  335. }
  336. /* Do access_ok check later - it depends on whether it's a load or a store. */
  337. }
  338. *address = addr;
  339. return 0;
  340. }
  341. static int user_mode_unaligned_fixup_count = 10;
  342. static int user_mode_unaligned_fixup_enable = 1;
  343. static int kernel_mode_unaligned_fixup_count = 32;
  344. static void misaligned_kernel_word_load(__u64 address, int do_sign_extend, __u64 *result)
  345. {
  346. unsigned short x;
  347. unsigned char *p, *q;
  348. p = (unsigned char *) (int) address;
  349. q = (unsigned char *) &x;
  350. q[0] = p[0];
  351. q[1] = p[1];
  352. if (do_sign_extend) {
  353. *result = (__u64)(__s64) *(short *) &x;
  354. } else {
  355. *result = (__u64) x;
  356. }
  357. }
  358. static void misaligned_kernel_word_store(__u64 address, __u64 value)
  359. {
  360. unsigned short x;
  361. unsigned char *p, *q;
  362. p = (unsigned char *) (int) address;
  363. q = (unsigned char *) &x;
  364. x = (__u16) value;
  365. p[0] = q[0];
  366. p[1] = q[1];
  367. }
  368. static int misaligned_load(struct pt_regs *regs,
  369. __u32 opcode,
  370. int displacement_not_indexed,
  371. int width_shift,
  372. int do_sign_extend)
  373. {
  374. /* Return -1 for a fault, 0 for OK */
  375. int error;
  376. int destreg;
  377. __u64 address;
  378. error = generate_and_check_address(regs, opcode,
  379. displacement_not_indexed, width_shift, &address);
  380. if (error < 0) {
  381. return error;
  382. }
  383. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, address);
  384. destreg = (opcode >> 4) & 0x3f;
  385. if (user_mode(regs)) {
  386. __u64 buffer;
  387. if (!access_ok(VERIFY_READ, (unsigned long) address, 1UL<<width_shift)) {
  388. return -1;
  389. }
  390. if (__copy_user(&buffer, (const void *)(int)address, (1 << width_shift)) > 0) {
  391. return -1; /* fault */
  392. }
  393. switch (width_shift) {
  394. case 1:
  395. if (do_sign_extend) {
  396. regs->regs[destreg] = (__u64)(__s64) *(__s16 *) &buffer;
  397. } else {
  398. regs->regs[destreg] = (__u64) *(__u16 *) &buffer;
  399. }
  400. break;
  401. case 2:
  402. regs->regs[destreg] = (__u64)(__s64) *(__s32 *) &buffer;
  403. break;
  404. case 3:
  405. regs->regs[destreg] = buffer;
  406. break;
  407. default:
  408. printk("Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
  409. width_shift, (unsigned long) regs->pc);
  410. break;
  411. }
  412. } else {
  413. /* kernel mode - we can take short cuts since if we fault, it's a genuine bug */
  414. __u64 lo, hi;
  415. switch (width_shift) {
  416. case 1:
  417. misaligned_kernel_word_load(address, do_sign_extend, &regs->regs[destreg]);
  418. break;
  419. case 2:
  420. asm ("ldlo.l %1, 0, %0" : "=r" (lo) : "r" (address));
  421. asm ("ldhi.l %1, 3, %0" : "=r" (hi) : "r" (address));
  422. regs->regs[destreg] = lo | hi;
  423. break;
  424. case 3:
  425. asm ("ldlo.q %1, 0, %0" : "=r" (lo) : "r" (address));
  426. asm ("ldhi.q %1, 7, %0" : "=r" (hi) : "r" (address));
  427. regs->regs[destreg] = lo | hi;
  428. break;
  429. default:
  430. printk("Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
  431. width_shift, (unsigned long) regs->pc);
  432. break;
  433. }
  434. }
  435. return 0;
  436. }
  437. static int misaligned_store(struct pt_regs *regs,
  438. __u32 opcode,
  439. int displacement_not_indexed,
  440. int width_shift)
  441. {
  442. /* Return -1 for a fault, 0 for OK */
  443. int error;
  444. int srcreg;
  445. __u64 address;
  446. error = generate_and_check_address(regs, opcode,
  447. displacement_not_indexed, width_shift, &address);
  448. if (error < 0) {
  449. return error;
  450. }
  451. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, address);
  452. srcreg = (opcode >> 4) & 0x3f;
  453. if (user_mode(regs)) {
  454. __u64 buffer;
  455. if (!access_ok(VERIFY_WRITE, (unsigned long) address, 1UL<<width_shift)) {
  456. return -1;
  457. }
  458. switch (width_shift) {
  459. case 1:
  460. *(__u16 *) &buffer = (__u16) regs->regs[srcreg];
  461. break;
  462. case 2:
  463. *(__u32 *) &buffer = (__u32) regs->regs[srcreg];
  464. break;
  465. case 3:
  466. buffer = regs->regs[srcreg];
  467. break;
  468. default:
  469. printk("Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
  470. width_shift, (unsigned long) regs->pc);
  471. break;
  472. }
  473. if (__copy_user((void *)(int)address, &buffer, (1 << width_shift)) > 0) {
  474. return -1; /* fault */
  475. }
  476. } else {
  477. /* kernel mode - we can take short cuts since if we fault, it's a genuine bug */
  478. __u64 val = regs->regs[srcreg];
  479. switch (width_shift) {
  480. case 1:
  481. misaligned_kernel_word_store(address, val);
  482. break;
  483. case 2:
  484. asm ("stlo.l %1, 0, %0" : : "r" (val), "r" (address));
  485. asm ("sthi.l %1, 3, %0" : : "r" (val), "r" (address));
  486. break;
  487. case 3:
  488. asm ("stlo.q %1, 0, %0" : : "r" (val), "r" (address));
  489. asm ("sthi.q %1, 7, %0" : : "r" (val), "r" (address));
  490. break;
  491. default:
  492. printk("Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
  493. width_shift, (unsigned long) regs->pc);
  494. break;
  495. }
  496. }
  497. return 0;
  498. }
  499. /* Never need to fix up misaligned FPU accesses within the kernel since that's a real
  500. error. */
  501. static int misaligned_fpu_load(struct pt_regs *regs,
  502. __u32 opcode,
  503. int displacement_not_indexed,
  504. int width_shift,
  505. int do_paired_load)
  506. {
  507. /* Return -1 for a fault, 0 for OK */
  508. int error;
  509. int destreg;
  510. __u64 address;
  511. error = generate_and_check_address(regs, opcode,
  512. displacement_not_indexed, width_shift, &address);
  513. if (error < 0) {
  514. return error;
  515. }
  516. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, address);
  517. destreg = (opcode >> 4) & 0x3f;
  518. if (user_mode(regs)) {
  519. __u64 buffer;
  520. __u32 buflo, bufhi;
  521. if (!access_ok(VERIFY_READ, (unsigned long) address, 1UL<<width_shift)) {
  522. return -1;
  523. }
  524. if (__copy_user(&buffer, (const void *)(int)address, (1 << width_shift)) > 0) {
  525. return -1; /* fault */
  526. }
  527. /* 'current' may be the current owner of the FPU state, so
  528. context switch the registers into memory so they can be
  529. indexed by register number. */
  530. if (last_task_used_math == current) {
  531. enable_fpu();
  532. save_fpu(current);
  533. disable_fpu();
  534. last_task_used_math = NULL;
  535. regs->sr |= SR_FD;
  536. }
  537. buflo = *(__u32*) &buffer;
  538. bufhi = *(1 + (__u32*) &buffer);
  539. switch (width_shift) {
  540. case 2:
  541. current->thread.xstate->hardfpu.fp_regs[destreg] = buflo;
  542. break;
  543. case 3:
  544. if (do_paired_load) {
  545. current->thread.xstate->hardfpu.fp_regs[destreg] = buflo;
  546. current->thread.xstate->hardfpu.fp_regs[destreg+1] = bufhi;
  547. } else {
  548. #if defined(CONFIG_CPU_LITTLE_ENDIAN)
  549. current->thread.xstate->hardfpu.fp_regs[destreg] = bufhi;
  550. current->thread.xstate->hardfpu.fp_regs[destreg+1] = buflo;
  551. #else
  552. current->thread.xstate->hardfpu.fp_regs[destreg] = buflo;
  553. current->thread.xstate->hardfpu.fp_regs[destreg+1] = bufhi;
  554. #endif
  555. }
  556. break;
  557. default:
  558. printk("Unexpected width_shift %d in misaligned_fpu_load, PC=%08lx\n",
  559. width_shift, (unsigned long) regs->pc);
  560. break;
  561. }
  562. return 0;
  563. } else {
  564. die ("Misaligned FPU load inside kernel", regs, 0);
  565. return -1;
  566. }
  567. }
  568. static int misaligned_fpu_store(struct pt_regs *regs,
  569. __u32 opcode,
  570. int displacement_not_indexed,
  571. int width_shift,
  572. int do_paired_load)
  573. {
  574. /* Return -1 for a fault, 0 for OK */
  575. int error;
  576. int srcreg;
  577. __u64 address;
  578. error = generate_and_check_address(regs, opcode,
  579. displacement_not_indexed, width_shift, &address);
  580. if (error < 0) {
  581. return error;
  582. }
  583. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, address);
  584. srcreg = (opcode >> 4) & 0x3f;
  585. if (user_mode(regs)) {
  586. __u64 buffer;
  587. /* Initialise these to NaNs. */
  588. __u32 buflo=0xffffffffUL, bufhi=0xffffffffUL;
  589. if (!access_ok(VERIFY_WRITE, (unsigned long) address, 1UL<<width_shift)) {
  590. return -1;
  591. }
  592. /* 'current' may be the current owner of the FPU state, so
  593. context switch the registers into memory so they can be
  594. indexed by register number. */
  595. if (last_task_used_math == current) {
  596. enable_fpu();
  597. save_fpu(current);
  598. disable_fpu();
  599. last_task_used_math = NULL;
  600. regs->sr |= SR_FD;
  601. }
  602. switch (width_shift) {
  603. case 2:
  604. buflo = current->thread.xstate->hardfpu.fp_regs[srcreg];
  605. break;
  606. case 3:
  607. if (do_paired_load) {
  608. buflo = current->thread.xstate->hardfpu.fp_regs[srcreg];
  609. bufhi = current->thread.xstate->hardfpu.fp_regs[srcreg+1];
  610. } else {
  611. #if defined(CONFIG_CPU_LITTLE_ENDIAN)
  612. bufhi = current->thread.xstate->hardfpu.fp_regs[srcreg];
  613. buflo = current->thread.xstate->hardfpu.fp_regs[srcreg+1];
  614. #else
  615. buflo = current->thread.xstate->hardfpu.fp_regs[srcreg];
  616. bufhi = current->thread.xstate->hardfpu.fp_regs[srcreg+1];
  617. #endif
  618. }
  619. break;
  620. default:
  621. printk("Unexpected width_shift %d in misaligned_fpu_store, PC=%08lx\n",
  622. width_shift, (unsigned long) regs->pc);
  623. break;
  624. }
  625. *(__u32*) &buffer = buflo;
  626. *(1 + (__u32*) &buffer) = bufhi;
  627. if (__copy_user((void *)(int)address, &buffer, (1 << width_shift)) > 0) {
  628. return -1; /* fault */
  629. }
  630. return 0;
  631. } else {
  632. die ("Misaligned FPU load inside kernel", regs, 0);
  633. return -1;
  634. }
  635. }
  636. static int misaligned_fixup(struct pt_regs *regs)
  637. {
  638. unsigned long opcode;
  639. int error;
  640. int major, minor;
  641. if (!user_mode_unaligned_fixup_enable)
  642. return -1;
  643. error = read_opcode(regs->pc, &opcode, user_mode(regs));
  644. if (error < 0) {
  645. return error;
  646. }
  647. major = (opcode >> 26) & 0x3f;
  648. minor = (opcode >> 16) & 0xf;
  649. if (user_mode(regs) && (user_mode_unaligned_fixup_count > 0)) {
  650. --user_mode_unaligned_fixup_count;
  651. /* Only do 'count' worth of these reports, to remove a potential DoS against syslog */
  652. printk("Fixing up unaligned userspace access in \"%s\" pid=%d pc=0x%08x ins=0x%08lx\n",
  653. current->comm, task_pid_nr(current), (__u32)regs->pc, opcode);
  654. } else if (!user_mode(regs) && (kernel_mode_unaligned_fixup_count > 0)) {
  655. --kernel_mode_unaligned_fixup_count;
  656. if (in_interrupt()) {
  657. printk("Fixing up unaligned kernelspace access in interrupt pc=0x%08x ins=0x%08lx\n",
  658. (__u32)regs->pc, opcode);
  659. } else {
  660. printk("Fixing up unaligned kernelspace access in \"%s\" pid=%d pc=0x%08x ins=0x%08lx\n",
  661. current->comm, task_pid_nr(current), (__u32)regs->pc, opcode);
  662. }
  663. }
  664. switch (major) {
  665. case (0x84>>2): /* LD.W */
  666. error = misaligned_load(regs, opcode, 1, 1, 1);
  667. break;
  668. case (0xb0>>2): /* LD.UW */
  669. error = misaligned_load(regs, opcode, 1, 1, 0);
  670. break;
  671. case (0x88>>2): /* LD.L */
  672. error = misaligned_load(regs, opcode, 1, 2, 1);
  673. break;
  674. case (0x8c>>2): /* LD.Q */
  675. error = misaligned_load(regs, opcode, 1, 3, 0);
  676. break;
  677. case (0xa4>>2): /* ST.W */
  678. error = misaligned_store(regs, opcode, 1, 1);
  679. break;
  680. case (0xa8>>2): /* ST.L */
  681. error = misaligned_store(regs, opcode, 1, 2);
  682. break;
  683. case (0xac>>2): /* ST.Q */
  684. error = misaligned_store(regs, opcode, 1, 3);
  685. break;
  686. case (0x40>>2): /* indexed loads */
  687. switch (minor) {
  688. case 0x1: /* LDX.W */
  689. error = misaligned_load(regs, opcode, 0, 1, 1);
  690. break;
  691. case 0x5: /* LDX.UW */
  692. error = misaligned_load(regs, opcode, 0, 1, 0);
  693. break;
  694. case 0x2: /* LDX.L */
  695. error = misaligned_load(regs, opcode, 0, 2, 1);
  696. break;
  697. case 0x3: /* LDX.Q */
  698. error = misaligned_load(regs, opcode, 0, 3, 0);
  699. break;
  700. default:
  701. error = -1;
  702. break;
  703. }
  704. break;
  705. case (0x60>>2): /* indexed stores */
  706. switch (minor) {
  707. case 0x1: /* STX.W */
  708. error = misaligned_store(regs, opcode, 0, 1);
  709. break;
  710. case 0x2: /* STX.L */
  711. error = misaligned_store(regs, opcode, 0, 2);
  712. break;
  713. case 0x3: /* STX.Q */
  714. error = misaligned_store(regs, opcode, 0, 3);
  715. break;
  716. default:
  717. error = -1;
  718. break;
  719. }
  720. break;
  721. case (0x94>>2): /* FLD.S */
  722. error = misaligned_fpu_load(regs, opcode, 1, 2, 0);
  723. break;
  724. case (0x98>>2): /* FLD.P */
  725. error = misaligned_fpu_load(regs, opcode, 1, 3, 1);
  726. break;
  727. case (0x9c>>2): /* FLD.D */
  728. error = misaligned_fpu_load(regs, opcode, 1, 3, 0);
  729. break;
  730. case (0x1c>>2): /* floating indexed loads */
  731. switch (minor) {
  732. case 0x8: /* FLDX.S */
  733. error = misaligned_fpu_load(regs, opcode, 0, 2, 0);
  734. break;
  735. case 0xd: /* FLDX.P */
  736. error = misaligned_fpu_load(regs, opcode, 0, 3, 1);
  737. break;
  738. case 0x9: /* FLDX.D */
  739. error = misaligned_fpu_load(regs, opcode, 0, 3, 0);
  740. break;
  741. default:
  742. error = -1;
  743. break;
  744. }
  745. break;
  746. case (0xb4>>2): /* FLD.S */
  747. error = misaligned_fpu_store(regs, opcode, 1, 2, 0);
  748. break;
  749. case (0xb8>>2): /* FLD.P */
  750. error = misaligned_fpu_store(regs, opcode, 1, 3, 1);
  751. break;
  752. case (0xbc>>2): /* FLD.D */
  753. error = misaligned_fpu_store(regs, opcode, 1, 3, 0);
  754. break;
  755. case (0x3c>>2): /* floating indexed stores */
  756. switch (minor) {
  757. case 0x8: /* FSTX.S */
  758. error = misaligned_fpu_store(regs, opcode, 0, 2, 0);
  759. break;
  760. case 0xd: /* FSTX.P */
  761. error = misaligned_fpu_store(regs, opcode, 0, 3, 1);
  762. break;
  763. case 0x9: /* FSTX.D */
  764. error = misaligned_fpu_store(regs, opcode, 0, 3, 0);
  765. break;
  766. default:
  767. error = -1;
  768. break;
  769. }
  770. break;
  771. default:
  772. /* Fault */
  773. error = -1;
  774. break;
  775. }
  776. if (error < 0) {
  777. return error;
  778. } else {
  779. regs->pc += 4; /* Skip the instruction that's just been emulated */
  780. return 0;
  781. }
  782. }
  783. static ctl_table unaligned_table[] = {
  784. {
  785. .procname = "kernel_reports",
  786. .data = &kernel_mode_unaligned_fixup_count,
  787. .maxlen = sizeof(int),
  788. .mode = 0644,
  789. .proc_handler = proc_dointvec
  790. },
  791. {
  792. .procname = "user_reports",
  793. .data = &user_mode_unaligned_fixup_count,
  794. .maxlen = sizeof(int),
  795. .mode = 0644,
  796. .proc_handler = proc_dointvec
  797. },
  798. {
  799. .procname = "user_enable",
  800. .data = &user_mode_unaligned_fixup_enable,
  801. .maxlen = sizeof(int),
  802. .mode = 0644,
  803. .proc_handler = proc_dointvec},
  804. {}
  805. };
  806. static ctl_table unaligned_root[] = {
  807. {
  808. .procname = "unaligned_fixup",
  809. .mode = 0555,
  810. .child = unaligned_table
  811. },
  812. {}
  813. };
  814. static ctl_table sh64_root[] = {
  815. {
  816. .procname = "sh64",
  817. .mode = 0555,
  818. .child = unaligned_root
  819. },
  820. {}
  821. };
  822. static struct ctl_table_header *sysctl_header;
  823. static int __init init_sysctl(void)
  824. {
  825. sysctl_header = register_sysctl_table(sh64_root);
  826. return 0;
  827. }
  828. __initcall(init_sysctl);
  829. asmlinkage void do_debug_interrupt(unsigned long code, struct pt_regs *regs)
  830. {
  831. u64 peek_real_address_q(u64 addr);
  832. u64 poke_real_address_q(u64 addr, u64 val);
  833. unsigned long long DM_EXP_CAUSE_PHY = 0x0c100010;
  834. unsigned long long exp_cause;
  835. /* It's not worth ioremapping the debug module registers for the amount
  836. of access we make to them - just go direct to their physical
  837. addresses. */
  838. exp_cause = peek_real_address_q(DM_EXP_CAUSE_PHY);
  839. if (exp_cause & ~4) {
  840. printk("DM.EXP_CAUSE had unexpected bits set (=%08lx)\n",
  841. (unsigned long)(exp_cause & 0xffffffff));
  842. }
  843. show_state();
  844. /* Clear all DEBUGINT causes */
  845. poke_real_address_q(DM_EXP_CAUSE_PHY, 0x0);
  846. }
  847. void __cpuinit per_cpu_trap_init(void)
  848. {
  849. /* Nothing to do for now, VBR initialization later. */
  850. }