unaligned.c 61 KB

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