book3s_emulate.c 14.6 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>
26
27
28
29
30
31
32

#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
33
#define OP_31_XOP_MTSR		210
34
35
36
#define OP_31_XOP_MTSRIN	242
#define OP_31_XOP_TLBIEL	274
#define OP_31_XOP_TLBIE		306
37
38
/* Opcode is officially reserved, reuse it as sc 1 when sc 1 doesn't trap */
#define OP_31_XOP_FAKE_SC1	308
39
40
41
#define OP_31_XOP_SLBMTE	402
#define OP_31_XOP_SLBIE		434
#define OP_31_XOP_SLBIA		498
42
#define OP_31_XOP_MFSR		595
43
#define OP_31_XOP_MFSRIN	659
44
#define OP_31_XOP_DCBA		758
45
46
47
48
49
50
51
#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

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

Alexander Graf's avatar
Alexander Graf committed
57
58
59
60
61
62
63
64
65
#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

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

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 */
	if ((vcpu->arch.shared->msr & MSR_PR) && level > PRIV_PROBLEM)
		return false;

	return true;
}

89
90
91
92
int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
                           unsigned int inst, int *advance)
{
	int emulated = EMULATE_DONE;
93
94
95
96
	int rt = get_rt(inst);
	int rs = get_rs(inst);
	int ra = get_ra(inst);
	int rb = get_rb(inst);
97
98
99
100
101
102

	switch (get_op(inst)) {
	case 19:
		switch (get_xop(inst)) {
		case OP_19_XOP_RFID:
		case OP_19_XOP_RFI:
103
104
			kvmppc_set_pc(vcpu, vcpu->arch.shared->srr0);
			kvmppc_set_msr(vcpu, vcpu->arch.shared->srr1);
105
106
107
108
109
110
111
112
113
114
115
			*advance = 0;
			break;

		default:
			emulated = EMULATE_FAIL;
			break;
		}
		break;
	case 31:
		switch (get_xop(inst)) {
		case OP_31_XOP_MFMSR:
116
			kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->msr);
117
118
119
			break;
		case OP_31_XOP_MTMSRD:
		{
120
			ulong rs_val = kvmppc_get_gpr(vcpu, rs);
121
			if (inst & 0x10000) {
122
123
124
125
				ulong new_msr = vcpu->arch.shared->msr;
				new_msr &= ~(MSR_RI | MSR_EE);
				new_msr |= rs_val & (MSR_RI | MSR_EE);
				vcpu->arch.shared->msr = new_msr;
126
			} else
127
				kvmppc_set_msr(vcpu, rs_val);
128
129
130
			break;
		}
		case OP_31_XOP_MTMSR:
131
			kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
132
			break;
133
134
135
136
137
138
139
140
		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);
141
				kvmppc_set_gpr(vcpu, rt, sr);
142
143
144
			}
			break;
		}
145
146
147
148
		case OP_31_XOP_MFSRIN:
		{
			int srnum;

149
			srnum = (kvmppc_get_gpr(vcpu, rb) >> 28) & 0xf;
150
151
152
			if (vcpu->arch.mmu.mfsrin) {
				u32 sr;
				sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
153
				kvmppc_set_gpr(vcpu, rt, sr);
154
155
156
			}
			break;
		}
157
158
159
		case OP_31_XOP_MTSR:
			vcpu->arch.mmu.mtsrin(vcpu,
				(inst >> 16) & 0xf,
160
				kvmppc_get_gpr(vcpu, rs));
161
			break;
162
163
		case OP_31_XOP_MTSRIN:
			vcpu->arch.mmu.mtsrin(vcpu,
164
165
				(kvmppc_get_gpr(vcpu, rb) >> 28) & 0xf,
				kvmppc_get_gpr(vcpu, rs));
166
167
168
169
170
			break;
		case OP_31_XOP_TLBIE:
		case OP_31_XOP_TLBIEL:
		{
			bool large = (inst & 0x00200000) ? true : false;
171
			ulong addr = kvmppc_get_gpr(vcpu, rb);
172
173
174
			vcpu->arch.mmu.tlbie(vcpu, addr, large);
			break;
		}
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#ifdef CONFIG_KVM_BOOK3S_64_PR
		case OP_31_XOP_FAKE_SC1:
		{
			/* SC 1 papr hypercalls */
			ulong cmd = kvmppc_get_gpr(vcpu, 3);
			int i;

		        if ((vcpu->arch.shared->msr & MSR_PR) ||
			    !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;
			}

197
198
			run->exit_reason = KVM_EXIT_PAPR_HCALL;
			vcpu->arch.hcall_needed = 1;
199
			emulated = EMULATE_EXIT_USER;
200
201
202
			break;
		}
#endif
203
204
205
206
207
208
		case OP_31_XOP_EIOIO:
			break;
		case OP_31_XOP_SLBMTE:
			if (!vcpu->arch.mmu.slbmte)
				return EMULATE_FAIL;

209
			vcpu->arch.mmu.slbmte(vcpu,
210
211
					kvmppc_get_gpr(vcpu, rs),
					kvmppc_get_gpr(vcpu, rb));
212
213
214
215
216
			break;
		case OP_31_XOP_SLBIE:
			if (!vcpu->arch.mmu.slbie)
				return EMULATE_FAIL;

217
			vcpu->arch.mmu.slbie(vcpu,
218
					kvmppc_get_gpr(vcpu, rb));
219
220
221
222
223
224
225
226
227
228
229
			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 {
230
				ulong t, rb_val;
231

232
233
234
				rb_val = kvmppc_get_gpr(vcpu, rb);
				t = vcpu->arch.mmu.slbmfee(vcpu, rb_val);
				kvmppc_set_gpr(vcpu, rt, t);
235
236
237
238
239
240
			}
			break;
		case OP_31_XOP_SLBMFEV:
			if (!vcpu->arch.mmu.slbmfev) {
				emulated = EMULATE_FAIL;
			} else {
241
				ulong t, rb_val;
242

243
244
245
				rb_val = kvmppc_get_gpr(vcpu, rb);
				t = vcpu->arch.mmu.slbmfev(vcpu, rb_val);
				kvmppc_set_gpr(vcpu, rt, t);
246
247
			}
			break;
248
249
250
		case OP_31_XOP_DCBA:
			/* Gets treated as NOP */
			break;
251
252
		case OP_31_XOP_DCBZ:
		{
253
254
			ulong rb_val = kvmppc_get_gpr(vcpu, rb);
			ulong ra_val = 0;
255
			ulong addr, vaddr;
256
			u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Alexander Graf's avatar
Alexander Graf committed
257
258
			u32 dsisr;
			int r;
259

260
261
			if (ra)
				ra_val = kvmppc_get_gpr(vcpu, ra);
262

263
			addr = (ra_val + rb_val) & ~31ULL;
264
			if (!(vcpu->arch.shared->msr & MSR_SF))
265
				addr &= 0xffffffff;
266
			vaddr = addr;
267

Alexander Graf's avatar
Alexander Graf committed
268
269
			r = kvmppc_st(vcpu, &addr, 32, zeros, true);
			if ((r == -ENOENT) || (r == -EPERM)) {
270
271
272
				struct kvmppc_book3s_shadow_vcpu *svcpu;

				svcpu = svcpu_get(vcpu);
Alexander Graf's avatar
Alexander Graf committed
273
				*advance = 0;
274
				vcpu->arch.shared->dar = vaddr;
275
				svcpu->fault_dar = vaddr;
Alexander Graf's avatar
Alexander Graf committed
276
277
278
279
280
281
282

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

283
				vcpu->arch.shared->dsisr = dsisr;
284
285
				svcpu->fault_dsisr = dsisr;
				svcpu_put(svcpu);
Alexander Graf's avatar
Alexander Graf committed
286

287
288
289
290
291
292
293
294
295
296
297
298
299
300
				kvmppc_book3s_queue_irqprio(vcpu,
					BOOK3S_INTERRUPT_DATA_STORAGE);
			}

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

301
302
303
	if (emulated == EMULATE_FAIL)
		emulated = kvmppc_emulate_paired_single(run, vcpu);

304
305
306
	return emulated;
}

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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);
	}
}

327
static struct kvmppc_bat *kvmppc_find_bat(struct kvm_vcpu *vcpu, int sprn)
Alexander Graf's avatar
Alexander Graf committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
{
	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();
	}

349
	return bat;
350
351
}

352
int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
353
354
355
356
357
{
	int emulated = EMULATE_DONE;

	switch (sprn) {
	case SPRN_SDR1:
358
359
		if (!spr_allowed(vcpu, PRIV_HYPER))
			goto unprivileged;
360
		to_book3s(vcpu)->sdr1 = spr_val;
361
362
		break;
	case SPRN_DSISR:
363
		vcpu->arch.shared->dsisr = spr_val;
364
365
		break;
	case SPRN_DAR:
366
		vcpu->arch.shared->dar = spr_val;
367
368
		break;
	case SPRN_HIOR:
369
		to_book3s(vcpu)->hior = spr_val;
370
371
372
373
374
		break;
	case SPRN_IBAT0U ... SPRN_IBAT3L:
	case SPRN_IBAT4U ... SPRN_IBAT7L:
	case SPRN_DBAT0U ... SPRN_DBAT3L:
	case SPRN_DBAT4U ... SPRN_DBAT7L:
375
376
377
378
	{
		struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);

		kvmppc_set_bat(vcpu, bat, !(sprn % 2), (u32)spr_val);
379
380
381
		/* BAT writes happen so rarely that we're ok to flush
		 * everything here */
		kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf's avatar
Alexander Graf committed
382
		kvmppc_mmu_flush_segments(vcpu);
383
		break;
384
	}
385
	case SPRN_HID0:
386
		to_book3s(vcpu)->hid[0] = spr_val;
387
388
		break;
	case SPRN_HID1:
389
		to_book3s(vcpu)->hid[1] = spr_val;
390
391
		break;
	case SPRN_HID2:
392
		to_book3s(vcpu)->hid[2] = spr_val;
393
		break;
Alexander Graf's avatar
Alexander Graf committed
394
395
396
397
398
399
400
401
402
403
404
405
	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 */
406
407
408
409
		case 0x00087200:	/* broadway */
			if (vcpu->arch.hflags & BOOK3S_HFLAG_NATIVE_PS) {
				/* Native paired singles */
			} else if (spr_val & (1 << 29)) { /* HID2.PSE */
Alexander Graf's avatar
Alexander Graf committed
410
411
412
413
414
415
416
417
				vcpu->arch.hflags |= BOOK3S_HFLAG_PAIRED_SINGLE;
				kvmppc_giveup_ext(vcpu, MSR_FP);
			} else {
				vcpu->arch.hflags &= ~BOOK3S_HFLAG_PAIRED_SINGLE;
			}
			break;
		}
		break;
418
	case SPRN_HID4:
Alexander Graf's avatar
Alexander Graf committed
419
	case SPRN_HID4_GEKKO:
420
		to_book3s(vcpu)->hid[4] = spr_val;
421
422
		break;
	case SPRN_HID5:
423
		to_book3s(vcpu)->hid[5] = spr_val;
424
425
426
427
428
		/* guest HID5 set can change is_dcbz32 */
		if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
		    (mfmsr() & MSR_HV))
			vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
		break;
429
430
431
432
433
434
	case SPRN_PURR:
		to_book3s(vcpu)->purr_offset = spr_val - get_tb();
		break;
	case SPRN_SPURR:
		to_book3s(vcpu)->spurr_offset = spr_val - get_tb();
		break;
Alexander Graf's avatar
Alexander Graf committed
435
436
437
438
439
440
441
442
443
444
	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;
445
446
447
448
449
450
	case SPRN_ICTC:
	case SPRN_THRM1:
	case SPRN_THRM2:
	case SPRN_THRM3:
	case SPRN_CTRLF:
	case SPRN_CTRLT:
Alexander Graf's avatar
Alexander Graf committed
451
	case SPRN_L2CR:
452
	case SPRN_DSCR:
Alexander Graf's avatar
Alexander Graf committed
453
454
455
456
457
458
459
	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:
460
	case SPRN_MSSSR0:
461
		break;
462
unprivileged:
463
464
465
466
467
468
469
470
471
472
473
	default:
		printk(KERN_INFO "KVM: invalid SPR write: %d\n", sprn);
#ifndef DEBUG_SPR
		emulated = EMULATE_FAIL;
#endif
		break;
	}

	return emulated;
}

474
int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
475
476
477
478
{
	int emulated = EMULATE_DONE;

	switch (sprn) {
Alexander Graf's avatar
Alexander Graf committed
479
480
481
482
	case SPRN_IBAT0U ... SPRN_IBAT3L:
	case SPRN_IBAT4U ... SPRN_IBAT7L:
	case SPRN_DBAT0U ... SPRN_DBAT3L:
	case SPRN_DBAT4U ... SPRN_DBAT7L:
483
484
485
486
	{
		struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);

		if (sprn % 2)
487
			*spr_val = bat->raw >> 32;
488
		else
489
			*spr_val = bat->raw;
490

Alexander Graf's avatar
Alexander Graf committed
491
		break;
492
	}
493
	case SPRN_SDR1:
494
495
		if (!spr_allowed(vcpu, PRIV_HYPER))
			goto unprivileged;
496
		*spr_val = to_book3s(vcpu)->sdr1;
497
498
		break;
	case SPRN_DSISR:
499
		*spr_val = vcpu->arch.shared->dsisr;
500
501
		break;
	case SPRN_DAR:
502
		*spr_val = vcpu->arch.shared->dar;
503
504
		break;
	case SPRN_HIOR:
505
		*spr_val = to_book3s(vcpu)->hior;
506
507
		break;
	case SPRN_HID0:
508
		*spr_val = to_book3s(vcpu)->hid[0];
509
510
		break;
	case SPRN_HID1:
511
		*spr_val = to_book3s(vcpu)->hid[1];
512
513
		break;
	case SPRN_HID2:
Alexander Graf's avatar
Alexander Graf committed
514
	case SPRN_HID2_GEKKO:
515
		*spr_val = to_book3s(vcpu)->hid[2];
516
517
		break;
	case SPRN_HID4:
Alexander Graf's avatar
Alexander Graf committed
518
	case SPRN_HID4_GEKKO:
519
		*spr_val = to_book3s(vcpu)->hid[4];
520
521
		break;
	case SPRN_HID5:
522
		*spr_val = to_book3s(vcpu)->hid[5];
523
		break;
524
	case SPRN_CFAR:
525
	case SPRN_DSCR:
526
		*spr_val = 0;
527
		break;
528
529
530
531
532
533
	case SPRN_PURR:
		*spr_val = get_tb() + to_book3s(vcpu)->purr_offset;
		break;
	case SPRN_SPURR:
		*spr_val = get_tb() + to_book3s(vcpu)->purr_offset;
		break;
Alexander Graf's avatar
Alexander Graf committed
534
535
536
537
538
539
540
541
	case SPRN_GQR0:
	case SPRN_GQR1:
	case SPRN_GQR2:
	case SPRN_GQR3:
	case SPRN_GQR4:
	case SPRN_GQR5:
	case SPRN_GQR6:
	case SPRN_GQR7:
542
		*spr_val = to_book3s(vcpu)->gqr[sprn - SPRN_GQR0];
Alexander Graf's avatar
Alexander Graf committed
543
		break;
544
545
546
547
548
	case SPRN_THRM1:
	case SPRN_THRM2:
	case SPRN_THRM3:
	case SPRN_CTRLF:
	case SPRN_CTRLT:
Alexander Graf's avatar
Alexander Graf committed
549
550
551
552
553
554
555
556
	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:
557
	case SPRN_MSSSR0:
558
		*spr_val = 0;
559
560
		break;
	default:
561
unprivileged:
562
563
564
565
566
567
568
569
570
571
		printk(KERN_INFO "KVM: invalid SPR read: %d\n", sprn);
#ifndef DEBUG_SPR
		emulated = EMULATE_FAIL;
#endif
		break;
	}

	return emulated;
}

572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst)
{
	u32 dsisr = 0;

	/*
	 * This is what the spec says about DSISR bits (not mentioned = 0):
	 *
	 * 12:13		[DS]	Set to bits 30:31
	 * 15:16		[X]	Set to bits 29:30
	 * 17			[X]	Set to bit 25
	 *			[D/DS]	Set to bit 5
	 * 18:21		[X]	Set to bits 21:24
	 *			[D/DS]	Set to bits 1:4
	 * 22:26			Set to bits 6:10 (RT/RS/FRT/FRS)
	 * 27:31			Set to bits 11:15 (RA)
	 */

	switch (get_op(inst)) {
	/* D-form */
	case OP_LFS:
	case OP_LFD:
	case OP_STFD:
	case OP_STFS:
		dsisr |= (inst >> 12) & 0x4000;	/* bit 17 */
		dsisr |= (inst >> 17) & 0x3c00; /* bits 18:21 */
		break;
	/* X-form */
	case 31:
		dsisr |= (inst << 14) & 0x18000; /* bits 15:16 */
		dsisr |= (inst << 8)  & 0x04000; /* bit 17 */
		dsisr |= (inst << 3)  & 0x03c00; /* bits 18:21 */
		break;
	default:
		printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
		break;
	}

	dsisr |= (inst >> 16) & 0x03ff; /* bits 22:31 */

	return dsisr;
}

ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst)
{
	ulong dar = 0;
617
618
	ulong ra = get_ra(inst);
	ulong rb = get_rb(inst);
619
620
621
622
623
624
625
626
627
628
629
630
631

	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);
632
		dar += kvmppc_get_gpr(vcpu, rb);
633
634
635
636
637
638
639
640
		break;
	default:
		printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
		break;
	}

	return dar;
}