emulate.c 11.8 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 <asm/ppc-opcode.h>
34
#include "timing.h"
35
#include "trace.h"
36

37
void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
38
{
A
Alexander Graf 已提交
39
	unsigned long dec_nsec;
40
	unsigned long long dec_time;
A
Alexander Graf 已提交
41

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

45
#ifdef CONFIG_PPC_BOOK3S
46 47 48
	/* mtdec lowers the interrupt line when positive. */
	kvmppc_core_dequeue_dec(vcpu);

49 50 51 52 53 54
	/* POWER4+ triggers a dec interrupt if the value is < 0 */
	if (vcpu->arch.dec & 0x80000000) {
		kvmppc_core_queue_dec(vcpu);
		return;
	}
#endif
55 56 57 58 59 60 61 62 63 64 65 66 67 68

#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;
69 70 71 72 73 74
	/*
	 * 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);
75 76 77 78
	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();
79 80
}

S
Scott Wood 已提交
81 82 83
u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
{
	u64 jd = tb - vcpu->arch.dec_jiffies;
84 85 86 87 88 89

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

S
Scott Wood 已提交
90 91 92
	return vcpu->arch.dec - jd;
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
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;

A
Alexander Graf 已提交
129 130 131
	/* PIR can legally be written, but we ignore it */
	case SPRN_PIR: break;

132
	default:
133 134
		emulated = vcpu->kvm->arch.kvm_ops->emulate_mtspr(vcpu, sprn,
								  spr_val);
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
		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:
194 195
		emulated = vcpu->kvm->arch.kvm_ops->emulate_mfspr(vcpu, sprn,
								  &spr_val);
196 197 198 199 200 201 202 203 204 205 206 207 208 209
		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;
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223
/* XXX to do:
 * lhax
 * lhaux
 * lswx
 * lswi
 * stswx
 * stswi
 * lha
 * lhau
 * lmw
 * stmw
 *
 * XXX is_bigendian should depend on MMU mapping or MSR[LE]
 */
224 225
/* XXX Should probably auto-generate instruction decoding for a particular core
 * from opcode tables in the future. */
226 227
int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
{
228
	u32 inst = kvmppc_get_last_inst(vcpu);
229 230 231 232
	int ra = get_ra(inst);
	int rs = get_rs(inst);
	int rt = get_rt(inst);
	int sprn = get_sprn(inst);
233 234 235
	enum emulation_result emulated = EMULATE_DONE;
	int advance = 1;

236 237 238
	/* this default type might be overwritten by subcategories */
	kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);

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

241
	switch (get_op(inst)) {
242
	case OP_TRAP:
243
#ifdef CONFIG_PPC_BOOK3S
244
	case OP_TRAP_64:
245
		kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
246
#else
247 248
		kvmppc_core_queue_program(vcpu,
					  vcpu->arch.shared->esr | ESR_PTR);
249
#endif
250 251 252 253 254 255
		advance = 0;
		break;

	case 31:
		switch (get_xop(inst)) {

256 257 258 259 260 261 262 263 264 265 266 267
		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;
268
		case OP_31_XOP_LWZX:
269 270 271
			emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
			break;

272
		case OP_31_XOP_LBZX:
273 274 275
			emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
			break;

276 277
		case OP_31_XOP_LBZUX:
			emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
278
			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
279 280
			break;

281
		case OP_31_XOP_STWX:
282
			emulated = kvmppc_handle_store(run, vcpu,
283
						       kvmppc_get_gpr(vcpu, rs),
284 285 286
			                               4, 1);
			break;

287
		case OP_31_XOP_STBX:
288
			emulated = kvmppc_handle_store(run, vcpu,
289
						       kvmppc_get_gpr(vcpu, rs),
290 291 292
			                               1, 1);
			break;

293
		case OP_31_XOP_STBUX:
294
			emulated = kvmppc_handle_store(run, vcpu,
295
						       kvmppc_get_gpr(vcpu, rs),
296
			                               1, 1);
A
Alexander Graf 已提交
297
			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
298 299
			break;

300 301 302 303
		case OP_31_XOP_LHAX:
			emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
			break;

304
		case OP_31_XOP_LHZX:
305 306 307
			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
			break;

308
		case OP_31_XOP_LHZUX:
309
			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
310
			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
311 312
			break;

313
		case OP_31_XOP_MFSPR:
314
			emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
315 316
			break;

317
		case OP_31_XOP_STHX:
318
			emulated = kvmppc_handle_store(run, vcpu,
319
						       kvmppc_get_gpr(vcpu, rs),
320 321 322
			                               2, 1);
			break;

323
		case OP_31_XOP_STHUX:
324
			emulated = kvmppc_handle_store(run, vcpu,
325
						       kvmppc_get_gpr(vcpu, rs),
326
			                               2, 1);
327
			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
328 329
			break;

330
		case OP_31_XOP_MTSPR:
331
			emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
332 333
			break;

S
Stuart Yoder 已提交
334
		case OP_31_XOP_DCBST:
A
Alexander Graf 已提交
335
		case OP_31_XOP_DCBF:
336
		case OP_31_XOP_DCBI:
337 338 339 340 341 342 343
			/* 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;

344
		case OP_31_XOP_LWBRX:
345 346 347
			emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
			break;

348
		case OP_31_XOP_TLBSYNC:
349 350
			break;

351
		case OP_31_XOP_STWBRX:
352
			emulated = kvmppc_handle_store(run, vcpu,
353
						       kvmppc_get_gpr(vcpu, rs),
354 355 356
			                               4, 0);
			break;

357
		case OP_31_XOP_LHBRX:
358 359 360
			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
			break;

361
		case OP_31_XOP_STHBRX:
362
			emulated = kvmppc_handle_store(run, vcpu,
363
						       kvmppc_get_gpr(vcpu, rs),
364 365 366 367
			                               2, 0);
			break;

		default:
368
			/* Attempt core-specific emulation below. */
369 370 371 372
			emulated = EMULATE_FAIL;
		}
		break;

373
	case OP_LWZ:
374 375 376
		emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
		break;

377 378 379 380 381 382
	/* 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;

383
	case OP_LWZU:
384
		emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
385
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
386 387
		break;

388
	case OP_LBZ:
389 390 391
		emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
		break;

392
	case OP_LBZU:
393
		emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
394
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
395 396
		break;

397
	case OP_STW:
398 399
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
400 401 402
		                               4, 1);
		break;

403 404 405 406 407 408 409 410
	/* 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;

411
	case OP_STWU:
412 413
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
414
		                               4, 1);
415
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
416 417
		break;

418
	case OP_STB:
419 420
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
421 422 423
		                               1, 1);
		break;

424
	case OP_STBU:
425 426
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
427
		                               1, 1);
428
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
429 430
		break;

431
	case OP_LHZ:
432 433 434
		emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
		break;

435
	case OP_LHZU:
436
		emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
437
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
438 439
		break;

A
Alexander Graf 已提交
440 441 442 443 444 445
	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);
446
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
A
Alexander Graf 已提交
447 448
		break;

449
	case OP_STH:
450 451
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
452 453 454
		                               2, 1);
		break;

455
	case OP_STHU:
456 457
		emulated = kvmppc_handle_store(run, vcpu,
					       kvmppc_get_gpr(vcpu, rs),
458
		                               2, 1);
459
		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
460 461 462 463
		break;

	default:
		emulated = EMULATE_FAIL;
464 465 466
	}

	if (emulated == EMULATE_FAIL) {
467 468
		emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst,
							       &advance);
469 470 471
		if (emulated == EMULATE_AGAIN) {
			advance = 0;
		} else if (emulated == EMULATE_FAIL) {
472 473 474
			advance = 0;
			printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
			       "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
475
			kvmppc_core_queue_program(vcpu, 0);
476
		}
477 478
	}

479
	trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
480

481
	/* Advance past emulated instruction. */
482
	if (advance)
483
		kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
484 485 486

	return emulated;
}
487
EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction);