booke_emulate.c 7.4 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. 2008
16
 * Copyright 2011 Freescale Semiconductor, Inc.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
 */

#include <linux/kvm_host.h>
#include <asm/disassemble.h>

#include "booke.h"

#define OP_19_XOP_RFI     50

#define OP_31_XOP_MFMSR   83
#define OP_31_XOP_WRTEE   131
#define OP_31_XOP_MTMSR   146
#define OP_31_XOP_WRTEEI  163

static void kvmppc_emul_rfi(struct kvm_vcpu *vcpu)
{
35 36
	vcpu->arch.pc = vcpu->arch.shared->srr0;
	kvmppc_set_msr(vcpu, vcpu->arch.shared->srr1);
37 38 39 40 41 42
}

int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
                            unsigned int inst, int *advance)
{
	int emulated = EMULATE_DONE;
43 44
	int rs = get_rs(inst);
	int rt = get_rt(inst);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

	switch (get_op(inst)) {
	case 19:
		switch (get_xop(inst)) {
		case OP_19_XOP_RFI:
			kvmppc_emul_rfi(vcpu);
			kvmppc_set_exit_type(vcpu, EMULATED_RFI_EXITS);
			*advance = 0;
			break;

		default:
			emulated = EMULATE_FAIL;
			break;
		}
		break;

	case 31:
		switch (get_xop(inst)) {

		case OP_31_XOP_MFMSR:
65
			kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->msr);
66 67 68 69 70
			kvmppc_set_exit_type(vcpu, EMULATED_MFMSR_EXITS);
			break;

		case OP_31_XOP_MTMSR:
			kvmppc_set_exit_type(vcpu, EMULATED_MTMSR_EXITS);
71
			kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
72 73 74
			break;

		case OP_31_XOP_WRTEE:
75
			vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE)
76
					| (kvmppc_get_gpr(vcpu, rs) & MSR_EE);
77 78 79 80
			kvmppc_set_exit_type(vcpu, EMULATED_WRTEE_EXITS);
			break;

		case OP_31_XOP_WRTEEI:
81
			vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
							 | (inst & MSR_EE);
			kvmppc_set_exit_type(vcpu, EMULATED_WRTEE_EXITS);
			break;

		default:
			emulated = EMULATE_FAIL;
		}

		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}

99 100 101 102 103 104
/*
 * NOTE: some of these registers are not emulated on BOOKE_HV (GS-mode).
 * Their backing store is in real registers, and these functions
 * will return the wrong result if called for them in another context
 * (such as debugging).
 */
105 106 107
int kvmppc_booke_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
{
	int emulated = EMULATE_DONE;
108
	ulong spr_val = kvmppc_get_gpr(vcpu, rs);
109 110 111

	switch (sprn) {
	case SPRN_DEAR:
112
		vcpu->arch.shared->dar = spr_val; break;
113
	case SPRN_ESR:
114
		vcpu->arch.shared->esr = spr_val; break;
115
	case SPRN_DBCR0:
116
		vcpu->arch.dbcr0 = spr_val; break;
117
	case SPRN_DBCR1:
118
		vcpu->arch.dbcr1 = spr_val; break;
119
	case SPRN_DBSR:
120
		vcpu->arch.dbsr &= ~spr_val; break;
121
	case SPRN_TSR:
122 123
		kvmppc_clr_tsr_bits(vcpu, spr_val);
		break;
124
	case SPRN_TCR:
125
		kvmppc_set_tcr(vcpu, spr_val);
126 127
		break;

128 129 130 131 132
	/*
	 * Note: SPRG4-7 are user-readable.
	 * These values are loaded into the real SPRGs when resuming the
	 * guest (PR-mode only).
	 */
133
	case SPRN_SPRG4:
134
		vcpu->arch.shared->sprg4 = spr_val; break;
135
	case SPRN_SPRG5:
136
		vcpu->arch.shared->sprg5 = spr_val; break;
137
	case SPRN_SPRG6:
138
		vcpu->arch.shared->sprg6 = spr_val; break;
139
	case SPRN_SPRG7:
140
		vcpu->arch.shared->sprg7 = spr_val; break;
141 142

	case SPRN_IVPR:
143
		vcpu->arch.ivpr = spr_val;
144 145 146
#ifdef CONFIG_KVM_BOOKE_HV
		mtspr(SPRN_GIVPR, spr_val);
#endif
147 148
		break;
	case SPRN_IVOR0:
149
		vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = spr_val;
150 151
		break;
	case SPRN_IVOR1:
152
		vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = spr_val;
153 154
		break;
	case SPRN_IVOR2:
155
		vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = spr_val;
156 157 158
#ifdef CONFIG_KVM_BOOKE_HV
		mtspr(SPRN_GIVOR2, spr_val);
#endif
159 160
		break;
	case SPRN_IVOR3:
161
		vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = spr_val;
162 163
		break;
	case SPRN_IVOR4:
164
		vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = spr_val;
165 166
		break;
	case SPRN_IVOR5:
167
		vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = spr_val;
168 169
		break;
	case SPRN_IVOR6:
170
		vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = spr_val;
171 172
		break;
	case SPRN_IVOR7:
173
		vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = spr_val;
174 175
		break;
	case SPRN_IVOR8:
176
		vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = spr_val;
177 178 179
#ifdef CONFIG_KVM_BOOKE_HV
		mtspr(SPRN_GIVOR8, spr_val);
#endif
180 181
		break;
	case SPRN_IVOR9:
182
		vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = spr_val;
183 184
		break;
	case SPRN_IVOR10:
185
		vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = spr_val;
186 187
		break;
	case SPRN_IVOR11:
188
		vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = spr_val;
189 190
		break;
	case SPRN_IVOR12:
191
		vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = spr_val;
192 193
		break;
	case SPRN_IVOR13:
194
		vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = spr_val;
195 196
		break;
	case SPRN_IVOR14:
197
		vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = spr_val;
198 199
		break;
	case SPRN_IVOR15:
200
		vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = spr_val;
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}

int kvmppc_booke_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
{
	int emulated = EMULATE_DONE;

	switch (sprn) {
	case SPRN_IVPR:
216
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivpr); break;
217
	case SPRN_DEAR:
218
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->dar); break;
219
	case SPRN_ESR:
220
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->esr); break;
221
	case SPRN_DBCR0:
222
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.dbcr0); break;
223
	case SPRN_DBCR1:
224
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.dbcr1); break;
225
	case SPRN_DBSR:
226
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.dbsr); break;
227 228 229 230
	case SPRN_TSR:
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.tsr); break;
	case SPRN_TCR:
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.tcr); break;
231 232

	case SPRN_IVOR0:
233
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL]);
234 235
		break;
	case SPRN_IVOR1:
236
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK]);
237 238
		break;
	case SPRN_IVOR2:
239
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]);
240 241
		break;
	case SPRN_IVOR3:
242
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE]);
243 244
		break;
	case SPRN_IVOR4:
245
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL]);
246 247
		break;
	case SPRN_IVOR5:
248
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT]);
249 250
		break;
	case SPRN_IVOR6:
251
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM]);
252 253
		break;
	case SPRN_IVOR7:
254
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL]);
255 256
		break;
	case SPRN_IVOR8:
257
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]);
258 259
		break;
	case SPRN_IVOR9:
260
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL]);
261 262
		break;
	case SPRN_IVOR10:
263
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER]);
264 265
		break;
	case SPRN_IVOR11:
266
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_FIT]);
267 268
		break;
	case SPRN_IVOR12:
269
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG]);
270 271
		break;
	case SPRN_IVOR13:
272
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS]);
273 274
		break;
	case SPRN_IVOR14:
275
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS]);
276 277
		break;
	case SPRN_IVOR15:
278
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG]);
279 280 281 282 283 284 285 286
		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}