unaligned.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386
  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 <linux/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. #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
  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 /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
  149. /* For CPUs without 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_HAS_LOAD_STORE_LR */
  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. #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
  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 /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
  248. /* For CPUs without 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_HAS_LOAD_STORE_LR */
  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. #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
  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 /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
  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_HAS_LOAD_STORE_LR */
  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. #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
  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 /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
  509. /* For CPUs without 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_HAS_LOAD_STORE_LR */
  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. #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
  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 /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
  608. /* For CPUs without 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_HAS_LOAD_STORE_LR */
  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. #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
  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 /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
  752. /* For CPUs without 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_HAS_LOAD_STORE_LR */
  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, preempted;
  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. union fpureg *fpr;
  855. enum msa_2b_fmt df;
  856. unsigned int wd;
  857. origpc = (unsigned long)pc;
  858. orig31 = regs->regs[31];
  859. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
  860. /*
  861. * This load never faults.
  862. */
  863. __get_user(insn.word, pc);
  864. switch (insn.i_format.opcode) {
  865. /*
  866. * These are instructions that a compiler doesn't generate. We
  867. * can assume therefore that the code is MIPS-aware and
  868. * really buggy. Emulating these instructions would break the
  869. * semantics anyway.
  870. */
  871. case ll_op:
  872. case lld_op:
  873. case sc_op:
  874. case scd_op:
  875. /*
  876. * For these instructions the only way to create an address
  877. * error is an attempted access to kernel/supervisor address
  878. * space.
  879. */
  880. case ldl_op:
  881. case ldr_op:
  882. case lwl_op:
  883. case lwr_op:
  884. case sdl_op:
  885. case sdr_op:
  886. case swl_op:
  887. case swr_op:
  888. case lb_op:
  889. case lbu_op:
  890. case sb_op:
  891. goto sigbus;
  892. /*
  893. * The remaining opcodes are the ones that are really of
  894. * interest.
  895. */
  896. case spec3_op:
  897. if (insn.dsp_format.func == lx_op) {
  898. switch (insn.dsp_format.op) {
  899. case lwx_op:
  900. if (!access_ok(VERIFY_READ, addr, 4))
  901. goto sigbus;
  902. LoadW(addr, value, res);
  903. if (res)
  904. goto fault;
  905. compute_return_epc(regs);
  906. regs->regs[insn.dsp_format.rd] = value;
  907. break;
  908. case lhx_op:
  909. if (!access_ok(VERIFY_READ, addr, 2))
  910. goto sigbus;
  911. LoadHW(addr, value, res);
  912. if (res)
  913. goto fault;
  914. compute_return_epc(regs);
  915. regs->regs[insn.dsp_format.rd] = value;
  916. break;
  917. default:
  918. goto sigill;
  919. }
  920. }
  921. #ifdef CONFIG_EVA
  922. else {
  923. /*
  924. * we can land here only from kernel accessing user
  925. * memory, so we need to "switch" the address limit to
  926. * user space, so that address check can work properly.
  927. */
  928. seg = get_fs();
  929. set_fs(USER_DS);
  930. switch (insn.spec3_format.func) {
  931. case lhe_op:
  932. if (!access_ok(VERIFY_READ, addr, 2)) {
  933. set_fs(seg);
  934. goto sigbus;
  935. }
  936. LoadHWE(addr, value, res);
  937. if (res) {
  938. set_fs(seg);
  939. goto fault;
  940. }
  941. compute_return_epc(regs);
  942. regs->regs[insn.spec3_format.rt] = value;
  943. break;
  944. case lwe_op:
  945. if (!access_ok(VERIFY_READ, addr, 4)) {
  946. set_fs(seg);
  947. goto sigbus;
  948. }
  949. LoadWE(addr, value, res);
  950. if (res) {
  951. set_fs(seg);
  952. goto fault;
  953. }
  954. compute_return_epc(regs);
  955. regs->regs[insn.spec3_format.rt] = value;
  956. break;
  957. case lhue_op:
  958. if (!access_ok(VERIFY_READ, addr, 2)) {
  959. set_fs(seg);
  960. goto sigbus;
  961. }
  962. LoadHWUE(addr, value, res);
  963. if (res) {
  964. set_fs(seg);
  965. goto fault;
  966. }
  967. compute_return_epc(regs);
  968. regs->regs[insn.spec3_format.rt] = value;
  969. break;
  970. case she_op:
  971. if (!access_ok(VERIFY_WRITE, addr, 2)) {
  972. set_fs(seg);
  973. goto sigbus;
  974. }
  975. compute_return_epc(regs);
  976. value = regs->regs[insn.spec3_format.rt];
  977. StoreHWE(addr, value, res);
  978. if (res) {
  979. set_fs(seg);
  980. goto fault;
  981. }
  982. break;
  983. case swe_op:
  984. if (!access_ok(VERIFY_WRITE, addr, 4)) {
  985. set_fs(seg);
  986. goto sigbus;
  987. }
  988. compute_return_epc(regs);
  989. value = regs->regs[insn.spec3_format.rt];
  990. StoreWE(addr, value, res);
  991. if (res) {
  992. set_fs(seg);
  993. goto fault;
  994. }
  995. break;
  996. default:
  997. set_fs(seg);
  998. goto sigill;
  999. }
  1000. set_fs(seg);
  1001. }
  1002. #endif
  1003. break;
  1004. case lh_op:
  1005. if (!access_ok(VERIFY_READ, addr, 2))
  1006. goto sigbus;
  1007. if (IS_ENABLED(CONFIG_EVA)) {
  1008. if (uaccess_kernel())
  1009. LoadHW(addr, value, res);
  1010. else
  1011. LoadHWE(addr, value, res);
  1012. } else {
  1013. LoadHW(addr, value, res);
  1014. }
  1015. if (res)
  1016. goto fault;
  1017. compute_return_epc(regs);
  1018. regs->regs[insn.i_format.rt] = value;
  1019. break;
  1020. case lw_op:
  1021. if (!access_ok(VERIFY_READ, addr, 4))
  1022. goto sigbus;
  1023. if (IS_ENABLED(CONFIG_EVA)) {
  1024. if (uaccess_kernel())
  1025. LoadW(addr, value, res);
  1026. else
  1027. LoadWE(addr, value, res);
  1028. } else {
  1029. LoadW(addr, value, res);
  1030. }
  1031. if (res)
  1032. goto fault;
  1033. compute_return_epc(regs);
  1034. regs->regs[insn.i_format.rt] = value;
  1035. break;
  1036. case lhu_op:
  1037. if (!access_ok(VERIFY_READ, addr, 2))
  1038. goto sigbus;
  1039. if (IS_ENABLED(CONFIG_EVA)) {
  1040. if (uaccess_kernel())
  1041. LoadHWU(addr, value, res);
  1042. else
  1043. LoadHWUE(addr, value, res);
  1044. } else {
  1045. LoadHWU(addr, value, res);
  1046. }
  1047. if (res)
  1048. goto fault;
  1049. compute_return_epc(regs);
  1050. regs->regs[insn.i_format.rt] = value;
  1051. break;
  1052. case lwu_op:
  1053. #ifdef CONFIG_64BIT
  1054. /*
  1055. * A 32-bit kernel might be running on a 64-bit processor. But
  1056. * if we're on a 32-bit processor and an i-cache incoherency
  1057. * or race makes us see a 64-bit instruction here the sdl/sdr
  1058. * would blow up, so for now we don't handle unaligned 64-bit
  1059. * instructions on 32-bit kernels.
  1060. */
  1061. if (!access_ok(VERIFY_READ, addr, 4))
  1062. goto sigbus;
  1063. LoadWU(addr, value, res);
  1064. if (res)
  1065. goto fault;
  1066. compute_return_epc(regs);
  1067. regs->regs[insn.i_format.rt] = value;
  1068. break;
  1069. #endif /* CONFIG_64BIT */
  1070. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1071. goto sigill;
  1072. case ld_op:
  1073. #ifdef CONFIG_64BIT
  1074. /*
  1075. * A 32-bit kernel might be running on a 64-bit processor. But
  1076. * if we're on a 32-bit processor and an i-cache incoherency
  1077. * or race makes us see a 64-bit instruction here the sdl/sdr
  1078. * would blow up, so for now we don't handle unaligned 64-bit
  1079. * instructions on 32-bit kernels.
  1080. */
  1081. if (!access_ok(VERIFY_READ, addr, 8))
  1082. goto sigbus;
  1083. LoadDW(addr, value, res);
  1084. if (res)
  1085. goto fault;
  1086. compute_return_epc(regs);
  1087. regs->regs[insn.i_format.rt] = value;
  1088. break;
  1089. #endif /* CONFIG_64BIT */
  1090. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1091. goto sigill;
  1092. case sh_op:
  1093. if (!access_ok(VERIFY_WRITE, addr, 2))
  1094. goto sigbus;
  1095. compute_return_epc(regs);
  1096. value = regs->regs[insn.i_format.rt];
  1097. if (IS_ENABLED(CONFIG_EVA)) {
  1098. if (uaccess_kernel())
  1099. StoreHW(addr, value, res);
  1100. else
  1101. StoreHWE(addr, value, res);
  1102. } else {
  1103. StoreHW(addr, value, res);
  1104. }
  1105. if (res)
  1106. goto fault;
  1107. break;
  1108. case sw_op:
  1109. if (!access_ok(VERIFY_WRITE, addr, 4))
  1110. goto sigbus;
  1111. compute_return_epc(regs);
  1112. value = regs->regs[insn.i_format.rt];
  1113. if (IS_ENABLED(CONFIG_EVA)) {
  1114. if (uaccess_kernel())
  1115. StoreW(addr, value, res);
  1116. else
  1117. StoreWE(addr, value, res);
  1118. } else {
  1119. StoreW(addr, value, res);
  1120. }
  1121. if (res)
  1122. goto fault;
  1123. break;
  1124. case sd_op:
  1125. #ifdef CONFIG_64BIT
  1126. /*
  1127. * A 32-bit kernel might be running on a 64-bit processor. But
  1128. * if we're on a 32-bit processor and an i-cache incoherency
  1129. * or race makes us see a 64-bit instruction here the sdl/sdr
  1130. * would blow up, so for now we don't handle unaligned 64-bit
  1131. * instructions on 32-bit kernels.
  1132. */
  1133. if (!access_ok(VERIFY_WRITE, addr, 8))
  1134. goto sigbus;
  1135. compute_return_epc(regs);
  1136. value = regs->regs[insn.i_format.rt];
  1137. StoreDW(addr, value, res);
  1138. if (res)
  1139. goto fault;
  1140. break;
  1141. #endif /* CONFIG_64BIT */
  1142. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1143. goto sigill;
  1144. case lwc1_op:
  1145. case ldc1_op:
  1146. case swc1_op:
  1147. case sdc1_op:
  1148. case cop1x_op:
  1149. die_if_kernel("Unaligned FP access in kernel code", regs);
  1150. BUG_ON(!used_math());
  1151. lose_fpu(1); /* Save FPU state for the emulator. */
  1152. res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  1153. &fault_addr);
  1154. own_fpu(1); /* Restore FPU state. */
  1155. /* Signal if something went wrong. */
  1156. process_fpemu_return(res, fault_addr, 0);
  1157. if (res == 0)
  1158. break;
  1159. return;
  1160. case msa_op:
  1161. if (!cpu_has_msa)
  1162. goto sigill;
  1163. /*
  1164. * If we've reached this point then userland should have taken
  1165. * the MSA disabled exception & initialised vector context at
  1166. * some point in the past.
  1167. */
  1168. BUG_ON(!thread_msa_context_live());
  1169. df = insn.msa_mi10_format.df;
  1170. wd = insn.msa_mi10_format.wd;
  1171. fpr = &current->thread.fpu.fpr[wd];
  1172. switch (insn.msa_mi10_format.func) {
  1173. case msa_ld_op:
  1174. if (!access_ok(VERIFY_READ, addr, sizeof(*fpr)))
  1175. goto sigbus;
  1176. do {
  1177. /*
  1178. * If we have live MSA context keep track of
  1179. * whether we get preempted in order to avoid
  1180. * the register context we load being clobbered
  1181. * by the live context as it's saved during
  1182. * preemption. If we don't have live context
  1183. * then it can't be saved to clobber the value
  1184. * we load.
  1185. */
  1186. preempted = test_thread_flag(TIF_USEDMSA);
  1187. res = __copy_from_user_inatomic(fpr, addr,
  1188. sizeof(*fpr));
  1189. if (res)
  1190. goto fault;
  1191. /*
  1192. * Update the hardware register if it is in use
  1193. * by the task in this quantum, in order to
  1194. * avoid having to save & restore the whole
  1195. * vector context.
  1196. */
  1197. preempt_disable();
  1198. if (test_thread_flag(TIF_USEDMSA)) {
  1199. write_msa_wr(wd, fpr, df);
  1200. preempted = 0;
  1201. }
  1202. preempt_enable();
  1203. } while (preempted);
  1204. break;
  1205. case msa_st_op:
  1206. if (!access_ok(VERIFY_WRITE, addr, sizeof(*fpr)))
  1207. goto sigbus;
  1208. /*
  1209. * Update from the hardware register if it is in use by
  1210. * the task in this quantum, in order to avoid having to
  1211. * save & restore the whole vector context.
  1212. */
  1213. preempt_disable();
  1214. if (test_thread_flag(TIF_USEDMSA))
  1215. read_msa_wr(wd, fpr, df);
  1216. preempt_enable();
  1217. res = __copy_to_user_inatomic(addr, fpr, sizeof(*fpr));
  1218. if (res)
  1219. goto fault;
  1220. break;
  1221. default:
  1222. goto sigbus;
  1223. }
  1224. compute_return_epc(regs);
  1225. break;
  1226. #ifndef CONFIG_CPU_MIPSR6
  1227. /*
  1228. * COP2 is available to implementor for application specific use.
  1229. * It's up to applications to register a notifier chain and do
  1230. * whatever they have to do, including possible sending of signals.
  1231. *
  1232. * This instruction has been reallocated in Release 6
  1233. */
  1234. case lwc2_op:
  1235. cu2_notifier_call_chain(CU2_LWC2_OP, regs);
  1236. break;
  1237. case ldc2_op:
  1238. cu2_notifier_call_chain(CU2_LDC2_OP, regs);
  1239. break;
  1240. case swc2_op:
  1241. cu2_notifier_call_chain(CU2_SWC2_OP, regs);
  1242. break;
  1243. case sdc2_op:
  1244. cu2_notifier_call_chain(CU2_SDC2_OP, regs);
  1245. break;
  1246. #endif
  1247. default:
  1248. /*
  1249. * Pheeee... We encountered an yet unknown instruction or
  1250. * cache coherence problem. Die sucker, die ...
  1251. */
  1252. goto sigill;
  1253. }
  1254. #ifdef CONFIG_DEBUG_FS
  1255. unaligned_instructions++;
  1256. #endif
  1257. return;
  1258. fault:
  1259. /* roll back jump/branch */
  1260. regs->cp0_epc = origpc;
  1261. regs->regs[31] = orig31;
  1262. /* Did we have an exception handler installed? */
  1263. if (fixup_exception(regs))
  1264. return;
  1265. die_if_kernel("Unhandled kernel unaligned access", regs);
  1266. force_sig(SIGSEGV, current);
  1267. return;
  1268. sigbus:
  1269. die_if_kernel("Unhandled kernel unaligned access", regs);
  1270. force_sig(SIGBUS, current);
  1271. return;
  1272. sigill:
  1273. die_if_kernel
  1274. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1275. force_sig(SIGILL, current);
  1276. }
  1277. /* Recode table from 16-bit register notation to 32-bit GPR. */
  1278. const int reg16to32[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
  1279. /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
  1280. static const int reg16to32st[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
  1281. static void emulate_load_store_microMIPS(struct pt_regs *regs,
  1282. void __user *addr)
  1283. {
  1284. unsigned long value;
  1285. unsigned int res;
  1286. int i;
  1287. unsigned int reg = 0, rvar;
  1288. unsigned long orig31;
  1289. u16 __user *pc16;
  1290. u16 halfword;
  1291. unsigned int word;
  1292. unsigned long origpc, contpc;
  1293. union mips_instruction insn;
  1294. struct mm_decoded_insn mminsn;
  1295. void __user *fault_addr = NULL;
  1296. origpc = regs->cp0_epc;
  1297. orig31 = regs->regs[31];
  1298. mminsn.micro_mips_mode = 1;
  1299. /*
  1300. * This load never faults.
  1301. */
  1302. pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
  1303. __get_user(halfword, pc16);
  1304. pc16++;
  1305. contpc = regs->cp0_epc + 2;
  1306. word = ((unsigned int)halfword << 16);
  1307. mminsn.pc_inc = 2;
  1308. if (!mm_insn_16bit(halfword)) {
  1309. __get_user(halfword, pc16);
  1310. pc16++;
  1311. contpc = regs->cp0_epc + 4;
  1312. mminsn.pc_inc = 4;
  1313. word |= halfword;
  1314. }
  1315. mminsn.insn = word;
  1316. if (get_user(halfword, pc16))
  1317. goto fault;
  1318. mminsn.next_pc_inc = 2;
  1319. word = ((unsigned int)halfword << 16);
  1320. if (!mm_insn_16bit(halfword)) {
  1321. pc16++;
  1322. if (get_user(halfword, pc16))
  1323. goto fault;
  1324. mminsn.next_pc_inc = 4;
  1325. word |= halfword;
  1326. }
  1327. mminsn.next_insn = word;
  1328. insn = (union mips_instruction)(mminsn.insn);
  1329. if (mm_isBranchInstr(regs, mminsn, &contpc))
  1330. insn = (union mips_instruction)(mminsn.next_insn);
  1331. /* Parse instruction to find what to do */
  1332. switch (insn.mm_i_format.opcode) {
  1333. case mm_pool32a_op:
  1334. switch (insn.mm_x_format.func) {
  1335. case mm_lwxs_op:
  1336. reg = insn.mm_x_format.rd;
  1337. goto loadW;
  1338. }
  1339. goto sigbus;
  1340. case mm_pool32b_op:
  1341. switch (insn.mm_m_format.func) {
  1342. case mm_lwp_func:
  1343. reg = insn.mm_m_format.rd;
  1344. if (reg == 31)
  1345. goto sigbus;
  1346. if (!access_ok(VERIFY_READ, addr, 8))
  1347. goto sigbus;
  1348. LoadW(addr, value, res);
  1349. if (res)
  1350. goto fault;
  1351. regs->regs[reg] = value;
  1352. addr += 4;
  1353. LoadW(addr, value, res);
  1354. if (res)
  1355. goto fault;
  1356. regs->regs[reg + 1] = value;
  1357. goto success;
  1358. case mm_swp_func:
  1359. reg = insn.mm_m_format.rd;
  1360. if (reg == 31)
  1361. goto sigbus;
  1362. if (!access_ok(VERIFY_WRITE, addr, 8))
  1363. goto sigbus;
  1364. value = regs->regs[reg];
  1365. StoreW(addr, value, res);
  1366. if (res)
  1367. goto fault;
  1368. addr += 4;
  1369. value = regs->regs[reg + 1];
  1370. StoreW(addr, value, res);
  1371. if (res)
  1372. goto fault;
  1373. goto success;
  1374. case mm_ldp_func:
  1375. #ifdef CONFIG_64BIT
  1376. reg = insn.mm_m_format.rd;
  1377. if (reg == 31)
  1378. goto sigbus;
  1379. if (!access_ok(VERIFY_READ, addr, 16))
  1380. goto sigbus;
  1381. LoadDW(addr, value, res);
  1382. if (res)
  1383. goto fault;
  1384. regs->regs[reg] = value;
  1385. addr += 8;
  1386. LoadDW(addr, value, res);
  1387. if (res)
  1388. goto fault;
  1389. regs->regs[reg + 1] = value;
  1390. goto success;
  1391. #endif /* CONFIG_64BIT */
  1392. goto sigill;
  1393. case mm_sdp_func:
  1394. #ifdef CONFIG_64BIT
  1395. reg = insn.mm_m_format.rd;
  1396. if (reg == 31)
  1397. goto sigbus;
  1398. if (!access_ok(VERIFY_WRITE, addr, 16))
  1399. goto sigbus;
  1400. value = regs->regs[reg];
  1401. StoreDW(addr, value, res);
  1402. if (res)
  1403. goto fault;
  1404. addr += 8;
  1405. value = regs->regs[reg + 1];
  1406. StoreDW(addr, value, res);
  1407. if (res)
  1408. goto fault;
  1409. goto success;
  1410. #endif /* CONFIG_64BIT */
  1411. goto sigill;
  1412. case mm_lwm32_func:
  1413. reg = insn.mm_m_format.rd;
  1414. rvar = reg & 0xf;
  1415. if ((rvar > 9) || !reg)
  1416. goto sigill;
  1417. if (reg & 0x10) {
  1418. if (!access_ok
  1419. (VERIFY_READ, addr, 4 * (rvar + 1)))
  1420. goto sigbus;
  1421. } else {
  1422. if (!access_ok(VERIFY_READ, addr, 4 * rvar))
  1423. goto sigbus;
  1424. }
  1425. if (rvar == 9)
  1426. rvar = 8;
  1427. for (i = 16; rvar; rvar--, i++) {
  1428. LoadW(addr, value, res);
  1429. if (res)
  1430. goto fault;
  1431. addr += 4;
  1432. regs->regs[i] = value;
  1433. }
  1434. if ((reg & 0xf) == 9) {
  1435. LoadW(addr, value, res);
  1436. if (res)
  1437. goto fault;
  1438. addr += 4;
  1439. regs->regs[30] = value;
  1440. }
  1441. if (reg & 0x10) {
  1442. LoadW(addr, value, res);
  1443. if (res)
  1444. goto fault;
  1445. regs->regs[31] = value;
  1446. }
  1447. goto success;
  1448. case mm_swm32_func:
  1449. reg = insn.mm_m_format.rd;
  1450. rvar = reg & 0xf;
  1451. if ((rvar > 9) || !reg)
  1452. goto sigill;
  1453. if (reg & 0x10) {
  1454. if (!access_ok
  1455. (VERIFY_WRITE, addr, 4 * (rvar + 1)))
  1456. goto sigbus;
  1457. } else {
  1458. if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
  1459. goto sigbus;
  1460. }
  1461. if (rvar == 9)
  1462. rvar = 8;
  1463. for (i = 16; rvar; rvar--, i++) {
  1464. value = regs->regs[i];
  1465. StoreW(addr, value, res);
  1466. if (res)
  1467. goto fault;
  1468. addr += 4;
  1469. }
  1470. if ((reg & 0xf) == 9) {
  1471. value = regs->regs[30];
  1472. StoreW(addr, value, res);
  1473. if (res)
  1474. goto fault;
  1475. addr += 4;
  1476. }
  1477. if (reg & 0x10) {
  1478. value = regs->regs[31];
  1479. StoreW(addr, value, res);
  1480. if (res)
  1481. goto fault;
  1482. }
  1483. goto success;
  1484. case mm_ldm_func:
  1485. #ifdef CONFIG_64BIT
  1486. reg = insn.mm_m_format.rd;
  1487. rvar = reg & 0xf;
  1488. if ((rvar > 9) || !reg)
  1489. goto sigill;
  1490. if (reg & 0x10) {
  1491. if (!access_ok
  1492. (VERIFY_READ, addr, 8 * (rvar + 1)))
  1493. goto sigbus;
  1494. } else {
  1495. if (!access_ok(VERIFY_READ, addr, 8 * rvar))
  1496. goto sigbus;
  1497. }
  1498. if (rvar == 9)
  1499. rvar = 8;
  1500. for (i = 16; rvar; rvar--, i++) {
  1501. LoadDW(addr, value, res);
  1502. if (res)
  1503. goto fault;
  1504. addr += 4;
  1505. regs->regs[i] = value;
  1506. }
  1507. if ((reg & 0xf) == 9) {
  1508. LoadDW(addr, value, res);
  1509. if (res)
  1510. goto fault;
  1511. addr += 8;
  1512. regs->regs[30] = value;
  1513. }
  1514. if (reg & 0x10) {
  1515. LoadDW(addr, value, res);
  1516. if (res)
  1517. goto fault;
  1518. regs->regs[31] = value;
  1519. }
  1520. goto success;
  1521. #endif /* CONFIG_64BIT */
  1522. goto sigill;
  1523. case mm_sdm_func:
  1524. #ifdef CONFIG_64BIT
  1525. reg = insn.mm_m_format.rd;
  1526. rvar = reg & 0xf;
  1527. if ((rvar > 9) || !reg)
  1528. goto sigill;
  1529. if (reg & 0x10) {
  1530. if (!access_ok
  1531. (VERIFY_WRITE, addr, 8 * (rvar + 1)))
  1532. goto sigbus;
  1533. } else {
  1534. if (!access_ok(VERIFY_WRITE, addr, 8 * rvar))
  1535. goto sigbus;
  1536. }
  1537. if (rvar == 9)
  1538. rvar = 8;
  1539. for (i = 16; rvar; rvar--, i++) {
  1540. value = regs->regs[i];
  1541. StoreDW(addr, value, res);
  1542. if (res)
  1543. goto fault;
  1544. addr += 8;
  1545. }
  1546. if ((reg & 0xf) == 9) {
  1547. value = regs->regs[30];
  1548. StoreDW(addr, value, res);
  1549. if (res)
  1550. goto fault;
  1551. addr += 8;
  1552. }
  1553. if (reg & 0x10) {
  1554. value = regs->regs[31];
  1555. StoreDW(addr, value, res);
  1556. if (res)
  1557. goto fault;
  1558. }
  1559. goto success;
  1560. #endif /* CONFIG_64BIT */
  1561. goto sigill;
  1562. /* LWC2, SWC2, LDC2, SDC2 are not serviced */
  1563. }
  1564. goto sigbus;
  1565. case mm_pool32c_op:
  1566. switch (insn.mm_m_format.func) {
  1567. case mm_lwu_func:
  1568. reg = insn.mm_m_format.rd;
  1569. goto loadWU;
  1570. }
  1571. /* LL,SC,LLD,SCD are not serviced */
  1572. goto sigbus;
  1573. case mm_pool32f_op:
  1574. switch (insn.mm_x_format.func) {
  1575. case mm_lwxc1_func:
  1576. case mm_swxc1_func:
  1577. case mm_ldxc1_func:
  1578. case mm_sdxc1_func:
  1579. goto fpu_emul;
  1580. }
  1581. goto sigbus;
  1582. case mm_ldc132_op:
  1583. case mm_sdc132_op:
  1584. case mm_lwc132_op:
  1585. case mm_swc132_op:
  1586. fpu_emul:
  1587. /* roll back jump/branch */
  1588. regs->cp0_epc = origpc;
  1589. regs->regs[31] = orig31;
  1590. die_if_kernel("Unaligned FP access in kernel code", regs);
  1591. BUG_ON(!used_math());
  1592. BUG_ON(!is_fpu_owner());
  1593. lose_fpu(1); /* save the FPU state for the emulator */
  1594. res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  1595. &fault_addr);
  1596. own_fpu(1); /* restore FPU state */
  1597. /* If something went wrong, signal */
  1598. process_fpemu_return(res, fault_addr, 0);
  1599. if (res == 0)
  1600. goto success;
  1601. return;
  1602. case mm_lh32_op:
  1603. reg = insn.mm_i_format.rt;
  1604. goto loadHW;
  1605. case mm_lhu32_op:
  1606. reg = insn.mm_i_format.rt;
  1607. goto loadHWU;
  1608. case mm_lw32_op:
  1609. reg = insn.mm_i_format.rt;
  1610. goto loadW;
  1611. case mm_sh32_op:
  1612. reg = insn.mm_i_format.rt;
  1613. goto storeHW;
  1614. case mm_sw32_op:
  1615. reg = insn.mm_i_format.rt;
  1616. goto storeW;
  1617. case mm_ld32_op:
  1618. reg = insn.mm_i_format.rt;
  1619. goto loadDW;
  1620. case mm_sd32_op:
  1621. reg = insn.mm_i_format.rt;
  1622. goto storeDW;
  1623. case mm_pool16c_op:
  1624. switch (insn.mm16_m_format.func) {
  1625. case mm_lwm16_op:
  1626. reg = insn.mm16_m_format.rlist;
  1627. rvar = reg + 1;
  1628. if (!access_ok(VERIFY_READ, addr, 4 * rvar))
  1629. goto sigbus;
  1630. for (i = 16; rvar; rvar--, i++) {
  1631. LoadW(addr, value, res);
  1632. if (res)
  1633. goto fault;
  1634. addr += 4;
  1635. regs->regs[i] = value;
  1636. }
  1637. LoadW(addr, value, res);
  1638. if (res)
  1639. goto fault;
  1640. regs->regs[31] = value;
  1641. goto success;
  1642. case mm_swm16_op:
  1643. reg = insn.mm16_m_format.rlist;
  1644. rvar = reg + 1;
  1645. if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
  1646. goto sigbus;
  1647. for (i = 16; rvar; rvar--, i++) {
  1648. value = regs->regs[i];
  1649. StoreW(addr, value, res);
  1650. if (res)
  1651. goto fault;
  1652. addr += 4;
  1653. }
  1654. value = regs->regs[31];
  1655. StoreW(addr, value, res);
  1656. if (res)
  1657. goto fault;
  1658. goto success;
  1659. }
  1660. goto sigbus;
  1661. case mm_lhu16_op:
  1662. reg = reg16to32[insn.mm16_rb_format.rt];
  1663. goto loadHWU;
  1664. case mm_lw16_op:
  1665. reg = reg16to32[insn.mm16_rb_format.rt];
  1666. goto loadW;
  1667. case mm_sh16_op:
  1668. reg = reg16to32st[insn.mm16_rb_format.rt];
  1669. goto storeHW;
  1670. case mm_sw16_op:
  1671. reg = reg16to32st[insn.mm16_rb_format.rt];
  1672. goto storeW;
  1673. case mm_lwsp16_op:
  1674. reg = insn.mm16_r5_format.rt;
  1675. goto loadW;
  1676. case mm_swsp16_op:
  1677. reg = insn.mm16_r5_format.rt;
  1678. goto storeW;
  1679. case mm_lwgp16_op:
  1680. reg = reg16to32[insn.mm16_r3_format.rt];
  1681. goto loadW;
  1682. default:
  1683. goto sigill;
  1684. }
  1685. loadHW:
  1686. if (!access_ok(VERIFY_READ, addr, 2))
  1687. goto sigbus;
  1688. LoadHW(addr, value, res);
  1689. if (res)
  1690. goto fault;
  1691. regs->regs[reg] = value;
  1692. goto success;
  1693. loadHWU:
  1694. if (!access_ok(VERIFY_READ, addr, 2))
  1695. goto sigbus;
  1696. LoadHWU(addr, value, res);
  1697. if (res)
  1698. goto fault;
  1699. regs->regs[reg] = value;
  1700. goto success;
  1701. loadW:
  1702. if (!access_ok(VERIFY_READ, addr, 4))
  1703. goto sigbus;
  1704. LoadW(addr, value, res);
  1705. if (res)
  1706. goto fault;
  1707. regs->regs[reg] = value;
  1708. goto success;
  1709. loadWU:
  1710. #ifdef CONFIG_64BIT
  1711. /*
  1712. * A 32-bit kernel might be running on a 64-bit processor. But
  1713. * if we're on a 32-bit processor and an i-cache incoherency
  1714. * or race makes us see a 64-bit instruction here the sdl/sdr
  1715. * would blow up, so for now we don't handle unaligned 64-bit
  1716. * instructions on 32-bit kernels.
  1717. */
  1718. if (!access_ok(VERIFY_READ, addr, 4))
  1719. goto sigbus;
  1720. LoadWU(addr, value, res);
  1721. if (res)
  1722. goto fault;
  1723. regs->regs[reg] = value;
  1724. goto success;
  1725. #endif /* CONFIG_64BIT */
  1726. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1727. goto sigill;
  1728. loadDW:
  1729. #ifdef CONFIG_64BIT
  1730. /*
  1731. * A 32-bit kernel might be running on a 64-bit processor. But
  1732. * if we're on a 32-bit processor and an i-cache incoherency
  1733. * or race makes us see a 64-bit instruction here the sdl/sdr
  1734. * would blow up, so for now we don't handle unaligned 64-bit
  1735. * instructions on 32-bit kernels.
  1736. */
  1737. if (!access_ok(VERIFY_READ, addr, 8))
  1738. goto sigbus;
  1739. LoadDW(addr, value, res);
  1740. if (res)
  1741. goto fault;
  1742. regs->regs[reg] = value;
  1743. goto success;
  1744. #endif /* CONFIG_64BIT */
  1745. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1746. goto sigill;
  1747. storeHW:
  1748. if (!access_ok(VERIFY_WRITE, addr, 2))
  1749. goto sigbus;
  1750. value = regs->regs[reg];
  1751. StoreHW(addr, value, res);
  1752. if (res)
  1753. goto fault;
  1754. goto success;
  1755. storeW:
  1756. if (!access_ok(VERIFY_WRITE, addr, 4))
  1757. goto sigbus;
  1758. value = regs->regs[reg];
  1759. StoreW(addr, value, res);
  1760. if (res)
  1761. goto fault;
  1762. goto success;
  1763. storeDW:
  1764. #ifdef CONFIG_64BIT
  1765. /*
  1766. * A 32-bit kernel might be running on a 64-bit processor. But
  1767. * if we're on a 32-bit processor and an i-cache incoherency
  1768. * or race makes us see a 64-bit instruction here the sdl/sdr
  1769. * would blow up, so for now we don't handle unaligned 64-bit
  1770. * instructions on 32-bit kernels.
  1771. */
  1772. if (!access_ok(VERIFY_WRITE, addr, 8))
  1773. goto sigbus;
  1774. value = regs->regs[reg];
  1775. StoreDW(addr, value, res);
  1776. if (res)
  1777. goto fault;
  1778. goto success;
  1779. #endif /* CONFIG_64BIT */
  1780. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1781. goto sigill;
  1782. success:
  1783. regs->cp0_epc = contpc; /* advance or branch */
  1784. #ifdef CONFIG_DEBUG_FS
  1785. unaligned_instructions++;
  1786. #endif
  1787. return;
  1788. fault:
  1789. /* roll back jump/branch */
  1790. regs->cp0_epc = origpc;
  1791. regs->regs[31] = orig31;
  1792. /* Did we have an exception handler installed? */
  1793. if (fixup_exception(regs))
  1794. return;
  1795. die_if_kernel("Unhandled kernel unaligned access", regs);
  1796. force_sig(SIGSEGV, current);
  1797. return;
  1798. sigbus:
  1799. die_if_kernel("Unhandled kernel unaligned access", regs);
  1800. force_sig(SIGBUS, current);
  1801. return;
  1802. sigill:
  1803. die_if_kernel
  1804. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1805. force_sig(SIGILL, current);
  1806. }
  1807. static void emulate_load_store_MIPS16e(struct pt_regs *regs, void __user * addr)
  1808. {
  1809. unsigned long value;
  1810. unsigned int res;
  1811. int reg;
  1812. unsigned long orig31;
  1813. u16 __user *pc16;
  1814. unsigned long origpc;
  1815. union mips16e_instruction mips16inst, oldinst;
  1816. unsigned int opcode;
  1817. int extended = 0;
  1818. origpc = regs->cp0_epc;
  1819. orig31 = regs->regs[31];
  1820. pc16 = (unsigned short __user *)msk_isa16_mode(origpc);
  1821. /*
  1822. * This load never faults.
  1823. */
  1824. __get_user(mips16inst.full, pc16);
  1825. oldinst = mips16inst;
  1826. /* skip EXTEND instruction */
  1827. if (mips16inst.ri.opcode == MIPS16e_extend_op) {
  1828. extended = 1;
  1829. pc16++;
  1830. __get_user(mips16inst.full, pc16);
  1831. } else if (delay_slot(regs)) {
  1832. /* skip jump instructions */
  1833. /* JAL/JALX are 32 bits but have OPCODE in first short int */
  1834. if (mips16inst.ri.opcode == MIPS16e_jal_op)
  1835. pc16++;
  1836. pc16++;
  1837. if (get_user(mips16inst.full, pc16))
  1838. goto sigbus;
  1839. }
  1840. opcode = mips16inst.ri.opcode;
  1841. switch (opcode) {
  1842. case MIPS16e_i64_op: /* I64 or RI64 instruction */
  1843. switch (mips16inst.i64.func) { /* I64/RI64 func field check */
  1844. case MIPS16e_ldpc_func:
  1845. case MIPS16e_ldsp_func:
  1846. reg = reg16to32[mips16inst.ri64.ry];
  1847. goto loadDW;
  1848. case MIPS16e_sdsp_func:
  1849. reg = reg16to32[mips16inst.ri64.ry];
  1850. goto writeDW;
  1851. case MIPS16e_sdrasp_func:
  1852. reg = 29; /* GPRSP */
  1853. goto writeDW;
  1854. }
  1855. goto sigbus;
  1856. case MIPS16e_swsp_op:
  1857. reg = reg16to32[mips16inst.ri.rx];
  1858. if (extended && cpu_has_mips16e2)
  1859. switch (mips16inst.ri.imm >> 5) {
  1860. case 0: /* SWSP */
  1861. case 1: /* SWGP */
  1862. break;
  1863. case 2: /* SHGP */
  1864. opcode = MIPS16e_sh_op;
  1865. break;
  1866. default:
  1867. goto sigbus;
  1868. }
  1869. break;
  1870. case MIPS16e_lwpc_op:
  1871. reg = reg16to32[mips16inst.ri.rx];
  1872. break;
  1873. case MIPS16e_lwsp_op:
  1874. reg = reg16to32[mips16inst.ri.rx];
  1875. if (extended && cpu_has_mips16e2)
  1876. switch (mips16inst.ri.imm >> 5) {
  1877. case 0: /* LWSP */
  1878. case 1: /* LWGP */
  1879. break;
  1880. case 2: /* LHGP */
  1881. opcode = MIPS16e_lh_op;
  1882. break;
  1883. case 4: /* LHUGP */
  1884. opcode = MIPS16e_lhu_op;
  1885. break;
  1886. default:
  1887. goto sigbus;
  1888. }
  1889. break;
  1890. case MIPS16e_i8_op:
  1891. if (mips16inst.i8.func != MIPS16e_swrasp_func)
  1892. goto sigbus;
  1893. reg = 29; /* GPRSP */
  1894. break;
  1895. default:
  1896. reg = reg16to32[mips16inst.rri.ry];
  1897. break;
  1898. }
  1899. switch (opcode) {
  1900. case MIPS16e_lb_op:
  1901. case MIPS16e_lbu_op:
  1902. case MIPS16e_sb_op:
  1903. goto sigbus;
  1904. case MIPS16e_lh_op:
  1905. if (!access_ok(VERIFY_READ, addr, 2))
  1906. goto sigbus;
  1907. LoadHW(addr, value, res);
  1908. if (res)
  1909. goto fault;
  1910. MIPS16e_compute_return_epc(regs, &oldinst);
  1911. regs->regs[reg] = value;
  1912. break;
  1913. case MIPS16e_lhu_op:
  1914. if (!access_ok(VERIFY_READ, addr, 2))
  1915. goto sigbus;
  1916. LoadHWU(addr, value, res);
  1917. if (res)
  1918. goto fault;
  1919. MIPS16e_compute_return_epc(regs, &oldinst);
  1920. regs->regs[reg] = value;
  1921. break;
  1922. case MIPS16e_lw_op:
  1923. case MIPS16e_lwpc_op:
  1924. case MIPS16e_lwsp_op:
  1925. if (!access_ok(VERIFY_READ, addr, 4))
  1926. goto sigbus;
  1927. LoadW(addr, value, res);
  1928. if (res)
  1929. goto fault;
  1930. MIPS16e_compute_return_epc(regs, &oldinst);
  1931. regs->regs[reg] = value;
  1932. break;
  1933. case MIPS16e_lwu_op:
  1934. #ifdef CONFIG_64BIT
  1935. /*
  1936. * A 32-bit kernel might be running on a 64-bit processor. But
  1937. * if we're on a 32-bit processor and an i-cache incoherency
  1938. * or race makes us see a 64-bit instruction here the sdl/sdr
  1939. * would blow up, so for now we don't handle unaligned 64-bit
  1940. * instructions on 32-bit kernels.
  1941. */
  1942. if (!access_ok(VERIFY_READ, addr, 4))
  1943. goto sigbus;
  1944. LoadWU(addr, value, res);
  1945. if (res)
  1946. goto fault;
  1947. MIPS16e_compute_return_epc(regs, &oldinst);
  1948. regs->regs[reg] = value;
  1949. break;
  1950. #endif /* CONFIG_64BIT */
  1951. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1952. goto sigill;
  1953. case MIPS16e_ld_op:
  1954. loadDW:
  1955. #ifdef CONFIG_64BIT
  1956. /*
  1957. * A 32-bit kernel might be running on a 64-bit processor. But
  1958. * if we're on a 32-bit processor and an i-cache incoherency
  1959. * or race makes us see a 64-bit instruction here the sdl/sdr
  1960. * would blow up, so for now we don't handle unaligned 64-bit
  1961. * instructions on 32-bit kernels.
  1962. */
  1963. if (!access_ok(VERIFY_READ, addr, 8))
  1964. goto sigbus;
  1965. LoadDW(addr, value, res);
  1966. if (res)
  1967. goto fault;
  1968. MIPS16e_compute_return_epc(regs, &oldinst);
  1969. regs->regs[reg] = value;
  1970. break;
  1971. #endif /* CONFIG_64BIT */
  1972. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1973. goto sigill;
  1974. case MIPS16e_sh_op:
  1975. if (!access_ok(VERIFY_WRITE, addr, 2))
  1976. goto sigbus;
  1977. MIPS16e_compute_return_epc(regs, &oldinst);
  1978. value = regs->regs[reg];
  1979. StoreHW(addr, value, res);
  1980. if (res)
  1981. goto fault;
  1982. break;
  1983. case MIPS16e_sw_op:
  1984. case MIPS16e_swsp_op:
  1985. case MIPS16e_i8_op: /* actually - MIPS16e_swrasp_func */
  1986. if (!access_ok(VERIFY_WRITE, addr, 4))
  1987. goto sigbus;
  1988. MIPS16e_compute_return_epc(regs, &oldinst);
  1989. value = regs->regs[reg];
  1990. StoreW(addr, value, res);
  1991. if (res)
  1992. goto fault;
  1993. break;
  1994. case MIPS16e_sd_op:
  1995. writeDW:
  1996. #ifdef CONFIG_64BIT
  1997. /*
  1998. * A 32-bit kernel might be running on a 64-bit processor. But
  1999. * if we're on a 32-bit processor and an i-cache incoherency
  2000. * or race makes us see a 64-bit instruction here the sdl/sdr
  2001. * would blow up, so for now we don't handle unaligned 64-bit
  2002. * instructions on 32-bit kernels.
  2003. */
  2004. if (!access_ok(VERIFY_WRITE, addr, 8))
  2005. goto sigbus;
  2006. MIPS16e_compute_return_epc(regs, &oldinst);
  2007. value = regs->regs[reg];
  2008. StoreDW(addr, value, res);
  2009. if (res)
  2010. goto fault;
  2011. break;
  2012. #endif /* CONFIG_64BIT */
  2013. /* Cannot handle 64-bit instructions in 32-bit kernel */
  2014. goto sigill;
  2015. default:
  2016. /*
  2017. * Pheeee... We encountered an yet unknown instruction or
  2018. * cache coherence problem. Die sucker, die ...
  2019. */
  2020. goto sigill;
  2021. }
  2022. #ifdef CONFIG_DEBUG_FS
  2023. unaligned_instructions++;
  2024. #endif
  2025. return;
  2026. fault:
  2027. /* roll back jump/branch */
  2028. regs->cp0_epc = origpc;
  2029. regs->regs[31] = orig31;
  2030. /* Did we have an exception handler installed? */
  2031. if (fixup_exception(regs))
  2032. return;
  2033. die_if_kernel("Unhandled kernel unaligned access", regs);
  2034. force_sig(SIGSEGV, current);
  2035. return;
  2036. sigbus:
  2037. die_if_kernel("Unhandled kernel unaligned access", regs);
  2038. force_sig(SIGBUS, current);
  2039. return;
  2040. sigill:
  2041. die_if_kernel
  2042. ("Unhandled kernel unaligned access or invalid instruction", regs);
  2043. force_sig(SIGILL, current);
  2044. }
  2045. asmlinkage void do_ade(struct pt_regs *regs)
  2046. {
  2047. enum ctx_state prev_state;
  2048. unsigned int __user *pc;
  2049. mm_segment_t seg;
  2050. prev_state = exception_enter();
  2051. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
  2052. 1, regs, regs->cp0_badvaddr);
  2053. /*
  2054. * Did we catch a fault trying to load an instruction?
  2055. */
  2056. if (regs->cp0_badvaddr == regs->cp0_epc)
  2057. goto sigbus;
  2058. if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
  2059. goto sigbus;
  2060. if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
  2061. goto sigbus;
  2062. /*
  2063. * Do branch emulation only if we didn't forward the exception.
  2064. * This is all so but ugly ...
  2065. */
  2066. /*
  2067. * Are we running in microMIPS mode?
  2068. */
  2069. if (get_isa16_mode(regs->cp0_epc)) {
  2070. /*
  2071. * Did we catch a fault trying to load an instruction in
  2072. * 16-bit mode?
  2073. */
  2074. if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
  2075. goto sigbus;
  2076. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  2077. show_registers(regs);
  2078. if (cpu_has_mmips) {
  2079. seg = get_fs();
  2080. if (!user_mode(regs))
  2081. set_fs(KERNEL_DS);
  2082. emulate_load_store_microMIPS(regs,
  2083. (void __user *)regs->cp0_badvaddr);
  2084. set_fs(seg);
  2085. return;
  2086. }
  2087. if (cpu_has_mips16) {
  2088. seg = get_fs();
  2089. if (!user_mode(regs))
  2090. set_fs(KERNEL_DS);
  2091. emulate_load_store_MIPS16e(regs,
  2092. (void __user *)regs->cp0_badvaddr);
  2093. set_fs(seg);
  2094. return;
  2095. }
  2096. goto sigbus;
  2097. }
  2098. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  2099. show_registers(regs);
  2100. pc = (unsigned int __user *)exception_epc(regs);
  2101. seg = get_fs();
  2102. if (!user_mode(regs))
  2103. set_fs(KERNEL_DS);
  2104. emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
  2105. set_fs(seg);
  2106. return;
  2107. sigbus:
  2108. die_if_kernel("Kernel unaligned instruction access", regs);
  2109. force_sig(SIGBUS, current);
  2110. /*
  2111. * XXX On return from the signal handler we should advance the epc
  2112. */
  2113. exception_exit(prev_state);
  2114. }
  2115. #ifdef CONFIG_DEBUG_FS
  2116. static int __init debugfs_unaligned(void)
  2117. {
  2118. struct dentry *d;
  2119. if (!mips_debugfs_dir)
  2120. return -ENODEV;
  2121. d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
  2122. mips_debugfs_dir, &unaligned_instructions);
  2123. if (!d)
  2124. return -ENOMEM;
  2125. d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
  2126. mips_debugfs_dir, &unaligned_action);
  2127. if (!d)
  2128. return -ENOMEM;
  2129. return 0;
  2130. }
  2131. arch_initcall(debugfs_unaligned);
  2132. #endif