blob: 631a2650e4e42c0038524b0940a9ae91bcdcd3a4 [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
Stuart Yoderc32498e2013-04-09 10:36:23 +000041#define OP_31_XOP_DCBST 54
Alexander Graf6df79df2012-03-13 22:15:45 +010042#define OP_31_XOP_TRAP_64 68
Alexander Grafd3286142013-01-17 13:50:25 +010043#define OP_31_XOP_DCBF 86
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060044#define OP_31_XOP_LBZX 87
45#define OP_31_XOP_STWX 151
46#define OP_31_XOP_STBX 215
Alexander Graf1c85e732010-03-24 21:48:27 +010047#define OP_31_XOP_LBZUX 119
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060048#define OP_31_XOP_STBUX 247
49#define OP_31_XOP_LHZX 279
50#define OP_31_XOP_LHZUX 311
51#define OP_31_XOP_MFSPR 339
Alexander Graf1c85e732010-03-24 21:48:27 +010052#define OP_31_XOP_LHAX 343
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060053#define OP_31_XOP_STHX 407
54#define OP_31_XOP_STHUX 439
55#define OP_31_XOP_MTSPR 467
56#define OP_31_XOP_DCBI 470
57#define OP_31_XOP_LWBRX 534
58#define OP_31_XOP_TLBSYNC 566
59#define OP_31_XOP_STWBRX 662
60#define OP_31_XOP_LHBRX 790
61#define OP_31_XOP_STHBRX 918
62
63#define OP_LWZ 32
Varun Sethi6c5cb732012-06-18 12:14:55 +000064#define OP_LD 58
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060065#define OP_LWZU 33
66#define OP_LBZ 34
67#define OP_LBZU 35
68#define OP_STW 36
69#define OP_STWU 37
Varun Sethi6c5cb732012-06-18 12:14:55 +000070#define OP_STD 62
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060071#define OP_STB 38
72#define OP_STBU 39
73#define OP_LHZ 40
74#define OP_LHZU 41
Alexander Graf3587d532010-02-19 11:00:30 +010075#define OP_LHA 42
76#define OP_LHAU 43
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060077#define OP_STH 44
78#define OP_STHU 45
79
Hollis Blanchard75f74f02008-11-05 09:36:16 -060080void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050081{
Alexander Graf544c6762009-11-02 12:02:31 +000082 unsigned long dec_nsec;
Bharat Bhushandc2babf2011-10-19 09:46:06 +053083 unsigned long long dec_time;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000084
Alexander Graf544c6762009-11-02 12:02:31 +000085 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Scott Wooddfd4d472011-11-17 12:39:59 +000086 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
87
Alexander Graf00c3a372010-04-16 00:11:42 +020088#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010089 /* mtdec lowers the interrupt line when positive. */
90 kvmppc_core_dequeue_dec(vcpu);
91
Alexander Graf513579e2009-10-30 05:47:16 +000092 /* POWER4+ triggers a dec interrupt if the value is < 0 */
93 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf513579e2009-10-30 05:47:16 +000094 kvmppc_core_queue_dec(vcpu);
95 return;
96 }
97#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050098
Scott Wooddfd4d472011-11-17 12:39:59 +000099#ifdef CONFIG_BOOKE
100 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
101 if (vcpu->arch.dec == 0)
102 return;
103#endif
104
105 /*
106 * The decrementer ticks at the same rate as the timebase, so
107 * that's how we convert the guest DEC value to the number of
108 * host ticks.
109 */
110
111 dec_time = vcpu->arch.dec;
Bharat Bhushan6e359942012-04-18 06:01:19 +0000112 /*
113 * Guest timebase ticks at the same frequency as host decrementer.
114 * So use the host decrementer calculations for decrementer emulation.
115 */
116 dec_time = dec_time << decrementer_clockevent.shift;
117 do_div(dec_time, decrementer_clockevent.mult);
Scott Wooddfd4d472011-11-17 12:39:59 +0000118 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
119 hrtimer_start(&vcpu->arch.dec_timer,
120 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
121 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500122}
123
Scott Wood5ce941e2011-04-27 17:24:21 -0500124u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
125{
126 u64 jd = tb - vcpu->arch.dec_jiffies;
Scott Wooddfd4d472011-11-17 12:39:59 +0000127
128#ifdef CONFIG_BOOKE
129 if (vcpu->arch.dec < jd)
130 return 0;
131#endif
132
Scott Wood5ce941e2011-04-27 17:24:21 -0500133 return vcpu->arch.dec - jd;
134}
135
Alexander Graf388cf9e2012-10-06 23:19:01 +0200136static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
137{
138 enum emulation_result emulated = EMULATE_DONE;
139 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
140
141 switch (sprn) {
142 case SPRN_SRR0:
143 vcpu->arch.shared->srr0 = spr_val;
144 break;
145 case SPRN_SRR1:
146 vcpu->arch.shared->srr1 = spr_val;
147 break;
148
149 /* XXX We need to context-switch the timebase for
150 * watchdog and FIT. */
151 case SPRN_TBWL: break;
152 case SPRN_TBWU: break;
153
Alexander Graf388cf9e2012-10-06 23:19:01 +0200154 case SPRN_DEC:
155 vcpu->arch.dec = spr_val;
156 kvmppc_emulate_dec(vcpu);
157 break;
158
159 case SPRN_SPRG0:
160 vcpu->arch.shared->sprg0 = spr_val;
161 break;
162 case SPRN_SPRG1:
163 vcpu->arch.shared->sprg1 = spr_val;
164 break;
165 case SPRN_SPRG2:
166 vcpu->arch.shared->sprg2 = spr_val;
167 break;
168 case SPRN_SPRG3:
169 vcpu->arch.shared->sprg3 = spr_val;
170 break;
171
172 default:
173 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn,
174 spr_val);
175 if (emulated == EMULATE_FAIL)
176 printk(KERN_INFO "mtspr: unknown spr "
177 "0x%x\n", sprn);
178 break;
179 }
180
181 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
182
183 return emulated;
184}
185
186static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
187{
188 enum emulation_result emulated = EMULATE_DONE;
189 ulong spr_val = 0;
190
191 switch (sprn) {
192 case SPRN_SRR0:
193 spr_val = vcpu->arch.shared->srr0;
194 break;
195 case SPRN_SRR1:
196 spr_val = vcpu->arch.shared->srr1;
197 break;
198 case SPRN_PVR:
199 spr_val = vcpu->arch.pvr;
200 break;
201 case SPRN_PIR:
202 spr_val = vcpu->vcpu_id;
203 break;
Alexander Graf388cf9e2012-10-06 23:19:01 +0200204
205 /* Note: mftb and TBRL/TBWL are user-accessible, so
206 * the guest can always access the real TB anyways.
207 * In fact, we probably will never see these traps. */
208 case SPRN_TBWL:
209 spr_val = get_tb() >> 32;
210 break;
211 case SPRN_TBWU:
212 spr_val = get_tb();
213 break;
214
215 case SPRN_SPRG0:
216 spr_val = vcpu->arch.shared->sprg0;
217 break;
218 case SPRN_SPRG1:
219 spr_val = vcpu->arch.shared->sprg1;
220 break;
221 case SPRN_SPRG2:
222 spr_val = vcpu->arch.shared->sprg2;
223 break;
224 case SPRN_SPRG3:
225 spr_val = vcpu->arch.shared->sprg3;
226 break;
227 /* Note: SPRG4-7 are user-readable, so we don't get
228 * a trap. */
229
230 case SPRN_DEC:
231 spr_val = kvmppc_get_dec(vcpu, get_tb());
232 break;
233 default:
234 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn,
235 &spr_val);
236 if (unlikely(emulated == EMULATE_FAIL)) {
237 printk(KERN_INFO "mfspr: unknown spr "
238 "0x%x\n", sprn);
239 }
240 break;
241 }
242
243 if (emulated == EMULATE_DONE)
244 kvmppc_set_gpr(vcpu, rt, spr_val);
245 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
246
247 return emulated;
248}
249
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500250/* XXX to do:
251 * lhax
252 * lhaux
253 * lswx
254 * lswi
255 * stswx
256 * stswi
257 * lha
258 * lhau
259 * lmw
260 * stmw
261 *
262 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
263 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600264/* XXX Should probably auto-generate instruction decoding for a particular core
265 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500266int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
267{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200268 u32 inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200269 int ra = get_ra(inst);
270 int rs = get_rs(inst);
271 int rt = get_rt(inst);
272 int sprn = get_sprn(inst);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500273 enum emulation_result emulated = EMULATE_DONE;
274 int advance = 1;
275
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600276 /* this default type might be overwritten by subcategories */
277 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
278
Joe Perches689fd142010-09-11 19:10:53 +0000279 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000280
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500281 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600282 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200283#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000284 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100285 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800286#else
Scott Woodb5904972011-11-08 18:23:30 -0600287 kvmppc_core_queue_program(vcpu,
288 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800289#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500290 advance = 0;
291 break;
292
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293 case 31:
294 switch (get_xop(inst)) {
295
Alexander Graf6df79df2012-03-13 22:15:45 +0100296 case OP_31_XOP_TRAP:
297#ifdef CONFIG_64BIT
298 case OP_31_XOP_TRAP_64:
299#endif
300#ifdef CONFIG_PPC_BOOK3S
301 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
302#else
303 kvmppc_core_queue_program(vcpu,
304 vcpu->arch.shared->esr | ESR_PTR);
305#endif
306 advance = 0;
307 break;
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600308 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500309 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
310 break;
311
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600312 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500313 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
314 break;
315
Alexander Graf1c85e732010-03-24 21:48:27 +0100316 case OP_31_XOP_LBZUX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100317 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100318 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf1c85e732010-03-24 21:48:27 +0100319 break;
320
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600321 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500322 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100323 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500324 4, 1);
325 break;
326
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600327 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500328 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100329 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500330 1, 1);
331 break;
332
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600333 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500334 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100335 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500336 1, 1);
Alexander Graf978b4fa2012-04-27 01:00:17 +0200337 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500338 break;
339
Alexander Graf1c85e732010-03-24 21:48:27 +0100340 case OP_31_XOP_LHAX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100341 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
342 break;
343
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600344 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500345 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
346 break;
347
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600348 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500349 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100350 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500351 break;
352
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600353 case OP_31_XOP_MFSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200354 emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500355 break;
356
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600357 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500358 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100359 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500360 2, 1);
361 break;
362
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600363 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500364 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100365 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500366 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100367 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500368 break;
369
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600370 case OP_31_XOP_MTSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200371 emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500372 break;
373
Stuart Yoderc32498e2013-04-09 10:36:23 +0000374 case OP_31_XOP_DCBST:
Alexander Grafd3286142013-01-17 13:50:25 +0100375 case OP_31_XOP_DCBF:
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 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
386 break;
387
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600388 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500389 break;
390
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600391 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500392 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100393 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500394 4, 0);
395 break;
396
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600397 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500398 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
399 break;
400
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600401 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500402 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100403 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500404 2, 0);
405 break;
406
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600408 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500409 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500410 }
411 break;
412
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600413 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500414 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
415 break;
416
Varun Sethi6c5cb732012-06-18 12:14:55 +0000417 /* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */
418 case OP_LD:
419 rt = get_rt(inst);
420 emulated = kvmppc_handle_load(run, vcpu, rt, 8, 1);
421 break;
422
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600423 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500424 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100425 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500426 break;
427
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600428 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500429 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
430 break;
431
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600432 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500433 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100434 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500435 break;
436
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600437 case OP_STW:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100438 emulated = kvmppc_handle_store(run, vcpu,
439 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500440 4, 1);
441 break;
442
Varun Sethi6c5cb732012-06-18 12:14:55 +0000443 /* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */
444 case OP_STD:
445 rs = get_rs(inst);
446 emulated = kvmppc_handle_store(run, vcpu,
447 kvmppc_get_gpr(vcpu, rs),
448 8, 1);
449 break;
450
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600451 case OP_STWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100452 emulated = kvmppc_handle_store(run, vcpu,
453 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500454 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100455 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500456 break;
457
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600458 case OP_STB:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100459 emulated = kvmppc_handle_store(run, vcpu,
460 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500461 1, 1);
462 break;
463
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600464 case OP_STBU:
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);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100468 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500469 break;
470
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600471 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500472 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
473 break;
474
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600475 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500476 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100477 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500478 break;
479
Alexander Graf3587d532010-02-19 11:00:30 +0100480 case OP_LHA:
Alexander Graf3587d532010-02-19 11:00:30 +0100481 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
482 break;
483
484 case OP_LHAU:
Alexander Graf3587d532010-02-19 11:00:30 +0100485 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100486 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf3587d532010-02-19 11:00:30 +0100487 break;
488
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600489 case OP_STH:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100490 emulated = kvmppc_handle_store(run, vcpu,
491 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500492 2, 1);
493 break;
494
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600495 case OP_STHU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100496 emulated = kvmppc_handle_store(run, vcpu,
497 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500498 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100499 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500500 break;
501
502 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500503 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600504 }
505
506 if (emulated == EMULATE_FAIL) {
507 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100508 if (emulated == EMULATE_AGAIN) {
509 advance = 0;
510 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600511 advance = 0;
512 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
513 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100514 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600515 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500516 }
517
Alexander Grafc7f38f42010-04-16 00:11:40 +0200518 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200519
Alexander Grafc7f38f42010-04-16 00:11:40 +0200520 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500521 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200522 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500523
524 return emulated;
525}