cp1emu.c 48 KB

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