blob: afc9154f1aef1bbff29fe1d7fa70d425061a70c9 [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
Alexander Graf6df79df2012-03-13 22:15:45 +010038#define OP_31_XOP_TRAP 4
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060039#define OP_31_XOP_LWZX 23
Alexander Graf6df79df2012-03-13 22:15:45 +010040#define OP_31_XOP_TRAP_64 68
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060041#define OP_31_XOP_LBZX 87
42#define OP_31_XOP_STWX 151
43#define OP_31_XOP_STBX 215
Alexander Graf1c85e732010-03-24 21:48:27 +010044#define OP_31_XOP_LBZUX 119
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060045#define OP_31_XOP_STBUX 247
46#define OP_31_XOP_LHZX 279
47#define OP_31_XOP_LHZUX 311
48#define OP_31_XOP_MFSPR 339
Alexander Graf1c85e732010-03-24 21:48:27 +010049#define OP_31_XOP_LHAX 343
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060050#define OP_31_XOP_STHX 407
51#define OP_31_XOP_STHUX 439
52#define OP_31_XOP_MTSPR 467
53#define OP_31_XOP_DCBI 470
54#define OP_31_XOP_LWBRX 534
55#define OP_31_XOP_TLBSYNC 566
56#define OP_31_XOP_STWBRX 662
57#define OP_31_XOP_LHBRX 790
58#define OP_31_XOP_STHBRX 918
59
60#define OP_LWZ 32
61#define OP_LWZU 33
62#define OP_LBZ 34
63#define OP_LBZU 35
64#define OP_STW 36
65#define OP_STWU 37
66#define OP_STB 38
67#define OP_STBU 39
68#define OP_LHZ 40
69#define OP_LHZU 41
Alexander Graf3587d532010-02-19 11:00:30 +010070#define OP_LHA 42
71#define OP_LHAU 43
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060072#define OP_STH 44
73#define OP_STHU 45
74
Hollis Blanchard75f74f02008-11-05 09:36:16 -060075void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050076{
Alexander Graf544c6762009-11-02 12:02:31 +000077 unsigned long dec_nsec;
Bharat Bhushandc2babf2011-10-19 09:46:06 +053078 unsigned long long dec_time;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000079
Alexander Graf544c6762009-11-02 12:02:31 +000080 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Scott Wooddfd4d472011-11-17 12:39:59 +000081 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
82
Alexander Graf00c3a372010-04-16 00:11:42 +020083#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010084 /* mtdec lowers the interrupt line when positive. */
85 kvmppc_core_dequeue_dec(vcpu);
86
Alexander Graf513579e2009-10-30 05:47:16 +000087 /* POWER4+ triggers a dec interrupt if the value is < 0 */
88 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf513579e2009-10-30 05:47:16 +000089 kvmppc_core_queue_dec(vcpu);
90 return;
91 }
92#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050093
Scott Wooddfd4d472011-11-17 12:39:59 +000094#ifdef CONFIG_BOOKE
95 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
96 if (vcpu->arch.dec == 0)
97 return;
98#endif
99
100 /*
101 * The decrementer ticks at the same rate as the timebase, so
102 * that's how we convert the guest DEC value to the number of
103 * host ticks.
104 */
105
106 dec_time = vcpu->arch.dec;
107 dec_time *= 1000;
108 do_div(dec_time, tb_ticks_per_usec);
109 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
110 hrtimer_start(&vcpu->arch.dec_timer,
111 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
112 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500113}
114
Scott Wood5ce941e2011-04-27 17:24:21 -0500115u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
116{
117 u64 jd = tb - vcpu->arch.dec_jiffies;
Scott Wooddfd4d472011-11-17 12:39:59 +0000118
119#ifdef CONFIG_BOOKE
120 if (vcpu->arch.dec < jd)
121 return 0;
122#endif
123
Scott Wood5ce941e2011-04-27 17:24:21 -0500124 return vcpu->arch.dec - jd;
125}
126
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500127/* XXX to do:
128 * lhax
129 * lhaux
130 * lswx
131 * lswi
132 * stswx
133 * stswi
134 * lha
135 * lhau
136 * lmw
137 * stmw
138 *
139 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
140 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600141/* XXX Should probably auto-generate instruction decoding for a particular core
142 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500143int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
144{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200145 u32 inst = kvmppc_get_last_inst(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500146 int ra;
147 int rb;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500148 int rs;
149 int rt;
150 int sprn;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500151 enum emulation_result emulated = EMULATE_DONE;
152 int advance = 1;
153
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600154 /* this default type might be overwritten by subcategories */
155 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
156
Joe Perches689fd142010-09-11 19:10:53 +0000157 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000158
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500159 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600160 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200161#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000162 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100163 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800164#else
Scott Woodb5904972011-11-08 18:23:30 -0600165 kvmppc_core_queue_program(vcpu,
166 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800167#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500168 advance = 0;
169 break;
170
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500171 case 31:
172 switch (get_xop(inst)) {
173
Alexander Graf6df79df2012-03-13 22:15:45 +0100174 case OP_31_XOP_TRAP:
175#ifdef CONFIG_64BIT
176 case OP_31_XOP_TRAP_64:
177#endif
178#ifdef CONFIG_PPC_BOOK3S
179 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
180#else
181 kvmppc_core_queue_program(vcpu,
182 vcpu->arch.shared->esr | ESR_PTR);
183#endif
184 advance = 0;
185 break;
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600186 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500187 rt = get_rt(inst);
188 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
189 break;
190
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600191 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500192 rt = get_rt(inst);
193 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
194 break;
195
Alexander Graf1c85e732010-03-24 21:48:27 +0100196 case OP_31_XOP_LBZUX:
197 rt = get_rt(inst);
198 ra = get_ra(inst);
199 rb = get_rb(inst);
200
Alexander Graf1c85e732010-03-24 21:48:27 +0100201 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100202 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf1c85e732010-03-24 21:48:27 +0100203 break;
204
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600205 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500206 rs = get_rs(inst);
207 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100208 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500209 4, 1);
210 break;
211
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600212 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500213 rs = get_rs(inst);
214 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100215 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500216 1, 1);
217 break;
218
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600219 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500220 rs = get_rs(inst);
221 ra = get_ra(inst);
222 rb = get_rb(inst);
223
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500224 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100225 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500226 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100227 kvmppc_set_gpr(vcpu, rs, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500228 break;
229
Alexander Graf1c85e732010-03-24 21:48:27 +0100230 case OP_31_XOP_LHAX:
231 rt = get_rt(inst);
232 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
233 break;
234
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600235 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500236 rt = get_rt(inst);
237 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
238 break;
239
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600240 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500241 rt = get_rt(inst);
242 ra = get_ra(inst);
243 rb = get_rb(inst);
244
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500245 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100246 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500247 break;
248
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600249 case OP_31_XOP_MFSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500250 sprn = get_sprn(inst);
251 rt = get_rt(inst);
252
253 switch (sprn) {
254 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200255 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr0);
256 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500257 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200258 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr1);
259 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500260 case SPRN_PVR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100261 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
Liu Yu06579dd2009-06-05 14:54:31 +0800262 case SPRN_PIR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100263 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
Alexander Graf513579e2009-10-30 05:47:16 +0000264 case SPRN_MSSSR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100265 kvmppc_set_gpr(vcpu, rt, 0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500266
267 /* Note: mftb and TBRL/TBWL are user-accessible, so
268 * the guest can always access the real TB anyways.
269 * In fact, we probably will never see these traps. */
270 case SPRN_TBWL:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100271 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500272 case SPRN_TBWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100273 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500274
275 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200276 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg0);
277 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500278 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200279 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg1);
280 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500281 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200282 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg2);
283 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500284 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200285 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg3);
286 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500287 /* Note: SPRG4-7 are user-readable, so we don't get
288 * a trap. */
289
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000290 case SPRN_DEC:
291 {
Scott Wood5ce941e2011-04-27 17:24:21 -0500292 kvmppc_set_gpr(vcpu, rt,
293 kvmppc_get_dec(vcpu, get_tb()));
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000294 break;
295 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500296 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600297 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
298 if (emulated == EMULATE_FAIL) {
299 printk("mfspr: unknown spr %x\n", sprn);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100300 kvmppc_set_gpr(vcpu, rt, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600301 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500302 break;
303 }
Scott Wood49ea0692011-03-28 15:01:24 -0500304 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500305 break;
306
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600307 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500308 rs = get_rs(inst);
309 ra = get_ra(inst);
310 rb = get_rb(inst);
311
312 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100313 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500314 2, 1);
315 break;
316
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600317 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500318 rs = get_rs(inst);
319 ra = get_ra(inst);
320 rb = get_rb(inst);
321
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500322 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100323 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500324 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100325 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500326 break;
327
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600328 case OP_31_XOP_MTSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500329 sprn = get_sprn(inst);
330 rs = get_rs(inst);
331 switch (sprn) {
332 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200333 vcpu->arch.shared->srr0 = kvmppc_get_gpr(vcpu, rs);
334 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500335 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200336 vcpu->arch.shared->srr1 = kvmppc_get_gpr(vcpu, rs);
337 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500338
339 /* XXX We need to context-switch the timebase for
340 * watchdog and FIT. */
341 case SPRN_TBWL: break;
342 case SPRN_TBWU: break;
343
Alexander Graf513579e2009-10-30 05:47:16 +0000344 case SPRN_MSSSR0: break;
345
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500346 case SPRN_DEC:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100347 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500348 kvmppc_emulate_dec(vcpu);
349 break;
350
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500351 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200352 vcpu->arch.shared->sprg0 = kvmppc_get_gpr(vcpu, rs);
353 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500354 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200355 vcpu->arch.shared->sprg1 = kvmppc_get_gpr(vcpu, rs);
356 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200358 vcpu->arch.shared->sprg2 = kvmppc_get_gpr(vcpu, rs);
359 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500360 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200361 vcpu->arch.shared->sprg3 = kvmppc_get_gpr(vcpu, rs);
362 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500363
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500364 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600365 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
366 if (emulated == EMULATE_FAIL)
367 printk("mtspr: unknown spr %x\n", sprn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500368 break;
369 }
Scott Wood49ea0692011-03-28 15:01:24 -0500370 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500371 break;
372
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600373 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500374 /* Do nothing. The guest is performing dcbi because
375 * hardware DMA is not snooped by the dcache, but
376 * emulated DMA either goes through the dcache as
377 * normal writes, or the host kernel has handled dcache
378 * coherence. */
379 break;
380
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600381 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500382 rt = get_rt(inst);
383 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
384 break;
385
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600386 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500387 break;
388
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600389 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500390 rs = get_rs(inst);
391 ra = get_ra(inst);
392 rb = get_rb(inst);
393
394 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100395 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500396 4, 0);
397 break;
398
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600399 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500400 rt = get_rt(inst);
401 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
402 break;
403
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600404 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500405 rs = get_rs(inst);
406 ra = get_ra(inst);
407 rb = get_rb(inst);
408
409 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100410 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500411 2, 0);
412 break;
413
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500414 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600415 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500416 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500417 }
418 break;
419
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600420 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500421 rt = get_rt(inst);
422 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
423 break;
424
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600425 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500426 ra = get_ra(inst);
427 rt = get_rt(inst);
428 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100429 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500430 break;
431
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600432 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500433 rt = get_rt(inst);
434 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
435 break;
436
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600437 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500438 ra = get_ra(inst);
439 rt = get_rt(inst);
440 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100441 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500442 break;
443
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600444 case OP_STW:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500445 rs = get_rs(inst);
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 4, 1);
449 break;
450
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600451 case OP_STWU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500452 ra = get_ra(inst);
453 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100454 emulated = kvmppc_handle_store(run, vcpu,
455 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500456 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100457 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500458 break;
459
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600460 case OP_STB:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500461 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100462 emulated = kvmppc_handle_store(run, vcpu,
463 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500464 1, 1);
465 break;
466
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600467 case OP_STBU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500468 ra = get_ra(inst);
469 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100470 emulated = kvmppc_handle_store(run, vcpu,
471 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500472 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100473 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500474 break;
475
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600476 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500477 rt = get_rt(inst);
478 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
479 break;
480
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600481 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500482 ra = get_ra(inst);
483 rt = get_rt(inst);
484 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100485 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500486 break;
487
Alexander Graf3587d532010-02-19 11:00:30 +0100488 case OP_LHA:
489 rt = get_rt(inst);
490 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
491 break;
492
493 case OP_LHAU:
494 ra = get_ra(inst);
495 rt = get_rt(inst);
496 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100497 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf3587d532010-02-19 11:00:30 +0100498 break;
499
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600500 case OP_STH:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500501 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100502 emulated = kvmppc_handle_store(run, vcpu,
503 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500504 2, 1);
505 break;
506
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600507 case OP_STHU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500508 ra = get_ra(inst);
509 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100510 emulated = kvmppc_handle_store(run, vcpu,
511 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500512 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100513 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500514 break;
515
516 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500517 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600518 }
519
520 if (emulated == EMULATE_FAIL) {
521 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100522 if (emulated == EMULATE_AGAIN) {
523 advance = 0;
524 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600525 advance = 0;
526 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
527 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100528 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600529 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500530 }
531
Alexander Grafc7f38f42010-04-16 00:11:40 +0200532 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200533
Alexander Grafc7f38f42010-04-16 00:11:40 +0200534 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500535 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200536 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500537
538 return emulated;
539}