unaligned.c 41 KB

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