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/timer.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>
33 void kvmppc_emulate_dec(struct kvm_vcpu
*vcpu
)
35 if (vcpu
->arch
.tcr
& TCR_DIE
) {
36 /* The decrementer ticks at the same rate as the timebase, so
37 * that's how we convert the guest DEC value to the number of
39 unsigned long nr_jiffies
;
41 nr_jiffies
= vcpu
->arch
.dec
/ tb_ticks_per_jiffy
;
42 mod_timer(&vcpu
->arch
.dec_timer
,
43 get_jiffies_64() + nr_jiffies
);
45 del_timer(&vcpu
->arch
.dec_timer
);
61 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
63 /* XXX Should probably auto-generate instruction decoding for a particular core
64 * from opcode tables in the future. */
65 int kvmppc_emulate_instruction(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
67 u32 inst
= vcpu
->arch
.last_inst
;
74 enum emulation_result emulated
= EMULATE_DONE
;
77 /* this default type might be overwritten by subcategories */
78 kvmppc_set_exit_type(vcpu
, EMULATED_INST_EXITS
);
80 switch (get_op(inst
)) {
82 vcpu
->arch
.esr
|= ESR_PTR
;
83 kvmppc_core_queue_program(vcpu
);
88 switch (get_xop(inst
)) {
92 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
97 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
102 emulated
= kvmppc_handle_store(run
, vcpu
,
109 emulated
= kvmppc_handle_store(run
, vcpu
,
114 case 247: /* stbux */
119 ea
= vcpu
->arch
.gpr
[rb
];
121 ea
+= vcpu
->arch
.gpr
[ra
];
123 emulated
= kvmppc_handle_store(run
, vcpu
,
126 vcpu
->arch
.gpr
[rs
] = ea
;
131 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
134 case 311: /* lhzux */
139 ea
= vcpu
->arch
.gpr
[rb
];
141 ea
+= vcpu
->arch
.gpr
[ra
];
143 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
144 vcpu
->arch
.gpr
[ra
] = ea
;
147 case 339: /* mfspr */
148 sprn
= get_sprn(inst
);
153 vcpu
->arch
.gpr
[rt
] = vcpu
->arch
.srr0
; break;
155 vcpu
->arch
.gpr
[rt
] = vcpu
->arch
.srr1
; break;
157 vcpu
->arch
.gpr
[rt
] = vcpu
->arch
.pvr
; break;
159 /* Note: mftb and TBRL/TBWL are user-accessible, so
160 * the guest can always access the real TB anyways.
161 * In fact, we probably will never see these traps. */
163 vcpu
->arch
.gpr
[rt
] = mftbl(); break;
165 vcpu
->arch
.gpr
[rt
] = mftbu(); break;
168 vcpu
->arch
.gpr
[rt
] = vcpu
->arch
.sprg0
; break;
170 vcpu
->arch
.gpr
[rt
] = vcpu
->arch
.sprg1
; break;
172 vcpu
->arch
.gpr
[rt
] = vcpu
->arch
.sprg2
; break;
174 vcpu
->arch
.gpr
[rt
] = vcpu
->arch
.sprg3
; break;
175 /* Note: SPRG4-7 are user-readable, so we don't get
179 emulated
= kvmppc_core_emulate_mfspr(vcpu
, sprn
, rt
);
180 if (emulated
== EMULATE_FAIL
) {
181 printk("mfspr: unknown spr %x\n", sprn
);
182 vcpu
->arch
.gpr
[rt
] = 0;
193 emulated
= kvmppc_handle_store(run
, vcpu
,
198 case 439: /* sthux */
203 ea
= vcpu
->arch
.gpr
[rb
];
205 ea
+= vcpu
->arch
.gpr
[ra
];
207 emulated
= kvmppc_handle_store(run
, vcpu
,
210 vcpu
->arch
.gpr
[ra
] = ea
;
213 case 467: /* mtspr */
214 sprn
= get_sprn(inst
);
218 vcpu
->arch
.srr0
= vcpu
->arch
.gpr
[rs
]; break;
220 vcpu
->arch
.srr1
= vcpu
->arch
.gpr
[rs
]; break;
222 /* XXX We need to context-switch the timebase for
223 * watchdog and FIT. */
224 case SPRN_TBWL
: break;
225 case SPRN_TBWU
: break;
228 vcpu
->arch
.dec
= vcpu
->arch
.gpr
[rs
];
229 kvmppc_emulate_dec(vcpu
);
233 vcpu
->arch
.sprg0
= vcpu
->arch
.gpr
[rs
]; break;
235 vcpu
->arch
.sprg1
= vcpu
->arch
.gpr
[rs
]; break;
237 vcpu
->arch
.sprg2
= vcpu
->arch
.gpr
[rs
]; break;
239 vcpu
->arch
.sprg3
= vcpu
->arch
.gpr
[rs
]; break;
242 emulated
= kvmppc_core_emulate_mtspr(vcpu
, sprn
, rs
);
243 if (emulated
== EMULATE_FAIL
)
244 printk("mtspr: unknown spr %x\n", sprn
);
250 /* Do nothing. The guest is performing dcbi because
251 * hardware DMA is not snooped by the dcache, but
252 * emulated DMA either goes through the dcache as
253 * normal writes, or the host kernel has handled dcache
257 case 534: /* lwbrx */
259 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 0);
262 case 566: /* tlbsync */
265 case 662: /* stwbrx */
270 emulated
= kvmppc_handle_store(run
, vcpu
,
275 case 790: /* lhbrx */
277 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 0);
280 case 918: /* sthbrx */
285 emulated
= kvmppc_handle_store(run
, vcpu
,
291 /* Attempt core-specific emulation below. */
292 emulated
= EMULATE_FAIL
;
298 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
304 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
305 vcpu
->arch
.gpr
[ra
] = vcpu
->arch
.paddr_accessed
;
310 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
316 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
317 vcpu
->arch
.gpr
[ra
] = vcpu
->arch
.paddr_accessed
;
322 emulated
= kvmppc_handle_store(run
, vcpu
, vcpu
->arch
.gpr
[rs
],
329 emulated
= kvmppc_handle_store(run
, vcpu
, vcpu
->arch
.gpr
[rs
],
331 vcpu
->arch
.gpr
[ra
] = vcpu
->arch
.paddr_accessed
;
336 emulated
= kvmppc_handle_store(run
, vcpu
, vcpu
->arch
.gpr
[rs
],
343 emulated
= kvmppc_handle_store(run
, vcpu
, vcpu
->arch
.gpr
[rs
],
345 vcpu
->arch
.gpr
[ra
] = vcpu
->arch
.paddr_accessed
;
350 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
356 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
357 vcpu
->arch
.gpr
[ra
] = vcpu
->arch
.paddr_accessed
;
362 emulated
= kvmppc_handle_store(run
, vcpu
, vcpu
->arch
.gpr
[rs
],
369 emulated
= kvmppc_handle_store(run
, vcpu
, vcpu
->arch
.gpr
[rs
],
371 vcpu
->arch
.gpr
[ra
] = vcpu
->arch
.paddr_accessed
;
375 emulated
= EMULATE_FAIL
;
378 if (emulated
== EMULATE_FAIL
) {
379 emulated
= kvmppc_core_emulate_op(run
, vcpu
, inst
, &advance
);
380 if (emulated
== EMULATE_FAIL
) {
382 printk(KERN_ERR
"Couldn't emulate instruction 0x%08x "
383 "(op %d xop %d)\n", inst
, get_op(inst
), get_xop(inst
));
387 KVMTRACE_3D(PPC_INSTR
, vcpu
, inst
, (int)vcpu
->arch
.pc
, emulated
, entryexit
);
390 vcpu
->arch
.pc
+= 4; /* Advance past emulated instruction. */