unaligned.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  1. /*
  2. * Handle unaligned accesses by emulation.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 1996, 1998, 1999, 2002 by Ralf Baechle
  9. * Copyright (C) 1999 Silicon Graphics, Inc.
  10. * Copyright (C) 2014 Imagination Technologies Ltd.
  11. *
  12. * This file contains exception handler for address error exception with the
  13. * special capability to execute faulting instructions in software. The
  14. * handler does not try to handle the case when the program counter points
  15. * to an address not aligned to a word boundary.
  16. *
  17. * Putting data to unaligned addresses is a bad practice even on Intel where
  18. * only the performance is affected. Much worse is that such code is non-
  19. * portable. Due to several programs that die on MIPS due to alignment
  20. * problems I decided to implement this handler anyway though I originally
  21. * didn't intend to do this at all for user code.
  22. *
  23. * For now I enable fixing of address errors by default to make life easier.
  24. * I however intend to disable this somewhen in the future when the alignment
  25. * problems with user programs have been fixed. For programmers this is the
  26. * right way to go.
  27. *
  28. * Fixing address errors is a per process option. The option is inherited
  29. * across fork(2) and execve(2) calls. If you really want to use the
  30. * option in your user programs - I discourage the use of the software
  31. * emulation strongly - use the following code in your userland stuff:
  32. *
  33. * #include <sys/sysmips.h>
  34. *
  35. * ...
  36. * sysmips(MIPS_FIXADE, x);
  37. * ...
  38. *
  39. * The argument x is 0 for disabling software emulation, enabled otherwise.
  40. *
  41. * Below a little program to play around with this feature.
  42. *
  43. * #include <stdio.h>
  44. * #include <sys/sysmips.h>
  45. *
  46. * struct foo {
  47. * unsigned char bar[8];
  48. * };
  49. *
  50. * main(int argc, char *argv[])
  51. * {
  52. * struct foo x = {0, 1, 2, 3, 4, 5, 6, 7};
  53. * unsigned int *p = (unsigned int *) (x.bar + 3);
  54. * int i;
  55. *
  56. * if (argc > 1)
  57. * sysmips(MIPS_FIXADE, atoi(argv[1]));
  58. *
  59. * printf("*p = %08lx\n", *p);
  60. *
  61. * *p = 0xdeadface;
  62. *
  63. * for(i = 0; i <= 7; i++)
  64. * printf("%02x ", x.bar[i]);
  65. * printf("\n");
  66. * }
  67. *
  68. * Coprocessor loads are not supported; I think this case is unimportant
  69. * in the practice.
  70. *
  71. * TODO: Handle ndc (attempted store to doubleword in uncached memory)
  72. * exception for the R6000.
  73. * A store crossing a page boundary might be executed only partially.
  74. * Undo the partial store in this case.
  75. */
  76. #include <linux/context_tracking.h>
  77. #include <linux/mm.h>
  78. #include <linux/signal.h>
  79. #include <linux/smp.h>
  80. #include <linux/sched.h>
  81. #include <linux/debugfs.h>
  82. #include <linux/perf_event.h>
  83. #include <asm/asm.h>
  84. #include <asm/branch.h>
  85. #include <asm/byteorder.h>
  86. #include <asm/cop2.h>
  87. #include <asm/debug.h>
  88. #include <asm/fpu.h>
  89. #include <asm/fpu_emulator.h>
  90. #include <asm/inst.h>
  91. #include <asm/unaligned-emul.h>
  92. #include <asm/mmu_context.h>
  93. #include <asm/traps.h>
  94. #include <linux/uaccess.h>
  95. #include "access-helper.h"
  96. enum {
  97. UNALIGNED_ACTION_QUIET,
  98. UNALIGNED_ACTION_SIGNAL,
  99. UNALIGNED_ACTION_SHOW,
  100. };
  101. #ifdef CONFIG_DEBUG_FS
  102. static u32 unaligned_instructions;
  103. static u32 unaligned_action;
  104. #else
  105. #define unaligned_action UNALIGNED_ACTION_QUIET
  106. #endif
  107. extern void show_registers(struct pt_regs *regs);
  108. static void emulate_load_store_insn(struct pt_regs *regs,
  109. void __user *addr, unsigned int *pc)
  110. {
  111. unsigned long origpc, orig31, value;
  112. union mips_instruction insn;
  113. unsigned int res;
  114. bool user = user_mode(regs);
  115. origpc = (unsigned long)pc;
  116. orig31 = regs->regs[31];
  117. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
  118. /*
  119. * This load never faults.
  120. */
  121. __get_inst32(&insn.word, pc, user);
  122. switch (insn.i_format.opcode) {
  123. /*
  124. * These are instructions that a compiler doesn't generate. We
  125. * can assume therefore that the code is MIPS-aware and
  126. * really buggy. Emulating these instructions would break the
  127. * semantics anyway.
  128. */
  129. case ll_op:
  130. case lld_op:
  131. case sc_op:
  132. case scd_op:
  133. /*
  134. * For these instructions the only way to create an address
  135. * error is an attempted access to kernel/supervisor address
  136. * space.
  137. */
  138. case ldl_op:
  139. case ldr_op:
  140. case lwl_op:
  141. case lwr_op:
  142. case sdl_op:
  143. case sdr_op:
  144. case swl_op:
  145. case swr_op:
  146. case lb_op:
  147. case lbu_op:
  148. case sb_op:
  149. goto sigbus;
  150. /*
  151. * The remaining opcodes are the ones that are really of
  152. * interest.
  153. */
  154. #ifdef CONFIG_MACH_INGENIC
  155. case spec2_op:
  156. if (insn.mxu_lx_format.func != mxu_lx_op)
  157. goto sigbus; /* other MXU instructions we don't care */
  158. switch (insn.mxu_lx_format.op) {
  159. case mxu_lxw_op:
  160. if (user && !access_ok(addr, 4))
  161. goto sigbus;
  162. LoadW(addr, value, res);
  163. if (res)
  164. goto fault;
  165. compute_return_epc(regs);
  166. regs->regs[insn.mxu_lx_format.rd] = value;
  167. break;
  168. case mxu_lxh_op:
  169. if (user && !access_ok(addr, 2))
  170. goto sigbus;
  171. LoadHW(addr, value, res);
  172. if (res)
  173. goto fault;
  174. compute_return_epc(regs);
  175. regs->regs[insn.dsp_format.rd] = value;
  176. break;
  177. case mxu_lxhu_op:
  178. if (user && !access_ok(addr, 2))
  179. goto sigbus;
  180. LoadHWU(addr, value, res);
  181. if (res)
  182. goto fault;
  183. compute_return_epc(regs);
  184. regs->regs[insn.dsp_format.rd] = value;
  185. break;
  186. case mxu_lxb_op:
  187. case mxu_lxbu_op:
  188. goto sigbus;
  189. default:
  190. goto sigill;
  191. }
  192. break;
  193. #endif
  194. case spec3_op:
  195. if (insn.dsp_format.func == lx_op) {
  196. switch (insn.dsp_format.op) {
  197. case lwx_op:
  198. if (user && !access_ok(addr, 4))
  199. goto sigbus;
  200. LoadW(addr, value, res);
  201. if (res)
  202. goto fault;
  203. compute_return_epc(regs);
  204. regs->regs[insn.dsp_format.rd] = value;
  205. break;
  206. case lhx_op:
  207. if (user && !access_ok(addr, 2))
  208. goto sigbus;
  209. LoadHW(addr, value, res);
  210. if (res)
  211. goto fault;
  212. compute_return_epc(regs);
  213. regs->regs[insn.dsp_format.rd] = value;
  214. break;
  215. default:
  216. goto sigill;
  217. }
  218. }
  219. #ifdef CONFIG_EVA
  220. else {
  221. /*
  222. * we can land here only from kernel accessing user
  223. * memory, so we need to "switch" the address limit to
  224. * user space, so that address check can work properly.
  225. */
  226. switch (insn.spec3_format.func) {
  227. case lhe_op:
  228. if (!access_ok(addr, 2))
  229. goto sigbus;
  230. LoadHWE(addr, value, res);
  231. if (res)
  232. goto fault;
  233. compute_return_epc(regs);
  234. regs->regs[insn.spec3_format.rt] = value;
  235. break;
  236. case lwe_op:
  237. if (!access_ok(addr, 4))
  238. goto sigbus;
  239. LoadWE(addr, value, res);
  240. if (res)
  241. goto fault;
  242. compute_return_epc(regs);
  243. regs->regs[insn.spec3_format.rt] = value;
  244. break;
  245. case lhue_op:
  246. if (!access_ok(addr, 2))
  247. goto sigbus;
  248. LoadHWUE(addr, value, res);
  249. if (res)
  250. goto fault;
  251. compute_return_epc(regs);
  252. regs->regs[insn.spec3_format.rt] = value;
  253. break;
  254. case she_op:
  255. if (!access_ok(addr, 2))
  256. goto sigbus;
  257. compute_return_epc(regs);
  258. value = regs->regs[insn.spec3_format.rt];
  259. StoreHWE(addr, value, res);
  260. if (res)
  261. goto fault;
  262. break;
  263. case swe_op:
  264. if (!access_ok(addr, 4))
  265. goto sigbus;
  266. compute_return_epc(regs);
  267. value = regs->regs[insn.spec3_format.rt];
  268. StoreWE(addr, value, res);
  269. if (res)
  270. goto fault;
  271. break;
  272. default:
  273. goto sigill;
  274. }
  275. }
  276. #endif
  277. break;
  278. case lh_op:
  279. if (user && !access_ok(addr, 2))
  280. goto sigbus;
  281. if (IS_ENABLED(CONFIG_EVA) && user)
  282. LoadHWE(addr, value, res);
  283. else
  284. LoadHW(addr, value, res);
  285. if (res)
  286. goto fault;
  287. compute_return_epc(regs);
  288. regs->regs[insn.i_format.rt] = value;
  289. break;
  290. case lw_op:
  291. if (user && !access_ok(addr, 4))
  292. goto sigbus;
  293. if (IS_ENABLED(CONFIG_EVA) && user)
  294. LoadWE(addr, value, res);
  295. else
  296. LoadW(addr, value, res);
  297. if (res)
  298. goto fault;
  299. compute_return_epc(regs);
  300. regs->regs[insn.i_format.rt] = value;
  301. break;
  302. case lhu_op:
  303. if (user && !access_ok(addr, 2))
  304. goto sigbus;
  305. if (IS_ENABLED(CONFIG_EVA) && user)
  306. LoadHWUE(addr, value, res);
  307. else
  308. LoadHWU(addr, value, res);
  309. if (res)
  310. goto fault;
  311. compute_return_epc(regs);
  312. regs->regs[insn.i_format.rt] = value;
  313. break;
  314. case lwu_op:
  315. #ifdef CONFIG_64BIT
  316. /*
  317. * A 32-bit kernel might be running on a 64-bit processor. But
  318. * if we're on a 32-bit processor and an i-cache incoherency
  319. * or race makes us see a 64-bit instruction here the sdl/sdr
  320. * would blow up, so for now we don't handle unaligned 64-bit
  321. * instructions on 32-bit kernels.
  322. */
  323. if (user && !access_ok(addr, 4))
  324. goto sigbus;
  325. LoadWU(addr, value, res);
  326. if (res)
  327. goto fault;
  328. compute_return_epc(regs);
  329. regs->regs[insn.i_format.rt] = value;
  330. break;
  331. #endif /* CONFIG_64BIT */
  332. /* Cannot handle 64-bit instructions in 32-bit kernel */
  333. goto sigill;
  334. case ld_op:
  335. #ifdef CONFIG_64BIT
  336. /*
  337. * A 32-bit kernel might be running on a 64-bit processor. But
  338. * if we're on a 32-bit processor and an i-cache incoherency
  339. * or race makes us see a 64-bit instruction here the sdl/sdr
  340. * would blow up, so for now we don't handle unaligned 64-bit
  341. * instructions on 32-bit kernels.
  342. */
  343. if (user && !access_ok(addr, 8))
  344. goto sigbus;
  345. LoadDW(addr, value, res);
  346. if (res)
  347. goto fault;
  348. compute_return_epc(regs);
  349. regs->regs[insn.i_format.rt] = value;
  350. break;
  351. #endif /* CONFIG_64BIT */
  352. /* Cannot handle 64-bit instructions in 32-bit kernel */
  353. goto sigill;
  354. case sh_op:
  355. if (user && !access_ok(addr, 2))
  356. goto sigbus;
  357. compute_return_epc(regs);
  358. value = regs->regs[insn.i_format.rt];
  359. if (IS_ENABLED(CONFIG_EVA) && user)
  360. StoreHWE(addr, value, res);
  361. else
  362. StoreHW(addr, value, res);
  363. if (res)
  364. goto fault;
  365. break;
  366. case sw_op:
  367. if (user && !access_ok(addr, 4))
  368. goto sigbus;
  369. compute_return_epc(regs);
  370. value = regs->regs[insn.i_format.rt];
  371. if (IS_ENABLED(CONFIG_EVA) && user)
  372. StoreWE(addr, value, res);
  373. else
  374. StoreW(addr, value, res);
  375. if (res)
  376. goto fault;
  377. break;
  378. case sd_op:
  379. #ifdef CONFIG_64BIT
  380. /*
  381. * A 32-bit kernel might be running on a 64-bit processor. But
  382. * if we're on a 32-bit processor and an i-cache incoherency
  383. * or race makes us see a 64-bit instruction here the sdl/sdr
  384. * would blow up, so for now we don't handle unaligned 64-bit
  385. * instructions on 32-bit kernels.
  386. */
  387. if (user && !access_ok(addr, 8))
  388. goto sigbus;
  389. compute_return_epc(regs);
  390. value = regs->regs[insn.i_format.rt];
  391. StoreDW(addr, value, res);
  392. if (res)
  393. goto fault;
  394. break;
  395. #endif /* CONFIG_64BIT */
  396. /* Cannot handle 64-bit instructions in 32-bit kernel */
  397. goto sigill;
  398. #ifdef CONFIG_MIPS_FP_SUPPORT
  399. case lwc1_op:
  400. case ldc1_op:
  401. case swc1_op:
  402. case sdc1_op:
  403. case cop1x_op: {
  404. void __user *fault_addr = NULL;
  405. die_if_kernel("Unaligned FP access in kernel code", regs);
  406. BUG_ON(!used_math());
  407. res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  408. &fault_addr);
  409. own_fpu(1); /* Restore FPU state. */
  410. /* Signal if something went wrong. */
  411. process_fpemu_return(res, fault_addr, 0);
  412. if (res == 0)
  413. break;
  414. return;
  415. }
  416. #endif /* CONFIG_MIPS_FP_SUPPORT */
  417. #ifdef CONFIG_CPU_HAS_MSA
  418. case msa_op: {
  419. unsigned int wd, preempted;
  420. enum msa_2b_fmt df;
  421. union fpureg *fpr;
  422. if (!cpu_has_msa)
  423. goto sigill;
  424. /*
  425. * If we've reached this point then userland should have taken
  426. * the MSA disabled exception & initialised vector context at
  427. * some point in the past.
  428. */
  429. BUG_ON(!thread_msa_context_live());
  430. df = insn.msa_mi10_format.df;
  431. wd = insn.msa_mi10_format.wd;
  432. fpr = &current->thread.fpu.fpr[wd];
  433. switch (insn.msa_mi10_format.func) {
  434. case msa_ld_op:
  435. if (!access_ok(addr, sizeof(*fpr)))
  436. goto sigbus;
  437. do {
  438. /*
  439. * If we have live MSA context keep track of
  440. * whether we get preempted in order to avoid
  441. * the register context we load being clobbered
  442. * by the live context as it's saved during
  443. * preemption. If we don't have live context
  444. * then it can't be saved to clobber the value
  445. * we load.
  446. */
  447. preempted = test_thread_flag(TIF_USEDMSA);
  448. res = __copy_from_user_inatomic(fpr, addr,
  449. sizeof(*fpr));
  450. if (res)
  451. goto fault;
  452. /*
  453. * Update the hardware register if it is in use
  454. * by the task in this quantum, in order to
  455. * avoid having to save & restore the whole
  456. * vector context.
  457. */
  458. preempt_disable();
  459. if (test_thread_flag(TIF_USEDMSA)) {
  460. write_msa_wr(wd, fpr, df);
  461. preempted = 0;
  462. }
  463. preempt_enable();
  464. } while (preempted);
  465. break;
  466. case msa_st_op:
  467. if (!access_ok(addr, sizeof(*fpr)))
  468. goto sigbus;
  469. /*
  470. * Update from the hardware register if it is in use by
  471. * the task in this quantum, in order to avoid having to
  472. * save & restore the whole vector context.
  473. */
  474. preempt_disable();
  475. if (test_thread_flag(TIF_USEDMSA))
  476. read_msa_wr(wd, fpr, df);
  477. preempt_enable();
  478. res = __copy_to_user_inatomic(addr, fpr, sizeof(*fpr));
  479. if (res)
  480. goto fault;
  481. break;
  482. default:
  483. goto sigbus;
  484. }
  485. compute_return_epc(regs);
  486. break;
  487. }
  488. #endif /* CONFIG_CPU_HAS_MSA */
  489. #ifndef CONFIG_CPU_MIPSR6
  490. /*
  491. * COP2 is available to implementor for application specific use.
  492. * It's up to applications to register a notifier chain and do
  493. * whatever they have to do, including possible sending of signals.
  494. *
  495. * This instruction has been reallocated in Release 6
  496. */
  497. case lwc2_op:
  498. cu2_notifier_call_chain(CU2_LWC2_OP, regs);
  499. break;
  500. case ldc2_op:
  501. cu2_notifier_call_chain(CU2_LDC2_OP, regs);
  502. break;
  503. case swc2_op:
  504. cu2_notifier_call_chain(CU2_SWC2_OP, regs);
  505. break;
  506. case sdc2_op:
  507. cu2_notifier_call_chain(CU2_SDC2_OP, regs);
  508. break;
  509. #endif
  510. default:
  511. /*
  512. * Pheeee... We encountered an yet unknown instruction or
  513. * cache coherence problem. Die sucker, die ...
  514. */
  515. goto sigill;
  516. }
  517. #ifdef CONFIG_DEBUG_FS
  518. unaligned_instructions++;
  519. #endif
  520. return;
  521. fault:
  522. /* roll back jump/branch */
  523. regs->cp0_epc = origpc;
  524. regs->regs[31] = orig31;
  525. /* Did we have an exception handler installed? */
  526. if (fixup_exception(regs))
  527. return;
  528. die_if_kernel("Unhandled kernel unaligned access", regs);
  529. force_sig(SIGSEGV);
  530. return;
  531. sigbus:
  532. die_if_kernel("Unhandled kernel unaligned access", regs);
  533. force_sig(SIGBUS);
  534. return;
  535. sigill:
  536. die_if_kernel
  537. ("Unhandled kernel unaligned access or invalid instruction", regs);
  538. force_sig(SIGILL);
  539. }
  540. /* Recode table from 16-bit register notation to 32-bit GPR. */
  541. const int reg16to32[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
  542. /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
  543. static const int reg16to32st[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
  544. static void emulate_load_store_microMIPS(struct pt_regs *regs,
  545. void __user *addr)
  546. {
  547. unsigned long value;
  548. unsigned int res;
  549. int i;
  550. unsigned int reg = 0, rvar;
  551. unsigned long orig31;
  552. u16 __user *pc16;
  553. u16 halfword;
  554. unsigned int word;
  555. unsigned long origpc, contpc;
  556. union mips_instruction insn;
  557. struct mm_decoded_insn mminsn;
  558. bool user = user_mode(regs);
  559. origpc = regs->cp0_epc;
  560. orig31 = regs->regs[31];
  561. mminsn.micro_mips_mode = 1;
  562. /*
  563. * This load never faults.
  564. */
  565. pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
  566. __get_user(halfword, pc16);
  567. pc16++;
  568. contpc = regs->cp0_epc + 2;
  569. word = ((unsigned int)halfword << 16);
  570. mminsn.pc_inc = 2;
  571. if (!mm_insn_16bit(halfword)) {
  572. __get_user(halfword, pc16);
  573. pc16++;
  574. contpc = regs->cp0_epc + 4;
  575. mminsn.pc_inc = 4;
  576. word |= halfword;
  577. }
  578. mminsn.insn = word;
  579. if (get_user(halfword, pc16))
  580. goto fault;
  581. mminsn.next_pc_inc = 2;
  582. word = ((unsigned int)halfword << 16);
  583. if (!mm_insn_16bit(halfword)) {
  584. pc16++;
  585. if (get_user(halfword, pc16))
  586. goto fault;
  587. mminsn.next_pc_inc = 4;
  588. word |= halfword;
  589. }
  590. mminsn.next_insn = word;
  591. insn = (union mips_instruction)(mminsn.insn);
  592. if (mm_isBranchInstr(regs, mminsn, &contpc))
  593. insn = (union mips_instruction)(mminsn.next_insn);
  594. /* Parse instruction to find what to do */
  595. switch (insn.mm_i_format.opcode) {
  596. case mm_pool32a_op:
  597. switch (insn.mm_x_format.func) {
  598. case mm_lwxs_op:
  599. reg = insn.mm_x_format.rd;
  600. goto loadW;
  601. }
  602. goto sigbus;
  603. case mm_pool32b_op:
  604. switch (insn.mm_m_format.func) {
  605. case mm_lwp_func:
  606. reg = insn.mm_m_format.rd;
  607. if (reg == 31)
  608. goto sigbus;
  609. if (user && !access_ok(addr, 8))
  610. goto sigbus;
  611. LoadW(addr, value, res);
  612. if (res)
  613. goto fault;
  614. regs->regs[reg] = value;
  615. addr += 4;
  616. LoadW(addr, value, res);
  617. if (res)
  618. goto fault;
  619. regs->regs[reg + 1] = value;
  620. goto success;
  621. case mm_swp_func:
  622. reg = insn.mm_m_format.rd;
  623. if (reg == 31)
  624. goto sigbus;
  625. if (user && !access_ok(addr, 8))
  626. goto sigbus;
  627. value = regs->regs[reg];
  628. StoreW(addr, value, res);
  629. if (res)
  630. goto fault;
  631. addr += 4;
  632. value = regs->regs[reg + 1];
  633. StoreW(addr, value, res);
  634. if (res)
  635. goto fault;
  636. goto success;
  637. case mm_ldp_func:
  638. #ifdef CONFIG_64BIT
  639. reg = insn.mm_m_format.rd;
  640. if (reg == 31)
  641. goto sigbus;
  642. if (user && !access_ok(addr, 16))
  643. goto sigbus;
  644. LoadDW(addr, value, res);
  645. if (res)
  646. goto fault;
  647. regs->regs[reg] = value;
  648. addr += 8;
  649. LoadDW(addr, value, res);
  650. if (res)
  651. goto fault;
  652. regs->regs[reg + 1] = value;
  653. goto success;
  654. #endif /* CONFIG_64BIT */
  655. goto sigill;
  656. case mm_sdp_func:
  657. #ifdef CONFIG_64BIT
  658. reg = insn.mm_m_format.rd;
  659. if (reg == 31)
  660. goto sigbus;
  661. if (user && !access_ok(addr, 16))
  662. goto sigbus;
  663. value = regs->regs[reg];
  664. StoreDW(addr, value, res);
  665. if (res)
  666. goto fault;
  667. addr += 8;
  668. value = regs->regs[reg + 1];
  669. StoreDW(addr, value, res);
  670. if (res)
  671. goto fault;
  672. goto success;
  673. #endif /* CONFIG_64BIT */
  674. goto sigill;
  675. case mm_lwm32_func:
  676. reg = insn.mm_m_format.rd;
  677. rvar = reg & 0xf;
  678. if ((rvar > 9) || !reg)
  679. goto sigill;
  680. if (reg & 0x10) {
  681. if (user && !access_ok(addr, 4 * (rvar + 1)))
  682. goto sigbus;
  683. } else {
  684. if (user && !access_ok(addr, 4 * rvar))
  685. goto sigbus;
  686. }
  687. if (rvar == 9)
  688. rvar = 8;
  689. for (i = 16; rvar; rvar--, i++) {
  690. LoadW(addr, value, res);
  691. if (res)
  692. goto fault;
  693. addr += 4;
  694. regs->regs[i] = value;
  695. }
  696. if ((reg & 0xf) == 9) {
  697. LoadW(addr, value, res);
  698. if (res)
  699. goto fault;
  700. addr += 4;
  701. regs->regs[30] = value;
  702. }
  703. if (reg & 0x10) {
  704. LoadW(addr, value, res);
  705. if (res)
  706. goto fault;
  707. regs->regs[31] = value;
  708. }
  709. goto success;
  710. case mm_swm32_func:
  711. reg = insn.mm_m_format.rd;
  712. rvar = reg & 0xf;
  713. if ((rvar > 9) || !reg)
  714. goto sigill;
  715. if (reg & 0x10) {
  716. if (user && !access_ok(addr, 4 * (rvar + 1)))
  717. goto sigbus;
  718. } else {
  719. if (user && !access_ok(addr, 4 * rvar))
  720. goto sigbus;
  721. }
  722. if (rvar == 9)
  723. rvar = 8;
  724. for (i = 16; rvar; rvar--, i++) {
  725. value = regs->regs[i];
  726. StoreW(addr, value, res);
  727. if (res)
  728. goto fault;
  729. addr += 4;
  730. }
  731. if ((reg & 0xf) == 9) {
  732. value = regs->regs[30];
  733. StoreW(addr, value, res);
  734. if (res)
  735. goto fault;
  736. addr += 4;
  737. }
  738. if (reg & 0x10) {
  739. value = regs->regs[31];
  740. StoreW(addr, value, res);
  741. if (res)
  742. goto fault;
  743. }
  744. goto success;
  745. case mm_ldm_func:
  746. #ifdef CONFIG_64BIT
  747. reg = insn.mm_m_format.rd;
  748. rvar = reg & 0xf;
  749. if ((rvar > 9) || !reg)
  750. goto sigill;
  751. if (reg & 0x10) {
  752. if (user && !access_ok(addr, 8 * (rvar + 1)))
  753. goto sigbus;
  754. } else {
  755. if (user && !access_ok(addr, 8 * rvar))
  756. goto sigbus;
  757. }
  758. if (rvar == 9)
  759. rvar = 8;
  760. for (i = 16; rvar; rvar--, i++) {
  761. LoadDW(addr, value, res);
  762. if (res)
  763. goto fault;
  764. addr += 4;
  765. regs->regs[i] = value;
  766. }
  767. if ((reg & 0xf) == 9) {
  768. LoadDW(addr, value, res);
  769. if (res)
  770. goto fault;
  771. addr += 8;
  772. regs->regs[30] = value;
  773. }
  774. if (reg & 0x10) {
  775. LoadDW(addr, value, res);
  776. if (res)
  777. goto fault;
  778. regs->regs[31] = value;
  779. }
  780. goto success;
  781. #endif /* CONFIG_64BIT */
  782. goto sigill;
  783. case mm_sdm_func:
  784. #ifdef CONFIG_64BIT
  785. reg = insn.mm_m_format.rd;
  786. rvar = reg & 0xf;
  787. if ((rvar > 9) || !reg)
  788. goto sigill;
  789. if (reg & 0x10) {
  790. if (user && !access_ok(addr, 8 * (rvar + 1)))
  791. goto sigbus;
  792. } else {
  793. if (user && !access_ok(addr, 8 * rvar))
  794. goto sigbus;
  795. }
  796. if (rvar == 9)
  797. rvar = 8;
  798. for (i = 16; rvar; rvar--, i++) {
  799. value = regs->regs[i];
  800. StoreDW(addr, value, res);
  801. if (res)
  802. goto fault;
  803. addr += 8;
  804. }
  805. if ((reg & 0xf) == 9) {
  806. value = regs->regs[30];
  807. StoreDW(addr, value, res);
  808. if (res)
  809. goto fault;
  810. addr += 8;
  811. }
  812. if (reg & 0x10) {
  813. value = regs->regs[31];
  814. StoreDW(addr, value, res);
  815. if (res)
  816. goto fault;
  817. }
  818. goto success;
  819. #endif /* CONFIG_64BIT */
  820. goto sigill;
  821. /* LWC2, SWC2, LDC2, SDC2 are not serviced */
  822. }
  823. goto sigbus;
  824. case mm_pool32c_op:
  825. switch (insn.mm_m_format.func) {
  826. case mm_lwu_func:
  827. reg = insn.mm_m_format.rd;
  828. goto loadWU;
  829. }
  830. /* LL,SC,LLD,SCD are not serviced */
  831. goto sigbus;
  832. #ifdef CONFIG_MIPS_FP_SUPPORT
  833. case mm_pool32f_op:
  834. switch (insn.mm_x_format.func) {
  835. case mm_lwxc1_func:
  836. case mm_swxc1_func:
  837. case mm_ldxc1_func:
  838. case mm_sdxc1_func:
  839. goto fpu_emul;
  840. }
  841. goto sigbus;
  842. case mm_ldc132_op:
  843. case mm_sdc132_op:
  844. case mm_lwc132_op:
  845. case mm_swc132_op: {
  846. void __user *fault_addr = NULL;
  847. fpu_emul:
  848. /* roll back jump/branch */
  849. regs->cp0_epc = origpc;
  850. regs->regs[31] = orig31;
  851. die_if_kernel("Unaligned FP access in kernel code", regs);
  852. BUG_ON(!used_math());
  853. BUG_ON(!is_fpu_owner());
  854. res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  855. &fault_addr);
  856. own_fpu(1); /* restore FPU state */
  857. /* If something went wrong, signal */
  858. process_fpemu_return(res, fault_addr, 0);
  859. if (res == 0)
  860. goto success;
  861. return;
  862. }
  863. #endif /* CONFIG_MIPS_FP_SUPPORT */
  864. case mm_lh32_op:
  865. reg = insn.mm_i_format.rt;
  866. goto loadHW;
  867. case mm_lhu32_op:
  868. reg = insn.mm_i_format.rt;
  869. goto loadHWU;
  870. case mm_lw32_op:
  871. reg = insn.mm_i_format.rt;
  872. goto loadW;
  873. case mm_sh32_op:
  874. reg = insn.mm_i_format.rt;
  875. goto storeHW;
  876. case mm_sw32_op:
  877. reg = insn.mm_i_format.rt;
  878. goto storeW;
  879. case mm_ld32_op:
  880. reg = insn.mm_i_format.rt;
  881. goto loadDW;
  882. case mm_sd32_op:
  883. reg = insn.mm_i_format.rt;
  884. goto storeDW;
  885. case mm_pool16c_op:
  886. switch (insn.mm16_m_format.func) {
  887. case mm_lwm16_op:
  888. reg = insn.mm16_m_format.rlist;
  889. rvar = reg + 1;
  890. if (user && !access_ok(addr, 4 * rvar))
  891. goto sigbus;
  892. for (i = 16; rvar; rvar--, i++) {
  893. LoadW(addr, value, res);
  894. if (res)
  895. goto fault;
  896. addr += 4;
  897. regs->regs[i] = value;
  898. }
  899. LoadW(addr, value, res);
  900. if (res)
  901. goto fault;
  902. regs->regs[31] = value;
  903. goto success;
  904. case mm_swm16_op:
  905. reg = insn.mm16_m_format.rlist;
  906. rvar = reg + 1;
  907. if (user && !access_ok(addr, 4 * rvar))
  908. goto sigbus;
  909. for (i = 16; rvar; rvar--, i++) {
  910. value = regs->regs[i];
  911. StoreW(addr, value, res);
  912. if (res)
  913. goto fault;
  914. addr += 4;
  915. }
  916. value = regs->regs[31];
  917. StoreW(addr, value, res);
  918. if (res)
  919. goto fault;
  920. goto success;
  921. }
  922. goto sigbus;
  923. case mm_lhu16_op:
  924. reg = reg16to32[insn.mm16_rb_format.rt];
  925. goto loadHWU;
  926. case mm_lw16_op:
  927. reg = reg16to32[insn.mm16_rb_format.rt];
  928. goto loadW;
  929. case mm_sh16_op:
  930. reg = reg16to32st[insn.mm16_rb_format.rt];
  931. goto storeHW;
  932. case mm_sw16_op:
  933. reg = reg16to32st[insn.mm16_rb_format.rt];
  934. goto storeW;
  935. case mm_lwsp16_op:
  936. reg = insn.mm16_r5_format.rt;
  937. goto loadW;
  938. case mm_swsp16_op:
  939. reg = insn.mm16_r5_format.rt;
  940. goto storeW;
  941. case mm_lwgp16_op:
  942. reg = reg16to32[insn.mm16_r3_format.rt];
  943. goto loadW;
  944. default:
  945. goto sigill;
  946. }
  947. loadHW:
  948. if (user && !access_ok(addr, 2))
  949. goto sigbus;
  950. LoadHW(addr, value, res);
  951. if (res)
  952. goto fault;
  953. regs->regs[reg] = value;
  954. goto success;
  955. loadHWU:
  956. if (user && !access_ok(addr, 2))
  957. goto sigbus;
  958. LoadHWU(addr, value, res);
  959. if (res)
  960. goto fault;
  961. regs->regs[reg] = value;
  962. goto success;
  963. loadW:
  964. if (user && !access_ok(addr, 4))
  965. goto sigbus;
  966. LoadW(addr, value, res);
  967. if (res)
  968. goto fault;
  969. regs->regs[reg] = value;
  970. goto success;
  971. loadWU:
  972. #ifdef CONFIG_64BIT
  973. /*
  974. * A 32-bit kernel might be running on a 64-bit processor. But
  975. * if we're on a 32-bit processor and an i-cache incoherency
  976. * or race makes us see a 64-bit instruction here the sdl/sdr
  977. * would blow up, so for now we don't handle unaligned 64-bit
  978. * instructions on 32-bit kernels.
  979. */
  980. if (user && !access_ok(addr, 4))
  981. goto sigbus;
  982. LoadWU(addr, value, res);
  983. if (res)
  984. goto fault;
  985. regs->regs[reg] = value;
  986. goto success;
  987. #endif /* CONFIG_64BIT */
  988. /* Cannot handle 64-bit instructions in 32-bit kernel */
  989. goto sigill;
  990. loadDW:
  991. #ifdef CONFIG_64BIT
  992. /*
  993. * A 32-bit kernel might be running on a 64-bit processor. But
  994. * if we're on a 32-bit processor and an i-cache incoherency
  995. * or race makes us see a 64-bit instruction here the sdl/sdr
  996. * would blow up, so for now we don't handle unaligned 64-bit
  997. * instructions on 32-bit kernels.
  998. */
  999. if (user && !access_ok(addr, 8))
  1000. goto sigbus;
  1001. LoadDW(addr, value, res);
  1002. if (res)
  1003. goto fault;
  1004. regs->regs[reg] = value;
  1005. goto success;
  1006. #endif /* CONFIG_64BIT */
  1007. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1008. goto sigill;
  1009. storeHW:
  1010. if (user && !access_ok(addr, 2))
  1011. goto sigbus;
  1012. value = regs->regs[reg];
  1013. StoreHW(addr, value, res);
  1014. if (res)
  1015. goto fault;
  1016. goto success;
  1017. storeW:
  1018. if (user && !access_ok(addr, 4))
  1019. goto sigbus;
  1020. value = regs->regs[reg];
  1021. StoreW(addr, value, res);
  1022. if (res)
  1023. goto fault;
  1024. goto success;
  1025. storeDW:
  1026. #ifdef CONFIG_64BIT
  1027. /*
  1028. * A 32-bit kernel might be running on a 64-bit processor. But
  1029. * if we're on a 32-bit processor and an i-cache incoherency
  1030. * or race makes us see a 64-bit instruction here the sdl/sdr
  1031. * would blow up, so for now we don't handle unaligned 64-bit
  1032. * instructions on 32-bit kernels.
  1033. */
  1034. if (user && !access_ok(addr, 8))
  1035. goto sigbus;
  1036. value = regs->regs[reg];
  1037. StoreDW(addr, value, res);
  1038. if (res)
  1039. goto fault;
  1040. goto success;
  1041. #endif /* CONFIG_64BIT */
  1042. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1043. goto sigill;
  1044. success:
  1045. regs->cp0_epc = contpc; /* advance or branch */
  1046. #ifdef CONFIG_DEBUG_FS
  1047. unaligned_instructions++;
  1048. #endif
  1049. return;
  1050. fault:
  1051. /* roll back jump/branch */
  1052. regs->cp0_epc = origpc;
  1053. regs->regs[31] = orig31;
  1054. /* Did we have an exception handler installed? */
  1055. if (fixup_exception(regs))
  1056. return;
  1057. die_if_kernel("Unhandled kernel unaligned access", regs);
  1058. force_sig(SIGSEGV);
  1059. return;
  1060. sigbus:
  1061. die_if_kernel("Unhandled kernel unaligned access", regs);
  1062. force_sig(SIGBUS);
  1063. return;
  1064. sigill:
  1065. die_if_kernel
  1066. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1067. force_sig(SIGILL);
  1068. }
  1069. static void emulate_load_store_MIPS16e(struct pt_regs *regs, void __user * addr)
  1070. {
  1071. unsigned long value;
  1072. unsigned int res;
  1073. int reg;
  1074. unsigned long orig31;
  1075. u16 __user *pc16;
  1076. unsigned long origpc;
  1077. union mips16e_instruction mips16inst, oldinst;
  1078. unsigned int opcode;
  1079. int extended = 0;
  1080. bool user = user_mode(regs);
  1081. origpc = regs->cp0_epc;
  1082. orig31 = regs->regs[31];
  1083. pc16 = (unsigned short __user *)msk_isa16_mode(origpc);
  1084. /*
  1085. * This load never faults.
  1086. */
  1087. __get_user(mips16inst.full, pc16);
  1088. oldinst = mips16inst;
  1089. /* skip EXTEND instruction */
  1090. if (mips16inst.ri.opcode == MIPS16e_extend_op) {
  1091. extended = 1;
  1092. pc16++;
  1093. __get_user(mips16inst.full, pc16);
  1094. } else if (delay_slot(regs)) {
  1095. /* skip jump instructions */
  1096. /* JAL/JALX are 32 bits but have OPCODE in first short int */
  1097. if (mips16inst.ri.opcode == MIPS16e_jal_op)
  1098. pc16++;
  1099. pc16++;
  1100. if (get_user(mips16inst.full, pc16))
  1101. goto sigbus;
  1102. }
  1103. opcode = mips16inst.ri.opcode;
  1104. switch (opcode) {
  1105. case MIPS16e_i64_op: /* I64 or RI64 instruction */
  1106. switch (mips16inst.i64.func) { /* I64/RI64 func field check */
  1107. case MIPS16e_ldpc_func:
  1108. case MIPS16e_ldsp_func:
  1109. reg = reg16to32[mips16inst.ri64.ry];
  1110. goto loadDW;
  1111. case MIPS16e_sdsp_func:
  1112. reg = reg16to32[mips16inst.ri64.ry];
  1113. goto writeDW;
  1114. case MIPS16e_sdrasp_func:
  1115. reg = 29; /* GPRSP */
  1116. goto writeDW;
  1117. }
  1118. goto sigbus;
  1119. case MIPS16e_swsp_op:
  1120. reg = reg16to32[mips16inst.ri.rx];
  1121. if (extended && cpu_has_mips16e2)
  1122. switch (mips16inst.ri.imm >> 5) {
  1123. case 0: /* SWSP */
  1124. case 1: /* SWGP */
  1125. break;
  1126. case 2: /* SHGP */
  1127. opcode = MIPS16e_sh_op;
  1128. break;
  1129. default:
  1130. goto sigbus;
  1131. }
  1132. break;
  1133. case MIPS16e_lwpc_op:
  1134. reg = reg16to32[mips16inst.ri.rx];
  1135. break;
  1136. case MIPS16e_lwsp_op:
  1137. reg = reg16to32[mips16inst.ri.rx];
  1138. if (extended && cpu_has_mips16e2)
  1139. switch (mips16inst.ri.imm >> 5) {
  1140. case 0: /* LWSP */
  1141. case 1: /* LWGP */
  1142. break;
  1143. case 2: /* LHGP */
  1144. opcode = MIPS16e_lh_op;
  1145. break;
  1146. case 4: /* LHUGP */
  1147. opcode = MIPS16e_lhu_op;
  1148. break;
  1149. default:
  1150. goto sigbus;
  1151. }
  1152. break;
  1153. case MIPS16e_i8_op:
  1154. if (mips16inst.i8.func != MIPS16e_swrasp_func)
  1155. goto sigbus;
  1156. reg = 29; /* GPRSP */
  1157. break;
  1158. default:
  1159. reg = reg16to32[mips16inst.rri.ry];
  1160. break;
  1161. }
  1162. switch (opcode) {
  1163. case MIPS16e_lb_op:
  1164. case MIPS16e_lbu_op:
  1165. case MIPS16e_sb_op:
  1166. goto sigbus;
  1167. case MIPS16e_lh_op:
  1168. if (user && !access_ok(addr, 2))
  1169. goto sigbus;
  1170. LoadHW(addr, value, res);
  1171. if (res)
  1172. goto fault;
  1173. MIPS16e_compute_return_epc(regs, &oldinst);
  1174. regs->regs[reg] = value;
  1175. break;
  1176. case MIPS16e_lhu_op:
  1177. if (user && !access_ok(addr, 2))
  1178. goto sigbus;
  1179. LoadHWU(addr, value, res);
  1180. if (res)
  1181. goto fault;
  1182. MIPS16e_compute_return_epc(regs, &oldinst);
  1183. regs->regs[reg] = value;
  1184. break;
  1185. case MIPS16e_lw_op:
  1186. case MIPS16e_lwpc_op:
  1187. case MIPS16e_lwsp_op:
  1188. if (user && !access_ok(addr, 4))
  1189. goto sigbus;
  1190. LoadW(addr, value, res);
  1191. if (res)
  1192. goto fault;
  1193. MIPS16e_compute_return_epc(regs, &oldinst);
  1194. regs->regs[reg] = value;
  1195. break;
  1196. case MIPS16e_lwu_op:
  1197. #ifdef CONFIG_64BIT
  1198. /*
  1199. * A 32-bit kernel might be running on a 64-bit processor. But
  1200. * if we're on a 32-bit processor and an i-cache incoherency
  1201. * or race makes us see a 64-bit instruction here the sdl/sdr
  1202. * would blow up, so for now we don't handle unaligned 64-bit
  1203. * instructions on 32-bit kernels.
  1204. */
  1205. if (user && !access_ok(addr, 4))
  1206. goto sigbus;
  1207. LoadWU(addr, value, res);
  1208. if (res)
  1209. goto fault;
  1210. MIPS16e_compute_return_epc(regs, &oldinst);
  1211. regs->regs[reg] = value;
  1212. break;
  1213. #endif /* CONFIG_64BIT */
  1214. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1215. goto sigill;
  1216. case MIPS16e_ld_op:
  1217. loadDW:
  1218. #ifdef CONFIG_64BIT
  1219. /*
  1220. * A 32-bit kernel might be running on a 64-bit processor. But
  1221. * if we're on a 32-bit processor and an i-cache incoherency
  1222. * or race makes us see a 64-bit instruction here the sdl/sdr
  1223. * would blow up, so for now we don't handle unaligned 64-bit
  1224. * instructions on 32-bit kernels.
  1225. */
  1226. if (user && !access_ok(addr, 8))
  1227. goto sigbus;
  1228. LoadDW(addr, value, res);
  1229. if (res)
  1230. goto fault;
  1231. MIPS16e_compute_return_epc(regs, &oldinst);
  1232. regs->regs[reg] = value;
  1233. break;
  1234. #endif /* CONFIG_64BIT */
  1235. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1236. goto sigill;
  1237. case MIPS16e_sh_op:
  1238. if (user && !access_ok(addr, 2))
  1239. goto sigbus;
  1240. MIPS16e_compute_return_epc(regs, &oldinst);
  1241. value = regs->regs[reg];
  1242. StoreHW(addr, value, res);
  1243. if (res)
  1244. goto fault;
  1245. break;
  1246. case MIPS16e_sw_op:
  1247. case MIPS16e_swsp_op:
  1248. case MIPS16e_i8_op: /* actually - MIPS16e_swrasp_func */
  1249. if (user && !access_ok(addr, 4))
  1250. goto sigbus;
  1251. MIPS16e_compute_return_epc(regs, &oldinst);
  1252. value = regs->regs[reg];
  1253. StoreW(addr, value, res);
  1254. if (res)
  1255. goto fault;
  1256. break;
  1257. case MIPS16e_sd_op:
  1258. writeDW:
  1259. #ifdef CONFIG_64BIT
  1260. /*
  1261. * A 32-bit kernel might be running on a 64-bit processor. But
  1262. * if we're on a 32-bit processor and an i-cache incoherency
  1263. * or race makes us see a 64-bit instruction here the sdl/sdr
  1264. * would blow up, so for now we don't handle unaligned 64-bit
  1265. * instructions on 32-bit kernels.
  1266. */
  1267. if (user && !access_ok(addr, 8))
  1268. goto sigbus;
  1269. MIPS16e_compute_return_epc(regs, &oldinst);
  1270. value = regs->regs[reg];
  1271. StoreDW(addr, value, res);
  1272. if (res)
  1273. goto fault;
  1274. break;
  1275. #endif /* CONFIG_64BIT */
  1276. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1277. goto sigill;
  1278. default:
  1279. /*
  1280. * Pheeee... We encountered an yet unknown instruction or
  1281. * cache coherence problem. Die sucker, die ...
  1282. */
  1283. goto sigill;
  1284. }
  1285. #ifdef CONFIG_DEBUG_FS
  1286. unaligned_instructions++;
  1287. #endif
  1288. return;
  1289. fault:
  1290. /* roll back jump/branch */
  1291. regs->cp0_epc = origpc;
  1292. regs->regs[31] = orig31;
  1293. /* Did we have an exception handler installed? */
  1294. if (fixup_exception(regs))
  1295. return;
  1296. die_if_kernel("Unhandled kernel unaligned access", regs);
  1297. force_sig(SIGSEGV);
  1298. return;
  1299. sigbus:
  1300. die_if_kernel("Unhandled kernel unaligned access", regs);
  1301. force_sig(SIGBUS);
  1302. return;
  1303. sigill:
  1304. die_if_kernel
  1305. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1306. force_sig(SIGILL);
  1307. }
  1308. asmlinkage void do_ade(struct pt_regs *regs)
  1309. {
  1310. enum ctx_state prev_state;
  1311. unsigned int *pc;
  1312. prev_state = exception_enter();
  1313. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
  1314. 1, regs, regs->cp0_badvaddr);
  1315. #ifdef CONFIG_64BIT
  1316. /*
  1317. * check, if we are hitting space between CPU implemented maximum
  1318. * virtual user address and 64bit maximum virtual user address
  1319. * and do exception handling to get EFAULTs for get_user/put_user
  1320. */
  1321. if ((regs->cp0_badvaddr >= (1UL << cpu_vmbits)) &&
  1322. (regs->cp0_badvaddr < XKSSEG)) {
  1323. if (fixup_exception(regs)) {
  1324. current->thread.cp0_baduaddr = regs->cp0_badvaddr;
  1325. return;
  1326. }
  1327. goto sigbus;
  1328. }
  1329. #endif
  1330. /*
  1331. * Did we catch a fault trying to load an instruction?
  1332. */
  1333. if (regs->cp0_badvaddr == regs->cp0_epc)
  1334. goto sigbus;
  1335. if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
  1336. goto sigbus;
  1337. if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
  1338. goto sigbus;
  1339. /*
  1340. * Do branch emulation only if we didn't forward the exception.
  1341. * This is all so but ugly ...
  1342. */
  1343. /*
  1344. * Are we running in microMIPS mode?
  1345. */
  1346. if (get_isa16_mode(regs->cp0_epc)) {
  1347. /*
  1348. * Did we catch a fault trying to load an instruction in
  1349. * 16-bit mode?
  1350. */
  1351. if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
  1352. goto sigbus;
  1353. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  1354. show_registers(regs);
  1355. if (cpu_has_mmips) {
  1356. emulate_load_store_microMIPS(regs,
  1357. (void __user *)regs->cp0_badvaddr);
  1358. return;
  1359. }
  1360. if (cpu_has_mips16) {
  1361. emulate_load_store_MIPS16e(regs,
  1362. (void __user *)regs->cp0_badvaddr);
  1363. return;
  1364. }
  1365. goto sigbus;
  1366. }
  1367. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  1368. show_registers(regs);
  1369. pc = (unsigned int *)exception_epc(regs);
  1370. emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
  1371. return;
  1372. sigbus:
  1373. die_if_kernel("Kernel unaligned instruction access", regs);
  1374. force_sig(SIGBUS);
  1375. /*
  1376. * XXX On return from the signal handler we should advance the epc
  1377. */
  1378. exception_exit(prev_state);
  1379. }
  1380. #ifdef CONFIG_DEBUG_FS
  1381. static int __init debugfs_unaligned(void)
  1382. {
  1383. debugfs_create_u32("unaligned_instructions", S_IRUGO, mips_debugfs_dir,
  1384. &unaligned_instructions);
  1385. debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
  1386. mips_debugfs_dir, &unaligned_action);
  1387. return 0;
  1388. }
  1389. arch_initcall(debugfs_unaligned);
  1390. #endif