unaligned.c 40 KB

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