blob: bda052e2264b5592b2e4fb25d4858fc102ee25d5 [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{
Bharat Bhushan7401f622011-10-31 14:22:08 +053080 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
81 return (vcpu->arch.tcr & TCR_DIE) && vcpu->arch.dec;
Alexander Graf513579e2009-10-30 05:47:16 +000082}
83#endif
84
Hollis Blanchard75f74f02008-11-05 09:36:16 -060085void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050086{
Alexander Graf544c6762009-11-02 12:02:31 +000087 unsigned long dec_nsec;
Bharat Bhushandc2babfe2011-10-19 09:46:06 +053088 unsigned long long dec_time;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000089
Alexander Graf544c6762009-11-02 12:02:31 +000090 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Alexander Graf00c3a372010-04-16 00:11:42 +020091#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010092 /* mtdec lowers the interrupt line when positive. */
93 kvmppc_core_dequeue_dec(vcpu);
94
Alexander Graf513579e2009-10-30 05:47:16 +000095 /* POWER4+ triggers a dec interrupt if the value is < 0 */
96 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf544c6762009-11-02 12:02:31 +000097 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
Alexander Graf513579e2009-10-30 05:47:16 +000098 kvmppc_core_queue_dec(vcpu);
99 return;
100 }
101#endif
102 if (kvmppc_dec_enabled(vcpu)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500103 /* The decrementer ticks at the same rate as the timebase, so
104 * that's how we convert the guest DEC value to the number of
105 * host ticks. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500106
Alexander Graf544c6762009-11-02 12:02:31 +0000107 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
Bharat Bhushandc2babfe2011-10-19 09:46:06 +0530108 dec_time = vcpu->arch.dec;
109 dec_time *= 1000;
110 do_div(dec_time, tb_ticks_per_usec);
111 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
112 hrtimer_start(&vcpu->arch.dec_timer,
113 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
Alexander Graf513579e2009-10-30 05:47:16 +0000114 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500115 } else {
Alexander Graf544c6762009-11-02 12:02:31 +0000116 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500117 }
118}
119
Scott Wood5ce941e2011-04-27 17:24:21 -0500120u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
121{
122 u64 jd = tb - vcpu->arch.dec_jiffies;
123 return vcpu->arch.dec - jd;
124}
125
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500126/* XXX to do:
127 * lhax
128 * lhaux
129 * lswx
130 * lswi
131 * stswx
132 * stswi
133 * lha
134 * lhau
135 * lmw
136 * stmw
137 *
138 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
139 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600140/* XXX Should probably auto-generate instruction decoding for a particular core
141 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500142int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
143{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200144 u32 inst = kvmppc_get_last_inst(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500145 u32 ea;
146 int ra;
147 int rb;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500148 int rs;
149 int rt;
150 int sprn;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500151 enum emulation_result emulated = EMULATE_DONE;
152 int advance = 1;
153
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600154 /* this default type might be overwritten by subcategories */
155 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
156
Joe Perches689fd142010-09-11 19:10:53 +0000157 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000158
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500159 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600160 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200161#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000162 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100163 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800164#else
Scott Woodb5904972011-11-08 18:23:30 -0600165 kvmppc_core_queue_program(vcpu,
166 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800167#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500168 advance = 0;
169 break;
170
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500171 case 31:
172 switch (get_xop(inst)) {
173
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600174 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500175 rt = get_rt(inst);
176 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
177 break;
178
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600179 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500180 rt = get_rt(inst);
181 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
182 break;
183
Alexander Graf1c85e732010-03-24 21:48:27 +0100184 case OP_31_XOP_LBZUX:
185 rt = get_rt(inst);
186 ra = get_ra(inst);
187 rb = get_rb(inst);
188
189 ea = kvmppc_get_gpr(vcpu, rb);
190 if (ra)
191 ea += kvmppc_get_gpr(vcpu, ra);
192
193 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
194 kvmppc_set_gpr(vcpu, ra, ea);
195 break;
196
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600197 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500198 rs = get_rs(inst);
199 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100200 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500201 4, 1);
202 break;
203
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600204 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500205 rs = get_rs(inst);
206 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100207 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500208 1, 1);
209 break;
210
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600211 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500212 rs = get_rs(inst);
213 ra = get_ra(inst);
214 rb = get_rb(inst);
215
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100216 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500217 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100218 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500219
220 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100221 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500222 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100223 kvmppc_set_gpr(vcpu, rs, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500224 break;
225
Alexander Graf1c85e732010-03-24 21:48:27 +0100226 case OP_31_XOP_LHAX:
227 rt = get_rt(inst);
228 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
229 break;
230
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600231 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500232 rt = get_rt(inst);
233 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
234 break;
235
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600236 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500237 rt = get_rt(inst);
238 ra = get_ra(inst);
239 rb = get_rb(inst);
240
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100241 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500242 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100243 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500244
245 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100246 kvmppc_set_gpr(vcpu, ra, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500247 break;
248
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600249 case OP_31_XOP_MFSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500250 sprn = get_sprn(inst);
251 rt = get_rt(inst);
252
253 switch (sprn) {
254 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200255 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr0);
256 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500257 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200258 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr1);
259 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500260 case SPRN_PVR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100261 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
Liu Yu06579dd2009-06-05 14:54:31 +0800262 case SPRN_PIR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100263 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
Alexander Graf513579e2009-10-30 05:47:16 +0000264 case SPRN_MSSSR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100265 kvmppc_set_gpr(vcpu, rt, 0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500266
267 /* Note: mftb and TBRL/TBWL are user-accessible, so
268 * the guest can always access the real TB anyways.
269 * In fact, we probably will never see these traps. */
270 case SPRN_TBWL:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100271 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500272 case SPRN_TBWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100273 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500274
275 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200276 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg0);
277 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500278 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200279 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg1);
280 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500281 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200282 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg2);
283 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500284 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200285 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg3);
286 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500287 /* Note: SPRG4-7 are user-readable, so we don't get
288 * a trap. */
289
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000290 case SPRN_DEC:
291 {
Scott Wood5ce941e2011-04-27 17:24:21 -0500292 kvmppc_set_gpr(vcpu, rt,
293 kvmppc_get_dec(vcpu, get_tb()));
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000294 break;
295 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500296 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600297 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
298 if (emulated == EMULATE_FAIL) {
299 printk("mfspr: unknown spr %x\n", sprn);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100300 kvmppc_set_gpr(vcpu, rt, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600301 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500302 break;
303 }
Scott Wood49ea0692011-03-28 15:01:24 -0500304 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500305 break;
306
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600307 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500308 rs = get_rs(inst);
309 ra = get_ra(inst);
310 rb = get_rb(inst);
311
312 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100313 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500314 2, 1);
315 break;
316
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600317 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500318 rs = get_rs(inst);
319 ra = get_ra(inst);
320 rb = get_rb(inst);
321
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100322 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500323 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100324 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500325
326 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100327 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500328 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100329 kvmppc_set_gpr(vcpu, ra, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500330 break;
331
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600332 case OP_31_XOP_MTSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500333 sprn = get_sprn(inst);
334 rs = get_rs(inst);
335 switch (sprn) {
336 case SPRN_SRR0:
Alexander Grafde7906c2010-07-29 14:47:46 +0200337 vcpu->arch.shared->srr0 = kvmppc_get_gpr(vcpu, rs);
338 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500339 case SPRN_SRR1:
Alexander Grafde7906c2010-07-29 14:47:46 +0200340 vcpu->arch.shared->srr1 = kvmppc_get_gpr(vcpu, rs);
341 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500342
343 /* XXX We need to context-switch the timebase for
344 * watchdog and FIT. */
345 case SPRN_TBWL: break;
346 case SPRN_TBWU: break;
347
Alexander Graf513579e2009-10-30 05:47:16 +0000348 case SPRN_MSSSR0: break;
349
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500350 case SPRN_DEC:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100351 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500352 kvmppc_emulate_dec(vcpu);
353 break;
354
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500355 case SPRN_SPRG0:
Alexander Grafa73a9592010-07-29 14:47:47 +0200356 vcpu->arch.shared->sprg0 = kvmppc_get_gpr(vcpu, rs);
357 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500358 case SPRN_SPRG1:
Alexander Grafa73a9592010-07-29 14:47:47 +0200359 vcpu->arch.shared->sprg1 = kvmppc_get_gpr(vcpu, rs);
360 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500361 case SPRN_SPRG2:
Alexander Grafa73a9592010-07-29 14:47:47 +0200362 vcpu->arch.shared->sprg2 = kvmppc_get_gpr(vcpu, rs);
363 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500364 case SPRN_SPRG3:
Alexander Grafa73a9592010-07-29 14:47:47 +0200365 vcpu->arch.shared->sprg3 = kvmppc_get_gpr(vcpu, rs);
366 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500367
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500368 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600369 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
370 if (emulated == EMULATE_FAIL)
371 printk("mtspr: unknown spr %x\n", sprn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500372 break;
373 }
Scott Wood49ea0692011-03-28 15:01:24 -0500374 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500375 break;
376
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600377 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500378 /* Do nothing. The guest is performing dcbi because
379 * hardware DMA is not snooped by the dcache, but
380 * emulated DMA either goes through the dcache as
381 * normal writes, or the host kernel has handled dcache
382 * coherence. */
383 break;
384
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600385 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500386 rt = get_rt(inst);
387 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
388 break;
389
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600390 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500391 break;
392
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600393 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500394 rs = get_rs(inst);
395 ra = get_ra(inst);
396 rb = get_rb(inst);
397
398 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100399 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500400 4, 0);
401 break;
402
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600403 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500404 rt = get_rt(inst);
405 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
406 break;
407
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600408 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500409 rs = get_rs(inst);
410 ra = get_ra(inst);
411 rb = get_rb(inst);
412
413 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100414 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500415 2, 0);
416 break;
417
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500418 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600419 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500420 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500421 }
422 break;
423
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600424 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500425 rt = get_rt(inst);
426 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
427 break;
428
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600429 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500430 ra = get_ra(inst);
431 rt = get_rt(inst);
432 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100433 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500434 break;
435
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600436 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500437 rt = get_rt(inst);
438 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
439 break;
440
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600441 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500442 ra = get_ra(inst);
443 rt = get_rt(inst);
444 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100445 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500446 break;
447
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600448 case OP_STW:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500449 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100450 emulated = kvmppc_handle_store(run, vcpu,
451 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500452 4, 1);
453 break;
454
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600455 case OP_STWU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500456 ra = get_ra(inst);
457 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100458 emulated = kvmppc_handle_store(run, vcpu,
459 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500460 4, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100461 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500462 break;
463
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600464 case OP_STB:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500465 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100466 emulated = kvmppc_handle_store(run, vcpu,
467 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500468 1, 1);
469 break;
470
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600471 case OP_STBU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500472 ra = get_ra(inst);
473 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100474 emulated = kvmppc_handle_store(run, vcpu,
475 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500476 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100477 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500478 break;
479
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600480 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500481 rt = get_rt(inst);
482 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
483 break;
484
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600485 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500486 ra = get_ra(inst);
487 rt = get_rt(inst);
488 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100489 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500490 break;
491
Alexander Graf3587d532010-02-19 11:00:30 +0100492 case OP_LHA:
493 rt = get_rt(inst);
494 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
495 break;
496
497 case OP_LHAU:
498 ra = get_ra(inst);
499 rt = get_rt(inst);
500 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
501 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
502 break;
503
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600504 case OP_STH:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500505 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100506 emulated = kvmppc_handle_store(run, vcpu,
507 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500508 2, 1);
509 break;
510
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600511 case OP_STHU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500512 ra = get_ra(inst);
513 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100514 emulated = kvmppc_handle_store(run, vcpu,
515 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500516 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100517 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500518 break;
519
520 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500521 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600522 }
523
524 if (emulated == EMULATE_FAIL) {
525 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100526 if (emulated == EMULATE_AGAIN) {
527 advance = 0;
528 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600529 advance = 0;
530 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
531 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100532 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600533 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500534 }
535
Alexander Grafc7f38f42010-04-16 00:11:40 +0200536 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200537
Alexander Grafc7f38f42010-04-16 00:11:40 +0200538 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500539 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200540 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500541
542 return emulated;
543}