traps.c 25 KB

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