book3s_emulate.c 17.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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 SUSE Linux Products GmbH 2009
 *
 * Authors: Alexander Graf <agraf@suse.de>
 */

#include <asm/kvm_ppc.h>
#include <asm/disassemble.h>
#include <asm/kvm_book3s.h>
#include <asm/reg.h>
24
#include <asm/switch_to.h>
25
#include <asm/time.h>
T
Thomas Huth 已提交
26
#include "book3s.h"
27 28 29 30 31 32 33

#define OP_19_XOP_RFID		18
#define OP_19_XOP_RFI		50

#define OP_31_XOP_MFMSR		83
#define OP_31_XOP_MTMSR		146
#define OP_31_XOP_MTMSRD	178
34
#define OP_31_XOP_MTSR		210
35 36 37
#define OP_31_XOP_MTSRIN	242
#define OP_31_XOP_TLBIEL	274
#define OP_31_XOP_TLBIE		306
38 39
/* Opcode is officially reserved, reuse it as sc 1 when sc 1 doesn't trap */
#define OP_31_XOP_FAKE_SC1	308
40 41 42
#define OP_31_XOP_SLBMTE	402
#define OP_31_XOP_SLBIE		434
#define OP_31_XOP_SLBIA		498
43
#define OP_31_XOP_MFSR		595
44
#define OP_31_XOP_MFSRIN	659
45
#define OP_31_XOP_DCBA		758
46 47 48 49 50 51 52
#define OP_31_XOP_SLBMFEV	851
#define OP_31_XOP_EIOIO		854
#define OP_31_XOP_SLBMFEE	915

/* DCBZ is actually 1014, but we patch it to 1010 so we get a trap */
#define OP_31_XOP_DCBZ		1010

53 54 55 56 57
#define OP_LFS			48
#define OP_LFD			50
#define OP_STFS			52
#define OP_STFD			54

A
Alexander Graf 已提交
58 59 60 61 62 63 64 65 66
#define SPRN_GQR0		912
#define SPRN_GQR1		913
#define SPRN_GQR2		914
#define SPRN_GQR3		915
#define SPRN_GQR4		916
#define SPRN_GQR5		917
#define SPRN_GQR6		918
#define SPRN_GQR7		919

67 68 69 70
/* Book3S_32 defines mfsrin(v) - but that messes up our abstract
 * function pointers, so let's just disable the define. */
#undef mfsrin

71 72 73 74 75 76 77 78 79 80 81 82 83
enum priv_level {
	PRIV_PROBLEM = 0,
	PRIV_SUPER = 1,
	PRIV_HYPER = 2,
};

static bool spr_allowed(struct kvm_vcpu *vcpu, enum priv_level level)
{
	/* PAPR VMs only access supervisor SPRs */
	if (vcpu->arch.papr_enabled && (level > PRIV_SUPER))
		return false;

	/* Limit user space to its own small SPR set */
84
	if ((kvmppc_get_msr(vcpu) & MSR_PR) && level > PRIV_PROBLEM)
85 86 87 88 89
		return false;

	return true;
}

90 91 92 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 129 130
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
static inline void kvmppc_copyto_vcpu_tm(struct kvm_vcpu *vcpu)
{
	memcpy(&vcpu->arch.gpr_tm[0], &vcpu->arch.regs.gpr[0],
			sizeof(vcpu->arch.gpr_tm));
	memcpy(&vcpu->arch.fp_tm, &vcpu->arch.fp,
			sizeof(struct thread_fp_state));
	memcpy(&vcpu->arch.vr_tm, &vcpu->arch.vr,
			sizeof(struct thread_vr_state));
	vcpu->arch.ppr_tm = vcpu->arch.ppr;
	vcpu->arch.dscr_tm = vcpu->arch.dscr;
	vcpu->arch.amr_tm = vcpu->arch.amr;
	vcpu->arch.ctr_tm = vcpu->arch.regs.ctr;
	vcpu->arch.tar_tm = vcpu->arch.tar;
	vcpu->arch.lr_tm = vcpu->arch.regs.link;
	vcpu->arch.cr_tm = vcpu->arch.cr;
	vcpu->arch.xer_tm = vcpu->arch.regs.xer;
	vcpu->arch.vrsave_tm = vcpu->arch.vrsave;
}

static inline void kvmppc_copyfrom_vcpu_tm(struct kvm_vcpu *vcpu)
{
	memcpy(&vcpu->arch.regs.gpr[0], &vcpu->arch.gpr_tm[0],
			sizeof(vcpu->arch.regs.gpr));
	memcpy(&vcpu->arch.fp, &vcpu->arch.fp_tm,
			sizeof(struct thread_fp_state));
	memcpy(&vcpu->arch.vr, &vcpu->arch.vr_tm,
			sizeof(struct thread_vr_state));
	vcpu->arch.ppr = vcpu->arch.ppr_tm;
	vcpu->arch.dscr = vcpu->arch.dscr_tm;
	vcpu->arch.amr = vcpu->arch.amr_tm;
	vcpu->arch.regs.ctr = vcpu->arch.ctr_tm;
	vcpu->arch.tar = vcpu->arch.tar_tm;
	vcpu->arch.regs.link = vcpu->arch.lr_tm;
	vcpu->arch.cr = vcpu->arch.cr_tm;
	vcpu->arch.regs.xer = vcpu->arch.xer_tm;
	vcpu->arch.vrsave = vcpu->arch.vrsave_tm;
}

#endif

131 132
int kvmppc_core_emulate_op_pr(struct kvm_run *run, struct kvm_vcpu *vcpu,
			      unsigned int inst, int *advance)
133 134
{
	int emulated = EMULATE_DONE;
135 136 137 138
	int rt = get_rt(inst);
	int rs = get_rs(inst);
	int ra = get_ra(inst);
	int rb = get_rb(inst);
139
	u32 inst_sc = 0x44000002;
140 141

	switch (get_op(inst)) {
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	case 0:
		emulated = EMULATE_FAIL;
		if ((kvmppc_get_msr(vcpu) & MSR_LE) &&
		    (inst == swab32(inst_sc))) {
			/*
			 * This is the byte reversed syscall instruction of our
			 * hypercall handler. Early versions of LE Linux didn't
			 * swap the instructions correctly and ended up in
			 * illegal instructions.
			 * Just always fail hypercalls on these broken systems.
			 */
			kvmppc_set_gpr(vcpu, 3, EV_UNIMPLEMENTED);
			kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
			emulated = EMULATE_DONE;
		}
		break;
158 159 160
	case 19:
		switch (get_xop(inst)) {
		case OP_19_XOP_RFID:
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
		case OP_19_XOP_RFI: {
			unsigned long srr1 = kvmppc_get_srr1(vcpu);
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
			unsigned long cur_msr = kvmppc_get_msr(vcpu);

			/*
			 * add rules to fit in ISA specification regarding TM
			 * state transistion in TM disable/Suspended state,
			 * and target TM state is TM inactive(00) state. (the
			 * change should be suppressed).
			 */
			if (((cur_msr & MSR_TM) == 0) &&
				((srr1 & MSR_TM) == 0) &&
				MSR_TM_SUSPENDED(cur_msr) &&
				!MSR_TM_ACTIVE(srr1))
				srr1 |= MSR_TS_S;
#endif
178
			kvmppc_set_pc(vcpu, kvmppc_get_srr0(vcpu));
179
			kvmppc_set_msr(vcpu, srr1);
180 181
			*advance = 0;
			break;
182
		}
183 184 185 186 187 188 189 190 191

		default:
			emulated = EMULATE_FAIL;
			break;
		}
		break;
	case 31:
		switch (get_xop(inst)) {
		case OP_31_XOP_MFMSR:
192
			kvmppc_set_gpr(vcpu, rt, kvmppc_get_msr(vcpu));
193 194 195
			break;
		case OP_31_XOP_MTMSRD:
		{
196
			ulong rs_val = kvmppc_get_gpr(vcpu, rs);
197
			if (inst & 0x10000) {
198
				ulong new_msr = kvmppc_get_msr(vcpu);
199 200
				new_msr &= ~(MSR_RI | MSR_EE);
				new_msr |= rs_val & (MSR_RI | MSR_EE);
201
				kvmppc_set_msr_fast(vcpu, new_msr);
202
			} else
203
				kvmppc_set_msr(vcpu, rs_val);
204 205 206
			break;
		}
		case OP_31_XOP_MTMSR:
207
			kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
208
			break;
209 210 211 212 213 214 215 216
		case OP_31_XOP_MFSR:
		{
			int srnum;

			srnum = kvmppc_get_field(inst, 12 + 32, 15 + 32);
			if (vcpu->arch.mmu.mfsrin) {
				u32 sr;
				sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
217
				kvmppc_set_gpr(vcpu, rt, sr);
218 219 220
			}
			break;
		}
221 222 223 224
		case OP_31_XOP_MFSRIN:
		{
			int srnum;

225
			srnum = (kvmppc_get_gpr(vcpu, rb) >> 28) & 0xf;
226 227 228
			if (vcpu->arch.mmu.mfsrin) {
				u32 sr;
				sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
229
				kvmppc_set_gpr(vcpu, rt, sr);
230 231 232
			}
			break;
		}
233 234 235
		case OP_31_XOP_MTSR:
			vcpu->arch.mmu.mtsrin(vcpu,
				(inst >> 16) & 0xf,
236
				kvmppc_get_gpr(vcpu, rs));
237
			break;
238 239
		case OP_31_XOP_MTSRIN:
			vcpu->arch.mmu.mtsrin(vcpu,
240 241
				(kvmppc_get_gpr(vcpu, rb) >> 28) & 0xf,
				kvmppc_get_gpr(vcpu, rs));
242 243 244 245 246
			break;
		case OP_31_XOP_TLBIE:
		case OP_31_XOP_TLBIEL:
		{
			bool large = (inst & 0x00200000) ? true : false;
247
			ulong addr = kvmppc_get_gpr(vcpu, rb);
248 249 250
			vcpu->arch.mmu.tlbie(vcpu, addr, large);
			break;
		}
251
#ifdef CONFIG_PPC_BOOK3S_64
252 253 254 255 256 257
		case OP_31_XOP_FAKE_SC1:
		{
			/* SC 1 papr hypercalls */
			ulong cmd = kvmppc_get_gpr(vcpu, 3);
			int i;

258
		        if ((kvmppc_get_msr(vcpu) & MSR_PR) ||
259 260 261 262 263 264 265 266 267 268 269 270 271 272
			    !vcpu->arch.papr_enabled) {
				emulated = EMULATE_FAIL;
				break;
			}

			if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE)
				break;

			run->papr_hcall.nr = cmd;
			for (i = 0; i < 9; ++i) {
				ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
				run->papr_hcall.args[i] = gpr;
			}

273 274
			run->exit_reason = KVM_EXIT_PAPR_HCALL;
			vcpu->arch.hcall_needed = 1;
275
			emulated = EMULATE_EXIT_USER;
276 277 278
			break;
		}
#endif
279 280 281 282 283 284
		case OP_31_XOP_EIOIO:
			break;
		case OP_31_XOP_SLBMTE:
			if (!vcpu->arch.mmu.slbmte)
				return EMULATE_FAIL;

285
			vcpu->arch.mmu.slbmte(vcpu,
286 287
					kvmppc_get_gpr(vcpu, rs),
					kvmppc_get_gpr(vcpu, rb));
288 289 290 291 292
			break;
		case OP_31_XOP_SLBIE:
			if (!vcpu->arch.mmu.slbie)
				return EMULATE_FAIL;

293
			vcpu->arch.mmu.slbie(vcpu,
294
					kvmppc_get_gpr(vcpu, rb));
295 296 297 298 299 300 301 302 303 304 305
			break;
		case OP_31_XOP_SLBIA:
			if (!vcpu->arch.mmu.slbia)
				return EMULATE_FAIL;

			vcpu->arch.mmu.slbia(vcpu);
			break;
		case OP_31_XOP_SLBMFEE:
			if (!vcpu->arch.mmu.slbmfee) {
				emulated = EMULATE_FAIL;
			} else {
306
				ulong t, rb_val;
307

308 309 310
				rb_val = kvmppc_get_gpr(vcpu, rb);
				t = vcpu->arch.mmu.slbmfee(vcpu, rb_val);
				kvmppc_set_gpr(vcpu, rt, t);
311 312 313 314 315 316
			}
			break;
		case OP_31_XOP_SLBMFEV:
			if (!vcpu->arch.mmu.slbmfev) {
				emulated = EMULATE_FAIL;
			} else {
317
				ulong t, rb_val;
318

319 320 321
				rb_val = kvmppc_get_gpr(vcpu, rb);
				t = vcpu->arch.mmu.slbmfev(vcpu, rb_val);
				kvmppc_set_gpr(vcpu, rt, t);
322 323
			}
			break;
324 325 326
		case OP_31_XOP_DCBA:
			/* Gets treated as NOP */
			break;
327 328
		case OP_31_XOP_DCBZ:
		{
329 330
			ulong rb_val = kvmppc_get_gpr(vcpu, rb);
			ulong ra_val = 0;
331
			ulong addr, vaddr;
332
			u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
A
Alexander Graf 已提交
333 334
			u32 dsisr;
			int r;
335

336 337
			if (ra)
				ra_val = kvmppc_get_gpr(vcpu, ra);
338

339
			addr = (ra_val + rb_val) & ~31ULL;
340
			if (!(kvmppc_get_msr(vcpu) & MSR_SF))
341
				addr &= 0xffffffff;
342
			vaddr = addr;
343

A
Alexander Graf 已提交
344 345 346
			r = kvmppc_st(vcpu, &addr, 32, zeros, true);
			if ((r == -ENOENT) || (r == -EPERM)) {
				*advance = 0;
347
				kvmppc_set_dar(vcpu, vaddr);
348
				vcpu->arch.fault_dar = vaddr;
A
Alexander Graf 已提交
349 350 351 352 353 354 355

				dsisr = DSISR_ISSTORE;
				if (r == -ENOENT)
					dsisr |= DSISR_NOHPTE;
				else if (r == -EPERM)
					dsisr |= DSISR_PROTFAULT;

356
				kvmppc_set_dsisr(vcpu, dsisr);
357
				vcpu->arch.fault_dsisr = dsisr;
A
Alexander Graf 已提交
358

359 360 361 362 363 364 365 366 367 368 369 370 371 372
				kvmppc_book3s_queue_irqprio(vcpu,
					BOOK3S_INTERRUPT_DATA_STORAGE);
			}

			break;
		}
		default:
			emulated = EMULATE_FAIL;
		}
		break;
	default:
		emulated = EMULATE_FAIL;
	}

373 374 375
	if (emulated == EMULATE_FAIL)
		emulated = kvmppc_emulate_paired_single(run, vcpu);

376 377 378
	return emulated;
}

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, bool upper,
                    u32 val)
{
	if (upper) {
		/* Upper BAT */
		u32 bl = (val >> 2) & 0x7ff;
		bat->bepi_mask = (~bl << 17);
		bat->bepi = val & 0xfffe0000;
		bat->vs = (val & 2) ? 1 : 0;
		bat->vp = (val & 1) ? 1 : 0;
		bat->raw = (bat->raw & 0xffffffff00000000ULL) | val;
	} else {
		/* Lower BAT */
		bat->brpn = val & 0xfffe0000;
		bat->wimg = (val >> 3) & 0xf;
		bat->pp = val & 3;
		bat->raw = (bat->raw & 0x00000000ffffffffULL) | ((u64)val << 32);
	}
}

399
static struct kvmppc_bat *kvmppc_find_bat(struct kvm_vcpu *vcpu, int sprn)
A
Alexander Graf 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
{
	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
	struct kvmppc_bat *bat;

	switch (sprn) {
	case SPRN_IBAT0U ... SPRN_IBAT3L:
		bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
		break;
	case SPRN_IBAT4U ... SPRN_IBAT7L:
		bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
		break;
	case SPRN_DBAT0U ... SPRN_DBAT3L:
		bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
		break;
	case SPRN_DBAT4U ... SPRN_DBAT7L:
		bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
		break;
	default:
		BUG();
	}

421
	return bat;
422 423
}

424
int kvmppc_core_emulate_mtspr_pr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
425 426 427 428 429
{
	int emulated = EMULATE_DONE;

	switch (sprn) {
	case SPRN_SDR1:
430 431
		if (!spr_allowed(vcpu, PRIV_HYPER))
			goto unprivileged;
432
		to_book3s(vcpu)->sdr1 = spr_val;
433 434
		break;
	case SPRN_DSISR:
435
		kvmppc_set_dsisr(vcpu, spr_val);
436 437
		break;
	case SPRN_DAR:
438
		kvmppc_set_dar(vcpu, spr_val);
439 440
		break;
	case SPRN_HIOR:
441
		to_book3s(vcpu)->hior = spr_val;
442 443 444 445 446
		break;
	case SPRN_IBAT0U ... SPRN_IBAT3L:
	case SPRN_IBAT4U ... SPRN_IBAT7L:
	case SPRN_DBAT0U ... SPRN_DBAT3L:
	case SPRN_DBAT4U ... SPRN_DBAT7L:
447 448 449 450
	{
		struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);

		kvmppc_set_bat(vcpu, bat, !(sprn % 2), (u32)spr_val);
451 452 453
		/* BAT writes happen so rarely that we're ok to flush
		 * everything here */
		kvmppc_mmu_pte_flush(vcpu, 0, 0);
A
Alexander Graf 已提交
454
		kvmppc_mmu_flush_segments(vcpu);
455
		break;
456
	}
457
	case SPRN_HID0:
458
		to_book3s(vcpu)->hid[0] = spr_val;
459 460
		break;
	case SPRN_HID1:
461
		to_book3s(vcpu)->hid[1] = spr_val;
462 463
		break;
	case SPRN_HID2:
464
		to_book3s(vcpu)->hid[2] = spr_val;
465
		break;
A
Alexander Graf 已提交
466 467 468 469 470 471 472 473 474 475 476 477
	case SPRN_HID2_GEKKO:
		to_book3s(vcpu)->hid[2] = spr_val;
		/* HID2.PSE controls paired single on gekko */
		switch (vcpu->arch.pvr) {
		case 0x00080200:	/* lonestar 2.0 */
		case 0x00088202:	/* lonestar 2.2 */
		case 0x70000100:	/* gekko 1.0 */
		case 0x00080100:	/* gekko 2.0 */
		case 0x00083203:	/* gekko 2.3a */
		case 0x00083213:	/* gekko 2.3b */
		case 0x00083204:	/* gekko 2.4 */
		case 0x00083214:	/* gekko 2.4e (8SE) - retail HW2 */
478 479 480 481
		case 0x00087200:	/* broadway */
			if (vcpu->arch.hflags & BOOK3S_HFLAG_NATIVE_PS) {
				/* Native paired singles */
			} else if (spr_val & (1 << 29)) { /* HID2.PSE */
A
Alexander Graf 已提交
482 483 484 485 486 487 488 489
				vcpu->arch.hflags |= BOOK3S_HFLAG_PAIRED_SINGLE;
				kvmppc_giveup_ext(vcpu, MSR_FP);
			} else {
				vcpu->arch.hflags &= ~BOOK3S_HFLAG_PAIRED_SINGLE;
			}
			break;
		}
		break;
490
	case SPRN_HID4:
A
Alexander Graf 已提交
491
	case SPRN_HID4_GEKKO:
492
		to_book3s(vcpu)->hid[4] = spr_val;
493 494
		break;
	case SPRN_HID5:
495
		to_book3s(vcpu)->hid[5] = spr_val;
496 497 498 499 500
		/* guest HID5 set can change is_dcbz32 */
		if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
		    (mfmsr() & MSR_HV))
			vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
		break;
A
Alexander Graf 已提交
501 502 503 504 505 506 507 508 509 510
	case SPRN_GQR0:
	case SPRN_GQR1:
	case SPRN_GQR2:
	case SPRN_GQR3:
	case SPRN_GQR4:
	case SPRN_GQR5:
	case SPRN_GQR6:
	case SPRN_GQR7:
		to_book3s(vcpu)->gqr[sprn - SPRN_GQR0] = spr_val;
		break;
511
#ifdef CONFIG_PPC_BOOK3S_64
512
	case SPRN_FSCR:
513
		kvmppc_set_fscr(vcpu, spr_val);
514
		break;
515 516 517 518 519 520 521 522 523
	case SPRN_BESCR:
		vcpu->arch.bescr = spr_val;
		break;
	case SPRN_EBBHR:
		vcpu->arch.ebbhr = spr_val;
		break;
	case SPRN_EBBRR:
		vcpu->arch.ebbrr = spr_val;
		break;
524 525 526 527 528 529 530 531 532 533 534
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
	case SPRN_TFHAR:
		vcpu->arch.tfhar = spr_val;
		break;
	case SPRN_TEXASR:
		vcpu->arch.texasr = spr_val;
		break;
	case SPRN_TFIAR:
		vcpu->arch.tfiar = spr_val;
		break;
#endif
535
#endif
536 537 538 539 540 541
	case SPRN_ICTC:
	case SPRN_THRM1:
	case SPRN_THRM2:
	case SPRN_THRM3:
	case SPRN_CTRLF:
	case SPRN_CTRLT:
A
Alexander Graf 已提交
542
	case SPRN_L2CR:
543
	case SPRN_DSCR:
A
Alexander Graf 已提交
544 545 546 547 548 549 550
	case SPRN_MMCR0_GEKKO:
	case SPRN_MMCR1_GEKKO:
	case SPRN_PMC1_GEKKO:
	case SPRN_PMC2_GEKKO:
	case SPRN_PMC3_GEKKO:
	case SPRN_PMC4_GEKKO:
	case SPRN_WPAR_GEKKO:
551
	case SPRN_MSSSR0:
552
	case SPRN_DABR:
553 554 555 556 557 558
#ifdef CONFIG_PPC_BOOK3S_64
	case SPRN_MMCRS:
	case SPRN_MMCRA:
	case SPRN_MMCR0:
	case SPRN_MMCR1:
	case SPRN_MMCR2:
559
	case SPRN_UMMCR2:
560
#endif
561
		break;
562
unprivileged:
563
	default:
564 565 566 567 568 569 570 571 572 573 574 575
		pr_info_ratelimited("KVM: invalid SPR write: %d\n", sprn);
		if (sprn & 0x10) {
			if (kvmppc_get_msr(vcpu) & MSR_PR) {
				kvmppc_core_queue_program(vcpu, SRR1_PROGPRIV);
				emulated = EMULATE_AGAIN;
			}
		} else {
			if ((kvmppc_get_msr(vcpu) & MSR_PR) || sprn == 0) {
				kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
				emulated = EMULATE_AGAIN;
			}
		}
576 577 578 579 580 581
		break;
	}

	return emulated;
}

582
int kvmppc_core_emulate_mfspr_pr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
583 584 585 586
{
	int emulated = EMULATE_DONE;

	switch (sprn) {
A
Alexander Graf 已提交
587 588 589 590
	case SPRN_IBAT0U ... SPRN_IBAT3L:
	case SPRN_IBAT4U ... SPRN_IBAT7L:
	case SPRN_DBAT0U ... SPRN_DBAT3L:
	case SPRN_DBAT4U ... SPRN_DBAT7L:
591 592 593 594
	{
		struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);

		if (sprn % 2)
595
			*spr_val = bat->raw >> 32;
596
		else
597
			*spr_val = bat->raw;
598

A
Alexander Graf 已提交
599
		break;
600
	}
601
	case SPRN_SDR1:
602 603
		if (!spr_allowed(vcpu, PRIV_HYPER))
			goto unprivileged;
604
		*spr_val = to_book3s(vcpu)->sdr1;
605 606
		break;
	case SPRN_DSISR:
607
		*spr_val = kvmppc_get_dsisr(vcpu);
608 609
		break;
	case SPRN_DAR:
610
		*spr_val = kvmppc_get_dar(vcpu);
611 612
		break;
	case SPRN_HIOR:
613
		*spr_val = to_book3s(vcpu)->hior;
614 615
		break;
	case SPRN_HID0:
616
		*spr_val = to_book3s(vcpu)->hid[0];
617 618
		break;
	case SPRN_HID1:
619
		*spr_val = to_book3s(vcpu)->hid[1];
620 621
		break;
	case SPRN_HID2:
A
Alexander Graf 已提交
622
	case SPRN_HID2_GEKKO:
623
		*spr_val = to_book3s(vcpu)->hid[2];
624 625
		break;
	case SPRN_HID4:
A
Alexander Graf 已提交
626
	case SPRN_HID4_GEKKO:
627
		*spr_val = to_book3s(vcpu)->hid[4];
628 629
		break;
	case SPRN_HID5:
630
		*spr_val = to_book3s(vcpu)->hid[5];
631
		break;
632
	case SPRN_CFAR:
633
	case SPRN_DSCR:
634
		*spr_val = 0;
635
		break;
636
	case SPRN_PURR:
637 638 639 640
		/*
		 * On exit we would have updated purr
		 */
		*spr_val = vcpu->arch.purr;
641 642
		break;
	case SPRN_SPURR:
643 644 645 646
		/*
		 * On exit we would have updated spurr
		 */
		*spr_val = vcpu->arch.spurr;
647
		break;
648
	case SPRN_VTB:
649
		*spr_val = to_book3s(vcpu)->vtb;
650
		break;
651 652 653
	case SPRN_IC:
		*spr_val = vcpu->arch.ic;
		break;
A
Alexander Graf 已提交
654 655 656 657 658 659 660 661
	case SPRN_GQR0:
	case SPRN_GQR1:
	case SPRN_GQR2:
	case SPRN_GQR3:
	case SPRN_GQR4:
	case SPRN_GQR5:
	case SPRN_GQR6:
	case SPRN_GQR7:
662
		*spr_val = to_book3s(vcpu)->gqr[sprn - SPRN_GQR0];
A
Alexander Graf 已提交
663
		break;
664
#ifdef CONFIG_PPC_BOOK3S_64
665 666 667
	case SPRN_FSCR:
		*spr_val = vcpu->arch.fscr;
		break;
668 669 670 671 672 673 674 675 676
	case SPRN_BESCR:
		*spr_val = vcpu->arch.bescr;
		break;
	case SPRN_EBBHR:
		*spr_val = vcpu->arch.ebbhr;
		break;
	case SPRN_EBBRR:
		*spr_val = vcpu->arch.ebbrr;
		break;
677 678 679 680 681 682 683 684 685 686 687
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
	case SPRN_TFHAR:
		*spr_val = vcpu->arch.tfhar;
		break;
	case SPRN_TEXASR:
		*spr_val = vcpu->arch.texasr;
		break;
	case SPRN_TFIAR:
		*spr_val = vcpu->arch.tfiar;
		break;
#endif
688
#endif
689 690 691 692 693
	case SPRN_THRM1:
	case SPRN_THRM2:
	case SPRN_THRM3:
	case SPRN_CTRLF:
	case SPRN_CTRLT:
A
Alexander Graf 已提交
694 695 696 697 698 699 700 701
	case SPRN_L2CR:
	case SPRN_MMCR0_GEKKO:
	case SPRN_MMCR1_GEKKO:
	case SPRN_PMC1_GEKKO:
	case SPRN_PMC2_GEKKO:
	case SPRN_PMC3_GEKKO:
	case SPRN_PMC4_GEKKO:
	case SPRN_WPAR_GEKKO:
702
	case SPRN_MSSSR0:
703
	case SPRN_DABR:
704 705 706 707 708 709
#ifdef CONFIG_PPC_BOOK3S_64
	case SPRN_MMCRS:
	case SPRN_MMCRA:
	case SPRN_MMCR0:
	case SPRN_MMCR1:
	case SPRN_MMCR2:
710
	case SPRN_UMMCR2:
711
	case SPRN_TIR:
712
#endif
713
		*spr_val = 0;
714 715
		break;
	default:
716
unprivileged:
717 718 719 720 721 722 723 724 725 726 727 728 729 730
		pr_info_ratelimited("KVM: invalid SPR read: %d\n", sprn);
		if (sprn & 0x10) {
			if (kvmppc_get_msr(vcpu) & MSR_PR) {
				kvmppc_core_queue_program(vcpu, SRR1_PROGPRIV);
				emulated = EMULATE_AGAIN;
			}
		} else {
			if ((kvmppc_get_msr(vcpu) & MSR_PR) || sprn == 0 ||
			    sprn == 4 || sprn == 5 || sprn == 6) {
				kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
				emulated = EMULATE_AGAIN;
			}
		}

731 732 733 734 735 736
		break;
	}

	return emulated;
}

737 738
u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst)
{
739
	return make_dsisr(inst);
740 741 742 743
}

ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst)
{
744 745 746 747 748 749
#ifdef CONFIG_PPC_BOOK3S_64
	/*
	 * Linux's fix_alignment() assumes that DAR is valid, so can we
	 */
	return vcpu->arch.fault_dar;
#else
750
	ulong dar = 0;
751 752
	ulong ra = get_ra(inst);
	ulong rb = get_rb(inst);
753 754 755 756 757 758 759 760 761 762 763 764 765

	switch (get_op(inst)) {
	case OP_LFS:
	case OP_LFD:
	case OP_STFD:
	case OP_STFS:
		if (ra)
			dar = kvmppc_get_gpr(vcpu, ra);
		dar += (s32)((s16)inst);
		break;
	case 31:
		if (ra)
			dar = kvmppc_get_gpr(vcpu, ra);
766
		dar += kvmppc_get_gpr(vcpu, rb);
767 768 769 770 771 772 773
		break;
	default:
		printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
		break;
	}

	return dar;
774
#endif
775
}