cp1emu.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  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. case mm_mfhc1_op:
  401. case mm_mthc1_op:
  402. if (insn.mm_fp1_format.op == mm_mfc1_op)
  403. op = mfc_op;
  404. else if (insn.mm_fp1_format.op == mm_mtc1_op)
  405. op = mtc_op;
  406. else if (insn.mm_fp1_format.op == mm_cfc1_op)
  407. op = cfc_op;
  408. else if (insn.mm_fp1_format.op == mm_ctc1_op)
  409. op = ctc_op;
  410. else if (insn.mm_fp1_format.op == mm_mfhc1_op)
  411. op = mfhc_op;
  412. else
  413. op = mthc_op;
  414. mips32_insn.fp1_format.opcode = cop1_op;
  415. mips32_insn.fp1_format.op = op;
  416. mips32_insn.fp1_format.rt =
  417. insn.mm_fp1_format.rt;
  418. mips32_insn.fp1_format.fs =
  419. insn.mm_fp1_format.fs;
  420. mips32_insn.fp1_format.fd = 0;
  421. mips32_insn.fp1_format.func = 0;
  422. break;
  423. default:
  424. return SIGILL;
  425. }
  426. break;
  427. case mm_32f_74_op: /* c.cond.fmt */
  428. mips32_insn.fp0_format.opcode = cop1_op;
  429. mips32_insn.fp0_format.fmt =
  430. sdps_format[insn.mm_fp4_format.fmt];
  431. mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
  432. mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
  433. mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
  434. mips32_insn.fp0_format.func =
  435. insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
  436. break;
  437. default:
  438. return SIGILL;
  439. }
  440. break;
  441. default:
  442. return SIGILL;
  443. }
  444. *insn_ptr = mips32_insn;
  445. return 0;
  446. }
  447. int mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
  448. unsigned long *contpc)
  449. {
  450. union mips_instruction insn = (union mips_instruction)dec_insn.insn;
  451. int bc_false = 0;
  452. unsigned int fcr31;
  453. unsigned int bit;
  454. if (!cpu_has_mmips)
  455. return 0;
  456. switch (insn.mm_i_format.opcode) {
  457. case mm_pool32a_op:
  458. if ((insn.mm_i_format.simmediate & MM_POOL32A_MINOR_MASK) ==
  459. mm_pool32axf_op) {
  460. switch (insn.mm_i_format.simmediate >>
  461. MM_POOL32A_MINOR_SHIFT) {
  462. case mm_jalr_op:
  463. case mm_jalrhb_op:
  464. case mm_jalrs_op:
  465. case mm_jalrshb_op:
  466. if (insn.mm_i_format.rt != 0) /* Not mm_jr */
  467. regs->regs[insn.mm_i_format.rt] =
  468. regs->cp0_epc +
  469. dec_insn.pc_inc +
  470. dec_insn.next_pc_inc;
  471. *contpc = regs->regs[insn.mm_i_format.rs];
  472. return 1;
  473. }
  474. }
  475. break;
  476. case mm_pool32i_op:
  477. switch (insn.mm_i_format.rt) {
  478. case mm_bltzals_op:
  479. case mm_bltzal_op:
  480. regs->regs[31] = regs->cp0_epc +
  481. dec_insn.pc_inc +
  482. dec_insn.next_pc_inc;
  483. /* Fall through */
  484. case mm_bltz_op:
  485. if ((long)regs->regs[insn.mm_i_format.rs] < 0)
  486. *contpc = regs->cp0_epc +
  487. dec_insn.pc_inc +
  488. (insn.mm_i_format.simmediate << 1);
  489. else
  490. *contpc = regs->cp0_epc +
  491. dec_insn.pc_inc +
  492. dec_insn.next_pc_inc;
  493. return 1;
  494. case mm_bgezals_op:
  495. case mm_bgezal_op:
  496. regs->regs[31] = regs->cp0_epc +
  497. dec_insn.pc_inc +
  498. dec_insn.next_pc_inc;
  499. /* Fall through */
  500. case mm_bgez_op:
  501. if ((long)regs->regs[insn.mm_i_format.rs] >= 0)
  502. *contpc = regs->cp0_epc +
  503. dec_insn.pc_inc +
  504. (insn.mm_i_format.simmediate << 1);
  505. else
  506. *contpc = regs->cp0_epc +
  507. dec_insn.pc_inc +
  508. dec_insn.next_pc_inc;
  509. return 1;
  510. case mm_blez_op:
  511. if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
  512. *contpc = regs->cp0_epc +
  513. dec_insn.pc_inc +
  514. (insn.mm_i_format.simmediate << 1);
  515. else
  516. *contpc = regs->cp0_epc +
  517. dec_insn.pc_inc +
  518. dec_insn.next_pc_inc;
  519. return 1;
  520. case mm_bgtz_op:
  521. if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
  522. *contpc = regs->cp0_epc +
  523. dec_insn.pc_inc +
  524. (insn.mm_i_format.simmediate << 1);
  525. else
  526. *contpc = regs->cp0_epc +
  527. dec_insn.pc_inc +
  528. dec_insn.next_pc_inc;
  529. return 1;
  530. case mm_bc2f_op:
  531. case mm_bc1f_op:
  532. bc_false = 1;
  533. /* Fall through */
  534. case mm_bc2t_op:
  535. case mm_bc1t_op:
  536. preempt_disable();
  537. if (is_fpu_owner())
  538. asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
  539. else
  540. fcr31 = current->thread.fpu.fcr31;
  541. preempt_enable();
  542. if (bc_false)
  543. fcr31 = ~fcr31;
  544. bit = (insn.mm_i_format.rs >> 2);
  545. bit += (bit != 0);
  546. bit += 23;
  547. if (fcr31 & (1 << bit))
  548. *contpc = regs->cp0_epc +
  549. dec_insn.pc_inc +
  550. (insn.mm_i_format.simmediate << 1);
  551. else
  552. *contpc = regs->cp0_epc +
  553. dec_insn.pc_inc + dec_insn.next_pc_inc;
  554. return 1;
  555. }
  556. break;
  557. case mm_pool16c_op:
  558. switch (insn.mm_i_format.rt) {
  559. case mm_jalr16_op:
  560. case mm_jalrs16_op:
  561. regs->regs[31] = regs->cp0_epc +
  562. dec_insn.pc_inc + dec_insn.next_pc_inc;
  563. /* Fall through */
  564. case mm_jr16_op:
  565. *contpc = regs->regs[insn.mm_i_format.rs];
  566. return 1;
  567. }
  568. break;
  569. case mm_beqz16_op:
  570. if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] == 0)
  571. *contpc = regs->cp0_epc +
  572. dec_insn.pc_inc +
  573. (insn.mm_b1_format.simmediate << 1);
  574. else
  575. *contpc = regs->cp0_epc +
  576. dec_insn.pc_inc + dec_insn.next_pc_inc;
  577. return 1;
  578. case mm_bnez16_op:
  579. if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] != 0)
  580. *contpc = regs->cp0_epc +
  581. dec_insn.pc_inc +
  582. (insn.mm_b1_format.simmediate << 1);
  583. else
  584. *contpc = regs->cp0_epc +
  585. dec_insn.pc_inc + dec_insn.next_pc_inc;
  586. return 1;
  587. case mm_b16_op:
  588. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  589. (insn.mm_b0_format.simmediate << 1);
  590. return 1;
  591. case mm_beq32_op:
  592. if (regs->regs[insn.mm_i_format.rs] ==
  593. regs->regs[insn.mm_i_format.rt])
  594. *contpc = regs->cp0_epc +
  595. dec_insn.pc_inc +
  596. (insn.mm_i_format.simmediate << 1);
  597. else
  598. *contpc = regs->cp0_epc +
  599. dec_insn.pc_inc +
  600. dec_insn.next_pc_inc;
  601. return 1;
  602. case mm_bne32_op:
  603. if (regs->regs[insn.mm_i_format.rs] !=
  604. regs->regs[insn.mm_i_format.rt])
  605. *contpc = regs->cp0_epc +
  606. dec_insn.pc_inc +
  607. (insn.mm_i_format.simmediate << 1);
  608. else
  609. *contpc = regs->cp0_epc +
  610. dec_insn.pc_inc + dec_insn.next_pc_inc;
  611. return 1;
  612. case mm_jalx32_op:
  613. regs->regs[31] = regs->cp0_epc +
  614. dec_insn.pc_inc + dec_insn.next_pc_inc;
  615. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  616. *contpc >>= 28;
  617. *contpc <<= 28;
  618. *contpc |= (insn.j_format.target << 2);
  619. return 1;
  620. case mm_jals32_op:
  621. case mm_jal32_op:
  622. regs->regs[31] = regs->cp0_epc +
  623. dec_insn.pc_inc + dec_insn.next_pc_inc;
  624. /* Fall through */
  625. case mm_j32_op:
  626. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  627. *contpc >>= 27;
  628. *contpc <<= 27;
  629. *contpc |= (insn.j_format.target << 1);
  630. set_isa16_mode(*contpc);
  631. return 1;
  632. }
  633. return 0;
  634. }
  635. /*
  636. * Redundant with logic already in kernel/branch.c,
  637. * embedded in compute_return_epc. At some point,
  638. * a single subroutine should be used across both
  639. * modules.
  640. */
  641. static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
  642. unsigned long *contpc)
  643. {
  644. union mips_instruction insn = (union mips_instruction)dec_insn.insn;
  645. unsigned int fcr31;
  646. unsigned int bit = 0;
  647. switch (insn.i_format.opcode) {
  648. case spec_op:
  649. switch (insn.r_format.func) {
  650. case jalr_op:
  651. regs->regs[insn.r_format.rd] =
  652. regs->cp0_epc + dec_insn.pc_inc +
  653. dec_insn.next_pc_inc;
  654. /* Fall through */
  655. case jr_op:
  656. *contpc = regs->regs[insn.r_format.rs];
  657. return 1;
  658. }
  659. break;
  660. case bcond_op:
  661. switch (insn.i_format.rt) {
  662. case bltzal_op:
  663. case bltzall_op:
  664. regs->regs[31] = regs->cp0_epc +
  665. dec_insn.pc_inc +
  666. dec_insn.next_pc_inc;
  667. /* Fall through */
  668. case bltz_op:
  669. case bltzl_op:
  670. if ((long)regs->regs[insn.i_format.rs] < 0)
  671. *contpc = regs->cp0_epc +
  672. dec_insn.pc_inc +
  673. (insn.i_format.simmediate << 2);
  674. else
  675. *contpc = regs->cp0_epc +
  676. dec_insn.pc_inc +
  677. dec_insn.next_pc_inc;
  678. return 1;
  679. case bgezal_op:
  680. case bgezall_op:
  681. regs->regs[31] = regs->cp0_epc +
  682. dec_insn.pc_inc +
  683. dec_insn.next_pc_inc;
  684. /* Fall through */
  685. case bgez_op:
  686. case bgezl_op:
  687. if ((long)regs->regs[insn.i_format.rs] >= 0)
  688. *contpc = regs->cp0_epc +
  689. dec_insn.pc_inc +
  690. (insn.i_format.simmediate << 2);
  691. else
  692. *contpc = regs->cp0_epc +
  693. dec_insn.pc_inc +
  694. dec_insn.next_pc_inc;
  695. return 1;
  696. }
  697. break;
  698. case jalx_op:
  699. set_isa16_mode(bit);
  700. case jal_op:
  701. regs->regs[31] = regs->cp0_epc +
  702. dec_insn.pc_inc +
  703. dec_insn.next_pc_inc;
  704. /* Fall through */
  705. case j_op:
  706. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  707. *contpc >>= 28;
  708. *contpc <<= 28;
  709. *contpc |= (insn.j_format.target << 2);
  710. /* Set microMIPS mode bit: XOR for jalx. */
  711. *contpc ^= bit;
  712. return 1;
  713. case beq_op:
  714. case beql_op:
  715. if (regs->regs[insn.i_format.rs] ==
  716. regs->regs[insn.i_format.rt])
  717. *contpc = regs->cp0_epc +
  718. dec_insn.pc_inc +
  719. (insn.i_format.simmediate << 2);
  720. else
  721. *contpc = regs->cp0_epc +
  722. dec_insn.pc_inc +
  723. dec_insn.next_pc_inc;
  724. return 1;
  725. case bne_op:
  726. case bnel_op:
  727. if (regs->regs[insn.i_format.rs] !=
  728. regs->regs[insn.i_format.rt])
  729. *contpc = regs->cp0_epc +
  730. dec_insn.pc_inc +
  731. (insn.i_format.simmediate << 2);
  732. else
  733. *contpc = regs->cp0_epc +
  734. dec_insn.pc_inc +
  735. dec_insn.next_pc_inc;
  736. return 1;
  737. case blez_op:
  738. case blezl_op:
  739. if ((long)regs->regs[insn.i_format.rs] <= 0)
  740. *contpc = regs->cp0_epc +
  741. dec_insn.pc_inc +
  742. (insn.i_format.simmediate << 2);
  743. else
  744. *contpc = regs->cp0_epc +
  745. dec_insn.pc_inc +
  746. dec_insn.next_pc_inc;
  747. return 1;
  748. case bgtz_op:
  749. case bgtzl_op:
  750. if ((long)regs->regs[insn.i_format.rs] > 0)
  751. *contpc = regs->cp0_epc +
  752. dec_insn.pc_inc +
  753. (insn.i_format.simmediate << 2);
  754. else
  755. *contpc = regs->cp0_epc +
  756. dec_insn.pc_inc +
  757. dec_insn.next_pc_inc;
  758. return 1;
  759. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  760. case lwc2_op: /* This is bbit0 on Octeon */
  761. if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 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 ldc2_op: /* This is bbit032 on Octeon */
  767. if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
  768. *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
  769. else
  770. *contpc = regs->cp0_epc + 8;
  771. return 1;
  772. case swc2_op: /* This is bbit1 on Octeon */
  773. if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
  774. *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
  775. else
  776. *contpc = regs->cp0_epc + 8;
  777. return 1;
  778. case sdc2_op: /* This is bbit132 on Octeon */
  779. if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
  780. *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
  781. else
  782. *contpc = regs->cp0_epc + 8;
  783. return 1;
  784. #endif
  785. case cop0_op:
  786. case cop1_op:
  787. case cop2_op:
  788. case cop1x_op:
  789. if (insn.i_format.rs == bc_op) {
  790. preempt_disable();
  791. if (is_fpu_owner())
  792. asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
  793. else
  794. fcr31 = current->thread.fpu.fcr31;
  795. preempt_enable();
  796. bit = (insn.i_format.rt >> 2);
  797. bit += (bit != 0);
  798. bit += 23;
  799. switch (insn.i_format.rt & 3) {
  800. case 0: /* bc1f */
  801. case 2: /* bc1fl */
  802. if (~fcr31 & (1 << bit))
  803. *contpc = regs->cp0_epc +
  804. dec_insn.pc_inc +
  805. (insn.i_format.simmediate << 2);
  806. else
  807. *contpc = regs->cp0_epc +
  808. dec_insn.pc_inc +
  809. dec_insn.next_pc_inc;
  810. return 1;
  811. case 1: /* bc1t */
  812. case 3: /* bc1tl */
  813. if (fcr31 & (1 << bit))
  814. *contpc = regs->cp0_epc +
  815. dec_insn.pc_inc +
  816. (insn.i_format.simmediate << 2);
  817. else
  818. *contpc = regs->cp0_epc +
  819. dec_insn.pc_inc +
  820. dec_insn.next_pc_inc;
  821. return 1;
  822. }
  823. }
  824. break;
  825. }
  826. return 0;
  827. }
  828. /*
  829. * In the Linux kernel, we support selection of FPR format on the
  830. * basis of the Status.FR bit. If an FPU is not present, the FR bit
  831. * is hardwired to zero, which would imply a 32-bit FPU even for
  832. * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
  833. * FPU emu is slow and bulky and optimizing this function offers fairly
  834. * sizeable benefits so we try to be clever and make this function return
  835. * a constant whenever possible, that is on 64-bit kernels without O32
  836. * compatibility enabled and on 32-bit without 64-bit FPU support.
  837. */
  838. static inline int cop1_64bit(struct pt_regs *xcp)
  839. {
  840. #if defined(CONFIG_64BIT) && !defined(CONFIG_MIPS32_O32)
  841. return 1;
  842. #elif defined(CONFIG_32BIT) && !defined(CONFIG_MIPS_O32_FP64_SUPPORT)
  843. return 0;
  844. #else
  845. return !test_thread_flag(TIF_32BIT_FPREGS);
  846. #endif
  847. }
  848. #define SIFROMREG(si, x) ((si) = cop1_64bit(xcp) || !(x & 1) ? \
  849. (int)ctx->fpr[x] : (int)(ctx->fpr[x & ~1] >> 32))
  850. #define SITOREG(si, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = \
  851. cop1_64bit(xcp) || !(x & 1) ? \
  852. ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
  853. ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
  854. #define SIFROMHREG(si, x) ((si) = (int)(ctx->fpr[x] >> 32))
  855. #define SITOHREG(si, x) (ctx->fpr[x] = \
  856. ctx->fpr[x] << 32 >> 32 | (u64)(si) << 32)
  857. #define DIFROMREG(di, x) ((di) = ctx->fpr[x & ~(cop1_64bit(xcp) == 0)])
  858. #define DITOREG(di, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = (di))
  859. #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
  860. #define SPTOREG(sp, x) SITOREG((sp).bits, x)
  861. #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
  862. #define DPTOREG(dp, x) DITOREG((dp).bits, x)
  863. /*
  864. * Emulate the single floating point instruction pointed at by EPC.
  865. * Two instructions if the instruction is in a branch delay slot.
  866. */
  867. static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  868. struct mm_decoded_insn dec_insn, void *__user *fault_addr)
  869. {
  870. mips_instruction ir;
  871. unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
  872. unsigned int cond;
  873. int pc_inc;
  874. /* XXX NEC Vr54xx bug workaround */
  875. if (xcp->cp0_cause & CAUSEF_BD) {
  876. if (dec_insn.micro_mips_mode) {
  877. if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
  878. xcp->cp0_cause &= ~CAUSEF_BD;
  879. } else {
  880. if (!isBranchInstr(xcp, dec_insn, &contpc))
  881. xcp->cp0_cause &= ~CAUSEF_BD;
  882. }
  883. }
  884. if (xcp->cp0_cause & CAUSEF_BD) {
  885. /*
  886. * The instruction to be emulated is in a branch delay slot
  887. * which means that we have to emulate the branch instruction
  888. * BEFORE we do the cop1 instruction.
  889. *
  890. * This branch could be a COP1 branch, but in that case we
  891. * would have had a trap for that instruction, and would not
  892. * come through this route.
  893. *
  894. * Linux MIPS branch emulator operates on context, updating the
  895. * cp0_epc.
  896. */
  897. ir = dec_insn.next_insn; /* process delay slot instr */
  898. pc_inc = dec_insn.next_pc_inc;
  899. } else {
  900. ir = dec_insn.insn; /* process current instr */
  901. pc_inc = dec_insn.pc_inc;
  902. }
  903. /*
  904. * Since microMIPS FPU instructios are a subset of MIPS32 FPU
  905. * instructions, we want to convert microMIPS FPU instructions
  906. * into MIPS32 instructions so that we could reuse all of the
  907. * FPU emulation code.
  908. *
  909. * NOTE: We cannot do this for branch instructions since they
  910. * are not a subset. Example: Cannot emulate a 16-bit
  911. * aligned target address with a MIPS32 instruction.
  912. */
  913. if (dec_insn.micro_mips_mode) {
  914. /*
  915. * If next instruction is a 16-bit instruction, then it
  916. * it cannot be a FPU instruction. This could happen
  917. * since we can be called for non-FPU instructions.
  918. */
  919. if ((pc_inc == 2) ||
  920. (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
  921. == SIGILL))
  922. return SIGILL;
  923. }
  924. emul:
  925. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
  926. MIPS_FPU_EMU_INC_STATS(emulated);
  927. switch (MIPSInst_OPCODE(ir)) {
  928. case ldc1_op:{
  929. u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  930. MIPSInst_SIMM(ir));
  931. u64 val;
  932. MIPS_FPU_EMU_INC_STATS(loads);
  933. if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
  934. MIPS_FPU_EMU_INC_STATS(errors);
  935. *fault_addr = va;
  936. return SIGBUS;
  937. }
  938. if (__get_user(val, va)) {
  939. MIPS_FPU_EMU_INC_STATS(errors);
  940. *fault_addr = va;
  941. return SIGSEGV;
  942. }
  943. DITOREG(val, MIPSInst_RT(ir));
  944. break;
  945. }
  946. case sdc1_op:{
  947. u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  948. MIPSInst_SIMM(ir));
  949. u64 val;
  950. MIPS_FPU_EMU_INC_STATS(stores);
  951. DIFROMREG(val, MIPSInst_RT(ir));
  952. if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
  953. MIPS_FPU_EMU_INC_STATS(errors);
  954. *fault_addr = va;
  955. return SIGBUS;
  956. }
  957. if (__put_user(val, va)) {
  958. MIPS_FPU_EMU_INC_STATS(errors);
  959. *fault_addr = va;
  960. return SIGSEGV;
  961. }
  962. break;
  963. }
  964. case lwc1_op:{
  965. u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  966. MIPSInst_SIMM(ir));
  967. u32 val;
  968. MIPS_FPU_EMU_INC_STATS(loads);
  969. if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
  970. MIPS_FPU_EMU_INC_STATS(errors);
  971. *fault_addr = va;
  972. return SIGBUS;
  973. }
  974. if (__get_user(val, va)) {
  975. MIPS_FPU_EMU_INC_STATS(errors);
  976. *fault_addr = va;
  977. return SIGSEGV;
  978. }
  979. SITOREG(val, MIPSInst_RT(ir));
  980. break;
  981. }
  982. case swc1_op:{
  983. u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
  984. MIPSInst_SIMM(ir));
  985. u32 val;
  986. MIPS_FPU_EMU_INC_STATS(stores);
  987. SIFROMREG(val, MIPSInst_RT(ir));
  988. if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
  989. MIPS_FPU_EMU_INC_STATS(errors);
  990. *fault_addr = va;
  991. return SIGBUS;
  992. }
  993. if (__put_user(val, va)) {
  994. MIPS_FPU_EMU_INC_STATS(errors);
  995. *fault_addr = va;
  996. return SIGSEGV;
  997. }
  998. break;
  999. }
  1000. case cop1_op:
  1001. switch (MIPSInst_RS(ir)) {
  1002. #if defined(__mips64)
  1003. case dmfc_op:
  1004. /* copregister fs -> gpr[rt] */
  1005. if (MIPSInst_RT(ir) != 0) {
  1006. DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  1007. MIPSInst_RD(ir));
  1008. }
  1009. break;
  1010. case dmtc_op:
  1011. /* copregister fs <- rt */
  1012. DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1013. break;
  1014. #endif
  1015. case mfhc_op:
  1016. if (!cpu_has_mips_r2)
  1017. goto sigill;
  1018. /* copregister rd -> gpr[rt] */
  1019. if (MIPSInst_RT(ir) != 0) {
  1020. SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
  1021. MIPSInst_RD(ir));
  1022. }
  1023. break;
  1024. case mthc_op:
  1025. if (!cpu_has_mips_r2)
  1026. goto sigill;
  1027. /* copregister rd <- gpr[rt] */
  1028. SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1029. break;
  1030. case mfc_op:
  1031. /* copregister rd -> gpr[rt] */
  1032. if (MIPSInst_RT(ir) != 0) {
  1033. SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
  1034. MIPSInst_RD(ir));
  1035. }
  1036. break;
  1037. case mtc_op:
  1038. /* copregister rd <- rt */
  1039. SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
  1040. break;
  1041. case cfc_op:{
  1042. /* cop control register rd -> gpr[rt] */
  1043. u32 value;
  1044. if (MIPSInst_RD(ir) == FPCREG_CSR) {
  1045. value = ctx->fcr31;
  1046. value = (value & ~FPU_CSR_RM) |
  1047. mips_rm[modeindex(value)];
  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. else if (MIPSInst_RD(ir) == FPCREG_RID)
  1055. value = 0;
  1056. else
  1057. value = 0;
  1058. if (MIPSInst_RT(ir))
  1059. xcp->regs[MIPSInst_RT(ir)] = value;
  1060. break;
  1061. }
  1062. case ctc_op:{
  1063. /* copregister rd <- rt */
  1064. u32 value;
  1065. if (MIPSInst_RT(ir) == 0)
  1066. value = 0;
  1067. else
  1068. value = xcp->regs[MIPSInst_RT(ir)];
  1069. /* we only have one writable control reg
  1070. */
  1071. if (MIPSInst_RD(ir) == FPCREG_CSR) {
  1072. #ifdef CSRTRACE
  1073. printk("%p gpr[%d]->csr=%08x\n",
  1074. (void *) (xcp->cp0_epc),
  1075. MIPSInst_RT(ir), value);
  1076. #endif
  1077. /*
  1078. * Don't write reserved bits,
  1079. * and convert to ieee library modes
  1080. */
  1081. ctx->fcr31 = (value &
  1082. ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
  1083. ieee_rm[modeindex(value)];
  1084. }
  1085. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1086. return SIGFPE;
  1087. }
  1088. break;
  1089. }
  1090. case bc_op:{
  1091. int likely = 0;
  1092. if (xcp->cp0_cause & CAUSEF_BD)
  1093. return SIGILL;
  1094. #if __mips >= 4
  1095. cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
  1096. #else
  1097. cond = ctx->fcr31 & FPU_CSR_COND;
  1098. #endif
  1099. switch (MIPSInst_RT(ir) & 3) {
  1100. case bcfl_op:
  1101. likely = 1;
  1102. case bcf_op:
  1103. cond = !cond;
  1104. break;
  1105. case bctl_op:
  1106. likely = 1;
  1107. case bct_op:
  1108. break;
  1109. default:
  1110. /* thats an illegal instruction */
  1111. return SIGILL;
  1112. }
  1113. xcp->cp0_cause |= CAUSEF_BD;
  1114. if (cond) {
  1115. /* branch taken: emulate dslot
  1116. * instruction
  1117. */
  1118. xcp->cp0_epc += dec_insn.pc_inc;
  1119. contpc = MIPSInst_SIMM(ir);
  1120. ir = dec_insn.next_insn;
  1121. if (dec_insn.micro_mips_mode) {
  1122. contpc = (xcp->cp0_epc + (contpc << 1));
  1123. /* If 16-bit instruction, not FPU. */
  1124. if ((dec_insn.next_pc_inc == 2) ||
  1125. (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
  1126. /*
  1127. * Since this instruction will
  1128. * be put on the stack with
  1129. * 32-bit words, get around
  1130. * this problem by putting a
  1131. * NOP16 as the second one.
  1132. */
  1133. if (dec_insn.next_pc_inc == 2)
  1134. ir = (ir & (~0xffff)) | MM_NOP16;
  1135. /*
  1136. * Single step the non-CP1
  1137. * instruction in the dslot.
  1138. */
  1139. return mips_dsemul(xcp, ir, contpc);
  1140. }
  1141. } else
  1142. contpc = (xcp->cp0_epc + (contpc << 2));
  1143. switch (MIPSInst_OPCODE(ir)) {
  1144. case lwc1_op:
  1145. case swc1_op:
  1146. #if (__mips >= 2 || defined(__mips64))
  1147. case ldc1_op:
  1148. case sdc1_op:
  1149. #endif
  1150. case cop1_op:
  1151. #if __mips >= 4 && __mips != 32
  1152. case cop1x_op:
  1153. #endif
  1154. /* its one of ours */
  1155. goto emul;
  1156. #if __mips >= 4
  1157. case spec_op:
  1158. if (MIPSInst_FUNC(ir) == movc_op)
  1159. goto emul;
  1160. break;
  1161. #endif
  1162. }
  1163. /*
  1164. * Single step the non-cp1
  1165. * instruction in the dslot
  1166. */
  1167. return mips_dsemul(xcp, ir, contpc);
  1168. }
  1169. else {
  1170. /* branch not taken */
  1171. if (likely) {
  1172. /*
  1173. * branch likely nullifies
  1174. * dslot if not taken
  1175. */
  1176. xcp->cp0_epc += dec_insn.pc_inc;
  1177. contpc += dec_insn.pc_inc;
  1178. /*
  1179. * else continue & execute
  1180. * dslot as normal insn
  1181. */
  1182. }
  1183. }
  1184. break;
  1185. }
  1186. default:
  1187. if (!(MIPSInst_RS(ir) & 0x10))
  1188. return SIGILL;
  1189. {
  1190. int sig;
  1191. /* a real fpu computation instruction */
  1192. if ((sig = fpu_emu(xcp, ctx, ir)))
  1193. return sig;
  1194. }
  1195. }
  1196. break;
  1197. #if __mips >= 4 && __mips != 32
  1198. case cop1x_op:{
  1199. int sig = fpux_emu(xcp, ctx, ir, fault_addr);
  1200. if (sig)
  1201. return sig;
  1202. break;
  1203. }
  1204. #endif
  1205. #if __mips >= 4
  1206. case spec_op:
  1207. if (MIPSInst_FUNC(ir) != movc_op)
  1208. return SIGILL;
  1209. cond = fpucondbit[MIPSInst_RT(ir) >> 2];
  1210. if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
  1211. xcp->regs[MIPSInst_RD(ir)] =
  1212. xcp->regs[MIPSInst_RS(ir)];
  1213. break;
  1214. #endif
  1215. default:
  1216. sigill:
  1217. return SIGILL;
  1218. }
  1219. /* we did it !! */
  1220. xcp->cp0_epc = contpc;
  1221. xcp->cp0_cause &= ~CAUSEF_BD;
  1222. return 0;
  1223. }
  1224. /*
  1225. * Conversion table from MIPS compare ops 48-63
  1226. * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
  1227. */
  1228. static const unsigned char cmptab[8] = {
  1229. 0, /* cmp_0 (sig) cmp_sf */
  1230. IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
  1231. IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
  1232. IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
  1233. IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
  1234. IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
  1235. IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
  1236. IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
  1237. };
  1238. #if __mips >= 4 && __mips != 32
  1239. /*
  1240. * Additional MIPS4 instructions
  1241. */
  1242. #define DEF3OP(name, p, f1, f2, f3) \
  1243. static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \
  1244. ieee754##p t) \
  1245. { \
  1246. struct _ieee754_csr ieee754_csr_save; \
  1247. s = f1(s, t); \
  1248. ieee754_csr_save = ieee754_csr; \
  1249. s = f2(s, r); \
  1250. ieee754_csr_save.cx |= ieee754_csr.cx; \
  1251. ieee754_csr_save.sx |= ieee754_csr.sx; \
  1252. s = f3(s); \
  1253. ieee754_csr.cx |= ieee754_csr_save.cx; \
  1254. ieee754_csr.sx |= ieee754_csr_save.sx; \
  1255. return s; \
  1256. }
  1257. static ieee754dp fpemu_dp_recip(ieee754dp d)
  1258. {
  1259. return ieee754dp_div(ieee754dp_one(0), d);
  1260. }
  1261. static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
  1262. {
  1263. return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
  1264. }
  1265. static ieee754sp fpemu_sp_recip(ieee754sp s)
  1266. {
  1267. return ieee754sp_div(ieee754sp_one(0), s);
  1268. }
  1269. static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
  1270. {
  1271. return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
  1272. }
  1273. DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
  1274. DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
  1275. DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
  1276. DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
  1277. DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
  1278. DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
  1279. DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
  1280. DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
  1281. static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1282. mips_instruction ir, void *__user *fault_addr)
  1283. {
  1284. unsigned rcsr = 0; /* resulting csr */
  1285. MIPS_FPU_EMU_INC_STATS(cp1xops);
  1286. switch (MIPSInst_FMA_FFMT(ir)) {
  1287. case s_fmt:{ /* 0 */
  1288. ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
  1289. ieee754sp fd, fr, fs, ft;
  1290. u32 __user *va;
  1291. u32 val;
  1292. switch (MIPSInst_FUNC(ir)) {
  1293. case lwxc1_op:
  1294. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1295. xcp->regs[MIPSInst_FT(ir)]);
  1296. MIPS_FPU_EMU_INC_STATS(loads);
  1297. if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
  1298. MIPS_FPU_EMU_INC_STATS(errors);
  1299. *fault_addr = va;
  1300. return SIGBUS;
  1301. }
  1302. if (__get_user(val, va)) {
  1303. MIPS_FPU_EMU_INC_STATS(errors);
  1304. *fault_addr = va;
  1305. return SIGSEGV;
  1306. }
  1307. SITOREG(val, MIPSInst_FD(ir));
  1308. break;
  1309. case swxc1_op:
  1310. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1311. xcp->regs[MIPSInst_FT(ir)]);
  1312. MIPS_FPU_EMU_INC_STATS(stores);
  1313. SIFROMREG(val, MIPSInst_FS(ir));
  1314. if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
  1315. MIPS_FPU_EMU_INC_STATS(errors);
  1316. *fault_addr = va;
  1317. return SIGBUS;
  1318. }
  1319. if (put_user(val, va)) {
  1320. MIPS_FPU_EMU_INC_STATS(errors);
  1321. *fault_addr = va;
  1322. return SIGSEGV;
  1323. }
  1324. break;
  1325. case madd_s_op:
  1326. handler = fpemu_sp_madd;
  1327. goto scoptop;
  1328. case msub_s_op:
  1329. handler = fpemu_sp_msub;
  1330. goto scoptop;
  1331. case nmadd_s_op:
  1332. handler = fpemu_sp_nmadd;
  1333. goto scoptop;
  1334. case nmsub_s_op:
  1335. handler = fpemu_sp_nmsub;
  1336. goto scoptop;
  1337. scoptop:
  1338. SPFROMREG(fr, MIPSInst_FR(ir));
  1339. SPFROMREG(fs, MIPSInst_FS(ir));
  1340. SPFROMREG(ft, MIPSInst_FT(ir));
  1341. fd = (*handler) (fr, fs, ft);
  1342. SPTOREG(fd, MIPSInst_FD(ir));
  1343. copcsr:
  1344. if (ieee754_cxtest(IEEE754_INEXACT))
  1345. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1346. if (ieee754_cxtest(IEEE754_UNDERFLOW))
  1347. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1348. if (ieee754_cxtest(IEEE754_OVERFLOW))
  1349. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1350. if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
  1351. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1352. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  1353. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1354. /*printk ("SIGFPE: fpu csr = %08x\n",
  1355. ctx->fcr31); */
  1356. return SIGFPE;
  1357. }
  1358. break;
  1359. default:
  1360. return SIGILL;
  1361. }
  1362. break;
  1363. }
  1364. case d_fmt:{ /* 1 */
  1365. ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
  1366. ieee754dp fd, fr, fs, ft;
  1367. u64 __user *va;
  1368. u64 val;
  1369. switch (MIPSInst_FUNC(ir)) {
  1370. case ldxc1_op:
  1371. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1372. xcp->regs[MIPSInst_FT(ir)]);
  1373. MIPS_FPU_EMU_INC_STATS(loads);
  1374. if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
  1375. MIPS_FPU_EMU_INC_STATS(errors);
  1376. *fault_addr = va;
  1377. return SIGBUS;
  1378. }
  1379. if (__get_user(val, va)) {
  1380. MIPS_FPU_EMU_INC_STATS(errors);
  1381. *fault_addr = va;
  1382. return SIGSEGV;
  1383. }
  1384. DITOREG(val, MIPSInst_FD(ir));
  1385. break;
  1386. case sdxc1_op:
  1387. va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
  1388. xcp->regs[MIPSInst_FT(ir)]);
  1389. MIPS_FPU_EMU_INC_STATS(stores);
  1390. DIFROMREG(val, MIPSInst_FS(ir));
  1391. if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
  1392. MIPS_FPU_EMU_INC_STATS(errors);
  1393. *fault_addr = va;
  1394. return SIGBUS;
  1395. }
  1396. if (__put_user(val, va)) {
  1397. MIPS_FPU_EMU_INC_STATS(errors);
  1398. *fault_addr = va;
  1399. return SIGSEGV;
  1400. }
  1401. break;
  1402. case madd_d_op:
  1403. handler = fpemu_dp_madd;
  1404. goto dcoptop;
  1405. case msub_d_op:
  1406. handler = fpemu_dp_msub;
  1407. goto dcoptop;
  1408. case nmadd_d_op:
  1409. handler = fpemu_dp_nmadd;
  1410. goto dcoptop;
  1411. case nmsub_d_op:
  1412. handler = fpemu_dp_nmsub;
  1413. goto dcoptop;
  1414. dcoptop:
  1415. DPFROMREG(fr, MIPSInst_FR(ir));
  1416. DPFROMREG(fs, MIPSInst_FS(ir));
  1417. DPFROMREG(ft, MIPSInst_FT(ir));
  1418. fd = (*handler) (fr, fs, ft);
  1419. DPTOREG(fd, MIPSInst_FD(ir));
  1420. goto copcsr;
  1421. default:
  1422. return SIGILL;
  1423. }
  1424. break;
  1425. }
  1426. case 0x3:
  1427. if (MIPSInst_FUNC(ir) != pfetch_op)
  1428. return SIGILL;
  1429. /* ignore prefx operation */
  1430. break;
  1431. default:
  1432. return SIGILL;
  1433. }
  1434. return 0;
  1435. }
  1436. #endif
  1437. /*
  1438. * Emulate a single COP1 arithmetic instruction.
  1439. */
  1440. static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1441. mips_instruction ir)
  1442. {
  1443. int rfmt; /* resulting format */
  1444. unsigned rcsr = 0; /* resulting csr */
  1445. unsigned cond;
  1446. union {
  1447. ieee754dp d;
  1448. ieee754sp s;
  1449. int w;
  1450. #ifdef __mips64
  1451. s64 l;
  1452. #endif
  1453. } rv; /* resulting value */
  1454. MIPS_FPU_EMU_INC_STATS(cp1ops);
  1455. switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
  1456. case s_fmt:{ /* 0 */
  1457. union {
  1458. ieee754sp(*b) (ieee754sp, ieee754sp);
  1459. ieee754sp(*u) (ieee754sp);
  1460. } handler;
  1461. switch (MIPSInst_FUNC(ir)) {
  1462. /* binary ops */
  1463. case fadd_op:
  1464. handler.b = ieee754sp_add;
  1465. goto scopbop;
  1466. case fsub_op:
  1467. handler.b = ieee754sp_sub;
  1468. goto scopbop;
  1469. case fmul_op:
  1470. handler.b = ieee754sp_mul;
  1471. goto scopbop;
  1472. case fdiv_op:
  1473. handler.b = ieee754sp_div;
  1474. goto scopbop;
  1475. /* unary ops */
  1476. #if __mips >= 2 || defined(__mips64)
  1477. case fsqrt_op:
  1478. handler.u = ieee754sp_sqrt;
  1479. goto scopuop;
  1480. #endif
  1481. #if __mips >= 4 && __mips != 32
  1482. case frsqrt_op:
  1483. handler.u = fpemu_sp_rsqrt;
  1484. goto scopuop;
  1485. case frecip_op:
  1486. handler.u = fpemu_sp_recip;
  1487. goto scopuop;
  1488. #endif
  1489. #if __mips >= 4
  1490. case fmovc_op:
  1491. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1492. if (((ctx->fcr31 & cond) != 0) !=
  1493. ((MIPSInst_FT(ir) & 1) != 0))
  1494. return 0;
  1495. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1496. break;
  1497. case fmovz_op:
  1498. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1499. return 0;
  1500. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1501. break;
  1502. case fmovn_op:
  1503. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1504. return 0;
  1505. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1506. break;
  1507. #endif
  1508. case fabs_op:
  1509. handler.u = ieee754sp_abs;
  1510. goto scopuop;
  1511. case fneg_op:
  1512. handler.u = ieee754sp_neg;
  1513. goto scopuop;
  1514. case fmov_op:
  1515. /* an easy one */
  1516. SPFROMREG(rv.s, MIPSInst_FS(ir));
  1517. goto copcsr;
  1518. /* binary op on handler */
  1519. scopbop:
  1520. {
  1521. ieee754sp fs, ft;
  1522. SPFROMREG(fs, MIPSInst_FS(ir));
  1523. SPFROMREG(ft, MIPSInst_FT(ir));
  1524. rv.s = (*handler.b) (fs, ft);
  1525. goto copcsr;
  1526. }
  1527. scopuop:
  1528. {
  1529. ieee754sp fs;
  1530. SPFROMREG(fs, MIPSInst_FS(ir));
  1531. rv.s = (*handler.u) (fs);
  1532. goto copcsr;
  1533. }
  1534. copcsr:
  1535. if (ieee754_cxtest(IEEE754_INEXACT))
  1536. rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
  1537. if (ieee754_cxtest(IEEE754_UNDERFLOW))
  1538. rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
  1539. if (ieee754_cxtest(IEEE754_OVERFLOW))
  1540. rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
  1541. if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
  1542. rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
  1543. if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
  1544. rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
  1545. break;
  1546. /* unary conv ops */
  1547. case fcvts_op:
  1548. return SIGILL; /* not defined */
  1549. case fcvtd_op:{
  1550. ieee754sp fs;
  1551. SPFROMREG(fs, MIPSInst_FS(ir));
  1552. rv.d = ieee754dp_fsp(fs);
  1553. rfmt = d_fmt;
  1554. goto copcsr;
  1555. }
  1556. case fcvtw_op:{
  1557. ieee754sp fs;
  1558. SPFROMREG(fs, MIPSInst_FS(ir));
  1559. rv.w = ieee754sp_tint(fs);
  1560. rfmt = w_fmt;
  1561. goto copcsr;
  1562. }
  1563. #if __mips >= 2 || defined(__mips64)
  1564. case fround_op:
  1565. case ftrunc_op:
  1566. case fceil_op:
  1567. case ffloor_op:{
  1568. unsigned int oldrm = ieee754_csr.rm;
  1569. ieee754sp fs;
  1570. SPFROMREG(fs, MIPSInst_FS(ir));
  1571. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1572. rv.w = ieee754sp_tint(fs);
  1573. ieee754_csr.rm = oldrm;
  1574. rfmt = w_fmt;
  1575. goto copcsr;
  1576. }
  1577. #endif /* __mips >= 2 */
  1578. #if defined(__mips64)
  1579. case fcvtl_op:{
  1580. ieee754sp fs;
  1581. SPFROMREG(fs, MIPSInst_FS(ir));
  1582. rv.l = ieee754sp_tlong(fs);
  1583. rfmt = l_fmt;
  1584. goto copcsr;
  1585. }
  1586. case froundl_op:
  1587. case ftruncl_op:
  1588. case fceill_op:
  1589. case ffloorl_op:{
  1590. unsigned int oldrm = ieee754_csr.rm;
  1591. ieee754sp fs;
  1592. SPFROMREG(fs, MIPSInst_FS(ir));
  1593. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1594. rv.l = ieee754sp_tlong(fs);
  1595. ieee754_csr.rm = oldrm;
  1596. rfmt = l_fmt;
  1597. goto copcsr;
  1598. }
  1599. #endif /* defined(__mips64) */
  1600. default:
  1601. if (MIPSInst_FUNC(ir) >= fcmp_op) {
  1602. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  1603. ieee754sp fs, ft;
  1604. SPFROMREG(fs, MIPSInst_FS(ir));
  1605. SPFROMREG(ft, MIPSInst_FT(ir));
  1606. rv.w = ieee754sp_cmp(fs, ft,
  1607. cmptab[cmpop & 0x7], cmpop & 0x8);
  1608. rfmt = -1;
  1609. if ((cmpop & 0x8) && ieee754_cxtest
  1610. (IEEE754_INVALID_OPERATION))
  1611. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  1612. else
  1613. goto copcsr;
  1614. }
  1615. else {
  1616. return SIGILL;
  1617. }
  1618. break;
  1619. }
  1620. break;
  1621. }
  1622. case d_fmt:{
  1623. union {
  1624. ieee754dp(*b) (ieee754dp, ieee754dp);
  1625. ieee754dp(*u) (ieee754dp);
  1626. } handler;
  1627. switch (MIPSInst_FUNC(ir)) {
  1628. /* binary ops */
  1629. case fadd_op:
  1630. handler.b = ieee754dp_add;
  1631. goto dcopbop;
  1632. case fsub_op:
  1633. handler.b = ieee754dp_sub;
  1634. goto dcopbop;
  1635. case fmul_op:
  1636. handler.b = ieee754dp_mul;
  1637. goto dcopbop;
  1638. case fdiv_op:
  1639. handler.b = ieee754dp_div;
  1640. goto dcopbop;
  1641. /* unary ops */
  1642. #if __mips >= 2 || defined(__mips64)
  1643. case fsqrt_op:
  1644. handler.u = ieee754dp_sqrt;
  1645. goto dcopuop;
  1646. #endif
  1647. #if __mips >= 4 && __mips != 32
  1648. case frsqrt_op:
  1649. handler.u = fpemu_dp_rsqrt;
  1650. goto dcopuop;
  1651. case frecip_op:
  1652. handler.u = fpemu_dp_recip;
  1653. goto dcopuop;
  1654. #endif
  1655. #if __mips >= 4
  1656. case fmovc_op:
  1657. cond = fpucondbit[MIPSInst_FT(ir) >> 2];
  1658. if (((ctx->fcr31 & cond) != 0) !=
  1659. ((MIPSInst_FT(ir) & 1) != 0))
  1660. return 0;
  1661. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1662. break;
  1663. case fmovz_op:
  1664. if (xcp->regs[MIPSInst_FT(ir)] != 0)
  1665. return 0;
  1666. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1667. break;
  1668. case fmovn_op:
  1669. if (xcp->regs[MIPSInst_FT(ir)] == 0)
  1670. return 0;
  1671. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1672. break;
  1673. #endif
  1674. case fabs_op:
  1675. handler.u = ieee754dp_abs;
  1676. goto dcopuop;
  1677. case fneg_op:
  1678. handler.u = ieee754dp_neg;
  1679. goto dcopuop;
  1680. case fmov_op:
  1681. /* an easy one */
  1682. DPFROMREG(rv.d, MIPSInst_FS(ir));
  1683. goto copcsr;
  1684. /* binary op on handler */
  1685. dcopbop:{
  1686. ieee754dp fs, ft;
  1687. DPFROMREG(fs, MIPSInst_FS(ir));
  1688. DPFROMREG(ft, MIPSInst_FT(ir));
  1689. rv.d = (*handler.b) (fs, ft);
  1690. goto copcsr;
  1691. }
  1692. dcopuop:{
  1693. ieee754dp fs;
  1694. DPFROMREG(fs, MIPSInst_FS(ir));
  1695. rv.d = (*handler.u) (fs);
  1696. goto copcsr;
  1697. }
  1698. /* unary conv ops */
  1699. case fcvts_op:{
  1700. ieee754dp fs;
  1701. DPFROMREG(fs, MIPSInst_FS(ir));
  1702. rv.s = ieee754sp_fdp(fs);
  1703. rfmt = s_fmt;
  1704. goto copcsr;
  1705. }
  1706. case fcvtd_op:
  1707. return SIGILL; /* not defined */
  1708. case fcvtw_op:{
  1709. ieee754dp fs;
  1710. DPFROMREG(fs, MIPSInst_FS(ir));
  1711. rv.w = ieee754dp_tint(fs); /* wrong */
  1712. rfmt = w_fmt;
  1713. goto copcsr;
  1714. }
  1715. #if __mips >= 2 || defined(__mips64)
  1716. case fround_op:
  1717. case ftrunc_op:
  1718. case fceil_op:
  1719. case ffloor_op:{
  1720. unsigned int oldrm = ieee754_csr.rm;
  1721. ieee754dp fs;
  1722. DPFROMREG(fs, MIPSInst_FS(ir));
  1723. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1724. rv.w = ieee754dp_tint(fs);
  1725. ieee754_csr.rm = oldrm;
  1726. rfmt = w_fmt;
  1727. goto copcsr;
  1728. }
  1729. #endif
  1730. #if defined(__mips64)
  1731. case fcvtl_op:{
  1732. ieee754dp fs;
  1733. DPFROMREG(fs, MIPSInst_FS(ir));
  1734. rv.l = ieee754dp_tlong(fs);
  1735. rfmt = l_fmt;
  1736. goto copcsr;
  1737. }
  1738. case froundl_op:
  1739. case ftruncl_op:
  1740. case fceill_op:
  1741. case ffloorl_op:{
  1742. unsigned int oldrm = ieee754_csr.rm;
  1743. ieee754dp fs;
  1744. DPFROMREG(fs, MIPSInst_FS(ir));
  1745. ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
  1746. rv.l = ieee754dp_tlong(fs);
  1747. ieee754_csr.rm = oldrm;
  1748. rfmt = l_fmt;
  1749. goto copcsr;
  1750. }
  1751. #endif /* __mips >= 3 */
  1752. default:
  1753. if (MIPSInst_FUNC(ir) >= fcmp_op) {
  1754. unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
  1755. ieee754dp fs, ft;
  1756. DPFROMREG(fs, MIPSInst_FS(ir));
  1757. DPFROMREG(ft, MIPSInst_FT(ir));
  1758. rv.w = ieee754dp_cmp(fs, ft,
  1759. cmptab[cmpop & 0x7], cmpop & 0x8);
  1760. rfmt = -1;
  1761. if ((cmpop & 0x8)
  1762. &&
  1763. ieee754_cxtest
  1764. (IEEE754_INVALID_OPERATION))
  1765. rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
  1766. else
  1767. goto copcsr;
  1768. }
  1769. else {
  1770. return SIGILL;
  1771. }
  1772. break;
  1773. }
  1774. break;
  1775. }
  1776. case w_fmt:{
  1777. ieee754sp fs;
  1778. switch (MIPSInst_FUNC(ir)) {
  1779. case fcvts_op:
  1780. /* convert word to single precision real */
  1781. SPFROMREG(fs, MIPSInst_FS(ir));
  1782. rv.s = ieee754sp_fint(fs.bits);
  1783. rfmt = s_fmt;
  1784. goto copcsr;
  1785. case fcvtd_op:
  1786. /* convert word to double precision real */
  1787. SPFROMREG(fs, MIPSInst_FS(ir));
  1788. rv.d = ieee754dp_fint(fs.bits);
  1789. rfmt = d_fmt;
  1790. goto copcsr;
  1791. default:
  1792. return SIGILL;
  1793. }
  1794. break;
  1795. }
  1796. #if defined(__mips64)
  1797. case l_fmt:{
  1798. switch (MIPSInst_FUNC(ir)) {
  1799. case fcvts_op:
  1800. /* convert long to single precision real */
  1801. rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
  1802. rfmt = s_fmt;
  1803. goto copcsr;
  1804. case fcvtd_op:
  1805. /* convert long to double precision real */
  1806. rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
  1807. rfmt = d_fmt;
  1808. goto copcsr;
  1809. default:
  1810. return SIGILL;
  1811. }
  1812. break;
  1813. }
  1814. #endif
  1815. default:
  1816. return SIGILL;
  1817. }
  1818. /*
  1819. * Update the fpu CSR register for this operation.
  1820. * If an exception is required, generate a tidy SIGFPE exception,
  1821. * without updating the result register.
  1822. * Note: cause exception bits do not accumulate, they are rewritten
  1823. * for each op; only the flag/sticky bits accumulate.
  1824. */
  1825. ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
  1826. if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
  1827. /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
  1828. return SIGFPE;
  1829. }
  1830. /*
  1831. * Now we can safely write the result back to the register file.
  1832. */
  1833. switch (rfmt) {
  1834. case -1:{
  1835. #if __mips >= 4
  1836. cond = fpucondbit[MIPSInst_FD(ir) >> 2];
  1837. #else
  1838. cond = FPU_CSR_COND;
  1839. #endif
  1840. if (rv.w)
  1841. ctx->fcr31 |= cond;
  1842. else
  1843. ctx->fcr31 &= ~cond;
  1844. break;
  1845. }
  1846. case d_fmt:
  1847. DPTOREG(rv.d, MIPSInst_FD(ir));
  1848. break;
  1849. case s_fmt:
  1850. SPTOREG(rv.s, MIPSInst_FD(ir));
  1851. break;
  1852. case w_fmt:
  1853. SITOREG(rv.w, MIPSInst_FD(ir));
  1854. break;
  1855. #if defined(__mips64)
  1856. case l_fmt:
  1857. DITOREG(rv.l, MIPSInst_FD(ir));
  1858. break;
  1859. #endif
  1860. default:
  1861. return SIGILL;
  1862. }
  1863. return 0;
  1864. }
  1865. int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
  1866. int has_fpu, void *__user *fault_addr)
  1867. {
  1868. unsigned long oldepc, prevepc;
  1869. struct mm_decoded_insn dec_insn;
  1870. u16 instr[4];
  1871. u16 *instr_ptr;
  1872. int sig = 0;
  1873. oldepc = xcp->cp0_epc;
  1874. do {
  1875. prevepc = xcp->cp0_epc;
  1876. if (get_isa16_mode(prevepc) && cpu_has_mmips) {
  1877. /*
  1878. * Get next 2 microMIPS instructions and convert them
  1879. * into 32-bit instructions.
  1880. */
  1881. if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
  1882. (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
  1883. (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
  1884. (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
  1885. MIPS_FPU_EMU_INC_STATS(errors);
  1886. return SIGBUS;
  1887. }
  1888. instr_ptr = instr;
  1889. /* Get first instruction. */
  1890. if (mm_insn_16bit(*instr_ptr)) {
  1891. /* Duplicate the half-word. */
  1892. dec_insn.insn = (*instr_ptr << 16) |
  1893. (*instr_ptr);
  1894. /* 16-bit instruction. */
  1895. dec_insn.pc_inc = 2;
  1896. instr_ptr += 1;
  1897. } else {
  1898. dec_insn.insn = (*instr_ptr << 16) |
  1899. *(instr_ptr+1);
  1900. /* 32-bit instruction. */
  1901. dec_insn.pc_inc = 4;
  1902. instr_ptr += 2;
  1903. }
  1904. /* Get second instruction. */
  1905. if (mm_insn_16bit(*instr_ptr)) {
  1906. /* Duplicate the half-word. */
  1907. dec_insn.next_insn = (*instr_ptr << 16) |
  1908. (*instr_ptr);
  1909. /* 16-bit instruction. */
  1910. dec_insn.next_pc_inc = 2;
  1911. } else {
  1912. dec_insn.next_insn = (*instr_ptr << 16) |
  1913. *(instr_ptr+1);
  1914. /* 32-bit instruction. */
  1915. dec_insn.next_pc_inc = 4;
  1916. }
  1917. dec_insn.micro_mips_mode = 1;
  1918. } else {
  1919. if ((get_user(dec_insn.insn,
  1920. (mips_instruction __user *) xcp->cp0_epc)) ||
  1921. (get_user(dec_insn.next_insn,
  1922. (mips_instruction __user *)(xcp->cp0_epc+4)))) {
  1923. MIPS_FPU_EMU_INC_STATS(errors);
  1924. return SIGBUS;
  1925. }
  1926. dec_insn.pc_inc = 4;
  1927. dec_insn.next_pc_inc = 4;
  1928. dec_insn.micro_mips_mode = 0;
  1929. }
  1930. if ((dec_insn.insn == 0) ||
  1931. ((dec_insn.pc_inc == 2) &&
  1932. ((dec_insn.insn & 0xffff) == MM_NOP16)))
  1933. xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
  1934. else {
  1935. /*
  1936. * The 'ieee754_csr' is an alias of
  1937. * ctx->fcr31. No need to copy ctx->fcr31 to
  1938. * ieee754_csr. But ieee754_csr.rm is ieee
  1939. * library modes. (not mips rounding mode)
  1940. */
  1941. /* convert to ieee library modes */
  1942. ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
  1943. sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
  1944. /* revert to mips rounding mode */
  1945. ieee754_csr.rm = mips_rm[ieee754_csr.rm];
  1946. }
  1947. if (has_fpu)
  1948. break;
  1949. if (sig)
  1950. break;
  1951. cond_resched();
  1952. } while (xcp->cp0_epc > prevepc);
  1953. /* SIGILL indicates a non-fpu instruction */
  1954. if (sig == SIGILL && xcp->cp0_epc != oldepc)
  1955. /* but if epc has advanced, then ignore it */
  1956. sig = 0;
  1957. return sig;
  1958. }
  1959. #ifdef CONFIG_DEBUG_FS
  1960. static int fpuemu_stat_get(void *data, u64 *val)
  1961. {
  1962. int cpu;
  1963. unsigned long sum = 0;
  1964. for_each_online_cpu(cpu) {
  1965. struct mips_fpu_emulator_stats *ps;
  1966. local_t *pv;
  1967. ps = &per_cpu(fpuemustats, cpu);
  1968. pv = (void *)ps + (unsigned long)data;
  1969. sum += local_read(pv);
  1970. }
  1971. *val = sum;
  1972. return 0;
  1973. }
  1974. DEFINE_SIMPLE_ATTRIBUTE(fops_fpuemu_stat, fpuemu_stat_get, NULL, "%llu\n");
  1975. extern struct dentry *mips_debugfs_dir;
  1976. static int __init debugfs_fpuemu(void)
  1977. {
  1978. struct dentry *d, *dir;
  1979. if (!mips_debugfs_dir)
  1980. return -ENODEV;
  1981. dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir);
  1982. if (!dir)
  1983. return -ENOMEM;
  1984. #define FPU_STAT_CREATE(M) \
  1985. do { \
  1986. d = debugfs_create_file(#M , S_IRUGO, dir, \
  1987. (void *)offsetof(struct mips_fpu_emulator_stats, M), \
  1988. &fops_fpuemu_stat); \
  1989. if (!d) \
  1990. return -ENOMEM; \
  1991. } while (0)
  1992. FPU_STAT_CREATE(emulated);
  1993. FPU_STAT_CREATE(loads);
  1994. FPU_STAT_CREATE(stores);
  1995. FPU_STAT_CREATE(cp1ops);
  1996. FPU_STAT_CREATE(cp1xops);
  1997. FPU_STAT_CREATE(errors);
  1998. return 0;
  1999. }
  2000. __initcall(debugfs_fpuemu);
  2001. #endif