blob: 7a73b6f72a8ba4a9031d3426c93b32abc03e0951 [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 Bhushandc2babfe2011-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
Alexander Graf388cf9e2012-10-06 23:19:01 +0200153 case SPRN_DEC:
154 vcpu->arch.dec = spr_val;
155 kvmppc_emulate_dec(vcpu);
156 break;
157
158 case SPRN_SPRG0:
159 vcpu->arch.shared->sprg0 = spr_val;
160 break;
161 case SPRN_SPRG1:
162 vcpu->arch.shared->sprg1 = spr_val;
163 break;
164 case SPRN_SPRG2:
165 vcpu->arch.shared->sprg2 = spr_val;
166 break;
167 case SPRN_SPRG3:
168 vcpu->arch.shared->sprg3 = spr_val;
169 break;
170
171 default:
172 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn,
173 spr_val);
174 if (emulated == EMULATE_FAIL)
175 printk(KERN_INFO "mtspr: unknown spr "
176 "0x%x\n", sprn);
177 break;
178 }
179
180 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
181
182 return emulated;
183}
184
185static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
186{
187 enum emulation_result emulated = EMULATE_DONE;
188 ulong spr_val = 0;
189
190 switch (sprn) {
191 case SPRN_SRR0:
192 spr_val = vcpu->arch.shared->srr0;
193 break;
194 case SPRN_SRR1:
195 spr_val = vcpu->arch.shared->srr1;
196 break;
197 case SPRN_PVR:
198 spr_val = vcpu->arch.pvr;
199 break;
200 case SPRN_PIR:
201 spr_val = vcpu->vcpu_id;
202 break;
Alexander Graf388cf9e2012-10-06 23:19:01 +0200203
204 /* Note: mftb and TBRL/TBWL are user-accessible, so
205 * the guest can always access the real TB anyways.
206 * In fact, we probably will never see these traps. */
207 case SPRN_TBWL:
208 spr_val = get_tb() >> 32;
209 break;
210 case SPRN_TBWU:
211 spr_val = get_tb();
212 break;
213
214 case SPRN_SPRG0:
215 spr_val = vcpu->arch.shared->sprg0;
216 break;
217 case SPRN_SPRG1:
218 spr_val = vcpu->arch.shared->sprg1;
219 break;
220 case SPRN_SPRG2:
221 spr_val = vcpu->arch.shared->sprg2;
222 break;
223 case SPRN_SPRG3:
224 spr_val = vcpu->arch.shared->sprg3;
225 break;
226 /* Note: SPRG4-7 are user-readable, so we don't get
227 * a trap. */
228
229 case SPRN_DEC:
230 spr_val = kvmppc_get_dec(vcpu, get_tb());
231 break;
232 default:
233 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn,
234 &spr_val);
235 if (unlikely(emulated == EMULATE_FAIL)) {
236 printk(KERN_INFO "mfspr: unknown spr "
237 "0x%x\n", sprn);
238 }
239 break;
240 }
241
242 if (emulated == EMULATE_DONE)
243 kvmppc_set_gpr(vcpu, rt, spr_val);
244 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
245
246 return emulated;
247}
248
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500249/* XXX to do:
250 * lhax
251 * lhaux
252 * lswx
253 * lswi
254 * stswx
255 * stswi
256 * lha
257 * lhau
258 * lmw
259 * stmw
260 *
261 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
262 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600263/* XXX Should probably auto-generate instruction decoding for a particular core
264 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500265int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
266{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200267 u32 inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200268 int ra = get_ra(inst);
269 int rs = get_rs(inst);
270 int rt = get_rt(inst);
271 int sprn = get_sprn(inst);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500272 enum emulation_result emulated = EMULATE_DONE;
273 int advance = 1;
274
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600275 /* this default type might be overwritten by subcategories */
276 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
277
Joe Perches689fd142010-09-11 19:10:53 +0000278 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000279
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500280 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600281 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200282#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000283 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100284 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800285#else
Scott Woodb5904972011-11-08 18:23:30 -0600286 kvmppc_core_queue_program(vcpu,
287 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800288#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500289 advance = 0;
290 break;
291
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500292 case 31:
293 switch (get_xop(inst)) {
294
Alexander Graf6df79df2012-03-13 22:15:45 +0100295 case OP_31_XOP_TRAP:
296#ifdef CONFIG_64BIT
297 case OP_31_XOP_TRAP_64:
298#endif
299#ifdef CONFIG_PPC_BOOK3S
300 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
301#else
302 kvmppc_core_queue_program(vcpu,
303 vcpu->arch.shared->esr | ESR_PTR);
304#endif
305 advance = 0;
306 break;
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600307 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500308 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
309 break;
310
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600311 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500312 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
313 break;
314
Alexander Graf1c85e732010-03-24 21:48:27 +0100315 case OP_31_XOP_LBZUX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100316 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100317 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf1c85e732010-03-24 21:48:27 +0100318 break;
319
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600320 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500321 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100322 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500323 4, 1);
324 break;
325
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600326 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500327 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100328 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500329 1, 1);
330 break;
331
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600332 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500333 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100334 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500335 1, 1);
Alexander Graf978b4fa2012-04-27 01:00:17 +0200336 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500337 break;
338
Alexander Graf1c85e732010-03-24 21:48:27 +0100339 case OP_31_XOP_LHAX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100340 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
341 break;
342
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600343 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500344 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
345 break;
346
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600347 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500348 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100349 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500350 break;
351
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600352 case OP_31_XOP_MFSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200353 emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500354 break;
355
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600356 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100358 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500359 2, 1);
360 break;
361
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600362 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500363 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100364 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500365 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100366 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500367 break;
368
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600369 case OP_31_XOP_MTSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200370 emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500371 break;
372
Alexander Grafd3286142013-01-17 13:50:25 +0100373 case OP_31_XOP_DCBF:
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600374 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500375 /* Do nothing. The guest is performing dcbi because
376 * hardware DMA is not snooped by the dcache, but
377 * emulated DMA either goes through the dcache as
378 * normal writes, or the host kernel has handled dcache
379 * coherence. */
380 break;
381
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600382 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500383 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
384 break;
385
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600386 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500387 break;
388
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600389 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500390 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100391 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500392 4, 0);
393 break;
394
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600395 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500396 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
397 break;
398
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600399 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500400 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100401 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500402 2, 0);
403 break;
404
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500405 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600406 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500408 }
409 break;
410
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600411 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500412 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
413 break;
414
Varun Sethi6c5cb732012-06-18 12:14:55 +0000415 /* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */
416 case OP_LD:
417 rt = get_rt(inst);
418 emulated = kvmppc_handle_load(run, vcpu, rt, 8, 1);
419 break;
420
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600421 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500422 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100423 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500424 break;
425
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600426 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500427 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
428 break;
429
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600430 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500431 emulated = kvmppc_handle_load(run, vcpu, rt, 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_STW:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100436 emulated = kvmppc_handle_store(run, vcpu,
437 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500438 4, 1);
439 break;
440
Varun Sethi6c5cb732012-06-18 12:14:55 +0000441 /* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */
442 case OP_STD:
443 rs = get_rs(inst);
444 emulated = kvmppc_handle_store(run, vcpu,
445 kvmppc_get_gpr(vcpu, rs),
446 8, 1);
447 break;
448
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600449 case OP_STWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100450 emulated = kvmppc_handle_store(run, vcpu,
451 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500452 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100453 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500454 break;
455
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600456 case OP_STB:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100457 emulated = kvmppc_handle_store(run, vcpu,
458 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500459 1, 1);
460 break;
461
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600462 case OP_STBU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100463 emulated = kvmppc_handle_store(run, vcpu,
464 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500465 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100466 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500467 break;
468
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600469 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500470 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
471 break;
472
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600473 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500474 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100475 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500476 break;
477
Alexander Graf3587d532010-02-19 11:00:30 +0100478 case OP_LHA:
Alexander Graf3587d532010-02-19 11:00:30 +0100479 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
480 break;
481
482 case OP_LHAU:
Alexander Graf3587d532010-02-19 11:00:30 +0100483 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100484 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf3587d532010-02-19 11:00:30 +0100485 break;
486
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600487 case OP_STH:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100488 emulated = kvmppc_handle_store(run, vcpu,
489 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500490 2, 1);
491 break;
492
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600493 case OP_STHU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100494 emulated = kvmppc_handle_store(run, vcpu,
495 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500496 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100497 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500498 break;
499
500 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500501 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600502 }
503
504 if (emulated == EMULATE_FAIL) {
505 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100506 if (emulated == EMULATE_AGAIN) {
507 advance = 0;
508 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600509 advance = 0;
510 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
511 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100512 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600513 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500514 }
515
Alexander Grafc7f38f42010-04-16 00:11:40 +0200516 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200517
Alexander Grafc7f38f42010-04-16 00:11:40 +0200518 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500519 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200520 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500521
522 return emulated;
523}