unaligned.c 60 KB

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