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.
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
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: %x\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
);
49 /* POWER4+ triggers a dec interrupt if the value is < 0 */
50 if (vcpu
->arch
.dec
& 0x80000000) {
51 kvmppc_core_queue_dec(vcpu
);
57 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
58 if (vcpu
->arch
.dec
== 0)
63 * The decrementer ticks at the same rate as the timebase, so
64 * that's how we convert the guest DEC value to the number of
68 dec_time
= vcpu
->arch
.dec
;
70 * Guest timebase ticks at the same frequency as host decrementer.
71 * So use the host decrementer calculations for decrementer emulation.
73 dec_time
= dec_time
<< decrementer_clockevent
.shift
;
74 do_div(dec_time
, decrementer_clockevent
.mult
);
75 dec_nsec
= do_div(dec_time
, NSEC_PER_SEC
);
76 hrtimer_start(&vcpu
->arch
.dec_timer
,
77 ktime_set(dec_time
, dec_nsec
), HRTIMER_MODE_REL
);
78 vcpu
->arch
.dec_jiffies
= get_tb();
81 u32
kvmppc_get_dec(struct kvm_vcpu
*vcpu
, u64 tb
)
83 u64 jd
= tb
- vcpu
->arch
.dec_jiffies
;
86 if (vcpu
->arch
.dec
< jd
)
90 return vcpu
->arch
.dec
- jd
;
93 static int kvmppc_emulate_mtspr(struct kvm_vcpu
*vcpu
, int sprn
, int rs
)
95 enum emulation_result emulated
= EMULATE_DONE
;
96 ulong spr_val
= kvmppc_get_gpr(vcpu
, rs
);
100 vcpu
->arch
.shared
->srr0
= spr_val
;
103 vcpu
->arch
.shared
->srr1
= spr_val
;
106 /* XXX We need to context-switch the timebase for
107 * watchdog and FIT. */
108 case SPRN_TBWL
: break;
109 case SPRN_TBWU
: break;
112 vcpu
->arch
.dec
= spr_val
;
113 kvmppc_emulate_dec(vcpu
);
117 vcpu
->arch
.shared
->sprg0
= spr_val
;
120 vcpu
->arch
.shared
->sprg1
= spr_val
;
123 vcpu
->arch
.shared
->sprg2
= spr_val
;
126 vcpu
->arch
.shared
->sprg3
= spr_val
;
129 /* PIR can legally be written, but we ignore it */
130 case SPRN_PIR
: break;
133 emulated
= vcpu
->kvm
->arch
.kvm_ops
->emulate_mtspr(vcpu
, sprn
,
135 if (emulated
== EMULATE_FAIL
)
136 printk(KERN_INFO
"mtspr: unknown spr "
141 kvmppc_set_exit_type(vcpu
, EMULATED_MTSPR_EXITS
);
146 static int kvmppc_emulate_mfspr(struct kvm_vcpu
*vcpu
, int sprn
, int rt
)
148 enum emulation_result emulated
= EMULATE_DONE
;
153 spr_val
= vcpu
->arch
.shared
->srr0
;
156 spr_val
= vcpu
->arch
.shared
->srr1
;
159 spr_val
= vcpu
->arch
.pvr
;
162 spr_val
= vcpu
->vcpu_id
;
165 /* Note: mftb and TBRL/TBWL are user-accessible, so
166 * the guest can always access the real TB anyways.
167 * In fact, we probably will never see these traps. */
169 spr_val
= get_tb() >> 32;
176 spr_val
= vcpu
->arch
.shared
->sprg0
;
179 spr_val
= vcpu
->arch
.shared
->sprg1
;
182 spr_val
= vcpu
->arch
.shared
->sprg2
;
185 spr_val
= vcpu
->arch
.shared
->sprg3
;
187 /* Note: SPRG4-7 are user-readable, so we don't get
191 spr_val
= kvmppc_get_dec(vcpu
, get_tb());
194 emulated
= vcpu
->kvm
->arch
.kvm_ops
->emulate_mfspr(vcpu
, sprn
,
196 if (unlikely(emulated
== EMULATE_FAIL
)) {
197 printk(KERN_INFO
"mfspr: unknown spr "
203 if (emulated
== EMULATE_DONE
)
204 kvmppc_set_gpr(vcpu
, rt
, spr_val
);
205 kvmppc_set_exit_type(vcpu
, EMULATED_MFSPR_EXITS
);
223 /* XXX Should probably auto-generate instruction decoding for a particular core
224 * from opcode tables in the future. */
225 int kvmppc_emulate_instruction(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
227 u32 inst
= kvmppc_get_last_inst(vcpu
);
228 int ra
= get_ra(inst
);
229 int rs
= get_rs(inst
);
230 int rt
= get_rt(inst
);
231 int sprn
= get_sprn(inst
);
232 enum emulation_result emulated
= EMULATE_DONE
;
235 /* this default type might be overwritten by subcategories */
236 kvmppc_set_exit_type(vcpu
, EMULATED_INST_EXITS
);
238 pr_debug("Emulating opcode %d / %d\n", get_op(inst
), get_xop(inst
));
240 switch (get_op(inst
)) {
242 #ifdef CONFIG_PPC_BOOK3S
244 kvmppc_core_queue_program(vcpu
, SRR1_PROGTRAP
);
246 kvmppc_core_queue_program(vcpu
,
247 vcpu
->arch
.shared
->esr
| ESR_PTR
);
253 switch (get_xop(inst
)) {
257 case OP_31_XOP_TRAP_64
:
259 #ifdef CONFIG_PPC_BOOK3S
260 kvmppc_core_queue_program(vcpu
, SRR1_PROGTRAP
);
262 kvmppc_core_queue_program(vcpu
,
263 vcpu
->arch
.shared
->esr
| ESR_PTR
);
268 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
272 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
275 case OP_31_XOP_LBZUX
:
276 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
277 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
281 emulated
= kvmppc_handle_store(run
, vcpu
,
282 kvmppc_get_gpr(vcpu
, rs
),
287 emulated
= kvmppc_handle_store(run
, vcpu
,
288 kvmppc_get_gpr(vcpu
, rs
),
292 case OP_31_XOP_STBUX
:
293 emulated
= kvmppc_handle_store(run
, vcpu
,
294 kvmppc_get_gpr(vcpu
, rs
),
296 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
300 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
304 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
307 case OP_31_XOP_LHZUX
:
308 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
309 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
312 case OP_31_XOP_MFSPR
:
313 emulated
= kvmppc_emulate_mfspr(vcpu
, sprn
, rt
);
317 emulated
= kvmppc_handle_store(run
, vcpu
,
318 kvmppc_get_gpr(vcpu
, rs
),
322 case OP_31_XOP_STHUX
:
323 emulated
= kvmppc_handle_store(run
, vcpu
,
324 kvmppc_get_gpr(vcpu
, rs
),
326 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
329 case OP_31_XOP_MTSPR
:
330 emulated
= kvmppc_emulate_mtspr(vcpu
, sprn
, rs
);
333 case OP_31_XOP_DCBST
:
336 /* Do nothing. The guest is performing dcbi because
337 * hardware DMA is not snooped by the dcache, but
338 * emulated DMA either goes through the dcache as
339 * normal writes, or the host kernel has handled dcache
343 case OP_31_XOP_LWBRX
:
344 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 0);
347 case OP_31_XOP_TLBSYNC
:
350 case OP_31_XOP_STWBRX
:
351 emulated
= kvmppc_handle_store(run
, vcpu
,
352 kvmppc_get_gpr(vcpu
, rs
),
356 case OP_31_XOP_LHBRX
:
357 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 0);
360 case OP_31_XOP_STHBRX
:
361 emulated
= kvmppc_handle_store(run
, vcpu
,
362 kvmppc_get_gpr(vcpu
, rs
),
367 /* Attempt core-specific emulation below. */
368 emulated
= EMULATE_FAIL
;
373 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
376 /* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */
379 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 8, 1);
383 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
384 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
388 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
392 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
393 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
397 emulated
= kvmppc_handle_store(run
, vcpu
,
398 kvmppc_get_gpr(vcpu
, rs
),
402 /* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */
405 emulated
= kvmppc_handle_store(run
, vcpu
,
406 kvmppc_get_gpr(vcpu
, rs
),
411 emulated
= kvmppc_handle_store(run
, vcpu
,
412 kvmppc_get_gpr(vcpu
, rs
),
414 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
418 emulated
= kvmppc_handle_store(run
, vcpu
,
419 kvmppc_get_gpr(vcpu
, rs
),
424 emulated
= kvmppc_handle_store(run
, vcpu
,
425 kvmppc_get_gpr(vcpu
, rs
),
427 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
431 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
435 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
436 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
440 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
444 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
445 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
449 emulated
= kvmppc_handle_store(run
, vcpu
,
450 kvmppc_get_gpr(vcpu
, rs
),
455 emulated
= kvmppc_handle_store(run
, vcpu
,
456 kvmppc_get_gpr(vcpu
, rs
),
458 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
462 emulated
= EMULATE_FAIL
;
465 if (emulated
== EMULATE_FAIL
) {
466 emulated
= vcpu
->kvm
->arch
.kvm_ops
->emulate_op(run
, vcpu
, inst
,
468 if (emulated
== EMULATE_AGAIN
) {
470 } else if (emulated
== EMULATE_FAIL
) {
472 printk(KERN_ERR
"Couldn't emulate instruction 0x%08x "
473 "(op %d xop %d)\n", inst
, get_op(inst
), get_xop(inst
));
474 kvmppc_core_queue_program(vcpu
, 0);
478 trace_kvm_ppc_instr(inst
, kvmppc_get_pc(vcpu
), emulated
);
480 /* Advance past emulated instruction. */
482 kvmppc_set_pc(vcpu
, kvmppc_get_pc(vcpu
) + 4);
486 EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction
);