traps_64.c 23 KB

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