traps_64.c 25 KB

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