emulate.c 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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
16
 * Copyright 2011 Freescale Semiconductor, Inc.
17 18 19 20 21
 *
 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
 */

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

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

36
#define OP_TRAP 3
37
#define OP_TRAP_64 2
38

39
#define OP_31_XOP_TRAP      4
40
#define OP_31_XOP_LWZX      23
41
#define OP_31_XOP_TRAP_64   68
A
Alexander Graf 已提交
42
#define OP_31_XOP_DCBF      86
43 44 45
#define OP_31_XOP_LBZX      87
#define OP_31_XOP_STWX      151
#define OP_31_XOP_STBX      215
46
#define OP_31_XOP_LBZUX     119
47 48 49 50
#define OP_31_XOP_STBUX     247
#define OP_31_XOP_LHZX      279
#define OP_31_XOP_LHZUX     311
#define OP_31_XOP_MFSPR     339
51
#define OP_31_XOP_LHAX      343
52 53 54 55 56 57 58 59 60 61 62
#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
63
#define OP_LD   58
64 65 66 67 68
#define OP_LWZU 33
#define OP_LBZ  34
#define OP_LBZU 35
#define OP_STW  36
#define OP_STWU 37
69
#define OP_STD  62
70 71 72 73
#define OP_STB  38
#define OP_STBU 39
#define OP_LHZ  40
#define OP_LHZU 41
A
Alexander Graf 已提交
74 75
#define OP_LHA  42
#define OP_LHAU 43
76 77 78
#define OP_STH  44
#define OP_STHU 45

79
void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
80
{
A
Alexander Graf 已提交
81
	unsigned long dec_nsec;
82
	unsigned long long dec_time;
A
Alexander Graf 已提交
83

A
Alexander Graf 已提交
84
	pr_debug("mtDEC: %x\n", vcpu->arch.dec);
85 86
	hrtimer_try_to_cancel(&vcpu->arch.dec_timer);

87
#ifdef CONFIG_PPC_BOOK3S
88 89 90
	/* mtdec lowers the interrupt line when positive. */
	kvmppc_core_dequeue_dec(vcpu);

91 92 93 94 95 96
	/* POWER4+ triggers a dec interrupt if the value is < 0 */
	if (vcpu->arch.dec & 0x80000000) {
		kvmppc_core_queue_dec(vcpu);
		return;
	}
#endif
97 98 99 100 101 102 103 104 105 106 107 108 109 110

#ifdef CONFIG_BOOKE
	/* On BOOKE, DEC = 0 is as good as decrementer not enabled */
	if (vcpu->arch.dec == 0)
		return;
#endif

	/*
	 * 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.
	 */

	dec_time = vcpu->arch.dec;
111 112 113 114 115 116
	/*
	 * Guest timebase ticks at the same frequency as host decrementer.
	 * So use the host decrementer calculations for decrementer emulation.
	 */
	dec_time = dec_time << decrementer_clockevent.shift;
	do_div(dec_time, decrementer_clockevent.mult);
117 118 119 120
	dec_nsec = do_div(dec_time, NSEC_PER_SEC);
	hrtimer_start(&vcpu->arch.dec_timer,
		ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
	vcpu->arch.dec_jiffies = get_tb();
121 122
}

S
Scott Wood 已提交
123 124 125
u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
{
	u64 jd = tb - vcpu->arch.dec_jiffies;
126 127 128 129 130 131

#ifdef CONFIG_BOOKE
	if (vcpu->arch.dec < jd)
		return 0;
#endif

S
Scott Wood 已提交
132 133 134
	return vcpu->arch.dec - jd;
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
{
	enum emulation_result emulated = EMULATE_DONE;
	ulong spr_val = kvmppc_get_gpr(vcpu, rs);

	switch (sprn) {
	case SPRN_SRR0:
		vcpu->arch.shared->srr0 = spr_val;
		break;
	case SPRN_SRR1:
		vcpu->arch.shared->srr1 = spr_val;
		break;

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

	case SPRN_DEC:
		vcpu->arch.dec = spr_val;
		kvmppc_emulate_dec(vcpu);
		break;

	case SPRN_SPRG0:
		vcpu->arch.shared->sprg0 = spr_val;
		break;
	case SPRN_SPRG1:
		vcpu->arch.shared->sprg1 = spr_val;
		break;
	case SPRN_SPRG2:
		vcpu->arch.shared->sprg2 = spr_val;
		break;
	case SPRN_SPRG3:
		vcpu->arch.shared->sprg3 = spr_val;
		break;

	default:
		emulated = kvmppc_core_emulate_mtspr(vcpu, sprn,
						     spr_val);
		if (emulated == EMULATE_FAIL)
			printk(KERN_INFO "mtspr: unknown spr "
				"0x%x\n", sprn);
		break;
	}

	kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);

	return emulated;
}

static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
{
	enum emulation_result emulated = EMULATE_DONE;
	ulong spr_val = 0;

	switch (sprn) {
	case SPRN_SRR0:
		spr_val = vcpu->arch.shared->srr0;
		break;
	case SPRN_SRR1:
		spr_val = vcpu->arch.shared->srr1;
		break;
	case SPRN_PVR:
		spr_val = vcpu->arch.pvr;
		break;
	case SPRN_PIR:
		spr_val = vcpu->vcpu_id;
		break;

	/* 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:
		spr_val = get_tb() >> 32;
		break;
	case SPRN_TBWU:
		spr_val = get_tb();
		break;

	case SPRN_SPRG0:
		spr_val = vcpu->arch.shared->sprg0;
		break;
	case SPRN_SPRG1:
		spr_val = vcpu->arch.shared->sprg1;
		break;
	case SPRN_SPRG2:
		spr_val = vcpu->arch.shared->sprg2;
		break;
	case SPRN_SPRG3:
		spr_val = vcpu->arch.shared->sprg3;
		break;
	/* Note: SPRG4-7 are user-readable, so we don't get
	 * a trap. */

	case SPRN_DEC:
		spr_val = kvmppc_get_dec(vcpu, get_tb());
		break;
	default:
		emulated = kvmppc_core_emulate_mfspr(vcpu, sprn,
						     &spr_val);
		if (unlikely(emulated == EMULATE_FAIL)) {
			printk(KERN_INFO "mfspr: unknown spr "
				"0x%x\n", sprn);
		}
		break;
	}

	if (emulated == EMULATE_DONE)
		kvmppc_set_gpr(vcpu, rt, spr_val);
	kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);

	return emulated;
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262
/* XXX to do:
 * lhax
 * lhaux
 * lswx
 * lswi
 * stswx
 * stswi
 * lha
 * lhau
 * lmw
 * stmw
 *
 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
 */
263 264
/* XXX Should probably auto-generate instruction decoding for a particular core
 * from opcode tables in the future. */
265 266
int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
{
267
	u32 inst = kvmppc_get_last_inst(vcpu);
268 269 270 271
	int ra = get_ra(inst);
	int rs = get_rs(inst);
	int rt = get_rt(inst);
	int sprn = get_sprn(inst);
272 273 274
	enum emulation_result emulated = EMULATE_DONE;
	int advance = 1;

275 276 277
	/* this default type might be overwritten by subcategories */
	kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);

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

280
	switch (get_op(inst)) {
281
	case OP_TRAP:
282
#ifdef CONFIG_PPC_BOOK3S
283
	case OP_TRAP_64:
284
		kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
285
#else
286 287
		kvmppc_core_queue_program(vcpu,
					  vcpu->arch.shared->esr | ESR_PTR);
288
#endif
289 290 291 292 293 294
		advance = 0;
		break;

	case 31:
		switch (get_xop(inst)) {

295 296 297 298 299 300 301 302 303 304 305 306
		case OP_31_XOP_TRAP:
#ifdef CONFIG_64BIT
		case OP_31_XOP_TRAP_64:
#endif
#ifdef CONFIG_PPC_BOOK3S
			kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
#else
			kvmppc_core_queue_program(vcpu,
					vcpu->arch.shared->esr | ESR_PTR);
#endif
			advance = 0;
			break;
307
		case OP_31_XOP_LWZX:
308 309 310
			emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
			break;

311
		case OP_31_XOP_LBZX:
312 313 314
			emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
			break;

315 316
		case OP_31_XOP_LBZUX:
			emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
317
			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
318 319
			break;

320
		case OP_31_XOP_STWX:
321
			emulated = kvmppc_handle_store(run, vcpu,
322
						       kvmppc_get_gpr(vcpu, rs),
323 324 325
			                               4, 1);
			break;

326
		case OP_31_XOP_STBX:
327
			emulated = kvmppc_handle_store(run, vcpu,
328
						       kvmppc_get_gpr(vcpu, rs),
329 330 331
			                               1, 1);
			break;

332
		case OP_31_XOP_STBUX:
333
			emulated = kvmppc_handle_store(run, vcpu,
334
						       kvmppc_get_gpr(vcpu, rs),
335
			                               1, 1);
A
Alexander Graf 已提交
336
			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
337 338
			break;

339 340 341 342
		case OP_31_XOP_LHAX:
			emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
			break;

343
		case OP_31_XOP_LHZX:
344 345 346
			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
			break;

347
		case OP_31_XOP_LHZUX:
348
			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
349
			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
350 351
			break;

352
		case OP_31_XOP_MFSPR:
353
			emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
354 355
			break;

356
		case OP_31_XOP_STHX:
357
			emulated = kvmppc_handle_store(run, vcpu,
358
						       kvmppc_get_gpr(vcpu, rs),
359 360 361
			                               2, 1);
			break;

362
		case OP_31_XOP_STHUX:
363
			emulated = kvmppc_handle_store(run, vcpu,
364
						       kvmppc_get_gpr(vcpu, rs),
365
			                               2, 1);
366
			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
367 368
			break;

369
		case OP_31_XOP_MTSPR:
370
			emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
371 372
			break;

A
Alexander Graf 已提交
373
		case OP_31_XOP_DCBF:
374
		case OP_31_XOP_DCBI:
375 376 377 378 379 380 381
			/* 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;

382
		case OP_31_XOP_LWBRX:
383 384 385
			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
			emulated = kvmppc_handle_store(run, vcpu,
391
						       kvmppc_get_gpr(vcpu, rs),
392 393 394
			                               4, 0);
			break;

395
		case OP_31_XOP_LHBRX:
396 397 398
			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
			break;

399
		case OP_31_XOP_STHBRX:
400
			emulated = kvmppc_handle_store(run, vcpu,
401
						       kvmppc_get_gpr(vcpu, rs),
402 403 404 405
			                               2, 0);
			break;

		default:
406
			/* Attempt core-specific emulation below. */
407 408 409 410
			emulated = EMULATE_FAIL;
		}
		break;

411
	case OP_LWZ:
412 413 414
		emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
		break;

415 416 417 418 419 420
	/* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */
	case OP_LD:
		rt = get_rt(inst);
		emulated = kvmppc_handle_load(run, vcpu, rt, 8, 1);
		break;

421
	case OP_LWZU:
422
		emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
423
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
424 425
		break;

426
	case OP_LBZ:
427 428 429
		emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
		break;

430
	case OP_LBZU:
431
		emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
432
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
433 434
		break;

435
	case OP_STW:
436 437
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
438 439 440
		                               4, 1);
		break;

441 442 443 444 445 446 447 448
	/* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */
	case OP_STD:
		rs = get_rs(inst);
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
		                               8, 1);
		break;

449
	case OP_STWU:
450 451
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
452
		                               4, 1);
453
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
454 455
		break;

456
	case OP_STB:
457 458
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
459 460 461
		                               1, 1);
		break;

462
	case OP_STBU:
463 464
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
465
		                               1, 1);
466
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
467 468
		break;

469
	case OP_LHZ:
470 471 472
		emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
		break;

473
	case OP_LHZU:
474
		emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
475
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
476 477
		break;

A
Alexander Graf 已提交
478 479 480 481 482 483
	case OP_LHA:
		emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
		break;

	case OP_LHAU:
		emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
484
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
A
Alexander Graf 已提交
485 486
		break;

487
	case OP_STH:
488 489
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
490 491 492
		                               2, 1);
		break;

493
	case OP_STHU:
494 495
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
496
		                               2, 1);
497
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
498 499 500 501
		break;

	default:
		emulated = EMULATE_FAIL;
502 503 504 505
	}

	if (emulated == EMULATE_FAIL) {
		emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
506 507 508
		if (emulated == EMULATE_AGAIN) {
			advance = 0;
		} else if (emulated == EMULATE_FAIL) {
509 510 511
			advance = 0;
			printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
			       "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
512
			kvmppc_core_queue_program(vcpu, 0);
513
		}
514 515
	}

516
	trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
517

518
	/* Advance past emulated instruction. */
519
	if (advance)
520
		kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
521 522 523

	return emulated;
}