blob: 9d9cddc5b346ff9518009539c08804b1cf0f9682 [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
Alexander Grafd3286142013-01-17 13:50:25 +010042#define OP_31_XOP_DCBF 86
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060043#define OP_31_XOP_LBZX 87
44#define OP_31_XOP_STWX 151
45#define OP_31_XOP_STBX 215
Alexander Graf1c85e732010-03-24 21:48:27 +010046#define OP_31_XOP_LBZUX 119
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060047#define OP_31_XOP_STBUX 247
48#define OP_31_XOP_LHZX 279
49#define OP_31_XOP_LHZUX 311
50#define OP_31_XOP_MFSPR 339
Alexander Graf1c85e732010-03-24 21:48:27 +010051#define OP_31_XOP_LHAX 343
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060052#define OP_31_XOP_STHX 407
53#define OP_31_XOP_STHUX 439
54#define OP_31_XOP_MTSPR 467
55#define OP_31_XOP_DCBI 470
56#define OP_31_XOP_LWBRX 534
57#define OP_31_XOP_TLBSYNC 566
58#define OP_31_XOP_STWBRX 662
59#define OP_31_XOP_LHBRX 790
60#define OP_31_XOP_STHBRX 918
61
62#define OP_LWZ 32
Varun Sethi6c5cb732012-06-18 12:14:55 +000063#define OP_LD 58
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060064#define OP_LWZU 33
65#define OP_LBZ 34
66#define OP_LBZU 35
67#define OP_STW 36
68#define OP_STWU 37
Varun Sethi6c5cb732012-06-18 12:14:55 +000069#define OP_STD 62
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060070#define OP_STB 38
71#define OP_STBU 39
72#define OP_LHZ 40
73#define OP_LHZU 41
Alexander Graf3587d532010-02-19 11:00:30 +010074#define OP_LHA 42
75#define OP_LHAU 43
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060076#define OP_STH 44
77#define OP_STHU 45
78
Hollis Blanchard75f74f02008-11-05 09:36:16 -060079void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050080{
Alexander Graf544c6762009-11-02 12:02:31 +000081 unsigned long dec_nsec;
Bharat Bhushandc2babf2011-10-19 09:46:06 +053082 unsigned long long dec_time;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000083
Alexander Graf544c6762009-11-02 12:02:31 +000084 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Scott Wooddfd4d472011-11-17 12:39:59 +000085 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
86
Alexander Graf00c3a372010-04-16 00:11:42 +020087#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010088 /* mtdec lowers the interrupt line when positive. */
89 kvmppc_core_dequeue_dec(vcpu);
90
Alexander Graf513579e2009-10-30 05:47:16 +000091 /* POWER4+ triggers a dec interrupt if the value is < 0 */
92 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf513579e2009-10-30 05:47:16 +000093 kvmppc_core_queue_dec(vcpu);
94 return;
95 }
96#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050097
Scott Wooddfd4d472011-11-17 12:39:59 +000098#ifdef CONFIG_BOOKE
99 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
100 if (vcpu->arch.dec == 0)
101 return;
102#endif
103
104 /*
105 * The decrementer ticks at the same rate as the timebase, so
106 * that's how we convert the guest DEC value to the number of
107 * host ticks.
108 */
109
110 dec_time = vcpu->arch.dec;
Bharat Bhushan6e359942012-04-18 06:01:19 +0000111 /*
112 * Guest timebase ticks at the same frequency as host decrementer.
113 * So use the host decrementer calculations for decrementer emulation.
114 */
115 dec_time = dec_time << decrementer_clockevent.shift;
116 do_div(dec_time, decrementer_clockevent.mult);
Scott Wooddfd4d472011-11-17 12:39:59 +0000117 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
118 hrtimer_start(&vcpu->arch.dec_timer,
119 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
120 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500121}
122
Scott Wood5ce941e2011-04-27 17:24:21 -0500123u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
124{
125 u64 jd = tb - vcpu->arch.dec_jiffies;
Scott Wooddfd4d472011-11-17 12:39:59 +0000126
127#ifdef CONFIG_BOOKE
128 if (vcpu->arch.dec < jd)
129 return 0;
130#endif
131
Scott Wood5ce941e2011-04-27 17:24:21 -0500132 return vcpu->arch.dec - jd;
133}
134
Alexander Graf388cf9e2012-10-06 23:19:01 +0200135static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
136{
137 enum emulation_result emulated = EMULATE_DONE;
138 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
139
140 switch (sprn) {
141 case SPRN_SRR0:
142 vcpu->arch.shared->srr0 = spr_val;
143 break;
144 case SPRN_SRR1:
145 vcpu->arch.shared->srr1 = spr_val;
146 break;
147
148 /* XXX We need to context-switch the timebase for
149 * watchdog and FIT. */
150 case SPRN_TBWL: break;
151 case SPRN_TBWU: break;
152
153 case SPRN_MSSSR0: break;
154
155 case SPRN_DEC:
156 vcpu->arch.dec = spr_val;
157 kvmppc_emulate_dec(vcpu);
158 break;
159
160 case SPRN_SPRG0:
161 vcpu->arch.shared->sprg0 = spr_val;
162 break;
163 case SPRN_SPRG1:
164 vcpu->arch.shared->sprg1 = spr_val;
165 break;
166 case SPRN_SPRG2:
167 vcpu->arch.shared->sprg2 = spr_val;
168 break;
169 case SPRN_SPRG3:
170 vcpu->arch.shared->sprg3 = spr_val;
171 break;
172
173 default:
174 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn,
175 spr_val);
176 if (emulated == EMULATE_FAIL)
177 printk(KERN_INFO "mtspr: unknown spr "
178 "0x%x\n", sprn);
179 break;
180 }
181
182 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
183
184 return emulated;
185}
186
187static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
188{
189 enum emulation_result emulated = EMULATE_DONE;
190 ulong spr_val = 0;
191
192 switch (sprn) {
193 case SPRN_SRR0:
194 spr_val = vcpu->arch.shared->srr0;
195 break;
196 case SPRN_SRR1:
197 spr_val = vcpu->arch.shared->srr1;
198 break;
199 case SPRN_PVR:
200 spr_val = vcpu->arch.pvr;
201 break;
202 case SPRN_PIR:
203 spr_val = vcpu->vcpu_id;
204 break;
205 case SPRN_MSSSR0:
206 spr_val = 0;
207 break;
208
209 /* Note: mftb and TBRL/TBWL are user-accessible, so
210 * the guest can always access the real TB anyways.
211 * In fact, we probably will never see these traps. */
212 case SPRN_TBWL:
213 spr_val = get_tb() >> 32;
214 break;
215 case SPRN_TBWU:
216 spr_val = get_tb();
217 break;
218
219 case SPRN_SPRG0:
220 spr_val = vcpu->arch.shared->sprg0;
221 break;
222 case SPRN_SPRG1:
223 spr_val = vcpu->arch.shared->sprg1;
224 break;
225 case SPRN_SPRG2:
226 spr_val = vcpu->arch.shared->sprg2;
227 break;
228 case SPRN_SPRG3:
229 spr_val = vcpu->arch.shared->sprg3;
230 break;
231 /* Note: SPRG4-7 are user-readable, so we don't get
232 * a trap. */
233
234 case SPRN_DEC:
235 spr_val = kvmppc_get_dec(vcpu, get_tb());
236 break;
237 default:
238 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn,
239 &spr_val);
240 if (unlikely(emulated == EMULATE_FAIL)) {
241 printk(KERN_INFO "mfspr: unknown spr "
242 "0x%x\n", sprn);
243 }
244 break;
245 }
246
247 if (emulated == EMULATE_DONE)
248 kvmppc_set_gpr(vcpu, rt, spr_val);
249 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
250
251 return emulated;
252}
253
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500254/* XXX to do:
255 * lhax
256 * lhaux
257 * lswx
258 * lswi
259 * stswx
260 * stswi
261 * lha
262 * lhau
263 * lmw
264 * stmw
265 *
266 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
267 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600268/* XXX Should probably auto-generate instruction decoding for a particular core
269 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500270int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
271{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200272 u32 inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200273 int ra = get_ra(inst);
274 int rs = get_rs(inst);
275 int rt = get_rt(inst);
276 int sprn = get_sprn(inst);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500277 enum emulation_result emulated = EMULATE_DONE;
278 int advance = 1;
279
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600280 /* this default type might be overwritten by subcategories */
281 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
282
Joe Perches689fd142010-09-11 19:10:53 +0000283 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000284
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500285 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600286 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200287#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000288 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100289 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800290#else
Scott Woodb5904972011-11-08 18:23:30 -0600291 kvmppc_core_queue_program(vcpu,
292 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800293#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500294 advance = 0;
295 break;
296
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500297 case 31:
298 switch (get_xop(inst)) {
299
Alexander Graf6df79df2012-03-13 22:15:45 +0100300 case OP_31_XOP_TRAP:
301#ifdef CONFIG_64BIT
302 case OP_31_XOP_TRAP_64:
303#endif
304#ifdef CONFIG_PPC_BOOK3S
305 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
306#else
307 kvmppc_core_queue_program(vcpu,
308 vcpu->arch.shared->esr | ESR_PTR);
309#endif
310 advance = 0;
311 break;
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600312 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500313 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
314 break;
315
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600316 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500317 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
318 break;
319
Alexander Graf1c85e732010-03-24 21:48:27 +0100320 case OP_31_XOP_LBZUX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100321 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100322 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf1c85e732010-03-24 21:48:27 +0100323 break;
324
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600325 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500326 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100327 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500328 4, 1);
329 break;
330
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600331 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500332 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100333 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500334 1, 1);
335 break;
336
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600337 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500338 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100339 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500340 1, 1);
Alexander Graf978b4fa2012-04-27 01:00:17 +0200341 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500342 break;
343
Alexander Graf1c85e732010-03-24 21:48:27 +0100344 case OP_31_XOP_LHAX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100345 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
346 break;
347
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600348 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500349 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
350 break;
351
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600352 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500353 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100354 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500355 break;
356
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600357 case OP_31_XOP_MFSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200358 emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500359 break;
360
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600361 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500362 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100363 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500364 2, 1);
365 break;
366
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600367 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500368 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100369 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500370 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100371 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500372 break;
373
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600374 case OP_31_XOP_MTSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200375 emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500376 break;
377
Alexander Grafd3286142013-01-17 13:50:25 +0100378 case OP_31_XOP_DCBF:
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600379 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500380 /* Do nothing. The guest is performing dcbi because
381 * hardware DMA is not snooped by the dcache, but
382 * emulated DMA either goes through the dcache as
383 * normal writes, or the host kernel has handled dcache
384 * coherence. */
385 break;
386
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600387 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500388 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
389 break;
390
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600391 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500392 break;
393
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600394 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500395 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100396 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500397 4, 0);
398 break;
399
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600400 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500401 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 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100406 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407 2, 0);
408 break;
409
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500410 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600411 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500412 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500413 }
414 break;
415
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600416 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500417 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
418 break;
419
Varun Sethi6c5cb732012-06-18 12:14:55 +0000420 /* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */
421 case OP_LD:
422 rt = get_rt(inst);
423 emulated = kvmppc_handle_load(run, vcpu, rt, 8, 1);
424 break;
425
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600426 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500427 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100428 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500429 break;
430
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600431 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500432 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
433 break;
434
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600435 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500436 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100437 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500438 break;
439
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600440 case OP_STW:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100441 emulated = kvmppc_handle_store(run, vcpu,
442 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500443 4, 1);
444 break;
445
Varun Sethi6c5cb732012-06-18 12:14:55 +0000446 /* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */
447 case OP_STD:
448 rs = get_rs(inst);
449 emulated = kvmppc_handle_store(run, vcpu,
450 kvmppc_get_gpr(vcpu, rs),
451 8, 1);
452 break;
453
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600454 case OP_STWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100455 emulated = kvmppc_handle_store(run, vcpu,
456 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500457 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100458 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500459 break;
460
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600461 case OP_STB:
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:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100468 emulated = kvmppc_handle_store(run, vcpu,
469 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500470 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100471 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500472 break;
473
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600474 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500475 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
476 break;
477
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600478 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500479 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100480 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500481 break;
482
Alexander Graf3587d532010-02-19 11:00:30 +0100483 case OP_LHA:
Alexander Graf3587d532010-02-19 11:00:30 +0100484 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
485 break;
486
487 case OP_LHAU:
Alexander Graf3587d532010-02-19 11:00:30 +0100488 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100489 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf3587d532010-02-19 11:00:30 +0100490 break;
491
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600492 case OP_STH:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100493 emulated = kvmppc_handle_store(run, vcpu,
494 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500495 2, 1);
496 break;
497
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600498 case OP_STHU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100499 emulated = kvmppc_handle_store(run, vcpu,
500 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500501 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100502 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500503 break;
504
505 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500506 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600507 }
508
509 if (emulated == EMULATE_FAIL) {
510 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100511 if (emulated == EMULATE_AGAIN) {
512 advance = 0;
513 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600514 advance = 0;
515 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
516 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100517 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600518 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500519 }
520
Alexander Grafc7f38f42010-04-16 00:11:40 +0200521 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200522
Alexander Grafc7f38f42010-04-16 00:11:40 +0200523 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500524 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200525 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500526
527 return emulated;
528}