KVM: PPC: Emulator: clean up SPR reads and writes
[linux-3.10.git] / arch / powerpc / kvm / booke_emulate.c
1 /*
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  * Copyright 2011 Freescale Semiconductor, Inc.
17  *
18  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19  */
20
21 #include <linux/kvm_host.h>
22 #include <asm/disassemble.h>
23
24 #include "booke.h"
25
26 #define OP_19_XOP_RFI     50
27
28 #define OP_31_XOP_MFMSR   83
29 #define OP_31_XOP_WRTEE   131
30 #define OP_31_XOP_MTMSR   146
31 #define OP_31_XOP_WRTEEI  163
32
33 static void kvmppc_emul_rfi(struct kvm_vcpu *vcpu)
34 {
35         vcpu->arch.pc = vcpu->arch.shared->srr0;
36         kvmppc_set_msr(vcpu, vcpu->arch.shared->srr1);
37 }
38
39 int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
40                             unsigned int inst, int *advance)
41 {
42         int emulated = EMULATE_DONE;
43         int rs = get_rs(inst);
44         int rt = get_rt(inst);
45
46         switch (get_op(inst)) {
47         case 19:
48                 switch (get_xop(inst)) {
49                 case OP_19_XOP_RFI:
50                         kvmppc_emul_rfi(vcpu);
51                         kvmppc_set_exit_type(vcpu, EMULATED_RFI_EXITS);
52                         *advance = 0;
53                         break;
54
55                 default:
56                         emulated = EMULATE_FAIL;
57                         break;
58                 }
59                 break;
60
61         case 31:
62                 switch (get_xop(inst)) {
63
64                 case OP_31_XOP_MFMSR:
65                         kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->msr);
66                         kvmppc_set_exit_type(vcpu, EMULATED_MFMSR_EXITS);
67                         break;
68
69                 case OP_31_XOP_MTMSR:
70                         kvmppc_set_exit_type(vcpu, EMULATED_MTMSR_EXITS);
71                         kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
72                         break;
73
74                 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                         kvmppc_set_exit_type(vcpu, EMULATED_WRTEE_EXITS);
78                         break;
79
80                 case OP_31_XOP_WRTEEI:
81                         vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE)
82                                                          | (inst & MSR_EE);
83                         kvmppc_set_exit_type(vcpu, EMULATED_WRTEE_EXITS);
84                         break;
85
86                 default:
87                         emulated = EMULATE_FAIL;
88                 }
89
90                 break;
91
92         default:
93                 emulated = EMULATE_FAIL;
94         }
95
96         return emulated;
97 }
98
99 /*
100  * NOTE: some of these registers are not emulated on BOOKE_HV (GS-mode).
101  * Their backing store is in real registers, and these functions
102  * will return the wrong result if called for them in another context
103  * (such as debugging).
104  */
105 int kvmppc_booke_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
106 {
107         int emulated = EMULATE_DONE;
108
109         switch (sprn) {
110         case SPRN_DEAR:
111                 vcpu->arch.shared->dar = spr_val;
112                 break;
113         case SPRN_ESR:
114                 vcpu->arch.shared->esr = spr_val;
115                 break;
116         case SPRN_DBCR0:
117                 vcpu->arch.dbcr0 = spr_val;
118                 break;
119         case SPRN_DBCR1:
120                 vcpu->arch.dbcr1 = spr_val;
121                 break;
122         case SPRN_DBSR:
123                 vcpu->arch.dbsr &= ~spr_val;
124                 break;
125         case SPRN_TSR:
126                 kvmppc_clr_tsr_bits(vcpu, spr_val);
127                 break;
128         case SPRN_TCR:
129                 kvmppc_set_tcr(vcpu, spr_val);
130                 break;
131
132         /*
133          * Note: SPRG4-7 are user-readable.
134          * These values are loaded into the real SPRGs when resuming the
135          * guest (PR-mode only).
136          */
137         case SPRN_SPRG4:
138                 vcpu->arch.shared->sprg4 = spr_val;
139                 break;
140         case SPRN_SPRG5:
141                 vcpu->arch.shared->sprg5 = spr_val;
142                 break;
143         case SPRN_SPRG6:
144                 vcpu->arch.shared->sprg6 = spr_val;
145                 break;
146         case SPRN_SPRG7:
147                 vcpu->arch.shared->sprg7 = spr_val;
148                 break;
149
150         case SPRN_IVPR:
151                 vcpu->arch.ivpr = spr_val;
152 #ifdef CONFIG_KVM_BOOKE_HV
153                 mtspr(SPRN_GIVPR, spr_val);
154 #endif
155                 break;
156         case SPRN_IVOR0:
157                 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = spr_val;
158                 break;
159         case SPRN_IVOR1:
160                 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = spr_val;
161                 break;
162         case SPRN_IVOR2:
163                 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = spr_val;
164 #ifdef CONFIG_KVM_BOOKE_HV
165                 mtspr(SPRN_GIVOR2, spr_val);
166 #endif
167                 break;
168         case SPRN_IVOR3:
169                 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = spr_val;
170                 break;
171         case SPRN_IVOR4:
172                 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = spr_val;
173                 break;
174         case SPRN_IVOR5:
175                 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = spr_val;
176                 break;
177         case SPRN_IVOR6:
178                 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = spr_val;
179                 break;
180         case SPRN_IVOR7:
181                 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = spr_val;
182                 break;
183         case SPRN_IVOR8:
184                 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = spr_val;
185 #ifdef CONFIG_KVM_BOOKE_HV
186                 mtspr(SPRN_GIVOR8, spr_val);
187 #endif
188                 break;
189         case SPRN_IVOR9:
190                 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = spr_val;
191                 break;
192         case SPRN_IVOR10:
193                 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = spr_val;
194                 break;
195         case SPRN_IVOR11:
196                 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = spr_val;
197                 break;
198         case SPRN_IVOR12:
199                 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = spr_val;
200                 break;
201         case SPRN_IVOR13:
202                 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = spr_val;
203                 break;
204         case SPRN_IVOR14:
205                 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = spr_val;
206                 break;
207         case SPRN_IVOR15:
208                 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = spr_val;
209                 break;
210
211         default:
212                 emulated = EMULATE_FAIL;
213         }
214
215         return emulated;
216 }
217
218 int kvmppc_booke_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
219 {
220         int emulated = EMULATE_DONE;
221
222         switch (sprn) {
223         case SPRN_IVPR:
224                 *spr_val = vcpu->arch.ivpr;
225                 break;
226         case SPRN_DEAR:
227                 *spr_val = vcpu->arch.shared->dar;
228                 break;
229         case SPRN_ESR:
230                 *spr_val = vcpu->arch.shared->esr;
231                 break;
232         case SPRN_DBCR0:
233                 *spr_val = vcpu->arch.dbcr0;
234                 break;
235         case SPRN_DBCR1:
236                 *spr_val = vcpu->arch.dbcr1;
237                 break;
238         case SPRN_DBSR:
239                 *spr_val = vcpu->arch.dbsr;
240                 break;
241         case SPRN_TSR:
242                 *spr_val = vcpu->arch.tsr;
243                 break;
244         case SPRN_TCR:
245                 *spr_val = vcpu->arch.tcr;
246                 break;
247
248         case SPRN_IVOR0:
249                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
250                 break;
251         case SPRN_IVOR1:
252                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
253                 break;
254         case SPRN_IVOR2:
255                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
256                 break;
257         case SPRN_IVOR3:
258                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
259                 break;
260         case SPRN_IVOR4:
261                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
262                 break;
263         case SPRN_IVOR5:
264                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
265                 break;
266         case SPRN_IVOR6:
267                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
268                 break;
269         case SPRN_IVOR7:
270                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
271                 break;
272         case SPRN_IVOR8:
273                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
274                 break;
275         case SPRN_IVOR9:
276                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
277                 break;
278         case SPRN_IVOR10:
279                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
280                 break;
281         case SPRN_IVOR11:
282                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
283                 break;
284         case SPRN_IVOR12:
285                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
286                 break;
287         case SPRN_IVOR13:
288                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
289                 break;
290         case SPRN_IVOR14:
291                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
292                 break;
293         case SPRN_IVOR15:
294                 *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
295                 break;
296
297         default:
298                 emulated = EMULATE_FAIL;
299         }
300
301         return emulated;
302 }