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
72 #ifdef CONFIG_PPC_BOOK3S
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
);
89 #ifdef CONFIG_PPC_BOOK3S
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
);
117 u32
kvmppc_get_dec(struct kvm_vcpu
*vcpu
, u64 tb
)
119 u64 jd
= tb
- vcpu
->arch
.dec_jiffies
;
120 return vcpu
->arch
.dec
- jd
;
135 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
137 /* XXX Should probably auto-generate instruction decoding for a particular core
138 * from opcode tables in the future. */
139 int kvmppc_emulate_instruction(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
141 u32 inst
= kvmppc_get_last_inst(vcpu
);
148 enum emulation_result emulated
= EMULATE_DONE
;
151 /* this default type might be overwritten by subcategories */
152 kvmppc_set_exit_type(vcpu
, EMULATED_INST_EXITS
);
154 pr_debug("Emulating opcode %d / %d\n", get_op(inst
), get_xop(inst
));
156 switch (get_op(inst
)) {
158 #ifdef CONFIG_PPC_BOOK3S
160 kvmppc_core_queue_program(vcpu
, SRR1_PROGTRAP
);
162 kvmppc_core_queue_program(vcpu
, vcpu
->arch
.esr
| ESR_PTR
);
168 switch (get_xop(inst
)) {
172 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
177 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
180 case OP_31_XOP_LBZUX
:
185 ea
= kvmppc_get_gpr(vcpu
, rb
);
187 ea
+= kvmppc_get_gpr(vcpu
, ra
);
189 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
190 kvmppc_set_gpr(vcpu
, ra
, ea
);
195 emulated
= kvmppc_handle_store(run
, vcpu
,
196 kvmppc_get_gpr(vcpu
, rs
),
202 emulated
= kvmppc_handle_store(run
, vcpu
,
203 kvmppc_get_gpr(vcpu
, rs
),
207 case OP_31_XOP_STBUX
:
212 ea
= kvmppc_get_gpr(vcpu
, rb
);
214 ea
+= kvmppc_get_gpr(vcpu
, ra
);
216 emulated
= kvmppc_handle_store(run
, vcpu
,
217 kvmppc_get_gpr(vcpu
, rs
),
219 kvmppc_set_gpr(vcpu
, rs
, ea
);
224 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
229 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
232 case OP_31_XOP_LHZUX
:
237 ea
= kvmppc_get_gpr(vcpu
, rb
);
239 ea
+= kvmppc_get_gpr(vcpu
, ra
);
241 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
242 kvmppc_set_gpr(vcpu
, ra
, ea
);
245 case OP_31_XOP_MFSPR
:
246 sprn
= get_sprn(inst
);
251 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.shared
->srr0
);
254 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.shared
->srr1
);
257 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.pvr
); break;
259 kvmppc_set_gpr(vcpu
, rt
, vcpu
->vcpu_id
); break;
261 kvmppc_set_gpr(vcpu
, rt
, 0); break;
263 /* Note: mftb and TBRL/TBWL are user-accessible, so
264 * the guest can always access the real TB anyways.
265 * In fact, we probably will never see these traps. */
267 kvmppc_set_gpr(vcpu
, rt
, get_tb() >> 32); break;
269 kvmppc_set_gpr(vcpu
, rt
, get_tb()); break;
272 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.shared
->sprg0
);
275 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.shared
->sprg1
);
278 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.shared
->sprg2
);
281 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.shared
->sprg3
);
283 /* Note: SPRG4-7 are user-readable, so we don't get
288 kvmppc_set_gpr(vcpu
, rt
,
289 kvmppc_get_dec(vcpu
, get_tb()));
293 emulated
= kvmppc_core_emulate_mfspr(vcpu
, sprn
, rt
);
294 if (emulated
== EMULATE_FAIL
) {
295 printk("mfspr: unknown spr %x\n", sprn
);
296 kvmppc_set_gpr(vcpu
, rt
, 0);
300 kvmppc_set_exit_type(vcpu
, EMULATED_MFSPR_EXITS
);
308 emulated
= kvmppc_handle_store(run
, vcpu
,
309 kvmppc_get_gpr(vcpu
, rs
),
313 case OP_31_XOP_STHUX
:
318 ea
= kvmppc_get_gpr(vcpu
, rb
);
320 ea
+= kvmppc_get_gpr(vcpu
, ra
);
322 emulated
= kvmppc_handle_store(run
, vcpu
,
323 kvmppc_get_gpr(vcpu
, rs
),
325 kvmppc_set_gpr(vcpu
, ra
, ea
);
328 case OP_31_XOP_MTSPR
:
329 sprn
= get_sprn(inst
);
333 vcpu
->arch
.shared
->srr0
= kvmppc_get_gpr(vcpu
, rs
);
336 vcpu
->arch
.shared
->srr1
= kvmppc_get_gpr(vcpu
, rs
);
339 /* XXX We need to context-switch the timebase for
340 * watchdog and FIT. */
341 case SPRN_TBWL
: break;
342 case SPRN_TBWU
: break;
344 case SPRN_MSSSR0
: break;
347 vcpu
->arch
.dec
= kvmppc_get_gpr(vcpu
, rs
);
348 kvmppc_emulate_dec(vcpu
);
352 vcpu
->arch
.shared
->sprg0
= kvmppc_get_gpr(vcpu
, rs
);
355 vcpu
->arch
.shared
->sprg1
= kvmppc_get_gpr(vcpu
, rs
);
358 vcpu
->arch
.shared
->sprg2
= kvmppc_get_gpr(vcpu
, rs
);
361 vcpu
->arch
.shared
->sprg3
= kvmppc_get_gpr(vcpu
, rs
);
365 emulated
= kvmppc_core_emulate_mtspr(vcpu
, sprn
, rs
);
366 if (emulated
== EMULATE_FAIL
)
367 printk("mtspr: unknown spr %x\n", sprn
);
370 kvmppc_set_exit_type(vcpu
, EMULATED_MTSPR_EXITS
);
374 /* Do nothing. The guest is performing dcbi because
375 * hardware DMA is not snooped by the dcache, but
376 * emulated DMA either goes through the dcache as
377 * normal writes, or the host kernel has handled dcache
381 case OP_31_XOP_LWBRX
:
383 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 0);
386 case OP_31_XOP_TLBSYNC
:
389 case OP_31_XOP_STWBRX
:
394 emulated
= kvmppc_handle_store(run
, vcpu
,
395 kvmppc_get_gpr(vcpu
, rs
),
399 case OP_31_XOP_LHBRX
:
401 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 0);
404 case OP_31_XOP_STHBRX
:
409 emulated
= kvmppc_handle_store(run
, vcpu
,
410 kvmppc_get_gpr(vcpu
, rs
),
415 /* Attempt core-specific emulation below. */
416 emulated
= EMULATE_FAIL
;
422 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
428 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
429 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
434 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
440 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
441 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
446 emulated
= kvmppc_handle_store(run
, vcpu
,
447 kvmppc_get_gpr(vcpu
, rs
),
454 emulated
= kvmppc_handle_store(run
, vcpu
,
455 kvmppc_get_gpr(vcpu
, rs
),
457 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
462 emulated
= kvmppc_handle_store(run
, vcpu
,
463 kvmppc_get_gpr(vcpu
, rs
),
470 emulated
= kvmppc_handle_store(run
, vcpu
,
471 kvmppc_get_gpr(vcpu
, rs
),
473 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
478 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
484 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
485 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
490 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
496 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
497 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
502 emulated
= kvmppc_handle_store(run
, vcpu
,
503 kvmppc_get_gpr(vcpu
, rs
),
510 emulated
= kvmppc_handle_store(run
, vcpu
,
511 kvmppc_get_gpr(vcpu
, rs
),
513 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
517 emulated
= EMULATE_FAIL
;
520 if (emulated
== EMULATE_FAIL
) {
521 emulated
= kvmppc_core_emulate_op(run
, vcpu
, inst
, &advance
);
522 if (emulated
== EMULATE_AGAIN
) {
524 } else if (emulated
== EMULATE_FAIL
) {
526 printk(KERN_ERR
"Couldn't emulate instruction 0x%08x "
527 "(op %d xop %d)\n", inst
, get_op(inst
), get_xop(inst
));
528 kvmppc_core_queue_program(vcpu
, 0);
532 trace_kvm_ppc_instr(inst
, kvmppc_get_pc(vcpu
), emulated
);
534 /* Advance past emulated instruction. */
536 kvmppc_set_pc(vcpu
, kvmppc_get_pc(vcpu
) + 4);