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
16 * Copyright 2011 Freescale Semiconductor, Inc.
21 #include <linux/jiffies.h>
22 #include <linux/hrtimer.h>
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/kvm_host.h>
26 #include <linux/clockchips.h>
30 #include <asm/byteorder.h>
31 #include <asm/kvm_ppc.h>
32 #include <asm/disassemble.h>
33 #include <asm/ppc-opcode.h>
37 void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
39 unsigned long dec_nsec;
40 unsigned long long dec_time;
42 pr_debug("mtDEC: %lx\n", vcpu->arch.dec);
43 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
45 #ifdef CONFIG_PPC_BOOK3S
46 /* mtdec lowers the interrupt line when positive. */
47 kvmppc_core_dequeue_dec(vcpu);
51 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
52 if (vcpu->arch.dec == 0)
57 * The decrementer ticks at the same rate as the timebase, so
58 * that's how we convert the guest DEC value to the number of
62 dec_time = vcpu->arch.dec;
64 * Guest timebase ticks at the same frequency as host timebase.
65 * So use the host timebase calculations for decrementer emulation.
67 dec_time = tb_to_ns(dec_time);
68 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
69 hrtimer_start(&vcpu->arch.dec_timer,
70 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
71 vcpu->arch.dec_jiffies = get_tb();
74 u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
76 u64 jd = tb - vcpu->arch.dec_jiffies;
79 if (vcpu->arch.dec < jd)
83 return vcpu->arch.dec - jd;
86 static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
88 enum emulation_result emulated = EMULATE_DONE;
89 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
93 kvmppc_set_srr0(vcpu, spr_val);
96 kvmppc_set_srr1(vcpu, spr_val);
99 /* XXX We need to context-switch the timebase for
100 * watchdog and FIT. */
101 case SPRN_TBWL: break;
102 case SPRN_TBWU: break;
105 vcpu->arch.dec = (u32) spr_val;
106 kvmppc_emulate_dec(vcpu);
110 kvmppc_set_sprg0(vcpu, spr_val);
113 kvmppc_set_sprg1(vcpu, spr_val);
116 kvmppc_set_sprg2(vcpu, spr_val);
119 kvmppc_set_sprg3(vcpu, spr_val);
122 /* PIR can legally be written, but we ignore it */
123 case SPRN_PIR: break;
126 emulated = vcpu->kvm->arch.kvm_ops->emulate_mtspr(vcpu, sprn,
128 if (emulated == EMULATE_FAIL)
129 printk(KERN_INFO "mtspr: unknown spr "
134 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
139 static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
141 enum emulation_result emulated = EMULATE_DONE;
146 spr_val = kvmppc_get_srr0(vcpu);
149 spr_val = kvmppc_get_srr1(vcpu);
152 spr_val = vcpu->arch.pvr;
155 spr_val = vcpu->vcpu_id;
158 /* Note: mftb and TBRL/TBWL are user-accessible, so
159 * the guest can always access the real TB anyways.
160 * In fact, we probably will never see these traps. */
162 spr_val = get_tb() >> 32;
169 spr_val = kvmppc_get_sprg0(vcpu);
172 spr_val = kvmppc_get_sprg1(vcpu);
175 spr_val = kvmppc_get_sprg2(vcpu);
178 spr_val = kvmppc_get_sprg3(vcpu);
180 /* Note: SPRG4-7 are user-readable, so we don't get
184 spr_val = kvmppc_get_dec(vcpu, get_tb());
187 emulated = vcpu->kvm->arch.kvm_ops->emulate_mfspr(vcpu, sprn,
189 if (unlikely(emulated == EMULATE_FAIL)) {
190 printk(KERN_INFO "mfspr: unknown spr "
196 if (emulated == EMULATE_DONE)
197 kvmppc_set_gpr(vcpu, rt, spr_val);
198 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
203 /* XXX Should probably auto-generate instruction decoding for a particular core
204 * from opcode tables in the future. */
205 int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
209 enum emulation_result emulated;
212 /* this default type might be overwritten by subcategories */
213 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
215 emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst);
216 if (emulated != EMULATE_DONE)
219 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
223 sprn = get_sprn(inst);
225 switch (get_op(inst)) {
227 #ifdef CONFIG_PPC_BOOK3S
229 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
231 kvmppc_core_queue_program(vcpu,
232 vcpu->arch.shared->esr | ESR_PTR);
238 switch (get_xop(inst)) {
242 case OP_31_XOP_TRAP_64:
244 #ifdef CONFIG_PPC_BOOK3S
245 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
247 kvmppc_core_queue_program(vcpu,
248 vcpu->arch.shared->esr | ESR_PTR);
253 case OP_31_XOP_MFSPR:
254 emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
255 if (emulated == EMULATE_AGAIN) {
256 emulated = EMULATE_DONE;
261 case OP_31_XOP_MTSPR:
262 emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
263 if (emulated == EMULATE_AGAIN) {
264 emulated = EMULATE_DONE;
269 case OP_31_XOP_TLBSYNC:
273 /* Attempt core-specific emulation below. */
274 emulated = EMULATE_FAIL;
280 * Instruction with primary opcode 0. Based on PowerISA
281 * these are illegal instructions.
283 if (inst == KVMPPC_INST_SW_BREAKPOINT) {
284 run->exit_reason = KVM_EXIT_DEBUG;
285 run->debug.arch.address = kvmppc_get_pc(vcpu);
286 emulated = EMULATE_EXIT_USER;
289 emulated = EMULATE_FAIL;
294 emulated = EMULATE_FAIL;
297 if (emulated == EMULATE_FAIL) {
298 emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst,
300 if (emulated == EMULATE_AGAIN) {
302 } else if (emulated == EMULATE_FAIL) {
304 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
305 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
309 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
311 /* Advance past emulated instruction. */
313 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
317 EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction);