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>
32 void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
34 if (vcpu->arch.tcr & TCR_DIE) {
35 /* The decrementer ticks at the same rate as the timebase, so
36 * that's how we convert the guest DEC value to the number of
38 unsigned long nr_jiffies;
40 nr_jiffies = vcpu->arch.dec / tb_ticks_per_jiffy;
41 mod_timer(&vcpu->arch.dec_timer,
42 get_jiffies_64() + nr_jiffies);
44 del_timer(&vcpu->arch.dec_timer);
60 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
62 /* XXX Should probably auto-generate instruction decoding for a particular core
63 * from opcode tables in the future. */
64 int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
66 u32 inst = vcpu->arch.last_inst;
73 enum emulation_result emulated = EMULATE_DONE;
76 switch (get_op(inst)) {
78 vcpu->arch.esr |= ESR_PTR;
79 kvmppc_core_queue_program(vcpu);
84 switch (get_xop(inst)) {
88 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
93 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
98 emulated = kvmppc_handle_store(run, vcpu,
105 emulated = kvmppc_handle_store(run, vcpu,
110 case 247: /* stbux */
115 ea = vcpu->arch.gpr[rb];
117 ea += vcpu->arch.gpr[ra];
119 emulated = kvmppc_handle_store(run, vcpu,
122 vcpu->arch.gpr[rs] = ea;
127 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
130 case 311: /* lhzux */
135 ea = vcpu->arch.gpr[rb];
137 ea += vcpu->arch.gpr[ra];
139 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
140 vcpu->arch.gpr[ra] = ea;
143 case 339: /* mfspr */
144 sprn = get_sprn(inst);
149 vcpu->arch.gpr[rt] = vcpu->arch.srr0; break;
151 vcpu->arch.gpr[rt] = vcpu->arch.srr1; break;
153 vcpu->arch.gpr[rt] = vcpu->arch.pvr; break;
155 /* Note: mftb and TBRL/TBWL are user-accessible, so
156 * the guest can always access the real TB anyways.
157 * In fact, we probably will never see these traps. */
159 vcpu->arch.gpr[rt] = mftbl(); break;
161 vcpu->arch.gpr[rt] = mftbu(); break;
164 vcpu->arch.gpr[rt] = vcpu->arch.sprg0; break;
166 vcpu->arch.gpr[rt] = vcpu->arch.sprg1; break;
168 vcpu->arch.gpr[rt] = vcpu->arch.sprg2; break;
170 vcpu->arch.gpr[rt] = vcpu->arch.sprg3; break;
171 /* Note: SPRG4-7 are user-readable, so we don't get
175 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
176 if (emulated == EMULATE_FAIL) {
177 printk("mfspr: unknown spr %x\n", sprn);
178 vcpu->arch.gpr[rt] = 0;
189 emulated = kvmppc_handle_store(run, vcpu,
194 case 439: /* sthux */
199 ea = vcpu->arch.gpr[rb];
201 ea += vcpu->arch.gpr[ra];
203 emulated = kvmppc_handle_store(run, vcpu,
206 vcpu->arch.gpr[ra] = ea;
209 case 467: /* mtspr */
210 sprn = get_sprn(inst);
214 vcpu->arch.srr0 = vcpu->arch.gpr[rs]; break;
216 vcpu->arch.srr1 = vcpu->arch.gpr[rs]; break;
218 /* XXX We need to context-switch the timebase for
219 * watchdog and FIT. */
220 case SPRN_TBWL: break;
221 case SPRN_TBWU: break;
224 vcpu->arch.dec = vcpu->arch.gpr[rs];
225 kvmppc_emulate_dec(vcpu);
229 vcpu->arch.sprg0 = vcpu->arch.gpr[rs]; break;
231 vcpu->arch.sprg1 = vcpu->arch.gpr[rs]; break;
233 vcpu->arch.sprg2 = vcpu->arch.gpr[rs]; break;
235 vcpu->arch.sprg3 = vcpu->arch.gpr[rs]; break;
238 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
239 if (emulated == EMULATE_FAIL)
240 printk("mtspr: unknown spr %x\n", sprn);
246 /* Do nothing. The guest is performing dcbi because
247 * hardware DMA is not snooped by the dcache, but
248 * emulated DMA either goes through the dcache as
249 * normal writes, or the host kernel has handled dcache
253 case 534: /* lwbrx */
255 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
258 case 566: /* tlbsync */
261 case 662: /* stwbrx */
266 emulated = kvmppc_handle_store(run, vcpu,
271 case 790: /* lhbrx */
273 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
276 case 918: /* sthbrx */
281 emulated = kvmppc_handle_store(run, vcpu,
287 /* Attempt core-specific emulation below. */
288 emulated = EMULATE_FAIL;
294 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
300 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
301 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
306 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
312 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
313 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
318 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
325 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
327 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
332 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
339 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
341 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
346 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
352 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
353 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
358 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
365 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
367 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
371 emulated = EMULATE_FAIL;
374 if (emulated == EMULATE_FAIL) {
375 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
376 if (emulated == EMULATE_FAIL) {
378 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
379 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
383 KVMTRACE_3D(PPC_INSTR, vcpu, inst, (int)vcpu->arch.pc, emulated, entryexit);
386 vcpu->arch.pc += 4; /* Advance past emulated instruction. */