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_STBUX 247
42 #define OP_31_XOP_LHZX 279
43 #define OP_31_XOP_LHZUX 311
44 #define OP_31_XOP_MFSPR 339
45 #define OP_31_XOP_STHX 407
46 #define OP_31_XOP_STHUX 439
47 #define OP_31_XOP_MTSPR 467
48 #define OP_31_XOP_DCBI 470
49 #define OP_31_XOP_LWBRX 534
50 #define OP_31_XOP_TLBSYNC 566
51 #define OP_31_XOP_STWBRX 662
52 #define OP_31_XOP_LHBRX 790
53 #define OP_31_XOP_STHBRX 918
69 static int kvmppc_dec_enabled(struct kvm_vcpu
*vcpu
)
74 static int kvmppc_dec_enabled(struct kvm_vcpu
*vcpu
)
76 return vcpu
->arch
.tcr
& TCR_DIE
;
80 void kvmppc_emulate_dec(struct kvm_vcpu
*vcpu
)
82 unsigned long dec_nsec
;
84 pr_debug("mtDEC: %x\n", vcpu
->arch
.dec
);
86 /* mtdec lowers the interrupt line when positive. */
87 kvmppc_core_dequeue_dec(vcpu
);
89 /* POWER4+ triggers a dec interrupt if the value is < 0 */
90 if (vcpu
->arch
.dec
& 0x80000000) {
91 hrtimer_try_to_cancel(&vcpu
->arch
.dec_timer
);
92 kvmppc_core_queue_dec(vcpu
);
96 if (kvmppc_dec_enabled(vcpu
)) {
97 /* The decrementer ticks at the same rate as the timebase, so
98 * that's how we convert the guest DEC value to the number of
101 hrtimer_try_to_cancel(&vcpu
->arch
.dec_timer
);
102 dec_nsec
= vcpu
->arch
.dec
;
104 dec_nsec
/= tb_ticks_per_usec
;
105 hrtimer_start(&vcpu
->arch
.dec_timer
, ktime_set(0, dec_nsec
),
107 vcpu
->arch
.dec_jiffies
= get_tb();
109 hrtimer_try_to_cancel(&vcpu
->arch
.dec_timer
);
125 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
127 /* XXX Should probably auto-generate instruction decoding for a particular core
128 * from opcode tables in the future. */
129 int kvmppc_emulate_instruction(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
131 u32 inst
= vcpu
->arch
.last_inst
;
138 enum emulation_result emulated
= EMULATE_DONE
;
141 /* this default type might be overwritten by subcategories */
142 kvmppc_set_exit_type(vcpu
, EMULATED_INST_EXITS
);
144 pr_debug(KERN_INFO
"Emulating opcode %d / %d\n", get_op(inst
), get_xop(inst
));
146 /* Try again next time */
147 if (inst
== KVM_INST_FETCH_FAILED
)
150 switch (get_op(inst
)) {
154 kvmppc_core_queue_program(vcpu
, SRR1_PROGTRAP
);
156 kvmppc_core_queue_program(vcpu
, vcpu
->arch
.esr
| ESR_PTR
);
162 switch (get_xop(inst
)) {
166 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
171 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
176 emulated
= kvmppc_handle_store(run
, vcpu
,
177 kvmppc_get_gpr(vcpu
, rs
),
183 emulated
= kvmppc_handle_store(run
, vcpu
,
184 kvmppc_get_gpr(vcpu
, rs
),
188 case OP_31_XOP_STBUX
:
193 ea
= kvmppc_get_gpr(vcpu
, rb
);
195 ea
+= kvmppc_get_gpr(vcpu
, ra
);
197 emulated
= kvmppc_handle_store(run
, vcpu
,
198 kvmppc_get_gpr(vcpu
, rs
),
200 kvmppc_set_gpr(vcpu
, rs
, ea
);
205 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
208 case OP_31_XOP_LHZUX
:
213 ea
= kvmppc_get_gpr(vcpu
, rb
);
215 ea
+= kvmppc_get_gpr(vcpu
, ra
);
217 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
218 kvmppc_set_gpr(vcpu
, ra
, ea
);
221 case OP_31_XOP_MFSPR
:
222 sprn
= get_sprn(inst
);
227 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.srr0
); break;
229 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.srr1
); break;
231 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.pvr
); break;
233 kvmppc_set_gpr(vcpu
, rt
, vcpu
->vcpu_id
); break;
235 kvmppc_set_gpr(vcpu
, rt
, 0); break;
237 /* Note: mftb and TBRL/TBWL are user-accessible, so
238 * the guest can always access the real TB anyways.
239 * In fact, we probably will never see these traps. */
241 kvmppc_set_gpr(vcpu
, rt
, get_tb() >> 32); break;
243 kvmppc_set_gpr(vcpu
, rt
, get_tb()); break;
246 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.sprg0
); break;
248 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.sprg1
); break;
250 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.sprg2
); break;
252 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.sprg3
); break;
253 /* Note: SPRG4-7 are user-readable, so we don't get
258 u64 jd
= get_tb() - vcpu
->arch
.dec_jiffies
;
259 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.dec
- jd
);
260 pr_debug(KERN_INFO
"mfDEC: %x - %llx = %lx\n",
262 kvmppc_get_gpr(vcpu
, rt
));
266 emulated
= kvmppc_core_emulate_mfspr(vcpu
, sprn
, rt
);
267 if (emulated
== EMULATE_FAIL
) {
268 printk("mfspr: unknown spr %x\n", sprn
);
269 kvmppc_set_gpr(vcpu
, rt
, 0);
280 emulated
= kvmppc_handle_store(run
, vcpu
,
281 kvmppc_get_gpr(vcpu
, rs
),
285 case OP_31_XOP_STHUX
:
290 ea
= kvmppc_get_gpr(vcpu
, rb
);
292 ea
+= kvmppc_get_gpr(vcpu
, ra
);
294 emulated
= kvmppc_handle_store(run
, vcpu
,
295 kvmppc_get_gpr(vcpu
, rs
),
297 kvmppc_set_gpr(vcpu
, ra
, ea
);
300 case OP_31_XOP_MTSPR
:
301 sprn
= get_sprn(inst
);
305 vcpu
->arch
.srr0
= kvmppc_get_gpr(vcpu
, rs
); break;
307 vcpu
->arch
.srr1
= kvmppc_get_gpr(vcpu
, rs
); break;
309 /* XXX We need to context-switch the timebase for
310 * watchdog and FIT. */
311 case SPRN_TBWL
: break;
312 case SPRN_TBWU
: break;
314 case SPRN_MSSSR0
: break;
317 vcpu
->arch
.dec
= kvmppc_get_gpr(vcpu
, rs
);
318 kvmppc_emulate_dec(vcpu
);
322 vcpu
->arch
.sprg0
= kvmppc_get_gpr(vcpu
, rs
); break;
324 vcpu
->arch
.sprg1
= kvmppc_get_gpr(vcpu
, rs
); break;
326 vcpu
->arch
.sprg2
= kvmppc_get_gpr(vcpu
, rs
); break;
328 vcpu
->arch
.sprg3
= kvmppc_get_gpr(vcpu
, rs
); break;
331 emulated
= kvmppc_core_emulate_mtspr(vcpu
, sprn
, rs
);
332 if (emulated
== EMULATE_FAIL
)
333 printk("mtspr: unknown spr %x\n", sprn
);
339 /* Do nothing. The guest is performing dcbi because
340 * hardware DMA is not snooped by the dcache, but
341 * emulated DMA either goes through the dcache as
342 * normal writes, or the host kernel has handled dcache
346 case OP_31_XOP_LWBRX
:
348 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 0);
351 case OP_31_XOP_TLBSYNC
:
354 case OP_31_XOP_STWBRX
:
359 emulated
= kvmppc_handle_store(run
, vcpu
,
360 kvmppc_get_gpr(vcpu
, rs
),
364 case OP_31_XOP_LHBRX
:
366 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 0);
369 case OP_31_XOP_STHBRX
:
374 emulated
= kvmppc_handle_store(run
, vcpu
,
375 kvmppc_get_gpr(vcpu
, rs
),
380 /* Attempt core-specific emulation below. */
381 emulated
= EMULATE_FAIL
;
387 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
393 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
394 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
399 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
405 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
406 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
411 emulated
= kvmppc_handle_store(run
, vcpu
,
412 kvmppc_get_gpr(vcpu
, rs
),
419 emulated
= kvmppc_handle_store(run
, vcpu
,
420 kvmppc_get_gpr(vcpu
, rs
),
422 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
427 emulated
= kvmppc_handle_store(run
, vcpu
,
428 kvmppc_get_gpr(vcpu
, rs
),
435 emulated
= kvmppc_handle_store(run
, vcpu
,
436 kvmppc_get_gpr(vcpu
, rs
),
438 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
443 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
449 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
450 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
455 emulated
= kvmppc_handle_store(run
, vcpu
,
456 kvmppc_get_gpr(vcpu
, rs
),
463 emulated
= kvmppc_handle_store(run
, vcpu
,
464 kvmppc_get_gpr(vcpu
, rs
),
466 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.paddr_accessed
);
470 emulated
= EMULATE_FAIL
;
473 if (emulated
== EMULATE_FAIL
) {
474 emulated
= kvmppc_core_emulate_op(run
, vcpu
, inst
, &advance
);
475 if (emulated
== EMULATE_FAIL
) {
477 printk(KERN_ERR
"Couldn't emulate instruction 0x%08x "
478 "(op %d xop %d)\n", inst
, get_op(inst
), get_xop(inst
));
479 kvmppc_core_queue_program(vcpu
, 0);
483 trace_kvm_ppc_instr(inst
, vcpu
->arch
.pc
, emulated
);
486 vcpu
->arch
.pc
+= 4; /* Advance past emulated instruction. */