cp1emu.c 53 KB

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