blob: 968f401018834aa4758ec45afeb32eee327a37c8 [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>
26
Hollis Blanchard75f74f02008-11-05 09:36:16 -060027#include <asm/reg.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050028#include <asm/time.h>
29#include <asm/byteorder.h>
30#include <asm/kvm_ppc.h>
Hollis Blanchardc381a042008-11-05 09:36:15 -060031#include <asm/disassemble.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060032#include "timing.h"
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030033#include "trace.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050034
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060035#define OP_TRAP 3
Alexander Graf513579e2009-10-30 05:47:16 +000036#define OP_TRAP_64 2
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060037
38#define OP_31_XOP_LWZX 23
39#define OP_31_XOP_LBZX 87
40#define OP_31_XOP_STWX 151
41#define OP_31_XOP_STBX 215
Alexander Graf1c85e732010-03-24 21:48:27 +010042#define OP_31_XOP_LBZUX 119
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060043#define OP_31_XOP_STBUX 247
44#define OP_31_XOP_LHZX 279
45#define OP_31_XOP_LHZUX 311
46#define OP_31_XOP_MFSPR 339
Alexander Graf1c85e732010-03-24 21:48:27 +010047#define OP_31_XOP_LHAX 343
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060048#define OP_31_XOP_STHX 407
49#define OP_31_XOP_STHUX 439
50#define OP_31_XOP_MTSPR 467
51#define OP_31_XOP_DCBI 470
52#define OP_31_XOP_LWBRX 534
53#define OP_31_XOP_TLBSYNC 566
54#define OP_31_XOP_STWBRX 662
55#define OP_31_XOP_LHBRX 790
56#define OP_31_XOP_STHBRX 918
57
58#define OP_LWZ 32
59#define OP_LWZU 33
60#define OP_LBZ 34
61#define OP_LBZU 35
62#define OP_STW 36
63#define OP_STWU 37
64#define OP_STB 38
65#define OP_STBU 39
66#define OP_LHZ 40
67#define OP_LHZU 41
Alexander Graf3587d532010-02-19 11:00:30 +010068#define OP_LHA 42
69#define OP_LHAU 43
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060070#define OP_STH 44
71#define OP_STHU 45
72
Hollis Blanchard75f74f02008-11-05 09:36:16 -060073void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050074{
Alexander Graf544c6762009-11-02 12:02:31 +000075 unsigned long dec_nsec;
Bharat Bhushandc2babfe2011-10-19 09:46:06 +053076 unsigned long long dec_time;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000077
Alexander Graf544c6762009-11-02 12:02:31 +000078 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Scott Wooddfd4d472011-11-17 12:39:59 +000079 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
80
Alexander Graf00c3a372010-04-16 00:11:42 +020081#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010082 /* mtdec lowers the interrupt line when positive. */
83 kvmppc_core_dequeue_dec(vcpu);
84
Alexander Graf513579e2009-10-30 05:47:16 +000085 /* POWER4+ triggers a dec interrupt if the value is < 0 */
86 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf513579e2009-10-30 05:47:16 +000087 kvmppc_core_queue_dec(vcpu);
88 return;
89 }
90#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050091
Scott Wooddfd4d472011-11-17 12:39:59 +000092#ifdef CONFIG_BOOKE
93 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
94 if (vcpu->arch.dec == 0)
95 return;
96#endif
97
98 /*
99 * The decrementer ticks at the same rate as the timebase, so
100 * that's how we convert the guest DEC value to the number of
101 * host ticks.
102 */
103
104 dec_time = vcpu->arch.dec;
105 dec_time *= 1000;
106 do_div(dec_time, tb_ticks_per_usec);
107 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
108 hrtimer_start(&vcpu->arch.dec_timer,
109 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
110 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500111}
112
Scott Wood5ce941e2011-04-27 17:24:21 -0500113u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
114{
115 u64 jd = tb - vcpu->arch.dec_jiffies;
Scott Wooddfd4d472011-11-17 12:39:59 +0000116
117#ifdef CONFIG_BOOKE
118 if (vcpu->arch.dec < jd)
119 return 0;
120#endif
121
Scott Wood5ce941e2011-04-27 17:24:21 -0500122 return vcpu->arch.dec - jd;
123}
124
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500125/* XXX to do:
126 * lhax
127 * lhaux
128 * lswx
129 * lswi
130 * stswx
131 * stswi
132 * lha
133 * lhau
134 * lmw
135 * stmw
136 *
137 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
138 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600139/* XXX Should probably auto-generate instruction decoding for a particular core
140 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500141int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
142{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200143 u32 inst = kvmppc_get_last_inst(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500144 u32 ea;
145 int ra;
146 int rb;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500147 int rs;
148 int rt;
149 int sprn;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500150 enum emulation_result emulated = EMULATE_DONE;
151 int advance = 1;
152
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600153 /* this default type might be overwritten by subcategories */
154 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
155
Joe Perches689fd142010-09-11 19:10:53 +0000156 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000157
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500158 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600159 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200160#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000161 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100162 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800163#else
Scott Woodb5904972011-11-08 18:23:30 -0600164 kvmppc_core_queue_program(vcpu,
165 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800166#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500167 advance = 0;
168 break;
169
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500170 case 31:
171 switch (get_xop(inst)) {
172
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600173 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500174 rt = get_rt(inst);
175 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
176 break;
177
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600178 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500179 rt = get_rt(inst);
180 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
181 break;
182
Alexander Graf1c85e732010-03-24 21:48:27 +0100183 case OP_31_XOP_LBZUX:
184 rt = get_rt(inst);
185 ra = get_ra(inst);
186 rb = get_rb(inst);
187
188 ea = kvmppc_get_gpr(vcpu, rb);
189 if (ra)
190 ea += kvmppc_get_gpr(vcpu, ra);
191
192 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
193 kvmppc_set_gpr(vcpu, ra, ea);
194 break;
195
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600196 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500197 rs = get_rs(inst);
198 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100199 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500200 4, 1);
201 break;
202
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600203 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500204 rs = get_rs(inst);
205 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100206 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500207 1, 1);
208 break;
209
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600210 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500211 rs = get_rs(inst);
212 ra = get_ra(inst);
213 rb = get_rb(inst);
214
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100215 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500216 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100217 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500218
219 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100220 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500221 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100222 kvmppc_set_gpr(vcpu, rs, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500223 break;
224
Alexander Graf1c85e732010-03-24 21:48:27 +0100225 case OP_31_XOP_LHAX:
226 rt = get_rt(inst);
227 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
228 break;
229
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600230 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500231 rt = get_rt(inst);
232 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
233 break;
234
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600235 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500236 rt = get_rt(inst);
237 ra = get_ra(inst);
238 rb = get_rb(inst);
239
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100240 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500241 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100242 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500243
244 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100245 kvmppc_set_gpr(vcpu, ra, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500246 break;
247
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600248 case OP_31_XOP_MFSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500249 sprn = get_sprn(inst);
250 rt = get_rt(inst);
251
252 switch (sprn) {
253 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200254 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr0);
255 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500256 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200257 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr1);
258 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500259 case SPRN_PVR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100260 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
Liu Yu06579dd2009-06-05 14:54:31 +0800261 case SPRN_PIR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100262 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
Alexander Graf513579e2009-10-30 05:47:16 +0000263 case SPRN_MSSSR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100264 kvmppc_set_gpr(vcpu, rt, 0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500265
266 /* Note: mftb and TBRL/TBWL are user-accessible, so
267 * the guest can always access the real TB anyways.
268 * In fact, we probably will never see these traps. */
269 case SPRN_TBWL:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100270 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500271 case SPRN_TBWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100272 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500273
274 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200275 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg0);
276 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500277 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200278 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg1);
279 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500280 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200281 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg2);
282 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500283 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200284 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg3);
285 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500286 /* Note: SPRG4-7 are user-readable, so we don't get
287 * a trap. */
288
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000289 case SPRN_DEC:
290 {
Scott Wood5ce941e2011-04-27 17:24:21 -0500291 kvmppc_set_gpr(vcpu, rt,
292 kvmppc_get_dec(vcpu, get_tb()));
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000293 break;
294 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500295 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600296 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
297 if (emulated == EMULATE_FAIL) {
298 printk("mfspr: unknown spr %x\n", sprn);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100299 kvmppc_set_gpr(vcpu, rt, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600300 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500301 break;
302 }
Scott Wood49ea0692011-03-28 15:01:24 -0500303 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500304 break;
305
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600306 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500307 rs = get_rs(inst);
308 ra = get_ra(inst);
309 rb = get_rb(inst);
310
311 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100312 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500313 2, 1);
314 break;
315
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600316 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500317 rs = get_rs(inst);
318 ra = get_ra(inst);
319 rb = get_rb(inst);
320
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100321 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500322 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100323 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500324
325 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100326 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500327 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100328 kvmppc_set_gpr(vcpu, ra, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500329 break;
330
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600331 case OP_31_XOP_MTSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500332 sprn = get_sprn(inst);
333 rs = get_rs(inst);
334 switch (sprn) {
335 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200336 vcpu->arch.shared->srr0 = kvmppc_get_gpr(vcpu, rs);
337 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500338 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200339 vcpu->arch.shared->srr1 = kvmppc_get_gpr(vcpu, rs);
340 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500341
342 /* XXX We need to context-switch the timebase for
343 * watchdog and FIT. */
344 case SPRN_TBWL: break;
345 case SPRN_TBWU: break;
346
Alexander Graf513579e2009-10-30 05:47:16 +0000347 case SPRN_MSSSR0: break;
348
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500349 case SPRN_DEC:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100350 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500351 kvmppc_emulate_dec(vcpu);
352 break;
353
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500354 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200355 vcpu->arch.shared->sprg0 = kvmppc_get_gpr(vcpu, rs);
356 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200358 vcpu->arch.shared->sprg1 = kvmppc_get_gpr(vcpu, rs);
359 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500360 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200361 vcpu->arch.shared->sprg2 = kvmppc_get_gpr(vcpu, rs);
362 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500363 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200364 vcpu->arch.shared->sprg3 = kvmppc_get_gpr(vcpu, rs);
365 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500366
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500367 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600368 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
369 if (emulated == EMULATE_FAIL)
370 printk("mtspr: unknown spr %x\n", sprn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500371 break;
372 }
Scott Wood49ea0692011-03-28 15:01:24 -0500373 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500374 break;
375
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600376 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500377 /* Do nothing. The guest is performing dcbi because
378 * hardware DMA is not snooped by the dcache, but
379 * emulated DMA either goes through the dcache as
380 * normal writes, or the host kernel has handled dcache
381 * coherence. */
382 break;
383
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600384 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500385 rt = get_rt(inst);
386 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
387 break;
388
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600389 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500390 break;
391
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600392 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500393 rs = get_rs(inst);
394 ra = get_ra(inst);
395 rb = get_rb(inst);
396
397 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100398 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500399 4, 0);
400 break;
401
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600402 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500403 rt = get_rt(inst);
404 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
405 break;
406
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600407 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500408 rs = get_rs(inst);
409 ra = get_ra(inst);
410 rb = get_rb(inst);
411
412 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100413 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500414 2, 0);
415 break;
416
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500417 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600418 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500419 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500420 }
421 break;
422
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600423 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500424 rt = get_rt(inst);
425 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
426 break;
427
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600428 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500429 ra = get_ra(inst);
430 rt = get_rt(inst);
431 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100432 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500433 break;
434
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600435 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500436 rt = get_rt(inst);
437 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
438 break;
439
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600440 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500441 ra = get_ra(inst);
442 rt = get_rt(inst);
443 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100444 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500445 break;
446
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600447 case OP_STW:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500448 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100449 emulated = kvmppc_handle_store(run, vcpu,
450 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500451 4, 1);
452 break;
453
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600454 case OP_STWU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500455 ra = get_ra(inst);
456 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100457 emulated = kvmppc_handle_store(run, vcpu,
458 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500459 4, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100460 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500461 break;
462
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600463 case OP_STB:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500464 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100465 emulated = kvmppc_handle_store(run, vcpu,
466 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500467 1, 1);
468 break;
469
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600470 case OP_STBU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500471 ra = get_ra(inst);
472 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100473 emulated = kvmppc_handle_store(run, vcpu,
474 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500475 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100476 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500477 break;
478
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600479 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500480 rt = get_rt(inst);
481 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
482 break;
483
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600484 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500485 ra = get_ra(inst);
486 rt = get_rt(inst);
487 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100488 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500489 break;
490
Alexander Graf3587d532010-02-19 11:00:30 +0100491 case OP_LHA:
492 rt = get_rt(inst);
493 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
494 break;
495
496 case OP_LHAU:
497 ra = get_ra(inst);
498 rt = get_rt(inst);
499 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
500 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
501 break;
502
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600503 case OP_STH:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500504 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100505 emulated = kvmppc_handle_store(run, vcpu,
506 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500507 2, 1);
508 break;
509
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600510 case OP_STHU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500511 ra = get_ra(inst);
512 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100513 emulated = kvmppc_handle_store(run, vcpu,
514 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500515 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100516 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500517 break;
518
519 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500520 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600521 }
522
523 if (emulated == EMULATE_FAIL) {
524 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100525 if (emulated == EMULATE_AGAIN) {
526 advance = 0;
527 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600528 advance = 0;
529 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
530 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100531 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600532 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500533 }
534
Alexander Grafc7f38f42010-04-16 00:11:40 +0200535 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200536
Alexander Grafc7f38f42010-04-16 00:11:40 +0200537 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500538 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200539 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500540
541 return emulated;
542}