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