cp1emu.c 52 KB

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