blob: 2c52ada3077536c04e145b63d243859f8d32cab0 [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
Alexander Grafa3ff5fb2013-06-27 01:07:15 +0200172 /* PIR can legally be written, but we ignore it */
173 case SPRN_PIR: break;
174
Alexander Graf388cf9e2012-10-06 23:19:01 +0200175 default:
176 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn,
177 spr_val);
178 if (emulated == EMULATE_FAIL)
179 printk(KERN_INFO "mtspr: unknown spr "
180 "0x%x\n", sprn);
181 break;
182 }
183
184 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
185
186 return emulated;
187}
188
189static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
190{
191 enum emulation_result emulated = EMULATE_DONE;
192 ulong spr_val = 0;
193
194 switch (sprn) {
195 case SPRN_SRR0:
196 spr_val = vcpu->arch.shared->srr0;
197 break;
198 case SPRN_SRR1:
199 spr_val = vcpu->arch.shared->srr1;
200 break;
201 case SPRN_PVR:
202 spr_val = vcpu->arch.pvr;
203 break;
204 case SPRN_PIR:
205 spr_val = vcpu->vcpu_id;
206 break;
Alexander Graf388cf9e2012-10-06 23:19:01 +0200207
208 /* Note: mftb and TBRL/TBWL are user-accessible, so
209 * the guest can always access the real TB anyways.
210 * In fact, we probably will never see these traps. */
211 case SPRN_TBWL:
212 spr_val = get_tb() >> 32;
213 break;
214 case SPRN_TBWU:
215 spr_val = get_tb();
216 break;
217
218 case SPRN_SPRG0:
219 spr_val = vcpu->arch.shared->sprg0;
220 break;
221 case SPRN_SPRG1:
222 spr_val = vcpu->arch.shared->sprg1;
223 break;
224 case SPRN_SPRG2:
225 spr_val = vcpu->arch.shared->sprg2;
226 break;
227 case SPRN_SPRG3:
228 spr_val = vcpu->arch.shared->sprg3;
229 break;
230 /* Note: SPRG4-7 are user-readable, so we don't get
231 * a trap. */
232
233 case SPRN_DEC:
234 spr_val = kvmppc_get_dec(vcpu, get_tb());
235 break;
236 default:
237 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn,
238 &spr_val);
239 if (unlikely(emulated == EMULATE_FAIL)) {
240 printk(KERN_INFO "mfspr: unknown spr "
241 "0x%x\n", sprn);
242 }
243 break;
244 }
245
246 if (emulated == EMULATE_DONE)
247 kvmppc_set_gpr(vcpu, rt, spr_val);
248 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
249
250 return emulated;
251}
252
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500253/* XXX to do:
254 * lhax
255 * lhaux
256 * lswx
257 * lswi
258 * stswx
259 * stswi
260 * lha
261 * lhau
262 * lmw
263 * stmw
264 *
265 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
266 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600267/* XXX Should probably auto-generate instruction decoding for a particular core
268 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500269int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
270{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200271 u32 inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200272 int ra = get_ra(inst);
273 int rs = get_rs(inst);
274 int rt = get_rt(inst);
275 int sprn = get_sprn(inst);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500276 enum emulation_result emulated = EMULATE_DONE;
277 int advance = 1;
278
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600279 /* this default type might be overwritten by subcategories */
280 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
281
Joe Perches689fd142010-09-11 19:10:53 +0000282 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000283
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500284 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600285 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200286#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000287 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100288 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800289#else
Scott Woodb5904972011-11-08 18:23:30 -0600290 kvmppc_core_queue_program(vcpu,
291 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800292#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293 advance = 0;
294 break;
295
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500296 case 31:
297 switch (get_xop(inst)) {
298
Alexander Graf6df79df2012-03-13 22:15:45 +0100299 case OP_31_XOP_TRAP:
300#ifdef CONFIG_64BIT
301 case OP_31_XOP_TRAP_64:
302#endif
303#ifdef CONFIG_PPC_BOOK3S
304 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
305#else
306 kvmppc_core_queue_program(vcpu,
307 vcpu->arch.shared->esr | ESR_PTR);
308#endif
309 advance = 0;
310 break;
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600311 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500312 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
313 break;
314
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600315 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500316 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
317 break;
318
Alexander Graf1c85e732010-03-24 21:48:27 +0100319 case OP_31_XOP_LBZUX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100320 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100321 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf1c85e732010-03-24 21:48:27 +0100322 break;
323
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600324 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500325 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100326 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500327 4, 1);
328 break;
329
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600330 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500331 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100332 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500333 1, 1);
334 break;
335
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600336 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500337 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100338 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500339 1, 1);
Alexander Graf978b4fa2012-04-27 01:00:17 +0200340 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500341 break;
342
Alexander Graf1c85e732010-03-24 21:48:27 +0100343 case OP_31_XOP_LHAX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100344 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
345 break;
346
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600347 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500348 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
349 break;
350
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600351 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500352 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100353 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500354 break;
355
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600356 case OP_31_XOP_MFSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200357 emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500358 break;
359
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600360 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500361 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100362 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500363 2, 1);
364 break;
365
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600366 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500367 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100368 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500369 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100370 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500371 break;
372
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600373 case OP_31_XOP_MTSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200374 emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500375 break;
376
Stuart Yoderc32498e2013-04-09 10:36:23 +0000377 case OP_31_XOP_DCBST:
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}