blob: 4337f99fa0fa0706b57e8a343af8d04e9e3158b9 [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
16 *
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 */
19
20#include <linux/jiffies.h>
Alexander Graf544c6762009-11-02 12:02:31 +000021#include <linux/hrtimer.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050022#include <linux/types.h>
23#include <linux/string.h>
24#include <linux/kvm_host.h>
25
Hollis Blanchard75f74f02008-11-05 09:36:16 -060026#include <asm/reg.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050027#include <asm/time.h>
28#include <asm/byteorder.h>
29#include <asm/kvm_ppc.h>
Hollis Blanchardc381a042008-11-05 09:36:15 -060030#include <asm/disassemble.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060031#include "timing.h"
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030032#include "trace.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050033
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060034#define OP_TRAP 3
Alexander Graf513579e2009-10-30 05:47:16 +000035#define OP_TRAP_64 2
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060036
37#define OP_31_XOP_LWZX 23
38#define OP_31_XOP_LBZX 87
39#define OP_31_XOP_STWX 151
40#define OP_31_XOP_STBX 215
Alexander Graf1c85e732010-03-24 21:48:27 +010041#define OP_31_XOP_LBZUX 119
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060042#define OP_31_XOP_STBUX 247
43#define OP_31_XOP_LHZX 279
44#define OP_31_XOP_LHZUX 311
45#define OP_31_XOP_MFSPR 339
Alexander Graf1c85e732010-03-24 21:48:27 +010046#define OP_31_XOP_LHAX 343
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060047#define OP_31_XOP_STHX 407
48#define OP_31_XOP_STHUX 439
49#define OP_31_XOP_MTSPR 467
50#define OP_31_XOP_DCBI 470
51#define OP_31_XOP_LWBRX 534
52#define OP_31_XOP_TLBSYNC 566
53#define OP_31_XOP_STWBRX 662
54#define OP_31_XOP_LHBRX 790
55#define OP_31_XOP_STHBRX 918
56
57#define OP_LWZ 32
58#define OP_LWZU 33
59#define OP_LBZ 34
60#define OP_LBZU 35
61#define OP_STW 36
62#define OP_STWU 37
63#define OP_STB 38
64#define OP_STBU 39
65#define OP_LHZ 40
66#define OP_LHZU 41
Alexander Graf3587d532010-02-19 11:00:30 +010067#define OP_LHA 42
68#define OP_LHAU 43
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060069#define OP_STH 44
70#define OP_STHU 45
71
Alexander Graf00c3a372010-04-16 00:11:42 +020072#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +000073static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
74{
75 return 1;
76}
77#else
78static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
79{
80 return vcpu->arch.tcr & TCR_DIE;
81}
82#endif
83
Hollis Blanchard75f74f02008-11-05 09:36:16 -060084void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050085{
Alexander Graf544c6762009-11-02 12:02:31 +000086 unsigned long dec_nsec;
Bharat Bhushandc2babf2011-10-19 09:46:06 +053087 unsigned long long dec_time;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000088
Alexander Graf544c6762009-11-02 12:02:31 +000089 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Alexander Graf00c3a372010-04-16 00:11:42 +020090#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010091 /* mtdec lowers the interrupt line when positive. */
92 kvmppc_core_dequeue_dec(vcpu);
93
Alexander Graf513579e2009-10-30 05:47:16 +000094 /* POWER4+ triggers a dec interrupt if the value is < 0 */
95 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf544c6762009-11-02 12:02:31 +000096 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
Alexander Graf513579e2009-10-30 05:47:16 +000097 kvmppc_core_queue_dec(vcpu);
98 return;
99 }
100#endif
101 if (kvmppc_dec_enabled(vcpu)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500102 /* The decrementer ticks at the same rate as the timebase, so
103 * that's how we convert the guest DEC value to the number of
104 * host ticks. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500105
Alexander Graf544c6762009-11-02 12:02:31 +0000106 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
Bharat Bhushandc2babf2011-10-19 09:46:06 +0530107 dec_time = vcpu->arch.dec;
108 dec_time *= 1000;
109 do_div(dec_time, tb_ticks_per_usec);
110 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
111 hrtimer_start(&vcpu->arch.dec_timer,
112 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
Alexander Graf513579e2009-10-30 05:47:16 +0000113 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500114 } else {
Alexander Graf544c6762009-11-02 12:02:31 +0000115 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500116 }
117}
118
Scott Wood5ce941e2011-04-27 17:24:21 -0500119u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
120{
121 u64 jd = tb - vcpu->arch.dec_jiffies;
122 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 u32 ea;
145 int ra;
146 int rb;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500147 int rs;
148 int rt;
149 int sprn;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500150 enum emulation_result emulated = EMULATE_DONE;
151 int advance = 1;
152
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600153 /* this default type might be overwritten by subcategories */
154 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
155
Joe Perches689fd142010-09-11 19:10:53 +0000156 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000157
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500158 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600159 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200160#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000161 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100162 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800163#else
164 kvmppc_core_queue_program(vcpu, vcpu->arch.esr | ESR_PTR);
165#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
187 ea = kvmppc_get_gpr(vcpu, rb);
188 if (ra)
189 ea += kvmppc_get_gpr(vcpu, ra);
190
191 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
192 kvmppc_set_gpr(vcpu, ra, ea);
193 break;
194
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600195 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500196 rs = get_rs(inst);
197 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100198 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500199 4, 1);
200 break;
201
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600202 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500203 rs = get_rs(inst);
204 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100205 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500206 1, 1);
207 break;
208
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600209 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500210 rs = get_rs(inst);
211 ra = get_ra(inst);
212 rb = get_rb(inst);
213
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100214 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500215 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100216 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500217
218 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100219 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500220 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100221 kvmppc_set_gpr(vcpu, rs, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500222 break;
223
Alexander Graf1c85e732010-03-24 21:48:27 +0100224 case OP_31_XOP_LHAX:
225 rt = get_rt(inst);
226 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
227 break;
228
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600229 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500230 rt = get_rt(inst);
231 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
232 break;
233
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600234 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500235 rt = get_rt(inst);
236 ra = get_ra(inst);
237 rb = get_rb(inst);
238
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100239 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500240 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100241 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500242
243 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100244 kvmppc_set_gpr(vcpu, ra, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500245 break;
246
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600247 case OP_31_XOP_MFSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500248 sprn = get_sprn(inst);
249 rt = get_rt(inst);
250
251 switch (sprn) {
252 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200253 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr0);
254 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500255 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200256 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr1);
257 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500258 case SPRN_PVR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100259 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
Liu Yu06579dd2009-06-05 14:54:31 +0800260 case SPRN_PIR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100261 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
Alexander Graf513579e2009-10-30 05:47:16 +0000262 case SPRN_MSSSR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100263 kvmppc_set_gpr(vcpu, rt, 0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500264
265 /* Note: mftb and TBRL/TBWL are user-accessible, so
266 * the guest can always access the real TB anyways.
267 * In fact, we probably will never see these traps. */
268 case SPRN_TBWL:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100269 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500270 case SPRN_TBWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100271 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500272
273 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200274 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg0);
275 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500276 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200277 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg1);
278 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500279 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200280 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg2);
281 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500282 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200283 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg3);
284 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500285 /* Note: SPRG4-7 are user-readable, so we don't get
286 * a trap. */
287
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000288 case SPRN_DEC:
289 {
Scott Wood5ce941e2011-04-27 17:24:21 -0500290 kvmppc_set_gpr(vcpu, rt,
291 kvmppc_get_dec(vcpu, get_tb()));
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000292 break;
293 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500294 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600295 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
296 if (emulated == EMULATE_FAIL) {
297 printk("mfspr: unknown spr %x\n", sprn);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100298 kvmppc_set_gpr(vcpu, rt, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600299 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500300 break;
301 }
Scott Wood49ea0692011-03-28 15:01:24 -0500302 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500303 break;
304
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600305 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500306 rs = get_rs(inst);
307 ra = get_ra(inst);
308 rb = get_rb(inst);
309
310 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100311 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500312 2, 1);
313 break;
314
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600315 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500316 rs = get_rs(inst);
317 ra = get_ra(inst);
318 rb = get_rb(inst);
319
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100320 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500321 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100322 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500323
324 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 Graf8e5b26b2010-01-08 02:58:01 +0100327 kvmppc_set_gpr(vcpu, ra, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500328 break;
329
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600330 case OP_31_XOP_MTSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500331 sprn = get_sprn(inst);
332 rs = get_rs(inst);
333 switch (sprn) {
334 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200335 vcpu->arch.shared->srr0 = kvmppc_get_gpr(vcpu, rs);
336 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500337 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200338 vcpu->arch.shared->srr1 = kvmppc_get_gpr(vcpu, rs);
339 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500340
341 /* XXX We need to context-switch the timebase for
342 * watchdog and FIT. */
343 case SPRN_TBWL: break;
344 case SPRN_TBWU: break;
345
Alexander Graf513579e2009-10-30 05:47:16 +0000346 case SPRN_MSSSR0: break;
347
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500348 case SPRN_DEC:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100349 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500350 kvmppc_emulate_dec(vcpu);
351 break;
352
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500353 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200354 vcpu->arch.shared->sprg0 = kvmppc_get_gpr(vcpu, rs);
355 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500356 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200357 vcpu->arch.shared->sprg1 = kvmppc_get_gpr(vcpu, rs);
358 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500359 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200360 vcpu->arch.shared->sprg2 = kvmppc_get_gpr(vcpu, rs);
361 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500362 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200363 vcpu->arch.shared->sprg3 = kvmppc_get_gpr(vcpu, rs);
364 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500365
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500366 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600367 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
368 if (emulated == EMULATE_FAIL)
369 printk("mtspr: unknown spr %x\n", sprn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500370 break;
371 }
Scott Wood49ea0692011-03-28 15:01:24 -0500372 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500373 break;
374
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600375 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500376 /* Do nothing. The guest is performing dcbi because
377 * hardware DMA is not snooped by the dcache, but
378 * emulated DMA either goes through the dcache as
379 * normal writes, or the host kernel has handled dcache
380 * coherence. */
381 break;
382
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600383 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500384 rt = get_rt(inst);
385 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
386 break;
387
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600388 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500389 break;
390
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600391 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500392 rs = get_rs(inst);
393 ra = get_ra(inst);
394 rb = get_rb(inst);
395
396 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100397 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500398 4, 0);
399 break;
400
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600401 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500402 rt = get_rt(inst);
403 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
404 break;
405
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600406 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407 rs = get_rs(inst);
408 ra = get_ra(inst);
409 rb = get_rb(inst);
410
411 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100412 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500413 2, 0);
414 break;
415
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500416 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600417 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500418 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500419 }
420 break;
421
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600422 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500423 rt = get_rt(inst);
424 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
425 break;
426
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600427 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500428 ra = get_ra(inst);
429 rt = get_rt(inst);
430 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100431 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500432 break;
433
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600434 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500435 rt = get_rt(inst);
436 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
437 break;
438
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600439 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500440 ra = get_ra(inst);
441 rt = get_rt(inst);
442 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100443 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500444 break;
445
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600446 case OP_STW:
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 4, 1);
451 break;
452
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600453 case OP_STWU:
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 4, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100459 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500460 break;
461
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600462 case OP_STB:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500463 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100464 emulated = kvmppc_handle_store(run, vcpu,
465 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500466 1, 1);
467 break;
468
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600469 case OP_STBU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500470 ra = get_ra(inst);
471 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100472 emulated = kvmppc_handle_store(run, vcpu,
473 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500474 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100475 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500476 break;
477
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600478 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500479 rt = get_rt(inst);
480 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
481 break;
482
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600483 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500484 ra = get_ra(inst);
485 rt = get_rt(inst);
486 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100487 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500488 break;
489
Alexander Graf3587d532010-02-19 11:00:30 +0100490 case OP_LHA:
491 rt = get_rt(inst);
492 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
493 break;
494
495 case OP_LHAU:
496 ra = get_ra(inst);
497 rt = get_rt(inst);
498 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
499 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
500 break;
501
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600502 case OP_STH:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500503 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100504 emulated = kvmppc_handle_store(run, vcpu,
505 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500506 2, 1);
507 break;
508
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600509 case OP_STHU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500510 ra = get_ra(inst);
511 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100512 emulated = kvmppc_handle_store(run, vcpu,
513 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500514 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100515 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500516 break;
517
518 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500519 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600520 }
521
522 if (emulated == EMULATE_FAIL) {
523 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100524 if (emulated == EMULATE_AGAIN) {
525 advance = 0;
526 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600527 advance = 0;
528 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
529 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100530 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600531 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500532 }
533
Alexander Grafc7f38f42010-04-16 00:11:40 +0200534 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200535
Alexander Grafc7f38f42010-04-16 00:11:40 +0200536 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500537 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200538 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500539
540 return emulated;
541}