cp1emu.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  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. switch (insn.mm_i_format.opcode) {
  452. case mm_pool32a_op:
  453. if ((insn.mm_i_format.simmediate & MM_POOL32A_MINOR_MASK) ==
  454. mm_pool32axf_op) {
  455. switch (insn.mm_i_format.simmediate >>
  456. MM_POOL32A_MINOR_SHIFT) {
  457. case mm_jalr_op:
  458. case mm_jalrhb_op:
  459. case mm_jalrs_op:
  460. case mm_jalrshb_op:
  461. if (insn.mm_i_format.rt != 0) /* Not mm_jr */
  462. regs->regs[insn.mm_i_format.rt] =
  463. regs->cp0_epc +
  464. dec_insn.pc_inc +
  465. dec_insn.next_pc_inc;
  466. *contpc = regs->regs[insn.mm_i_format.rs];
  467. return 1;
  468. break;
  469. }
  470. }
  471. break;
  472. case mm_pool32i_op:
  473. switch (insn.mm_i_format.rt) {
  474. case mm_bltzals_op:
  475. case mm_bltzal_op:
  476. regs->regs[31] = regs->cp0_epc +
  477. dec_insn.pc_inc +
  478. dec_insn.next_pc_inc;
  479. /* Fall through */
  480. case mm_bltz_op:
  481. if ((long)regs->regs[insn.mm_i_format.rs] < 0)
  482. *contpc = regs->cp0_epc +
  483. dec_insn.pc_inc +
  484. (insn.mm_i_format.simmediate << 1);
  485. else
  486. *contpc = regs->cp0_epc +
  487. dec_insn.pc_inc +
  488. dec_insn.next_pc_inc;
  489. return 1;
  490. break;
  491. case mm_bgezals_op:
  492. case mm_bgezal_op:
  493. regs->regs[31] = regs->cp0_epc +
  494. dec_insn.pc_inc +
  495. dec_insn.next_pc_inc;
  496. /* Fall through */
  497. case mm_bgez_op:
  498. if ((long)regs->regs[insn.mm_i_format.rs] >= 0)
  499. *contpc = regs->cp0_epc +
  500. dec_insn.pc_inc +
  501. (insn.mm_i_format.simmediate << 1);
  502. else
  503. *contpc = regs->cp0_epc +
  504. dec_insn.pc_inc +
  505. dec_insn.next_pc_inc;
  506. return 1;
  507. break;
  508. case mm_blez_op:
  509. if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
  510. *contpc = regs->cp0_epc +
  511. dec_insn.pc_inc +
  512. (insn.mm_i_format.simmediate << 1);
  513. else
  514. *contpc = regs->cp0_epc +
  515. dec_insn.pc_inc +
  516. dec_insn.next_pc_inc;
  517. return 1;
  518. break;
  519. case mm_bgtz_op:
  520. if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
  521. *contpc = regs->cp0_epc +
  522. dec_insn.pc_inc +
  523. (insn.mm_i_format.simmediate << 1);
  524. else
  525. *contpc = regs->cp0_epc +
  526. dec_insn.pc_inc +
  527. dec_insn.next_pc_inc;
  528. return 1;
  529. break;
  530. case mm_bc2f_op:
  531. case mm_bc1f_op:
  532. bc_false = 1;
  533. /* Fall through */
  534. case mm_bc2t_op:
  535. case mm_bc1t_op:
  536. preempt_disable();
  537. if (is_fpu_owner())
  538. asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
  539. else
  540. fcr31 = current->thread.fpu.fcr31;
  541. preempt_enable();
  542. if (bc_false)
  543. fcr31 = ~fcr31;
  544. bit = (insn.mm_i_format.rs >> 2);
  545. bit += (bit != 0);
  546. bit += 23;
  547. if (fcr31 & (1 << bit))
  548. *contpc = regs->cp0_epc +
  549. dec_insn.pc_inc +
  550. (insn.mm_i_format.simmediate << 1);
  551. else
  552. *contpc = regs->cp0_epc +
  553. dec_insn.pc_inc + dec_insn.next_pc_inc;
  554. return 1;
  555. break;
  556. }
  557. break;
  558. case mm_pool16c_op:
  559. switch (insn.mm_i_format.rt) {
  560. case mm_jalr16_op:
  561. case mm_jalrs16_op:
  562. regs->regs[31] = regs->cp0_epc +
  563. dec_insn.pc_inc + dec_insn.next_pc_inc;
  564. /* Fall through */
  565. case mm_jr16_op:
  566. *contpc = regs->regs[insn.mm_i_format.rs];
  567. return 1;
  568. break;
  569. }
  570. break;
  571. case mm_beqz16_op:
  572. if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] == 0)
  573. *contpc = regs->cp0_epc +
  574. dec_insn.pc_inc +
  575. (insn.mm_b1_format.simmediate << 1);
  576. else
  577. *contpc = regs->cp0_epc +
  578. dec_insn.pc_inc + dec_insn.next_pc_inc;
  579. return 1;
  580. break;
  581. case mm_bnez16_op:
  582. if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] != 0)
  583. *contpc = regs->cp0_epc +
  584. dec_insn.pc_inc +
  585. (insn.mm_b1_format.simmediate << 1);
  586. else
  587. *contpc = regs->cp0_epc +
  588. dec_insn.pc_inc + dec_insn.next_pc_inc;
  589. return 1;
  590. break;
  591. case mm_b16_op:
  592. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  593. (insn.mm_b0_format.simmediate << 1);
  594. return 1;
  595. break;
  596. case mm_beq32_op:
  597. if (regs->regs[insn.mm_i_format.rs] ==
  598. regs->regs[insn.mm_i_format.rt])
  599. *contpc = regs->cp0_epc +
  600. dec_insn.pc_inc +
  601. (insn.mm_i_format.simmediate << 1);
  602. else
  603. *contpc = regs->cp0_epc +
  604. dec_insn.pc_inc +
  605. dec_insn.next_pc_inc;
  606. return 1;
  607. break;
  608. case mm_bne32_op:
  609. if (regs->regs[insn.mm_i_format.rs] !=
  610. regs->regs[insn.mm_i_format.rt])
  611. *contpc = regs->cp0_epc +
  612. dec_insn.pc_inc +
  613. (insn.mm_i_format.simmediate << 1);
  614. else
  615. *contpc = regs->cp0_epc +
  616. dec_insn.pc_inc + dec_insn.next_pc_inc;
  617. return 1;
  618. break;
  619. case mm_jalx32_op:
  620. regs->regs[31] = regs->cp0_epc +
  621. dec_insn.pc_inc + dec_insn.next_pc_inc;
  622. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  623. *contpc >>= 28;
  624. *contpc <<= 28;
  625. *contpc |= (insn.j_format.target << 2);
  626. return 1;
  627. break;
  628. case mm_jals32_op:
  629. case mm_jal32_op:
  630. regs->regs[31] = regs->cp0_epc +
  631. dec_insn.pc_inc + dec_insn.next_pc_inc;
  632. /* Fall through */
  633. case mm_j32_op:
  634. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  635. *contpc >>= 27;
  636. *contpc <<= 27;
  637. *contpc |= (insn.j_format.target << 1);
  638. set_isa16_mode(*contpc);
  639. return 1;
  640. break;
  641. }
  642. return 0;
  643. }
  644. /*
  645. * Redundant with logic already in kernel/branch.c,
  646. * embedded in compute_return_epc. At some point,
  647. * a single subroutine should be used across both
  648. * modules.
  649. */
  650. static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
  651. unsigned long *contpc)
  652. {
  653. union mips_instruction insn = (union mips_instruction)dec_insn.insn;
  654. unsigned int fcr31;
  655. unsigned int bit = 0;
  656. switch (insn.i_format.opcode) {
  657. case spec_op:
  658. switch (insn.r_format.func) {
  659. case jalr_op:
  660. regs->regs[insn.r_format.rd] =
  661. regs->cp0_epc + dec_insn.pc_inc +
  662. dec_insn.next_pc_inc;
  663. /* Fall through */
  664. case jr_op:
  665. *contpc = regs->regs[insn.r_format.rs];
  666. return 1;
  667. break;
  668. }
  669. break;
  670. case bcond_op:
  671. switch (insn.i_format.rt) {
  672. case bltzal_op:
  673. case bltzall_op:
  674. regs->regs[31] = regs->cp0_epc +
  675. dec_insn.pc_inc +
  676. dec_insn.next_pc_inc;
  677. /* Fall through */
  678. case bltz_op:
  679. case bltzl_op:
  680. if ((long)regs->regs[insn.i_format.rs] < 0)
  681. *contpc = regs->cp0_epc +
  682. dec_insn.pc_inc +
  683. (insn.i_format.simmediate << 2);
  684. else
  685. *contpc = regs->cp0_epc +
  686. dec_insn.pc_inc +
  687. dec_insn.next_pc_inc;
  688. return 1;
  689. break;
  690. case bgezal_op:
  691. case bgezall_op:
  692. regs->regs[31] = regs->cp0_epc +
  693. dec_insn.pc_inc +
  694. dec_insn.next_pc_inc;
  695. /* Fall through */
  696. case bgez_op:
  697. case bgezl_op:
  698. if ((long)regs->regs[insn.i_format.rs] >= 0)
  699. *contpc = regs->cp0_epc +
  700. dec_insn.pc_inc +
  701. (insn.i_format.simmediate << 2);
  702. else
  703. *contpc = regs->cp0_epc +
  704. dec_insn.pc_inc +
  705. dec_insn.next_pc_inc;
  706. return 1;
  707. break;
  708. }
  709. break;
  710. case jalx_op:
  711. set_isa16_mode(bit);
  712. case jal_op:
  713. regs->regs[31] = regs->cp0_epc +
  714. dec_insn.pc_inc +
  715. dec_insn.next_pc_inc;
  716. /* Fall through */
  717. case j_op:
  718. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  719. *contpc >>= 28;
  720. *contpc <<= 28;
  721. *contpc |= (insn.j_format.target << 2);
  722. /* Set microMIPS mode bit: XOR for jalx. */
  723. *contpc ^= bit;
  724. return 1;
  725. break;
  726. case beq_op:
  727. case beql_op:
  728. if (regs->regs[insn.i_format.rs] ==
  729. regs->regs[insn.i_format.rt])
  730. *contpc = regs->cp0_epc +
  731. dec_insn.pc_inc +
  732. (insn.i_format.simmediate << 2);
  733. else
  734. *contpc = regs->cp0_epc +
  735. dec_insn.pc_inc +
  736. dec_insn.next_pc_inc;
  737. return 1;
  738. break;
  739. case bne_op:
  740. case bnel_op:
  741. if (regs->regs[insn.i_format.rs] !=
  742. regs->regs[insn.i_format.rt])
  743. *contpc = regs->cp0_epc +
  744. dec_insn.pc_inc +
  745. (insn.i_format.simmediate << 2);
  746. else
  747. *contpc = regs->cp0_epc +
  748. dec_insn.pc_inc +
  749. dec_insn.next_pc_inc;
  750. return 1;
  751. break;
  752. case blez_op:
  753. case blezl_op:
  754. if ((long)regs->regs[insn.i_format.rs] <= 0)
  755. *contpc = regs->cp0_epc +
  756. dec_insn.pc_inc +
  757. (insn.i_format.simmediate << 2);
  758. else
  759. *contpc = regs->cp0_epc +
  760. dec_insn.pc_inc +
  761. dec_insn.next_pc_inc;
  762. return 1;
  763. break;
  764. case bgtz_op:
  765. case bgtzl_op:
  766. if ((long)regs->regs[insn.i_format.rs] > 0)
  767. *contpc = regs->cp0_epc +
  768. dec_insn.pc_inc +
  769. (insn.i_format.simmediate << 2);
  770. else
  771. *contpc = regs->cp0_epc +
  772. dec_insn.pc_inc +
  773. dec_insn.next_pc_inc;
  774. return 1;
  775. break;
  776. case cop0_op:
  777. case cop1_op:
  778. case cop2_op:
  779. case cop1x_op:
  780. if (insn.i_format.rs == bc_op) {
  781. preempt_disable();
  782. if (is_fpu_owner())
  783. asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
  784. else
  785. fcr31 = current->thread.fpu.fcr31;
  786. preempt_enable();
  787. bit = (insn.i_format.rt >> 2);
  788. bit += (bit != 0);
  789. bit += 23;
  790. switch (insn.i_format.rt & 3) {
  791. case 0: /* bc1f */
  792. case 2: /* bc1fl */
  793. if (~fcr31 & (1 << bit))
  794. *contpc = regs->cp0_epc +
  795. dec_insn.pc_inc +
  796. (insn.i_format.simmediate << 2);
  797. else
  798. *contpc = regs->cp0_epc +
  799. dec_insn.pc_inc +
  800. dec_insn.next_pc_inc;
  801. return 1;
  802. break;
  803. case 1: /* bc1t */
  804. case 3: /* bc1tl */
  805. if (fcr31 & (1 << bit))
  806. *contpc = regs->cp0_epc +
  807. dec_insn.pc_inc +
  808. (insn.i_format.simmediate << 2);
  809. else
  810. *contpc = regs->cp0_epc +
  811. dec_insn.pc_inc +
  812. dec_insn.next_pc_inc;
  813. return 1;
  814. break;
  815. }
  816. }
  817. break;
  818. }
  819. return 0;
  820. }
  821. /*
  822. * In the Linux kernel, we support selection of FPR format on the
  823. * basis of the Status.FR bit. If an FPU is not present, the FR bit
  824. * is hardwired to zero, which would imply a 32-bit FPU even for
  825. * 64-bit CPUs so we rather look at TIF_32BIT_REGS.
  826. * FPU emu is slow and bulky and optimizing this function offers fairly
  827. * sizeable benefits so we try to be clever and make this function return
  828. * a constant whenever possible, that is on 64-bit kernels without O32
  829. * compatibility enabled and on 32-bit kernels.
  830. */
  831. static inline int cop1_64bit(struct pt_regs *xcp)
  832. {
  833. #if defined(CONFIG_64BIT) && !defined(CONFIG_MIPS32_O32)
  834. return 1;
  835. #elif defined(CONFIG_64BIT) && defined(CONFIG_MIPS32_O32)
  836. return !test_thread_flag(TIF_32BIT_REGS);
  837. #else
  838. return 0;
  839. #endif
  840. }
  841. #define SIFROMREG(si, x) ((si) = cop1_64bit(xcp) || !(x & 1) ? \
  842. (int)ctx->fpr[x] : (int)(ctx->fpr[x & ~1] >> 32))
  843. #define SITOREG(si, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = \
  844. cop1_64bit(xcp) || !(x & 1) ? \
  845. ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
  846. ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
  847. #define DIFROMREG(di, x) ((di) = ctx->fpr[x & ~(cop1_64bit(xcp) == 0)])
  848. #define DITOREG(di, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = (di))
  849. #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
  850. #define SPTOREG(sp, x) SITOREG((sp).bits, x)
  851. #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
  852. #define DPTOREG(dp, x) DITOREG((dp).bits, x)
  853. /*
  854. * Emulate the single floating point instruction pointed at by EPC.
  855. * Two instructions if the instruction is in a branch delay slot.
  856. */
  857. static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  858. struct mm_decoded_insn dec_insn, void *__user *fault_addr)
  859. {
  860. mips_instruction ir;
  861. unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
  862. unsigned int cond;
  863. int pc_inc;
  864. /* XXX NEC Vr54xx bug workaround */
  865. if (xcp->cp0_cause & CAUSEF_BD) {
  866. if (dec_insn.micro_mips_mode) {
  867. if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
  868. xcp->cp0_cause &= ~CAUSEF_BD;
  869. } else {
  870. if (!isBranchInstr(xcp, dec_insn, &contpc))
  871. xcp->cp0_cause &= ~CAUSEF_BD;
  872. }
  873. }
  874. if (xcp->cp0_cause & CAUSEF_BD) {
  875. /*
  876. * The instruction to be emulated is in a branch delay slot
  877. * which means that we have to emulate the branch instruction
  878. * BEFORE we do the cop1 instruction.
  879. *
  880. * This branch could be a COP1 branch, but in that case we
  881. * would have had a trap for that instruction, and would not
  882. * come through this route.
  883. *
  884. * Linux MIPS branch emulator operates on context, updating the
  885. * cp0_epc.
  886. */
  887. ir = dec_insn.next_insn; /* process delay slot instr */
  888. pc_inc = dec_insn.next_pc_inc;
  889. } else {
  890. ir = dec_insn.insn; /* process current instr */
  891. pc_inc = dec_insn.pc_inc;
  892. }
  893. /*
  894. * Since microMIPS FPU instructios are a subset of MIPS32 FPU
  895. * instructions, we want to convert microMIPS FPU instructions
  896. * into MIPS32 instructions so that we could reuse all of the
  897. * FPU emulation code.
  898. *
  899. * NOTE: We cannot do this for branch instructions since they
  900. * are not a subset. Example: Cannot emulate a 16-bit
  901. * aligned target address with a MIPS32 instruction.
  902. */
  903. if (dec_insn.micro_mips_mode) {
  904. /*
  905. * If next instruction is a 16-bit instruction, then it
  906. * it cannot be a FPU instruction. This could happen
  907. * since we can be called for non-FPU instructions.
  908. */
  909. if ((pc_inc == 2) ||
  910. (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
  911. == SIGILL))
  912. return SIGILL;
  913. }
  914. emul:
  915. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
  916. MIPS_FPU_EMU_INC_STATS(emulated);
  917. switch (MIPSInst_OPCODE(ir)) {
  918. case ldc1_op:{
  919. u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  920. MIPSInst_SIMM(ir));
  921. u64 val;
  922. MIPS_FPU_EMU_INC_STATS(loads);
  923. if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
  924. MIPS_FPU_EMU_INC_STATS(errors);
  925. *fault_addr = va;
  926. return SIGBUS;
  927. }
  928. if (__get_user(val, va)) {
  929. MIPS_FPU_EMU_INC_STATS(errors);
  930. *fault_addr = va;
  931. return SIGSEGV;
  932. }
  933. DITOREG(val, MIPSInst_RT(ir));
  934. break;
  935. }
  936. case sdc1_op:{
  937. u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  938. MIPSInst_SIMM(ir));
  939. u64 val;
  940. MIPS_FPU_EMU_INC_STATS(stores);
  941. DIFROMREG(val, MIPSInst_RT(ir));
  942. if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
  943. MIPS_FPU_EMU_INC_STATS(errors);
  944. *fault_addr = va;
  945. return SIGBUS;
  946. }
  947. if (__put_user(val, va)) {
  948. MIPS_FPU_EMU_INC_STATS(errors);
  949. *fault_addr = va;
  950. return SIGSEGV;
  951. }
  952. break;
  953. }
  954. case lwc1_op:{
  955. u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  956. MIPSInst_SIMM(ir));
  957. u32 val;
  958. MIPS_FPU_EMU_INC_STATS(loads);
  959. if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
  960. MIPS_FPU_EMU_INC_STATS(errors);
  961. *fault_addr = va;
  962. return SIGBUS;
  963. }
  964. if (__get_user(val, va)) {
  965. MIPS_FPU_EMU_INC_STATS(errors);
  966. *fault_addr = va;
  967. return SIGSEGV;
  968. }
  969. SITOREG(val, MIPSInst_RT(ir));
  970. break;
  971. }
  972. case swc1_op:{
  973. u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  974. MIPSInst_SIMM(ir));
  975. u32 val;
  976. MIPS_FPU_EMU_INC_STATS(stores);
  977. SIFROMREG(val, MIPSInst_RT(ir));
  978. if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
  979. MIPS_FPU_EMU_INC_STATS(errors);
  980. *fault_addr = va;
  981. return SIGBUS;
  982. }
  983. if (__put_user(val, va)) {
  984. MIPS_FPU_EMU_INC_STATS(errors);
  985. *fault_addr = va;
  986. return SIGSEGV;
  987. }
  988. break;
  989. }
  990. case cop1_op:
  991. switch (MIPSInst_RS(ir)) {
  992. #if defined(__mips64)
  993. case dmfc_op:
  994. /* copregister fs -> gpr[rt] */
  995. if (MIPSInst_RT(ir) != 0) {
  996. DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  997. MIPSInst_RD(ir));
  998. }
  999. break;
  1000. case dmtc_op:
  1001. /* copregister fs <- rt */
  1002. DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1003. break;
  1004. #endif
  1005. case mfc_op:
  1006. /* copregister rd -> gpr[rt] */
  1007. if (MIPSInst_RT(ir) != 0) {
  1008. SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  1009. MIPSInst_RD(ir));
  1010. }
  1011. break;
  1012. case mtc_op:
  1013. /* copregister rd <- rt */
  1014. SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1015. break;
  1016. case cfc_op:{
  1017. /* cop control register rd -> gpr[rt] */
  1018. u32 value;
  1019. if (MIPSInst_RD(ir) == FPCREG_CSR) {
  1020. value = ctx->fcr31;
  1021. value = (value & ~FPU_CSR_RM) |
  1022. mips_rm[modeindex(value)];
  1023. #ifdef CSRTRACE
  1024. printk("%p gpr[%d]<-csr=%08x\n",
  1025. (void *) (xcp->cp0_epc),
  1026. MIPSInst_RT(ir), value);
  1027. #endif
  1028. }
  1029. else if (MIPSInst_RD(ir) == FPCREG_RID)
  1030. value = 0;
  1031. else
  1032. value = 0;
  1033. if (MIPSInst_RT(ir))
  1034. xcp->regs[MIPSInst_RT(ir)] = value;
  1035. break;
  1036. }
  1037. case ctc_op:{
  1038. /* copregister rd <- rt */
  1039. u32 value;
  1040. if (MIPSInst_RT(ir) == 0)
  1041. value = 0;
  1042. else
  1043. value = xcp->regs[MIPSInst_RT(ir)];
  1044. /* we only have one writable control reg
  1045. */
  1046. if (MIPSInst_RD(ir) == FPCREG_CSR) {
  1047. #ifdef CSRTRACE
  1048. printk("%p gpr[%d]->csr=%08x\n",
  1049. (void *) (xcp->cp0_epc),
  1050. MIPSInst_RT(ir), value);
  1051. #endif
  1052. /*
  1053. * Don't write reserved bits,
  1054. * and convert to ieee library modes
  1055. */
  1056. ctx->fcr31 = (value &
  1057. ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
  1058. ieee_rm[modeindex(value)];
  1059. }
  1060. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1061. return SIGFPE;
  1062. }
  1063. break;
  1064. }
  1065. case bc_op:{
  1066. int likely = 0;
  1067. if (xcp->cp0_cause & CAUSEF_BD)
  1068. return SIGILL;
  1069. #if __mips >= 4
  1070. cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
  1071. #else
  1072. cond = ctx->fcr31 & FPU_CSR_COND;
  1073. #endif
  1074. switch (MIPSInst_RT(ir) & 3) {
  1075. case bcfl_op:
  1076. likely = 1;
  1077. case bcf_op:
  1078. cond = !cond;
  1079. break;
  1080. case bctl_op:
  1081. likely = 1;
  1082. case bct_op:
  1083. break;
  1084. default:
  1085. /* thats an illegal instruction */
  1086. return SIGILL;
  1087. }
  1088. xcp->cp0_cause |= CAUSEF_BD;
  1089. if (cond) {
  1090. /* branch taken: emulate dslot
  1091. * instruction
  1092. */
  1093. xcp->cp0_epc += dec_insn.pc_inc;
  1094. contpc = MIPSInst_SIMM(ir);
  1095. ir = dec_insn.next_insn;
  1096. if (dec_insn.micro_mips_mode) {
  1097. contpc = (xcp->cp0_epc + (contpc << 1));
  1098. /* If 16-bit instruction, not FPU. */
  1099. if ((dec_insn.next_pc_inc == 2) ||
  1100. (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
  1101. /*
  1102. * Since this instruction will
  1103. * be put on the stack with
  1104. * 32-bit words, get around
  1105. * this problem by putting a
  1106. * NOP16 as the second one.
  1107. */
  1108. if (dec_insn.next_pc_inc == 2)
  1109. ir = (ir & (~0xffff)) | MM_NOP16;
  1110. /*
  1111. * Single step the non-CP1
  1112. * instruction in the dslot.
  1113. */
  1114. return mips_dsemul(xcp, ir, contpc);
  1115. }
  1116. } else
  1117. contpc = (xcp->cp0_epc + (contpc << 2));
  1118. switch (MIPSInst_OPCODE(ir)) {
  1119. case lwc1_op:
  1120. case swc1_op:
  1121. #if (__mips >= 2 || defined(__mips64))
  1122. case ldc1_op:
  1123. case sdc1_op:
  1124. #endif
  1125. case cop1_op:
  1126. #if __mips >= 4 && __mips != 32
  1127. case cop1x_op:
  1128. #endif
  1129. /* its one of ours */
  1130. goto emul;
  1131. #if __mips >= 4
  1132. case spec_op:
  1133. if (MIPSInst_FUNC(ir) == movc_op)
  1134. goto emul;
  1135. break;
  1136. #endif
  1137. }
  1138. /*
  1139. * Single step the non-cp1
  1140. * instruction in the dslot
  1141. */
  1142. return mips_dsemul(xcp, ir, contpc);
  1143. }
  1144. else {
  1145. /* branch not taken */
  1146. if (likely) {
  1147. /*
  1148. * branch likely nullifies
  1149. * dslot if not taken
  1150. */
  1151. xcp->cp0_epc += dec_insn.pc_inc;
  1152. contpc += dec_insn.pc_inc;
  1153. /*
  1154. * else continue & execute
  1155. * dslot as normal insn
  1156. */
  1157. }
  1158. }
  1159. break;
  1160. }
  1161. default:
  1162. if (!(MIPSInst_RS(ir) & 0x10))
  1163. return SIGILL;
  1164. {
  1165. int sig;
  1166. /* a real fpu computation instruction */
  1167. if ((sig = fpu_emu(xcp, ctx, ir)))
  1168. return sig;
  1169. }
  1170. }
  1171. break;
  1172. #if __mips >= 4 && __mips != 32
  1173. case cop1x_op:{
  1174. int sig = fpux_emu(xcp, ctx, ir, fault_addr);
  1175. if (sig)
  1176. return sig;
  1177. break;
  1178. }
  1179. #endif
  1180. #if __mips >= 4
  1181. case spec_op:
  1182. if (MIPSInst_FUNC(ir) != movc_op)
  1183. return SIGILL;
  1184. cond = fpucondbit[MIPSInst_RT(ir) >> 2];
  1185. if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
  1186. xcp->regs[MIPSInst_RD(ir)] =
  1187. xcp->regs[MIPSInst_RS(ir)];
  1188. break;
  1189. #endif
  1190. default:
  1191. return SIGILL;
  1192. }
  1193. /* we did it !! */
  1194. xcp->cp0_epc = contpc;
  1195. xcp->cp0_cause &= ~CAUSEF_BD;
  1196. return 0;
  1197. }
  1198. /*
  1199. * Conversion table from MIPS compare ops 48-63
  1200. * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
  1201. */
  1202. static const unsigned char cmptab[8] = {
  1203. 0, /* cmp_0 (sig) cmp_sf */
  1204. IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
  1205. IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
  1206. IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
  1207. IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
  1208. IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
  1209. IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
  1210. IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
  1211. };
  1212. #if __mips >= 4 && __mips != 32
  1213. /*
  1214. * Additional MIPS4 instructions
  1215. */
  1216. #define DEF3OP(name, p, f1, f2, f3) \
  1217. static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \
  1218. ieee754##p t) \
  1219. { \
  1220. struct _ieee754_csr ieee754_csr_save; \
  1221. s = f1(s, t); \
  1222. ieee754_csr_save = ieee754_csr; \
  1223. s = f2(s, r); \
  1224. ieee754_csr_save.cx |= ieee754_csr.cx; \
  1225. ieee754_csr_save.sx |= ieee754_csr.sx; \
  1226. s = f3(s); \
  1227. ieee754_csr.cx |= ieee754_csr_save.cx; \
  1228. ieee754_csr.sx |= ieee754_csr_save.sx; \
  1229. return s; \
  1230. }
  1231. static ieee754dp fpemu_dp_recip(ieee754dp d)
  1232. {
  1233. return ieee754dp_div(ieee754dp_one(0), d);
  1234. }
  1235. static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
  1236. {
  1237. return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
  1238. }
  1239. static ieee754sp fpemu_sp_recip(ieee754sp s)
  1240. {
  1241. return ieee754sp_div(ieee754sp_one(0), s);
  1242. }
  1243. static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
  1244. {
  1245. return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
  1246. }
  1247. DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
  1248. DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
  1249. DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
  1250. DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
  1251. DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
  1252. DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
  1253. DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
  1254. DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
  1255. static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1256. mips_instruction ir, void *__user *fault_addr)
  1257. {
  1258. unsigned rcsr = 0; /* resulting csr */
  1259. MIPS_FPU_EMU_INC_STATS(cp1xops);
  1260. switch (MIPSInst_FMA_FFMT(ir)) {
  1261. case s_fmt:{ /* 0 */
  1262. ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
  1263. ieee754sp fd, fr, fs, ft;
  1264. u32 __user *va;
  1265. u32 val;
  1266. switch (MIPSInst_FUNC(ir)) {
  1267. case lwxc1_op:
  1268. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1269. xcp->regs[MIPSInst_FT(ir)]);
  1270. MIPS_FPU_EMU_INC_STATS(loads);
  1271. if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
  1272. MIPS_FPU_EMU_INC_STATS(errors);
  1273. *fault_addr = va;
  1274. return SIGBUS;
  1275. }
  1276. if (__get_user(val, va)) {
  1277. MIPS_FPU_EMU_INC_STATS(errors);
  1278. *fault_addr = va;
  1279. return SIGSEGV;
  1280. }
  1281. SITOREG(val, MIPSInst_FD(ir));
  1282. break;
  1283. case swxc1_op:
  1284. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1285. xcp->regs[MIPSInst_FT(ir)]);
  1286. MIPS_FPU_EMU_INC_STATS(stores);
  1287. SIFROMREG(val, MIPSInst_FS(ir));
  1288. if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
  1289. MIPS_FPU_EMU_INC_STATS(errors);
  1290. *fault_addr = va;
  1291. return SIGBUS;
  1292. }
  1293. if (put_user(val, va)) {
  1294. MIPS_FPU_EMU_INC_STATS(errors);
  1295. *fault_addr = va;
  1296. return SIGSEGV;
  1297. }
  1298. break;
  1299. case madd_s_op:
  1300. handler = fpemu_sp_madd;
  1301. goto scoptop;
  1302. case msub_s_op:
  1303. handler = fpemu_sp_msub;
  1304. goto scoptop;
  1305. case nmadd_s_op:
  1306. handler = fpemu_sp_nmadd;
  1307. goto scoptop;
  1308. case nmsub_s_op:
  1309. handler = fpemu_sp_nmsub;
  1310. goto scoptop;
  1311. scoptop:
  1312. SPFROMREG(fr, MIPSInst_FR(ir));
  1313. SPFROMREG(fs, MIPSInst_FS(ir));
  1314. SPFROMREG(ft, MIPSInst_FT(ir));
  1315. fd = (*handler) (fr, fs, ft);
  1316. SPTOREG(fd, MIPSInst_FD(ir));
  1317. copcsr:
  1318. if (ieee754_cxtest(IEEE754_INEXACT))
  1319. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1320. if (ieee754_cxtest(IEEE754_UNDERFLOW))
  1321. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1322. if (ieee754_cxtest(IEEE754_OVERFLOW))
  1323. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1324. if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
  1325. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1326. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  1327. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1328. /*printk ("SIGFPE: fpu csr = %08x\n",
  1329. ctx->fcr31); */
  1330. return SIGFPE;
  1331. }
  1332. break;
  1333. default:
  1334. return SIGILL;
  1335. }
  1336. break;
  1337. }
  1338. case d_fmt:{ /* 1 */
  1339. ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
  1340. ieee754dp fd, fr, fs, ft;
  1341. u64 __user *va;
  1342. u64 val;
  1343. switch (MIPSInst_FUNC(ir)) {
  1344. case ldxc1_op:
  1345. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1346. xcp->regs[MIPSInst_FT(ir)]);
  1347. MIPS_FPU_EMU_INC_STATS(loads);
  1348. if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
  1349. MIPS_FPU_EMU_INC_STATS(errors);
  1350. *fault_addr = va;
  1351. return SIGBUS;
  1352. }
  1353. if (__get_user(val, va)) {
  1354. MIPS_FPU_EMU_INC_STATS(errors);
  1355. *fault_addr = va;
  1356. return SIGSEGV;
  1357. }
  1358. DITOREG(val, MIPSInst_FD(ir));
  1359. break;
  1360. case sdxc1_op:
  1361. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1362. xcp->regs[MIPSInst_FT(ir)]);
  1363. MIPS_FPU_EMU_INC_STATS(stores);
  1364. DIFROMREG(val, MIPSInst_FS(ir));
  1365. if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
  1366. MIPS_FPU_EMU_INC_STATS(errors);
  1367. *fault_addr = va;
  1368. return SIGBUS;
  1369. }
  1370. if (__put_user(val, va)) {
  1371. MIPS_FPU_EMU_INC_STATS(errors);
  1372. *fault_addr = va;
  1373. return SIGSEGV;
  1374. }
  1375. break;
  1376. case madd_d_op:
  1377. handler = fpemu_dp_madd;
  1378. goto dcoptop;
  1379. case msub_d_op:
  1380. handler = fpemu_dp_msub;
  1381. goto dcoptop;
  1382. case nmadd_d_op:
  1383. handler = fpemu_dp_nmadd;
  1384. goto dcoptop;
  1385. case nmsub_d_op:
  1386. handler = fpemu_dp_nmsub;
  1387. goto dcoptop;
  1388. dcoptop:
  1389. DPFROMREG(fr, MIPSInst_FR(ir));
  1390. DPFROMREG(fs, MIPSInst_FS(ir));
  1391. DPFROMREG(ft, MIPSInst_FT(ir));
  1392. fd = (*handler) (fr, fs, ft);
  1393. DPTOREG(fd, MIPSInst_FD(ir));
  1394. goto copcsr;
  1395. default:
  1396. return SIGILL;
  1397. }
  1398. break;
  1399. }
  1400. case 0x7: /* 7 */
  1401. if (MIPSInst_FUNC(ir) != pfetch_op) {
  1402. return SIGILL;
  1403. }
  1404. /* ignore prefx operation */
  1405. break;
  1406. default:
  1407. return SIGILL;
  1408. }
  1409. return 0;
  1410. }
  1411. #endif
  1412. /*
  1413. * Emulate a single COP1 arithmetic instruction.
  1414. */
  1415. static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1416. mips_instruction ir)
  1417. {
  1418. int rfmt; /* resulting format */
  1419. unsigned rcsr = 0; /* resulting csr */
  1420. unsigned cond;
  1421. union {
  1422. ieee754dp d;
  1423. ieee754sp s;
  1424. int w;
  1425. #ifdef __mips64
  1426. s64 l;
  1427. #endif
  1428. } rv; /* resulting value */
  1429. MIPS_FPU_EMU_INC_STATS(cp1ops);
  1430. switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
  1431. case s_fmt:{ /* 0 */
  1432. union {
  1433. ieee754sp(*b) (ieee754sp, ieee754sp);
  1434. ieee754sp(*u) (ieee754sp);
  1435. } handler;
  1436. switch (MIPSInst_FUNC(ir)) {
  1437. /* binary ops */
  1438. case fadd_op:
  1439. handler.b = ieee754sp_add;
  1440. goto scopbop;
  1441. case fsub_op:
  1442. handler.b = ieee754sp_sub;
  1443. goto scopbop;
  1444. case fmul_op:
  1445. handler.b = ieee754sp_mul;
  1446. goto scopbop;
  1447. case fdiv_op:
  1448. handler.b = ieee754sp_div;
  1449. goto scopbop;
  1450. /* unary ops */
  1451. #if __mips >= 2 || defined(__mips64)
  1452. case fsqrt_op:
  1453. handler.u = ieee754sp_sqrt;
  1454. goto scopuop;
  1455. #endif
  1456. #if __mips >= 4 && __mips != 32
  1457. case frsqrt_op:
  1458. handler.u = fpemu_sp_rsqrt;
  1459. goto scopuop;
  1460. case frecip_op:
  1461. handler.u = fpemu_sp_recip;
  1462. goto scopuop;
  1463. #endif
  1464. #if __mips >= 4
  1465. case fmovc_op:
  1466. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1467. if (((ctx->fcr31 & cond) != 0) !=
  1468. ((MIPSInst_FT(ir) & 1) != 0))
  1469. return 0;
  1470. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1471. break;
  1472. case fmovz_op:
  1473. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1474. return 0;
  1475. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1476. break;
  1477. case fmovn_op:
  1478. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1479. return 0;
  1480. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1481. break;
  1482. #endif
  1483. case fabs_op:
  1484. handler.u = ieee754sp_abs;
  1485. goto scopuop;
  1486. case fneg_op:
  1487. handler.u = ieee754sp_neg;
  1488. goto scopuop;
  1489. case fmov_op:
  1490. /* an easy one */
  1491. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1492. goto copcsr;
  1493. /* binary op on handler */
  1494. scopbop:
  1495. {
  1496. ieee754sp fs, ft;
  1497. SPFROMREG(fs, MIPSInst_FS(ir));
  1498. SPFROMREG(ft, MIPSInst_FT(ir));
  1499. rv.s = (*handler.b) (fs, ft);
  1500. goto copcsr;
  1501. }
  1502. scopuop:
  1503. {
  1504. ieee754sp fs;
  1505. SPFROMREG(fs, MIPSInst_FS(ir));
  1506. rv.s = (*handler.u) (fs);
  1507. goto copcsr;
  1508. }
  1509. copcsr:
  1510. if (ieee754_cxtest(IEEE754_INEXACT))
  1511. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1512. if (ieee754_cxtest(IEEE754_UNDERFLOW))
  1513. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1514. if (ieee754_cxtest(IEEE754_OVERFLOW))
  1515. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1516. if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
  1517. rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
  1518. if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
  1519. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1520. break;
  1521. /* unary conv ops */
  1522. case fcvts_op:
  1523. return SIGILL; /* not defined */
  1524. case fcvtd_op:{
  1525. ieee754sp fs;
  1526. SPFROMREG(fs, MIPSInst_FS(ir));
  1527. rv.d = ieee754dp_fsp(fs);
  1528. rfmt = d_fmt;
  1529. goto copcsr;
  1530. }
  1531. case fcvtw_op:{
  1532. ieee754sp fs;
  1533. SPFROMREG(fs, MIPSInst_FS(ir));
  1534. rv.w = ieee754sp_tint(fs);
  1535. rfmt = w_fmt;
  1536. goto copcsr;
  1537. }
  1538. #if __mips >= 2 || defined(__mips64)
  1539. case fround_op:
  1540. case ftrunc_op:
  1541. case fceil_op:
  1542. case ffloor_op:{
  1543. unsigned int oldrm = ieee754_csr.rm;
  1544. ieee754sp fs;
  1545. SPFROMREG(fs, MIPSInst_FS(ir));
  1546. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1547. rv.w = ieee754sp_tint(fs);
  1548. ieee754_csr.rm = oldrm;
  1549. rfmt = w_fmt;
  1550. goto copcsr;
  1551. }
  1552. #endif /* __mips >= 2 */
  1553. #if defined(__mips64)
  1554. case fcvtl_op:{
  1555. ieee754sp fs;
  1556. SPFROMREG(fs, MIPSInst_FS(ir));
  1557. rv.l = ieee754sp_tlong(fs);
  1558. rfmt = l_fmt;
  1559. goto copcsr;
  1560. }
  1561. case froundl_op:
  1562. case ftruncl_op:
  1563. case fceill_op:
  1564. case ffloorl_op:{
  1565. unsigned int oldrm = ieee754_csr.rm;
  1566. ieee754sp fs;
  1567. SPFROMREG(fs, MIPSInst_FS(ir));
  1568. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1569. rv.l = ieee754sp_tlong(fs);
  1570. ieee754_csr.rm = oldrm;
  1571. rfmt = l_fmt;
  1572. goto copcsr;
  1573. }
  1574. #endif /* defined(__mips64) */
  1575. default:
  1576. if (MIPSInst_FUNC(ir) >= fcmp_op) {
  1577. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  1578. ieee754sp fs, ft;
  1579. SPFROMREG(fs, MIPSInst_FS(ir));
  1580. SPFROMREG(ft, MIPSInst_FT(ir));
  1581. rv.w = ieee754sp_cmp(fs, ft,
  1582. cmptab[cmpop & 0x7], cmpop & 0x8);
  1583. rfmt = -1;
  1584. if ((cmpop & 0x8) && ieee754_cxtest
  1585. (IEEE754_INVALID_OPERATION))
  1586. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  1587. else
  1588. goto copcsr;
  1589. }
  1590. else {
  1591. return SIGILL;
  1592. }
  1593. break;
  1594. }
  1595. break;
  1596. }
  1597. case d_fmt:{
  1598. union {
  1599. ieee754dp(*b) (ieee754dp, ieee754dp);
  1600. ieee754dp(*u) (ieee754dp);
  1601. } handler;
  1602. switch (MIPSInst_FUNC(ir)) {
  1603. /* binary ops */
  1604. case fadd_op:
  1605. handler.b = ieee754dp_add;
  1606. goto dcopbop;
  1607. case fsub_op:
  1608. handler.b = ieee754dp_sub;
  1609. goto dcopbop;
  1610. case fmul_op:
  1611. handler.b = ieee754dp_mul;
  1612. goto dcopbop;
  1613. case fdiv_op:
  1614. handler.b = ieee754dp_div;
  1615. goto dcopbop;
  1616. /* unary ops */
  1617. #if __mips >= 2 || defined(__mips64)
  1618. case fsqrt_op:
  1619. handler.u = ieee754dp_sqrt;
  1620. goto dcopuop;
  1621. #endif
  1622. #if __mips >= 4 && __mips != 32
  1623. case frsqrt_op:
  1624. handler.u = fpemu_dp_rsqrt;
  1625. goto dcopuop;
  1626. case frecip_op:
  1627. handler.u = fpemu_dp_recip;
  1628. goto dcopuop;
  1629. #endif
  1630. #if __mips >= 4
  1631. case fmovc_op:
  1632. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1633. if (((ctx->fcr31 & cond) != 0) !=
  1634. ((MIPSInst_FT(ir) & 1) != 0))
  1635. return 0;
  1636. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1637. break;
  1638. case fmovz_op:
  1639. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1640. return 0;
  1641. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1642. break;
  1643. case fmovn_op:
  1644. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1645. return 0;
  1646. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1647. break;
  1648. #endif
  1649. case fabs_op:
  1650. handler.u = ieee754dp_abs;
  1651. goto dcopuop;
  1652. case fneg_op:
  1653. handler.u = ieee754dp_neg;
  1654. goto dcopuop;
  1655. case fmov_op:
  1656. /* an easy one */
  1657. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1658. goto copcsr;
  1659. /* binary op on handler */
  1660. dcopbop:{
  1661. ieee754dp fs, ft;
  1662. DPFROMREG(fs, MIPSInst_FS(ir));
  1663. DPFROMREG(ft, MIPSInst_FT(ir));
  1664. rv.d = (*handler.b) (fs, ft);
  1665. goto copcsr;
  1666. }
  1667. dcopuop:{
  1668. ieee754dp fs;
  1669. DPFROMREG(fs, MIPSInst_FS(ir));
  1670. rv.d = (*handler.u) (fs);
  1671. goto copcsr;
  1672. }
  1673. /* unary conv ops */
  1674. case fcvts_op:{
  1675. ieee754dp fs;
  1676. DPFROMREG(fs, MIPSInst_FS(ir));
  1677. rv.s = ieee754sp_fdp(fs);
  1678. rfmt = s_fmt;
  1679. goto copcsr;
  1680. }
  1681. case fcvtd_op:
  1682. return SIGILL; /* not defined */
  1683. case fcvtw_op:{
  1684. ieee754dp fs;
  1685. DPFROMREG(fs, MIPSInst_FS(ir));
  1686. rv.w = ieee754dp_tint(fs); /* wrong */
  1687. rfmt = w_fmt;
  1688. goto copcsr;
  1689. }
  1690. #if __mips >= 2 || defined(__mips64)
  1691. case fround_op:
  1692. case ftrunc_op:
  1693. case fceil_op:
  1694. case ffloor_op:{
  1695. unsigned int oldrm = ieee754_csr.rm;
  1696. ieee754dp fs;
  1697. DPFROMREG(fs, MIPSInst_FS(ir));
  1698. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1699. rv.w = ieee754dp_tint(fs);
  1700. ieee754_csr.rm = oldrm;
  1701. rfmt = w_fmt;
  1702. goto copcsr;
  1703. }
  1704. #endif
  1705. #if defined(__mips64)
  1706. case fcvtl_op:{
  1707. ieee754dp fs;
  1708. DPFROMREG(fs, MIPSInst_FS(ir));
  1709. rv.l = ieee754dp_tlong(fs);
  1710. rfmt = l_fmt;
  1711. goto copcsr;
  1712. }
  1713. case froundl_op:
  1714. case ftruncl_op:
  1715. case fceill_op:
  1716. case ffloorl_op:{
  1717. unsigned int oldrm = ieee754_csr.rm;
  1718. ieee754dp fs;
  1719. DPFROMREG(fs, MIPSInst_FS(ir));
  1720. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1721. rv.l = ieee754dp_tlong(fs);
  1722. ieee754_csr.rm = oldrm;
  1723. rfmt = l_fmt;
  1724. goto copcsr;
  1725. }
  1726. #endif /* __mips >= 3 */
  1727. default:
  1728. if (MIPSInst_FUNC(ir) >= fcmp_op) {
  1729. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  1730. ieee754dp fs, ft;
  1731. DPFROMREG(fs, MIPSInst_FS(ir));
  1732. DPFROMREG(ft, MIPSInst_FT(ir));
  1733. rv.w = ieee754dp_cmp(fs, ft,
  1734. cmptab[cmpop & 0x7], cmpop & 0x8);
  1735. rfmt = -1;
  1736. if ((cmpop & 0x8)
  1737. &&
  1738. ieee754_cxtest
  1739. (IEEE754_INVALID_OPERATION))
  1740. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  1741. else
  1742. goto copcsr;
  1743. }
  1744. else {
  1745. return SIGILL;
  1746. }
  1747. break;
  1748. }
  1749. break;
  1750. }
  1751. case w_fmt:{
  1752. ieee754sp fs;
  1753. switch (MIPSInst_FUNC(ir)) {
  1754. case fcvts_op:
  1755. /* convert word to single precision real */
  1756. SPFROMREG(fs, MIPSInst_FS(ir));
  1757. rv.s = ieee754sp_fint(fs.bits);
  1758. rfmt = s_fmt;
  1759. goto copcsr;
  1760. case fcvtd_op:
  1761. /* convert word to double precision real */
  1762. SPFROMREG(fs, MIPSInst_FS(ir));
  1763. rv.d = ieee754dp_fint(fs.bits);
  1764. rfmt = d_fmt;
  1765. goto copcsr;
  1766. default:
  1767. return SIGILL;
  1768. }
  1769. break;
  1770. }
  1771. #if defined(__mips64)
  1772. case l_fmt:{
  1773. switch (MIPSInst_FUNC(ir)) {
  1774. case fcvts_op:
  1775. /* convert long to single precision real */
  1776. rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
  1777. rfmt = s_fmt;
  1778. goto copcsr;
  1779. case fcvtd_op:
  1780. /* convert long to double precision real */
  1781. rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
  1782. rfmt = d_fmt;
  1783. goto copcsr;
  1784. default:
  1785. return SIGILL;
  1786. }
  1787. break;
  1788. }
  1789. #endif
  1790. default:
  1791. return SIGILL;
  1792. }
  1793. /*
  1794. * Update the fpu CSR register for this operation.
  1795. * If an exception is required, generate a tidy SIGFPE exception,
  1796. * without updating the result register.
  1797. * Note: cause exception bits do not accumulate, they are rewritten
  1798. * for each op; only the flag/sticky bits accumulate.
  1799. */
  1800. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  1801. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1802. /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
  1803. return SIGFPE;
  1804. }
  1805. /*
  1806. * Now we can safely write the result back to the register file.
  1807. */
  1808. switch (rfmt) {
  1809. case -1:{
  1810. #if __mips >= 4
  1811. cond = fpucondbit[MIPSInst_FD(ir) >> 2];
  1812. #else
  1813. cond = FPU_CSR_COND;
  1814. #endif
  1815. if (rv.w)
  1816. ctx->fcr31 |= cond;
  1817. else
  1818. ctx->fcr31 &= ~cond;
  1819. break;
  1820. }
  1821. case d_fmt:
  1822. DPTOREG(rv.d, MIPSInst_FD(ir));
  1823. break;
  1824. case s_fmt:
  1825. SPTOREG(rv.s, MIPSInst_FD(ir));
  1826. break;
  1827. case w_fmt:
  1828. SITOREG(rv.w, MIPSInst_FD(ir));
  1829. break;
  1830. #if defined(__mips64)
  1831. case l_fmt:
  1832. DITOREG(rv.l, MIPSInst_FD(ir));
  1833. break;
  1834. #endif
  1835. default:
  1836. return SIGILL;
  1837. }
  1838. return 0;
  1839. }
  1840. int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1841. int has_fpu, void *__user *fault_addr)
  1842. {
  1843. unsigned long oldepc, prevepc;
  1844. struct mm_decoded_insn dec_insn;
  1845. u16 instr[4];
  1846. u16 *instr_ptr;
  1847. int sig = 0;
  1848. oldepc = xcp->cp0_epc;
  1849. do {
  1850. prevepc = xcp->cp0_epc;
  1851. if (get_isa16_mode(prevepc) && cpu_has_mmips) {
  1852. /*
  1853. * Get next 2 microMIPS instructions and convert them
  1854. * into 32-bit instructions.
  1855. */
  1856. if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
  1857. (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
  1858. (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
  1859. (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
  1860. MIPS_FPU_EMU_INC_STATS(errors);
  1861. return SIGBUS;
  1862. }
  1863. instr_ptr = instr;
  1864. /* Get first instruction. */
  1865. if (mm_insn_16bit(*instr_ptr)) {
  1866. /* Duplicate the half-word. */
  1867. dec_insn.insn = (*instr_ptr << 16) |
  1868. (*instr_ptr);
  1869. /* 16-bit instruction. */
  1870. dec_insn.pc_inc = 2;
  1871. instr_ptr += 1;
  1872. } else {
  1873. dec_insn.insn = (*instr_ptr << 16) |
  1874. *(instr_ptr+1);
  1875. /* 32-bit instruction. */
  1876. dec_insn.pc_inc = 4;
  1877. instr_ptr += 2;
  1878. }
  1879. /* Get second instruction. */
  1880. if (mm_insn_16bit(*instr_ptr)) {
  1881. /* Duplicate the half-word. */
  1882. dec_insn.next_insn = (*instr_ptr << 16) |
  1883. (*instr_ptr);
  1884. /* 16-bit instruction. */
  1885. dec_insn.next_pc_inc = 2;
  1886. } else {
  1887. dec_insn.next_insn = (*instr_ptr << 16) |
  1888. *(instr_ptr+1);
  1889. /* 32-bit instruction. */
  1890. dec_insn.next_pc_inc = 4;
  1891. }
  1892. dec_insn.micro_mips_mode = 1;
  1893. } else {
  1894. if ((get_user(dec_insn.insn,
  1895. (mips_instruction __user *) xcp->cp0_epc)) ||
  1896. (get_user(dec_insn.next_insn,
  1897. (mips_instruction __user *)(xcp->cp0_epc+4)))) {
  1898. MIPS_FPU_EMU_INC_STATS(errors);
  1899. return SIGBUS;
  1900. }
  1901. dec_insn.pc_inc = 4;
  1902. dec_insn.next_pc_inc = 4;
  1903. dec_insn.micro_mips_mode = 0;
  1904. }
  1905. if ((dec_insn.insn == 0) ||
  1906. ((dec_insn.pc_inc == 2) &&
  1907. ((dec_insn.insn & 0xffff) == MM_NOP16)))
  1908. xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
  1909. else {
  1910. /*
  1911. * The 'ieee754_csr' is an alias of
  1912. * ctx->fcr31. No need to copy ctx->fcr31 to
  1913. * ieee754_csr. But ieee754_csr.rm is ieee
  1914. * library modes. (not mips rounding mode)
  1915. */
  1916. /* convert to ieee library modes */
  1917. ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
  1918. sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
  1919. /* revert to mips rounding mode */
  1920. ieee754_csr.rm = mips_rm[ieee754_csr.rm];
  1921. }
  1922. if (has_fpu)
  1923. break;
  1924. if (sig)
  1925. break;
  1926. cond_resched();
  1927. } while (xcp->cp0_epc > prevepc);
  1928. /* SIGILL indicates a non-fpu instruction */
  1929. if (sig == SIGILL && xcp->cp0_epc != oldepc)
  1930. /* but if epc has advanced, then ignore it */
  1931. sig = 0;
  1932. return sig;
  1933. }
  1934. #ifdef CONFIG_DEBUG_FS
  1935. static int fpuemu_stat_get(void *data, u64 *val)
  1936. {
  1937. int cpu;
  1938. unsigned long sum = 0;
  1939. for_each_online_cpu(cpu) {
  1940. struct mips_fpu_emulator_stats *ps;
  1941. local_t *pv;
  1942. ps = &per_cpu(fpuemustats, cpu);
  1943. pv = (void *)ps + (unsigned long)data;
  1944. sum += local_read(pv);
  1945. }
  1946. *val = sum;
  1947. return 0;
  1948. }
  1949. DEFINE_SIMPLE_ATTRIBUTE(fops_fpuemu_stat, fpuemu_stat_get, NULL, "%llu\n");
  1950. extern struct dentry *mips_debugfs_dir;
  1951. static int __init debugfs_fpuemu(void)
  1952. {
  1953. struct dentry *d, *dir;
  1954. if (!mips_debugfs_dir)
  1955. return -ENODEV;
  1956. dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir);
  1957. if (!dir)
  1958. return -ENOMEM;
  1959. #define FPU_STAT_CREATE(M) \
  1960. do { \
  1961. d = debugfs_create_file(#M , S_IRUGO, dir, \
  1962. (void *)offsetof(struct mips_fpu_emulator_stats, M), \
  1963. &fops_fpuemu_stat); \
  1964. if (!d) \
  1965. return -ENOMEM; \
  1966. } while (0)
  1967. FPU_STAT_CREATE(emulated);
  1968. FPU_STAT_CREATE(loads);
  1969. FPU_STAT_CREATE(stores);
  1970. FPU_STAT_CREATE(cp1ops);
  1971. FPU_STAT_CREATE(cp1xops);
  1972. FPU_STAT_CREATE(errors);
  1973. return 0;
  1974. }
  1975. __initcall(debugfs_fpuemu);
  1976. #endif