]> Pileus Git - ~andy/linux/blob - arch/powerpc/kvm/powerpc.c
b5e9046462fd958c8d4035fd6c014b85e17ede5b
[~andy/linux] / 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/vmalloc.h>
25 #include <linux/hrtimer.h>
26 #include <linux/fs.h>
27 #include <linux/slab.h>
28 #include <asm/cputable.h>
29 #include <asm/uaccess.h>
30 #include <asm/kvm_ppc.h>
31 #include <asm/tlbflush.h>
32 #include <asm/cputhreads.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         return !(v->arch.shared->msr & MSR_WE) ||
42                !!(v->arch.pending_exceptions) ||
43                v->requests;
44 }
45
46 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
47 {
48         return 1;
49 }
50
51 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
52 {
53         int nr = kvmppc_get_gpr(vcpu, 11);
54         int r;
55         unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
56         unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
57         unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
58         unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
59         unsigned long r2 = 0;
60
61         if (!(vcpu->arch.shared->msr & MSR_SF)) {
62                 /* 32 bit mode */
63                 param1 &= 0xffffffff;
64                 param2 &= 0xffffffff;
65                 param3 &= 0xffffffff;
66                 param4 &= 0xffffffff;
67         }
68
69         switch (nr) {
70         case HC_VENDOR_KVM | KVM_HC_PPC_MAP_MAGIC_PAGE:
71         {
72                 vcpu->arch.magic_page_pa = param1;
73                 vcpu->arch.magic_page_ea = param2;
74
75                 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
76
77                 r = HC_EV_SUCCESS;
78                 break;
79         }
80         case HC_VENDOR_KVM | KVM_HC_FEATURES:
81                 r = HC_EV_SUCCESS;
82 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500)
83                 /* XXX Missing magic page on 44x */
84                 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
85 #endif
86
87                 /* Second return value is in r4 */
88                 break;
89         default:
90                 r = HC_EV_UNIMPLEMENTED;
91                 break;
92         }
93
94         kvmppc_set_gpr(vcpu, 4, r2);
95
96         return r;
97 }
98
99 int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
100 {
101         int r = false;
102
103         /* We have to know what CPU to virtualize */
104         if (!vcpu->arch.pvr)
105                 goto out;
106
107         /* PAPR only works with book3s_64 */
108         if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
109                 goto out;
110
111 #ifdef CONFIG_KVM_BOOK3S_64_HV
112         /* HV KVM can only do PAPR mode for now */
113         if (!vcpu->arch.papr_enabled)
114                 goto out;
115 #endif
116
117         r = true;
118
119 out:
120         vcpu->arch.sane = r;
121         return r ? 0 : -EINVAL;
122 }
123
124 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
125 {
126         enum emulation_result er;
127         int r;
128
129         er = kvmppc_emulate_instruction(run, vcpu);
130         switch (er) {
131         case EMULATE_DONE:
132                 /* Future optimization: only reload non-volatiles if they were
133                  * actually modified. */
134                 r = RESUME_GUEST_NV;
135                 break;
136         case EMULATE_DO_MMIO:
137                 run->exit_reason = KVM_EXIT_MMIO;
138                 /* We must reload nonvolatiles because "update" load/store
139                  * instructions modify register state. */
140                 /* Future optimization: only reload non-volatiles if they were
141                  * actually modified. */
142                 r = RESUME_HOST_NV;
143                 break;
144         case EMULATE_FAIL:
145                 /* XXX Deliver Program interrupt to guest. */
146                 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
147                        kvmppc_get_last_inst(vcpu));
148                 r = RESUME_HOST;
149                 break;
150         default:
151                 BUG();
152         }
153
154         return r;
155 }
156
157 int kvm_arch_hardware_enable(void *garbage)
158 {
159         return 0;
160 }
161
162 void kvm_arch_hardware_disable(void *garbage)
163 {
164 }
165
166 int kvm_arch_hardware_setup(void)
167 {
168         return 0;
169 }
170
171 void kvm_arch_hardware_unsetup(void)
172 {
173 }
174
175 void kvm_arch_check_processor_compat(void *rtn)
176 {
177         *(int *)rtn = kvmppc_core_check_processor_compat();
178 }
179
180 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
181 {
182         if (type)
183                 return -EINVAL;
184
185         return kvmppc_core_init_vm(kvm);
186 }
187
188 void kvm_arch_destroy_vm(struct kvm *kvm)
189 {
190         unsigned int i;
191         struct kvm_vcpu *vcpu;
192
193         kvm_for_each_vcpu(i, vcpu, kvm)
194                 kvm_arch_vcpu_free(vcpu);
195
196         mutex_lock(&kvm->lock);
197         for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
198                 kvm->vcpus[i] = NULL;
199
200         atomic_set(&kvm->online_vcpus, 0);
201
202         kvmppc_core_destroy_vm(kvm);
203
204         mutex_unlock(&kvm->lock);
205 }
206
207 void kvm_arch_sync_events(struct kvm *kvm)
208 {
209 }
210
211 int kvm_dev_ioctl_check_extension(long ext)
212 {
213         int r;
214
215         switch (ext) {
216 #ifdef CONFIG_BOOKE
217         case KVM_CAP_PPC_BOOKE_SREGS:
218 #else
219         case KVM_CAP_PPC_SEGSTATE:
220         case KVM_CAP_PPC_HIOR:
221         case KVM_CAP_PPC_PAPR:
222 #endif
223         case KVM_CAP_PPC_UNSET_IRQ:
224         case KVM_CAP_PPC_IRQ_LEVEL:
225         case KVM_CAP_ENABLE_CAP:
226         case KVM_CAP_ONE_REG:
227                 r = 1;
228                 break;
229 #ifndef CONFIG_KVM_BOOK3S_64_HV
230         case KVM_CAP_PPC_PAIRED_SINGLES:
231         case KVM_CAP_PPC_OSI:
232         case KVM_CAP_PPC_GET_PVINFO:
233 #ifdef CONFIG_KVM_E500
234         case KVM_CAP_SW_TLB:
235 #endif
236                 r = 1;
237                 break;
238         case KVM_CAP_COALESCED_MMIO:
239                 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
240                 break;
241 #endif
242 #ifdef CONFIG_KVM_BOOK3S_64_HV
243         case KVM_CAP_SPAPR_TCE:
244                 r = 1;
245                 break;
246         case KVM_CAP_PPC_SMT:
247                 r = threads_per_core;
248                 break;
249         case KVM_CAP_PPC_RMA:
250                 r = 1;
251                 /* PPC970 requires an RMA */
252                 if (cpu_has_feature(CPU_FTR_ARCH_201))
253                         r = 2;
254                 break;
255         case KVM_CAP_SYNC_MMU:
256                 r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
257                 break;
258 #endif
259         case KVM_CAP_NR_VCPUS:
260                 /*
261                  * Recommending a number of CPUs is somewhat arbitrary; we
262                  * return the number of present CPUs for -HV (since a host
263                  * will have secondary threads "offline"), and for other KVM
264                  * implementations just count online CPUs.
265                  */
266 #ifdef CONFIG_KVM_BOOK3S_64_HV
267                 r = num_present_cpus();
268 #else
269                 r = num_online_cpus();
270 #endif
271                 break;
272         case KVM_CAP_MAX_VCPUS:
273                 r = KVM_MAX_VCPUS;
274                 break;
275         default:
276                 r = 0;
277                 break;
278         }
279         return r;
280
281 }
282
283 long kvm_arch_dev_ioctl(struct file *filp,
284                         unsigned int ioctl, unsigned long arg)
285 {
286         return -EINVAL;
287 }
288
289 void kvm_arch_free_memslot(struct kvm_memory_slot *free,
290                            struct kvm_memory_slot *dont)
291 {
292 }
293
294 int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
295 {
296         return 0;
297 }
298
299 int kvm_arch_prepare_memory_region(struct kvm *kvm,
300                                    struct kvm_memory_slot *memslot,
301                                    struct kvm_memory_slot old,
302                                    struct kvm_userspace_memory_region *mem,
303                                    int user_alloc)
304 {
305         return kvmppc_core_prepare_memory_region(kvm, mem);
306 }
307
308 void kvm_arch_commit_memory_region(struct kvm *kvm,
309                struct kvm_userspace_memory_region *mem,
310                struct kvm_memory_slot old,
311                int user_alloc)
312 {
313         kvmppc_core_commit_memory_region(kvm, mem);
314 }
315
316
317 void kvm_arch_flush_shadow(struct kvm *kvm)
318 {
319 }
320
321 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
322 {
323         struct kvm_vcpu *vcpu;
324         vcpu = kvmppc_core_vcpu_create(kvm, id);
325         if (!IS_ERR(vcpu)) {
326                 vcpu->arch.wqp = &vcpu->wq;
327                 kvmppc_create_vcpu_debugfs(vcpu, id);
328         }
329         return vcpu;
330 }
331
332 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
333 {
334         /* Make sure we're not using the vcpu anymore */
335         hrtimer_cancel(&vcpu->arch.dec_timer);
336         tasklet_kill(&vcpu->arch.tasklet);
337
338         kvmppc_remove_vcpu_debugfs(vcpu);
339         kvmppc_core_vcpu_free(vcpu);
340 }
341
342 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
343 {
344         kvm_arch_vcpu_free(vcpu);
345 }
346
347 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
348 {
349         return kvmppc_core_pending_dec(vcpu);
350 }
351
352 /*
353  * low level hrtimer wake routine. Because this runs in hardirq context
354  * we schedule a tasklet to do the real work.
355  */
356 enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
357 {
358         struct kvm_vcpu *vcpu;
359
360         vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
361         tasklet_schedule(&vcpu->arch.tasklet);
362
363         return HRTIMER_NORESTART;
364 }
365
366 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
367 {
368         hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
369         tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
370         vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
371         vcpu->arch.dec_expires = ~(u64)0;
372
373 #ifdef CONFIG_KVM_EXIT_TIMING
374         mutex_init(&vcpu->arch.exit_timing_lock);
375 #endif
376
377         return 0;
378 }
379
380 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
381 {
382         kvmppc_mmu_destroy(vcpu);
383 }
384
385 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
386 {
387 #ifdef CONFIG_BOOKE
388         /*
389          * vrsave (formerly usprg0) isn't used by Linux, but may
390          * be used by the guest.
391          *
392          * On non-booke this is associated with Altivec and
393          * is handled by code in book3s.c.
394          */
395         mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
396 #endif
397         kvmppc_core_vcpu_load(vcpu, cpu);
398         vcpu->cpu = smp_processor_id();
399 }
400
401 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
402 {
403         kvmppc_core_vcpu_put(vcpu);
404 #ifdef CONFIG_BOOKE
405         vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
406 #endif
407         vcpu->cpu = -1;
408 }
409
410 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
411                                         struct kvm_guest_debug *dbg)
412 {
413         return -EINVAL;
414 }
415
416 static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
417                                      struct kvm_run *run)
418 {
419         kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
420 }
421
422 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
423                                       struct kvm_run *run)
424 {
425         u64 uninitialized_var(gpr);
426
427         if (run->mmio.len > sizeof(gpr)) {
428                 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
429                 return;
430         }
431
432         if (vcpu->arch.mmio_is_bigendian) {
433                 switch (run->mmio.len) {
434                 case 8: gpr = *(u64 *)run->mmio.data; break;
435                 case 4: gpr = *(u32 *)run->mmio.data; break;
436                 case 2: gpr = *(u16 *)run->mmio.data; break;
437                 case 1: gpr = *(u8 *)run->mmio.data; break;
438                 }
439         } else {
440                 /* Convert BE data from userland back to LE. */
441                 switch (run->mmio.len) {
442                 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
443                 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
444                 case 1: gpr = *(u8 *)run->mmio.data; break;
445                 }
446         }
447
448         if (vcpu->arch.mmio_sign_extend) {
449                 switch (run->mmio.len) {
450 #ifdef CONFIG_PPC64
451                 case 4:
452                         gpr = (s64)(s32)gpr;
453                         break;
454 #endif
455                 case 2:
456                         gpr = (s64)(s16)gpr;
457                         break;
458                 case 1:
459                         gpr = (s64)(s8)gpr;
460                         break;
461                 }
462         }
463
464         kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
465
466         switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
467         case KVM_MMIO_REG_GPR:
468                 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
469                 break;
470         case KVM_MMIO_REG_FPR:
471                 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
472                 break;
473 #ifdef CONFIG_PPC_BOOK3S
474         case KVM_MMIO_REG_QPR:
475                 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
476                 break;
477         case KVM_MMIO_REG_FQPR:
478                 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
479                 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
480                 break;
481 #endif
482         default:
483                 BUG();
484         }
485 }
486
487 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
488                        unsigned int rt, unsigned int bytes, int is_bigendian)
489 {
490         if (bytes > sizeof(run->mmio.data)) {
491                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
492                        run->mmio.len);
493         }
494
495         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
496         run->mmio.len = bytes;
497         run->mmio.is_write = 0;
498
499         vcpu->arch.io_gpr = rt;
500         vcpu->arch.mmio_is_bigendian = is_bigendian;
501         vcpu->mmio_needed = 1;
502         vcpu->mmio_is_write = 0;
503         vcpu->arch.mmio_sign_extend = 0;
504
505         return EMULATE_DO_MMIO;
506 }
507
508 /* Same as above, but sign extends */
509 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
510                         unsigned int rt, unsigned int bytes, int is_bigendian)
511 {
512         int r;
513
514         r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
515         vcpu->arch.mmio_sign_extend = 1;
516
517         return r;
518 }
519
520 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
521                         u64 val, unsigned int bytes, int is_bigendian)
522 {
523         void *data = run->mmio.data;
524
525         if (bytes > sizeof(run->mmio.data)) {
526                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
527                        run->mmio.len);
528         }
529
530         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
531         run->mmio.len = bytes;
532         run->mmio.is_write = 1;
533         vcpu->mmio_needed = 1;
534         vcpu->mmio_is_write = 1;
535
536         /* Store the value at the lowest bytes in 'data'. */
537         if (is_bigendian) {
538                 switch (bytes) {
539                 case 8: *(u64 *)data = val; break;
540                 case 4: *(u32 *)data = val; break;
541                 case 2: *(u16 *)data = val; break;
542                 case 1: *(u8  *)data = val; break;
543                 }
544         } else {
545                 /* Store LE value into 'data'. */
546                 switch (bytes) {
547                 case 4: st_le32(data, val); break;
548                 case 2: st_le16(data, val); break;
549                 case 1: *(u8 *)data = val; break;
550                 }
551         }
552
553         return EMULATE_DO_MMIO;
554 }
555
556 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
557 {
558         int r;
559         sigset_t sigsaved;
560
561         if (vcpu->sigset_active)
562                 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
563
564         if (vcpu->mmio_needed) {
565                 if (!vcpu->mmio_is_write)
566                         kvmppc_complete_mmio_load(vcpu, run);
567                 vcpu->mmio_needed = 0;
568         } else if (vcpu->arch.dcr_needed) {
569                 if (!vcpu->arch.dcr_is_write)
570                         kvmppc_complete_dcr_load(vcpu, run);
571                 vcpu->arch.dcr_needed = 0;
572         } else if (vcpu->arch.osi_needed) {
573                 u64 *gprs = run->osi.gprs;
574                 int i;
575
576                 for (i = 0; i < 32; i++)
577                         kvmppc_set_gpr(vcpu, i, gprs[i]);
578                 vcpu->arch.osi_needed = 0;
579         } else if (vcpu->arch.hcall_needed) {
580                 int i;
581
582                 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
583                 for (i = 0; i < 9; ++i)
584                         kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
585                 vcpu->arch.hcall_needed = 0;
586         }
587
588         r = kvmppc_vcpu_run(run, vcpu);
589
590         if (vcpu->sigset_active)
591                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
592
593         return r;
594 }
595
596 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
597 {
598         if (irq->irq == KVM_INTERRUPT_UNSET) {
599                 kvmppc_core_dequeue_external(vcpu, irq);
600                 return 0;
601         }
602
603         kvmppc_core_queue_external(vcpu, irq);
604
605         kvm_vcpu_kick(vcpu);
606
607         return 0;
608 }
609
610 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
611                                      struct kvm_enable_cap *cap)
612 {
613         int r;
614
615         if (cap->flags)
616                 return -EINVAL;
617
618         switch (cap->cap) {
619         case KVM_CAP_PPC_OSI:
620                 r = 0;
621                 vcpu->arch.osi_enabled = true;
622                 break;
623         case KVM_CAP_PPC_PAPR:
624                 r = 0;
625                 vcpu->arch.papr_enabled = true;
626                 break;
627 #ifdef CONFIG_KVM_E500
628         case KVM_CAP_SW_TLB: {
629                 struct kvm_config_tlb cfg;
630                 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
631
632                 r = -EFAULT;
633                 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
634                         break;
635
636                 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
637                 break;
638         }
639 #endif
640         default:
641                 r = -EINVAL;
642                 break;
643         }
644
645         if (!r)
646                 r = kvmppc_sanity_check(vcpu);
647
648         return r;
649 }
650
651 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
652                                     struct kvm_mp_state *mp_state)
653 {
654         return -EINVAL;
655 }
656
657 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
658                                     struct kvm_mp_state *mp_state)
659 {
660         return -EINVAL;
661 }
662
663 long kvm_arch_vcpu_ioctl(struct file *filp,
664                          unsigned int ioctl, unsigned long arg)
665 {
666         struct kvm_vcpu *vcpu = filp->private_data;
667         void __user *argp = (void __user *)arg;
668         long r;
669
670         switch (ioctl) {
671         case KVM_INTERRUPT: {
672                 struct kvm_interrupt irq;
673                 r = -EFAULT;
674                 if (copy_from_user(&irq, argp, sizeof(irq)))
675                         goto out;
676                 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
677                 goto out;
678         }
679
680         case KVM_ENABLE_CAP:
681         {
682                 struct kvm_enable_cap cap;
683                 r = -EFAULT;
684                 if (copy_from_user(&cap, argp, sizeof(cap)))
685                         goto out;
686                 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
687                 break;
688         }
689
690         case KVM_SET_ONE_REG:
691         case KVM_GET_ONE_REG:
692         {
693                 struct kvm_one_reg reg;
694                 r = -EFAULT;
695                 if (copy_from_user(&reg, argp, sizeof(reg)))
696                         goto out;
697                 if (ioctl == KVM_SET_ONE_REG)
698                         r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
699                 else
700                         r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
701                 break;
702         }
703
704 #ifdef CONFIG_KVM_E500
705         case KVM_DIRTY_TLB: {
706                 struct kvm_dirty_tlb dirty;
707                 r = -EFAULT;
708                 if (copy_from_user(&dirty, argp, sizeof(dirty)))
709                         goto out;
710                 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
711                 break;
712         }
713 #endif
714
715         default:
716                 r = -EINVAL;
717         }
718
719 out:
720         return r;
721 }
722
723 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
724 {
725         return VM_FAULT_SIGBUS;
726 }
727
728 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
729 {
730         u32 inst_lis = 0x3c000000;
731         u32 inst_ori = 0x60000000;
732         u32 inst_nop = 0x60000000;
733         u32 inst_sc = 0x44000002;
734         u32 inst_imm_mask = 0xffff;
735
736         /*
737          * The hypercall to get into KVM from within guest context is as
738          * follows:
739          *
740          *    lis r0, r0, KVM_SC_MAGIC_R0@h
741          *    ori r0, KVM_SC_MAGIC_R0@l
742          *    sc
743          *    nop
744          */
745         pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
746         pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
747         pvinfo->hcall[2] = inst_sc;
748         pvinfo->hcall[3] = inst_nop;
749
750         return 0;
751 }
752
753 long kvm_arch_vm_ioctl(struct file *filp,
754                        unsigned int ioctl, unsigned long arg)
755 {
756         void __user *argp = (void __user *)arg;
757         long r;
758
759         switch (ioctl) {
760         case KVM_PPC_GET_PVINFO: {
761                 struct kvm_ppc_pvinfo pvinfo;
762                 memset(&pvinfo, 0, sizeof(pvinfo));
763                 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
764                 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
765                         r = -EFAULT;
766                         goto out;
767                 }
768
769                 break;
770         }
771 #ifdef CONFIG_KVM_BOOK3S_64_HV
772         case KVM_CREATE_SPAPR_TCE: {
773                 struct kvm_create_spapr_tce create_tce;
774                 struct kvm *kvm = filp->private_data;
775
776                 r = -EFAULT;
777                 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
778                         goto out;
779                 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
780                 goto out;
781         }
782
783         case KVM_ALLOCATE_RMA: {
784                 struct kvm *kvm = filp->private_data;
785                 struct kvm_allocate_rma rma;
786
787                 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
788                 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
789                         r = -EFAULT;
790                 break;
791         }
792 #endif /* CONFIG_KVM_BOOK3S_64_HV */
793
794         default:
795                 r = -ENOTTY;
796         }
797
798 out:
799         return r;
800 }
801
802 int kvm_arch_init(void *opaque)
803 {
804         return 0;
805 }
806
807 void kvm_arch_exit(void)
808 {
809 }