traps.c 26 KB

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