cp1emu.c 52 KB

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