blob: f90e86dea7a2cfdef3a3321a878a370cdde4b287 [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 Bhushandc2babf2011-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;
Alexander Graf54771e62012-05-04 14:55:12 +0200157 ulong spr_val = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500158
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600159 /* this default type might be overwritten by subcategories */
160 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
161
Joe Perches689fd142010-09-11 19:10:53 +0000162 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000163
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500164 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600165 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200166#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000167 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100168 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800169#else
Scott Woodb5904972011-11-08 18:23:30 -0600170 kvmppc_core_queue_program(vcpu,
171 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800172#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500173 advance = 0;
174 break;
175
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500176 case 31:
177 switch (get_xop(inst)) {
178
Alexander Graf6df79df2012-03-13 22:15:45 +0100179 case OP_31_XOP_TRAP:
180#ifdef CONFIG_64BIT
181 case OP_31_XOP_TRAP_64:
182#endif
183#ifdef CONFIG_PPC_BOOK3S
184 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
185#else
186 kvmppc_core_queue_program(vcpu,
187 vcpu->arch.shared->esr | ESR_PTR);
188#endif
189 advance = 0;
190 break;
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600191 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500192 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
193 break;
194
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600195 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500196 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
197 break;
198
Alexander Graf1c85e732010-03-24 21:48:27 +0100199 case OP_31_XOP_LBZUX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100200 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100201 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf1c85e732010-03-24 21:48:27 +0100202 break;
203
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600204 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500205 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100206 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500207 4, 1);
208 break;
209
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600210 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500211 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100212 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500213 1, 1);
214 break;
215
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600216 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500217 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100218 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500219 1, 1);
Alexander Graf978b4fa2012-04-27 01:00:17 +0200220 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500221 break;
222
Alexander Graf1c85e732010-03-24 21:48:27 +0100223 case OP_31_XOP_LHAX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100224 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
225 break;
226
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600227 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500228 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
229 break;
230
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600231 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500232 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100233 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500234 break;
235
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600236 case OP_31_XOP_MFSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500237 switch (sprn) {
238 case SPRN_SRR0:
Alexander Graf54771e62012-05-04 14:55:12 +0200239 spr_val = vcpu->arch.shared->srr0;
Alexander Grafde7906c2010-07-29 14:47:46 +0200240 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500241 case SPRN_SRR1:
Alexander Graf54771e62012-05-04 14:55:12 +0200242 spr_val = vcpu->arch.shared->srr1;
Alexander Grafde7906c2010-07-29 14:47:46 +0200243 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500244 case SPRN_PVR:
Alexander Graf54771e62012-05-04 14:55:12 +0200245 spr_val = vcpu->arch.pvr;
246 break;
Liu Yu06579dd2009-06-05 14:54:31 +0800247 case SPRN_PIR:
Alexander Graf54771e62012-05-04 14:55:12 +0200248 spr_val = vcpu->vcpu_id;
249 break;
Alexander Graf513579e2009-10-30 05:47:16 +0000250 case SPRN_MSSSR0:
Alexander Graf54771e62012-05-04 14:55:12 +0200251 spr_val = 0;
252 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500253
254 /* Note: mftb and TBRL/TBWL are user-accessible, so
255 * the guest can always access the real TB anyways.
256 * In fact, we probably will never see these traps. */
257 case SPRN_TBWL:
Alexander Graf54771e62012-05-04 14:55:12 +0200258 spr_val = get_tb() >> 32;
259 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500260 case SPRN_TBWU:
Alexander Graf54771e62012-05-04 14:55:12 +0200261 spr_val = get_tb();
262 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500263
264 case SPRN_SPRG0:
Alexander Graf54771e62012-05-04 14:55:12 +0200265 spr_val = vcpu->arch.shared->sprg0;
Alexander Grafa73a9592010-07-29 14:47:47 +0200266 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500267 case SPRN_SPRG1:
Alexander Graf54771e62012-05-04 14:55:12 +0200268 spr_val = vcpu->arch.shared->sprg1;
Alexander Grafa73a9592010-07-29 14:47:47 +0200269 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500270 case SPRN_SPRG2:
Alexander Graf54771e62012-05-04 14:55:12 +0200271 spr_val = vcpu->arch.shared->sprg2;
Alexander Grafa73a9592010-07-29 14:47:47 +0200272 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500273 case SPRN_SPRG3:
Alexander Graf54771e62012-05-04 14:55:12 +0200274 spr_val = vcpu->arch.shared->sprg3;
Alexander Grafa73a9592010-07-29 14:47:47 +0200275 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500276 /* Note: SPRG4-7 are user-readable, so we don't get
277 * a trap. */
278
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000279 case SPRN_DEC:
Alexander Graf54771e62012-05-04 14:55:12 +0200280 spr_val = kvmppc_get_dec(vcpu, get_tb());
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000281 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500282 default:
Alexander Graf54771e62012-05-04 14:55:12 +0200283 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn,
284 &spr_val);
285 if (unlikely(emulated == EMULATE_FAIL)) {
286 printk(KERN_INFO "mfspr: unknown spr "
287 "0x%x\n", sprn);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600288 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500289 break;
290 }
Alexander Graf54771e62012-05-04 14:55:12 +0200291 kvmppc_set_gpr(vcpu, rt, spr_val);
Scott Wood49ea0692011-03-28 15:01:24 -0500292 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293 break;
294
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600295 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500296 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100297 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500298 2, 1);
299 break;
300
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600301 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500302 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100303 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500304 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100305 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500306 break;
307
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600308 case OP_31_XOP_MTSPR:
Alexander Graf54771e62012-05-04 14:55:12 +0200309 spr_val = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500310 switch (sprn) {
311 case SPRN_SRR0:
Alexander Graf54771e62012-05-04 14:55:12 +0200312 vcpu->arch.shared->srr0 = spr_val;
Alexander Grafde7906c2010-07-29 14:47:46 +0200313 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500314 case SPRN_SRR1:
Alexander Graf54771e62012-05-04 14:55:12 +0200315 vcpu->arch.shared->srr1 = spr_val;
Alexander Grafde7906c2010-07-29 14:47:46 +0200316 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500317
318 /* XXX We need to context-switch the timebase for
319 * watchdog and FIT. */
320 case SPRN_TBWL: break;
321 case SPRN_TBWU: break;
322
Alexander Graf513579e2009-10-30 05:47:16 +0000323 case SPRN_MSSSR0: break;
324
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500325 case SPRN_DEC:
Alexander Graf54771e62012-05-04 14:55:12 +0200326 vcpu->arch.dec = spr_val;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500327 kvmppc_emulate_dec(vcpu);
328 break;
329
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500330 case SPRN_SPRG0:
Alexander Graf54771e62012-05-04 14:55:12 +0200331 vcpu->arch.shared->sprg0 = spr_val;
Alexander Grafa73a9592010-07-29 14:47:47 +0200332 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500333 case SPRN_SPRG1:
Alexander Graf54771e62012-05-04 14:55:12 +0200334 vcpu->arch.shared->sprg1 = spr_val;
Alexander Grafa73a9592010-07-29 14:47:47 +0200335 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500336 case SPRN_SPRG2:
Alexander Graf54771e62012-05-04 14:55:12 +0200337 vcpu->arch.shared->sprg2 = spr_val;
Alexander Grafa73a9592010-07-29 14:47:47 +0200338 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500339 case SPRN_SPRG3:
Alexander Graf54771e62012-05-04 14:55:12 +0200340 vcpu->arch.shared->sprg3 = spr_val;
Alexander Grafa73a9592010-07-29 14:47:47 +0200341 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500342
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500343 default:
Alexander Graf54771e62012-05-04 14:55:12 +0200344 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn,
345 spr_val);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600346 if (emulated == EMULATE_FAIL)
Alexander Graf54771e62012-05-04 14:55:12 +0200347 printk(KERN_INFO "mtspr: unknown spr "
348 "0x%x\n", sprn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500349 break;
350 }
Scott Wood49ea0692011-03-28 15:01:24 -0500351 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500352 break;
353
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600354 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500355 /* Do nothing. The guest is performing dcbi because
356 * hardware DMA is not snooped by the dcache, but
357 * emulated DMA either goes through the dcache as
358 * normal writes, or the host kernel has handled dcache
359 * coherence. */
360 break;
361
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600362 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500363 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
364 break;
365
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600366 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500367 break;
368
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600369 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500370 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100371 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500372 4, 0);
373 break;
374
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600375 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500376 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
377 break;
378
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600379 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500380 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100381 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500382 2, 0);
383 break;
384
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500385 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600386 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500387 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500388 }
389 break;
390
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600391 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500392 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
393 break;
394
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600395 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500396 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100397 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500398 break;
399
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600400 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500401 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
402 break;
403
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600404 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500405 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100406 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407 break;
408
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600409 case OP_STW:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100410 emulated = kvmppc_handle_store(run, vcpu,
411 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500412 4, 1);
413 break;
414
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600415 case OP_STWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100416 emulated = kvmppc_handle_store(run, vcpu,
417 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500418 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100419 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500420 break;
421
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600422 case OP_STB:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100423 emulated = kvmppc_handle_store(run, vcpu,
424 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500425 1, 1);
426 break;
427
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600428 case OP_STBU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100429 emulated = kvmppc_handle_store(run, vcpu,
430 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500431 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100432 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500433 break;
434
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600435 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500436 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
437 break;
438
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600439 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500440 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 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
Alexander Graf3587d532010-02-19 11:00:30 +0100444 case OP_LHA:
Alexander Graf3587d532010-02-19 11:00:30 +0100445 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
446 break;
447
448 case OP_LHAU:
Alexander Graf3587d532010-02-19 11:00:30 +0100449 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100450 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf3587d532010-02-19 11:00:30 +0100451 break;
452
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600453 case OP_STH:
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 2, 1);
457 break;
458
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600459 case OP_STHU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100460 emulated = kvmppc_handle_store(run, vcpu,
461 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500462 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100463 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500464 break;
465
466 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500467 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600468 }
469
470 if (emulated == EMULATE_FAIL) {
471 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100472 if (emulated == EMULATE_AGAIN) {
473 advance = 0;
474 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600475 advance = 0;
476 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
477 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100478 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600479 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500480 }
481
Alexander Grafc7f38f42010-04-16 00:11:40 +0200482 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200483
Alexander Grafc7f38f42010-04-16 00:11:40 +0200484 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500485 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200486 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500487
488 return emulated;
489}