blob: a561d6e8da1c19b8c054967333db709f7595a28b [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>
21#include <linux/timer.h>
22#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"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050032
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -060033#define OP_TRAP 3
34
35#define OP_31_XOP_LWZX 23
36#define OP_31_XOP_LBZX 87
37#define OP_31_XOP_STWX 151
38#define OP_31_XOP_STBX 215
39#define OP_31_XOP_STBUX 247
40#define OP_31_XOP_LHZX 279
41#define OP_31_XOP_LHZUX 311
42#define OP_31_XOP_MFSPR 339
43#define OP_31_XOP_STHX 407
44#define OP_31_XOP_STHUX 439
45#define OP_31_XOP_MTSPR 467
46#define OP_31_XOP_DCBI 470
47#define OP_31_XOP_LWBRX 534
48#define OP_31_XOP_TLBSYNC 566
49#define OP_31_XOP_STWBRX 662
50#define OP_31_XOP_LHBRX 790
51#define OP_31_XOP_STHBRX 918
52
53#define OP_LWZ 32
54#define OP_LWZU 33
55#define OP_LBZ 34
56#define OP_LBZU 35
57#define OP_STW 36
58#define OP_STWU 37
59#define OP_STB 38
60#define OP_STBU 39
61#define OP_LHZ 40
62#define OP_LHZU 41
63#define OP_STH 44
64#define OP_STHU 45
65
Hollis Blanchard75f74f02008-11-05 09:36:16 -060066void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050067{
68 if (vcpu->arch.tcr & TCR_DIE) {
69 /* The decrementer ticks at the same rate as the timebase, so
70 * that's how we convert the guest DEC value to the number of
71 * host ticks. */
72 unsigned long nr_jiffies;
73
74 nr_jiffies = vcpu->arch.dec / tb_ticks_per_jiffy;
75 mod_timer(&vcpu->arch.dec_timer,
76 get_jiffies_64() + nr_jiffies);
77 } else {
78 del_timer(&vcpu->arch.dec_timer);
79 }
80}
81
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050082/* XXX to do:
83 * lhax
84 * lhaux
85 * lswx
86 * lswi
87 * stswx
88 * stswi
89 * lha
90 * lhau
91 * lmw
92 * stmw
93 *
94 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
95 */
Hollis Blanchard75f74f02008-11-05 09:36:16 -060096/* XXX Should probably auto-generate instruction decoding for a particular core
97 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050098int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
99{
100 u32 inst = vcpu->arch.last_inst;
101 u32 ea;
102 int ra;
103 int rb;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500104 int rs;
105 int rt;
106 int sprn;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500107 enum emulation_result emulated = EMULATE_DONE;
108 int advance = 1;
109
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600110 /* this default type might be overwritten by subcategories */
111 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
112
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500113 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600114 case OP_TRAP:
Hollis Blanchardfcfdbd22008-11-05 09:36:24 -0600115 vcpu->arch.esr |= ESR_PTR;
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600116 kvmppc_core_queue_program(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500117 advance = 0;
118 break;
119
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500120 case 31:
121 switch (get_xop(inst)) {
122
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600123 case OP_31_XOP_LWZX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500124 rt = get_rt(inst);
125 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
126 break;
127
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600128 case OP_31_XOP_LBZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500129 rt = get_rt(inst);
130 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
131 break;
132
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600133 case OP_31_XOP_STWX:
Hollis Blanchardac3cd342008-05-21 18:22:52 -0500134 rs = get_rs(inst);
135 emulated = kvmppc_handle_store(run, vcpu,
136 vcpu->arch.gpr[rs],
137 4, 1);
138 break;
139
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600140 case OP_31_XOP_STBX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500141 rs = get_rs(inst);
142 emulated = kvmppc_handle_store(run, vcpu,
143 vcpu->arch.gpr[rs],
144 1, 1);
145 break;
146
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600147 case OP_31_XOP_STBUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500148 rs = get_rs(inst);
149 ra = get_ra(inst);
150 rb = get_rb(inst);
151
152 ea = vcpu->arch.gpr[rb];
153 if (ra)
154 ea += vcpu->arch.gpr[ra];
155
156 emulated = kvmppc_handle_store(run, vcpu,
157 vcpu->arch.gpr[rs],
158 1, 1);
159 vcpu->arch.gpr[rs] = ea;
160 break;
161
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600162 case OP_31_XOP_LHZX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500163 rt = get_rt(inst);
164 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
165 break;
166
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600167 case OP_31_XOP_LHZUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500168 rt = get_rt(inst);
169 ra = get_ra(inst);
170 rb = get_rb(inst);
171
172 ea = vcpu->arch.gpr[rb];
173 if (ra)
174 ea += vcpu->arch.gpr[ra];
175
176 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
177 vcpu->arch.gpr[ra] = ea;
178 break;
179
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600180 case OP_31_XOP_MFSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500181 sprn = get_sprn(inst);
182 rt = get_rt(inst);
183
184 switch (sprn) {
185 case SPRN_SRR0:
186 vcpu->arch.gpr[rt] = vcpu->arch.srr0; break;
187 case SPRN_SRR1:
188 vcpu->arch.gpr[rt] = vcpu->arch.srr1; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500189 case SPRN_PVR:
190 vcpu->arch.gpr[rt] = vcpu->arch.pvr; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500191
192 /* Note: mftb and TBRL/TBWL are user-accessible, so
193 * the guest can always access the real TB anyways.
194 * In fact, we probably will never see these traps. */
195 case SPRN_TBWL:
196 vcpu->arch.gpr[rt] = mftbl(); break;
197 case SPRN_TBWU:
198 vcpu->arch.gpr[rt] = mftbu(); break;
199
200 case SPRN_SPRG0:
201 vcpu->arch.gpr[rt] = vcpu->arch.sprg0; break;
202 case SPRN_SPRG1:
203 vcpu->arch.gpr[rt] = vcpu->arch.sprg1; break;
204 case SPRN_SPRG2:
205 vcpu->arch.gpr[rt] = vcpu->arch.sprg2; break;
206 case SPRN_SPRG3:
207 vcpu->arch.gpr[rt] = vcpu->arch.sprg3; break;
208 /* Note: SPRG4-7 are user-readable, so we don't get
209 * a trap. */
210
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500211 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600212 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
213 if (emulated == EMULATE_FAIL) {
214 printk("mfspr: unknown spr %x\n", sprn);
215 vcpu->arch.gpr[rt] = 0;
216 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500217 break;
218 }
219 break;
220
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600221 case OP_31_XOP_STHX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500222 rs = get_rs(inst);
223 ra = get_ra(inst);
224 rb = get_rb(inst);
225
226 emulated = kvmppc_handle_store(run, vcpu,
227 vcpu->arch.gpr[rs],
228 2, 1);
229 break;
230
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600231 case OP_31_XOP_STHUX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500232 rs = get_rs(inst);
233 ra = get_ra(inst);
234 rb = get_rb(inst);
235
236 ea = vcpu->arch.gpr[rb];
237 if (ra)
238 ea += vcpu->arch.gpr[ra];
239
240 emulated = kvmppc_handle_store(run, vcpu,
241 vcpu->arch.gpr[rs],
242 2, 1);
243 vcpu->arch.gpr[ra] = ea;
244 break;
245
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600246 case OP_31_XOP_MTSPR:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500247 sprn = get_sprn(inst);
248 rs = get_rs(inst);
249 switch (sprn) {
250 case SPRN_SRR0:
251 vcpu->arch.srr0 = vcpu->arch.gpr[rs]; break;
252 case SPRN_SRR1:
253 vcpu->arch.srr1 = vcpu->arch.gpr[rs]; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500254
255 /* XXX We need to context-switch the timebase for
256 * watchdog and FIT. */
257 case SPRN_TBWL: break;
258 case SPRN_TBWU: break;
259
260 case SPRN_DEC:
261 vcpu->arch.dec = vcpu->arch.gpr[rs];
262 kvmppc_emulate_dec(vcpu);
263 break;
264
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500265 case SPRN_SPRG0:
266 vcpu->arch.sprg0 = vcpu->arch.gpr[rs]; break;
267 case SPRN_SPRG1:
268 vcpu->arch.sprg1 = vcpu->arch.gpr[rs]; break;
269 case SPRN_SPRG2:
270 vcpu->arch.sprg2 = vcpu->arch.gpr[rs]; break;
271 case SPRN_SPRG3:
272 vcpu->arch.sprg3 = vcpu->arch.gpr[rs]; break;
273
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500274 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600275 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
276 if (emulated == EMULATE_FAIL)
277 printk("mtspr: unknown spr %x\n", sprn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500278 break;
279 }
280 break;
281
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600282 case OP_31_XOP_DCBI:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500283 /* Do nothing. The guest is performing dcbi because
284 * hardware DMA is not snooped by the dcache, but
285 * emulated DMA either goes through the dcache as
286 * normal writes, or the host kernel has handled dcache
287 * coherence. */
288 break;
289
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600290 case OP_31_XOP_LWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500291 rt = get_rt(inst);
292 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
293 break;
294
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600295 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500296 break;
297
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600298 case OP_31_XOP_STWBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500299 rs = get_rs(inst);
300 ra = get_ra(inst);
301 rb = get_rb(inst);
302
303 emulated = kvmppc_handle_store(run, vcpu,
304 vcpu->arch.gpr[rs],
305 4, 0);
306 break;
307
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600308 case OP_31_XOP_LHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500309 rt = get_rt(inst);
310 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
311 break;
312
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600313 case OP_31_XOP_STHBRX:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500314 rs = get_rs(inst);
315 ra = get_ra(inst);
316 rb = get_rb(inst);
317
318 emulated = kvmppc_handle_store(run, vcpu,
319 vcpu->arch.gpr[rs],
320 2, 0);
321 break;
322
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500323 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600324 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500325 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500326 }
327 break;
328
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600329 case OP_LWZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500330 rt = get_rt(inst);
331 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
332 break;
333
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600334 case OP_LWZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500335 ra = get_ra(inst);
336 rt = get_rt(inst);
337 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
338 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
339 break;
340
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600341 case OP_LBZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500342 rt = get_rt(inst);
343 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
344 break;
345
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600346 case OP_LBZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500347 ra = get_ra(inst);
348 rt = get_rt(inst);
349 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
350 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
351 break;
352
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600353 case OP_STW:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500354 rs = get_rs(inst);
355 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
356 4, 1);
357 break;
358
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600359 case OP_STWU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500360 ra = get_ra(inst);
361 rs = get_rs(inst);
362 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
363 4, 1);
364 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
365 break;
366
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600367 case OP_STB:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500368 rs = get_rs(inst);
369 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
370 1, 1);
371 break;
372
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600373 case OP_STBU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500374 ra = get_ra(inst);
375 rs = get_rs(inst);
376 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
377 1, 1);
378 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
379 break;
380
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600381 case OP_LHZ:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500382 rt = get_rt(inst);
383 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
384 break;
385
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600386 case OP_LHZU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500387 ra = get_ra(inst);
388 rt = get_rt(inst);
389 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
390 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
391 break;
392
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600393 case OP_STH:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500394 rs = get_rs(inst);
395 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
396 2, 1);
397 break;
398
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600399 case OP_STHU:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500400 ra = get_ra(inst);
401 rs = get_rs(inst);
402 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
403 2, 1);
404 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
405 break;
406
407 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500408 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600409 }
410
411 if (emulated == EMULATE_FAIL) {
412 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
413 if (emulated == EMULATE_FAIL) {
414 advance = 0;
415 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
416 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
417 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500418 }
419
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600420 KVMTRACE_3D(PPC_INSTR, vcpu, inst, (int)vcpu->arch.pc, emulated, entryexit);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200421
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500422 if (advance)
423 vcpu->arch.pc += 4; /* Advance past emulated instruction. */
424
425 return emulated;
426}