traps.c 26 KB

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