cp1emu.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  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. * 59 Temple Place - Suite 330, Boston MA 02111-1307, 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/module.h>
  38. #include <linux/debugfs.h>
  39. #include <linux/perf_event.h>
  40. #include <asm/inst.h>
  41. #include <asm/bootinfo.h>
  42. #include <asm/processor.h>
  43. #include <asm/ptrace.h>
  44. #include <asm/signal.h>
  45. #include <asm/mipsregs.h>
  46. #include <asm/fpu_emulator.h>
  47. #include <asm/fpu.h>
  48. #include <asm/uaccess.h>
  49. #include <asm/branch.h>
  50. #include "ieee754.h"
  51. /* Strap kernel emulator for full MIPS IV emulation */
  52. #ifdef __mips
  53. #undef __mips
  54. #endif
  55. #define __mips 4
  56. /* Function which emulates a floating point instruction. */
  57. static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
  58. mips_instruction);
  59. #if __mips >= 4 && __mips != 32
  60. static int fpux_emu(struct pt_regs *,
  61. struct mips_fpu_struct *, mips_instruction, void *__user *);
  62. #endif
  63. /* Further private data for which no space exists in mips_fpu_struct */
  64. #ifdef CONFIG_DEBUG_FS
  65. DEFINE_PER_CPU(struct mips_fpu_emulator_stats, fpuemustats);
  66. #endif
  67. /* Control registers */
  68. #define FPCREG_RID 0 /* $0 = revision id */
  69. #define FPCREG_CSR 31 /* $31 = csr */
  70. /* Determine rounding mode from the RM bits of the FCSR */
  71. #define modeindex(v) ((v) & FPU_CSR_RM)
  72. /* microMIPS bitfields */
  73. #define MM_POOL32A_MINOR_MASK 0x3f
  74. #define MM_POOL32A_MINOR_SHIFT 0x6
  75. #define MM_MIPS32_COND_FC 0x30
  76. /* Convert Mips rounding mode (0..3) to IEEE library modes. */
  77. static const unsigned char ieee_rm[4] = {
  78. [FPU_CSR_RN] = IEEE754_RN,
  79. [FPU_CSR_RZ] = IEEE754_RZ,
  80. [FPU_CSR_RU] = IEEE754_RU,
  81. [FPU_CSR_RD] = IEEE754_RD,
  82. };
  83. /* Convert IEEE library modes to Mips rounding mode (0..3). */
  84. static const unsigned char mips_rm[4] = {
  85. [IEEE754_RN] = FPU_CSR_RN,
  86. [IEEE754_RZ] = FPU_CSR_RZ,
  87. [IEEE754_RD] = FPU_CSR_RD,
  88. [IEEE754_RU] = FPU_CSR_RU,
  89. };
  90. #if __mips >= 4
  91. /* convert condition code register number to csr bit */
  92. static const unsigned int fpucondbit[8] = {
  93. FPU_CSR_COND0,
  94. FPU_CSR_COND1,
  95. FPU_CSR_COND2,
  96. FPU_CSR_COND3,
  97. FPU_CSR_COND4,
  98. FPU_CSR_COND5,
  99. FPU_CSR_COND6,
  100. FPU_CSR_COND7
  101. };
  102. #endif
  103. /* (microMIPS) Convert 16-bit register encoding to 32-bit register encoding. */
  104. static const unsigned int reg16to32map[8] = {16, 17, 2, 3, 4, 5, 6, 7};
  105. /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
  106. static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
  107. static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
  108. static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
  109. static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
  110. /*
  111. * This functions translates a 32-bit microMIPS instruction
  112. * into a 32-bit MIPS32 instruction. Returns 0 on success
  113. * and SIGILL otherwise.
  114. */
  115. static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
  116. {
  117. union mips_instruction insn = *insn_ptr;
  118. union mips_instruction mips32_insn = insn;
  119. int func, fmt, op;
  120. switch (insn.mm_i_format.opcode) {
  121. case mm_ldc132_op:
  122. mips32_insn.mm_i_format.opcode = ldc1_op;
  123. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  124. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  125. break;
  126. case mm_lwc132_op:
  127. mips32_insn.mm_i_format.opcode = lwc1_op;
  128. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  129. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  130. break;
  131. case mm_sdc132_op:
  132. mips32_insn.mm_i_format.opcode = sdc1_op;
  133. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  134. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  135. break;
  136. case mm_swc132_op:
  137. mips32_insn.mm_i_format.opcode = swc1_op;
  138. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  139. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  140. break;
  141. case mm_pool32i_op:
  142. /* NOTE: offset is << by 1 if in microMIPS mode. */
  143. if ((insn.mm_i_format.rt == mm_bc1f_op) ||
  144. (insn.mm_i_format.rt == mm_bc1t_op)) {
  145. mips32_insn.fb_format.opcode = cop1_op;
  146. mips32_insn.fb_format.bc = bc_op;
  147. mips32_insn.fb_format.flag =
  148. (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
  149. } else
  150. return SIGILL;
  151. break;
  152. case mm_pool32f_op:
  153. switch (insn.mm_fp0_format.func) {
  154. case mm_32f_01_op:
  155. case mm_32f_11_op:
  156. case mm_32f_02_op:
  157. case mm_32f_12_op:
  158. case mm_32f_41_op:
  159. case mm_32f_51_op:
  160. case mm_32f_42_op:
  161. case mm_32f_52_op:
  162. op = insn.mm_fp0_format.func;
  163. if (op == mm_32f_01_op)
  164. func = madd_s_op;
  165. else if (op == mm_32f_11_op)
  166. func = madd_d_op;
  167. else if (op == mm_32f_02_op)
  168. func = nmadd_s_op;
  169. else if (op == mm_32f_12_op)
  170. func = nmadd_d_op;
  171. else if (op == mm_32f_41_op)
  172. func = msub_s_op;
  173. else if (op == mm_32f_51_op)
  174. func = msub_d_op;
  175. else if (op == mm_32f_42_op)
  176. func = nmsub_s_op;
  177. else
  178. func = nmsub_d_op;
  179. mips32_insn.fp6_format.opcode = cop1x_op;
  180. mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
  181. mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
  182. mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
  183. mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
  184. mips32_insn.fp6_format.func = func;
  185. break;
  186. case mm_32f_10_op:
  187. func = -1; /* Invalid */
  188. op = insn.mm_fp5_format.op & 0x7;
  189. if (op == mm_ldxc1_op)
  190. func = ldxc1_op;
  191. else if (op == mm_sdxc1_op)
  192. func = sdxc1_op;
  193. else if (op == mm_lwxc1_op)
  194. func = lwxc1_op;
  195. else if (op == mm_swxc1_op)
  196. func = swxc1_op;
  197. if (func != -1) {
  198. mips32_insn.r_format.opcode = cop1x_op;
  199. mips32_insn.r_format.rs =
  200. insn.mm_fp5_format.base;
  201. mips32_insn.r_format.rt =
  202. insn.mm_fp5_format.index;
  203. mips32_insn.r_format.rd = 0;
  204. mips32_insn.r_format.re = insn.mm_fp5_format.fd;
  205. mips32_insn.r_format.func = func;
  206. } else
  207. return SIGILL;
  208. break;
  209. case mm_32f_40_op:
  210. op = -1; /* Invalid */
  211. if (insn.mm_fp2_format.op == mm_fmovt_op)
  212. op = 1;
  213. else if (insn.mm_fp2_format.op == mm_fmovf_op)
  214. op = 0;
  215. if (op != -1) {
  216. mips32_insn.fp0_format.opcode = cop1_op;
  217. mips32_insn.fp0_format.fmt =
  218. sdps_format[insn.mm_fp2_format.fmt];
  219. mips32_insn.fp0_format.ft =
  220. (insn.mm_fp2_format.cc<<2) + op;
  221. mips32_insn.fp0_format.fs =
  222. insn.mm_fp2_format.fs;
  223. mips32_insn.fp0_format.fd =
  224. insn.mm_fp2_format.fd;
  225. mips32_insn.fp0_format.func = fmovc_op;
  226. } else
  227. return SIGILL;
  228. break;
  229. case mm_32f_60_op:
  230. func = -1; /* Invalid */
  231. if (insn.mm_fp0_format.op == mm_fadd_op)
  232. func = fadd_op;
  233. else if (insn.mm_fp0_format.op == mm_fsub_op)
  234. func = fsub_op;
  235. else if (insn.mm_fp0_format.op == mm_fmul_op)
  236. func = fmul_op;
  237. else if (insn.mm_fp0_format.op == mm_fdiv_op)
  238. func = fdiv_op;
  239. if (func != -1) {
  240. mips32_insn.fp0_format.opcode = cop1_op;
  241. mips32_insn.fp0_format.fmt =
  242. sdps_format[insn.mm_fp0_format.fmt];
  243. mips32_insn.fp0_format.ft =
  244. insn.mm_fp0_format.ft;
  245. mips32_insn.fp0_format.fs =
  246. insn.mm_fp0_format.fs;
  247. mips32_insn.fp0_format.fd =
  248. insn.mm_fp0_format.fd;
  249. mips32_insn.fp0_format.func = func;
  250. } else
  251. return SIGILL;
  252. break;
  253. case mm_32f_70_op:
  254. func = -1; /* Invalid */
  255. if (insn.mm_fp0_format.op == mm_fmovn_op)
  256. func = fmovn_op;
  257. else if (insn.mm_fp0_format.op == mm_fmovz_op)
  258. func = fmovz_op;
  259. if (func != -1) {
  260. mips32_insn.fp0_format.opcode = cop1_op;
  261. mips32_insn.fp0_format.fmt =
  262. sdps_format[insn.mm_fp0_format.fmt];
  263. mips32_insn.fp0_format.ft =
  264. insn.mm_fp0_format.ft;
  265. mips32_insn.fp0_format.fs =
  266. insn.mm_fp0_format.fs;
  267. mips32_insn.fp0_format.fd =
  268. insn.mm_fp0_format.fd;
  269. mips32_insn.fp0_format.func = func;
  270. } else
  271. return SIGILL;
  272. break;
  273. case mm_32f_73_op: /* POOL32FXF */
  274. switch (insn.mm_fp1_format.op) {
  275. case mm_movf0_op:
  276. case mm_movf1_op:
  277. case mm_movt0_op:
  278. case mm_movt1_op:
  279. if ((insn.mm_fp1_format.op & 0x7f) ==
  280. mm_movf0_op)
  281. op = 0;
  282. else
  283. op = 1;
  284. mips32_insn.r_format.opcode = spec_op;
  285. mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
  286. mips32_insn.r_format.rt =
  287. (insn.mm_fp4_format.cc << 2) + op;
  288. mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
  289. mips32_insn.r_format.re = 0;
  290. mips32_insn.r_format.func = movc_op;
  291. break;
  292. case mm_fcvtd0_op:
  293. case mm_fcvtd1_op:
  294. case mm_fcvts0_op:
  295. case mm_fcvts1_op:
  296. if ((insn.mm_fp1_format.op & 0x7f) ==
  297. mm_fcvtd0_op) {
  298. func = fcvtd_op;
  299. fmt = swl_format[insn.mm_fp3_format.fmt];
  300. } else {
  301. func = fcvts_op;
  302. fmt = dwl_format[insn.mm_fp3_format.fmt];
  303. }
  304. mips32_insn.fp0_format.opcode = cop1_op;
  305. mips32_insn.fp0_format.fmt = fmt;
  306. mips32_insn.fp0_format.ft = 0;
  307. mips32_insn.fp0_format.fs =
  308. insn.mm_fp3_format.fs;
  309. mips32_insn.fp0_format.fd =
  310. insn.mm_fp3_format.rt;
  311. mips32_insn.fp0_format.func = func;
  312. break;
  313. case mm_fmov0_op:
  314. case mm_fmov1_op:
  315. case mm_fabs0_op:
  316. case mm_fabs1_op:
  317. case mm_fneg0_op:
  318. case mm_fneg1_op:
  319. if ((insn.mm_fp1_format.op & 0x7f) ==
  320. mm_fmov0_op)
  321. func = fmov_op;
  322. else if ((insn.mm_fp1_format.op & 0x7f) ==
  323. mm_fabs0_op)
  324. func = fabs_op;
  325. else
  326. func = fneg_op;
  327. mips32_insn.fp0_format.opcode = cop1_op;
  328. mips32_insn.fp0_format.fmt =
  329. sdps_format[insn.mm_fp3_format.fmt];
  330. mips32_insn.fp0_format.ft = 0;
  331. mips32_insn.fp0_format.fs =
  332. insn.mm_fp3_format.fs;
  333. mips32_insn.fp0_format.fd =
  334. insn.mm_fp3_format.rt;
  335. mips32_insn.fp0_format.func = func;
  336. break;
  337. case mm_ffloorl_op:
  338. case mm_ffloorw_op:
  339. case mm_fceill_op:
  340. case mm_fceilw_op:
  341. case mm_ftruncl_op:
  342. case mm_ftruncw_op:
  343. case mm_froundl_op:
  344. case mm_froundw_op:
  345. case mm_fcvtl_op:
  346. case mm_fcvtw_op:
  347. if (insn.mm_fp1_format.op == mm_ffloorl_op)
  348. func = ffloorl_op;
  349. else if (insn.mm_fp1_format.op == mm_ffloorw_op)
  350. func = ffloor_op;
  351. else if (insn.mm_fp1_format.op == mm_fceill_op)
  352. func = fceill_op;
  353. else if (insn.mm_fp1_format.op == mm_fceilw_op)
  354. func = fceil_op;
  355. else if (insn.mm_fp1_format.op == mm_ftruncl_op)
  356. func = ftruncl_op;
  357. else if (insn.mm_fp1_format.op == mm_ftruncw_op)
  358. func = ftrunc_op;
  359. else if (insn.mm_fp1_format.op == mm_froundl_op)
  360. func = froundl_op;
  361. else if (insn.mm_fp1_format.op == mm_froundw_op)
  362. func = fround_op;
  363. else if (insn.mm_fp1_format.op == mm_fcvtl_op)
  364. func = fcvtl_op;
  365. else
  366. func = fcvtw_op;
  367. mips32_insn.fp0_format.opcode = cop1_op;
  368. mips32_insn.fp0_format.fmt =
  369. sd_format[insn.mm_fp1_format.fmt];
  370. mips32_insn.fp0_format.ft = 0;
  371. mips32_insn.fp0_format.fs =
  372. insn.mm_fp1_format.fs;
  373. mips32_insn.fp0_format.fd =
  374. insn.mm_fp1_format.rt;
  375. mips32_insn.fp0_format.func = func;
  376. break;
  377. case mm_frsqrt_op:
  378. case mm_fsqrt_op:
  379. case mm_frecip_op:
  380. if (insn.mm_fp1_format.op == mm_frsqrt_op)
  381. func = frsqrt_op;
  382. else if (insn.mm_fp1_format.op == mm_fsqrt_op)
  383. func = fsqrt_op;
  384. else
  385. func = frecip_op;
  386. mips32_insn.fp0_format.opcode = cop1_op;
  387. mips32_insn.fp0_format.fmt =
  388. sdps_format[insn.mm_fp1_format.fmt];
  389. mips32_insn.fp0_format.ft = 0;
  390. mips32_insn.fp0_format.fs =
  391. insn.mm_fp1_format.fs;
  392. mips32_insn.fp0_format.fd =
  393. insn.mm_fp1_format.rt;
  394. mips32_insn.fp0_format.func = func;
  395. break;
  396. case mm_mfc1_op:
  397. case mm_mtc1_op:
  398. case mm_cfc1_op:
  399. case mm_ctc1_op:
  400. if (insn.mm_fp1_format.op == mm_mfc1_op)
  401. op = mfc_op;
  402. else if (insn.mm_fp1_format.op == mm_mtc1_op)
  403. op = mtc_op;
  404. else if (insn.mm_fp1_format.op == mm_cfc1_op)
  405. op = cfc_op;
  406. else
  407. op = ctc_op;
  408. mips32_insn.fp1_format.opcode = cop1_op;
  409. mips32_insn.fp1_format.op = op;
  410. mips32_insn.fp1_format.rt =
  411. insn.mm_fp1_format.rt;
  412. mips32_insn.fp1_format.fs =
  413. insn.mm_fp1_format.fs;
  414. mips32_insn.fp1_format.fd = 0;
  415. mips32_insn.fp1_format.func = 0;
  416. break;
  417. default:
  418. return SIGILL;
  419. break;
  420. }
  421. break;
  422. case mm_32f_74_op: /* c.cond.fmt */
  423. mips32_insn.fp0_format.opcode = cop1_op;
  424. mips32_insn.fp0_format.fmt =
  425. sdps_format[insn.mm_fp4_format.fmt];
  426. mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
  427. mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
  428. mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
  429. mips32_insn.fp0_format.func =
  430. insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
  431. break;
  432. default:
  433. return SIGILL;
  434. break;
  435. }
  436. break;
  437. default:
  438. return SIGILL;
  439. break;
  440. }
  441. *insn_ptr = mips32_insn;
  442. return 0;
  443. }
  444. int mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
  445. unsigned long *contpc)
  446. {
  447. union mips_instruction insn = (union mips_instruction)dec_insn.insn;
  448. int bc_false = 0;
  449. unsigned int fcr31;
  450. unsigned int bit;
  451. if (!cpu_has_mmips)
  452. return 0;
  453. switch (insn.mm_i_format.opcode) {
  454. case mm_pool32a_op:
  455. if ((insn.mm_i_format.simmediate & MM_POOL32A_MINOR_MASK) ==
  456. mm_pool32axf_op) {
  457. switch (insn.mm_i_format.simmediate >>
  458. MM_POOL32A_MINOR_SHIFT) {
  459. case mm_jalr_op:
  460. case mm_jalrhb_op:
  461. case mm_jalrs_op:
  462. case mm_jalrshb_op:
  463. if (insn.mm_i_format.rt != 0) /* Not mm_jr */
  464. regs->regs[insn.mm_i_format.rt] =
  465. regs->cp0_epc +
  466. dec_insn.pc_inc +
  467. dec_insn.next_pc_inc;
  468. *contpc = regs->regs[insn.mm_i_format.rs];
  469. return 1;
  470. break;
  471. }
  472. }
  473. break;
  474. case mm_pool32i_op:
  475. switch (insn.mm_i_format.rt) {
  476. case mm_bltzals_op:
  477. case mm_bltzal_op:
  478. regs->regs[31] = regs->cp0_epc +
  479. dec_insn.pc_inc +
  480. dec_insn.next_pc_inc;
  481. /* Fall through */
  482. case mm_bltz_op:
  483. if ((long)regs->regs[insn.mm_i_format.rs] < 0)
  484. *contpc = regs->cp0_epc +
  485. dec_insn.pc_inc +
  486. (insn.mm_i_format.simmediate << 1);
  487. else
  488. *contpc = regs->cp0_epc +
  489. dec_insn.pc_inc +
  490. dec_insn.next_pc_inc;
  491. return 1;
  492. break;
  493. case mm_bgezals_op:
  494. case mm_bgezal_op:
  495. regs->regs[31] = regs->cp0_epc +
  496. dec_insn.pc_inc +
  497. dec_insn.next_pc_inc;
  498. /* Fall through */
  499. case mm_bgez_op:
  500. if ((long)regs->regs[insn.mm_i_format.rs] >= 0)
  501. *contpc = regs->cp0_epc +
  502. dec_insn.pc_inc +
  503. (insn.mm_i_format.simmediate << 1);
  504. else
  505. *contpc = regs->cp0_epc +
  506. dec_insn.pc_inc +
  507. dec_insn.next_pc_inc;
  508. return 1;
  509. break;
  510. case mm_blez_op:
  511. if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
  512. *contpc = regs->cp0_epc +
  513. dec_insn.pc_inc +
  514. (insn.mm_i_format.simmediate << 1);
  515. else
  516. *contpc = regs->cp0_epc +
  517. dec_insn.pc_inc +
  518. dec_insn.next_pc_inc;
  519. return 1;
  520. break;
  521. case mm_bgtz_op:
  522. if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
  523. *contpc = regs->cp0_epc +
  524. dec_insn.pc_inc +
  525. (insn.mm_i_format.simmediate << 1);
  526. else
  527. *contpc = regs->cp0_epc +
  528. dec_insn.pc_inc +
  529. dec_insn.next_pc_inc;
  530. return 1;
  531. break;
  532. case mm_bc2f_op:
  533. case mm_bc1f_op:
  534. bc_false = 1;
  535. /* Fall through */
  536. case mm_bc2t_op:
  537. case mm_bc1t_op:
  538. preempt_disable();
  539. if (is_fpu_owner())
  540. asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
  541. else
  542. fcr31 = current->thread.fpu.fcr31;
  543. preempt_enable();
  544. if (bc_false)
  545. fcr31 = ~fcr31;
  546. bit = (insn.mm_i_format.rs >> 2);
  547. bit += (bit != 0);
  548. bit += 23;
  549. if (fcr31 & (1 << bit))
  550. *contpc = regs->cp0_epc +
  551. dec_insn.pc_inc +
  552. (insn.mm_i_format.simmediate << 1);
  553. else
  554. *contpc = regs->cp0_epc +
  555. dec_insn.pc_inc + dec_insn.next_pc_inc;
  556. return 1;
  557. break;
  558. }
  559. break;
  560. case mm_pool16c_op:
  561. switch (insn.mm_i_format.rt) {
  562. case mm_jalr16_op:
  563. case mm_jalrs16_op:
  564. regs->regs[31] = regs->cp0_epc +
  565. dec_insn.pc_inc + dec_insn.next_pc_inc;
  566. /* Fall through */
  567. case mm_jr16_op:
  568. *contpc = regs->regs[insn.mm_i_format.rs];
  569. return 1;
  570. break;
  571. }
  572. break;
  573. case mm_beqz16_op:
  574. if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] == 0)
  575. *contpc = regs->cp0_epc +
  576. dec_insn.pc_inc +
  577. (insn.mm_b1_format.simmediate << 1);
  578. else
  579. *contpc = regs->cp0_epc +
  580. dec_insn.pc_inc + dec_insn.next_pc_inc;
  581. return 1;
  582. break;
  583. case mm_bnez16_op:
  584. if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] != 0)
  585. *contpc = regs->cp0_epc +
  586. dec_insn.pc_inc +
  587. (insn.mm_b1_format.simmediate << 1);
  588. else
  589. *contpc = regs->cp0_epc +
  590. dec_insn.pc_inc + dec_insn.next_pc_inc;
  591. return 1;
  592. break;
  593. case mm_b16_op:
  594. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  595. (insn.mm_b0_format.simmediate << 1);
  596. return 1;
  597. break;
  598. case mm_beq32_op:
  599. if (regs->regs[insn.mm_i_format.rs] ==
  600. regs->regs[insn.mm_i_format.rt])
  601. *contpc = regs->cp0_epc +
  602. dec_insn.pc_inc +
  603. (insn.mm_i_format.simmediate << 1);
  604. else
  605. *contpc = regs->cp0_epc +
  606. dec_insn.pc_inc +
  607. dec_insn.next_pc_inc;
  608. return 1;
  609. break;
  610. case mm_bne32_op:
  611. if (regs->regs[insn.mm_i_format.rs] !=
  612. regs->regs[insn.mm_i_format.rt])
  613. *contpc = regs->cp0_epc +
  614. dec_insn.pc_inc +
  615. (insn.mm_i_format.simmediate << 1);
  616. else
  617. *contpc = regs->cp0_epc +
  618. dec_insn.pc_inc + dec_insn.next_pc_inc;
  619. return 1;
  620. break;
  621. case mm_jalx32_op:
  622. regs->regs[31] = regs->cp0_epc +
  623. dec_insn.pc_inc + dec_insn.next_pc_inc;
  624. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  625. *contpc >>= 28;
  626. *contpc <<= 28;
  627. *contpc |= (insn.j_format.target << 2);
  628. return 1;
  629. break;
  630. case mm_jals32_op:
  631. case mm_jal32_op:
  632. regs->regs[31] = regs->cp0_epc +
  633. dec_insn.pc_inc + dec_insn.next_pc_inc;
  634. /* Fall through */
  635. case mm_j32_op:
  636. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  637. *contpc >>= 27;
  638. *contpc <<= 27;
  639. *contpc |= (insn.j_format.target << 1);
  640. set_isa16_mode(*contpc);
  641. return 1;
  642. break;
  643. }
  644. return 0;
  645. }
  646. /*
  647. * Redundant with logic already in kernel/branch.c,
  648. * embedded in compute_return_epc. At some point,
  649. * a single subroutine should be used across both
  650. * modules.
  651. */
  652. static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
  653. unsigned long *contpc)
  654. {
  655. union mips_instruction insn = (union mips_instruction)dec_insn.insn;
  656. unsigned int fcr31;
  657. unsigned int bit = 0;
  658. switch (insn.i_format.opcode) {
  659. case spec_op:
  660. switch (insn.r_format.func) {
  661. case jalr_op:
  662. regs->regs[insn.r_format.rd] =
  663. regs->cp0_epc + dec_insn.pc_inc +
  664. dec_insn.next_pc_inc;
  665. /* Fall through */
  666. case jr_op:
  667. *contpc = regs->regs[insn.r_format.rs];
  668. return 1;
  669. break;
  670. }
  671. break;
  672. case bcond_op:
  673. switch (insn.i_format.rt) {
  674. case bltzal_op:
  675. case bltzall_op:
  676. regs->regs[31] = regs->cp0_epc +
  677. dec_insn.pc_inc +
  678. dec_insn.next_pc_inc;
  679. /* Fall through */
  680. case bltz_op:
  681. case bltzl_op:
  682. if ((long)regs->regs[insn.i_format.rs] < 0)
  683. *contpc = regs->cp0_epc +
  684. dec_insn.pc_inc +
  685. (insn.i_format.simmediate << 2);
  686. else
  687. *contpc = regs->cp0_epc +
  688. dec_insn.pc_inc +
  689. dec_insn.next_pc_inc;
  690. return 1;
  691. break;
  692. case bgezal_op:
  693. case bgezall_op:
  694. regs->regs[31] = regs->cp0_epc +
  695. dec_insn.pc_inc +
  696. dec_insn.next_pc_inc;
  697. /* Fall through */
  698. case bgez_op:
  699. case bgezl_op:
  700. if ((long)regs->regs[insn.i_format.rs] >= 0)
  701. *contpc = regs->cp0_epc +
  702. dec_insn.pc_inc +
  703. (insn.i_format.simmediate << 2);
  704. else
  705. *contpc = regs->cp0_epc +
  706. dec_insn.pc_inc +
  707. dec_insn.next_pc_inc;
  708. return 1;
  709. break;
  710. }
  711. break;
  712. case jalx_op:
  713. set_isa16_mode(bit);
  714. case jal_op:
  715. regs->regs[31] = regs->cp0_epc +
  716. dec_insn.pc_inc +
  717. dec_insn.next_pc_inc;
  718. /* Fall through */
  719. case j_op:
  720. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  721. *contpc >>= 28;
  722. *contpc <<= 28;
  723. *contpc |= (insn.j_format.target << 2);
  724. /* Set microMIPS mode bit: XOR for jalx. */
  725. *contpc ^= bit;
  726. return 1;
  727. break;
  728. case beq_op:
  729. case beql_op:
  730. if (regs->regs[insn.i_format.rs] ==
  731. regs->regs[insn.i_format.rt])
  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. break;
  741. case bne_op:
  742. case bnel_op:
  743. if (regs->regs[insn.i_format.rs] !=
  744. regs->regs[insn.i_format.rt])
  745. *contpc = regs->cp0_epc +
  746. dec_insn.pc_inc +
  747. (insn.i_format.simmediate << 2);
  748. else
  749. *contpc = regs->cp0_epc +
  750. dec_insn.pc_inc +
  751. dec_insn.next_pc_inc;
  752. return 1;
  753. break;
  754. case blez_op:
  755. case blezl_op:
  756. if ((long)regs->regs[insn.i_format.rs] <= 0)
  757. *contpc = regs->cp0_epc +
  758. dec_insn.pc_inc +
  759. (insn.i_format.simmediate << 2);
  760. else
  761. *contpc = regs->cp0_epc +
  762. dec_insn.pc_inc +
  763. dec_insn.next_pc_inc;
  764. return 1;
  765. break;
  766. case bgtz_op:
  767. case bgtzl_op:
  768. if ((long)regs->regs[insn.i_format.rs] > 0)
  769. *contpc = regs->cp0_epc +
  770. dec_insn.pc_inc +
  771. (insn.i_format.simmediate << 2);
  772. else
  773. *contpc = regs->cp0_epc +
  774. dec_insn.pc_inc +
  775. dec_insn.next_pc_inc;
  776. return 1;
  777. break;
  778. case cop0_op:
  779. case cop1_op:
  780. case cop2_op:
  781. case cop1x_op:
  782. if (insn.i_format.rs == bc_op) {
  783. preempt_disable();
  784. if (is_fpu_owner())
  785. asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
  786. else
  787. fcr31 = current->thread.fpu.fcr31;
  788. preempt_enable();
  789. bit = (insn.i_format.rt >> 2);
  790. bit += (bit != 0);
  791. bit += 23;
  792. switch (insn.i_format.rt & 3) {
  793. case 0: /* bc1f */
  794. case 2: /* bc1fl */
  795. if (~fcr31 & (1 << bit))
  796. *contpc = regs->cp0_epc +
  797. dec_insn.pc_inc +
  798. (insn.i_format.simmediate << 2);
  799. else
  800. *contpc = regs->cp0_epc +
  801. dec_insn.pc_inc +
  802. dec_insn.next_pc_inc;
  803. return 1;
  804. break;
  805. case 1: /* bc1t */
  806. case 3: /* bc1tl */
  807. if (fcr31 & (1 << bit))
  808. *contpc = regs->cp0_epc +
  809. dec_insn.pc_inc +
  810. (insn.i_format.simmediate << 2);
  811. else
  812. *contpc = regs->cp0_epc +
  813. dec_insn.pc_inc +
  814. dec_insn.next_pc_inc;
  815. return 1;
  816. break;
  817. }
  818. }
  819. break;
  820. }
  821. return 0;
  822. }
  823. /*
  824. * In the Linux kernel, we support selection of FPR format on the
  825. * basis of the Status.FR bit. If an FPU is not present, the FR bit
  826. * is hardwired to zero, which would imply a 32-bit FPU even for
  827. * 64-bit CPUs so we rather look at TIF_32BIT_REGS.
  828. * FPU emu is slow and bulky and optimizing this function offers fairly
  829. * sizeable benefits so we try to be clever and make this function return
  830. * a constant whenever possible, that is on 64-bit kernels without O32
  831. * compatibility enabled and on 32-bit kernels.
  832. */
  833. static inline int cop1_64bit(struct pt_regs *xcp)
  834. {
  835. #if defined(CONFIG_64BIT) && !defined(CONFIG_MIPS32_O32)
  836. return 1;
  837. #elif defined(CONFIG_64BIT) && defined(CONFIG_MIPS32_O32)
  838. return !test_thread_flag(TIF_32BIT_REGS);
  839. #else
  840. return 0;
  841. #endif
  842. }
  843. #define SIFROMREG(si, x) ((si) = cop1_64bit(xcp) || !(x & 1) ? \
  844. (int)ctx->fpr[x] : (int)(ctx->fpr[x & ~1] >> 32))
  845. #define SITOREG(si, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = \
  846. cop1_64bit(xcp) || !(x & 1) ? \
  847. ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
  848. ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
  849. #define DIFROMREG(di, x) ((di) = ctx->fpr[x & ~(cop1_64bit(xcp) == 0)])
  850. #define DITOREG(di, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = (di))
  851. #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
  852. #define SPTOREG(sp, x) SITOREG((sp).bits, x)
  853. #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
  854. #define DPTOREG(dp, x) DITOREG((dp).bits, x)
  855. /*
  856. * Emulate the single floating point instruction pointed at by EPC.
  857. * Two instructions if the instruction is in a branch delay slot.
  858. */
  859. static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  860. struct mm_decoded_insn dec_insn, void *__user *fault_addr)
  861. {
  862. mips_instruction ir;
  863. unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
  864. unsigned int cond;
  865. int pc_inc;
  866. /* XXX NEC Vr54xx bug workaround */
  867. if (xcp->cp0_cause & CAUSEF_BD) {
  868. if (dec_insn.micro_mips_mode) {
  869. if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
  870. xcp->cp0_cause &= ~CAUSEF_BD;
  871. } else {
  872. if (!isBranchInstr(xcp, dec_insn, &contpc))
  873. xcp->cp0_cause &= ~CAUSEF_BD;
  874. }
  875. }
  876. if (xcp->cp0_cause & CAUSEF_BD) {
  877. /*
  878. * The instruction to be emulated is in a branch delay slot
  879. * which means that we have to emulate the branch instruction
  880. * BEFORE we do the cop1 instruction.
  881. *
  882. * This branch could be a COP1 branch, but in that case we
  883. * would have had a trap for that instruction, and would not
  884. * come through this route.
  885. *
  886. * Linux MIPS branch emulator operates on context, updating the
  887. * cp0_epc.
  888. */
  889. ir = dec_insn.next_insn; /* process delay slot instr */
  890. pc_inc = dec_insn.next_pc_inc;
  891. } else {
  892. ir = dec_insn.insn; /* process current instr */
  893. pc_inc = dec_insn.pc_inc;
  894. }
  895. /*
  896. * Since microMIPS FPU instructios are a subset of MIPS32 FPU
  897. * instructions, we want to convert microMIPS FPU instructions
  898. * into MIPS32 instructions so that we could reuse all of the
  899. * FPU emulation code.
  900. *
  901. * NOTE: We cannot do this for branch instructions since they
  902. * are not a subset. Example: Cannot emulate a 16-bit
  903. * aligned target address with a MIPS32 instruction.
  904. */
  905. if (dec_insn.micro_mips_mode) {
  906. /*
  907. * If next instruction is a 16-bit instruction, then it
  908. * it cannot be a FPU instruction. This could happen
  909. * since we can be called for non-FPU instructions.
  910. */
  911. if ((pc_inc == 2) ||
  912. (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
  913. == SIGILL))
  914. return SIGILL;
  915. }
  916. emul:
  917. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
  918. MIPS_FPU_EMU_INC_STATS(emulated);
  919. switch (MIPSInst_OPCODE(ir)) {
  920. case ldc1_op:{
  921. u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  922. MIPSInst_SIMM(ir));
  923. u64 val;
  924. MIPS_FPU_EMU_INC_STATS(loads);
  925. if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
  926. MIPS_FPU_EMU_INC_STATS(errors);
  927. *fault_addr = va;
  928. return SIGBUS;
  929. }
  930. if (__get_user(val, va)) {
  931. MIPS_FPU_EMU_INC_STATS(errors);
  932. *fault_addr = va;
  933. return SIGSEGV;
  934. }
  935. DITOREG(val, MIPSInst_RT(ir));
  936. break;
  937. }
  938. case sdc1_op:{
  939. u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  940. MIPSInst_SIMM(ir));
  941. u64 val;
  942. MIPS_FPU_EMU_INC_STATS(stores);
  943. DIFROMREG(val, MIPSInst_RT(ir));
  944. if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
  945. MIPS_FPU_EMU_INC_STATS(errors);
  946. *fault_addr = va;
  947. return SIGBUS;
  948. }
  949. if (__put_user(val, va)) {
  950. MIPS_FPU_EMU_INC_STATS(errors);
  951. *fault_addr = va;
  952. return SIGSEGV;
  953. }
  954. break;
  955. }
  956. case lwc1_op:{
  957. u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  958. MIPSInst_SIMM(ir));
  959. u32 val;
  960. MIPS_FPU_EMU_INC_STATS(loads);
  961. if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
  962. MIPS_FPU_EMU_INC_STATS(errors);
  963. *fault_addr = va;
  964. return SIGBUS;
  965. }
  966. if (__get_user(val, va)) {
  967. MIPS_FPU_EMU_INC_STATS(errors);
  968. *fault_addr = va;
  969. return SIGSEGV;
  970. }
  971. SITOREG(val, MIPSInst_RT(ir));
  972. break;
  973. }
  974. case swc1_op:{
  975. u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  976. MIPSInst_SIMM(ir));
  977. u32 val;
  978. MIPS_FPU_EMU_INC_STATS(stores);
  979. SIFROMREG(val, MIPSInst_RT(ir));
  980. if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
  981. MIPS_FPU_EMU_INC_STATS(errors);
  982. *fault_addr = va;
  983. return SIGBUS;
  984. }
  985. if (__put_user(val, va)) {
  986. MIPS_FPU_EMU_INC_STATS(errors);
  987. *fault_addr = va;
  988. return SIGSEGV;
  989. }
  990. break;
  991. }
  992. case cop1_op:
  993. switch (MIPSInst_RS(ir)) {
  994. #if defined(__mips64)
  995. case dmfc_op:
  996. /* copregister fs -> gpr[rt] */
  997. if (MIPSInst_RT(ir) != 0) {
  998. DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  999. MIPSInst_RD(ir));
  1000. }
  1001. break;
  1002. case dmtc_op:
  1003. /* copregister fs <- rt */
  1004. DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1005. break;
  1006. #endif
  1007. case mfc_op:
  1008. /* copregister rd -> gpr[rt] */
  1009. if (MIPSInst_RT(ir) != 0) {
  1010. SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  1011. MIPSInst_RD(ir));
  1012. }
  1013. break;
  1014. case mtc_op:
  1015. /* copregister rd <- rt */
  1016. SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1017. break;
  1018. case cfc_op:{
  1019. /* cop control register rd -> gpr[rt] */
  1020. u32 value;
  1021. if (MIPSInst_RD(ir) == FPCREG_CSR) {
  1022. value = ctx->fcr31;
  1023. value = (value & ~FPU_CSR_RM) |
  1024. mips_rm[modeindex(value)];
  1025. #ifdef CSRTRACE
  1026. printk("%p gpr[%d]<-csr=%08x\n",
  1027. (void *) (xcp->cp0_epc),
  1028. MIPSInst_RT(ir), value);
  1029. #endif
  1030. }
  1031. else if (MIPSInst_RD(ir) == FPCREG_RID)
  1032. value = 0;
  1033. else
  1034. value = 0;
  1035. if (MIPSInst_RT(ir))
  1036. xcp->regs[MIPSInst_RT(ir)] = value;
  1037. break;
  1038. }
  1039. case ctc_op:{
  1040. /* copregister rd <- rt */
  1041. u32 value;
  1042. if (MIPSInst_RT(ir) == 0)
  1043. value = 0;
  1044. else
  1045. value = xcp->regs[MIPSInst_RT(ir)];
  1046. /* we only have one writable control reg
  1047. */
  1048. if (MIPSInst_RD(ir) == FPCREG_CSR) {
  1049. #ifdef CSRTRACE
  1050. printk("%p gpr[%d]->csr=%08x\n",
  1051. (void *) (xcp->cp0_epc),
  1052. MIPSInst_RT(ir), value);
  1053. #endif
  1054. /*
  1055. * Don't write reserved bits,
  1056. * and convert to ieee library modes
  1057. */
  1058. ctx->fcr31 = (value &
  1059. ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
  1060. ieee_rm[modeindex(value)];
  1061. }
  1062. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1063. return SIGFPE;
  1064. }
  1065. break;
  1066. }
  1067. case bc_op:{
  1068. int likely = 0;
  1069. if (xcp->cp0_cause & CAUSEF_BD)
  1070. return SIGILL;
  1071. #if __mips >= 4
  1072. cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
  1073. #else
  1074. cond = ctx->fcr31 & FPU_CSR_COND;
  1075. #endif
  1076. switch (MIPSInst_RT(ir) & 3) {
  1077. case bcfl_op:
  1078. likely = 1;
  1079. case bcf_op:
  1080. cond = !cond;
  1081. break;
  1082. case bctl_op:
  1083. likely = 1;
  1084. case bct_op:
  1085. break;
  1086. default:
  1087. /* thats an illegal instruction */
  1088. return SIGILL;
  1089. }
  1090. xcp->cp0_cause |= CAUSEF_BD;
  1091. if (cond) {
  1092. /* branch taken: emulate dslot
  1093. * instruction
  1094. */
  1095. xcp->cp0_epc += dec_insn.pc_inc;
  1096. contpc = MIPSInst_SIMM(ir);
  1097. ir = dec_insn.next_insn;
  1098. if (dec_insn.micro_mips_mode) {
  1099. contpc = (xcp->cp0_epc + (contpc << 1));
  1100. /* If 16-bit instruction, not FPU. */
  1101. if ((dec_insn.next_pc_inc == 2) ||
  1102. (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
  1103. /*
  1104. * Since this instruction will
  1105. * be put on the stack with
  1106. * 32-bit words, get around
  1107. * this problem by putting a
  1108. * NOP16 as the second one.
  1109. */
  1110. if (dec_insn.next_pc_inc == 2)
  1111. ir = (ir & (~0xffff)) | MM_NOP16;
  1112. /*
  1113. * Single step the non-CP1
  1114. * instruction in the dslot.
  1115. */
  1116. return mips_dsemul(xcp, ir, contpc);
  1117. }
  1118. } else
  1119. contpc = (xcp->cp0_epc + (contpc << 2));
  1120. switch (MIPSInst_OPCODE(ir)) {
  1121. case lwc1_op:
  1122. case swc1_op:
  1123. #if (__mips >= 2 || defined(__mips64))
  1124. case ldc1_op:
  1125. case sdc1_op:
  1126. #endif
  1127. case cop1_op:
  1128. #if __mips >= 4 && __mips != 32
  1129. case cop1x_op:
  1130. #endif
  1131. /* its one of ours */
  1132. goto emul;
  1133. #if __mips >= 4
  1134. case spec_op:
  1135. if (MIPSInst_FUNC(ir) == movc_op)
  1136. goto emul;
  1137. break;
  1138. #endif
  1139. }
  1140. /*
  1141. * Single step the non-cp1
  1142. * instruction in the dslot
  1143. */
  1144. return mips_dsemul(xcp, ir, contpc);
  1145. }
  1146. else {
  1147. /* branch not taken */
  1148. if (likely) {
  1149. /*
  1150. * branch likely nullifies
  1151. * dslot if not taken
  1152. */
  1153. xcp->cp0_epc += dec_insn.pc_inc;
  1154. contpc += dec_insn.pc_inc;
  1155. /*
  1156. * else continue & execute
  1157. * dslot as normal insn
  1158. */
  1159. }
  1160. }
  1161. break;
  1162. }
  1163. default:
  1164. if (!(MIPSInst_RS(ir) & 0x10))
  1165. return SIGILL;
  1166. {
  1167. int sig;
  1168. /* a real fpu computation instruction */
  1169. if ((sig = fpu_emu(xcp, ctx, ir)))
  1170. return sig;
  1171. }
  1172. }
  1173. break;
  1174. #if __mips >= 4 && __mips != 32
  1175. case cop1x_op:{
  1176. int sig = fpux_emu(xcp, ctx, ir, fault_addr);
  1177. if (sig)
  1178. return sig;
  1179. break;
  1180. }
  1181. #endif
  1182. #if __mips >= 4
  1183. case spec_op:
  1184. if (MIPSInst_FUNC(ir) != movc_op)
  1185. return SIGILL;
  1186. cond = fpucondbit[MIPSInst_RT(ir) >> 2];
  1187. if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
  1188. xcp->regs[MIPSInst_RD(ir)] =
  1189. xcp->regs[MIPSInst_RS(ir)];
  1190. break;
  1191. #endif
  1192. default:
  1193. return SIGILL;
  1194. }
  1195. /* we did it !! */
  1196. xcp->cp0_epc = contpc;
  1197. xcp->cp0_cause &= ~CAUSEF_BD;
  1198. return 0;
  1199. }
  1200. /*
  1201. * Conversion table from MIPS compare ops 48-63
  1202. * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
  1203. */
  1204. static const unsigned char cmptab[8] = {
  1205. 0, /* cmp_0 (sig) cmp_sf */
  1206. IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
  1207. IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
  1208. IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
  1209. IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
  1210. IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
  1211. IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
  1212. IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
  1213. };
  1214. #if __mips >= 4 && __mips != 32
  1215. /*
  1216. * Additional MIPS4 instructions
  1217. */
  1218. #define DEF3OP(name, p, f1, f2, f3) \
  1219. static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \
  1220. ieee754##p t) \
  1221. { \
  1222. struct _ieee754_csr ieee754_csr_save; \
  1223. s = f1(s, t); \
  1224. ieee754_csr_save = ieee754_csr; \
  1225. s = f2(s, r); \
  1226. ieee754_csr_save.cx |= ieee754_csr.cx; \
  1227. ieee754_csr_save.sx |= ieee754_csr.sx; \
  1228. s = f3(s); \
  1229. ieee754_csr.cx |= ieee754_csr_save.cx; \
  1230. ieee754_csr.sx |= ieee754_csr_save.sx; \
  1231. return s; \
  1232. }
  1233. static ieee754dp fpemu_dp_recip(ieee754dp d)
  1234. {
  1235. return ieee754dp_div(ieee754dp_one(0), d);
  1236. }
  1237. static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
  1238. {
  1239. return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
  1240. }
  1241. static ieee754sp fpemu_sp_recip(ieee754sp s)
  1242. {
  1243. return ieee754sp_div(ieee754sp_one(0), s);
  1244. }
  1245. static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
  1246. {
  1247. return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
  1248. }
  1249. DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
  1250. DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
  1251. DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
  1252. DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
  1253. DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
  1254. DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
  1255. DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
  1256. DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
  1257. static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1258. mips_instruction ir, void *__user *fault_addr)
  1259. {
  1260. unsigned rcsr = 0; /* resulting csr */
  1261. MIPS_FPU_EMU_INC_STATS(cp1xops);
  1262. switch (MIPSInst_FMA_FFMT(ir)) {
  1263. case s_fmt:{ /* 0 */
  1264. ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
  1265. ieee754sp fd, fr, fs, ft;
  1266. u32 __user *va;
  1267. u32 val;
  1268. switch (MIPSInst_FUNC(ir)) {
  1269. case lwxc1_op:
  1270. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1271. xcp->regs[MIPSInst_FT(ir)]);
  1272. MIPS_FPU_EMU_INC_STATS(loads);
  1273. if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
  1274. MIPS_FPU_EMU_INC_STATS(errors);
  1275. *fault_addr = va;
  1276. return SIGBUS;
  1277. }
  1278. if (__get_user(val, va)) {
  1279. MIPS_FPU_EMU_INC_STATS(errors);
  1280. *fault_addr = va;
  1281. return SIGSEGV;
  1282. }
  1283. SITOREG(val, MIPSInst_FD(ir));
  1284. break;
  1285. case swxc1_op:
  1286. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1287. xcp->regs[MIPSInst_FT(ir)]);
  1288. MIPS_FPU_EMU_INC_STATS(stores);
  1289. SIFROMREG(val, MIPSInst_FS(ir));
  1290. if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
  1291. MIPS_FPU_EMU_INC_STATS(errors);
  1292. *fault_addr = va;
  1293. return SIGBUS;
  1294. }
  1295. if (put_user(val, va)) {
  1296. MIPS_FPU_EMU_INC_STATS(errors);
  1297. *fault_addr = va;
  1298. return SIGSEGV;
  1299. }
  1300. break;
  1301. case madd_s_op:
  1302. handler = fpemu_sp_madd;
  1303. goto scoptop;
  1304. case msub_s_op:
  1305. handler = fpemu_sp_msub;
  1306. goto scoptop;
  1307. case nmadd_s_op:
  1308. handler = fpemu_sp_nmadd;
  1309. goto scoptop;
  1310. case nmsub_s_op:
  1311. handler = fpemu_sp_nmsub;
  1312. goto scoptop;
  1313. scoptop:
  1314. SPFROMREG(fr, MIPSInst_FR(ir));
  1315. SPFROMREG(fs, MIPSInst_FS(ir));
  1316. SPFROMREG(ft, MIPSInst_FT(ir));
  1317. fd = (*handler) (fr, fs, ft);
  1318. SPTOREG(fd, MIPSInst_FD(ir));
  1319. copcsr:
  1320. if (ieee754_cxtest(IEEE754_INEXACT))
  1321. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1322. if (ieee754_cxtest(IEEE754_UNDERFLOW))
  1323. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1324. if (ieee754_cxtest(IEEE754_OVERFLOW))
  1325. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1326. if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
  1327. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1328. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  1329. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1330. /*printk ("SIGFPE: fpu csr = %08x\n",
  1331. ctx->fcr31); */
  1332. return SIGFPE;
  1333. }
  1334. break;
  1335. default:
  1336. return SIGILL;
  1337. }
  1338. break;
  1339. }
  1340. case d_fmt:{ /* 1 */
  1341. ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
  1342. ieee754dp fd, fr, fs, ft;
  1343. u64 __user *va;
  1344. u64 val;
  1345. switch (MIPSInst_FUNC(ir)) {
  1346. case ldxc1_op:
  1347. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1348. xcp->regs[MIPSInst_FT(ir)]);
  1349. MIPS_FPU_EMU_INC_STATS(loads);
  1350. if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
  1351. MIPS_FPU_EMU_INC_STATS(errors);
  1352. *fault_addr = va;
  1353. return SIGBUS;
  1354. }
  1355. if (__get_user(val, va)) {
  1356. MIPS_FPU_EMU_INC_STATS(errors);
  1357. *fault_addr = va;
  1358. return SIGSEGV;
  1359. }
  1360. DITOREG(val, MIPSInst_FD(ir));
  1361. break;
  1362. case sdxc1_op:
  1363. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1364. xcp->regs[MIPSInst_FT(ir)]);
  1365. MIPS_FPU_EMU_INC_STATS(stores);
  1366. DIFROMREG(val, MIPSInst_FS(ir));
  1367. if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
  1368. MIPS_FPU_EMU_INC_STATS(errors);
  1369. *fault_addr = va;
  1370. return SIGBUS;
  1371. }
  1372. if (__put_user(val, va)) {
  1373. MIPS_FPU_EMU_INC_STATS(errors);
  1374. *fault_addr = va;
  1375. return SIGSEGV;
  1376. }
  1377. break;
  1378. case madd_d_op:
  1379. handler = fpemu_dp_madd;
  1380. goto dcoptop;
  1381. case msub_d_op:
  1382. handler = fpemu_dp_msub;
  1383. goto dcoptop;
  1384. case nmadd_d_op:
  1385. handler = fpemu_dp_nmadd;
  1386. goto dcoptop;
  1387. case nmsub_d_op:
  1388. handler = fpemu_dp_nmsub;
  1389. goto dcoptop;
  1390. dcoptop:
  1391. DPFROMREG(fr, MIPSInst_FR(ir));
  1392. DPFROMREG(fs, MIPSInst_FS(ir));
  1393. DPFROMREG(ft, MIPSInst_FT(ir));
  1394. fd = (*handler) (fr, fs, ft);
  1395. DPTOREG(fd, MIPSInst_FD(ir));
  1396. goto copcsr;
  1397. default:
  1398. return SIGILL;
  1399. }
  1400. break;
  1401. }
  1402. case 0x7: /* 7 */
  1403. if (MIPSInst_FUNC(ir) != pfetch_op) {
  1404. return SIGILL;
  1405. }
  1406. /* ignore prefx operation */
  1407. break;
  1408. default:
  1409. return SIGILL;
  1410. }
  1411. return 0;
  1412. }
  1413. #endif
  1414. /*
  1415. * Emulate a single COP1 arithmetic instruction.
  1416. */
  1417. static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1418. mips_instruction ir)
  1419. {
  1420. int rfmt; /* resulting format */
  1421. unsigned rcsr = 0; /* resulting csr */
  1422. unsigned cond;
  1423. union {
  1424. ieee754dp d;
  1425. ieee754sp s;
  1426. int w;
  1427. #ifdef __mips64
  1428. s64 l;
  1429. #endif
  1430. } rv; /* resulting value */
  1431. MIPS_FPU_EMU_INC_STATS(cp1ops);
  1432. switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
  1433. case s_fmt:{ /* 0 */
  1434. union {
  1435. ieee754sp(*b) (ieee754sp, ieee754sp);
  1436. ieee754sp(*u) (ieee754sp);
  1437. } handler;
  1438. switch (MIPSInst_FUNC(ir)) {
  1439. /* binary ops */
  1440. case fadd_op:
  1441. handler.b = ieee754sp_add;
  1442. goto scopbop;
  1443. case fsub_op:
  1444. handler.b = ieee754sp_sub;
  1445. goto scopbop;
  1446. case fmul_op:
  1447. handler.b = ieee754sp_mul;
  1448. goto scopbop;
  1449. case fdiv_op:
  1450. handler.b = ieee754sp_div;
  1451. goto scopbop;
  1452. /* unary ops */
  1453. #if __mips >= 2 || defined(__mips64)
  1454. case fsqrt_op:
  1455. handler.u = ieee754sp_sqrt;
  1456. goto scopuop;
  1457. #endif
  1458. #if __mips >= 4 && __mips != 32
  1459. case frsqrt_op:
  1460. handler.u = fpemu_sp_rsqrt;
  1461. goto scopuop;
  1462. case frecip_op:
  1463. handler.u = fpemu_sp_recip;
  1464. goto scopuop;
  1465. #endif
  1466. #if __mips >= 4
  1467. case fmovc_op:
  1468. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1469. if (((ctx->fcr31 & cond) != 0) !=
  1470. ((MIPSInst_FT(ir) & 1) != 0))
  1471. return 0;
  1472. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1473. break;
  1474. case fmovz_op:
  1475. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1476. return 0;
  1477. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1478. break;
  1479. case fmovn_op:
  1480. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1481. return 0;
  1482. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1483. break;
  1484. #endif
  1485. case fabs_op:
  1486. handler.u = ieee754sp_abs;
  1487. goto scopuop;
  1488. case fneg_op:
  1489. handler.u = ieee754sp_neg;
  1490. goto scopuop;
  1491. case fmov_op:
  1492. /* an easy one */
  1493. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1494. goto copcsr;
  1495. /* binary op on handler */
  1496. scopbop:
  1497. {
  1498. ieee754sp fs, ft;
  1499. SPFROMREG(fs, MIPSInst_FS(ir));
  1500. SPFROMREG(ft, MIPSInst_FT(ir));
  1501. rv.s = (*handler.b) (fs, ft);
  1502. goto copcsr;
  1503. }
  1504. scopuop:
  1505. {
  1506. ieee754sp fs;
  1507. SPFROMREG(fs, MIPSInst_FS(ir));
  1508. rv.s = (*handler.u) (fs);
  1509. goto copcsr;
  1510. }
  1511. copcsr:
  1512. if (ieee754_cxtest(IEEE754_INEXACT))
  1513. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1514. if (ieee754_cxtest(IEEE754_UNDERFLOW))
  1515. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1516. if (ieee754_cxtest(IEEE754_OVERFLOW))
  1517. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1518. if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
  1519. rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
  1520. if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
  1521. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1522. break;
  1523. /* unary conv ops */
  1524. case fcvts_op:
  1525. return SIGILL; /* not defined */
  1526. case fcvtd_op:{
  1527. ieee754sp fs;
  1528. SPFROMREG(fs, MIPSInst_FS(ir));
  1529. rv.d = ieee754dp_fsp(fs);
  1530. rfmt = d_fmt;
  1531. goto copcsr;
  1532. }
  1533. case fcvtw_op:{
  1534. ieee754sp fs;
  1535. SPFROMREG(fs, MIPSInst_FS(ir));
  1536. rv.w = ieee754sp_tint(fs);
  1537. rfmt = w_fmt;
  1538. goto copcsr;
  1539. }
  1540. #if __mips >= 2 || defined(__mips64)
  1541. case fround_op:
  1542. case ftrunc_op:
  1543. case fceil_op:
  1544. case ffloor_op:{
  1545. unsigned int oldrm = ieee754_csr.rm;
  1546. ieee754sp fs;
  1547. SPFROMREG(fs, MIPSInst_FS(ir));
  1548. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1549. rv.w = ieee754sp_tint(fs);
  1550. ieee754_csr.rm = oldrm;
  1551. rfmt = w_fmt;
  1552. goto copcsr;
  1553. }
  1554. #endif /* __mips >= 2 */
  1555. #if defined(__mips64)
  1556. case fcvtl_op:{
  1557. ieee754sp fs;
  1558. SPFROMREG(fs, MIPSInst_FS(ir));
  1559. rv.l = ieee754sp_tlong(fs);
  1560. rfmt = l_fmt;
  1561. goto copcsr;
  1562. }
  1563. case froundl_op:
  1564. case ftruncl_op:
  1565. case fceill_op:
  1566. case ffloorl_op:{
  1567. unsigned int oldrm = ieee754_csr.rm;
  1568. ieee754sp fs;
  1569. SPFROMREG(fs, MIPSInst_FS(ir));
  1570. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1571. rv.l = ieee754sp_tlong(fs);
  1572. ieee754_csr.rm = oldrm;
  1573. rfmt = l_fmt;
  1574. goto copcsr;
  1575. }
  1576. #endif /* defined(__mips64) */
  1577. default:
  1578. if (MIPSInst_FUNC(ir) >= fcmp_op) {
  1579. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  1580. ieee754sp fs, ft;
  1581. SPFROMREG(fs, MIPSInst_FS(ir));
  1582. SPFROMREG(ft, MIPSInst_FT(ir));
  1583. rv.w = ieee754sp_cmp(fs, ft,
  1584. cmptab[cmpop & 0x7], cmpop & 0x8);
  1585. rfmt = -1;
  1586. if ((cmpop & 0x8) && ieee754_cxtest
  1587. (IEEE754_INVALID_OPERATION))
  1588. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  1589. else
  1590. goto copcsr;
  1591. }
  1592. else {
  1593. return SIGILL;
  1594. }
  1595. break;
  1596. }
  1597. break;
  1598. }
  1599. case d_fmt:{
  1600. union {
  1601. ieee754dp(*b) (ieee754dp, ieee754dp);
  1602. ieee754dp(*u) (ieee754dp);
  1603. } handler;
  1604. switch (MIPSInst_FUNC(ir)) {
  1605. /* binary ops */
  1606. case fadd_op:
  1607. handler.b = ieee754dp_add;
  1608. goto dcopbop;
  1609. case fsub_op:
  1610. handler.b = ieee754dp_sub;
  1611. goto dcopbop;
  1612. case fmul_op:
  1613. handler.b = ieee754dp_mul;
  1614. goto dcopbop;
  1615. case fdiv_op:
  1616. handler.b = ieee754dp_div;
  1617. goto dcopbop;
  1618. /* unary ops */
  1619. #if __mips >= 2 || defined(__mips64)
  1620. case fsqrt_op:
  1621. handler.u = ieee754dp_sqrt;
  1622. goto dcopuop;
  1623. #endif
  1624. #if __mips >= 4 && __mips != 32
  1625. case frsqrt_op:
  1626. handler.u = fpemu_dp_rsqrt;
  1627. goto dcopuop;
  1628. case frecip_op:
  1629. handler.u = fpemu_dp_recip;
  1630. goto dcopuop;
  1631. #endif
  1632. #if __mips >= 4
  1633. case fmovc_op:
  1634. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1635. if (((ctx->fcr31 & cond) != 0) !=
  1636. ((MIPSInst_FT(ir) & 1) != 0))
  1637. return 0;
  1638. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1639. break;
  1640. case fmovz_op:
  1641. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1642. return 0;
  1643. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1644. break;
  1645. case fmovn_op:
  1646. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1647. return 0;
  1648. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1649. break;
  1650. #endif
  1651. case fabs_op:
  1652. handler.u = ieee754dp_abs;
  1653. goto dcopuop;
  1654. case fneg_op:
  1655. handler.u = ieee754dp_neg;
  1656. goto dcopuop;
  1657. case fmov_op:
  1658. /* an easy one */
  1659. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1660. goto copcsr;
  1661. /* binary op on handler */
  1662. dcopbop:{
  1663. ieee754dp fs, ft;
  1664. DPFROMREG(fs, MIPSInst_FS(ir));
  1665. DPFROMREG(ft, MIPSInst_FT(ir));
  1666. rv.d = (*handler.b) (fs, ft);
  1667. goto copcsr;
  1668. }
  1669. dcopuop:{
  1670. ieee754dp fs;
  1671. DPFROMREG(fs, MIPSInst_FS(ir));
  1672. rv.d = (*handler.u) (fs);
  1673. goto copcsr;
  1674. }
  1675. /* unary conv ops */
  1676. case fcvts_op:{
  1677. ieee754dp fs;
  1678. DPFROMREG(fs, MIPSInst_FS(ir));
  1679. rv.s = ieee754sp_fdp(fs);
  1680. rfmt = s_fmt;
  1681. goto copcsr;
  1682. }
  1683. case fcvtd_op:
  1684. return SIGILL; /* not defined */
  1685. case fcvtw_op:{
  1686. ieee754dp fs;
  1687. DPFROMREG(fs, MIPSInst_FS(ir));
  1688. rv.w = ieee754dp_tint(fs); /* wrong */
  1689. rfmt = w_fmt;
  1690. goto copcsr;
  1691. }
  1692. #if __mips >= 2 || defined(__mips64)
  1693. case fround_op:
  1694. case ftrunc_op:
  1695. case fceil_op:
  1696. case ffloor_op:{
  1697. unsigned int oldrm = ieee754_csr.rm;
  1698. ieee754dp fs;
  1699. DPFROMREG(fs, MIPSInst_FS(ir));
  1700. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1701. rv.w = ieee754dp_tint(fs);
  1702. ieee754_csr.rm = oldrm;
  1703. rfmt = w_fmt;
  1704. goto copcsr;
  1705. }
  1706. #endif
  1707. #if defined(__mips64)
  1708. case fcvtl_op:{
  1709. ieee754dp fs;
  1710. DPFROMREG(fs, MIPSInst_FS(ir));
  1711. rv.l = ieee754dp_tlong(fs);
  1712. rfmt = l_fmt;
  1713. goto copcsr;
  1714. }
  1715. case froundl_op:
  1716. case ftruncl_op:
  1717. case fceill_op:
  1718. case ffloorl_op:{
  1719. unsigned int oldrm = ieee754_csr.rm;
  1720. ieee754dp fs;
  1721. DPFROMREG(fs, MIPSInst_FS(ir));
  1722. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1723. rv.l = ieee754dp_tlong(fs);
  1724. ieee754_csr.rm = oldrm;
  1725. rfmt = l_fmt;
  1726. goto copcsr;
  1727. }
  1728. #endif /* __mips >= 3 */
  1729. default:
  1730. if (MIPSInst_FUNC(ir) >= fcmp_op) {
  1731. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  1732. ieee754dp fs, ft;
  1733. DPFROMREG(fs, MIPSInst_FS(ir));
  1734. DPFROMREG(ft, MIPSInst_FT(ir));
  1735. rv.w = ieee754dp_cmp(fs, ft,
  1736. cmptab[cmpop & 0x7], cmpop & 0x8);
  1737. rfmt = -1;
  1738. if ((cmpop & 0x8)
  1739. &&
  1740. ieee754_cxtest
  1741. (IEEE754_INVALID_OPERATION))
  1742. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  1743. else
  1744. goto copcsr;
  1745. }
  1746. else {
  1747. return SIGILL;
  1748. }
  1749. break;
  1750. }
  1751. break;
  1752. }
  1753. case w_fmt:{
  1754. ieee754sp fs;
  1755. switch (MIPSInst_FUNC(ir)) {
  1756. case fcvts_op:
  1757. /* convert word to single precision real */
  1758. SPFROMREG(fs, MIPSInst_FS(ir));
  1759. rv.s = ieee754sp_fint(fs.bits);
  1760. rfmt = s_fmt;
  1761. goto copcsr;
  1762. case fcvtd_op:
  1763. /* convert word to double precision real */
  1764. SPFROMREG(fs, MIPSInst_FS(ir));
  1765. rv.d = ieee754dp_fint(fs.bits);
  1766. rfmt = d_fmt;
  1767. goto copcsr;
  1768. default:
  1769. return SIGILL;
  1770. }
  1771. break;
  1772. }
  1773. #if defined(__mips64)
  1774. case l_fmt:{
  1775. switch (MIPSInst_FUNC(ir)) {
  1776. case fcvts_op:
  1777. /* convert long to single precision real */
  1778. rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
  1779. rfmt = s_fmt;
  1780. goto copcsr;
  1781. case fcvtd_op:
  1782. /* convert long to double precision real */
  1783. rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
  1784. rfmt = d_fmt;
  1785. goto copcsr;
  1786. default:
  1787. return SIGILL;
  1788. }
  1789. break;
  1790. }
  1791. #endif
  1792. default:
  1793. return SIGILL;
  1794. }
  1795. /*
  1796. * Update the fpu CSR register for this operation.
  1797. * If an exception is required, generate a tidy SIGFPE exception,
  1798. * without updating the result register.
  1799. * Note: cause exception bits do not accumulate, they are rewritten
  1800. * for each op; only the flag/sticky bits accumulate.
  1801. */
  1802. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  1803. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1804. /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
  1805. return SIGFPE;
  1806. }
  1807. /*
  1808. * Now we can safely write the result back to the register file.
  1809. */
  1810. switch (rfmt) {
  1811. case -1:{
  1812. #if __mips >= 4
  1813. cond = fpucondbit[MIPSInst_FD(ir) >> 2];
  1814. #else
  1815. cond = FPU_CSR_COND;
  1816. #endif
  1817. if (rv.w)
  1818. ctx->fcr31 |= cond;
  1819. else
  1820. ctx->fcr31 &= ~cond;
  1821. break;
  1822. }
  1823. case d_fmt:
  1824. DPTOREG(rv.d, MIPSInst_FD(ir));
  1825. break;
  1826. case s_fmt:
  1827. SPTOREG(rv.s, MIPSInst_FD(ir));
  1828. break;
  1829. case w_fmt:
  1830. SITOREG(rv.w, MIPSInst_FD(ir));
  1831. break;
  1832. #if defined(__mips64)
  1833. case l_fmt:
  1834. DITOREG(rv.l, MIPSInst_FD(ir));
  1835. break;
  1836. #endif
  1837. default:
  1838. return SIGILL;
  1839. }
  1840. return 0;
  1841. }
  1842. int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1843. int has_fpu, void *__user *fault_addr)
  1844. {
  1845. unsigned long oldepc, prevepc;
  1846. struct mm_decoded_insn dec_insn;
  1847. u16 instr[4];
  1848. u16 *instr_ptr;
  1849. int sig = 0;
  1850. oldepc = xcp->cp0_epc;
  1851. do {
  1852. prevepc = xcp->cp0_epc;
  1853. if (get_isa16_mode(prevepc) && cpu_has_mmips) {
  1854. /*
  1855. * Get next 2 microMIPS instructions and convert them
  1856. * into 32-bit instructions.
  1857. */
  1858. if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
  1859. (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
  1860. (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
  1861. (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
  1862. MIPS_FPU_EMU_INC_STATS(errors);
  1863. return SIGBUS;
  1864. }
  1865. instr_ptr = instr;
  1866. /* Get first instruction. */
  1867. if (mm_insn_16bit(*instr_ptr)) {
  1868. /* Duplicate the half-word. */
  1869. dec_insn.insn = (*instr_ptr << 16) |
  1870. (*instr_ptr);
  1871. /* 16-bit instruction. */
  1872. dec_insn.pc_inc = 2;
  1873. instr_ptr += 1;
  1874. } else {
  1875. dec_insn.insn = (*instr_ptr << 16) |
  1876. *(instr_ptr+1);
  1877. /* 32-bit instruction. */
  1878. dec_insn.pc_inc = 4;
  1879. instr_ptr += 2;
  1880. }
  1881. /* Get second instruction. */
  1882. if (mm_insn_16bit(*instr_ptr)) {
  1883. /* Duplicate the half-word. */
  1884. dec_insn.next_insn = (*instr_ptr << 16) |
  1885. (*instr_ptr);
  1886. /* 16-bit instruction. */
  1887. dec_insn.next_pc_inc = 2;
  1888. } else {
  1889. dec_insn.next_insn = (*instr_ptr << 16) |
  1890. *(instr_ptr+1);
  1891. /* 32-bit instruction. */
  1892. dec_insn.next_pc_inc = 4;
  1893. }
  1894. dec_insn.micro_mips_mode = 1;
  1895. } else {
  1896. if ((get_user(dec_insn.insn,
  1897. (mips_instruction __user *) xcp->cp0_epc)) ||
  1898. (get_user(dec_insn.next_insn,
  1899. (mips_instruction __user *)(xcp->cp0_epc+4)))) {
  1900. MIPS_FPU_EMU_INC_STATS(errors);
  1901. return SIGBUS;
  1902. }
  1903. dec_insn.pc_inc = 4;
  1904. dec_insn.next_pc_inc = 4;
  1905. dec_insn.micro_mips_mode = 0;
  1906. }
  1907. if ((dec_insn.insn == 0) ||
  1908. ((dec_insn.pc_inc == 2) &&
  1909. ((dec_insn.insn & 0xffff) == MM_NOP16)))
  1910. xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
  1911. else {
  1912. /*
  1913. * The 'ieee754_csr' is an alias of
  1914. * ctx->fcr31. No need to copy ctx->fcr31 to
  1915. * ieee754_csr. But ieee754_csr.rm is ieee
  1916. * library modes. (not mips rounding mode)
  1917. */
  1918. /* convert to ieee library modes */
  1919. ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
  1920. sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
  1921. /* revert to mips rounding mode */
  1922. ieee754_csr.rm = mips_rm[ieee754_csr.rm];
  1923. }
  1924. if (has_fpu)
  1925. break;
  1926. if (sig)
  1927. break;
  1928. cond_resched();
  1929. } while (xcp->cp0_epc > prevepc);
  1930. /* SIGILL indicates a non-fpu instruction */
  1931. if (sig == SIGILL && xcp->cp0_epc != oldepc)
  1932. /* but if epc has advanced, then ignore it */
  1933. sig = 0;
  1934. return sig;
  1935. }
  1936. #ifdef CONFIG_DEBUG_FS
  1937. static int fpuemu_stat_get(void *data, u64 *val)
  1938. {
  1939. int cpu;
  1940. unsigned long sum = 0;
  1941. for_each_online_cpu(cpu) {
  1942. struct mips_fpu_emulator_stats *ps;
  1943. local_t *pv;
  1944. ps = &per_cpu(fpuemustats, cpu);
  1945. pv = (void *)ps + (unsigned long)data;
  1946. sum += local_read(pv);
  1947. }
  1948. *val = sum;
  1949. return 0;
  1950. }
  1951. DEFINE_SIMPLE_ATTRIBUTE(fops_fpuemu_stat, fpuemu_stat_get, NULL, "%llu\n");
  1952. extern struct dentry *mips_debugfs_dir;
  1953. static int __init debugfs_fpuemu(void)
  1954. {
  1955. struct dentry *d, *dir;
  1956. if (!mips_debugfs_dir)
  1957. return -ENODEV;
  1958. dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir);
  1959. if (!dir)
  1960. return -ENOMEM;
  1961. #define FPU_STAT_CREATE(M) \
  1962. do { \
  1963. d = debugfs_create_file(#M , S_IRUGO, dir, \
  1964. (void *)offsetof(struct mips_fpu_emulator_stats, M), \
  1965. &fops_fpuemu_stat); \
  1966. if (!d) \
  1967. return -ENOMEM; \
  1968. } while (0)
  1969. FPU_STAT_CREATE(emulated);
  1970. FPU_STAT_CREATE(loads);
  1971. FPU_STAT_CREATE(stores);
  1972. FPU_STAT_CREATE(cp1ops);
  1973. FPU_STAT_CREATE(cp1xops);
  1974. FPU_STAT_CREATE(errors);
  1975. return 0;
  1976. }
  1977. __initcall(debugfs_fpuemu);
  1978. #endif