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.
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.
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.
15 * Copyright IBM Corp. 2007
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
20 #include <linux/jiffies.h>
21 #include <linux/timer.h>
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/kvm_host.h>
27 #include <asm/dcr-regs.h>
29 #include <asm/byteorder.h>
30 #include <asm/kvm_ppc.h>
32 /* Instruction decoding */
33 static inline unsigned int get_op(u32 inst)
38 static inline unsigned int get_xop(u32 inst)
40 return (inst >> 1) & 0x3ff;
43 static inline unsigned int get_sprn(u32 inst)
45 return ((inst >> 16) & 0x1f) | ((inst >> 6) & 0x3e0);
48 static inline unsigned int get_dcrn(u32 inst)
50 return ((inst >> 16) & 0x1f) | ((inst >> 6) & 0x3e0);
53 static inline unsigned int get_rt(u32 inst)
55 return (inst >> 21) & 0x1f;
58 static inline unsigned int get_rs(u32 inst)
60 return (inst >> 21) & 0x1f;
63 static inline unsigned int get_ra(u32 inst)
65 return (inst >> 16) & 0x1f;
68 static inline unsigned int get_rb(u32 inst)
70 return (inst >> 11) & 0x1f;
73 static inline unsigned int get_rc(u32 inst)
78 static inline unsigned int get_ws(u32 inst)
80 return (inst >> 11) & 0x1f;
83 static inline unsigned int get_d(u32 inst)
88 static void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
90 if (vcpu->arch.tcr & TCR_DIE) {
91 /* The decrementer ticks at the same rate as the timebase, so
92 * that's how we convert the guest DEC value to the number of
94 unsigned long nr_jiffies;
96 nr_jiffies = vcpu->arch.dec / tb_ticks_per_jiffy;
97 mod_timer(&vcpu->arch.dec_timer,
98 get_jiffies_64() + nr_jiffies);
100 del_timer(&vcpu->arch.dec_timer);
104 static void kvmppc_emul_rfi(struct kvm_vcpu *vcpu)
106 vcpu->arch.pc = vcpu->arch.srr0;
107 kvmppc_set_msr(vcpu, vcpu->arch.srr1);
122 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
124 int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
126 u32 inst = vcpu->arch.last_inst;
136 enum emulation_result emulated = EMULATE_DONE;
139 switch (get_op(inst)) {
142 kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_PROGRAM);
147 switch (get_xop(inst)) {
149 kvmppc_emul_rfi(vcpu);
154 emulated = EMULATE_FAIL;
160 switch (get_xop(inst)) {
164 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
169 vcpu->arch.gpr[rt] = vcpu->arch.msr;
174 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
177 case 131: /* wrtee */
179 vcpu->arch.msr = (vcpu->arch.msr & ~MSR_EE)
180 | (vcpu->arch.gpr[rs] & MSR_EE);
183 case 146: /* mtmsr */
185 kvmppc_set_msr(vcpu, vcpu->arch.gpr[rs]);
190 emulated = kvmppc_handle_store(run, vcpu,
195 case 163: /* wrteei */
196 vcpu->arch.msr = (vcpu->arch.msr & ~MSR_EE)
202 emulated = kvmppc_handle_store(run, vcpu,
207 case 247: /* stbux */
212 ea = vcpu->arch.gpr[rb];
214 ea += vcpu->arch.gpr[ra];
216 emulated = kvmppc_handle_store(run, vcpu,
219 vcpu->arch.gpr[rs] = ea;
224 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
227 case 311: /* lhzux */
232 ea = vcpu->arch.gpr[rb];
234 ea += vcpu->arch.gpr[ra];
236 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
237 vcpu->arch.gpr[ra] = ea;
240 case 323: /* mfdcr */
241 dcrn = get_dcrn(inst);
244 /* The guest may access CPR0 registers to determine the timebase
245 * frequency, and it must know the real host frequency because it
246 * can directly access the timebase registers.
248 * It would be possible to emulate those accesses in userspace,
249 * but userspace can really only figure out the end frequency.
250 * We could decompose that into the factors that compute it, but
251 * that's tricky math, and it's easier to just report the real
255 case DCRN_CPR0_CONFIG_ADDR:
256 vcpu->arch.gpr[rt] = vcpu->arch.cpr0_cfgaddr;
258 case DCRN_CPR0_CONFIG_DATA:
260 mtdcr(DCRN_CPR0_CONFIG_ADDR,
261 vcpu->arch.cpr0_cfgaddr);
262 vcpu->arch.gpr[rt] = mfdcr(DCRN_CPR0_CONFIG_DATA);
266 run->dcr.dcrn = dcrn;
268 run->dcr.is_write = 0;
269 vcpu->arch.io_gpr = rt;
270 vcpu->arch.dcr_needed = 1;
271 emulated = EMULATE_DO_DCR;
276 case 339: /* mfspr */
277 sprn = get_sprn(inst);
282 vcpu->arch.gpr[rt] = vcpu->arch.srr0; break;
284 vcpu->arch.gpr[rt] = vcpu->arch.srr1; break;
286 vcpu->arch.gpr[rt] = vcpu->arch.mmucr; break;
288 vcpu->arch.gpr[rt] = vcpu->arch.pid; break;
290 vcpu->arch.gpr[rt] = vcpu->arch.ivpr; break;
292 vcpu->arch.gpr[rt] = vcpu->arch.ccr0; break;
294 vcpu->arch.gpr[rt] = vcpu->arch.ccr1; break;
296 vcpu->arch.gpr[rt] = vcpu->arch.pvr; break;
298 vcpu->arch.gpr[rt] = vcpu->arch.dear; break;
300 vcpu->arch.gpr[rt] = vcpu->arch.esr; break;
302 vcpu->arch.gpr[rt] = vcpu->arch.dbcr0; break;
304 vcpu->arch.gpr[rt] = vcpu->arch.dbcr1; break;
306 /* Note: mftb and TBRL/TBWL are user-accessible, so
307 * the guest can always access the real TB anyways.
308 * In fact, we probably will never see these traps. */
310 vcpu->arch.gpr[rt] = mftbl(); break;
312 vcpu->arch.gpr[rt] = mftbu(); break;
315 vcpu->arch.gpr[rt] = vcpu->arch.sprg0; break;
317 vcpu->arch.gpr[rt] = vcpu->arch.sprg1; break;
319 vcpu->arch.gpr[rt] = vcpu->arch.sprg2; break;
321 vcpu->arch.gpr[rt] = vcpu->arch.sprg3; break;
322 /* Note: SPRG4-7 are user-readable, so we don't get
326 vcpu->arch.gpr[rt] = vcpu->arch.ivor[0]; break;
328 vcpu->arch.gpr[rt] = vcpu->arch.ivor[1]; break;
330 vcpu->arch.gpr[rt] = vcpu->arch.ivor[2]; break;
332 vcpu->arch.gpr[rt] = vcpu->arch.ivor[3]; break;
334 vcpu->arch.gpr[rt] = vcpu->arch.ivor[4]; break;
336 vcpu->arch.gpr[rt] = vcpu->arch.ivor[5]; break;
338 vcpu->arch.gpr[rt] = vcpu->arch.ivor[6]; break;
340 vcpu->arch.gpr[rt] = vcpu->arch.ivor[7]; break;
342 vcpu->arch.gpr[rt] = vcpu->arch.ivor[8]; break;
344 vcpu->arch.gpr[rt] = vcpu->arch.ivor[9]; break;
346 vcpu->arch.gpr[rt] = vcpu->arch.ivor[10]; break;
348 vcpu->arch.gpr[rt] = vcpu->arch.ivor[11]; break;
350 vcpu->arch.gpr[rt] = vcpu->arch.ivor[12]; break;
352 vcpu->arch.gpr[rt] = vcpu->arch.ivor[13]; break;
354 vcpu->arch.gpr[rt] = vcpu->arch.ivor[14]; break;
356 vcpu->arch.gpr[rt] = vcpu->arch.ivor[15]; break;
359 printk("mfspr: unknown spr %x\n", sprn);
360 vcpu->arch.gpr[rt] = 0;
370 emulated = kvmppc_handle_store(run, vcpu,
375 case 439: /* sthux */
380 ea = vcpu->arch.gpr[rb];
382 ea += vcpu->arch.gpr[ra];
384 emulated = kvmppc_handle_store(run, vcpu,
387 vcpu->arch.gpr[ra] = ea;
390 case 451: /* mtdcr */
391 dcrn = get_dcrn(inst);
394 /* emulate some access in kernel */
396 case DCRN_CPR0_CONFIG_ADDR:
397 vcpu->arch.cpr0_cfgaddr = vcpu->arch.gpr[rs];
400 run->dcr.dcrn = dcrn;
401 run->dcr.data = vcpu->arch.gpr[rs];
402 run->dcr.is_write = 1;
403 vcpu->arch.dcr_needed = 1;
404 emulated = EMULATE_DO_DCR;
409 case 467: /* mtspr */
410 sprn = get_sprn(inst);
414 vcpu->arch.srr0 = vcpu->arch.gpr[rs]; break;
416 vcpu->arch.srr1 = vcpu->arch.gpr[rs]; break;
418 vcpu->arch.mmucr = vcpu->arch.gpr[rs]; break;
420 kvmppc_set_pid(vcpu, vcpu->arch.gpr[rs]); break;
422 vcpu->arch.ccr0 = vcpu->arch.gpr[rs]; break;
424 vcpu->arch.ccr1 = vcpu->arch.gpr[rs]; break;
426 vcpu->arch.dear = vcpu->arch.gpr[rs]; break;
428 vcpu->arch.esr = vcpu->arch.gpr[rs]; break;
430 vcpu->arch.dbcr0 = vcpu->arch.gpr[rs]; break;
432 vcpu->arch.dbcr1 = vcpu->arch.gpr[rs]; break;
434 /* XXX We need to context-switch the timebase for
435 * watchdog and FIT. */
436 case SPRN_TBWL: break;
437 case SPRN_TBWU: break;
440 vcpu->arch.dec = vcpu->arch.gpr[rs];
441 kvmppc_emulate_dec(vcpu);
445 vcpu->arch.tsr &= ~vcpu->arch.gpr[rs]; break;
448 vcpu->arch.tcr = vcpu->arch.gpr[rs];
449 kvmppc_emulate_dec(vcpu);
453 vcpu->arch.sprg0 = vcpu->arch.gpr[rs]; break;
455 vcpu->arch.sprg1 = vcpu->arch.gpr[rs]; break;
457 vcpu->arch.sprg2 = vcpu->arch.gpr[rs]; break;
459 vcpu->arch.sprg3 = vcpu->arch.gpr[rs]; break;
461 /* Note: SPRG4-7 are user-readable. These values are
462 * loaded into the real SPRGs when resuming the
465 vcpu->arch.sprg4 = vcpu->arch.gpr[rs]; break;
467 vcpu->arch.sprg5 = vcpu->arch.gpr[rs]; break;
469 vcpu->arch.sprg6 = vcpu->arch.gpr[rs]; break;
471 vcpu->arch.sprg7 = vcpu->arch.gpr[rs]; break;
474 vcpu->arch.ivpr = vcpu->arch.gpr[rs]; break;
476 vcpu->arch.ivor[0] = vcpu->arch.gpr[rs]; break;
478 vcpu->arch.ivor[1] = vcpu->arch.gpr[rs]; break;
480 vcpu->arch.ivor[2] = vcpu->arch.gpr[rs]; break;
482 vcpu->arch.ivor[3] = vcpu->arch.gpr[rs]; break;
484 vcpu->arch.ivor[4] = vcpu->arch.gpr[rs]; break;
486 vcpu->arch.ivor[5] = vcpu->arch.gpr[rs]; break;
488 vcpu->arch.ivor[6] = vcpu->arch.gpr[rs]; break;
490 vcpu->arch.ivor[7] = vcpu->arch.gpr[rs]; break;
492 vcpu->arch.ivor[8] = vcpu->arch.gpr[rs]; break;
494 vcpu->arch.ivor[9] = vcpu->arch.gpr[rs]; break;
496 vcpu->arch.ivor[10] = vcpu->arch.gpr[rs]; break;
498 vcpu->arch.ivor[11] = vcpu->arch.gpr[rs]; break;
500 vcpu->arch.ivor[12] = vcpu->arch.gpr[rs]; break;
502 vcpu->arch.ivor[13] = vcpu->arch.gpr[rs]; break;
504 vcpu->arch.ivor[14] = vcpu->arch.gpr[rs]; break;
506 vcpu->arch.ivor[15] = vcpu->arch.gpr[rs]; break;
509 printk("mtspr: unknown spr %x\n", sprn);
510 emulated = EMULATE_FAIL;
516 /* Do nothing. The guest is performing dcbi because
517 * hardware DMA is not snooped by the dcache, but
518 * emulated DMA either goes through the dcache as
519 * normal writes, or the host kernel has handled dcache
523 case 534: /* lwbrx */
525 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
528 case 566: /* tlbsync */
531 case 662: /* stwbrx */
536 emulated = kvmppc_handle_store(run, vcpu,
541 case 978: /* tlbwe */
545 emulated = kvmppc_emul_tlbwe(vcpu, ra, rs, ws);
548 case 914: /* tlbsx */
553 emulated = kvmppc_emul_tlbsx(vcpu, rt, ra, rb, rc);
556 case 790: /* lhbrx */
558 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
561 case 918: /* sthbrx */
566 emulated = kvmppc_handle_store(run, vcpu,
571 case 966: /* iccci */
575 printk("unknown: op %d xop %d\n", get_op(inst),
577 emulated = EMULATE_FAIL;
584 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
590 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
591 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
596 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
602 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
603 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
608 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
615 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
617 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
622 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
629 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
631 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
636 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
642 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
643 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
648 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
655 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
657 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
661 printk("unknown op %d\n", get_op(inst));
662 emulated = EMULATE_FAIL;
666 KVMTRACE_3D(PPC_INSTR, vcpu, inst, vcpu->arch.pc, emulated, entryexit);
669 vcpu->arch.pc += 4; /* Advance past emulated instruction. */