cp1emu.c 54 KB

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