4337f99fa0fa0706b57e8a343af8d04e9e3158b9
[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/hrtimer.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 #include "trace.h"
33
34 #define OP_TRAP 3
35 #define OP_TRAP_64 2
36
37 #define OP_31_XOP_LWZX      23
38 #define OP_31_XOP_LBZX      87
39 #define OP_31_XOP_STWX      151
40 #define OP_31_XOP_STBX      215
41 #define OP_31_XOP_LBZUX     119
42 #define OP_31_XOP_STBUX     247
43 #define OP_31_XOP_LHZX      279
44 #define OP_31_XOP_LHZUX     311
45 #define OP_31_XOP_MFSPR     339
46 #define OP_31_XOP_LHAX      343
47 #define OP_31_XOP_STHX      407
48 #define OP_31_XOP_STHUX     439
49 #define OP_31_XOP_MTSPR     467
50 #define OP_31_XOP_DCBI      470
51 #define OP_31_XOP_LWBRX     534
52 #define OP_31_XOP_TLBSYNC   566
53 #define OP_31_XOP_STWBRX    662
54 #define OP_31_XOP_LHBRX     790
55 #define OP_31_XOP_STHBRX    918
56
57 #define OP_LWZ  32
58 #define OP_LWZU 33
59 #define OP_LBZ  34
60 #define OP_LBZU 35
61 #define OP_STW  36
62 #define OP_STWU 37
63 #define OP_STB  38
64 #define OP_STBU 39
65 #define OP_LHZ  40
66 #define OP_LHZU 41
67 #define OP_LHA  42
68 #define OP_LHAU 43
69 #define OP_STH  44
70 #define OP_STHU 45
71
72 #ifdef CONFIG_PPC_BOOK3S
73 static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
74 {
75         return 1;
76 }
77 #else
78 static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu)
79 {
80         return vcpu->arch.tcr & TCR_DIE;
81 }
82 #endif
83
84 void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
85 {
86         unsigned long dec_nsec;
87         unsigned long long dec_time;
88
89         pr_debug("mtDEC: %x\n", vcpu->arch.dec);
90 #ifdef CONFIG_PPC_BOOK3S
91         /* mtdec lowers the interrupt line when positive. */
92         kvmppc_core_dequeue_dec(vcpu);
93
94         /* POWER4+ triggers a dec interrupt if the value is < 0 */
95         if (vcpu->arch.dec & 0x80000000) {
96                 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
97                 kvmppc_core_queue_dec(vcpu);
98                 return;
99         }
100 #endif
101         if (kvmppc_dec_enabled(vcpu)) {
102                 /* The decrementer ticks at the same rate as the timebase, so
103                  * that's how we convert the guest DEC value to the number of
104                  * host ticks. */
105
106                 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
107                 dec_time = vcpu->arch.dec;
108                 dec_time *= 1000;
109                 do_div(dec_time, tb_ticks_per_usec);
110                 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
111                 hrtimer_start(&vcpu->arch.dec_timer,
112                         ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
113                 vcpu->arch.dec_jiffies = get_tb();
114         } else {
115                 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
116         }
117 }
118
119 u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
120 {
121         u64 jd = tb - vcpu->arch.dec_jiffies;
122         return vcpu->arch.dec - jd;
123 }
124
125 /* XXX to do:
126  * lhax
127  * lhaux
128  * lswx
129  * lswi
130  * stswx
131  * stswi
132  * lha
133  * lhau
134  * lmw
135  * stmw
136  *
137  * XXX is_bigendian should depend on MMU mapping or MSR[LE]
138  */
139 /* XXX Should probably auto-generate instruction decoding for a particular core
140  * from opcode tables in the future. */
141 int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
142 {
143         u32 inst = kvmppc_get_last_inst(vcpu);
144         u32 ea;
145         int ra;
146         int rb;
147         int rs;
148         int rt;
149         int sprn;
150         enum emulation_result emulated = EMULATE_DONE;
151         int advance = 1;
152
153         /* this default type might be overwritten by subcategories */
154         kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
155
156         pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
157
158         switch (get_op(inst)) {
159         case OP_TRAP:
160 #ifdef CONFIG_PPC_BOOK3S
161         case OP_TRAP_64:
162                 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
163 #else
164                 kvmppc_core_queue_program(vcpu, vcpu->arch.esr | ESR_PTR);
165 #endif
166                 advance = 0;
167                 break;
168
169         case 31:
170                 switch (get_xop(inst)) {
171
172                 case OP_31_XOP_LWZX:
173                         rt = get_rt(inst);
174                         emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
175                         break;
176
177                 case OP_31_XOP_LBZX:
178                         rt = get_rt(inst);
179                         emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
180                         break;
181
182                 case OP_31_XOP_LBZUX:
183                         rt = get_rt(inst);
184                         ra = get_ra(inst);
185                         rb = get_rb(inst);
186
187                         ea = kvmppc_get_gpr(vcpu, rb);
188                         if (ra)
189                                 ea += kvmppc_get_gpr(vcpu, ra);
190
191                         emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
192                         kvmppc_set_gpr(vcpu, ra, ea);
193                         break;
194
195                 case OP_31_XOP_STWX:
196                         rs = get_rs(inst);
197                         emulated = kvmppc_handle_store(run, vcpu,
198                                                        kvmppc_get_gpr(vcpu, rs),
199                                                        4, 1);
200                         break;
201
202                 case OP_31_XOP_STBX:
203                         rs = get_rs(inst);
204                         emulated = kvmppc_handle_store(run, vcpu,
205                                                        kvmppc_get_gpr(vcpu, rs),
206                                                        1, 1);
207                         break;
208
209                 case OP_31_XOP_STBUX:
210                         rs = get_rs(inst);
211                         ra = get_ra(inst);
212                         rb = get_rb(inst);
213
214                         ea = kvmppc_get_gpr(vcpu, rb);
215                         if (ra)
216                                 ea += kvmppc_get_gpr(vcpu, ra);
217
218                         emulated = kvmppc_handle_store(run, vcpu,
219                                                        kvmppc_get_gpr(vcpu, rs),
220                                                        1, 1);
221                         kvmppc_set_gpr(vcpu, rs, ea);
222                         break;
223
224                 case OP_31_XOP_LHAX:
225                         rt = get_rt(inst);
226                         emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
227                         break;
228
229                 case OP_31_XOP_LHZX:
230                         rt = get_rt(inst);
231                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
232                         break;
233
234                 case OP_31_XOP_LHZUX:
235                         rt = get_rt(inst);
236                         ra = get_ra(inst);
237                         rb = get_rb(inst);
238
239                         ea = kvmppc_get_gpr(vcpu, rb);
240                         if (ra)
241                                 ea += kvmppc_get_gpr(vcpu, ra);
242
243                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
244                         kvmppc_set_gpr(vcpu, ra, ea);
245                         break;
246
247                 case OP_31_XOP_MFSPR:
248                         sprn = get_sprn(inst);
249                         rt = get_rt(inst);
250
251                         switch (sprn) {
252                         case SPRN_SRR0:
253                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr0);
254                                 break;
255                         case SPRN_SRR1:
256                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr1);
257                                 break;
258                         case SPRN_PVR:
259                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
260                         case SPRN_PIR:
261                                 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
262                         case SPRN_MSSSR0:
263                                 kvmppc_set_gpr(vcpu, rt, 0); break;
264
265                         /* Note: mftb and TBRL/TBWL are user-accessible, so
266                          * the guest can always access the real TB anyways.
267                          * In fact, we probably will never see these traps. */
268                         case SPRN_TBWL:
269                                 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
270                         case SPRN_TBWU:
271                                 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
272
273                         case SPRN_SPRG0:
274                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg0);
275                                 break;
276                         case SPRN_SPRG1:
277                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg1);
278                                 break;
279                         case SPRN_SPRG2:
280                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg2);
281                                 break;
282                         case SPRN_SPRG3:
283                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg3);
284                                 break;
285                         /* Note: SPRG4-7 are user-readable, so we don't get
286                          * a trap. */
287
288                         case SPRN_DEC:
289                         {
290                                 kvmppc_set_gpr(vcpu, rt,
291                                                kvmppc_get_dec(vcpu, get_tb()));
292                                 break;
293                         }
294                         default:
295                                 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
296                                 if (emulated == EMULATE_FAIL) {
297                                         printk("mfspr: unknown spr %x\n", sprn);
298                                         kvmppc_set_gpr(vcpu, rt, 0);
299                                 }
300                                 break;
301                         }
302                         kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
303                         break;
304
305                 case OP_31_XOP_STHX:
306                         rs = get_rs(inst);
307                         ra = get_ra(inst);
308                         rb = get_rb(inst);
309
310                         emulated = kvmppc_handle_store(run, vcpu,
311                                                        kvmppc_get_gpr(vcpu, rs),
312                                                        2, 1);
313                         break;
314
315                 case OP_31_XOP_STHUX:
316                         rs = get_rs(inst);
317                         ra = get_ra(inst);
318                         rb = get_rb(inst);
319
320                         ea = kvmppc_get_gpr(vcpu, rb);
321                         if (ra)
322                                 ea += kvmppc_get_gpr(vcpu, ra);
323
324                         emulated = kvmppc_handle_store(run, vcpu,
325                                                        kvmppc_get_gpr(vcpu, rs),
326                                                        2, 1);
327                         kvmppc_set_gpr(vcpu, ra, ea);
328                         break;
329
330                 case OP_31_XOP_MTSPR:
331                         sprn = get_sprn(inst);
332                         rs = get_rs(inst);
333                         switch (sprn) {
334                         case SPRN_SRR0:
335                                 vcpu->arch.shared->srr0 = kvmppc_get_gpr(vcpu, rs);
336                                 break;
337                         case SPRN_SRR1:
338                                 vcpu->arch.shared->srr1 = kvmppc_get_gpr(vcpu, rs);
339                                 break;
340
341                         /* XXX We need to context-switch the timebase for
342                          * watchdog and FIT. */
343                         case SPRN_TBWL: break;
344                         case SPRN_TBWU: break;
345
346                         case SPRN_MSSSR0: break;
347
348                         case SPRN_DEC:
349                                 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
350                                 kvmppc_emulate_dec(vcpu);
351                                 break;
352
353                         case SPRN_SPRG0:
354                                 vcpu->arch.shared->sprg0 = kvmppc_get_gpr(vcpu, rs);
355                                 break;
356                         case SPRN_SPRG1:
357                                 vcpu->arch.shared->sprg1 = kvmppc_get_gpr(vcpu, rs);
358                                 break;
359                         case SPRN_SPRG2:
360                                 vcpu->arch.shared->sprg2 = kvmppc_get_gpr(vcpu, rs);
361                                 break;
362                         case SPRN_SPRG3:
363                                 vcpu->arch.shared->sprg3 = kvmppc_get_gpr(vcpu, rs);
364                                 break;
365
366                         default:
367                                 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
368                                 if (emulated == EMULATE_FAIL)
369                                         printk("mtspr: unknown spr %x\n", sprn);
370                                 break;
371                         }
372                         kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
373                         break;
374
375                 case OP_31_XOP_DCBI:
376                         /* Do nothing. The guest is performing dcbi because
377                          * hardware DMA is not snooped by the dcache, but
378                          * emulated DMA either goes through the dcache as
379                          * normal writes, or the host kernel has handled dcache
380                          * coherence. */
381                         break;
382
383                 case OP_31_XOP_LWBRX:
384                         rt = get_rt(inst);
385                         emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
386                         break;
387
388                 case OP_31_XOP_TLBSYNC:
389                         break;
390
391                 case OP_31_XOP_STWBRX:
392                         rs = get_rs(inst);
393                         ra = get_ra(inst);
394                         rb = get_rb(inst);
395
396                         emulated = kvmppc_handle_store(run, vcpu,
397                                                        kvmppc_get_gpr(vcpu, rs),
398                                                        4, 0);
399                         break;
400
401                 case OP_31_XOP_LHBRX:
402                         rt = get_rt(inst);
403                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
404                         break;
405
406                 case OP_31_XOP_STHBRX:
407                         rs = get_rs(inst);
408                         ra = get_ra(inst);
409                         rb = get_rb(inst);
410
411                         emulated = kvmppc_handle_store(run, vcpu,
412                                                        kvmppc_get_gpr(vcpu, rs),
413                                                        2, 0);
414                         break;
415
416                 default:
417                         /* Attempt core-specific emulation below. */
418                         emulated = EMULATE_FAIL;
419                 }
420                 break;
421
422         case OP_LWZ:
423                 rt = get_rt(inst);
424                 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
425                 break;
426
427         case OP_LWZU:
428                 ra = get_ra(inst);
429                 rt = get_rt(inst);
430                 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
431                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
432                 break;
433
434         case OP_LBZ:
435                 rt = get_rt(inst);
436                 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
437                 break;
438
439         case OP_LBZU:
440                 ra = get_ra(inst);
441                 rt = get_rt(inst);
442                 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
443                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
444                 break;
445
446         case OP_STW:
447                 rs = get_rs(inst);
448                 emulated = kvmppc_handle_store(run, vcpu,
449                                                kvmppc_get_gpr(vcpu, rs),
450                                                4, 1);
451                 break;
452
453         case OP_STWU:
454                 ra = get_ra(inst);
455                 rs = get_rs(inst);
456                 emulated = kvmppc_handle_store(run, vcpu,
457                                                kvmppc_get_gpr(vcpu, rs),
458                                                4, 1);
459                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
460                 break;
461
462         case OP_STB:
463                 rs = get_rs(inst);
464                 emulated = kvmppc_handle_store(run, vcpu,
465                                                kvmppc_get_gpr(vcpu, rs),
466                                                1, 1);
467                 break;
468
469         case OP_STBU:
470                 ra = get_ra(inst);
471                 rs = get_rs(inst);
472                 emulated = kvmppc_handle_store(run, vcpu,
473                                                kvmppc_get_gpr(vcpu, rs),
474                                                1, 1);
475                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
476                 break;
477
478         case OP_LHZ:
479                 rt = get_rt(inst);
480                 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
481                 break;
482
483         case OP_LHZU:
484                 ra = get_ra(inst);
485                 rt = get_rt(inst);
486                 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
487                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
488                 break;
489
490         case OP_LHA:
491                 rt = get_rt(inst);
492                 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
493                 break;
494
495         case OP_LHAU:
496                 ra = get_ra(inst);
497                 rt = get_rt(inst);
498                 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
499                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
500                 break;
501
502         case OP_STH:
503                 rs = get_rs(inst);
504                 emulated = kvmppc_handle_store(run, vcpu,
505                                                kvmppc_get_gpr(vcpu, rs),
506                                                2, 1);
507                 break;
508
509         case OP_STHU:
510                 ra = get_ra(inst);
511                 rs = get_rs(inst);
512                 emulated = kvmppc_handle_store(run, vcpu,
513                                                kvmppc_get_gpr(vcpu, rs),
514                                                2, 1);
515                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed);
516                 break;
517
518         default:
519                 emulated = EMULATE_FAIL;
520         }
521
522         if (emulated == EMULATE_FAIL) {
523                 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
524                 if (emulated == EMULATE_AGAIN) {
525                         advance = 0;
526                 } else if (emulated == EMULATE_FAIL) {
527                         advance = 0;
528                         printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
529                                "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
530                         kvmppc_core_queue_program(vcpu, 0);
531                 }
532         }
533
534         trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
535
536         /* Advance past emulated instruction. */
537         if (advance)
538                 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
539
540         return emulated;
541 }