book3s_paired_singles.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright Novell Inc 2010
  16. *
  17. * Authors: Alexander Graf <agraf@suse.de>
  18. */
  19. #include <asm/kvm.h>
  20. #include <asm/kvm_ppc.h>
  21. #include <asm/disassemble.h>
  22. #include <asm/kvm_book3s.h>
  23. #include <asm/kvm_fpu.h>
  24. #include <asm/reg.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/switch_to.h>
  27. #include <linux/vmalloc.h>
  28. /* #define DEBUG */
  29. #ifdef DEBUG
  30. #define dprintk printk
  31. #else
  32. #define dprintk(...) do { } while(0);
  33. #endif
  34. #define OP_LFS 48
  35. #define OP_LFSU 49
  36. #define OP_LFD 50
  37. #define OP_LFDU 51
  38. #define OP_STFS 52
  39. #define OP_STFSU 53
  40. #define OP_STFD 54
  41. #define OP_STFDU 55
  42. #define OP_PSQ_L 56
  43. #define OP_PSQ_LU 57
  44. #define OP_PSQ_ST 60
  45. #define OP_PSQ_STU 61
  46. #define OP_31_LFSX 535
  47. #define OP_31_LFSUX 567
  48. #define OP_31_LFDX 599
  49. #define OP_31_LFDUX 631
  50. #define OP_31_STFSX 663
  51. #define OP_31_STFSUX 695
  52. #define OP_31_STFX 727
  53. #define OP_31_STFUX 759
  54. #define OP_31_LWIZX 887
  55. #define OP_31_STFIWX 983
  56. #define OP_59_FADDS 21
  57. #define OP_59_FSUBS 20
  58. #define OP_59_FSQRTS 22
  59. #define OP_59_FDIVS 18
  60. #define OP_59_FRES 24
  61. #define OP_59_FMULS 25
  62. #define OP_59_FRSQRTES 26
  63. #define OP_59_FMSUBS 28
  64. #define OP_59_FMADDS 29
  65. #define OP_59_FNMSUBS 30
  66. #define OP_59_FNMADDS 31
  67. #define OP_63_FCMPU 0
  68. #define OP_63_FCPSGN 8
  69. #define OP_63_FRSP 12
  70. #define OP_63_FCTIW 14
  71. #define OP_63_FCTIWZ 15
  72. #define OP_63_FDIV 18
  73. #define OP_63_FADD 21
  74. #define OP_63_FSQRT 22
  75. #define OP_63_FSEL 23
  76. #define OP_63_FRE 24
  77. #define OP_63_FMUL 25
  78. #define OP_63_FRSQRTE 26
  79. #define OP_63_FMSUB 28
  80. #define OP_63_FMADD 29
  81. #define OP_63_FNMSUB 30
  82. #define OP_63_FNMADD 31
  83. #define OP_63_FCMPO 32
  84. #define OP_63_MTFSB1 38 // XXX
  85. #define OP_63_FSUB 20
  86. #define OP_63_FNEG 40
  87. #define OP_63_MCRFS 64
  88. #define OP_63_MTFSB0 70
  89. #define OP_63_FMR 72
  90. #define OP_63_MTFSFI 134
  91. #define OP_63_FABS 264
  92. #define OP_63_MFFS 583
  93. #define OP_63_MTFSF 711
  94. #define OP_4X_PS_CMPU0 0
  95. #define OP_4X_PSQ_LX 6
  96. #define OP_4XW_PSQ_STX 7
  97. #define OP_4A_PS_SUM0 10
  98. #define OP_4A_PS_SUM1 11
  99. #define OP_4A_PS_MULS0 12
  100. #define OP_4A_PS_MULS1 13
  101. #define OP_4A_PS_MADDS0 14
  102. #define OP_4A_PS_MADDS1 15
  103. #define OP_4A_PS_DIV 18
  104. #define OP_4A_PS_SUB 20
  105. #define OP_4A_PS_ADD 21
  106. #define OP_4A_PS_SEL 23
  107. #define OP_4A_PS_RES 24
  108. #define OP_4A_PS_MUL 25
  109. #define OP_4A_PS_RSQRTE 26
  110. #define OP_4A_PS_MSUB 28
  111. #define OP_4A_PS_MADD 29
  112. #define OP_4A_PS_NMSUB 30
  113. #define OP_4A_PS_NMADD 31
  114. #define OP_4X_PS_CMPO0 32
  115. #define OP_4X_PSQ_LUX 38
  116. #define OP_4XW_PSQ_STUX 39
  117. #define OP_4X_PS_NEG 40
  118. #define OP_4X_PS_CMPU1 64
  119. #define OP_4X_PS_MR 72
  120. #define OP_4X_PS_CMPO1 96
  121. #define OP_4X_PS_NABS 136
  122. #define OP_4X_PS_ABS 264
  123. #define OP_4X_PS_MERGE00 528
  124. #define OP_4X_PS_MERGE01 560
  125. #define OP_4X_PS_MERGE10 592
  126. #define OP_4X_PS_MERGE11 624
  127. #define SCALAR_NONE 0
  128. #define SCALAR_HIGH (1 << 0)
  129. #define SCALAR_LOW (1 << 1)
  130. #define SCALAR_NO_PS0 (1 << 2)
  131. #define SCALAR_NO_PS1 (1 << 3)
  132. #define GQR_ST_TYPE_MASK 0x00000007
  133. #define GQR_ST_TYPE_SHIFT 0
  134. #define GQR_ST_SCALE_MASK 0x00003f00
  135. #define GQR_ST_SCALE_SHIFT 8
  136. #define GQR_LD_TYPE_MASK 0x00070000
  137. #define GQR_LD_TYPE_SHIFT 16
  138. #define GQR_LD_SCALE_MASK 0x3f000000
  139. #define GQR_LD_SCALE_SHIFT 24
  140. #define GQR_QUANTIZE_FLOAT 0
  141. #define GQR_QUANTIZE_U8 4
  142. #define GQR_QUANTIZE_U16 5
  143. #define GQR_QUANTIZE_S8 6
  144. #define GQR_QUANTIZE_S16 7
  145. #define FPU_LS_SINGLE 0
  146. #define FPU_LS_DOUBLE 1
  147. #define FPU_LS_SINGLE_LOW 2
  148. static inline void kvmppc_sync_qpr(struct kvm_vcpu *vcpu, int rt)
  149. {
  150. kvm_cvt_df(&VCPU_FPR(vcpu, rt), &vcpu->arch.qpr[rt]);
  151. }
  152. static void kvmppc_inject_pf(struct kvm_vcpu *vcpu, ulong eaddr, bool is_store)
  153. {
  154. u32 dsisr;
  155. u64 msr = kvmppc_get_msr(vcpu);
  156. msr = kvmppc_set_field(msr, 33, 36, 0);
  157. msr = kvmppc_set_field(msr, 42, 47, 0);
  158. kvmppc_set_msr(vcpu, msr);
  159. kvmppc_set_dar(vcpu, eaddr);
  160. /* Page Fault */
  161. dsisr = kvmppc_set_field(0, 33, 33, 1);
  162. if (is_store)
  163. dsisr = kvmppc_set_field(dsisr, 38, 38, 1);
  164. kvmppc_set_dsisr(vcpu, dsisr);
  165. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DATA_STORAGE);
  166. }
  167. static int kvmppc_emulate_fpr_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  168. int rs, ulong addr, int ls_type)
  169. {
  170. int emulated = EMULATE_FAIL;
  171. int r;
  172. char tmp[8];
  173. int len = sizeof(u32);
  174. if (ls_type == FPU_LS_DOUBLE)
  175. len = sizeof(u64);
  176. /* read from memory */
  177. r = kvmppc_ld(vcpu, &addr, len, tmp, true);
  178. vcpu->arch.paddr_accessed = addr;
  179. if (r < 0) {
  180. kvmppc_inject_pf(vcpu, addr, false);
  181. goto done_load;
  182. } else if (r == EMULATE_DO_MMIO) {
  183. emulated = kvmppc_handle_load(run, vcpu, KVM_MMIO_REG_FPR | rs,
  184. len, 1);
  185. goto done_load;
  186. }
  187. emulated = EMULATE_DONE;
  188. /* put in registers */
  189. switch (ls_type) {
  190. case FPU_LS_SINGLE:
  191. kvm_cvt_fd((u32*)tmp, &VCPU_FPR(vcpu, rs));
  192. vcpu->arch.qpr[rs] = *((u32*)tmp);
  193. break;
  194. case FPU_LS_DOUBLE:
  195. VCPU_FPR(vcpu, rs) = *((u64*)tmp);
  196. break;
  197. }
  198. dprintk(KERN_INFO "KVM: FPR_LD [0x%llx] at 0x%lx (%d)\n", *(u64*)tmp,
  199. addr, len);
  200. done_load:
  201. return emulated;
  202. }
  203. static int kvmppc_emulate_fpr_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  204. int rs, ulong addr, int ls_type)
  205. {
  206. int emulated = EMULATE_FAIL;
  207. int r;
  208. char tmp[8];
  209. u64 val;
  210. int len;
  211. switch (ls_type) {
  212. case FPU_LS_SINGLE:
  213. kvm_cvt_df(&VCPU_FPR(vcpu, rs), (u32*)tmp);
  214. val = *((u32*)tmp);
  215. len = sizeof(u32);
  216. break;
  217. case FPU_LS_SINGLE_LOW:
  218. *((u32*)tmp) = VCPU_FPR(vcpu, rs);
  219. val = VCPU_FPR(vcpu, rs) & 0xffffffff;
  220. len = sizeof(u32);
  221. break;
  222. case FPU_LS_DOUBLE:
  223. *((u64*)tmp) = VCPU_FPR(vcpu, rs);
  224. val = VCPU_FPR(vcpu, rs);
  225. len = sizeof(u64);
  226. break;
  227. default:
  228. val = 0;
  229. len = 0;
  230. }
  231. r = kvmppc_st(vcpu, &addr, len, tmp, true);
  232. vcpu->arch.paddr_accessed = addr;
  233. if (r < 0) {
  234. kvmppc_inject_pf(vcpu, addr, true);
  235. } else if (r == EMULATE_DO_MMIO) {
  236. emulated = kvmppc_handle_store(run, vcpu, val, len, 1);
  237. } else {
  238. emulated = EMULATE_DONE;
  239. }
  240. dprintk(KERN_INFO "KVM: FPR_ST [0x%llx] at 0x%lx (%d)\n",
  241. val, addr, len);
  242. return emulated;
  243. }
  244. static int kvmppc_emulate_psq_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  245. int rs, ulong addr, bool w, int i)
  246. {
  247. int emulated = EMULATE_FAIL;
  248. int r;
  249. float one = 1.0;
  250. u32 tmp[2];
  251. /* read from memory */
  252. if (w) {
  253. r = kvmppc_ld(vcpu, &addr, sizeof(u32), tmp, true);
  254. memcpy(&tmp[1], &one, sizeof(u32));
  255. } else {
  256. r = kvmppc_ld(vcpu, &addr, sizeof(u32) * 2, tmp, true);
  257. }
  258. vcpu->arch.paddr_accessed = addr;
  259. if (r < 0) {
  260. kvmppc_inject_pf(vcpu, addr, false);
  261. goto done_load;
  262. } else if ((r == EMULATE_DO_MMIO) && w) {
  263. emulated = kvmppc_handle_load(run, vcpu, KVM_MMIO_REG_FPR | rs,
  264. 4, 1);
  265. vcpu->arch.qpr[rs] = tmp[1];
  266. goto done_load;
  267. } else if (r == EMULATE_DO_MMIO) {
  268. emulated = kvmppc_handle_load(run, vcpu, KVM_MMIO_REG_FQPR | rs,
  269. 8, 1);
  270. goto done_load;
  271. }
  272. emulated = EMULATE_DONE;
  273. /* put in registers */
  274. kvm_cvt_fd(&tmp[0], &VCPU_FPR(vcpu, rs));
  275. vcpu->arch.qpr[rs] = tmp[1];
  276. dprintk(KERN_INFO "KVM: PSQ_LD [0x%x, 0x%x] at 0x%lx (%d)\n", tmp[0],
  277. tmp[1], addr, w ? 4 : 8);
  278. done_load:
  279. return emulated;
  280. }
  281. static int kvmppc_emulate_psq_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  282. int rs, ulong addr, bool w, int i)
  283. {
  284. int emulated = EMULATE_FAIL;
  285. int r;
  286. u32 tmp[2];
  287. int len = w ? sizeof(u32) : sizeof(u64);
  288. kvm_cvt_df(&VCPU_FPR(vcpu, rs), &tmp[0]);
  289. tmp[1] = vcpu->arch.qpr[rs];
  290. r = kvmppc_st(vcpu, &addr, len, tmp, true);
  291. vcpu->arch.paddr_accessed = addr;
  292. if (r < 0) {
  293. kvmppc_inject_pf(vcpu, addr, true);
  294. } else if ((r == EMULATE_DO_MMIO) && w) {
  295. emulated = kvmppc_handle_store(run, vcpu, tmp[0], 4, 1);
  296. } else if (r == EMULATE_DO_MMIO) {
  297. u64 val = ((u64)tmp[0] << 32) | tmp[1];
  298. emulated = kvmppc_handle_store(run, vcpu, val, 8, 1);
  299. } else {
  300. emulated = EMULATE_DONE;
  301. }
  302. dprintk(KERN_INFO "KVM: PSQ_ST [0x%x, 0x%x] at 0x%lx (%d)\n",
  303. tmp[0], tmp[1], addr, len);
  304. return emulated;
  305. }
  306. /*
  307. * Cuts out inst bits with ordering according to spec.
  308. * That means the leftmost bit is zero. All given bits are included.
  309. */
  310. static inline u32 inst_get_field(u32 inst, int msb, int lsb)
  311. {
  312. return kvmppc_get_field(inst, msb + 32, lsb + 32);
  313. }
  314. static bool kvmppc_inst_is_paired_single(struct kvm_vcpu *vcpu, u32 inst)
  315. {
  316. if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
  317. return false;
  318. switch (get_op(inst)) {
  319. case OP_PSQ_L:
  320. case OP_PSQ_LU:
  321. case OP_PSQ_ST:
  322. case OP_PSQ_STU:
  323. case OP_LFS:
  324. case OP_LFSU:
  325. case OP_LFD:
  326. case OP_LFDU:
  327. case OP_STFS:
  328. case OP_STFSU:
  329. case OP_STFD:
  330. case OP_STFDU:
  331. return true;
  332. case 4:
  333. /* X form */
  334. switch (inst_get_field(inst, 21, 30)) {
  335. case OP_4X_PS_CMPU0:
  336. case OP_4X_PSQ_LX:
  337. case OP_4X_PS_CMPO0:
  338. case OP_4X_PSQ_LUX:
  339. case OP_4X_PS_NEG:
  340. case OP_4X_PS_CMPU1:
  341. case OP_4X_PS_MR:
  342. case OP_4X_PS_CMPO1:
  343. case OP_4X_PS_NABS:
  344. case OP_4X_PS_ABS:
  345. case OP_4X_PS_MERGE00:
  346. case OP_4X_PS_MERGE01:
  347. case OP_4X_PS_MERGE10:
  348. case OP_4X_PS_MERGE11:
  349. return true;
  350. }
  351. /* XW form */
  352. switch (inst_get_field(inst, 25, 30)) {
  353. case OP_4XW_PSQ_STX:
  354. case OP_4XW_PSQ_STUX:
  355. return true;
  356. }
  357. /* A form */
  358. switch (inst_get_field(inst, 26, 30)) {
  359. case OP_4A_PS_SUM1:
  360. case OP_4A_PS_SUM0:
  361. case OP_4A_PS_MULS0:
  362. case OP_4A_PS_MULS1:
  363. case OP_4A_PS_MADDS0:
  364. case OP_4A_PS_MADDS1:
  365. case OP_4A_PS_DIV:
  366. case OP_4A_PS_SUB:
  367. case OP_4A_PS_ADD:
  368. case OP_4A_PS_SEL:
  369. case OP_4A_PS_RES:
  370. case OP_4A_PS_MUL:
  371. case OP_4A_PS_RSQRTE:
  372. case OP_4A_PS_MSUB:
  373. case OP_4A_PS_MADD:
  374. case OP_4A_PS_NMSUB:
  375. case OP_4A_PS_NMADD:
  376. return true;
  377. }
  378. break;
  379. case 59:
  380. switch (inst_get_field(inst, 21, 30)) {
  381. case OP_59_FADDS:
  382. case OP_59_FSUBS:
  383. case OP_59_FDIVS:
  384. case OP_59_FRES:
  385. case OP_59_FRSQRTES:
  386. return true;
  387. }
  388. switch (inst_get_field(inst, 26, 30)) {
  389. case OP_59_FMULS:
  390. case OP_59_FMSUBS:
  391. case OP_59_FMADDS:
  392. case OP_59_FNMSUBS:
  393. case OP_59_FNMADDS:
  394. return true;
  395. }
  396. break;
  397. case 63:
  398. switch (inst_get_field(inst, 21, 30)) {
  399. case OP_63_MTFSB0:
  400. case OP_63_MTFSB1:
  401. case OP_63_MTFSF:
  402. case OP_63_MTFSFI:
  403. case OP_63_MCRFS:
  404. case OP_63_MFFS:
  405. case OP_63_FCMPU:
  406. case OP_63_FCMPO:
  407. case OP_63_FNEG:
  408. case OP_63_FMR:
  409. case OP_63_FABS:
  410. case OP_63_FRSP:
  411. case OP_63_FDIV:
  412. case OP_63_FADD:
  413. case OP_63_FSUB:
  414. case OP_63_FCTIW:
  415. case OP_63_FCTIWZ:
  416. case OP_63_FRSQRTE:
  417. case OP_63_FCPSGN:
  418. return true;
  419. }
  420. switch (inst_get_field(inst, 26, 30)) {
  421. case OP_63_FMUL:
  422. case OP_63_FSEL:
  423. case OP_63_FMSUB:
  424. case OP_63_FMADD:
  425. case OP_63_FNMSUB:
  426. case OP_63_FNMADD:
  427. return true;
  428. }
  429. break;
  430. case 31:
  431. switch (inst_get_field(inst, 21, 30)) {
  432. case OP_31_LFSX:
  433. case OP_31_LFSUX:
  434. case OP_31_LFDX:
  435. case OP_31_LFDUX:
  436. case OP_31_STFSX:
  437. case OP_31_STFSUX:
  438. case OP_31_STFX:
  439. case OP_31_STFUX:
  440. case OP_31_STFIWX:
  441. return true;
  442. }
  443. break;
  444. }
  445. return false;
  446. }
  447. static int get_d_signext(u32 inst)
  448. {
  449. int d = inst & 0x8ff;
  450. if (d & 0x800)
  451. return -(d & 0x7ff);
  452. return (d & 0x7ff);
  453. }
  454. static int kvmppc_ps_three_in(struct kvm_vcpu *vcpu, bool rc,
  455. int reg_out, int reg_in1, int reg_in2,
  456. int reg_in3, int scalar,
  457. void (*func)(u64 *fpscr,
  458. u32 *dst, u32 *src1,
  459. u32 *src2, u32 *src3))
  460. {
  461. u32 *qpr = vcpu->arch.qpr;
  462. u32 ps0_out;
  463. u32 ps0_in1, ps0_in2, ps0_in3;
  464. u32 ps1_in1, ps1_in2, ps1_in3;
  465. /* RC */
  466. WARN_ON(rc);
  467. /* PS0 */
  468. kvm_cvt_df(&VCPU_FPR(vcpu, reg_in1), &ps0_in1);
  469. kvm_cvt_df(&VCPU_FPR(vcpu, reg_in2), &ps0_in2);
  470. kvm_cvt_df(&VCPU_FPR(vcpu, reg_in3), &ps0_in3);
  471. if (scalar & SCALAR_LOW)
  472. ps0_in2 = qpr[reg_in2];
  473. func(&vcpu->arch.fp.fpscr, &ps0_out, &ps0_in1, &ps0_in2, &ps0_in3);
  474. dprintk(KERN_INFO "PS3 ps0 -> f(0x%x, 0x%x, 0x%x) = 0x%x\n",
  475. ps0_in1, ps0_in2, ps0_in3, ps0_out);
  476. if (!(scalar & SCALAR_NO_PS0))
  477. kvm_cvt_fd(&ps0_out, &VCPU_FPR(vcpu, reg_out));
  478. /* PS1 */
  479. ps1_in1 = qpr[reg_in1];
  480. ps1_in2 = qpr[reg_in2];
  481. ps1_in3 = qpr[reg_in3];
  482. if (scalar & SCALAR_HIGH)
  483. ps1_in2 = ps0_in2;
  484. if (!(scalar & SCALAR_NO_PS1))
  485. func(&vcpu->arch.fp.fpscr, &qpr[reg_out], &ps1_in1, &ps1_in2, &ps1_in3);
  486. dprintk(KERN_INFO "PS3 ps1 -> f(0x%x, 0x%x, 0x%x) = 0x%x\n",
  487. ps1_in1, ps1_in2, ps1_in3, qpr[reg_out]);
  488. return EMULATE_DONE;
  489. }
  490. static int kvmppc_ps_two_in(struct kvm_vcpu *vcpu, bool rc,
  491. int reg_out, int reg_in1, int reg_in2,
  492. int scalar,
  493. void (*func)(u64 *fpscr,
  494. u32 *dst, u32 *src1,
  495. u32 *src2))
  496. {
  497. u32 *qpr = vcpu->arch.qpr;
  498. u32 ps0_out;
  499. u32 ps0_in1, ps0_in2;
  500. u32 ps1_out;
  501. u32 ps1_in1, ps1_in2;
  502. /* RC */
  503. WARN_ON(rc);
  504. /* PS0 */
  505. kvm_cvt_df(&VCPU_FPR(vcpu, reg_in1), &ps0_in1);
  506. if (scalar & SCALAR_LOW)
  507. ps0_in2 = qpr[reg_in2];
  508. else
  509. kvm_cvt_df(&VCPU_FPR(vcpu, reg_in2), &ps0_in2);
  510. func(&vcpu->arch.fp.fpscr, &ps0_out, &ps0_in1, &ps0_in2);
  511. if (!(scalar & SCALAR_NO_PS0)) {
  512. dprintk(KERN_INFO "PS2 ps0 -> f(0x%x, 0x%x) = 0x%x\n",
  513. ps0_in1, ps0_in2, ps0_out);
  514. kvm_cvt_fd(&ps0_out, &VCPU_FPR(vcpu, reg_out));
  515. }
  516. /* PS1 */
  517. ps1_in1 = qpr[reg_in1];
  518. ps1_in2 = qpr[reg_in2];
  519. if (scalar & SCALAR_HIGH)
  520. ps1_in2 = ps0_in2;
  521. func(&vcpu->arch.fp.fpscr, &ps1_out, &ps1_in1, &ps1_in2);
  522. if (!(scalar & SCALAR_NO_PS1)) {
  523. qpr[reg_out] = ps1_out;
  524. dprintk(KERN_INFO "PS2 ps1 -> f(0x%x, 0x%x) = 0x%x\n",
  525. ps1_in1, ps1_in2, qpr[reg_out]);
  526. }
  527. return EMULATE_DONE;
  528. }
  529. static int kvmppc_ps_one_in(struct kvm_vcpu *vcpu, bool rc,
  530. int reg_out, int reg_in,
  531. void (*func)(u64 *t,
  532. u32 *dst, u32 *src1))
  533. {
  534. u32 *qpr = vcpu->arch.qpr;
  535. u32 ps0_out, ps0_in;
  536. u32 ps1_in;
  537. /* RC */
  538. WARN_ON(rc);
  539. /* PS0 */
  540. kvm_cvt_df(&VCPU_FPR(vcpu, reg_in), &ps0_in);
  541. func(&vcpu->arch.fp.fpscr, &ps0_out, &ps0_in);
  542. dprintk(KERN_INFO "PS1 ps0 -> f(0x%x) = 0x%x\n",
  543. ps0_in, ps0_out);
  544. kvm_cvt_fd(&ps0_out, &VCPU_FPR(vcpu, reg_out));
  545. /* PS1 */
  546. ps1_in = qpr[reg_in];
  547. func(&vcpu->arch.fp.fpscr, &qpr[reg_out], &ps1_in);
  548. dprintk(KERN_INFO "PS1 ps1 -> f(0x%x) = 0x%x\n",
  549. ps1_in, qpr[reg_out]);
  550. return EMULATE_DONE;
  551. }
  552. int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu)
  553. {
  554. u32 inst;
  555. enum emulation_result emulated = EMULATE_DONE;
  556. int ax_rd, ax_ra, ax_rb, ax_rc;
  557. short full_d;
  558. u64 *fpr_d, *fpr_a, *fpr_b, *fpr_c;
  559. bool rcomp;
  560. u32 cr;
  561. #ifdef DEBUG
  562. int i;
  563. #endif
  564. emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst);
  565. if (emulated != EMULATE_DONE)
  566. return emulated;
  567. ax_rd = inst_get_field(inst, 6, 10);
  568. ax_ra = inst_get_field(inst, 11, 15);
  569. ax_rb = inst_get_field(inst, 16, 20);
  570. ax_rc = inst_get_field(inst, 21, 25);
  571. full_d = inst_get_field(inst, 16, 31);
  572. fpr_d = &VCPU_FPR(vcpu, ax_rd);
  573. fpr_a = &VCPU_FPR(vcpu, ax_ra);
  574. fpr_b = &VCPU_FPR(vcpu, ax_rb);
  575. fpr_c = &VCPU_FPR(vcpu, ax_rc);
  576. rcomp = (inst & 1) ? true : false;
  577. cr = kvmppc_get_cr(vcpu);
  578. if (!kvmppc_inst_is_paired_single(vcpu, inst))
  579. return EMULATE_FAIL;
  580. if (!(kvmppc_get_msr(vcpu) & MSR_FP)) {
  581. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL);
  582. return EMULATE_AGAIN;
  583. }
  584. kvmppc_giveup_ext(vcpu, MSR_FP);
  585. preempt_disable();
  586. enable_kernel_fp();
  587. /* Do we need to clear FE0 / FE1 here? Don't think so. */
  588. #ifdef DEBUG
  589. for (i = 0; i < ARRAY_SIZE(vcpu->arch.fp.fpr); i++) {
  590. u32 f;
  591. kvm_cvt_df(&VCPU_FPR(vcpu, i), &f);
  592. dprintk(KERN_INFO "FPR[%d] = 0x%x / 0x%llx QPR[%d] = 0x%x\n",
  593. i, f, VCPU_FPR(vcpu, i), i, vcpu->arch.qpr[i]);
  594. }
  595. #endif
  596. switch (get_op(inst)) {
  597. case OP_PSQ_L:
  598. {
  599. ulong addr = ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0;
  600. bool w = inst_get_field(inst, 16, 16) ? true : false;
  601. int i = inst_get_field(inst, 17, 19);
  602. addr += get_d_signext(inst);
  603. emulated = kvmppc_emulate_psq_load(run, vcpu, ax_rd, addr, w, i);
  604. break;
  605. }
  606. case OP_PSQ_LU:
  607. {
  608. ulong addr = kvmppc_get_gpr(vcpu, ax_ra);
  609. bool w = inst_get_field(inst, 16, 16) ? true : false;
  610. int i = inst_get_field(inst, 17, 19);
  611. addr += get_d_signext(inst);
  612. emulated = kvmppc_emulate_psq_load(run, vcpu, ax_rd, addr, w, i);
  613. if (emulated == EMULATE_DONE)
  614. kvmppc_set_gpr(vcpu, ax_ra, addr);
  615. break;
  616. }
  617. case OP_PSQ_ST:
  618. {
  619. ulong addr = ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0;
  620. bool w = inst_get_field(inst, 16, 16) ? true : false;
  621. int i = inst_get_field(inst, 17, 19);
  622. addr += get_d_signext(inst);
  623. emulated = kvmppc_emulate_psq_store(run, vcpu, ax_rd, addr, w, i);
  624. break;
  625. }
  626. case OP_PSQ_STU:
  627. {
  628. ulong addr = kvmppc_get_gpr(vcpu, ax_ra);
  629. bool w = inst_get_field(inst, 16, 16) ? true : false;
  630. int i = inst_get_field(inst, 17, 19);
  631. addr += get_d_signext(inst);
  632. emulated = kvmppc_emulate_psq_store(run, vcpu, ax_rd, addr, w, i);
  633. if (emulated == EMULATE_DONE)
  634. kvmppc_set_gpr(vcpu, ax_ra, addr);
  635. break;
  636. }
  637. case 4:
  638. /* X form */
  639. switch (inst_get_field(inst, 21, 30)) {
  640. case OP_4X_PS_CMPU0:
  641. /* XXX */
  642. emulated = EMULATE_FAIL;
  643. break;
  644. case OP_4X_PSQ_LX:
  645. {
  646. ulong addr = ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0;
  647. bool w = inst_get_field(inst, 21, 21) ? true : false;
  648. int i = inst_get_field(inst, 22, 24);
  649. addr += kvmppc_get_gpr(vcpu, ax_rb);
  650. emulated = kvmppc_emulate_psq_load(run, vcpu, ax_rd, addr, w, i);
  651. break;
  652. }
  653. case OP_4X_PS_CMPO0:
  654. /* XXX */
  655. emulated = EMULATE_FAIL;
  656. break;
  657. case OP_4X_PSQ_LUX:
  658. {
  659. ulong addr = kvmppc_get_gpr(vcpu, ax_ra);
  660. bool w = inst_get_field(inst, 21, 21) ? true : false;
  661. int i = inst_get_field(inst, 22, 24);
  662. addr += kvmppc_get_gpr(vcpu, ax_rb);
  663. emulated = kvmppc_emulate_psq_load(run, vcpu, ax_rd, addr, w, i);
  664. if (emulated == EMULATE_DONE)
  665. kvmppc_set_gpr(vcpu, ax_ra, addr);
  666. break;
  667. }
  668. case OP_4X_PS_NEG:
  669. VCPU_FPR(vcpu, ax_rd) = VCPU_FPR(vcpu, ax_rb);
  670. VCPU_FPR(vcpu, ax_rd) ^= 0x8000000000000000ULL;
  671. vcpu->arch.qpr[ax_rd] = vcpu->arch.qpr[ax_rb];
  672. vcpu->arch.qpr[ax_rd] ^= 0x80000000;
  673. break;
  674. case OP_4X_PS_CMPU1:
  675. /* XXX */
  676. emulated = EMULATE_FAIL;
  677. break;
  678. case OP_4X_PS_MR:
  679. WARN_ON(rcomp);
  680. VCPU_FPR(vcpu, ax_rd) = VCPU_FPR(vcpu, ax_rb);
  681. vcpu->arch.qpr[ax_rd] = vcpu->arch.qpr[ax_rb];
  682. break;
  683. case OP_4X_PS_CMPO1:
  684. /* XXX */
  685. emulated = EMULATE_FAIL;
  686. break;
  687. case OP_4X_PS_NABS:
  688. WARN_ON(rcomp);
  689. VCPU_FPR(vcpu, ax_rd) = VCPU_FPR(vcpu, ax_rb);
  690. VCPU_FPR(vcpu, ax_rd) |= 0x8000000000000000ULL;
  691. vcpu->arch.qpr[ax_rd] = vcpu->arch.qpr[ax_rb];
  692. vcpu->arch.qpr[ax_rd] |= 0x80000000;
  693. break;
  694. case OP_4X_PS_ABS:
  695. WARN_ON(rcomp);
  696. VCPU_FPR(vcpu, ax_rd) = VCPU_FPR(vcpu, ax_rb);
  697. VCPU_FPR(vcpu, ax_rd) &= ~0x8000000000000000ULL;
  698. vcpu->arch.qpr[ax_rd] = vcpu->arch.qpr[ax_rb];
  699. vcpu->arch.qpr[ax_rd] &= ~0x80000000;
  700. break;
  701. case OP_4X_PS_MERGE00:
  702. WARN_ON(rcomp);
  703. VCPU_FPR(vcpu, ax_rd) = VCPU_FPR(vcpu, ax_ra);
  704. /* vcpu->arch.qpr[ax_rd] = VCPU_FPR(vcpu, ax_rb); */
  705. kvm_cvt_df(&VCPU_FPR(vcpu, ax_rb),
  706. &vcpu->arch.qpr[ax_rd]);
  707. break;
  708. case OP_4X_PS_MERGE01:
  709. WARN_ON(rcomp);
  710. VCPU_FPR(vcpu, ax_rd) = VCPU_FPR(vcpu, ax_ra);
  711. vcpu->arch.qpr[ax_rd] = vcpu->arch.qpr[ax_rb];
  712. break;
  713. case OP_4X_PS_MERGE10:
  714. WARN_ON(rcomp);
  715. /* VCPU_FPR(vcpu, ax_rd) = vcpu->arch.qpr[ax_ra]; */
  716. kvm_cvt_fd(&vcpu->arch.qpr[ax_ra],
  717. &VCPU_FPR(vcpu, ax_rd));
  718. /* vcpu->arch.qpr[ax_rd] = VCPU_FPR(vcpu, ax_rb); */
  719. kvm_cvt_df(&VCPU_FPR(vcpu, ax_rb),
  720. &vcpu->arch.qpr[ax_rd]);
  721. break;
  722. case OP_4X_PS_MERGE11:
  723. WARN_ON(rcomp);
  724. /* VCPU_FPR(vcpu, ax_rd) = vcpu->arch.qpr[ax_ra]; */
  725. kvm_cvt_fd(&vcpu->arch.qpr[ax_ra],
  726. &VCPU_FPR(vcpu, ax_rd));
  727. vcpu->arch.qpr[ax_rd] = vcpu->arch.qpr[ax_rb];
  728. break;
  729. }
  730. /* XW form */
  731. switch (inst_get_field(inst, 25, 30)) {
  732. case OP_4XW_PSQ_STX:
  733. {
  734. ulong addr = ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0;
  735. bool w = inst_get_field(inst, 21, 21) ? true : false;
  736. int i = inst_get_field(inst, 22, 24);
  737. addr += kvmppc_get_gpr(vcpu, ax_rb);
  738. emulated = kvmppc_emulate_psq_store(run, vcpu, ax_rd, addr, w, i);
  739. break;
  740. }
  741. case OP_4XW_PSQ_STUX:
  742. {
  743. ulong addr = kvmppc_get_gpr(vcpu, ax_ra);
  744. bool w = inst_get_field(inst, 21, 21) ? true : false;
  745. int i = inst_get_field(inst, 22, 24);
  746. addr += kvmppc_get_gpr(vcpu, ax_rb);
  747. emulated = kvmppc_emulate_psq_store(run, vcpu, ax_rd, addr, w, i);
  748. if (emulated == EMULATE_DONE)
  749. kvmppc_set_gpr(vcpu, ax_ra, addr);
  750. break;
  751. }
  752. }
  753. /* A form */
  754. switch (inst_get_field(inst, 26, 30)) {
  755. case OP_4A_PS_SUM1:
  756. emulated = kvmppc_ps_two_in(vcpu, rcomp, ax_rd,
  757. ax_rb, ax_ra, SCALAR_NO_PS0 | SCALAR_HIGH, fps_fadds);
  758. VCPU_FPR(vcpu, ax_rd) = VCPU_FPR(vcpu, ax_rc);
  759. break;
  760. case OP_4A_PS_SUM0:
  761. emulated = kvmppc_ps_two_in(vcpu, rcomp, ax_rd,
  762. ax_ra, ax_rb, SCALAR_NO_PS1 | SCALAR_LOW, fps_fadds);
  763. vcpu->arch.qpr[ax_rd] = vcpu->arch.qpr[ax_rc];
  764. break;
  765. case OP_4A_PS_MULS0:
  766. emulated = kvmppc_ps_two_in(vcpu, rcomp, ax_rd,
  767. ax_ra, ax_rc, SCALAR_HIGH, fps_fmuls);
  768. break;
  769. case OP_4A_PS_MULS1:
  770. emulated = kvmppc_ps_two_in(vcpu, rcomp, ax_rd,
  771. ax_ra, ax_rc, SCALAR_LOW, fps_fmuls);
  772. break;
  773. case OP_4A_PS_MADDS0:
  774. emulated = kvmppc_ps_three_in(vcpu, rcomp, ax_rd,
  775. ax_ra, ax_rc, ax_rb, SCALAR_HIGH, fps_fmadds);
  776. break;
  777. case OP_4A_PS_MADDS1:
  778. emulated = kvmppc_ps_three_in(vcpu, rcomp, ax_rd,
  779. ax_ra, ax_rc, ax_rb, SCALAR_LOW, fps_fmadds);
  780. break;
  781. case OP_4A_PS_DIV:
  782. emulated = kvmppc_ps_two_in(vcpu, rcomp, ax_rd,
  783. ax_ra, ax_rb, SCALAR_NONE, fps_fdivs);
  784. break;
  785. case OP_4A_PS_SUB:
  786. emulated = kvmppc_ps_two_in(vcpu, rcomp, ax_rd,
  787. ax_ra, ax_rb, SCALAR_NONE, fps_fsubs);
  788. break;
  789. case OP_4A_PS_ADD:
  790. emulated = kvmppc_ps_two_in(vcpu, rcomp, ax_rd,
  791. ax_ra, ax_rb, SCALAR_NONE, fps_fadds);
  792. break;
  793. case OP_4A_PS_SEL:
  794. emulated = kvmppc_ps_three_in(vcpu, rcomp, ax_rd,
  795. ax_ra, ax_rc, ax_rb, SCALAR_NONE, fps_fsel);
  796. break;
  797. case OP_4A_PS_RES:
  798. emulated = kvmppc_ps_one_in(vcpu, rcomp, ax_rd,
  799. ax_rb, fps_fres);
  800. break;
  801. case OP_4A_PS_MUL:
  802. emulated = kvmppc_ps_two_in(vcpu, rcomp, ax_rd,
  803. ax_ra, ax_rc, SCALAR_NONE, fps_fmuls);
  804. break;
  805. case OP_4A_PS_RSQRTE:
  806. emulated = kvmppc_ps_one_in(vcpu, rcomp, ax_rd,
  807. ax_rb, fps_frsqrte);
  808. break;
  809. case OP_4A_PS_MSUB:
  810. emulated = kvmppc_ps_three_in(vcpu, rcomp, ax_rd,
  811. ax_ra, ax_rc, ax_rb, SCALAR_NONE, fps_fmsubs);
  812. break;
  813. case OP_4A_PS_MADD:
  814. emulated = kvmppc_ps_three_in(vcpu, rcomp, ax_rd,
  815. ax_ra, ax_rc, ax_rb, SCALAR_NONE, fps_fmadds);
  816. break;
  817. case OP_4A_PS_NMSUB:
  818. emulated = kvmppc_ps_three_in(vcpu, rcomp, ax_rd,
  819. ax_ra, ax_rc, ax_rb, SCALAR_NONE, fps_fnmsubs);
  820. break;
  821. case OP_4A_PS_NMADD:
  822. emulated = kvmppc_ps_three_in(vcpu, rcomp, ax_rd,
  823. ax_ra, ax_rc, ax_rb, SCALAR_NONE, fps_fnmadds);
  824. break;
  825. }
  826. break;
  827. /* Real FPU operations */
  828. case OP_LFS:
  829. {
  830. ulong addr = (ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0) + full_d;
  831. emulated = kvmppc_emulate_fpr_load(run, vcpu, ax_rd, addr,
  832. FPU_LS_SINGLE);
  833. break;
  834. }
  835. case OP_LFSU:
  836. {
  837. ulong addr = kvmppc_get_gpr(vcpu, ax_ra) + full_d;
  838. emulated = kvmppc_emulate_fpr_load(run, vcpu, ax_rd, addr,
  839. FPU_LS_SINGLE);
  840. if (emulated == EMULATE_DONE)
  841. kvmppc_set_gpr(vcpu, ax_ra, addr);
  842. break;
  843. }
  844. case OP_LFD:
  845. {
  846. ulong addr = (ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0) + full_d;
  847. emulated = kvmppc_emulate_fpr_load(run, vcpu, ax_rd, addr,
  848. FPU_LS_DOUBLE);
  849. break;
  850. }
  851. case OP_LFDU:
  852. {
  853. ulong addr = kvmppc_get_gpr(vcpu, ax_ra) + full_d;
  854. emulated = kvmppc_emulate_fpr_load(run, vcpu, ax_rd, addr,
  855. FPU_LS_DOUBLE);
  856. if (emulated == EMULATE_DONE)
  857. kvmppc_set_gpr(vcpu, ax_ra, addr);
  858. break;
  859. }
  860. case OP_STFS:
  861. {
  862. ulong addr = (ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0) + full_d;
  863. emulated = kvmppc_emulate_fpr_store(run, vcpu, ax_rd, addr,
  864. FPU_LS_SINGLE);
  865. break;
  866. }
  867. case OP_STFSU:
  868. {
  869. ulong addr = kvmppc_get_gpr(vcpu, ax_ra) + full_d;
  870. emulated = kvmppc_emulate_fpr_store(run, vcpu, ax_rd, addr,
  871. FPU_LS_SINGLE);
  872. if (emulated == EMULATE_DONE)
  873. kvmppc_set_gpr(vcpu, ax_ra, addr);
  874. break;
  875. }
  876. case OP_STFD:
  877. {
  878. ulong addr = (ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0) + full_d;
  879. emulated = kvmppc_emulate_fpr_store(run, vcpu, ax_rd, addr,
  880. FPU_LS_DOUBLE);
  881. break;
  882. }
  883. case OP_STFDU:
  884. {
  885. ulong addr = kvmppc_get_gpr(vcpu, ax_ra) + full_d;
  886. emulated = kvmppc_emulate_fpr_store(run, vcpu, ax_rd, addr,
  887. FPU_LS_DOUBLE);
  888. if (emulated == EMULATE_DONE)
  889. kvmppc_set_gpr(vcpu, ax_ra, addr);
  890. break;
  891. }
  892. case 31:
  893. switch (inst_get_field(inst, 21, 30)) {
  894. case OP_31_LFSX:
  895. {
  896. ulong addr = ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0;
  897. addr += kvmppc_get_gpr(vcpu, ax_rb);
  898. emulated = kvmppc_emulate_fpr_load(run, vcpu, ax_rd,
  899. addr, FPU_LS_SINGLE);
  900. break;
  901. }
  902. case OP_31_LFSUX:
  903. {
  904. ulong addr = kvmppc_get_gpr(vcpu, ax_ra) +
  905. kvmppc_get_gpr(vcpu, ax_rb);
  906. emulated = kvmppc_emulate_fpr_load(run, vcpu, ax_rd,
  907. addr, FPU_LS_SINGLE);
  908. if (emulated == EMULATE_DONE)
  909. kvmppc_set_gpr(vcpu, ax_ra, addr);
  910. break;
  911. }
  912. case OP_31_LFDX:
  913. {
  914. ulong addr = (ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0) +
  915. kvmppc_get_gpr(vcpu, ax_rb);
  916. emulated = kvmppc_emulate_fpr_load(run, vcpu, ax_rd,
  917. addr, FPU_LS_DOUBLE);
  918. break;
  919. }
  920. case OP_31_LFDUX:
  921. {
  922. ulong addr = kvmppc_get_gpr(vcpu, ax_ra) +
  923. kvmppc_get_gpr(vcpu, ax_rb);
  924. emulated = kvmppc_emulate_fpr_load(run, vcpu, ax_rd,
  925. addr, FPU_LS_DOUBLE);
  926. if (emulated == EMULATE_DONE)
  927. kvmppc_set_gpr(vcpu, ax_ra, addr);
  928. break;
  929. }
  930. case OP_31_STFSX:
  931. {
  932. ulong addr = (ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0) +
  933. kvmppc_get_gpr(vcpu, ax_rb);
  934. emulated = kvmppc_emulate_fpr_store(run, vcpu, ax_rd,
  935. addr, FPU_LS_SINGLE);
  936. break;
  937. }
  938. case OP_31_STFSUX:
  939. {
  940. ulong addr = kvmppc_get_gpr(vcpu, ax_ra) +
  941. kvmppc_get_gpr(vcpu, ax_rb);
  942. emulated = kvmppc_emulate_fpr_store(run, vcpu, ax_rd,
  943. addr, FPU_LS_SINGLE);
  944. if (emulated == EMULATE_DONE)
  945. kvmppc_set_gpr(vcpu, ax_ra, addr);
  946. break;
  947. }
  948. case OP_31_STFX:
  949. {
  950. ulong addr = (ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0) +
  951. kvmppc_get_gpr(vcpu, ax_rb);
  952. emulated = kvmppc_emulate_fpr_store(run, vcpu, ax_rd,
  953. addr, FPU_LS_DOUBLE);
  954. break;
  955. }
  956. case OP_31_STFUX:
  957. {
  958. ulong addr = kvmppc_get_gpr(vcpu, ax_ra) +
  959. kvmppc_get_gpr(vcpu, ax_rb);
  960. emulated = kvmppc_emulate_fpr_store(run, vcpu, ax_rd,
  961. addr, FPU_LS_DOUBLE);
  962. if (emulated == EMULATE_DONE)
  963. kvmppc_set_gpr(vcpu, ax_ra, addr);
  964. break;
  965. }
  966. case OP_31_STFIWX:
  967. {
  968. ulong addr = (ax_ra ? kvmppc_get_gpr(vcpu, ax_ra) : 0) +
  969. kvmppc_get_gpr(vcpu, ax_rb);
  970. emulated = kvmppc_emulate_fpr_store(run, vcpu, ax_rd,
  971. addr,
  972. FPU_LS_SINGLE_LOW);
  973. break;
  974. }
  975. break;
  976. }
  977. break;
  978. case 59:
  979. switch (inst_get_field(inst, 21, 30)) {
  980. case OP_59_FADDS:
  981. fpd_fadds(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_b);
  982. kvmppc_sync_qpr(vcpu, ax_rd);
  983. break;
  984. case OP_59_FSUBS:
  985. fpd_fsubs(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_b);
  986. kvmppc_sync_qpr(vcpu, ax_rd);
  987. break;
  988. case OP_59_FDIVS:
  989. fpd_fdivs(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_b);
  990. kvmppc_sync_qpr(vcpu, ax_rd);
  991. break;
  992. case OP_59_FRES:
  993. fpd_fres(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_b);
  994. kvmppc_sync_qpr(vcpu, ax_rd);
  995. break;
  996. case OP_59_FRSQRTES:
  997. fpd_frsqrtes(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_b);
  998. kvmppc_sync_qpr(vcpu, ax_rd);
  999. break;
  1000. }
  1001. switch (inst_get_field(inst, 26, 30)) {
  1002. case OP_59_FMULS:
  1003. fpd_fmuls(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_c);
  1004. kvmppc_sync_qpr(vcpu, ax_rd);
  1005. break;
  1006. case OP_59_FMSUBS:
  1007. fpd_fmsubs(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_c, fpr_b);
  1008. kvmppc_sync_qpr(vcpu, ax_rd);
  1009. break;
  1010. case OP_59_FMADDS:
  1011. fpd_fmadds(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_c, fpr_b);
  1012. kvmppc_sync_qpr(vcpu, ax_rd);
  1013. break;
  1014. case OP_59_FNMSUBS:
  1015. fpd_fnmsubs(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_c, fpr_b);
  1016. kvmppc_sync_qpr(vcpu, ax_rd);
  1017. break;
  1018. case OP_59_FNMADDS:
  1019. fpd_fnmadds(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_c, fpr_b);
  1020. kvmppc_sync_qpr(vcpu, ax_rd);
  1021. break;
  1022. }
  1023. break;
  1024. case 63:
  1025. switch (inst_get_field(inst, 21, 30)) {
  1026. case OP_63_MTFSB0:
  1027. case OP_63_MTFSB1:
  1028. case OP_63_MCRFS:
  1029. case OP_63_MTFSFI:
  1030. /* XXX need to implement */
  1031. break;
  1032. case OP_63_MFFS:
  1033. /* XXX missing CR */
  1034. *fpr_d = vcpu->arch.fp.fpscr;
  1035. break;
  1036. case OP_63_MTFSF:
  1037. /* XXX missing fm bits */
  1038. /* XXX missing CR */
  1039. vcpu->arch.fp.fpscr = *fpr_b;
  1040. break;
  1041. case OP_63_FCMPU:
  1042. {
  1043. u32 tmp_cr;
  1044. u32 cr0_mask = 0xf0000000;
  1045. u32 cr_shift = inst_get_field(inst, 6, 8) * 4;
  1046. fpd_fcmpu(&vcpu->arch.fp.fpscr, &tmp_cr, fpr_a, fpr_b);
  1047. cr &= ~(cr0_mask >> cr_shift);
  1048. cr |= (cr & cr0_mask) >> cr_shift;
  1049. break;
  1050. }
  1051. case OP_63_FCMPO:
  1052. {
  1053. u32 tmp_cr;
  1054. u32 cr0_mask = 0xf0000000;
  1055. u32 cr_shift = inst_get_field(inst, 6, 8) * 4;
  1056. fpd_fcmpo(&vcpu->arch.fp.fpscr, &tmp_cr, fpr_a, fpr_b);
  1057. cr &= ~(cr0_mask >> cr_shift);
  1058. cr |= (cr & cr0_mask) >> cr_shift;
  1059. break;
  1060. }
  1061. case OP_63_FNEG:
  1062. fpd_fneg(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_b);
  1063. break;
  1064. case OP_63_FMR:
  1065. *fpr_d = *fpr_b;
  1066. break;
  1067. case OP_63_FABS:
  1068. fpd_fabs(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_b);
  1069. break;
  1070. case OP_63_FCPSGN:
  1071. fpd_fcpsgn(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_b);
  1072. break;
  1073. case OP_63_FDIV:
  1074. fpd_fdiv(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_b);
  1075. break;
  1076. case OP_63_FADD:
  1077. fpd_fadd(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_b);
  1078. break;
  1079. case OP_63_FSUB:
  1080. fpd_fsub(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_b);
  1081. break;
  1082. case OP_63_FCTIW:
  1083. fpd_fctiw(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_b);
  1084. break;
  1085. case OP_63_FCTIWZ:
  1086. fpd_fctiwz(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_b);
  1087. break;
  1088. case OP_63_FRSP:
  1089. fpd_frsp(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_b);
  1090. kvmppc_sync_qpr(vcpu, ax_rd);
  1091. break;
  1092. case OP_63_FRSQRTE:
  1093. {
  1094. double one = 1.0f;
  1095. /* fD = sqrt(fB) */
  1096. fpd_fsqrt(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_b);
  1097. /* fD = 1.0f / fD */
  1098. fpd_fdiv(&vcpu->arch.fp.fpscr, &cr, fpr_d, (u64*)&one, fpr_d);
  1099. break;
  1100. }
  1101. }
  1102. switch (inst_get_field(inst, 26, 30)) {
  1103. case OP_63_FMUL:
  1104. fpd_fmul(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_c);
  1105. break;
  1106. case OP_63_FSEL:
  1107. fpd_fsel(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_c, fpr_b);
  1108. break;
  1109. case OP_63_FMSUB:
  1110. fpd_fmsub(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_c, fpr_b);
  1111. break;
  1112. case OP_63_FMADD:
  1113. fpd_fmadd(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_c, fpr_b);
  1114. break;
  1115. case OP_63_FNMSUB:
  1116. fpd_fnmsub(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_c, fpr_b);
  1117. break;
  1118. case OP_63_FNMADD:
  1119. fpd_fnmadd(&vcpu->arch.fp.fpscr, &cr, fpr_d, fpr_a, fpr_c, fpr_b);
  1120. break;
  1121. }
  1122. break;
  1123. }
  1124. #ifdef DEBUG
  1125. for (i = 0; i < ARRAY_SIZE(vcpu->arch.fp.fpr); i++) {
  1126. u32 f;
  1127. kvm_cvt_df(&VCPU_FPR(vcpu, i), &f);
  1128. dprintk(KERN_INFO "FPR[%d] = 0x%x\n", i, f);
  1129. }
  1130. #endif
  1131. if (rcomp)
  1132. kvmppc_set_cr(vcpu, cr);
  1133. preempt_enable();
  1134. return emulated;
  1135. }