KVM: PPC: Reimplement LOAD_VMX/STORE_VMX instruction mmio emulation with analyse_inst...
[linux-2.6-microblaze.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/vmalloc.h>
25 #include <linux/hrtimer.h>
26 #include <linux/sched/signal.h>
27 #include <linux/fs.h>
28 #include <linux/slab.h>
29 #include <linux/file.h>
30 #include <linux/module.h>
31 #include <linux/irqbypass.h>
32 #include <linux/kvm_irqfd.h>
33 #include <asm/cputable.h>
34 #include <linux/uaccess.h>
35 #include <asm/kvm_ppc.h>
36 #include <asm/tlbflush.h>
37 #include <asm/cputhreads.h>
38 #include <asm/irqflags.h>
39 #include <asm/iommu.h>
40 #include <asm/switch_to.h>
41 #include <asm/xive.h>
42 #ifdef CONFIG_PPC_PSERIES
43 #include <asm/hvcall.h>
44 #include <asm/plpar_wrappers.h>
45 #endif
46
47 #include "timing.h"
48 #include "irq.h"
49 #include "../mm/mmu_decl.h"
50
51 #define CREATE_TRACE_POINTS
52 #include "trace.h"
53
54 struct kvmppc_ops *kvmppc_hv_ops;
55 EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
56 struct kvmppc_ops *kvmppc_pr_ops;
57 EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
58
59
60 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
61 {
62         return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
63 }
64
65 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
66 {
67         return false;
68 }
69
70 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
71 {
72         return 1;
73 }
74
75 /*
76  * Common checks before entering the guest world.  Call with interrupts
77  * disabled.
78  *
79  * returns:
80  *
81  * == 1 if we're ready to go into guest state
82  * <= 0 if we need to go back to the host with return value
83  */
84 int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
85 {
86         int r;
87
88         WARN_ON(irqs_disabled());
89         hard_irq_disable();
90
91         while (true) {
92                 if (need_resched()) {
93                         local_irq_enable();
94                         cond_resched();
95                         hard_irq_disable();
96                         continue;
97                 }
98
99                 if (signal_pending(current)) {
100                         kvmppc_account_exit(vcpu, SIGNAL_EXITS);
101                         vcpu->run->exit_reason = KVM_EXIT_INTR;
102                         r = -EINTR;
103                         break;
104                 }
105
106                 vcpu->mode = IN_GUEST_MODE;
107
108                 /*
109                  * Reading vcpu->requests must happen after setting vcpu->mode,
110                  * so we don't miss a request because the requester sees
111                  * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
112                  * before next entering the guest (and thus doesn't IPI).
113                  * This also orders the write to mode from any reads
114                  * to the page tables done while the VCPU is running.
115                  * Please see the comment in kvm_flush_remote_tlbs.
116                  */
117                 smp_mb();
118
119                 if (kvm_request_pending(vcpu)) {
120                         /* Make sure we process requests preemptable */
121                         local_irq_enable();
122                         trace_kvm_check_requests(vcpu);
123                         r = kvmppc_core_check_requests(vcpu);
124                         hard_irq_disable();
125                         if (r > 0)
126                                 continue;
127                         break;
128                 }
129
130                 if (kvmppc_core_prepare_to_enter(vcpu)) {
131                         /* interrupts got enabled in between, so we
132                            are back at square 1 */
133                         continue;
134                 }
135
136                 guest_enter_irqoff();
137                 return 1;
138         }
139
140         /* return to host */
141         local_irq_enable();
142         return r;
143 }
144 EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
145
146 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
147 static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
148 {
149         struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
150         int i;
151
152         shared->sprg0 = swab64(shared->sprg0);
153         shared->sprg1 = swab64(shared->sprg1);
154         shared->sprg2 = swab64(shared->sprg2);
155         shared->sprg3 = swab64(shared->sprg3);
156         shared->srr0 = swab64(shared->srr0);
157         shared->srr1 = swab64(shared->srr1);
158         shared->dar = swab64(shared->dar);
159         shared->msr = swab64(shared->msr);
160         shared->dsisr = swab32(shared->dsisr);
161         shared->int_pending = swab32(shared->int_pending);
162         for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
163                 shared->sr[i] = swab32(shared->sr[i]);
164 }
165 #endif
166
167 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
168 {
169         int nr = kvmppc_get_gpr(vcpu, 11);
170         int r;
171         unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
172         unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
173         unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
174         unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
175         unsigned long r2 = 0;
176
177         if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
178                 /* 32 bit mode */
179                 param1 &= 0xffffffff;
180                 param2 &= 0xffffffff;
181                 param3 &= 0xffffffff;
182                 param4 &= 0xffffffff;
183         }
184
185         switch (nr) {
186         case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
187         {
188 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
189                 /* Book3S can be little endian, find it out here */
190                 int shared_big_endian = true;
191                 if (vcpu->arch.intr_msr & MSR_LE)
192                         shared_big_endian = false;
193                 if (shared_big_endian != vcpu->arch.shared_big_endian)
194                         kvmppc_swab_shared(vcpu);
195                 vcpu->arch.shared_big_endian = shared_big_endian;
196 #endif
197
198                 if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
199                         /*
200                          * Older versions of the Linux magic page code had
201                          * a bug where they would map their trampoline code
202                          * NX. If that's the case, remove !PR NX capability.
203                          */
204                         vcpu->arch.disable_kernel_nx = true;
205                         kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
206                 }
207
208                 vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
209                 vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
210
211 #ifdef CONFIG_PPC_64K_PAGES
212                 /*
213                  * Make sure our 4k magic page is in the same window of a 64k
214                  * page within the guest and within the host's page.
215                  */
216                 if ((vcpu->arch.magic_page_pa & 0xf000) !=
217                     ((ulong)vcpu->arch.shared & 0xf000)) {
218                         void *old_shared = vcpu->arch.shared;
219                         ulong shared = (ulong)vcpu->arch.shared;
220                         void *new_shared;
221
222                         shared &= PAGE_MASK;
223                         shared |= vcpu->arch.magic_page_pa & 0xf000;
224                         new_shared = (void*)shared;
225                         memcpy(new_shared, old_shared, 0x1000);
226                         vcpu->arch.shared = new_shared;
227                 }
228 #endif
229
230                 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
231
232                 r = EV_SUCCESS;
233                 break;
234         }
235         case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
236                 r = EV_SUCCESS;
237 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
238                 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
239 #endif
240
241                 /* Second return value is in r4 */
242                 break;
243         case EV_HCALL_TOKEN(EV_IDLE):
244                 r = EV_SUCCESS;
245                 kvm_vcpu_block(vcpu);
246                 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
247                 break;
248         default:
249                 r = EV_UNIMPLEMENTED;
250                 break;
251         }
252
253         kvmppc_set_gpr(vcpu, 4, r2);
254
255         return r;
256 }
257 EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
258
259 int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
260 {
261         int r = false;
262
263         /* We have to know what CPU to virtualize */
264         if (!vcpu->arch.pvr)
265                 goto out;
266
267         /* PAPR only works with book3s_64 */
268         if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
269                 goto out;
270
271         /* HV KVM can only do PAPR mode for now */
272         if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
273                 goto out;
274
275 #ifdef CONFIG_KVM_BOOKE_HV
276         if (!cpu_has_feature(CPU_FTR_EMB_HV))
277                 goto out;
278 #endif
279
280         r = true;
281
282 out:
283         vcpu->arch.sane = r;
284         return r ? 0 : -EINVAL;
285 }
286 EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
287
288 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
289 {
290         enum emulation_result er;
291         int r;
292
293         er = kvmppc_emulate_loadstore(vcpu);
294         switch (er) {
295         case EMULATE_DONE:
296                 /* Future optimization: only reload non-volatiles if they were
297                  * actually modified. */
298                 r = RESUME_GUEST_NV;
299                 break;
300         case EMULATE_AGAIN:
301                 r = RESUME_GUEST;
302                 break;
303         case EMULATE_DO_MMIO:
304                 run->exit_reason = KVM_EXIT_MMIO;
305                 /* We must reload nonvolatiles because "update" load/store
306                  * instructions modify register state. */
307                 /* Future optimization: only reload non-volatiles if they were
308                  * actually modified. */
309                 r = RESUME_HOST_NV;
310                 break;
311         case EMULATE_FAIL:
312         {
313                 u32 last_inst;
314
315                 kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
316                 /* XXX Deliver Program interrupt to guest. */
317                 pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
318                 r = RESUME_HOST;
319                 break;
320         }
321         default:
322                 WARN_ON(1);
323                 r = RESUME_GUEST;
324         }
325
326         return r;
327 }
328 EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
329
330 int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
331               bool data)
332 {
333         ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
334         struct kvmppc_pte pte;
335         int r;
336
337         vcpu->stat.st++;
338
339         r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
340                          XLATE_WRITE, &pte);
341         if (r < 0)
342                 return r;
343
344         *eaddr = pte.raddr;
345
346         if (!pte.may_write)
347                 return -EPERM;
348
349         /* Magic page override */
350         if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
351             ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
352             !(kvmppc_get_msr(vcpu) & MSR_PR)) {
353                 void *magic = vcpu->arch.shared;
354                 magic += pte.eaddr & 0xfff;
355                 memcpy(magic, ptr, size);
356                 return EMULATE_DONE;
357         }
358
359         if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
360                 return EMULATE_DO_MMIO;
361
362         return EMULATE_DONE;
363 }
364 EXPORT_SYMBOL_GPL(kvmppc_st);
365
366 int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
367                       bool data)
368 {
369         ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
370         struct kvmppc_pte pte;
371         int rc;
372
373         vcpu->stat.ld++;
374
375         rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
376                           XLATE_READ, &pte);
377         if (rc)
378                 return rc;
379
380         *eaddr = pte.raddr;
381
382         if (!pte.may_read)
383                 return -EPERM;
384
385         if (!data && !pte.may_execute)
386                 return -ENOEXEC;
387
388         /* Magic page override */
389         if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
390             ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
391             !(kvmppc_get_msr(vcpu) & MSR_PR)) {
392                 void *magic = vcpu->arch.shared;
393                 magic += pte.eaddr & 0xfff;
394                 memcpy(ptr, magic, size);
395                 return EMULATE_DONE;
396         }
397
398         if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
399                 return EMULATE_DO_MMIO;
400
401         return EMULATE_DONE;
402 }
403 EXPORT_SYMBOL_GPL(kvmppc_ld);
404
405 int kvm_arch_hardware_enable(void)
406 {
407         return 0;
408 }
409
410 int kvm_arch_hardware_setup(void)
411 {
412         return 0;
413 }
414
415 void kvm_arch_check_processor_compat(void *rtn)
416 {
417         *(int *)rtn = kvmppc_core_check_processor_compat();
418 }
419
420 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
421 {
422         struct kvmppc_ops *kvm_ops = NULL;
423         /*
424          * if we have both HV and PR enabled, default is HV
425          */
426         if (type == 0) {
427                 if (kvmppc_hv_ops)
428                         kvm_ops = kvmppc_hv_ops;
429                 else
430                         kvm_ops = kvmppc_pr_ops;
431                 if (!kvm_ops)
432                         goto err_out;
433         } else  if (type == KVM_VM_PPC_HV) {
434                 if (!kvmppc_hv_ops)
435                         goto err_out;
436                 kvm_ops = kvmppc_hv_ops;
437         } else if (type == KVM_VM_PPC_PR) {
438                 if (!kvmppc_pr_ops)
439                         goto err_out;
440                 kvm_ops = kvmppc_pr_ops;
441         } else
442                 goto err_out;
443
444         if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
445                 return -ENOENT;
446
447         kvm->arch.kvm_ops = kvm_ops;
448         return kvmppc_core_init_vm(kvm);
449 err_out:
450         return -EINVAL;
451 }
452
453 bool kvm_arch_has_vcpu_debugfs(void)
454 {
455         return false;
456 }
457
458 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
459 {
460         return 0;
461 }
462
463 void kvm_arch_destroy_vm(struct kvm *kvm)
464 {
465         unsigned int i;
466         struct kvm_vcpu *vcpu;
467
468 #ifdef CONFIG_KVM_XICS
469         /*
470          * We call kick_all_cpus_sync() to ensure that all
471          * CPUs have executed any pending IPIs before we
472          * continue and free VCPUs structures below.
473          */
474         if (is_kvmppc_hv_enabled(kvm))
475                 kick_all_cpus_sync();
476 #endif
477
478         kvm_for_each_vcpu(i, vcpu, kvm)
479                 kvm_arch_vcpu_free(vcpu);
480
481         mutex_lock(&kvm->lock);
482         for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
483                 kvm->vcpus[i] = NULL;
484
485         atomic_set(&kvm->online_vcpus, 0);
486
487         kvmppc_core_destroy_vm(kvm);
488
489         mutex_unlock(&kvm->lock);
490
491         /* drop the module reference */
492         module_put(kvm->arch.kvm_ops->owner);
493 }
494
495 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
496 {
497         int r;
498         /* Assume we're using HV mode when the HV module is loaded */
499         int hv_enabled = kvmppc_hv_ops ? 1 : 0;
500
501         if (kvm) {
502                 /*
503                  * Hooray - we know which VM type we're running on. Depend on
504                  * that rather than the guess above.
505                  */
506                 hv_enabled = is_kvmppc_hv_enabled(kvm);
507         }
508
509         switch (ext) {
510 #ifdef CONFIG_BOOKE
511         case KVM_CAP_PPC_BOOKE_SREGS:
512         case KVM_CAP_PPC_BOOKE_WATCHDOG:
513         case KVM_CAP_PPC_EPR:
514 #else
515         case KVM_CAP_PPC_SEGSTATE:
516         case KVM_CAP_PPC_HIOR:
517         case KVM_CAP_PPC_PAPR:
518 #endif
519         case KVM_CAP_PPC_UNSET_IRQ:
520         case KVM_CAP_PPC_IRQ_LEVEL:
521         case KVM_CAP_ENABLE_CAP:
522         case KVM_CAP_ENABLE_CAP_VM:
523         case KVM_CAP_ONE_REG:
524         case KVM_CAP_IOEVENTFD:
525         case KVM_CAP_DEVICE_CTRL:
526         case KVM_CAP_IMMEDIATE_EXIT:
527                 r = 1;
528                 break;
529         case KVM_CAP_PPC_PAIRED_SINGLES:
530         case KVM_CAP_PPC_OSI:
531         case KVM_CAP_PPC_GET_PVINFO:
532 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
533         case KVM_CAP_SW_TLB:
534 #endif
535                 /* We support this only for PR */
536                 r = !hv_enabled;
537                 break;
538 #ifdef CONFIG_KVM_MPIC
539         case KVM_CAP_IRQ_MPIC:
540                 r = 1;
541                 break;
542 #endif
543
544 #ifdef CONFIG_PPC_BOOK3S_64
545         case KVM_CAP_SPAPR_TCE:
546         case KVM_CAP_SPAPR_TCE_64:
547                 /* fallthrough */
548         case KVM_CAP_SPAPR_TCE_VFIO:
549         case KVM_CAP_PPC_RTAS:
550         case KVM_CAP_PPC_FIXUP_HCALL:
551         case KVM_CAP_PPC_ENABLE_HCALL:
552 #ifdef CONFIG_KVM_XICS
553         case KVM_CAP_IRQ_XICS:
554 #endif
555         case KVM_CAP_PPC_GET_CPU_CHAR:
556                 r = 1;
557                 break;
558
559         case KVM_CAP_PPC_ALLOC_HTAB:
560                 r = hv_enabled;
561                 break;
562 #endif /* CONFIG_PPC_BOOK3S_64 */
563 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
564         case KVM_CAP_PPC_SMT:
565                 r = 0;
566                 if (kvm) {
567                         if (kvm->arch.emul_smt_mode > 1)
568                                 r = kvm->arch.emul_smt_mode;
569                         else
570                                 r = kvm->arch.smt_mode;
571                 } else if (hv_enabled) {
572                         if (cpu_has_feature(CPU_FTR_ARCH_300))
573                                 r = 1;
574                         else
575                                 r = threads_per_subcore;
576                 }
577                 break;
578         case KVM_CAP_PPC_SMT_POSSIBLE:
579                 r = 1;
580                 if (hv_enabled) {
581                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
582                                 r = ((threads_per_subcore << 1) - 1);
583                         else
584                                 /* P9 can emulate dbells, so allow any mode */
585                                 r = 8 | 4 | 2 | 1;
586                 }
587                 break;
588         case KVM_CAP_PPC_RMA:
589                 r = 0;
590                 break;
591         case KVM_CAP_PPC_HWRNG:
592                 r = kvmppc_hwrng_present();
593                 break;
594         case KVM_CAP_PPC_MMU_RADIX:
595                 r = !!(hv_enabled && radix_enabled());
596                 break;
597         case KVM_CAP_PPC_MMU_HASH_V3:
598                 r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300));
599                 break;
600 #endif
601         case KVM_CAP_SYNC_MMU:
602 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
603                 r = hv_enabled;
604 #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
605                 r = 1;
606 #else
607                 r = 0;
608 #endif
609                 break;
610 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
611         case KVM_CAP_PPC_HTAB_FD:
612                 r = hv_enabled;
613                 break;
614 #endif
615         case KVM_CAP_NR_VCPUS:
616                 /*
617                  * Recommending a number of CPUs is somewhat arbitrary; we
618                  * return the number of present CPUs for -HV (since a host
619                  * will have secondary threads "offline"), and for other KVM
620                  * implementations just count online CPUs.
621                  */
622                 if (hv_enabled)
623                         r = num_present_cpus();
624                 else
625                         r = num_online_cpus();
626                 break;
627         case KVM_CAP_NR_MEMSLOTS:
628                 r = KVM_USER_MEM_SLOTS;
629                 break;
630         case KVM_CAP_MAX_VCPUS:
631                 r = KVM_MAX_VCPUS;
632                 break;
633 #ifdef CONFIG_PPC_BOOK3S_64
634         case KVM_CAP_PPC_GET_SMMU_INFO:
635                 r = 1;
636                 break;
637         case KVM_CAP_SPAPR_MULTITCE:
638                 r = 1;
639                 break;
640         case KVM_CAP_SPAPR_RESIZE_HPT:
641                 r = !!hv_enabled;
642                 break;
643 #endif
644 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
645         case KVM_CAP_PPC_FWNMI:
646                 r = hv_enabled;
647                 break;
648 #endif
649 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
650         case KVM_CAP_PPC_HTM:
651                 r = hv_enabled &&
652                     (!!(cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_HTM) ||
653                      cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST));
654                 break;
655 #endif
656         default:
657                 r = 0;
658                 break;
659         }
660         return r;
661
662 }
663
664 long kvm_arch_dev_ioctl(struct file *filp,
665                         unsigned int ioctl, unsigned long arg)
666 {
667         return -EINVAL;
668 }
669
670 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
671                            struct kvm_memory_slot *dont)
672 {
673         kvmppc_core_free_memslot(kvm, free, dont);
674 }
675
676 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
677                             unsigned long npages)
678 {
679         return kvmppc_core_create_memslot(kvm, slot, npages);
680 }
681
682 int kvm_arch_prepare_memory_region(struct kvm *kvm,
683                                    struct kvm_memory_slot *memslot,
684                                    const struct kvm_userspace_memory_region *mem,
685                                    enum kvm_mr_change change)
686 {
687         return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
688 }
689
690 void kvm_arch_commit_memory_region(struct kvm *kvm,
691                                    const struct kvm_userspace_memory_region *mem,
692                                    const struct kvm_memory_slot *old,
693                                    const struct kvm_memory_slot *new,
694                                    enum kvm_mr_change change)
695 {
696         kvmppc_core_commit_memory_region(kvm, mem, old, new);
697 }
698
699 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
700                                    struct kvm_memory_slot *slot)
701 {
702         kvmppc_core_flush_memslot(kvm, slot);
703 }
704
705 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
706 {
707         struct kvm_vcpu *vcpu;
708         vcpu = kvmppc_core_vcpu_create(kvm, id);
709         if (!IS_ERR(vcpu)) {
710                 vcpu->arch.wqp = &vcpu->wq;
711                 kvmppc_create_vcpu_debugfs(vcpu, id);
712         }
713         return vcpu;
714 }
715
716 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
717 {
718 }
719
720 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
721 {
722         /* Make sure we're not using the vcpu anymore */
723         hrtimer_cancel(&vcpu->arch.dec_timer);
724
725         kvmppc_remove_vcpu_debugfs(vcpu);
726
727         switch (vcpu->arch.irq_type) {
728         case KVMPPC_IRQ_MPIC:
729                 kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
730                 break;
731         case KVMPPC_IRQ_XICS:
732                 if (xive_enabled())
733                         kvmppc_xive_cleanup_vcpu(vcpu);
734                 else
735                         kvmppc_xics_free_icp(vcpu);
736                 break;
737         }
738
739         kvmppc_core_vcpu_free(vcpu);
740 }
741
742 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
743 {
744         kvm_arch_vcpu_free(vcpu);
745 }
746
747 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
748 {
749         return kvmppc_core_pending_dec(vcpu);
750 }
751
752 static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
753 {
754         struct kvm_vcpu *vcpu;
755
756         vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
757         kvmppc_decrementer_func(vcpu);
758
759         return HRTIMER_NORESTART;
760 }
761
762 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
763 {
764         int ret;
765
766         hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
767         vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
768         vcpu->arch.dec_expires = get_tb();
769
770 #ifdef CONFIG_KVM_EXIT_TIMING
771         mutex_init(&vcpu->arch.exit_timing_lock);
772 #endif
773         ret = kvmppc_subarch_vcpu_init(vcpu);
774         return ret;
775 }
776
777 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
778 {
779         kvmppc_mmu_destroy(vcpu);
780         kvmppc_subarch_vcpu_uninit(vcpu);
781 }
782
783 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
784 {
785 #ifdef CONFIG_BOOKE
786         /*
787          * vrsave (formerly usprg0) isn't used by Linux, but may
788          * be used by the guest.
789          *
790          * On non-booke this is associated with Altivec and
791          * is handled by code in book3s.c.
792          */
793         mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
794 #endif
795         kvmppc_core_vcpu_load(vcpu, cpu);
796 }
797
798 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
799 {
800         kvmppc_core_vcpu_put(vcpu);
801 #ifdef CONFIG_BOOKE
802         vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
803 #endif
804 }
805
806 /*
807  * irq_bypass_add_producer and irq_bypass_del_producer are only
808  * useful if the architecture supports PCI passthrough.
809  * irq_bypass_stop and irq_bypass_start are not needed and so
810  * kvm_ops are not defined for them.
811  */
812 bool kvm_arch_has_irq_bypass(void)
813 {
814         return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
815                 (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
816 }
817
818 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
819                                      struct irq_bypass_producer *prod)
820 {
821         struct kvm_kernel_irqfd *irqfd =
822                 container_of(cons, struct kvm_kernel_irqfd, consumer);
823         struct kvm *kvm = irqfd->kvm;
824
825         if (kvm->arch.kvm_ops->irq_bypass_add_producer)
826                 return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
827
828         return 0;
829 }
830
831 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
832                                       struct irq_bypass_producer *prod)
833 {
834         struct kvm_kernel_irqfd *irqfd =
835                 container_of(cons, struct kvm_kernel_irqfd, consumer);
836         struct kvm *kvm = irqfd->kvm;
837
838         if (kvm->arch.kvm_ops->irq_bypass_del_producer)
839                 kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
840 }
841
842 #ifdef CONFIG_VSX
843 static inline int kvmppc_get_vsr_dword_offset(int index)
844 {
845         int offset;
846
847         if ((index != 0) && (index != 1))
848                 return -1;
849
850 #ifdef __BIG_ENDIAN
851         offset =  index;
852 #else
853         offset = 1 - index;
854 #endif
855
856         return offset;
857 }
858
859 static inline int kvmppc_get_vsr_word_offset(int index)
860 {
861         int offset;
862
863         if ((index > 3) || (index < 0))
864                 return -1;
865
866 #ifdef __BIG_ENDIAN
867         offset = index;
868 #else
869         offset = 3 - index;
870 #endif
871         return offset;
872 }
873
874 static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu,
875         u64 gpr)
876 {
877         union kvmppc_one_reg val;
878         int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
879         int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
880
881         if (offset == -1)
882                 return;
883
884         if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
885                 val.vval = VCPU_VSX_VR(vcpu, index);
886                 val.vsxval[offset] = gpr;
887                 VCPU_VSX_VR(vcpu, index) = val.vval;
888         } else {
889                 VCPU_VSX_FPR(vcpu, index, offset) = gpr;
890         }
891 }
892
893 static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu,
894         u64 gpr)
895 {
896         union kvmppc_one_reg val;
897         int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
898
899         if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
900                 val.vval = VCPU_VSX_VR(vcpu, index);
901                 val.vsxval[0] = gpr;
902                 val.vsxval[1] = gpr;
903                 VCPU_VSX_VR(vcpu, index) = val.vval;
904         } else {
905                 VCPU_VSX_FPR(vcpu, index, 0) = gpr;
906                 VCPU_VSX_FPR(vcpu, index, 1) = gpr;
907         }
908 }
909
910 static inline void kvmppc_set_vsr_word_dump(struct kvm_vcpu *vcpu,
911         u32 gpr)
912 {
913         union kvmppc_one_reg val;
914         int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
915
916         if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
917                 val.vsx32val[0] = gpr;
918                 val.vsx32val[1] = gpr;
919                 val.vsx32val[2] = gpr;
920                 val.vsx32val[3] = gpr;
921                 VCPU_VSX_VR(vcpu, index) = val.vval;
922         } else {
923                 val.vsx32val[0] = gpr;
924                 val.vsx32val[1] = gpr;
925                 VCPU_VSX_FPR(vcpu, index, 0) = val.vsxval[0];
926                 VCPU_VSX_FPR(vcpu, index, 1) = val.vsxval[0];
927         }
928 }
929
930 static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu,
931         u32 gpr32)
932 {
933         union kvmppc_one_reg val;
934         int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
935         int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
936         int dword_offset, word_offset;
937
938         if (offset == -1)
939                 return;
940
941         if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
942                 val.vval = VCPU_VSX_VR(vcpu, index);
943                 val.vsx32val[offset] = gpr32;
944                 VCPU_VSX_VR(vcpu, index) = val.vval;
945         } else {
946                 dword_offset = offset / 2;
947                 word_offset = offset % 2;
948                 val.vsxval[0] = VCPU_VSX_FPR(vcpu, index, dword_offset);
949                 val.vsx32val[word_offset] = gpr32;
950                 VCPU_VSX_FPR(vcpu, index, dword_offset) = val.vsxval[0];
951         }
952 }
953 #endif /* CONFIG_VSX */
954
955 #ifdef CONFIG_ALTIVEC
956 static inline int kvmppc_get_vmx_offset_generic(struct kvm_vcpu *vcpu,
957                 int index, int element_size)
958 {
959         int offset;
960         int elts = sizeof(vector128)/element_size;
961
962         if ((index < 0) || (index >= elts))
963                 return -1;
964
965         if (kvmppc_need_byteswap(vcpu))
966                 offset = elts - index - 1;
967         else
968                 offset = index;
969
970         return offset;
971 }
972
973 static inline int kvmppc_get_vmx_dword_offset(struct kvm_vcpu *vcpu,
974                 int index)
975 {
976         return kvmppc_get_vmx_offset_generic(vcpu, index, 8);
977 }
978
979 static inline int kvmppc_get_vmx_word_offset(struct kvm_vcpu *vcpu,
980                 int index)
981 {
982         return kvmppc_get_vmx_offset_generic(vcpu, index, 4);
983 }
984
985 static inline int kvmppc_get_vmx_hword_offset(struct kvm_vcpu *vcpu,
986                 int index)
987 {
988         return kvmppc_get_vmx_offset_generic(vcpu, index, 2);
989 }
990
991 static inline int kvmppc_get_vmx_byte_offset(struct kvm_vcpu *vcpu,
992                 int index)
993 {
994         return kvmppc_get_vmx_offset_generic(vcpu, index, 1);
995 }
996
997
998 static inline void kvmppc_set_vmx_dword(struct kvm_vcpu *vcpu,
999         u64 gpr)
1000 {
1001         union kvmppc_one_reg val;
1002         int offset = kvmppc_get_vmx_dword_offset(vcpu,
1003                         vcpu->arch.mmio_vmx_offset);
1004         int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1005
1006         if (offset == -1)
1007                 return;
1008
1009         val.vval = VCPU_VSX_VR(vcpu, index);
1010         val.vsxval[offset] = gpr;
1011         VCPU_VSX_VR(vcpu, index) = val.vval;
1012 }
1013
1014 static inline void kvmppc_set_vmx_word(struct kvm_vcpu *vcpu,
1015         u32 gpr32)
1016 {
1017         union kvmppc_one_reg val;
1018         int offset = kvmppc_get_vmx_word_offset(vcpu,
1019                         vcpu->arch.mmio_vmx_offset);
1020         int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1021
1022         if (offset == -1)
1023                 return;
1024
1025         val.vval = VCPU_VSX_VR(vcpu, index);
1026         val.vsx32val[offset] = gpr32;
1027         VCPU_VSX_VR(vcpu, index) = val.vval;
1028 }
1029
1030 static inline void kvmppc_set_vmx_hword(struct kvm_vcpu *vcpu,
1031         u16 gpr16)
1032 {
1033         union kvmppc_one_reg val;
1034         int offset = kvmppc_get_vmx_hword_offset(vcpu,
1035                         vcpu->arch.mmio_vmx_offset);
1036         int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1037
1038         if (offset == -1)
1039                 return;
1040
1041         val.vval = VCPU_VSX_VR(vcpu, index);
1042         val.vsx16val[offset] = gpr16;
1043         VCPU_VSX_VR(vcpu, index) = val.vval;
1044 }
1045
1046 static inline void kvmppc_set_vmx_byte(struct kvm_vcpu *vcpu,
1047         u8 gpr8)
1048 {
1049         union kvmppc_one_reg val;
1050         int offset = kvmppc_get_vmx_byte_offset(vcpu,
1051                         vcpu->arch.mmio_vmx_offset);
1052         int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1053
1054         if (offset == -1)
1055                 return;
1056
1057         val.vval = VCPU_VSX_VR(vcpu, index);
1058         val.vsx8val[offset] = gpr8;
1059         VCPU_VSX_VR(vcpu, index) = val.vval;
1060 }
1061 #endif /* CONFIG_ALTIVEC */
1062
1063 #ifdef CONFIG_PPC_FPU
1064 static inline u64 sp_to_dp(u32 fprs)
1065 {
1066         u64 fprd;
1067
1068         preempt_disable();
1069         enable_kernel_fp();
1070         asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m" (fprd) : "m" (fprs)
1071              : "fr0");
1072         preempt_enable();
1073         return fprd;
1074 }
1075
1076 static inline u32 dp_to_sp(u64 fprd)
1077 {
1078         u32 fprs;
1079
1080         preempt_disable();
1081         enable_kernel_fp();
1082         asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m" (fprs) : "m" (fprd)
1083              : "fr0");
1084         preempt_enable();
1085         return fprs;
1086 }
1087
1088 #else
1089 #define sp_to_dp(x)     (x)
1090 #define dp_to_sp(x)     (x)
1091 #endif /* CONFIG_PPC_FPU */
1092
1093 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
1094                                       struct kvm_run *run)
1095 {
1096         u64 uninitialized_var(gpr);
1097
1098         if (run->mmio.len > sizeof(gpr)) {
1099                 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
1100                 return;
1101         }
1102
1103         if (!vcpu->arch.mmio_host_swabbed) {
1104                 switch (run->mmio.len) {
1105                 case 8: gpr = *(u64 *)run->mmio.data; break;
1106                 case 4: gpr = *(u32 *)run->mmio.data; break;
1107                 case 2: gpr = *(u16 *)run->mmio.data; break;
1108                 case 1: gpr = *(u8 *)run->mmio.data; break;
1109                 }
1110         } else {
1111                 switch (run->mmio.len) {
1112                 case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
1113                 case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
1114                 case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
1115                 case 1: gpr = *(u8 *)run->mmio.data; break;
1116                 }
1117         }
1118
1119         /* conversion between single and double precision */
1120         if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4))
1121                 gpr = sp_to_dp(gpr);
1122
1123         if (vcpu->arch.mmio_sign_extend) {
1124                 switch (run->mmio.len) {
1125 #ifdef CONFIG_PPC64
1126                 case 4:
1127                         gpr = (s64)(s32)gpr;
1128                         break;
1129 #endif
1130                 case 2:
1131                         gpr = (s64)(s16)gpr;
1132                         break;
1133                 case 1:
1134                         gpr = (s64)(s8)gpr;
1135                         break;
1136                 }
1137         }
1138
1139         switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
1140         case KVM_MMIO_REG_GPR:
1141                 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
1142                 break;
1143         case KVM_MMIO_REG_FPR:
1144                 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1145                         vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_FP);
1146
1147                 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1148                 break;
1149 #ifdef CONFIG_PPC_BOOK3S
1150         case KVM_MMIO_REG_QPR:
1151                 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1152                 break;
1153         case KVM_MMIO_REG_FQPR:
1154                 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1155                 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1156                 break;
1157 #endif
1158 #ifdef CONFIG_VSX
1159         case KVM_MMIO_REG_VSX:
1160                 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1161                         vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VSX);
1162
1163                 if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_DWORD)
1164                         kvmppc_set_vsr_dword(vcpu, gpr);
1165                 else if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_WORD)
1166                         kvmppc_set_vsr_word(vcpu, gpr);
1167                 else if (vcpu->arch.mmio_copy_type ==
1168                                 KVMPPC_VSX_COPY_DWORD_LOAD_DUMP)
1169                         kvmppc_set_vsr_dword_dump(vcpu, gpr);
1170                 else if (vcpu->arch.mmio_copy_type ==
1171                                 KVMPPC_VSX_COPY_WORD_LOAD_DUMP)
1172                         kvmppc_set_vsr_word_dump(vcpu, gpr);
1173                 break;
1174 #endif
1175 #ifdef CONFIG_ALTIVEC
1176         case KVM_MMIO_REG_VMX:
1177                 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1178                         vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VEC);
1179
1180                 if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_DWORD)
1181                         kvmppc_set_vmx_dword(vcpu, gpr);
1182                 else if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_WORD)
1183                         kvmppc_set_vmx_word(vcpu, gpr);
1184                 else if (vcpu->arch.mmio_copy_type ==
1185                                 KVMPPC_VMX_COPY_HWORD)
1186                         kvmppc_set_vmx_hword(vcpu, gpr);
1187                 else if (vcpu->arch.mmio_copy_type ==
1188                                 KVMPPC_VMX_COPY_BYTE)
1189                         kvmppc_set_vmx_byte(vcpu, gpr);
1190                 break;
1191 #endif
1192         default:
1193                 BUG();
1194         }
1195 }
1196
1197 static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1198                                 unsigned int rt, unsigned int bytes,
1199                                 int is_default_endian, int sign_extend)
1200 {
1201         int idx, ret;
1202         bool host_swabbed;
1203
1204         /* Pity C doesn't have a logical XOR operator */
1205         if (kvmppc_need_byteswap(vcpu)) {
1206                 host_swabbed = is_default_endian;
1207         } else {
1208                 host_swabbed = !is_default_endian;
1209         }
1210
1211         if (bytes > sizeof(run->mmio.data)) {
1212                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1213                        run->mmio.len);
1214         }
1215
1216         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1217         run->mmio.len = bytes;
1218         run->mmio.is_write = 0;
1219
1220         vcpu->arch.io_gpr = rt;
1221         vcpu->arch.mmio_host_swabbed = host_swabbed;
1222         vcpu->mmio_needed = 1;
1223         vcpu->mmio_is_write = 0;
1224         vcpu->arch.mmio_sign_extend = sign_extend;
1225
1226         idx = srcu_read_lock(&vcpu->kvm->srcu);
1227
1228         ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1229                               bytes, &run->mmio.data);
1230
1231         srcu_read_unlock(&vcpu->kvm->srcu, idx);
1232
1233         if (!ret) {
1234                 kvmppc_complete_mmio_load(vcpu, run);
1235                 vcpu->mmio_needed = 0;
1236                 return EMULATE_DONE;
1237         }
1238
1239         return EMULATE_DO_MMIO;
1240 }
1241
1242 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1243                        unsigned int rt, unsigned int bytes,
1244                        int is_default_endian)
1245 {
1246         return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0);
1247 }
1248 EXPORT_SYMBOL_GPL(kvmppc_handle_load);
1249
1250 /* Same as above, but sign extends */
1251 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
1252                         unsigned int rt, unsigned int bytes,
1253                         int is_default_endian)
1254 {
1255         return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1);
1256 }
1257
1258 #ifdef CONFIG_VSX
1259 int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1260                         unsigned int rt, unsigned int bytes,
1261                         int is_default_endian, int mmio_sign_extend)
1262 {
1263         enum emulation_result emulated = EMULATE_DONE;
1264
1265         /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1266         if (vcpu->arch.mmio_vsx_copy_nums > 4)
1267                 return EMULATE_FAIL;
1268
1269         while (vcpu->arch.mmio_vsx_copy_nums) {
1270                 emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
1271                         is_default_endian, mmio_sign_extend);
1272
1273                 if (emulated != EMULATE_DONE)
1274                         break;
1275
1276                 vcpu->arch.paddr_accessed += run->mmio.len;
1277
1278                 vcpu->arch.mmio_vsx_copy_nums--;
1279                 vcpu->arch.mmio_vsx_offset++;
1280         }
1281         return emulated;
1282 }
1283 #endif /* CONFIG_VSX */
1284
1285 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1286                         u64 val, unsigned int bytes, int is_default_endian)
1287 {
1288         void *data = run->mmio.data;
1289         int idx, ret;
1290         bool host_swabbed;
1291
1292         /* Pity C doesn't have a logical XOR operator */
1293         if (kvmppc_need_byteswap(vcpu)) {
1294                 host_swabbed = is_default_endian;
1295         } else {
1296                 host_swabbed = !is_default_endian;
1297         }
1298
1299         if (bytes > sizeof(run->mmio.data)) {
1300                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1301                        run->mmio.len);
1302         }
1303
1304         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1305         run->mmio.len = bytes;
1306         run->mmio.is_write = 1;
1307         vcpu->mmio_needed = 1;
1308         vcpu->mmio_is_write = 1;
1309
1310         if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4))
1311                 val = dp_to_sp(val);
1312
1313         /* Store the value at the lowest bytes in 'data'. */
1314         if (!host_swabbed) {
1315                 switch (bytes) {
1316                 case 8: *(u64 *)data = val; break;
1317                 case 4: *(u32 *)data = val; break;
1318                 case 2: *(u16 *)data = val; break;
1319                 case 1: *(u8  *)data = val; break;
1320                 }
1321         } else {
1322                 switch (bytes) {
1323                 case 8: *(u64 *)data = swab64(val); break;
1324                 case 4: *(u32 *)data = swab32(val); break;
1325                 case 2: *(u16 *)data = swab16(val); break;
1326                 case 1: *(u8  *)data = val; break;
1327                 }
1328         }
1329
1330         idx = srcu_read_lock(&vcpu->kvm->srcu);
1331
1332         ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1333                                bytes, &run->mmio.data);
1334
1335         srcu_read_unlock(&vcpu->kvm->srcu, idx);
1336
1337         if (!ret) {
1338                 vcpu->mmio_needed = 0;
1339                 return EMULATE_DONE;
1340         }
1341
1342         return EMULATE_DO_MMIO;
1343 }
1344 EXPORT_SYMBOL_GPL(kvmppc_handle_store);
1345
1346 #ifdef CONFIG_VSX
1347 static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
1348 {
1349         u32 dword_offset, word_offset;
1350         union kvmppc_one_reg reg;
1351         int vsx_offset = 0;
1352         int copy_type = vcpu->arch.mmio_copy_type;
1353         int result = 0;
1354
1355         switch (copy_type) {
1356         case KVMPPC_VSX_COPY_DWORD:
1357                 vsx_offset =
1358                         kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
1359
1360                 if (vsx_offset == -1) {
1361                         result = -1;
1362                         break;
1363                 }
1364
1365                 if (!vcpu->arch.mmio_vsx_tx_sx_enabled) {
1366                         *val = VCPU_VSX_FPR(vcpu, rs, vsx_offset);
1367                 } else {
1368                         reg.vval = VCPU_VSX_VR(vcpu, rs);
1369                         *val = reg.vsxval[vsx_offset];
1370                 }
1371                 break;
1372
1373         case KVMPPC_VSX_COPY_WORD:
1374                 vsx_offset =
1375                         kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
1376
1377                 if (vsx_offset == -1) {
1378                         result = -1;
1379                         break;
1380                 }
1381
1382                 if (!vcpu->arch.mmio_vsx_tx_sx_enabled) {
1383                         dword_offset = vsx_offset / 2;
1384                         word_offset = vsx_offset % 2;
1385                         reg.vsxval[0] = VCPU_VSX_FPR(vcpu, rs, dword_offset);
1386                         *val = reg.vsx32val[word_offset];
1387                 } else {
1388                         reg.vval = VCPU_VSX_VR(vcpu, rs);
1389                         *val = reg.vsx32val[vsx_offset];
1390                 }
1391                 break;
1392
1393         default:
1394                 result = -1;
1395                 break;
1396         }
1397
1398         return result;
1399 }
1400
1401 int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1402                         int rs, unsigned int bytes, int is_default_endian)
1403 {
1404         u64 val;
1405         enum emulation_result emulated = EMULATE_DONE;
1406
1407         vcpu->arch.io_gpr = rs;
1408
1409         /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1410         if (vcpu->arch.mmio_vsx_copy_nums > 4)
1411                 return EMULATE_FAIL;
1412
1413         while (vcpu->arch.mmio_vsx_copy_nums) {
1414                 if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1)
1415                         return EMULATE_FAIL;
1416
1417                 emulated = kvmppc_handle_store(run, vcpu,
1418                          val, bytes, is_default_endian);
1419
1420                 if (emulated != EMULATE_DONE)
1421                         break;
1422
1423                 vcpu->arch.paddr_accessed += run->mmio.len;
1424
1425                 vcpu->arch.mmio_vsx_copy_nums--;
1426                 vcpu->arch.mmio_vsx_offset++;
1427         }
1428
1429         return emulated;
1430 }
1431
1432 static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu,
1433                         struct kvm_run *run)
1434 {
1435         enum emulation_result emulated = EMULATE_FAIL;
1436         int r;
1437
1438         vcpu->arch.paddr_accessed += run->mmio.len;
1439
1440         if (!vcpu->mmio_is_write) {
1441                 emulated = kvmppc_handle_vsx_load(run, vcpu, vcpu->arch.io_gpr,
1442                          run->mmio.len, 1, vcpu->arch.mmio_sign_extend);
1443         } else {
1444                 emulated = kvmppc_handle_vsx_store(run, vcpu,
1445                          vcpu->arch.io_gpr, run->mmio.len, 1);
1446         }
1447
1448         switch (emulated) {
1449         case EMULATE_DO_MMIO:
1450                 run->exit_reason = KVM_EXIT_MMIO;
1451                 r = RESUME_HOST;
1452                 break;
1453         case EMULATE_FAIL:
1454                 pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
1455                 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1456                 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1457                 r = RESUME_HOST;
1458                 break;
1459         default:
1460                 r = RESUME_GUEST;
1461                 break;
1462         }
1463         return r;
1464 }
1465 #endif /* CONFIG_VSX */
1466
1467 #ifdef CONFIG_ALTIVEC
1468 int kvmppc_handle_vmx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1469                 unsigned int rt, unsigned int bytes, int is_default_endian)
1470 {
1471         enum emulation_result emulated = EMULATE_DONE;
1472
1473         if (vcpu->arch.mmio_vsx_copy_nums > 2)
1474                 return EMULATE_FAIL;
1475
1476         while (vcpu->arch.mmio_vmx_copy_nums) {
1477                 emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
1478                                 is_default_endian, 0);
1479
1480                 if (emulated != EMULATE_DONE)
1481                         break;
1482
1483                 vcpu->arch.paddr_accessed += run->mmio.len;
1484                 vcpu->arch.mmio_vmx_copy_nums--;
1485                 vcpu->arch.mmio_vmx_offset++;
1486         }
1487
1488         return emulated;
1489 }
1490
1491 int kvmppc_get_vmx_dword(struct kvm_vcpu *vcpu, int index, u64 *val)
1492 {
1493         union kvmppc_one_reg reg;
1494         int vmx_offset = 0;
1495         int result = 0;
1496
1497         vmx_offset =
1498                 kvmppc_get_vmx_dword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1499
1500         if (vmx_offset == -1)
1501                 return -1;
1502
1503         reg.vval = VCPU_VSX_VR(vcpu, index);
1504         *val = reg.vsxval[vmx_offset];
1505
1506         return result;
1507 }
1508
1509 int kvmppc_get_vmx_word(struct kvm_vcpu *vcpu, int index, u64 *val)
1510 {
1511         union kvmppc_one_reg reg;
1512         int vmx_offset = 0;
1513         int result = 0;
1514
1515         vmx_offset =
1516                 kvmppc_get_vmx_word_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1517
1518         if (vmx_offset == -1)
1519                 return -1;
1520
1521         reg.vval = VCPU_VSX_VR(vcpu, index);
1522         *val = reg.vsx32val[vmx_offset];
1523
1524         return result;
1525 }
1526
1527 int kvmppc_get_vmx_hword(struct kvm_vcpu *vcpu, int index, u64 *val)
1528 {
1529         union kvmppc_one_reg reg;
1530         int vmx_offset = 0;
1531         int result = 0;
1532
1533         vmx_offset =
1534                 kvmppc_get_vmx_hword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1535
1536         if (vmx_offset == -1)
1537                 return -1;
1538
1539         reg.vval = VCPU_VSX_VR(vcpu, index);
1540         *val = reg.vsx16val[vmx_offset];
1541
1542         return result;
1543 }
1544
1545 int kvmppc_get_vmx_byte(struct kvm_vcpu *vcpu, int index, u64 *val)
1546 {
1547         union kvmppc_one_reg reg;
1548         int vmx_offset = 0;
1549         int result = 0;
1550
1551         vmx_offset =
1552                 kvmppc_get_vmx_byte_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1553
1554         if (vmx_offset == -1)
1555                 return -1;
1556
1557         reg.vval = VCPU_VSX_VR(vcpu, index);
1558         *val = reg.vsx8val[vmx_offset];
1559
1560         return result;
1561 }
1562
1563 int kvmppc_handle_vmx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1564                 unsigned int rs, unsigned int bytes, int is_default_endian)
1565 {
1566         u64 val = 0;
1567         unsigned int index = rs & KVM_MMIO_REG_MASK;
1568         enum emulation_result emulated = EMULATE_DONE;
1569
1570         if (vcpu->arch.mmio_vsx_copy_nums > 2)
1571                 return EMULATE_FAIL;
1572
1573         vcpu->arch.io_gpr = rs;
1574
1575         while (vcpu->arch.mmio_vmx_copy_nums) {
1576                 switch (vcpu->arch.mmio_copy_type) {
1577                 case KVMPPC_VMX_COPY_DWORD:
1578                         if (kvmppc_get_vmx_dword(vcpu, index, &val) == -1)
1579                                 return EMULATE_FAIL;
1580
1581                         break;
1582                 case KVMPPC_VMX_COPY_WORD:
1583                         if (kvmppc_get_vmx_word(vcpu, index, &val) == -1)
1584                                 return EMULATE_FAIL;
1585                         break;
1586                 case KVMPPC_VMX_COPY_HWORD:
1587                         if (kvmppc_get_vmx_hword(vcpu, index, &val) == -1)
1588                                 return EMULATE_FAIL;
1589                         break;
1590                 case KVMPPC_VMX_COPY_BYTE:
1591                         if (kvmppc_get_vmx_byte(vcpu, index, &val) == -1)
1592                                 return EMULATE_FAIL;
1593                         break;
1594                 default:
1595                         return EMULATE_FAIL;
1596                 }
1597
1598                 emulated = kvmppc_handle_store(run, vcpu, val, bytes,
1599                                 is_default_endian);
1600                 if (emulated != EMULATE_DONE)
1601                         break;
1602
1603                 vcpu->arch.paddr_accessed += run->mmio.len;
1604                 vcpu->arch.mmio_vmx_copy_nums--;
1605                 vcpu->arch.mmio_vmx_offset++;
1606         }
1607
1608         return emulated;
1609 }
1610
1611 static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu *vcpu,
1612                 struct kvm_run *run)
1613 {
1614         enum emulation_result emulated = EMULATE_FAIL;
1615         int r;
1616
1617         vcpu->arch.paddr_accessed += run->mmio.len;
1618
1619         if (!vcpu->mmio_is_write) {
1620                 emulated = kvmppc_handle_vmx_load(run, vcpu,
1621                                 vcpu->arch.io_gpr, run->mmio.len, 1);
1622         } else {
1623                 emulated = kvmppc_handle_vmx_store(run, vcpu,
1624                                 vcpu->arch.io_gpr, run->mmio.len, 1);
1625         }
1626
1627         switch (emulated) {
1628         case EMULATE_DO_MMIO:
1629                 run->exit_reason = KVM_EXIT_MMIO;
1630                 r = RESUME_HOST;
1631                 break;
1632         case EMULATE_FAIL:
1633                 pr_info("KVM: MMIO emulation failed (VMX repeat)\n");
1634                 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1635                 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1636                 r = RESUME_HOST;
1637                 break;
1638         default:
1639                 r = RESUME_GUEST;
1640                 break;
1641         }
1642         return r;
1643 }
1644 #endif /* CONFIG_ALTIVEC */
1645
1646 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1647 {
1648         int r = 0;
1649         union kvmppc_one_reg val;
1650         int size;
1651
1652         size = one_reg_size(reg->id);
1653         if (size > sizeof(val))
1654                 return -EINVAL;
1655
1656         r = kvmppc_get_one_reg(vcpu, reg->id, &val);
1657         if (r == -EINVAL) {
1658                 r = 0;
1659                 switch (reg->id) {
1660 #ifdef CONFIG_ALTIVEC
1661                 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1662                         if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1663                                 r = -ENXIO;
1664                                 break;
1665                         }
1666                         val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
1667                         break;
1668                 case KVM_REG_PPC_VSCR:
1669                         if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1670                                 r = -ENXIO;
1671                                 break;
1672                         }
1673                         val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
1674                         break;
1675                 case KVM_REG_PPC_VRSAVE:
1676                         val = get_reg_val(reg->id, vcpu->arch.vrsave);
1677                         break;
1678 #endif /* CONFIG_ALTIVEC */
1679                 default:
1680                         r = -EINVAL;
1681                         break;
1682                 }
1683         }
1684
1685         if (r)
1686                 return r;
1687
1688         if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1689                 r = -EFAULT;
1690
1691         return r;
1692 }
1693
1694 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1695 {
1696         int r;
1697         union kvmppc_one_reg val;
1698         int size;
1699
1700         size = one_reg_size(reg->id);
1701         if (size > sizeof(val))
1702                 return -EINVAL;
1703
1704         if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1705                 return -EFAULT;
1706
1707         r = kvmppc_set_one_reg(vcpu, reg->id, &val);
1708         if (r == -EINVAL) {
1709                 r = 0;
1710                 switch (reg->id) {
1711 #ifdef CONFIG_ALTIVEC
1712                 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1713                         if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1714                                 r = -ENXIO;
1715                                 break;
1716                         }
1717                         vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
1718                         break;
1719                 case KVM_REG_PPC_VSCR:
1720                         if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1721                                 r = -ENXIO;
1722                                 break;
1723                         }
1724                         vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
1725                         break;
1726                 case KVM_REG_PPC_VRSAVE:
1727                         if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1728                                 r = -ENXIO;
1729                                 break;
1730                         }
1731                         vcpu->arch.vrsave = set_reg_val(reg->id, val);
1732                         break;
1733 #endif /* CONFIG_ALTIVEC */
1734                 default:
1735                         r = -EINVAL;
1736                         break;
1737                 }
1738         }
1739
1740         return r;
1741 }
1742
1743 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
1744 {
1745         int r;
1746
1747         vcpu_load(vcpu);
1748
1749         if (vcpu->mmio_needed) {
1750                 vcpu->mmio_needed = 0;
1751                 if (!vcpu->mmio_is_write)
1752                         kvmppc_complete_mmio_load(vcpu, run);
1753 #ifdef CONFIG_VSX
1754                 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1755                         vcpu->arch.mmio_vsx_copy_nums--;
1756                         vcpu->arch.mmio_vsx_offset++;
1757                 }
1758
1759                 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1760                         r = kvmppc_emulate_mmio_vsx_loadstore(vcpu, run);
1761                         if (r == RESUME_HOST) {
1762                                 vcpu->mmio_needed = 1;
1763                                 goto out;
1764                         }
1765                 }
1766 #endif
1767 #ifdef CONFIG_ALTIVEC
1768                 if (vcpu->arch.mmio_vmx_copy_nums > 0) {
1769                         vcpu->arch.mmio_vmx_copy_nums--;
1770                         vcpu->arch.mmio_vmx_offset++;
1771                 }
1772
1773                 if (vcpu->arch.mmio_vmx_copy_nums > 0) {
1774                         r = kvmppc_emulate_mmio_vmx_loadstore(vcpu, run);
1775                         if (r == RESUME_HOST) {
1776                                 vcpu->mmio_needed = 1;
1777                                 goto out;
1778                         }
1779                 }
1780 #endif
1781         } else if (vcpu->arch.osi_needed) {
1782                 u64 *gprs = run->osi.gprs;
1783                 int i;
1784
1785                 for (i = 0; i < 32; i++)
1786                         kvmppc_set_gpr(vcpu, i, gprs[i]);
1787                 vcpu->arch.osi_needed = 0;
1788         } else if (vcpu->arch.hcall_needed) {
1789                 int i;
1790
1791                 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
1792                 for (i = 0; i < 9; ++i)
1793                         kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
1794                 vcpu->arch.hcall_needed = 0;
1795 #ifdef CONFIG_BOOKE
1796         } else if (vcpu->arch.epr_needed) {
1797                 kvmppc_set_epr(vcpu, run->epr.epr);
1798                 vcpu->arch.epr_needed = 0;
1799 #endif
1800         }
1801
1802         kvm_sigset_activate(vcpu);
1803
1804         if (run->immediate_exit)
1805                 r = -EINTR;
1806         else
1807                 r = kvmppc_vcpu_run(run, vcpu);
1808
1809         kvm_sigset_deactivate(vcpu);
1810
1811 #ifdef CONFIG_ALTIVEC
1812 out:
1813 #endif
1814         vcpu_put(vcpu);
1815         return r;
1816 }
1817
1818 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
1819 {
1820         if (irq->irq == KVM_INTERRUPT_UNSET) {
1821                 kvmppc_core_dequeue_external(vcpu);
1822                 return 0;
1823         }
1824
1825         kvmppc_core_queue_external(vcpu, irq);
1826
1827         kvm_vcpu_kick(vcpu);
1828
1829         return 0;
1830 }
1831
1832 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1833                                      struct kvm_enable_cap *cap)
1834 {
1835         int r;
1836
1837         if (cap->flags)
1838                 return -EINVAL;
1839
1840         switch (cap->cap) {
1841         case KVM_CAP_PPC_OSI:
1842                 r = 0;
1843                 vcpu->arch.osi_enabled = true;
1844                 break;
1845         case KVM_CAP_PPC_PAPR:
1846                 r = 0;
1847                 vcpu->arch.papr_enabled = true;
1848                 break;
1849         case KVM_CAP_PPC_EPR:
1850                 r = 0;
1851                 if (cap->args[0])
1852                         vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
1853                 else
1854                         vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
1855                 break;
1856 #ifdef CONFIG_BOOKE
1857         case KVM_CAP_PPC_BOOKE_WATCHDOG:
1858                 r = 0;
1859                 vcpu->arch.watchdog_enabled = true;
1860                 break;
1861 #endif
1862 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
1863         case KVM_CAP_SW_TLB: {
1864                 struct kvm_config_tlb cfg;
1865                 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
1866
1867                 r = -EFAULT;
1868                 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
1869                         break;
1870
1871                 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
1872                 break;
1873         }
1874 #endif
1875 #ifdef CONFIG_KVM_MPIC
1876         case KVM_CAP_IRQ_MPIC: {
1877                 struct fd f;
1878                 struct kvm_device *dev;
1879
1880                 r = -EBADF;
1881                 f = fdget(cap->args[0]);
1882                 if (!f.file)
1883                         break;
1884
1885                 r = -EPERM;
1886                 dev = kvm_device_from_filp(f.file);
1887                 if (dev)
1888                         r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
1889
1890                 fdput(f);
1891                 break;
1892         }
1893 #endif
1894 #ifdef CONFIG_KVM_XICS
1895         case KVM_CAP_IRQ_XICS: {
1896                 struct fd f;
1897                 struct kvm_device *dev;
1898
1899                 r = -EBADF;
1900                 f = fdget(cap->args[0]);
1901                 if (!f.file)
1902                         break;
1903
1904                 r = -EPERM;
1905                 dev = kvm_device_from_filp(f.file);
1906                 if (dev) {
1907                         if (xive_enabled())
1908                                 r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]);
1909                         else
1910                                 r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
1911                 }
1912
1913                 fdput(f);
1914                 break;
1915         }
1916 #endif /* CONFIG_KVM_XICS */
1917 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1918         case KVM_CAP_PPC_FWNMI:
1919                 r = -EINVAL;
1920                 if (!is_kvmppc_hv_enabled(vcpu->kvm))
1921                         break;
1922                 r = 0;
1923                 vcpu->kvm->arch.fwnmi_enabled = true;
1924                 break;
1925 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1926         default:
1927                 r = -EINVAL;
1928                 break;
1929         }
1930
1931         if (!r)
1932                 r = kvmppc_sanity_check(vcpu);
1933
1934         return r;
1935 }
1936
1937 bool kvm_arch_intc_initialized(struct kvm *kvm)
1938 {
1939 #ifdef CONFIG_KVM_MPIC
1940         if (kvm->arch.mpic)
1941                 return true;
1942 #endif
1943 #ifdef CONFIG_KVM_XICS
1944         if (kvm->arch.xics || kvm->arch.xive)
1945                 return true;
1946 #endif
1947         return false;
1948 }
1949
1950 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1951                                     struct kvm_mp_state *mp_state)
1952 {
1953         return -EINVAL;
1954 }
1955
1956 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1957                                     struct kvm_mp_state *mp_state)
1958 {
1959         return -EINVAL;
1960 }
1961
1962 long kvm_arch_vcpu_async_ioctl(struct file *filp,
1963                                unsigned int ioctl, unsigned long arg)
1964 {
1965         struct kvm_vcpu *vcpu = filp->private_data;
1966         void __user *argp = (void __user *)arg;
1967
1968         if (ioctl == KVM_INTERRUPT) {
1969                 struct kvm_interrupt irq;
1970                 if (copy_from_user(&irq, argp, sizeof(irq)))
1971                         return -EFAULT;
1972                 return kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1973         }
1974         return -ENOIOCTLCMD;
1975 }
1976
1977 long kvm_arch_vcpu_ioctl(struct file *filp,
1978                          unsigned int ioctl, unsigned long arg)
1979 {
1980         struct kvm_vcpu *vcpu = filp->private_data;
1981         void __user *argp = (void __user *)arg;
1982         long r;
1983
1984         vcpu_load(vcpu);
1985
1986         switch (ioctl) {
1987         case KVM_ENABLE_CAP:
1988         {
1989                 struct kvm_enable_cap cap;
1990                 r = -EFAULT;
1991                 if (copy_from_user(&cap, argp, sizeof(cap)))
1992                         goto out;
1993                 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1994                 break;
1995         }
1996
1997         case KVM_SET_ONE_REG:
1998         case KVM_GET_ONE_REG:
1999         {
2000                 struct kvm_one_reg reg;
2001                 r = -EFAULT;
2002                 if (copy_from_user(&reg, argp, sizeof(reg)))
2003                         goto out;
2004                 if (ioctl == KVM_SET_ONE_REG)
2005                         r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
2006                 else
2007                         r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
2008                 break;
2009         }
2010
2011 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
2012         case KVM_DIRTY_TLB: {
2013                 struct kvm_dirty_tlb dirty;
2014                 r = -EFAULT;
2015                 if (copy_from_user(&dirty, argp, sizeof(dirty)))
2016                         goto out;
2017                 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
2018                 break;
2019         }
2020 #endif
2021         default:
2022                 r = -EINVAL;
2023         }
2024
2025 out:
2026         vcpu_put(vcpu);
2027         return r;
2028 }
2029
2030 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2031 {
2032         return VM_FAULT_SIGBUS;
2033 }
2034
2035 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
2036 {
2037         u32 inst_nop = 0x60000000;
2038 #ifdef CONFIG_KVM_BOOKE_HV
2039         u32 inst_sc1 = 0x44000022;
2040         pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
2041         pvinfo->hcall[1] = cpu_to_be32(inst_nop);
2042         pvinfo->hcall[2] = cpu_to_be32(inst_nop);
2043         pvinfo->hcall[3] = cpu_to_be32(inst_nop);
2044 #else
2045         u32 inst_lis = 0x3c000000;
2046         u32 inst_ori = 0x60000000;
2047         u32 inst_sc = 0x44000002;
2048         u32 inst_imm_mask = 0xffff;
2049
2050         /*
2051          * The hypercall to get into KVM from within guest context is as
2052          * follows:
2053          *
2054          *    lis r0, r0, KVM_SC_MAGIC_R0@h
2055          *    ori r0, KVM_SC_MAGIC_R0@l
2056          *    sc
2057          *    nop
2058          */
2059         pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
2060         pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
2061         pvinfo->hcall[2] = cpu_to_be32(inst_sc);
2062         pvinfo->hcall[3] = cpu_to_be32(inst_nop);
2063 #endif
2064
2065         pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
2066
2067         return 0;
2068 }
2069
2070 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
2071                           bool line_status)
2072 {
2073         if (!irqchip_in_kernel(kvm))
2074                 return -ENXIO;
2075
2076         irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2077                                         irq_event->irq, irq_event->level,
2078                                         line_status);
2079         return 0;
2080 }
2081
2082
2083 static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
2084                                    struct kvm_enable_cap *cap)
2085 {
2086         int r;
2087
2088         if (cap->flags)
2089                 return -EINVAL;
2090
2091         switch (cap->cap) {
2092 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
2093         case KVM_CAP_PPC_ENABLE_HCALL: {
2094                 unsigned long hcall = cap->args[0];
2095
2096                 r = -EINVAL;
2097                 if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
2098                     cap->args[1] > 1)
2099                         break;
2100                 if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
2101                         break;
2102                 if (cap->args[1])
2103                         set_bit(hcall / 4, kvm->arch.enabled_hcalls);
2104                 else
2105                         clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
2106                 r = 0;
2107                 break;
2108         }
2109         case KVM_CAP_PPC_SMT: {
2110                 unsigned long mode = cap->args[0];
2111                 unsigned long flags = cap->args[1];
2112
2113                 r = -EINVAL;
2114                 if (kvm->arch.kvm_ops->set_smt_mode)
2115                         r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
2116                 break;
2117         }
2118 #endif
2119         default:
2120                 r = -EINVAL;
2121                 break;
2122         }
2123
2124         return r;
2125 }
2126
2127 #ifdef CONFIG_PPC_BOOK3S_64
2128 /*
2129  * These functions check whether the underlying hardware is safe
2130  * against attacks based on observing the effects of speculatively
2131  * executed instructions, and whether it supplies instructions for
2132  * use in workarounds.  The information comes from firmware, either
2133  * via the device tree on powernv platforms or from an hcall on
2134  * pseries platforms.
2135  */
2136 #ifdef CONFIG_PPC_PSERIES
2137 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2138 {
2139         struct h_cpu_char_result c;
2140         unsigned long rc;
2141
2142         if (!machine_is(pseries))
2143                 return -ENOTTY;
2144
2145         rc = plpar_get_cpu_characteristics(&c);
2146         if (rc == H_SUCCESS) {
2147                 cp->character = c.character;
2148                 cp->behaviour = c.behaviour;
2149                 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
2150                         KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
2151                         KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
2152                         KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
2153                         KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
2154                         KVM_PPC_CPU_CHAR_BR_HINT_HONOURED |
2155                         KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF |
2156                         KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
2157                 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
2158                         KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
2159                         KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
2160         }
2161         return 0;
2162 }
2163 #else
2164 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2165 {
2166         return -ENOTTY;
2167 }
2168 #endif
2169
2170 static inline bool have_fw_feat(struct device_node *fw_features,
2171                                 const char *state, const char *name)
2172 {
2173         struct device_node *np;
2174         bool r = false;
2175
2176         np = of_get_child_by_name(fw_features, name);
2177         if (np) {
2178                 r = of_property_read_bool(np, state);
2179                 of_node_put(np);
2180         }
2181         return r;
2182 }
2183
2184 static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2185 {
2186         struct device_node *np, *fw_features;
2187         int r;
2188
2189         memset(cp, 0, sizeof(*cp));
2190         r = pseries_get_cpu_char(cp);
2191         if (r != -ENOTTY)
2192                 return r;
2193
2194         np = of_find_node_by_name(NULL, "ibm,opal");
2195         if (np) {
2196                 fw_features = of_get_child_by_name(np, "fw-features");
2197                 of_node_put(np);
2198                 if (!fw_features)
2199                         return 0;
2200                 if (have_fw_feat(fw_features, "enabled",
2201                                  "inst-spec-barrier-ori31,31,0"))
2202                         cp->character |= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31;
2203                 if (have_fw_feat(fw_features, "enabled",
2204                                  "fw-bcctrl-serialized"))
2205                         cp->character |= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED;
2206                 if (have_fw_feat(fw_features, "enabled",
2207                                  "inst-l1d-flush-ori30,30,0"))
2208                         cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30;
2209                 if (have_fw_feat(fw_features, "enabled",
2210                                  "inst-l1d-flush-trig2"))
2211                         cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2;
2212                 if (have_fw_feat(fw_features, "enabled",
2213                                  "fw-l1d-thread-split"))
2214                         cp->character |= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV;
2215                 if (have_fw_feat(fw_features, "enabled",
2216                                  "fw-count-cache-disabled"))
2217                         cp->character |= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
2218                 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
2219                         KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
2220                         KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
2221                         KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
2222                         KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
2223                         KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
2224
2225                 if (have_fw_feat(fw_features, "enabled",
2226                                  "speculation-policy-favor-security"))
2227                         cp->behaviour |= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY;
2228                 if (!have_fw_feat(fw_features, "disabled",
2229                                   "needs-l1d-flush-msr-pr-0-to-1"))
2230                         cp->behaviour |= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR;
2231                 if (!have_fw_feat(fw_features, "disabled",
2232                                   "needs-spec-barrier-for-bound-checks"))
2233                         cp->behaviour |= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
2234                 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
2235                         KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
2236                         KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
2237
2238                 of_node_put(fw_features);
2239         }
2240
2241         return 0;
2242 }
2243 #endif
2244
2245 long kvm_arch_vm_ioctl(struct file *filp,
2246                        unsigned int ioctl, unsigned long arg)
2247 {
2248         struct kvm *kvm __maybe_unused = filp->private_data;
2249         void __user *argp = (void __user *)arg;
2250         long r;
2251
2252         switch (ioctl) {
2253         case KVM_PPC_GET_PVINFO: {
2254                 struct kvm_ppc_pvinfo pvinfo;
2255                 memset(&pvinfo, 0, sizeof(pvinfo));
2256                 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
2257                 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
2258                         r = -EFAULT;
2259                         goto out;
2260                 }
2261
2262                 break;
2263         }
2264         case KVM_ENABLE_CAP:
2265         {
2266                 struct kvm_enable_cap cap;
2267                 r = -EFAULT;
2268                 if (copy_from_user(&cap, argp, sizeof(cap)))
2269                         goto out;
2270                 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
2271                 break;
2272         }
2273 #ifdef CONFIG_SPAPR_TCE_IOMMU
2274         case KVM_CREATE_SPAPR_TCE_64: {
2275                 struct kvm_create_spapr_tce_64 create_tce_64;
2276
2277                 r = -EFAULT;
2278                 if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
2279                         goto out;
2280                 if (create_tce_64.flags) {
2281                         r = -EINVAL;
2282                         goto out;
2283                 }
2284                 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
2285                 goto out;
2286         }
2287         case KVM_CREATE_SPAPR_TCE: {
2288                 struct kvm_create_spapr_tce create_tce;
2289                 struct kvm_create_spapr_tce_64 create_tce_64;
2290
2291                 r = -EFAULT;
2292                 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
2293                         goto out;
2294
2295                 create_tce_64.liobn = create_tce.liobn;
2296                 create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
2297                 create_tce_64.offset = 0;
2298                 create_tce_64.size = create_tce.window_size >>
2299                                 IOMMU_PAGE_SHIFT_4K;
2300                 create_tce_64.flags = 0;
2301                 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
2302                 goto out;
2303         }
2304 #endif
2305 #ifdef CONFIG_PPC_BOOK3S_64
2306         case KVM_PPC_GET_SMMU_INFO: {
2307                 struct kvm_ppc_smmu_info info;
2308                 struct kvm *kvm = filp->private_data;
2309
2310                 memset(&info, 0, sizeof(info));
2311                 r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
2312                 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
2313                         r = -EFAULT;
2314                 break;
2315         }
2316         case KVM_PPC_RTAS_DEFINE_TOKEN: {
2317                 struct kvm *kvm = filp->private_data;
2318
2319                 r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
2320                 break;
2321         }
2322         case KVM_PPC_CONFIGURE_V3_MMU: {
2323                 struct kvm *kvm = filp->private_data;
2324                 struct kvm_ppc_mmuv3_cfg cfg;
2325
2326                 r = -EINVAL;
2327                 if (!kvm->arch.kvm_ops->configure_mmu)
2328                         goto out;
2329                 r = -EFAULT;
2330                 if (copy_from_user(&cfg, argp, sizeof(cfg)))
2331                         goto out;
2332                 r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
2333                 break;
2334         }
2335         case KVM_PPC_GET_RMMU_INFO: {
2336                 struct kvm *kvm = filp->private_data;
2337                 struct kvm_ppc_rmmu_info info;
2338
2339                 r = -EINVAL;
2340                 if (!kvm->arch.kvm_ops->get_rmmu_info)
2341                         goto out;
2342                 r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
2343                 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
2344                         r = -EFAULT;
2345                 break;
2346         }
2347         case KVM_PPC_GET_CPU_CHAR: {
2348                 struct kvm_ppc_cpu_char cpuchar;
2349
2350                 r = kvmppc_get_cpu_char(&cpuchar);
2351                 if (r >= 0 && copy_to_user(argp, &cpuchar, sizeof(cpuchar)))
2352                         r = -EFAULT;
2353                 break;
2354         }
2355         default: {
2356                 struct kvm *kvm = filp->private_data;
2357                 r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
2358         }
2359 #else /* CONFIG_PPC_BOOK3S_64 */
2360         default:
2361                 r = -ENOTTY;
2362 #endif
2363         }
2364 out:
2365         return r;
2366 }
2367
2368 static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
2369 static unsigned long nr_lpids;
2370
2371 long kvmppc_alloc_lpid(void)
2372 {
2373         long lpid;
2374
2375         do {
2376                 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
2377                 if (lpid >= nr_lpids) {
2378                         pr_err("%s: No LPIDs free\n", __func__);
2379                         return -ENOMEM;
2380                 }
2381         } while (test_and_set_bit(lpid, lpid_inuse));
2382
2383         return lpid;
2384 }
2385 EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
2386
2387 void kvmppc_claim_lpid(long lpid)
2388 {
2389         set_bit(lpid, lpid_inuse);
2390 }
2391 EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
2392
2393 void kvmppc_free_lpid(long lpid)
2394 {
2395         clear_bit(lpid, lpid_inuse);
2396 }
2397 EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
2398
2399 void kvmppc_init_lpid(unsigned long nr_lpids_param)
2400 {
2401         nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
2402         memset(lpid_inuse, 0, sizeof(lpid_inuse));
2403 }
2404 EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
2405
2406 int kvm_arch_init(void *opaque)
2407 {
2408         return 0;
2409 }
2410
2411 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);