cp1emu.c 63 KB

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