blob: b83ba581fd8edb13e4d552ec8b6c9f5c6eb73076 [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;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000087
Alexander Graf544c6762009-11-02 12:02:31 +000088 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Alexander Graf00c3a372010-04-16 00:11:42 +020089#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010090 /* mtdec lowers the interrupt line when positive. */
91 kvmppc_core_dequeue_dec(vcpu);
92
Alexander Graf513579e2009-10-30 05:47:16 +000093 /* POWER4+ triggers a dec interrupt if the value is < 0 */
94 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf544c6762009-11-02 12:02:31 +000095 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
Alexander Graf513579e2009-10-30 05:47:16 +000096 kvmppc_core_queue_dec(vcpu);
97 return;
98 }
99#endif
100 if (kvmppc_dec_enabled(vcpu)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500101 /* The decrementer ticks at the same rate as the timebase, so
102 * that's how we convert the guest DEC value to the number of
103 * host ticks. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500104
Alexander Graf544c6762009-11-02 12:02:31 +0000105 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
106 dec_nsec = vcpu->arch.dec;
107 dec_nsec *= 1000;
108 dec_nsec /= tb_ticks_per_usec;
109 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
110 HRTIMER_MODE_REL);
Alexander Graf513579e2009-10-30 05:47:16 +0000111 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500112 } else {
Alexander Graf544c6762009-11-02 12:02:31 +0000113 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500114 }
115}
116
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500117/* XXX to do:
118 * lhax
119 * lhaux
120 * lswx
121 * lswi
122 * stswx
123 * stswi
124 * lha
125 * lhau
126 * lmw
127 * stmw
128 *
129 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
130 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600131/* XXX Should probably auto-generate instruction decoding for a particular core
132 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500133int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
134{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200135 u32 inst = kvmppc_get_last_inst(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500136 u32 ea;
137 int ra;
138 int rb;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500139 int rs;
140 int rt;
141 int sprn;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500142 enum emulation_result emulated = EMULATE_DONE;
143 int advance = 1;
144
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600145 /* this default type might be overwritten by subcategories */
146 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
147
Joe Perches689fd142010-09-11 19:10:53 +0000148 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000149
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500150 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600151 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200152#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000153 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100154 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800155#else
156 kvmppc_core_queue_program(vcpu, vcpu->arch.esr | ESR_PTR);
157#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500158 advance = 0;
159 break;
160
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500161 case 31:
162 switch (get_xop(inst)) {
163
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600164 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500165 rt = get_rt(inst);
166 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
167 break;
168
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600169 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500170 rt = get_rt(inst);
171 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
172 break;
173
Alexander Graf1c85e732010-03-24 21:48:27 +0100174 case OP_31_XOP_LBZUX:
175 rt = get_rt(inst);
176 ra = get_ra(inst);
177 rb = get_rb(inst);
178
179 ea = kvmppc_get_gpr(vcpu, rb);
180 if (ra)
181 ea += kvmppc_get_gpr(vcpu, ra);
182
183 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
184 kvmppc_set_gpr(vcpu, ra, ea);
185 break;
186
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600187 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500188 rs = get_rs(inst);
189 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100190 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500191 4, 1);
192 break;
193
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600194 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500195 rs = get_rs(inst);
196 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100197 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500198 1, 1);
199 break;
200
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600201 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500202 rs = get_rs(inst);
203 ra = get_ra(inst);
204 rb = get_rb(inst);
205
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100206 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500207 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100208 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500209
210 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 Graf8e5b26b2010-01-08 02:58:01 +0100213 kvmppc_set_gpr(vcpu, rs, ea);
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
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100231 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500232 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100233 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500234
235 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100236 kvmppc_set_gpr(vcpu, ra, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500237 break;
238
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600239 case OP_31_XOP_MFSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500240 sprn = get_sprn(inst);
241 rt = get_rt(inst);
242
243 switch (sprn) {
244 case SPRN_SRR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100245 kvmppc_set_gpr(vcpu, rt, vcpu->arch.srr0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500246 case SPRN_SRR1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100247 kvmppc_set_gpr(vcpu, rt, vcpu->arch.srr1); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500248 case SPRN_PVR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100249 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
Liu Yu06579dd2009-06-05 14:54:31 +0800250 case SPRN_PIR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100251 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
Alexander Graf513579e2009-10-30 05:47:16 +0000252 case SPRN_MSSSR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100253 kvmppc_set_gpr(vcpu, rt, 0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500254
255 /* Note: mftb and TBRL/TBWL are user-accessible, so
256 * the guest can always access the real TB anyways.
257 * In fact, we probably will never see these traps. */
258 case SPRN_TBWL:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100259 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500260 case SPRN_TBWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100261 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500262
263 case SPRN_SPRG0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100264 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500265 case SPRN_SPRG1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100266 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg1); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500267 case SPRN_SPRG2:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100268 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg2); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500269 case SPRN_SPRG3:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100270 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg3); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500271 /* Note: SPRG4-7 are user-readable, so we don't get
272 * a trap. */
273
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000274 case SPRN_DEC:
275 {
Alexander Graf513579e2009-10-30 05:47:16 +0000276 u64 jd = get_tb() - vcpu->arch.dec_jiffies;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100277 kvmppc_set_gpr(vcpu, rt, vcpu->arch.dec - jd);
Joe Perches689fd142010-09-11 19:10:53 +0000278 pr_debug("mfDEC: %x - %llx = %lx\n",
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100279 vcpu->arch.dec, jd,
280 kvmppc_get_gpr(vcpu, rt));
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000281 break;
282 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500283 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600284 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
285 if (emulated == EMULATE_FAIL) {
286 printk("mfspr: unknown spr %x\n", sprn);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100287 kvmppc_set_gpr(vcpu, rt, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600288 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500289 break;
290 }
291 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
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100308 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500309 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100310 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500311
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);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100315 kvmppc_set_gpr(vcpu, ra, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500316 break;
317
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600318 case OP_31_XOP_MTSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500319 sprn = get_sprn(inst);
320 rs = get_rs(inst);
321 switch (sprn) {
322 case SPRN_SRR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100323 vcpu->arch.srr0 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500324 case SPRN_SRR1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100325 vcpu->arch.srr1 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500326
327 /* XXX We need to context-switch the timebase for
328 * watchdog and FIT. */
329 case SPRN_TBWL: break;
330 case SPRN_TBWU: break;
331
Alexander Graf513579e2009-10-30 05:47:16 +0000332 case SPRN_MSSSR0: break;
333
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500334 case SPRN_DEC:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100335 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500336 kvmppc_emulate_dec(vcpu);
337 break;
338
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500339 case SPRN_SPRG0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100340 vcpu->arch.sprg0 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500341 case SPRN_SPRG1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100342 vcpu->arch.sprg1 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500343 case SPRN_SPRG2:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100344 vcpu->arch.sprg2 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500345 case SPRN_SPRG3:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100346 vcpu->arch.sprg3 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500347
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500348 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600349 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
350 if (emulated == EMULATE_FAIL)
351 printk("mtspr: unknown spr %x\n", sprn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500352 break;
353 }
354 break;
355
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600356 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 /* Do nothing. The guest is performing dcbi because
358 * hardware DMA is not snooped by the dcache, but
359 * emulated DMA either goes through the dcache as
360 * normal writes, or the host kernel has handled dcache
361 * coherence. */
362 break;
363
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600364 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500365 rt = get_rt(inst);
366 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
367 break;
368
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600369 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500370 break;
371
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600372 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500373 rs = get_rs(inst);
374 ra = get_ra(inst);
375 rb = get_rb(inst);
376
377 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100378 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500379 4, 0);
380 break;
381
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600382 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500383 rt = get_rt(inst);
384 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
385 break;
386
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600387 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500388 rs = get_rs(inst);
389 ra = get_ra(inst);
390 rb = get_rb(inst);
391
392 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100393 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500394 2, 0);
395 break;
396
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500397 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600398 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500399 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500400 }
401 break;
402
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600403 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500404 rt = get_rt(inst);
405 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
406 break;
407
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600408 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500409 ra = get_ra(inst);
410 rt = get_rt(inst);
411 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100412 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500413 break;
414
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600415 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500416 rt = get_rt(inst);
417 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
418 break;
419
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600420 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500421 ra = get_ra(inst);
422 rt = get_rt(inst);
423 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100424 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500425 break;
426
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600427 case OP_STW:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500428 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100429 emulated = kvmppc_handle_store(run, vcpu,
430 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500431 4, 1);
432 break;
433
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600434 case OP_STWU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500435 ra = get_ra(inst);
436 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100437 emulated = kvmppc_handle_store(run, vcpu,
438 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500439 4, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100440 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500441 break;
442
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600443 case OP_STB:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500444 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100445 emulated = kvmppc_handle_store(run, vcpu,
446 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500447 1, 1);
448 break;
449
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600450 case OP_STBU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500451 ra = get_ra(inst);
452 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100453 emulated = kvmppc_handle_store(run, vcpu,
454 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500455 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100456 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500457 break;
458
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600459 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500460 rt = get_rt(inst);
461 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
462 break;
463
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600464 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500465 ra = get_ra(inst);
466 rt = get_rt(inst);
467 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100468 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500469 break;
470
Alexander Graf3587d532010-02-19 11:00:30 +0100471 case OP_LHA:
472 rt = get_rt(inst);
473 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
474 break;
475
476 case OP_LHAU:
477 ra = get_ra(inst);
478 rt = get_rt(inst);
479 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
480 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
481 break;
482
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600483 case OP_STH:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500484 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100485 emulated = kvmppc_handle_store(run, vcpu,
486 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500487 2, 1);
488 break;
489
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600490 case OP_STHU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500491 ra = get_ra(inst);
492 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100493 emulated = kvmppc_handle_store(run, vcpu,
494 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500495 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100496 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500497 break;
498
499 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500500 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600501 }
502
503 if (emulated == EMULATE_FAIL) {
504 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100505 if (emulated == EMULATE_AGAIN) {
506 advance = 0;
507 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600508 advance = 0;
509 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
510 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100511 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600512 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500513 }
514
Alexander Grafc7f38f42010-04-16 00:11:40 +0200515 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200516
Alexander Grafc7f38f42010-04-16 00:11:40 +0200517 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500518 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200519 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500520
521 return emulated;
522}