cp1emu.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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. #define SIFROMREG(si, x) \
  626. do { \
  627. if (cop1_64bit(xcp)) \
  628. (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
  629. else \
  630. (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
  631. } while (0)
  632. #define SITOREG(si, x) \
  633. do { \
  634. if (cop1_64bit(xcp)) { \
  635. unsigned i; \
  636. set_fpr32(&ctx->fpr[x], 0, si); \
  637. for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
  638. set_fpr32(&ctx->fpr[x], i, 0); \
  639. } else { \
  640. set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
  641. } \
  642. } while (0)
  643. #define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
  644. #define SITOHREG(si, x) \
  645. do { \
  646. unsigned i; \
  647. set_fpr32(&ctx->fpr[x], 1, si); \
  648. for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
  649. set_fpr32(&ctx->fpr[x], i, 0); \
  650. } while (0)
  651. #define DIFROMREG(di, x) \
  652. ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
  653. #define DITOREG(di, x) \
  654. do { \
  655. unsigned fpr, i; \
  656. fpr = (x) & ~(cop1_64bit(xcp) == 0); \
  657. set_fpr64(&ctx->fpr[fpr], 0, di); \
  658. for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
  659. set_fpr64(&ctx->fpr[fpr], i, 0); \
  660. } while (0)
  661. #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
  662. #define SPTOREG(sp, x) SITOREG((sp).bits, x)
  663. #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
  664. #define DPTOREG(dp, x) DITOREG((dp).bits, x)
  665. /*
  666. * Emulate the single floating point instruction pointed at by EPC.
  667. * Two instructions if the instruction is in a branch delay slot.
  668. */
  669. static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  670. struct mm_decoded_insn dec_insn, void *__user *fault_addr)
  671. {
  672. unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
  673. unsigned int cond, cbit;
  674. mips_instruction ir;
  675. int likely, pc_inc;
  676. u32 __user *wva;
  677. u64 __user *dva;
  678. u32 value;
  679. u32 wval;
  680. u64 dval;
  681. int sig;
  682. /*
  683. * These are giving gcc a gentle hint about what to expect in
  684. * dec_inst in order to do better optimization.
  685. */
  686. if (!cpu_has_mmips && dec_insn.micro_mips_mode)
  687. unreachable();
  688. /* XXX NEC Vr54xx bug workaround */
  689. if (delay_slot(xcp)) {
  690. if (dec_insn.micro_mips_mode) {
  691. if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
  692. clear_delay_slot(xcp);
  693. } else {
  694. if (!isBranchInstr(xcp, dec_insn, &contpc))
  695. clear_delay_slot(xcp);
  696. }
  697. }
  698. if (delay_slot(xcp)) {
  699. /*
  700. * The instruction to be emulated is in a branch delay slot
  701. * which means that we have to emulate the branch instruction
  702. * BEFORE we do the cop1 instruction.
  703. *
  704. * This branch could be a COP1 branch, but in that case we
  705. * would have had a trap for that instruction, and would not
  706. * come through this route.
  707. *
  708. * Linux MIPS branch emulator operates on context, updating the
  709. * cp0_epc.
  710. */
  711. ir = dec_insn.next_insn; /* process delay slot instr */
  712. pc_inc = dec_insn.next_pc_inc;
  713. } else {
  714. ir = dec_insn.insn; /* process current instr */
  715. pc_inc = dec_insn.pc_inc;
  716. }
  717. /*
  718. * Since microMIPS FPU instructios are a subset of MIPS32 FPU
  719. * instructions, we want to convert microMIPS FPU instructions
  720. * into MIPS32 instructions so that we could reuse all of the
  721. * FPU emulation code.
  722. *
  723. * NOTE: We cannot do this for branch instructions since they
  724. * are not a subset. Example: Cannot emulate a 16-bit
  725. * aligned target address with a MIPS32 instruction.
  726. */
  727. if (dec_insn.micro_mips_mode) {
  728. /*
  729. * If next instruction is a 16-bit instruction, then it
  730. * it cannot be a FPU instruction. This could happen
  731. * since we can be called for non-FPU instructions.
  732. */
  733. if ((pc_inc == 2) ||
  734. (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
  735. == SIGILL))
  736. return SIGILL;
  737. }
  738. emul:
  739. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
  740. MIPS_FPU_EMU_INC_STATS(emulated);
  741. switch (MIPSInst_OPCODE(ir)) {
  742. case ldc1_op:
  743. dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  744. MIPSInst_SIMM(ir));
  745. MIPS_FPU_EMU_INC_STATS(loads);
  746. if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
  747. MIPS_FPU_EMU_INC_STATS(errors);
  748. *fault_addr = dva;
  749. return SIGBUS;
  750. }
  751. if (__get_user(dval, dva)) {
  752. MIPS_FPU_EMU_INC_STATS(errors);
  753. *fault_addr = dva;
  754. return SIGSEGV;
  755. }
  756. DITOREG(dval, MIPSInst_RT(ir));
  757. break;
  758. case sdc1_op:
  759. dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  760. MIPSInst_SIMM(ir));
  761. MIPS_FPU_EMU_INC_STATS(stores);
  762. DIFROMREG(dval, MIPSInst_RT(ir));
  763. if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
  764. MIPS_FPU_EMU_INC_STATS(errors);
  765. *fault_addr = dva;
  766. return SIGBUS;
  767. }
  768. if (__put_user(dval, dva)) {
  769. MIPS_FPU_EMU_INC_STATS(errors);
  770. *fault_addr = dva;
  771. return SIGSEGV;
  772. }
  773. break;
  774. case lwc1_op:
  775. wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  776. MIPSInst_SIMM(ir));
  777. MIPS_FPU_EMU_INC_STATS(loads);
  778. if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
  779. MIPS_FPU_EMU_INC_STATS(errors);
  780. *fault_addr = wva;
  781. return SIGBUS;
  782. }
  783. if (__get_user(wval, wva)) {
  784. MIPS_FPU_EMU_INC_STATS(errors);
  785. *fault_addr = wva;
  786. return SIGSEGV;
  787. }
  788. SITOREG(wval, MIPSInst_RT(ir));
  789. break;
  790. case swc1_op:
  791. wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  792. MIPSInst_SIMM(ir));
  793. MIPS_FPU_EMU_INC_STATS(stores);
  794. SIFROMREG(wval, MIPSInst_RT(ir));
  795. if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
  796. MIPS_FPU_EMU_INC_STATS(errors);
  797. *fault_addr = wva;
  798. return SIGBUS;
  799. }
  800. if (__put_user(wval, wva)) {
  801. MIPS_FPU_EMU_INC_STATS(errors);
  802. *fault_addr = wva;
  803. return SIGSEGV;
  804. }
  805. break;
  806. case cop1_op:
  807. switch (MIPSInst_RS(ir)) {
  808. case dmfc_op:
  809. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  810. return SIGILL;
  811. /* copregister fs -> gpr[rt] */
  812. if (MIPSInst_RT(ir) != 0) {
  813. DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  814. MIPSInst_RD(ir));
  815. }
  816. break;
  817. case dmtc_op:
  818. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  819. return SIGILL;
  820. /* copregister fs <- rt */
  821. DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  822. break;
  823. case mfhc_op:
  824. if (!cpu_has_mips_r2)
  825. goto sigill;
  826. /* copregister rd -> gpr[rt] */
  827. if (MIPSInst_RT(ir) != 0) {
  828. SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
  829. MIPSInst_RD(ir));
  830. }
  831. break;
  832. case mthc_op:
  833. if (!cpu_has_mips_r2)
  834. goto sigill;
  835. /* copregister rd <- gpr[rt] */
  836. SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  837. break;
  838. case mfc_op:
  839. /* copregister rd -> gpr[rt] */
  840. if (MIPSInst_RT(ir) != 0) {
  841. SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  842. MIPSInst_RD(ir));
  843. }
  844. break;
  845. case mtc_op:
  846. /* copregister rd <- rt */
  847. SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  848. break;
  849. case cfc_op:
  850. /* cop control register rd -> gpr[rt] */
  851. if (MIPSInst_RD(ir) == FPCREG_CSR) {
  852. value = ctx->fcr31;
  853. value = (value & ~FPU_CSR_RM) | modeindex(value);
  854. pr_debug("%p gpr[%d]<-csr=%08x\n",
  855. (void *) (xcp->cp0_epc),
  856. MIPSInst_RT(ir), value);
  857. }
  858. else if (MIPSInst_RD(ir) == FPCREG_RID)
  859. value = 0;
  860. else
  861. value = 0;
  862. if (MIPSInst_RT(ir))
  863. xcp->regs[MIPSInst_RT(ir)] = value;
  864. break;
  865. case ctc_op:
  866. /* copregister rd <- rt */
  867. if (MIPSInst_RT(ir) == 0)
  868. value = 0;
  869. else
  870. value = xcp->regs[MIPSInst_RT(ir)];
  871. /* we only have one writable control reg
  872. */
  873. if (MIPSInst_RD(ir) == FPCREG_CSR) {
  874. pr_debug("%p gpr[%d]->csr=%08x\n",
  875. (void *) (xcp->cp0_epc),
  876. MIPSInst_RT(ir), value);
  877. /*
  878. * Don't write reserved bits,
  879. * and convert to ieee library modes
  880. */
  881. ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
  882. modeindex(value);
  883. }
  884. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  885. return SIGFPE;
  886. }
  887. break;
  888. case bc_op:
  889. if (delay_slot(xcp))
  890. return SIGILL;
  891. if (cpu_has_mips_4_5_r)
  892. cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
  893. else
  894. cbit = FPU_CSR_COND;
  895. cond = ctx->fcr31 & cbit;
  896. likely = 0;
  897. switch (MIPSInst_RT(ir) & 3) {
  898. case bcfl_op:
  899. likely = 1;
  900. case bcf_op:
  901. cond = !cond;
  902. break;
  903. case bctl_op:
  904. likely = 1;
  905. case bct_op:
  906. break;
  907. default:
  908. /* thats an illegal instruction */
  909. return SIGILL;
  910. }
  911. set_delay_slot(xcp);
  912. if (cond) {
  913. /*
  914. * Branch taken: emulate dslot instruction
  915. */
  916. xcp->cp0_epc += dec_insn.pc_inc;
  917. contpc = MIPSInst_SIMM(ir);
  918. ir = dec_insn.next_insn;
  919. if (dec_insn.micro_mips_mode) {
  920. contpc = (xcp->cp0_epc + (contpc << 1));
  921. /* If 16-bit instruction, not FPU. */
  922. if ((dec_insn.next_pc_inc == 2) ||
  923. (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
  924. /*
  925. * Since this instruction will
  926. * be put on the stack with
  927. * 32-bit words, get around
  928. * this problem by putting a
  929. * NOP16 as the second one.
  930. */
  931. if (dec_insn.next_pc_inc == 2)
  932. ir = (ir & (~0xffff)) | MM_NOP16;
  933. /*
  934. * Single step the non-CP1
  935. * instruction in the dslot.
  936. */
  937. return mips_dsemul(xcp, ir, contpc);
  938. }
  939. } else
  940. contpc = (xcp->cp0_epc + (contpc << 2));
  941. switch (MIPSInst_OPCODE(ir)) {
  942. case lwc1_op:
  943. goto emul;
  944. case swc1_op:
  945. goto emul;
  946. case ldc1_op:
  947. case sdc1_op:
  948. if (cpu_has_mips_2_3_4_5 ||
  949. cpu_has_mips64)
  950. goto emul;
  951. return SIGILL;
  952. goto emul;
  953. case cop1_op:
  954. goto emul;
  955. case cop1x_op:
  956. if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
  957. /* its one of ours */
  958. goto emul;
  959. return SIGILL;
  960. case spec_op:
  961. if (!cpu_has_mips_4_5_r)
  962. return SIGILL;
  963. if (MIPSInst_FUNC(ir) == movc_op)
  964. goto emul;
  965. break;
  966. }
  967. /*
  968. * Single step the non-cp1
  969. * instruction in the dslot
  970. */
  971. return mips_dsemul(xcp, ir, contpc);
  972. } else if (likely) { /* branch not taken */
  973. /*
  974. * branch likely nullifies
  975. * dslot if not taken
  976. */
  977. xcp->cp0_epc += dec_insn.pc_inc;
  978. contpc += dec_insn.pc_inc;
  979. /*
  980. * else continue & execute
  981. * dslot as normal insn
  982. */
  983. }
  984. break;
  985. default:
  986. if (!(MIPSInst_RS(ir) & 0x10))
  987. return SIGILL;
  988. /* a real fpu computation instruction */
  989. if ((sig = fpu_emu(xcp, ctx, ir)))
  990. return sig;
  991. }
  992. break;
  993. case cop1x_op:
  994. if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
  995. return SIGILL;
  996. sig = fpux_emu(xcp, ctx, ir, fault_addr);
  997. if (sig)
  998. return sig;
  999. break;
  1000. case spec_op:
  1001. if (!cpu_has_mips_4_5_r)
  1002. return SIGILL;
  1003. if (MIPSInst_FUNC(ir) != movc_op)
  1004. return SIGILL;
  1005. cond = fpucondbit[MIPSInst_RT(ir) >> 2];
  1006. if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
  1007. xcp->regs[MIPSInst_RD(ir)] =
  1008. xcp->regs[MIPSInst_RS(ir)];
  1009. break;
  1010. default:
  1011. sigill:
  1012. return SIGILL;
  1013. }
  1014. /* we did it !! */
  1015. xcp->cp0_epc = contpc;
  1016. clear_delay_slot(xcp);
  1017. return 0;
  1018. }
  1019. /*
  1020. * Conversion table from MIPS compare ops 48-63
  1021. * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
  1022. */
  1023. static const unsigned char cmptab[8] = {
  1024. 0, /* cmp_0 (sig) cmp_sf */
  1025. IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
  1026. IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
  1027. IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
  1028. IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
  1029. IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
  1030. IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
  1031. IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
  1032. };
  1033. /*
  1034. * Additional MIPS4 instructions
  1035. */
  1036. #define DEF3OP(name, p, f1, f2, f3) \
  1037. static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
  1038. union ieee754##p s, union ieee754##p t) \
  1039. { \
  1040. struct _ieee754_csr ieee754_csr_save; \
  1041. s = f1(s, t); \
  1042. ieee754_csr_save = ieee754_csr; \
  1043. s = f2(s, r); \
  1044. ieee754_csr_save.cx |= ieee754_csr.cx; \
  1045. ieee754_csr_save.sx |= ieee754_csr.sx; \
  1046. s = f3(s); \
  1047. ieee754_csr.cx |= ieee754_csr_save.cx; \
  1048. ieee754_csr.sx |= ieee754_csr_save.sx; \
  1049. return s; \
  1050. }
  1051. static union ieee754dp fpemu_dp_recip(union ieee754dp d)
  1052. {
  1053. return ieee754dp_div(ieee754dp_one(0), d);
  1054. }
  1055. static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
  1056. {
  1057. return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
  1058. }
  1059. static union ieee754sp fpemu_sp_recip(union ieee754sp s)
  1060. {
  1061. return ieee754sp_div(ieee754sp_one(0), s);
  1062. }
  1063. static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
  1064. {
  1065. return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
  1066. }
  1067. DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
  1068. DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
  1069. DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
  1070. DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
  1071. DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
  1072. DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
  1073. DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
  1074. DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
  1075. static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1076. mips_instruction ir, void *__user *fault_addr)
  1077. {
  1078. unsigned rcsr = 0; /* resulting csr */
  1079. MIPS_FPU_EMU_INC_STATS(cp1xops);
  1080. switch (MIPSInst_FMA_FFMT(ir)) {
  1081. case s_fmt:{ /* 0 */
  1082. union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
  1083. union ieee754sp fd, fr, fs, ft;
  1084. u32 __user *va;
  1085. u32 val;
  1086. switch (MIPSInst_FUNC(ir)) {
  1087. case lwxc1_op:
  1088. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1089. xcp->regs[MIPSInst_FT(ir)]);
  1090. MIPS_FPU_EMU_INC_STATS(loads);
  1091. if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
  1092. MIPS_FPU_EMU_INC_STATS(errors);
  1093. *fault_addr = va;
  1094. return SIGBUS;
  1095. }
  1096. if (__get_user(val, va)) {
  1097. MIPS_FPU_EMU_INC_STATS(errors);
  1098. *fault_addr = va;
  1099. return SIGSEGV;
  1100. }
  1101. SITOREG(val, MIPSInst_FD(ir));
  1102. break;
  1103. case swxc1_op:
  1104. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1105. xcp->regs[MIPSInst_FT(ir)]);
  1106. MIPS_FPU_EMU_INC_STATS(stores);
  1107. SIFROMREG(val, MIPSInst_FS(ir));
  1108. if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
  1109. MIPS_FPU_EMU_INC_STATS(errors);
  1110. *fault_addr = va;
  1111. return SIGBUS;
  1112. }
  1113. if (put_user(val, va)) {
  1114. MIPS_FPU_EMU_INC_STATS(errors);
  1115. *fault_addr = va;
  1116. return SIGSEGV;
  1117. }
  1118. break;
  1119. case madd_s_op:
  1120. handler = fpemu_sp_madd;
  1121. goto scoptop;
  1122. case msub_s_op:
  1123. handler = fpemu_sp_msub;
  1124. goto scoptop;
  1125. case nmadd_s_op:
  1126. handler = fpemu_sp_nmadd;
  1127. goto scoptop;
  1128. case nmsub_s_op:
  1129. handler = fpemu_sp_nmsub;
  1130. goto scoptop;
  1131. scoptop:
  1132. SPFROMREG(fr, MIPSInst_FR(ir));
  1133. SPFROMREG(fs, MIPSInst_FS(ir));
  1134. SPFROMREG(ft, MIPSInst_FT(ir));
  1135. fd = (*handler) (fr, fs, ft);
  1136. SPTOREG(fd, MIPSInst_FD(ir));
  1137. copcsr:
  1138. if (ieee754_cxtest(IEEE754_INEXACT)) {
  1139. MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
  1140. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1141. }
  1142. if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
  1143. MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
  1144. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1145. }
  1146. if (ieee754_cxtest(IEEE754_OVERFLOW)) {
  1147. MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
  1148. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1149. }
  1150. if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
  1151. MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
  1152. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1153. }
  1154. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  1155. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1156. /*printk ("SIGFPE: FPU csr = %08x\n",
  1157. ctx->fcr31); */
  1158. return SIGFPE;
  1159. }
  1160. break;
  1161. default:
  1162. return SIGILL;
  1163. }
  1164. break;
  1165. }
  1166. case d_fmt:{ /* 1 */
  1167. union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
  1168. union ieee754dp fd, fr, fs, ft;
  1169. u64 __user *va;
  1170. u64 val;
  1171. switch (MIPSInst_FUNC(ir)) {
  1172. case ldxc1_op:
  1173. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1174. xcp->regs[MIPSInst_FT(ir)]);
  1175. MIPS_FPU_EMU_INC_STATS(loads);
  1176. if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
  1177. MIPS_FPU_EMU_INC_STATS(errors);
  1178. *fault_addr = va;
  1179. return SIGBUS;
  1180. }
  1181. if (__get_user(val, va)) {
  1182. MIPS_FPU_EMU_INC_STATS(errors);
  1183. *fault_addr = va;
  1184. return SIGSEGV;
  1185. }
  1186. DITOREG(val, MIPSInst_FD(ir));
  1187. break;
  1188. case sdxc1_op:
  1189. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1190. xcp->regs[MIPSInst_FT(ir)]);
  1191. MIPS_FPU_EMU_INC_STATS(stores);
  1192. DIFROMREG(val, MIPSInst_FS(ir));
  1193. if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
  1194. MIPS_FPU_EMU_INC_STATS(errors);
  1195. *fault_addr = va;
  1196. return SIGBUS;
  1197. }
  1198. if (__put_user(val, va)) {
  1199. MIPS_FPU_EMU_INC_STATS(errors);
  1200. *fault_addr = va;
  1201. return SIGSEGV;
  1202. }
  1203. break;
  1204. case madd_d_op:
  1205. handler = fpemu_dp_madd;
  1206. goto dcoptop;
  1207. case msub_d_op:
  1208. handler = fpemu_dp_msub;
  1209. goto dcoptop;
  1210. case nmadd_d_op:
  1211. handler = fpemu_dp_nmadd;
  1212. goto dcoptop;
  1213. case nmsub_d_op:
  1214. handler = fpemu_dp_nmsub;
  1215. goto dcoptop;
  1216. dcoptop:
  1217. DPFROMREG(fr, MIPSInst_FR(ir));
  1218. DPFROMREG(fs, MIPSInst_FS(ir));
  1219. DPFROMREG(ft, MIPSInst_FT(ir));
  1220. fd = (*handler) (fr, fs, ft);
  1221. DPTOREG(fd, MIPSInst_FD(ir));
  1222. goto copcsr;
  1223. default:
  1224. return SIGILL;
  1225. }
  1226. break;
  1227. }
  1228. case 0x3:
  1229. if (MIPSInst_FUNC(ir) != pfetch_op)
  1230. return SIGILL;
  1231. /* ignore prefx operation */
  1232. break;
  1233. default:
  1234. return SIGILL;
  1235. }
  1236. return 0;
  1237. }
  1238. /*
  1239. * Emulate a single COP1 arithmetic instruction.
  1240. */
  1241. static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1242. mips_instruction ir)
  1243. {
  1244. int rfmt; /* resulting format */
  1245. unsigned rcsr = 0; /* resulting csr */
  1246. unsigned int oldrm;
  1247. unsigned int cbit;
  1248. unsigned cond;
  1249. union {
  1250. union ieee754dp d;
  1251. union ieee754sp s;
  1252. int w;
  1253. s64 l;
  1254. } rv; /* resulting value */
  1255. u64 bits;
  1256. MIPS_FPU_EMU_INC_STATS(cp1ops);
  1257. switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
  1258. case s_fmt: { /* 0 */
  1259. union {
  1260. union ieee754sp(*b) (union ieee754sp, union ieee754sp);
  1261. union ieee754sp(*u) (union ieee754sp);
  1262. } handler;
  1263. union ieee754sp fs, ft;
  1264. switch (MIPSInst_FUNC(ir)) {
  1265. /* binary ops */
  1266. case fadd_op:
  1267. handler.b = ieee754sp_add;
  1268. goto scopbop;
  1269. case fsub_op:
  1270. handler.b = ieee754sp_sub;
  1271. goto scopbop;
  1272. case fmul_op:
  1273. handler.b = ieee754sp_mul;
  1274. goto scopbop;
  1275. case fdiv_op:
  1276. handler.b = ieee754sp_div;
  1277. goto scopbop;
  1278. /* unary ops */
  1279. case fsqrt_op:
  1280. if (!cpu_has_mips_4_5_r)
  1281. return SIGILL;
  1282. handler.u = ieee754sp_sqrt;
  1283. goto scopuop;
  1284. /*
  1285. * Note that on some MIPS IV implementations such as the
  1286. * R5000 and R8000 the FSQRT and FRECIP instructions do not
  1287. * achieve full IEEE-754 accuracy - however this emulator does.
  1288. */
  1289. case frsqrt_op:
  1290. if (!cpu_has_mips_4_5_r2)
  1291. return SIGILL;
  1292. handler.u = fpemu_sp_rsqrt;
  1293. goto scopuop;
  1294. case frecip_op:
  1295. if (!cpu_has_mips_4_5_r2)
  1296. return SIGILL;
  1297. handler.u = fpemu_sp_recip;
  1298. goto scopuop;
  1299. case fmovc_op:
  1300. if (!cpu_has_mips_4_5_r)
  1301. return SIGILL;
  1302. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1303. if (((ctx->fcr31 & cond) != 0) !=
  1304. ((MIPSInst_FT(ir) & 1) != 0))
  1305. return 0;
  1306. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1307. break;
  1308. case fmovz_op:
  1309. if (!cpu_has_mips_4_5_r)
  1310. return SIGILL;
  1311. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1312. return 0;
  1313. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1314. break;
  1315. case fmovn_op:
  1316. if (!cpu_has_mips_4_5_r)
  1317. return SIGILL;
  1318. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1319. return 0;
  1320. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1321. break;
  1322. case fabs_op:
  1323. handler.u = ieee754sp_abs;
  1324. goto scopuop;
  1325. case fneg_op:
  1326. handler.u = ieee754sp_neg;
  1327. goto scopuop;
  1328. case fmov_op:
  1329. /* an easy one */
  1330. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1331. goto copcsr;
  1332. /* binary op on handler */
  1333. scopbop:
  1334. SPFROMREG(fs, MIPSInst_FS(ir));
  1335. SPFROMREG(ft, MIPSInst_FT(ir));
  1336. rv.s = (*handler.b) (fs, ft);
  1337. goto copcsr;
  1338. scopuop:
  1339. SPFROMREG(fs, MIPSInst_FS(ir));
  1340. rv.s = (*handler.u) (fs);
  1341. goto copcsr;
  1342. copcsr:
  1343. if (ieee754_cxtest(IEEE754_INEXACT)) {
  1344. MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
  1345. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1346. }
  1347. if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
  1348. MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
  1349. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1350. }
  1351. if (ieee754_cxtest(IEEE754_OVERFLOW)) {
  1352. MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
  1353. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1354. }
  1355. if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
  1356. MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
  1357. rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
  1358. }
  1359. if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
  1360. MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
  1361. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1362. }
  1363. break;
  1364. /* unary conv ops */
  1365. case fcvts_op:
  1366. return SIGILL; /* not defined */
  1367. case fcvtd_op:
  1368. SPFROMREG(fs, MIPSInst_FS(ir));
  1369. rv.d = ieee754dp_fsp(fs);
  1370. rfmt = d_fmt;
  1371. goto copcsr;
  1372. case fcvtw_op:
  1373. SPFROMREG(fs, MIPSInst_FS(ir));
  1374. rv.w = ieee754sp_tint(fs);
  1375. rfmt = w_fmt;
  1376. goto copcsr;
  1377. case fround_op:
  1378. case ftrunc_op:
  1379. case fceil_op:
  1380. case ffloor_op:
  1381. if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
  1382. return SIGILL;
  1383. oldrm = ieee754_csr.rm;
  1384. SPFROMREG(fs, MIPSInst_FS(ir));
  1385. ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
  1386. rv.w = ieee754sp_tint(fs);
  1387. ieee754_csr.rm = oldrm;
  1388. rfmt = w_fmt;
  1389. goto copcsr;
  1390. case fcvtl_op:
  1391. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  1392. return SIGILL;
  1393. SPFROMREG(fs, MIPSInst_FS(ir));
  1394. rv.l = ieee754sp_tlong(fs);
  1395. rfmt = l_fmt;
  1396. goto copcsr;
  1397. case froundl_op:
  1398. case ftruncl_op:
  1399. case fceill_op:
  1400. case ffloorl_op:
  1401. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  1402. return SIGILL;
  1403. oldrm = ieee754_csr.rm;
  1404. SPFROMREG(fs, MIPSInst_FS(ir));
  1405. ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
  1406. rv.l = ieee754sp_tlong(fs);
  1407. ieee754_csr.rm = oldrm;
  1408. rfmt = l_fmt;
  1409. goto copcsr;
  1410. default:
  1411. if (MIPSInst_FUNC(ir) >= fcmp_op) {
  1412. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  1413. union ieee754sp fs, ft;
  1414. SPFROMREG(fs, MIPSInst_FS(ir));
  1415. SPFROMREG(ft, MIPSInst_FT(ir));
  1416. rv.w = ieee754sp_cmp(fs, ft,
  1417. cmptab[cmpop & 0x7], cmpop & 0x8);
  1418. rfmt = -1;
  1419. if ((cmpop & 0x8) && ieee754_cxtest
  1420. (IEEE754_INVALID_OPERATION))
  1421. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  1422. else
  1423. goto copcsr;
  1424. } else
  1425. return SIGILL;
  1426. break;
  1427. }
  1428. break;
  1429. }
  1430. case d_fmt: {
  1431. union ieee754dp fs, ft;
  1432. union {
  1433. union ieee754dp(*b) (union ieee754dp, union ieee754dp);
  1434. union ieee754dp(*u) (union ieee754dp);
  1435. } handler;
  1436. switch (MIPSInst_FUNC(ir)) {
  1437. /* binary ops */
  1438. case fadd_op:
  1439. handler.b = ieee754dp_add;
  1440. goto dcopbop;
  1441. case fsub_op:
  1442. handler.b = ieee754dp_sub;
  1443. goto dcopbop;
  1444. case fmul_op:
  1445. handler.b = ieee754dp_mul;
  1446. goto dcopbop;
  1447. case fdiv_op:
  1448. handler.b = ieee754dp_div;
  1449. goto dcopbop;
  1450. /* unary ops */
  1451. case fsqrt_op:
  1452. if (!cpu_has_mips_2_3_4_5_r)
  1453. return SIGILL;
  1454. handler.u = ieee754dp_sqrt;
  1455. goto dcopuop;
  1456. /*
  1457. * Note that on some MIPS IV implementations such as the
  1458. * R5000 and R8000 the FSQRT and FRECIP instructions do not
  1459. * achieve full IEEE-754 accuracy - however this emulator does.
  1460. */
  1461. case frsqrt_op:
  1462. if (!cpu_has_mips_4_5_r2)
  1463. return SIGILL;
  1464. handler.u = fpemu_dp_rsqrt;
  1465. goto dcopuop;
  1466. case frecip_op:
  1467. if (!cpu_has_mips_4_5_r2)
  1468. return SIGILL;
  1469. handler.u = fpemu_dp_recip;
  1470. goto dcopuop;
  1471. case fmovc_op:
  1472. if (!cpu_has_mips_4_5_r)
  1473. return SIGILL;
  1474. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1475. if (((ctx->fcr31 & cond) != 0) !=
  1476. ((MIPSInst_FT(ir) & 1) != 0))
  1477. return 0;
  1478. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1479. break;
  1480. case fmovz_op:
  1481. if (!cpu_has_mips_4_5_r)
  1482. return SIGILL;
  1483. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1484. return 0;
  1485. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1486. break;
  1487. case fmovn_op:
  1488. if (!cpu_has_mips_4_5_r)
  1489. return SIGILL;
  1490. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1491. return 0;
  1492. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1493. break;
  1494. case fabs_op:
  1495. handler.u = ieee754dp_abs;
  1496. goto dcopuop;
  1497. case fneg_op:
  1498. handler.u = ieee754dp_neg;
  1499. goto dcopuop;
  1500. case fmov_op:
  1501. /* an easy one */
  1502. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1503. goto copcsr;
  1504. /* binary op on handler */
  1505. dcopbop:
  1506. DPFROMREG(fs, MIPSInst_FS(ir));
  1507. DPFROMREG(ft, MIPSInst_FT(ir));
  1508. rv.d = (*handler.b) (fs, ft);
  1509. goto copcsr;
  1510. dcopuop:
  1511. DPFROMREG(fs, MIPSInst_FS(ir));
  1512. rv.d = (*handler.u) (fs);
  1513. goto copcsr;
  1514. /*
  1515. * unary conv ops
  1516. */
  1517. case fcvts_op:
  1518. DPFROMREG(fs, MIPSInst_FS(ir));
  1519. rv.s = ieee754sp_fdp(fs);
  1520. rfmt = s_fmt;
  1521. goto copcsr;
  1522. case fcvtd_op:
  1523. return SIGILL; /* not defined */
  1524. case fcvtw_op:
  1525. DPFROMREG(fs, MIPSInst_FS(ir));
  1526. rv.w = ieee754dp_tint(fs); /* wrong */
  1527. rfmt = w_fmt;
  1528. goto copcsr;
  1529. case fround_op:
  1530. case ftrunc_op:
  1531. case fceil_op:
  1532. case ffloor_op:
  1533. if (!cpu_has_mips_2_3_4_5_r)
  1534. return SIGILL;
  1535. oldrm = ieee754_csr.rm;
  1536. DPFROMREG(fs, MIPSInst_FS(ir));
  1537. ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
  1538. rv.w = ieee754dp_tint(fs);
  1539. ieee754_csr.rm = oldrm;
  1540. rfmt = w_fmt;
  1541. goto copcsr;
  1542. case fcvtl_op:
  1543. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  1544. return SIGILL;
  1545. DPFROMREG(fs, MIPSInst_FS(ir));
  1546. rv.l = ieee754dp_tlong(fs);
  1547. rfmt = l_fmt;
  1548. goto copcsr;
  1549. case froundl_op:
  1550. case ftruncl_op:
  1551. case fceill_op:
  1552. case ffloorl_op:
  1553. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  1554. return SIGILL;
  1555. oldrm = ieee754_csr.rm;
  1556. DPFROMREG(fs, MIPSInst_FS(ir));
  1557. ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
  1558. rv.l = ieee754dp_tlong(fs);
  1559. ieee754_csr.rm = oldrm;
  1560. rfmt = l_fmt;
  1561. goto copcsr;
  1562. default:
  1563. if (MIPSInst_FUNC(ir) >= fcmp_op) {
  1564. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  1565. union ieee754dp fs, ft;
  1566. DPFROMREG(fs, MIPSInst_FS(ir));
  1567. DPFROMREG(ft, MIPSInst_FT(ir));
  1568. rv.w = ieee754dp_cmp(fs, ft,
  1569. cmptab[cmpop & 0x7], cmpop & 0x8);
  1570. rfmt = -1;
  1571. if ((cmpop & 0x8)
  1572. &&
  1573. ieee754_cxtest
  1574. (IEEE754_INVALID_OPERATION))
  1575. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  1576. else
  1577. goto copcsr;
  1578. }
  1579. else {
  1580. return SIGILL;
  1581. }
  1582. break;
  1583. }
  1584. break;
  1585. case w_fmt:
  1586. switch (MIPSInst_FUNC(ir)) {
  1587. case fcvts_op:
  1588. /* convert word to single precision real */
  1589. SPFROMREG(fs, MIPSInst_FS(ir));
  1590. rv.s = ieee754sp_fint(fs.bits);
  1591. rfmt = s_fmt;
  1592. goto copcsr;
  1593. case fcvtd_op:
  1594. /* convert word to double precision real */
  1595. SPFROMREG(fs, MIPSInst_FS(ir));
  1596. rv.d = ieee754dp_fint(fs.bits);
  1597. rfmt = d_fmt;
  1598. goto copcsr;
  1599. default:
  1600. return SIGILL;
  1601. }
  1602. break;
  1603. }
  1604. case l_fmt:
  1605. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  1606. return SIGILL;
  1607. DIFROMREG(bits, MIPSInst_FS(ir));
  1608. switch (MIPSInst_FUNC(ir)) {
  1609. case fcvts_op:
  1610. /* convert long to single precision real */
  1611. rv.s = ieee754sp_flong(bits);
  1612. rfmt = s_fmt;
  1613. goto copcsr;
  1614. case fcvtd_op:
  1615. /* convert long to double precision real */
  1616. rv.d = ieee754dp_flong(bits);
  1617. rfmt = d_fmt;
  1618. goto copcsr;
  1619. default:
  1620. return SIGILL;
  1621. }
  1622. break;
  1623. default:
  1624. return SIGILL;
  1625. }
  1626. /*
  1627. * Update the fpu CSR register for this operation.
  1628. * If an exception is required, generate a tidy SIGFPE exception,
  1629. * without updating the result register.
  1630. * Note: cause exception bits do not accumulate, they are rewritten
  1631. * for each op; only the flag/sticky bits accumulate.
  1632. */
  1633. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  1634. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1635. /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
  1636. return SIGFPE;
  1637. }
  1638. /*
  1639. * Now we can safely write the result back to the register file.
  1640. */
  1641. switch (rfmt) {
  1642. case -1:
  1643. if (cpu_has_mips_4_5_r)
  1644. cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
  1645. else
  1646. cbit = FPU_CSR_COND;
  1647. if (rv.w)
  1648. ctx->fcr31 |= cbit;
  1649. else
  1650. ctx->fcr31 &= ~cbit;
  1651. break;
  1652. case d_fmt:
  1653. DPTOREG(rv.d, MIPSInst_FD(ir));
  1654. break;
  1655. case s_fmt:
  1656. SPTOREG(rv.s, MIPSInst_FD(ir));
  1657. break;
  1658. case w_fmt:
  1659. SITOREG(rv.w, MIPSInst_FD(ir));
  1660. break;
  1661. case l_fmt:
  1662. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  1663. return SIGILL;
  1664. DITOREG(rv.l, MIPSInst_FD(ir));
  1665. break;
  1666. default:
  1667. return SIGILL;
  1668. }
  1669. return 0;
  1670. }
  1671. int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1672. int has_fpu, void *__user *fault_addr)
  1673. {
  1674. unsigned long oldepc, prevepc;
  1675. struct mm_decoded_insn dec_insn;
  1676. u16 instr[4];
  1677. u16 *instr_ptr;
  1678. int sig = 0;
  1679. oldepc = xcp->cp0_epc;
  1680. do {
  1681. prevepc = xcp->cp0_epc;
  1682. if (get_isa16_mode(prevepc) && cpu_has_mmips) {
  1683. /*
  1684. * Get next 2 microMIPS instructions and convert them
  1685. * into 32-bit instructions.
  1686. */
  1687. if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
  1688. (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
  1689. (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
  1690. (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
  1691. MIPS_FPU_EMU_INC_STATS(errors);
  1692. return SIGBUS;
  1693. }
  1694. instr_ptr = instr;
  1695. /* Get first instruction. */
  1696. if (mm_insn_16bit(*instr_ptr)) {
  1697. /* Duplicate the half-word. */
  1698. dec_insn.insn = (*instr_ptr << 16) |
  1699. (*instr_ptr);
  1700. /* 16-bit instruction. */
  1701. dec_insn.pc_inc = 2;
  1702. instr_ptr += 1;
  1703. } else {
  1704. dec_insn.insn = (*instr_ptr << 16) |
  1705. *(instr_ptr+1);
  1706. /* 32-bit instruction. */
  1707. dec_insn.pc_inc = 4;
  1708. instr_ptr += 2;
  1709. }
  1710. /* Get second instruction. */
  1711. if (mm_insn_16bit(*instr_ptr)) {
  1712. /* Duplicate the half-word. */
  1713. dec_insn.next_insn = (*instr_ptr << 16) |
  1714. (*instr_ptr);
  1715. /* 16-bit instruction. */
  1716. dec_insn.next_pc_inc = 2;
  1717. } else {
  1718. dec_insn.next_insn = (*instr_ptr << 16) |
  1719. *(instr_ptr+1);
  1720. /* 32-bit instruction. */
  1721. dec_insn.next_pc_inc = 4;
  1722. }
  1723. dec_insn.micro_mips_mode = 1;
  1724. } else {
  1725. if ((get_user(dec_insn.insn,
  1726. (mips_instruction __user *) xcp->cp0_epc)) ||
  1727. (get_user(dec_insn.next_insn,
  1728. (mips_instruction __user *)(xcp->cp0_epc+4)))) {
  1729. MIPS_FPU_EMU_INC_STATS(errors);
  1730. return SIGBUS;
  1731. }
  1732. dec_insn.pc_inc = 4;
  1733. dec_insn.next_pc_inc = 4;
  1734. dec_insn.micro_mips_mode = 0;
  1735. }
  1736. if ((dec_insn.insn == 0) ||
  1737. ((dec_insn.pc_inc == 2) &&
  1738. ((dec_insn.insn & 0xffff) == MM_NOP16)))
  1739. xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
  1740. else {
  1741. /*
  1742. * The 'ieee754_csr' is an alias of
  1743. * ctx->fcr31. No need to copy ctx->fcr31 to
  1744. * ieee754_csr. But ieee754_csr.rm is ieee
  1745. * library modes. (not mips rounding mode)
  1746. */
  1747. sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
  1748. }
  1749. if (has_fpu)
  1750. break;
  1751. if (sig)
  1752. break;
  1753. cond_resched();
  1754. } while (xcp->cp0_epc > prevepc);
  1755. /* SIGILL indicates a non-fpu instruction */
  1756. if (sig == SIGILL && xcp->cp0_epc != oldepc)
  1757. /* but if EPC has advanced, then ignore it */
  1758. sig = 0;
  1759. return sig;
  1760. }