unaligned.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  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/fpu.h>
  88. #include <asm/fpu_emulator.h>
  89. #include <asm/inst.h>
  90. #include <asm/uaccess.h>
  91. #include <asm/fpu.h>
  92. #include <asm/fpu_emulator.h>
  93. #define STR(x) __STR(x)
  94. #define __STR(x) #x
  95. enum {
  96. UNALIGNED_ACTION_QUIET,
  97. UNALIGNED_ACTION_SIGNAL,
  98. UNALIGNED_ACTION_SHOW,
  99. };
  100. #ifdef CONFIG_DEBUG_FS
  101. static u32 unaligned_instructions;
  102. static u32 unaligned_action;
  103. #else
  104. #define unaligned_action UNALIGNED_ACTION_QUIET
  105. #endif
  106. extern void show_registers(struct pt_regs *regs);
  107. #ifdef __BIG_ENDIAN
  108. #define LoadHW(addr, value, res) \
  109. __asm__ __volatile__ (".set\tnoat\n" \
  110. "1:\t"user_lb("%0", "0(%2)")"\n" \
  111. "2:\t"user_lbu("$1", "1(%2)")"\n\t" \
  112. "sll\t%0, 0x8\n\t" \
  113. "or\t%0, $1\n\t" \
  114. "li\t%1, 0\n" \
  115. "3:\t.set\tat\n\t" \
  116. ".insn\n\t" \
  117. ".section\t.fixup,\"ax\"\n\t" \
  118. "4:\tli\t%1, %3\n\t" \
  119. "j\t3b\n\t" \
  120. ".previous\n\t" \
  121. ".section\t__ex_table,\"a\"\n\t" \
  122. STR(PTR)"\t1b, 4b\n\t" \
  123. STR(PTR)"\t2b, 4b\n\t" \
  124. ".previous" \
  125. : "=&r" (value), "=r" (res) \
  126. : "r" (addr), "i" (-EFAULT));
  127. #define LoadW(addr, value, res) \
  128. __asm__ __volatile__ ( \
  129. "1:\t"user_lwl("%0", "(%2)")"\n" \
  130. "2:\t"user_lwr("%0", "3(%2)")"\n\t" \
  131. "li\t%1, 0\n" \
  132. "3:\n\t" \
  133. ".insn\n\t" \
  134. ".section\t.fixup,\"ax\"\n\t" \
  135. "4:\tli\t%1, %3\n\t" \
  136. "j\t3b\n\t" \
  137. ".previous\n\t" \
  138. ".section\t__ex_table,\"a\"\n\t" \
  139. STR(PTR)"\t1b, 4b\n\t" \
  140. STR(PTR)"\t2b, 4b\n\t" \
  141. ".previous" \
  142. : "=&r" (value), "=r" (res) \
  143. : "r" (addr), "i" (-EFAULT));
  144. #define LoadHWU(addr, value, res) \
  145. __asm__ __volatile__ ( \
  146. ".set\tnoat\n" \
  147. "1:\t"user_lbu("%0", "0(%2)")"\n" \
  148. "2:\t"user_lbu("$1", "1(%2)")"\n\t" \
  149. "sll\t%0, 0x8\n\t" \
  150. "or\t%0, $1\n\t" \
  151. "li\t%1, 0\n" \
  152. "3:\n\t" \
  153. ".insn\n\t" \
  154. ".set\tat\n\t" \
  155. ".section\t.fixup,\"ax\"\n\t" \
  156. "4:\tli\t%1, %3\n\t" \
  157. "j\t3b\n\t" \
  158. ".previous\n\t" \
  159. ".section\t__ex_table,\"a\"\n\t" \
  160. STR(PTR)"\t1b, 4b\n\t" \
  161. STR(PTR)"\t2b, 4b\n\t" \
  162. ".previous" \
  163. : "=&r" (value), "=r" (res) \
  164. : "r" (addr), "i" (-EFAULT));
  165. #define LoadWU(addr, value, res) \
  166. __asm__ __volatile__ ( \
  167. "1:\t"user_lwl("%0", "(%2)")"\n" \
  168. "2:\t"user_lwr("%0", "3(%2)")"\n\t" \
  169. "dsll\t%0, %0, 32\n\t" \
  170. "dsrl\t%0, %0, 32\n\t" \
  171. "li\t%1, 0\n" \
  172. "3:\n\t" \
  173. ".insn\n\t" \
  174. "\t.section\t.fixup,\"ax\"\n\t" \
  175. "4:\tli\t%1, %3\n\t" \
  176. "j\t3b\n\t" \
  177. ".previous\n\t" \
  178. ".section\t__ex_table,\"a\"\n\t" \
  179. STR(PTR)"\t1b, 4b\n\t" \
  180. STR(PTR)"\t2b, 4b\n\t" \
  181. ".previous" \
  182. : "=&r" (value), "=r" (res) \
  183. : "r" (addr), "i" (-EFAULT));
  184. #define LoadDW(addr, value, res) \
  185. __asm__ __volatile__ ( \
  186. "1:\tldl\t%0, (%2)\n" \
  187. "2:\tldr\t%0, 7(%2)\n\t" \
  188. "li\t%1, 0\n" \
  189. "3:\n\t" \
  190. ".insn\n\t" \
  191. "\t.section\t.fixup,\"ax\"\n\t" \
  192. "4:\tli\t%1, %3\n\t" \
  193. "j\t3b\n\t" \
  194. ".previous\n\t" \
  195. ".section\t__ex_table,\"a\"\n\t" \
  196. STR(PTR)"\t1b, 4b\n\t" \
  197. STR(PTR)"\t2b, 4b\n\t" \
  198. ".previous" \
  199. : "=&r" (value), "=r" (res) \
  200. : "r" (addr), "i" (-EFAULT));
  201. #define StoreHW(addr, value, res) \
  202. __asm__ __volatile__ ( \
  203. ".set\tnoat\n" \
  204. "1:\t"user_sb("%1", "1(%2)")"\n" \
  205. "srl\t$1, %1, 0x8\n" \
  206. "2:\t"user_sb("$1", "0(%2)")"\n" \
  207. ".set\tat\n\t" \
  208. "li\t%0, 0\n" \
  209. "3:\n\t" \
  210. ".insn\n\t" \
  211. ".section\t.fixup,\"ax\"\n\t" \
  212. "4:\tli\t%0, %3\n\t" \
  213. "j\t3b\n\t" \
  214. ".previous\n\t" \
  215. ".section\t__ex_table,\"a\"\n\t" \
  216. STR(PTR)"\t1b, 4b\n\t" \
  217. STR(PTR)"\t2b, 4b\n\t" \
  218. ".previous" \
  219. : "=r" (res) \
  220. : "r" (value), "r" (addr), "i" (-EFAULT));
  221. #define StoreW(addr, value, res) \
  222. __asm__ __volatile__ ( \
  223. "1:\t"user_swl("%1", "(%2)")"\n" \
  224. "2:\t"user_swr("%1", "3(%2)")"\n\t" \
  225. "li\t%0, 0\n" \
  226. "3:\n\t" \
  227. ".insn\n\t" \
  228. ".section\t.fixup,\"ax\"\n\t" \
  229. "4:\tli\t%0, %3\n\t" \
  230. "j\t3b\n\t" \
  231. ".previous\n\t" \
  232. ".section\t__ex_table,\"a\"\n\t" \
  233. STR(PTR)"\t1b, 4b\n\t" \
  234. STR(PTR)"\t2b, 4b\n\t" \
  235. ".previous" \
  236. : "=r" (res) \
  237. : "r" (value), "r" (addr), "i" (-EFAULT));
  238. #define StoreDW(addr, value, res) \
  239. __asm__ __volatile__ ( \
  240. "1:\tsdl\t%1,(%2)\n" \
  241. "2:\tsdr\t%1, 7(%2)\n\t" \
  242. "li\t%0, 0\n" \
  243. "3:\n\t" \
  244. ".insn\n\t" \
  245. ".section\t.fixup,\"ax\"\n\t" \
  246. "4:\tli\t%0, %3\n\t" \
  247. "j\t3b\n\t" \
  248. ".previous\n\t" \
  249. ".section\t__ex_table,\"a\"\n\t" \
  250. STR(PTR)"\t1b, 4b\n\t" \
  251. STR(PTR)"\t2b, 4b\n\t" \
  252. ".previous" \
  253. : "=r" (res) \
  254. : "r" (value), "r" (addr), "i" (-EFAULT));
  255. #endif
  256. #ifdef __LITTLE_ENDIAN
  257. #define LoadHW(addr, value, res) \
  258. __asm__ __volatile__ (".set\tnoat\n" \
  259. "1:\t"user_lb("%0", "1(%2)")"\n" \
  260. "2:\t"user_lbu("$1", "0(%2)")"\n\t" \
  261. "sll\t%0, 0x8\n\t" \
  262. "or\t%0, $1\n\t" \
  263. "li\t%1, 0\n" \
  264. "3:\t.set\tat\n\t" \
  265. ".insn\n\t" \
  266. ".section\t.fixup,\"ax\"\n\t" \
  267. "4:\tli\t%1, %3\n\t" \
  268. "j\t3b\n\t" \
  269. ".previous\n\t" \
  270. ".section\t__ex_table,\"a\"\n\t" \
  271. STR(PTR)"\t1b, 4b\n\t" \
  272. STR(PTR)"\t2b, 4b\n\t" \
  273. ".previous" \
  274. : "=&r" (value), "=r" (res) \
  275. : "r" (addr), "i" (-EFAULT));
  276. #define LoadW(addr, value, res) \
  277. __asm__ __volatile__ ( \
  278. "1:\t"user_lwl("%0", "3(%2)")"\n" \
  279. "2:\t"user_lwr("%0", "(%2)")"\n\t" \
  280. "li\t%1, 0\n" \
  281. "3:\n\t" \
  282. ".insn\n\t" \
  283. ".section\t.fixup,\"ax\"\n\t" \
  284. "4:\tli\t%1, %3\n\t" \
  285. "j\t3b\n\t" \
  286. ".previous\n\t" \
  287. ".section\t__ex_table,\"a\"\n\t" \
  288. STR(PTR)"\t1b, 4b\n\t" \
  289. STR(PTR)"\t2b, 4b\n\t" \
  290. ".previous" \
  291. : "=&r" (value), "=r" (res) \
  292. : "r" (addr), "i" (-EFAULT));
  293. #define LoadHWU(addr, value, res) \
  294. __asm__ __volatile__ ( \
  295. ".set\tnoat\n" \
  296. "1:\t"user_lbu("%0", "1(%2)")"\n" \
  297. "2:\t"user_lbu("$1", "0(%2)")"\n\t" \
  298. "sll\t%0, 0x8\n\t" \
  299. "or\t%0, $1\n\t" \
  300. "li\t%1, 0\n" \
  301. "3:\n\t" \
  302. ".insn\n\t" \
  303. ".set\tat\n\t" \
  304. ".section\t.fixup,\"ax\"\n\t" \
  305. "4:\tli\t%1, %3\n\t" \
  306. "j\t3b\n\t" \
  307. ".previous\n\t" \
  308. ".section\t__ex_table,\"a\"\n\t" \
  309. STR(PTR)"\t1b, 4b\n\t" \
  310. STR(PTR)"\t2b, 4b\n\t" \
  311. ".previous" \
  312. : "=&r" (value), "=r" (res) \
  313. : "r" (addr), "i" (-EFAULT));
  314. #define LoadWU(addr, value, res) \
  315. __asm__ __volatile__ ( \
  316. "1:\t"user_lwl("%0", "3(%2)")"\n" \
  317. "2:\t"user_lwr("%0", "(%2)")"\n\t" \
  318. "dsll\t%0, %0, 32\n\t" \
  319. "dsrl\t%0, %0, 32\n\t" \
  320. "li\t%1, 0\n" \
  321. "3:\n\t" \
  322. ".insn\n\t" \
  323. "\t.section\t.fixup,\"ax\"\n\t" \
  324. "4:\tli\t%1, %3\n\t" \
  325. "j\t3b\n\t" \
  326. ".previous\n\t" \
  327. ".section\t__ex_table,\"a\"\n\t" \
  328. STR(PTR)"\t1b, 4b\n\t" \
  329. STR(PTR)"\t2b, 4b\n\t" \
  330. ".previous" \
  331. : "=&r" (value), "=r" (res) \
  332. : "r" (addr), "i" (-EFAULT));
  333. #define LoadDW(addr, value, res) \
  334. __asm__ __volatile__ ( \
  335. "1:\tldl\t%0, 7(%2)\n" \
  336. "2:\tldr\t%0, (%2)\n\t" \
  337. "li\t%1, 0\n" \
  338. "3:\n\t" \
  339. ".insn\n\t" \
  340. "\t.section\t.fixup,\"ax\"\n\t" \
  341. "4:\tli\t%1, %3\n\t" \
  342. "j\t3b\n\t" \
  343. ".previous\n\t" \
  344. ".section\t__ex_table,\"a\"\n\t" \
  345. STR(PTR)"\t1b, 4b\n\t" \
  346. STR(PTR)"\t2b, 4b\n\t" \
  347. ".previous" \
  348. : "=&r" (value), "=r" (res) \
  349. : "r" (addr), "i" (-EFAULT));
  350. #define StoreHW(addr, value, res) \
  351. __asm__ __volatile__ ( \
  352. ".set\tnoat\n" \
  353. "1:\t"user_sb("%1", "0(%2)")"\n" \
  354. "srl\t$1,%1, 0x8\n" \
  355. "2:\t"user_sb("$1", "1(%2)")"\n" \
  356. ".set\tat\n\t" \
  357. "li\t%0, 0\n" \
  358. "3:\n\t" \
  359. ".insn\n\t" \
  360. ".section\t.fixup,\"ax\"\n\t" \
  361. "4:\tli\t%0, %3\n\t" \
  362. "j\t3b\n\t" \
  363. ".previous\n\t" \
  364. ".section\t__ex_table,\"a\"\n\t" \
  365. STR(PTR)"\t1b, 4b\n\t" \
  366. STR(PTR)"\t2b, 4b\n\t" \
  367. ".previous" \
  368. : "=r" (res) \
  369. : "r" (value), "r" (addr), "i" (-EFAULT));
  370. #define StoreW(addr, value, res) \
  371. __asm__ __volatile__ ( \
  372. "1:\t"user_swl("%1", "3(%2)")"\n" \
  373. "2:\t"user_swr("%1", "(%2)")"\n\t" \
  374. "li\t%0, 0\n" \
  375. "3:\n\t" \
  376. ".insn\n\t" \
  377. ".section\t.fixup,\"ax\"\n\t" \
  378. "4:\tli\t%0, %3\n\t" \
  379. "j\t3b\n\t" \
  380. ".previous\n\t" \
  381. ".section\t__ex_table,\"a\"\n\t" \
  382. STR(PTR)"\t1b, 4b\n\t" \
  383. STR(PTR)"\t2b, 4b\n\t" \
  384. ".previous" \
  385. : "=r" (res) \
  386. : "r" (value), "r" (addr), "i" (-EFAULT));
  387. #define StoreDW(addr, value, res) \
  388. __asm__ __volatile__ ( \
  389. "1:\tsdl\t%1, 7(%2)\n" \
  390. "2:\tsdr\t%1, (%2)\n\t" \
  391. "li\t%0, 0\n" \
  392. "3:\n\t" \
  393. ".insn\n\t" \
  394. ".section\t.fixup,\"ax\"\n\t" \
  395. "4:\tli\t%0, %3\n\t" \
  396. "j\t3b\n\t" \
  397. ".previous\n\t" \
  398. ".section\t__ex_table,\"a\"\n\t" \
  399. STR(PTR)"\t1b, 4b\n\t" \
  400. STR(PTR)"\t2b, 4b\n\t" \
  401. ".previous" \
  402. : "=r" (res) \
  403. : "r" (value), "r" (addr), "i" (-EFAULT));
  404. #endif
  405. static void emulate_load_store_insn(struct pt_regs *regs,
  406. void __user *addr, unsigned int __user *pc)
  407. {
  408. union mips_instruction insn;
  409. unsigned long value;
  410. unsigned int res;
  411. unsigned long origpc;
  412. unsigned long orig31;
  413. void __user *fault_addr = NULL;
  414. #ifdef CONFIG_EVA
  415. mm_segment_t seg;
  416. #endif
  417. origpc = (unsigned long)pc;
  418. orig31 = regs->regs[31];
  419. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
  420. /*
  421. * This load never faults.
  422. */
  423. __get_user(insn.word, pc);
  424. switch (insn.i_format.opcode) {
  425. /*
  426. * These are instructions that a compiler doesn't generate. We
  427. * can assume therefore that the code is MIPS-aware and
  428. * really buggy. Emulating these instructions would break the
  429. * semantics anyway.
  430. */
  431. case ll_op:
  432. case lld_op:
  433. case sc_op:
  434. case scd_op:
  435. /*
  436. * For these instructions the only way to create an address
  437. * error is an attempted access to kernel/supervisor address
  438. * space.
  439. */
  440. case ldl_op:
  441. case ldr_op:
  442. case lwl_op:
  443. case lwr_op:
  444. case sdl_op:
  445. case sdr_op:
  446. case swl_op:
  447. case swr_op:
  448. case lb_op:
  449. case lbu_op:
  450. case sb_op:
  451. goto sigbus;
  452. /*
  453. * The remaining opcodes are the ones that are really of
  454. * interest.
  455. */
  456. #ifdef CONFIG_EVA
  457. case spec3_op:
  458. /*
  459. * we can land here only from kernel accessing user memory,
  460. * so we need to "switch" the address limit to user space, so
  461. * address check can work properly.
  462. */
  463. seg = get_fs();
  464. set_fs(USER_DS);
  465. switch (insn.spec3_format.func) {
  466. case lhe_op:
  467. if (!access_ok(VERIFY_READ, addr, 2)) {
  468. set_fs(seg);
  469. goto sigbus;
  470. }
  471. LoadHW(addr, value, res);
  472. if (res) {
  473. set_fs(seg);
  474. goto fault;
  475. }
  476. compute_return_epc(regs);
  477. regs->regs[insn.spec3_format.rt] = value;
  478. break;
  479. case lwe_op:
  480. if (!access_ok(VERIFY_READ, addr, 4)) {
  481. set_fs(seg);
  482. goto sigbus;
  483. }
  484. LoadW(addr, value, res);
  485. if (res) {
  486. set_fs(seg);
  487. goto fault;
  488. }
  489. compute_return_epc(regs);
  490. regs->regs[insn.spec3_format.rt] = value;
  491. break;
  492. case lhue_op:
  493. if (!access_ok(VERIFY_READ, addr, 2)) {
  494. set_fs(seg);
  495. goto sigbus;
  496. }
  497. LoadHWU(addr, value, res);
  498. if (res) {
  499. set_fs(seg);
  500. goto fault;
  501. }
  502. compute_return_epc(regs);
  503. regs->regs[insn.spec3_format.rt] = value;
  504. break;
  505. case she_op:
  506. if (!access_ok(VERIFY_WRITE, addr, 2)) {
  507. set_fs(seg);
  508. goto sigbus;
  509. }
  510. compute_return_epc(regs);
  511. value = regs->regs[insn.spec3_format.rt];
  512. StoreHW(addr, value, res);
  513. if (res) {
  514. set_fs(seg);
  515. goto fault;
  516. }
  517. break;
  518. case swe_op:
  519. if (!access_ok(VERIFY_WRITE, addr, 4)) {
  520. set_fs(seg);
  521. goto sigbus;
  522. }
  523. compute_return_epc(regs);
  524. value = regs->regs[insn.spec3_format.rt];
  525. StoreW(addr, value, res);
  526. if (res) {
  527. set_fs(seg);
  528. goto fault;
  529. }
  530. break;
  531. default:
  532. set_fs(seg);
  533. goto sigill;
  534. }
  535. set_fs(seg);
  536. break;
  537. #endif
  538. case lh_op:
  539. if (!access_ok(VERIFY_READ, addr, 2))
  540. goto sigbus;
  541. LoadHW(addr, value, res);
  542. if (res)
  543. goto fault;
  544. compute_return_epc(regs);
  545. regs->regs[insn.i_format.rt] = value;
  546. break;
  547. case lw_op:
  548. if (!access_ok(VERIFY_READ, addr, 4))
  549. goto sigbus;
  550. LoadW(addr, value, res);
  551. if (res)
  552. goto fault;
  553. compute_return_epc(regs);
  554. regs->regs[insn.i_format.rt] = value;
  555. break;
  556. case lhu_op:
  557. if (!access_ok(VERIFY_READ, addr, 2))
  558. goto sigbus;
  559. LoadHWU(addr, value, res);
  560. if (res)
  561. goto fault;
  562. compute_return_epc(regs);
  563. regs->regs[insn.i_format.rt] = value;
  564. break;
  565. case lwu_op:
  566. #ifdef CONFIG_64BIT
  567. /*
  568. * A 32-bit kernel might be running on a 64-bit processor. But
  569. * if we're on a 32-bit processor and an i-cache incoherency
  570. * or race makes us see a 64-bit instruction here the sdl/sdr
  571. * would blow up, so for now we don't handle unaligned 64-bit
  572. * instructions on 32-bit kernels.
  573. */
  574. if (!access_ok(VERIFY_READ, addr, 4))
  575. goto sigbus;
  576. LoadWU(addr, value, res);
  577. if (res)
  578. goto fault;
  579. compute_return_epc(regs);
  580. regs->regs[insn.i_format.rt] = value;
  581. break;
  582. #endif /* CONFIG_64BIT */
  583. /* Cannot handle 64-bit instructions in 32-bit kernel */
  584. goto sigill;
  585. case ld_op:
  586. #ifdef CONFIG_64BIT
  587. /*
  588. * A 32-bit kernel might be running on a 64-bit processor. But
  589. * if we're on a 32-bit processor and an i-cache incoherency
  590. * or race makes us see a 64-bit instruction here the sdl/sdr
  591. * would blow up, so for now we don't handle unaligned 64-bit
  592. * instructions on 32-bit kernels.
  593. */
  594. if (!access_ok(VERIFY_READ, addr, 8))
  595. goto sigbus;
  596. LoadDW(addr, value, res);
  597. if (res)
  598. goto fault;
  599. compute_return_epc(regs);
  600. regs->regs[insn.i_format.rt] = value;
  601. break;
  602. #endif /* CONFIG_64BIT */
  603. /* Cannot handle 64-bit instructions in 32-bit kernel */
  604. goto sigill;
  605. case sh_op:
  606. if (!access_ok(VERIFY_WRITE, addr, 2))
  607. goto sigbus;
  608. compute_return_epc(regs);
  609. value = regs->regs[insn.i_format.rt];
  610. StoreHW(addr, value, res);
  611. if (res)
  612. goto fault;
  613. break;
  614. case sw_op:
  615. if (!access_ok(VERIFY_WRITE, addr, 4))
  616. goto sigbus;
  617. compute_return_epc(regs);
  618. value = regs->regs[insn.i_format.rt];
  619. StoreW(addr, value, res);
  620. if (res)
  621. goto fault;
  622. break;
  623. case sd_op:
  624. #ifdef CONFIG_64BIT
  625. /*
  626. * A 32-bit kernel might be running on a 64-bit processor. But
  627. * if we're on a 32-bit processor and an i-cache incoherency
  628. * or race makes us see a 64-bit instruction here the sdl/sdr
  629. * would blow up, so for now we don't handle unaligned 64-bit
  630. * instructions on 32-bit kernels.
  631. */
  632. if (!access_ok(VERIFY_WRITE, addr, 8))
  633. goto sigbus;
  634. compute_return_epc(regs);
  635. value = regs->regs[insn.i_format.rt];
  636. StoreDW(addr, value, res);
  637. if (res)
  638. goto fault;
  639. break;
  640. #endif /* CONFIG_64BIT */
  641. /* Cannot handle 64-bit instructions in 32-bit kernel */
  642. goto sigill;
  643. case lwc1_op:
  644. case ldc1_op:
  645. case swc1_op:
  646. case sdc1_op:
  647. die_if_kernel("Unaligned FP access in kernel code", regs);
  648. BUG_ON(!used_math());
  649. BUG_ON(!is_fpu_owner());
  650. lose_fpu(1); /* Save FPU state for the emulator. */
  651. res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  652. &fault_addr);
  653. own_fpu(1); /* Restore FPU state. */
  654. /* Signal if something went wrong. */
  655. process_fpemu_return(res, fault_addr);
  656. if (res == 0)
  657. break;
  658. return;
  659. /*
  660. * COP2 is available to implementor for application specific use.
  661. * It's up to applications to register a notifier chain and do
  662. * whatever they have to do, including possible sending of signals.
  663. */
  664. case lwc2_op:
  665. cu2_notifier_call_chain(CU2_LWC2_OP, regs);
  666. break;
  667. case ldc2_op:
  668. cu2_notifier_call_chain(CU2_LDC2_OP, regs);
  669. break;
  670. case swc2_op:
  671. cu2_notifier_call_chain(CU2_SWC2_OP, regs);
  672. break;
  673. case sdc2_op:
  674. cu2_notifier_call_chain(CU2_SDC2_OP, regs);
  675. break;
  676. default:
  677. /*
  678. * Pheeee... We encountered an yet unknown instruction or
  679. * cache coherence problem. Die sucker, die ...
  680. */
  681. goto sigill;
  682. }
  683. #ifdef CONFIG_DEBUG_FS
  684. unaligned_instructions++;
  685. #endif
  686. return;
  687. fault:
  688. /* roll back jump/branch */
  689. regs->cp0_epc = origpc;
  690. regs->regs[31] = orig31;
  691. /* Did we have an exception handler installed? */
  692. if (fixup_exception(regs))
  693. return;
  694. die_if_kernel("Unhandled kernel unaligned access", regs);
  695. force_sig(SIGSEGV, current);
  696. return;
  697. sigbus:
  698. die_if_kernel("Unhandled kernel unaligned access", regs);
  699. force_sig(SIGBUS, current);
  700. return;
  701. sigill:
  702. die_if_kernel
  703. ("Unhandled kernel unaligned access or invalid instruction", regs);
  704. force_sig(SIGILL, current);
  705. }
  706. /* Recode table from 16-bit register notation to 32-bit GPR. */
  707. const int reg16to32[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
  708. /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
  709. const int reg16to32st[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
  710. static void emulate_load_store_microMIPS(struct pt_regs *regs,
  711. void __user *addr)
  712. {
  713. unsigned long value;
  714. unsigned int res;
  715. int i;
  716. unsigned int reg = 0, rvar;
  717. unsigned long orig31;
  718. u16 __user *pc16;
  719. u16 halfword;
  720. unsigned int word;
  721. unsigned long origpc, contpc;
  722. union mips_instruction insn;
  723. struct mm_decoded_insn mminsn;
  724. void __user *fault_addr = NULL;
  725. origpc = regs->cp0_epc;
  726. orig31 = regs->regs[31];
  727. mminsn.micro_mips_mode = 1;
  728. /*
  729. * This load never faults.
  730. */
  731. pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
  732. __get_user(halfword, pc16);
  733. pc16++;
  734. contpc = regs->cp0_epc + 2;
  735. word = ((unsigned int)halfword << 16);
  736. mminsn.pc_inc = 2;
  737. if (!mm_insn_16bit(halfword)) {
  738. __get_user(halfword, pc16);
  739. pc16++;
  740. contpc = regs->cp0_epc + 4;
  741. mminsn.pc_inc = 4;
  742. word |= halfword;
  743. }
  744. mminsn.insn = word;
  745. if (get_user(halfword, pc16))
  746. goto fault;
  747. mminsn.next_pc_inc = 2;
  748. word = ((unsigned int)halfword << 16);
  749. if (!mm_insn_16bit(halfword)) {
  750. pc16++;
  751. if (get_user(halfword, pc16))
  752. goto fault;
  753. mminsn.next_pc_inc = 4;
  754. word |= halfword;
  755. }
  756. mminsn.next_insn = word;
  757. insn = (union mips_instruction)(mminsn.insn);
  758. if (mm_isBranchInstr(regs, mminsn, &contpc))
  759. insn = (union mips_instruction)(mminsn.next_insn);
  760. /* Parse instruction to find what to do */
  761. switch (insn.mm_i_format.opcode) {
  762. case mm_pool32a_op:
  763. switch (insn.mm_x_format.func) {
  764. case mm_lwxs_op:
  765. reg = insn.mm_x_format.rd;
  766. goto loadW;
  767. }
  768. goto sigbus;
  769. case mm_pool32b_op:
  770. switch (insn.mm_m_format.func) {
  771. case mm_lwp_func:
  772. reg = insn.mm_m_format.rd;
  773. if (reg == 31)
  774. goto sigbus;
  775. if (!access_ok(VERIFY_READ, addr, 8))
  776. goto sigbus;
  777. LoadW(addr, value, res);
  778. if (res)
  779. goto fault;
  780. regs->regs[reg] = value;
  781. addr += 4;
  782. LoadW(addr, value, res);
  783. if (res)
  784. goto fault;
  785. regs->regs[reg + 1] = value;
  786. goto success;
  787. case mm_swp_func:
  788. reg = insn.mm_m_format.rd;
  789. if (reg == 31)
  790. goto sigbus;
  791. if (!access_ok(VERIFY_WRITE, addr, 8))
  792. goto sigbus;
  793. value = regs->regs[reg];
  794. StoreW(addr, value, res);
  795. if (res)
  796. goto fault;
  797. addr += 4;
  798. value = regs->regs[reg + 1];
  799. StoreW(addr, value, res);
  800. if (res)
  801. goto fault;
  802. goto success;
  803. case mm_ldp_func:
  804. #ifdef CONFIG_64BIT
  805. reg = insn.mm_m_format.rd;
  806. if (reg == 31)
  807. goto sigbus;
  808. if (!access_ok(VERIFY_READ, addr, 16))
  809. goto sigbus;
  810. LoadDW(addr, value, res);
  811. if (res)
  812. goto fault;
  813. regs->regs[reg] = value;
  814. addr += 8;
  815. LoadDW(addr, value, res);
  816. if (res)
  817. goto fault;
  818. regs->regs[reg + 1] = value;
  819. goto success;
  820. #endif /* CONFIG_64BIT */
  821. goto sigill;
  822. case mm_sdp_func:
  823. #ifdef CONFIG_64BIT
  824. reg = insn.mm_m_format.rd;
  825. if (reg == 31)
  826. goto sigbus;
  827. if (!access_ok(VERIFY_WRITE, addr, 16))
  828. goto sigbus;
  829. value = regs->regs[reg];
  830. StoreDW(addr, value, res);
  831. if (res)
  832. goto fault;
  833. addr += 8;
  834. value = regs->regs[reg + 1];
  835. StoreDW(addr, value, res);
  836. if (res)
  837. goto fault;
  838. goto success;
  839. #endif /* CONFIG_64BIT */
  840. goto sigill;
  841. case mm_lwm32_func:
  842. reg = insn.mm_m_format.rd;
  843. rvar = reg & 0xf;
  844. if ((rvar > 9) || !reg)
  845. goto sigill;
  846. if (reg & 0x10) {
  847. if (!access_ok
  848. (VERIFY_READ, addr, 4 * (rvar + 1)))
  849. goto sigbus;
  850. } else {
  851. if (!access_ok(VERIFY_READ, addr, 4 * rvar))
  852. goto sigbus;
  853. }
  854. if (rvar == 9)
  855. rvar = 8;
  856. for (i = 16; rvar; rvar--, i++) {
  857. LoadW(addr, value, res);
  858. if (res)
  859. goto fault;
  860. addr += 4;
  861. regs->regs[i] = value;
  862. }
  863. if ((reg & 0xf) == 9) {
  864. LoadW(addr, value, res);
  865. if (res)
  866. goto fault;
  867. addr += 4;
  868. regs->regs[30] = value;
  869. }
  870. if (reg & 0x10) {
  871. LoadW(addr, value, res);
  872. if (res)
  873. goto fault;
  874. regs->regs[31] = value;
  875. }
  876. goto success;
  877. case mm_swm32_func:
  878. reg = insn.mm_m_format.rd;
  879. rvar = reg & 0xf;
  880. if ((rvar > 9) || !reg)
  881. goto sigill;
  882. if (reg & 0x10) {
  883. if (!access_ok
  884. (VERIFY_WRITE, addr, 4 * (rvar + 1)))
  885. goto sigbus;
  886. } else {
  887. if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
  888. goto sigbus;
  889. }
  890. if (rvar == 9)
  891. rvar = 8;
  892. for (i = 16; rvar; rvar--, i++) {
  893. value = regs->regs[i];
  894. StoreW(addr, value, res);
  895. if (res)
  896. goto fault;
  897. addr += 4;
  898. }
  899. if ((reg & 0xf) == 9) {
  900. value = regs->regs[30];
  901. StoreW(addr, value, res);
  902. if (res)
  903. goto fault;
  904. addr += 4;
  905. }
  906. if (reg & 0x10) {
  907. value = regs->regs[31];
  908. StoreW(addr, value, res);
  909. if (res)
  910. goto fault;
  911. }
  912. goto success;
  913. case mm_ldm_func:
  914. #ifdef CONFIG_64BIT
  915. reg = insn.mm_m_format.rd;
  916. rvar = reg & 0xf;
  917. if ((rvar > 9) || !reg)
  918. goto sigill;
  919. if (reg & 0x10) {
  920. if (!access_ok
  921. (VERIFY_READ, addr, 8 * (rvar + 1)))
  922. goto sigbus;
  923. } else {
  924. if (!access_ok(VERIFY_READ, addr, 8 * rvar))
  925. goto sigbus;
  926. }
  927. if (rvar == 9)
  928. rvar = 8;
  929. for (i = 16; rvar; rvar--, i++) {
  930. LoadDW(addr, value, res);
  931. if (res)
  932. goto fault;
  933. addr += 4;
  934. regs->regs[i] = value;
  935. }
  936. if ((reg & 0xf) == 9) {
  937. LoadDW(addr, value, res);
  938. if (res)
  939. goto fault;
  940. addr += 8;
  941. regs->regs[30] = value;
  942. }
  943. if (reg & 0x10) {
  944. LoadDW(addr, value, res);
  945. if (res)
  946. goto fault;
  947. regs->regs[31] = value;
  948. }
  949. goto success;
  950. #endif /* CONFIG_64BIT */
  951. goto sigill;
  952. case mm_sdm_func:
  953. #ifdef CONFIG_64BIT
  954. reg = insn.mm_m_format.rd;
  955. rvar = reg & 0xf;
  956. if ((rvar > 9) || !reg)
  957. goto sigill;
  958. if (reg & 0x10) {
  959. if (!access_ok
  960. (VERIFY_WRITE, addr, 8 * (rvar + 1)))
  961. goto sigbus;
  962. } else {
  963. if (!access_ok(VERIFY_WRITE, addr, 8 * rvar))
  964. goto sigbus;
  965. }
  966. if (rvar == 9)
  967. rvar = 8;
  968. for (i = 16; rvar; rvar--, i++) {
  969. value = regs->regs[i];
  970. StoreDW(addr, value, res);
  971. if (res)
  972. goto fault;
  973. addr += 8;
  974. }
  975. if ((reg & 0xf) == 9) {
  976. value = regs->regs[30];
  977. StoreDW(addr, value, res);
  978. if (res)
  979. goto fault;
  980. addr += 8;
  981. }
  982. if (reg & 0x10) {
  983. value = regs->regs[31];
  984. StoreDW(addr, value, res);
  985. if (res)
  986. goto fault;
  987. }
  988. goto success;
  989. #endif /* CONFIG_64BIT */
  990. goto sigill;
  991. /* LWC2, SWC2, LDC2, SDC2 are not serviced */
  992. }
  993. goto sigbus;
  994. case mm_pool32c_op:
  995. switch (insn.mm_m_format.func) {
  996. case mm_lwu_func:
  997. reg = insn.mm_m_format.rd;
  998. goto loadWU;
  999. }
  1000. /* LL,SC,LLD,SCD are not serviced */
  1001. goto sigbus;
  1002. case mm_pool32f_op:
  1003. switch (insn.mm_x_format.func) {
  1004. case mm_lwxc1_func:
  1005. case mm_swxc1_func:
  1006. case mm_ldxc1_func:
  1007. case mm_sdxc1_func:
  1008. goto fpu_emul;
  1009. }
  1010. goto sigbus;
  1011. case mm_ldc132_op:
  1012. case mm_sdc132_op:
  1013. case mm_lwc132_op:
  1014. case mm_swc132_op:
  1015. fpu_emul:
  1016. /* roll back jump/branch */
  1017. regs->cp0_epc = origpc;
  1018. regs->regs[31] = orig31;
  1019. die_if_kernel("Unaligned FP access in kernel code", regs);
  1020. BUG_ON(!used_math());
  1021. BUG_ON(!is_fpu_owner());
  1022. lose_fpu(1); /* save the FPU state for the emulator */
  1023. res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  1024. &fault_addr);
  1025. own_fpu(1); /* restore FPU state */
  1026. /* If something went wrong, signal */
  1027. process_fpemu_return(res, fault_addr);
  1028. if (res == 0)
  1029. goto success;
  1030. return;
  1031. case mm_lh32_op:
  1032. reg = insn.mm_i_format.rt;
  1033. goto loadHW;
  1034. case mm_lhu32_op:
  1035. reg = insn.mm_i_format.rt;
  1036. goto loadHWU;
  1037. case mm_lw32_op:
  1038. reg = insn.mm_i_format.rt;
  1039. goto loadW;
  1040. case mm_sh32_op:
  1041. reg = insn.mm_i_format.rt;
  1042. goto storeHW;
  1043. case mm_sw32_op:
  1044. reg = insn.mm_i_format.rt;
  1045. goto storeW;
  1046. case mm_ld32_op:
  1047. reg = insn.mm_i_format.rt;
  1048. goto loadDW;
  1049. case mm_sd32_op:
  1050. reg = insn.mm_i_format.rt;
  1051. goto storeDW;
  1052. case mm_pool16c_op:
  1053. switch (insn.mm16_m_format.func) {
  1054. case mm_lwm16_op:
  1055. reg = insn.mm16_m_format.rlist;
  1056. rvar = reg + 1;
  1057. if (!access_ok(VERIFY_READ, addr, 4 * rvar))
  1058. goto sigbus;
  1059. for (i = 16; rvar; rvar--, i++) {
  1060. LoadW(addr, value, res);
  1061. if (res)
  1062. goto fault;
  1063. addr += 4;
  1064. regs->regs[i] = value;
  1065. }
  1066. LoadW(addr, value, res);
  1067. if (res)
  1068. goto fault;
  1069. regs->regs[31] = value;
  1070. goto success;
  1071. case mm_swm16_op:
  1072. reg = insn.mm16_m_format.rlist;
  1073. rvar = reg + 1;
  1074. if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
  1075. goto sigbus;
  1076. for (i = 16; rvar; rvar--, i++) {
  1077. value = regs->regs[i];
  1078. StoreW(addr, value, res);
  1079. if (res)
  1080. goto fault;
  1081. addr += 4;
  1082. }
  1083. value = regs->regs[31];
  1084. StoreW(addr, value, res);
  1085. if (res)
  1086. goto fault;
  1087. goto success;
  1088. }
  1089. goto sigbus;
  1090. case mm_lhu16_op:
  1091. reg = reg16to32[insn.mm16_rb_format.rt];
  1092. goto loadHWU;
  1093. case mm_lw16_op:
  1094. reg = reg16to32[insn.mm16_rb_format.rt];
  1095. goto loadW;
  1096. case mm_sh16_op:
  1097. reg = reg16to32st[insn.mm16_rb_format.rt];
  1098. goto storeHW;
  1099. case mm_sw16_op:
  1100. reg = reg16to32st[insn.mm16_rb_format.rt];
  1101. goto storeW;
  1102. case mm_lwsp16_op:
  1103. reg = insn.mm16_r5_format.rt;
  1104. goto loadW;
  1105. case mm_swsp16_op:
  1106. reg = insn.mm16_r5_format.rt;
  1107. goto storeW;
  1108. case mm_lwgp16_op:
  1109. reg = reg16to32[insn.mm16_r3_format.rt];
  1110. goto loadW;
  1111. default:
  1112. goto sigill;
  1113. }
  1114. loadHW:
  1115. if (!access_ok(VERIFY_READ, addr, 2))
  1116. goto sigbus;
  1117. LoadHW(addr, value, res);
  1118. if (res)
  1119. goto fault;
  1120. regs->regs[reg] = value;
  1121. goto success;
  1122. loadHWU:
  1123. if (!access_ok(VERIFY_READ, addr, 2))
  1124. goto sigbus;
  1125. LoadHWU(addr, value, res);
  1126. if (res)
  1127. goto fault;
  1128. regs->regs[reg] = value;
  1129. goto success;
  1130. loadW:
  1131. if (!access_ok(VERIFY_READ, addr, 4))
  1132. goto sigbus;
  1133. LoadW(addr, value, res);
  1134. if (res)
  1135. goto fault;
  1136. regs->regs[reg] = value;
  1137. goto success;
  1138. loadWU:
  1139. #ifdef CONFIG_64BIT
  1140. /*
  1141. * A 32-bit kernel might be running on a 64-bit processor. But
  1142. * if we're on a 32-bit processor and an i-cache incoherency
  1143. * or race makes us see a 64-bit instruction here the sdl/sdr
  1144. * would blow up, so for now we don't handle unaligned 64-bit
  1145. * instructions on 32-bit kernels.
  1146. */
  1147. if (!access_ok(VERIFY_READ, addr, 4))
  1148. goto sigbus;
  1149. LoadWU(addr, value, res);
  1150. if (res)
  1151. goto fault;
  1152. regs->regs[reg] = value;
  1153. goto success;
  1154. #endif /* CONFIG_64BIT */
  1155. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1156. goto sigill;
  1157. loadDW:
  1158. #ifdef CONFIG_64BIT
  1159. /*
  1160. * A 32-bit kernel might be running on a 64-bit processor. But
  1161. * if we're on a 32-bit processor and an i-cache incoherency
  1162. * or race makes us see a 64-bit instruction here the sdl/sdr
  1163. * would blow up, so for now we don't handle unaligned 64-bit
  1164. * instructions on 32-bit kernels.
  1165. */
  1166. if (!access_ok(VERIFY_READ, addr, 8))
  1167. goto sigbus;
  1168. LoadDW(addr, value, res);
  1169. if (res)
  1170. goto fault;
  1171. regs->regs[reg] = value;
  1172. goto success;
  1173. #endif /* CONFIG_64BIT */
  1174. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1175. goto sigill;
  1176. storeHW:
  1177. if (!access_ok(VERIFY_WRITE, addr, 2))
  1178. goto sigbus;
  1179. value = regs->regs[reg];
  1180. StoreHW(addr, value, res);
  1181. if (res)
  1182. goto fault;
  1183. goto success;
  1184. storeW:
  1185. if (!access_ok(VERIFY_WRITE, addr, 4))
  1186. goto sigbus;
  1187. value = regs->regs[reg];
  1188. StoreW(addr, value, res);
  1189. if (res)
  1190. goto fault;
  1191. goto success;
  1192. storeDW:
  1193. #ifdef CONFIG_64BIT
  1194. /*
  1195. * A 32-bit kernel might be running on a 64-bit processor. But
  1196. * if we're on a 32-bit processor and an i-cache incoherency
  1197. * or race makes us see a 64-bit instruction here the sdl/sdr
  1198. * would blow up, so for now we don't handle unaligned 64-bit
  1199. * instructions on 32-bit kernels.
  1200. */
  1201. if (!access_ok(VERIFY_WRITE, addr, 8))
  1202. goto sigbus;
  1203. value = regs->regs[reg];
  1204. StoreDW(addr, value, res);
  1205. if (res)
  1206. goto fault;
  1207. goto success;
  1208. #endif /* CONFIG_64BIT */
  1209. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1210. goto sigill;
  1211. success:
  1212. regs->cp0_epc = contpc; /* advance or branch */
  1213. #ifdef CONFIG_DEBUG_FS
  1214. unaligned_instructions++;
  1215. #endif
  1216. return;
  1217. fault:
  1218. /* roll back jump/branch */
  1219. regs->cp0_epc = origpc;
  1220. regs->regs[31] = orig31;
  1221. /* Did we have an exception handler installed? */
  1222. if (fixup_exception(regs))
  1223. return;
  1224. die_if_kernel("Unhandled kernel unaligned access", regs);
  1225. force_sig(SIGSEGV, current);
  1226. return;
  1227. sigbus:
  1228. die_if_kernel("Unhandled kernel unaligned access", regs);
  1229. force_sig(SIGBUS, current);
  1230. return;
  1231. sigill:
  1232. die_if_kernel
  1233. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1234. force_sig(SIGILL, current);
  1235. }
  1236. static void emulate_load_store_MIPS16e(struct pt_regs *regs, void __user * addr)
  1237. {
  1238. unsigned long value;
  1239. unsigned int res;
  1240. int reg;
  1241. unsigned long orig31;
  1242. u16 __user *pc16;
  1243. unsigned long origpc;
  1244. union mips16e_instruction mips16inst, oldinst;
  1245. origpc = regs->cp0_epc;
  1246. orig31 = regs->regs[31];
  1247. pc16 = (unsigned short __user *)msk_isa16_mode(origpc);
  1248. /*
  1249. * This load never faults.
  1250. */
  1251. __get_user(mips16inst.full, pc16);
  1252. oldinst = mips16inst;
  1253. /* skip EXTEND instruction */
  1254. if (mips16inst.ri.opcode == MIPS16e_extend_op) {
  1255. pc16++;
  1256. __get_user(mips16inst.full, pc16);
  1257. } else if (delay_slot(regs)) {
  1258. /* skip jump instructions */
  1259. /* JAL/JALX are 32 bits but have OPCODE in first short int */
  1260. if (mips16inst.ri.opcode == MIPS16e_jal_op)
  1261. pc16++;
  1262. pc16++;
  1263. if (get_user(mips16inst.full, pc16))
  1264. goto sigbus;
  1265. }
  1266. switch (mips16inst.ri.opcode) {
  1267. case MIPS16e_i64_op: /* I64 or RI64 instruction */
  1268. switch (mips16inst.i64.func) { /* I64/RI64 func field check */
  1269. case MIPS16e_ldpc_func:
  1270. case MIPS16e_ldsp_func:
  1271. reg = reg16to32[mips16inst.ri64.ry];
  1272. goto loadDW;
  1273. case MIPS16e_sdsp_func:
  1274. reg = reg16to32[mips16inst.ri64.ry];
  1275. goto writeDW;
  1276. case MIPS16e_sdrasp_func:
  1277. reg = 29; /* GPRSP */
  1278. goto writeDW;
  1279. }
  1280. goto sigbus;
  1281. case MIPS16e_swsp_op:
  1282. case MIPS16e_lwpc_op:
  1283. case MIPS16e_lwsp_op:
  1284. reg = reg16to32[mips16inst.ri.rx];
  1285. break;
  1286. case MIPS16e_i8_op:
  1287. if (mips16inst.i8.func != MIPS16e_swrasp_func)
  1288. goto sigbus;
  1289. reg = 29; /* GPRSP */
  1290. break;
  1291. default:
  1292. reg = reg16to32[mips16inst.rri.ry];
  1293. break;
  1294. }
  1295. switch (mips16inst.ri.opcode) {
  1296. case MIPS16e_lb_op:
  1297. case MIPS16e_lbu_op:
  1298. case MIPS16e_sb_op:
  1299. goto sigbus;
  1300. case MIPS16e_lh_op:
  1301. if (!access_ok(VERIFY_READ, addr, 2))
  1302. goto sigbus;
  1303. LoadHW(addr, value, res);
  1304. if (res)
  1305. goto fault;
  1306. MIPS16e_compute_return_epc(regs, &oldinst);
  1307. regs->regs[reg] = value;
  1308. break;
  1309. case MIPS16e_lhu_op:
  1310. if (!access_ok(VERIFY_READ, addr, 2))
  1311. goto sigbus;
  1312. LoadHWU(addr, value, res);
  1313. if (res)
  1314. goto fault;
  1315. MIPS16e_compute_return_epc(regs, &oldinst);
  1316. regs->regs[reg] = value;
  1317. break;
  1318. case MIPS16e_lw_op:
  1319. case MIPS16e_lwpc_op:
  1320. case MIPS16e_lwsp_op:
  1321. if (!access_ok(VERIFY_READ, addr, 4))
  1322. goto sigbus;
  1323. LoadW(addr, value, res);
  1324. if (res)
  1325. goto fault;
  1326. MIPS16e_compute_return_epc(regs, &oldinst);
  1327. regs->regs[reg] = value;
  1328. break;
  1329. case MIPS16e_lwu_op:
  1330. #ifdef CONFIG_64BIT
  1331. /*
  1332. * A 32-bit kernel might be running on a 64-bit processor. But
  1333. * if we're on a 32-bit processor and an i-cache incoherency
  1334. * or race makes us see a 64-bit instruction here the sdl/sdr
  1335. * would blow up, so for now we don't handle unaligned 64-bit
  1336. * instructions on 32-bit kernels.
  1337. */
  1338. if (!access_ok(VERIFY_READ, addr, 4))
  1339. goto sigbus;
  1340. LoadWU(addr, value, res);
  1341. if (res)
  1342. goto fault;
  1343. MIPS16e_compute_return_epc(regs, &oldinst);
  1344. regs->regs[reg] = value;
  1345. break;
  1346. #endif /* CONFIG_64BIT */
  1347. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1348. goto sigill;
  1349. case MIPS16e_ld_op:
  1350. loadDW:
  1351. #ifdef CONFIG_64BIT
  1352. /*
  1353. * A 32-bit kernel might be running on a 64-bit processor. But
  1354. * if we're on a 32-bit processor and an i-cache incoherency
  1355. * or race makes us see a 64-bit instruction here the sdl/sdr
  1356. * would blow up, so for now we don't handle unaligned 64-bit
  1357. * instructions on 32-bit kernels.
  1358. */
  1359. if (!access_ok(VERIFY_READ, addr, 8))
  1360. goto sigbus;
  1361. LoadDW(addr, value, res);
  1362. if (res)
  1363. goto fault;
  1364. MIPS16e_compute_return_epc(regs, &oldinst);
  1365. regs->regs[reg] = value;
  1366. break;
  1367. #endif /* CONFIG_64BIT */
  1368. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1369. goto sigill;
  1370. case MIPS16e_sh_op:
  1371. if (!access_ok(VERIFY_WRITE, addr, 2))
  1372. goto sigbus;
  1373. MIPS16e_compute_return_epc(regs, &oldinst);
  1374. value = regs->regs[reg];
  1375. StoreHW(addr, value, res);
  1376. if (res)
  1377. goto fault;
  1378. break;
  1379. case MIPS16e_sw_op:
  1380. case MIPS16e_swsp_op:
  1381. case MIPS16e_i8_op: /* actually - MIPS16e_swrasp_func */
  1382. if (!access_ok(VERIFY_WRITE, addr, 4))
  1383. goto sigbus;
  1384. MIPS16e_compute_return_epc(regs, &oldinst);
  1385. value = regs->regs[reg];
  1386. StoreW(addr, value, res);
  1387. if (res)
  1388. goto fault;
  1389. break;
  1390. case MIPS16e_sd_op:
  1391. writeDW:
  1392. #ifdef CONFIG_64BIT
  1393. /*
  1394. * A 32-bit kernel might be running on a 64-bit processor. But
  1395. * if we're on a 32-bit processor and an i-cache incoherency
  1396. * or race makes us see a 64-bit instruction here the sdl/sdr
  1397. * would blow up, so for now we don't handle unaligned 64-bit
  1398. * instructions on 32-bit kernels.
  1399. */
  1400. if (!access_ok(VERIFY_WRITE, addr, 8))
  1401. goto sigbus;
  1402. MIPS16e_compute_return_epc(regs, &oldinst);
  1403. value = regs->regs[reg];
  1404. StoreDW(addr, value, res);
  1405. if (res)
  1406. goto fault;
  1407. break;
  1408. #endif /* CONFIG_64BIT */
  1409. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1410. goto sigill;
  1411. default:
  1412. /*
  1413. * Pheeee... We encountered an yet unknown instruction or
  1414. * cache coherence problem. Die sucker, die ...
  1415. */
  1416. goto sigill;
  1417. }
  1418. #ifdef CONFIG_DEBUG_FS
  1419. unaligned_instructions++;
  1420. #endif
  1421. return;
  1422. fault:
  1423. /* roll back jump/branch */
  1424. regs->cp0_epc = origpc;
  1425. regs->regs[31] = orig31;
  1426. /* Did we have an exception handler installed? */
  1427. if (fixup_exception(regs))
  1428. return;
  1429. die_if_kernel("Unhandled kernel unaligned access", regs);
  1430. force_sig(SIGSEGV, current);
  1431. return;
  1432. sigbus:
  1433. die_if_kernel("Unhandled kernel unaligned access", regs);
  1434. force_sig(SIGBUS, current);
  1435. return;
  1436. sigill:
  1437. die_if_kernel
  1438. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1439. force_sig(SIGILL, current);
  1440. }
  1441. asmlinkage void do_ade(struct pt_regs *regs)
  1442. {
  1443. enum ctx_state prev_state;
  1444. unsigned int __user *pc;
  1445. mm_segment_t seg;
  1446. prev_state = exception_enter();
  1447. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
  1448. 1, regs, regs->cp0_badvaddr);
  1449. /*
  1450. * Did we catch a fault trying to load an instruction?
  1451. */
  1452. if (regs->cp0_badvaddr == regs->cp0_epc)
  1453. goto sigbus;
  1454. if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
  1455. goto sigbus;
  1456. if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
  1457. goto sigbus;
  1458. /*
  1459. * Do branch emulation only if we didn't forward the exception.
  1460. * This is all so but ugly ...
  1461. */
  1462. /*
  1463. * Are we running in microMIPS mode?
  1464. */
  1465. if (get_isa16_mode(regs->cp0_epc)) {
  1466. /*
  1467. * Did we catch a fault trying to load an instruction in
  1468. * 16-bit mode?
  1469. */
  1470. if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
  1471. goto sigbus;
  1472. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  1473. show_registers(regs);
  1474. if (cpu_has_mmips) {
  1475. seg = get_fs();
  1476. if (!user_mode(regs))
  1477. set_fs(KERNEL_DS);
  1478. emulate_load_store_microMIPS(regs,
  1479. (void __user *)regs->cp0_badvaddr);
  1480. set_fs(seg);
  1481. return;
  1482. }
  1483. if (cpu_has_mips16) {
  1484. seg = get_fs();
  1485. if (!user_mode(regs))
  1486. set_fs(KERNEL_DS);
  1487. emulate_load_store_MIPS16e(regs,
  1488. (void __user *)regs->cp0_badvaddr);
  1489. set_fs(seg);
  1490. return;
  1491. }
  1492. goto sigbus;
  1493. }
  1494. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  1495. show_registers(regs);
  1496. pc = (unsigned int __user *)exception_epc(regs);
  1497. seg = get_fs();
  1498. if (!user_mode(regs))
  1499. set_fs(KERNEL_DS);
  1500. emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
  1501. set_fs(seg);
  1502. return;
  1503. sigbus:
  1504. die_if_kernel("Kernel unaligned instruction access", regs);
  1505. force_sig(SIGBUS, current);
  1506. /*
  1507. * XXX On return from the signal handler we should advance the epc
  1508. */
  1509. exception_exit(prev_state);
  1510. }
  1511. #ifdef CONFIG_DEBUG_FS
  1512. extern struct dentry *mips_debugfs_dir;
  1513. static int __init debugfs_unaligned(void)
  1514. {
  1515. struct dentry *d;
  1516. if (!mips_debugfs_dir)
  1517. return -ENODEV;
  1518. d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
  1519. mips_debugfs_dir, &unaligned_instructions);
  1520. if (!d)
  1521. return -ENOMEM;
  1522. d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
  1523. mips_debugfs_dir, &unaligned_action);
  1524. if (!d)
  1525. return -ENOMEM;
  1526. return 0;
  1527. }
  1528. __initcall(debugfs_unaligned);
  1529. #endif