KVM: ppc: e500: Add MMUCFG and PVR emulation
[linux-3.10.git] / arch / powerpc / kvm / 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. 2007
16  *
17  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18  */
19
20 #include <linux/jiffies.h>
21 #include <linux/timer.h>
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/kvm_host.h>
25
26 #include <asm/reg.h>
27 #include <asm/time.h>
28 #include <asm/byteorder.h>
29 #include <asm/kvm_ppc.h>
30 #include <asm/disassemble.h>
31 #include "timing.h"
32
33 #define OP_TRAP 3
34
35 #define OP_31_XOP_LWZX      23
36 #define OP_31_XOP_LBZX      87
37 #define OP_31_XOP_STWX      151
38 #define OP_31_XOP_STBX      215
39 #define OP_31_XOP_STBUX     247
40 #define OP_31_XOP_LHZX      279
41 #define OP_31_XOP_LHZUX     311
42 #define OP_31_XOP_MFSPR     339
43 #define OP_31_XOP_STHX      407
44 #define OP_31_XOP_STHUX     439
45 #define OP_31_XOP_MTSPR     467
46 #define OP_31_XOP_DCBI      470
47 #define OP_31_XOP_LWBRX     534
48 #define OP_31_XOP_TLBSYNC   566
49 #define OP_31_XOP_STWBRX    662
50 #define OP_31_XOP_LHBRX     790
51 #define OP_31_XOP_STHBRX    918
52
53 #define OP_LWZ  32
54 #define OP_LWZU 33
55 #define OP_LBZ  34
56 #define OP_LBZU 35
57 #define OP_STW  36
58 #define OP_STWU 37
59 #define OP_STB  38
60 #define OP_STBU 39
61 #define OP_LHZ  40
62 #define OP_LHZU 41
63 #define OP_STH  44
64 #define OP_STHU 45
65
66 void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
67 {
68         if (vcpu->arch.tcr & TCR_DIE) {
69                 /* The decrementer ticks at the same rate as the timebase, so
70                  * that's how we convert the guest DEC value to the number of
71                  * host ticks. */
72                 unsigned long nr_jiffies;
73
74                 nr_jiffies = vcpu->arch.dec / tb_ticks_per_jiffy;
75                 mod_timer(&vcpu->arch.dec_timer,
76                           get_jiffies_64() + nr_jiffies);
77         } else {
78                 del_timer(&vcpu->arch.dec_timer);
79         }
80 }
81
82 /* XXX to do:
83  * lhax
84  * lhaux
85  * lswx
86  * lswi
87  * stswx
88  * stswi
89  * lha
90  * lhau
91  * lmw
92  * stmw
93  *
94  * XXX is_bigendian should depend on MMU mapping or MSR[LE]
95  */
96 /* XXX Should probably auto-generate instruction decoding for a particular core
97  * from opcode tables in the future. */
98 int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
99 {
100         u32 inst = vcpu->arch.last_inst;
101         u32 ea;
102         int ra;
103         int rb;
104         int rs;
105         int rt;
106         int sprn;
107         enum emulation_result emulated = EMULATE_DONE;
108         int advance = 1;
109
110         /* this default type might be overwritten by subcategories */
111         kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
112
113         switch (get_op(inst)) {
114         case OP_TRAP:
115                 vcpu->arch.esr |= ESR_PTR;
116                 kvmppc_core_queue_program(vcpu);
117                 advance = 0;
118                 break;
119
120         case 31:
121                 switch (get_xop(inst)) {
122
123                 case OP_31_XOP_LWZX:
124                         rt = get_rt(inst);
125                         emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
126                         break;
127
128                 case OP_31_XOP_LBZX:
129                         rt = get_rt(inst);
130                         emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
131                         break;
132
133                 case OP_31_XOP_STWX:
134                         rs = get_rs(inst);
135                         emulated = kvmppc_handle_store(run, vcpu,
136                                                        vcpu->arch.gpr[rs],
137                                                        4, 1);
138                         break;
139
140                 case OP_31_XOP_STBX:
141                         rs = get_rs(inst);
142                         emulated = kvmppc_handle_store(run, vcpu,
143                                                        vcpu->arch.gpr[rs],
144                                                        1, 1);
145                         break;
146
147                 case OP_31_XOP_STBUX:
148                         rs = get_rs(inst);
149                         ra = get_ra(inst);
150                         rb = get_rb(inst);
151
152                         ea = vcpu->arch.gpr[rb];
153                         if (ra)
154                                 ea += vcpu->arch.gpr[ra];
155
156                         emulated = kvmppc_handle_store(run, vcpu,
157                                                        vcpu->arch.gpr[rs],
158                                                        1, 1);
159                         vcpu->arch.gpr[rs] = ea;
160                         break;
161
162                 case OP_31_XOP_LHZX:
163                         rt = get_rt(inst);
164                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
165                         break;
166
167                 case OP_31_XOP_LHZUX:
168                         rt = get_rt(inst);
169                         ra = get_ra(inst);
170                         rb = get_rb(inst);
171
172                         ea = vcpu->arch.gpr[rb];
173                         if (ra)
174                                 ea += vcpu->arch.gpr[ra];
175
176                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
177                         vcpu->arch.gpr[ra] = ea;
178                         break;
179
180                 case OP_31_XOP_MFSPR:
181                         sprn = get_sprn(inst);
182                         rt = get_rt(inst);
183
184                         switch (sprn) {
185                         case SPRN_SRR0:
186                                 vcpu->arch.gpr[rt] = vcpu->arch.srr0; break;
187                         case SPRN_SRR1:
188                                 vcpu->arch.gpr[rt] = vcpu->arch.srr1; break;
189                         case SPRN_PVR:
190                                 vcpu->arch.gpr[rt] = mfspr(SPRN_PVR); break;
191                         case SPRN_PIR:
192                                 vcpu->arch.gpr[rt] = mfspr(SPRN_PIR); break;
193
194                         /* Note: mftb and TBRL/TBWL are user-accessible, so
195                          * the guest can always access the real TB anyways.
196                          * In fact, we probably will never see these traps. */
197                         case SPRN_TBWL:
198                                 vcpu->arch.gpr[rt] = mftbl(); break;
199                         case SPRN_TBWU:
200                                 vcpu->arch.gpr[rt] = mftbu(); break;
201
202                         case SPRN_SPRG0:
203                                 vcpu->arch.gpr[rt] = vcpu->arch.sprg0; break;
204                         case SPRN_SPRG1:
205                                 vcpu->arch.gpr[rt] = vcpu->arch.sprg1; break;
206                         case SPRN_SPRG2:
207                                 vcpu->arch.gpr[rt] = vcpu->arch.sprg2; break;
208                         case SPRN_SPRG3:
209                                 vcpu->arch.gpr[rt] = vcpu->arch.sprg3; break;
210                         /* Note: SPRG4-7 are user-readable, so we don't get
211                          * a trap. */
212
213                         default:
214                                 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
215                                 if (emulated == EMULATE_FAIL) {
216                                         printk("mfspr: unknown spr %x\n", sprn);
217                                         vcpu->arch.gpr[rt] = 0;
218                                 }
219                                 break;
220                         }
221                         break;
222
223                 case OP_31_XOP_STHX:
224                         rs = get_rs(inst);
225                         ra = get_ra(inst);
226                         rb = get_rb(inst);
227
228                         emulated = kvmppc_handle_store(run, vcpu,
229                                                        vcpu->arch.gpr[rs],
230                                                        2, 1);
231                         break;
232
233                 case OP_31_XOP_STHUX:
234                         rs = get_rs(inst);
235                         ra = get_ra(inst);
236                         rb = get_rb(inst);
237
238                         ea = vcpu->arch.gpr[rb];
239                         if (ra)
240                                 ea += vcpu->arch.gpr[ra];
241
242                         emulated = kvmppc_handle_store(run, vcpu,
243                                                        vcpu->arch.gpr[rs],
244                                                        2, 1);
245                         vcpu->arch.gpr[ra] = ea;
246                         break;
247
248                 case OP_31_XOP_MTSPR:
249                         sprn = get_sprn(inst);
250                         rs = get_rs(inst);
251                         switch (sprn) {
252                         case SPRN_SRR0:
253                                 vcpu->arch.srr0 = vcpu->arch.gpr[rs]; break;
254                         case SPRN_SRR1:
255                                 vcpu->arch.srr1 = vcpu->arch.gpr[rs]; break;
256
257                         /* XXX We need to context-switch the timebase for
258                          * watchdog and FIT. */
259                         case SPRN_TBWL: break;
260                         case SPRN_TBWU: break;
261
262                         case SPRN_DEC:
263                                 vcpu->arch.dec = vcpu->arch.gpr[rs];
264                                 kvmppc_emulate_dec(vcpu);
265                                 break;
266
267                         case SPRN_SPRG0:
268                                 vcpu->arch.sprg0 = vcpu->arch.gpr[rs]; break;
269                         case SPRN_SPRG1:
270                                 vcpu->arch.sprg1 = vcpu->arch.gpr[rs]; break;
271                         case SPRN_SPRG2:
272                                 vcpu->arch.sprg2 = vcpu->arch.gpr[rs]; break;
273                         case SPRN_SPRG3:
274                                 vcpu->arch.sprg3 = vcpu->arch.gpr[rs]; break;
275
276                         default:
277                                 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
278                                 if (emulated == EMULATE_FAIL)
279                                         printk("mtspr: unknown spr %x\n", sprn);
280                                 break;
281                         }
282                         break;
283
284                 case OP_31_XOP_DCBI:
285                         /* Do nothing. The guest is performing dcbi because
286                          * hardware DMA is not snooped by the dcache, but
287                          * emulated DMA either goes through the dcache as
288                          * normal writes, or the host kernel has handled dcache
289                          * coherence. */
290                         break;
291
292                 case OP_31_XOP_LWBRX:
293                         rt = get_rt(inst);
294                         emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
295                         break;
296
297                 case OP_31_XOP_TLBSYNC:
298                         break;
299
300                 case OP_31_XOP_STWBRX:
301                         rs = get_rs(inst);
302                         ra = get_ra(inst);
303                         rb = get_rb(inst);
304
305                         emulated = kvmppc_handle_store(run, vcpu,
306                                                        vcpu->arch.gpr[rs],
307                                                        4, 0);
308                         break;
309
310                 case OP_31_XOP_LHBRX:
311                         rt = get_rt(inst);
312                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
313                         break;
314
315                 case OP_31_XOP_STHBRX:
316                         rs = get_rs(inst);
317                         ra = get_ra(inst);
318                         rb = get_rb(inst);
319
320                         emulated = kvmppc_handle_store(run, vcpu,
321                                                        vcpu->arch.gpr[rs],
322                                                        2, 0);
323                         break;
324
325                 default:
326                         /* Attempt core-specific emulation below. */
327                         emulated = EMULATE_FAIL;
328                 }
329                 break;
330
331         case OP_LWZ:
332                 rt = get_rt(inst);
333                 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
334                 break;
335
336         case OP_LWZU:
337                 ra = get_ra(inst);
338                 rt = get_rt(inst);
339                 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
340                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
341                 break;
342
343         case OP_LBZ:
344                 rt = get_rt(inst);
345                 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
346                 break;
347
348         case OP_LBZU:
349                 ra = get_ra(inst);
350                 rt = get_rt(inst);
351                 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
352                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
353                 break;
354
355         case OP_STW:
356                 rs = get_rs(inst);
357                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
358                                                4, 1);
359                 break;
360
361         case OP_STWU:
362                 ra = get_ra(inst);
363                 rs = get_rs(inst);
364                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
365                                                4, 1);
366                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
367                 break;
368
369         case OP_STB:
370                 rs = get_rs(inst);
371                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
372                                                1, 1);
373                 break;
374
375         case OP_STBU:
376                 ra = get_ra(inst);
377                 rs = get_rs(inst);
378                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
379                                                1, 1);
380                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
381                 break;
382
383         case OP_LHZ:
384                 rt = get_rt(inst);
385                 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
386                 break;
387
388         case OP_LHZU:
389                 ra = get_ra(inst);
390                 rt = get_rt(inst);
391                 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
392                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
393                 break;
394
395         case OP_STH:
396                 rs = get_rs(inst);
397                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
398                                                2, 1);
399                 break;
400
401         case OP_STHU:
402                 ra = get_ra(inst);
403                 rs = get_rs(inst);
404                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
405                                                2, 1);
406                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
407                 break;
408
409         default:
410                 emulated = EMULATE_FAIL;
411         }
412
413         if (emulated == EMULATE_FAIL) {
414                 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
415                 if (emulated == EMULATE_FAIL) {
416                         advance = 0;
417                         printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
418                                "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
419                 }
420         }
421
422         KVMTRACE_3D(PPC_INSTR, vcpu, inst, (int)vcpu->arch.pc, emulated, entryexit);
423
424         if (advance)
425                 vcpu->arch.pc += 4; /* Advance past emulated instruction. */
426
427         return emulated;
428 }