booke_emulate.c 6.8 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 24 25 26 27 28 29 30 31 32 33
/*
 * 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
 *
 * 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)
{
34 35
	vcpu->arch.pc = vcpu->arch.shared->srr0;
	kvmppc_set_msr(vcpu, vcpu->arch.shared->srr1);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
}

int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
                            unsigned int inst, int *advance)
{
	int emulated = EMULATE_DONE;
	int rs;
	int rt;

	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:
			rt = get_rt(inst);
65
			kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->msr);
66 67 68 69 70 71
			kvmppc_set_exit_type(vcpu, EMULATED_MFMSR_EXITS);
			break;

		case OP_31_XOP_MTMSR:
			rs = get_rs(inst);
			kvmppc_set_exit_type(vcpu, EMULATED_MTMSR_EXITS);
72
			kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
73 74 75 76
			break;

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

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

		default:
			emulated = EMULATE_FAIL;
		}

		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}

int kvmppc_booke_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
{
	int emulated = EMULATE_DONE;
104
	ulong spr_val = kvmppc_get_gpr(vcpu, rs);
105 106 107

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

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

	case SPRN_IVPR:
137
		vcpu->arch.ivpr = spr_val;
138 139
		break;
	case SPRN_IVOR0:
140
		vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = spr_val;
141 142
		break;
	case SPRN_IVOR1:
143
		vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = spr_val;
144 145
		break;
	case SPRN_IVOR2:
146
		vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = spr_val;
147 148
		break;
	case SPRN_IVOR3:
149
		vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = spr_val;
150 151
		break;
	case SPRN_IVOR4:
152
		vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = spr_val;
153 154
		break;
	case SPRN_IVOR5:
155
		vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = spr_val;
156 157
		break;
	case SPRN_IVOR6:
158
		vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = spr_val;
159 160
		break;
	case SPRN_IVOR7:
161
		vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = spr_val;
162 163
		break;
	case SPRN_IVOR8:
164
		vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = spr_val;
165 166
		break;
	case SPRN_IVOR9:
167
		vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = spr_val;
168 169
		break;
	case SPRN_IVOR10:
170
		vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = spr_val;
171 172
		break;
	case SPRN_IVOR11:
173
		vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = spr_val;
174 175
		break;
	case SPRN_IVOR12:
176
		vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = spr_val;
177 178
		break;
	case SPRN_IVOR13:
179
		vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = spr_val;
180 181
		break;
	case SPRN_IVOR14:
182
		vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = spr_val;
183 184
		break;
	case SPRN_IVOR15:
185
		vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = spr_val;
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
		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:
201
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivpr); break;
202
	case SPRN_DEAR:
203
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->dar); break;
204
	case SPRN_ESR:
205
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->esr); break;
206
	case SPRN_DBCR0:
207
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.dbcr0); break;
208
	case SPRN_DBCR1:
209
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.dbcr1); break;
210
	case SPRN_DBSR:
211
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.dbsr); break;
212 213

	case SPRN_IVOR0:
214
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL]);
215 216
		break;
	case SPRN_IVOR1:
217
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK]);
218 219
		break;
	case SPRN_IVOR2:
220
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]);
221 222
		break;
	case SPRN_IVOR3:
223
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE]);
224 225
		break;
	case SPRN_IVOR4:
226
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL]);
227 228
		break;
	case SPRN_IVOR5:
229
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT]);
230 231
		break;
	case SPRN_IVOR6:
232
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM]);
233 234
		break;
	case SPRN_IVOR7:
235
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL]);
236 237
		break;
	case SPRN_IVOR8:
238
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]);
239 240
		break;
	case SPRN_IVOR9:
241
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL]);
242 243
		break;
	case SPRN_IVOR10:
244
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER]);
245 246
		break;
	case SPRN_IVOR11:
247
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_FIT]);
248 249
		break;
	case SPRN_IVOR12:
250
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG]);
251 252
		break;
	case SPRN_IVOR13:
253
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS]);
254 255
		break;
	case SPRN_IVOR14:
256
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS]);
257 258
		break;
	case SPRN_IVOR15:
259
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG]);
260 261 262 263 264 265 266 267
		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}