cp1emu.c 55 KB

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