blob: 9ef79c78ede9dbac55b7e255da586927e51eba66 [file] [log] [blame]
Hollis Blanchard75f74f02008-11-05 09:36:16 -06001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright IBM Corp. 2008
16 *
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 */
19
20#include <asm/kvm_ppc.h>
21#include <asm/dcr.h>
22#include <asm/dcr-regs.h>
23#include <asm/disassemble.h>
Hollis Blanchardfe4e7712008-11-10 14:57:36 -060024#include <asm/kvm_44x.h>
Hollis Blanchard75f74f02008-11-05 09:36:16 -060025
26#include "booke.h"
27#include "44x_tlb.h"
28
29#define OP_RFI 19
30
31#define XOP_RFI 50
32#define XOP_MFMSR 83
33#define XOP_WRTEE 131
34#define XOP_MTMSR 146
35#define XOP_WRTEEI 163
36#define XOP_MFDCR 323
37#define XOP_MTDCR 451
38#define XOP_TLBSX 914
39#define XOP_ICCCI 966
40#define XOP_TLBWE 978
41
Hollis Blanchard75f74f02008-11-05 09:36:16 -060042static void kvmppc_emul_rfi(struct kvm_vcpu *vcpu)
43{
44 vcpu->arch.pc = vcpu->arch.srr0;
45 kvmppc_set_msr(vcpu, vcpu->arch.srr1);
46}
47
48int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
49 unsigned int inst, int *advance)
50{
51 int emulated = EMULATE_DONE;
52 int dcrn;
53 int ra;
54 int rb;
55 int rc;
56 int rs;
57 int rt;
58 int ws;
59
60 switch (get_op(inst)) {
61
62 case OP_RFI:
63 switch (get_xop(inst)) {
64 case XOP_RFI:
65 kvmppc_emul_rfi(vcpu);
66 *advance = 0;
67 break;
68
69 default:
70 emulated = EMULATE_FAIL;
71 break;
72 }
73 break;
74
75 case 31:
76 switch (get_xop(inst)) {
77
78 case XOP_MFMSR:
79 rt = get_rt(inst);
80 vcpu->arch.gpr[rt] = vcpu->arch.msr;
81 break;
82
83 case XOP_MTMSR:
84 rs = get_rs(inst);
85 kvmppc_set_msr(vcpu, vcpu->arch.gpr[rs]);
86 break;
87
88 case XOP_WRTEE:
89 rs = get_rs(inst);
90 vcpu->arch.msr = (vcpu->arch.msr & ~MSR_EE)
91 | (vcpu->arch.gpr[rs] & MSR_EE);
92 break;
93
94 case XOP_WRTEEI:
95 vcpu->arch.msr = (vcpu->arch.msr & ~MSR_EE)
96 | (inst & MSR_EE);
97 break;
98
99 case XOP_MFDCR:
100 dcrn = get_dcrn(inst);
101 rt = get_rt(inst);
102
103 /* The guest may access CPR0 registers to determine the timebase
104 * frequency, and it must know the real host frequency because it
105 * can directly access the timebase registers.
106 *
107 * It would be possible to emulate those accesses in userspace,
108 * but userspace can really only figure out the end frequency.
109 * We could decompose that into the factors that compute it, but
110 * that's tricky math, and it's easier to just report the real
111 * CPR0 values.
112 */
113 switch (dcrn) {
114 case DCRN_CPR0_CONFIG_ADDR:
115 vcpu->arch.gpr[rt] = vcpu->arch.cpr0_cfgaddr;
116 break;
117 case DCRN_CPR0_CONFIG_DATA:
118 local_irq_disable();
119 mtdcr(DCRN_CPR0_CONFIG_ADDR,
120 vcpu->arch.cpr0_cfgaddr);
121 vcpu->arch.gpr[rt] = mfdcr(DCRN_CPR0_CONFIG_DATA);
122 local_irq_enable();
123 break;
124 default:
125 run->dcr.dcrn = dcrn;
126 run->dcr.data = 0;
127 run->dcr.is_write = 0;
128 vcpu->arch.io_gpr = rt;
129 vcpu->arch.dcr_needed = 1;
130 emulated = EMULATE_DO_DCR;
131 }
132
133 break;
134
135 case XOP_MTDCR:
136 dcrn = get_dcrn(inst);
137 rs = get_rs(inst);
138
139 /* emulate some access in kernel */
140 switch (dcrn) {
141 case DCRN_CPR0_CONFIG_ADDR:
142 vcpu->arch.cpr0_cfgaddr = vcpu->arch.gpr[rs];
143 break;
144 default:
145 run->dcr.dcrn = dcrn;
146 run->dcr.data = vcpu->arch.gpr[rs];
147 run->dcr.is_write = 1;
148 vcpu->arch.dcr_needed = 1;
149 emulated = EMULATE_DO_DCR;
150 }
151
152 break;
153
154 case XOP_TLBWE:
155 ra = get_ra(inst);
156 rs = get_rs(inst);
157 ws = get_ws(inst);
158 emulated = kvmppc_44x_emul_tlbwe(vcpu, ra, rs, ws);
159 break;
160
161 case XOP_TLBSX:
162 rt = get_rt(inst);
163 ra = get_ra(inst);
164 rb = get_rb(inst);
165 rc = get_rc(inst);
166 emulated = kvmppc_44x_emul_tlbsx(vcpu, rt, ra, rb, rc);
167 break;
168
169 case XOP_ICCCI:
170 break;
171
172 default:
173 emulated = EMULATE_FAIL;
174 }
175
176 break;
177
178 default:
179 emulated = EMULATE_FAIL;
180 }
181
182 return emulated;
183}
184
185int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
186{
187 switch (sprn) {
188 case SPRN_MMUCR:
189 vcpu->arch.mmucr = vcpu->arch.gpr[rs]; break;
190 case SPRN_PID:
191 kvmppc_set_pid(vcpu, vcpu->arch.gpr[rs]); break;
192 case SPRN_CCR0:
193 vcpu->arch.ccr0 = vcpu->arch.gpr[rs]; break;
194 case SPRN_CCR1:
195 vcpu->arch.ccr1 = vcpu->arch.gpr[rs]; break;
196 case SPRN_DEAR:
197 vcpu->arch.dear = vcpu->arch.gpr[rs]; break;
198 case SPRN_ESR:
199 vcpu->arch.esr = vcpu->arch.gpr[rs]; break;
200 case SPRN_DBCR0:
201 vcpu->arch.dbcr0 = vcpu->arch.gpr[rs]; break;
202 case SPRN_DBCR1:
203 vcpu->arch.dbcr1 = vcpu->arch.gpr[rs]; break;
204 case SPRN_TSR:
205 vcpu->arch.tsr &= ~vcpu->arch.gpr[rs]; break;
206 case SPRN_TCR:
207 vcpu->arch.tcr = vcpu->arch.gpr[rs];
208 kvmppc_emulate_dec(vcpu);
209 break;
210
211 /* Note: SPRG4-7 are user-readable. These values are
212 * loaded into the real SPRGs when resuming the
213 * guest. */
214 case SPRN_SPRG4:
215 vcpu->arch.sprg4 = vcpu->arch.gpr[rs]; break;
216 case SPRN_SPRG5:
217 vcpu->arch.sprg5 = vcpu->arch.gpr[rs]; break;
218 case SPRN_SPRG6:
219 vcpu->arch.sprg6 = vcpu->arch.gpr[rs]; break;
220 case SPRN_SPRG7:
221 vcpu->arch.sprg7 = vcpu->arch.gpr[rs]; break;
222
223 case SPRN_IVPR:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600224 vcpu->arch.ivpr = vcpu->arch.gpr[rs];
225 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600226 case SPRN_IVOR0:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600227 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = vcpu->arch.gpr[rs];
228 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600229 case SPRN_IVOR1:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600230 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = vcpu->arch.gpr[rs];
231 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600232 case SPRN_IVOR2:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600233 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = vcpu->arch.gpr[rs];
234 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600235 case SPRN_IVOR3:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600236 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = vcpu->arch.gpr[rs];
237 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600238 case SPRN_IVOR4:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600239 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = vcpu->arch.gpr[rs];
240 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600241 case SPRN_IVOR5:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600242 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = vcpu->arch.gpr[rs];
243 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600244 case SPRN_IVOR6:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600245 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = vcpu->arch.gpr[rs];
246 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600247 case SPRN_IVOR7:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600248 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = vcpu->arch.gpr[rs];
249 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600250 case SPRN_IVOR8:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600251 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = vcpu->arch.gpr[rs];
252 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600253 case SPRN_IVOR9:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600254 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = vcpu->arch.gpr[rs];
255 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600256 case SPRN_IVOR10:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600257 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = vcpu->arch.gpr[rs];
258 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600259 case SPRN_IVOR11:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600260 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = vcpu->arch.gpr[rs];
261 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600262 case SPRN_IVOR12:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600263 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = vcpu->arch.gpr[rs];
264 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600265 case SPRN_IVOR13:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600266 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = vcpu->arch.gpr[rs];
267 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600268 case SPRN_IVOR14:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600269 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = vcpu->arch.gpr[rs];
270 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600271 case SPRN_IVOR15:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600272 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = vcpu->arch.gpr[rs];
273 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600274
275 default:
276 return EMULATE_FAIL;
277 }
278
279 return EMULATE_DONE;
280}
281
282int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
283{
284 switch (sprn) {
285 /* 440 */
286 case SPRN_MMUCR:
287 vcpu->arch.gpr[rt] = vcpu->arch.mmucr; break;
288 case SPRN_CCR0:
289 vcpu->arch.gpr[rt] = vcpu->arch.ccr0; break;
290 case SPRN_CCR1:
291 vcpu->arch.gpr[rt] = vcpu->arch.ccr1; break;
292
293 /* Book E */
294 case SPRN_PID:
295 vcpu->arch.gpr[rt] = vcpu->arch.pid; break;
296 case SPRN_IVPR:
297 vcpu->arch.gpr[rt] = vcpu->arch.ivpr; break;
298 case SPRN_DEAR:
299 vcpu->arch.gpr[rt] = vcpu->arch.dear; break;
300 case SPRN_ESR:
301 vcpu->arch.gpr[rt] = vcpu->arch.esr; break;
302 case SPRN_DBCR0:
303 vcpu->arch.gpr[rt] = vcpu->arch.dbcr0; break;
304 case SPRN_DBCR1:
305 vcpu->arch.gpr[rt] = vcpu->arch.dbcr1; break;
306
307 case SPRN_IVOR0:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600308 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
309 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600310 case SPRN_IVOR1:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600311 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
312 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600313 case SPRN_IVOR2:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600314 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
315 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600316 case SPRN_IVOR3:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600317 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
318 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600319 case SPRN_IVOR4:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600320 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
321 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600322 case SPRN_IVOR5:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600323 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
324 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600325 case SPRN_IVOR6:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600326 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
327 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600328 case SPRN_IVOR7:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600329 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
330 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600331 case SPRN_IVOR8:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600332 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
333 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600334 case SPRN_IVOR9:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600335 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
336 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600337 case SPRN_IVOR10:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600338 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
339 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600340 case SPRN_IVOR11:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600341 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
342 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600343 case SPRN_IVOR12:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600344 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
345 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600346 case SPRN_IVOR13:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600347 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
348 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600349 case SPRN_IVOR14:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600350 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
351 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600352 case SPRN_IVOR15:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600353 vcpu->arch.gpr[rt] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
354 break;
355
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600356 default:
357 return EMULATE_FAIL;
358 }
359
360 return EMULATE_DONE;
361}
362