KVM: PPC: Book3S: Only report KVM_CAP_SPAPR_TCE_VFIO on powernv machines
[linux/fpc-iii.git] / arch / powerpc / kvm / powerpc.c
blob75e2e471442feb782dbf4fdcfe61bd8e2d8af0c0
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.
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.
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.
15 * Copyright IBM Corp. 2007
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
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/cputhreads.h>
37 #include <asm/irqflags.h>
38 #include <asm/iommu.h>
39 #include <asm/switch_to.h>
40 #include <asm/xive.h>
41 #ifdef CONFIG_PPC_PSERIES
42 #include <asm/hvcall.h>
43 #include <asm/plpar_wrappers.h>
44 #endif
46 #include "timing.h"
47 #include "irq.h"
48 #include "../mm/mmu_decl.h"
50 #define CREATE_TRACE_POINTS
51 #include "trace.h"
53 struct kvmppc_ops *kvmppc_hv_ops;
54 EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
55 struct kvmppc_ops *kvmppc_pr_ops;
56 EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
59 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
61 return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
64 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
66 return false;
69 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
71 return 1;
75 * Common checks before entering the guest world. Call with interrupts
76 * disabled.
78 * returns:
80 * == 1 if we're ready to go into guest state
81 * <= 0 if we need to go back to the host with return value
83 int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
85 int r;
87 WARN_ON(irqs_disabled());
88 hard_irq_disable();
90 while (true) {
91 if (need_resched()) {
92 local_irq_enable();
93 cond_resched();
94 hard_irq_disable();
95 continue;
98 if (signal_pending(current)) {
99 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
100 vcpu->run->exit_reason = KVM_EXIT_INTR;
101 r = -EINTR;
102 break;
105 vcpu->mode = IN_GUEST_MODE;
108 * Reading vcpu->requests must happen after setting vcpu->mode,
109 * so we don't miss a request because the requester sees
110 * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
111 * before next entering the guest (and thus doesn't IPI).
112 * This also orders the write to mode from any reads
113 * to the page tables done while the VCPU is running.
114 * Please see the comment in kvm_flush_remote_tlbs.
116 smp_mb();
118 if (kvm_request_pending(vcpu)) {
119 /* Make sure we process requests preemptable */
120 local_irq_enable();
121 trace_kvm_check_requests(vcpu);
122 r = kvmppc_core_check_requests(vcpu);
123 hard_irq_disable();
124 if (r > 0)
125 continue;
126 break;
129 if (kvmppc_core_prepare_to_enter(vcpu)) {
130 /* interrupts got enabled in between, so we
131 are back at square 1 */
132 continue;
135 guest_enter_irqoff();
136 return 1;
139 /* return to host */
140 local_irq_enable();
141 return r;
143 EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
145 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
146 static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
148 struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
149 int i;
151 shared->sprg0 = swab64(shared->sprg0);
152 shared->sprg1 = swab64(shared->sprg1);
153 shared->sprg2 = swab64(shared->sprg2);
154 shared->sprg3 = swab64(shared->sprg3);
155 shared->srr0 = swab64(shared->srr0);
156 shared->srr1 = swab64(shared->srr1);
157 shared->dar = swab64(shared->dar);
158 shared->msr = swab64(shared->msr);
159 shared->dsisr = swab32(shared->dsisr);
160 shared->int_pending = swab32(shared->int_pending);
161 for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
162 shared->sr[i] = swab32(shared->sr[i]);
164 #endif
166 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
168 int nr = kvmppc_get_gpr(vcpu, 11);
169 int r;
170 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
171 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
172 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
173 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
174 unsigned long r2 = 0;
176 if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
177 /* 32 bit mode */
178 param1 &= 0xffffffff;
179 param2 &= 0xffffffff;
180 param3 &= 0xffffffff;
181 param4 &= 0xffffffff;
184 switch (nr) {
185 case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
187 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
188 /* Book3S can be little endian, find it out here */
189 int shared_big_endian = true;
190 if (vcpu->arch.intr_msr & MSR_LE)
191 shared_big_endian = false;
192 if (shared_big_endian != vcpu->arch.shared_big_endian)
193 kvmppc_swab_shared(vcpu);
194 vcpu->arch.shared_big_endian = shared_big_endian;
195 #endif
197 if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
199 * Older versions of the Linux magic page code had
200 * a bug where they would map their trampoline code
201 * NX. If that's the case, remove !PR NX capability.
203 vcpu->arch.disable_kernel_nx = true;
204 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
207 vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
208 vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
210 #ifdef CONFIG_PPC_64K_PAGES
212 * Make sure our 4k magic page is in the same window of a 64k
213 * page within the guest and within the host's page.
215 if ((vcpu->arch.magic_page_pa & 0xf000) !=
216 ((ulong)vcpu->arch.shared & 0xf000)) {
217 void *old_shared = vcpu->arch.shared;
218 ulong shared = (ulong)vcpu->arch.shared;
219 void *new_shared;
221 shared &= PAGE_MASK;
222 shared |= vcpu->arch.magic_page_pa & 0xf000;
223 new_shared = (void*)shared;
224 memcpy(new_shared, old_shared, 0x1000);
225 vcpu->arch.shared = new_shared;
227 #endif
229 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
231 r = EV_SUCCESS;
232 break;
234 case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
235 r = EV_SUCCESS;
236 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
237 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
238 #endif
240 /* Second return value is in r4 */
241 break;
242 case EV_HCALL_TOKEN(EV_IDLE):
243 r = EV_SUCCESS;
244 kvm_vcpu_block(vcpu);
245 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
246 break;
247 default:
248 r = EV_UNIMPLEMENTED;
249 break;
252 kvmppc_set_gpr(vcpu, 4, r2);
254 return r;
256 EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
258 int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
260 int r = false;
262 /* We have to know what CPU to virtualize */
263 if (!vcpu->arch.pvr)
264 goto out;
266 /* PAPR only works with book3s_64 */
267 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
268 goto out;
270 /* HV KVM can only do PAPR mode for now */
271 if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
272 goto out;
274 #ifdef CONFIG_KVM_BOOKE_HV
275 if (!cpu_has_feature(CPU_FTR_EMB_HV))
276 goto out;
277 #endif
279 r = true;
281 out:
282 vcpu->arch.sane = r;
283 return r ? 0 : -EINVAL;
285 EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
287 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
289 enum emulation_result er;
290 int r;
292 er = kvmppc_emulate_loadstore(vcpu);
293 switch (er) {
294 case EMULATE_DONE:
295 /* Future optimization: only reload non-volatiles if they were
296 * actually modified. */
297 r = RESUME_GUEST_NV;
298 break;
299 case EMULATE_AGAIN:
300 r = RESUME_GUEST;
301 break;
302 case EMULATE_DO_MMIO:
303 run->exit_reason = KVM_EXIT_MMIO;
304 /* We must reload nonvolatiles because "update" load/store
305 * instructions modify register state. */
306 /* Future optimization: only reload non-volatiles if they were
307 * actually modified. */
308 r = RESUME_HOST_NV;
309 break;
310 case EMULATE_FAIL:
312 u32 last_inst;
314 kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
315 /* XXX Deliver Program interrupt to guest. */
316 pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
317 r = RESUME_HOST;
318 break;
320 default:
321 WARN_ON(1);
322 r = RESUME_GUEST;
325 return r;
327 EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
329 int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
330 bool data)
332 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
333 struct kvmppc_pte pte;
334 int r;
336 vcpu->stat.st++;
338 r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
339 XLATE_WRITE, &pte);
340 if (r < 0)
341 return r;
343 *eaddr = pte.raddr;
345 if (!pte.may_write)
346 return -EPERM;
348 /* Magic page override */
349 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
350 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
351 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
352 void *magic = vcpu->arch.shared;
353 magic += pte.eaddr & 0xfff;
354 memcpy(magic, ptr, size);
355 return EMULATE_DONE;
358 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
359 return EMULATE_DO_MMIO;
361 return EMULATE_DONE;
363 EXPORT_SYMBOL_GPL(kvmppc_st);
365 int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
366 bool data)
368 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
369 struct kvmppc_pte pte;
370 int rc;
372 vcpu->stat.ld++;
374 rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
375 XLATE_READ, &pte);
376 if (rc)
377 return rc;
379 *eaddr = pte.raddr;
381 if (!pte.may_read)
382 return -EPERM;
384 if (!data && !pte.may_execute)
385 return -ENOEXEC;
387 /* Magic page override */
388 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
389 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
390 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
391 void *magic = vcpu->arch.shared;
392 magic += pte.eaddr & 0xfff;
393 memcpy(ptr, magic, size);
394 return EMULATE_DONE;
397 if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
398 return EMULATE_DO_MMIO;
400 return EMULATE_DONE;
402 EXPORT_SYMBOL_GPL(kvmppc_ld);
404 int kvm_arch_hardware_enable(void)
406 return 0;
409 int kvm_arch_hardware_setup(void)
411 return 0;
414 void kvm_arch_check_processor_compat(void *rtn)
416 *(int *)rtn = kvmppc_core_check_processor_compat();
419 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
421 struct kvmppc_ops *kvm_ops = NULL;
423 * if we have both HV and PR enabled, default is HV
425 if (type == 0) {
426 if (kvmppc_hv_ops)
427 kvm_ops = kvmppc_hv_ops;
428 else
429 kvm_ops = kvmppc_pr_ops;
430 if (!kvm_ops)
431 goto err_out;
432 } else if (type == KVM_VM_PPC_HV) {
433 if (!kvmppc_hv_ops)
434 goto err_out;
435 kvm_ops = kvmppc_hv_ops;
436 } else if (type == KVM_VM_PPC_PR) {
437 if (!kvmppc_pr_ops)
438 goto err_out;
439 kvm_ops = kvmppc_pr_ops;
440 } else
441 goto err_out;
443 if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
444 return -ENOENT;
446 kvm->arch.kvm_ops = kvm_ops;
447 return kvmppc_core_init_vm(kvm);
448 err_out:
449 return -EINVAL;
452 bool kvm_arch_has_vcpu_debugfs(void)
454 return false;
457 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
459 return 0;
462 void kvm_arch_destroy_vm(struct kvm *kvm)
464 unsigned int i;
465 struct kvm_vcpu *vcpu;
467 #ifdef CONFIG_KVM_XICS
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.
473 if (is_kvmppc_hv_enabled(kvm))
474 kick_all_cpus_sync();
475 #endif
477 kvm_for_each_vcpu(i, vcpu, kvm)
478 kvm_arch_vcpu_free(vcpu);
480 mutex_lock(&kvm->lock);
481 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
482 kvm->vcpus[i] = NULL;
484 atomic_set(&kvm->online_vcpus, 0);
486 kvmppc_core_destroy_vm(kvm);
488 mutex_unlock(&kvm->lock);
490 /* drop the module reference */
491 module_put(kvm->arch.kvm_ops->owner);
494 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
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;
500 if (kvm) {
502 * Hooray - we know which VM type we're running on. Depend on
503 * that rather than the guess above.
505 hv_enabled = is_kvmppc_hv_enabled(kvm);
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_ENABLE_CAP_VM:
522 case KVM_CAP_ONE_REG:
523 case KVM_CAP_IOEVENTFD:
524 case KVM_CAP_DEVICE_CTRL:
525 case KVM_CAP_IMMEDIATE_EXIT:
526 r = 1;
527 break;
528 case KVM_CAP_PPC_PAIRED_SINGLES:
529 case KVM_CAP_PPC_OSI:
530 case KVM_CAP_PPC_GET_PVINFO:
531 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
532 case KVM_CAP_SW_TLB:
533 #endif
534 /* We support this only for PR */
535 r = !hv_enabled;
536 break;
537 #ifdef CONFIG_KVM_MPIC
538 case KVM_CAP_IRQ_MPIC:
539 r = 1;
540 break;
541 #endif
543 #ifdef CONFIG_PPC_BOOK3S_64
544 case KVM_CAP_SPAPR_TCE:
545 case KVM_CAP_SPAPR_TCE_64:
546 r = 1;
547 break;
548 case KVM_CAP_SPAPR_TCE_VFIO:
549 r = !!cpu_has_feature(CPU_FTR_HVMODE);
550 break;
551 case KVM_CAP_PPC_RTAS:
552 case KVM_CAP_PPC_FIXUP_HCALL:
553 case KVM_CAP_PPC_ENABLE_HCALL:
554 #ifdef CONFIG_KVM_XICS
555 case KVM_CAP_IRQ_XICS:
556 #endif
557 case KVM_CAP_PPC_GET_CPU_CHAR:
558 r = 1;
559 break;
561 case KVM_CAP_PPC_ALLOC_HTAB:
562 r = hv_enabled;
563 break;
564 #endif /* CONFIG_PPC_BOOK3S_64 */
565 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
566 case KVM_CAP_PPC_SMT:
567 r = 0;
568 if (kvm) {
569 if (kvm->arch.emul_smt_mode > 1)
570 r = kvm->arch.emul_smt_mode;
571 else
572 r = kvm->arch.smt_mode;
573 } else if (hv_enabled) {
574 if (cpu_has_feature(CPU_FTR_ARCH_300))
575 r = 1;
576 else
577 r = threads_per_subcore;
579 break;
580 case KVM_CAP_PPC_SMT_POSSIBLE:
581 r = 1;
582 if (hv_enabled) {
583 if (!cpu_has_feature(CPU_FTR_ARCH_300))
584 r = ((threads_per_subcore << 1) - 1);
585 else
586 /* P9 can emulate dbells, so allow any mode */
587 r = 8 | 4 | 2 | 1;
589 break;
590 case KVM_CAP_PPC_RMA:
591 r = 0;
592 break;
593 case KVM_CAP_PPC_HWRNG:
594 r = kvmppc_hwrng_present();
595 break;
596 case KVM_CAP_PPC_MMU_RADIX:
597 r = !!(hv_enabled && radix_enabled());
598 break;
599 case KVM_CAP_PPC_MMU_HASH_V3:
600 r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300) &&
601 cpu_has_feature(CPU_FTR_HVMODE));
602 break;
603 case KVM_CAP_PPC_NESTED_HV:
604 r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&
605 !kvmppc_hv_ops->enable_nested(NULL));
606 break;
607 #endif
608 case KVM_CAP_SYNC_MMU:
609 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
610 r = hv_enabled;
611 #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
612 r = 1;
613 #else
614 r = 0;
615 #endif
616 break;
617 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
618 case KVM_CAP_PPC_HTAB_FD:
619 r = hv_enabled;
620 break;
621 #endif
622 case KVM_CAP_NR_VCPUS:
624 * Recommending a number of CPUs is somewhat arbitrary; we
625 * return the number of present CPUs for -HV (since a host
626 * will have secondary threads "offline"), and for other KVM
627 * implementations just count online CPUs.
629 if (hv_enabled)
630 r = num_present_cpus();
631 else
632 r = num_online_cpus();
633 break;
634 case KVM_CAP_NR_MEMSLOTS:
635 r = KVM_USER_MEM_SLOTS;
636 break;
637 case KVM_CAP_MAX_VCPUS:
638 r = KVM_MAX_VCPUS;
639 break;
640 #ifdef CONFIG_PPC_BOOK3S_64
641 case KVM_CAP_PPC_GET_SMMU_INFO:
642 r = 1;
643 break;
644 case KVM_CAP_SPAPR_MULTITCE:
645 r = 1;
646 break;
647 case KVM_CAP_SPAPR_RESIZE_HPT:
648 r = !!hv_enabled;
649 break;
650 #endif
651 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
652 case KVM_CAP_PPC_FWNMI:
653 r = hv_enabled;
654 break;
655 #endif
656 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
657 case KVM_CAP_PPC_HTM:
658 r = !!(cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_HTM) ||
659 (hv_enabled && cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST));
660 break;
661 #endif
662 default:
663 r = 0;
664 break;
666 return r;
670 long kvm_arch_dev_ioctl(struct file *filp,
671 unsigned int ioctl, unsigned long arg)
673 return -EINVAL;
676 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
677 struct kvm_memory_slot *dont)
679 kvmppc_core_free_memslot(kvm, free, dont);
682 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
683 unsigned long npages)
685 return kvmppc_core_create_memslot(kvm, slot, npages);
688 int kvm_arch_prepare_memory_region(struct kvm *kvm,
689 struct kvm_memory_slot *memslot,
690 const struct kvm_userspace_memory_region *mem,
691 enum kvm_mr_change change)
693 return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
696 void kvm_arch_commit_memory_region(struct kvm *kvm,
697 const struct kvm_userspace_memory_region *mem,
698 const struct kvm_memory_slot *old,
699 const struct kvm_memory_slot *new,
700 enum kvm_mr_change change)
702 kvmppc_core_commit_memory_region(kvm, mem, old, new);
705 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
706 struct kvm_memory_slot *slot)
708 kvmppc_core_flush_memslot(kvm, slot);
711 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
713 struct kvm_vcpu *vcpu;
714 vcpu = kvmppc_core_vcpu_create(kvm, id);
715 if (!IS_ERR(vcpu)) {
716 vcpu->arch.wqp = &vcpu->wq;
717 kvmppc_create_vcpu_debugfs(vcpu, id);
719 return vcpu;
722 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
726 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
728 /* Make sure we're not using the vcpu anymore */
729 hrtimer_cancel(&vcpu->arch.dec_timer);
731 kvmppc_remove_vcpu_debugfs(vcpu);
733 switch (vcpu->arch.irq_type) {
734 case KVMPPC_IRQ_MPIC:
735 kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
736 break;
737 case KVMPPC_IRQ_XICS:
738 if (xive_enabled())
739 kvmppc_xive_cleanup_vcpu(vcpu);
740 else
741 kvmppc_xics_free_icp(vcpu);
742 break;
745 kvmppc_core_vcpu_free(vcpu);
748 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
750 kvm_arch_vcpu_free(vcpu);
753 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
755 return kvmppc_core_pending_dec(vcpu);
758 static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
760 struct kvm_vcpu *vcpu;
762 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
763 kvmppc_decrementer_func(vcpu);
765 return HRTIMER_NORESTART;
768 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
770 int ret;
772 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
773 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
774 vcpu->arch.dec_expires = get_tb();
776 #ifdef CONFIG_KVM_EXIT_TIMING
777 mutex_init(&vcpu->arch.exit_timing_lock);
778 #endif
779 ret = kvmppc_subarch_vcpu_init(vcpu);
780 return ret;
783 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
785 kvmppc_mmu_destroy(vcpu);
786 kvmppc_subarch_vcpu_uninit(vcpu);
789 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
791 #ifdef CONFIG_BOOKE
793 * vrsave (formerly usprg0) isn't used by Linux, but may
794 * be used by the guest.
796 * On non-booke this is associated with Altivec and
797 * is handled by code in book3s.c.
799 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
800 #endif
801 kvmppc_core_vcpu_load(vcpu, cpu);
804 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
806 kvmppc_core_vcpu_put(vcpu);
807 #ifdef CONFIG_BOOKE
808 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
809 #endif
813 * irq_bypass_add_producer and irq_bypass_del_producer are only
814 * useful if the architecture supports PCI passthrough.
815 * irq_bypass_stop and irq_bypass_start are not needed and so
816 * kvm_ops are not defined for them.
818 bool kvm_arch_has_irq_bypass(void)
820 return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
821 (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
824 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
825 struct irq_bypass_producer *prod)
827 struct kvm_kernel_irqfd *irqfd =
828 container_of(cons, struct kvm_kernel_irqfd, consumer);
829 struct kvm *kvm = irqfd->kvm;
831 if (kvm->arch.kvm_ops->irq_bypass_add_producer)
832 return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
834 return 0;
837 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
838 struct irq_bypass_producer *prod)
840 struct kvm_kernel_irqfd *irqfd =
841 container_of(cons, struct kvm_kernel_irqfd, consumer);
842 struct kvm *kvm = irqfd->kvm;
844 if (kvm->arch.kvm_ops->irq_bypass_del_producer)
845 kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
848 #ifdef CONFIG_VSX
849 static inline int kvmppc_get_vsr_dword_offset(int index)
851 int offset;
853 if ((index != 0) && (index != 1))
854 return -1;
856 #ifdef __BIG_ENDIAN
857 offset = index;
858 #else
859 offset = 1 - index;
860 #endif
862 return offset;
865 static inline int kvmppc_get_vsr_word_offset(int index)
867 int offset;
869 if ((index > 3) || (index < 0))
870 return -1;
872 #ifdef __BIG_ENDIAN
873 offset = index;
874 #else
875 offset = 3 - index;
876 #endif
877 return offset;
880 static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu,
881 u64 gpr)
883 union kvmppc_one_reg val;
884 int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
885 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
887 if (offset == -1)
888 return;
890 if (index >= 32) {
891 val.vval = VCPU_VSX_VR(vcpu, index - 32);
892 val.vsxval[offset] = gpr;
893 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
894 } else {
895 VCPU_VSX_FPR(vcpu, index, offset) = gpr;
899 static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu,
900 u64 gpr)
902 union kvmppc_one_reg val;
903 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
905 if (index >= 32) {
906 val.vval = VCPU_VSX_VR(vcpu, index - 32);
907 val.vsxval[0] = gpr;
908 val.vsxval[1] = gpr;
909 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
910 } else {
911 VCPU_VSX_FPR(vcpu, index, 0) = gpr;
912 VCPU_VSX_FPR(vcpu, index, 1) = gpr;
916 static inline void kvmppc_set_vsr_word_dump(struct kvm_vcpu *vcpu,
917 u32 gpr)
919 union kvmppc_one_reg val;
920 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
922 if (index >= 32) {
923 val.vsx32val[0] = gpr;
924 val.vsx32val[1] = gpr;
925 val.vsx32val[2] = gpr;
926 val.vsx32val[3] = gpr;
927 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
928 } else {
929 val.vsx32val[0] = gpr;
930 val.vsx32val[1] = gpr;
931 VCPU_VSX_FPR(vcpu, index, 0) = val.vsxval[0];
932 VCPU_VSX_FPR(vcpu, index, 1) = val.vsxval[0];
936 static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu,
937 u32 gpr32)
939 union kvmppc_one_reg val;
940 int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
941 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
942 int dword_offset, word_offset;
944 if (offset == -1)
945 return;
947 if (index >= 32) {
948 val.vval = VCPU_VSX_VR(vcpu, index - 32);
949 val.vsx32val[offset] = gpr32;
950 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
951 } else {
952 dword_offset = offset / 2;
953 word_offset = offset % 2;
954 val.vsxval[0] = VCPU_VSX_FPR(vcpu, index, dword_offset);
955 val.vsx32val[word_offset] = gpr32;
956 VCPU_VSX_FPR(vcpu, index, dword_offset) = val.vsxval[0];
959 #endif /* CONFIG_VSX */
961 #ifdef CONFIG_ALTIVEC
962 static inline int kvmppc_get_vmx_offset_generic(struct kvm_vcpu *vcpu,
963 int index, int element_size)
965 int offset;
966 int elts = sizeof(vector128)/element_size;
968 if ((index < 0) || (index >= elts))
969 return -1;
971 if (kvmppc_need_byteswap(vcpu))
972 offset = elts - index - 1;
973 else
974 offset = index;
976 return offset;
979 static inline int kvmppc_get_vmx_dword_offset(struct kvm_vcpu *vcpu,
980 int index)
982 return kvmppc_get_vmx_offset_generic(vcpu, index, 8);
985 static inline int kvmppc_get_vmx_word_offset(struct kvm_vcpu *vcpu,
986 int index)
988 return kvmppc_get_vmx_offset_generic(vcpu, index, 4);
991 static inline int kvmppc_get_vmx_hword_offset(struct kvm_vcpu *vcpu,
992 int index)
994 return kvmppc_get_vmx_offset_generic(vcpu, index, 2);
997 static inline int kvmppc_get_vmx_byte_offset(struct kvm_vcpu *vcpu,
998 int index)
1000 return kvmppc_get_vmx_offset_generic(vcpu, index, 1);
1004 static inline void kvmppc_set_vmx_dword(struct kvm_vcpu *vcpu,
1005 u64 gpr)
1007 union kvmppc_one_reg val;
1008 int offset = kvmppc_get_vmx_dword_offset(vcpu,
1009 vcpu->arch.mmio_vmx_offset);
1010 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1012 if (offset == -1)
1013 return;
1015 val.vval = VCPU_VSX_VR(vcpu, index);
1016 val.vsxval[offset] = gpr;
1017 VCPU_VSX_VR(vcpu, index) = val.vval;
1020 static inline void kvmppc_set_vmx_word(struct kvm_vcpu *vcpu,
1021 u32 gpr32)
1023 union kvmppc_one_reg val;
1024 int offset = kvmppc_get_vmx_word_offset(vcpu,
1025 vcpu->arch.mmio_vmx_offset);
1026 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1028 if (offset == -1)
1029 return;
1031 val.vval = VCPU_VSX_VR(vcpu, index);
1032 val.vsx32val[offset] = gpr32;
1033 VCPU_VSX_VR(vcpu, index) = val.vval;
1036 static inline void kvmppc_set_vmx_hword(struct kvm_vcpu *vcpu,
1037 u16 gpr16)
1039 union kvmppc_one_reg val;
1040 int offset = kvmppc_get_vmx_hword_offset(vcpu,
1041 vcpu->arch.mmio_vmx_offset);
1042 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1044 if (offset == -1)
1045 return;
1047 val.vval = VCPU_VSX_VR(vcpu, index);
1048 val.vsx16val[offset] = gpr16;
1049 VCPU_VSX_VR(vcpu, index) = val.vval;
1052 static inline void kvmppc_set_vmx_byte(struct kvm_vcpu *vcpu,
1053 u8 gpr8)
1055 union kvmppc_one_reg val;
1056 int offset = kvmppc_get_vmx_byte_offset(vcpu,
1057 vcpu->arch.mmio_vmx_offset);
1058 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1060 if (offset == -1)
1061 return;
1063 val.vval = VCPU_VSX_VR(vcpu, index);
1064 val.vsx8val[offset] = gpr8;
1065 VCPU_VSX_VR(vcpu, index) = val.vval;
1067 #endif /* CONFIG_ALTIVEC */
1069 #ifdef CONFIG_PPC_FPU
1070 static inline u64 sp_to_dp(u32 fprs)
1072 u64 fprd;
1074 preempt_disable();
1075 enable_kernel_fp();
1076 asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m" (fprd) : "m" (fprs)
1077 : "fr0");
1078 preempt_enable();
1079 return fprd;
1082 static inline u32 dp_to_sp(u64 fprd)
1084 u32 fprs;
1086 preempt_disable();
1087 enable_kernel_fp();
1088 asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m" (fprs) : "m" (fprd)
1089 : "fr0");
1090 preempt_enable();
1091 return fprs;
1094 #else
1095 #define sp_to_dp(x) (x)
1096 #define dp_to_sp(x) (x)
1097 #endif /* CONFIG_PPC_FPU */
1099 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
1100 struct kvm_run *run)
1102 u64 uninitialized_var(gpr);
1104 if (run->mmio.len > sizeof(gpr)) {
1105 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
1106 return;
1109 if (!vcpu->arch.mmio_host_swabbed) {
1110 switch (run->mmio.len) {
1111 case 8: gpr = *(u64 *)run->mmio.data; break;
1112 case 4: gpr = *(u32 *)run->mmio.data; break;
1113 case 2: gpr = *(u16 *)run->mmio.data; break;
1114 case 1: gpr = *(u8 *)run->mmio.data; break;
1116 } else {
1117 switch (run->mmio.len) {
1118 case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
1119 case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
1120 case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
1121 case 1: gpr = *(u8 *)run->mmio.data; break;
1125 /* conversion between single and double precision */
1126 if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4))
1127 gpr = sp_to_dp(gpr);
1129 if (vcpu->arch.mmio_sign_extend) {
1130 switch (run->mmio.len) {
1131 #ifdef CONFIG_PPC64
1132 case 4:
1133 gpr = (s64)(s32)gpr;
1134 break;
1135 #endif
1136 case 2:
1137 gpr = (s64)(s16)gpr;
1138 break;
1139 case 1:
1140 gpr = (s64)(s8)gpr;
1141 break;
1145 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
1146 case KVM_MMIO_REG_GPR:
1147 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
1148 break;
1149 case KVM_MMIO_REG_FPR:
1150 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1151 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_FP);
1153 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1154 break;
1155 #ifdef CONFIG_PPC_BOOK3S
1156 case KVM_MMIO_REG_QPR:
1157 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1158 break;
1159 case KVM_MMIO_REG_FQPR:
1160 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1161 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1162 break;
1163 #endif
1164 #ifdef CONFIG_VSX
1165 case KVM_MMIO_REG_VSX:
1166 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1167 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VSX);
1169 if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_DWORD)
1170 kvmppc_set_vsr_dword(vcpu, gpr);
1171 else if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_WORD)
1172 kvmppc_set_vsr_word(vcpu, gpr);
1173 else if (vcpu->arch.mmio_copy_type ==
1174 KVMPPC_VSX_COPY_DWORD_LOAD_DUMP)
1175 kvmppc_set_vsr_dword_dump(vcpu, gpr);
1176 else if (vcpu->arch.mmio_copy_type ==
1177 KVMPPC_VSX_COPY_WORD_LOAD_DUMP)
1178 kvmppc_set_vsr_word_dump(vcpu, gpr);
1179 break;
1180 #endif
1181 #ifdef CONFIG_ALTIVEC
1182 case KVM_MMIO_REG_VMX:
1183 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1184 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VEC);
1186 if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_DWORD)
1187 kvmppc_set_vmx_dword(vcpu, gpr);
1188 else if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_WORD)
1189 kvmppc_set_vmx_word(vcpu, gpr);
1190 else if (vcpu->arch.mmio_copy_type ==
1191 KVMPPC_VMX_COPY_HWORD)
1192 kvmppc_set_vmx_hword(vcpu, gpr);
1193 else if (vcpu->arch.mmio_copy_type ==
1194 KVMPPC_VMX_COPY_BYTE)
1195 kvmppc_set_vmx_byte(vcpu, gpr);
1196 break;
1197 #endif
1198 default:
1199 BUG();
1203 static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1204 unsigned int rt, unsigned int bytes,
1205 int is_default_endian, int sign_extend)
1207 int idx, ret;
1208 bool host_swabbed;
1210 /* Pity C doesn't have a logical XOR operator */
1211 if (kvmppc_need_byteswap(vcpu)) {
1212 host_swabbed = is_default_endian;
1213 } else {
1214 host_swabbed = !is_default_endian;
1217 if (bytes > sizeof(run->mmio.data)) {
1218 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1219 run->mmio.len);
1222 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1223 run->mmio.len = bytes;
1224 run->mmio.is_write = 0;
1226 vcpu->arch.io_gpr = rt;
1227 vcpu->arch.mmio_host_swabbed = host_swabbed;
1228 vcpu->mmio_needed = 1;
1229 vcpu->mmio_is_write = 0;
1230 vcpu->arch.mmio_sign_extend = sign_extend;
1232 idx = srcu_read_lock(&vcpu->kvm->srcu);
1234 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1235 bytes, &run->mmio.data);
1237 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1239 if (!ret) {
1240 kvmppc_complete_mmio_load(vcpu, run);
1241 vcpu->mmio_needed = 0;
1242 return EMULATE_DONE;
1245 return EMULATE_DO_MMIO;
1248 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1249 unsigned int rt, unsigned int bytes,
1250 int is_default_endian)
1252 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0);
1254 EXPORT_SYMBOL_GPL(kvmppc_handle_load);
1256 /* Same as above, but sign extends */
1257 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
1258 unsigned int rt, unsigned int bytes,
1259 int is_default_endian)
1261 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1);
1264 #ifdef CONFIG_VSX
1265 int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1266 unsigned int rt, unsigned int bytes,
1267 int is_default_endian, int mmio_sign_extend)
1269 enum emulation_result emulated = EMULATE_DONE;
1271 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1272 if (vcpu->arch.mmio_vsx_copy_nums > 4)
1273 return EMULATE_FAIL;
1275 while (vcpu->arch.mmio_vsx_copy_nums) {
1276 emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
1277 is_default_endian, mmio_sign_extend);
1279 if (emulated != EMULATE_DONE)
1280 break;
1282 vcpu->arch.paddr_accessed += run->mmio.len;
1284 vcpu->arch.mmio_vsx_copy_nums--;
1285 vcpu->arch.mmio_vsx_offset++;
1287 return emulated;
1289 #endif /* CONFIG_VSX */
1291 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1292 u64 val, unsigned int bytes, int is_default_endian)
1294 void *data = run->mmio.data;
1295 int idx, ret;
1296 bool host_swabbed;
1298 /* Pity C doesn't have a logical XOR operator */
1299 if (kvmppc_need_byteswap(vcpu)) {
1300 host_swabbed = is_default_endian;
1301 } else {
1302 host_swabbed = !is_default_endian;
1305 if (bytes > sizeof(run->mmio.data)) {
1306 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1307 run->mmio.len);
1310 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1311 run->mmio.len = bytes;
1312 run->mmio.is_write = 1;
1313 vcpu->mmio_needed = 1;
1314 vcpu->mmio_is_write = 1;
1316 if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4))
1317 val = dp_to_sp(val);
1319 /* Store the value at the lowest bytes in 'data'. */
1320 if (!host_swabbed) {
1321 switch (bytes) {
1322 case 8: *(u64 *)data = val; break;
1323 case 4: *(u32 *)data = val; break;
1324 case 2: *(u16 *)data = val; break;
1325 case 1: *(u8 *)data = val; break;
1327 } else {
1328 switch (bytes) {
1329 case 8: *(u64 *)data = swab64(val); break;
1330 case 4: *(u32 *)data = swab32(val); break;
1331 case 2: *(u16 *)data = swab16(val); break;
1332 case 1: *(u8 *)data = val; break;
1336 idx = srcu_read_lock(&vcpu->kvm->srcu);
1338 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1339 bytes, &run->mmio.data);
1341 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1343 if (!ret) {
1344 vcpu->mmio_needed = 0;
1345 return EMULATE_DONE;
1348 return EMULATE_DO_MMIO;
1350 EXPORT_SYMBOL_GPL(kvmppc_handle_store);
1352 #ifdef CONFIG_VSX
1353 static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
1355 u32 dword_offset, word_offset;
1356 union kvmppc_one_reg reg;
1357 int vsx_offset = 0;
1358 int copy_type = vcpu->arch.mmio_copy_type;
1359 int result = 0;
1361 switch (copy_type) {
1362 case KVMPPC_VSX_COPY_DWORD:
1363 vsx_offset =
1364 kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
1366 if (vsx_offset == -1) {
1367 result = -1;
1368 break;
1371 if (rs < 32) {
1372 *val = VCPU_VSX_FPR(vcpu, rs, vsx_offset);
1373 } else {
1374 reg.vval = VCPU_VSX_VR(vcpu, rs - 32);
1375 *val = reg.vsxval[vsx_offset];
1377 break;
1379 case KVMPPC_VSX_COPY_WORD:
1380 vsx_offset =
1381 kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
1383 if (vsx_offset == -1) {
1384 result = -1;
1385 break;
1388 if (rs < 32) {
1389 dword_offset = vsx_offset / 2;
1390 word_offset = vsx_offset % 2;
1391 reg.vsxval[0] = VCPU_VSX_FPR(vcpu, rs, dword_offset);
1392 *val = reg.vsx32val[word_offset];
1393 } else {
1394 reg.vval = VCPU_VSX_VR(vcpu, rs - 32);
1395 *val = reg.vsx32val[vsx_offset];
1397 break;
1399 default:
1400 result = -1;
1401 break;
1404 return result;
1407 int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1408 int rs, unsigned int bytes, int is_default_endian)
1410 u64 val;
1411 enum emulation_result emulated = EMULATE_DONE;
1413 vcpu->arch.io_gpr = rs;
1415 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1416 if (vcpu->arch.mmio_vsx_copy_nums > 4)
1417 return EMULATE_FAIL;
1419 while (vcpu->arch.mmio_vsx_copy_nums) {
1420 if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1)
1421 return EMULATE_FAIL;
1423 emulated = kvmppc_handle_store(run, vcpu,
1424 val, bytes, is_default_endian);
1426 if (emulated != EMULATE_DONE)
1427 break;
1429 vcpu->arch.paddr_accessed += run->mmio.len;
1431 vcpu->arch.mmio_vsx_copy_nums--;
1432 vcpu->arch.mmio_vsx_offset++;
1435 return emulated;
1438 static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu,
1439 struct kvm_run *run)
1441 enum emulation_result emulated = EMULATE_FAIL;
1442 int r;
1444 vcpu->arch.paddr_accessed += run->mmio.len;
1446 if (!vcpu->mmio_is_write) {
1447 emulated = kvmppc_handle_vsx_load(run, vcpu, vcpu->arch.io_gpr,
1448 run->mmio.len, 1, vcpu->arch.mmio_sign_extend);
1449 } else {
1450 emulated = kvmppc_handle_vsx_store(run, vcpu,
1451 vcpu->arch.io_gpr, run->mmio.len, 1);
1454 switch (emulated) {
1455 case EMULATE_DO_MMIO:
1456 run->exit_reason = KVM_EXIT_MMIO;
1457 r = RESUME_HOST;
1458 break;
1459 case EMULATE_FAIL:
1460 pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
1461 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1462 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1463 r = RESUME_HOST;
1464 break;
1465 default:
1466 r = RESUME_GUEST;
1467 break;
1469 return r;
1471 #endif /* CONFIG_VSX */
1473 #ifdef CONFIG_ALTIVEC
1474 int kvmppc_handle_vmx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1475 unsigned int rt, unsigned int bytes, int is_default_endian)
1477 enum emulation_result emulated = EMULATE_DONE;
1479 if (vcpu->arch.mmio_vsx_copy_nums > 2)
1480 return EMULATE_FAIL;
1482 while (vcpu->arch.mmio_vmx_copy_nums) {
1483 emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
1484 is_default_endian, 0);
1486 if (emulated != EMULATE_DONE)
1487 break;
1489 vcpu->arch.paddr_accessed += run->mmio.len;
1490 vcpu->arch.mmio_vmx_copy_nums--;
1491 vcpu->arch.mmio_vmx_offset++;
1494 return emulated;
1497 int kvmppc_get_vmx_dword(struct kvm_vcpu *vcpu, int index, u64 *val)
1499 union kvmppc_one_reg reg;
1500 int vmx_offset = 0;
1501 int result = 0;
1503 vmx_offset =
1504 kvmppc_get_vmx_dword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1506 if (vmx_offset == -1)
1507 return -1;
1509 reg.vval = VCPU_VSX_VR(vcpu, index);
1510 *val = reg.vsxval[vmx_offset];
1512 return result;
1515 int kvmppc_get_vmx_word(struct kvm_vcpu *vcpu, int index, u64 *val)
1517 union kvmppc_one_reg reg;
1518 int vmx_offset = 0;
1519 int result = 0;
1521 vmx_offset =
1522 kvmppc_get_vmx_word_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1524 if (vmx_offset == -1)
1525 return -1;
1527 reg.vval = VCPU_VSX_VR(vcpu, index);
1528 *val = reg.vsx32val[vmx_offset];
1530 return result;
1533 int kvmppc_get_vmx_hword(struct kvm_vcpu *vcpu, int index, u64 *val)
1535 union kvmppc_one_reg reg;
1536 int vmx_offset = 0;
1537 int result = 0;
1539 vmx_offset =
1540 kvmppc_get_vmx_hword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1542 if (vmx_offset == -1)
1543 return -1;
1545 reg.vval = VCPU_VSX_VR(vcpu, index);
1546 *val = reg.vsx16val[vmx_offset];
1548 return result;
1551 int kvmppc_get_vmx_byte(struct kvm_vcpu *vcpu, int index, u64 *val)
1553 union kvmppc_one_reg reg;
1554 int vmx_offset = 0;
1555 int result = 0;
1557 vmx_offset =
1558 kvmppc_get_vmx_byte_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1560 if (vmx_offset == -1)
1561 return -1;
1563 reg.vval = VCPU_VSX_VR(vcpu, index);
1564 *val = reg.vsx8val[vmx_offset];
1566 return result;
1569 int kvmppc_handle_vmx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1570 unsigned int rs, unsigned int bytes, int is_default_endian)
1572 u64 val = 0;
1573 unsigned int index = rs & KVM_MMIO_REG_MASK;
1574 enum emulation_result emulated = EMULATE_DONE;
1576 if (vcpu->arch.mmio_vsx_copy_nums > 2)
1577 return EMULATE_FAIL;
1579 vcpu->arch.io_gpr = rs;
1581 while (vcpu->arch.mmio_vmx_copy_nums) {
1582 switch (vcpu->arch.mmio_copy_type) {
1583 case KVMPPC_VMX_COPY_DWORD:
1584 if (kvmppc_get_vmx_dword(vcpu, index, &val) == -1)
1585 return EMULATE_FAIL;
1587 break;
1588 case KVMPPC_VMX_COPY_WORD:
1589 if (kvmppc_get_vmx_word(vcpu, index, &val) == -1)
1590 return EMULATE_FAIL;
1591 break;
1592 case KVMPPC_VMX_COPY_HWORD:
1593 if (kvmppc_get_vmx_hword(vcpu, index, &val) == -1)
1594 return EMULATE_FAIL;
1595 break;
1596 case KVMPPC_VMX_COPY_BYTE:
1597 if (kvmppc_get_vmx_byte(vcpu, index, &val) == -1)
1598 return EMULATE_FAIL;
1599 break;
1600 default:
1601 return EMULATE_FAIL;
1604 emulated = kvmppc_handle_store(run, vcpu, val, bytes,
1605 is_default_endian);
1606 if (emulated != EMULATE_DONE)
1607 break;
1609 vcpu->arch.paddr_accessed += run->mmio.len;
1610 vcpu->arch.mmio_vmx_copy_nums--;
1611 vcpu->arch.mmio_vmx_offset++;
1614 return emulated;
1617 static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu *vcpu,
1618 struct kvm_run *run)
1620 enum emulation_result emulated = EMULATE_FAIL;
1621 int r;
1623 vcpu->arch.paddr_accessed += run->mmio.len;
1625 if (!vcpu->mmio_is_write) {
1626 emulated = kvmppc_handle_vmx_load(run, vcpu,
1627 vcpu->arch.io_gpr, run->mmio.len, 1);
1628 } else {
1629 emulated = kvmppc_handle_vmx_store(run, vcpu,
1630 vcpu->arch.io_gpr, run->mmio.len, 1);
1633 switch (emulated) {
1634 case EMULATE_DO_MMIO:
1635 run->exit_reason = KVM_EXIT_MMIO;
1636 r = RESUME_HOST;
1637 break;
1638 case EMULATE_FAIL:
1639 pr_info("KVM: MMIO emulation failed (VMX repeat)\n");
1640 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1641 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1642 r = RESUME_HOST;
1643 break;
1644 default:
1645 r = RESUME_GUEST;
1646 break;
1648 return r;
1650 #endif /* CONFIG_ALTIVEC */
1652 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1654 int r = 0;
1655 union kvmppc_one_reg val;
1656 int size;
1658 size = one_reg_size(reg->id);
1659 if (size > sizeof(val))
1660 return -EINVAL;
1662 r = kvmppc_get_one_reg(vcpu, reg->id, &val);
1663 if (r == -EINVAL) {
1664 r = 0;
1665 switch (reg->id) {
1666 #ifdef CONFIG_ALTIVEC
1667 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1668 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1669 r = -ENXIO;
1670 break;
1672 val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
1673 break;
1674 case KVM_REG_PPC_VSCR:
1675 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1676 r = -ENXIO;
1677 break;
1679 val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
1680 break;
1681 case KVM_REG_PPC_VRSAVE:
1682 val = get_reg_val(reg->id, vcpu->arch.vrsave);
1683 break;
1684 #endif /* CONFIG_ALTIVEC */
1685 default:
1686 r = -EINVAL;
1687 break;
1691 if (r)
1692 return r;
1694 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1695 r = -EFAULT;
1697 return r;
1700 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1702 int r;
1703 union kvmppc_one_reg val;
1704 int size;
1706 size = one_reg_size(reg->id);
1707 if (size > sizeof(val))
1708 return -EINVAL;
1710 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1711 return -EFAULT;
1713 r = kvmppc_set_one_reg(vcpu, reg->id, &val);
1714 if (r == -EINVAL) {
1715 r = 0;
1716 switch (reg->id) {
1717 #ifdef CONFIG_ALTIVEC
1718 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1719 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1720 r = -ENXIO;
1721 break;
1723 vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
1724 break;
1725 case KVM_REG_PPC_VSCR:
1726 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1727 r = -ENXIO;
1728 break;
1730 vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
1731 break;
1732 case KVM_REG_PPC_VRSAVE:
1733 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1734 r = -ENXIO;
1735 break;
1737 vcpu->arch.vrsave = set_reg_val(reg->id, val);
1738 break;
1739 #endif /* CONFIG_ALTIVEC */
1740 default:
1741 r = -EINVAL;
1742 break;
1746 return r;
1749 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
1751 int r;
1753 vcpu_load(vcpu);
1755 if (vcpu->mmio_needed) {
1756 vcpu->mmio_needed = 0;
1757 if (!vcpu->mmio_is_write)
1758 kvmppc_complete_mmio_load(vcpu, run);
1759 #ifdef CONFIG_VSX
1760 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1761 vcpu->arch.mmio_vsx_copy_nums--;
1762 vcpu->arch.mmio_vsx_offset++;
1765 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1766 r = kvmppc_emulate_mmio_vsx_loadstore(vcpu, run);
1767 if (r == RESUME_HOST) {
1768 vcpu->mmio_needed = 1;
1769 goto out;
1772 #endif
1773 #ifdef CONFIG_ALTIVEC
1774 if (vcpu->arch.mmio_vmx_copy_nums > 0) {
1775 vcpu->arch.mmio_vmx_copy_nums--;
1776 vcpu->arch.mmio_vmx_offset++;
1779 if (vcpu->arch.mmio_vmx_copy_nums > 0) {
1780 r = kvmppc_emulate_mmio_vmx_loadstore(vcpu, run);
1781 if (r == RESUME_HOST) {
1782 vcpu->mmio_needed = 1;
1783 goto out;
1786 #endif
1787 } else if (vcpu->arch.osi_needed) {
1788 u64 *gprs = run->osi.gprs;
1789 int i;
1791 for (i = 0; i < 32; i++)
1792 kvmppc_set_gpr(vcpu, i, gprs[i]);
1793 vcpu->arch.osi_needed = 0;
1794 } else if (vcpu->arch.hcall_needed) {
1795 int i;
1797 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
1798 for (i = 0; i < 9; ++i)
1799 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
1800 vcpu->arch.hcall_needed = 0;
1801 #ifdef CONFIG_BOOKE
1802 } else if (vcpu->arch.epr_needed) {
1803 kvmppc_set_epr(vcpu, run->epr.epr);
1804 vcpu->arch.epr_needed = 0;
1805 #endif
1808 kvm_sigset_activate(vcpu);
1810 if (run->immediate_exit)
1811 r = -EINTR;
1812 else
1813 r = kvmppc_vcpu_run(run, vcpu);
1815 kvm_sigset_deactivate(vcpu);
1817 #ifdef CONFIG_ALTIVEC
1818 out:
1819 #endif
1820 vcpu_put(vcpu);
1821 return r;
1824 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
1826 if (irq->irq == KVM_INTERRUPT_UNSET) {
1827 kvmppc_core_dequeue_external(vcpu);
1828 return 0;
1831 kvmppc_core_queue_external(vcpu, irq);
1833 kvm_vcpu_kick(vcpu);
1835 return 0;
1838 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1839 struct kvm_enable_cap *cap)
1841 int r;
1843 if (cap->flags)
1844 return -EINVAL;
1846 switch (cap->cap) {
1847 case KVM_CAP_PPC_OSI:
1848 r = 0;
1849 vcpu->arch.osi_enabled = true;
1850 break;
1851 case KVM_CAP_PPC_PAPR:
1852 r = 0;
1853 vcpu->arch.papr_enabled = true;
1854 break;
1855 case KVM_CAP_PPC_EPR:
1856 r = 0;
1857 if (cap->args[0])
1858 vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
1859 else
1860 vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
1861 break;
1862 #ifdef CONFIG_BOOKE
1863 case KVM_CAP_PPC_BOOKE_WATCHDOG:
1864 r = 0;
1865 vcpu->arch.watchdog_enabled = true;
1866 break;
1867 #endif
1868 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
1869 case KVM_CAP_SW_TLB: {
1870 struct kvm_config_tlb cfg;
1871 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
1873 r = -EFAULT;
1874 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
1875 break;
1877 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
1878 break;
1880 #endif
1881 #ifdef CONFIG_KVM_MPIC
1882 case KVM_CAP_IRQ_MPIC: {
1883 struct fd f;
1884 struct kvm_device *dev;
1886 r = -EBADF;
1887 f = fdget(cap->args[0]);
1888 if (!f.file)
1889 break;
1891 r = -EPERM;
1892 dev = kvm_device_from_filp(f.file);
1893 if (dev)
1894 r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
1896 fdput(f);
1897 break;
1899 #endif
1900 #ifdef CONFIG_KVM_XICS
1901 case KVM_CAP_IRQ_XICS: {
1902 struct fd f;
1903 struct kvm_device *dev;
1905 r = -EBADF;
1906 f = fdget(cap->args[0]);
1907 if (!f.file)
1908 break;
1910 r = -EPERM;
1911 dev = kvm_device_from_filp(f.file);
1912 if (dev) {
1913 if (xive_enabled())
1914 r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]);
1915 else
1916 r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
1919 fdput(f);
1920 break;
1922 #endif /* CONFIG_KVM_XICS */
1923 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1924 case KVM_CAP_PPC_FWNMI:
1925 r = -EINVAL;
1926 if (!is_kvmppc_hv_enabled(vcpu->kvm))
1927 break;
1928 r = 0;
1929 vcpu->kvm->arch.fwnmi_enabled = true;
1930 break;
1931 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1932 default:
1933 r = -EINVAL;
1934 break;
1937 if (!r)
1938 r = kvmppc_sanity_check(vcpu);
1940 return r;
1943 bool kvm_arch_intc_initialized(struct kvm *kvm)
1945 #ifdef CONFIG_KVM_MPIC
1946 if (kvm->arch.mpic)
1947 return true;
1948 #endif
1949 #ifdef CONFIG_KVM_XICS
1950 if (kvm->arch.xics || kvm->arch.xive)
1951 return true;
1952 #endif
1953 return false;
1956 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1957 struct kvm_mp_state *mp_state)
1959 return -EINVAL;
1962 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1963 struct kvm_mp_state *mp_state)
1965 return -EINVAL;
1968 long kvm_arch_vcpu_async_ioctl(struct file *filp,
1969 unsigned int ioctl, unsigned long arg)
1971 struct kvm_vcpu *vcpu = filp->private_data;
1972 void __user *argp = (void __user *)arg;
1974 if (ioctl == KVM_INTERRUPT) {
1975 struct kvm_interrupt irq;
1976 if (copy_from_user(&irq, argp, sizeof(irq)))
1977 return -EFAULT;
1978 return kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1980 return -ENOIOCTLCMD;
1983 long kvm_arch_vcpu_ioctl(struct file *filp,
1984 unsigned int ioctl, unsigned long arg)
1986 struct kvm_vcpu *vcpu = filp->private_data;
1987 void __user *argp = (void __user *)arg;
1988 long r;
1990 switch (ioctl) {
1991 case KVM_ENABLE_CAP:
1993 struct kvm_enable_cap cap;
1994 r = -EFAULT;
1995 vcpu_load(vcpu);
1996 if (copy_from_user(&cap, argp, sizeof(cap)))
1997 goto out;
1998 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1999 vcpu_put(vcpu);
2000 break;
2003 case KVM_SET_ONE_REG:
2004 case KVM_GET_ONE_REG:
2006 struct kvm_one_reg reg;
2007 r = -EFAULT;
2008 if (copy_from_user(&reg, argp, sizeof(reg)))
2009 goto out;
2010 if (ioctl == KVM_SET_ONE_REG)
2011 r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
2012 else
2013 r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
2014 break;
2017 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
2018 case KVM_DIRTY_TLB: {
2019 struct kvm_dirty_tlb dirty;
2020 r = -EFAULT;
2021 vcpu_load(vcpu);
2022 if (copy_from_user(&dirty, argp, sizeof(dirty)))
2023 goto out;
2024 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
2025 vcpu_put(vcpu);
2026 break;
2028 #endif
2029 default:
2030 r = -EINVAL;
2033 out:
2034 return r;
2037 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2039 return VM_FAULT_SIGBUS;
2042 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
2044 u32 inst_nop = 0x60000000;
2045 #ifdef CONFIG_KVM_BOOKE_HV
2046 u32 inst_sc1 = 0x44000022;
2047 pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
2048 pvinfo->hcall[1] = cpu_to_be32(inst_nop);
2049 pvinfo->hcall[2] = cpu_to_be32(inst_nop);
2050 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
2051 #else
2052 u32 inst_lis = 0x3c000000;
2053 u32 inst_ori = 0x60000000;
2054 u32 inst_sc = 0x44000002;
2055 u32 inst_imm_mask = 0xffff;
2058 * The hypercall to get into KVM from within guest context is as
2059 * follows:
2061 * lis r0, r0, KVM_SC_MAGIC_R0@h
2062 * ori r0, KVM_SC_MAGIC_R0@l
2063 * sc
2064 * nop
2066 pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
2067 pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
2068 pvinfo->hcall[2] = cpu_to_be32(inst_sc);
2069 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
2070 #endif
2072 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
2074 return 0;
2077 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
2078 bool line_status)
2080 if (!irqchip_in_kernel(kvm))
2081 return -ENXIO;
2083 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2084 irq_event->irq, irq_event->level,
2085 line_status);
2086 return 0;
2090 static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
2091 struct kvm_enable_cap *cap)
2093 int r;
2095 if (cap->flags)
2096 return -EINVAL;
2098 switch (cap->cap) {
2099 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
2100 case KVM_CAP_PPC_ENABLE_HCALL: {
2101 unsigned long hcall = cap->args[0];
2103 r = -EINVAL;
2104 if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
2105 cap->args[1] > 1)
2106 break;
2107 if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
2108 break;
2109 if (cap->args[1])
2110 set_bit(hcall / 4, kvm->arch.enabled_hcalls);
2111 else
2112 clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
2113 r = 0;
2114 break;
2116 case KVM_CAP_PPC_SMT: {
2117 unsigned long mode = cap->args[0];
2118 unsigned long flags = cap->args[1];
2120 r = -EINVAL;
2121 if (kvm->arch.kvm_ops->set_smt_mode)
2122 r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
2123 break;
2126 case KVM_CAP_PPC_NESTED_HV:
2127 r = -EINVAL;
2128 if (!is_kvmppc_hv_enabled(kvm) ||
2129 !kvm->arch.kvm_ops->enable_nested)
2130 break;
2131 r = kvm->arch.kvm_ops->enable_nested(kvm);
2132 break;
2133 #endif
2134 default:
2135 r = -EINVAL;
2136 break;
2139 return r;
2142 #ifdef CONFIG_PPC_BOOK3S_64
2144 * These functions check whether the underlying hardware is safe
2145 * against attacks based on observing the effects of speculatively
2146 * executed instructions, and whether it supplies instructions for
2147 * use in workarounds. The information comes from firmware, either
2148 * via the device tree on powernv platforms or from an hcall on
2149 * pseries platforms.
2151 #ifdef CONFIG_PPC_PSERIES
2152 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2154 struct h_cpu_char_result c;
2155 unsigned long rc;
2157 if (!machine_is(pseries))
2158 return -ENOTTY;
2160 rc = plpar_get_cpu_characteristics(&c);
2161 if (rc == H_SUCCESS) {
2162 cp->character = c.character;
2163 cp->behaviour = c.behaviour;
2164 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
2165 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
2166 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
2167 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
2168 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
2169 KVM_PPC_CPU_CHAR_BR_HINT_HONOURED |
2170 KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF |
2171 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
2172 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
2173 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
2174 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
2176 return 0;
2178 #else
2179 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2181 return -ENOTTY;
2183 #endif
2185 static inline bool have_fw_feat(struct device_node *fw_features,
2186 const char *state, const char *name)
2188 struct device_node *np;
2189 bool r = false;
2191 np = of_get_child_by_name(fw_features, name);
2192 if (np) {
2193 r = of_property_read_bool(np, state);
2194 of_node_put(np);
2196 return r;
2199 static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2201 struct device_node *np, *fw_features;
2202 int r;
2204 memset(cp, 0, sizeof(*cp));
2205 r = pseries_get_cpu_char(cp);
2206 if (r != -ENOTTY)
2207 return r;
2209 np = of_find_node_by_name(NULL, "ibm,opal");
2210 if (np) {
2211 fw_features = of_get_child_by_name(np, "fw-features");
2212 of_node_put(np);
2213 if (!fw_features)
2214 return 0;
2215 if (have_fw_feat(fw_features, "enabled",
2216 "inst-spec-barrier-ori31,31,0"))
2217 cp->character |= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31;
2218 if (have_fw_feat(fw_features, "enabled",
2219 "fw-bcctrl-serialized"))
2220 cp->character |= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED;
2221 if (have_fw_feat(fw_features, "enabled",
2222 "inst-l1d-flush-ori30,30,0"))
2223 cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30;
2224 if (have_fw_feat(fw_features, "enabled",
2225 "inst-l1d-flush-trig2"))
2226 cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2;
2227 if (have_fw_feat(fw_features, "enabled",
2228 "fw-l1d-thread-split"))
2229 cp->character |= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV;
2230 if (have_fw_feat(fw_features, "enabled",
2231 "fw-count-cache-disabled"))
2232 cp->character |= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
2233 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
2234 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
2235 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
2236 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
2237 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
2238 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
2240 if (have_fw_feat(fw_features, "enabled",
2241 "speculation-policy-favor-security"))
2242 cp->behaviour |= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY;
2243 if (!have_fw_feat(fw_features, "disabled",
2244 "needs-l1d-flush-msr-pr-0-to-1"))
2245 cp->behaviour |= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR;
2246 if (!have_fw_feat(fw_features, "disabled",
2247 "needs-spec-barrier-for-bound-checks"))
2248 cp->behaviour |= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
2249 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
2250 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
2251 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
2253 of_node_put(fw_features);
2256 return 0;
2258 #endif
2260 long kvm_arch_vm_ioctl(struct file *filp,
2261 unsigned int ioctl, unsigned long arg)
2263 struct kvm *kvm __maybe_unused = filp->private_data;
2264 void __user *argp = (void __user *)arg;
2265 long r;
2267 switch (ioctl) {
2268 case KVM_PPC_GET_PVINFO: {
2269 struct kvm_ppc_pvinfo pvinfo;
2270 memset(&pvinfo, 0, sizeof(pvinfo));
2271 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
2272 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
2273 r = -EFAULT;
2274 goto out;
2277 break;
2279 case KVM_ENABLE_CAP:
2281 struct kvm_enable_cap cap;
2282 r = -EFAULT;
2283 if (copy_from_user(&cap, argp, sizeof(cap)))
2284 goto out;
2285 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
2286 break;
2288 #ifdef CONFIG_SPAPR_TCE_IOMMU
2289 case KVM_CREATE_SPAPR_TCE_64: {
2290 struct kvm_create_spapr_tce_64 create_tce_64;
2292 r = -EFAULT;
2293 if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
2294 goto out;
2295 if (create_tce_64.flags) {
2296 r = -EINVAL;
2297 goto out;
2299 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
2300 goto out;
2302 case KVM_CREATE_SPAPR_TCE: {
2303 struct kvm_create_spapr_tce create_tce;
2304 struct kvm_create_spapr_tce_64 create_tce_64;
2306 r = -EFAULT;
2307 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
2308 goto out;
2310 create_tce_64.liobn = create_tce.liobn;
2311 create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
2312 create_tce_64.offset = 0;
2313 create_tce_64.size = create_tce.window_size >>
2314 IOMMU_PAGE_SHIFT_4K;
2315 create_tce_64.flags = 0;
2316 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
2317 goto out;
2319 #endif
2320 #ifdef CONFIG_PPC_BOOK3S_64
2321 case KVM_PPC_GET_SMMU_INFO: {
2322 struct kvm_ppc_smmu_info info;
2323 struct kvm *kvm = filp->private_data;
2325 memset(&info, 0, sizeof(info));
2326 r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
2327 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
2328 r = -EFAULT;
2329 break;
2331 case KVM_PPC_RTAS_DEFINE_TOKEN: {
2332 struct kvm *kvm = filp->private_data;
2334 r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
2335 break;
2337 case KVM_PPC_CONFIGURE_V3_MMU: {
2338 struct kvm *kvm = filp->private_data;
2339 struct kvm_ppc_mmuv3_cfg cfg;
2341 r = -EINVAL;
2342 if (!kvm->arch.kvm_ops->configure_mmu)
2343 goto out;
2344 r = -EFAULT;
2345 if (copy_from_user(&cfg, argp, sizeof(cfg)))
2346 goto out;
2347 r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
2348 break;
2350 case KVM_PPC_GET_RMMU_INFO: {
2351 struct kvm *kvm = filp->private_data;
2352 struct kvm_ppc_rmmu_info info;
2354 r = -EINVAL;
2355 if (!kvm->arch.kvm_ops->get_rmmu_info)
2356 goto out;
2357 r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
2358 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
2359 r = -EFAULT;
2360 break;
2362 case KVM_PPC_GET_CPU_CHAR: {
2363 struct kvm_ppc_cpu_char cpuchar;
2365 r = kvmppc_get_cpu_char(&cpuchar);
2366 if (r >= 0 && copy_to_user(argp, &cpuchar, sizeof(cpuchar)))
2367 r = -EFAULT;
2368 break;
2370 default: {
2371 struct kvm *kvm = filp->private_data;
2372 r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
2374 #else /* CONFIG_PPC_BOOK3S_64 */
2375 default:
2376 r = -ENOTTY;
2377 #endif
2379 out:
2380 return r;
2383 static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
2384 static unsigned long nr_lpids;
2386 long kvmppc_alloc_lpid(void)
2388 long lpid;
2390 do {
2391 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
2392 if (lpid >= nr_lpids) {
2393 pr_err("%s: No LPIDs free\n", __func__);
2394 return -ENOMEM;
2396 } while (test_and_set_bit(lpid, lpid_inuse));
2398 return lpid;
2400 EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
2402 void kvmppc_claim_lpid(long lpid)
2404 set_bit(lpid, lpid_inuse);
2406 EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
2408 void kvmppc_free_lpid(long lpid)
2410 clear_bit(lpid, lpid_inuse);
2412 EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
2414 void kvmppc_init_lpid(unsigned long nr_lpids_param)
2416 nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
2417 memset(lpid_inuse, 0, sizeof(lpid_inuse));
2419 EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
2421 int kvm_arch_init(void *opaque)
2423 return 0;
2426 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);