cp1emu.c 52 KB

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