blob: 11789dd33a132d5defc7cb3f2fc0459f3544ea02 [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
41#define OP_31_XOP_STBUX 247
42#define OP_31_XOP_LHZX 279
43#define OP_31_XOP_LHZUX 311
44#define OP_31_XOP_MFSPR 339
45#define OP_31_XOP_STHX 407
46#define OP_31_XOP_STHUX 439
47#define OP_31_XOP_MTSPR 467
48#define OP_31_XOP_DCBI 470
49#define OP_31_XOP_LWBRX 534
50#define OP_31_XOP_TLBSYNC 566
51#define OP_31_XOP_STWBRX 662
52#define OP_31_XOP_LHBRX 790
53#define OP_31_XOP_STHBRX 918
54
55#define OP_LWZ 32
56#define OP_LWZU 33
57#define OP_LBZ 34
58#define OP_LBZU 35
59#define OP_STW 36
60#define OP_STWU 37
61#define OP_STB 38
62#define OP_STBU 39
63#define OP_LHZ 40
64#define OP_LHZU 41
Alexander Graf3587d532010-02-19 11:00:30 +010065#define OP_LHA 42
66#define OP_LHAU 43
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060067#define OP_STH 44
68#define OP_STHU 45
69
Alexander Graf513579e2009-10-30 05:47:16 +000070#ifdef CONFIG_PPC64
71static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
72{
73 return 1;
74}
75#else
76static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
77{
78 return vcpu->arch.tcr & TCR_DIE;
79}
80#endif
81
Hollis Blanchard75f74f02008-11-05 09:36:16 -060082void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050083{
Alexander Graf544c6762009-11-02 12:02:31 +000084 unsigned long dec_nsec;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000085
Alexander Graf544c6762009-11-02 12:02:31 +000086 pr_debug("mtDEC: %x\n", vcpu->arch.dec);
Alexander Graf513579e2009-10-30 05:47:16 +000087#ifdef CONFIG_PPC64
Alexander Graf7706664d2009-12-21 20:21:24 +010088 /* mtdec lowers the interrupt line when positive. */
89 kvmppc_core_dequeue_dec(vcpu);
90
Alexander Graf513579e2009-10-30 05:47:16 +000091 /* POWER4+ triggers a dec interrupt if the value is < 0 */
92 if (vcpu->arch.dec & 0x80000000) {
Alexander Graf544c6762009-11-02 12:02:31 +000093 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
Alexander Graf513579e2009-10-30 05:47:16 +000094 kvmppc_core_queue_dec(vcpu);
95 return;
96 }
97#endif
98 if (kvmppc_dec_enabled(vcpu)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050099 /* 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. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500102
Alexander Graf544c6762009-11-02 12:02:31 +0000103 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
104 dec_nsec = vcpu->arch.dec;
105 dec_nsec *= 1000;
106 dec_nsec /= tb_ticks_per_usec;
107 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
108 HRTIMER_MODE_REL);
Alexander Graf513579e2009-10-30 05:47:16 +0000109 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500110 } else {
Alexander Graf544c6762009-11-02 12:02:31 +0000111 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500112 }
113}
114
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500115/* XXX to do:
116 * lhax
117 * lhaux
118 * lswx
119 * lswi
120 * stswx
121 * stswi
122 * lha
123 * lhau
124 * lmw
125 * stmw
126 *
127 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
128 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600129/* XXX Should probably auto-generate instruction decoding for a particular core
130 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500131int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
132{
133 u32 inst = vcpu->arch.last_inst;
134 u32 ea;
135 int ra;
136 int rb;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500137 int rs;
138 int rt;
139 int sprn;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500140 enum emulation_result emulated = EMULATE_DONE;
141 int advance = 1;
142
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600143 /* this default type might be overwritten by subcategories */
144 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
145
Alexander Graf513579e2009-10-30 05:47:16 +0000146 pr_debug(KERN_INFO "Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
147
Alexander Grafb4433a72010-01-08 02:58:04 +0100148 /* Try again next time */
149 if (inst == KVM_INST_FETCH_FAILED)
150 return EMULATE_DONE;
151
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500152 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600153 case OP_TRAP:
Alexander Graf513579e2009-10-30 05:47:16 +0000154#ifdef CONFIG_PPC64
155 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100156 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800157#else
158 kvmppc_core_queue_program(vcpu, vcpu->arch.esr | ESR_PTR);
159#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500160 advance = 0;
161 break;
162
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500163 case 31:
164 switch (get_xop(inst)) {
165
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600166 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500167 rt = get_rt(inst);
168 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
169 break;
170
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600171 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500172 rt = get_rt(inst);
173 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
174 break;
175
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600176 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500177 rs = get_rs(inst);
178 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100179 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500180 4, 1);
181 break;
182
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600183 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500184 rs = get_rs(inst);
185 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100186 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500187 1, 1);
188 break;
189
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600190 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500191 rs = get_rs(inst);
192 ra = get_ra(inst);
193 rb = get_rb(inst);
194
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100195 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500196 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100197 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500198
199 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100200 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500201 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100202 kvmppc_set_gpr(vcpu, rs, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500203 break;
204
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600205 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500206 rt = get_rt(inst);
207 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
208 break;
209
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600210 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500211 rt = get_rt(inst);
212 ra = get_ra(inst);
213 rb = get_rb(inst);
214
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100215 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500216 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100217 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500218
219 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100220 kvmppc_set_gpr(vcpu, ra, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500221 break;
222
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600223 case OP_31_XOP_MFSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500224 sprn = get_sprn(inst);
225 rt = get_rt(inst);
226
227 switch (sprn) {
228 case SPRN_SRR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100229 kvmppc_set_gpr(vcpu, rt, vcpu->arch.srr0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500230 case SPRN_SRR1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100231 kvmppc_set_gpr(vcpu, rt, vcpu->arch.srr1); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500232 case SPRN_PVR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100233 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
Liu Yu06579dd2009-06-05 14:54:31 +0800234 case SPRN_PIR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100235 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
Alexander Graf513579e2009-10-30 05:47:16 +0000236 case SPRN_MSSSR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100237 kvmppc_set_gpr(vcpu, rt, 0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500238
239 /* Note: mftb and TBRL/TBWL are user-accessible, so
240 * the guest can always access the real TB anyways.
241 * In fact, we probably will never see these traps. */
242 case SPRN_TBWL:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100243 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500244 case SPRN_TBWU:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100245 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500246
247 case SPRN_SPRG0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100248 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg0); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500249 case SPRN_SPRG1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100250 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg1); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500251 case SPRN_SPRG2:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100252 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg2); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500253 case SPRN_SPRG3:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100254 kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg3); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500255 /* Note: SPRG4-7 are user-readable, so we don't get
256 * a trap. */
257
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000258 case SPRN_DEC:
259 {
Alexander Graf513579e2009-10-30 05:47:16 +0000260 u64 jd = get_tb() - vcpu->arch.dec_jiffies;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100261 kvmppc_set_gpr(vcpu, rt, vcpu->arch.dec - jd);
262 pr_debug(KERN_INFO "mfDEC: %x - %llx = %lx\n",
263 vcpu->arch.dec, jd,
264 kvmppc_get_gpr(vcpu, rt));
Alexander Graf9a7a9b02009-10-30 05:47:15 +0000265 break;
266 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500267 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600268 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
269 if (emulated == EMULATE_FAIL) {
270 printk("mfspr: unknown spr %x\n", sprn);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100271 kvmppc_set_gpr(vcpu, rt, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600272 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500273 break;
274 }
275 break;
276
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600277 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500278 rs = get_rs(inst);
279 ra = get_ra(inst);
280 rb = get_rb(inst);
281
282 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100283 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500284 2, 1);
285 break;
286
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600287 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500288 rs = get_rs(inst);
289 ra = get_ra(inst);
290 rb = get_rb(inst);
291
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100292 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293 if (ra)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100294 ea += kvmppc_get_gpr(vcpu, ra);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500295
296 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100297 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500298 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100299 kvmppc_set_gpr(vcpu, ra, ea);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500300 break;
301
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600302 case OP_31_XOP_MTSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500303 sprn = get_sprn(inst);
304 rs = get_rs(inst);
305 switch (sprn) {
306 case SPRN_SRR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100307 vcpu->arch.srr0 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500308 case SPRN_SRR1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100309 vcpu->arch.srr1 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500310
311 /* XXX We need to context-switch the timebase for
312 * watchdog and FIT. */
313 case SPRN_TBWL: break;
314 case SPRN_TBWU: break;
315
Alexander Graf513579e2009-10-30 05:47:16 +0000316 case SPRN_MSSSR0: break;
317
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500318 case SPRN_DEC:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100319 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500320 kvmppc_emulate_dec(vcpu);
321 break;
322
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500323 case SPRN_SPRG0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100324 vcpu->arch.sprg0 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500325 case SPRN_SPRG1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100326 vcpu->arch.sprg1 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500327 case SPRN_SPRG2:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100328 vcpu->arch.sprg2 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500329 case SPRN_SPRG3:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100330 vcpu->arch.sprg3 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500331
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500332 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600333 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
334 if (emulated == EMULATE_FAIL)
335 printk("mtspr: unknown spr %x\n", sprn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500336 break;
337 }
338 break;
339
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600340 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500341 /* Do nothing. The guest is performing dcbi because
342 * hardware DMA is not snooped by the dcache, but
343 * emulated DMA either goes through the dcache as
344 * normal writes, or the host kernel has handled dcache
345 * coherence. */
346 break;
347
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600348 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500349 rt = get_rt(inst);
350 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
351 break;
352
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600353 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500354 break;
355
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600356 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 rs = get_rs(inst);
358 ra = get_ra(inst);
359 rb = get_rb(inst);
360
361 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100362 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500363 4, 0);
364 break;
365
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600366 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500367 rt = get_rt(inst);
368 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
369 break;
370
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600371 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500372 rs = get_rs(inst);
373 ra = get_ra(inst);
374 rb = get_rb(inst);
375
376 emulated = kvmppc_handle_store(run, vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100377 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500378 2, 0);
379 break;
380
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500381 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600382 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500383 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500384 }
385 break;
386
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600387 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500388 rt = get_rt(inst);
389 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
390 break;
391
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600392 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500393 ra = get_ra(inst);
394 rt = get_rt(inst);
395 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100396 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500397 break;
398
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600399 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500400 rt = get_rt(inst);
401 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
402 break;
403
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600404 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500405 ra = get_ra(inst);
406 rt = get_rt(inst);
407 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100408 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500409 break;
410
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600411 case OP_STW:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500412 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100413 emulated = kvmppc_handle_store(run, vcpu,
414 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500415 4, 1);
416 break;
417
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600418 case OP_STWU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500419 ra = get_ra(inst);
420 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100421 emulated = kvmppc_handle_store(run, vcpu,
422 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500423 4, 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_STB:
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 1, 1);
432 break;
433
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600434 case OP_STBU:
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 1, 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_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500444 rt = get_rt(inst);
445 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
446 break;
447
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600448 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500449 ra = get_ra(inst);
450 rt = get_rt(inst);
451 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100452 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500453 break;
454
Alexander Graf3587d532010-02-19 11:00:30 +0100455 case OP_LHA:
456 rt = get_rt(inst);
457 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
458 break;
459
460 case OP_LHAU:
461 ra = get_ra(inst);
462 rt = get_rt(inst);
463 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
464 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
465 break;
466
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600467 case OP_STH:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500468 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100469 emulated = kvmppc_handle_store(run, vcpu,
470 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500471 2, 1);
472 break;
473
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600474 case OP_STHU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500475 ra = get_ra(inst);
476 rs = get_rs(inst);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100477 emulated = kvmppc_handle_store(run, vcpu,
478 kvmppc_get_gpr(vcpu, rs),
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500479 2, 1);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100480 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500481 break;
482
483 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500484 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600485 }
486
487 if (emulated == EMULATE_FAIL) {
488 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
489 if (emulated == EMULATE_FAIL) {
490 advance = 0;
491 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
492 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Alexander Graf5f2b1052010-01-10 03:27:32 +0100493 kvmppc_core_queue_program(vcpu, 0);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600494 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500495 }
496
Marcelo Tosatti46f43c62009-06-18 11:47:27 -0300497 trace_kvm_ppc_instr(inst, vcpu->arch.pc, emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200498
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500499 if (advance)
500 vcpu->arch.pc += 4; /* Advance past emulated instruction. */
501
502 return emulated;
503}