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_DCBF 86
39 #define OP_31_XOP_LBZX 87
40 #define OP_31_XOP_STWX 151
41 #define OP_31_XOP_STBX 215
42 #define OP_31_XOP_LBZUX 119
43 #define OP_31_XOP_STBUX 247
44 #define OP_31_XOP_LHZX 279
45 #define OP_31_XOP_LHZUX 311
46 #define OP_31_XOP_MFSPR 339
47 #define OP_31_XOP_LHAX 343
48 #define OP_31_XOP_STHX 407
49 #define OP_31_XOP_STHUX 439
50 #define OP_31_XOP_MTSPR 467
51 #define OP_31_XOP_DCBI 470
52 #define OP_31_XOP_LWBRX 534
53 #define OP_31_XOP_TLBSYNC 566
54 #define OP_31_XOP_STWBRX 662
55 #define OP_31_XOP_LHBRX 790
56 #define OP_31_XOP_STHBRX 918
73 #ifdef CONFIG_PPC_BOOK3S
74 static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
79 static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
81 return vcpu->arch.tcr & TCR_DIE;
85 void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
87 unsigned long dec_nsec;
89 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
90 #ifdef CONFIG_PPC_BOOK3S
91 /* mtdec lowers the interrupt line when positive. */
92 kvmppc_core_dequeue_dec(vcpu);
94 /* POWER4+ triggers a dec interrupt if the value is < 0 */
95 if (vcpu->arch.dec & 0x80000000) {
96 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
97 kvmppc_core_queue_dec(vcpu);
101 if (kvmppc_dec_enabled(vcpu)) {
102 /* The decrementer ticks at the same rate as the timebase, so
103 * that's how we convert the guest DEC value to the number of
106 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
107 dec_nsec = vcpu->arch.dec;
109 dec_nsec /= tb_ticks_per_usec;
110 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
112 vcpu->arch.dec_jiffies = get_tb();
114 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
118 u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
120 u64 jd = tb - vcpu->arch.dec_jiffies;
121 return vcpu->arch.dec - jd;
136 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
138 /* XXX Should probably auto-generate instruction decoding for a particular core
139 * from opcode tables in the future. */
140 int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
142 u32 inst = kvmppc_get_last_inst(vcpu);
149 enum emulation_result emulated = EMULATE_DONE;
152 /* this default type might be overwritten by subcategories */
153 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
155 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
157 switch (get_op(inst)) {
159 #ifdef CONFIG_PPC_BOOK3S
161 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
163 kvmppc_core_queue_program(vcpu, vcpu->arch.esr | ESR_PTR);
169 switch (get_xop(inst)) {
173 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
178 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
181 case OP_31_XOP_LBZUX:
186 ea = kvmppc_get_gpr(vcpu, rb);
188 ea += kvmppc_get_gpr(vcpu, ra);
190 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
191 kvmppc_set_gpr(vcpu, ra, ea);
196 emulated = kvmppc_handle_store(run, vcpu,
197 kvmppc_get_gpr(vcpu, rs),
203 emulated = kvmppc_handle_store(run, vcpu,
204 kvmppc_get_gpr(vcpu, rs),
208 case OP_31_XOP_STBUX:
213 ea = kvmppc_get_gpr(vcpu, rb);
215 ea += kvmppc_get_gpr(vcpu, ra);
217 emulated = kvmppc_handle_store(run, vcpu,
218 kvmppc_get_gpr(vcpu, rs),
220 kvmppc_set_gpr(vcpu, rs, ea);
225 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
230 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
233 case OP_31_XOP_LHZUX:
238 ea = kvmppc_get_gpr(vcpu, rb);
240 ea += kvmppc_get_gpr(vcpu, ra);
242 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
243 kvmppc_set_gpr(vcpu, ra, ea);
246 case OP_31_XOP_MFSPR:
247 sprn = get_sprn(inst);
252 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr0);
255 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr1);
258 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
260 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
262 kvmppc_set_gpr(vcpu, rt, 0); break;
264 /* Note: mftb and TBRL/TBWL are user-accessible, so
265 * the guest can always access the real TB anyways.
266 * In fact, we probably will never see these traps. */
268 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
270 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
273 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg0);
276 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg1);
279 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg2);
282 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg3);
284 /* Note: SPRG4-7 are user-readable, so we don't get
289 kvmppc_set_gpr(vcpu, rt,
290 kvmppc_get_dec(vcpu, get_tb()));
294 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
295 if (emulated == EMULATE_FAIL) {
296 printk("mfspr: unknown spr %x\n", sprn);
297 kvmppc_set_gpr(vcpu, rt, 0);
301 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
309 emulated = kvmppc_handle_store(run, vcpu,
310 kvmppc_get_gpr(vcpu, rs),
314 case OP_31_XOP_STHUX:
319 ea = kvmppc_get_gpr(vcpu, rb);
321 ea += kvmppc_get_gpr(vcpu, ra);
323 emulated = kvmppc_handle_store(run, vcpu,
324 kvmppc_get_gpr(vcpu, rs),
326 kvmppc_set_gpr(vcpu, ra, ea);
329 case OP_31_XOP_MTSPR:
330 sprn = get_sprn(inst);
334 vcpu->arch.shared->srr0 = kvmppc_get_gpr(vcpu, rs);
337 vcpu->arch.shared->srr1 = kvmppc_get_gpr(vcpu, rs);
340 /* XXX We need to context-switch the timebase for
341 * watchdog and FIT. */
342 case SPRN_TBWL: break;
343 case SPRN_TBWU: break;
345 case SPRN_MSSSR0: break;
348 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
349 kvmppc_emulate_dec(vcpu);
353 vcpu->arch.shared->sprg0 = kvmppc_get_gpr(vcpu, rs);
356 vcpu->arch.shared->sprg1 = kvmppc_get_gpr(vcpu, rs);
359 vcpu->arch.shared->sprg2 = kvmppc_get_gpr(vcpu, rs);
362 vcpu->arch.shared->sprg3 = kvmppc_get_gpr(vcpu, rs);
366 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
367 if (emulated == EMULATE_FAIL)
368 printk("mtspr: unknown spr %x\n", sprn);
371 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
376 /* Do nothing. The guest is performing dcbi because
377 * hardware DMA is not snooped by the dcache, but
378 * emulated DMA either goes through the dcache as
379 * normal writes, or the host kernel has handled dcache
383 case OP_31_XOP_LWBRX:
385 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
388 case OP_31_XOP_TLBSYNC:
391 case OP_31_XOP_STWBRX:
396 emulated = kvmppc_handle_store(run, vcpu,
397 kvmppc_get_gpr(vcpu, rs),
401 case OP_31_XOP_LHBRX:
403 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
406 case OP_31_XOP_STHBRX:
411 emulated = kvmppc_handle_store(run, vcpu,
412 kvmppc_get_gpr(vcpu, rs),
417 /* Attempt core-specific emulation below. */
418 emulated = EMULATE_FAIL;
424 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
430 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
431 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
436 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
442 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
443 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
448 emulated = kvmppc_handle_store(run, vcpu,
449 kvmppc_get_gpr(vcpu, rs),
456 emulated = kvmppc_handle_store(run, vcpu,
457 kvmppc_get_gpr(vcpu, rs),
459 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
464 emulated = kvmppc_handle_store(run, vcpu,
465 kvmppc_get_gpr(vcpu, rs),
472 emulated = kvmppc_handle_store(run, vcpu,
473 kvmppc_get_gpr(vcpu, rs),
475 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
480 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
486 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
487 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
492 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
498 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
499 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
504 emulated = kvmppc_handle_store(run, vcpu,
505 kvmppc_get_gpr(vcpu, rs),
512 emulated = kvmppc_handle_store(run, vcpu,
513 kvmppc_get_gpr(vcpu, rs),
515 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
519 emulated = EMULATE_FAIL;
522 if (emulated == EMULATE_FAIL) {
523 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
524 if (emulated == EMULATE_AGAIN) {
526 } else if (emulated == EMULATE_FAIL) {
528 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
529 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
530 kvmppc_core_queue_program(vcpu, 0);
534 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
536 /* Advance past emulated instruction. */
538 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);