traps_64.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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 <asm/system.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/io.h>
  30. #include <asm/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. spinlock_t 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. destreg = (opcode >> 4) & 0x3f;
  384. if (user_mode(regs)) {
  385. __u64 buffer;
  386. if (!access_ok(VERIFY_READ, (unsigned long) address, 1UL<<width_shift)) {
  387. return -1;
  388. }
  389. if (__copy_user(&buffer, (const void *)(int)address, (1 << width_shift)) > 0) {
  390. return -1; /* fault */
  391. }
  392. switch (width_shift) {
  393. case 1:
  394. if (do_sign_extend) {
  395. regs->regs[destreg] = (__u64)(__s64) *(__s16 *) &buffer;
  396. } else {
  397. regs->regs[destreg] = (__u64) *(__u16 *) &buffer;
  398. }
  399. break;
  400. case 2:
  401. regs->regs[destreg] = (__u64)(__s64) *(__s32 *) &buffer;
  402. break;
  403. case 3:
  404. regs->regs[destreg] = buffer;
  405. break;
  406. default:
  407. printk("Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
  408. width_shift, (unsigned long) regs->pc);
  409. break;
  410. }
  411. } else {
  412. /* kernel mode - we can take short cuts since if we fault, it's a genuine bug */
  413. __u64 lo, hi;
  414. switch (width_shift) {
  415. case 1:
  416. misaligned_kernel_word_load(address, do_sign_extend, &regs->regs[destreg]);
  417. break;
  418. case 2:
  419. asm ("ldlo.l %1, 0, %0" : "=r" (lo) : "r" (address));
  420. asm ("ldhi.l %1, 3, %0" : "=r" (hi) : "r" (address));
  421. regs->regs[destreg] = lo | hi;
  422. break;
  423. case 3:
  424. asm ("ldlo.q %1, 0, %0" : "=r" (lo) : "r" (address));
  425. asm ("ldhi.q %1, 7, %0" : "=r" (hi) : "r" (address));
  426. regs->regs[destreg] = lo | hi;
  427. break;
  428. default:
  429. printk("Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
  430. width_shift, (unsigned long) regs->pc);
  431. break;
  432. }
  433. }
  434. return 0;
  435. }
  436. static int misaligned_store(struct pt_regs *regs,
  437. __u32 opcode,
  438. int displacement_not_indexed,
  439. int width_shift)
  440. {
  441. /* Return -1 for a fault, 0 for OK */
  442. int error;
  443. int srcreg;
  444. __u64 address;
  445. error = generate_and_check_address(regs, opcode,
  446. displacement_not_indexed, width_shift, &address);
  447. if (error < 0) {
  448. return error;
  449. }
  450. srcreg = (opcode >> 4) & 0x3f;
  451. if (user_mode(regs)) {
  452. __u64 buffer;
  453. if (!access_ok(VERIFY_WRITE, (unsigned long) address, 1UL<<width_shift)) {
  454. return -1;
  455. }
  456. switch (width_shift) {
  457. case 1:
  458. *(__u16 *) &buffer = (__u16) regs->regs[srcreg];
  459. break;
  460. case 2:
  461. *(__u32 *) &buffer = (__u32) regs->regs[srcreg];
  462. break;
  463. case 3:
  464. buffer = regs->regs[srcreg];
  465. break;
  466. default:
  467. printk("Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
  468. width_shift, (unsigned long) regs->pc);
  469. break;
  470. }
  471. if (__copy_user((void *)(int)address, &buffer, (1 << width_shift)) > 0) {
  472. return -1; /* fault */
  473. }
  474. } else {
  475. /* kernel mode - we can take short cuts since if we fault, it's a genuine bug */
  476. __u64 val = regs->regs[srcreg];
  477. switch (width_shift) {
  478. case 1:
  479. misaligned_kernel_word_store(address, val);
  480. break;
  481. case 2:
  482. asm ("stlo.l %1, 0, %0" : : "r" (val), "r" (address));
  483. asm ("sthi.l %1, 3, %0" : : "r" (val), "r" (address));
  484. break;
  485. case 3:
  486. asm ("stlo.q %1, 0, %0" : : "r" (val), "r" (address));
  487. asm ("sthi.q %1, 7, %0" : : "r" (val), "r" (address));
  488. break;
  489. default:
  490. printk("Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
  491. width_shift, (unsigned long) regs->pc);
  492. break;
  493. }
  494. }
  495. return 0;
  496. }
  497. /* Never need to fix up misaligned FPU accesses within the kernel since that's a real
  498. error. */
  499. static int misaligned_fpu_load(struct pt_regs *regs,
  500. __u32 opcode,
  501. int displacement_not_indexed,
  502. int width_shift,
  503. int do_paired_load)
  504. {
  505. /* Return -1 for a fault, 0 for OK */
  506. int error;
  507. int destreg;
  508. __u64 address;
  509. error = generate_and_check_address(regs, opcode,
  510. displacement_not_indexed, width_shift, &address);
  511. if (error < 0) {
  512. return error;
  513. }
  514. destreg = (opcode >> 4) & 0x3f;
  515. if (user_mode(regs)) {
  516. __u64 buffer;
  517. __u32 buflo, bufhi;
  518. if (!access_ok(VERIFY_READ, (unsigned long) address, 1UL<<width_shift)) {
  519. return -1;
  520. }
  521. if (__copy_user(&buffer, (const void *)(int)address, (1 << width_shift)) > 0) {
  522. return -1; /* fault */
  523. }
  524. /* 'current' may be the current owner of the FPU state, so
  525. context switch the registers into memory so they can be
  526. indexed by register number. */
  527. if (last_task_used_math == current) {
  528. enable_fpu();
  529. save_fpu(current, regs);
  530. disable_fpu();
  531. last_task_used_math = NULL;
  532. regs->sr |= SR_FD;
  533. }
  534. buflo = *(__u32*) &buffer;
  535. bufhi = *(1 + (__u32*) &buffer);
  536. switch (width_shift) {
  537. case 2:
  538. current->thread.fpu.hard.fp_regs[destreg] = buflo;
  539. break;
  540. case 3:
  541. if (do_paired_load) {
  542. current->thread.fpu.hard.fp_regs[destreg] = buflo;
  543. current->thread.fpu.hard.fp_regs[destreg+1] = bufhi;
  544. } else {
  545. #if defined(CONFIG_CPU_LITTLE_ENDIAN)
  546. current->thread.fpu.hard.fp_regs[destreg] = bufhi;
  547. current->thread.fpu.hard.fp_regs[destreg+1] = buflo;
  548. #else
  549. current->thread.fpu.hard.fp_regs[destreg] = buflo;
  550. current->thread.fpu.hard.fp_regs[destreg+1] = bufhi;
  551. #endif
  552. }
  553. break;
  554. default:
  555. printk("Unexpected width_shift %d in misaligned_fpu_load, PC=%08lx\n",
  556. width_shift, (unsigned long) regs->pc);
  557. break;
  558. }
  559. return 0;
  560. } else {
  561. die ("Misaligned FPU load inside kernel", regs, 0);
  562. return -1;
  563. }
  564. }
  565. static int misaligned_fpu_store(struct pt_regs *regs,
  566. __u32 opcode,
  567. int displacement_not_indexed,
  568. int width_shift,
  569. int do_paired_load)
  570. {
  571. /* Return -1 for a fault, 0 for OK */
  572. int error;
  573. int srcreg;
  574. __u64 address;
  575. error = generate_and_check_address(regs, opcode,
  576. displacement_not_indexed, width_shift, &address);
  577. if (error < 0) {
  578. return error;
  579. }
  580. srcreg = (opcode >> 4) & 0x3f;
  581. if (user_mode(regs)) {
  582. __u64 buffer;
  583. /* Initialise these to NaNs. */
  584. __u32 buflo=0xffffffffUL, bufhi=0xffffffffUL;
  585. if (!access_ok(VERIFY_WRITE, (unsigned long) address, 1UL<<width_shift)) {
  586. return -1;
  587. }
  588. /* 'current' may be the current owner of the FPU state, so
  589. context switch the registers into memory so they can be
  590. indexed by register number. */
  591. if (last_task_used_math == current) {
  592. enable_fpu();
  593. save_fpu(current, regs);
  594. disable_fpu();
  595. last_task_used_math = NULL;
  596. regs->sr |= SR_FD;
  597. }
  598. switch (width_shift) {
  599. case 2:
  600. buflo = current->thread.fpu.hard.fp_regs[srcreg];
  601. break;
  602. case 3:
  603. if (do_paired_load) {
  604. buflo = current->thread.fpu.hard.fp_regs[srcreg];
  605. bufhi = current->thread.fpu.hard.fp_regs[srcreg+1];
  606. } else {
  607. #if defined(CONFIG_CPU_LITTLE_ENDIAN)
  608. bufhi = current->thread.fpu.hard.fp_regs[srcreg];
  609. buflo = current->thread.fpu.hard.fp_regs[srcreg+1];
  610. #else
  611. buflo = current->thread.fpu.hard.fp_regs[srcreg];
  612. bufhi = current->thread.fpu.hard.fp_regs[srcreg+1];
  613. #endif
  614. }
  615. break;
  616. default:
  617. printk("Unexpected width_shift %d in misaligned_fpu_store, PC=%08lx\n",
  618. width_shift, (unsigned long) regs->pc);
  619. break;
  620. }
  621. *(__u32*) &buffer = buflo;
  622. *(1 + (__u32*) &buffer) = bufhi;
  623. if (__copy_user((void *)(int)address, &buffer, (1 << width_shift)) > 0) {
  624. return -1; /* fault */
  625. }
  626. return 0;
  627. } else {
  628. die ("Misaligned FPU load inside kernel", regs, 0);
  629. return -1;
  630. }
  631. }
  632. static int misaligned_fixup(struct pt_regs *regs)
  633. {
  634. unsigned long opcode;
  635. int error;
  636. int major, minor;
  637. if (!user_mode_unaligned_fixup_enable)
  638. return -1;
  639. error = read_opcode(regs->pc, &opcode, user_mode(regs));
  640. if (error < 0) {
  641. return error;
  642. }
  643. major = (opcode >> 26) & 0x3f;
  644. minor = (opcode >> 16) & 0xf;
  645. if (user_mode(regs) && (user_mode_unaligned_fixup_count > 0)) {
  646. --user_mode_unaligned_fixup_count;
  647. /* Only do 'count' worth of these reports, to remove a potential DoS against syslog */
  648. printk("Fixing up unaligned userspace access in \"%s\" pid=%d pc=0x%08x ins=0x%08lx\n",
  649. current->comm, task_pid_nr(current), (__u32)regs->pc, opcode);
  650. } else if (!user_mode(regs) && (kernel_mode_unaligned_fixup_count > 0)) {
  651. --kernel_mode_unaligned_fixup_count;
  652. if (in_interrupt()) {
  653. printk("Fixing up unaligned kernelspace access in interrupt pc=0x%08x ins=0x%08lx\n",
  654. (__u32)regs->pc, opcode);
  655. } else {
  656. printk("Fixing up unaligned kernelspace access in \"%s\" pid=%d pc=0x%08x ins=0x%08lx\n",
  657. current->comm, task_pid_nr(current), (__u32)regs->pc, opcode);
  658. }
  659. }
  660. switch (major) {
  661. case (0x84>>2): /* LD.W */
  662. error = misaligned_load(regs, opcode, 1, 1, 1);
  663. break;
  664. case (0xb0>>2): /* LD.UW */
  665. error = misaligned_load(regs, opcode, 1, 1, 0);
  666. break;
  667. case (0x88>>2): /* LD.L */
  668. error = misaligned_load(regs, opcode, 1, 2, 1);
  669. break;
  670. case (0x8c>>2): /* LD.Q */
  671. error = misaligned_load(regs, opcode, 1, 3, 0);
  672. break;
  673. case (0xa4>>2): /* ST.W */
  674. error = misaligned_store(regs, opcode, 1, 1);
  675. break;
  676. case (0xa8>>2): /* ST.L */
  677. error = misaligned_store(regs, opcode, 1, 2);
  678. break;
  679. case (0xac>>2): /* ST.Q */
  680. error = misaligned_store(regs, opcode, 1, 3);
  681. break;
  682. case (0x40>>2): /* indexed loads */
  683. switch (minor) {
  684. case 0x1: /* LDX.W */
  685. error = misaligned_load(regs, opcode, 0, 1, 1);
  686. break;
  687. case 0x5: /* LDX.UW */
  688. error = misaligned_load(regs, opcode, 0, 1, 0);
  689. break;
  690. case 0x2: /* LDX.L */
  691. error = misaligned_load(regs, opcode, 0, 2, 1);
  692. break;
  693. case 0x3: /* LDX.Q */
  694. error = misaligned_load(regs, opcode, 0, 3, 0);
  695. break;
  696. default:
  697. error = -1;
  698. break;
  699. }
  700. break;
  701. case (0x60>>2): /* indexed stores */
  702. switch (minor) {
  703. case 0x1: /* STX.W */
  704. error = misaligned_store(regs, opcode, 0, 1);
  705. break;
  706. case 0x2: /* STX.L */
  707. error = misaligned_store(regs, opcode, 0, 2);
  708. break;
  709. case 0x3: /* STX.Q */
  710. error = misaligned_store(regs, opcode, 0, 3);
  711. break;
  712. default:
  713. error = -1;
  714. break;
  715. }
  716. break;
  717. case (0x94>>2): /* FLD.S */
  718. error = misaligned_fpu_load(regs, opcode, 1, 2, 0);
  719. break;
  720. case (0x98>>2): /* FLD.P */
  721. error = misaligned_fpu_load(regs, opcode, 1, 3, 1);
  722. break;
  723. case (0x9c>>2): /* FLD.D */
  724. error = misaligned_fpu_load(regs, opcode, 1, 3, 0);
  725. break;
  726. case (0x1c>>2): /* floating indexed loads */
  727. switch (minor) {
  728. case 0x8: /* FLDX.S */
  729. error = misaligned_fpu_load(regs, opcode, 0, 2, 0);
  730. break;
  731. case 0xd: /* FLDX.P */
  732. error = misaligned_fpu_load(regs, opcode, 0, 3, 1);
  733. break;
  734. case 0x9: /* FLDX.D */
  735. error = misaligned_fpu_load(regs, opcode, 0, 3, 0);
  736. break;
  737. default:
  738. error = -1;
  739. break;
  740. }
  741. break;
  742. case (0xb4>>2): /* FLD.S */
  743. error = misaligned_fpu_store(regs, opcode, 1, 2, 0);
  744. break;
  745. case (0xb8>>2): /* FLD.P */
  746. error = misaligned_fpu_store(regs, opcode, 1, 3, 1);
  747. break;
  748. case (0xbc>>2): /* FLD.D */
  749. error = misaligned_fpu_store(regs, opcode, 1, 3, 0);
  750. break;
  751. case (0x3c>>2): /* floating indexed stores */
  752. switch (minor) {
  753. case 0x8: /* FSTX.S */
  754. error = misaligned_fpu_store(regs, opcode, 0, 2, 0);
  755. break;
  756. case 0xd: /* FSTX.P */
  757. error = misaligned_fpu_store(regs, opcode, 0, 3, 1);
  758. break;
  759. case 0x9: /* FSTX.D */
  760. error = misaligned_fpu_store(regs, opcode, 0, 3, 0);
  761. break;
  762. default:
  763. error = -1;
  764. break;
  765. }
  766. break;
  767. default:
  768. /* Fault */
  769. error = -1;
  770. break;
  771. }
  772. if (error < 0) {
  773. return error;
  774. } else {
  775. regs->pc += 4; /* Skip the instruction that's just been emulated */
  776. return 0;
  777. }
  778. }
  779. static ctl_table unaligned_table[] = {
  780. {
  781. .ctl_name = CTL_UNNUMBERED,
  782. .procname = "kernel_reports",
  783. .data = &kernel_mode_unaligned_fixup_count,
  784. .maxlen = sizeof(int),
  785. .mode = 0644,
  786. .proc_handler = &proc_dointvec
  787. },
  788. {
  789. .ctl_name = CTL_UNNUMBERED,
  790. .procname = "user_reports",
  791. .data = &user_mode_unaligned_fixup_count,
  792. .maxlen = sizeof(int),
  793. .mode = 0644,
  794. .proc_handler = &proc_dointvec
  795. },
  796. {
  797. .ctl_name = CTL_UNNUMBERED,
  798. .procname = "user_enable",
  799. .data = &user_mode_unaligned_fixup_enable,
  800. .maxlen = sizeof(int),
  801. .mode = 0644,
  802. .proc_handler = &proc_dointvec},
  803. {}
  804. };
  805. static ctl_table unaligned_root[] = {
  806. {
  807. .ctl_name = CTL_UNNUMBERED,
  808. .procname = "unaligned_fixup",
  809. .mode = 0555,
  810. unaligned_table
  811. },
  812. {}
  813. };
  814. static ctl_table sh64_root[] = {
  815. {
  816. .ctl_name = CTL_UNNUMBERED,
  817. .procname = "sh64",
  818. .mode = 0555,
  819. .child = unaligned_root
  820. },
  821. {}
  822. };
  823. static struct ctl_table_header *sysctl_header;
  824. static int __init init_sysctl(void)
  825. {
  826. sysctl_header = register_sysctl_table(sh64_root);
  827. return 0;
  828. }
  829. __initcall(init_sysctl);
  830. asmlinkage void do_debug_interrupt(unsigned long code, struct pt_regs *regs)
  831. {
  832. u64 peek_real_address_q(u64 addr);
  833. u64 poke_real_address_q(u64 addr, u64 val);
  834. unsigned long long DM_EXP_CAUSE_PHY = 0x0c100010;
  835. unsigned long long exp_cause;
  836. /* It's not worth ioremapping the debug module registers for the amount
  837. of access we make to them - just go direct to their physical
  838. addresses. */
  839. exp_cause = peek_real_address_q(DM_EXP_CAUSE_PHY);
  840. if (exp_cause & ~4) {
  841. printk("DM.EXP_CAUSE had unexpected bits set (=%08lx)\n",
  842. (unsigned long)(exp_cause & 0xffffffff));
  843. }
  844. show_state();
  845. /* Clear all DEBUGINT causes */
  846. poke_real_address_q(DM_EXP_CAUSE_PHY, 0x0);
  847. }