cp1emu.c 70 KB

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