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>
39 #define OP_31_XOP_TRAP 4
40 #define OP_31_XOP_LWZX 23
41 #define OP_31_XOP_TRAP_64 68
42 #define OP_31_XOP_LBZX 87
43 #define OP_31_XOP_STWX 151
44 #define OP_31_XOP_STBX 215
45 #define OP_31_XOP_LBZUX 119
46 #define OP_31_XOP_STBUX 247
47 #define OP_31_XOP_LHZX 279
48 #define OP_31_XOP_LHZUX 311
49 #define OP_31_XOP_MFSPR 339
50 #define OP_31_XOP_LHAX 343
51 #define OP_31_XOP_STHX 407
52 #define OP_31_XOP_STHUX 439
53 #define OP_31_XOP_MTSPR 467
54 #define OP_31_XOP_DCBI 470
55 #define OP_31_XOP_LWBRX 534
56 #define OP_31_XOP_TLBSYNC 566
57 #define OP_31_XOP_STWBRX 662
58 #define OP_31_XOP_LHBRX 790
59 #define OP_31_XOP_STHBRX 918
78 void kvmppc_emulate_dec(struct kvm_vcpu
*vcpu
)
80 unsigned long dec_nsec
;
81 unsigned long long dec_time
;
83 pr_debug("mtDEC: %x\n", vcpu
->arch
.dec
);
84 hrtimer_try_to_cancel(&vcpu
->arch
.dec_timer
);
86 #ifdef CONFIG_PPC_BOOK3S
87 /* mtdec lowers the interrupt line when positive. */
88 kvmppc_core_dequeue_dec(vcpu
);
90 /* POWER4+ triggers a dec interrupt if the value is < 0 */
91 if (vcpu
->arch
.dec
& 0x80000000) {
92 kvmppc_core_queue_dec(vcpu
);
98 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
99 if (vcpu
->arch
.dec
== 0)
104 * The decrementer ticks at the same rate as the timebase, so
105 * that's how we convert the guest DEC value to the number of
109 dec_time
= vcpu
->arch
.dec
;
111 * Guest timebase ticks at the same frequency as host decrementer.
112 * So use the host decrementer calculations for decrementer emulation.
114 dec_time
= dec_time
<< decrementer_clockevent
.shift
;
115 do_div(dec_time
, decrementer_clockevent
.mult
);
116 dec_nsec
= do_div(dec_time
, NSEC_PER_SEC
);
117 hrtimer_start(&vcpu
->arch
.dec_timer
,
118 ktime_set(dec_time
, dec_nsec
), HRTIMER_MODE_REL
);
119 vcpu
->arch
.dec_jiffies
= get_tb();
122 u32
kvmppc_get_dec(struct kvm_vcpu
*vcpu
, u64 tb
)
124 u64 jd
= tb
- vcpu
->arch
.dec_jiffies
;
127 if (vcpu
->arch
.dec
< jd
)
131 return vcpu
->arch
.dec
- jd
;
134 static int kvmppc_emulate_mtspr(struct kvm_vcpu
*vcpu
, int sprn
, int rs
)
136 enum emulation_result emulated
= EMULATE_DONE
;
137 ulong spr_val
= kvmppc_get_gpr(vcpu
, rs
);
141 vcpu
->arch
.shared
->srr0
= spr_val
;
144 vcpu
->arch
.shared
->srr1
= spr_val
;
147 /* XXX We need to context-switch the timebase for
148 * watchdog and FIT. */
149 case SPRN_TBWL
: break;
150 case SPRN_TBWU
: break;
152 case SPRN_MSSSR0
: break;
155 vcpu
->arch
.dec
= spr_val
;
156 kvmppc_emulate_dec(vcpu
);
160 vcpu
->arch
.shared
->sprg0
= spr_val
;
163 vcpu
->arch
.shared
->sprg1
= spr_val
;
166 vcpu
->arch
.shared
->sprg2
= spr_val
;
169 vcpu
->arch
.shared
->sprg3
= spr_val
;
173 emulated
= kvmppc_core_emulate_mtspr(vcpu
, sprn
,
175 if (emulated
== EMULATE_FAIL
)
176 printk(KERN_INFO
"mtspr: unknown spr "
181 kvmppc_set_exit_type(vcpu
, EMULATED_MTSPR_EXITS
);
186 static int kvmppc_emulate_mfspr(struct kvm_vcpu
*vcpu
, int sprn
, int rt
)
188 enum emulation_result emulated
= EMULATE_DONE
;
193 spr_val
= vcpu
->arch
.shared
->srr0
;
196 spr_val
= vcpu
->arch
.shared
->srr1
;
199 spr_val
= vcpu
->arch
.pvr
;
202 spr_val
= vcpu
->vcpu_id
;
208 /* Note: mftb and TBRL/TBWL are user-accessible, so
209 * the guest can always access the real TB anyways.
210 * In fact, we probably will never see these traps. */
212 spr_val
= get_tb() >> 32;
219 spr_val
= vcpu
->arch
.shared
->sprg0
;
222 spr_val
= vcpu
->arch
.shared
->sprg1
;
225 spr_val
= vcpu
->arch
.shared
->sprg2
;
228 spr_val
= vcpu
->arch
.shared
->sprg3
;
230 /* Note: SPRG4-7 are user-readable, so we don't get
234 spr_val
= kvmppc_get_dec(vcpu
, get_tb());
237 emulated
= kvmppc_core_emulate_mfspr(vcpu
, sprn
,
239 if (unlikely(emulated
== EMULATE_FAIL
)) {
240 printk(KERN_INFO
"mfspr: unknown spr "
246 if (emulated
== EMULATE_DONE
)
247 kvmppc_set_gpr(vcpu
, rt
, spr_val
);
248 kvmppc_set_exit_type(vcpu
, EMULATED_MFSPR_EXITS
);
265 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
267 /* XXX Should probably auto-generate instruction decoding for a particular core
268 * from opcode tables in the future. */
269 int kvmppc_emulate_instruction(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
271 u32 inst
= kvmppc_get_last_inst(vcpu
);
272 int ra
= get_ra(inst
);
273 int rs
= get_rs(inst
);
274 int rt
= get_rt(inst
);
275 int sprn
= get_sprn(inst
);
276 enum emulation_result emulated
= EMULATE_DONE
;
279 /* this default type might be overwritten by subcategories */
280 kvmppc_set_exit_type(vcpu
, EMULATED_INST_EXITS
);
282 pr_debug("Emulating opcode %d / %d\n", get_op(inst
), get_xop(inst
));
284 switch (get_op(inst
)) {
286 #ifdef CONFIG_PPC_BOOK3S
288 kvmppc_core_queue_program(vcpu
, SRR1_PROGTRAP
);
290 kvmppc_core_queue_program(vcpu
,
291 vcpu
->arch
.shared
->esr
| ESR_PTR
);
297 switch (get_xop(inst
)) {
301 case OP_31_XOP_TRAP_64
:
303 #ifdef CONFIG_PPC_BOOK3S
304 kvmppc_core_queue_program(vcpu
, SRR1_PROGTRAP
);
306 kvmppc_core_queue_program(vcpu
,
307 vcpu
->arch
.shared
->esr
| ESR_PTR
);
312 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
316 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
319 case OP_31_XOP_LBZUX
:
320 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
321 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
325 emulated
= kvmppc_handle_store(run
, vcpu
,
326 kvmppc_get_gpr(vcpu
, rs
),
331 emulated
= kvmppc_handle_store(run
, vcpu
,
332 kvmppc_get_gpr(vcpu
, rs
),
336 case OP_31_XOP_STBUX
:
337 emulated
= kvmppc_handle_store(run
, vcpu
,
338 kvmppc_get_gpr(vcpu
, rs
),
340 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
344 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
348 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
351 case OP_31_XOP_LHZUX
:
352 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
353 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
356 case OP_31_XOP_MFSPR
:
357 emulated
= kvmppc_emulate_mfspr(vcpu
, sprn
, rt
);
361 emulated
= kvmppc_handle_store(run
, vcpu
,
362 kvmppc_get_gpr(vcpu
, rs
),
366 case OP_31_XOP_STHUX
:
367 emulated
= kvmppc_handle_store(run
, vcpu
,
368 kvmppc_get_gpr(vcpu
, rs
),
370 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
373 case OP_31_XOP_MTSPR
:
374 emulated
= kvmppc_emulate_mtspr(vcpu
, sprn
, rs
);
378 /* Do nothing. The guest is performing dcbi because
379 * hardware DMA is not snooped by the dcache, but
380 * emulated DMA either goes through the dcache as
381 * normal writes, or the host kernel has handled dcache
385 case OP_31_XOP_LWBRX
:
386 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 0);
389 case OP_31_XOP_TLBSYNC
:
392 case OP_31_XOP_STWBRX
:
393 emulated
= kvmppc_handle_store(run
, vcpu
,
394 kvmppc_get_gpr(vcpu
, rs
),
398 case OP_31_XOP_LHBRX
:
399 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 0);
402 case OP_31_XOP_STHBRX
:
403 emulated
= kvmppc_handle_store(run
, vcpu
,
404 kvmppc_get_gpr(vcpu
, rs
),
409 /* Attempt core-specific emulation below. */
410 emulated
= EMULATE_FAIL
;
415 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
418 /* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */
421 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 8, 1);
425 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
426 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
430 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
434 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
435 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
439 emulated
= kvmppc_handle_store(run
, vcpu
,
440 kvmppc_get_gpr(vcpu
, rs
),
444 /* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */
447 emulated
= kvmppc_handle_store(run
, vcpu
,
448 kvmppc_get_gpr(vcpu
, rs
),
453 emulated
= kvmppc_handle_store(run
, vcpu
,
454 kvmppc_get_gpr(vcpu
, rs
),
456 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
460 emulated
= kvmppc_handle_store(run
, vcpu
,
461 kvmppc_get_gpr(vcpu
, rs
),
466 emulated
= kvmppc_handle_store(run
, vcpu
,
467 kvmppc_get_gpr(vcpu
, rs
),
469 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
473 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
477 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
478 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
482 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
486 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
487 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
491 emulated
= kvmppc_handle_store(run
, vcpu
,
492 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
.vaddr_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);