blob: ee04abaefe233c89e692a9e1a84e4686d4da8030 [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
Varun Sethi6c5cb732012-06-18 12:14:55 +000062#define OP_LD 58
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060063#define OP_LWZU 33
64#define OP_LBZ 34
65#define OP_LBZU 35
66#define OP_STW 36
67#define OP_STWU 37
Varun Sethi6c5cb732012-06-18 12:14:55 +000068#define OP_STD 62
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060069#define OP_STB 38
70#define OP_STBU 39
71#define OP_LHZ 40
72#define OP_LHZU 41
Alexander Graf3587d532010-02-19 11:00:30 +010073#define OP_LHA 42
74#define OP_LHAU 43
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060075#define OP_STH 44
76#define OP_STHU 45
77
Hollis Blanchard75f74f02008-11-05 09:36:16 -060078void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050079{
Alexander Graf544c6762009-11-02 12:02:31 +000080 unsigned long dec_nsec;
Bharat Bhushandc2babf2011-10-19 09:46:06 +053081 unsigned long long dec_time;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000082
Alexander Graf544c6762009-11-02 12:02:31 +000083 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Scott Wooddfd4d472011-11-17 12:39:59 +000084 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
85
Alexander Graf00c3a372010-04-16 00:11:42 +020086#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010087 /* mtdec lowers the interrupt line when positive. */
88 kvmppc_core_dequeue_dec(vcpu);
89
Alexander Graf513579e2009-10-30 05:47:16 +000090 /* POWER4+ triggers a dec interrupt if the value is < 0 */
91 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf513579e2009-10-30 05:47:16 +000092 kvmppc_core_queue_dec(vcpu);
93 return;
94 }
95#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050096
Scott Wooddfd4d472011-11-17 12:39:59 +000097#ifdef CONFIG_BOOKE
98 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
99 if (vcpu->arch.dec == 0)
100 return;
101#endif
102
103 /*
104 * The decrementer ticks at the same rate as the timebase, so
105 * that's how we convert the guest DEC value to the number of
106 * host ticks.
107 */
108
109 dec_time = vcpu->arch.dec;
Bharat Bhushan6e359942012-04-18 06:01:19 +0000110 /*
111 * Guest timebase ticks at the same frequency as host decrementer.
112 * So use the host decrementer calculations for decrementer emulation.
113 */
114 dec_time = dec_time << decrementer_clockevent.shift;
115 do_div(dec_time, decrementer_clockevent.mult);
Scott Wooddfd4d472011-11-17 12:39:59 +0000116 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
117 hrtimer_start(&vcpu->arch.dec_timer,
118 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
119 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500120}
121
Scott Wood5ce941e2011-04-27 17:24:21 -0500122u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
123{
124 u64 jd = tb - vcpu->arch.dec_jiffies;
Scott Wooddfd4d472011-11-17 12:39:59 +0000125
126#ifdef CONFIG_BOOKE
127 if (vcpu->arch.dec < jd)
128 return 0;
129#endif
130
Scott Wood5ce941e2011-04-27 17:24:21 -0500131 return vcpu->arch.dec - jd;
132}
133
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500134/* XXX to do:
135 * lhax
136 * lhaux
137 * lswx
138 * lswi
139 * stswx
140 * stswi
141 * lha
142 * lhau
143 * lmw
144 * stmw
145 *
146 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
147 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600148/* XXX Should probably auto-generate instruction decoding for a particular core
149 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500150int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
151{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200152 u32 inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200153 int ra = get_ra(inst);
154 int rs = get_rs(inst);
155 int rt = get_rt(inst);
156 int sprn = get_sprn(inst);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500157 enum emulation_result emulated = EMULATE_DONE;
158 int advance = 1;
Alexander Graf54771e62012-05-04 14:55:12 +0200159 ulong spr_val = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500160
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600161 /* this default type might be overwritten by subcategories */
162 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
163
Joe Perches689fd142010-09-11 19:10:53 +0000164 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000165
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500166 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600167 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200168#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000169 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100170 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800171#else
Scott Woodb5904972011-11-08 18:23:30 -0600172 kvmppc_core_queue_program(vcpu,
173 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800174#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500175 advance = 0;
176 break;
177
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500178 case 31:
179 switch (get_xop(inst)) {
180
Alexander Graf6df79df2012-03-13 22:15:45 +0100181 case OP_31_XOP_TRAP:
182#ifdef CONFIG_64BIT
183 case OP_31_XOP_TRAP_64:
184#endif
185#ifdef CONFIG_PPC_BOOK3S
186 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
187#else
188 kvmppc_core_queue_program(vcpu,
189 vcpu->arch.shared->esr | ESR_PTR);
190#endif
191 advance = 0;
192 break;
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600193 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500194 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
195 break;
196
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600197 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500198 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
199 break;
200
Alexander Graf1c85e732010-03-24 21:48:27 +0100201 case OP_31_XOP_LBZUX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100202 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100203 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf1c85e732010-03-24 21:48:27 +0100204 break;
205
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600206 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500207 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 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100214 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500215 1, 1);
216 break;
217
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600218 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500219 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 Graf978b4fa2012-04-27 01:00:17 +0200222 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500223 break;
224
Alexander Graf1c85e732010-03-24 21:48:27 +0100225 case OP_31_XOP_LHAX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100226 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
227 break;
228
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600229 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500230 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
231 break;
232
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600233 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500234 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100235 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500236 break;
237
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600238 case OP_31_XOP_MFSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500239 switch (sprn) {
240 case SPRN_SRR0:
Alexander Graf54771e62012-05-04 14:55:12 +0200241 spr_val = vcpu->arch.shared->srr0;
Alexander Grafde7906c2010-07-29 14:47:46 +0200242 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500243 case SPRN_SRR1:
Alexander Graf54771e62012-05-04 14:55:12 +0200244 spr_val = vcpu->arch.shared->srr1;
Alexander Grafde7906c2010-07-29 14:47:46 +0200245 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500246 case SPRN_PVR:
Alexander Graf54771e62012-05-04 14:55:12 +0200247 spr_val = vcpu->arch.pvr;
248 break;
Liu Yu06579dd2009-06-05 14:54:31 +0800249 case SPRN_PIR:
Alexander Graf54771e62012-05-04 14:55:12 +0200250 spr_val = vcpu->vcpu_id;
251 break;
Alexander Graf513579e2009-10-30 05:47:16 +0000252 case SPRN_MSSSR0:
Alexander Graf54771e62012-05-04 14:55:12 +0200253 spr_val = 0;
254 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500255
256 /* Note: mftb and TBRL/TBWL are user-accessible, so
257 * the guest can always access the real TB anyways.
258 * In fact, we probably will never see these traps. */
259 case SPRN_TBWL:
Alexander Graf54771e62012-05-04 14:55:12 +0200260 spr_val = get_tb() >> 32;
261 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500262 case SPRN_TBWU:
Alexander Graf54771e62012-05-04 14:55:12 +0200263 spr_val = get_tb();
264 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500265
266 case SPRN_SPRG0:
Alexander Graf54771e62012-05-04 14:55:12 +0200267 spr_val = vcpu->arch.shared->sprg0;
Alexander Grafa73a9592010-07-29 14:47:47 +0200268 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500269 case SPRN_SPRG1:
Alexander Graf54771e62012-05-04 14:55:12 +0200270 spr_val = vcpu->arch.shared->sprg1;
Alexander Grafa73a9592010-07-29 14:47:47 +0200271 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500272 case SPRN_SPRG2:
Alexander Graf54771e62012-05-04 14:55:12 +0200273 spr_val = vcpu->arch.shared->sprg2;
Alexander Grafa73a9592010-07-29 14:47:47 +0200274 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500275 case SPRN_SPRG3:
Alexander Graf54771e62012-05-04 14:55:12 +0200276 spr_val = vcpu->arch.shared->sprg3;
Alexander Grafa73a9592010-07-29 14:47:47 +0200277 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500278 /* Note: SPRG4-7 are user-readable, so we don't get
279 * a trap. */
280
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000281 case SPRN_DEC:
Alexander Graf54771e62012-05-04 14:55:12 +0200282 spr_val = kvmppc_get_dec(vcpu, get_tb());
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000283 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500284 default:
Alexander Graf54771e62012-05-04 14:55:12 +0200285 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn,
286 &spr_val);
287 if (unlikely(emulated == EMULATE_FAIL)) {
288 printk(KERN_INFO "mfspr: unknown spr "
289 "0x%x\n", sprn);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600290 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500291 break;
292 }
Alexander Graf54771e62012-05-04 14:55:12 +0200293 kvmppc_set_gpr(vcpu, rt, spr_val);
Scott Wood49ea0692011-03-28 15:01:24 -0500294 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500295 break;
296
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600297 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500298 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100299 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500300 2, 1);
301 break;
302
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600303 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500304 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100305 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500306 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100307 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500308 break;
309
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600310 case OP_31_XOP_MTSPR:
Alexander Graf54771e62012-05-04 14:55:12 +0200311 spr_val = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500312 switch (sprn) {
313 case SPRN_SRR0:
Alexander Graf54771e62012-05-04 14:55:12 +0200314 vcpu->arch.shared->srr0 = spr_val;
Alexander Grafde7906c2010-07-29 14:47:46 +0200315 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500316 case SPRN_SRR1:
Alexander Graf54771e62012-05-04 14:55:12 +0200317 vcpu->arch.shared->srr1 = spr_val;
Alexander Grafde7906c2010-07-29 14:47:46 +0200318 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500319
320 /* XXX We need to context-switch the timebase for
321 * watchdog and FIT. */
322 case SPRN_TBWL: break;
323 case SPRN_TBWU: break;
324
Alexander Graf513579e2009-10-30 05:47:16 +0000325 case SPRN_MSSSR0: break;
326
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500327 case SPRN_DEC:
Alexander Graf54771e62012-05-04 14:55:12 +0200328 vcpu->arch.dec = spr_val;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500329 kvmppc_emulate_dec(vcpu);
330 break;
331
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500332 case SPRN_SPRG0:
Alexander Graf54771e62012-05-04 14:55:12 +0200333 vcpu->arch.shared->sprg0 = spr_val;
Alexander Grafa73a9592010-07-29 14:47:47 +0200334 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500335 case SPRN_SPRG1:
Alexander Graf54771e62012-05-04 14:55:12 +0200336 vcpu->arch.shared->sprg1 = spr_val;
Alexander Grafa73a9592010-07-29 14:47:47 +0200337 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500338 case SPRN_SPRG2:
Alexander Graf54771e62012-05-04 14:55:12 +0200339 vcpu->arch.shared->sprg2 = spr_val;
Alexander Grafa73a9592010-07-29 14:47:47 +0200340 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500341 case SPRN_SPRG3:
Alexander Graf54771e62012-05-04 14:55:12 +0200342 vcpu->arch.shared->sprg3 = spr_val;
Alexander Grafa73a9592010-07-29 14:47:47 +0200343 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500344
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500345 default:
Alexander Graf54771e62012-05-04 14:55:12 +0200346 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn,
347 spr_val);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600348 if (emulated == EMULATE_FAIL)
Alexander Graf54771e62012-05-04 14:55:12 +0200349 printk(KERN_INFO "mtspr: unknown spr "
350 "0x%x\n", sprn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500351 break;
352 }
Scott Wood49ea0692011-03-28 15:01:24 -0500353 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500354 break;
355
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600356 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 /* Do nothing. The guest is performing dcbi because
358 * hardware DMA is not snooped by the dcache, but
359 * emulated DMA either goes through the dcache as
360 * normal writes, or the host kernel has handled dcache
361 * coherence. */
362 break;
363
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600364 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500365 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
366 break;
367
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600368 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500369 break;
370
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600371 case OP_31_XOP_STWBRX:
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 4, 0);
375 break;
376
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600377 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500378 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
379 break;
380
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600381 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500382 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100383 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500384 2, 0);
385 break;
386
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500387 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600388 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500389 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500390 }
391 break;
392
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600393 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500394 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
395 break;
396
Varun Sethi6c5cb732012-06-18 12:14:55 +0000397 /* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */
398 case OP_LD:
399 rt = get_rt(inst);
400 emulated = kvmppc_handle_load(run, vcpu, rt, 8, 1);
401 break;
402
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600403 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500404 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100405 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500406 break;
407
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600408 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500409 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
410 break;
411
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600412 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500413 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100414 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500415 break;
416
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600417 case OP_STW:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100418 emulated = kvmppc_handle_store(run, vcpu,
419 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500420 4, 1);
421 break;
422
Varun Sethi6c5cb732012-06-18 12:14:55 +0000423 /* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */
424 case OP_STD:
425 rs = get_rs(inst);
426 emulated = kvmppc_handle_store(run, vcpu,
427 kvmppc_get_gpr(vcpu, rs),
428 8, 1);
429 break;
430
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600431 case OP_STWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100432 emulated = kvmppc_handle_store(run, vcpu,
433 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500434 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100435 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500436 break;
437
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600438 case OP_STB:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100439 emulated = kvmppc_handle_store(run, vcpu,
440 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500441 1, 1);
442 break;
443
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600444 case OP_STBU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100445 emulated = kvmppc_handle_store(run, vcpu,
446 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500447 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100448 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500449 break;
450
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600451 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500452 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
453 break;
454
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600455 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500456 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 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
Alexander Graf3587d532010-02-19 11:00:30 +0100460 case OP_LHA:
Alexander Graf3587d532010-02-19 11:00:30 +0100461 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
462 break;
463
464 case OP_LHAU:
Alexander Graf3587d532010-02-19 11:00:30 +0100465 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100466 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf3587d532010-02-19 11:00:30 +0100467 break;
468
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600469 case OP_STH:
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 2, 1);
473 break;
474
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600475 case OP_STHU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100476 emulated = kvmppc_handle_store(run, vcpu,
477 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500478 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100479 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500480 break;
481
482 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500483 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600484 }
485
486 if (emulated == EMULATE_FAIL) {
487 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100488 if (emulated == EMULATE_AGAIN) {
489 advance = 0;
490 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600491 advance = 0;
492 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
493 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100494 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600495 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500496 }
497
Alexander Grafc7f38f42010-04-16 00:11:40 +0200498 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200499
Alexander Grafc7f38f42010-04-16 00:11:40 +0200500 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500501 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200502 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500503
504 return emulated;
505}