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>
35 #define OP_31_XOP_LWZX 23
36 #define OP_31_XOP_LBZX 87
37 #define OP_31_XOP_STWX 151
38 #define OP_31_XOP_STBX 215
39 #define OP_31_XOP_STBUX 247
40 #define OP_31_XOP_LHZX 279
41 #define OP_31_XOP_LHZUX 311
42 #define OP_31_XOP_MFSPR 339
43 #define OP_31_XOP_STHX 407
44 #define OP_31_XOP_STHUX 439
45 #define OP_31_XOP_MTSPR 467
46 #define OP_31_XOP_DCBI 470
47 #define OP_31_XOP_LWBRX 534
48 #define OP_31_XOP_TLBSYNC 566
49 #define OP_31_XOP_STWBRX 662
50 #define OP_31_XOP_LHBRX 790
51 #define OP_31_XOP_STHBRX 918
66 void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
68 if (vcpu->arch.tcr & TCR_DIE) {
69 /* The decrementer ticks at the same rate as the timebase, so
70 * that's how we convert the guest DEC value to the number of
72 unsigned long nr_jiffies;
74 nr_jiffies = vcpu->arch.dec / tb_ticks_per_jiffy;
75 mod_timer(&vcpu->arch.dec_timer,
76 get_jiffies_64() + nr_jiffies);
78 del_timer(&vcpu->arch.dec_timer);
94 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
96 /* XXX Should probably auto-generate instruction decoding for a particular core
97 * from opcode tables in the future. */
98 int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
100 u32 inst = vcpu->arch.last_inst;
107 enum emulation_result emulated = EMULATE_DONE;
110 /* this default type might be overwritten by subcategories */
111 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
113 switch (get_op(inst)) {
115 vcpu->arch.esr |= ESR_PTR;
116 kvmppc_core_queue_program(vcpu);
121 switch (get_xop(inst)) {
125 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
130 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
135 emulated = kvmppc_handle_store(run, vcpu,
142 emulated = kvmppc_handle_store(run, vcpu,
147 case OP_31_XOP_STBUX:
152 ea = vcpu->arch.gpr[rb];
154 ea += vcpu->arch.gpr[ra];
156 emulated = kvmppc_handle_store(run, vcpu,
159 vcpu->arch.gpr[rs] = ea;
164 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
167 case OP_31_XOP_LHZUX:
172 ea = vcpu->arch.gpr[rb];
174 ea += vcpu->arch.gpr[ra];
176 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
177 vcpu->arch.gpr[ra] = ea;
180 case OP_31_XOP_MFSPR:
181 sprn = get_sprn(inst);
186 vcpu->arch.gpr[rt] = vcpu->arch.srr0; break;
188 vcpu->arch.gpr[rt] = vcpu->arch.srr1; break;
190 vcpu->arch.gpr[rt] = vcpu->arch.pvr; break;
192 /* Note: mftb and TBRL/TBWL are user-accessible, so
193 * the guest can always access the real TB anyways.
194 * In fact, we probably will never see these traps. */
196 vcpu->arch.gpr[rt] = mftbl(); break;
198 vcpu->arch.gpr[rt] = mftbu(); break;
201 vcpu->arch.gpr[rt] = vcpu->arch.sprg0; break;
203 vcpu->arch.gpr[rt] = vcpu->arch.sprg1; break;
205 vcpu->arch.gpr[rt] = vcpu->arch.sprg2; break;
207 vcpu->arch.gpr[rt] = vcpu->arch.sprg3; break;
208 /* Note: SPRG4-7 are user-readable, so we don't get
212 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
213 if (emulated == EMULATE_FAIL) {
214 printk("mfspr: unknown spr %x\n", sprn);
215 vcpu->arch.gpr[rt] = 0;
226 emulated = kvmppc_handle_store(run, vcpu,
231 case OP_31_XOP_STHUX:
236 ea = vcpu->arch.gpr[rb];
238 ea += vcpu->arch.gpr[ra];
240 emulated = kvmppc_handle_store(run, vcpu,
243 vcpu->arch.gpr[ra] = ea;
246 case OP_31_XOP_MTSPR:
247 sprn = get_sprn(inst);
251 vcpu->arch.srr0 = vcpu->arch.gpr[rs]; break;
253 vcpu->arch.srr1 = vcpu->arch.gpr[rs]; break;
255 /* XXX We need to context-switch the timebase for
256 * watchdog and FIT. */
257 case SPRN_TBWL: break;
258 case SPRN_TBWU: break;
261 vcpu->arch.dec = vcpu->arch.gpr[rs];
262 kvmppc_emulate_dec(vcpu);
266 vcpu->arch.sprg0 = vcpu->arch.gpr[rs]; break;
268 vcpu->arch.sprg1 = vcpu->arch.gpr[rs]; break;
270 vcpu->arch.sprg2 = vcpu->arch.gpr[rs]; break;
272 vcpu->arch.sprg3 = vcpu->arch.gpr[rs]; break;
275 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
276 if (emulated == EMULATE_FAIL)
277 printk("mtspr: unknown spr %x\n", sprn);
283 /* Do nothing. The guest is performing dcbi because
284 * hardware DMA is not snooped by the dcache, but
285 * emulated DMA either goes through the dcache as
286 * normal writes, or the host kernel has handled dcache
290 case OP_31_XOP_LWBRX:
292 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
295 case OP_31_XOP_TLBSYNC:
298 case OP_31_XOP_STWBRX:
303 emulated = kvmppc_handle_store(run, vcpu,
308 case OP_31_XOP_LHBRX:
310 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
313 case OP_31_XOP_STHBRX:
318 emulated = kvmppc_handle_store(run, vcpu,
324 /* Attempt core-specific emulation below. */
325 emulated = EMULATE_FAIL;
331 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
337 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
338 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
343 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
349 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
350 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
355 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
362 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
364 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
369 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
376 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
378 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
383 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
389 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
390 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
395 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
402 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
404 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
408 emulated = EMULATE_FAIL;
411 if (emulated == EMULATE_FAIL) {
412 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
413 if (emulated == EMULATE_FAIL) {
415 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
416 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
420 KVMTRACE_3D(PPC_INSTR, vcpu, inst, (int)vcpu->arch.pc, emulated, entryexit);
423 vcpu->arch.pc += 4; /* Advance past emulated instruction. */