unaligned.c 59 KB

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