KVM: PPC: Handle some PAPR hcalls in the kernel
[pandora-kernel.git] / arch / powerpc / kvm / powerpc.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  *          Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
19  */
20
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/kvm_host.h>
24 #include <linux/module.h>
25 #include <linux/vmalloc.h>
26 #include <linux/hrtimer.h>
27 #include <linux/fs.h>
28 #include <linux/slab.h>
29 #include <asm/cputable.h>
30 #include <asm/uaccess.h>
31 #include <asm/kvm_ppc.h>
32 #include <asm/tlbflush.h>
33 #include "timing.h"
34 #include "../mm/mmu_decl.h"
35
36 #define CREATE_TRACE_POINTS
37 #include "trace.h"
38
39 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
40 {
41 #ifndef CONFIG_KVM_BOOK3S_64_HV
42         return !(v->arch.shared->msr & MSR_WE) ||
43                !!(v->arch.pending_exceptions);
44 #else
45         return !(v->arch.ceded) || !!(v->arch.pending_exceptions);
46 #endif
47 }
48
49 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
50 {
51         int nr = kvmppc_get_gpr(vcpu, 11);
52         int r;
53         unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
54         unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
55         unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
56         unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
57         unsigned long r2 = 0;
58
59         if (!(vcpu->arch.shared->msr & MSR_SF)) {
60                 /* 32 bit mode */
61                 param1 &= 0xffffffff;
62                 param2 &= 0xffffffff;
63                 param3 &= 0xffffffff;
64                 param4 &= 0xffffffff;
65         }
66
67         switch (nr) {
68         case HC_VENDOR_KVM | KVM_HC_PPC_MAP_MAGIC_PAGE:
69         {
70                 vcpu->arch.magic_page_pa = param1;
71                 vcpu->arch.magic_page_ea = param2;
72
73                 r2 = KVM_MAGIC_FEAT_SR;
74
75                 r = HC_EV_SUCCESS;
76                 break;
77         }
78         case HC_VENDOR_KVM | KVM_HC_FEATURES:
79                 r = HC_EV_SUCCESS;
80 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500)
81                 /* XXX Missing magic page on 44x */
82                 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
83 #endif
84
85                 /* Second return value is in r4 */
86                 break;
87         default:
88                 r = HC_EV_UNIMPLEMENTED;
89                 break;
90         }
91
92         kvmppc_set_gpr(vcpu, 4, r2);
93
94         return r;
95 }
96
97 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
98 {
99         enum emulation_result er;
100         int r;
101
102         er = kvmppc_emulate_instruction(run, vcpu);
103         switch (er) {
104         case EMULATE_DONE:
105                 /* Future optimization: only reload non-volatiles if they were
106                  * actually modified. */
107                 r = RESUME_GUEST_NV;
108                 break;
109         case EMULATE_DO_MMIO:
110                 run->exit_reason = KVM_EXIT_MMIO;
111                 /* We must reload nonvolatiles because "update" load/store
112                  * instructions modify register state. */
113                 /* Future optimization: only reload non-volatiles if they were
114                  * actually modified. */
115                 r = RESUME_HOST_NV;
116                 break;
117         case EMULATE_FAIL:
118                 /* XXX Deliver Program interrupt to guest. */
119                 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
120                        kvmppc_get_last_inst(vcpu));
121                 r = RESUME_HOST;
122                 break;
123         default:
124                 BUG();
125         }
126
127         return r;
128 }
129
130 int kvm_arch_hardware_enable(void *garbage)
131 {
132         return 0;
133 }
134
135 void kvm_arch_hardware_disable(void *garbage)
136 {
137 }
138
139 int kvm_arch_hardware_setup(void)
140 {
141         return 0;
142 }
143
144 void kvm_arch_hardware_unsetup(void)
145 {
146 }
147
148 void kvm_arch_check_processor_compat(void *rtn)
149 {
150         *(int *)rtn = kvmppc_core_check_processor_compat();
151 }
152
153 int kvm_arch_init_vm(struct kvm *kvm)
154 {
155         return kvmppc_core_init_vm(kvm);
156 }
157
158 void kvm_arch_destroy_vm(struct kvm *kvm)
159 {
160         unsigned int i;
161         struct kvm_vcpu *vcpu;
162
163         kvm_for_each_vcpu(i, vcpu, kvm)
164                 kvm_arch_vcpu_free(vcpu);
165
166         mutex_lock(&kvm->lock);
167         for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
168                 kvm->vcpus[i] = NULL;
169
170         atomic_set(&kvm->online_vcpus, 0);
171
172         kvmppc_core_destroy_vm(kvm);
173
174         mutex_unlock(&kvm->lock);
175 }
176
177 void kvm_arch_sync_events(struct kvm *kvm)
178 {
179 }
180
181 int kvm_dev_ioctl_check_extension(long ext)
182 {
183         int r;
184
185         switch (ext) {
186 #ifdef CONFIG_BOOKE
187         case KVM_CAP_PPC_BOOKE_SREGS:
188 #else
189         case KVM_CAP_PPC_SEGSTATE:
190 #endif
191         case KVM_CAP_PPC_UNSET_IRQ:
192         case KVM_CAP_PPC_IRQ_LEVEL:
193         case KVM_CAP_ENABLE_CAP:
194                 r = 1;
195                 break;
196 #ifndef CONFIG_KVM_BOOK3S_64_HV
197         case KVM_CAP_PPC_PAIRED_SINGLES:
198         case KVM_CAP_PPC_OSI:
199         case KVM_CAP_PPC_GET_PVINFO:
200                 r = 1;
201                 break;
202         case KVM_CAP_COALESCED_MMIO:
203                 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
204                 break;
205 #endif
206         default:
207                 r = 0;
208                 break;
209         }
210         return r;
211
212 }
213
214 long kvm_arch_dev_ioctl(struct file *filp,
215                         unsigned int ioctl, unsigned long arg)
216 {
217         return -EINVAL;
218 }
219
220 int kvm_arch_prepare_memory_region(struct kvm *kvm,
221                                    struct kvm_memory_slot *memslot,
222                                    struct kvm_memory_slot old,
223                                    struct kvm_userspace_memory_region *mem,
224                                    int user_alloc)
225 {
226         return kvmppc_core_prepare_memory_region(kvm, mem);
227 }
228
229 void kvm_arch_commit_memory_region(struct kvm *kvm,
230                struct kvm_userspace_memory_region *mem,
231                struct kvm_memory_slot old,
232                int user_alloc)
233 {
234         kvmppc_core_commit_memory_region(kvm, mem);
235 }
236
237
238 void kvm_arch_flush_shadow(struct kvm *kvm)
239 {
240 }
241
242 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
243 {
244         struct kvm_vcpu *vcpu;
245         vcpu = kvmppc_core_vcpu_create(kvm, id);
246         if (!IS_ERR(vcpu))
247                 kvmppc_create_vcpu_debugfs(vcpu, id);
248         return vcpu;
249 }
250
251 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
252 {
253         /* Make sure we're not using the vcpu anymore */
254         hrtimer_cancel(&vcpu->arch.dec_timer);
255         tasklet_kill(&vcpu->arch.tasklet);
256
257         kvmppc_remove_vcpu_debugfs(vcpu);
258         kvmppc_core_vcpu_free(vcpu);
259 }
260
261 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
262 {
263         kvm_arch_vcpu_free(vcpu);
264 }
265
266 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
267 {
268         return kvmppc_core_pending_dec(vcpu);
269 }
270
271 static void kvmppc_decrementer_func(unsigned long data)
272 {
273         struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
274
275         kvmppc_core_queue_dec(vcpu);
276
277         if (waitqueue_active(&vcpu->wq)) {
278                 wake_up_interruptible(&vcpu->wq);
279                 vcpu->stat.halt_wakeup++;
280         }
281 }
282
283 /*
284  * low level hrtimer wake routine. Because this runs in hardirq context
285  * we schedule a tasklet to do the real work.
286  */
287 enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
288 {
289         struct kvm_vcpu *vcpu;
290
291         vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
292         tasklet_schedule(&vcpu->arch.tasklet);
293
294         return HRTIMER_NORESTART;
295 }
296
297 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
298 {
299         hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
300         tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
301         vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
302         vcpu->arch.dec_expires = ~(u64)0;
303
304 #ifdef CONFIG_KVM_EXIT_TIMING
305         mutex_init(&vcpu->arch.exit_timing_lock);
306 #endif
307
308         return 0;
309 }
310
311 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
312 {
313         kvmppc_mmu_destroy(vcpu);
314 }
315
316 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
317 {
318 #ifdef CONFIG_BOOKE
319         /*
320          * vrsave (formerly usprg0) isn't used by Linux, but may
321          * be used by the guest.
322          *
323          * On non-booke this is associated with Altivec and
324          * is handled by code in book3s.c.
325          */
326         mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
327 #endif
328         kvmppc_core_vcpu_load(vcpu, cpu);
329         vcpu->cpu = smp_processor_id();
330 }
331
332 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
333 {
334         kvmppc_core_vcpu_put(vcpu);
335 #ifdef CONFIG_BOOKE
336         vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
337 #endif
338         vcpu->cpu = -1;
339 }
340
341 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
342                                         struct kvm_guest_debug *dbg)
343 {
344         return -EINVAL;
345 }
346
347 static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
348                                      struct kvm_run *run)
349 {
350         kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
351 }
352
353 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
354                                       struct kvm_run *run)
355 {
356         u64 uninitialized_var(gpr);
357
358         if (run->mmio.len > sizeof(gpr)) {
359                 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
360                 return;
361         }
362
363         if (vcpu->arch.mmio_is_bigendian) {
364                 switch (run->mmio.len) {
365                 case 8: gpr = *(u64 *)run->mmio.data; break;
366                 case 4: gpr = *(u32 *)run->mmio.data; break;
367                 case 2: gpr = *(u16 *)run->mmio.data; break;
368                 case 1: gpr = *(u8 *)run->mmio.data; break;
369                 }
370         } else {
371                 /* Convert BE data from userland back to LE. */
372                 switch (run->mmio.len) {
373                 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
374                 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
375                 case 1: gpr = *(u8 *)run->mmio.data; break;
376                 }
377         }
378
379         if (vcpu->arch.mmio_sign_extend) {
380                 switch (run->mmio.len) {
381 #ifdef CONFIG_PPC64
382                 case 4:
383                         gpr = (s64)(s32)gpr;
384                         break;
385 #endif
386                 case 2:
387                         gpr = (s64)(s16)gpr;
388                         break;
389                 case 1:
390                         gpr = (s64)(s8)gpr;
391                         break;
392                 }
393         }
394
395         kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
396
397         switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) {
398         case KVM_REG_GPR:
399                 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
400                 break;
401         case KVM_REG_FPR:
402                 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
403                 break;
404 #ifdef CONFIG_PPC_BOOK3S
405         case KVM_REG_QPR:
406                 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
407                 break;
408         case KVM_REG_FQPR:
409                 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
410                 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
411                 break;
412 #endif
413         default:
414                 BUG();
415         }
416 }
417
418 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
419                        unsigned int rt, unsigned int bytes, int is_bigendian)
420 {
421         if (bytes > sizeof(run->mmio.data)) {
422                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
423                        run->mmio.len);
424         }
425
426         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
427         run->mmio.len = bytes;
428         run->mmio.is_write = 0;
429
430         vcpu->arch.io_gpr = rt;
431         vcpu->arch.mmio_is_bigendian = is_bigendian;
432         vcpu->mmio_needed = 1;
433         vcpu->mmio_is_write = 0;
434         vcpu->arch.mmio_sign_extend = 0;
435
436         return EMULATE_DO_MMIO;
437 }
438
439 /* Same as above, but sign extends */
440 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
441                         unsigned int rt, unsigned int bytes, int is_bigendian)
442 {
443         int r;
444
445         r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
446         vcpu->arch.mmio_sign_extend = 1;
447
448         return r;
449 }
450
451 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
452                         u64 val, unsigned int bytes, int is_bigendian)
453 {
454         void *data = run->mmio.data;
455
456         if (bytes > sizeof(run->mmio.data)) {
457                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
458                        run->mmio.len);
459         }
460
461         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
462         run->mmio.len = bytes;
463         run->mmio.is_write = 1;
464         vcpu->mmio_needed = 1;
465         vcpu->mmio_is_write = 1;
466
467         /* Store the value at the lowest bytes in 'data'. */
468         if (is_bigendian) {
469                 switch (bytes) {
470                 case 8: *(u64 *)data = val; break;
471                 case 4: *(u32 *)data = val; break;
472                 case 2: *(u16 *)data = val; break;
473                 case 1: *(u8  *)data = val; break;
474                 }
475         } else {
476                 /* Store LE value into 'data'. */
477                 switch (bytes) {
478                 case 4: st_le32(data, val); break;
479                 case 2: st_le16(data, val); break;
480                 case 1: *(u8 *)data = val; break;
481                 }
482         }
483
484         return EMULATE_DO_MMIO;
485 }
486
487 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
488 {
489         int r;
490         sigset_t sigsaved;
491
492         if (vcpu->sigset_active)
493                 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
494
495         if (vcpu->mmio_needed) {
496                 if (!vcpu->mmio_is_write)
497                         kvmppc_complete_mmio_load(vcpu, run);
498                 vcpu->mmio_needed = 0;
499         } else if (vcpu->arch.dcr_needed) {
500                 if (!vcpu->arch.dcr_is_write)
501                         kvmppc_complete_dcr_load(vcpu, run);
502                 vcpu->arch.dcr_needed = 0;
503         } else if (vcpu->arch.osi_needed) {
504                 u64 *gprs = run->osi.gprs;
505                 int i;
506
507                 for (i = 0; i < 32; i++)
508                         kvmppc_set_gpr(vcpu, i, gprs[i]);
509                 vcpu->arch.osi_needed = 0;
510         } else if (vcpu->arch.hcall_needed) {
511                 int i;
512
513                 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
514                 for (i = 0; i < 9; ++i)
515                         kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
516                 vcpu->arch.hcall_needed = 0;
517         }
518
519         kvmppc_core_deliver_interrupts(vcpu);
520
521         r = kvmppc_vcpu_run(run, vcpu);
522
523         if (vcpu->sigset_active)
524                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
525
526         return r;
527 }
528
529 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
530 {
531         if (irq->irq == KVM_INTERRUPT_UNSET)
532                 kvmppc_core_dequeue_external(vcpu, irq);
533         else
534                 kvmppc_core_queue_external(vcpu, irq);
535
536         if (waitqueue_active(&vcpu->wq)) {
537                 wake_up_interruptible(&vcpu->wq);
538                 vcpu->stat.halt_wakeup++;
539         } else if (vcpu->cpu != -1) {
540                 smp_send_reschedule(vcpu->cpu);
541         }
542
543         return 0;
544 }
545
546 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
547                                      struct kvm_enable_cap *cap)
548 {
549         int r;
550
551         if (cap->flags)
552                 return -EINVAL;
553
554         switch (cap->cap) {
555         case KVM_CAP_PPC_OSI:
556                 r = 0;
557                 vcpu->arch.osi_enabled = true;
558                 break;
559         default:
560                 r = -EINVAL;
561                 break;
562         }
563
564         return r;
565 }
566
567 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
568                                     struct kvm_mp_state *mp_state)
569 {
570         return -EINVAL;
571 }
572
573 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
574                                     struct kvm_mp_state *mp_state)
575 {
576         return -EINVAL;
577 }
578
579 long kvm_arch_vcpu_ioctl(struct file *filp,
580                          unsigned int ioctl, unsigned long arg)
581 {
582         struct kvm_vcpu *vcpu = filp->private_data;
583         void __user *argp = (void __user *)arg;
584         long r;
585
586         switch (ioctl) {
587         case KVM_INTERRUPT: {
588                 struct kvm_interrupt irq;
589                 r = -EFAULT;
590                 if (copy_from_user(&irq, argp, sizeof(irq)))
591                         goto out;
592                 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
593                 goto out;
594         }
595
596         case KVM_ENABLE_CAP:
597         {
598                 struct kvm_enable_cap cap;
599                 r = -EFAULT;
600                 if (copy_from_user(&cap, argp, sizeof(cap)))
601                         goto out;
602                 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
603                 break;
604         }
605         default:
606                 r = -EINVAL;
607         }
608
609 out:
610         return r;
611 }
612
613 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
614 {
615         u32 inst_lis = 0x3c000000;
616         u32 inst_ori = 0x60000000;
617         u32 inst_nop = 0x60000000;
618         u32 inst_sc = 0x44000002;
619         u32 inst_imm_mask = 0xffff;
620
621         /*
622          * The hypercall to get into KVM from within guest context is as
623          * follows:
624          *
625          *    lis r0, r0, KVM_SC_MAGIC_R0@h
626          *    ori r0, KVM_SC_MAGIC_R0@l
627          *    sc
628          *    nop
629          */
630         pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
631         pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
632         pvinfo->hcall[2] = inst_sc;
633         pvinfo->hcall[3] = inst_nop;
634
635         return 0;
636 }
637
638 long kvm_arch_vm_ioctl(struct file *filp,
639                        unsigned int ioctl, unsigned long arg)
640 {
641         void __user *argp = (void __user *)arg;
642         long r;
643
644         switch (ioctl) {
645         case KVM_PPC_GET_PVINFO: {
646                 struct kvm_ppc_pvinfo pvinfo;
647                 memset(&pvinfo, 0, sizeof(pvinfo));
648                 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
649                 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
650                         r = -EFAULT;
651                         goto out;
652                 }
653
654                 break;
655         }
656         default:
657                 r = -ENOTTY;
658         }
659
660 out:
661         return r;
662 }
663
664 int kvm_arch_init(void *opaque)
665 {
666         return 0;
667 }
668
669 void kvm_arch_exit(void)
670 {
671 }