cp1emu.c 52 KB

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