unaligned.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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) \
  109. __asm__ __volatile__ (".set\tnoat\n" \
  110. "1:\t"user_lb("%0", "0(%2)")"\n" \
  111. "2:\t"user_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. #define LoadW(addr, value, res) \
  128. __asm__ __volatile__ ( \
  129. "1:\t"user_lwl("%0", "(%2)")"\n" \
  130. "2:\t"user_lwr("%0", "3(%2)")"\n\t" \
  131. "li\t%1, 0\n" \
  132. "3:\n\t" \
  133. ".insn\n\t" \
  134. ".section\t.fixup,\"ax\"\n\t" \
  135. "4:\tli\t%1, %3\n\t" \
  136. "j\t3b\n\t" \
  137. ".previous\n\t" \
  138. ".section\t__ex_table,\"a\"\n\t" \
  139. STR(PTR)"\t1b, 4b\n\t" \
  140. STR(PTR)"\t2b, 4b\n\t" \
  141. ".previous" \
  142. : "=&r" (value), "=r" (res) \
  143. : "r" (addr), "i" (-EFAULT));
  144. #define LoadHWU(addr, value, res) \
  145. __asm__ __volatile__ ( \
  146. ".set\tnoat\n" \
  147. "1:\t"user_lbu("%0", "0(%2)")"\n" \
  148. "2:\t"user_lbu("$1", "1(%2)")"\n\t" \
  149. "sll\t%0, 0x8\n\t" \
  150. "or\t%0, $1\n\t" \
  151. "li\t%1, 0\n" \
  152. "3:\n\t" \
  153. ".insn\n\t" \
  154. ".set\tat\n\t" \
  155. ".section\t.fixup,\"ax\"\n\t" \
  156. "4:\tli\t%1, %3\n\t" \
  157. "j\t3b\n\t" \
  158. ".previous\n\t" \
  159. ".section\t__ex_table,\"a\"\n\t" \
  160. STR(PTR)"\t1b, 4b\n\t" \
  161. STR(PTR)"\t2b, 4b\n\t" \
  162. ".previous" \
  163. : "=&r" (value), "=r" (res) \
  164. : "r" (addr), "i" (-EFAULT));
  165. #define LoadWU(addr, value, res) \
  166. __asm__ __volatile__ ( \
  167. "1:\t"user_lwl("%0", "(%2)")"\n" \
  168. "2:\t"user_lwr("%0", "3(%2)")"\n\t" \
  169. "dsll\t%0, %0, 32\n\t" \
  170. "dsrl\t%0, %0, 32\n\t" \
  171. "li\t%1, 0\n" \
  172. "3:\n\t" \
  173. ".insn\n\t" \
  174. "\t.section\t.fixup,\"ax\"\n\t" \
  175. "4:\tli\t%1, %3\n\t" \
  176. "j\t3b\n\t" \
  177. ".previous\n\t" \
  178. ".section\t__ex_table,\"a\"\n\t" \
  179. STR(PTR)"\t1b, 4b\n\t" \
  180. STR(PTR)"\t2b, 4b\n\t" \
  181. ".previous" \
  182. : "=&r" (value), "=r" (res) \
  183. : "r" (addr), "i" (-EFAULT));
  184. #define LoadDW(addr, value, res) \
  185. __asm__ __volatile__ ( \
  186. "1:\tldl\t%0, (%2)\n" \
  187. "2:\tldr\t%0, 7(%2)\n\t" \
  188. "li\t%1, 0\n" \
  189. "3:\n\t" \
  190. ".insn\n\t" \
  191. "\t.section\t.fixup,\"ax\"\n\t" \
  192. "4:\tli\t%1, %3\n\t" \
  193. "j\t3b\n\t" \
  194. ".previous\n\t" \
  195. ".section\t__ex_table,\"a\"\n\t" \
  196. STR(PTR)"\t1b, 4b\n\t" \
  197. STR(PTR)"\t2b, 4b\n\t" \
  198. ".previous" \
  199. : "=&r" (value), "=r" (res) \
  200. : "r" (addr), "i" (-EFAULT));
  201. #define StoreHW(addr, value, res) \
  202. __asm__ __volatile__ ( \
  203. ".set\tnoat\n" \
  204. "1:\t"user_sb("%1", "1(%2)")"\n" \
  205. "srl\t$1, %1, 0x8\n" \
  206. "2:\t"user_sb("$1", "0(%2)")"\n" \
  207. ".set\tat\n\t" \
  208. "li\t%0, 0\n" \
  209. "3:\n\t" \
  210. ".insn\n\t" \
  211. ".section\t.fixup,\"ax\"\n\t" \
  212. "4:\tli\t%0, %3\n\t" \
  213. "j\t3b\n\t" \
  214. ".previous\n\t" \
  215. ".section\t__ex_table,\"a\"\n\t" \
  216. STR(PTR)"\t1b, 4b\n\t" \
  217. STR(PTR)"\t2b, 4b\n\t" \
  218. ".previous" \
  219. : "=r" (res) \
  220. : "r" (value), "r" (addr), "i" (-EFAULT));
  221. #define StoreW(addr, value, res) \
  222. __asm__ __volatile__ ( \
  223. "1:\t"user_swl("%1", "(%2)")"\n" \
  224. "2:\t"user_swr("%1", "3(%2)")"\n\t" \
  225. "li\t%0, 0\n" \
  226. "3:\n\t" \
  227. ".insn\n\t" \
  228. ".section\t.fixup,\"ax\"\n\t" \
  229. "4:\tli\t%0, %3\n\t" \
  230. "j\t3b\n\t" \
  231. ".previous\n\t" \
  232. ".section\t__ex_table,\"a\"\n\t" \
  233. STR(PTR)"\t1b, 4b\n\t" \
  234. STR(PTR)"\t2b, 4b\n\t" \
  235. ".previous" \
  236. : "=r" (res) \
  237. : "r" (value), "r" (addr), "i" (-EFAULT));
  238. #define StoreDW(addr, value, res) \
  239. __asm__ __volatile__ ( \
  240. "1:\tsdl\t%1,(%2)\n" \
  241. "2:\tsdr\t%1, 7(%2)\n\t" \
  242. "li\t%0, 0\n" \
  243. "3:\n\t" \
  244. ".insn\n\t" \
  245. ".section\t.fixup,\"ax\"\n\t" \
  246. "4:\tli\t%0, %3\n\t" \
  247. "j\t3b\n\t" \
  248. ".previous\n\t" \
  249. ".section\t__ex_table,\"a\"\n\t" \
  250. STR(PTR)"\t1b, 4b\n\t" \
  251. STR(PTR)"\t2b, 4b\n\t" \
  252. ".previous" \
  253. : "=r" (res) \
  254. : "r" (value), "r" (addr), "i" (-EFAULT));
  255. #endif
  256. #ifdef __LITTLE_ENDIAN
  257. #define LoadHW(addr, value, res) \
  258. __asm__ __volatile__ (".set\tnoat\n" \
  259. "1:\t"user_lb("%0", "1(%2)")"\n" \
  260. "2:\t"user_lbu("$1", "0(%2)")"\n\t" \
  261. "sll\t%0, 0x8\n\t" \
  262. "or\t%0, $1\n\t" \
  263. "li\t%1, 0\n" \
  264. "3:\t.set\tat\n\t" \
  265. ".insn\n\t" \
  266. ".section\t.fixup,\"ax\"\n\t" \
  267. "4:\tli\t%1, %3\n\t" \
  268. "j\t3b\n\t" \
  269. ".previous\n\t" \
  270. ".section\t__ex_table,\"a\"\n\t" \
  271. STR(PTR)"\t1b, 4b\n\t" \
  272. STR(PTR)"\t2b, 4b\n\t" \
  273. ".previous" \
  274. : "=&r" (value), "=r" (res) \
  275. : "r" (addr), "i" (-EFAULT));
  276. #define LoadW(addr, value, res) \
  277. __asm__ __volatile__ ( \
  278. "1:\t"user_lwl("%0", "3(%2)")"\n" \
  279. "2:\t"user_lwr("%0", "(%2)")"\n\t" \
  280. "li\t%1, 0\n" \
  281. "3:\n\t" \
  282. ".insn\n\t" \
  283. ".section\t.fixup,\"ax\"\n\t" \
  284. "4:\tli\t%1, %3\n\t" \
  285. "j\t3b\n\t" \
  286. ".previous\n\t" \
  287. ".section\t__ex_table,\"a\"\n\t" \
  288. STR(PTR)"\t1b, 4b\n\t" \
  289. STR(PTR)"\t2b, 4b\n\t" \
  290. ".previous" \
  291. : "=&r" (value), "=r" (res) \
  292. : "r" (addr), "i" (-EFAULT));
  293. #define LoadHWU(addr, value, res) \
  294. __asm__ __volatile__ ( \
  295. ".set\tnoat\n" \
  296. "1:\t"user_lbu("%0", "1(%2)")"\n" \
  297. "2:\t"user_lbu("$1", "0(%2)")"\n\t" \
  298. "sll\t%0, 0x8\n\t" \
  299. "or\t%0, $1\n\t" \
  300. "li\t%1, 0\n" \
  301. "3:\n\t" \
  302. ".insn\n\t" \
  303. ".set\tat\n\t" \
  304. ".section\t.fixup,\"ax\"\n\t" \
  305. "4:\tli\t%1, %3\n\t" \
  306. "j\t3b\n\t" \
  307. ".previous\n\t" \
  308. ".section\t__ex_table,\"a\"\n\t" \
  309. STR(PTR)"\t1b, 4b\n\t" \
  310. STR(PTR)"\t2b, 4b\n\t" \
  311. ".previous" \
  312. : "=&r" (value), "=r" (res) \
  313. : "r" (addr), "i" (-EFAULT));
  314. #define LoadWU(addr, value, res) \
  315. __asm__ __volatile__ ( \
  316. "1:\t"user_lwl("%0", "3(%2)")"\n" \
  317. "2:\t"user_lwr("%0", "(%2)")"\n\t" \
  318. "dsll\t%0, %0, 32\n\t" \
  319. "dsrl\t%0, %0, 32\n\t" \
  320. "li\t%1, 0\n" \
  321. "3:\n\t" \
  322. ".insn\n\t" \
  323. "\t.section\t.fixup,\"ax\"\n\t" \
  324. "4:\tli\t%1, %3\n\t" \
  325. "j\t3b\n\t" \
  326. ".previous\n\t" \
  327. ".section\t__ex_table,\"a\"\n\t" \
  328. STR(PTR)"\t1b, 4b\n\t" \
  329. STR(PTR)"\t2b, 4b\n\t" \
  330. ".previous" \
  331. : "=&r" (value), "=r" (res) \
  332. : "r" (addr), "i" (-EFAULT));
  333. #define LoadDW(addr, value, res) \
  334. __asm__ __volatile__ ( \
  335. "1:\tldl\t%0, 7(%2)\n" \
  336. "2:\tldr\t%0, (%2)\n\t" \
  337. "li\t%1, 0\n" \
  338. "3:\n\t" \
  339. ".insn\n\t" \
  340. "\t.section\t.fixup,\"ax\"\n\t" \
  341. "4:\tli\t%1, %3\n\t" \
  342. "j\t3b\n\t" \
  343. ".previous\n\t" \
  344. ".section\t__ex_table,\"a\"\n\t" \
  345. STR(PTR)"\t1b, 4b\n\t" \
  346. STR(PTR)"\t2b, 4b\n\t" \
  347. ".previous" \
  348. : "=&r" (value), "=r" (res) \
  349. : "r" (addr), "i" (-EFAULT));
  350. #define StoreHW(addr, value, res) \
  351. __asm__ __volatile__ ( \
  352. ".set\tnoat\n" \
  353. "1:\t"user_sb("%1", "0(%2)")"\n" \
  354. "srl\t$1,%1, 0x8\n" \
  355. "2:\t"user_sb("$1", "1(%2)")"\n" \
  356. ".set\tat\n\t" \
  357. "li\t%0, 0\n" \
  358. "3:\n\t" \
  359. ".insn\n\t" \
  360. ".section\t.fixup,\"ax\"\n\t" \
  361. "4:\tli\t%0, %3\n\t" \
  362. "j\t3b\n\t" \
  363. ".previous\n\t" \
  364. ".section\t__ex_table,\"a\"\n\t" \
  365. STR(PTR)"\t1b, 4b\n\t" \
  366. STR(PTR)"\t2b, 4b\n\t" \
  367. ".previous" \
  368. : "=r" (res) \
  369. : "r" (value), "r" (addr), "i" (-EFAULT));
  370. #define StoreW(addr, value, res) \
  371. __asm__ __volatile__ ( \
  372. "1:\t"user_swl("%1", "3(%2)")"\n" \
  373. "2:\t"user_swr("%1", "(%2)")"\n\t" \
  374. "li\t%0, 0\n" \
  375. "3:\n\t" \
  376. ".insn\n\t" \
  377. ".section\t.fixup,\"ax\"\n\t" \
  378. "4:\tli\t%0, %3\n\t" \
  379. "j\t3b\n\t" \
  380. ".previous\n\t" \
  381. ".section\t__ex_table,\"a\"\n\t" \
  382. STR(PTR)"\t1b, 4b\n\t" \
  383. STR(PTR)"\t2b, 4b\n\t" \
  384. ".previous" \
  385. : "=r" (res) \
  386. : "r" (value), "r" (addr), "i" (-EFAULT));
  387. #define StoreDW(addr, value, res) \
  388. __asm__ __volatile__ ( \
  389. "1:\tsdl\t%1, 7(%2)\n" \
  390. "2:\tsdr\t%1, (%2)\n\t" \
  391. "li\t%0, 0\n" \
  392. "3:\n\t" \
  393. ".insn\n\t" \
  394. ".section\t.fixup,\"ax\"\n\t" \
  395. "4:\tli\t%0, %3\n\t" \
  396. "j\t3b\n\t" \
  397. ".previous\n\t" \
  398. ".section\t__ex_table,\"a\"\n\t" \
  399. STR(PTR)"\t1b, 4b\n\t" \
  400. STR(PTR)"\t2b, 4b\n\t" \
  401. ".previous" \
  402. : "=r" (res) \
  403. : "r" (value), "r" (addr), "i" (-EFAULT));
  404. #endif
  405. static void emulate_load_store_insn(struct pt_regs *regs,
  406. void __user *addr, unsigned int __user *pc)
  407. {
  408. union mips_instruction insn;
  409. unsigned long value;
  410. unsigned int res;
  411. unsigned long origpc;
  412. unsigned long orig31;
  413. void __user *fault_addr = NULL;
  414. #ifdef CONFIG_EVA
  415. mm_segment_t seg;
  416. #endif
  417. origpc = (unsigned long)pc;
  418. orig31 = regs->regs[31];
  419. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
  420. /*
  421. * This load never faults.
  422. */
  423. __get_user(insn.word, pc);
  424. switch (insn.i_format.opcode) {
  425. /*
  426. * These are instructions that a compiler doesn't generate. We
  427. * can assume therefore that the code is MIPS-aware and
  428. * really buggy. Emulating these instructions would break the
  429. * semantics anyway.
  430. */
  431. case ll_op:
  432. case lld_op:
  433. case sc_op:
  434. case scd_op:
  435. /*
  436. * For these instructions the only way to create an address
  437. * error is an attempted access to kernel/supervisor address
  438. * space.
  439. */
  440. case ldl_op:
  441. case ldr_op:
  442. case lwl_op:
  443. case lwr_op:
  444. case sdl_op:
  445. case sdr_op:
  446. case swl_op:
  447. case swr_op:
  448. case lb_op:
  449. case lbu_op:
  450. case sb_op:
  451. goto sigbus;
  452. /*
  453. * The remaining opcodes are the ones that are really of
  454. * interest.
  455. */
  456. #ifdef CONFIG_EVA
  457. case spec3_op:
  458. /*
  459. * we can land here only from kernel accessing user memory,
  460. * so we need to "switch" the address limit to user space, so
  461. * address check can work properly.
  462. */
  463. seg = get_fs();
  464. set_fs(USER_DS);
  465. switch (insn.spec3_format.func) {
  466. case lhe_op:
  467. if (!access_ok(VERIFY_READ, addr, 2)) {
  468. set_fs(seg);
  469. goto sigbus;
  470. }
  471. LoadHW(addr, value, res);
  472. if (res) {
  473. set_fs(seg);
  474. goto fault;
  475. }
  476. compute_return_epc(regs);
  477. regs->regs[insn.spec3_format.rt] = value;
  478. break;
  479. case lwe_op:
  480. if (!access_ok(VERIFY_READ, addr, 4)) {
  481. set_fs(seg);
  482. goto sigbus;
  483. }
  484. LoadW(addr, value, res);
  485. if (res) {
  486. set_fs(seg);
  487. goto fault;
  488. }
  489. compute_return_epc(regs);
  490. regs->regs[insn.spec3_format.rt] = value;
  491. break;
  492. case lhue_op:
  493. if (!access_ok(VERIFY_READ, addr, 2)) {
  494. set_fs(seg);
  495. goto sigbus;
  496. }
  497. LoadHWU(addr, value, res);
  498. if (res) {
  499. set_fs(seg);
  500. goto fault;
  501. }
  502. compute_return_epc(regs);
  503. regs->regs[insn.spec3_format.rt] = value;
  504. break;
  505. case she_op:
  506. if (!access_ok(VERIFY_WRITE, addr, 2)) {
  507. set_fs(seg);
  508. goto sigbus;
  509. }
  510. compute_return_epc(regs);
  511. value = regs->regs[insn.spec3_format.rt];
  512. StoreHW(addr, value, res);
  513. if (res) {
  514. set_fs(seg);
  515. goto fault;
  516. }
  517. break;
  518. case swe_op:
  519. if (!access_ok(VERIFY_WRITE, addr, 4)) {
  520. set_fs(seg);
  521. goto sigbus;
  522. }
  523. compute_return_epc(regs);
  524. value = regs->regs[insn.spec3_format.rt];
  525. StoreW(addr, value, res);
  526. if (res) {
  527. set_fs(seg);
  528. goto fault;
  529. }
  530. break;
  531. default:
  532. set_fs(seg);
  533. goto sigill;
  534. }
  535. set_fs(seg);
  536. break;
  537. #endif
  538. case lh_op:
  539. if (!access_ok(VERIFY_READ, addr, 2))
  540. goto sigbus;
  541. LoadHW(addr, value, res);
  542. if (res)
  543. goto fault;
  544. compute_return_epc(regs);
  545. regs->regs[insn.i_format.rt] = value;
  546. break;
  547. case lw_op:
  548. if (!access_ok(VERIFY_READ, addr, 4))
  549. goto sigbus;
  550. LoadW(addr, value, res);
  551. if (res)
  552. goto fault;
  553. compute_return_epc(regs);
  554. regs->regs[insn.i_format.rt] = value;
  555. break;
  556. case lhu_op:
  557. if (!access_ok(VERIFY_READ, addr, 2))
  558. goto sigbus;
  559. LoadHWU(addr, value, res);
  560. if (res)
  561. goto fault;
  562. compute_return_epc(regs);
  563. regs->regs[insn.i_format.rt] = value;
  564. break;
  565. case lwu_op:
  566. #ifdef CONFIG_64BIT
  567. /*
  568. * A 32-bit kernel might be running on a 64-bit processor. But
  569. * if we're on a 32-bit processor and an i-cache incoherency
  570. * or race makes us see a 64-bit instruction here the sdl/sdr
  571. * would blow up, so for now we don't handle unaligned 64-bit
  572. * instructions on 32-bit kernels.
  573. */
  574. if (!access_ok(VERIFY_READ, addr, 4))
  575. goto sigbus;
  576. LoadWU(addr, value, res);
  577. if (res)
  578. goto fault;
  579. compute_return_epc(regs);
  580. regs->regs[insn.i_format.rt] = value;
  581. break;
  582. #endif /* CONFIG_64BIT */
  583. /* Cannot handle 64-bit instructions in 32-bit kernel */
  584. goto sigill;
  585. case ld_op:
  586. #ifdef CONFIG_64BIT
  587. /*
  588. * A 32-bit kernel might be running on a 64-bit processor. But
  589. * if we're on a 32-bit processor and an i-cache incoherency
  590. * or race makes us see a 64-bit instruction here the sdl/sdr
  591. * would blow up, so for now we don't handle unaligned 64-bit
  592. * instructions on 32-bit kernels.
  593. */
  594. if (!access_ok(VERIFY_READ, addr, 8))
  595. goto sigbus;
  596. LoadDW(addr, value, res);
  597. if (res)
  598. goto fault;
  599. compute_return_epc(regs);
  600. regs->regs[insn.i_format.rt] = value;
  601. break;
  602. #endif /* CONFIG_64BIT */
  603. /* Cannot handle 64-bit instructions in 32-bit kernel */
  604. goto sigill;
  605. case sh_op:
  606. if (!access_ok(VERIFY_WRITE, addr, 2))
  607. goto sigbus;
  608. compute_return_epc(regs);
  609. value = regs->regs[insn.i_format.rt];
  610. StoreHW(addr, value, res);
  611. if (res)
  612. goto fault;
  613. break;
  614. case sw_op:
  615. if (!access_ok(VERIFY_WRITE, addr, 4))
  616. goto sigbus;
  617. compute_return_epc(regs);
  618. value = regs->regs[insn.i_format.rt];
  619. StoreW(addr, value, res);
  620. if (res)
  621. goto fault;
  622. break;
  623. case sd_op:
  624. #ifdef CONFIG_64BIT
  625. /*
  626. * A 32-bit kernel might be running on a 64-bit processor. But
  627. * if we're on a 32-bit processor and an i-cache incoherency
  628. * or race makes us see a 64-bit instruction here the sdl/sdr
  629. * would blow up, so for now we don't handle unaligned 64-bit
  630. * instructions on 32-bit kernels.
  631. */
  632. if (!access_ok(VERIFY_WRITE, addr, 8))
  633. goto sigbus;
  634. compute_return_epc(regs);
  635. value = regs->regs[insn.i_format.rt];
  636. StoreDW(addr, value, res);
  637. if (res)
  638. goto fault;
  639. break;
  640. #endif /* CONFIG_64BIT */
  641. /* Cannot handle 64-bit instructions in 32-bit kernel */
  642. goto sigill;
  643. case lwc1_op:
  644. case ldc1_op:
  645. case swc1_op:
  646. case sdc1_op:
  647. die_if_kernel("Unaligned FP access in kernel code", regs);
  648. BUG_ON(!used_math());
  649. lose_fpu(1); /* Save FPU state for the emulator. */
  650. res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  651. &fault_addr);
  652. own_fpu(1); /* Restore FPU state. */
  653. /* Signal if something went wrong. */
  654. process_fpemu_return(res, fault_addr);
  655. if (res == 0)
  656. break;
  657. return;
  658. /*
  659. * COP2 is available to implementor for application specific use.
  660. * It's up to applications to register a notifier chain and do
  661. * whatever they have to do, including possible sending of signals.
  662. */
  663. case lwc2_op:
  664. cu2_notifier_call_chain(CU2_LWC2_OP, regs);
  665. break;
  666. case ldc2_op:
  667. cu2_notifier_call_chain(CU2_LDC2_OP, regs);
  668. break;
  669. case swc2_op:
  670. cu2_notifier_call_chain(CU2_SWC2_OP, regs);
  671. break;
  672. case sdc2_op:
  673. cu2_notifier_call_chain(CU2_SDC2_OP, regs);
  674. break;
  675. default:
  676. /*
  677. * Pheeee... We encountered an yet unknown instruction or
  678. * cache coherence problem. Die sucker, die ...
  679. */
  680. goto sigill;
  681. }
  682. #ifdef CONFIG_DEBUG_FS
  683. unaligned_instructions++;
  684. #endif
  685. return;
  686. fault:
  687. /* roll back jump/branch */
  688. regs->cp0_epc = origpc;
  689. regs->regs[31] = orig31;
  690. /* Did we have an exception handler installed? */
  691. if (fixup_exception(regs))
  692. return;
  693. die_if_kernel("Unhandled kernel unaligned access", regs);
  694. force_sig(SIGSEGV, current);
  695. return;
  696. sigbus:
  697. die_if_kernel("Unhandled kernel unaligned access", regs);
  698. force_sig(SIGBUS, current);
  699. return;
  700. sigill:
  701. die_if_kernel
  702. ("Unhandled kernel unaligned access or invalid instruction", regs);
  703. force_sig(SIGILL, current);
  704. }
  705. /* Recode table from 16-bit register notation to 32-bit GPR. */
  706. const int reg16to32[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
  707. /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
  708. const int reg16to32st[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
  709. static void emulate_load_store_microMIPS(struct pt_regs *regs,
  710. void __user *addr)
  711. {
  712. unsigned long value;
  713. unsigned int res;
  714. int i;
  715. unsigned int reg = 0, rvar;
  716. unsigned long orig31;
  717. u16 __user *pc16;
  718. u16 halfword;
  719. unsigned int word;
  720. unsigned long origpc, contpc;
  721. union mips_instruction insn;
  722. struct mm_decoded_insn mminsn;
  723. void __user *fault_addr = NULL;
  724. origpc = regs->cp0_epc;
  725. orig31 = regs->regs[31];
  726. mminsn.micro_mips_mode = 1;
  727. /*
  728. * This load never faults.
  729. */
  730. pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
  731. __get_user(halfword, pc16);
  732. pc16++;
  733. contpc = regs->cp0_epc + 2;
  734. word = ((unsigned int)halfword << 16);
  735. mminsn.pc_inc = 2;
  736. if (!mm_insn_16bit(halfword)) {
  737. __get_user(halfword, pc16);
  738. pc16++;
  739. contpc = regs->cp0_epc + 4;
  740. mminsn.pc_inc = 4;
  741. word |= halfword;
  742. }
  743. mminsn.insn = word;
  744. if (get_user(halfword, pc16))
  745. goto fault;
  746. mminsn.next_pc_inc = 2;
  747. word = ((unsigned int)halfword << 16);
  748. if (!mm_insn_16bit(halfword)) {
  749. pc16++;
  750. if (get_user(halfword, pc16))
  751. goto fault;
  752. mminsn.next_pc_inc = 4;
  753. word |= halfword;
  754. }
  755. mminsn.next_insn = word;
  756. insn = (union mips_instruction)(mminsn.insn);
  757. if (mm_isBranchInstr(regs, mminsn, &contpc))
  758. insn = (union mips_instruction)(mminsn.next_insn);
  759. /* Parse instruction to find what to do */
  760. switch (insn.mm_i_format.opcode) {
  761. case mm_pool32a_op:
  762. switch (insn.mm_x_format.func) {
  763. case mm_lwxs_op:
  764. reg = insn.mm_x_format.rd;
  765. goto loadW;
  766. }
  767. goto sigbus;
  768. case mm_pool32b_op:
  769. switch (insn.mm_m_format.func) {
  770. case mm_lwp_func:
  771. reg = insn.mm_m_format.rd;
  772. if (reg == 31)
  773. goto sigbus;
  774. if (!access_ok(VERIFY_READ, addr, 8))
  775. goto sigbus;
  776. LoadW(addr, value, res);
  777. if (res)
  778. goto fault;
  779. regs->regs[reg] = value;
  780. addr += 4;
  781. LoadW(addr, value, res);
  782. if (res)
  783. goto fault;
  784. regs->regs[reg + 1] = value;
  785. goto success;
  786. case mm_swp_func:
  787. reg = insn.mm_m_format.rd;
  788. if (reg == 31)
  789. goto sigbus;
  790. if (!access_ok(VERIFY_WRITE, addr, 8))
  791. goto sigbus;
  792. value = regs->regs[reg];
  793. StoreW(addr, value, res);
  794. if (res)
  795. goto fault;
  796. addr += 4;
  797. value = regs->regs[reg + 1];
  798. StoreW(addr, value, res);
  799. if (res)
  800. goto fault;
  801. goto success;
  802. case mm_ldp_func:
  803. #ifdef CONFIG_64BIT
  804. reg = insn.mm_m_format.rd;
  805. if (reg == 31)
  806. goto sigbus;
  807. if (!access_ok(VERIFY_READ, addr, 16))
  808. goto sigbus;
  809. LoadDW(addr, value, res);
  810. if (res)
  811. goto fault;
  812. regs->regs[reg] = value;
  813. addr += 8;
  814. LoadDW(addr, value, res);
  815. if (res)
  816. goto fault;
  817. regs->regs[reg + 1] = value;
  818. goto success;
  819. #endif /* CONFIG_64BIT */
  820. goto sigill;
  821. case mm_sdp_func:
  822. #ifdef CONFIG_64BIT
  823. reg = insn.mm_m_format.rd;
  824. if (reg == 31)
  825. goto sigbus;
  826. if (!access_ok(VERIFY_WRITE, addr, 16))
  827. goto sigbus;
  828. value = regs->regs[reg];
  829. StoreDW(addr, value, res);
  830. if (res)
  831. goto fault;
  832. addr += 8;
  833. value = regs->regs[reg + 1];
  834. StoreDW(addr, value, res);
  835. if (res)
  836. goto fault;
  837. goto success;
  838. #endif /* CONFIG_64BIT */
  839. goto sigill;
  840. case mm_lwm32_func:
  841. reg = insn.mm_m_format.rd;
  842. rvar = reg & 0xf;
  843. if ((rvar > 9) || !reg)
  844. goto sigill;
  845. if (reg & 0x10) {
  846. if (!access_ok
  847. (VERIFY_READ, addr, 4 * (rvar + 1)))
  848. goto sigbus;
  849. } else {
  850. if (!access_ok(VERIFY_READ, addr, 4 * rvar))
  851. goto sigbus;
  852. }
  853. if (rvar == 9)
  854. rvar = 8;
  855. for (i = 16; rvar; rvar--, i++) {
  856. LoadW(addr, value, res);
  857. if (res)
  858. goto fault;
  859. addr += 4;
  860. regs->regs[i] = value;
  861. }
  862. if ((reg & 0xf) == 9) {
  863. LoadW(addr, value, res);
  864. if (res)
  865. goto fault;
  866. addr += 4;
  867. regs->regs[30] = value;
  868. }
  869. if (reg & 0x10) {
  870. LoadW(addr, value, res);
  871. if (res)
  872. goto fault;
  873. regs->regs[31] = value;
  874. }
  875. goto success;
  876. case mm_swm32_func:
  877. reg = insn.mm_m_format.rd;
  878. rvar = reg & 0xf;
  879. if ((rvar > 9) || !reg)
  880. goto sigill;
  881. if (reg & 0x10) {
  882. if (!access_ok
  883. (VERIFY_WRITE, addr, 4 * (rvar + 1)))
  884. goto sigbus;
  885. } else {
  886. if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
  887. goto sigbus;
  888. }
  889. if (rvar == 9)
  890. rvar = 8;
  891. for (i = 16; rvar; rvar--, i++) {
  892. value = regs->regs[i];
  893. StoreW(addr, value, res);
  894. if (res)
  895. goto fault;
  896. addr += 4;
  897. }
  898. if ((reg & 0xf) == 9) {
  899. value = regs->regs[30];
  900. StoreW(addr, value, res);
  901. if (res)
  902. goto fault;
  903. addr += 4;
  904. }
  905. if (reg & 0x10) {
  906. value = regs->regs[31];
  907. StoreW(addr, value, res);
  908. if (res)
  909. goto fault;
  910. }
  911. goto success;
  912. case mm_ldm_func:
  913. #ifdef CONFIG_64BIT
  914. reg = insn.mm_m_format.rd;
  915. rvar = reg & 0xf;
  916. if ((rvar > 9) || !reg)
  917. goto sigill;
  918. if (reg & 0x10) {
  919. if (!access_ok
  920. (VERIFY_READ, addr, 8 * (rvar + 1)))
  921. goto sigbus;
  922. } else {
  923. if (!access_ok(VERIFY_READ, addr, 8 * rvar))
  924. goto sigbus;
  925. }
  926. if (rvar == 9)
  927. rvar = 8;
  928. for (i = 16; rvar; rvar--, i++) {
  929. LoadDW(addr, value, res);
  930. if (res)
  931. goto fault;
  932. addr += 4;
  933. regs->regs[i] = value;
  934. }
  935. if ((reg & 0xf) == 9) {
  936. LoadDW(addr, value, res);
  937. if (res)
  938. goto fault;
  939. addr += 8;
  940. regs->regs[30] = value;
  941. }
  942. if (reg & 0x10) {
  943. LoadDW(addr, value, res);
  944. if (res)
  945. goto fault;
  946. regs->regs[31] = value;
  947. }
  948. goto success;
  949. #endif /* CONFIG_64BIT */
  950. goto sigill;
  951. case mm_sdm_func:
  952. #ifdef CONFIG_64BIT
  953. reg = insn.mm_m_format.rd;
  954. rvar = reg & 0xf;
  955. if ((rvar > 9) || !reg)
  956. goto sigill;
  957. if (reg & 0x10) {
  958. if (!access_ok
  959. (VERIFY_WRITE, addr, 8 * (rvar + 1)))
  960. goto sigbus;
  961. } else {
  962. if (!access_ok(VERIFY_WRITE, addr, 8 * rvar))
  963. goto sigbus;
  964. }
  965. if (rvar == 9)
  966. rvar = 8;
  967. for (i = 16; rvar; rvar--, i++) {
  968. value = regs->regs[i];
  969. StoreDW(addr, value, res);
  970. if (res)
  971. goto fault;
  972. addr += 8;
  973. }
  974. if ((reg & 0xf) == 9) {
  975. value = regs->regs[30];
  976. StoreDW(addr, value, res);
  977. if (res)
  978. goto fault;
  979. addr += 8;
  980. }
  981. if (reg & 0x10) {
  982. value = regs->regs[31];
  983. StoreDW(addr, value, res);
  984. if (res)
  985. goto fault;
  986. }
  987. goto success;
  988. #endif /* CONFIG_64BIT */
  989. goto sigill;
  990. /* LWC2, SWC2, LDC2, SDC2 are not serviced */
  991. }
  992. goto sigbus;
  993. case mm_pool32c_op:
  994. switch (insn.mm_m_format.func) {
  995. case mm_lwu_func:
  996. reg = insn.mm_m_format.rd;
  997. goto loadWU;
  998. }
  999. /* LL,SC,LLD,SCD are not serviced */
  1000. goto sigbus;
  1001. case mm_pool32f_op:
  1002. switch (insn.mm_x_format.func) {
  1003. case mm_lwxc1_func:
  1004. case mm_swxc1_func:
  1005. case mm_ldxc1_func:
  1006. case mm_sdxc1_func:
  1007. goto fpu_emul;
  1008. }
  1009. goto sigbus;
  1010. case mm_ldc132_op:
  1011. case mm_sdc132_op:
  1012. case mm_lwc132_op:
  1013. case mm_swc132_op:
  1014. fpu_emul:
  1015. /* roll back jump/branch */
  1016. regs->cp0_epc = origpc;
  1017. regs->regs[31] = orig31;
  1018. die_if_kernel("Unaligned FP access in kernel code", regs);
  1019. BUG_ON(!used_math());
  1020. BUG_ON(!is_fpu_owner());
  1021. lose_fpu(1); /* save the FPU state for the emulator */
  1022. res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  1023. &fault_addr);
  1024. own_fpu(1); /* restore FPU state */
  1025. /* If something went wrong, signal */
  1026. process_fpemu_return(res, fault_addr);
  1027. if (res == 0)
  1028. goto success;
  1029. return;
  1030. case mm_lh32_op:
  1031. reg = insn.mm_i_format.rt;
  1032. goto loadHW;
  1033. case mm_lhu32_op:
  1034. reg = insn.mm_i_format.rt;
  1035. goto loadHWU;
  1036. case mm_lw32_op:
  1037. reg = insn.mm_i_format.rt;
  1038. goto loadW;
  1039. case mm_sh32_op:
  1040. reg = insn.mm_i_format.rt;
  1041. goto storeHW;
  1042. case mm_sw32_op:
  1043. reg = insn.mm_i_format.rt;
  1044. goto storeW;
  1045. case mm_ld32_op:
  1046. reg = insn.mm_i_format.rt;
  1047. goto loadDW;
  1048. case mm_sd32_op:
  1049. reg = insn.mm_i_format.rt;
  1050. goto storeDW;
  1051. case mm_pool16c_op:
  1052. switch (insn.mm16_m_format.func) {
  1053. case mm_lwm16_op:
  1054. reg = insn.mm16_m_format.rlist;
  1055. rvar = reg + 1;
  1056. if (!access_ok(VERIFY_READ, addr, 4 * rvar))
  1057. goto sigbus;
  1058. for (i = 16; rvar; rvar--, i++) {
  1059. LoadW(addr, value, res);
  1060. if (res)
  1061. goto fault;
  1062. addr += 4;
  1063. regs->regs[i] = value;
  1064. }
  1065. LoadW(addr, value, res);
  1066. if (res)
  1067. goto fault;
  1068. regs->regs[31] = value;
  1069. goto success;
  1070. case mm_swm16_op:
  1071. reg = insn.mm16_m_format.rlist;
  1072. rvar = reg + 1;
  1073. if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
  1074. goto sigbus;
  1075. for (i = 16; rvar; rvar--, i++) {
  1076. value = regs->regs[i];
  1077. StoreW(addr, value, res);
  1078. if (res)
  1079. goto fault;
  1080. addr += 4;
  1081. }
  1082. value = regs->regs[31];
  1083. StoreW(addr, value, res);
  1084. if (res)
  1085. goto fault;
  1086. goto success;
  1087. }
  1088. goto sigbus;
  1089. case mm_lhu16_op:
  1090. reg = reg16to32[insn.mm16_rb_format.rt];
  1091. goto loadHWU;
  1092. case mm_lw16_op:
  1093. reg = reg16to32[insn.mm16_rb_format.rt];
  1094. goto loadW;
  1095. case mm_sh16_op:
  1096. reg = reg16to32st[insn.mm16_rb_format.rt];
  1097. goto storeHW;
  1098. case mm_sw16_op:
  1099. reg = reg16to32st[insn.mm16_rb_format.rt];
  1100. goto storeW;
  1101. case mm_lwsp16_op:
  1102. reg = insn.mm16_r5_format.rt;
  1103. goto loadW;
  1104. case mm_swsp16_op:
  1105. reg = insn.mm16_r5_format.rt;
  1106. goto storeW;
  1107. case mm_lwgp16_op:
  1108. reg = reg16to32[insn.mm16_r3_format.rt];
  1109. goto loadW;
  1110. default:
  1111. goto sigill;
  1112. }
  1113. loadHW:
  1114. if (!access_ok(VERIFY_READ, addr, 2))
  1115. goto sigbus;
  1116. LoadHW(addr, value, res);
  1117. if (res)
  1118. goto fault;
  1119. regs->regs[reg] = value;
  1120. goto success;
  1121. loadHWU:
  1122. if (!access_ok(VERIFY_READ, addr, 2))
  1123. goto sigbus;
  1124. LoadHWU(addr, value, res);
  1125. if (res)
  1126. goto fault;
  1127. regs->regs[reg] = value;
  1128. goto success;
  1129. loadW:
  1130. if (!access_ok(VERIFY_READ, addr, 4))
  1131. goto sigbus;
  1132. LoadW(addr, value, res);
  1133. if (res)
  1134. goto fault;
  1135. regs->regs[reg] = value;
  1136. goto success;
  1137. loadWU:
  1138. #ifdef CONFIG_64BIT
  1139. /*
  1140. * A 32-bit kernel might be running on a 64-bit processor. But
  1141. * if we're on a 32-bit processor and an i-cache incoherency
  1142. * or race makes us see a 64-bit instruction here the sdl/sdr
  1143. * would blow up, so for now we don't handle unaligned 64-bit
  1144. * instructions on 32-bit kernels.
  1145. */
  1146. if (!access_ok(VERIFY_READ, addr, 4))
  1147. goto sigbus;
  1148. LoadWU(addr, value, res);
  1149. if (res)
  1150. goto fault;
  1151. regs->regs[reg] = value;
  1152. goto success;
  1153. #endif /* CONFIG_64BIT */
  1154. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1155. goto sigill;
  1156. loadDW:
  1157. #ifdef CONFIG_64BIT
  1158. /*
  1159. * A 32-bit kernel might be running on a 64-bit processor. But
  1160. * if we're on a 32-bit processor and an i-cache incoherency
  1161. * or race makes us see a 64-bit instruction here the sdl/sdr
  1162. * would blow up, so for now we don't handle unaligned 64-bit
  1163. * instructions on 32-bit kernels.
  1164. */
  1165. if (!access_ok(VERIFY_READ, addr, 8))
  1166. goto sigbus;
  1167. LoadDW(addr, value, res);
  1168. if (res)
  1169. goto fault;
  1170. regs->regs[reg] = value;
  1171. goto success;
  1172. #endif /* CONFIG_64BIT */
  1173. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1174. goto sigill;
  1175. storeHW:
  1176. if (!access_ok(VERIFY_WRITE, addr, 2))
  1177. goto sigbus;
  1178. value = regs->regs[reg];
  1179. StoreHW(addr, value, res);
  1180. if (res)
  1181. goto fault;
  1182. goto success;
  1183. storeW:
  1184. if (!access_ok(VERIFY_WRITE, addr, 4))
  1185. goto sigbus;
  1186. value = regs->regs[reg];
  1187. StoreW(addr, value, res);
  1188. if (res)
  1189. goto fault;
  1190. goto success;
  1191. storeDW:
  1192. #ifdef CONFIG_64BIT
  1193. /*
  1194. * A 32-bit kernel might be running on a 64-bit processor. But
  1195. * if we're on a 32-bit processor and an i-cache incoherency
  1196. * or race makes us see a 64-bit instruction here the sdl/sdr
  1197. * would blow up, so for now we don't handle unaligned 64-bit
  1198. * instructions on 32-bit kernels.
  1199. */
  1200. if (!access_ok(VERIFY_WRITE, addr, 8))
  1201. goto sigbus;
  1202. value = regs->regs[reg];
  1203. StoreDW(addr, value, res);
  1204. if (res)
  1205. goto fault;
  1206. goto success;
  1207. #endif /* CONFIG_64BIT */
  1208. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1209. goto sigill;
  1210. success:
  1211. regs->cp0_epc = contpc; /* advance or branch */
  1212. #ifdef CONFIG_DEBUG_FS
  1213. unaligned_instructions++;
  1214. #endif
  1215. return;
  1216. fault:
  1217. /* roll back jump/branch */
  1218. regs->cp0_epc = origpc;
  1219. regs->regs[31] = orig31;
  1220. /* Did we have an exception handler installed? */
  1221. if (fixup_exception(regs))
  1222. return;
  1223. die_if_kernel("Unhandled kernel unaligned access", regs);
  1224. force_sig(SIGSEGV, current);
  1225. return;
  1226. sigbus:
  1227. die_if_kernel("Unhandled kernel unaligned access", regs);
  1228. force_sig(SIGBUS, current);
  1229. return;
  1230. sigill:
  1231. die_if_kernel
  1232. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1233. force_sig(SIGILL, current);
  1234. }
  1235. static void emulate_load_store_MIPS16e(struct pt_regs *regs, void __user * addr)
  1236. {
  1237. unsigned long value;
  1238. unsigned int res;
  1239. int reg;
  1240. unsigned long orig31;
  1241. u16 __user *pc16;
  1242. unsigned long origpc;
  1243. union mips16e_instruction mips16inst, oldinst;
  1244. origpc = regs->cp0_epc;
  1245. orig31 = regs->regs[31];
  1246. pc16 = (unsigned short __user *)msk_isa16_mode(origpc);
  1247. /*
  1248. * This load never faults.
  1249. */
  1250. __get_user(mips16inst.full, pc16);
  1251. oldinst = mips16inst;
  1252. /* skip EXTEND instruction */
  1253. if (mips16inst.ri.opcode == MIPS16e_extend_op) {
  1254. pc16++;
  1255. __get_user(mips16inst.full, pc16);
  1256. } else if (delay_slot(regs)) {
  1257. /* skip jump instructions */
  1258. /* JAL/JALX are 32 bits but have OPCODE in first short int */
  1259. if (mips16inst.ri.opcode == MIPS16e_jal_op)
  1260. pc16++;
  1261. pc16++;
  1262. if (get_user(mips16inst.full, pc16))
  1263. goto sigbus;
  1264. }
  1265. switch (mips16inst.ri.opcode) {
  1266. case MIPS16e_i64_op: /* I64 or RI64 instruction */
  1267. switch (mips16inst.i64.func) { /* I64/RI64 func field check */
  1268. case MIPS16e_ldpc_func:
  1269. case MIPS16e_ldsp_func:
  1270. reg = reg16to32[mips16inst.ri64.ry];
  1271. goto loadDW;
  1272. case MIPS16e_sdsp_func:
  1273. reg = reg16to32[mips16inst.ri64.ry];
  1274. goto writeDW;
  1275. case MIPS16e_sdrasp_func:
  1276. reg = 29; /* GPRSP */
  1277. goto writeDW;
  1278. }
  1279. goto sigbus;
  1280. case MIPS16e_swsp_op:
  1281. case MIPS16e_lwpc_op:
  1282. case MIPS16e_lwsp_op:
  1283. reg = reg16to32[mips16inst.ri.rx];
  1284. break;
  1285. case MIPS16e_i8_op:
  1286. if (mips16inst.i8.func != MIPS16e_swrasp_func)
  1287. goto sigbus;
  1288. reg = 29; /* GPRSP */
  1289. break;
  1290. default:
  1291. reg = reg16to32[mips16inst.rri.ry];
  1292. break;
  1293. }
  1294. switch (mips16inst.ri.opcode) {
  1295. case MIPS16e_lb_op:
  1296. case MIPS16e_lbu_op:
  1297. case MIPS16e_sb_op:
  1298. goto sigbus;
  1299. case MIPS16e_lh_op:
  1300. if (!access_ok(VERIFY_READ, addr, 2))
  1301. goto sigbus;
  1302. LoadHW(addr, value, res);
  1303. if (res)
  1304. goto fault;
  1305. MIPS16e_compute_return_epc(regs, &oldinst);
  1306. regs->regs[reg] = value;
  1307. break;
  1308. case MIPS16e_lhu_op:
  1309. if (!access_ok(VERIFY_READ, addr, 2))
  1310. goto sigbus;
  1311. LoadHWU(addr, value, res);
  1312. if (res)
  1313. goto fault;
  1314. MIPS16e_compute_return_epc(regs, &oldinst);
  1315. regs->regs[reg] = value;
  1316. break;
  1317. case MIPS16e_lw_op:
  1318. case MIPS16e_lwpc_op:
  1319. case MIPS16e_lwsp_op:
  1320. if (!access_ok(VERIFY_READ, addr, 4))
  1321. goto sigbus;
  1322. LoadW(addr, value, res);
  1323. if (res)
  1324. goto fault;
  1325. MIPS16e_compute_return_epc(regs, &oldinst);
  1326. regs->regs[reg] = value;
  1327. break;
  1328. case MIPS16e_lwu_op:
  1329. #ifdef CONFIG_64BIT
  1330. /*
  1331. * A 32-bit kernel might be running on a 64-bit processor. But
  1332. * if we're on a 32-bit processor and an i-cache incoherency
  1333. * or race makes us see a 64-bit instruction here the sdl/sdr
  1334. * would blow up, so for now we don't handle unaligned 64-bit
  1335. * instructions on 32-bit kernels.
  1336. */
  1337. if (!access_ok(VERIFY_READ, addr, 4))
  1338. goto sigbus;
  1339. LoadWU(addr, value, res);
  1340. if (res)
  1341. goto fault;
  1342. MIPS16e_compute_return_epc(regs, &oldinst);
  1343. regs->regs[reg] = value;
  1344. break;
  1345. #endif /* CONFIG_64BIT */
  1346. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1347. goto sigill;
  1348. case MIPS16e_ld_op:
  1349. loadDW:
  1350. #ifdef CONFIG_64BIT
  1351. /*
  1352. * A 32-bit kernel might be running on a 64-bit processor. But
  1353. * if we're on a 32-bit processor and an i-cache incoherency
  1354. * or race makes us see a 64-bit instruction here the sdl/sdr
  1355. * would blow up, so for now we don't handle unaligned 64-bit
  1356. * instructions on 32-bit kernels.
  1357. */
  1358. if (!access_ok(VERIFY_READ, addr, 8))
  1359. goto sigbus;
  1360. LoadDW(addr, value, res);
  1361. if (res)
  1362. goto fault;
  1363. MIPS16e_compute_return_epc(regs, &oldinst);
  1364. regs->regs[reg] = value;
  1365. break;
  1366. #endif /* CONFIG_64BIT */
  1367. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1368. goto sigill;
  1369. case MIPS16e_sh_op:
  1370. if (!access_ok(VERIFY_WRITE, addr, 2))
  1371. goto sigbus;
  1372. MIPS16e_compute_return_epc(regs, &oldinst);
  1373. value = regs->regs[reg];
  1374. StoreHW(addr, value, res);
  1375. if (res)
  1376. goto fault;
  1377. break;
  1378. case MIPS16e_sw_op:
  1379. case MIPS16e_swsp_op:
  1380. case MIPS16e_i8_op: /* actually - MIPS16e_swrasp_func */
  1381. if (!access_ok(VERIFY_WRITE, addr, 4))
  1382. goto sigbus;
  1383. MIPS16e_compute_return_epc(regs, &oldinst);
  1384. value = regs->regs[reg];
  1385. StoreW(addr, value, res);
  1386. if (res)
  1387. goto fault;
  1388. break;
  1389. case MIPS16e_sd_op:
  1390. writeDW:
  1391. #ifdef CONFIG_64BIT
  1392. /*
  1393. * A 32-bit kernel might be running on a 64-bit processor. But
  1394. * if we're on a 32-bit processor and an i-cache incoherency
  1395. * or race makes us see a 64-bit instruction here the sdl/sdr
  1396. * would blow up, so for now we don't handle unaligned 64-bit
  1397. * instructions on 32-bit kernels.
  1398. */
  1399. if (!access_ok(VERIFY_WRITE, addr, 8))
  1400. goto sigbus;
  1401. MIPS16e_compute_return_epc(regs, &oldinst);
  1402. value = regs->regs[reg];
  1403. StoreDW(addr, value, res);
  1404. if (res)
  1405. goto fault;
  1406. break;
  1407. #endif /* CONFIG_64BIT */
  1408. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1409. goto sigill;
  1410. default:
  1411. /*
  1412. * Pheeee... We encountered an yet unknown instruction or
  1413. * cache coherence problem. Die sucker, die ...
  1414. */
  1415. goto sigill;
  1416. }
  1417. #ifdef CONFIG_DEBUG_FS
  1418. unaligned_instructions++;
  1419. #endif
  1420. return;
  1421. fault:
  1422. /* roll back jump/branch */
  1423. regs->cp0_epc = origpc;
  1424. regs->regs[31] = orig31;
  1425. /* Did we have an exception handler installed? */
  1426. if (fixup_exception(regs))
  1427. return;
  1428. die_if_kernel("Unhandled kernel unaligned access", regs);
  1429. force_sig(SIGSEGV, current);
  1430. return;
  1431. sigbus:
  1432. die_if_kernel("Unhandled kernel unaligned access", regs);
  1433. force_sig(SIGBUS, current);
  1434. return;
  1435. sigill:
  1436. die_if_kernel
  1437. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1438. force_sig(SIGILL, current);
  1439. }
  1440. asmlinkage void do_ade(struct pt_regs *regs)
  1441. {
  1442. enum ctx_state prev_state;
  1443. unsigned int __user *pc;
  1444. mm_segment_t seg;
  1445. prev_state = exception_enter();
  1446. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
  1447. 1, regs, regs->cp0_badvaddr);
  1448. /*
  1449. * Did we catch a fault trying to load an instruction?
  1450. */
  1451. if (regs->cp0_badvaddr == regs->cp0_epc)
  1452. goto sigbus;
  1453. if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
  1454. goto sigbus;
  1455. if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
  1456. goto sigbus;
  1457. /*
  1458. * Do branch emulation only if we didn't forward the exception.
  1459. * This is all so but ugly ...
  1460. */
  1461. /*
  1462. * Are we running in microMIPS mode?
  1463. */
  1464. if (get_isa16_mode(regs->cp0_epc)) {
  1465. /*
  1466. * Did we catch a fault trying to load an instruction in
  1467. * 16-bit mode?
  1468. */
  1469. if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
  1470. goto sigbus;
  1471. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  1472. show_registers(regs);
  1473. if (cpu_has_mmips) {
  1474. seg = get_fs();
  1475. if (!user_mode(regs))
  1476. set_fs(KERNEL_DS);
  1477. emulate_load_store_microMIPS(regs,
  1478. (void __user *)regs->cp0_badvaddr);
  1479. set_fs(seg);
  1480. return;
  1481. }
  1482. if (cpu_has_mips16) {
  1483. seg = get_fs();
  1484. if (!user_mode(regs))
  1485. set_fs(KERNEL_DS);
  1486. emulate_load_store_MIPS16e(regs,
  1487. (void __user *)regs->cp0_badvaddr);
  1488. set_fs(seg);
  1489. return;
  1490. }
  1491. goto sigbus;
  1492. }
  1493. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  1494. show_registers(regs);
  1495. pc = (unsigned int __user *)exception_epc(regs);
  1496. seg = get_fs();
  1497. if (!user_mode(regs))
  1498. set_fs(KERNEL_DS);
  1499. emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
  1500. set_fs(seg);
  1501. return;
  1502. sigbus:
  1503. die_if_kernel("Kernel unaligned instruction access", regs);
  1504. force_sig(SIGBUS, current);
  1505. /*
  1506. * XXX On return from the signal handler we should advance the epc
  1507. */
  1508. exception_exit(prev_state);
  1509. }
  1510. #ifdef CONFIG_DEBUG_FS
  1511. extern struct dentry *mips_debugfs_dir;
  1512. static int __init debugfs_unaligned(void)
  1513. {
  1514. struct dentry *d;
  1515. if (!mips_debugfs_dir)
  1516. return -ENODEV;
  1517. d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
  1518. mips_debugfs_dir, &unaligned_instructions);
  1519. if (!d)
  1520. return -ENOMEM;
  1521. d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
  1522. mips_debugfs_dir, &unaligned_action);
  1523. if (!d)
  1524. return -ENOMEM;
  1525. return 0;
  1526. }
  1527. __initcall(debugfs_unaligned);
  1528. #endif