blob: e79a620608ab2ec6881481c01334c07e44f491c5 [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>
26
Hollis Blanchard75f74f02008-11-05 09:36:16 -060027#include <asm/reg.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050028#include <asm/time.h>
29#include <asm/byteorder.h>
30#include <asm/kvm_ppc.h>
Hollis Blanchardc381a042008-11-05 09:36:15 -060031#include <asm/disassemble.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060032#include "timing.h"
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030033#include "trace.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050034
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060035#define OP_TRAP 3
Alexander Graf513579e2009-10-30 05:47:16 +000036#define OP_TRAP_64 2
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060037
38#define OP_31_XOP_LWZX 23
39#define OP_31_XOP_LBZX 87
40#define OP_31_XOP_STWX 151
41#define OP_31_XOP_STBX 215
Alexander Graf1c85e732010-03-24 21:48:27 +010042#define OP_31_XOP_LBZUX 119
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060043#define OP_31_XOP_STBUX 247
44#define OP_31_XOP_LHZX 279
45#define OP_31_XOP_LHZUX 311
46#define OP_31_XOP_MFSPR 339
Alexander Graf1c85e732010-03-24 21:48:27 +010047#define OP_31_XOP_LHAX 343
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060048#define OP_31_XOP_STHX 407
49#define OP_31_XOP_STHUX 439
50#define OP_31_XOP_MTSPR 467
51#define OP_31_XOP_DCBI 470
52#define OP_31_XOP_LWBRX 534
53#define OP_31_XOP_TLBSYNC 566
54#define OP_31_XOP_STWBRX 662
55#define OP_31_XOP_LHBRX 790
56#define OP_31_XOP_STHBRX 918
57
58#define OP_LWZ 32
59#define OP_LWZU 33
60#define OP_LBZ 34
61#define OP_LBZU 35
62#define OP_STW 36
63#define OP_STWU 37
64#define OP_STB 38
65#define OP_STBU 39
66#define OP_LHZ 40
67#define OP_LHZU 41
Alexander Graf3587d532010-02-19 11:00:30 +010068#define OP_LHA 42
69#define OP_LHAU 43
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060070#define OP_STH 44
71#define OP_STHU 45
72
Hollis Blanchard75f74f02008-11-05 09:36:16 -060073void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050074{
Alexander Graf544c6762009-11-02 12:02:31 +000075 unsigned long dec_nsec;
Bharat Bhushandc2babfe2011-10-19 09:46:06 +053076 unsigned long long dec_time;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000077
Alexander Graf544c6762009-11-02 12:02:31 +000078 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Scott Wooddfd4d472011-11-17 12:39:59 +000079 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
80
Alexander Graf00c3a372010-04-16 00:11:42 +020081#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010082 /* mtdec lowers the interrupt line when positive. */
83 kvmppc_core_dequeue_dec(vcpu);
84
Alexander Graf513579e2009-10-30 05:47:16 +000085 /* POWER4+ triggers a dec interrupt if the value is < 0 */
86 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf513579e2009-10-30 05:47:16 +000087 kvmppc_core_queue_dec(vcpu);
88 return;
89 }
90#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050091
Scott Wooddfd4d472011-11-17 12:39:59 +000092#ifdef CONFIG_BOOKE
93 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
94 if (vcpu->arch.dec == 0)
95 return;
96#endif
97
98 /*
99 * The decrementer ticks at the same rate as the timebase, so
100 * that's how we convert the guest DEC value to the number of
101 * host ticks.
102 */
103
104 dec_time = vcpu->arch.dec;
105 dec_time *= 1000;
106 do_div(dec_time, tb_ticks_per_usec);
107 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
108 hrtimer_start(&vcpu->arch.dec_timer,
109 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
110 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500111}
112
Scott Wood5ce941e2011-04-27 17:24:21 -0500113u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
114{
115 u64 jd = tb - vcpu->arch.dec_jiffies;
Scott Wooddfd4d472011-11-17 12:39:59 +0000116
117#ifdef CONFIG_BOOKE
118 if (vcpu->arch.dec < jd)
119 return 0;
120#endif
121
Scott Wood5ce941e2011-04-27 17:24:21 -0500122 return vcpu->arch.dec - jd;
123}
124
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500125/* XXX to do:
126 * lhax
127 * lhaux
128 * lswx
129 * lswi
130 * stswx
131 * stswi
132 * lha
133 * lhau
134 * lmw
135 * stmw
136 *
137 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
138 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600139/* XXX Should probably auto-generate instruction decoding for a particular core
140 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500141int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
142{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200143 u32 inst = kvmppc_get_last_inst(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500144 int ra;
145 int rb;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500146 int rs;
147 int rt;
148 int sprn;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500149 enum emulation_result emulated = EMULATE_DONE;
150 int advance = 1;
151
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600152 /* this default type might be overwritten by subcategories */
153 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
154
Joe Perches689fd142010-09-11 19:10:53 +0000155 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000156
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500157 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600158 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200159#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000160 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100161 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800162#else
Scott Woodb5904972011-11-08 18:23:30 -0600163 kvmppc_core_queue_program(vcpu,
164 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800165#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500166 advance = 0;
167 break;
168
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500169 case 31:
170 switch (get_xop(inst)) {
171
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600172 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500173 rt = get_rt(inst);
174 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
175 break;
176
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600177 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500178 rt = get_rt(inst);
179 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
180 break;
181
Alexander Graf1c85e732010-03-24 21:48:27 +0100182 case OP_31_XOP_LBZUX:
183 rt = get_rt(inst);
184 ra = get_ra(inst);
185 rb = get_rb(inst);
186
Alexander Graf1c85e732010-03-24 21:48:27 +0100187 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100188 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf1c85e732010-03-24 21:48:27 +0100189 break;
190
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600191 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500192 rs = get_rs(inst);
193 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100194 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500195 4, 1);
196 break;
197
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600198 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500199 rs = get_rs(inst);
200 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100201 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500202 1, 1);
203 break;
204
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600205 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500206 rs = get_rs(inst);
207 ra = get_ra(inst);
208 rb = get_rb(inst);
209
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500210 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100211 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500212 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100213 kvmppc_set_gpr(vcpu, rs, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500214 break;
215
Alexander Graf1c85e732010-03-24 21:48:27 +0100216 case OP_31_XOP_LHAX:
217 rt = get_rt(inst);
218 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
219 break;
220
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600221 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500222 rt = get_rt(inst);
223 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
224 break;
225
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600226 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500227 rt = get_rt(inst);
228 ra = get_ra(inst);
229 rb = get_rb(inst);
230
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500231 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100232 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500233 break;
234
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600235 case OP_31_XOP_MFSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500236 sprn = get_sprn(inst);
237 rt = get_rt(inst);
238
239 switch (sprn) {
240 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200241 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr0);
242 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500243 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200244 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr1);
245 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500246 case SPRN_PVR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100247 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
Liu Yu06579dd2009-06-05 14:54:31 +0800248 case SPRN_PIR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100249 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
Alexander Graf513579e2009-10-30 05:47:16 +0000250 case SPRN_MSSSR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100251 kvmppc_set_gpr(vcpu, rt, 0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500252
253 /* Note: mftb and TBRL/TBWL are user-accessible, so
254 * the guest can always access the real TB anyways.
255 * In fact, we probably will never see these traps. */
256 case SPRN_TBWL:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100257 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500258 case SPRN_TBWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100259 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500260
261 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200262 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg0);
263 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500264 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200265 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg1);
266 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500267 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200268 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg2);
269 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500270 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200271 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg3);
272 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500273 /* Note: SPRG4-7 are user-readable, so we don't get
274 * a trap. */
275
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000276 case SPRN_DEC:
277 {
Scott Wood5ce941e2011-04-27 17:24:21 -0500278 kvmppc_set_gpr(vcpu, rt,
279 kvmppc_get_dec(vcpu, get_tb()));
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000280 break;
281 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500282 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600283 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
284 if (emulated == EMULATE_FAIL) {
285 printk("mfspr: unknown spr %x\n", sprn);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100286 kvmppc_set_gpr(vcpu, rt, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600287 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500288 break;
289 }
Scott Wood49ea0692011-03-28 15:01:24 -0500290 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500291 break;
292
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600293 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500294 rs = get_rs(inst);
295 ra = get_ra(inst);
296 rb = get_rb(inst);
297
298 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100299 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500300 2, 1);
301 break;
302
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600303 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500304 rs = get_rs(inst);
305 ra = get_ra(inst);
306 rb = get_rb(inst);
307
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500308 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100309 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500310 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100311 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500312 break;
313
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600314 case OP_31_XOP_MTSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500315 sprn = get_sprn(inst);
316 rs = get_rs(inst);
317 switch (sprn) {
318 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200319 vcpu->arch.shared->srr0 = kvmppc_get_gpr(vcpu, rs);
320 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500321 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200322 vcpu->arch.shared->srr1 = kvmppc_get_gpr(vcpu, rs);
323 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500324
325 /* XXX We need to context-switch the timebase for
326 * watchdog and FIT. */
327 case SPRN_TBWL: break;
328 case SPRN_TBWU: break;
329
Alexander Graf513579e2009-10-30 05:47:16 +0000330 case SPRN_MSSSR0: break;
331
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500332 case SPRN_DEC:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100333 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500334 kvmppc_emulate_dec(vcpu);
335 break;
336
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500337 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200338 vcpu->arch.shared->sprg0 = kvmppc_get_gpr(vcpu, rs);
339 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500340 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200341 vcpu->arch.shared->sprg1 = kvmppc_get_gpr(vcpu, rs);
342 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500343 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200344 vcpu->arch.shared->sprg2 = kvmppc_get_gpr(vcpu, rs);
345 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500346 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200347 vcpu->arch.shared->sprg3 = kvmppc_get_gpr(vcpu, rs);
348 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500349
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500350 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600351 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
352 if (emulated == EMULATE_FAIL)
353 printk("mtspr: unknown spr %x\n", sprn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500354 break;
355 }
Scott Wood49ea0692011-03-28 15:01:24 -0500356 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 break;
358
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600359 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500360 /* Do nothing. The guest is performing dcbi because
361 * hardware DMA is not snooped by the dcache, but
362 * emulated DMA either goes through the dcache as
363 * normal writes, or the host kernel has handled dcache
364 * coherence. */
365 break;
366
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600367 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500368 rt = get_rt(inst);
369 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
370 break;
371
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600372 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500373 break;
374
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600375 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500376 rs = get_rs(inst);
377 ra = get_ra(inst);
378 rb = get_rb(inst);
379
380 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100381 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500382 4, 0);
383 break;
384
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600385 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500386 rt = get_rt(inst);
387 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
388 break;
389
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600390 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500391 rs = get_rs(inst);
392 ra = get_ra(inst);
393 rb = get_rb(inst);
394
395 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 2, 0);
398 break;
399
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500400 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600401 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500402 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500403 }
404 break;
405
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600406 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407 rt = get_rt(inst);
408 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
409 break;
410
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600411 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500412 ra = get_ra(inst);
413 rt = get_rt(inst);
414 emulated = kvmppc_handle_load(run, vcpu, rt, 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_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500419 rt = get_rt(inst);
420 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
421 break;
422
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600423 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500424 ra = get_ra(inst);
425 rt = get_rt(inst);
426 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100427 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500428 break;
429
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600430 case OP_STW:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500431 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100432 emulated = kvmppc_handle_store(run, vcpu,
433 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500434 4, 1);
435 break;
436
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600437 case OP_STWU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500438 ra = get_ra(inst);
439 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100440 emulated = kvmppc_handle_store(run, vcpu,
441 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500442 4, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100443 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500444 break;
445
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600446 case OP_STB:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500447 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100448 emulated = kvmppc_handle_store(run, vcpu,
449 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500450 1, 1);
451 break;
452
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600453 case OP_STBU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500454 ra = get_ra(inst);
455 rs = get_rs(inst);
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 1, 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
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600462 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500463 rt = get_rt(inst);
464 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
465 break;
466
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600467 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500468 ra = get_ra(inst);
469 rt = get_rt(inst);
470 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 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
Alexander Graf3587d532010-02-19 11:00:30 +0100474 case OP_LHA:
475 rt = get_rt(inst);
476 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
477 break;
478
479 case OP_LHAU:
480 ra = get_ra(inst);
481 rt = get_rt(inst);
482 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
Alexander Graf6020c0f2012-03-12 02:26:30 +0100483 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
Alexander Graf3587d532010-02-19 11:00:30 +0100484 break;
485
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600486 case OP_STH:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500487 rs = get_rs(inst);
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:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500494 ra = get_ra(inst);
495 rs = get_rs(inst);
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}