cp1emu.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622
  1. /*
  2. * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
  3. *
  4. * MIPS floating point support
  5. * Copyright (C) 1994-2000 Algorithmics Ltd.
  6. *
  7. * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
  8. * Copyright (C) 2000 MIPS Technologies, Inc.
  9. *
  10. * This program is free software; you can distribute it and/or modify it
  11. * under the terms of the GNU General Public License (Version 2) as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * A complete emulator for MIPS coprocessor 1 instructions. This is
  24. * required for #float(switch) or #float(trap), where it catches all
  25. * COP1 instructions via the "CoProcessor Unusable" exception.
  26. *
  27. * More surprisingly it is also required for #float(ieee), to help out
  28. * the hardware FPU at the boundaries of the IEEE-754 representation
  29. * (denormalised values, infinities, underflow, etc). It is made
  30. * quite nasty because emulation of some non-COP1 instructions is
  31. * required, e.g. in branch delay slots.
  32. *
  33. * Note if you know that you won't have an FPU, then you'll get much
  34. * better performance by compiling with -msoft-float!
  35. */
  36. #include <linux/sched.h>
  37. #include <linux/debugfs.h>
  38. #include <linux/percpu-defs.h>
  39. #include <linux/perf_event.h>
  40. #include <asm/branch.h>
  41. #include <asm/inst.h>
  42. #include <asm/ptrace.h>
  43. #include <asm/signal.h>
  44. #include <linux/uaccess.h>
  45. #include <asm/cpu-info.h>
  46. #include <asm/processor.h>
  47. #include <asm/fpu_emulator.h>
  48. #include <asm/fpu.h>
  49. #include <asm/mips-r2-to-r6-emul.h>
  50. #include "ieee754.h"
  51. /* Function which emulates a floating point instruction. */
  52. static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
  53. mips_instruction);
  54. static int fpux_emu(struct pt_regs *,
  55. struct mips_fpu_struct *, mips_instruction, void *__user *);
  56. /* Control registers */
  57. #define FPCREG_RID 0 /* $0 = revision id */
  58. #define FPCREG_FCCR 25 /* $25 = fccr */
  59. #define FPCREG_FEXR 26 /* $26 = fexr */
  60. #define FPCREG_FENR 28 /* $28 = fenr */
  61. #define FPCREG_CSR 31 /* $31 = csr */
  62. /* convert condition code register number to csr bit */
  63. const unsigned int fpucondbit[8] = {
  64. FPU_CSR_COND,
  65. FPU_CSR_COND1,
  66. FPU_CSR_COND2,
  67. FPU_CSR_COND3,
  68. FPU_CSR_COND4,
  69. FPU_CSR_COND5,
  70. FPU_CSR_COND6,
  71. FPU_CSR_COND7
  72. };
  73. /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
  74. static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
  75. static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
  76. static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
  77. static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
  78. /*
  79. * This functions translates a 32-bit microMIPS instruction
  80. * into a 32-bit MIPS32 instruction. Returns 0 on success
  81. * and SIGILL otherwise.
  82. */
  83. static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
  84. {
  85. union mips_instruction insn = *insn_ptr;
  86. union mips_instruction mips32_insn = insn;
  87. int func, fmt, op;
  88. switch (insn.mm_i_format.opcode) {
  89. case mm_ldc132_op:
  90. mips32_insn.mm_i_format.opcode = ldc1_op;
  91. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  92. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  93. break;
  94. case mm_lwc132_op:
  95. mips32_insn.mm_i_format.opcode = lwc1_op;
  96. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  97. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  98. break;
  99. case mm_sdc132_op:
  100. mips32_insn.mm_i_format.opcode = sdc1_op;
  101. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  102. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  103. break;
  104. case mm_swc132_op:
  105. mips32_insn.mm_i_format.opcode = swc1_op;
  106. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  107. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  108. break;
  109. case mm_pool32i_op:
  110. /* NOTE: offset is << by 1 if in microMIPS mode. */
  111. if ((insn.mm_i_format.rt == mm_bc1f_op) ||
  112. (insn.mm_i_format.rt == mm_bc1t_op)) {
  113. mips32_insn.fb_format.opcode = cop1_op;
  114. mips32_insn.fb_format.bc = bc_op;
  115. mips32_insn.fb_format.flag =
  116. (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
  117. } else
  118. return SIGILL;
  119. break;
  120. case mm_pool32f_op:
  121. switch (insn.mm_fp0_format.func) {
  122. case mm_32f_01_op:
  123. case mm_32f_11_op:
  124. case mm_32f_02_op:
  125. case mm_32f_12_op:
  126. case mm_32f_41_op:
  127. case mm_32f_51_op:
  128. case mm_32f_42_op:
  129. case mm_32f_52_op:
  130. op = insn.mm_fp0_format.func;
  131. if (op == mm_32f_01_op)
  132. func = madd_s_op;
  133. else if (op == mm_32f_11_op)
  134. func = madd_d_op;
  135. else if (op == mm_32f_02_op)
  136. func = nmadd_s_op;
  137. else if (op == mm_32f_12_op)
  138. func = nmadd_d_op;
  139. else if (op == mm_32f_41_op)
  140. func = msub_s_op;
  141. else if (op == mm_32f_51_op)
  142. func = msub_d_op;
  143. else if (op == mm_32f_42_op)
  144. func = nmsub_s_op;
  145. else
  146. func = nmsub_d_op;
  147. mips32_insn.fp6_format.opcode = cop1x_op;
  148. mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
  149. mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
  150. mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
  151. mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
  152. mips32_insn.fp6_format.func = func;
  153. break;
  154. case mm_32f_10_op:
  155. func = -1; /* Invalid */
  156. op = insn.mm_fp5_format.op & 0x7;
  157. if (op == mm_ldxc1_op)
  158. func = ldxc1_op;
  159. else if (op == mm_sdxc1_op)
  160. func = sdxc1_op;
  161. else if (op == mm_lwxc1_op)
  162. func = lwxc1_op;
  163. else if (op == mm_swxc1_op)
  164. func = swxc1_op;
  165. if (func != -1) {
  166. mips32_insn.r_format.opcode = cop1x_op;
  167. mips32_insn.r_format.rs =
  168. insn.mm_fp5_format.base;
  169. mips32_insn.r_format.rt =
  170. insn.mm_fp5_format.index;
  171. mips32_insn.r_format.rd = 0;
  172. mips32_insn.r_format.re = insn.mm_fp5_format.fd;
  173. mips32_insn.r_format.func = func;
  174. } else
  175. return SIGILL;
  176. break;
  177. case mm_32f_40_op:
  178. op = -1; /* Invalid */
  179. if (insn.mm_fp2_format.op == mm_fmovt_op)
  180. op = 1;
  181. else if (insn.mm_fp2_format.op == mm_fmovf_op)
  182. op = 0;
  183. if (op != -1) {
  184. mips32_insn.fp0_format.opcode = cop1_op;
  185. mips32_insn.fp0_format.fmt =
  186. sdps_format[insn.mm_fp2_format.fmt];
  187. mips32_insn.fp0_format.ft =
  188. (insn.mm_fp2_format.cc<<2) + op;
  189. mips32_insn.fp0_format.fs =
  190. insn.mm_fp2_format.fs;
  191. mips32_insn.fp0_format.fd =
  192. insn.mm_fp2_format.fd;
  193. mips32_insn.fp0_format.func = fmovc_op;
  194. } else
  195. return SIGILL;
  196. break;
  197. case mm_32f_60_op:
  198. func = -1; /* Invalid */
  199. if (insn.mm_fp0_format.op == mm_fadd_op)
  200. func = fadd_op;
  201. else if (insn.mm_fp0_format.op == mm_fsub_op)
  202. func = fsub_op;
  203. else if (insn.mm_fp0_format.op == mm_fmul_op)
  204. func = fmul_op;
  205. else if (insn.mm_fp0_format.op == mm_fdiv_op)
  206. func = fdiv_op;
  207. if (func != -1) {
  208. mips32_insn.fp0_format.opcode = cop1_op;
  209. mips32_insn.fp0_format.fmt =
  210. sdps_format[insn.mm_fp0_format.fmt];
  211. mips32_insn.fp0_format.ft =
  212. insn.mm_fp0_format.ft;
  213. mips32_insn.fp0_format.fs =
  214. insn.mm_fp0_format.fs;
  215. mips32_insn.fp0_format.fd =
  216. insn.mm_fp0_format.fd;
  217. mips32_insn.fp0_format.func = func;
  218. } else
  219. return SIGILL;
  220. break;
  221. case mm_32f_70_op:
  222. func = -1; /* Invalid */
  223. if (insn.mm_fp0_format.op == mm_fmovn_op)
  224. func = fmovn_op;
  225. else if (insn.mm_fp0_format.op == mm_fmovz_op)
  226. func = fmovz_op;
  227. if (func != -1) {
  228. mips32_insn.fp0_format.opcode = cop1_op;
  229. mips32_insn.fp0_format.fmt =
  230. sdps_format[insn.mm_fp0_format.fmt];
  231. mips32_insn.fp0_format.ft =
  232. insn.mm_fp0_format.ft;
  233. mips32_insn.fp0_format.fs =
  234. insn.mm_fp0_format.fs;
  235. mips32_insn.fp0_format.fd =
  236. insn.mm_fp0_format.fd;
  237. mips32_insn.fp0_format.func = func;
  238. } else
  239. return SIGILL;
  240. break;
  241. case mm_32f_73_op: /* POOL32FXF */
  242. switch (insn.mm_fp1_format.op) {
  243. case mm_movf0_op:
  244. case mm_movf1_op:
  245. case mm_movt0_op:
  246. case mm_movt1_op:
  247. if ((insn.mm_fp1_format.op & 0x7f) ==
  248. mm_movf0_op)
  249. op = 0;
  250. else
  251. op = 1;
  252. mips32_insn.r_format.opcode = spec_op;
  253. mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
  254. mips32_insn.r_format.rt =
  255. (insn.mm_fp4_format.cc << 2) + op;
  256. mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
  257. mips32_insn.r_format.re = 0;
  258. mips32_insn.r_format.func = movc_op;
  259. break;
  260. case mm_fcvtd0_op:
  261. case mm_fcvtd1_op:
  262. case mm_fcvts0_op:
  263. case mm_fcvts1_op:
  264. if ((insn.mm_fp1_format.op & 0x7f) ==
  265. mm_fcvtd0_op) {
  266. func = fcvtd_op;
  267. fmt = swl_format[insn.mm_fp3_format.fmt];
  268. } else {
  269. func = fcvts_op;
  270. fmt = dwl_format[insn.mm_fp3_format.fmt];
  271. }
  272. mips32_insn.fp0_format.opcode = cop1_op;
  273. mips32_insn.fp0_format.fmt = fmt;
  274. mips32_insn.fp0_format.ft = 0;
  275. mips32_insn.fp0_format.fs =
  276. insn.mm_fp3_format.fs;
  277. mips32_insn.fp0_format.fd =
  278. insn.mm_fp3_format.rt;
  279. mips32_insn.fp0_format.func = func;
  280. break;
  281. case mm_fmov0_op:
  282. case mm_fmov1_op:
  283. case mm_fabs0_op:
  284. case mm_fabs1_op:
  285. case mm_fneg0_op:
  286. case mm_fneg1_op:
  287. if ((insn.mm_fp1_format.op & 0x7f) ==
  288. mm_fmov0_op)
  289. func = fmov_op;
  290. else if ((insn.mm_fp1_format.op & 0x7f) ==
  291. mm_fabs0_op)
  292. func = fabs_op;
  293. else
  294. func = fneg_op;
  295. mips32_insn.fp0_format.opcode = cop1_op;
  296. mips32_insn.fp0_format.fmt =
  297. sdps_format[insn.mm_fp3_format.fmt];
  298. mips32_insn.fp0_format.ft = 0;
  299. mips32_insn.fp0_format.fs =
  300. insn.mm_fp3_format.fs;
  301. mips32_insn.fp0_format.fd =
  302. insn.mm_fp3_format.rt;
  303. mips32_insn.fp0_format.func = func;
  304. break;
  305. case mm_ffloorl_op:
  306. case mm_ffloorw_op:
  307. case mm_fceill_op:
  308. case mm_fceilw_op:
  309. case mm_ftruncl_op:
  310. case mm_ftruncw_op:
  311. case mm_froundl_op:
  312. case mm_froundw_op:
  313. case mm_fcvtl_op:
  314. case mm_fcvtw_op:
  315. if (insn.mm_fp1_format.op == mm_ffloorl_op)
  316. func = ffloorl_op;
  317. else if (insn.mm_fp1_format.op == mm_ffloorw_op)
  318. func = ffloor_op;
  319. else if (insn.mm_fp1_format.op == mm_fceill_op)
  320. func = fceill_op;
  321. else if (insn.mm_fp1_format.op == mm_fceilw_op)
  322. func = fceil_op;
  323. else if (insn.mm_fp1_format.op == mm_ftruncl_op)
  324. func = ftruncl_op;
  325. else if (insn.mm_fp1_format.op == mm_ftruncw_op)
  326. func = ftrunc_op;
  327. else if (insn.mm_fp1_format.op == mm_froundl_op)
  328. func = froundl_op;
  329. else if (insn.mm_fp1_format.op == mm_froundw_op)
  330. func = fround_op;
  331. else if (insn.mm_fp1_format.op == mm_fcvtl_op)
  332. func = fcvtl_op;
  333. else
  334. func = fcvtw_op;
  335. mips32_insn.fp0_format.opcode = cop1_op;
  336. mips32_insn.fp0_format.fmt =
  337. sd_format[insn.mm_fp1_format.fmt];
  338. mips32_insn.fp0_format.ft = 0;
  339. mips32_insn.fp0_format.fs =
  340. insn.mm_fp1_format.fs;
  341. mips32_insn.fp0_format.fd =
  342. insn.mm_fp1_format.rt;
  343. mips32_insn.fp0_format.func = func;
  344. break;
  345. case mm_frsqrt_op:
  346. case mm_fsqrt_op:
  347. case mm_frecip_op:
  348. if (insn.mm_fp1_format.op == mm_frsqrt_op)
  349. func = frsqrt_op;
  350. else if (insn.mm_fp1_format.op == mm_fsqrt_op)
  351. func = fsqrt_op;
  352. else
  353. func = frecip_op;
  354. mips32_insn.fp0_format.opcode = cop1_op;
  355. mips32_insn.fp0_format.fmt =
  356. sdps_format[insn.mm_fp1_format.fmt];
  357. mips32_insn.fp0_format.ft = 0;
  358. mips32_insn.fp0_format.fs =
  359. insn.mm_fp1_format.fs;
  360. mips32_insn.fp0_format.fd =
  361. insn.mm_fp1_format.rt;
  362. mips32_insn.fp0_format.func = func;
  363. break;
  364. case mm_mfc1_op:
  365. case mm_mtc1_op:
  366. case mm_cfc1_op:
  367. case mm_ctc1_op:
  368. case mm_mfhc1_op:
  369. case mm_mthc1_op:
  370. if (insn.mm_fp1_format.op == mm_mfc1_op)
  371. op = mfc_op;
  372. else if (insn.mm_fp1_format.op == mm_mtc1_op)
  373. op = mtc_op;
  374. else if (insn.mm_fp1_format.op == mm_cfc1_op)
  375. op = cfc_op;
  376. else if (insn.mm_fp1_format.op == mm_ctc1_op)
  377. op = ctc_op;
  378. else if (insn.mm_fp1_format.op == mm_mfhc1_op)
  379. op = mfhc_op;
  380. else
  381. op = mthc_op;
  382. mips32_insn.fp1_format.opcode = cop1_op;
  383. mips32_insn.fp1_format.op = op;
  384. mips32_insn.fp1_format.rt =
  385. insn.mm_fp1_format.rt;
  386. mips32_insn.fp1_format.fs =
  387. insn.mm_fp1_format.fs;
  388. mips32_insn.fp1_format.fd = 0;
  389. mips32_insn.fp1_format.func = 0;
  390. break;
  391. default:
  392. return SIGILL;
  393. }
  394. break;
  395. case mm_32f_74_op: /* c.cond.fmt */
  396. mips32_insn.fp0_format.opcode = cop1_op;
  397. mips32_insn.fp0_format.fmt =
  398. sdps_format[insn.mm_fp4_format.fmt];
  399. mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
  400. mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
  401. mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
  402. mips32_insn.fp0_format.func =
  403. insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
  404. break;
  405. default:
  406. return SIGILL;
  407. }
  408. break;
  409. default:
  410. return SIGILL;
  411. }
  412. *insn_ptr = mips32_insn;
  413. return 0;
  414. }
  415. /*
  416. * Redundant with logic already in kernel/branch.c,
  417. * embedded in compute_return_epc. At some point,
  418. * a single subroutine should be used across both
  419. * modules.
  420. */
  421. int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
  422. unsigned long *contpc)
  423. {
  424. union mips_instruction insn = (union mips_instruction)dec_insn.insn;
  425. unsigned int fcr31;
  426. unsigned int bit = 0;
  427. unsigned int bit0;
  428. union fpureg *fpr;
  429. switch (insn.i_format.opcode) {
  430. case spec_op:
  431. switch (insn.r_format.func) {
  432. case jalr_op:
  433. if (insn.r_format.rd != 0) {
  434. regs->regs[insn.r_format.rd] =
  435. regs->cp0_epc + dec_insn.pc_inc +
  436. dec_insn.next_pc_inc;
  437. }
  438. /* Fall through */
  439. case jr_op:
  440. /* For R6, JR already emulated in jalr_op */
  441. if (NO_R6EMU && insn.r_format.func == jr_op)
  442. break;
  443. *contpc = regs->regs[insn.r_format.rs];
  444. return 1;
  445. }
  446. break;
  447. case bcond_op:
  448. switch (insn.i_format.rt) {
  449. case bltzal_op:
  450. case bltzall_op:
  451. if (NO_R6EMU && (insn.i_format.rs ||
  452. insn.i_format.rt == bltzall_op))
  453. break;
  454. regs->regs[31] = regs->cp0_epc +
  455. dec_insn.pc_inc +
  456. dec_insn.next_pc_inc;
  457. /* Fall through */
  458. case bltzl_op:
  459. if (NO_R6EMU)
  460. break;
  461. case bltz_op:
  462. if ((long)regs->regs[insn.i_format.rs] < 0)
  463. *contpc = regs->cp0_epc +
  464. dec_insn.pc_inc +
  465. (insn.i_format.simmediate << 2);
  466. else
  467. *contpc = regs->cp0_epc +
  468. dec_insn.pc_inc +
  469. dec_insn.next_pc_inc;
  470. return 1;
  471. case bgezal_op:
  472. case bgezall_op:
  473. if (NO_R6EMU && (insn.i_format.rs ||
  474. insn.i_format.rt == bgezall_op))
  475. break;
  476. regs->regs[31] = regs->cp0_epc +
  477. dec_insn.pc_inc +
  478. dec_insn.next_pc_inc;
  479. /* Fall through */
  480. case bgezl_op:
  481. if (NO_R6EMU)
  482. break;
  483. case bgez_op:
  484. if ((long)regs->regs[insn.i_format.rs] >= 0)
  485. *contpc = regs->cp0_epc +
  486. dec_insn.pc_inc +
  487. (insn.i_format.simmediate << 2);
  488. else
  489. *contpc = regs->cp0_epc +
  490. dec_insn.pc_inc +
  491. dec_insn.next_pc_inc;
  492. return 1;
  493. }
  494. break;
  495. case jalx_op:
  496. set_isa16_mode(bit);
  497. case jal_op:
  498. regs->regs[31] = regs->cp0_epc +
  499. dec_insn.pc_inc +
  500. dec_insn.next_pc_inc;
  501. /* Fall through */
  502. case j_op:
  503. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  504. *contpc >>= 28;
  505. *contpc <<= 28;
  506. *contpc |= (insn.j_format.target << 2);
  507. /* Set microMIPS mode bit: XOR for jalx. */
  508. *contpc ^= bit;
  509. return 1;
  510. case beql_op:
  511. if (NO_R6EMU)
  512. break;
  513. case beq_op:
  514. if (regs->regs[insn.i_format.rs] ==
  515. regs->regs[insn.i_format.rt])
  516. *contpc = regs->cp0_epc +
  517. dec_insn.pc_inc +
  518. (insn.i_format.simmediate << 2);
  519. else
  520. *contpc = regs->cp0_epc +
  521. dec_insn.pc_inc +
  522. dec_insn.next_pc_inc;
  523. return 1;
  524. case bnel_op:
  525. if (NO_R6EMU)
  526. break;
  527. case bne_op:
  528. if (regs->regs[insn.i_format.rs] !=
  529. regs->regs[insn.i_format.rt])
  530. *contpc = regs->cp0_epc +
  531. dec_insn.pc_inc +
  532. (insn.i_format.simmediate << 2);
  533. else
  534. *contpc = regs->cp0_epc +
  535. dec_insn.pc_inc +
  536. dec_insn.next_pc_inc;
  537. return 1;
  538. case blezl_op:
  539. if (!insn.i_format.rt && NO_R6EMU)
  540. break;
  541. case blez_op:
  542. /*
  543. * Compact branches for R6 for the
  544. * blez and blezl opcodes.
  545. * BLEZ | rs = 0 | rt != 0 == BLEZALC
  546. * BLEZ | rs = rt != 0 == BGEZALC
  547. * BLEZ | rs != 0 | rt != 0 == BGEUC
  548. * BLEZL | rs = 0 | rt != 0 == BLEZC
  549. * BLEZL | rs = rt != 0 == BGEZC
  550. * BLEZL | rs != 0 | rt != 0 == BGEC
  551. *
  552. * For real BLEZ{,L}, rt is always 0.
  553. */
  554. if (cpu_has_mips_r6 && insn.i_format.rt) {
  555. if ((insn.i_format.opcode == blez_op) &&
  556. ((!insn.i_format.rs && insn.i_format.rt) ||
  557. (insn.i_format.rs == insn.i_format.rt)))
  558. regs->regs[31] = regs->cp0_epc +
  559. dec_insn.pc_inc;
  560. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  561. dec_insn.next_pc_inc;
  562. return 1;
  563. }
  564. if ((long)regs->regs[insn.i_format.rs] <= 0)
  565. *contpc = regs->cp0_epc +
  566. dec_insn.pc_inc +
  567. (insn.i_format.simmediate << 2);
  568. else
  569. *contpc = regs->cp0_epc +
  570. dec_insn.pc_inc +
  571. dec_insn.next_pc_inc;
  572. return 1;
  573. case bgtzl_op:
  574. if (!insn.i_format.rt && NO_R6EMU)
  575. break;
  576. case bgtz_op:
  577. /*
  578. * Compact branches for R6 for the
  579. * bgtz and bgtzl opcodes.
  580. * BGTZ | rs = 0 | rt != 0 == BGTZALC
  581. * BGTZ | rs = rt != 0 == BLTZALC
  582. * BGTZ | rs != 0 | rt != 0 == BLTUC
  583. * BGTZL | rs = 0 | rt != 0 == BGTZC
  584. * BGTZL | rs = rt != 0 == BLTZC
  585. * BGTZL | rs != 0 | rt != 0 == BLTC
  586. *
  587. * *ZALC varint for BGTZ &&& rt != 0
  588. * For real GTZ{,L}, rt is always 0.
  589. */
  590. if (cpu_has_mips_r6 && insn.i_format.rt) {
  591. if ((insn.i_format.opcode == blez_op) &&
  592. ((!insn.i_format.rs && insn.i_format.rt) ||
  593. (insn.i_format.rs == insn.i_format.rt)))
  594. regs->regs[31] = regs->cp0_epc +
  595. dec_insn.pc_inc;
  596. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  597. dec_insn.next_pc_inc;
  598. return 1;
  599. }
  600. if ((long)regs->regs[insn.i_format.rs] > 0)
  601. *contpc = regs->cp0_epc +
  602. dec_insn.pc_inc +
  603. (insn.i_format.simmediate << 2);
  604. else
  605. *contpc = regs->cp0_epc +
  606. dec_insn.pc_inc +
  607. dec_insn.next_pc_inc;
  608. return 1;
  609. case pop10_op:
  610. case pop30_op:
  611. if (!cpu_has_mips_r6)
  612. break;
  613. if (insn.i_format.rt && !insn.i_format.rs)
  614. regs->regs[31] = regs->cp0_epc + 4;
  615. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  616. dec_insn.next_pc_inc;
  617. return 1;
  618. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  619. case lwc2_op: /* This is bbit0 on Octeon */
  620. if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
  621. *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
  622. else
  623. *contpc = regs->cp0_epc + 8;
  624. return 1;
  625. case ldc2_op: /* This is bbit032 on Octeon */
  626. if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
  627. *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
  628. else
  629. *contpc = regs->cp0_epc + 8;
  630. return 1;
  631. case swc2_op: /* This is bbit1 on Octeon */
  632. if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
  633. *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
  634. else
  635. *contpc = regs->cp0_epc + 8;
  636. return 1;
  637. case sdc2_op: /* This is bbit132 on Octeon */
  638. if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
  639. *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
  640. else
  641. *contpc = regs->cp0_epc + 8;
  642. return 1;
  643. #else
  644. case bc6_op:
  645. /*
  646. * Only valid for MIPS R6 but we can still end up
  647. * here from a broken userland so just tell emulator
  648. * this is not a branch and let it break later on.
  649. */
  650. if (!cpu_has_mips_r6)
  651. break;
  652. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  653. dec_insn.next_pc_inc;
  654. return 1;
  655. case balc6_op:
  656. if (!cpu_has_mips_r6)
  657. break;
  658. regs->regs[31] = regs->cp0_epc + 4;
  659. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  660. dec_insn.next_pc_inc;
  661. return 1;
  662. case pop66_op:
  663. if (!cpu_has_mips_r6)
  664. break;
  665. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  666. dec_insn.next_pc_inc;
  667. return 1;
  668. case pop76_op:
  669. if (!cpu_has_mips_r6)
  670. break;
  671. if (!insn.i_format.rs)
  672. regs->regs[31] = regs->cp0_epc + 4;
  673. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  674. dec_insn.next_pc_inc;
  675. return 1;
  676. #endif
  677. case cop0_op:
  678. case cop1_op:
  679. /* Need to check for R6 bc1nez and bc1eqz branches */
  680. if (cpu_has_mips_r6 &&
  681. ((insn.i_format.rs == bc1eqz_op) ||
  682. (insn.i_format.rs == bc1nez_op))) {
  683. bit = 0;
  684. fpr = &current->thread.fpu.fpr[insn.i_format.rt];
  685. bit0 = get_fpr32(fpr, 0) & 0x1;
  686. switch (insn.i_format.rs) {
  687. case bc1eqz_op:
  688. bit = bit0 == 0;
  689. break;
  690. case bc1nez_op:
  691. bit = bit0 != 0;
  692. break;
  693. }
  694. if (bit)
  695. *contpc = regs->cp0_epc +
  696. dec_insn.pc_inc +
  697. (insn.i_format.simmediate << 2);
  698. else
  699. *contpc = regs->cp0_epc +
  700. dec_insn.pc_inc +
  701. dec_insn.next_pc_inc;
  702. return 1;
  703. }
  704. /* R2/R6 compatible cop1 instruction. Fall through */
  705. case cop2_op:
  706. case cop1x_op:
  707. if (insn.i_format.rs == bc_op) {
  708. preempt_disable();
  709. if (is_fpu_owner())
  710. fcr31 = read_32bit_cp1_register(CP1_STATUS);
  711. else
  712. fcr31 = current->thread.fpu.fcr31;
  713. preempt_enable();
  714. bit = (insn.i_format.rt >> 2);
  715. bit += (bit != 0);
  716. bit += 23;
  717. switch (insn.i_format.rt & 3) {
  718. case 0: /* bc1f */
  719. case 2: /* bc1fl */
  720. if (~fcr31 & (1 << bit))
  721. *contpc = regs->cp0_epc +
  722. dec_insn.pc_inc +
  723. (insn.i_format.simmediate << 2);
  724. else
  725. *contpc = regs->cp0_epc +
  726. dec_insn.pc_inc +
  727. dec_insn.next_pc_inc;
  728. return 1;
  729. case 1: /* bc1t */
  730. case 3: /* bc1tl */
  731. if (fcr31 & (1 << bit))
  732. *contpc = regs->cp0_epc +
  733. dec_insn.pc_inc +
  734. (insn.i_format.simmediate << 2);
  735. else
  736. *contpc = regs->cp0_epc +
  737. dec_insn.pc_inc +
  738. dec_insn.next_pc_inc;
  739. return 1;
  740. }
  741. }
  742. break;
  743. }
  744. return 0;
  745. }
  746. /*
  747. * In the Linux kernel, we support selection of FPR format on the
  748. * basis of the Status.FR bit. If an FPU is not present, the FR bit
  749. * is hardwired to zero, which would imply a 32-bit FPU even for
  750. * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
  751. * FPU emu is slow and bulky and optimizing this function offers fairly
  752. * sizeable benefits so we try to be clever and make this function return
  753. * a constant whenever possible, that is on 64-bit kernels without O32
  754. * compatibility enabled and on 32-bit without 64-bit FPU support.
  755. */
  756. static inline int cop1_64bit(struct pt_regs *xcp)
  757. {
  758. if (IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_MIPS32_O32))
  759. return 1;
  760. else if (IS_ENABLED(CONFIG_32BIT) &&
  761. !IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
  762. return 0;
  763. return !test_thread_flag(TIF_32BIT_FPREGS);
  764. }
  765. static inline bool hybrid_fprs(void)
  766. {
  767. return test_thread_flag(TIF_HYBRID_FPREGS);
  768. }
  769. #define SIFROMREG(si, x) \
  770. do { \
  771. if (cop1_64bit(xcp) && !hybrid_fprs()) \
  772. (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
  773. else \
  774. (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
  775. } while (0)
  776. #define SITOREG(si, x) \
  777. do { \
  778. if (cop1_64bit(xcp) && !hybrid_fprs()) { \
  779. unsigned i; \
  780. set_fpr32(&ctx->fpr[x], 0, si); \
  781. for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
  782. set_fpr32(&ctx->fpr[x], i, 0); \
  783. } else { \
  784. set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
  785. } \
  786. } while (0)
  787. #define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
  788. #define SITOHREG(si, x) \
  789. do { \
  790. unsigned i; \
  791. set_fpr32(&ctx->fpr[x], 1, si); \
  792. for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
  793. set_fpr32(&ctx->fpr[x], i, 0); \
  794. } while (0)
  795. #define DIFROMREG(di, x) \
  796. ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
  797. #define DITOREG(di, x) \
  798. do { \
  799. unsigned fpr, i; \
  800. fpr = (x) & ~(cop1_64bit(xcp) == 0); \
  801. set_fpr64(&ctx->fpr[fpr], 0, di); \
  802. for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
  803. set_fpr64(&ctx->fpr[fpr], i, 0); \
  804. } while (0)
  805. #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
  806. #define SPTOREG(sp, x) SITOREG((sp).bits, x)
  807. #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
  808. #define DPTOREG(dp, x) DITOREG((dp).bits, x)
  809. /*
  810. * Emulate a CFC1 instruction.
  811. */
  812. static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  813. mips_instruction ir)
  814. {
  815. u32 fcr31 = ctx->fcr31;
  816. u32 value = 0;
  817. switch (MIPSInst_RD(ir)) {
  818. case FPCREG_CSR:
  819. value = fcr31;
  820. pr_debug("%p gpr[%d]<-csr=%08x\n",
  821. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  822. break;
  823. case FPCREG_FENR:
  824. if (!cpu_has_mips_r)
  825. break;
  826. value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
  827. MIPS_FENR_FS;
  828. value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
  829. pr_debug("%p gpr[%d]<-enr=%08x\n",
  830. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  831. break;
  832. case FPCREG_FEXR:
  833. if (!cpu_has_mips_r)
  834. break;
  835. value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
  836. pr_debug("%p gpr[%d]<-exr=%08x\n",
  837. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  838. break;
  839. case FPCREG_FCCR:
  840. if (!cpu_has_mips_r)
  841. break;
  842. value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
  843. MIPS_FCCR_COND0;
  844. value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
  845. (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
  846. pr_debug("%p gpr[%d]<-ccr=%08x\n",
  847. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  848. break;
  849. case FPCREG_RID:
  850. value = boot_cpu_data.fpu_id;
  851. break;
  852. default:
  853. break;
  854. }
  855. if (MIPSInst_RT(ir))
  856. xcp->regs[MIPSInst_RT(ir)] = value;
  857. }
  858. /*
  859. * Emulate a CTC1 instruction.
  860. */
  861. static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  862. mips_instruction ir)
  863. {
  864. u32 fcr31 = ctx->fcr31;
  865. u32 value;
  866. u32 mask;
  867. if (MIPSInst_RT(ir) == 0)
  868. value = 0;
  869. else
  870. value = xcp->regs[MIPSInst_RT(ir)];
  871. switch (MIPSInst_RD(ir)) {
  872. case FPCREG_CSR:
  873. pr_debug("%p gpr[%d]->csr=%08x\n",
  874. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  875. /* Preserve read-only bits. */
  876. mask = boot_cpu_data.fpu_msk31;
  877. fcr31 = (value & ~mask) | (fcr31 & mask);
  878. break;
  879. case FPCREG_FENR:
  880. if (!cpu_has_mips_r)
  881. break;
  882. pr_debug("%p gpr[%d]->enr=%08x\n",
  883. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  884. fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
  885. fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
  886. FPU_CSR_FS;
  887. fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
  888. break;
  889. case FPCREG_FEXR:
  890. if (!cpu_has_mips_r)
  891. break;
  892. pr_debug("%p gpr[%d]->exr=%08x\n",
  893. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  894. fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
  895. fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
  896. break;
  897. case FPCREG_FCCR:
  898. if (!cpu_has_mips_r)
  899. break;
  900. pr_debug("%p gpr[%d]->ccr=%08x\n",
  901. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  902. fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
  903. fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
  904. FPU_CSR_COND;
  905. fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
  906. FPU_CSR_CONDX;
  907. break;
  908. default:
  909. break;
  910. }
  911. ctx->fcr31 = fcr31;
  912. }
  913. /*
  914. * Emulate the single floating point instruction pointed at by EPC.
  915. * Two instructions if the instruction is in a branch delay slot.
  916. */
  917. static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  918. struct mm_decoded_insn dec_insn, void *__user *fault_addr)
  919. {
  920. unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
  921. unsigned int cond, cbit, bit0;
  922. mips_instruction ir;
  923. int likely, pc_inc;
  924. union fpureg *fpr;
  925. u32 __user *wva;
  926. u64 __user *dva;
  927. u32 wval;
  928. u64 dval;
  929. int sig;
  930. /*
  931. * These are giving gcc a gentle hint about what to expect in
  932. * dec_inst in order to do better optimization.
  933. */
  934. if (!cpu_has_mmips && dec_insn.micro_mips_mode)
  935. unreachable();
  936. /* XXX NEC Vr54xx bug workaround */
  937. if (delay_slot(xcp)) {
  938. if (dec_insn.micro_mips_mode) {
  939. if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
  940. clear_delay_slot(xcp);
  941. } else {
  942. if (!isBranchInstr(xcp, dec_insn, &contpc))
  943. clear_delay_slot(xcp);
  944. }
  945. }
  946. if (delay_slot(xcp)) {
  947. /*
  948. * The instruction to be emulated is in a branch delay slot
  949. * which means that we have to emulate the branch instruction
  950. * BEFORE we do the cop1 instruction.
  951. *
  952. * This branch could be a COP1 branch, but in that case we
  953. * would have had a trap for that instruction, and would not
  954. * come through this route.
  955. *
  956. * Linux MIPS branch emulator operates on context, updating the
  957. * cp0_epc.
  958. */
  959. ir = dec_insn.next_insn; /* process delay slot instr */
  960. pc_inc = dec_insn.next_pc_inc;
  961. } else {
  962. ir = dec_insn.insn; /* process current instr */
  963. pc_inc = dec_insn.pc_inc;
  964. }
  965. /*
  966. * Since microMIPS FPU instructios are a subset of MIPS32 FPU
  967. * instructions, we want to convert microMIPS FPU instructions
  968. * into MIPS32 instructions so that we could reuse all of the
  969. * FPU emulation code.
  970. *
  971. * NOTE: We cannot do this for branch instructions since they
  972. * are not a subset. Example: Cannot emulate a 16-bit
  973. * aligned target address with a MIPS32 instruction.
  974. */
  975. if (dec_insn.micro_mips_mode) {
  976. /*
  977. * If next instruction is a 16-bit instruction, then it
  978. * it cannot be a FPU instruction. This could happen
  979. * since we can be called for non-FPU instructions.
  980. */
  981. if ((pc_inc == 2) ||
  982. (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
  983. == SIGILL))
  984. return SIGILL;
  985. }
  986. emul:
  987. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
  988. MIPS_FPU_EMU_INC_STATS(emulated);
  989. switch (MIPSInst_OPCODE(ir)) {
  990. case ldc1_op:
  991. dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  992. MIPSInst_SIMM(ir));
  993. MIPS_FPU_EMU_INC_STATS(loads);
  994. if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
  995. MIPS_FPU_EMU_INC_STATS(errors);
  996. *fault_addr = dva;
  997. return SIGBUS;
  998. }
  999. if (__get_user(dval, dva)) {
  1000. MIPS_FPU_EMU_INC_STATS(errors);
  1001. *fault_addr = dva;
  1002. return SIGSEGV;
  1003. }
  1004. DITOREG(dval, MIPSInst_RT(ir));
  1005. break;
  1006. case sdc1_op:
  1007. dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  1008. MIPSInst_SIMM(ir));
  1009. MIPS_FPU_EMU_INC_STATS(stores);
  1010. DIFROMREG(dval, MIPSInst_RT(ir));
  1011. if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
  1012. MIPS_FPU_EMU_INC_STATS(errors);
  1013. *fault_addr = dva;
  1014. return SIGBUS;
  1015. }
  1016. if (__put_user(dval, dva)) {
  1017. MIPS_FPU_EMU_INC_STATS(errors);
  1018. *fault_addr = dva;
  1019. return SIGSEGV;
  1020. }
  1021. break;
  1022. case lwc1_op:
  1023. wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  1024. MIPSInst_SIMM(ir));
  1025. MIPS_FPU_EMU_INC_STATS(loads);
  1026. if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
  1027. MIPS_FPU_EMU_INC_STATS(errors);
  1028. *fault_addr = wva;
  1029. return SIGBUS;
  1030. }
  1031. if (__get_user(wval, wva)) {
  1032. MIPS_FPU_EMU_INC_STATS(errors);
  1033. *fault_addr = wva;
  1034. return SIGSEGV;
  1035. }
  1036. SITOREG(wval, MIPSInst_RT(ir));
  1037. break;
  1038. case swc1_op:
  1039. wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  1040. MIPSInst_SIMM(ir));
  1041. MIPS_FPU_EMU_INC_STATS(stores);
  1042. SIFROMREG(wval, MIPSInst_RT(ir));
  1043. if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
  1044. MIPS_FPU_EMU_INC_STATS(errors);
  1045. *fault_addr = wva;
  1046. return SIGBUS;
  1047. }
  1048. if (__put_user(wval, wva)) {
  1049. MIPS_FPU_EMU_INC_STATS(errors);
  1050. *fault_addr = wva;
  1051. return SIGSEGV;
  1052. }
  1053. break;
  1054. case cop1_op:
  1055. switch (MIPSInst_RS(ir)) {
  1056. case dmfc_op:
  1057. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  1058. return SIGILL;
  1059. /* copregister fs -> gpr[rt] */
  1060. if (MIPSInst_RT(ir) != 0) {
  1061. DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  1062. MIPSInst_RD(ir));
  1063. }
  1064. break;
  1065. case dmtc_op:
  1066. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  1067. return SIGILL;
  1068. /* copregister fs <- rt */
  1069. DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1070. break;
  1071. case mfhc_op:
  1072. if (!cpu_has_mips_r2_r6)
  1073. goto sigill;
  1074. /* copregister rd -> gpr[rt] */
  1075. if (MIPSInst_RT(ir) != 0) {
  1076. SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
  1077. MIPSInst_RD(ir));
  1078. }
  1079. break;
  1080. case mthc_op:
  1081. if (!cpu_has_mips_r2_r6)
  1082. goto sigill;
  1083. /* copregister rd <- gpr[rt] */
  1084. SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1085. break;
  1086. case mfc_op:
  1087. /* copregister rd -> gpr[rt] */
  1088. if (MIPSInst_RT(ir) != 0) {
  1089. SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  1090. MIPSInst_RD(ir));
  1091. }
  1092. break;
  1093. case mtc_op:
  1094. /* copregister rd <- rt */
  1095. SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1096. break;
  1097. case cfc_op:
  1098. /* cop control register rd -> gpr[rt] */
  1099. cop1_cfc(xcp, ctx, ir);
  1100. break;
  1101. case ctc_op:
  1102. /* copregister rd <- rt */
  1103. cop1_ctc(xcp, ctx, ir);
  1104. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1105. return SIGFPE;
  1106. }
  1107. break;
  1108. case bc1eqz_op:
  1109. case bc1nez_op:
  1110. if (!cpu_has_mips_r6 || delay_slot(xcp))
  1111. return SIGILL;
  1112. cond = likely = 0;
  1113. fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
  1114. bit0 = get_fpr32(fpr, 0) & 0x1;
  1115. switch (MIPSInst_RS(ir)) {
  1116. case bc1eqz_op:
  1117. cond = bit0 == 0;
  1118. break;
  1119. case bc1nez_op:
  1120. cond = bit0 != 0;
  1121. break;
  1122. }
  1123. goto branch_common;
  1124. case bc_op:
  1125. if (delay_slot(xcp))
  1126. return SIGILL;
  1127. if (cpu_has_mips_4_5_r)
  1128. cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
  1129. else
  1130. cbit = FPU_CSR_COND;
  1131. cond = ctx->fcr31 & cbit;
  1132. likely = 0;
  1133. switch (MIPSInst_RT(ir) & 3) {
  1134. case bcfl_op:
  1135. if (cpu_has_mips_2_3_4_5_r)
  1136. likely = 1;
  1137. /* Fall through */
  1138. case bcf_op:
  1139. cond = !cond;
  1140. break;
  1141. case bctl_op:
  1142. if (cpu_has_mips_2_3_4_5_r)
  1143. likely = 1;
  1144. /* Fall through */
  1145. case bct_op:
  1146. break;
  1147. }
  1148. branch_common:
  1149. set_delay_slot(xcp);
  1150. if (cond) {
  1151. /*
  1152. * Branch taken: emulate dslot instruction
  1153. */
  1154. unsigned long bcpc;
  1155. /*
  1156. * Remember EPC at the branch to point back
  1157. * at so that any delay-slot instruction
  1158. * signal is not silently ignored.
  1159. */
  1160. bcpc = xcp->cp0_epc;
  1161. xcp->cp0_epc += dec_insn.pc_inc;
  1162. contpc = MIPSInst_SIMM(ir);
  1163. ir = dec_insn.next_insn;
  1164. if (dec_insn.micro_mips_mode) {
  1165. contpc = (xcp->cp0_epc + (contpc << 1));
  1166. /* If 16-bit instruction, not FPU. */
  1167. if ((dec_insn.next_pc_inc == 2) ||
  1168. (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
  1169. /*
  1170. * Since this instruction will
  1171. * be put on the stack with
  1172. * 32-bit words, get around
  1173. * this problem by putting a
  1174. * NOP16 as the second one.
  1175. */
  1176. if (dec_insn.next_pc_inc == 2)
  1177. ir = (ir & (~0xffff)) | MM_NOP16;
  1178. /*
  1179. * Single step the non-CP1
  1180. * instruction in the dslot.
  1181. */
  1182. sig = mips_dsemul(xcp, ir,
  1183. bcpc, contpc);
  1184. if (sig < 0)
  1185. break;
  1186. if (sig)
  1187. xcp->cp0_epc = bcpc;
  1188. /*
  1189. * SIGILL forces out of
  1190. * the emulation loop.
  1191. */
  1192. return sig ? sig : SIGILL;
  1193. }
  1194. } else
  1195. contpc = (xcp->cp0_epc + (contpc << 2));
  1196. switch (MIPSInst_OPCODE(ir)) {
  1197. case lwc1_op:
  1198. case swc1_op:
  1199. goto emul;
  1200. case ldc1_op:
  1201. case sdc1_op:
  1202. if (cpu_has_mips_2_3_4_5_r)
  1203. goto emul;
  1204. goto bc_sigill;
  1205. case cop1_op:
  1206. goto emul;
  1207. case cop1x_op:
  1208. if (cpu_has_mips_4_5_64_r2_r6)
  1209. /* its one of ours */
  1210. goto emul;
  1211. goto bc_sigill;
  1212. case spec_op:
  1213. switch (MIPSInst_FUNC(ir)) {
  1214. case movc_op:
  1215. if (cpu_has_mips_4_5_r)
  1216. goto emul;
  1217. goto bc_sigill;
  1218. }
  1219. break;
  1220. bc_sigill:
  1221. xcp->cp0_epc = bcpc;
  1222. return SIGILL;
  1223. }
  1224. /*
  1225. * Single step the non-cp1
  1226. * instruction in the dslot
  1227. */
  1228. sig = mips_dsemul(xcp, ir, bcpc, contpc);
  1229. if (sig < 0)
  1230. break;
  1231. if (sig)
  1232. xcp->cp0_epc = bcpc;
  1233. /* SIGILL forces out of the emulation loop. */
  1234. return sig ? sig : SIGILL;
  1235. } else if (likely) { /* branch not taken */
  1236. /*
  1237. * branch likely nullifies
  1238. * dslot if not taken
  1239. */
  1240. xcp->cp0_epc += dec_insn.pc_inc;
  1241. contpc += dec_insn.pc_inc;
  1242. /*
  1243. * else continue & execute
  1244. * dslot as normal insn
  1245. */
  1246. }
  1247. break;
  1248. default:
  1249. if (!(MIPSInst_RS(ir) & 0x10))
  1250. return SIGILL;
  1251. /* a real fpu computation instruction */
  1252. if ((sig = fpu_emu(xcp, ctx, ir)))
  1253. return sig;
  1254. }
  1255. break;
  1256. case cop1x_op:
  1257. if (!cpu_has_mips_4_5_64_r2_r6)
  1258. return SIGILL;
  1259. sig = fpux_emu(xcp, ctx, ir, fault_addr);
  1260. if (sig)
  1261. return sig;
  1262. break;
  1263. case spec_op:
  1264. if (!cpu_has_mips_4_5_r)
  1265. return SIGILL;
  1266. if (MIPSInst_FUNC(ir) != movc_op)
  1267. return SIGILL;
  1268. cond = fpucondbit[MIPSInst_RT(ir) >> 2];
  1269. if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
  1270. xcp->regs[MIPSInst_RD(ir)] =
  1271. xcp->regs[MIPSInst_RS(ir)];
  1272. break;
  1273. default:
  1274. sigill:
  1275. return SIGILL;
  1276. }
  1277. /* we did it !! */
  1278. xcp->cp0_epc = contpc;
  1279. clear_delay_slot(xcp);
  1280. return 0;
  1281. }
  1282. /*
  1283. * Conversion table from MIPS compare ops 48-63
  1284. * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
  1285. */
  1286. static const unsigned char cmptab[8] = {
  1287. 0, /* cmp_0 (sig) cmp_sf */
  1288. IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
  1289. IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
  1290. IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
  1291. IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
  1292. IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
  1293. IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
  1294. IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
  1295. };
  1296. static const unsigned char negative_cmptab[8] = {
  1297. 0, /* Reserved */
  1298. IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
  1299. IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
  1300. IEEE754_CLT | IEEE754_CGT,
  1301. /* Reserved */
  1302. };
  1303. /*
  1304. * Additional MIPS4 instructions
  1305. */
  1306. #define DEF3OP(name, p, f1, f2, f3) \
  1307. static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
  1308. union ieee754##p s, union ieee754##p t) \
  1309. { \
  1310. struct _ieee754_csr ieee754_csr_save; \
  1311. s = f1(s, t); \
  1312. ieee754_csr_save = ieee754_csr; \
  1313. s = f2(s, r); \
  1314. ieee754_csr_save.cx |= ieee754_csr.cx; \
  1315. ieee754_csr_save.sx |= ieee754_csr.sx; \
  1316. s = f3(s); \
  1317. ieee754_csr.cx |= ieee754_csr_save.cx; \
  1318. ieee754_csr.sx |= ieee754_csr_save.sx; \
  1319. return s; \
  1320. }
  1321. static union ieee754dp fpemu_dp_recip(union ieee754dp d)
  1322. {
  1323. return ieee754dp_div(ieee754dp_one(0), d);
  1324. }
  1325. static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
  1326. {
  1327. return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
  1328. }
  1329. static union ieee754sp fpemu_sp_recip(union ieee754sp s)
  1330. {
  1331. return ieee754sp_div(ieee754sp_one(0), s);
  1332. }
  1333. static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
  1334. {
  1335. return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
  1336. }
  1337. DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
  1338. DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
  1339. DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
  1340. DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
  1341. DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
  1342. DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
  1343. DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
  1344. DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
  1345. static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1346. mips_instruction ir, void *__user *fault_addr)
  1347. {
  1348. unsigned rcsr = 0; /* resulting csr */
  1349. MIPS_FPU_EMU_INC_STATS(cp1xops);
  1350. switch (MIPSInst_FMA_FFMT(ir)) {
  1351. case s_fmt:{ /* 0 */
  1352. union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
  1353. union ieee754sp fd, fr, fs, ft;
  1354. u32 __user *va;
  1355. u32 val;
  1356. switch (MIPSInst_FUNC(ir)) {
  1357. case lwxc1_op:
  1358. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1359. xcp->regs[MIPSInst_FT(ir)]);
  1360. MIPS_FPU_EMU_INC_STATS(loads);
  1361. if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
  1362. MIPS_FPU_EMU_INC_STATS(errors);
  1363. *fault_addr = va;
  1364. return SIGBUS;
  1365. }
  1366. if (__get_user(val, va)) {
  1367. MIPS_FPU_EMU_INC_STATS(errors);
  1368. *fault_addr = va;
  1369. return SIGSEGV;
  1370. }
  1371. SITOREG(val, MIPSInst_FD(ir));
  1372. break;
  1373. case swxc1_op:
  1374. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1375. xcp->regs[MIPSInst_FT(ir)]);
  1376. MIPS_FPU_EMU_INC_STATS(stores);
  1377. SIFROMREG(val, MIPSInst_FS(ir));
  1378. if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
  1379. MIPS_FPU_EMU_INC_STATS(errors);
  1380. *fault_addr = va;
  1381. return SIGBUS;
  1382. }
  1383. if (put_user(val, va)) {
  1384. MIPS_FPU_EMU_INC_STATS(errors);
  1385. *fault_addr = va;
  1386. return SIGSEGV;
  1387. }
  1388. break;
  1389. case madd_s_op:
  1390. handler = fpemu_sp_madd;
  1391. goto scoptop;
  1392. case msub_s_op:
  1393. handler = fpemu_sp_msub;
  1394. goto scoptop;
  1395. case nmadd_s_op:
  1396. handler = fpemu_sp_nmadd;
  1397. goto scoptop;
  1398. case nmsub_s_op:
  1399. handler = fpemu_sp_nmsub;
  1400. goto scoptop;
  1401. scoptop:
  1402. SPFROMREG(fr, MIPSInst_FR(ir));
  1403. SPFROMREG(fs, MIPSInst_FS(ir));
  1404. SPFROMREG(ft, MIPSInst_FT(ir));
  1405. fd = (*handler) (fr, fs, ft);
  1406. SPTOREG(fd, MIPSInst_FD(ir));
  1407. copcsr:
  1408. if (ieee754_cxtest(IEEE754_INEXACT)) {
  1409. MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
  1410. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1411. }
  1412. if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
  1413. MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
  1414. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1415. }
  1416. if (ieee754_cxtest(IEEE754_OVERFLOW)) {
  1417. MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
  1418. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1419. }
  1420. if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
  1421. MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
  1422. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1423. }
  1424. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  1425. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1426. /*printk ("SIGFPE: FPU csr = %08x\n",
  1427. ctx->fcr31); */
  1428. return SIGFPE;
  1429. }
  1430. break;
  1431. default:
  1432. return SIGILL;
  1433. }
  1434. break;
  1435. }
  1436. case d_fmt:{ /* 1 */
  1437. union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
  1438. union ieee754dp fd, fr, fs, ft;
  1439. u64 __user *va;
  1440. u64 val;
  1441. switch (MIPSInst_FUNC(ir)) {
  1442. case ldxc1_op:
  1443. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1444. xcp->regs[MIPSInst_FT(ir)]);
  1445. MIPS_FPU_EMU_INC_STATS(loads);
  1446. if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
  1447. MIPS_FPU_EMU_INC_STATS(errors);
  1448. *fault_addr = va;
  1449. return SIGBUS;
  1450. }
  1451. if (__get_user(val, va)) {
  1452. MIPS_FPU_EMU_INC_STATS(errors);
  1453. *fault_addr = va;
  1454. return SIGSEGV;
  1455. }
  1456. DITOREG(val, MIPSInst_FD(ir));
  1457. break;
  1458. case sdxc1_op:
  1459. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1460. xcp->regs[MIPSInst_FT(ir)]);
  1461. MIPS_FPU_EMU_INC_STATS(stores);
  1462. DIFROMREG(val, MIPSInst_FS(ir));
  1463. if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
  1464. MIPS_FPU_EMU_INC_STATS(errors);
  1465. *fault_addr = va;
  1466. return SIGBUS;
  1467. }
  1468. if (__put_user(val, va)) {
  1469. MIPS_FPU_EMU_INC_STATS(errors);
  1470. *fault_addr = va;
  1471. return SIGSEGV;
  1472. }
  1473. break;
  1474. case madd_d_op:
  1475. handler = fpemu_dp_madd;
  1476. goto dcoptop;
  1477. case msub_d_op:
  1478. handler = fpemu_dp_msub;
  1479. goto dcoptop;
  1480. case nmadd_d_op:
  1481. handler = fpemu_dp_nmadd;
  1482. goto dcoptop;
  1483. case nmsub_d_op:
  1484. handler = fpemu_dp_nmsub;
  1485. goto dcoptop;
  1486. dcoptop:
  1487. DPFROMREG(fr, MIPSInst_FR(ir));
  1488. DPFROMREG(fs, MIPSInst_FS(ir));
  1489. DPFROMREG(ft, MIPSInst_FT(ir));
  1490. fd = (*handler) (fr, fs, ft);
  1491. DPTOREG(fd, MIPSInst_FD(ir));
  1492. goto copcsr;
  1493. default:
  1494. return SIGILL;
  1495. }
  1496. break;
  1497. }
  1498. case 0x3:
  1499. if (MIPSInst_FUNC(ir) != pfetch_op)
  1500. return SIGILL;
  1501. /* ignore prefx operation */
  1502. break;
  1503. default:
  1504. return SIGILL;
  1505. }
  1506. return 0;
  1507. }
  1508. /*
  1509. * Emulate a single COP1 arithmetic instruction.
  1510. */
  1511. static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1512. mips_instruction ir)
  1513. {
  1514. int rfmt; /* resulting format */
  1515. unsigned rcsr = 0; /* resulting csr */
  1516. unsigned int oldrm;
  1517. unsigned int cbit;
  1518. unsigned cond;
  1519. union {
  1520. union ieee754dp d;
  1521. union ieee754sp s;
  1522. int w;
  1523. s64 l;
  1524. } rv; /* resulting value */
  1525. u64 bits;
  1526. MIPS_FPU_EMU_INC_STATS(cp1ops);
  1527. switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
  1528. case s_fmt: { /* 0 */
  1529. union {
  1530. union ieee754sp(*b) (union ieee754sp, union ieee754sp);
  1531. union ieee754sp(*u) (union ieee754sp);
  1532. } handler;
  1533. union ieee754sp fd, fs, ft;
  1534. switch (MIPSInst_FUNC(ir)) {
  1535. /* binary ops */
  1536. case fadd_op:
  1537. handler.b = ieee754sp_add;
  1538. goto scopbop;
  1539. case fsub_op:
  1540. handler.b = ieee754sp_sub;
  1541. goto scopbop;
  1542. case fmul_op:
  1543. handler.b = ieee754sp_mul;
  1544. goto scopbop;
  1545. case fdiv_op:
  1546. handler.b = ieee754sp_div;
  1547. goto scopbop;
  1548. /* unary ops */
  1549. case fsqrt_op:
  1550. if (!cpu_has_mips_2_3_4_5_r)
  1551. return SIGILL;
  1552. handler.u = ieee754sp_sqrt;
  1553. goto scopuop;
  1554. /*
  1555. * Note that on some MIPS IV implementations such as the
  1556. * R5000 and R8000 the FSQRT and FRECIP instructions do not
  1557. * achieve full IEEE-754 accuracy - however this emulator does.
  1558. */
  1559. case frsqrt_op:
  1560. if (!cpu_has_mips_4_5_64_r2_r6)
  1561. return SIGILL;
  1562. handler.u = fpemu_sp_rsqrt;
  1563. goto scopuop;
  1564. case frecip_op:
  1565. if (!cpu_has_mips_4_5_64_r2_r6)
  1566. return SIGILL;
  1567. handler.u = fpemu_sp_recip;
  1568. goto scopuop;
  1569. case fmovc_op:
  1570. if (!cpu_has_mips_4_5_r)
  1571. return SIGILL;
  1572. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1573. if (((ctx->fcr31 & cond) != 0) !=
  1574. ((MIPSInst_FT(ir) & 1) != 0))
  1575. return 0;
  1576. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1577. break;
  1578. case fmovz_op:
  1579. if (!cpu_has_mips_4_5_r)
  1580. return SIGILL;
  1581. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1582. return 0;
  1583. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1584. break;
  1585. case fmovn_op:
  1586. if (!cpu_has_mips_4_5_r)
  1587. return SIGILL;
  1588. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1589. return 0;
  1590. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1591. break;
  1592. case fseleqz_op:
  1593. if (!cpu_has_mips_r6)
  1594. return SIGILL;
  1595. SPFROMREG(rv.s, MIPSInst_FT(ir));
  1596. if (rv.w & 0x1)
  1597. rv.w = 0;
  1598. else
  1599. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1600. break;
  1601. case fselnez_op:
  1602. if (!cpu_has_mips_r6)
  1603. return SIGILL;
  1604. SPFROMREG(rv.s, MIPSInst_FT(ir));
  1605. if (rv.w & 0x1)
  1606. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1607. else
  1608. rv.w = 0;
  1609. break;
  1610. case fmaddf_op: {
  1611. union ieee754sp ft, fs, fd;
  1612. if (!cpu_has_mips_r6)
  1613. return SIGILL;
  1614. SPFROMREG(ft, MIPSInst_FT(ir));
  1615. SPFROMREG(fs, MIPSInst_FS(ir));
  1616. SPFROMREG(fd, MIPSInst_FD(ir));
  1617. rv.s = ieee754sp_maddf(fd, fs, ft);
  1618. break;
  1619. }
  1620. case fmsubf_op: {
  1621. union ieee754sp ft, fs, fd;
  1622. if (!cpu_has_mips_r6)
  1623. return SIGILL;
  1624. SPFROMREG(ft, MIPSInst_FT(ir));
  1625. SPFROMREG(fs, MIPSInst_FS(ir));
  1626. SPFROMREG(fd, MIPSInst_FD(ir));
  1627. rv.s = ieee754sp_msubf(fd, fs, ft);
  1628. break;
  1629. }
  1630. case frint_op: {
  1631. union ieee754sp fs;
  1632. if (!cpu_has_mips_r6)
  1633. return SIGILL;
  1634. SPFROMREG(fs, MIPSInst_FS(ir));
  1635. rv.l = ieee754sp_tlong(fs);
  1636. rv.s = ieee754sp_flong(rv.l);
  1637. goto copcsr;
  1638. }
  1639. case fclass_op: {
  1640. union ieee754sp fs;
  1641. if (!cpu_has_mips_r6)
  1642. return SIGILL;
  1643. SPFROMREG(fs, MIPSInst_FS(ir));
  1644. rv.w = ieee754sp_2008class(fs);
  1645. rfmt = w_fmt;
  1646. break;
  1647. }
  1648. case fmin_op: {
  1649. union ieee754sp fs, ft;
  1650. if (!cpu_has_mips_r6)
  1651. return SIGILL;
  1652. SPFROMREG(ft, MIPSInst_FT(ir));
  1653. SPFROMREG(fs, MIPSInst_FS(ir));
  1654. rv.s = ieee754sp_fmin(fs, ft);
  1655. break;
  1656. }
  1657. case fmina_op: {
  1658. union ieee754sp fs, ft;
  1659. if (!cpu_has_mips_r6)
  1660. return SIGILL;
  1661. SPFROMREG(ft, MIPSInst_FT(ir));
  1662. SPFROMREG(fs, MIPSInst_FS(ir));
  1663. rv.s = ieee754sp_fmina(fs, ft);
  1664. break;
  1665. }
  1666. case fmax_op: {
  1667. union ieee754sp fs, ft;
  1668. if (!cpu_has_mips_r6)
  1669. return SIGILL;
  1670. SPFROMREG(ft, MIPSInst_FT(ir));
  1671. SPFROMREG(fs, MIPSInst_FS(ir));
  1672. rv.s = ieee754sp_fmax(fs, ft);
  1673. break;
  1674. }
  1675. case fmaxa_op: {
  1676. union ieee754sp fs, ft;
  1677. if (!cpu_has_mips_r6)
  1678. return SIGILL;
  1679. SPFROMREG(ft, MIPSInst_FT(ir));
  1680. SPFROMREG(fs, MIPSInst_FS(ir));
  1681. rv.s = ieee754sp_fmaxa(fs, ft);
  1682. break;
  1683. }
  1684. case fabs_op:
  1685. handler.u = ieee754sp_abs;
  1686. goto scopuop;
  1687. case fneg_op:
  1688. handler.u = ieee754sp_neg;
  1689. goto scopuop;
  1690. case fmov_op:
  1691. /* an easy one */
  1692. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1693. goto copcsr;
  1694. /* binary op on handler */
  1695. scopbop:
  1696. SPFROMREG(fs, MIPSInst_FS(ir));
  1697. SPFROMREG(ft, MIPSInst_FT(ir));
  1698. rv.s = (*handler.b) (fs, ft);
  1699. goto copcsr;
  1700. scopuop:
  1701. SPFROMREG(fs, MIPSInst_FS(ir));
  1702. rv.s = (*handler.u) (fs);
  1703. goto copcsr;
  1704. copcsr:
  1705. if (ieee754_cxtest(IEEE754_INEXACT)) {
  1706. MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
  1707. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1708. }
  1709. if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
  1710. MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
  1711. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1712. }
  1713. if (ieee754_cxtest(IEEE754_OVERFLOW)) {
  1714. MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
  1715. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1716. }
  1717. if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
  1718. MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
  1719. rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
  1720. }
  1721. if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
  1722. MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
  1723. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1724. }
  1725. break;
  1726. /* unary conv ops */
  1727. case fcvts_op:
  1728. return SIGILL; /* not defined */
  1729. case fcvtd_op:
  1730. SPFROMREG(fs, MIPSInst_FS(ir));
  1731. rv.d = ieee754dp_fsp(fs);
  1732. rfmt = d_fmt;
  1733. goto copcsr;
  1734. case fcvtw_op:
  1735. SPFROMREG(fs, MIPSInst_FS(ir));
  1736. rv.w = ieee754sp_tint(fs);
  1737. rfmt = w_fmt;
  1738. goto copcsr;
  1739. case fround_op:
  1740. case ftrunc_op:
  1741. case fceil_op:
  1742. case ffloor_op:
  1743. if (!cpu_has_mips_2_3_4_5_r)
  1744. return SIGILL;
  1745. oldrm = ieee754_csr.rm;
  1746. SPFROMREG(fs, MIPSInst_FS(ir));
  1747. ieee754_csr.rm = MIPSInst_FUNC(ir);
  1748. rv.w = ieee754sp_tint(fs);
  1749. ieee754_csr.rm = oldrm;
  1750. rfmt = w_fmt;
  1751. goto copcsr;
  1752. case fsel_op:
  1753. if (!cpu_has_mips_r6)
  1754. return SIGILL;
  1755. SPFROMREG(fd, MIPSInst_FD(ir));
  1756. if (fd.bits & 0x1)
  1757. SPFROMREG(rv.s, MIPSInst_FT(ir));
  1758. else
  1759. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1760. break;
  1761. case fcvtl_op:
  1762. if (!cpu_has_mips_3_4_5_64_r2_r6)
  1763. return SIGILL;
  1764. SPFROMREG(fs, MIPSInst_FS(ir));
  1765. rv.l = ieee754sp_tlong(fs);
  1766. rfmt = l_fmt;
  1767. goto copcsr;
  1768. case froundl_op:
  1769. case ftruncl_op:
  1770. case fceill_op:
  1771. case ffloorl_op:
  1772. if (!cpu_has_mips_3_4_5_64_r2_r6)
  1773. return SIGILL;
  1774. oldrm = ieee754_csr.rm;
  1775. SPFROMREG(fs, MIPSInst_FS(ir));
  1776. ieee754_csr.rm = MIPSInst_FUNC(ir);
  1777. rv.l = ieee754sp_tlong(fs);
  1778. ieee754_csr.rm = oldrm;
  1779. rfmt = l_fmt;
  1780. goto copcsr;
  1781. default:
  1782. if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
  1783. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  1784. union ieee754sp fs, ft;
  1785. SPFROMREG(fs, MIPSInst_FS(ir));
  1786. SPFROMREG(ft, MIPSInst_FT(ir));
  1787. rv.w = ieee754sp_cmp(fs, ft,
  1788. cmptab[cmpop & 0x7], cmpop & 0x8);
  1789. rfmt = -1;
  1790. if ((cmpop & 0x8) && ieee754_cxtest
  1791. (IEEE754_INVALID_OPERATION))
  1792. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  1793. else
  1794. goto copcsr;
  1795. } else
  1796. return SIGILL;
  1797. break;
  1798. }
  1799. break;
  1800. }
  1801. case d_fmt: {
  1802. union ieee754dp fd, fs, ft;
  1803. union {
  1804. union ieee754dp(*b) (union ieee754dp, union ieee754dp);
  1805. union ieee754dp(*u) (union ieee754dp);
  1806. } handler;
  1807. switch (MIPSInst_FUNC(ir)) {
  1808. /* binary ops */
  1809. case fadd_op:
  1810. handler.b = ieee754dp_add;
  1811. goto dcopbop;
  1812. case fsub_op:
  1813. handler.b = ieee754dp_sub;
  1814. goto dcopbop;
  1815. case fmul_op:
  1816. handler.b = ieee754dp_mul;
  1817. goto dcopbop;
  1818. case fdiv_op:
  1819. handler.b = ieee754dp_div;
  1820. goto dcopbop;
  1821. /* unary ops */
  1822. case fsqrt_op:
  1823. if (!cpu_has_mips_2_3_4_5_r)
  1824. return SIGILL;
  1825. handler.u = ieee754dp_sqrt;
  1826. goto dcopuop;
  1827. /*
  1828. * Note that on some MIPS IV implementations such as the
  1829. * R5000 and R8000 the FSQRT and FRECIP instructions do not
  1830. * achieve full IEEE-754 accuracy - however this emulator does.
  1831. */
  1832. case frsqrt_op:
  1833. if (!cpu_has_mips_4_5_64_r2_r6)
  1834. return SIGILL;
  1835. handler.u = fpemu_dp_rsqrt;
  1836. goto dcopuop;
  1837. case frecip_op:
  1838. if (!cpu_has_mips_4_5_64_r2_r6)
  1839. return SIGILL;
  1840. handler.u = fpemu_dp_recip;
  1841. goto dcopuop;
  1842. case fmovc_op:
  1843. if (!cpu_has_mips_4_5_r)
  1844. return SIGILL;
  1845. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1846. if (((ctx->fcr31 & cond) != 0) !=
  1847. ((MIPSInst_FT(ir) & 1) != 0))
  1848. return 0;
  1849. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1850. break;
  1851. case fmovz_op:
  1852. if (!cpu_has_mips_4_5_r)
  1853. return SIGILL;
  1854. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1855. return 0;
  1856. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1857. break;
  1858. case fmovn_op:
  1859. if (!cpu_has_mips_4_5_r)
  1860. return SIGILL;
  1861. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1862. return 0;
  1863. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1864. break;
  1865. case fseleqz_op:
  1866. if (!cpu_has_mips_r6)
  1867. return SIGILL;
  1868. DPFROMREG(rv.d, MIPSInst_FT(ir));
  1869. if (rv.l & 0x1)
  1870. rv.l = 0;
  1871. else
  1872. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1873. break;
  1874. case fselnez_op:
  1875. if (!cpu_has_mips_r6)
  1876. return SIGILL;
  1877. DPFROMREG(rv.d, MIPSInst_FT(ir));
  1878. if (rv.l & 0x1)
  1879. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1880. else
  1881. rv.l = 0;
  1882. break;
  1883. case fmaddf_op: {
  1884. union ieee754dp ft, fs, fd;
  1885. if (!cpu_has_mips_r6)
  1886. return SIGILL;
  1887. DPFROMREG(ft, MIPSInst_FT(ir));
  1888. DPFROMREG(fs, MIPSInst_FS(ir));
  1889. DPFROMREG(fd, MIPSInst_FD(ir));
  1890. rv.d = ieee754dp_maddf(fd, fs, ft);
  1891. break;
  1892. }
  1893. case fmsubf_op: {
  1894. union ieee754dp ft, fs, fd;
  1895. if (!cpu_has_mips_r6)
  1896. return SIGILL;
  1897. DPFROMREG(ft, MIPSInst_FT(ir));
  1898. DPFROMREG(fs, MIPSInst_FS(ir));
  1899. DPFROMREG(fd, MIPSInst_FD(ir));
  1900. rv.d = ieee754dp_msubf(fd, fs, ft);
  1901. break;
  1902. }
  1903. case frint_op: {
  1904. union ieee754dp fs;
  1905. if (!cpu_has_mips_r6)
  1906. return SIGILL;
  1907. DPFROMREG(fs, MIPSInst_FS(ir));
  1908. rv.l = ieee754dp_tlong(fs);
  1909. rv.d = ieee754dp_flong(rv.l);
  1910. goto copcsr;
  1911. }
  1912. case fclass_op: {
  1913. union ieee754dp fs;
  1914. if (!cpu_has_mips_r6)
  1915. return SIGILL;
  1916. DPFROMREG(fs, MIPSInst_FS(ir));
  1917. rv.w = ieee754dp_2008class(fs);
  1918. rfmt = w_fmt;
  1919. break;
  1920. }
  1921. case fmin_op: {
  1922. union ieee754dp fs, ft;
  1923. if (!cpu_has_mips_r6)
  1924. return SIGILL;
  1925. DPFROMREG(ft, MIPSInst_FT(ir));
  1926. DPFROMREG(fs, MIPSInst_FS(ir));
  1927. rv.d = ieee754dp_fmin(fs, ft);
  1928. break;
  1929. }
  1930. case fmina_op: {
  1931. union ieee754dp fs, ft;
  1932. if (!cpu_has_mips_r6)
  1933. return SIGILL;
  1934. DPFROMREG(ft, MIPSInst_FT(ir));
  1935. DPFROMREG(fs, MIPSInst_FS(ir));
  1936. rv.d = ieee754dp_fmina(fs, ft);
  1937. break;
  1938. }
  1939. case fmax_op: {
  1940. union ieee754dp fs, ft;
  1941. if (!cpu_has_mips_r6)
  1942. return SIGILL;
  1943. DPFROMREG(ft, MIPSInst_FT(ir));
  1944. DPFROMREG(fs, MIPSInst_FS(ir));
  1945. rv.d = ieee754dp_fmax(fs, ft);
  1946. break;
  1947. }
  1948. case fmaxa_op: {
  1949. union ieee754dp fs, ft;
  1950. if (!cpu_has_mips_r6)
  1951. return SIGILL;
  1952. DPFROMREG(ft, MIPSInst_FT(ir));
  1953. DPFROMREG(fs, MIPSInst_FS(ir));
  1954. rv.d = ieee754dp_fmaxa(fs, ft);
  1955. break;
  1956. }
  1957. case fabs_op:
  1958. handler.u = ieee754dp_abs;
  1959. goto dcopuop;
  1960. case fneg_op:
  1961. handler.u = ieee754dp_neg;
  1962. goto dcopuop;
  1963. case fmov_op:
  1964. /* an easy one */
  1965. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1966. goto copcsr;
  1967. /* binary op on handler */
  1968. dcopbop:
  1969. DPFROMREG(fs, MIPSInst_FS(ir));
  1970. DPFROMREG(ft, MIPSInst_FT(ir));
  1971. rv.d = (*handler.b) (fs, ft);
  1972. goto copcsr;
  1973. dcopuop:
  1974. DPFROMREG(fs, MIPSInst_FS(ir));
  1975. rv.d = (*handler.u) (fs);
  1976. goto copcsr;
  1977. /*
  1978. * unary conv ops
  1979. */
  1980. case fcvts_op:
  1981. DPFROMREG(fs, MIPSInst_FS(ir));
  1982. rv.s = ieee754sp_fdp(fs);
  1983. rfmt = s_fmt;
  1984. goto copcsr;
  1985. case fcvtd_op:
  1986. return SIGILL; /* not defined */
  1987. case fcvtw_op:
  1988. DPFROMREG(fs, MIPSInst_FS(ir));
  1989. rv.w = ieee754dp_tint(fs); /* wrong */
  1990. rfmt = w_fmt;
  1991. goto copcsr;
  1992. case fround_op:
  1993. case ftrunc_op:
  1994. case fceil_op:
  1995. case ffloor_op:
  1996. if (!cpu_has_mips_2_3_4_5_r)
  1997. return SIGILL;
  1998. oldrm = ieee754_csr.rm;
  1999. DPFROMREG(fs, MIPSInst_FS(ir));
  2000. ieee754_csr.rm = MIPSInst_FUNC(ir);
  2001. rv.w = ieee754dp_tint(fs);
  2002. ieee754_csr.rm = oldrm;
  2003. rfmt = w_fmt;
  2004. goto copcsr;
  2005. case fsel_op:
  2006. if (!cpu_has_mips_r6)
  2007. return SIGILL;
  2008. DPFROMREG(fd, MIPSInst_FD(ir));
  2009. if (fd.bits & 0x1)
  2010. DPFROMREG(rv.d, MIPSInst_FT(ir));
  2011. else
  2012. DPFROMREG(rv.d, MIPSInst_FS(ir));
  2013. break;
  2014. case fcvtl_op:
  2015. if (!cpu_has_mips_3_4_5_64_r2_r6)
  2016. return SIGILL;
  2017. DPFROMREG(fs, MIPSInst_FS(ir));
  2018. rv.l = ieee754dp_tlong(fs);
  2019. rfmt = l_fmt;
  2020. goto copcsr;
  2021. case froundl_op:
  2022. case ftruncl_op:
  2023. case fceill_op:
  2024. case ffloorl_op:
  2025. if (!cpu_has_mips_3_4_5_64_r2_r6)
  2026. return SIGILL;
  2027. oldrm = ieee754_csr.rm;
  2028. DPFROMREG(fs, MIPSInst_FS(ir));
  2029. ieee754_csr.rm = MIPSInst_FUNC(ir);
  2030. rv.l = ieee754dp_tlong(fs);
  2031. ieee754_csr.rm = oldrm;
  2032. rfmt = l_fmt;
  2033. goto copcsr;
  2034. default:
  2035. if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
  2036. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  2037. union ieee754dp fs, ft;
  2038. DPFROMREG(fs, MIPSInst_FS(ir));
  2039. DPFROMREG(ft, MIPSInst_FT(ir));
  2040. rv.w = ieee754dp_cmp(fs, ft,
  2041. cmptab[cmpop & 0x7], cmpop & 0x8);
  2042. rfmt = -1;
  2043. if ((cmpop & 0x8)
  2044. &&
  2045. ieee754_cxtest
  2046. (IEEE754_INVALID_OPERATION))
  2047. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  2048. else
  2049. goto copcsr;
  2050. }
  2051. else {
  2052. return SIGILL;
  2053. }
  2054. break;
  2055. }
  2056. break;
  2057. }
  2058. case w_fmt: {
  2059. union ieee754dp fs;
  2060. switch (MIPSInst_FUNC(ir)) {
  2061. case fcvts_op:
  2062. /* convert word to single precision real */
  2063. SPFROMREG(fs, MIPSInst_FS(ir));
  2064. rv.s = ieee754sp_fint(fs.bits);
  2065. rfmt = s_fmt;
  2066. goto copcsr;
  2067. case fcvtd_op:
  2068. /* convert word to double precision real */
  2069. SPFROMREG(fs, MIPSInst_FS(ir));
  2070. rv.d = ieee754dp_fint(fs.bits);
  2071. rfmt = d_fmt;
  2072. goto copcsr;
  2073. default: {
  2074. /* Emulating the new CMP.condn.fmt R6 instruction */
  2075. #define CMPOP_MASK 0x7
  2076. #define SIGN_BIT (0x1 << 3)
  2077. #define PREDICATE_BIT (0x1 << 4)
  2078. int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
  2079. int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
  2080. union ieee754sp fs, ft;
  2081. /* This is an R6 only instruction */
  2082. if (!cpu_has_mips_r6 ||
  2083. (MIPSInst_FUNC(ir) & 0x20))
  2084. return SIGILL;
  2085. /* fmt is w_fmt for single precision so fix it */
  2086. rfmt = s_fmt;
  2087. /* default to false */
  2088. rv.w = 0;
  2089. /* CMP.condn.S */
  2090. SPFROMREG(fs, MIPSInst_FS(ir));
  2091. SPFROMREG(ft, MIPSInst_FT(ir));
  2092. /* positive predicates */
  2093. if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
  2094. if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
  2095. sig))
  2096. rv.w = -1; /* true, all 1s */
  2097. if ((sig) &&
  2098. ieee754_cxtest(IEEE754_INVALID_OPERATION))
  2099. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  2100. else
  2101. goto copcsr;
  2102. } else {
  2103. /* negative predicates */
  2104. switch (cmpop) {
  2105. case 1:
  2106. case 2:
  2107. case 3:
  2108. if (ieee754sp_cmp(fs, ft,
  2109. negative_cmptab[cmpop],
  2110. sig))
  2111. rv.w = -1; /* true, all 1s */
  2112. if (sig &&
  2113. ieee754_cxtest(IEEE754_INVALID_OPERATION))
  2114. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  2115. else
  2116. goto copcsr;
  2117. break;
  2118. default:
  2119. /* Reserved R6 ops */
  2120. pr_err("Reserved MIPS R6 CMP.condn.S operation\n");
  2121. return SIGILL;
  2122. }
  2123. }
  2124. break;
  2125. }
  2126. }
  2127. }
  2128. case l_fmt:
  2129. if (!cpu_has_mips_3_4_5_64_r2_r6)
  2130. return SIGILL;
  2131. DIFROMREG(bits, MIPSInst_FS(ir));
  2132. switch (MIPSInst_FUNC(ir)) {
  2133. case fcvts_op:
  2134. /* convert long to single precision real */
  2135. rv.s = ieee754sp_flong(bits);
  2136. rfmt = s_fmt;
  2137. goto copcsr;
  2138. case fcvtd_op:
  2139. /* convert long to double precision real */
  2140. rv.d = ieee754dp_flong(bits);
  2141. rfmt = d_fmt;
  2142. goto copcsr;
  2143. default: {
  2144. /* Emulating the new CMP.condn.fmt R6 instruction */
  2145. int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
  2146. int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
  2147. union ieee754dp fs, ft;
  2148. if (!cpu_has_mips_r6 ||
  2149. (MIPSInst_FUNC(ir) & 0x20))
  2150. return SIGILL;
  2151. /* fmt is l_fmt for double precision so fix it */
  2152. rfmt = d_fmt;
  2153. /* default to false */
  2154. rv.l = 0;
  2155. /* CMP.condn.D */
  2156. DPFROMREG(fs, MIPSInst_FS(ir));
  2157. DPFROMREG(ft, MIPSInst_FT(ir));
  2158. /* positive predicates */
  2159. if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
  2160. if (ieee754dp_cmp(fs, ft,
  2161. cmptab[cmpop], sig))
  2162. rv.l = -1LL; /* true, all 1s */
  2163. if (sig &&
  2164. ieee754_cxtest(IEEE754_INVALID_OPERATION))
  2165. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  2166. else
  2167. goto copcsr;
  2168. } else {
  2169. /* negative predicates */
  2170. switch (cmpop) {
  2171. case 1:
  2172. case 2:
  2173. case 3:
  2174. if (ieee754dp_cmp(fs, ft,
  2175. negative_cmptab[cmpop],
  2176. sig))
  2177. rv.l = -1LL; /* true, all 1s */
  2178. if (sig &&
  2179. ieee754_cxtest(IEEE754_INVALID_OPERATION))
  2180. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  2181. else
  2182. goto copcsr;
  2183. break;
  2184. default:
  2185. /* Reserved R6 ops */
  2186. pr_err("Reserved MIPS R6 CMP.condn.D operation\n");
  2187. return SIGILL;
  2188. }
  2189. }
  2190. break;
  2191. }
  2192. }
  2193. default:
  2194. return SIGILL;
  2195. }
  2196. /*
  2197. * Update the fpu CSR register for this operation.
  2198. * If an exception is required, generate a tidy SIGFPE exception,
  2199. * without updating the result register.
  2200. * Note: cause exception bits do not accumulate, they are rewritten
  2201. * for each op; only the flag/sticky bits accumulate.
  2202. */
  2203. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  2204. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  2205. /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
  2206. return SIGFPE;
  2207. }
  2208. /*
  2209. * Now we can safely write the result back to the register file.
  2210. */
  2211. switch (rfmt) {
  2212. case -1:
  2213. if (cpu_has_mips_4_5_r)
  2214. cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
  2215. else
  2216. cbit = FPU_CSR_COND;
  2217. if (rv.w)
  2218. ctx->fcr31 |= cbit;
  2219. else
  2220. ctx->fcr31 &= ~cbit;
  2221. break;
  2222. case d_fmt:
  2223. DPTOREG(rv.d, MIPSInst_FD(ir));
  2224. break;
  2225. case s_fmt:
  2226. SPTOREG(rv.s, MIPSInst_FD(ir));
  2227. break;
  2228. case w_fmt:
  2229. SITOREG(rv.w, MIPSInst_FD(ir));
  2230. break;
  2231. case l_fmt:
  2232. if (!cpu_has_mips_3_4_5_64_r2_r6)
  2233. return SIGILL;
  2234. DITOREG(rv.l, MIPSInst_FD(ir));
  2235. break;
  2236. default:
  2237. return SIGILL;
  2238. }
  2239. return 0;
  2240. }
  2241. int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  2242. int has_fpu, void *__user *fault_addr)
  2243. {
  2244. unsigned long oldepc, prevepc;
  2245. struct mm_decoded_insn dec_insn;
  2246. u16 instr[4];
  2247. u16 *instr_ptr;
  2248. int sig = 0;
  2249. oldepc = xcp->cp0_epc;
  2250. do {
  2251. prevepc = xcp->cp0_epc;
  2252. if (get_isa16_mode(prevepc) && cpu_has_mmips) {
  2253. /*
  2254. * Get next 2 microMIPS instructions and convert them
  2255. * into 32-bit instructions.
  2256. */
  2257. if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
  2258. (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
  2259. (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
  2260. (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
  2261. MIPS_FPU_EMU_INC_STATS(errors);
  2262. return SIGBUS;
  2263. }
  2264. instr_ptr = instr;
  2265. /* Get first instruction. */
  2266. if (mm_insn_16bit(*instr_ptr)) {
  2267. /* Duplicate the half-word. */
  2268. dec_insn.insn = (*instr_ptr << 16) |
  2269. (*instr_ptr);
  2270. /* 16-bit instruction. */
  2271. dec_insn.pc_inc = 2;
  2272. instr_ptr += 1;
  2273. } else {
  2274. dec_insn.insn = (*instr_ptr << 16) |
  2275. *(instr_ptr+1);
  2276. /* 32-bit instruction. */
  2277. dec_insn.pc_inc = 4;
  2278. instr_ptr += 2;
  2279. }
  2280. /* Get second instruction. */
  2281. if (mm_insn_16bit(*instr_ptr)) {
  2282. /* Duplicate the half-word. */
  2283. dec_insn.next_insn = (*instr_ptr << 16) |
  2284. (*instr_ptr);
  2285. /* 16-bit instruction. */
  2286. dec_insn.next_pc_inc = 2;
  2287. } else {
  2288. dec_insn.next_insn = (*instr_ptr << 16) |
  2289. *(instr_ptr+1);
  2290. /* 32-bit instruction. */
  2291. dec_insn.next_pc_inc = 4;
  2292. }
  2293. dec_insn.micro_mips_mode = 1;
  2294. } else {
  2295. if ((get_user(dec_insn.insn,
  2296. (mips_instruction __user *) xcp->cp0_epc)) ||
  2297. (get_user(dec_insn.next_insn,
  2298. (mips_instruction __user *)(xcp->cp0_epc+4)))) {
  2299. MIPS_FPU_EMU_INC_STATS(errors);
  2300. return SIGBUS;
  2301. }
  2302. dec_insn.pc_inc = 4;
  2303. dec_insn.next_pc_inc = 4;
  2304. dec_insn.micro_mips_mode = 0;
  2305. }
  2306. if ((dec_insn.insn == 0) ||
  2307. ((dec_insn.pc_inc == 2) &&
  2308. ((dec_insn.insn & 0xffff) == MM_NOP16)))
  2309. xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
  2310. else {
  2311. /*
  2312. * The 'ieee754_csr' is an alias of ctx->fcr31.
  2313. * No need to copy ctx->fcr31 to ieee754_csr.
  2314. */
  2315. sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
  2316. }
  2317. if (has_fpu)
  2318. break;
  2319. if (sig)
  2320. break;
  2321. cond_resched();
  2322. } while (xcp->cp0_epc > prevepc);
  2323. /* SIGILL indicates a non-fpu instruction */
  2324. if (sig == SIGILL && xcp->cp0_epc != oldepc)
  2325. /* but if EPC has advanced, then ignore it */
  2326. sig = 0;
  2327. return sig;
  2328. }