cp1emu.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929
  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/percpu-defs.h>
  39. #include <linux/perf_event.h>
  40. #include <asm/branch.h>
  41. #include <asm/inst.h>
  42. #include <asm/ptrace.h>
  43. #include <asm/signal.h>
  44. #include <linux/uaccess.h>
  45. #include <asm/cpu-info.h>
  46. #include <asm/processor.h>
  47. #include <asm/fpu_emulator.h>
  48. #include <asm/fpu.h>
  49. #include <asm/mips-r2-to-r6-emul.h>
  50. #include "ieee754.h"
  51. /* Function which emulates a floating point instruction. */
  52. static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
  53. mips_instruction);
  54. static int fpux_emu(struct pt_regs *,
  55. struct mips_fpu_struct *, mips_instruction, void __user **);
  56. /* Control registers */
  57. #define FPCREG_RID 0 /* $0 = revision id */
  58. #define FPCREG_FCCR 25 /* $25 = fccr */
  59. #define FPCREG_FEXR 26 /* $26 = fexr */
  60. #define FPCREG_FENR 28 /* $28 = fenr */
  61. #define FPCREG_CSR 31 /* $31 = csr */
  62. /* convert condition code register number to csr bit */
  63. const unsigned int fpucondbit[8] = {
  64. FPU_CSR_COND,
  65. FPU_CSR_COND1,
  66. FPU_CSR_COND2,
  67. FPU_CSR_COND3,
  68. FPU_CSR_COND4,
  69. FPU_CSR_COND5,
  70. FPU_CSR_COND6,
  71. FPU_CSR_COND7
  72. };
  73. /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
  74. static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
  75. static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
  76. static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
  77. static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
  78. /*
  79. * This functions translates a 32-bit microMIPS instruction
  80. * into a 32-bit MIPS32 instruction. Returns 0 on success
  81. * and SIGILL otherwise.
  82. */
  83. static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
  84. {
  85. union mips_instruction insn = *insn_ptr;
  86. union mips_instruction mips32_insn = insn;
  87. int func, fmt, op;
  88. switch (insn.mm_i_format.opcode) {
  89. case mm_ldc132_op:
  90. mips32_insn.mm_i_format.opcode = ldc1_op;
  91. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  92. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  93. break;
  94. case mm_lwc132_op:
  95. mips32_insn.mm_i_format.opcode = lwc1_op;
  96. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  97. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  98. break;
  99. case mm_sdc132_op:
  100. mips32_insn.mm_i_format.opcode = sdc1_op;
  101. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  102. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  103. break;
  104. case mm_swc132_op:
  105. mips32_insn.mm_i_format.opcode = swc1_op;
  106. mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
  107. mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
  108. break;
  109. case mm_pool32i_op:
  110. /* NOTE: offset is << by 1 if in microMIPS mode. */
  111. if ((insn.mm_i_format.rt == mm_bc1f_op) ||
  112. (insn.mm_i_format.rt == mm_bc1t_op)) {
  113. mips32_insn.fb_format.opcode = cop1_op;
  114. mips32_insn.fb_format.bc = bc_op;
  115. mips32_insn.fb_format.flag =
  116. (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
  117. } else
  118. return SIGILL;
  119. break;
  120. case mm_pool32f_op:
  121. switch (insn.mm_fp0_format.func) {
  122. case mm_32f_01_op:
  123. case mm_32f_11_op:
  124. case mm_32f_02_op:
  125. case mm_32f_12_op:
  126. case mm_32f_41_op:
  127. case mm_32f_51_op:
  128. case mm_32f_42_op:
  129. case mm_32f_52_op:
  130. op = insn.mm_fp0_format.func;
  131. if (op == mm_32f_01_op)
  132. func = madd_s_op;
  133. else if (op == mm_32f_11_op)
  134. func = madd_d_op;
  135. else if (op == mm_32f_02_op)
  136. func = nmadd_s_op;
  137. else if (op == mm_32f_12_op)
  138. func = nmadd_d_op;
  139. else if (op == mm_32f_41_op)
  140. func = msub_s_op;
  141. else if (op == mm_32f_51_op)
  142. func = msub_d_op;
  143. else if (op == mm_32f_42_op)
  144. func = nmsub_s_op;
  145. else
  146. func = nmsub_d_op;
  147. mips32_insn.fp6_format.opcode = cop1x_op;
  148. mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
  149. mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
  150. mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
  151. mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
  152. mips32_insn.fp6_format.func = func;
  153. break;
  154. case mm_32f_10_op:
  155. func = -1; /* Invalid */
  156. op = insn.mm_fp5_format.op & 0x7;
  157. if (op == mm_ldxc1_op)
  158. func = ldxc1_op;
  159. else if (op == mm_sdxc1_op)
  160. func = sdxc1_op;
  161. else if (op == mm_lwxc1_op)
  162. func = lwxc1_op;
  163. else if (op == mm_swxc1_op)
  164. func = swxc1_op;
  165. if (func != -1) {
  166. mips32_insn.r_format.opcode = cop1x_op;
  167. mips32_insn.r_format.rs =
  168. insn.mm_fp5_format.base;
  169. mips32_insn.r_format.rt =
  170. insn.mm_fp5_format.index;
  171. mips32_insn.r_format.rd = 0;
  172. mips32_insn.r_format.re = insn.mm_fp5_format.fd;
  173. mips32_insn.r_format.func = func;
  174. } else
  175. return SIGILL;
  176. break;
  177. case mm_32f_40_op:
  178. op = -1; /* Invalid */
  179. if (insn.mm_fp2_format.op == mm_fmovt_op)
  180. op = 1;
  181. else if (insn.mm_fp2_format.op == mm_fmovf_op)
  182. op = 0;
  183. if (op != -1) {
  184. mips32_insn.fp0_format.opcode = cop1_op;
  185. mips32_insn.fp0_format.fmt =
  186. sdps_format[insn.mm_fp2_format.fmt];
  187. mips32_insn.fp0_format.ft =
  188. (insn.mm_fp2_format.cc<<2) + op;
  189. mips32_insn.fp0_format.fs =
  190. insn.mm_fp2_format.fs;
  191. mips32_insn.fp0_format.fd =
  192. insn.mm_fp2_format.fd;
  193. mips32_insn.fp0_format.func = fmovc_op;
  194. } else
  195. return SIGILL;
  196. break;
  197. case mm_32f_60_op:
  198. func = -1; /* Invalid */
  199. if (insn.mm_fp0_format.op == mm_fadd_op)
  200. func = fadd_op;
  201. else if (insn.mm_fp0_format.op == mm_fsub_op)
  202. func = fsub_op;
  203. else if (insn.mm_fp0_format.op == mm_fmul_op)
  204. func = fmul_op;
  205. else if (insn.mm_fp0_format.op == mm_fdiv_op)
  206. func = fdiv_op;
  207. if (func != -1) {
  208. mips32_insn.fp0_format.opcode = cop1_op;
  209. mips32_insn.fp0_format.fmt =
  210. sdps_format[insn.mm_fp0_format.fmt];
  211. mips32_insn.fp0_format.ft =
  212. insn.mm_fp0_format.ft;
  213. mips32_insn.fp0_format.fs =
  214. insn.mm_fp0_format.fs;
  215. mips32_insn.fp0_format.fd =
  216. insn.mm_fp0_format.fd;
  217. mips32_insn.fp0_format.func = func;
  218. } else
  219. return SIGILL;
  220. break;
  221. case mm_32f_70_op:
  222. func = -1; /* Invalid */
  223. if (insn.mm_fp0_format.op == mm_fmovn_op)
  224. func = fmovn_op;
  225. else if (insn.mm_fp0_format.op == mm_fmovz_op)
  226. func = fmovz_op;
  227. if (func != -1) {
  228. mips32_insn.fp0_format.opcode = cop1_op;
  229. mips32_insn.fp0_format.fmt =
  230. sdps_format[insn.mm_fp0_format.fmt];
  231. mips32_insn.fp0_format.ft =
  232. insn.mm_fp0_format.ft;
  233. mips32_insn.fp0_format.fs =
  234. insn.mm_fp0_format.fs;
  235. mips32_insn.fp0_format.fd =
  236. insn.mm_fp0_format.fd;
  237. mips32_insn.fp0_format.func = func;
  238. } else
  239. return SIGILL;
  240. break;
  241. case mm_32f_73_op: /* POOL32FXF */
  242. switch (insn.mm_fp1_format.op) {
  243. case mm_movf0_op:
  244. case mm_movf1_op:
  245. case mm_movt0_op:
  246. case mm_movt1_op:
  247. if ((insn.mm_fp1_format.op & 0x7f) ==
  248. mm_movf0_op)
  249. op = 0;
  250. else
  251. op = 1;
  252. mips32_insn.r_format.opcode = spec_op;
  253. mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
  254. mips32_insn.r_format.rt =
  255. (insn.mm_fp4_format.cc << 2) + op;
  256. mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
  257. mips32_insn.r_format.re = 0;
  258. mips32_insn.r_format.func = movc_op;
  259. break;
  260. case mm_fcvtd0_op:
  261. case mm_fcvtd1_op:
  262. case mm_fcvts0_op:
  263. case mm_fcvts1_op:
  264. if ((insn.mm_fp1_format.op & 0x7f) ==
  265. mm_fcvtd0_op) {
  266. func = fcvtd_op;
  267. fmt = swl_format[insn.mm_fp3_format.fmt];
  268. } else {
  269. func = fcvts_op;
  270. fmt = dwl_format[insn.mm_fp3_format.fmt];
  271. }
  272. mips32_insn.fp0_format.opcode = cop1_op;
  273. mips32_insn.fp0_format.fmt = fmt;
  274. mips32_insn.fp0_format.ft = 0;
  275. mips32_insn.fp0_format.fs =
  276. insn.mm_fp3_format.fs;
  277. mips32_insn.fp0_format.fd =
  278. insn.mm_fp3_format.rt;
  279. mips32_insn.fp0_format.func = func;
  280. break;
  281. case mm_fmov0_op:
  282. case mm_fmov1_op:
  283. case mm_fabs0_op:
  284. case mm_fabs1_op:
  285. case mm_fneg0_op:
  286. case mm_fneg1_op:
  287. if ((insn.mm_fp1_format.op & 0x7f) ==
  288. mm_fmov0_op)
  289. func = fmov_op;
  290. else if ((insn.mm_fp1_format.op & 0x7f) ==
  291. mm_fabs0_op)
  292. func = fabs_op;
  293. else
  294. func = fneg_op;
  295. mips32_insn.fp0_format.opcode = cop1_op;
  296. mips32_insn.fp0_format.fmt =
  297. sdps_format[insn.mm_fp3_format.fmt];
  298. mips32_insn.fp0_format.ft = 0;
  299. mips32_insn.fp0_format.fs =
  300. insn.mm_fp3_format.fs;
  301. mips32_insn.fp0_format.fd =
  302. insn.mm_fp3_format.rt;
  303. mips32_insn.fp0_format.func = func;
  304. break;
  305. case mm_ffloorl_op:
  306. case mm_ffloorw_op:
  307. case mm_fceill_op:
  308. case mm_fceilw_op:
  309. case mm_ftruncl_op:
  310. case mm_ftruncw_op:
  311. case mm_froundl_op:
  312. case mm_froundw_op:
  313. case mm_fcvtl_op:
  314. case mm_fcvtw_op:
  315. if (insn.mm_fp1_format.op == mm_ffloorl_op)
  316. func = ffloorl_op;
  317. else if (insn.mm_fp1_format.op == mm_ffloorw_op)
  318. func = ffloor_op;
  319. else if (insn.mm_fp1_format.op == mm_fceill_op)
  320. func = fceill_op;
  321. else if (insn.mm_fp1_format.op == mm_fceilw_op)
  322. func = fceil_op;
  323. else if (insn.mm_fp1_format.op == mm_ftruncl_op)
  324. func = ftruncl_op;
  325. else if (insn.mm_fp1_format.op == mm_ftruncw_op)
  326. func = ftrunc_op;
  327. else if (insn.mm_fp1_format.op == mm_froundl_op)
  328. func = froundl_op;
  329. else if (insn.mm_fp1_format.op == mm_froundw_op)
  330. func = fround_op;
  331. else if (insn.mm_fp1_format.op == mm_fcvtl_op)
  332. func = fcvtl_op;
  333. else
  334. func = fcvtw_op;
  335. mips32_insn.fp0_format.opcode = cop1_op;
  336. mips32_insn.fp0_format.fmt =
  337. sd_format[insn.mm_fp1_format.fmt];
  338. mips32_insn.fp0_format.ft = 0;
  339. mips32_insn.fp0_format.fs =
  340. insn.mm_fp1_format.fs;
  341. mips32_insn.fp0_format.fd =
  342. insn.mm_fp1_format.rt;
  343. mips32_insn.fp0_format.func = func;
  344. break;
  345. case mm_frsqrt_op:
  346. case mm_fsqrt_op:
  347. case mm_frecip_op:
  348. if (insn.mm_fp1_format.op == mm_frsqrt_op)
  349. func = frsqrt_op;
  350. else if (insn.mm_fp1_format.op == mm_fsqrt_op)
  351. func = fsqrt_op;
  352. else
  353. func = frecip_op;
  354. mips32_insn.fp0_format.opcode = cop1_op;
  355. mips32_insn.fp0_format.fmt =
  356. sdps_format[insn.mm_fp1_format.fmt];
  357. mips32_insn.fp0_format.ft = 0;
  358. mips32_insn.fp0_format.fs =
  359. insn.mm_fp1_format.fs;
  360. mips32_insn.fp0_format.fd =
  361. insn.mm_fp1_format.rt;
  362. mips32_insn.fp0_format.func = func;
  363. break;
  364. case mm_mfc1_op:
  365. case mm_mtc1_op:
  366. case mm_cfc1_op:
  367. case mm_ctc1_op:
  368. case mm_mfhc1_op:
  369. case mm_mthc1_op:
  370. if (insn.mm_fp1_format.op == mm_mfc1_op)
  371. op = mfc_op;
  372. else if (insn.mm_fp1_format.op == mm_mtc1_op)
  373. op = mtc_op;
  374. else if (insn.mm_fp1_format.op == mm_cfc1_op)
  375. op = cfc_op;
  376. else if (insn.mm_fp1_format.op == mm_ctc1_op)
  377. op = ctc_op;
  378. else if (insn.mm_fp1_format.op == mm_mfhc1_op)
  379. op = mfhc_op;
  380. else
  381. op = mthc_op;
  382. mips32_insn.fp1_format.opcode = cop1_op;
  383. mips32_insn.fp1_format.op = op;
  384. mips32_insn.fp1_format.rt =
  385. insn.mm_fp1_format.rt;
  386. mips32_insn.fp1_format.fs =
  387. insn.mm_fp1_format.fs;
  388. mips32_insn.fp1_format.fd = 0;
  389. mips32_insn.fp1_format.func = 0;
  390. break;
  391. default:
  392. return SIGILL;
  393. }
  394. break;
  395. case mm_32f_74_op: /* c.cond.fmt */
  396. mips32_insn.fp0_format.opcode = cop1_op;
  397. mips32_insn.fp0_format.fmt =
  398. sdps_format[insn.mm_fp4_format.fmt];
  399. mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
  400. mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
  401. mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
  402. mips32_insn.fp0_format.func =
  403. insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
  404. break;
  405. default:
  406. return SIGILL;
  407. }
  408. break;
  409. default:
  410. return SIGILL;
  411. }
  412. *insn_ptr = mips32_insn;
  413. return 0;
  414. }
  415. /*
  416. * Redundant with logic already in kernel/branch.c,
  417. * embedded in compute_return_epc. At some point,
  418. * a single subroutine should be used across both
  419. * modules.
  420. */
  421. int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
  422. unsigned long *contpc)
  423. {
  424. union mips_instruction insn = (union mips_instruction)dec_insn.insn;
  425. unsigned int fcr31;
  426. unsigned int bit = 0;
  427. unsigned int bit0;
  428. union fpureg *fpr;
  429. switch (insn.i_format.opcode) {
  430. case spec_op:
  431. switch (insn.r_format.func) {
  432. case jalr_op:
  433. if (insn.r_format.rd != 0) {
  434. regs->regs[insn.r_format.rd] =
  435. regs->cp0_epc + dec_insn.pc_inc +
  436. dec_insn.next_pc_inc;
  437. }
  438. /* fall through */
  439. case jr_op:
  440. /* For R6, JR already emulated in jalr_op */
  441. if (NO_R6EMU && insn.r_format.func == jr_op)
  442. break;
  443. *contpc = regs->regs[insn.r_format.rs];
  444. return 1;
  445. }
  446. break;
  447. case bcond_op:
  448. switch (insn.i_format.rt) {
  449. case bltzal_op:
  450. case bltzall_op:
  451. if (NO_R6EMU && (insn.i_format.rs ||
  452. insn.i_format.rt == bltzall_op))
  453. break;
  454. regs->regs[31] = regs->cp0_epc +
  455. dec_insn.pc_inc +
  456. dec_insn.next_pc_inc;
  457. /* fall through */
  458. case bltzl_op:
  459. if (NO_R6EMU)
  460. break;
  461. /* fall through */
  462. case bltz_op:
  463. if ((long)regs->regs[insn.i_format.rs] < 0)
  464. *contpc = regs->cp0_epc +
  465. dec_insn.pc_inc +
  466. (insn.i_format.simmediate << 2);
  467. else
  468. *contpc = regs->cp0_epc +
  469. dec_insn.pc_inc +
  470. dec_insn.next_pc_inc;
  471. return 1;
  472. case bgezal_op:
  473. case bgezall_op:
  474. if (NO_R6EMU && (insn.i_format.rs ||
  475. insn.i_format.rt == bgezall_op))
  476. break;
  477. regs->regs[31] = regs->cp0_epc +
  478. dec_insn.pc_inc +
  479. dec_insn.next_pc_inc;
  480. /* fall through */
  481. case bgezl_op:
  482. if (NO_R6EMU)
  483. break;
  484. /* fall through */
  485. case bgez_op:
  486. if ((long)regs->regs[insn.i_format.rs] >= 0)
  487. *contpc = regs->cp0_epc +
  488. dec_insn.pc_inc +
  489. (insn.i_format.simmediate << 2);
  490. else
  491. *contpc = regs->cp0_epc +
  492. dec_insn.pc_inc +
  493. dec_insn.next_pc_inc;
  494. return 1;
  495. }
  496. break;
  497. case jalx_op:
  498. set_isa16_mode(bit);
  499. /* fall through */
  500. case jal_op:
  501. regs->regs[31] = regs->cp0_epc +
  502. dec_insn.pc_inc +
  503. dec_insn.next_pc_inc;
  504. /* fall through */
  505. case j_op:
  506. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  507. *contpc >>= 28;
  508. *contpc <<= 28;
  509. *contpc |= (insn.j_format.target << 2);
  510. /* Set microMIPS mode bit: XOR for jalx. */
  511. *contpc ^= bit;
  512. return 1;
  513. case beql_op:
  514. if (NO_R6EMU)
  515. break;
  516. /* fall through */
  517. case beq_op:
  518. if (regs->regs[insn.i_format.rs] ==
  519. regs->regs[insn.i_format.rt])
  520. *contpc = regs->cp0_epc +
  521. dec_insn.pc_inc +
  522. (insn.i_format.simmediate << 2);
  523. else
  524. *contpc = regs->cp0_epc +
  525. dec_insn.pc_inc +
  526. dec_insn.next_pc_inc;
  527. return 1;
  528. case bnel_op:
  529. if (NO_R6EMU)
  530. break;
  531. /* fall through */
  532. case bne_op:
  533. if (regs->regs[insn.i_format.rs] !=
  534. regs->regs[insn.i_format.rt])
  535. *contpc = regs->cp0_epc +
  536. dec_insn.pc_inc +
  537. (insn.i_format.simmediate << 2);
  538. else
  539. *contpc = regs->cp0_epc +
  540. dec_insn.pc_inc +
  541. dec_insn.next_pc_inc;
  542. return 1;
  543. case blezl_op:
  544. if (!insn.i_format.rt && NO_R6EMU)
  545. break;
  546. /* fall through */
  547. case blez_op:
  548. /*
  549. * Compact branches for R6 for the
  550. * blez and blezl opcodes.
  551. * BLEZ | rs = 0 | rt != 0 == BLEZALC
  552. * BLEZ | rs = rt != 0 == BGEZALC
  553. * BLEZ | rs != 0 | rt != 0 == BGEUC
  554. * BLEZL | rs = 0 | rt != 0 == BLEZC
  555. * BLEZL | rs = rt != 0 == BGEZC
  556. * BLEZL | rs != 0 | rt != 0 == BGEC
  557. *
  558. * For real BLEZ{,L}, rt is always 0.
  559. */
  560. if (cpu_has_mips_r6 && insn.i_format.rt) {
  561. if ((insn.i_format.opcode == blez_op) &&
  562. ((!insn.i_format.rs && insn.i_format.rt) ||
  563. (insn.i_format.rs == insn.i_format.rt)))
  564. regs->regs[31] = regs->cp0_epc +
  565. dec_insn.pc_inc;
  566. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  567. dec_insn.next_pc_inc;
  568. return 1;
  569. }
  570. if ((long)regs->regs[insn.i_format.rs] <= 0)
  571. *contpc = regs->cp0_epc +
  572. dec_insn.pc_inc +
  573. (insn.i_format.simmediate << 2);
  574. else
  575. *contpc = regs->cp0_epc +
  576. dec_insn.pc_inc +
  577. dec_insn.next_pc_inc;
  578. return 1;
  579. case bgtzl_op:
  580. if (!insn.i_format.rt && NO_R6EMU)
  581. break;
  582. /* fall through */
  583. case bgtz_op:
  584. /*
  585. * Compact branches for R6 for the
  586. * bgtz and bgtzl opcodes.
  587. * BGTZ | rs = 0 | rt != 0 == BGTZALC
  588. * BGTZ | rs = rt != 0 == BLTZALC
  589. * BGTZ | rs != 0 | rt != 0 == BLTUC
  590. * BGTZL | rs = 0 | rt != 0 == BGTZC
  591. * BGTZL | rs = rt != 0 == BLTZC
  592. * BGTZL | rs != 0 | rt != 0 == BLTC
  593. *
  594. * *ZALC varint for BGTZ &&& rt != 0
  595. * For real GTZ{,L}, rt is always 0.
  596. */
  597. if (cpu_has_mips_r6 && insn.i_format.rt) {
  598. if ((insn.i_format.opcode == blez_op) &&
  599. ((!insn.i_format.rs && insn.i_format.rt) ||
  600. (insn.i_format.rs == insn.i_format.rt)))
  601. regs->regs[31] = regs->cp0_epc +
  602. dec_insn.pc_inc;
  603. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  604. dec_insn.next_pc_inc;
  605. return 1;
  606. }
  607. if ((long)regs->regs[insn.i_format.rs] > 0)
  608. *contpc = regs->cp0_epc +
  609. dec_insn.pc_inc +
  610. (insn.i_format.simmediate << 2);
  611. else
  612. *contpc = regs->cp0_epc +
  613. dec_insn.pc_inc +
  614. dec_insn.next_pc_inc;
  615. return 1;
  616. case pop10_op:
  617. case pop30_op:
  618. if (!cpu_has_mips_r6)
  619. break;
  620. if (insn.i_format.rt && !insn.i_format.rs)
  621. regs->regs[31] = regs->cp0_epc + 4;
  622. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  623. dec_insn.next_pc_inc;
  624. return 1;
  625. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  626. case lwc2_op: /* This is bbit0 on Octeon */
  627. if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
  628. *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
  629. else
  630. *contpc = regs->cp0_epc + 8;
  631. return 1;
  632. case ldc2_op: /* This is bbit032 on Octeon */
  633. if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
  634. *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
  635. else
  636. *contpc = regs->cp0_epc + 8;
  637. return 1;
  638. case swc2_op: /* This is bbit1 on Octeon */
  639. if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
  640. *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
  641. else
  642. *contpc = regs->cp0_epc + 8;
  643. return 1;
  644. case sdc2_op: /* This is bbit132 on Octeon */
  645. if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
  646. *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
  647. else
  648. *contpc = regs->cp0_epc + 8;
  649. return 1;
  650. #else
  651. case bc6_op:
  652. /*
  653. * Only valid for MIPS R6 but we can still end up
  654. * here from a broken userland so just tell emulator
  655. * this is not a branch and let it break later on.
  656. */
  657. if (!cpu_has_mips_r6)
  658. break;
  659. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  660. dec_insn.next_pc_inc;
  661. return 1;
  662. case balc6_op:
  663. if (!cpu_has_mips_r6)
  664. break;
  665. regs->regs[31] = regs->cp0_epc + 4;
  666. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  667. dec_insn.next_pc_inc;
  668. return 1;
  669. case pop66_op:
  670. if (!cpu_has_mips_r6)
  671. break;
  672. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  673. dec_insn.next_pc_inc;
  674. return 1;
  675. case pop76_op:
  676. if (!cpu_has_mips_r6)
  677. break;
  678. if (!insn.i_format.rs)
  679. regs->regs[31] = regs->cp0_epc + 4;
  680. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  681. dec_insn.next_pc_inc;
  682. return 1;
  683. #endif
  684. case cop0_op:
  685. case cop1_op:
  686. /* Need to check for R6 bc1nez and bc1eqz branches */
  687. if (cpu_has_mips_r6 &&
  688. ((insn.i_format.rs == bc1eqz_op) ||
  689. (insn.i_format.rs == bc1nez_op))) {
  690. bit = 0;
  691. fpr = &current->thread.fpu.fpr[insn.i_format.rt];
  692. bit0 = get_fpr32(fpr, 0) & 0x1;
  693. switch (insn.i_format.rs) {
  694. case bc1eqz_op:
  695. bit = bit0 == 0;
  696. break;
  697. case bc1nez_op:
  698. bit = bit0 != 0;
  699. break;
  700. }
  701. if (bit)
  702. *contpc = regs->cp0_epc +
  703. dec_insn.pc_inc +
  704. (insn.i_format.simmediate << 2);
  705. else
  706. *contpc = regs->cp0_epc +
  707. dec_insn.pc_inc +
  708. dec_insn.next_pc_inc;
  709. return 1;
  710. }
  711. /* R2/R6 compatible cop1 instruction */
  712. /* fall through */
  713. case cop2_op:
  714. case cop1x_op:
  715. if (insn.i_format.rs == bc_op) {
  716. preempt_disable();
  717. if (is_fpu_owner())
  718. fcr31 = read_32bit_cp1_register(CP1_STATUS);
  719. else
  720. fcr31 = current->thread.fpu.fcr31;
  721. preempt_enable();
  722. bit = (insn.i_format.rt >> 2);
  723. bit += (bit != 0);
  724. bit += 23;
  725. switch (insn.i_format.rt & 3) {
  726. case 0: /* bc1f */
  727. case 2: /* bc1fl */
  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. case 1: /* bc1t */
  738. case 3: /* bc1tl */
  739. if (fcr31 & (1 << bit))
  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. }
  749. }
  750. break;
  751. }
  752. return 0;
  753. }
  754. /*
  755. * In the Linux kernel, we support selection of FPR format on the
  756. * basis of the Status.FR bit. If an FPU is not present, the FR bit
  757. * is hardwired to zero, which would imply a 32-bit FPU even for
  758. * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
  759. * FPU emu is slow and bulky and optimizing this function offers fairly
  760. * sizeable benefits so we try to be clever and make this function return
  761. * a constant whenever possible, that is on 64-bit kernels without O32
  762. * compatibility enabled and on 32-bit without 64-bit FPU support.
  763. */
  764. static inline int cop1_64bit(struct pt_regs *xcp)
  765. {
  766. if (IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_MIPS32_O32))
  767. return 1;
  768. else if (IS_ENABLED(CONFIG_32BIT) &&
  769. !IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
  770. return 0;
  771. return !test_thread_flag(TIF_32BIT_FPREGS);
  772. }
  773. static inline bool hybrid_fprs(void)
  774. {
  775. return test_thread_flag(TIF_HYBRID_FPREGS);
  776. }
  777. #define SIFROMREG(si, x) \
  778. do { \
  779. if (cop1_64bit(xcp) && !hybrid_fprs()) \
  780. (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
  781. else \
  782. (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
  783. } while (0)
  784. #define SITOREG(si, x) \
  785. do { \
  786. if (cop1_64bit(xcp) && !hybrid_fprs()) { \
  787. unsigned int i; \
  788. set_fpr32(&ctx->fpr[x], 0, si); \
  789. for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
  790. set_fpr32(&ctx->fpr[x], i, 0); \
  791. } else { \
  792. set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
  793. } \
  794. } while (0)
  795. #define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
  796. #define SITOHREG(si, x) \
  797. do { \
  798. unsigned int i; \
  799. set_fpr32(&ctx->fpr[x], 1, si); \
  800. for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
  801. set_fpr32(&ctx->fpr[x], i, 0); \
  802. } while (0)
  803. #define DIFROMREG(di, x) \
  804. ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) ^ 1)], 0))
  805. #define DITOREG(di, x) \
  806. do { \
  807. unsigned int fpr, i; \
  808. fpr = (x) & ~(cop1_64bit(xcp) ^ 1); \
  809. set_fpr64(&ctx->fpr[fpr], 0, di); \
  810. for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
  811. set_fpr64(&ctx->fpr[fpr], i, 0); \
  812. } while (0)
  813. #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
  814. #define SPTOREG(sp, x) SITOREG((sp).bits, x)
  815. #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
  816. #define DPTOREG(dp, x) DITOREG((dp).bits, x)
  817. /*
  818. * Emulate a CFC1 instruction.
  819. */
  820. static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  821. mips_instruction ir)
  822. {
  823. u32 fcr31 = ctx->fcr31;
  824. u32 value = 0;
  825. switch (MIPSInst_RD(ir)) {
  826. case FPCREG_CSR:
  827. value = fcr31;
  828. pr_debug("%p gpr[%d]<-csr=%08x\n",
  829. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  830. break;
  831. case FPCREG_FENR:
  832. if (!cpu_has_mips_r)
  833. break;
  834. value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
  835. MIPS_FENR_FS;
  836. value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
  837. pr_debug("%p gpr[%d]<-enr=%08x\n",
  838. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  839. break;
  840. case FPCREG_FEXR:
  841. if (!cpu_has_mips_r)
  842. break;
  843. value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
  844. pr_debug("%p gpr[%d]<-exr=%08x\n",
  845. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  846. break;
  847. case FPCREG_FCCR:
  848. if (!cpu_has_mips_r)
  849. break;
  850. value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
  851. MIPS_FCCR_COND0;
  852. value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
  853. (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
  854. pr_debug("%p gpr[%d]<-ccr=%08x\n",
  855. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  856. break;
  857. case FPCREG_RID:
  858. value = boot_cpu_data.fpu_id;
  859. break;
  860. default:
  861. break;
  862. }
  863. if (MIPSInst_RT(ir))
  864. xcp->regs[MIPSInst_RT(ir)] = value;
  865. }
  866. /*
  867. * Emulate a CTC1 instruction.
  868. */
  869. static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  870. mips_instruction ir)
  871. {
  872. u32 fcr31 = ctx->fcr31;
  873. u32 value;
  874. u32 mask;
  875. if (MIPSInst_RT(ir) == 0)
  876. value = 0;
  877. else
  878. value = xcp->regs[MIPSInst_RT(ir)];
  879. switch (MIPSInst_RD(ir)) {
  880. case FPCREG_CSR:
  881. pr_debug("%p gpr[%d]->csr=%08x\n",
  882. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  883. /* Preserve read-only bits. */
  884. mask = boot_cpu_data.fpu_msk31;
  885. fcr31 = (value & ~mask) | (fcr31 & mask);
  886. break;
  887. case FPCREG_FENR:
  888. if (!cpu_has_mips_r)
  889. break;
  890. pr_debug("%p gpr[%d]->enr=%08x\n",
  891. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  892. fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
  893. fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
  894. FPU_CSR_FS;
  895. fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
  896. break;
  897. case FPCREG_FEXR:
  898. if (!cpu_has_mips_r)
  899. break;
  900. pr_debug("%p gpr[%d]->exr=%08x\n",
  901. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  902. fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
  903. fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
  904. break;
  905. case FPCREG_FCCR:
  906. if (!cpu_has_mips_r)
  907. break;
  908. pr_debug("%p gpr[%d]->ccr=%08x\n",
  909. (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
  910. fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
  911. fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
  912. FPU_CSR_COND;
  913. fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
  914. FPU_CSR_CONDX;
  915. break;
  916. default:
  917. break;
  918. }
  919. ctx->fcr31 = fcr31;
  920. }
  921. /*
  922. * Emulate the single floating point instruction pointed at by EPC.
  923. * Two instructions if the instruction is in a branch delay slot.
  924. */
  925. static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  926. struct mm_decoded_insn dec_insn, void __user **fault_addr)
  927. {
  928. unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
  929. unsigned int cond, cbit, bit0;
  930. mips_instruction ir;
  931. int likely, pc_inc;
  932. union fpureg *fpr;
  933. u32 __user *wva;
  934. u64 __user *dva;
  935. u32 wval;
  936. u64 dval;
  937. int sig;
  938. /*
  939. * These are giving gcc a gentle hint about what to expect in
  940. * dec_inst in order to do better optimization.
  941. */
  942. if (!cpu_has_mmips && dec_insn.micro_mips_mode)
  943. unreachable();
  944. /* XXX NEC Vr54xx bug workaround */
  945. if (delay_slot(xcp)) {
  946. if (dec_insn.micro_mips_mode) {
  947. if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
  948. clear_delay_slot(xcp);
  949. } else {
  950. if (!isBranchInstr(xcp, dec_insn, &contpc))
  951. clear_delay_slot(xcp);
  952. }
  953. }
  954. if (delay_slot(xcp)) {
  955. /*
  956. * The instruction to be emulated is in a branch delay slot
  957. * which means that we have to emulate the branch instruction
  958. * BEFORE we do the cop1 instruction.
  959. *
  960. * This branch could be a COP1 branch, but in that case we
  961. * would have had a trap for that instruction, and would not
  962. * come through this route.
  963. *
  964. * Linux MIPS branch emulator operates on context, updating the
  965. * cp0_epc.
  966. */
  967. ir = dec_insn.next_insn; /* process delay slot instr */
  968. pc_inc = dec_insn.next_pc_inc;
  969. } else {
  970. ir = dec_insn.insn; /* process current instr */
  971. pc_inc = dec_insn.pc_inc;
  972. }
  973. /*
  974. * Since microMIPS FPU instructios are a subset of MIPS32 FPU
  975. * instructions, we want to convert microMIPS FPU instructions
  976. * into MIPS32 instructions so that we could reuse all of the
  977. * FPU emulation code.
  978. *
  979. * NOTE: We cannot do this for branch instructions since they
  980. * are not a subset. Example: Cannot emulate a 16-bit
  981. * aligned target address with a MIPS32 instruction.
  982. */
  983. if (dec_insn.micro_mips_mode) {
  984. /*
  985. * If next instruction is a 16-bit instruction, then it
  986. * it cannot be a FPU instruction. This could happen
  987. * since we can be called for non-FPU instructions.
  988. */
  989. if ((pc_inc == 2) ||
  990. (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
  991. == SIGILL))
  992. return SIGILL;
  993. }
  994. emul:
  995. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
  996. MIPS_FPU_EMU_INC_STATS(emulated);
  997. switch (MIPSInst_OPCODE(ir)) {
  998. case ldc1_op:
  999. dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  1000. MIPSInst_SIMM(ir));
  1001. MIPS_FPU_EMU_INC_STATS(loads);
  1002. if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
  1003. MIPS_FPU_EMU_INC_STATS(errors);
  1004. *fault_addr = dva;
  1005. return SIGBUS;
  1006. }
  1007. if (__get_user(dval, dva)) {
  1008. MIPS_FPU_EMU_INC_STATS(errors);
  1009. *fault_addr = dva;
  1010. return SIGSEGV;
  1011. }
  1012. DITOREG(dval, MIPSInst_RT(ir));
  1013. break;
  1014. case sdc1_op:
  1015. dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  1016. MIPSInst_SIMM(ir));
  1017. MIPS_FPU_EMU_INC_STATS(stores);
  1018. DIFROMREG(dval, MIPSInst_RT(ir));
  1019. if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
  1020. MIPS_FPU_EMU_INC_STATS(errors);
  1021. *fault_addr = dva;
  1022. return SIGBUS;
  1023. }
  1024. if (__put_user(dval, dva)) {
  1025. MIPS_FPU_EMU_INC_STATS(errors);
  1026. *fault_addr = dva;
  1027. return SIGSEGV;
  1028. }
  1029. break;
  1030. case lwc1_op:
  1031. wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  1032. MIPSInst_SIMM(ir));
  1033. MIPS_FPU_EMU_INC_STATS(loads);
  1034. if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
  1035. MIPS_FPU_EMU_INC_STATS(errors);
  1036. *fault_addr = wva;
  1037. return SIGBUS;
  1038. }
  1039. if (__get_user(wval, wva)) {
  1040. MIPS_FPU_EMU_INC_STATS(errors);
  1041. *fault_addr = wva;
  1042. return SIGSEGV;
  1043. }
  1044. SITOREG(wval, MIPSInst_RT(ir));
  1045. break;
  1046. case swc1_op:
  1047. wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  1048. MIPSInst_SIMM(ir));
  1049. MIPS_FPU_EMU_INC_STATS(stores);
  1050. SIFROMREG(wval, MIPSInst_RT(ir));
  1051. if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
  1052. MIPS_FPU_EMU_INC_STATS(errors);
  1053. *fault_addr = wva;
  1054. return SIGBUS;
  1055. }
  1056. if (__put_user(wval, wva)) {
  1057. MIPS_FPU_EMU_INC_STATS(errors);
  1058. *fault_addr = wva;
  1059. return SIGSEGV;
  1060. }
  1061. break;
  1062. case cop1_op:
  1063. switch (MIPSInst_RS(ir)) {
  1064. case dmfc_op:
  1065. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  1066. return SIGILL;
  1067. /* copregister fs -> gpr[rt] */
  1068. if (MIPSInst_RT(ir) != 0) {
  1069. DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  1070. MIPSInst_RD(ir));
  1071. }
  1072. break;
  1073. case dmtc_op:
  1074. if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
  1075. return SIGILL;
  1076. /* copregister fs <- rt */
  1077. DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1078. break;
  1079. case mfhc_op:
  1080. if (!cpu_has_mips_r2_r6)
  1081. return SIGILL;
  1082. /* copregister rd -> gpr[rt] */
  1083. if (MIPSInst_RT(ir) != 0) {
  1084. SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
  1085. MIPSInst_RD(ir));
  1086. }
  1087. break;
  1088. case mthc_op:
  1089. if (!cpu_has_mips_r2_r6)
  1090. return SIGILL;
  1091. /* copregister rd <- gpr[rt] */
  1092. SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1093. break;
  1094. case mfc_op:
  1095. /* copregister rd -> gpr[rt] */
  1096. if (MIPSInst_RT(ir) != 0) {
  1097. SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  1098. MIPSInst_RD(ir));
  1099. }
  1100. break;
  1101. case mtc_op:
  1102. /* copregister rd <- rt */
  1103. SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1104. break;
  1105. case cfc_op:
  1106. /* cop control register rd -> gpr[rt] */
  1107. cop1_cfc(xcp, ctx, ir);
  1108. break;
  1109. case ctc_op:
  1110. /* copregister rd <- rt */
  1111. cop1_ctc(xcp, ctx, ir);
  1112. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1113. return SIGFPE;
  1114. }
  1115. break;
  1116. case bc1eqz_op:
  1117. case bc1nez_op:
  1118. if (!cpu_has_mips_r6 || delay_slot(xcp))
  1119. return SIGILL;
  1120. likely = 0;
  1121. cond = 0;
  1122. fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
  1123. bit0 = get_fpr32(fpr, 0) & 0x1;
  1124. switch (MIPSInst_RS(ir)) {
  1125. case bc1eqz_op:
  1126. MIPS_FPU_EMU_INC_STATS(bc1eqz);
  1127. cond = bit0 == 0;
  1128. break;
  1129. case bc1nez_op:
  1130. MIPS_FPU_EMU_INC_STATS(bc1nez);
  1131. cond = bit0 != 0;
  1132. break;
  1133. }
  1134. goto branch_common;
  1135. case bc_op:
  1136. if (delay_slot(xcp))
  1137. return SIGILL;
  1138. if (cpu_has_mips_4_5_r)
  1139. cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
  1140. else
  1141. cbit = FPU_CSR_COND;
  1142. cond = ctx->fcr31 & cbit;
  1143. likely = 0;
  1144. switch (MIPSInst_RT(ir) & 3) {
  1145. case bcfl_op:
  1146. if (cpu_has_mips_2_3_4_5_r)
  1147. likely = 1;
  1148. /* fall through */
  1149. case bcf_op:
  1150. cond = !cond;
  1151. break;
  1152. case bctl_op:
  1153. if (cpu_has_mips_2_3_4_5_r)
  1154. likely = 1;
  1155. /* fall through */
  1156. case bct_op:
  1157. break;
  1158. }
  1159. branch_common:
  1160. MIPS_FPU_EMU_INC_STATS(branches);
  1161. set_delay_slot(xcp);
  1162. if (cond) {
  1163. /*
  1164. * Branch taken: emulate dslot instruction
  1165. */
  1166. unsigned long bcpc;
  1167. /*
  1168. * Remember EPC at the branch to point back
  1169. * at so that any delay-slot instruction
  1170. * signal is not silently ignored.
  1171. */
  1172. bcpc = xcp->cp0_epc;
  1173. xcp->cp0_epc += dec_insn.pc_inc;
  1174. contpc = MIPSInst_SIMM(ir);
  1175. ir = dec_insn.next_insn;
  1176. if (dec_insn.micro_mips_mode) {
  1177. contpc = (xcp->cp0_epc + (contpc << 1));
  1178. /* If 16-bit instruction, not FPU. */
  1179. if ((dec_insn.next_pc_inc == 2) ||
  1180. (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
  1181. /*
  1182. * Since this instruction will
  1183. * be put on the stack with
  1184. * 32-bit words, get around
  1185. * this problem by putting a
  1186. * NOP16 as the second one.
  1187. */
  1188. if (dec_insn.next_pc_inc == 2)
  1189. ir = (ir & (~0xffff)) | MM_NOP16;
  1190. /*
  1191. * Single step the non-CP1
  1192. * instruction in the dslot.
  1193. */
  1194. sig = mips_dsemul(xcp, ir,
  1195. bcpc, contpc);
  1196. if (sig < 0)
  1197. break;
  1198. if (sig)
  1199. xcp->cp0_epc = bcpc;
  1200. /*
  1201. * SIGILL forces out of
  1202. * the emulation loop.
  1203. */
  1204. return sig ? sig : SIGILL;
  1205. }
  1206. } else
  1207. contpc = (xcp->cp0_epc + (contpc << 2));
  1208. switch (MIPSInst_OPCODE(ir)) {
  1209. case lwc1_op:
  1210. case swc1_op:
  1211. goto emul;
  1212. case ldc1_op:
  1213. case sdc1_op:
  1214. if (cpu_has_mips_2_3_4_5_r)
  1215. goto emul;
  1216. goto bc_sigill;
  1217. case cop1_op:
  1218. goto emul;
  1219. case cop1x_op:
  1220. if (cpu_has_mips_4_5_64_r2_r6)
  1221. /* its one of ours */
  1222. goto emul;
  1223. goto bc_sigill;
  1224. case spec_op:
  1225. switch (MIPSInst_FUNC(ir)) {
  1226. case movc_op:
  1227. if (cpu_has_mips_4_5_r)
  1228. goto emul;
  1229. goto bc_sigill;
  1230. }
  1231. break;
  1232. bc_sigill:
  1233. xcp->cp0_epc = bcpc;
  1234. return SIGILL;
  1235. }
  1236. /*
  1237. * Single step the non-cp1
  1238. * instruction in the dslot
  1239. */
  1240. sig = mips_dsemul(xcp, ir, bcpc, contpc);
  1241. if (sig < 0)
  1242. break;
  1243. if (sig)
  1244. xcp->cp0_epc = bcpc;
  1245. /* SIGILL forces out of the emulation loop. */
  1246. return sig ? sig : SIGILL;
  1247. } else if (likely) { /* branch not taken */
  1248. /*
  1249. * branch likely nullifies
  1250. * dslot if not taken
  1251. */
  1252. xcp->cp0_epc += dec_insn.pc_inc;
  1253. contpc += dec_insn.pc_inc;
  1254. /*
  1255. * else continue & execute
  1256. * dslot as normal insn
  1257. */
  1258. }
  1259. break;
  1260. default:
  1261. if (!(MIPSInst_RS(ir) & 0x10))
  1262. return SIGILL;
  1263. /* a real fpu computation instruction */
  1264. sig = fpu_emu(xcp, ctx, ir);
  1265. if (sig)
  1266. return sig;
  1267. }
  1268. break;
  1269. case cop1x_op:
  1270. if (!cpu_has_mips_4_5_64_r2_r6)
  1271. return SIGILL;
  1272. sig = fpux_emu(xcp, ctx, ir, fault_addr);
  1273. if (sig)
  1274. return sig;
  1275. break;
  1276. case spec_op:
  1277. if (!cpu_has_mips_4_5_r)
  1278. return SIGILL;
  1279. if (MIPSInst_FUNC(ir) != movc_op)
  1280. return SIGILL;
  1281. cond = fpucondbit[MIPSInst_RT(ir) >> 2];
  1282. if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
  1283. xcp->regs[MIPSInst_RD(ir)] =
  1284. xcp->regs[MIPSInst_RS(ir)];
  1285. break;
  1286. default:
  1287. return SIGILL;
  1288. }
  1289. /* we did it !! */
  1290. xcp->cp0_epc = contpc;
  1291. clear_delay_slot(xcp);
  1292. return 0;
  1293. }
  1294. /*
  1295. * Conversion table from MIPS compare ops 48-63
  1296. * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
  1297. */
  1298. static const unsigned char cmptab[8] = {
  1299. 0, /* cmp_0 (sig) cmp_sf */
  1300. IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
  1301. IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
  1302. IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
  1303. IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
  1304. IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
  1305. IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
  1306. IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
  1307. };
  1308. static const unsigned char negative_cmptab[8] = {
  1309. 0, /* Reserved */
  1310. IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
  1311. IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
  1312. IEEE754_CLT | IEEE754_CGT,
  1313. /* Reserved */
  1314. };
  1315. /*
  1316. * Additional MIPS4 instructions
  1317. */
  1318. #define DEF3OP(name, p, f1, f2, f3) \
  1319. static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
  1320. union ieee754##p s, union ieee754##p t) \
  1321. { \
  1322. struct _ieee754_csr ieee754_csr_save; \
  1323. s = f1(s, t); \
  1324. ieee754_csr_save = ieee754_csr; \
  1325. s = f2(s, r); \
  1326. ieee754_csr_save.cx |= ieee754_csr.cx; \
  1327. ieee754_csr_save.sx |= ieee754_csr.sx; \
  1328. s = f3(s); \
  1329. ieee754_csr.cx |= ieee754_csr_save.cx; \
  1330. ieee754_csr.sx |= ieee754_csr_save.sx; \
  1331. return s; \
  1332. }
  1333. static union ieee754dp fpemu_dp_recip(union ieee754dp d)
  1334. {
  1335. return ieee754dp_div(ieee754dp_one(0), d);
  1336. }
  1337. static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
  1338. {
  1339. return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
  1340. }
  1341. static union ieee754sp fpemu_sp_recip(union ieee754sp s)
  1342. {
  1343. return ieee754sp_div(ieee754sp_one(0), s);
  1344. }
  1345. static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
  1346. {
  1347. return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
  1348. }
  1349. DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
  1350. DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
  1351. DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
  1352. DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
  1353. DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
  1354. DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
  1355. DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
  1356. DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
  1357. static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1358. mips_instruction ir, void __user **fault_addr)
  1359. {
  1360. unsigned int rcsr = 0; /* resulting csr */
  1361. MIPS_FPU_EMU_INC_STATS(cp1xops);
  1362. switch (MIPSInst_FMA_FFMT(ir)) {
  1363. case s_fmt:{ /* 0 */
  1364. union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
  1365. union ieee754sp fd, fr, fs, ft;
  1366. u32 __user *va;
  1367. u32 val;
  1368. switch (MIPSInst_FUNC(ir)) {
  1369. case lwxc1_op:
  1370. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1371. xcp->regs[MIPSInst_FT(ir)]);
  1372. MIPS_FPU_EMU_INC_STATS(loads);
  1373. if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
  1374. MIPS_FPU_EMU_INC_STATS(errors);
  1375. *fault_addr = va;
  1376. return SIGBUS;
  1377. }
  1378. if (__get_user(val, va)) {
  1379. MIPS_FPU_EMU_INC_STATS(errors);
  1380. *fault_addr = va;
  1381. return SIGSEGV;
  1382. }
  1383. SITOREG(val, MIPSInst_FD(ir));
  1384. break;
  1385. case swxc1_op:
  1386. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1387. xcp->regs[MIPSInst_FT(ir)]);
  1388. MIPS_FPU_EMU_INC_STATS(stores);
  1389. SIFROMREG(val, MIPSInst_FS(ir));
  1390. if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
  1391. MIPS_FPU_EMU_INC_STATS(errors);
  1392. *fault_addr = va;
  1393. return SIGBUS;
  1394. }
  1395. if (put_user(val, va)) {
  1396. MIPS_FPU_EMU_INC_STATS(errors);
  1397. *fault_addr = va;
  1398. return SIGSEGV;
  1399. }
  1400. break;
  1401. case madd_s_op:
  1402. handler = fpemu_sp_madd;
  1403. goto scoptop;
  1404. case msub_s_op:
  1405. handler = fpemu_sp_msub;
  1406. goto scoptop;
  1407. case nmadd_s_op:
  1408. handler = fpemu_sp_nmadd;
  1409. goto scoptop;
  1410. case nmsub_s_op:
  1411. handler = fpemu_sp_nmsub;
  1412. goto scoptop;
  1413. scoptop:
  1414. SPFROMREG(fr, MIPSInst_FR(ir));
  1415. SPFROMREG(fs, MIPSInst_FS(ir));
  1416. SPFROMREG(ft, MIPSInst_FT(ir));
  1417. fd = (*handler) (fr, fs, ft);
  1418. SPTOREG(fd, MIPSInst_FD(ir));
  1419. copcsr:
  1420. if (ieee754_cxtest(IEEE754_INEXACT)) {
  1421. MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
  1422. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1423. }
  1424. if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
  1425. MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
  1426. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1427. }
  1428. if (ieee754_cxtest(IEEE754_OVERFLOW)) {
  1429. MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
  1430. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1431. }
  1432. if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
  1433. MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
  1434. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1435. }
  1436. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  1437. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1438. /*printk ("SIGFPE: FPU csr = %08x\n",
  1439. ctx->fcr31); */
  1440. return SIGFPE;
  1441. }
  1442. break;
  1443. default:
  1444. return SIGILL;
  1445. }
  1446. break;
  1447. }
  1448. case d_fmt:{ /* 1 */
  1449. union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
  1450. union ieee754dp fd, fr, fs, ft;
  1451. u64 __user *va;
  1452. u64 val;
  1453. switch (MIPSInst_FUNC(ir)) {
  1454. case ldxc1_op:
  1455. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1456. xcp->regs[MIPSInst_FT(ir)]);
  1457. MIPS_FPU_EMU_INC_STATS(loads);
  1458. if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
  1459. MIPS_FPU_EMU_INC_STATS(errors);
  1460. *fault_addr = va;
  1461. return SIGBUS;
  1462. }
  1463. if (__get_user(val, va)) {
  1464. MIPS_FPU_EMU_INC_STATS(errors);
  1465. *fault_addr = va;
  1466. return SIGSEGV;
  1467. }
  1468. DITOREG(val, MIPSInst_FD(ir));
  1469. break;
  1470. case sdxc1_op:
  1471. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1472. xcp->regs[MIPSInst_FT(ir)]);
  1473. MIPS_FPU_EMU_INC_STATS(stores);
  1474. DIFROMREG(val, MIPSInst_FS(ir));
  1475. if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
  1476. MIPS_FPU_EMU_INC_STATS(errors);
  1477. *fault_addr = va;
  1478. return SIGBUS;
  1479. }
  1480. if (__put_user(val, va)) {
  1481. MIPS_FPU_EMU_INC_STATS(errors);
  1482. *fault_addr = va;
  1483. return SIGSEGV;
  1484. }
  1485. break;
  1486. case madd_d_op:
  1487. handler = fpemu_dp_madd;
  1488. goto dcoptop;
  1489. case msub_d_op:
  1490. handler = fpemu_dp_msub;
  1491. goto dcoptop;
  1492. case nmadd_d_op:
  1493. handler = fpemu_dp_nmadd;
  1494. goto dcoptop;
  1495. case nmsub_d_op:
  1496. handler = fpemu_dp_nmsub;
  1497. goto dcoptop;
  1498. dcoptop:
  1499. DPFROMREG(fr, MIPSInst_FR(ir));
  1500. DPFROMREG(fs, MIPSInst_FS(ir));
  1501. DPFROMREG(ft, MIPSInst_FT(ir));
  1502. fd = (*handler) (fr, fs, ft);
  1503. DPTOREG(fd, MIPSInst_FD(ir));
  1504. goto copcsr;
  1505. default:
  1506. return SIGILL;
  1507. }
  1508. break;
  1509. }
  1510. case 0x3:
  1511. if (MIPSInst_FUNC(ir) != pfetch_op)
  1512. return SIGILL;
  1513. /* ignore prefx operation */
  1514. break;
  1515. default:
  1516. return SIGILL;
  1517. }
  1518. return 0;
  1519. }
  1520. /*
  1521. * Emulate a single COP1 arithmetic instruction.
  1522. */
  1523. static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1524. mips_instruction ir)
  1525. {
  1526. int rfmt; /* resulting format */
  1527. unsigned int rcsr = 0; /* resulting csr */
  1528. unsigned int oldrm;
  1529. unsigned int cbit;
  1530. unsigned int cond;
  1531. union {
  1532. union ieee754dp d;
  1533. union ieee754sp s;
  1534. int w;
  1535. s64 l;
  1536. } rv; /* resulting value */
  1537. u64 bits;
  1538. MIPS_FPU_EMU_INC_STATS(cp1ops);
  1539. switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
  1540. case s_fmt: { /* 0 */
  1541. union {
  1542. union ieee754sp(*b) (union ieee754sp, union ieee754sp);
  1543. union ieee754sp(*u) (union ieee754sp);
  1544. } handler;
  1545. union ieee754sp fd, fs, ft;
  1546. switch (MIPSInst_FUNC(ir)) {
  1547. /* binary ops */
  1548. case fadd_op:
  1549. MIPS_FPU_EMU_INC_STATS(add_s);
  1550. handler.b = ieee754sp_add;
  1551. goto scopbop;
  1552. case fsub_op:
  1553. MIPS_FPU_EMU_INC_STATS(sub_s);
  1554. handler.b = ieee754sp_sub;
  1555. goto scopbop;
  1556. case fmul_op:
  1557. MIPS_FPU_EMU_INC_STATS(mul_s);
  1558. handler.b = ieee754sp_mul;
  1559. goto scopbop;
  1560. case fdiv_op:
  1561. MIPS_FPU_EMU_INC_STATS(div_s);
  1562. handler.b = ieee754sp_div;
  1563. goto scopbop;
  1564. /* unary ops */
  1565. case fsqrt_op:
  1566. if (!cpu_has_mips_2_3_4_5_r)
  1567. return SIGILL;
  1568. MIPS_FPU_EMU_INC_STATS(sqrt_s);
  1569. handler.u = ieee754sp_sqrt;
  1570. goto scopuop;
  1571. /*
  1572. * Note that on some MIPS IV implementations such as the
  1573. * R5000 and R8000 the FSQRT and FRECIP instructions do not
  1574. * achieve full IEEE-754 accuracy - however this emulator does.
  1575. */
  1576. case frsqrt_op:
  1577. if (!cpu_has_mips_4_5_64_r2_r6)
  1578. return SIGILL;
  1579. MIPS_FPU_EMU_INC_STATS(rsqrt_s);
  1580. handler.u = fpemu_sp_rsqrt;
  1581. goto scopuop;
  1582. case frecip_op:
  1583. if (!cpu_has_mips_4_5_64_r2_r6)
  1584. return SIGILL;
  1585. MIPS_FPU_EMU_INC_STATS(recip_s);
  1586. handler.u = fpemu_sp_recip;
  1587. goto scopuop;
  1588. case fmovc_op:
  1589. if (!cpu_has_mips_4_5_r)
  1590. return SIGILL;
  1591. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1592. if (((ctx->fcr31 & cond) != 0) !=
  1593. ((MIPSInst_FT(ir) & 1) != 0))
  1594. return 0;
  1595. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1596. break;
  1597. case fmovz_op:
  1598. if (!cpu_has_mips_4_5_r)
  1599. return SIGILL;
  1600. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1601. return 0;
  1602. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1603. break;
  1604. case fmovn_op:
  1605. if (!cpu_has_mips_4_5_r)
  1606. return SIGILL;
  1607. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1608. return 0;
  1609. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1610. break;
  1611. case fseleqz_op:
  1612. if (!cpu_has_mips_r6)
  1613. return SIGILL;
  1614. MIPS_FPU_EMU_INC_STATS(seleqz_s);
  1615. SPFROMREG(rv.s, MIPSInst_FT(ir));
  1616. if (rv.w & 0x1)
  1617. rv.w = 0;
  1618. else
  1619. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1620. break;
  1621. case fselnez_op:
  1622. if (!cpu_has_mips_r6)
  1623. return SIGILL;
  1624. MIPS_FPU_EMU_INC_STATS(selnez_s);
  1625. SPFROMREG(rv.s, MIPSInst_FT(ir));
  1626. if (rv.w & 0x1)
  1627. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1628. else
  1629. rv.w = 0;
  1630. break;
  1631. case fmaddf_op: {
  1632. union ieee754sp ft, fs, fd;
  1633. if (!cpu_has_mips_r6)
  1634. return SIGILL;
  1635. MIPS_FPU_EMU_INC_STATS(maddf_s);
  1636. SPFROMREG(ft, MIPSInst_FT(ir));
  1637. SPFROMREG(fs, MIPSInst_FS(ir));
  1638. SPFROMREG(fd, MIPSInst_FD(ir));
  1639. rv.s = ieee754sp_maddf(fd, fs, ft);
  1640. goto copcsr;
  1641. }
  1642. case fmsubf_op: {
  1643. union ieee754sp ft, fs, fd;
  1644. if (!cpu_has_mips_r6)
  1645. return SIGILL;
  1646. MIPS_FPU_EMU_INC_STATS(msubf_s);
  1647. SPFROMREG(ft, MIPSInst_FT(ir));
  1648. SPFROMREG(fs, MIPSInst_FS(ir));
  1649. SPFROMREG(fd, MIPSInst_FD(ir));
  1650. rv.s = ieee754sp_msubf(fd, fs, ft);
  1651. goto copcsr;
  1652. }
  1653. case frint_op: {
  1654. union ieee754sp fs;
  1655. if (!cpu_has_mips_r6)
  1656. return SIGILL;
  1657. MIPS_FPU_EMU_INC_STATS(rint_s);
  1658. SPFROMREG(fs, MIPSInst_FS(ir));
  1659. rv.s = ieee754sp_rint(fs);
  1660. goto copcsr;
  1661. }
  1662. case fclass_op: {
  1663. union ieee754sp fs;
  1664. if (!cpu_has_mips_r6)
  1665. return SIGILL;
  1666. MIPS_FPU_EMU_INC_STATS(class_s);
  1667. SPFROMREG(fs, MIPSInst_FS(ir));
  1668. rv.w = ieee754sp_2008class(fs);
  1669. rfmt = w_fmt;
  1670. goto copcsr;
  1671. }
  1672. case fmin_op: {
  1673. union ieee754sp fs, ft;
  1674. if (!cpu_has_mips_r6)
  1675. return SIGILL;
  1676. MIPS_FPU_EMU_INC_STATS(min_s);
  1677. SPFROMREG(ft, MIPSInst_FT(ir));
  1678. SPFROMREG(fs, MIPSInst_FS(ir));
  1679. rv.s = ieee754sp_fmin(fs, ft);
  1680. goto copcsr;
  1681. }
  1682. case fmina_op: {
  1683. union ieee754sp fs, ft;
  1684. if (!cpu_has_mips_r6)
  1685. return SIGILL;
  1686. MIPS_FPU_EMU_INC_STATS(mina_s);
  1687. SPFROMREG(ft, MIPSInst_FT(ir));
  1688. SPFROMREG(fs, MIPSInst_FS(ir));
  1689. rv.s = ieee754sp_fmina(fs, ft);
  1690. goto copcsr;
  1691. }
  1692. case fmax_op: {
  1693. union ieee754sp fs, ft;
  1694. if (!cpu_has_mips_r6)
  1695. return SIGILL;
  1696. MIPS_FPU_EMU_INC_STATS(max_s);
  1697. SPFROMREG(ft, MIPSInst_FT(ir));
  1698. SPFROMREG(fs, MIPSInst_FS(ir));
  1699. rv.s = ieee754sp_fmax(fs, ft);
  1700. goto copcsr;
  1701. }
  1702. case fmaxa_op: {
  1703. union ieee754sp fs, ft;
  1704. if (!cpu_has_mips_r6)
  1705. return SIGILL;
  1706. MIPS_FPU_EMU_INC_STATS(maxa_s);
  1707. SPFROMREG(ft, MIPSInst_FT(ir));
  1708. SPFROMREG(fs, MIPSInst_FS(ir));
  1709. rv.s = ieee754sp_fmaxa(fs, ft);
  1710. goto copcsr;
  1711. }
  1712. case fabs_op:
  1713. MIPS_FPU_EMU_INC_STATS(abs_s);
  1714. handler.u = ieee754sp_abs;
  1715. goto scopuop;
  1716. case fneg_op:
  1717. MIPS_FPU_EMU_INC_STATS(neg_s);
  1718. handler.u = ieee754sp_neg;
  1719. goto scopuop;
  1720. case fmov_op:
  1721. /* an easy one */
  1722. MIPS_FPU_EMU_INC_STATS(mov_s);
  1723. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1724. goto copcsr;
  1725. /* binary op on handler */
  1726. scopbop:
  1727. SPFROMREG(fs, MIPSInst_FS(ir));
  1728. SPFROMREG(ft, MIPSInst_FT(ir));
  1729. rv.s = (*handler.b) (fs, ft);
  1730. goto copcsr;
  1731. scopuop:
  1732. SPFROMREG(fs, MIPSInst_FS(ir));
  1733. rv.s = (*handler.u) (fs);
  1734. goto copcsr;
  1735. copcsr:
  1736. if (ieee754_cxtest(IEEE754_INEXACT)) {
  1737. MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
  1738. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1739. }
  1740. if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
  1741. MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
  1742. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1743. }
  1744. if (ieee754_cxtest(IEEE754_OVERFLOW)) {
  1745. MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
  1746. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1747. }
  1748. if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
  1749. MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
  1750. rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
  1751. }
  1752. if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
  1753. MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
  1754. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1755. }
  1756. break;
  1757. /* unary conv ops */
  1758. case fcvts_op:
  1759. return SIGILL; /* not defined */
  1760. case fcvtd_op:
  1761. MIPS_FPU_EMU_INC_STATS(cvt_d_s);
  1762. SPFROMREG(fs, MIPSInst_FS(ir));
  1763. rv.d = ieee754dp_fsp(fs);
  1764. rfmt = d_fmt;
  1765. goto copcsr;
  1766. case fcvtw_op:
  1767. MIPS_FPU_EMU_INC_STATS(cvt_w_s);
  1768. SPFROMREG(fs, MIPSInst_FS(ir));
  1769. rv.w = ieee754sp_tint(fs);
  1770. rfmt = w_fmt;
  1771. goto copcsr;
  1772. case fround_op:
  1773. case ftrunc_op:
  1774. case fceil_op:
  1775. case ffloor_op:
  1776. if (!cpu_has_mips_2_3_4_5_r)
  1777. return SIGILL;
  1778. if (MIPSInst_FUNC(ir) == fceil_op)
  1779. MIPS_FPU_EMU_INC_STATS(ceil_w_s);
  1780. if (MIPSInst_FUNC(ir) == ffloor_op)
  1781. MIPS_FPU_EMU_INC_STATS(floor_w_s);
  1782. if (MIPSInst_FUNC(ir) == fround_op)
  1783. MIPS_FPU_EMU_INC_STATS(round_w_s);
  1784. if (MIPSInst_FUNC(ir) == ftrunc_op)
  1785. MIPS_FPU_EMU_INC_STATS(trunc_w_s);
  1786. oldrm = ieee754_csr.rm;
  1787. SPFROMREG(fs, MIPSInst_FS(ir));
  1788. ieee754_csr.rm = MIPSInst_FUNC(ir);
  1789. rv.w = ieee754sp_tint(fs);
  1790. ieee754_csr.rm = oldrm;
  1791. rfmt = w_fmt;
  1792. goto copcsr;
  1793. case fsel_op:
  1794. if (!cpu_has_mips_r6)
  1795. return SIGILL;
  1796. MIPS_FPU_EMU_INC_STATS(sel_s);
  1797. SPFROMREG(fd, MIPSInst_FD(ir));
  1798. if (fd.bits & 0x1)
  1799. SPFROMREG(rv.s, MIPSInst_FT(ir));
  1800. else
  1801. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1802. break;
  1803. case fcvtl_op:
  1804. if (!cpu_has_mips_3_4_5_64_r2_r6)
  1805. return SIGILL;
  1806. MIPS_FPU_EMU_INC_STATS(cvt_l_s);
  1807. SPFROMREG(fs, MIPSInst_FS(ir));
  1808. rv.l = ieee754sp_tlong(fs);
  1809. rfmt = l_fmt;
  1810. goto copcsr;
  1811. case froundl_op:
  1812. case ftruncl_op:
  1813. case fceill_op:
  1814. case ffloorl_op:
  1815. if (!cpu_has_mips_3_4_5_64_r2_r6)
  1816. return SIGILL;
  1817. if (MIPSInst_FUNC(ir) == fceill_op)
  1818. MIPS_FPU_EMU_INC_STATS(ceil_l_s);
  1819. if (MIPSInst_FUNC(ir) == ffloorl_op)
  1820. MIPS_FPU_EMU_INC_STATS(floor_l_s);
  1821. if (MIPSInst_FUNC(ir) == froundl_op)
  1822. MIPS_FPU_EMU_INC_STATS(round_l_s);
  1823. if (MIPSInst_FUNC(ir) == ftruncl_op)
  1824. MIPS_FPU_EMU_INC_STATS(trunc_l_s);
  1825. oldrm = ieee754_csr.rm;
  1826. SPFROMREG(fs, MIPSInst_FS(ir));
  1827. ieee754_csr.rm = MIPSInst_FUNC(ir);
  1828. rv.l = ieee754sp_tlong(fs);
  1829. ieee754_csr.rm = oldrm;
  1830. rfmt = l_fmt;
  1831. goto copcsr;
  1832. default:
  1833. if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
  1834. unsigned int cmpop;
  1835. union ieee754sp fs, ft;
  1836. cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  1837. SPFROMREG(fs, MIPSInst_FS(ir));
  1838. SPFROMREG(ft, MIPSInst_FT(ir));
  1839. rv.w = ieee754sp_cmp(fs, ft,
  1840. cmptab[cmpop & 0x7], cmpop & 0x8);
  1841. rfmt = -1;
  1842. if ((cmpop & 0x8) && ieee754_cxtest
  1843. (IEEE754_INVALID_OPERATION))
  1844. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  1845. else
  1846. goto copcsr;
  1847. } else
  1848. return SIGILL;
  1849. break;
  1850. }
  1851. break;
  1852. }
  1853. case d_fmt: {
  1854. union ieee754dp fd, fs, ft;
  1855. union {
  1856. union ieee754dp(*b) (union ieee754dp, union ieee754dp);
  1857. union ieee754dp(*u) (union ieee754dp);
  1858. } handler;
  1859. switch (MIPSInst_FUNC(ir)) {
  1860. /* binary ops */
  1861. case fadd_op:
  1862. MIPS_FPU_EMU_INC_STATS(add_d);
  1863. handler.b = ieee754dp_add;
  1864. goto dcopbop;
  1865. case fsub_op:
  1866. MIPS_FPU_EMU_INC_STATS(sub_d);
  1867. handler.b = ieee754dp_sub;
  1868. goto dcopbop;
  1869. case fmul_op:
  1870. MIPS_FPU_EMU_INC_STATS(mul_d);
  1871. handler.b = ieee754dp_mul;
  1872. goto dcopbop;
  1873. case fdiv_op:
  1874. MIPS_FPU_EMU_INC_STATS(div_d);
  1875. handler.b = ieee754dp_div;
  1876. goto dcopbop;
  1877. /* unary ops */
  1878. case fsqrt_op:
  1879. if (!cpu_has_mips_2_3_4_5_r)
  1880. return SIGILL;
  1881. MIPS_FPU_EMU_INC_STATS(sqrt_d);
  1882. handler.u = ieee754dp_sqrt;
  1883. goto dcopuop;
  1884. /*
  1885. * Note that on some MIPS IV implementations such as the
  1886. * R5000 and R8000 the FSQRT and FRECIP instructions do not
  1887. * achieve full IEEE-754 accuracy - however this emulator does.
  1888. */
  1889. case frsqrt_op:
  1890. if (!cpu_has_mips_4_5_64_r2_r6)
  1891. return SIGILL;
  1892. MIPS_FPU_EMU_INC_STATS(rsqrt_d);
  1893. handler.u = fpemu_dp_rsqrt;
  1894. goto dcopuop;
  1895. case frecip_op:
  1896. if (!cpu_has_mips_4_5_64_r2_r6)
  1897. return SIGILL;
  1898. MIPS_FPU_EMU_INC_STATS(recip_d);
  1899. handler.u = fpemu_dp_recip;
  1900. goto dcopuop;
  1901. case fmovc_op:
  1902. if (!cpu_has_mips_4_5_r)
  1903. return SIGILL;
  1904. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1905. if (((ctx->fcr31 & cond) != 0) !=
  1906. ((MIPSInst_FT(ir) & 1) != 0))
  1907. return 0;
  1908. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1909. break;
  1910. case fmovz_op:
  1911. if (!cpu_has_mips_4_5_r)
  1912. return SIGILL;
  1913. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1914. return 0;
  1915. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1916. break;
  1917. case fmovn_op:
  1918. if (!cpu_has_mips_4_5_r)
  1919. return SIGILL;
  1920. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1921. return 0;
  1922. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1923. break;
  1924. case fseleqz_op:
  1925. if (!cpu_has_mips_r6)
  1926. return SIGILL;
  1927. MIPS_FPU_EMU_INC_STATS(seleqz_d);
  1928. DPFROMREG(rv.d, MIPSInst_FT(ir));
  1929. if (rv.l & 0x1)
  1930. rv.l = 0;
  1931. else
  1932. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1933. break;
  1934. case fselnez_op:
  1935. if (!cpu_has_mips_r6)
  1936. return SIGILL;
  1937. MIPS_FPU_EMU_INC_STATS(selnez_d);
  1938. DPFROMREG(rv.d, MIPSInst_FT(ir));
  1939. if (rv.l & 0x1)
  1940. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1941. else
  1942. rv.l = 0;
  1943. break;
  1944. case fmaddf_op: {
  1945. union ieee754dp ft, fs, fd;
  1946. if (!cpu_has_mips_r6)
  1947. return SIGILL;
  1948. MIPS_FPU_EMU_INC_STATS(maddf_d);
  1949. DPFROMREG(ft, MIPSInst_FT(ir));
  1950. DPFROMREG(fs, MIPSInst_FS(ir));
  1951. DPFROMREG(fd, MIPSInst_FD(ir));
  1952. rv.d = ieee754dp_maddf(fd, fs, ft);
  1953. goto copcsr;
  1954. }
  1955. case fmsubf_op: {
  1956. union ieee754dp ft, fs, fd;
  1957. if (!cpu_has_mips_r6)
  1958. return SIGILL;
  1959. MIPS_FPU_EMU_INC_STATS(msubf_d);
  1960. DPFROMREG(ft, MIPSInst_FT(ir));
  1961. DPFROMREG(fs, MIPSInst_FS(ir));
  1962. DPFROMREG(fd, MIPSInst_FD(ir));
  1963. rv.d = ieee754dp_msubf(fd, fs, ft);
  1964. goto copcsr;
  1965. }
  1966. case frint_op: {
  1967. union ieee754dp fs;
  1968. if (!cpu_has_mips_r6)
  1969. return SIGILL;
  1970. MIPS_FPU_EMU_INC_STATS(rint_d);
  1971. DPFROMREG(fs, MIPSInst_FS(ir));
  1972. rv.d = ieee754dp_rint(fs);
  1973. goto copcsr;
  1974. }
  1975. case fclass_op: {
  1976. union ieee754dp fs;
  1977. if (!cpu_has_mips_r6)
  1978. return SIGILL;
  1979. MIPS_FPU_EMU_INC_STATS(class_d);
  1980. DPFROMREG(fs, MIPSInst_FS(ir));
  1981. rv.l = ieee754dp_2008class(fs);
  1982. rfmt = l_fmt;
  1983. goto copcsr;
  1984. }
  1985. case fmin_op: {
  1986. union ieee754dp fs, ft;
  1987. if (!cpu_has_mips_r6)
  1988. return SIGILL;
  1989. MIPS_FPU_EMU_INC_STATS(min_d);
  1990. DPFROMREG(ft, MIPSInst_FT(ir));
  1991. DPFROMREG(fs, MIPSInst_FS(ir));
  1992. rv.d = ieee754dp_fmin(fs, ft);
  1993. goto copcsr;
  1994. }
  1995. case fmina_op: {
  1996. union ieee754dp fs, ft;
  1997. if (!cpu_has_mips_r6)
  1998. return SIGILL;
  1999. MIPS_FPU_EMU_INC_STATS(mina_d);
  2000. DPFROMREG(ft, MIPSInst_FT(ir));
  2001. DPFROMREG(fs, MIPSInst_FS(ir));
  2002. rv.d = ieee754dp_fmina(fs, ft);
  2003. goto copcsr;
  2004. }
  2005. case fmax_op: {
  2006. union ieee754dp fs, ft;
  2007. if (!cpu_has_mips_r6)
  2008. return SIGILL;
  2009. MIPS_FPU_EMU_INC_STATS(max_d);
  2010. DPFROMREG(ft, MIPSInst_FT(ir));
  2011. DPFROMREG(fs, MIPSInst_FS(ir));
  2012. rv.d = ieee754dp_fmax(fs, ft);
  2013. goto copcsr;
  2014. }
  2015. case fmaxa_op: {
  2016. union ieee754dp fs, ft;
  2017. if (!cpu_has_mips_r6)
  2018. return SIGILL;
  2019. MIPS_FPU_EMU_INC_STATS(maxa_d);
  2020. DPFROMREG(ft, MIPSInst_FT(ir));
  2021. DPFROMREG(fs, MIPSInst_FS(ir));
  2022. rv.d = ieee754dp_fmaxa(fs, ft);
  2023. goto copcsr;
  2024. }
  2025. case fabs_op:
  2026. MIPS_FPU_EMU_INC_STATS(abs_d);
  2027. handler.u = ieee754dp_abs;
  2028. goto dcopuop;
  2029. case fneg_op:
  2030. MIPS_FPU_EMU_INC_STATS(neg_d);
  2031. handler.u = ieee754dp_neg;
  2032. goto dcopuop;
  2033. case fmov_op:
  2034. /* an easy one */
  2035. MIPS_FPU_EMU_INC_STATS(mov_d);
  2036. DPFROMREG(rv.d, MIPSInst_FS(ir));
  2037. goto copcsr;
  2038. /* binary op on handler */
  2039. dcopbop:
  2040. DPFROMREG(fs, MIPSInst_FS(ir));
  2041. DPFROMREG(ft, MIPSInst_FT(ir));
  2042. rv.d = (*handler.b) (fs, ft);
  2043. goto copcsr;
  2044. dcopuop:
  2045. DPFROMREG(fs, MIPSInst_FS(ir));
  2046. rv.d = (*handler.u) (fs);
  2047. goto copcsr;
  2048. /*
  2049. * unary conv ops
  2050. */
  2051. case fcvts_op:
  2052. MIPS_FPU_EMU_INC_STATS(cvt_s_d);
  2053. DPFROMREG(fs, MIPSInst_FS(ir));
  2054. rv.s = ieee754sp_fdp(fs);
  2055. rfmt = s_fmt;
  2056. goto copcsr;
  2057. case fcvtd_op:
  2058. return SIGILL; /* not defined */
  2059. case fcvtw_op:
  2060. MIPS_FPU_EMU_INC_STATS(cvt_w_d);
  2061. DPFROMREG(fs, MIPSInst_FS(ir));
  2062. rv.w = ieee754dp_tint(fs); /* wrong */
  2063. rfmt = w_fmt;
  2064. goto copcsr;
  2065. case fround_op:
  2066. case ftrunc_op:
  2067. case fceil_op:
  2068. case ffloor_op:
  2069. if (!cpu_has_mips_2_3_4_5_r)
  2070. return SIGILL;
  2071. if (MIPSInst_FUNC(ir) == fceil_op)
  2072. MIPS_FPU_EMU_INC_STATS(ceil_w_d);
  2073. if (MIPSInst_FUNC(ir) == ffloor_op)
  2074. MIPS_FPU_EMU_INC_STATS(floor_w_d);
  2075. if (MIPSInst_FUNC(ir) == fround_op)
  2076. MIPS_FPU_EMU_INC_STATS(round_w_d);
  2077. if (MIPSInst_FUNC(ir) == ftrunc_op)
  2078. MIPS_FPU_EMU_INC_STATS(trunc_w_d);
  2079. oldrm = ieee754_csr.rm;
  2080. DPFROMREG(fs, MIPSInst_FS(ir));
  2081. ieee754_csr.rm = MIPSInst_FUNC(ir);
  2082. rv.w = ieee754dp_tint(fs);
  2083. ieee754_csr.rm = oldrm;
  2084. rfmt = w_fmt;
  2085. goto copcsr;
  2086. case fsel_op:
  2087. if (!cpu_has_mips_r6)
  2088. return SIGILL;
  2089. MIPS_FPU_EMU_INC_STATS(sel_d);
  2090. DPFROMREG(fd, MIPSInst_FD(ir));
  2091. if (fd.bits & 0x1)
  2092. DPFROMREG(rv.d, MIPSInst_FT(ir));
  2093. else
  2094. DPFROMREG(rv.d, MIPSInst_FS(ir));
  2095. break;
  2096. case fcvtl_op:
  2097. if (!cpu_has_mips_3_4_5_64_r2_r6)
  2098. return SIGILL;
  2099. MIPS_FPU_EMU_INC_STATS(cvt_l_d);
  2100. DPFROMREG(fs, MIPSInst_FS(ir));
  2101. rv.l = ieee754dp_tlong(fs);
  2102. rfmt = l_fmt;
  2103. goto copcsr;
  2104. case froundl_op:
  2105. case ftruncl_op:
  2106. case fceill_op:
  2107. case ffloorl_op:
  2108. if (!cpu_has_mips_3_4_5_64_r2_r6)
  2109. return SIGILL;
  2110. if (MIPSInst_FUNC(ir) == fceill_op)
  2111. MIPS_FPU_EMU_INC_STATS(ceil_l_d);
  2112. if (MIPSInst_FUNC(ir) == ffloorl_op)
  2113. MIPS_FPU_EMU_INC_STATS(floor_l_d);
  2114. if (MIPSInst_FUNC(ir) == froundl_op)
  2115. MIPS_FPU_EMU_INC_STATS(round_l_d);
  2116. if (MIPSInst_FUNC(ir) == ftruncl_op)
  2117. MIPS_FPU_EMU_INC_STATS(trunc_l_d);
  2118. oldrm = ieee754_csr.rm;
  2119. DPFROMREG(fs, MIPSInst_FS(ir));
  2120. ieee754_csr.rm = MIPSInst_FUNC(ir);
  2121. rv.l = ieee754dp_tlong(fs);
  2122. ieee754_csr.rm = oldrm;
  2123. rfmt = l_fmt;
  2124. goto copcsr;
  2125. default:
  2126. if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
  2127. unsigned int cmpop;
  2128. union ieee754dp fs, ft;
  2129. cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  2130. DPFROMREG(fs, MIPSInst_FS(ir));
  2131. DPFROMREG(ft, MIPSInst_FT(ir));
  2132. rv.w = ieee754dp_cmp(fs, ft,
  2133. cmptab[cmpop & 0x7], cmpop & 0x8);
  2134. rfmt = -1;
  2135. if ((cmpop & 0x8)
  2136. &&
  2137. ieee754_cxtest
  2138. (IEEE754_INVALID_OPERATION))
  2139. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  2140. else
  2141. goto copcsr;
  2142. }
  2143. else {
  2144. return SIGILL;
  2145. }
  2146. break;
  2147. }
  2148. break;
  2149. }
  2150. case w_fmt: {
  2151. union ieee754dp fs;
  2152. switch (MIPSInst_FUNC(ir)) {
  2153. case fcvts_op:
  2154. /* convert word to single precision real */
  2155. MIPS_FPU_EMU_INC_STATS(cvt_s_w);
  2156. SPFROMREG(fs, MIPSInst_FS(ir));
  2157. rv.s = ieee754sp_fint(fs.bits);
  2158. rfmt = s_fmt;
  2159. goto copcsr;
  2160. case fcvtd_op:
  2161. /* convert word to double precision real */
  2162. MIPS_FPU_EMU_INC_STATS(cvt_d_w);
  2163. SPFROMREG(fs, MIPSInst_FS(ir));
  2164. rv.d = ieee754dp_fint(fs.bits);
  2165. rfmt = d_fmt;
  2166. goto copcsr;
  2167. default: {
  2168. /* Emulating the new CMP.condn.fmt R6 instruction */
  2169. #define CMPOP_MASK 0x7
  2170. #define SIGN_BIT (0x1 << 3)
  2171. #define PREDICATE_BIT (0x1 << 4)
  2172. int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
  2173. int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
  2174. union ieee754sp fs, ft;
  2175. /* This is an R6 only instruction */
  2176. if (!cpu_has_mips_r6 ||
  2177. (MIPSInst_FUNC(ir) & 0x20))
  2178. return SIGILL;
  2179. if (!sig) {
  2180. if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
  2181. switch (cmpop) {
  2182. case 0:
  2183. MIPS_FPU_EMU_INC_STATS(cmp_af_s);
  2184. break;
  2185. case 1:
  2186. MIPS_FPU_EMU_INC_STATS(cmp_un_s);
  2187. break;
  2188. case 2:
  2189. MIPS_FPU_EMU_INC_STATS(cmp_eq_s);
  2190. break;
  2191. case 3:
  2192. MIPS_FPU_EMU_INC_STATS(cmp_ueq_s);
  2193. break;
  2194. case 4:
  2195. MIPS_FPU_EMU_INC_STATS(cmp_lt_s);
  2196. break;
  2197. case 5:
  2198. MIPS_FPU_EMU_INC_STATS(cmp_ult_s);
  2199. break;
  2200. case 6:
  2201. MIPS_FPU_EMU_INC_STATS(cmp_le_s);
  2202. break;
  2203. case 7:
  2204. MIPS_FPU_EMU_INC_STATS(cmp_ule_s);
  2205. break;
  2206. }
  2207. } else {
  2208. switch (cmpop) {
  2209. case 1:
  2210. MIPS_FPU_EMU_INC_STATS(cmp_or_s);
  2211. break;
  2212. case 2:
  2213. MIPS_FPU_EMU_INC_STATS(cmp_une_s);
  2214. break;
  2215. case 3:
  2216. MIPS_FPU_EMU_INC_STATS(cmp_ne_s);
  2217. break;
  2218. }
  2219. }
  2220. } else {
  2221. if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
  2222. switch (cmpop) {
  2223. case 0:
  2224. MIPS_FPU_EMU_INC_STATS(cmp_saf_s);
  2225. break;
  2226. case 1:
  2227. MIPS_FPU_EMU_INC_STATS(cmp_sun_s);
  2228. break;
  2229. case 2:
  2230. MIPS_FPU_EMU_INC_STATS(cmp_seq_s);
  2231. break;
  2232. case 3:
  2233. MIPS_FPU_EMU_INC_STATS(cmp_sueq_s);
  2234. break;
  2235. case 4:
  2236. MIPS_FPU_EMU_INC_STATS(cmp_slt_s);
  2237. break;
  2238. case 5:
  2239. MIPS_FPU_EMU_INC_STATS(cmp_sult_s);
  2240. break;
  2241. case 6:
  2242. MIPS_FPU_EMU_INC_STATS(cmp_sle_s);
  2243. break;
  2244. case 7:
  2245. MIPS_FPU_EMU_INC_STATS(cmp_sule_s);
  2246. break;
  2247. }
  2248. } else {
  2249. switch (cmpop) {
  2250. case 1:
  2251. MIPS_FPU_EMU_INC_STATS(cmp_sor_s);
  2252. break;
  2253. case 2:
  2254. MIPS_FPU_EMU_INC_STATS(cmp_sune_s);
  2255. break;
  2256. case 3:
  2257. MIPS_FPU_EMU_INC_STATS(cmp_sne_s);
  2258. break;
  2259. }
  2260. }
  2261. }
  2262. /* fmt is w_fmt for single precision so fix it */
  2263. rfmt = s_fmt;
  2264. /* default to false */
  2265. rv.w = 0;
  2266. /* CMP.condn.S */
  2267. SPFROMREG(fs, MIPSInst_FS(ir));
  2268. SPFROMREG(ft, MIPSInst_FT(ir));
  2269. /* positive predicates */
  2270. if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
  2271. if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
  2272. sig))
  2273. rv.w = -1; /* true, all 1s */
  2274. if ((sig) &&
  2275. ieee754_cxtest(IEEE754_INVALID_OPERATION))
  2276. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  2277. else
  2278. goto copcsr;
  2279. } else {
  2280. /* negative predicates */
  2281. switch (cmpop) {
  2282. case 1:
  2283. case 2:
  2284. case 3:
  2285. if (ieee754sp_cmp(fs, ft,
  2286. negative_cmptab[cmpop],
  2287. sig))
  2288. rv.w = -1; /* true, all 1s */
  2289. if (sig &&
  2290. ieee754_cxtest(IEEE754_INVALID_OPERATION))
  2291. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  2292. else
  2293. goto copcsr;
  2294. break;
  2295. default:
  2296. /* Reserved R6 ops */
  2297. return SIGILL;
  2298. }
  2299. }
  2300. break;
  2301. }
  2302. }
  2303. break;
  2304. }
  2305. case l_fmt:
  2306. if (!cpu_has_mips_3_4_5_64_r2_r6)
  2307. return SIGILL;
  2308. DIFROMREG(bits, MIPSInst_FS(ir));
  2309. switch (MIPSInst_FUNC(ir)) {
  2310. case fcvts_op:
  2311. /* convert long to single precision real */
  2312. MIPS_FPU_EMU_INC_STATS(cvt_s_l);
  2313. rv.s = ieee754sp_flong(bits);
  2314. rfmt = s_fmt;
  2315. goto copcsr;
  2316. case fcvtd_op:
  2317. /* convert long to double precision real */
  2318. MIPS_FPU_EMU_INC_STATS(cvt_d_l);
  2319. rv.d = ieee754dp_flong(bits);
  2320. rfmt = d_fmt;
  2321. goto copcsr;
  2322. default: {
  2323. /* Emulating the new CMP.condn.fmt R6 instruction */
  2324. int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
  2325. int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
  2326. union ieee754dp fs, ft;
  2327. if (!cpu_has_mips_r6 ||
  2328. (MIPSInst_FUNC(ir) & 0x20))
  2329. return SIGILL;
  2330. if (!sig) {
  2331. if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
  2332. switch (cmpop) {
  2333. case 0:
  2334. MIPS_FPU_EMU_INC_STATS(cmp_af_d);
  2335. break;
  2336. case 1:
  2337. MIPS_FPU_EMU_INC_STATS(cmp_un_d);
  2338. break;
  2339. case 2:
  2340. MIPS_FPU_EMU_INC_STATS(cmp_eq_d);
  2341. break;
  2342. case 3:
  2343. MIPS_FPU_EMU_INC_STATS(cmp_ueq_d);
  2344. break;
  2345. case 4:
  2346. MIPS_FPU_EMU_INC_STATS(cmp_lt_d);
  2347. break;
  2348. case 5:
  2349. MIPS_FPU_EMU_INC_STATS(cmp_ult_d);
  2350. break;
  2351. case 6:
  2352. MIPS_FPU_EMU_INC_STATS(cmp_le_d);
  2353. break;
  2354. case 7:
  2355. MIPS_FPU_EMU_INC_STATS(cmp_ule_d);
  2356. break;
  2357. }
  2358. } else {
  2359. switch (cmpop) {
  2360. case 1:
  2361. MIPS_FPU_EMU_INC_STATS(cmp_or_d);
  2362. break;
  2363. case 2:
  2364. MIPS_FPU_EMU_INC_STATS(cmp_une_d);
  2365. break;
  2366. case 3:
  2367. MIPS_FPU_EMU_INC_STATS(cmp_ne_d);
  2368. break;
  2369. }
  2370. }
  2371. } else {
  2372. if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
  2373. switch (cmpop) {
  2374. case 0:
  2375. MIPS_FPU_EMU_INC_STATS(cmp_saf_d);
  2376. break;
  2377. case 1:
  2378. MIPS_FPU_EMU_INC_STATS(cmp_sun_d);
  2379. break;
  2380. case 2:
  2381. MIPS_FPU_EMU_INC_STATS(cmp_seq_d);
  2382. break;
  2383. case 3:
  2384. MIPS_FPU_EMU_INC_STATS(cmp_sueq_d);
  2385. break;
  2386. case 4:
  2387. MIPS_FPU_EMU_INC_STATS(cmp_slt_d);
  2388. break;
  2389. case 5:
  2390. MIPS_FPU_EMU_INC_STATS(cmp_sult_d);
  2391. break;
  2392. case 6:
  2393. MIPS_FPU_EMU_INC_STATS(cmp_sle_d);
  2394. break;
  2395. case 7:
  2396. MIPS_FPU_EMU_INC_STATS(cmp_sule_d);
  2397. break;
  2398. }
  2399. } else {
  2400. switch (cmpop) {
  2401. case 1:
  2402. MIPS_FPU_EMU_INC_STATS(cmp_sor_d);
  2403. break;
  2404. case 2:
  2405. MIPS_FPU_EMU_INC_STATS(cmp_sune_d);
  2406. break;
  2407. case 3:
  2408. MIPS_FPU_EMU_INC_STATS(cmp_sne_d);
  2409. break;
  2410. }
  2411. }
  2412. }
  2413. /* fmt is l_fmt for double precision so fix it */
  2414. rfmt = d_fmt;
  2415. /* default to false */
  2416. rv.l = 0;
  2417. /* CMP.condn.D */
  2418. DPFROMREG(fs, MIPSInst_FS(ir));
  2419. DPFROMREG(ft, MIPSInst_FT(ir));
  2420. /* positive predicates */
  2421. if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
  2422. if (ieee754dp_cmp(fs, ft,
  2423. cmptab[cmpop], sig))
  2424. rv.l = -1LL; /* true, all 1s */
  2425. if (sig &&
  2426. ieee754_cxtest(IEEE754_INVALID_OPERATION))
  2427. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  2428. else
  2429. goto copcsr;
  2430. } else {
  2431. /* negative predicates */
  2432. switch (cmpop) {
  2433. case 1:
  2434. case 2:
  2435. case 3:
  2436. if (ieee754dp_cmp(fs, ft,
  2437. negative_cmptab[cmpop],
  2438. sig))
  2439. rv.l = -1LL; /* true, all 1s */
  2440. if (sig &&
  2441. ieee754_cxtest(IEEE754_INVALID_OPERATION))
  2442. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  2443. else
  2444. goto copcsr;
  2445. break;
  2446. default:
  2447. /* Reserved R6 ops */
  2448. return SIGILL;
  2449. }
  2450. }
  2451. break;
  2452. }
  2453. }
  2454. break;
  2455. default:
  2456. return SIGILL;
  2457. }
  2458. /*
  2459. * Update the fpu CSR register for this operation.
  2460. * If an exception is required, generate a tidy SIGFPE exception,
  2461. * without updating the result register.
  2462. * Note: cause exception bits do not accumulate, they are rewritten
  2463. * for each op; only the flag/sticky bits accumulate.
  2464. */
  2465. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  2466. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  2467. /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
  2468. return SIGFPE;
  2469. }
  2470. /*
  2471. * Now we can safely write the result back to the register file.
  2472. */
  2473. switch (rfmt) {
  2474. case -1:
  2475. if (cpu_has_mips_4_5_r)
  2476. cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
  2477. else
  2478. cbit = FPU_CSR_COND;
  2479. if (rv.w)
  2480. ctx->fcr31 |= cbit;
  2481. else
  2482. ctx->fcr31 &= ~cbit;
  2483. break;
  2484. case d_fmt:
  2485. DPTOREG(rv.d, MIPSInst_FD(ir));
  2486. break;
  2487. case s_fmt:
  2488. SPTOREG(rv.s, MIPSInst_FD(ir));
  2489. break;
  2490. case w_fmt:
  2491. SITOREG(rv.w, MIPSInst_FD(ir));
  2492. break;
  2493. case l_fmt:
  2494. if (!cpu_has_mips_3_4_5_64_r2_r6)
  2495. return SIGILL;
  2496. DITOREG(rv.l, MIPSInst_FD(ir));
  2497. break;
  2498. default:
  2499. return SIGILL;
  2500. }
  2501. return 0;
  2502. }
  2503. /*
  2504. * Emulate FPU instructions.
  2505. *
  2506. * If we use FPU hardware, then we have been typically called to handle
  2507. * an unimplemented operation, such as where an operand is a NaN or
  2508. * denormalized. In that case exit the emulation loop after a single
  2509. * iteration so as to let hardware execute any subsequent instructions.
  2510. *
  2511. * If we have no FPU hardware or it has been disabled, then continue
  2512. * emulating floating-point instructions until one of these conditions
  2513. * has occurred:
  2514. *
  2515. * - a non-FPU instruction has been encountered,
  2516. *
  2517. * - an attempt to emulate has ended with a signal,
  2518. *
  2519. * - the ISA mode has been switched.
  2520. *
  2521. * We need to terminate the emulation loop if we got switched to the
  2522. * MIPS16 mode, whether supported or not, so that we do not attempt
  2523. * to emulate a MIPS16 instruction as a regular MIPS FPU instruction.
  2524. * Similarly if we got switched to the microMIPS mode and only the
  2525. * regular MIPS mode is supported, so that we do not attempt to emulate
  2526. * a microMIPS instruction as a regular MIPS FPU instruction. Or if
  2527. * we got switched to the regular MIPS mode and only the microMIPS mode
  2528. * is supported, so that we do not attempt to emulate a regular MIPS
  2529. * instruction that should cause an Address Error exception instead.
  2530. * For simplicity we always terminate upon an ISA mode switch.
  2531. */
  2532. int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  2533. int has_fpu, void __user **fault_addr)
  2534. {
  2535. unsigned long oldepc, prevepc;
  2536. struct mm_decoded_insn dec_insn;
  2537. u16 instr[4];
  2538. u16 *instr_ptr;
  2539. int sig = 0;
  2540. oldepc = xcp->cp0_epc;
  2541. do {
  2542. prevepc = xcp->cp0_epc;
  2543. if (get_isa16_mode(prevepc) && cpu_has_mmips) {
  2544. /*
  2545. * Get next 2 microMIPS instructions and convert them
  2546. * into 32-bit instructions.
  2547. */
  2548. if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
  2549. (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
  2550. (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
  2551. (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
  2552. MIPS_FPU_EMU_INC_STATS(errors);
  2553. return SIGBUS;
  2554. }
  2555. instr_ptr = instr;
  2556. /* Get first instruction. */
  2557. if (mm_insn_16bit(*instr_ptr)) {
  2558. /* Duplicate the half-word. */
  2559. dec_insn.insn = (*instr_ptr << 16) |
  2560. (*instr_ptr);
  2561. /* 16-bit instruction. */
  2562. dec_insn.pc_inc = 2;
  2563. instr_ptr += 1;
  2564. } else {
  2565. dec_insn.insn = (*instr_ptr << 16) |
  2566. *(instr_ptr+1);
  2567. /* 32-bit instruction. */
  2568. dec_insn.pc_inc = 4;
  2569. instr_ptr += 2;
  2570. }
  2571. /* Get second instruction. */
  2572. if (mm_insn_16bit(*instr_ptr)) {
  2573. /* Duplicate the half-word. */
  2574. dec_insn.next_insn = (*instr_ptr << 16) |
  2575. (*instr_ptr);
  2576. /* 16-bit instruction. */
  2577. dec_insn.next_pc_inc = 2;
  2578. } else {
  2579. dec_insn.next_insn = (*instr_ptr << 16) |
  2580. *(instr_ptr+1);
  2581. /* 32-bit instruction. */
  2582. dec_insn.next_pc_inc = 4;
  2583. }
  2584. dec_insn.micro_mips_mode = 1;
  2585. } else {
  2586. if ((get_user(dec_insn.insn,
  2587. (mips_instruction __user *) xcp->cp0_epc)) ||
  2588. (get_user(dec_insn.next_insn,
  2589. (mips_instruction __user *)(xcp->cp0_epc+4)))) {
  2590. MIPS_FPU_EMU_INC_STATS(errors);
  2591. return SIGBUS;
  2592. }
  2593. dec_insn.pc_inc = 4;
  2594. dec_insn.next_pc_inc = 4;
  2595. dec_insn.micro_mips_mode = 0;
  2596. }
  2597. if ((dec_insn.insn == 0) ||
  2598. ((dec_insn.pc_inc == 2) &&
  2599. ((dec_insn.insn & 0xffff) == MM_NOP16)))
  2600. xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
  2601. else {
  2602. /*
  2603. * The 'ieee754_csr' is an alias of ctx->fcr31.
  2604. * No need to copy ctx->fcr31 to ieee754_csr.
  2605. */
  2606. sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
  2607. }
  2608. if (has_fpu)
  2609. break;
  2610. if (sig)
  2611. break;
  2612. /*
  2613. * We have to check for the ISA bit explicitly here,
  2614. * because `get_isa16_mode' may return 0 if support
  2615. * for code compression has been globally disabled,
  2616. * or otherwise we may produce the wrong signal or
  2617. * even proceed successfully where we must not.
  2618. */
  2619. if ((xcp->cp0_epc ^ prevepc) & 0x1)
  2620. break;
  2621. cond_resched();
  2622. } while (xcp->cp0_epc > prevepc);
  2623. /* SIGILL indicates a non-fpu instruction */
  2624. if (sig == SIGILL && xcp->cp0_epc != oldepc)
  2625. /* but if EPC has advanced, then ignore it */
  2626. sig = 0;
  2627. return sig;
  2628. }