unaligned.c 60 KB

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