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
16 * Copyright 2011 Freescale Semiconductor, Inc.
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
21 #include <linux/jiffies.h>
22 #include <linux/hrtimer.h>
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/kvm_host.h>
26 #include <linux/clockchips.h>
30 #include <asm/byteorder.h>
31 #include <asm/kvm_ppc.h>
32 #include <asm/disassemble.h>
33 #include <asm/ppc-opcode.h>
50 int kvmppc_emulate_loadstore(struct kvm_vcpu
*vcpu
)
52 struct kvm_run
*run
= vcpu
->run
;
55 enum emulation_result emulated
;
58 /* this default type might be overwritten by subcategories */
59 kvmppc_set_exit_type(vcpu
, EMULATED_INST_EXITS
);
61 emulated
= kvmppc_get_last_inst(vcpu
, INST_GENERIC
, &inst
);
62 if (emulated
!= EMULATE_DONE
)
69 switch (get_op(inst
)) {
71 switch (get_xop(inst
)) {
73 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
77 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
81 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
82 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
86 emulated
= kvmppc_handle_store(run
, vcpu
,
87 kvmppc_get_gpr(vcpu
, rs
),
92 emulated
= kvmppc_handle_store(run
, vcpu
,
93 kvmppc_get_gpr(vcpu
, rs
),
98 emulated
= kvmppc_handle_store(run
, vcpu
,
99 kvmppc_get_gpr(vcpu
, rs
),
101 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
105 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
109 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
112 case OP_31_XOP_LHZUX
:
113 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
114 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
118 emulated
= kvmppc_handle_store(run
, vcpu
,
119 kvmppc_get_gpr(vcpu
, rs
),
123 case OP_31_XOP_STHUX
:
124 emulated
= kvmppc_handle_store(run
, vcpu
,
125 kvmppc_get_gpr(vcpu
, rs
),
127 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
130 case OP_31_XOP_DCBST
:
133 /* Do nothing. The guest is performing dcbi because
134 * hardware DMA is not snooped by the dcache, but
135 * emulated DMA either goes through the dcache as
136 * normal writes, or the host kernel has handled dcache
140 case OP_31_XOP_LWBRX
:
141 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 0);
144 case OP_31_XOP_STWBRX
:
145 emulated
= kvmppc_handle_store(run
, vcpu
,
146 kvmppc_get_gpr(vcpu
, rs
),
150 case OP_31_XOP_LHBRX
:
151 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 0);
154 case OP_31_XOP_STHBRX
:
155 emulated
= kvmppc_handle_store(run
, vcpu
,
156 kvmppc_get_gpr(vcpu
, rs
),
161 emulated
= EMULATE_FAIL
;
167 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
170 /* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */
173 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 8, 1);
177 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 4, 1);
178 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
182 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
186 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 1, 1);
187 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
191 emulated
= kvmppc_handle_store(run
, vcpu
,
192 kvmppc_get_gpr(vcpu
, rs
),
196 /* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */
199 emulated
= kvmppc_handle_store(run
, vcpu
,
200 kvmppc_get_gpr(vcpu
, rs
),
205 emulated
= kvmppc_handle_store(run
, vcpu
,
206 kvmppc_get_gpr(vcpu
, rs
),
208 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
212 emulated
= kvmppc_handle_store(run
, vcpu
,
213 kvmppc_get_gpr(vcpu
, rs
),
218 emulated
= kvmppc_handle_store(run
, vcpu
,
219 kvmppc_get_gpr(vcpu
, rs
),
221 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
225 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
229 emulated
= kvmppc_handle_load(run
, vcpu
, rt
, 2, 1);
230 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
234 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
238 emulated
= kvmppc_handle_loads(run
, vcpu
, rt
, 2, 1);
239 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
243 emulated
= kvmppc_handle_store(run
, vcpu
,
244 kvmppc_get_gpr(vcpu
, rs
),
249 emulated
= kvmppc_handle_store(run
, vcpu
,
250 kvmppc_get_gpr(vcpu
, rs
),
252 kvmppc_set_gpr(vcpu
, ra
, vcpu
->arch
.vaddr_accessed
);
256 emulated
= EMULATE_FAIL
;
260 if (emulated
== EMULATE_FAIL
) {
262 kvmppc_core_queue_program(vcpu
, 0);
265 trace_kvm_ppc_instr(inst
, kvmppc_get_pc(vcpu
), emulated
);
267 /* Advance past emulated instruction. */
269 kvmppc_set_pc(vcpu
, kvmppc_get_pc(vcpu
) + 4);