cp1emu.c 61 KB

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