traps_64.c 26 KB

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