traps_64.c 25 KB

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