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