unaligned.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325
  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, preempted;
  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. do {
  1151. /*
  1152. * If we have live MSA context keep track of
  1153. * whether we get preempted in order to avoid
  1154. * the register context we load being clobbered
  1155. * by the live context as it's saved during
  1156. * preemption. If we don't have live context
  1157. * then it can't be saved to clobber the value
  1158. * we load.
  1159. */
  1160. preempted = test_thread_flag(TIF_USEDMSA);
  1161. res = __copy_from_user_inatomic(fpr, addr,
  1162. sizeof(*fpr));
  1163. if (res)
  1164. goto fault;
  1165. /*
  1166. * Update the hardware register if it is in use
  1167. * by the task in this quantum, in order to
  1168. * avoid having to save & restore the whole
  1169. * vector context.
  1170. */
  1171. preempt_disable();
  1172. if (test_thread_flag(TIF_USEDMSA)) {
  1173. write_msa_wr(wd, fpr, df);
  1174. preempted = 0;
  1175. }
  1176. preempt_enable();
  1177. } while (preempted);
  1178. break;
  1179. case msa_st_op:
  1180. if (!access_ok(VERIFY_WRITE, addr, sizeof(*fpr)))
  1181. goto sigbus;
  1182. /*
  1183. * Update from the hardware register if it is in use by
  1184. * the task in this quantum, in order to avoid having to
  1185. * save & restore the whole vector context.
  1186. */
  1187. preempt_disable();
  1188. if (test_thread_flag(TIF_USEDMSA))
  1189. read_msa_wr(wd, fpr, df);
  1190. preempt_enable();
  1191. res = __copy_to_user_inatomic(addr, fpr, sizeof(*fpr));
  1192. if (res)
  1193. goto fault;
  1194. break;
  1195. default:
  1196. goto sigbus;
  1197. }
  1198. compute_return_epc(regs);
  1199. break;
  1200. #ifndef CONFIG_CPU_MIPSR6
  1201. /*
  1202. * COP2 is available to implementor for application specific use.
  1203. * It's up to applications to register a notifier chain and do
  1204. * whatever they have to do, including possible sending of signals.
  1205. *
  1206. * This instruction has been reallocated in Release 6
  1207. */
  1208. case lwc2_op:
  1209. cu2_notifier_call_chain(CU2_LWC2_OP, regs);
  1210. break;
  1211. case ldc2_op:
  1212. cu2_notifier_call_chain(CU2_LDC2_OP, regs);
  1213. break;
  1214. case swc2_op:
  1215. cu2_notifier_call_chain(CU2_SWC2_OP, regs);
  1216. break;
  1217. case sdc2_op:
  1218. cu2_notifier_call_chain(CU2_SDC2_OP, regs);
  1219. break;
  1220. #endif
  1221. default:
  1222. /*
  1223. * Pheeee... We encountered an yet unknown instruction or
  1224. * cache coherence problem. Die sucker, die ...
  1225. */
  1226. goto sigill;
  1227. }
  1228. #ifdef CONFIG_DEBUG_FS
  1229. unaligned_instructions++;
  1230. #endif
  1231. return;
  1232. fault:
  1233. /* roll back jump/branch */
  1234. regs->cp0_epc = origpc;
  1235. regs->regs[31] = orig31;
  1236. /* Did we have an exception handler installed? */
  1237. if (fixup_exception(regs))
  1238. return;
  1239. die_if_kernel("Unhandled kernel unaligned access", regs);
  1240. force_sig(SIGSEGV, current);
  1241. return;
  1242. sigbus:
  1243. die_if_kernel("Unhandled kernel unaligned access", regs);
  1244. force_sig(SIGBUS, current);
  1245. return;
  1246. sigill:
  1247. die_if_kernel
  1248. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1249. force_sig(SIGILL, current);
  1250. }
  1251. /* Recode table from 16-bit register notation to 32-bit GPR. */
  1252. const int reg16to32[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
  1253. /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
  1254. const int reg16to32st[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
  1255. static void emulate_load_store_microMIPS(struct pt_regs *regs,
  1256. void __user *addr)
  1257. {
  1258. unsigned long value;
  1259. unsigned int res;
  1260. int i;
  1261. unsigned int reg = 0, rvar;
  1262. unsigned long orig31;
  1263. u16 __user *pc16;
  1264. u16 halfword;
  1265. unsigned int word;
  1266. unsigned long origpc, contpc;
  1267. union mips_instruction insn;
  1268. struct mm_decoded_insn mminsn;
  1269. void __user *fault_addr = NULL;
  1270. origpc = regs->cp0_epc;
  1271. orig31 = regs->regs[31];
  1272. mminsn.micro_mips_mode = 1;
  1273. /*
  1274. * This load never faults.
  1275. */
  1276. pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
  1277. __get_user(halfword, pc16);
  1278. pc16++;
  1279. contpc = regs->cp0_epc + 2;
  1280. word = ((unsigned int)halfword << 16);
  1281. mminsn.pc_inc = 2;
  1282. if (!mm_insn_16bit(halfword)) {
  1283. __get_user(halfword, pc16);
  1284. pc16++;
  1285. contpc = regs->cp0_epc + 4;
  1286. mminsn.pc_inc = 4;
  1287. word |= halfword;
  1288. }
  1289. mminsn.insn = word;
  1290. if (get_user(halfword, pc16))
  1291. goto fault;
  1292. mminsn.next_pc_inc = 2;
  1293. word = ((unsigned int)halfword << 16);
  1294. if (!mm_insn_16bit(halfword)) {
  1295. pc16++;
  1296. if (get_user(halfword, pc16))
  1297. goto fault;
  1298. mminsn.next_pc_inc = 4;
  1299. word |= halfword;
  1300. }
  1301. mminsn.next_insn = word;
  1302. insn = (union mips_instruction)(mminsn.insn);
  1303. if (mm_isBranchInstr(regs, mminsn, &contpc))
  1304. insn = (union mips_instruction)(mminsn.next_insn);
  1305. /* Parse instruction to find what to do */
  1306. switch (insn.mm_i_format.opcode) {
  1307. case mm_pool32a_op:
  1308. switch (insn.mm_x_format.func) {
  1309. case mm_lwxs_op:
  1310. reg = insn.mm_x_format.rd;
  1311. goto loadW;
  1312. }
  1313. goto sigbus;
  1314. case mm_pool32b_op:
  1315. switch (insn.mm_m_format.func) {
  1316. case mm_lwp_func:
  1317. reg = insn.mm_m_format.rd;
  1318. if (reg == 31)
  1319. goto sigbus;
  1320. if (!access_ok(VERIFY_READ, addr, 8))
  1321. goto sigbus;
  1322. LoadW(addr, value, res);
  1323. if (res)
  1324. goto fault;
  1325. regs->regs[reg] = value;
  1326. addr += 4;
  1327. LoadW(addr, value, res);
  1328. if (res)
  1329. goto fault;
  1330. regs->regs[reg + 1] = value;
  1331. goto success;
  1332. case mm_swp_func:
  1333. reg = insn.mm_m_format.rd;
  1334. if (reg == 31)
  1335. goto sigbus;
  1336. if (!access_ok(VERIFY_WRITE, addr, 8))
  1337. goto sigbus;
  1338. value = regs->regs[reg];
  1339. StoreW(addr, value, res);
  1340. if (res)
  1341. goto fault;
  1342. addr += 4;
  1343. value = regs->regs[reg + 1];
  1344. StoreW(addr, value, res);
  1345. if (res)
  1346. goto fault;
  1347. goto success;
  1348. case mm_ldp_func:
  1349. #ifdef CONFIG_64BIT
  1350. reg = insn.mm_m_format.rd;
  1351. if (reg == 31)
  1352. goto sigbus;
  1353. if (!access_ok(VERIFY_READ, addr, 16))
  1354. goto sigbus;
  1355. LoadDW(addr, value, res);
  1356. if (res)
  1357. goto fault;
  1358. regs->regs[reg] = value;
  1359. addr += 8;
  1360. LoadDW(addr, value, res);
  1361. if (res)
  1362. goto fault;
  1363. regs->regs[reg + 1] = value;
  1364. goto success;
  1365. #endif /* CONFIG_64BIT */
  1366. goto sigill;
  1367. case mm_sdp_func:
  1368. #ifdef CONFIG_64BIT
  1369. reg = insn.mm_m_format.rd;
  1370. if (reg == 31)
  1371. goto sigbus;
  1372. if (!access_ok(VERIFY_WRITE, addr, 16))
  1373. goto sigbus;
  1374. value = regs->regs[reg];
  1375. StoreDW(addr, value, res);
  1376. if (res)
  1377. goto fault;
  1378. addr += 8;
  1379. value = regs->regs[reg + 1];
  1380. StoreDW(addr, value, res);
  1381. if (res)
  1382. goto fault;
  1383. goto success;
  1384. #endif /* CONFIG_64BIT */
  1385. goto sigill;
  1386. case mm_lwm32_func:
  1387. reg = insn.mm_m_format.rd;
  1388. rvar = reg & 0xf;
  1389. if ((rvar > 9) || !reg)
  1390. goto sigill;
  1391. if (reg & 0x10) {
  1392. if (!access_ok
  1393. (VERIFY_READ, addr, 4 * (rvar + 1)))
  1394. goto sigbus;
  1395. } else {
  1396. if (!access_ok(VERIFY_READ, addr, 4 * rvar))
  1397. goto sigbus;
  1398. }
  1399. if (rvar == 9)
  1400. rvar = 8;
  1401. for (i = 16; rvar; rvar--, i++) {
  1402. LoadW(addr, value, res);
  1403. if (res)
  1404. goto fault;
  1405. addr += 4;
  1406. regs->regs[i] = value;
  1407. }
  1408. if ((reg & 0xf) == 9) {
  1409. LoadW(addr, value, res);
  1410. if (res)
  1411. goto fault;
  1412. addr += 4;
  1413. regs->regs[30] = value;
  1414. }
  1415. if (reg & 0x10) {
  1416. LoadW(addr, value, res);
  1417. if (res)
  1418. goto fault;
  1419. regs->regs[31] = value;
  1420. }
  1421. goto success;
  1422. case mm_swm32_func:
  1423. reg = insn.mm_m_format.rd;
  1424. rvar = reg & 0xf;
  1425. if ((rvar > 9) || !reg)
  1426. goto sigill;
  1427. if (reg & 0x10) {
  1428. if (!access_ok
  1429. (VERIFY_WRITE, addr, 4 * (rvar + 1)))
  1430. goto sigbus;
  1431. } else {
  1432. if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
  1433. goto sigbus;
  1434. }
  1435. if (rvar == 9)
  1436. rvar = 8;
  1437. for (i = 16; rvar; rvar--, i++) {
  1438. value = regs->regs[i];
  1439. StoreW(addr, value, res);
  1440. if (res)
  1441. goto fault;
  1442. addr += 4;
  1443. }
  1444. if ((reg & 0xf) == 9) {
  1445. value = regs->regs[30];
  1446. StoreW(addr, value, res);
  1447. if (res)
  1448. goto fault;
  1449. addr += 4;
  1450. }
  1451. if (reg & 0x10) {
  1452. value = regs->regs[31];
  1453. StoreW(addr, value, res);
  1454. if (res)
  1455. goto fault;
  1456. }
  1457. goto success;
  1458. case mm_ldm_func:
  1459. #ifdef CONFIG_64BIT
  1460. reg = insn.mm_m_format.rd;
  1461. rvar = reg & 0xf;
  1462. if ((rvar > 9) || !reg)
  1463. goto sigill;
  1464. if (reg & 0x10) {
  1465. if (!access_ok
  1466. (VERIFY_READ, addr, 8 * (rvar + 1)))
  1467. goto sigbus;
  1468. } else {
  1469. if (!access_ok(VERIFY_READ, addr, 8 * rvar))
  1470. goto sigbus;
  1471. }
  1472. if (rvar == 9)
  1473. rvar = 8;
  1474. for (i = 16; rvar; rvar--, i++) {
  1475. LoadDW(addr, value, res);
  1476. if (res)
  1477. goto fault;
  1478. addr += 4;
  1479. regs->regs[i] = value;
  1480. }
  1481. if ((reg & 0xf) == 9) {
  1482. LoadDW(addr, value, res);
  1483. if (res)
  1484. goto fault;
  1485. addr += 8;
  1486. regs->regs[30] = value;
  1487. }
  1488. if (reg & 0x10) {
  1489. LoadDW(addr, value, res);
  1490. if (res)
  1491. goto fault;
  1492. regs->regs[31] = value;
  1493. }
  1494. goto success;
  1495. #endif /* CONFIG_64BIT */
  1496. goto sigill;
  1497. case mm_sdm_func:
  1498. #ifdef CONFIG_64BIT
  1499. reg = insn.mm_m_format.rd;
  1500. rvar = reg & 0xf;
  1501. if ((rvar > 9) || !reg)
  1502. goto sigill;
  1503. if (reg & 0x10) {
  1504. if (!access_ok
  1505. (VERIFY_WRITE, addr, 8 * (rvar + 1)))
  1506. goto sigbus;
  1507. } else {
  1508. if (!access_ok(VERIFY_WRITE, addr, 8 * rvar))
  1509. goto sigbus;
  1510. }
  1511. if (rvar == 9)
  1512. rvar = 8;
  1513. for (i = 16; rvar; rvar--, i++) {
  1514. value = regs->regs[i];
  1515. StoreDW(addr, value, res);
  1516. if (res)
  1517. goto fault;
  1518. addr += 8;
  1519. }
  1520. if ((reg & 0xf) == 9) {
  1521. value = regs->regs[30];
  1522. StoreDW(addr, value, res);
  1523. if (res)
  1524. goto fault;
  1525. addr += 8;
  1526. }
  1527. if (reg & 0x10) {
  1528. value = regs->regs[31];
  1529. StoreDW(addr, value, res);
  1530. if (res)
  1531. goto fault;
  1532. }
  1533. goto success;
  1534. #endif /* CONFIG_64BIT */
  1535. goto sigill;
  1536. /* LWC2, SWC2, LDC2, SDC2 are not serviced */
  1537. }
  1538. goto sigbus;
  1539. case mm_pool32c_op:
  1540. switch (insn.mm_m_format.func) {
  1541. case mm_lwu_func:
  1542. reg = insn.mm_m_format.rd;
  1543. goto loadWU;
  1544. }
  1545. /* LL,SC,LLD,SCD are not serviced */
  1546. goto sigbus;
  1547. case mm_pool32f_op:
  1548. switch (insn.mm_x_format.func) {
  1549. case mm_lwxc1_func:
  1550. case mm_swxc1_func:
  1551. case mm_ldxc1_func:
  1552. case mm_sdxc1_func:
  1553. goto fpu_emul;
  1554. }
  1555. goto sigbus;
  1556. case mm_ldc132_op:
  1557. case mm_sdc132_op:
  1558. case mm_lwc132_op:
  1559. case mm_swc132_op:
  1560. fpu_emul:
  1561. /* roll back jump/branch */
  1562. regs->cp0_epc = origpc;
  1563. regs->regs[31] = orig31;
  1564. die_if_kernel("Unaligned FP access in kernel code", regs);
  1565. BUG_ON(!used_math());
  1566. BUG_ON(!is_fpu_owner());
  1567. lose_fpu(1); /* save the FPU state for the emulator */
  1568. res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  1569. &fault_addr);
  1570. own_fpu(1); /* restore FPU state */
  1571. /* If something went wrong, signal */
  1572. process_fpemu_return(res, fault_addr, 0);
  1573. if (res == 0)
  1574. goto success;
  1575. return;
  1576. case mm_lh32_op:
  1577. reg = insn.mm_i_format.rt;
  1578. goto loadHW;
  1579. case mm_lhu32_op:
  1580. reg = insn.mm_i_format.rt;
  1581. goto loadHWU;
  1582. case mm_lw32_op:
  1583. reg = insn.mm_i_format.rt;
  1584. goto loadW;
  1585. case mm_sh32_op:
  1586. reg = insn.mm_i_format.rt;
  1587. goto storeHW;
  1588. case mm_sw32_op:
  1589. reg = insn.mm_i_format.rt;
  1590. goto storeW;
  1591. case mm_ld32_op:
  1592. reg = insn.mm_i_format.rt;
  1593. goto loadDW;
  1594. case mm_sd32_op:
  1595. reg = insn.mm_i_format.rt;
  1596. goto storeDW;
  1597. case mm_pool16c_op:
  1598. switch (insn.mm16_m_format.func) {
  1599. case mm_lwm16_op:
  1600. reg = insn.mm16_m_format.rlist;
  1601. rvar = reg + 1;
  1602. if (!access_ok(VERIFY_READ, addr, 4 * rvar))
  1603. goto sigbus;
  1604. for (i = 16; rvar; rvar--, i++) {
  1605. LoadW(addr, value, res);
  1606. if (res)
  1607. goto fault;
  1608. addr += 4;
  1609. regs->regs[i] = value;
  1610. }
  1611. LoadW(addr, value, res);
  1612. if (res)
  1613. goto fault;
  1614. regs->regs[31] = value;
  1615. goto success;
  1616. case mm_swm16_op:
  1617. reg = insn.mm16_m_format.rlist;
  1618. rvar = reg + 1;
  1619. if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
  1620. goto sigbus;
  1621. for (i = 16; rvar; rvar--, i++) {
  1622. value = regs->regs[i];
  1623. StoreW(addr, value, res);
  1624. if (res)
  1625. goto fault;
  1626. addr += 4;
  1627. }
  1628. value = regs->regs[31];
  1629. StoreW(addr, value, res);
  1630. if (res)
  1631. goto fault;
  1632. goto success;
  1633. }
  1634. goto sigbus;
  1635. case mm_lhu16_op:
  1636. reg = reg16to32[insn.mm16_rb_format.rt];
  1637. goto loadHWU;
  1638. case mm_lw16_op:
  1639. reg = reg16to32[insn.mm16_rb_format.rt];
  1640. goto loadW;
  1641. case mm_sh16_op:
  1642. reg = reg16to32st[insn.mm16_rb_format.rt];
  1643. goto storeHW;
  1644. case mm_sw16_op:
  1645. reg = reg16to32st[insn.mm16_rb_format.rt];
  1646. goto storeW;
  1647. case mm_lwsp16_op:
  1648. reg = insn.mm16_r5_format.rt;
  1649. goto loadW;
  1650. case mm_swsp16_op:
  1651. reg = insn.mm16_r5_format.rt;
  1652. goto storeW;
  1653. case mm_lwgp16_op:
  1654. reg = reg16to32[insn.mm16_r3_format.rt];
  1655. goto loadW;
  1656. default:
  1657. goto sigill;
  1658. }
  1659. loadHW:
  1660. if (!access_ok(VERIFY_READ, addr, 2))
  1661. goto sigbus;
  1662. LoadHW(addr, value, res);
  1663. if (res)
  1664. goto fault;
  1665. regs->regs[reg] = value;
  1666. goto success;
  1667. loadHWU:
  1668. if (!access_ok(VERIFY_READ, addr, 2))
  1669. goto sigbus;
  1670. LoadHWU(addr, value, res);
  1671. if (res)
  1672. goto fault;
  1673. regs->regs[reg] = value;
  1674. goto success;
  1675. loadW:
  1676. if (!access_ok(VERIFY_READ, addr, 4))
  1677. goto sigbus;
  1678. LoadW(addr, value, res);
  1679. if (res)
  1680. goto fault;
  1681. regs->regs[reg] = value;
  1682. goto success;
  1683. loadWU:
  1684. #ifdef CONFIG_64BIT
  1685. /*
  1686. * A 32-bit kernel might be running on a 64-bit processor. But
  1687. * if we're on a 32-bit processor and an i-cache incoherency
  1688. * or race makes us see a 64-bit instruction here the sdl/sdr
  1689. * would blow up, so for now we don't handle unaligned 64-bit
  1690. * instructions on 32-bit kernels.
  1691. */
  1692. if (!access_ok(VERIFY_READ, addr, 4))
  1693. goto sigbus;
  1694. LoadWU(addr, value, res);
  1695. if (res)
  1696. goto fault;
  1697. regs->regs[reg] = value;
  1698. goto success;
  1699. #endif /* CONFIG_64BIT */
  1700. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1701. goto sigill;
  1702. loadDW:
  1703. #ifdef CONFIG_64BIT
  1704. /*
  1705. * A 32-bit kernel might be running on a 64-bit processor. But
  1706. * if we're on a 32-bit processor and an i-cache incoherency
  1707. * or race makes us see a 64-bit instruction here the sdl/sdr
  1708. * would blow up, so for now we don't handle unaligned 64-bit
  1709. * instructions on 32-bit kernels.
  1710. */
  1711. if (!access_ok(VERIFY_READ, addr, 8))
  1712. goto sigbus;
  1713. LoadDW(addr, value, res);
  1714. if (res)
  1715. goto fault;
  1716. regs->regs[reg] = value;
  1717. goto success;
  1718. #endif /* CONFIG_64BIT */
  1719. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1720. goto sigill;
  1721. storeHW:
  1722. if (!access_ok(VERIFY_WRITE, addr, 2))
  1723. goto sigbus;
  1724. value = regs->regs[reg];
  1725. StoreHW(addr, value, res);
  1726. if (res)
  1727. goto fault;
  1728. goto success;
  1729. storeW:
  1730. if (!access_ok(VERIFY_WRITE, addr, 4))
  1731. goto sigbus;
  1732. value = regs->regs[reg];
  1733. StoreW(addr, value, res);
  1734. if (res)
  1735. goto fault;
  1736. goto success;
  1737. storeDW:
  1738. #ifdef CONFIG_64BIT
  1739. /*
  1740. * A 32-bit kernel might be running on a 64-bit processor. But
  1741. * if we're on a 32-bit processor and an i-cache incoherency
  1742. * or race makes us see a 64-bit instruction here the sdl/sdr
  1743. * would blow up, so for now we don't handle unaligned 64-bit
  1744. * instructions on 32-bit kernels.
  1745. */
  1746. if (!access_ok(VERIFY_WRITE, addr, 8))
  1747. goto sigbus;
  1748. value = regs->regs[reg];
  1749. StoreDW(addr, value, res);
  1750. if (res)
  1751. goto fault;
  1752. goto success;
  1753. #endif /* CONFIG_64BIT */
  1754. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1755. goto sigill;
  1756. success:
  1757. regs->cp0_epc = contpc; /* advance or branch */
  1758. #ifdef CONFIG_DEBUG_FS
  1759. unaligned_instructions++;
  1760. #endif
  1761. return;
  1762. fault:
  1763. /* roll back jump/branch */
  1764. regs->cp0_epc = origpc;
  1765. regs->regs[31] = orig31;
  1766. /* Did we have an exception handler installed? */
  1767. if (fixup_exception(regs))
  1768. return;
  1769. die_if_kernel("Unhandled kernel unaligned access", regs);
  1770. force_sig(SIGSEGV, current);
  1771. return;
  1772. sigbus:
  1773. die_if_kernel("Unhandled kernel unaligned access", regs);
  1774. force_sig(SIGBUS, current);
  1775. return;
  1776. sigill:
  1777. die_if_kernel
  1778. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1779. force_sig(SIGILL, current);
  1780. }
  1781. static void emulate_load_store_MIPS16e(struct pt_regs *regs, void __user * addr)
  1782. {
  1783. unsigned long value;
  1784. unsigned int res;
  1785. int reg;
  1786. unsigned long orig31;
  1787. u16 __user *pc16;
  1788. unsigned long origpc;
  1789. union mips16e_instruction mips16inst, oldinst;
  1790. origpc = regs->cp0_epc;
  1791. orig31 = regs->regs[31];
  1792. pc16 = (unsigned short __user *)msk_isa16_mode(origpc);
  1793. /*
  1794. * This load never faults.
  1795. */
  1796. __get_user(mips16inst.full, pc16);
  1797. oldinst = mips16inst;
  1798. /* skip EXTEND instruction */
  1799. if (mips16inst.ri.opcode == MIPS16e_extend_op) {
  1800. pc16++;
  1801. __get_user(mips16inst.full, pc16);
  1802. } else if (delay_slot(regs)) {
  1803. /* skip jump instructions */
  1804. /* JAL/JALX are 32 bits but have OPCODE in first short int */
  1805. if (mips16inst.ri.opcode == MIPS16e_jal_op)
  1806. pc16++;
  1807. pc16++;
  1808. if (get_user(mips16inst.full, pc16))
  1809. goto sigbus;
  1810. }
  1811. switch (mips16inst.ri.opcode) {
  1812. case MIPS16e_i64_op: /* I64 or RI64 instruction */
  1813. switch (mips16inst.i64.func) { /* I64/RI64 func field check */
  1814. case MIPS16e_ldpc_func:
  1815. case MIPS16e_ldsp_func:
  1816. reg = reg16to32[mips16inst.ri64.ry];
  1817. goto loadDW;
  1818. case MIPS16e_sdsp_func:
  1819. reg = reg16to32[mips16inst.ri64.ry];
  1820. goto writeDW;
  1821. case MIPS16e_sdrasp_func:
  1822. reg = 29; /* GPRSP */
  1823. goto writeDW;
  1824. }
  1825. goto sigbus;
  1826. case MIPS16e_swsp_op:
  1827. case MIPS16e_lwpc_op:
  1828. case MIPS16e_lwsp_op:
  1829. reg = reg16to32[mips16inst.ri.rx];
  1830. break;
  1831. case MIPS16e_i8_op:
  1832. if (mips16inst.i8.func != MIPS16e_swrasp_func)
  1833. goto sigbus;
  1834. reg = 29; /* GPRSP */
  1835. break;
  1836. default:
  1837. reg = reg16to32[mips16inst.rri.ry];
  1838. break;
  1839. }
  1840. switch (mips16inst.ri.opcode) {
  1841. case MIPS16e_lb_op:
  1842. case MIPS16e_lbu_op:
  1843. case MIPS16e_sb_op:
  1844. goto sigbus;
  1845. case MIPS16e_lh_op:
  1846. if (!access_ok(VERIFY_READ, addr, 2))
  1847. goto sigbus;
  1848. LoadHW(addr, value, res);
  1849. if (res)
  1850. goto fault;
  1851. MIPS16e_compute_return_epc(regs, &oldinst);
  1852. regs->regs[reg] = value;
  1853. break;
  1854. case MIPS16e_lhu_op:
  1855. if (!access_ok(VERIFY_READ, addr, 2))
  1856. goto sigbus;
  1857. LoadHWU(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_lw_op:
  1864. case MIPS16e_lwpc_op:
  1865. case MIPS16e_lwsp_op:
  1866. if (!access_ok(VERIFY_READ, addr, 4))
  1867. goto sigbus;
  1868. LoadW(addr, value, res);
  1869. if (res)
  1870. goto fault;
  1871. MIPS16e_compute_return_epc(regs, &oldinst);
  1872. regs->regs[reg] = value;
  1873. break;
  1874. case MIPS16e_lwu_op:
  1875. #ifdef CONFIG_64BIT
  1876. /*
  1877. * A 32-bit kernel might be running on a 64-bit processor. But
  1878. * if we're on a 32-bit processor and an i-cache incoherency
  1879. * or race makes us see a 64-bit instruction here the sdl/sdr
  1880. * would blow up, so for now we don't handle unaligned 64-bit
  1881. * instructions on 32-bit kernels.
  1882. */
  1883. if (!access_ok(VERIFY_READ, addr, 4))
  1884. goto sigbus;
  1885. LoadWU(addr, value, res);
  1886. if (res)
  1887. goto fault;
  1888. MIPS16e_compute_return_epc(regs, &oldinst);
  1889. regs->regs[reg] = value;
  1890. break;
  1891. #endif /* CONFIG_64BIT */
  1892. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1893. goto sigill;
  1894. case MIPS16e_ld_op:
  1895. loadDW:
  1896. #ifdef CONFIG_64BIT
  1897. /*
  1898. * A 32-bit kernel might be running on a 64-bit processor. But
  1899. * if we're on a 32-bit processor and an i-cache incoherency
  1900. * or race makes us see a 64-bit instruction here the sdl/sdr
  1901. * would blow up, so for now we don't handle unaligned 64-bit
  1902. * instructions on 32-bit kernels.
  1903. */
  1904. if (!access_ok(VERIFY_READ, addr, 8))
  1905. goto sigbus;
  1906. LoadDW(addr, value, res);
  1907. if (res)
  1908. goto fault;
  1909. MIPS16e_compute_return_epc(regs, &oldinst);
  1910. regs->regs[reg] = value;
  1911. break;
  1912. #endif /* CONFIG_64BIT */
  1913. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1914. goto sigill;
  1915. case MIPS16e_sh_op:
  1916. if (!access_ok(VERIFY_WRITE, addr, 2))
  1917. goto sigbus;
  1918. MIPS16e_compute_return_epc(regs, &oldinst);
  1919. value = regs->regs[reg];
  1920. StoreHW(addr, value, res);
  1921. if (res)
  1922. goto fault;
  1923. break;
  1924. case MIPS16e_sw_op:
  1925. case MIPS16e_swsp_op:
  1926. case MIPS16e_i8_op: /* actually - MIPS16e_swrasp_func */
  1927. if (!access_ok(VERIFY_WRITE, addr, 4))
  1928. goto sigbus;
  1929. MIPS16e_compute_return_epc(regs, &oldinst);
  1930. value = regs->regs[reg];
  1931. StoreW(addr, value, res);
  1932. if (res)
  1933. goto fault;
  1934. break;
  1935. case MIPS16e_sd_op:
  1936. writeDW:
  1937. #ifdef CONFIG_64BIT
  1938. /*
  1939. * A 32-bit kernel might be running on a 64-bit processor. But
  1940. * if we're on a 32-bit processor and an i-cache incoherency
  1941. * or race makes us see a 64-bit instruction here the sdl/sdr
  1942. * would blow up, so for now we don't handle unaligned 64-bit
  1943. * instructions on 32-bit kernels.
  1944. */
  1945. if (!access_ok(VERIFY_WRITE, addr, 8))
  1946. goto sigbus;
  1947. MIPS16e_compute_return_epc(regs, &oldinst);
  1948. value = regs->regs[reg];
  1949. StoreDW(addr, value, res);
  1950. if (res)
  1951. goto fault;
  1952. break;
  1953. #endif /* CONFIG_64BIT */
  1954. /* Cannot handle 64-bit instructions in 32-bit kernel */
  1955. goto sigill;
  1956. default:
  1957. /*
  1958. * Pheeee... We encountered an yet unknown instruction or
  1959. * cache coherence problem. Die sucker, die ...
  1960. */
  1961. goto sigill;
  1962. }
  1963. #ifdef CONFIG_DEBUG_FS
  1964. unaligned_instructions++;
  1965. #endif
  1966. return;
  1967. fault:
  1968. /* roll back jump/branch */
  1969. regs->cp0_epc = origpc;
  1970. regs->regs[31] = orig31;
  1971. /* Did we have an exception handler installed? */
  1972. if (fixup_exception(regs))
  1973. return;
  1974. die_if_kernel("Unhandled kernel unaligned access", regs);
  1975. force_sig(SIGSEGV, current);
  1976. return;
  1977. sigbus:
  1978. die_if_kernel("Unhandled kernel unaligned access", regs);
  1979. force_sig(SIGBUS, current);
  1980. return;
  1981. sigill:
  1982. die_if_kernel
  1983. ("Unhandled kernel unaligned access or invalid instruction", regs);
  1984. force_sig(SIGILL, current);
  1985. }
  1986. asmlinkage void do_ade(struct pt_regs *regs)
  1987. {
  1988. enum ctx_state prev_state;
  1989. unsigned int __user *pc;
  1990. mm_segment_t seg;
  1991. prev_state = exception_enter();
  1992. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
  1993. 1, regs, regs->cp0_badvaddr);
  1994. /*
  1995. * Did we catch a fault trying to load an instruction?
  1996. */
  1997. if (regs->cp0_badvaddr == regs->cp0_epc)
  1998. goto sigbus;
  1999. if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
  2000. goto sigbus;
  2001. if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
  2002. goto sigbus;
  2003. /*
  2004. * Do branch emulation only if we didn't forward the exception.
  2005. * This is all so but ugly ...
  2006. */
  2007. /*
  2008. * Are we running in microMIPS mode?
  2009. */
  2010. if (get_isa16_mode(regs->cp0_epc)) {
  2011. /*
  2012. * Did we catch a fault trying to load an instruction in
  2013. * 16-bit mode?
  2014. */
  2015. if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
  2016. goto sigbus;
  2017. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  2018. show_registers(regs);
  2019. if (cpu_has_mmips) {
  2020. seg = get_fs();
  2021. if (!user_mode(regs))
  2022. set_fs(KERNEL_DS);
  2023. emulate_load_store_microMIPS(regs,
  2024. (void __user *)regs->cp0_badvaddr);
  2025. set_fs(seg);
  2026. return;
  2027. }
  2028. if (cpu_has_mips16) {
  2029. seg = get_fs();
  2030. if (!user_mode(regs))
  2031. set_fs(KERNEL_DS);
  2032. emulate_load_store_MIPS16e(regs,
  2033. (void __user *)regs->cp0_badvaddr);
  2034. set_fs(seg);
  2035. return;
  2036. }
  2037. goto sigbus;
  2038. }
  2039. if (unaligned_action == UNALIGNED_ACTION_SHOW)
  2040. show_registers(regs);
  2041. pc = (unsigned int __user *)exception_epc(regs);
  2042. seg = get_fs();
  2043. if (!user_mode(regs))
  2044. set_fs(KERNEL_DS);
  2045. emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
  2046. set_fs(seg);
  2047. return;
  2048. sigbus:
  2049. die_if_kernel("Kernel unaligned instruction access", regs);
  2050. force_sig(SIGBUS, current);
  2051. /*
  2052. * XXX On return from the signal handler we should advance the epc
  2053. */
  2054. exception_exit(prev_state);
  2055. }
  2056. #ifdef CONFIG_DEBUG_FS
  2057. static int __init debugfs_unaligned(void)
  2058. {
  2059. struct dentry *d;
  2060. if (!mips_debugfs_dir)
  2061. return -ENODEV;
  2062. d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
  2063. mips_debugfs_dir, &unaligned_instructions);
  2064. if (!d)
  2065. return -ENOMEM;
  2066. d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
  2067. mips_debugfs_dir, &unaligned_action);
  2068. if (!d)
  2069. return -ENOMEM;
  2070. return 0;
  2071. }
  2072. arch_initcall(debugfs_unaligned);
  2073. #endif