OSDN Git Service

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