blob: 751cd45f65a0de5fcd23864e5dba7b82b1f8999a [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>
Hongtao Jia9123c5e2013-04-28 13:20:07 +080033#include <asm/ppc-opcode.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060034#include "timing.h"
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030035#include "trace.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050036
Hollis Blanchard75f74f02008-11-05 09:36:16 -060037void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050038{
Alexander Graf544c6762009-11-02 12:02:31 +000039 unsigned long dec_nsec;
Bharat Bhushandc2babfe2011-10-19 09:46:06 +053040 unsigned long long dec_time;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000041
Alexander Graf544c6762009-11-02 12:02:31 +000042 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Scott Wooddfd4d472011-11-17 12:39:59 +000043 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
44
Alexander Graf00c3a372010-04-16 00:11:42 +020045#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010046 /* mtdec lowers the interrupt line when positive. */
47 kvmppc_core_dequeue_dec(vcpu);
48
Alexander Graf513579e2009-10-30 05:47:16 +000049 /* POWER4+ triggers a dec interrupt if the value is < 0 */
50 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf513579e2009-10-30 05:47:16 +000051 kvmppc_core_queue_dec(vcpu);
52 return;
53 }
54#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050055
Scott Wooddfd4d472011-11-17 12:39:59 +000056#ifdef CONFIG_BOOKE
57 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
58 if (vcpu->arch.dec == 0)
59 return;
60#endif
61
62 /*
63 * The decrementer ticks at the same rate as the timebase, so
64 * that's how we convert the guest DEC value to the number of
65 * host ticks.
66 */
67
68 dec_time = vcpu->arch.dec;
Bharat Bhushan6e359942012-04-18 06:01:19 +000069 /*
70 * Guest timebase ticks at the same frequency as host decrementer.
71 * So use the host decrementer calculations for decrementer emulation.
72 */
73 dec_time = dec_time << decrementer_clockevent.shift;
74 do_div(dec_time, decrementer_clockevent.mult);
Scott Wooddfd4d472011-11-17 12:39:59 +000075 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
76 hrtimer_start(&vcpu->arch.dec_timer,
77 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
78 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050079}
80
Scott Wood5ce941e2011-04-27 17:24:21 -050081u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
82{
83 u64 jd = tb - vcpu->arch.dec_jiffies;
Scott Wooddfd4d472011-11-17 12:39:59 +000084
85#ifdef CONFIG_BOOKE
86 if (vcpu->arch.dec < jd)
87 return 0;
88#endif
89
Scott Wood5ce941e2011-04-27 17:24:21 -050090 return vcpu->arch.dec - jd;
91}
92
Alexander Graf388cf9e2012-10-06 23:19:01 +020093static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
94{
95 enum emulation_result emulated = EMULATE_DONE;
96 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
97
98 switch (sprn) {
99 case SPRN_SRR0:
100 vcpu->arch.shared->srr0 = spr_val;
101 break;
102 case SPRN_SRR1:
103 vcpu->arch.shared->srr1 = spr_val;
104 break;
105
106 /* XXX We need to context-switch the timebase for
107 * watchdog and FIT. */
108 case SPRN_TBWL: break;
109 case SPRN_TBWU: break;
110
Alexander Graf388cf9e2012-10-06 23:19:01 +0200111 case SPRN_DEC:
112 vcpu->arch.dec = spr_val;
113 kvmppc_emulate_dec(vcpu);
114 break;
115
116 case SPRN_SPRG0:
117 vcpu->arch.shared->sprg0 = spr_val;
118 break;
119 case SPRN_SPRG1:
120 vcpu->arch.shared->sprg1 = spr_val;
121 break;
122 case SPRN_SPRG2:
123 vcpu->arch.shared->sprg2 = spr_val;
124 break;
125 case SPRN_SPRG3:
126 vcpu->arch.shared->sprg3 = spr_val;
127 break;
128
Alexander Grafa3ff5fb2013-06-27 01:07:15 +0200129 /* PIR can legally be written, but we ignore it */
130 case SPRN_PIR: break;
131
Alexander Graf388cf9e2012-10-06 23:19:01 +0200132 default:
133 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn,
134 spr_val);
135 if (emulated == EMULATE_FAIL)
136 printk(KERN_INFO "mtspr: unknown spr "
137 "0x%x\n", sprn);
138 break;
139 }
140
141 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
142
143 return emulated;
144}
145
146static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
147{
148 enum emulation_result emulated = EMULATE_DONE;
149 ulong spr_val = 0;
150
151 switch (sprn) {
152 case SPRN_SRR0:
153 spr_val = vcpu->arch.shared->srr0;
154 break;
155 case SPRN_SRR1:
156 spr_val = vcpu->arch.shared->srr1;
157 break;
158 case SPRN_PVR:
159 spr_val = vcpu->arch.pvr;
160 break;
161 case SPRN_PIR:
162 spr_val = vcpu->vcpu_id;
163 break;
Alexander Graf388cf9e2012-10-06 23:19:01 +0200164
165 /* Note: mftb and TBRL/TBWL are user-accessible, so
166 * the guest can always access the real TB anyways.
167 * In fact, we probably will never see these traps. */
168 case SPRN_TBWL:
169 spr_val = get_tb() >> 32;
170 break;
171 case SPRN_TBWU:
172 spr_val = get_tb();
173 break;
174
175 case SPRN_SPRG0:
176 spr_val = vcpu->arch.shared->sprg0;
177 break;
178 case SPRN_SPRG1:
179 spr_val = vcpu->arch.shared->sprg1;
180 break;
181 case SPRN_SPRG2:
182 spr_val = vcpu->arch.shared->sprg2;
183 break;
184 case SPRN_SPRG3:
185 spr_val = vcpu->arch.shared->sprg3;
186 break;
187 /* Note: SPRG4-7 are user-readable, so we don't get
188 * a trap. */
189
190 case SPRN_DEC:
191 spr_val = kvmppc_get_dec(vcpu, get_tb());
192 break;
193 default:
194 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn,
195 &spr_val);
196 if (unlikely(emulated == EMULATE_FAIL)) {
197 printk(KERN_INFO "mfspr: unknown spr "
198 "0x%x\n", sprn);
199 }
200 break;
201 }
202
203 if (emulated == EMULATE_DONE)
204 kvmppc_set_gpr(vcpu, rt, spr_val);
205 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
206
207 return emulated;
208}
209
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500210/* XXX to do:
211 * lhax
212 * lhaux
213 * lswx
214 * lswi
215 * stswx
216 * stswi
217 * lha
218 * lhau
219 * lmw
220 * stmw
221 *
222 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
223 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600224/* XXX Should probably auto-generate instruction decoding for a particular core
225 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500226int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
227{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200228 u32 inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200229 int ra = get_ra(inst);
230 int rs = get_rs(inst);
231 int rt = get_rt(inst);
232 int sprn = get_sprn(inst);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500233 enum emulation_result emulated = EMULATE_DONE;
234 int advance = 1;
235
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600236 /* this default type might be overwritten by subcategories */
237 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
238
Joe Perches689fd142010-09-11 19:10:53 +0000239 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000240
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500241 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600242 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200243#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000244 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100245 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800246#else
Scott Woodb5904972011-11-08 18:23:30 -0600247 kvmppc_core_queue_program(vcpu,
248 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800249#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500250 advance = 0;
251 break;
252
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500253 case 31:
254 switch (get_xop(inst)) {
255
Alexander Graf6df79df2012-03-13 22:15:45 +0100256 case OP_31_XOP_TRAP:
257#ifdef CONFIG_64BIT
258 case OP_31_XOP_TRAP_64:
259#endif
260#ifdef CONFIG_PPC_BOOK3S
261 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
262#else
263 kvmppc_core_queue_program(vcpu,
264 vcpu->arch.shared->esr | ESR_PTR);
265#endif
266 advance = 0;
267 break;
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600268 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500269 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
270 break;
271
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600272 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500273 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
274 break;
275
Alexander Graf1c85e732010-03-24 21:48:27 +0100276 case OP_31_XOP_LBZUX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100277 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100278 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf1c85e732010-03-24 21:48:27 +0100279 break;
280
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600281 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500282 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100283 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500284 4, 1);
285 break;
286
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600287 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500288 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100289 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500290 1, 1);
291 break;
292
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600293 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500294 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100295 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500296 1, 1);
Alexander Graf978b4fa2012-04-27 01:00:17 +0200297 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500298 break;
299
Alexander Graf1c85e732010-03-24 21:48:27 +0100300 case OP_31_XOP_LHAX:
Alexander Graf1c85e732010-03-24 21:48:27 +0100301 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
302 break;
303
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600304 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500305 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
306 break;
307
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600308 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500309 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100310 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500311 break;
312
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600313 case OP_31_XOP_MFSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200314 emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500315 break;
316
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600317 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500318 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100319 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500320 2, 1);
321 break;
322
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600323 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500324 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100325 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500326 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100327 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500328 break;
329
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600330 case OP_31_XOP_MTSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200331 emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500332 break;
333
Stuart Yoderc32498e2013-04-09 10:36:23 +0000334 case OP_31_XOP_DCBST:
Alexander Grafd3286142013-01-17 13:50:25 +0100335 case OP_31_XOP_DCBF:
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600336 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500337 /* Do nothing. The guest is performing dcbi because
338 * hardware DMA is not snooped by the dcache, but
339 * emulated DMA either goes through the dcache as
340 * normal writes, or the host kernel has handled dcache
341 * coherence. */
342 break;
343
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600344 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500345 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
346 break;
347
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600348 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500349 break;
350
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600351 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500352 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100353 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500354 4, 0);
355 break;
356
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600357 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500358 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
359 break;
360
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600361 case OP_31_XOP_STHBRX:
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, 0);
365 break;
366
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500367 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600368 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500369 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500370 }
371 break;
372
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600373 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500374 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
375 break;
376
Varun Sethi6c5cb732012-06-18 12:14:55 +0000377 /* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */
378 case OP_LD:
379 rt = get_rt(inst);
380 emulated = kvmppc_handle_load(run, vcpu, rt, 8, 1);
381 break;
382
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600383 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500384 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100385 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500386 break;
387
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600388 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500389 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
390 break;
391
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600392 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500393 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100394 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500395 break;
396
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600397 case OP_STW:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100398 emulated = kvmppc_handle_store(run, vcpu,
399 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500400 4, 1);
401 break;
402
Varun Sethi6c5cb732012-06-18 12:14:55 +0000403 /* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */
404 case OP_STD:
405 rs = get_rs(inst);
406 emulated = kvmppc_handle_store(run, vcpu,
407 kvmppc_get_gpr(vcpu, rs),
408 8, 1);
409 break;
410
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600411 case OP_STWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100412 emulated = kvmppc_handle_store(run, vcpu,
413 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500414 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100415 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500416 break;
417
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600418 case OP_STB:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100419 emulated = kvmppc_handle_store(run, vcpu,
420 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500421 1, 1);
422 break;
423
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600424 case OP_STBU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100425 emulated = kvmppc_handle_store(run, vcpu,
426 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500427 1, 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_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500432 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
433 break;
434
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600435 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500436 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 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
Alexander Graf3587d532010-02-19 11:00:30 +0100440 case OP_LHA:
Alexander Graf3587d532010-02-19 11:00:30 +0100441 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
442 break;
443
444 case OP_LHAU:
Alexander Graf3587d532010-02-19 11:00:30 +0100445 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100446 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf3587d532010-02-19 11:00:30 +0100447 break;
448
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600449 case OP_STH:
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 2, 1);
453 break;
454
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600455 case OP_STHU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100456 emulated = kvmppc_handle_store(run, vcpu,
457 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500458 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100459 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500460 break;
461
462 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500463 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600464 }
465
466 if (emulated == EMULATE_FAIL) {
467 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100468 if (emulated == EMULATE_AGAIN) {
469 advance = 0;
470 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600471 advance = 0;
472 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
473 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100474 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600475 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500476 }
477
Alexander Grafc7f38f42010-04-16 00:11:40 +0200478 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200479
Alexander Grafc7f38f42010-04-16 00:11:40 +0200480 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500481 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200482 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500483
484 return emulated;
485}