blob: f63b5cbd82219de06d42782a0c56de9987a8ec72 [file] [log] [blame]
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001/*
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.
5 *
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.
10 *
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.
14 *
15 * Copyright IBM Corp. 2007
Scott Wooddfd4d472011-11-17 12:39:59 +000016 * Copyright 2011 Freescale Semiconductor, Inc.
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050017 *
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19 */
20
21#include <linux/jiffies.h>
Alexander Graf544c6762009-11-02 12:02:31 +000022#include <linux/hrtimer.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/kvm_host.h>
Bharat Bhushan6e359942012-04-18 06:01:19 +000026#include <linux/clockchips.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050027
Hollis Blanchard75f74f02008-11-05 09:36:16 -060028#include <asm/reg.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050029#include <asm/time.h>
30#include <asm/byteorder.h>
31#include <asm/kvm_ppc.h>
Hollis Blanchardc381a042008-11-05 09:36:15 -060032#include <asm/disassemble.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060033#include "timing.h"
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030034#include "trace.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050035
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060036#define OP_TRAP 3
Alexander Graf513579e2009-10-30 05:47:16 +000037#define OP_TRAP_64 2
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060038
Alexander Graf6df79df2012-03-13 22:15:45 +010039#define OP_31_XOP_TRAP 4
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060040#define OP_31_XOP_LWZX 23
Alexander Graf6df79df2012-03-13 22:15:45 +010041#define OP_31_XOP_TRAP_64 68
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060042#define OP_31_XOP_LBZX 87
43#define OP_31_XOP_STWX 151
44#define OP_31_XOP_STBX 215
Alexander Graf1c85e732010-03-24 21:48:27 +010045#define OP_31_XOP_LBZUX 119
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060046#define OP_31_XOP_STBUX 247
47#define OP_31_XOP_LHZX 279
48#define OP_31_XOP_LHZUX 311
49#define OP_31_XOP_MFSPR 339
Alexander Graf1c85e732010-03-24 21:48:27 +010050#define OP_31_XOP_LHAX 343
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060051#define OP_31_XOP_STHX 407
52#define OP_31_XOP_STHUX 439
53#define OP_31_XOP_MTSPR 467
54#define OP_31_XOP_DCBI 470
55#define OP_31_XOP_LWBRX 534
56#define OP_31_XOP_TLBSYNC 566
57#define OP_31_XOP_STWBRX 662
58#define OP_31_XOP_LHBRX 790
59#define OP_31_XOP_STHBRX 918
60
61#define OP_LWZ 32
62#define OP_LWZU 33
63#define OP_LBZ 34
64#define OP_LBZU 35
65#define OP_STW 36
66#define OP_STWU 37
67#define OP_STB 38
68#define OP_STBU 39
69#define OP_LHZ 40
70#define OP_LHZU 41
Alexander Graf3587d532010-02-19 11:00:30 +010071#define OP_LHA 42
72#define OP_LHAU 43
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060073#define OP_STH 44
74#define OP_STHU 45
75
Hollis Blanchard75f74f02008-11-05 09:36:16 -060076void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050077{
Alexander Graf544c6762009-11-02 12:02:31 +000078 unsigned long dec_nsec;
Bharat Bhushandc2babfe2011-10-19 09:46:06 +053079 unsigned long long dec_time;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000080
Alexander Graf544c6762009-11-02 12:02:31 +000081 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Scott Wooddfd4d472011-11-17 12:39:59 +000082 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
83
Alexander Graf00c3a372010-04-16 00:11:42 +020084#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010085 /* mtdec lowers the interrupt line when positive. */
86 kvmppc_core_dequeue_dec(vcpu);
87
Alexander Graf513579e2009-10-30 05:47:16 +000088 /* POWER4+ triggers a dec interrupt if the value is < 0 */
89 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf513579e2009-10-30 05:47:16 +000090 kvmppc_core_queue_dec(vcpu);
91 return;
92 }
93#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050094
Scott Wooddfd4d472011-11-17 12:39:59 +000095#ifdef CONFIG_BOOKE
96 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
97 if (vcpu->arch.dec == 0)
98 return;
99#endif
100
101 /*
102 * The decrementer ticks at the same rate as the timebase, so
103 * that's how we convert the guest DEC value to the number of
104 * host ticks.
105 */
106
107 dec_time = vcpu->arch.dec;
Bharat Bhushan6e359942012-04-18 06:01:19 +0000108 /*
109 * Guest timebase ticks at the same frequency as host decrementer.
110 * So use the host decrementer calculations for decrementer emulation.
111 */
112 dec_time = dec_time << decrementer_clockevent.shift;
113 do_div(dec_time, decrementer_clockevent.mult);
Scott Wooddfd4d472011-11-17 12:39:59 +0000114 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
115 hrtimer_start(&vcpu->arch.dec_timer,
116 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
117 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500118}
119
Scott Wood5ce941e2011-04-27 17:24:21 -0500120u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
121{
122 u64 jd = tb - vcpu->arch.dec_jiffies;
Scott Wooddfd4d472011-11-17 12:39:59 +0000123
124#ifdef CONFIG_BOOKE
125 if (vcpu->arch.dec < jd)
126 return 0;
127#endif
128
Scott Wood5ce941e2011-04-27 17:24:21 -0500129 return vcpu->arch.dec - jd;
130}
131
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500132/* XXX to do:
133 * lhax
134 * lhaux
135 * lswx
136 * lswi
137 * stswx
138 * stswi
139 * lha
140 * lhau
141 * lmw
142 * stmw
143 *
144 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
145 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600146/* XXX Should probably auto-generate instruction decoding for a particular core
147 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500148int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
149{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200150 u32 inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200151 int ra = get_ra(inst);
152 int rs = get_rs(inst);
153 int rt = get_rt(inst);
154 int sprn = get_sprn(inst);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500155 enum emulation_result emulated = EMULATE_DONE;
156 int advance = 1;
157
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600158 /* this default type might be overwritten by subcategories */
159 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
160
Joe Perches689fd142010-09-11 19:10:53 +0000161 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000162
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500163 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600164 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200165#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000166 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100167 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800168#else
Scott Woodb5904972011-11-08 18:23:30 -0600169 kvmppc_core_queue_program(vcpu,
170 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800171#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500172 advance = 0;
173 break;
174
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500175 case 31:
176 switch (get_xop(inst)) {
177
Alexander Graf6df79df2012-03-13 22:15:45 +0100178 case OP_31_XOP_TRAP:
179#ifdef CONFIG_64BIT
180 case OP_31_XOP_TRAP_64:
181#endif
182#ifdef CONFIG_PPC_BOOK3S
183 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
184#else
185 kvmppc_core_queue_program(vcpu,
186 vcpu->arch.shared->esr | ESR_PTR);
187#endif
188 advance = 0;
189 break;
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600190 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500191 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
192 break;
193
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600194 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500195 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
196 break;
197
Alexander Graf1c85e732010-03-24 21:48:27 +0100198 case OP_31_XOP_LBZUX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100199 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100200 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf1c85e732010-03-24 21:48:27 +0100201 break;
202
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600203 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500204 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100205 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500206 4, 1);
207 break;
208
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600209 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500210 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100211 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500212 1, 1);
213 break;
214
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600215 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500216 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100217 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500218 1, 1);
Alexander Graf978b4fa2012-04-27 01:00:17 +0200219 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500220 break;
221
Alexander Graf1c85e732010-03-24 21:48:27 +0100222 case OP_31_XOP_LHAX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100223 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
224 break;
225
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600226 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500227 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
228 break;
229
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600230 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500231 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100232 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500233 break;
234
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600235 case OP_31_XOP_MFSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500236 switch (sprn) {
237 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200238 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr0);
239 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500240 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200241 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr1);
242 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500243 case SPRN_PVR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100244 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
Liu Yu06579dd2009-06-05 14:54:31 +0800245 case SPRN_PIR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100246 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
Alexander Graf513579e2009-10-30 05:47:16 +0000247 case SPRN_MSSSR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100248 kvmppc_set_gpr(vcpu, rt, 0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500249
250 /* Note: mftb and TBRL/TBWL are user-accessible, so
251 * the guest can always access the real TB anyways.
252 * In fact, we probably will never see these traps. */
253 case SPRN_TBWL:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100254 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500255 case SPRN_TBWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100256 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500257
258 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200259 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg0);
260 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500261 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200262 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg1);
263 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500264 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200265 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg2);
266 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500267 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200268 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg3);
269 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500270 /* Note: SPRG4-7 are user-readable, so we don't get
271 * a trap. */
272
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000273 case SPRN_DEC:
274 {
Scott Wood5ce941e2011-04-27 17:24:21 -0500275 kvmppc_set_gpr(vcpu, rt,
276 kvmppc_get_dec(vcpu, get_tb()));
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000277 break;
278 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500279 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600280 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
281 if (emulated == EMULATE_FAIL) {
282 printk("mfspr: unknown spr %x\n", sprn);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100283 kvmppc_set_gpr(vcpu, rt, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600284 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500285 break;
286 }
Scott Wood49ea0692011-03-28 15:01:24 -0500287 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500288 break;
289
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600290 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500291 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100292 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293 2, 1);
294 break;
295
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600296 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500297 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100298 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500299 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100300 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500301 break;
302
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600303 case OP_31_XOP_MTSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500304 switch (sprn) {
305 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200306 vcpu->arch.shared->srr0 = kvmppc_get_gpr(vcpu, rs);
307 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500308 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200309 vcpu->arch.shared->srr1 = kvmppc_get_gpr(vcpu, rs);
310 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500311
312 /* XXX We need to context-switch the timebase for
313 * watchdog and FIT. */
314 case SPRN_TBWL: break;
315 case SPRN_TBWU: break;
316
Alexander Graf513579e2009-10-30 05:47:16 +0000317 case SPRN_MSSSR0: break;
318
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500319 case SPRN_DEC:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100320 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500321 kvmppc_emulate_dec(vcpu);
322 break;
323
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500324 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200325 vcpu->arch.shared->sprg0 = kvmppc_get_gpr(vcpu, rs);
326 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500327 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200328 vcpu->arch.shared->sprg1 = kvmppc_get_gpr(vcpu, rs);
329 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500330 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200331 vcpu->arch.shared->sprg2 = kvmppc_get_gpr(vcpu, rs);
332 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500333 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200334 vcpu->arch.shared->sprg3 = kvmppc_get_gpr(vcpu, rs);
335 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500336
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500337 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600338 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
339 if (emulated == EMULATE_FAIL)
340 printk("mtspr: unknown spr %x\n", sprn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500341 break;
342 }
Scott Wood49ea0692011-03-28 15:01:24 -0500343 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500344 break;
345
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600346 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500347 /* Do nothing. The guest is performing dcbi because
348 * hardware DMA is not snooped by the dcache, but
349 * emulated DMA either goes through the dcache as
350 * normal writes, or the host kernel has handled dcache
351 * coherence. */
352 break;
353
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600354 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500355 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
356 break;
357
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600358 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500359 break;
360
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600361 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500362 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100363 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500364 4, 0);
365 break;
366
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600367 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500368 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
369 break;
370
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600371 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500372 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100373 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500374 2, 0);
375 break;
376
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500377 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600378 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500379 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500380 }
381 break;
382
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600383 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500384 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
385 break;
386
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600387 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500388 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100389 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500390 break;
391
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600392 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500393 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
394 break;
395
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600396 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500397 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100398 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500399 break;
400
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600401 case OP_STW:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100402 emulated = kvmppc_handle_store(run, vcpu,
403 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500404 4, 1);
405 break;
406
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600407 case OP_STWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100408 emulated = kvmppc_handle_store(run, vcpu,
409 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500410 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100411 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500412 break;
413
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600414 case OP_STB:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100415 emulated = kvmppc_handle_store(run, vcpu,
416 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500417 1, 1);
418 break;
419
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600420 case OP_STBU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100421 emulated = kvmppc_handle_store(run, vcpu,
422 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500423 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100424 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500425 break;
426
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600427 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500428 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
429 break;
430
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600431 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500432 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100433 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500434 break;
435
Alexander Graf3587d532010-02-19 11:00:30 +0100436 case OP_LHA:
Alexander Graf3587d532010-02-19 11:00:30 +0100437 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
438 break;
439
440 case OP_LHAU:
Alexander Graf3587d532010-02-19 11:00:30 +0100441 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100442 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf3587d532010-02-19 11:00:30 +0100443 break;
444
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600445 case OP_STH:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100446 emulated = kvmppc_handle_store(run, vcpu,
447 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500448 2, 1);
449 break;
450
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600451 case OP_STHU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100452 emulated = kvmppc_handle_store(run, vcpu,
453 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500454 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100455 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500456 break;
457
458 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500459 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600460 }
461
462 if (emulated == EMULATE_FAIL) {
463 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100464 if (emulated == EMULATE_AGAIN) {
465 advance = 0;
466 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600467 advance = 0;
468 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
469 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100470 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600471 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500472 }
473
Alexander Grafc7f38f42010-04-16 00:11:40 +0200474 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200475
Alexander Grafc7f38f42010-04-16 00:11:40 +0200476 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500477 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200478 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500479
480 return emulated;
481}