unaligned.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  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. #define STR(x) __STR(x)
  92. #define __STR(x) #x
  93. enum {
  94. UNALIGNED_ACTION_QUIET,
  95. UNALIGNED_ACTION_SIGNAL,
  96. UNALIGNED_ACTION_SHOW,
  97. };
  98. #ifdef CONFIG_DEBUG_FS
  99. static u32 unaligned_instructions;
  100. static u32 unaligned_action;
  101. #else
  102. #define unaligned_action UNALIGNED_ACTION_QUIET
  103. #endif
  104. extern void show_registers(struct pt_regs *regs);
  105. #ifdef __BIG_ENDIAN
  106. #define _LoadHW(addr, value, res, type) \
  107. do { \
  108. __asm__ __volatile__ (".set\tnoat\n" \
  109. "1:\t"type##_lb("%0", "0(%2)")"\n" \
  110. "2:\t"type##_lbu("$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. } while(0)
  127. #ifndef CONFIG_CPU_MIPSR6
  128. #define _LoadW(addr, value, res, type) \
  129. do { \
  130. __asm__ __volatile__ ( \
  131. "1:\t"type##_lwl("%0", "(%2)")"\n" \
  132. "2:\t"type##_lwr("%0", "3(%2)")"\n\t"\
  133. "li\t%1, 0\n" \
  134. "3:\n\t" \
  135. ".insn\n\t" \
  136. ".section\t.fixup,\"ax\"\n\t" \
  137. "4:\tli\t%1, %3\n\t" \
  138. "j\t3b\n\t" \
  139. ".previous\n\t" \
  140. ".section\t__ex_table,\"a\"\n\t" \
  141. STR(PTR)"\t1b, 4b\n\t" \
  142. STR(PTR)"\t2b, 4b\n\t" \
  143. ".previous" \
  144. : "=&r" (value), "=r" (res) \
  145. : "r" (addr), "i" (-EFAULT)); \
  146. } while(0)
  147. #else
  148. /* MIPSR6 has no lwl instruction */
  149. #define _LoadW(addr, value, res, type) \
  150. do { \
  151. __asm__ __volatile__ ( \
  152. ".set\tpush\n" \
  153. ".set\tnoat\n\t" \
  154. "1:"type##_lb("%0", "0(%2)")"\n\t" \
  155. "2:"type##_lbu("$1", "1(%2)")"\n\t" \
  156. "sll\t%0, 0x8\n\t" \
  157. "or\t%0, $1\n\t" \
  158. "3:"type##_lbu("$1", "2(%2)")"\n\t" \
  159. "sll\t%0, 0x8\n\t" \
  160. "or\t%0, $1\n\t" \
  161. "4:"type##_lbu("$1", "3(%2)")"\n\t" \
  162. "sll\t%0, 0x8\n\t" \
  163. "or\t%0, $1\n\t" \
  164. "li\t%1, 0\n" \
  165. ".set\tpop\n" \
  166. "10:\n\t" \
  167. ".insn\n\t" \
  168. ".section\t.fixup,\"ax\"\n\t" \
  169. "11:\tli\t%1, %3\n\t" \
  170. "j\t10b\n\t" \
  171. ".previous\n\t" \
  172. ".section\t__ex_table,\"a\"\n\t" \
  173. STR(PTR)"\t1b, 11b\n\t" \
  174. STR(PTR)"\t2b, 11b\n\t" \
  175. STR(PTR)"\t3b, 11b\n\t" \
  176. STR(PTR)"\t4b, 11b\n\t" \
  177. ".previous" \
  178. : "=&r" (value), "=r" (res) \
  179. : "r" (addr), "i" (-EFAULT)); \
  180. } while(0)
  181. #endif /* CONFIG_CPU_MIPSR6 */
  182. #define _LoadHWU(addr, value, res, type) \
  183. do { \
  184. __asm__ __volatile__ ( \
  185. ".set\tnoat\n" \
  186. "1:\t"type##_lbu("%0", "0(%2)")"\n" \
  187. "2:\t"type##_lbu("$1", "1(%2)")"\n\t"\
  188. "sll\t%0, 0x8\n\t" \
  189. "or\t%0, $1\n\t" \
  190. "li\t%1, 0\n" \
  191. "3:\n\t" \
  192. ".insn\n\t" \
  193. ".set\tat\n\t" \
  194. ".section\t.fixup,\"ax\"\n\t" \
  195. "4:\tli\t%1, %3\n\t" \
  196. "j\t3b\n\t" \
  197. ".previous\n\t" \
  198. ".section\t__ex_table,\"a\"\n\t" \
  199. STR(PTR)"\t1b, 4b\n\t" \
  200. STR(PTR)"\t2b, 4b\n\t" \
  201. ".previous" \
  202. : "=&r" (value), "=r" (res) \
  203. : "r" (addr), "i" (-EFAULT)); \
  204. } while(0)
  205. #ifndef CONFIG_CPU_MIPSR6
  206. #define _LoadWU(addr, value, res, type) \
  207. do { \
  208. __asm__ __volatile__ ( \
  209. "1:\t"type##_lwl("%0", "(%2)")"\n" \
  210. "2:\t"type##_lwr("%0", "3(%2)")"\n\t"\
  211. "dsll\t%0, %0, 32\n\t" \
  212. "dsrl\t%0, %0, 32\n\t" \
  213. "li\t%1, 0\n" \
  214. "3:\n\t" \
  215. ".insn\n\t" \
  216. "\t.section\t.fixup,\"ax\"\n\t" \
  217. "4:\tli\t%1, %3\n\t" \
  218. "j\t3b\n\t" \
  219. ".previous\n\t" \
  220. ".section\t__ex_table,\"a\"\n\t" \
  221. STR(PTR)"\t1b, 4b\n\t" \
  222. STR(PTR)"\t2b, 4b\n\t" \
  223. ".previous" \
  224. : "=&r" (value), "=r" (res) \
  225. : "r" (addr), "i" (-EFAULT)); \
  226. } while(0)
  227. #define _LoadDW(addr, value, res) \
  228. do { \
  229. __asm__ __volatile__ ( \
  230. "1:\tldl\t%0, (%2)\n" \
  231. "2:\tldr\t%0, 7(%2)\n\t" \
  232. "li\t%1, 0\n" \
  233. "3:\n\t" \
  234. ".insn\n\t" \
  235. "\t.section\t.fixup,\"ax\"\n\t" \
  236. "4:\tli\t%1, %3\n\t" \
  237. "j\t3b\n\t" \
  238. ".previous\n\t" \
  239. ".section\t__ex_table,\"a\"\n\t" \
  240. STR(PTR)"\t1b, 4b\n\t" \
  241. STR(PTR)"\t2b, 4b\n\t" \
  242. ".previous" \
  243. : "=&r" (value), "=r" (res) \
  244. : "r" (addr), "i" (-EFAULT)); \
  245. } while(0)
  246. #else
  247. /* MIPSR6 has not lwl and ldl instructions */
  248. #define _LoadWU(addr, value, res, type) \
  249. do { \
  250. __asm__ __volatile__ ( \
  251. ".set\tpush\n\t" \
  252. ".set\tnoat\n\t" \
  253. "1:"type##_lbu("%0", "0(%2)")"\n\t" \
  254. "2:"type##_lbu("$1", "1(%2)")"\n\t" \
  255. "sll\t%0, 0x8\n\t" \
  256. "or\t%0, $1\n\t" \
  257. "3:"type##_lbu("$1", "2(%2)")"\n\t" \
  258. "sll\t%0, 0x8\n\t" \
  259. "or\t%0, $1\n\t" \
  260. "4:"type##_lbu("$1", "3(%2)")"\n\t" \
  261. "sll\t%0, 0x8\n\t" \
  262. "or\t%0, $1\n\t" \
  263. "li\t%1, 0\n" \
  264. ".set\tpop\n" \
  265. "10:\n\t" \
  266. ".insn\n\t" \
  267. ".section\t.fixup,\"ax\"\n\t" \
  268. "11:\tli\t%1, %3\n\t" \
  269. "j\t10b\n\t" \
  270. ".previous\n\t" \
  271. ".section\t__ex_table,\"a\"\n\t" \
  272. STR(PTR)"\t1b, 11b\n\t" \
  273. STR(PTR)"\t2b, 11b\n\t" \
  274. STR(PTR)"\t3b, 11b\n\t" \
  275. STR(PTR)"\t4b, 11b\n\t" \
  276. ".previous" \
  277. : "=&r" (value), "=r" (res) \
  278. : "r" (addr), "i" (-EFAULT)); \
  279. } while(0)
  280. #define _LoadDW(addr, value, res) \
  281. do { \
  282. __asm__ __volatile__ ( \
  283. ".set\tpush\n\t" \
  284. ".set\tnoat\n\t" \
  285. "1:lb\t%0, 0(%2)\n\t" \
  286. "2:lbu\t $1, 1(%2)\n\t" \
  287. "dsll\t%0, 0x8\n\t" \
  288. "or\t%0, $1\n\t" \
  289. "3:lbu\t$1, 2(%2)\n\t" \
  290. "dsll\t%0, 0x8\n\t" \
  291. "or\t%0, $1\n\t" \
  292. "4:lbu\t$1, 3(%2)\n\t" \
  293. "dsll\t%0, 0x8\n\t" \
  294. "or\t%0, $1\n\t" \
  295. "5:lbu\t$1, 4(%2)\n\t" \
  296. "dsll\t%0, 0x8\n\t" \
  297. "or\t%0, $1\n\t" \
  298. "6:lbu\t$1, 5(%2)\n\t" \
  299. "dsll\t%0, 0x8\n\t" \
  300. "or\t%0, $1\n\t" \
  301. "7:lbu\t$1, 6(%2)\n\t" \
  302. "dsll\t%0, 0x8\n\t" \
  303. "or\t%0, $1\n\t" \
  304. "8:lbu\t$1, 7(%2)\n\t" \
  305. "dsll\t%0, 0x8\n\t" \
  306. "or\t%0, $1\n\t" \
  307. "li\t%1, 0\n" \
  308. ".set\tpop\n\t" \
  309. "10:\n\t" \
  310. ".insn\n\t" \
  311. ".section\t.fixup,\"ax\"\n\t" \
  312. "11:\tli\t%1, %3\n\t" \
  313. "j\t10b\n\t" \
  314. ".previous\n\t" \
  315. ".section\t__ex_table,\"a\"\n\t" \
  316. STR(PTR)"\t1b, 11b\n\t" \
  317. STR(PTR)"\t2b, 11b\n\t" \
  318. STR(PTR)"\t3b, 11b\n\t" \
  319. STR(PTR)"\t4b, 11b\n\t" \
  320. STR(PTR)"\t5b, 11b\n\t" \
  321. STR(PTR)"\t6b, 11b\n\t" \
  322. STR(PTR)"\t7b, 11b\n\t" \
  323. STR(PTR)"\t8b, 11b\n\t" \
  324. ".previous" \
  325. : "=&r" (value), "=r" (res) \
  326. : "r" (addr), "i" (-EFAULT)); \
  327. } while(0)
  328. #endif /* CONFIG_CPU_MIPSR6 */
  329. #define _StoreHW(addr, value, res, type) \
  330. do { \
  331. __asm__ __volatile__ ( \
  332. ".set\tnoat\n" \
  333. "1:\t"type##_sb("%1", "1(%2)")"\n" \
  334. "srl\t$1, %1, 0x8\n" \
  335. "2:\t"type##_sb("$1", "0(%2)")"\n" \
  336. ".set\tat\n\t" \
  337. "li\t%0, 0\n" \
  338. "3:\n\t" \
  339. ".insn\n\t" \
  340. ".section\t.fixup,\"ax\"\n\t" \
  341. "4:\tli\t%0, %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" (res) \
  349. : "r" (value), "r" (addr), "i" (-EFAULT));\
  350. } while(0)
  351. #ifndef CONFIG_CPU_MIPSR6
  352. #define _StoreW(addr, value, res, type) \
  353. do { \
  354. __asm__ __volatile__ ( \
  355. "1:\t"type##_swl("%1", "(%2)")"\n" \
  356. "2:\t"type##_swr("%1", "3(%2)")"\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. } while(0)
  371. #define _StoreDW(addr, value, res) \
  372. do { \
  373. __asm__ __volatile__ ( \
  374. "1:\tsdl\t%1,(%2)\n" \
  375. "2:\tsdr\t%1, 7(%2)\n\t" \
  376. "li\t%0, 0\n" \
  377. "3:\n\t" \
  378. ".insn\n\t" \
  379. ".section\t.fixup,\"ax\"\n\t" \
  380. "4:\tli\t%0, %3\n\t" \
  381. "j\t3b\n\t" \
  382. ".previous\n\t" \
  383. ".section\t__ex_table,\"a\"\n\t" \
  384. STR(PTR)"\t1b, 4b\n\t" \
  385. STR(PTR)"\t2b, 4b\n\t" \
  386. ".previous" \
  387. : "=r" (res) \
  388. : "r" (value), "r" (addr), "i" (-EFAULT)); \
  389. } while(0)
  390. #else
  391. /* MIPSR6 has no swl and sdl instructions */
  392. #define _StoreW(addr, value, res, type) \
  393. do { \
  394. __asm__ __volatile__ ( \
  395. ".set\tpush\n\t" \
  396. ".set\tnoat\n\t" \
  397. "1:"type##_sb("%1", "3(%2)")"\n\t" \
  398. "srl\t$1, %1, 0x8\n\t" \
  399. "2:"type##_sb("$1", "2(%2)")"\n\t" \
  400. "srl\t$1, $1, 0x8\n\t" \
  401. "3:"type##_sb("$1", "1(%2)")"\n\t" \
  402. "srl\t$1, $1, 0x8\n\t" \
  403. "4:"type##_sb("$1", "0(%2)")"\n\t" \
  404. ".set\tpop\n\t" \
  405. "li\t%0, 0\n" \
  406. "10:\n\t" \
  407. ".insn\n\t" \
  408. ".section\t.fixup,\"ax\"\n\t" \
  409. "11:\tli\t%0, %3\n\t" \
  410. "j\t10b\n\t" \
  411. ".previous\n\t" \
  412. ".section\t__ex_table,\"a\"\n\t" \
  413. STR(PTR)"\t1b, 11b\n\t" \
  414. STR(PTR)"\t2b, 11b\n\t" \
  415. STR(PTR)"\t3b, 11b\n\t" \
  416. STR(PTR)"\t4b, 11b\n\t" \
  417. ".previous" \
  418. : "=&r" (res) \
  419. : "r" (value), "r" (addr), "i" (-EFAULT) \
  420. : "memory"); \
  421. } while(0)
  422. #define StoreDW(addr, value, res) \
  423. do { \
  424. __asm__ __volatile__ ( \
  425. ".set\tpush\n\t" \
  426. ".set\tnoat\n\t" \
  427. "1:sb\t%1, 7(%2)\n\t" \
  428. "dsrl\t$1, %1, 0x8\n\t" \
  429. "2:sb\t$1, 6(%2)\n\t" \
  430. "dsrl\t$1, $1, 0x8\n\t" \
  431. "3:sb\t$1, 5(%2)\n\t" \
  432. "dsrl\t$1, $1, 0x8\n\t" \
  433. "4:sb\t$1, 4(%2)\n\t" \
  434. "dsrl\t$1, $1, 0x8\n\t" \
  435. "5:sb\t$1, 3(%2)\n\t" \
  436. "dsrl\t$1, $1, 0x8\n\t" \
  437. "6:sb\t$1, 2(%2)\n\t" \
  438. "dsrl\t$1, $1, 0x8\n\t" \
  439. "7:sb\t$1, 1(%2)\n\t" \
  440. "dsrl\t$1, $1, 0x8\n\t" \
  441. "8:sb\t$1, 0(%2)\n\t" \
  442. "dsrl\t$1, $1, 0x8\n\t" \
  443. ".set\tpop\n\t" \
  444. "li\t%0, 0\n" \
  445. "10:\n\t" \
  446. ".insn\n\t" \
  447. ".section\t.fixup,\"ax\"\n\t" \
  448. "11:\tli\t%0, %3\n\t" \
  449. "j\t10b\n\t" \
  450. ".previous\n\t" \
  451. ".section\t__ex_table,\"a\"\n\t" \
  452. STR(PTR)"\t1b, 11b\n\t" \
  453. STR(PTR)"\t2b, 11b\n\t" \
  454. STR(PTR)"\t3b, 11b\n\t" \
  455. STR(PTR)"\t4b, 11b\n\t" \
  456. STR(PTR)"\t5b, 11b\n\t" \
  457. STR(PTR)"\t6b, 11b\n\t" \
  458. STR(PTR)"\t7b, 11b\n\t" \
  459. STR(PTR)"\t8b, 11b\n\t" \
  460. ".previous" \
  461. : "=&r" (res) \
  462. : "r" (value), "r" (addr), "i" (-EFAULT) \
  463. : "memory"); \
  464. } while(0)
  465. #endif /* CONFIG_CPU_MIPSR6 */
  466. #else /* __BIG_ENDIAN */
  467. #define _LoadHW(addr, value, res, type) \
  468. do { \
  469. __asm__ __volatile__ (".set\tnoat\n" \
  470. "1:\t"type##_lb("%0", "1(%2)")"\n" \
  471. "2:\t"type##_lbu("$1", "0(%2)")"\n\t"\
  472. "sll\t%0, 0x8\n\t" \
  473. "or\t%0, $1\n\t" \
  474. "li\t%1, 0\n" \
  475. "3:\t.set\tat\n\t" \
  476. ".insn\n\t" \
  477. ".section\t.fixup,\"ax\"\n\t" \
  478. "4:\tli\t%1, %3\n\t" \
  479. "j\t3b\n\t" \
  480. ".previous\n\t" \
  481. ".section\t__ex_table,\"a\"\n\t" \
  482. STR(PTR)"\t1b, 4b\n\t" \
  483. STR(PTR)"\t2b, 4b\n\t" \
  484. ".previous" \
  485. : "=&r" (value), "=r" (res) \
  486. : "r" (addr), "i" (-EFAULT)); \
  487. } while(0)
  488. #ifndef CONFIG_CPU_MIPSR6
  489. #define _LoadW(addr, value, res, type) \
  490. do { \
  491. __asm__ __volatile__ ( \
  492. "1:\t"type##_lwl("%0", "3(%2)")"\n" \
  493. "2:\t"type##_lwr("%0", "(%2)")"\n\t"\
  494. "li\t%1, 0\n" \
  495. "3:\n\t" \
  496. ".insn\n\t" \
  497. ".section\t.fixup,\"ax\"\n\t" \
  498. "4:\tli\t%1, %3\n\t" \
  499. "j\t3b\n\t" \
  500. ".previous\n\t" \
  501. ".section\t__ex_table,\"a\"\n\t" \
  502. STR(PTR)"\t1b, 4b\n\t" \
  503. STR(PTR)"\t2b, 4b\n\t" \
  504. ".previous" \
  505. : "=&r" (value), "=r" (res) \
  506. : "r" (addr), "i" (-EFAULT)); \
  507. } while(0)
  508. #else
  509. /* MIPSR6 has no lwl instruction */
  510. #define _LoadW(addr, value, res, type) \
  511. do { \
  512. __asm__ __volatile__ ( \
  513. ".set\tpush\n" \
  514. ".set\tnoat\n\t" \
  515. "1:"type##_lb("%0", "3(%2)")"\n\t" \
  516. "2:"type##_lbu("$1", "2(%2)")"\n\t" \
  517. "sll\t%0, 0x8\n\t" \
  518. "or\t%0, $1\n\t" \
  519. "3:"type##_lbu("$1", "1(%2)")"\n\t" \
  520. "sll\t%0, 0x8\n\t" \
  521. "or\t%0, $1\n\t" \
  522. "4:"type##_lbu("$1", "0(%2)")"\n\t" \
  523. "sll\t%0, 0x8\n\t" \
  524. "or\t%0, $1\n\t" \
  525. "li\t%1, 0\n" \
  526. ".set\tpop\n" \
  527. "10:\n\t" \
  528. ".insn\n\t" \
  529. ".section\t.fixup,\"ax\"\n\t" \
  530. "11:\tli\t%1, %3\n\t" \
  531. "j\t10b\n\t" \
  532. ".previous\n\t" \
  533. ".section\t__ex_table,\"a\"\n\t" \
  534. STR(PTR)"\t1b, 11b\n\t" \
  535. STR(PTR)"\t2b, 11b\n\t" \
  536. STR(PTR)"\t3b, 11b\n\t" \
  537. STR(PTR)"\t4b, 11b\n\t" \
  538. ".previous" \
  539. : "=&r" (value), "=r" (res) \
  540. : "r" (addr), "i" (-EFAULT)); \
  541. } while(0)
  542. #endif /* CONFIG_CPU_MIPSR6 */
  543. #define _LoadHWU(addr, value, res, type) \
  544. do { \
  545. __asm__ __volatile__ ( \
  546. ".set\tnoat\n" \
  547. "1:\t"type##_lbu("%0", "1(%2)")"\n" \
  548. "2:\t"type##_lbu("$1", "0(%2)")"\n\t"\
  549. "sll\t%0, 0x8\n\t" \
  550. "or\t%0, $1\n\t" \
  551. "li\t%1, 0\n" \
  552. "3:\n\t" \
  553. ".insn\n\t" \
  554. ".set\tat\n\t" \
  555. ".section\t.fixup,\"ax\"\n\t" \
  556. "4:\tli\t%1, %3\n\t" \
  557. "j\t3b\n\t" \
  558. ".previous\n\t" \
  559. ".section\t__ex_table,\"a\"\n\t" \
  560. STR(PTR)"\t1b, 4b\n\t" \
  561. STR(PTR)"\t2b, 4b\n\t" \
  562. ".previous" \
  563. : "=&r" (value), "=r" (res) \
  564. : "r" (addr), "i" (-EFAULT)); \
  565. } while(0)
  566. #ifndef CONFIG_CPU_MIPSR6
  567. #define _LoadWU(addr, value, res, type) \
  568. do { \
  569. __asm__ __volatile__ ( \
  570. "1:\t"type##_lwl("%0", "3(%2)")"\n" \
  571. "2:\t"type##_lwr("%0", "(%2)")"\n\t"\
  572. "dsll\t%0, %0, 32\n\t" \
  573. "dsrl\t%0, %0, 32\n\t" \
  574. "li\t%1, 0\n" \
  575. "3:\n\t" \
  576. ".insn\n\t" \
  577. "\t.section\t.fixup,\"ax\"\n\t" \
  578. "4:\tli\t%1, %3\n\t" \
  579. "j\t3b\n\t" \
  580. ".previous\n\t" \
  581. ".section\t__ex_table,\"a\"\n\t" \
  582. STR(PTR)"\t1b, 4b\n\t" \
  583. STR(PTR)"\t2b, 4b\n\t" \
  584. ".previous" \
  585. : "=&r" (value), "=r" (res) \
  586. : "r" (addr), "i" (-EFAULT)); \
  587. } while(0)
  588. #define _LoadDW(addr, value, res) \
  589. do { \
  590. __asm__ __volatile__ ( \
  591. "1:\tldl\t%0, 7(%2)\n" \
  592. "2:\tldr\t%0, (%2)\n\t" \
  593. "li\t%1, 0\n" \
  594. "3:\n\t" \
  595. ".insn\n\t" \
  596. "\t.section\t.fixup,\"ax\"\n\t" \
  597. "4:\tli\t%1, %3\n\t" \
  598. "j\t3b\n\t" \
  599. ".previous\n\t" \
  600. ".section\t__ex_table,\"a\"\n\t" \
  601. STR(PTR)"\t1b, 4b\n\t" \
  602. STR(PTR)"\t2b, 4b\n\t" \
  603. ".previous" \
  604. : "=&r" (value), "=r" (res) \
  605. : "r" (addr), "i" (-EFAULT)); \
  606. } while(0)
  607. #else
  608. /* MIPSR6 has not lwl and ldl instructions */
  609. #define _LoadWU(addr, value, res, type) \
  610. do { \
  611. __asm__ __volatile__ ( \
  612. ".set\tpush\n\t" \
  613. ".set\tnoat\n\t" \
  614. "1:"type##_lbu("%0", "3(%2)")"\n\t" \
  615. "2:"type##_lbu("$1", "2(%2)")"\n\t" \
  616. "sll\t%0, 0x8\n\t" \
  617. "or\t%0, $1\n\t" \
  618. "3:"type##_lbu("$1", "1(%2)")"\n\t" \
  619. "sll\t%0, 0x8\n\t" \
  620. "or\t%0, $1\n\t" \
  621. "4:"type##_lbu("$1", "0(%2)")"\n\t" \
  622. "sll\t%0, 0x8\n\t" \
  623. "or\t%0, $1\n\t" \
  624. "li\t%1, 0\n" \
  625. ".set\tpop\n" \
  626. "10:\n\t" \
  627. ".insn\n\t" \
  628. ".section\t.fixup,\"ax\"\n\t" \
  629. "11:\tli\t%1, %3\n\t" \
  630. "j\t10b\n\t" \
  631. ".previous\n\t" \
  632. ".section\t__ex_table,\"a\"\n\t" \
  633. STR(PTR)"\t1b, 11b\n\t" \
  634. STR(PTR)"\t2b, 11b\n\t" \
  635. STR(PTR)"\t3b, 11b\n\t" \
  636. STR(PTR)"\t4b, 11b\n\t" \
  637. ".previous" \
  638. : "=&r" (value), "=r" (res) \
  639. : "r" (addr), "i" (-EFAULT)); \
  640. } while(0)
  641. #define _LoadDW(addr, value, res) \
  642. do { \
  643. __asm__ __volatile__ ( \
  644. ".set\tpush\n\t" \
  645. ".set\tnoat\n\t" \
  646. "1:lb\t%0, 7(%2)\n\t" \
  647. "2:lbu\t$1, 6(%2)\n\t" \
  648. "dsll\t%0, 0x8\n\t" \
  649. "or\t%0, $1\n\t" \
  650. "3:lbu\t$1, 5(%2)\n\t" \
  651. "dsll\t%0, 0x8\n\t" \
  652. "or\t%0, $1\n\t" \
  653. "4:lbu\t$1, 4(%2)\n\t" \
  654. "dsll\t%0, 0x8\n\t" \
  655. "or\t%0, $1\n\t" \
  656. "5:lbu\t$1, 3(%2)\n\t" \
  657. "dsll\t%0, 0x8\n\t" \
  658. "or\t%0, $1\n\t" \
  659. "6:lbu\t$1, 2(%2)\n\t" \
  660. "dsll\t%0, 0x8\n\t" \
  661. "or\t%0, $1\n\t" \
  662. "7:lbu\t$1, 1(%2)\n\t" \
  663. "dsll\t%0, 0x8\n\t" \
  664. "or\t%0, $1\n\t" \
  665. "8:lbu\t$1, 0(%2)\n\t" \
  666. "dsll\t%0, 0x8\n\t" \
  667. "or\t%0, $1\n\t" \
  668. "li\t%1, 0\n" \
  669. ".set\tpop\n\t" \
  670. "10:\n\t" \
  671. ".insn\n\t" \
  672. ".section\t.fixup,\"ax\"\n\t" \
  673. "11:\tli\t%1, %3\n\t" \
  674. "j\t10b\n\t" \
  675. ".previous\n\t" \
  676. ".section\t__ex_table,\"a\"\n\t" \
  677. STR(PTR)"\t1b, 11b\n\t" \
  678. STR(PTR)"\t2b, 11b\n\t" \
  679. STR(PTR)"\t3b, 11b\n\t" \
  680. STR(PTR)"\t4b, 11b\n\t" \
  681. STR(PTR)"\t5b, 11b\n\t" \
  682. STR(PTR)"\t6b, 11b\n\t" \
  683. STR(PTR)"\t7b, 11b\n\t" \
  684. STR(PTR)"\t8b, 11b\n\t" \
  685. ".previous" \
  686. : "=&r" (value), "=r" (res) \
  687. : "r" (addr), "i" (-EFAULT)); \
  688. } while(0)
  689. #endif /* CONFIG_CPU_MIPSR6 */
  690. #define _StoreHW(addr, value, res, type) \
  691. do { \
  692. __asm__ __volatile__ ( \
  693. ".set\tnoat\n" \
  694. "1:\t"type##_sb("%1", "0(%2)")"\n" \
  695. "srl\t$1,%1, 0x8\n" \
  696. "2:\t"type##_sb("$1", "1(%2)")"\n" \
  697. ".set\tat\n\t" \
  698. "li\t%0, 0\n" \
  699. "3:\n\t" \
  700. ".insn\n\t" \
  701. ".section\t.fixup,\"ax\"\n\t" \
  702. "4:\tli\t%0, %3\n\t" \
  703. "j\t3b\n\t" \
  704. ".previous\n\t" \
  705. ".section\t__ex_table,\"a\"\n\t" \
  706. STR(PTR)"\t1b, 4b\n\t" \
  707. STR(PTR)"\t2b, 4b\n\t" \
  708. ".previous" \
  709. : "=r" (res) \
  710. : "r" (value), "r" (addr), "i" (-EFAULT));\
  711. } while(0)
  712. #ifndef CONFIG_CPU_MIPSR6
  713. #define _StoreW(addr, value, res, type) \
  714. do { \
  715. __asm__ __volatile__ ( \
  716. "1:\t"type##_swl("%1", "3(%2)")"\n" \
  717. "2:\t"type##_swr("%1", "(%2)")"\n\t"\
  718. "li\t%0, 0\n" \
  719. "3:\n\t" \
  720. ".insn\n\t" \
  721. ".section\t.fixup,\"ax\"\n\t" \
  722. "4:\tli\t%0, %3\n\t" \
  723. "j\t3b\n\t" \
  724. ".previous\n\t" \
  725. ".section\t__ex_table,\"a\"\n\t" \
  726. STR(PTR)"\t1b, 4b\n\t" \
  727. STR(PTR)"\t2b, 4b\n\t" \
  728. ".previous" \
  729. : "=r" (res) \
  730. : "r" (value), "r" (addr), "i" (-EFAULT)); \
  731. } while(0)
  732. #define _StoreDW(addr, value, res) \
  733. do { \
  734. __asm__ __volatile__ ( \
  735. "1:\tsdl\t%1, 7(%2)\n" \
  736. "2:\tsdr\t%1, (%2)\n\t" \
  737. "li\t%0, 0\n" \
  738. "3:\n\t" \
  739. ".insn\n\t" \
  740. ".section\t.fixup,\"ax\"\n\t" \
  741. "4:\tli\t%0, %3\n\t" \
  742. "j\t3b\n\t" \
  743. ".previous\n\t" \
  744. ".section\t__ex_table,\"a\"\n\t" \
  745. STR(PTR)"\t1b, 4b\n\t" \
  746. STR(PTR)"\t2b, 4b\n\t" \
  747. ".previous" \
  748. : "=r" (res) \
  749. : "r" (value), "r" (addr), "i" (-EFAULT)); \
  750. } while(0)
  751. #else
  752. /* MIPSR6 has no swl and sdl instructions */
  753. #define _StoreW(addr, value, res, type) \
  754. do { \
  755. __asm__ __volatile__ ( \
  756. ".set\tpush\n\t" \
  757. ".set\tnoat\n\t" \
  758. "1:"type##_sb("%1", "0(%2)")"\n\t" \
  759. "srl\t$1, %1, 0x8\n\t" \
  760. "2:"type##_sb("$1", "1(%2)")"\n\t" \
  761. "srl\t$1, $1, 0x8\n\t" \
  762. "3:"type##_sb("$1", "2(%2)")"\n\t" \
  763. "srl\t$1, $1, 0x8\n\t" \
  764. "4:"type##_sb("$1", "3(%2)")"\n\t" \
  765. ".set\tpop\n\t" \
  766. "li\t%0, 0\n" \
  767. "10:\n\t" \
  768. ".insn\n\t" \
  769. ".section\t.fixup,\"ax\"\n\t" \
  770. "11:\tli\t%0, %3\n\t" \
  771. "j\t10b\n\t" \
  772. ".previous\n\t" \
  773. ".section\t__ex_table,\"a\"\n\t" \
  774. STR(PTR)"\t1b, 11b\n\t" \
  775. STR(PTR)"\t2b, 11b\n\t" \
  776. STR(PTR)"\t3b, 11b\n\t" \
  777. STR(PTR)"\t4b, 11b\n\t" \
  778. ".previous" \
  779. : "=&r" (res) \
  780. : "r" (value), "r" (addr), "i" (-EFAULT) \
  781. : "memory"); \
  782. } while(0)
  783. #define _StoreDW(addr, value, res) \
  784. do { \
  785. __asm__ __volatile__ ( \
  786. ".set\tpush\n\t" \
  787. ".set\tnoat\n\t" \
  788. "1:sb\t%1, 0(%2)\n\t" \
  789. "dsrl\t$1, %1, 0x8\n\t" \
  790. "2:sb\t$1, 1(%2)\n\t" \
  791. "dsrl\t$1, $1, 0x8\n\t" \
  792. "3:sb\t$1, 2(%2)\n\t" \
  793. "dsrl\t$1, $1, 0x8\n\t" \
  794. "4:sb\t$1, 3(%2)\n\t" \
  795. "dsrl\t$1, $1, 0x8\n\t" \
  796. "5:sb\t$1, 4(%2)\n\t" \
  797. "dsrl\t$1, $1, 0x8\n\t" \
  798. "6:sb\t$1, 5(%2)\n\t" \
  799. "dsrl\t$1, $1, 0x8\n\t" \
  800. "7:sb\t$1, 6(%2)\n\t" \
  801. "dsrl\t$1, $1, 0x8\n\t" \
  802. "8:sb\t$1, 7(%2)\n\t" \
  803. "dsrl\t$1, $1, 0x8\n\t" \
  804. ".set\tpop\n\t" \
  805. "li\t%0, 0\n" \
  806. "10:\n\t" \
  807. ".insn\n\t" \
  808. ".section\t.fixup,\"ax\"\n\t" \
  809. "11:\tli\t%0, %3\n\t" \
  810. "j\t10b\n\t" \
  811. ".previous\n\t" \
  812. ".section\t__ex_table,\"a\"\n\t" \
  813. STR(PTR)"\t1b, 11b\n\t" \
  814. STR(PTR)"\t2b, 11b\n\t" \
  815. STR(PTR)"\t3b, 11b\n\t" \
  816. STR(PTR)"\t4b, 11b\n\t" \
  817. STR(PTR)"\t5b, 11b\n\t" \
  818. STR(PTR)"\t6b, 11b\n\t" \
  819. STR(PTR)"\t7b, 11b\n\t" \
  820. STR(PTR)"\t8b, 11b\n\t" \
  821. ".previous" \
  822. : "=&r" (res) \
  823. : "r" (value), "r" (addr), "i" (-EFAULT) \
  824. : "memory"); \
  825. } while(0)
  826. #endif /* CONFIG_CPU_MIPSR6 */
  827. #endif
  828. #define LoadHWU(addr, value, res) _LoadHWU(addr, value, res, kernel)
  829. #define LoadHWUE(addr, value, res) _LoadHWU(addr, value, res, user)
  830. #define LoadWU(addr, value, res) _LoadWU(addr, value, res, kernel)
  831. #define LoadWUE(addr, value, res) _LoadWU(addr, value, res, user)
  832. #define LoadHW(addr, value, res) _LoadHW(addr, value, res, kernel)
  833. #define LoadHWE(addr, value, res) _LoadHW(addr, value, res, user)
  834. #define LoadW(addr, value, res) _LoadW(addr, value, res, kernel)
  835. #define LoadWE(addr, value, res) _LoadW(addr, value, res, user)
  836. #define LoadDW(addr, value, res) _LoadDW(addr, value, res)
  837. #define StoreHW(addr, value, res) _StoreHW(addr, value, res, kernel)
  838. #define StoreHWE(addr, value, res) _StoreHW(addr, value, res, user)
  839. #define StoreW(addr, value, res) _StoreW(addr, value, res, kernel)
  840. #define StoreWE(addr, value, res) _StoreW(addr, value, res, user)
  841. #define StoreDW(addr, value, res) _StoreDW(addr, value, res)
  842. static void emulate_load_store_insn(struct pt_regs *regs,
  843. void __user *addr, unsigned int __user *pc)
  844. {
  845. union mips_instruction insn;
  846. unsigned long value;
  847. unsigned int res;
  848. unsigned long origpc;
  849. unsigned long orig31;
  850. void __user *fault_addr = NULL;
  851. #ifdef CONFIG_EVA
  852. mm_segment_t seg;
  853. #endif
  854. origpc = (unsigned long)pc;
  855. orig31 = regs->regs[31];
  856. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
  857. /*
  858. * This load never faults.
  859. */
  860. __get_user(insn.word, pc);
  861. switch (insn.i_format.opcode) {
  862. /*
  863. * These are instructions that a compiler doesn't generate. We
  864. * can assume therefore that the code is MIPS-aware and
  865. * really buggy. Emulating these instructions would break the
  866. * semantics anyway.
  867. */
  868. case ll_op:
  869. case lld_op:
  870. case sc_op:
  871. case scd_op:
  872. /*
  873. * For these instructions the only way to create an address
  874. * error is an attempted access to kernel/supervisor address
  875. * space.
  876. */
  877. case ldl_op:
  878. case ldr_op:
  879. case lwl_op:
  880. case lwr_op:
  881. case sdl_op:
  882. case sdr_op:
  883. case swl_op:
  884. case swr_op:
  885. case lb_op:
  886. case lbu_op:
  887. case sb_op:
  888. goto sigbus;
  889. /*
  890. * The remaining opcodes are the ones that are really of
  891. * interest.
  892. */
  893. #ifdef CONFIG_EVA
  894. case spec3_op:
  895. /*
  896. * we can land here only from kernel accessing user memory,
  897. * so we need to "switch" the address limit to user space, so
  898. * address check can work properly.
  899. */
  900. seg = get_fs();
  901. set_fs(USER_DS);
  902. switch (insn.spec3_format.func) {
  903. case lhe_op:
  904. if (!access_ok(VERIFY_READ, addr, 2)) {
  905. set_fs(seg);
  906. goto sigbus;
  907. }
  908. LoadHWE(addr, value, res);
  909. if (res) {
  910. set_fs(seg);
  911. goto fault;
  912. }
  913. compute_return_epc(regs);
  914. regs->regs[insn.spec3_format.rt] = value;
  915. break;
  916. case lwe_op:
  917. if (!access_ok(VERIFY_READ, addr, 4)) {
  918. set_fs(seg);
  919. goto sigbus;
  920. }
  921. LoadWE(addr, value, res);
  922. if (res) {
  923. set_fs(seg);
  924. goto fault;
  925. }
  926. compute_return_epc(regs);
  927. regs->regs[insn.spec3_format.rt] = value;
  928. break;
  929. case lhue_op:
  930. if (!access_ok(VERIFY_READ, addr, 2)) {
  931. set_fs(seg);
  932. goto sigbus;
  933. }
  934. LoadHWUE(addr, value, res);
  935. if (res) {
  936. set_fs(seg);
  937. goto fault;
  938. }
  939. compute_return_epc(regs);
  940. regs->regs[insn.spec3_format.rt] = value;
  941. break;
  942. case she_op:
  943. if (!access_ok(VERIFY_WRITE, addr, 2)) {
  944. set_fs(seg);
  945. goto sigbus;
  946. }
  947. compute_return_epc(regs);
  948. value = regs->regs[insn.spec3_format.rt];
  949. StoreHWE(addr, value, res);
  950. if (res) {
  951. set_fs(seg);
  952. goto fault;
  953. }
  954. break;
  955. case swe_op:
  956. if (!access_ok(VERIFY_WRITE, addr, 4)) {
  957. set_fs(seg);
  958. goto sigbus;
  959. }
  960. compute_return_epc(regs);
  961. value = regs->regs[insn.spec3_format.rt];
  962. StoreWE(addr, value, res);
  963. if (res) {
  964. set_fs(seg);
  965. goto fault;
  966. }
  967. break;
  968. default:
  969. set_fs(seg);
  970. goto sigill;
  971. }
  972. set_fs(seg);
  973. break;
  974. #endif
  975. case lh_op:
  976. if (!access_ok(VERIFY_READ, addr, 2))
  977. goto sigbus;
  978. if (config_enabled(CONFIG_EVA)) {
  979. if (segment_eq(get_fs(), get_ds()))
  980. LoadHW(addr, value, res);
  981. else
  982. LoadHWE(addr, value, res);
  983. } else {
  984. LoadHW(addr, value, res);
  985. }
  986. if (res)
  987. goto fault;
  988. compute_return_epc(regs);
  989. regs->regs[insn.i_format.rt] = value;
  990. break;
  991. case lw_op:
  992. if (!access_ok(VERIFY_READ, addr, 4))
  993. goto sigbus;
  994. if (config_enabled(CONFIG_EVA)) {
  995. if (segment_eq(get_fs(), get_ds()))
  996. LoadW(addr, value, res);
  997. else
  998. LoadWE(addr, value, res);
  999. } else {
  1000. LoadW(addr, value, res);
  1001. }
  1002. if (res)
  1003. goto fault;
  1004. compute_return_epc(regs);
  1005. regs->regs[insn.i_format.rt] = value;
  1006. break;
  1007. case lhu_op:
  1008. if (!access_ok(VERIFY_READ, addr, 2))
  1009. goto sigbus;
  1010. if (config_enabled(CONFIG_EVA)) {
  1011. if (segment_eq(get_fs(), get_ds()))
  1012. LoadHWU(addr, value, res);
  1013. else
  1014. LoadHWUE(addr, value, res);
  1015. } else {
  1016. LoadHWU(addr, value, res);
  1017. }
  1018. if (res)
  1019. goto fault;
  1020. compute_return_epc(regs);
  1021. regs->regs[insn.i_format.rt] = value;
  1022. break;
  1023. case lwu_op:
  1024. #ifdef CONFIG_64BIT
  1025. /*
  1026. * A 32-bit kernel might be running on a 64-bit processor. But
  1027. * if we're on a 32-bit processor and an i-cache incoherency
  1028. * or race makes us see a 64-bit instruction here the sdl/sdr
  1029. * would blow up, so for now we don't handle unaligned 64-bit
  1030. * instructions on 32-bit kernels.
  1031. */
  1032. if (!access_ok(VERIFY_READ, addr, 4))
  1033. goto sigbus;
  1034. LoadWU(addr, value, res);
  1035. if (res)
  1036. goto fault;
  1037. compute_return_epc(regs);
  1038. regs->regs[insn.i_format.rt] = value;
  1039. break;
  1040. #endif /* CONFIG_64BIT */
  1041. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1042. goto sigill;
  1043. case ld_op:
  1044. #ifdef CONFIG_64BIT
  1045. /*
  1046. * A 32-bit kernel might be running on a 64-bit processor. But
  1047. * if we're on a 32-bit processor and an i-cache incoherency
  1048. * or race makes us see a 64-bit instruction here the sdl/sdr
  1049. * would blow up, so for now we don't handle unaligned 64-bit
  1050. * instructions on 32-bit kernels.
  1051. */
  1052. if (!access_ok(VERIFY_READ, addr, 8))
  1053. goto sigbus;
  1054. LoadDW(addr, value, res);
  1055. if (res)
  1056. goto fault;
  1057. compute_return_epc(regs);
  1058. regs->regs[insn.i_format.rt] = value;
  1059. break;
  1060. #endif /* CONFIG_64BIT */
  1061. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1062. goto sigill;
  1063. case sh_op:
  1064. if (!access_ok(VERIFY_WRITE, addr, 2))
  1065. goto sigbus;
  1066. compute_return_epc(regs);
  1067. value = regs->regs[insn.i_format.rt];
  1068. if (config_enabled(CONFIG_EVA)) {
  1069. if (segment_eq(get_fs(), get_ds()))
  1070. StoreHW(addr, value, res);
  1071. else
  1072. StoreHWE(addr, value, res);
  1073. } else {
  1074. StoreHW(addr, value, res);
  1075. }
  1076. if (res)
  1077. goto fault;
  1078. break;
  1079. case sw_op:
  1080. if (!access_ok(VERIFY_WRITE, addr, 4))
  1081. goto sigbus;
  1082. compute_return_epc(regs);
  1083. value = regs->regs[insn.i_format.rt];
  1084. if (config_enabled(CONFIG_EVA)) {
  1085. if (segment_eq(get_fs(), get_ds()))
  1086. StoreW(addr, value, res);
  1087. else
  1088. StoreWE(addr, value, res);
  1089. } else {
  1090. StoreW(addr, value, res);
  1091. }
  1092. if (res)
  1093. goto fault;
  1094. break;
  1095. case sd_op:
  1096. #ifdef CONFIG_64BIT
  1097. /*
  1098. * A 32-bit kernel might be running on a 64-bit processor. But
  1099. * if we're on a 32-bit processor and an i-cache incoherency
  1100. * or race makes us see a 64-bit instruction here the sdl/sdr
  1101. * would blow up, so for now we don't handle unaligned 64-bit
  1102. * instructions on 32-bit kernels.
  1103. */
  1104. if (!access_ok(VERIFY_WRITE, addr, 8))
  1105. goto sigbus;
  1106. compute_return_epc(regs);
  1107. value = regs->regs[insn.i_format.rt];
  1108. StoreDW(addr, value, res);
  1109. if (res)
  1110. goto fault;
  1111. break;
  1112. #endif /* CONFIG_64BIT */
  1113. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1114. goto sigill;
  1115. case lwc1_op:
  1116. case ldc1_op:
  1117. case swc1_op:
  1118. case sdc1_op:
  1119. die_if_kernel("Unaligned FP access in kernel code", regs);
  1120. BUG_ON(!used_math());
  1121. lose_fpu(1); /* Save FPU state for the emulator. */
  1122. res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  1123. &fault_addr);
  1124. own_fpu(1); /* Restore FPU state. */
  1125. /* Signal if something went wrong. */
  1126. process_fpemu_return(res, fault_addr, 0);
  1127. if (res == 0)
  1128. break;
  1129. return;
  1130. #ifndef CONFIG_CPU_MIPSR6
  1131. /*
  1132. * COP2 is available to implementor for application specific use.
  1133. * It's up to applications to register a notifier chain and do
  1134. * whatever they have to do, including possible sending of signals.
  1135. *
  1136. * This instruction has been reallocated in Release 6
  1137. */
  1138. case lwc2_op:
  1139. cu2_notifier_call_chain(CU2_LWC2_OP, regs);
  1140. break;
  1141. case ldc2_op:
  1142. cu2_notifier_call_chain(CU2_LDC2_OP, regs);
  1143. break;
  1144. case swc2_op:
  1145. cu2_notifier_call_chain(CU2_SWC2_OP, regs);
  1146. break;
  1147. case sdc2_op:
  1148. cu2_notifier_call_chain(CU2_SDC2_OP, regs);
  1149. break;
  1150. #endif
  1151. default:
  1152. /*
  1153. * Pheeee... We encountered an yet unknown instruction or
  1154. * cache coherence problem. Die sucker, die ...
  1155. */
  1156. goto sigill;
  1157. }
  1158. #ifdef CONFIG_DEBUG_FS
  1159. unaligned_instructions++;
  1160. #endif
  1161. return;
  1162. fault:
  1163. /* roll back jump/branch */
  1164. regs->cp0_epc = origpc;
  1165. regs->regs[31] = orig31;
  1166. /* Did we have an exception handler installed? */
  1167. if (fixup_exception(regs))
  1168. return;
  1169. die_if_kernel("Unhandled kernel unaligned access", regs);
  1170. force_sig(SIGSEGV, current);
  1171. return;
  1172. sigbus:
  1173. die_if_kernel("Unhandled kernel unaligned access", regs);
  1174. force_sig(SIGBUS, current);
  1175. return;
  1176. sigill:
  1177. die_if_kernel
  1178. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1179. force_sig(SIGILL, current);
  1180. }
  1181. /* Recode table from 16-bit register notation to 32-bit GPR. */
  1182. const int reg16to32[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
  1183. /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
  1184. const int reg16to32st[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
  1185. static void emulate_load_store_microMIPS(struct pt_regs *regs,
  1186. void __user *addr)
  1187. {
  1188. unsigned long value;
  1189. unsigned int res;
  1190. int i;
  1191. unsigned int reg = 0, rvar;
  1192. unsigned long orig31;
  1193. u16 __user *pc16;
  1194. u16 halfword;
  1195. unsigned int word;
  1196. unsigned long origpc, contpc;
  1197. union mips_instruction insn;
  1198. struct mm_decoded_insn mminsn;
  1199. void __user *fault_addr = NULL;
  1200. origpc = regs->cp0_epc;
  1201. orig31 = regs->regs[31];
  1202. mminsn.micro_mips_mode = 1;
  1203. /*
  1204. * This load never faults.
  1205. */
  1206. pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
  1207. __get_user(halfword, pc16);
  1208. pc16++;
  1209. contpc = regs->cp0_epc + 2;
  1210. word = ((unsigned int)halfword << 16);
  1211. mminsn.pc_inc = 2;
  1212. if (!mm_insn_16bit(halfword)) {
  1213. __get_user(halfword, pc16);
  1214. pc16++;
  1215. contpc = regs->cp0_epc + 4;
  1216. mminsn.pc_inc = 4;
  1217. word |= halfword;
  1218. }
  1219. mminsn.insn = word;
  1220. if (get_user(halfword, pc16))
  1221. goto fault;
  1222. mminsn.next_pc_inc = 2;
  1223. word = ((unsigned int)halfword << 16);
  1224. if (!mm_insn_16bit(halfword)) {
  1225. pc16++;
  1226. if (get_user(halfword, pc16))
  1227. goto fault;
  1228. mminsn.next_pc_inc = 4;
  1229. word |= halfword;
  1230. }
  1231. mminsn.next_insn = word;
  1232. insn = (union mips_instruction)(mminsn.insn);
  1233. if (mm_isBranchInstr(regs, mminsn, &contpc))
  1234. insn = (union mips_instruction)(mminsn.next_insn);
  1235. /* Parse instruction to find what to do */
  1236. switch (insn.mm_i_format.opcode) {
  1237. case mm_pool32a_op:
  1238. switch (insn.mm_x_format.func) {
  1239. case mm_lwxs_op:
  1240. reg = insn.mm_x_format.rd;
  1241. goto loadW;
  1242. }
  1243. goto sigbus;
  1244. case mm_pool32b_op:
  1245. switch (insn.mm_m_format.func) {
  1246. case mm_lwp_func:
  1247. reg = insn.mm_m_format.rd;
  1248. if (reg == 31)
  1249. goto sigbus;
  1250. if (!access_ok(VERIFY_READ, addr, 8))
  1251. goto sigbus;
  1252. LoadW(addr, value, res);
  1253. if (res)
  1254. goto fault;
  1255. regs->regs[reg] = value;
  1256. addr += 4;
  1257. LoadW(addr, value, res);
  1258. if (res)
  1259. goto fault;
  1260. regs->regs[reg + 1] = value;
  1261. goto success;
  1262. case mm_swp_func:
  1263. reg = insn.mm_m_format.rd;
  1264. if (reg == 31)
  1265. goto sigbus;
  1266. if (!access_ok(VERIFY_WRITE, addr, 8))
  1267. goto sigbus;
  1268. value = regs->regs[reg];
  1269. StoreW(addr, value, res);
  1270. if (res)
  1271. goto fault;
  1272. addr += 4;
  1273. value = regs->regs[reg + 1];
  1274. StoreW(addr, value, res);
  1275. if (res)
  1276. goto fault;
  1277. goto success;
  1278. case mm_ldp_func:
  1279. #ifdef CONFIG_64BIT
  1280. reg = insn.mm_m_format.rd;
  1281. if (reg == 31)
  1282. goto sigbus;
  1283. if (!access_ok(VERIFY_READ, addr, 16))
  1284. goto sigbus;
  1285. LoadDW(addr, value, res);
  1286. if (res)
  1287. goto fault;
  1288. regs->regs[reg] = value;
  1289. addr += 8;
  1290. LoadDW(addr, value, res);
  1291. if (res)
  1292. goto fault;
  1293. regs->regs[reg + 1] = value;
  1294. goto success;
  1295. #endif /* CONFIG_64BIT */
  1296. goto sigill;
  1297. case mm_sdp_func:
  1298. #ifdef CONFIG_64BIT
  1299. reg = insn.mm_m_format.rd;
  1300. if (reg == 31)
  1301. goto sigbus;
  1302. if (!access_ok(VERIFY_WRITE, addr, 16))
  1303. goto sigbus;
  1304. value = regs->regs[reg];
  1305. StoreDW(addr, value, res);
  1306. if (res)
  1307. goto fault;
  1308. addr += 8;
  1309. value = regs->regs[reg + 1];
  1310. StoreDW(addr, value, res);
  1311. if (res)
  1312. goto fault;
  1313. goto success;
  1314. #endif /* CONFIG_64BIT */
  1315. goto sigill;
  1316. case mm_lwm32_func:
  1317. reg = insn.mm_m_format.rd;
  1318. rvar = reg & 0xf;
  1319. if ((rvar > 9) || !reg)
  1320. goto sigill;
  1321. if (reg & 0x10) {
  1322. if (!access_ok
  1323. (VERIFY_READ, addr, 4 * (rvar + 1)))
  1324. goto sigbus;
  1325. } else {
  1326. if (!access_ok(VERIFY_READ, addr, 4 * rvar))
  1327. goto sigbus;
  1328. }
  1329. if (rvar == 9)
  1330. rvar = 8;
  1331. for (i = 16; rvar; rvar--, i++) {
  1332. LoadW(addr, value, res);
  1333. if (res)
  1334. goto fault;
  1335. addr += 4;
  1336. regs->regs[i] = value;
  1337. }
  1338. if ((reg & 0xf) == 9) {
  1339. LoadW(addr, value, res);
  1340. if (res)
  1341. goto fault;
  1342. addr += 4;
  1343. regs->regs[30] = value;
  1344. }
  1345. if (reg & 0x10) {
  1346. LoadW(addr, value, res);
  1347. if (res)
  1348. goto fault;
  1349. regs->regs[31] = value;
  1350. }
  1351. goto success;
  1352. case mm_swm32_func:
  1353. reg = insn.mm_m_format.rd;
  1354. rvar = reg & 0xf;
  1355. if ((rvar > 9) || !reg)
  1356. goto sigill;
  1357. if (reg & 0x10) {
  1358. if (!access_ok
  1359. (VERIFY_WRITE, addr, 4 * (rvar + 1)))
  1360. goto sigbus;
  1361. } else {
  1362. if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
  1363. goto sigbus;
  1364. }
  1365. if (rvar == 9)
  1366. rvar = 8;
  1367. for (i = 16; rvar; rvar--, i++) {
  1368. value = regs->regs[i];
  1369. StoreW(addr, value, res);
  1370. if (res)
  1371. goto fault;
  1372. addr += 4;
  1373. }
  1374. if ((reg & 0xf) == 9) {
  1375. value = regs->regs[30];
  1376. StoreW(addr, value, res);
  1377. if (res)
  1378. goto fault;
  1379. addr += 4;
  1380. }
  1381. if (reg & 0x10) {
  1382. value = regs->regs[31];
  1383. StoreW(addr, value, res);
  1384. if (res)
  1385. goto fault;
  1386. }
  1387. goto success;
  1388. case mm_ldm_func:
  1389. #ifdef CONFIG_64BIT
  1390. reg = insn.mm_m_format.rd;
  1391. rvar = reg & 0xf;
  1392. if ((rvar > 9) || !reg)
  1393. goto sigill;
  1394. if (reg & 0x10) {
  1395. if (!access_ok
  1396. (VERIFY_READ, addr, 8 * (rvar + 1)))
  1397. goto sigbus;
  1398. } else {
  1399. if (!access_ok(VERIFY_READ, addr, 8 * rvar))
  1400. goto sigbus;
  1401. }
  1402. if (rvar == 9)
  1403. rvar = 8;
  1404. for (i = 16; rvar; rvar--, i++) {
  1405. LoadDW(addr, value, res);
  1406. if (res)
  1407. goto fault;
  1408. addr += 4;
  1409. regs->regs[i] = value;
  1410. }
  1411. if ((reg & 0xf) == 9) {
  1412. LoadDW(addr, value, res);
  1413. if (res)
  1414. goto fault;
  1415. addr += 8;
  1416. regs->regs[30] = value;
  1417. }
  1418. if (reg & 0x10) {
  1419. LoadDW(addr, value, res);
  1420. if (res)
  1421. goto fault;
  1422. regs->regs[31] = value;
  1423. }
  1424. goto success;
  1425. #endif /* CONFIG_64BIT */
  1426. goto sigill;
  1427. case mm_sdm_func:
  1428. #ifdef CONFIG_64BIT
  1429. reg = insn.mm_m_format.rd;
  1430. rvar = reg & 0xf;
  1431. if ((rvar > 9) || !reg)
  1432. goto sigill;
  1433. if (reg & 0x10) {
  1434. if (!access_ok
  1435. (VERIFY_WRITE, addr, 8 * (rvar + 1)))
  1436. goto sigbus;
  1437. } else {
  1438. if (!access_ok(VERIFY_WRITE, addr, 8 * rvar))
  1439. goto sigbus;
  1440. }
  1441. if (rvar == 9)
  1442. rvar = 8;
  1443. for (i = 16; rvar; rvar--, i++) {
  1444. value = regs->regs[i];
  1445. StoreDW(addr, value, res);
  1446. if (res)
  1447. goto fault;
  1448. addr += 8;
  1449. }
  1450. if ((reg & 0xf) == 9) {
  1451. value = regs->regs[30];
  1452. StoreDW(addr, value, res);
  1453. if (res)
  1454. goto fault;
  1455. addr += 8;
  1456. }
  1457. if (reg & 0x10) {
  1458. value = regs->regs[31];
  1459. StoreDW(addr, value, res);
  1460. if (res)
  1461. goto fault;
  1462. }
  1463. goto success;
  1464. #endif /* CONFIG_64BIT */
  1465. goto sigill;
  1466. /* LWC2, SWC2, LDC2, SDC2 are not serviced */
  1467. }
  1468. goto sigbus;
  1469. case mm_pool32c_op:
  1470. switch (insn.mm_m_format.func) {
  1471. case mm_lwu_func:
  1472. reg = insn.mm_m_format.rd;
  1473. goto loadWU;
  1474. }
  1475. /* LL,SC,LLD,SCD are not serviced */
  1476. goto sigbus;
  1477. case mm_pool32f_op:
  1478. switch (insn.mm_x_format.func) {
  1479. case mm_lwxc1_func:
  1480. case mm_swxc1_func:
  1481. case mm_ldxc1_func:
  1482. case mm_sdxc1_func:
  1483. goto fpu_emul;
  1484. }
  1485. goto sigbus;
  1486. case mm_ldc132_op:
  1487. case mm_sdc132_op:
  1488. case mm_lwc132_op:
  1489. case mm_swc132_op:
  1490. fpu_emul:
  1491. /* roll back jump/branch */
  1492. regs->cp0_epc = origpc;
  1493. regs->regs[31] = orig31;
  1494. die_if_kernel("Unaligned FP access in kernel code", regs);
  1495. BUG_ON(!used_math());
  1496. BUG_ON(!is_fpu_owner());
  1497. lose_fpu(1); /* save the FPU state for the emulator */
  1498. res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  1499. &fault_addr);
  1500. own_fpu(1); /* restore FPU state */
  1501. /* If something went wrong, signal */
  1502. process_fpemu_return(res, fault_addr, 0);
  1503. if (res == 0)
  1504. goto success;
  1505. return;
  1506. case mm_lh32_op:
  1507. reg = insn.mm_i_format.rt;
  1508. goto loadHW;
  1509. case mm_lhu32_op:
  1510. reg = insn.mm_i_format.rt;
  1511. goto loadHWU;
  1512. case mm_lw32_op:
  1513. reg = insn.mm_i_format.rt;
  1514. goto loadW;
  1515. case mm_sh32_op:
  1516. reg = insn.mm_i_format.rt;
  1517. goto storeHW;
  1518. case mm_sw32_op:
  1519. reg = insn.mm_i_format.rt;
  1520. goto storeW;
  1521. case mm_ld32_op:
  1522. reg = insn.mm_i_format.rt;
  1523. goto loadDW;
  1524. case mm_sd32_op:
  1525. reg = insn.mm_i_format.rt;
  1526. goto storeDW;
  1527. case mm_pool16c_op:
  1528. switch (insn.mm16_m_format.func) {
  1529. case mm_lwm16_op:
  1530. reg = insn.mm16_m_format.rlist;
  1531. rvar = reg + 1;
  1532. if (!access_ok(VERIFY_READ, addr, 4 * rvar))
  1533. goto sigbus;
  1534. for (i = 16; rvar; rvar--, i++) {
  1535. LoadW(addr, value, res);
  1536. if (res)
  1537. goto fault;
  1538. addr += 4;
  1539. regs->regs[i] = value;
  1540. }
  1541. LoadW(addr, value, res);
  1542. if (res)
  1543. goto fault;
  1544. regs->regs[31] = value;
  1545. goto success;
  1546. case mm_swm16_op:
  1547. reg = insn.mm16_m_format.rlist;
  1548. rvar = reg + 1;
  1549. if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
  1550. goto sigbus;
  1551. for (i = 16; rvar; rvar--, i++) {
  1552. value = regs->regs[i];
  1553. StoreW(addr, value, res);
  1554. if (res)
  1555. goto fault;
  1556. addr += 4;
  1557. }
  1558. value = regs->regs[31];
  1559. StoreW(addr, value, res);
  1560. if (res)
  1561. goto fault;
  1562. goto success;
  1563. }
  1564. goto sigbus;
  1565. case mm_lhu16_op:
  1566. reg = reg16to32[insn.mm16_rb_format.rt];
  1567. goto loadHWU;
  1568. case mm_lw16_op:
  1569. reg = reg16to32[insn.mm16_rb_format.rt];
  1570. goto loadW;
  1571. case mm_sh16_op:
  1572. reg = reg16to32st[insn.mm16_rb_format.rt];
  1573. goto storeHW;
  1574. case mm_sw16_op:
  1575. reg = reg16to32st[insn.mm16_rb_format.rt];
  1576. goto storeW;
  1577. case mm_lwsp16_op:
  1578. reg = insn.mm16_r5_format.rt;
  1579. goto loadW;
  1580. case mm_swsp16_op:
  1581. reg = insn.mm16_r5_format.rt;
  1582. goto storeW;
  1583. case mm_lwgp16_op:
  1584. reg = reg16to32[insn.mm16_r3_format.rt];
  1585. goto loadW;
  1586. default:
  1587. goto sigill;
  1588. }
  1589. loadHW:
  1590. if (!access_ok(VERIFY_READ, addr, 2))
  1591. goto sigbus;
  1592. LoadHW(addr, value, res);
  1593. if (res)
  1594. goto fault;
  1595. regs->regs[reg] = value;
  1596. goto success;
  1597. loadHWU:
  1598. if (!access_ok(VERIFY_READ, addr, 2))
  1599. goto sigbus;
  1600. LoadHWU(addr, value, res);
  1601. if (res)
  1602. goto fault;
  1603. regs->regs[reg] = value;
  1604. goto success;
  1605. loadW:
  1606. if (!access_ok(VERIFY_READ, addr, 4))
  1607. goto sigbus;
  1608. LoadW(addr, value, res);
  1609. if (res)
  1610. goto fault;
  1611. regs->regs[reg] = value;
  1612. goto success;
  1613. loadWU:
  1614. #ifdef CONFIG_64BIT
  1615. /*
  1616. * A 32-bit kernel might be running on a 64-bit processor. But
  1617. * if we're on a 32-bit processor and an i-cache incoherency
  1618. * or race makes us see a 64-bit instruction here the sdl/sdr
  1619. * would blow up, so for now we don't handle unaligned 64-bit
  1620. * instructions on 32-bit kernels.
  1621. */
  1622. if (!access_ok(VERIFY_READ, addr, 4))
  1623. goto sigbus;
  1624. LoadWU(addr, value, res);
  1625. if (res)
  1626. goto fault;
  1627. regs->regs[reg] = value;
  1628. goto success;
  1629. #endif /* CONFIG_64BIT */
  1630. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1631. goto sigill;
  1632. loadDW:
  1633. #ifdef CONFIG_64BIT
  1634. /*
  1635. * A 32-bit kernel might be running on a 64-bit processor. But
  1636. * if we're on a 32-bit processor and an i-cache incoherency
  1637. * or race makes us see a 64-bit instruction here the sdl/sdr
  1638. * would blow up, so for now we don't handle unaligned 64-bit
  1639. * instructions on 32-bit kernels.
  1640. */
  1641. if (!access_ok(VERIFY_READ, addr, 8))
  1642. goto sigbus;
  1643. LoadDW(addr, value, res);
  1644. if (res)
  1645. goto fault;
  1646. regs->regs[reg] = value;
  1647. goto success;
  1648. #endif /* CONFIG_64BIT */
  1649. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1650. goto sigill;
  1651. storeHW:
  1652. if (!access_ok(VERIFY_WRITE, addr, 2))
  1653. goto sigbus;
  1654. value = regs->regs[reg];
  1655. StoreHW(addr, value, res);
  1656. if (res)
  1657. goto fault;
  1658. goto success;
  1659. storeW:
  1660. if (!access_ok(VERIFY_WRITE, addr, 4))
  1661. goto sigbus;
  1662. value = regs->regs[reg];
  1663. StoreW(addr, value, res);
  1664. if (res)
  1665. goto fault;
  1666. goto success;
  1667. storeDW:
  1668. #ifdef CONFIG_64BIT
  1669. /*
  1670. * A 32-bit kernel might be running on a 64-bit processor. But
  1671. * if we're on a 32-bit processor and an i-cache incoherency
  1672. * or race makes us see a 64-bit instruction here the sdl/sdr
  1673. * would blow up, so for now we don't handle unaligned 64-bit
  1674. * instructions on 32-bit kernels.
  1675. */
  1676. if (!access_ok(VERIFY_WRITE, addr, 8))
  1677. goto sigbus;
  1678. value = regs->regs[reg];
  1679. StoreDW(addr, value, res);
  1680. if (res)
  1681. goto fault;
  1682. goto success;
  1683. #endif /* CONFIG_64BIT */
  1684. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1685. goto sigill;
  1686. success:
  1687. regs->cp0_epc = contpc; /* advance or branch */
  1688. #ifdef CONFIG_DEBUG_FS
  1689. unaligned_instructions++;
  1690. #endif
  1691. return;
  1692. fault:
  1693. /* roll back jump/branch */
  1694. regs->cp0_epc = origpc;
  1695. regs->regs[31] = orig31;
  1696. /* Did we have an exception handler installed? */
  1697. if (fixup_exception(regs))
  1698. return;
  1699. die_if_kernel("Unhandled kernel unaligned access", regs);
  1700. force_sig(SIGSEGV, current);
  1701. return;
  1702. sigbus:
  1703. die_if_kernel("Unhandled kernel unaligned access", regs);
  1704. force_sig(SIGBUS, current);
  1705. return;
  1706. sigill:
  1707. die_if_kernel
  1708. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1709. force_sig(SIGILL, current);
  1710. }
  1711. static void emulate_load_store_MIPS16e(struct pt_regs *regs, void __user * addr)
  1712. {
  1713. unsigned long value;
  1714. unsigned int res;
  1715. int reg;
  1716. unsigned long orig31;
  1717. u16 __user *pc16;
  1718. unsigned long origpc;
  1719. union mips16e_instruction mips16inst, oldinst;
  1720. origpc = regs->cp0_epc;
  1721. orig31 = regs->regs[31];
  1722. pc16 = (unsigned short __user *)msk_isa16_mode(origpc);
  1723. /*
  1724. * This load never faults.
  1725. */
  1726. __get_user(mips16inst.full, pc16);
  1727. oldinst = mips16inst;
  1728. /* skip EXTEND instruction */
  1729. if (mips16inst.ri.opcode == MIPS16e_extend_op) {
  1730. pc16++;
  1731. __get_user(mips16inst.full, pc16);
  1732. } else if (delay_slot(regs)) {
  1733. /* skip jump instructions */
  1734. /* JAL/JALX are 32 bits but have OPCODE in first short int */
  1735. if (mips16inst.ri.opcode == MIPS16e_jal_op)
  1736. pc16++;
  1737. pc16++;
  1738. if (get_user(mips16inst.full, pc16))
  1739. goto sigbus;
  1740. }
  1741. switch (mips16inst.ri.opcode) {
  1742. case MIPS16e_i64_op: /* I64 or RI64 instruction */
  1743. switch (mips16inst.i64.func) { /* I64/RI64 func field check */
  1744. case MIPS16e_ldpc_func:
  1745. case MIPS16e_ldsp_func:
  1746. reg = reg16to32[mips16inst.ri64.ry];
  1747. goto loadDW;
  1748. case MIPS16e_sdsp_func:
  1749. reg = reg16to32[mips16inst.ri64.ry];
  1750. goto writeDW;
  1751. case MIPS16e_sdrasp_func:
  1752. reg = 29; /* GPRSP */
  1753. goto writeDW;
  1754. }
  1755. goto sigbus;
  1756. case MIPS16e_swsp_op:
  1757. case MIPS16e_lwpc_op:
  1758. case MIPS16e_lwsp_op:
  1759. reg = reg16to32[mips16inst.ri.rx];
  1760. break;
  1761. case MIPS16e_i8_op:
  1762. if (mips16inst.i8.func != MIPS16e_swrasp_func)
  1763. goto sigbus;
  1764. reg = 29; /* GPRSP */
  1765. break;
  1766. default:
  1767. reg = reg16to32[mips16inst.rri.ry];
  1768. break;
  1769. }
  1770. switch (mips16inst.ri.opcode) {
  1771. case MIPS16e_lb_op:
  1772. case MIPS16e_lbu_op:
  1773. case MIPS16e_sb_op:
  1774. goto sigbus;
  1775. case MIPS16e_lh_op:
  1776. if (!access_ok(VERIFY_READ, addr, 2))
  1777. goto sigbus;
  1778. LoadHW(addr, value, res);
  1779. if (res)
  1780. goto fault;
  1781. MIPS16e_compute_return_epc(regs, &oldinst);
  1782. regs->regs[reg] = value;
  1783. break;
  1784. case MIPS16e_lhu_op:
  1785. if (!access_ok(VERIFY_READ, addr, 2))
  1786. goto sigbus;
  1787. LoadHWU(addr, value, res);
  1788. if (res)
  1789. goto fault;
  1790. MIPS16e_compute_return_epc(regs, &oldinst);
  1791. regs->regs[reg] = value;
  1792. break;
  1793. case MIPS16e_lw_op:
  1794. case MIPS16e_lwpc_op:
  1795. case MIPS16e_lwsp_op:
  1796. if (!access_ok(VERIFY_READ, addr, 4))
  1797. goto sigbus;
  1798. LoadW(addr, value, res);
  1799. if (res)
  1800. goto fault;
  1801. MIPS16e_compute_return_epc(regs, &oldinst);
  1802. regs->regs[reg] = value;
  1803. break;
  1804. case MIPS16e_lwu_op:
  1805. #ifdef CONFIG_64BIT
  1806. /*
  1807. * A 32-bit kernel might be running on a 64-bit processor. But
  1808. * if we're on a 32-bit processor and an i-cache incoherency
  1809. * or race makes us see a 64-bit instruction here the sdl/sdr
  1810. * would blow up, so for now we don't handle unaligned 64-bit
  1811. * instructions on 32-bit kernels.
  1812. */
  1813. if (!access_ok(VERIFY_READ, addr, 4))
  1814. goto sigbus;
  1815. LoadWU(addr, value, res);
  1816. if (res)
  1817. goto fault;
  1818. MIPS16e_compute_return_epc(regs, &oldinst);
  1819. regs->regs[reg] = value;
  1820. break;
  1821. #endif /* CONFIG_64BIT */
  1822. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1823. goto sigill;
  1824. case MIPS16e_ld_op:
  1825. loadDW:
  1826. #ifdef CONFIG_64BIT
  1827. /*
  1828. * A 32-bit kernel might be running on a 64-bit processor. But
  1829. * if we're on a 32-bit processor and an i-cache incoherency
  1830. * or race makes us see a 64-bit instruction here the sdl/sdr
  1831. * would blow up, so for now we don't handle unaligned 64-bit
  1832. * instructions on 32-bit kernels.
  1833. */
  1834. if (!access_ok(VERIFY_READ, addr, 8))
  1835. goto sigbus;
  1836. LoadDW(addr, value, res);
  1837. if (res)
  1838. goto fault;
  1839. MIPS16e_compute_return_epc(regs, &oldinst);
  1840. regs->regs[reg] = value;
  1841. break;
  1842. #endif /* CONFIG_64BIT */
  1843. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1844. goto sigill;
  1845. case MIPS16e_sh_op:
  1846. if (!access_ok(VERIFY_WRITE, addr, 2))
  1847. goto sigbus;
  1848. MIPS16e_compute_return_epc(regs, &oldinst);
  1849. value = regs->regs[reg];
  1850. StoreHW(addr, value, res);
  1851. if (res)
  1852. goto fault;
  1853. break;
  1854. case MIPS16e_sw_op:
  1855. case MIPS16e_swsp_op:
  1856. case MIPS16e_i8_op: /* actually - MIPS16e_swrasp_func */
  1857. if (!access_ok(VERIFY_WRITE, addr, 4))
  1858. goto sigbus;
  1859. MIPS16e_compute_return_epc(regs, &oldinst);
  1860. value = regs->regs[reg];
  1861. StoreW(addr, value, res);
  1862. if (res)
  1863. goto fault;
  1864. break;
  1865. case MIPS16e_sd_op:
  1866. writeDW:
  1867. #ifdef CONFIG_64BIT
  1868. /*
  1869. * A 32-bit kernel might be running on a 64-bit processor. But
  1870. * if we're on a 32-bit processor and an i-cache incoherency
  1871. * or race makes us see a 64-bit instruction here the sdl/sdr
  1872. * would blow up, so for now we don't handle unaligned 64-bit
  1873. * instructions on 32-bit kernels.
  1874. */
  1875. if (!access_ok(VERIFY_WRITE, addr, 8))
  1876. goto sigbus;
  1877. MIPS16e_compute_return_epc(regs, &oldinst);
  1878. value = regs->regs[reg];
  1879. StoreDW(addr, value, res);
  1880. if (res)
  1881. goto fault;
  1882. break;
  1883. #endif /* CONFIG_64BIT */
  1884. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1885. goto sigill;
  1886. default:
  1887. /*
  1888. * Pheeee... We encountered an yet unknown instruction or
  1889. * cache coherence problem. Die sucker, die ...
  1890. */
  1891. goto sigill;
  1892. }
  1893. #ifdef CONFIG_DEBUG_FS
  1894. unaligned_instructions++;
  1895. #endif
  1896. return;
  1897. fault:
  1898. /* roll back jump/branch */
  1899. regs->cp0_epc = origpc;
  1900. regs->regs[31] = orig31;
  1901. /* Did we have an exception handler installed? */
  1902. if (fixup_exception(regs))
  1903. return;
  1904. die_if_kernel("Unhandled kernel unaligned access", regs);
  1905. force_sig(SIGSEGV, current);
  1906. return;
  1907. sigbus:
  1908. die_if_kernel("Unhandled kernel unaligned access", regs);
  1909. force_sig(SIGBUS, current);
  1910. return;
  1911. sigill:
  1912. die_if_kernel
  1913. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1914. force_sig(SIGILL, current);
  1915. }
  1916. asmlinkage void do_ade(struct pt_regs *regs)
  1917. {
  1918. enum ctx_state prev_state;
  1919. unsigned int __user *pc;
  1920. mm_segment_t seg;
  1921. prev_state = exception_enter();
  1922. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
  1923. 1, regs, regs->cp0_badvaddr);
  1924. /*
  1925. * Did we catch a fault trying to load an instruction?
  1926. */
  1927. if (regs->cp0_badvaddr == regs->cp0_epc)
  1928. goto sigbus;
  1929. if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
  1930. goto sigbus;
  1931. if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
  1932. goto sigbus;
  1933. /*
  1934. * Do branch emulation only if we didn't forward the exception.
  1935. * This is all so but ugly ...
  1936. */
  1937. /*
  1938. * Are we running in microMIPS mode?
  1939. */
  1940. if (get_isa16_mode(regs->cp0_epc)) {
  1941. /*
  1942. * Did we catch a fault trying to load an instruction in
  1943. * 16-bit mode?
  1944. */
  1945. if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
  1946. goto sigbus;
  1947. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  1948. show_registers(regs);
  1949. if (cpu_has_mmips) {
  1950. seg = get_fs();
  1951. if (!user_mode(regs))
  1952. set_fs(KERNEL_DS);
  1953. emulate_load_store_microMIPS(regs,
  1954. (void __user *)regs->cp0_badvaddr);
  1955. set_fs(seg);
  1956. return;
  1957. }
  1958. if (cpu_has_mips16) {
  1959. seg = get_fs();
  1960. if (!user_mode(regs))
  1961. set_fs(KERNEL_DS);
  1962. emulate_load_store_MIPS16e(regs,
  1963. (void __user *)regs->cp0_badvaddr);
  1964. set_fs(seg);
  1965. return;
  1966. }
  1967. goto sigbus;
  1968. }
  1969. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  1970. show_registers(regs);
  1971. pc = (unsigned int __user *)exception_epc(regs);
  1972. seg = get_fs();
  1973. if (!user_mode(regs))
  1974. set_fs(KERNEL_DS);
  1975. emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
  1976. set_fs(seg);
  1977. return;
  1978. sigbus:
  1979. die_if_kernel("Kernel unaligned instruction access", regs);
  1980. force_sig(SIGBUS, current);
  1981. /*
  1982. * XXX On return from the signal handler we should advance the epc
  1983. */
  1984. exception_exit(prev_state);
  1985. }
  1986. #ifdef CONFIG_DEBUG_FS
  1987. extern struct dentry *mips_debugfs_dir;
  1988. static int __init debugfs_unaligned(void)
  1989. {
  1990. struct dentry *d;
  1991. if (!mips_debugfs_dir)
  1992. return -ENODEV;
  1993. d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
  1994. mips_debugfs_dir, &unaligned_instructions);
  1995. if (!d)
  1996. return -ENOMEM;
  1997. d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
  1998. mips_debugfs_dir, &unaligned_action);
  1999. if (!d)
  2000. return -ENOMEM;
  2001. return 0;
  2002. }
  2003. __initcall(debugfs_unaligned);
  2004. #endif