emulate.c 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Copyright IBM Corp. 2007
 *
 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
 */

#include <linux/jiffies.h>
A
Alexander Graf 已提交
21
#include <linux/hrtimer.h>
22 23 24 25
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kvm_host.h>

26
#include <asm/reg.h>
27 28 29
#include <asm/time.h>
#include <asm/byteorder.h>
#include <asm/kvm_ppc.h>
30
#include <asm/disassemble.h>
31
#include "timing.h"
32
#include "trace.h"
33

34
#define OP_TRAP 3
35
#define OP_TRAP_64 2
36 37 38 39 40

#define OP_31_XOP_LWZX      23
#define OP_31_XOP_LBZX      87
#define OP_31_XOP_STWX      151
#define OP_31_XOP_STBX      215
41
#define OP_31_XOP_LBZUX     119
42 43 44 45
#define OP_31_XOP_STBUX     247
#define OP_31_XOP_LHZX      279
#define OP_31_XOP_LHZUX     311
#define OP_31_XOP_MFSPR     339
46
#define OP_31_XOP_LHAX      343
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#define OP_31_XOP_STHX      407
#define OP_31_XOP_STHUX     439
#define OP_31_XOP_MTSPR     467
#define OP_31_XOP_DCBI      470
#define OP_31_XOP_LWBRX     534
#define OP_31_XOP_TLBSYNC   566
#define OP_31_XOP_STWBRX    662
#define OP_31_XOP_LHBRX     790
#define OP_31_XOP_STHBRX    918

#define OP_LWZ  32
#define OP_LWZU 33
#define OP_LBZ  34
#define OP_LBZU 35
#define OP_STW  36
#define OP_STWU 37
#define OP_STB  38
#define OP_STBU 39
#define OP_LHZ  40
#define OP_LHZU 41
A
Alexander Graf 已提交
67 68
#define OP_LHA  42
#define OP_LHAU 43
69 70 71
#define OP_STH  44
#define OP_STHU 45

72
#ifdef CONFIG_PPC_BOOK3S
73 74 75 76 77 78 79 80 81 82 83
static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
{
	return 1;
}
#else
static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
{
	return vcpu->arch.tcr & TCR_DIE;
}
#endif

84
void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
85
{
A
Alexander Graf 已提交
86
	unsigned long dec_nsec;
A
Alexander Graf 已提交
87

A
Alexander Graf 已提交
88
	pr_debug("mtDEC: %x\n", vcpu->arch.dec);
89
#ifdef CONFIG_PPC_BOOK3S
90 91 92
	/* mtdec lowers the interrupt line when positive. */
	kvmppc_core_dequeue_dec(vcpu);

93 94
	/* POWER4+ triggers a dec interrupt if the value is < 0 */
	if (vcpu->arch.dec & 0x80000000) {
A
Alexander Graf 已提交
95
		hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
96 97 98 99 100
		kvmppc_core_queue_dec(vcpu);
		return;
	}
#endif
	if (kvmppc_dec_enabled(vcpu)) {
101 102 103 104
		/* The decrementer ticks at the same rate as the timebase, so
		 * that's how we convert the guest DEC value to the number of
		 * host ticks. */

A
Alexander Graf 已提交
105 106 107 108 109 110
		hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
		dec_nsec = vcpu->arch.dec;
		dec_nsec *= 1000;
		dec_nsec /= tb_ticks_per_usec;
		hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
			      HRTIMER_MODE_REL);
111
		vcpu->arch.dec_jiffies = get_tb();
112
	} else {
A
Alexander Graf 已提交
113
		hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
114 115 116
	}
}

S
Scott Wood 已提交
117 118 119 120 121 122
u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
{
	u64 jd = tb - vcpu->arch.dec_jiffies;
	return vcpu->arch.dec - jd;
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136
/* XXX to do:
 * lhax
 * lhaux
 * lswx
 * lswi
 * stswx
 * stswi
 * lha
 * lhau
 * lmw
 * stmw
 *
 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
 */
137 138
/* XXX Should probably auto-generate instruction decoding for a particular core
 * from opcode tables in the future. */
139 140
int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
{
141
	u32 inst = kvmppc_get_last_inst(vcpu);
142 143 144 145 146 147 148 149 150
	u32 ea;
	int ra;
	int rb;
	int rs;
	int rt;
	int sprn;
	enum emulation_result emulated = EMULATE_DONE;
	int advance = 1;

151 152 153
	/* this default type might be overwritten by subcategories */
	kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);

154
	pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
155

156
	switch (get_op(inst)) {
157
	case OP_TRAP:
158
#ifdef CONFIG_PPC_BOOK3S
159
	case OP_TRAP_64:
160
		kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
161
#else
162
		kvmppc_core_queue_program(vcpu, vcpu->arch.esr | ESR_PTR);
163
#endif
164 165 166 167 168 169
		advance = 0;
		break;

	case 31:
		switch (get_xop(inst)) {

170
		case OP_31_XOP_LWZX:
171 172 173 174
			rt = get_rt(inst);
			emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
			break;

175
		case OP_31_XOP_LBZX:
176 177 178 179
			rt = get_rt(inst);
			emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
			break;

180 181 182 183 184 185 186 187 188 189 190 191 192
		case OP_31_XOP_LBZUX:
			rt = get_rt(inst);
			ra = get_ra(inst);
			rb = get_rb(inst);

			ea = kvmppc_get_gpr(vcpu, rb);
			if (ra)
				ea += kvmppc_get_gpr(vcpu, ra);

			emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
			kvmppc_set_gpr(vcpu, ra, ea);
			break;

193
		case OP_31_XOP_STWX:
194 195
			rs = get_rs(inst);
			emulated = kvmppc_handle_store(run, vcpu,
196
						       kvmppc_get_gpr(vcpu, rs),
197 198 199
			                               4, 1);
			break;

200
		case OP_31_XOP_STBX:
201 202
			rs = get_rs(inst);
			emulated = kvmppc_handle_store(run, vcpu,
203
						       kvmppc_get_gpr(vcpu, rs),
204 205 206
			                               1, 1);
			break;

207
		case OP_31_XOP_STBUX:
208 209 210 211
			rs = get_rs(inst);
			ra = get_ra(inst);
			rb = get_rb(inst);

212
			ea = kvmppc_get_gpr(vcpu, rb);
213
			if (ra)
214
				ea += kvmppc_get_gpr(vcpu, ra);
215 216

			emulated = kvmppc_handle_store(run, vcpu,
217
						       kvmppc_get_gpr(vcpu, rs),
218
			                               1, 1);
219
			kvmppc_set_gpr(vcpu, rs, ea);
220 221
			break;

222 223 224 225 226
		case OP_31_XOP_LHAX:
			rt = get_rt(inst);
			emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
			break;

227
		case OP_31_XOP_LHZX:
228 229 230 231
			rt = get_rt(inst);
			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
			break;

232
		case OP_31_XOP_LHZUX:
233 234 235 236
			rt = get_rt(inst);
			ra = get_ra(inst);
			rb = get_rb(inst);

237
			ea = kvmppc_get_gpr(vcpu, rb);
238
			if (ra)
239
				ea += kvmppc_get_gpr(vcpu, ra);
240 241

			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
242
			kvmppc_set_gpr(vcpu, ra, ea);
243 244
			break;

245
		case OP_31_XOP_MFSPR:
246 247 248 249 250
			sprn = get_sprn(inst);
			rt = get_rt(inst);

			switch (sprn) {
			case SPRN_SRR0:
251 252
				kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr0);
				break;
253
			case SPRN_SRR1:
254 255
				kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr1);
				break;
256
			case SPRN_PVR:
257
				kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
258
			case SPRN_PIR:
259
				kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
260
			case SPRN_MSSSR0:
261
				kvmppc_set_gpr(vcpu, rt, 0); break;
262 263 264 265 266

			/* Note: mftb and TBRL/TBWL are user-accessible, so
			 * the guest can always access the real TB anyways.
			 * In fact, we probably will never see these traps. */
			case SPRN_TBWL:
267
				kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
268
			case SPRN_TBWU:
269
				kvmppc_set_gpr(vcpu, rt, get_tb()); break;
270 271

			case SPRN_SPRG0:
272 273
				kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg0);
				break;
274
			case SPRN_SPRG1:
275 276
				kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg1);
				break;
277
			case SPRN_SPRG2:
278 279
				kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg2);
				break;
280
			case SPRN_SPRG3:
281 282
				kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg3);
				break;
283 284 285
			/* Note: SPRG4-7 are user-readable, so we don't get
			 * a trap. */

A
Alexander Graf 已提交
286 287
			case SPRN_DEC:
			{
S
Scott Wood 已提交
288 289
				kvmppc_set_gpr(vcpu, rt,
					       kvmppc_get_dec(vcpu, get_tb()));
A
Alexander Graf 已提交
290 291
				break;
			}
292
			default:
293 294 295
				emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
				if (emulated == EMULATE_FAIL) {
					printk("mfspr: unknown spr %x\n", sprn);
296
					kvmppc_set_gpr(vcpu, rt, 0);
297
				}
298 299
				break;
			}
300
			kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
301 302
			break;

303
		case OP_31_XOP_STHX:
304 305 306 307 308
			rs = get_rs(inst);
			ra = get_ra(inst);
			rb = get_rb(inst);

			emulated = kvmppc_handle_store(run, vcpu,
309
						       kvmppc_get_gpr(vcpu, rs),
310 311 312
			                               2, 1);
			break;

313
		case OP_31_XOP_STHUX:
314 315 316 317
			rs = get_rs(inst);
			ra = get_ra(inst);
			rb = get_rb(inst);

318
			ea = kvmppc_get_gpr(vcpu, rb);
319
			if (ra)
320
				ea += kvmppc_get_gpr(vcpu, ra);
321 322

			emulated = kvmppc_handle_store(run, vcpu,
323
						       kvmppc_get_gpr(vcpu, rs),
324
			                               2, 1);
325
			kvmppc_set_gpr(vcpu, ra, ea);
326 327
			break;

328
		case OP_31_XOP_MTSPR:
329 330 331 332
			sprn = get_sprn(inst);
			rs = get_rs(inst);
			switch (sprn) {
			case SPRN_SRR0:
333 334
				vcpu->arch.shared->srr0 = kvmppc_get_gpr(vcpu, rs);
				break;
335
			case SPRN_SRR1:
336 337
				vcpu->arch.shared->srr1 = kvmppc_get_gpr(vcpu, rs);
				break;
338 339 340 341 342 343

			/* XXX We need to context-switch the timebase for
			 * watchdog and FIT. */
			case SPRN_TBWL: break;
			case SPRN_TBWU: break;

344 345
			case SPRN_MSSSR0: break;

346
			case SPRN_DEC:
347
				vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
348 349 350 351
				kvmppc_emulate_dec(vcpu);
				break;

			case SPRN_SPRG0:
352 353
				vcpu->arch.shared->sprg0 = kvmppc_get_gpr(vcpu, rs);
				break;
354
			case SPRN_SPRG1:
355 356
				vcpu->arch.shared->sprg1 = kvmppc_get_gpr(vcpu, rs);
				break;
357
			case SPRN_SPRG2:
358 359
				vcpu->arch.shared->sprg2 = kvmppc_get_gpr(vcpu, rs);
				break;
360
			case SPRN_SPRG3:
361 362
				vcpu->arch.shared->sprg3 = kvmppc_get_gpr(vcpu, rs);
				break;
363 364

			default:
365 366 367
				emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
				if (emulated == EMULATE_FAIL)
					printk("mtspr: unknown spr %x\n", sprn);
368 369
				break;
			}
370
			kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
371 372
			break;

373
		case OP_31_XOP_DCBI:
374 375 376 377 378 379 380
			/* Do nothing. The guest is performing dcbi because
			 * hardware DMA is not snooped by the dcache, but
			 * emulated DMA either goes through the dcache as
			 * normal writes, or the host kernel has handled dcache
			 * coherence. */
			break;

381
		case OP_31_XOP_LWBRX:
382 383 384 385
			rt = get_rt(inst);
			emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
			break;

386
		case OP_31_XOP_TLBSYNC:
387 388
			break;

389
		case OP_31_XOP_STWBRX:
390 391 392 393 394
			rs = get_rs(inst);
			ra = get_ra(inst);
			rb = get_rb(inst);

			emulated = kvmppc_handle_store(run, vcpu,
395
						       kvmppc_get_gpr(vcpu, rs),
396 397 398
			                               4, 0);
			break;

399
		case OP_31_XOP_LHBRX:
400 401 402 403
			rt = get_rt(inst);
			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
			break;

404
		case OP_31_XOP_STHBRX:
405 406 407 408 409
			rs = get_rs(inst);
			ra = get_ra(inst);
			rb = get_rb(inst);

			emulated = kvmppc_handle_store(run, vcpu,
410
						       kvmppc_get_gpr(vcpu, rs),
411 412 413 414
			                               2, 0);
			break;

		default:
415
			/* Attempt core-specific emulation below. */
416 417 418 419
			emulated = EMULATE_FAIL;
		}
		break;

420
	case OP_LWZ:
421 422 423 424
		rt = get_rt(inst);
		emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
		break;

425
	case OP_LWZU:
426 427 428
		ra = get_ra(inst);
		rt = get_rt(inst);
		emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
429
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
430 431
		break;

432
	case OP_LBZ:
433 434 435 436
		rt = get_rt(inst);
		emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
		break;

437
	case OP_LBZU:
438 439 440
		ra = get_ra(inst);
		rt = get_rt(inst);
		emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
441
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
442 443
		break;

444
	case OP_STW:
445
		rs = get_rs(inst);
446 447
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
448 449 450
		                               4, 1);
		break;

451
	case OP_STWU:
452 453
		ra = get_ra(inst);
		rs = get_rs(inst);
454 455
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
456
		                               4, 1);
457
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
458 459
		break;

460
	case OP_STB:
461
		rs = get_rs(inst);
462 463
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
464 465 466
		                               1, 1);
		break;

467
	case OP_STBU:
468 469
		ra = get_ra(inst);
		rs = get_rs(inst);
470 471
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
472
		                               1, 1);
473
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
474 475
		break;

476
	case OP_LHZ:
477 478 479 480
		rt = get_rt(inst);
		emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
		break;

481
	case OP_LHZU:
482 483 484
		ra = get_ra(inst);
		rt = get_rt(inst);
		emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
485
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
486 487
		break;

A
Alexander Graf 已提交
488 489 490 491 492 493 494 495 496 497 498 499
	case OP_LHA:
		rt = get_rt(inst);
		emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
		break;

	case OP_LHAU:
		ra = get_ra(inst);
		rt = get_rt(inst);
		emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
		break;

500
	case OP_STH:
501
		rs = get_rs(inst);
502 503
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
504 505 506
		                               2, 1);
		break;

507
	case OP_STHU:
508 509
		ra = get_ra(inst);
		rs = get_rs(inst);
510 511
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
512
		                               2, 1);
513
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
514 515 516 517
		break;

	default:
		emulated = EMULATE_FAIL;
518 519 520 521
	}

	if (emulated == EMULATE_FAIL) {
		emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
522 523 524
		if (emulated == EMULATE_AGAIN) {
			advance = 0;
		} else if (emulated == EMULATE_FAIL) {
525 526 527
			advance = 0;
			printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
			       "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
528
			kvmppc_core_queue_program(vcpu, 0);
529
		}
530 531
	}

532
	trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
533

534
	/* Advance past emulated instruction. */
535
	if (advance)
536
		kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
537 538 539

	return emulated;
}