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/hrtimer.h>
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/kvm_host.h>
28 #include <asm/byteorder.h>
29 #include <asm/kvm_ppc.h>
30 #include <asm/disassemble.h>
37 #define OP_31_XOP_LWZX 23
38 #define OP_31_XOP_LBZX 87
39 #define OP_31_XOP_STWX 151
40 #define OP_31_XOP_STBX 215
41 #define OP_31_XOP_LBZUX 119
42 #define OP_31_XOP_STBUX 247
43 #define OP_31_XOP_LHZX 279
44 #define OP_31_XOP_LHZUX 311
45 #define OP_31_XOP_MFSPR 339
46 #define OP_31_XOP_LHAX 343
47 #define OP_31_XOP_STHX 407
48 #define OP_31_XOP_STHUX 439
49 #define OP_31_XOP_MTSPR 467
50 #define OP_31_XOP_DCBI 470
51 #define OP_31_XOP_LWBRX 534
52 #define OP_31_XOP_TLBSYNC 566
53 #define OP_31_XOP_STWBRX 662
54 #define OP_31_XOP_LHBRX 790
55 #define OP_31_XOP_STHBRX 918
73 static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
78 static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
80 return vcpu->arch.tcr & TCR_DIE;
84 void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
86 unsigned long dec_nsec;
88 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
90 /* mtdec lowers the interrupt line when positive. */
91 kvmppc_core_dequeue_dec(vcpu);
93 /* POWER4+ triggers a dec interrupt if the value is < 0 */
94 if (vcpu->arch.dec & 0x80000000) {
95 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
96 kvmppc_core_queue_dec(vcpu);
100 if (kvmppc_dec_enabled(vcpu)) {
101 /* The decrementer ticks at the same rate as the timebase, so
102 * that's how we convert the guest DEC value to the number of
105 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
106 dec_nsec = vcpu->arch.dec;
108 dec_nsec /= tb_ticks_per_usec;
109 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
111 vcpu->arch.dec_jiffies = get_tb();
113 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
129 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
131 /* XXX Should probably auto-generate instruction decoding for a particular core
132 * from opcode tables in the future. */
133 int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
135 u32 inst = kvmppc_get_last_inst(vcpu);
142 enum emulation_result emulated = EMULATE_DONE;
145 /* this default type might be overwritten by subcategories */
146 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
148 pr_debug(KERN_INFO "Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
150 /* Try again next time */
151 if (inst == KVM_INST_FETCH_FAILED)
154 switch (get_op(inst)) {
158 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
160 kvmppc_core_queue_program(vcpu, vcpu->arch.esr | ESR_PTR);
166 switch (get_xop(inst)) {
170 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
175 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
178 case OP_31_XOP_LBZUX:
183 ea = kvmppc_get_gpr(vcpu, rb);
185 ea += kvmppc_get_gpr(vcpu, ra);
187 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
188 kvmppc_set_gpr(vcpu, ra, ea);
193 emulated = kvmppc_handle_store(run, vcpu,
194 kvmppc_get_gpr(vcpu, rs),
200 emulated = kvmppc_handle_store(run, vcpu,
201 kvmppc_get_gpr(vcpu, rs),
205 case OP_31_XOP_STBUX:
210 ea = kvmppc_get_gpr(vcpu, rb);
212 ea += kvmppc_get_gpr(vcpu, ra);
214 emulated = kvmppc_handle_store(run, vcpu,
215 kvmppc_get_gpr(vcpu, rs),
217 kvmppc_set_gpr(vcpu, rs, ea);
222 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
227 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
230 case OP_31_XOP_LHZUX:
235 ea = kvmppc_get_gpr(vcpu, rb);
237 ea += kvmppc_get_gpr(vcpu, ra);
239 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
240 kvmppc_set_gpr(vcpu, ra, ea);
243 case OP_31_XOP_MFSPR:
244 sprn = get_sprn(inst);
249 kvmppc_set_gpr(vcpu, rt, vcpu->arch.srr0); break;
251 kvmppc_set_gpr(vcpu, rt, vcpu->arch.srr1); break;
253 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
255 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
257 kvmppc_set_gpr(vcpu, rt, 0); break;
259 /* Note: mftb and TBRL/TBWL are user-accessible, so
260 * the guest can always access the real TB anyways.
261 * In fact, we probably will never see these traps. */
263 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
265 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
268 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg0); break;
270 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg1); break;
272 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg2); break;
274 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg3); break;
275 /* Note: SPRG4-7 are user-readable, so we don't get
280 u64 jd = get_tb() - vcpu->arch.dec_jiffies;
281 kvmppc_set_gpr(vcpu, rt, vcpu->arch.dec - jd);
282 pr_debug(KERN_INFO "mfDEC: %x - %llx = %lx\n",
284 kvmppc_get_gpr(vcpu, rt));
288 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
289 if (emulated == EMULATE_FAIL) {
290 printk("mfspr: unknown spr %x\n", sprn);
291 kvmppc_set_gpr(vcpu, rt, 0);
302 emulated = kvmppc_handle_store(run, vcpu,
303 kvmppc_get_gpr(vcpu, rs),
307 case OP_31_XOP_STHUX:
312 ea = kvmppc_get_gpr(vcpu, rb);
314 ea += kvmppc_get_gpr(vcpu, ra);
316 emulated = kvmppc_handle_store(run, vcpu,
317 kvmppc_get_gpr(vcpu, rs),
319 kvmppc_set_gpr(vcpu, ra, ea);
322 case OP_31_XOP_MTSPR:
323 sprn = get_sprn(inst);
327 vcpu->arch.srr0 = kvmppc_get_gpr(vcpu, rs); break;
329 vcpu->arch.srr1 = kvmppc_get_gpr(vcpu, rs); break;
331 /* XXX We need to context-switch the timebase for
332 * watchdog and FIT. */
333 case SPRN_TBWL: break;
334 case SPRN_TBWU: break;
336 case SPRN_MSSSR0: break;
339 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
340 kvmppc_emulate_dec(vcpu);
344 vcpu->arch.sprg0 = kvmppc_get_gpr(vcpu, rs); break;
346 vcpu->arch.sprg1 = kvmppc_get_gpr(vcpu, rs); break;
348 vcpu->arch.sprg2 = kvmppc_get_gpr(vcpu, rs); break;
350 vcpu->arch.sprg3 = kvmppc_get_gpr(vcpu, rs); break;
353 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
354 if (emulated == EMULATE_FAIL)
355 printk("mtspr: unknown spr %x\n", sprn);
361 /* Do nothing. The guest is performing dcbi because
362 * hardware DMA is not snooped by the dcache, but
363 * emulated DMA either goes through the dcache as
364 * normal writes, or the host kernel has handled dcache
368 case OP_31_XOP_LWBRX:
370 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
373 case OP_31_XOP_TLBSYNC:
376 case OP_31_XOP_STWBRX:
381 emulated = kvmppc_handle_store(run, vcpu,
382 kvmppc_get_gpr(vcpu, rs),
386 case OP_31_XOP_LHBRX:
388 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
391 case OP_31_XOP_STHBRX:
396 emulated = kvmppc_handle_store(run, vcpu,
397 kvmppc_get_gpr(vcpu, rs),
402 /* Attempt core-specific emulation below. */
403 emulated = EMULATE_FAIL;
409 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
415 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
416 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
421 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
427 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
428 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
433 emulated = kvmppc_handle_store(run, vcpu,
434 kvmppc_get_gpr(vcpu, rs),
441 emulated = kvmppc_handle_store(run, vcpu,
442 kvmppc_get_gpr(vcpu, rs),
444 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
449 emulated = kvmppc_handle_store(run, vcpu,
450 kvmppc_get_gpr(vcpu, rs),
457 emulated = kvmppc_handle_store(run, vcpu,
458 kvmppc_get_gpr(vcpu, rs),
460 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
465 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
471 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
472 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
477 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
483 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
484 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
489 emulated = kvmppc_handle_store(run, vcpu,
490 kvmppc_get_gpr(vcpu, rs),
497 emulated = kvmppc_handle_store(run, vcpu,
498 kvmppc_get_gpr(vcpu, rs),
500 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
504 emulated = EMULATE_FAIL;
507 if (emulated == EMULATE_FAIL) {
508 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
509 if (emulated == EMULATE_AGAIN) {
511 } else if (emulated == EMULATE_FAIL) {
513 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
514 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
515 kvmppc_core_queue_program(vcpu, 0);
519 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
521 /* Advance past emulated instruction. */
523 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);