unaligned.c 57 KB

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