1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
11 * Description: KVM functions specific to running on Book 3S
12 * processors in hypervisor mode (specifically POWER7 and later).
14 * This file is derived from arch/powerpc/kvm/book3s.c,
18 #include <linux/kvm_host.h>
19 #include <linux/kernel.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <linux/preempt.h>
23 #include <linux/sched/signal.h>
24 #include <linux/sched/stat.h>
25 #include <linux/delay.h>
26 #include <linux/export.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/cpu.h>
30 #include <linux/cpumask.h>
31 #include <linux/spinlock.h>
32 #include <linux/page-flags.h>
33 #include <linux/srcu.h>
34 #include <linux/miscdevice.h>
35 #include <linux/debugfs.h>
36 #include <linux/gfp.h>
37 #include <linux/vmalloc.h>
38 #include <linux/highmem.h>
39 #include <linux/hugetlb.h>
40 #include <linux/kvm_irqfd.h>
41 #include <linux/irqbypass.h>
42 #include <linux/module.h>
43 #include <linux/compiler.h>
45 #include <linux/irqdomain.h>
46 #include <linux/smp.h>
48 #include <asm/ftrace.h>
50 #include <asm/ppc-opcode.h>
51 #include <asm/asm-prototypes.h>
52 #include <asm/archrandom.h>
53 #include <asm/debug.h>
54 #include <asm/disassemble.h>
55 #include <asm/cputable.h>
56 #include <asm/cacheflush.h>
57 #include <linux/uaccess.h>
58 #include <asm/interrupt.h>
60 #include <asm/kvm_ppc.h>
61 #include <asm/kvm_book3s.h>
62 #include <asm/mmu_context.h>
63 #include <asm/lppaca.h>
65 #include <asm/processor.h>
66 #include <asm/cputhreads.h>
68 #include <asm/hvcall.h>
69 #include <asm/switch_to.h>
71 #include <asm/dbell.h>
73 #include <asm/pnv-pci.h>
78 #include <asm/hw_breakpoint.h>
79 #include <asm/kvm_book3s_uvmem.h>
80 #include <asm/ultravisor.h>
82 #include <asm/plpar_wrappers.h>
84 #include <trace/events/ipi.h>
87 #include "book3s_hv.h"
89 #define CREATE_TRACE_POINTS
92 /* #define EXIT_DEBUG */
93 /* #define EXIT_DEBUG_SIMPLE */
94 /* #define EXIT_DEBUG_INT */
96 /* Used to indicate that a guest page fault needs to be handled */
97 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
98 /* Used to indicate that a guest passthrough interrupt needs to be handled */
99 #define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
101 /* Used as a "null" value for timebase values */
102 #define TB_NIL (~(u64)0)
104 static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
106 static int dynamic_mt_modes = 6;
107 module_param(dynamic_mt_modes, int, 0644);
108 MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
109 static int target_smt_mode;
110 module_param(target_smt_mode, int, 0644);
111 MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
113 static bool one_vm_per_core;
114 module_param(one_vm_per_core, bool, S_IRUGO | S_IWUSR);
115 MODULE_PARM_DESC(one_vm_per_core, "Only run vCPUs from the same VM on a core (requires POWER8 or older)");
117 #ifdef CONFIG_KVM_XICS
118 static const struct kernel_param_ops module_param_ops = {
119 .set = param_set_int,
120 .get = param_get_int,
123 module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass, 0644);
124 MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
126 module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644);
127 MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
130 /* If set, guests are allowed to create and control nested guests */
131 static bool nested = true;
132 module_param(nested, bool, S_IRUGO | S_IWUSR);
133 MODULE_PARM_DESC(nested, "Enable nested virtualization (only on POWER9)");
135 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
138 * RWMR values for POWER8. These control the rate at which PURR
139 * and SPURR count and should be set according to the number of
140 * online threads in the vcore being run.
142 #define RWMR_RPA_P8_1THREAD 0x164520C62609AECAUL
143 #define RWMR_RPA_P8_2THREAD 0x7FFF2908450D8DA9UL
144 #define RWMR_RPA_P8_3THREAD 0x164520C62609AECAUL
145 #define RWMR_RPA_P8_4THREAD 0x199A421245058DA9UL
146 #define RWMR_RPA_P8_5THREAD 0x164520C62609AECAUL
147 #define RWMR_RPA_P8_6THREAD 0x164520C62609AECAUL
148 #define RWMR_RPA_P8_7THREAD 0x164520C62609AECAUL
149 #define RWMR_RPA_P8_8THREAD 0x164520C62609AECAUL
151 static unsigned long p8_rwmr_values[MAX_SMT_THREADS + 1] = {
163 static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
167 struct kvm_vcpu *vcpu;
169 while (++i < MAX_SMT_THREADS) {
170 vcpu = READ_ONCE(vc->runnable_threads[i]);
179 /* Used to traverse the list of runnable threads for a given vcore */
180 #define for_each_runnable_thread(i, vcpu, vc) \
181 for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
183 static bool kvmppc_ipi_thread(int cpu)
185 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
187 /* If we're a nested hypervisor, fall back to ordinary IPIs for now */
188 if (kvmhv_on_pseries())
191 /* On POWER9 we can use msgsnd to IPI any cpu */
192 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
193 msg |= get_hard_smp_processor_id(cpu);
195 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
199 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
200 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
202 if (cpu_first_thread_sibling(cpu) ==
203 cpu_first_thread_sibling(smp_processor_id())) {
204 msg |= cpu_thread_in_core(cpu);
206 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
213 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
214 if (cpu >= 0 && cpu < nr_cpu_ids) {
215 if (paca_ptrs[cpu]->kvm_hstate.xics_phys) {
219 opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
227 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
230 struct rcuwait *waitp;
233 * rcuwait_wake_up contains smp_mb() which orders prior stores that
234 * create pending work vs below loads of cpu fields. The other side
235 * is the barrier in vcpu run that orders setting the cpu fields vs
236 * testing for pending work.
239 waitp = kvm_arch_vcpu_get_wait(vcpu);
240 if (rcuwait_wake_up(waitp))
241 ++vcpu->stat.generic.halt_wakeup;
243 cpu = READ_ONCE(vcpu->arch.thread_cpu);
244 if (cpu >= 0 && kvmppc_ipi_thread(cpu))
247 /* CPU points to the first thread of the core */
249 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
250 smp_send_reschedule(cpu);
254 * We use the vcpu_load/put functions to measure stolen time.
256 * Stolen time is counted as time when either the vcpu is able to
257 * run as part of a virtual core, but the task running the vcore
258 * is preempted or sleeping, or when the vcpu needs something done
259 * in the kernel by the task running the vcpu, but that task is
260 * preempted or sleeping. Those two things have to be counted
261 * separately, since one of the vcpu tasks will take on the job
262 * of running the core, and the other vcpu tasks in the vcore will
263 * sleep waiting for it to do that, but that sleep shouldn't count
266 * Hence we accumulate stolen time when the vcpu can run as part of
267 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
268 * needs its task to do other things in the kernel (for example,
269 * service a page fault) in busy_stolen. We don't accumulate
270 * stolen time for a vcore when it is inactive, or for a vcpu
271 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
272 * a misnomer; it means that the vcpu task is not executing in
273 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
274 * the kernel. We don't have any way of dividing up that time
275 * between time that the vcpu is genuinely stopped, time that
276 * the task is actively working on behalf of the vcpu, and time
277 * that the task is preempted, so we don't count any of it as
280 * Updates to busy_stolen are protected by arch.tbacct_lock;
281 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
282 * lock. The stolen times are measured in units of timebase ticks.
283 * (Note that the != TB_NIL checks below are purely defensive;
284 * they should never fail.)
286 * The POWER9 path is simpler, one vcpu per virtual core so the
287 * former case does not exist. If a vcpu is preempted when it is
288 * BUSY_IN_HOST and not ceded or otherwise blocked, then accumulate
289 * the stolen cycles in busy_stolen. RUNNING is not a preemptible
290 * state in the P9 path.
293 static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc, u64 tb)
297 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
299 spin_lock_irqsave(&vc->stoltb_lock, flags);
301 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
304 static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc, u64 tb)
308 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
310 spin_lock_irqsave(&vc->stoltb_lock, flags);
311 if (vc->preempt_tb != TB_NIL) {
312 vc->stolen_tb += tb - vc->preempt_tb;
313 vc->preempt_tb = TB_NIL;
315 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
318 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
320 struct kvmppc_vcore *vc = vcpu->arch.vcore;
324 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
325 if (vcpu->arch.busy_preempt != TB_NIL) {
326 WARN_ON_ONCE(vcpu->arch.state != KVMPPC_VCPU_BUSY_IN_HOST);
327 vc->stolen_tb += mftb() - vcpu->arch.busy_preempt;
328 vcpu->arch.busy_preempt = TB_NIL;
336 * We can test vc->runner without taking the vcore lock,
337 * because only this task ever sets vc->runner to this
338 * vcpu, and once it is set to this vcpu, only this task
339 * ever sets it to NULL.
341 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
342 kvmppc_core_end_stolen(vc, now);
344 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
345 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
346 vcpu->arch.busy_preempt != TB_NIL) {
347 vcpu->arch.busy_stolen += now - vcpu->arch.busy_preempt;
348 vcpu->arch.busy_preempt = TB_NIL;
350 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
353 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
355 struct kvmppc_vcore *vc = vcpu->arch.vcore;
359 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
361 * In the P9 path, RUNNABLE is not preemptible
362 * (nor takes host interrupts)
364 WARN_ON_ONCE(vcpu->arch.state == KVMPPC_VCPU_RUNNABLE);
366 * Account stolen time when preempted while the vcpu task is
367 * running in the kernel (but not in qemu, which is INACTIVE).
369 if (task_is_running(current) &&
370 vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
371 vcpu->arch.busy_preempt = mftb();
377 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
378 kvmppc_core_start_stolen(vc, now);
380 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
381 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
382 vcpu->arch.busy_preempt = now;
383 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
386 static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
388 vcpu->arch.pvr = pvr;
391 /* Dummy value used in computing PCR value below */
392 #define PCR_ARCH_31 (PCR_ARCH_300 << 1)
394 static inline unsigned long map_pcr_to_cap(unsigned long pcr)
396 unsigned long cap = 0;
400 cap = H_GUEST_CAP_POWER9;
403 if (cpu_has_feature(CPU_FTR_P11_PVR))
404 cap = H_GUEST_CAP_POWER11;
406 cap = H_GUEST_CAP_POWER10;
415 static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
417 unsigned long host_pcr_bit = 0, guest_pcr_bit = 0, cap = 0;
418 struct kvmppc_vcore *vc = vcpu->arch.vcore;
420 /* We can (emulate) our own architecture version and anything older */
421 if (cpu_has_feature(CPU_FTR_P11_PVR) || cpu_has_feature(CPU_FTR_ARCH_31))
422 host_pcr_bit = PCR_ARCH_31;
423 else if (cpu_has_feature(CPU_FTR_ARCH_300))
424 host_pcr_bit = PCR_ARCH_300;
425 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
426 host_pcr_bit = PCR_ARCH_207;
427 else if (cpu_has_feature(CPU_FTR_ARCH_206))
428 host_pcr_bit = PCR_ARCH_206;
430 host_pcr_bit = PCR_ARCH_205;
432 /* Determine lowest PCR bit needed to run guest in given PVR level */
433 guest_pcr_bit = host_pcr_bit;
435 switch (arch_compat) {
437 guest_pcr_bit = PCR_ARCH_205;
441 guest_pcr_bit = PCR_ARCH_206;
444 guest_pcr_bit = PCR_ARCH_207;
447 guest_pcr_bit = PCR_ARCH_300;
450 case PVR_ARCH_31_P11:
451 guest_pcr_bit = PCR_ARCH_31;
458 /* Check requested PCR bits don't exceed our capabilities */
459 if (guest_pcr_bit > host_pcr_bit)
462 if (kvmhv_on_pseries() && kvmhv_is_nestedv2()) {
464 * 'arch_compat == 0' would mean the guest should default to
465 * L1's compatibility. In this case, the guest would pick
466 * host's PCR and evaluate the corresponding capabilities.
468 cap = map_pcr_to_cap(guest_pcr_bit);
469 if (!(cap & nested_capabilities))
473 spin_lock(&vc->lock);
474 vc->arch_compat = arch_compat;
475 kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LOGICAL_PVR);
477 * Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit
478 * Also set all reserved PCR bits
480 vc->pcr = (host_pcr_bit - guest_pcr_bit) | PCR_MASK;
481 spin_unlock(&vc->lock);
486 static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
490 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
491 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
492 vcpu->arch.regs.nip, vcpu->arch.shregs.msr, vcpu->arch.trap);
493 for (r = 0; r < 16; ++r)
494 pr_err("r%2d = %.16lx r%d = %.16lx\n",
495 r, kvmppc_get_gpr(vcpu, r),
496 r+16, kvmppc_get_gpr(vcpu, r+16));
497 pr_err("ctr = %.16lx lr = %.16lx\n",
498 vcpu->arch.regs.ctr, vcpu->arch.regs.link);
499 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
500 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
501 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
502 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
503 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
504 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
505 pr_err("cr = %.8lx xer = %.16lx dsisr = %.8x\n",
506 vcpu->arch.regs.ccr, vcpu->arch.regs.xer, vcpu->arch.shregs.dsisr);
507 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
508 pr_err("fault dar = %.16lx dsisr = %.8x\n",
509 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
510 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
511 for (r = 0; r < vcpu->arch.slb_max; ++r)
512 pr_err(" ESID = %.16llx VSID = %.16llx\n",
513 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
514 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.16lx\n",
515 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
516 vcpu->arch.last_inst);
519 static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
521 return kvm_get_vcpu_by_id(kvm, id);
524 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
526 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
527 vpa->yield_count = cpu_to_be32(1);
530 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
531 unsigned long addr, unsigned long len)
533 /* check address is cacheline aligned */
534 if (addr & (L1_CACHE_BYTES - 1))
536 spin_lock(&vcpu->arch.vpa_update_lock);
537 if (v->next_gpa != addr || v->len != len) {
539 v->len = addr ? len : 0;
540 v->update_pending = 1;
542 spin_unlock(&vcpu->arch.vpa_update_lock);
546 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
555 static int vpa_is_registered(struct kvmppc_vpa *vpap)
557 if (vpap->update_pending)
558 return vpap->next_gpa != 0;
559 return vpap->pinned_addr != NULL;
562 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
564 unsigned long vcpuid, unsigned long vpa)
566 struct kvm *kvm = vcpu->kvm;
567 unsigned long len, nb;
569 struct kvm_vcpu *tvcpu;
572 struct kvmppc_vpa *vpap;
574 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
578 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
579 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
580 subfunc == H_VPA_REG_SLB) {
581 /* Registering new area - address must be cache-line aligned */
582 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
585 /* convert logical addr to kernel addr and read length */
586 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
589 if (subfunc == H_VPA_REG_VPA)
590 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
592 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
593 kvmppc_unpin_guest_page(kvm, va, vpa, false);
596 if (len > nb || len < sizeof(struct reg_vpa))
605 spin_lock(&tvcpu->arch.vpa_update_lock);
608 case H_VPA_REG_VPA: /* register VPA */
610 * The size of our lppaca is 1kB because of the way we align
611 * it for the guest to avoid crossing a 4kB boundary. We only
612 * use 640 bytes of the structure though, so we should accept
613 * clients that set a size of 640.
615 BUILD_BUG_ON(sizeof(struct lppaca) != 640);
616 if (len < sizeof(struct lppaca))
618 vpap = &tvcpu->arch.vpa;
622 case H_VPA_REG_DTL: /* register DTL */
623 if (len < sizeof(struct dtl_entry))
625 len -= len % sizeof(struct dtl_entry);
627 /* Check that they have previously registered a VPA */
629 if (!vpa_is_registered(&tvcpu->arch.vpa))
632 vpap = &tvcpu->arch.dtl;
636 case H_VPA_REG_SLB: /* register SLB shadow buffer */
637 /* Check that they have previously registered a VPA */
639 if (!vpa_is_registered(&tvcpu->arch.vpa))
642 vpap = &tvcpu->arch.slb_shadow;
646 case H_VPA_DEREG_VPA: /* deregister VPA */
647 /* Check they don't still have a DTL or SLB buf registered */
649 if (vpa_is_registered(&tvcpu->arch.dtl) ||
650 vpa_is_registered(&tvcpu->arch.slb_shadow))
653 vpap = &tvcpu->arch.vpa;
657 case H_VPA_DEREG_DTL: /* deregister DTL */
658 vpap = &tvcpu->arch.dtl;
662 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
663 vpap = &tvcpu->arch.slb_shadow;
669 vpap->next_gpa = vpa;
671 vpap->update_pending = 1;
674 spin_unlock(&tvcpu->arch.vpa_update_lock);
679 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap,
680 struct kvmppc_vpa *old_vpap)
682 struct kvm *kvm = vcpu->kvm;
688 * We need to pin the page pointed to by vpap->next_gpa,
689 * but we can't call kvmppc_pin_guest_page under the lock
690 * as it does get_user_pages() and down_read(). So we
691 * have to drop the lock, pin the page, then get the lock
692 * again and check that a new area didn't get registered
696 gpa = vpap->next_gpa;
697 spin_unlock(&vcpu->arch.vpa_update_lock);
701 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
702 spin_lock(&vcpu->arch.vpa_update_lock);
703 if (gpa == vpap->next_gpa)
705 /* sigh... unpin that one and try again */
707 kvmppc_unpin_guest_page(kvm, va, gpa, false);
710 vpap->update_pending = 0;
711 if (va && nb < vpap->len) {
713 * If it's now too short, it must be that userspace
714 * has changed the mappings underlying guest memory,
715 * so unregister the region.
717 kvmppc_unpin_guest_page(kvm, va, gpa, false);
723 vpap->pinned_addr = va;
726 vpap->pinned_end = va + vpap->len;
729 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
731 struct kvm *kvm = vcpu->kvm;
732 struct kvmppc_vpa old_vpa = { 0 };
734 if (!(vcpu->arch.vpa.update_pending ||
735 vcpu->arch.slb_shadow.update_pending ||
736 vcpu->arch.dtl.update_pending))
739 spin_lock(&vcpu->arch.vpa_update_lock);
740 if (vcpu->arch.vpa.update_pending) {
741 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa, &old_vpa);
742 if (old_vpa.pinned_addr) {
743 if (kvmhv_is_nestedv2())
744 kvmhv_nestedv2_set_vpa(vcpu, ~0ull);
745 kvmppc_unpin_guest_page(kvm, old_vpa.pinned_addr, old_vpa.gpa,
748 if (vcpu->arch.vpa.pinned_addr) {
749 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
750 if (kvmhv_is_nestedv2())
751 kvmhv_nestedv2_set_vpa(vcpu, __pa(vcpu->arch.vpa.pinned_addr));
754 if (vcpu->arch.dtl.update_pending) {
755 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl, &old_vpa);
756 if (old_vpa.pinned_addr)
757 kvmppc_unpin_guest_page(kvm, old_vpa.pinned_addr, old_vpa.gpa,
759 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
760 vcpu->arch.dtl_index = 0;
762 if (vcpu->arch.slb_shadow.update_pending) {
763 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow, &old_vpa);
764 if (old_vpa.pinned_addr)
765 kvmppc_unpin_guest_page(kvm, old_vpa.pinned_addr, old_vpa.gpa,
769 spin_unlock(&vcpu->arch.vpa_update_lock);
773 * Return the accumulated stolen time for the vcore up until `now'.
774 * The caller should hold the vcore lock.
776 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
781 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
783 spin_lock_irqsave(&vc->stoltb_lock, flags);
785 if (vc->vcore_state != VCORE_INACTIVE &&
786 vc->preempt_tb != TB_NIL)
787 p += now - vc->preempt_tb;
788 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
792 static void __kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
794 unsigned int pcpu, u64 now,
795 unsigned long stolen)
797 struct dtl_entry *dt;
799 dt = vcpu->arch.dtl_ptr;
804 dt->dispatch_reason = 7;
805 dt->preempt_reason = 0;
806 dt->processor_id = cpu_to_be16(pcpu + vcpu->arch.ptid);
807 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
808 dt->ready_to_enqueue_time = 0;
809 dt->waiting_to_ready_time = 0;
810 dt->timebase = cpu_to_be64(now);
812 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
813 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
816 if (dt == vcpu->arch.dtl.pinned_end)
817 dt = vcpu->arch.dtl.pinned_addr;
818 vcpu->arch.dtl_ptr = dt;
819 /* order writing *dt vs. writing vpa->dtl_idx */
821 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
823 /* vcpu->arch.dtl.dirty is set by the caller */
826 static void kvmppc_update_vpa_dispatch(struct kvm_vcpu *vcpu,
827 struct kvmppc_vcore *vc)
830 unsigned long stolen;
831 unsigned long core_stolen;
835 vpa = vcpu->arch.vpa.pinned_addr;
841 core_stolen = vcore_stolen_time(vc, now);
842 stolen = core_stolen - vcpu->arch.stolen_logged;
843 vcpu->arch.stolen_logged = core_stolen;
844 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
845 stolen += vcpu->arch.busy_stolen;
846 vcpu->arch.busy_stolen = 0;
847 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
849 vpa->enqueue_dispatch_tb = cpu_to_be64(be64_to_cpu(vpa->enqueue_dispatch_tb) + stolen);
851 __kvmppc_create_dtl_entry(vcpu, vpa, vc->pcpu, now + kvmppc_get_tb_offset(vcpu), stolen);
853 vcpu->arch.vpa.dirty = true;
856 static void kvmppc_update_vpa_dispatch_p9(struct kvm_vcpu *vcpu,
857 struct kvmppc_vcore *vc,
861 unsigned long stolen;
862 unsigned long stolen_delta;
864 vpa = vcpu->arch.vpa.pinned_addr;
868 stolen = vc->stolen_tb;
869 stolen_delta = stolen - vcpu->arch.stolen_logged;
870 vcpu->arch.stolen_logged = stolen;
872 vpa->enqueue_dispatch_tb = cpu_to_be64(stolen);
874 __kvmppc_create_dtl_entry(vcpu, vpa, vc->pcpu, now, stolen_delta);
876 vcpu->arch.vpa.dirty = true;
879 /* See if there is a doorbell interrupt pending for a vcpu */
880 static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu)
883 struct kvmppc_vcore *vc;
885 if (vcpu->arch.doorbell_request)
887 if (cpu_has_feature(CPU_FTR_ARCH_300))
890 * Ensure that the read of vcore->dpdes comes after the read
891 * of vcpu->doorbell_request. This barrier matches the
892 * smp_wmb() in kvmppc_guest_entry_inject().
895 vc = vcpu->arch.vcore;
896 thr = vcpu->vcpu_id - vc->first_vcpuid;
897 return !!(vc->dpdes & (1 << thr));
900 static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
902 if (kvmppc_get_arch_compat(vcpu) >= PVR_ARCH_207)
904 if ((!kvmppc_get_arch_compat(vcpu)) &&
905 cpu_has_feature(CPU_FTR_ARCH_207S))
910 static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
911 unsigned long resource, unsigned long value1,
912 unsigned long value2)
915 case H_SET_MODE_RESOURCE_SET_CIABR:
916 if (!kvmppc_power8_compatible(vcpu))
921 return H_UNSUPPORTED_FLAG_START;
922 /* Guests can't breakpoint the hypervisor */
923 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
925 kvmppc_set_ciabr_hv(vcpu, value1);
927 case H_SET_MODE_RESOURCE_SET_DAWR0:
928 if (!kvmppc_power8_compatible(vcpu))
930 if (!ppc_breakpoint_available())
933 return H_UNSUPPORTED_FLAG_START;
934 if (value2 & DABRX_HYP)
936 kvmppc_set_dawr0_hv(vcpu, value1);
937 kvmppc_set_dawrx0_hv(vcpu, value2);
939 case H_SET_MODE_RESOURCE_SET_DAWR1:
940 if (!kvmppc_power8_compatible(vcpu))
942 if (!ppc_breakpoint_available())
944 if (!cpu_has_feature(CPU_FTR_DAWR1))
946 if (!vcpu->kvm->arch.dawr1_enabled)
949 return H_UNSUPPORTED_FLAG_START;
950 if (value2 & DABRX_HYP)
952 kvmppc_set_dawr1_hv(vcpu, value1);
953 kvmppc_set_dawrx1_hv(vcpu, value2);
955 case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
957 * KVM does not support mflags=2 (AIL=2) and AIL=1 is reserved.
958 * Keep this in synch with kvmppc_filter_guest_lpcr_hv.
960 if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) &&
961 kvmhv_vcpu_is_radix(vcpu) && mflags == 3)
962 return H_UNSUPPORTED_FLAG_START;
969 /* Copy guest memory in place - must reside within a single memslot */
970 static int kvmppc_copy_guest(struct kvm *kvm, gpa_t to, gpa_t from,
973 struct kvm_memory_slot *to_memslot = NULL;
974 struct kvm_memory_slot *from_memslot = NULL;
975 unsigned long to_addr, from_addr;
978 /* Get HPA for from address */
979 from_memslot = gfn_to_memslot(kvm, from >> PAGE_SHIFT);
982 if ((from + len) >= ((from_memslot->base_gfn + from_memslot->npages)
985 from_addr = gfn_to_hva_memslot(from_memslot, from >> PAGE_SHIFT);
986 if (kvm_is_error_hva(from_addr))
988 from_addr |= (from & (PAGE_SIZE - 1));
990 /* Get HPA for to address */
991 to_memslot = gfn_to_memslot(kvm, to >> PAGE_SHIFT);
994 if ((to + len) >= ((to_memslot->base_gfn + to_memslot->npages)
997 to_addr = gfn_to_hva_memslot(to_memslot, to >> PAGE_SHIFT);
998 if (kvm_is_error_hva(to_addr))
1000 to_addr |= (to & (PAGE_SIZE - 1));
1003 r = raw_copy_in_user((void __user *)to_addr, (void __user *)from_addr,
1007 mark_page_dirty(kvm, to >> PAGE_SHIFT);
1011 static long kvmppc_h_page_init(struct kvm_vcpu *vcpu, unsigned long flags,
1012 unsigned long dest, unsigned long src)
1014 u64 pg_sz = SZ_4K; /* 4K page size */
1015 u64 pg_mask = SZ_4K - 1;
1018 /* Check for invalid flags (H_PAGE_SET_LOANED covers all CMO flags) */
1019 if (flags & ~(H_ICACHE_INVALIDATE | H_ICACHE_SYNCHRONIZE |
1020 H_ZERO_PAGE | H_COPY_PAGE | H_PAGE_SET_LOANED))
1023 /* dest (and src if copy_page flag set) must be page aligned */
1024 if ((dest & pg_mask) || ((flags & H_COPY_PAGE) && (src & pg_mask)))
1027 /* zero and/or copy the page as determined by the flags */
1028 if (flags & H_COPY_PAGE) {
1029 ret = kvmppc_copy_guest(vcpu->kvm, dest, src, pg_sz);
1032 } else if (flags & H_ZERO_PAGE) {
1033 ret = kvm_clear_guest(vcpu->kvm, dest, pg_sz);
1038 /* We can ignore the remaining flags */
1043 static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
1045 struct kvmppc_vcore *vcore = target->arch.vcore;
1048 * We expect to have been called by the real mode handler
1049 * (kvmppc_rm_h_confer()) which would have directly returned
1050 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
1051 * have useful work to do and should not confer) so we don't
1052 * recheck that here.
1054 * In the case of the P9 single vcpu per vcore case, the real
1055 * mode handler is not called but no other threads are in the
1058 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
1059 spin_lock(&vcore->lock);
1060 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
1061 vcore->vcore_state != VCORE_INACTIVE &&
1063 target = vcore->runner;
1064 spin_unlock(&vcore->lock);
1067 return kvm_vcpu_yield_to(target);
1070 static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
1072 int yield_count = 0;
1073 struct lppaca *lppaca;
1075 spin_lock(&vcpu->arch.vpa_update_lock);
1076 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
1078 yield_count = be32_to_cpu(lppaca->yield_count);
1079 spin_unlock(&vcpu->arch.vpa_update_lock);
1084 * H_RPT_INVALIDATE hcall handler for nested guests.
1086 * Handles only nested process-scoped invalidation requests in L0.
1088 static int kvmppc_nested_h_rpt_invalidate(struct kvm_vcpu *vcpu)
1090 unsigned long type = kvmppc_get_gpr(vcpu, 6);
1091 unsigned long pid, pg_sizes, start, end;
1094 * The partition-scoped invalidations aren't handled here in L0.
1096 if (type & H_RPTI_TYPE_NESTED)
1099 pid = kvmppc_get_gpr(vcpu, 4);
1100 pg_sizes = kvmppc_get_gpr(vcpu, 7);
1101 start = kvmppc_get_gpr(vcpu, 8);
1102 end = kvmppc_get_gpr(vcpu, 9);
1104 do_h_rpt_invalidate_prt(pid, vcpu->arch.nested->shadow_lpid,
1105 type, pg_sizes, start, end);
1107 kvmppc_set_gpr(vcpu, 3, H_SUCCESS);
1108 return RESUME_GUEST;
1111 static long kvmppc_h_rpt_invalidate(struct kvm_vcpu *vcpu,
1112 unsigned long id, unsigned long target,
1113 unsigned long type, unsigned long pg_sizes,
1114 unsigned long start, unsigned long end)
1116 if (!kvm_is_radix(vcpu->kvm))
1117 return H_UNSUPPORTED;
1123 * Partition-scoped invalidation for nested guests.
1125 if (type & H_RPTI_TYPE_NESTED) {
1126 if (!nesting_enabled(vcpu->kvm))
1129 /* Support only cores as target */
1130 if (target != H_RPTI_TARGET_CMMU)
1133 return do_h_rpt_invalidate_pat(vcpu, id, type, pg_sizes,
1138 * Process-scoped invalidation for L1 guests.
1140 do_h_rpt_invalidate_prt(id, vcpu->kvm->arch.lpid,
1141 type, pg_sizes, start, end);
1145 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
1147 struct kvm *kvm = vcpu->kvm;
1148 unsigned long req = kvmppc_get_gpr(vcpu, 3);
1149 unsigned long target, ret = H_SUCCESS;
1151 struct kvm_vcpu *tvcpu;
1154 if (req <= MAX_HCALL_OPCODE &&
1155 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
1160 ret = kvmppc_h_remove(vcpu, kvmppc_get_gpr(vcpu, 4),
1161 kvmppc_get_gpr(vcpu, 5),
1162 kvmppc_get_gpr(vcpu, 6));
1163 if (ret == H_TOO_HARD)
1167 ret = kvmppc_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
1168 kvmppc_get_gpr(vcpu, 5),
1169 kvmppc_get_gpr(vcpu, 6),
1170 kvmppc_get_gpr(vcpu, 7));
1171 if (ret == H_TOO_HARD)
1175 ret = kvmppc_h_read(vcpu, kvmppc_get_gpr(vcpu, 4),
1176 kvmppc_get_gpr(vcpu, 5));
1177 if (ret == H_TOO_HARD)
1181 ret = kvmppc_h_clear_mod(vcpu, kvmppc_get_gpr(vcpu, 4),
1182 kvmppc_get_gpr(vcpu, 5));
1183 if (ret == H_TOO_HARD)
1187 ret = kvmppc_h_clear_ref(vcpu, kvmppc_get_gpr(vcpu, 4),
1188 kvmppc_get_gpr(vcpu, 5));
1189 if (ret == H_TOO_HARD)
1193 ret = kvmppc_h_protect(vcpu, kvmppc_get_gpr(vcpu, 4),
1194 kvmppc_get_gpr(vcpu, 5),
1195 kvmppc_get_gpr(vcpu, 6));
1196 if (ret == H_TOO_HARD)
1200 ret = kvmppc_h_bulk_remove(vcpu);
1201 if (ret == H_TOO_HARD)
1208 target = kvmppc_get_gpr(vcpu, 4);
1209 tvcpu = kvmppc_find_vcpu(kvm, target);
1214 tvcpu->arch.prodded = 1;
1215 smp_mb(); /* This orders prodded store vs ceded load */
1216 if (tvcpu->arch.ceded)
1217 kvmppc_fast_vcpu_kick_hv(tvcpu);
1220 target = kvmppc_get_gpr(vcpu, 4);
1223 tvcpu = kvmppc_find_vcpu(kvm, target);
1228 yield_count = kvmppc_get_gpr(vcpu, 5);
1229 if (kvmppc_get_yield_count(tvcpu) != yield_count)
1231 kvm_arch_vcpu_yield_to(tvcpu);
1233 case H_REGISTER_VPA:
1234 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
1235 kvmppc_get_gpr(vcpu, 5),
1236 kvmppc_get_gpr(vcpu, 6));
1239 if (list_empty(&kvm->arch.rtas_tokens))
1242 idx = srcu_read_lock(&kvm->srcu);
1243 rc = kvmppc_rtas_hcall(vcpu);
1244 srcu_read_unlock(&kvm->srcu, idx);
1251 /* Send the error out to userspace via KVM_RUN */
1253 case H_LOGICAL_CI_LOAD:
1254 ret = kvmppc_h_logical_ci_load(vcpu);
1255 if (ret == H_TOO_HARD)
1258 case H_LOGICAL_CI_STORE:
1259 ret = kvmppc_h_logical_ci_store(vcpu);
1260 if (ret == H_TOO_HARD)
1264 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
1265 kvmppc_get_gpr(vcpu, 5),
1266 kvmppc_get_gpr(vcpu, 6),
1267 kvmppc_get_gpr(vcpu, 7));
1268 if (ret == H_TOO_HARD)
1277 if (kvmppc_xics_enabled(vcpu)) {
1278 if (xics_on_xive()) {
1279 ret = H_NOT_AVAILABLE;
1280 return RESUME_GUEST;
1282 ret = kvmppc_xics_hcall(vcpu, req);
1287 ret = kvmppc_h_set_dabr(vcpu, kvmppc_get_gpr(vcpu, 4));
1290 ret = kvmppc_h_set_xdabr(vcpu, kvmppc_get_gpr(vcpu, 4),
1291 kvmppc_get_gpr(vcpu, 5));
1293 #ifdef CONFIG_SPAPR_TCE_IOMMU
1295 ret = kvmppc_h_get_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1296 kvmppc_get_gpr(vcpu, 5));
1297 if (ret == H_TOO_HARD)
1301 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1302 kvmppc_get_gpr(vcpu, 5),
1303 kvmppc_get_gpr(vcpu, 6));
1304 if (ret == H_TOO_HARD)
1307 case H_PUT_TCE_INDIRECT:
1308 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
1309 kvmppc_get_gpr(vcpu, 5),
1310 kvmppc_get_gpr(vcpu, 6),
1311 kvmppc_get_gpr(vcpu, 7));
1312 if (ret == H_TOO_HARD)
1316 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1317 kvmppc_get_gpr(vcpu, 5),
1318 kvmppc_get_gpr(vcpu, 6),
1319 kvmppc_get_gpr(vcpu, 7));
1320 if (ret == H_TOO_HARD)
1327 if (!arch_get_random_seed_longs(&rand, 1))
1329 kvmppc_set_gpr(vcpu, 4, rand);
1332 case H_RPT_INVALIDATE:
1333 ret = kvmppc_h_rpt_invalidate(vcpu, kvmppc_get_gpr(vcpu, 4),
1334 kvmppc_get_gpr(vcpu, 5),
1335 kvmppc_get_gpr(vcpu, 6),
1336 kvmppc_get_gpr(vcpu, 7),
1337 kvmppc_get_gpr(vcpu, 8),
1338 kvmppc_get_gpr(vcpu, 9));
1341 case H_SET_PARTITION_TABLE:
1343 if (nesting_enabled(kvm))
1344 ret = kvmhv_set_partition_table(vcpu);
1346 case H_ENTER_NESTED:
1348 if (!nesting_enabled(kvm))
1350 ret = kvmhv_enter_nested_guest(vcpu);
1351 if (ret == H_INTERRUPT) {
1352 kvmppc_set_gpr(vcpu, 3, 0);
1353 vcpu->arch.hcall_needed = 0;
1355 } else if (ret == H_TOO_HARD) {
1356 kvmppc_set_gpr(vcpu, 3, 0);
1357 vcpu->arch.hcall_needed = 0;
1361 case H_TLB_INVALIDATE:
1363 if (nesting_enabled(kvm))
1364 ret = kvmhv_do_nested_tlbie(vcpu);
1366 case H_COPY_TOFROM_GUEST:
1368 if (nesting_enabled(kvm))
1369 ret = kvmhv_copy_tofrom_guest_nested(vcpu);
1372 ret = kvmppc_h_page_init(vcpu, kvmppc_get_gpr(vcpu, 4),
1373 kvmppc_get_gpr(vcpu, 5),
1374 kvmppc_get_gpr(vcpu, 6));
1377 ret = H_UNSUPPORTED;
1378 if (kvmppc_get_srr1(vcpu) & MSR_S)
1379 ret = kvmppc_h_svm_page_in(kvm,
1380 kvmppc_get_gpr(vcpu, 4),
1381 kvmppc_get_gpr(vcpu, 5),
1382 kvmppc_get_gpr(vcpu, 6));
1384 case H_SVM_PAGE_OUT:
1385 ret = H_UNSUPPORTED;
1386 if (kvmppc_get_srr1(vcpu) & MSR_S)
1387 ret = kvmppc_h_svm_page_out(kvm,
1388 kvmppc_get_gpr(vcpu, 4),
1389 kvmppc_get_gpr(vcpu, 5),
1390 kvmppc_get_gpr(vcpu, 6));
1392 case H_SVM_INIT_START:
1393 ret = H_UNSUPPORTED;
1394 if (kvmppc_get_srr1(vcpu) & MSR_S)
1395 ret = kvmppc_h_svm_init_start(kvm);
1397 case H_SVM_INIT_DONE:
1398 ret = H_UNSUPPORTED;
1399 if (kvmppc_get_srr1(vcpu) & MSR_S)
1400 ret = kvmppc_h_svm_init_done(kvm);
1402 case H_SVM_INIT_ABORT:
1404 * Even if that call is made by the Ultravisor, the SSR1 value
1405 * is the guest context one, with the secure bit clear as it has
1406 * not yet been secured. So we can't check it here.
1407 * Instead the kvm->arch.secure_guest flag is checked inside
1408 * kvmppc_h_svm_init_abort().
1410 ret = kvmppc_h_svm_init_abort(kvm);
1416 WARN_ON_ONCE(ret == H_TOO_HARD);
1417 kvmppc_set_gpr(vcpu, 3, ret);
1418 vcpu->arch.hcall_needed = 0;
1419 return RESUME_GUEST;
1423 * Handle H_CEDE in the P9 path where we don't call the real-mode hcall
1424 * handlers in book3s_hv_rmhandlers.S.
1426 * This has to be done early, not in kvmppc_pseries_do_hcall(), so
1427 * that the cede logic in kvmppc_run_single_vcpu() works properly.
1429 static void kvmppc_cede(struct kvm_vcpu *vcpu)
1431 __kvmppc_set_msr_hv(vcpu, __kvmppc_get_msr_hv(vcpu) | MSR_EE);
1432 vcpu->arch.ceded = 1;
1434 if (vcpu->arch.prodded) {
1435 vcpu->arch.prodded = 0;
1437 vcpu->arch.ceded = 0;
1441 static int kvmppc_hcall_impl_hv(unsigned long cmd)
1447 case H_REGISTER_VPA:
1449 #ifdef CONFIG_SPAPR_TCE_IOMMU
1452 case H_PUT_TCE_INDIRECT:
1455 case H_LOGICAL_CI_LOAD:
1456 case H_LOGICAL_CI_STORE:
1457 #ifdef CONFIG_KVM_XICS
1466 case H_RPT_INVALIDATE:
1470 /* See if it's in the real-mode table */
1471 return kvmppc_hcall_impl_hv_realmode(cmd);
1474 static int kvmppc_emulate_debug_inst(struct kvm_vcpu *vcpu)
1476 ppc_inst_t last_inst;
1478 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
1481 * Fetch failed, so return to guest and
1482 * try executing it again.
1484 return RESUME_GUEST;
1487 if (ppc_inst_val(last_inst) == KVMPPC_INST_SW_BREAKPOINT) {
1488 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
1489 vcpu->run->debug.arch.address = kvmppc_get_pc(vcpu);
1492 kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
1493 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
1494 return RESUME_GUEST;
1498 static void do_nothing(void *x)
1502 static unsigned long kvmppc_read_dpdes(struct kvm_vcpu *vcpu)
1504 int thr, cpu, pcpu, nthreads;
1506 unsigned long dpdes;
1508 nthreads = vcpu->kvm->arch.emul_smt_mode;
1510 cpu = vcpu->vcpu_id & ~(nthreads - 1);
1511 for (thr = 0; thr < nthreads; ++thr, ++cpu) {
1512 v = kvmppc_find_vcpu(vcpu->kvm, cpu);
1516 * If the vcpu is currently running on a physical cpu thread,
1517 * interrupt it in order to pull it out of the guest briefly,
1518 * which will update its vcore->dpdes value.
1520 pcpu = READ_ONCE(v->cpu);
1522 smp_call_function_single(pcpu, do_nothing, NULL, 1);
1523 if (kvmppc_doorbell_pending(v))
1530 * On POWER9, emulate doorbell-related instructions in order to
1531 * give the guest the illusion of running on a multi-threaded core.
1532 * The instructions emulated are msgsndp, msgclrp, mfspr TIR,
1535 static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)
1539 struct kvm *kvm = vcpu->kvm;
1540 struct kvm_vcpu *tvcpu;
1543 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &pinst) != EMULATE_DONE)
1544 return RESUME_GUEST;
1545 inst = ppc_inst_val(pinst);
1546 if (get_op(inst) != 31)
1547 return EMULATE_FAIL;
1549 thr = vcpu->vcpu_id & (kvm->arch.emul_smt_mode - 1);
1550 switch (get_xop(inst)) {
1551 case OP_31_XOP_MSGSNDP:
1552 arg = kvmppc_get_gpr(vcpu, rb);
1553 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
1556 if (arg >= kvm->arch.emul_smt_mode)
1558 tvcpu = kvmppc_find_vcpu(kvm, vcpu->vcpu_id - thr + arg);
1561 if (!tvcpu->arch.doorbell_request) {
1562 tvcpu->arch.doorbell_request = 1;
1563 kvmppc_fast_vcpu_kick_hv(tvcpu);
1566 case OP_31_XOP_MSGCLRP:
1567 arg = kvmppc_get_gpr(vcpu, rb);
1568 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
1570 vcpu->arch.vcore->dpdes = 0;
1571 vcpu->arch.doorbell_request = 0;
1573 case OP_31_XOP_MFSPR:
1574 switch (get_sprn(inst)) {
1579 arg = kvmppc_read_dpdes(vcpu);
1582 return EMULATE_FAIL;
1584 kvmppc_set_gpr(vcpu, get_rt(inst), arg);
1587 return EMULATE_FAIL;
1589 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
1590 return RESUME_GUEST;
1594 * If the lppaca had pmcregs_in_use clear when we exited the guest, then
1595 * HFSCR_PM is cleared for next entry. If the guest then tries to access
1596 * the PMU SPRs, we get this facility unavailable interrupt. Putting HFSCR_PM
1597 * back in the guest HFSCR will cause the next entry to load the PMU SPRs and
1598 * allow the guest access to continue.
1600 static int kvmppc_pmu_unavailable(struct kvm_vcpu *vcpu)
1602 if (!(vcpu->arch.hfscr_permitted & HFSCR_PM))
1603 return EMULATE_FAIL;
1605 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_PM);
1607 return RESUME_GUEST;
1610 static int kvmppc_ebb_unavailable(struct kvm_vcpu *vcpu)
1612 if (!(vcpu->arch.hfscr_permitted & HFSCR_EBB))
1613 return EMULATE_FAIL;
1615 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_EBB);
1617 return RESUME_GUEST;
1620 static int kvmppc_tm_unavailable(struct kvm_vcpu *vcpu)
1622 if (!(vcpu->arch.hfscr_permitted & HFSCR_TM))
1623 return EMULATE_FAIL;
1625 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_TM);
1627 return RESUME_GUEST;
1630 static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,
1631 struct task_struct *tsk)
1633 struct kvm_run *run = vcpu->run;
1634 int r = RESUME_HOST;
1636 vcpu->stat.sum_exits++;
1639 * This can happen if an interrupt occurs in the last stages
1640 * of guest entry or the first stages of guest exit (i.e. after
1641 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
1642 * and before setting it to KVM_GUEST_MODE_HOST_HV).
1643 * That can happen due to a bug, or due to a machine check
1644 * occurring at just the wrong time.
1646 if (!kvmhv_is_nestedv2() && (__kvmppc_get_msr_hv(vcpu) & MSR_HV)) {
1647 printk(KERN_EMERG "KVM trap in HV mode!\n");
1648 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1649 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1650 vcpu->arch.shregs.msr);
1651 kvmppc_dump_regs(vcpu);
1652 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1653 run->hw.hardware_exit_reason = vcpu->arch.trap;
1656 run->exit_reason = KVM_EXIT_UNKNOWN;
1657 run->ready_for_interrupt_injection = 1;
1658 switch (vcpu->arch.trap) {
1659 /* We're good on these - the host merely wanted to get our attention */
1660 case BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER:
1661 WARN_ON_ONCE(1); /* Should never happen */
1662 vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER;
1664 case BOOK3S_INTERRUPT_HV_DECREMENTER:
1665 vcpu->stat.dec_exits++;
1668 case BOOK3S_INTERRUPT_EXTERNAL:
1669 case BOOK3S_INTERRUPT_H_DOORBELL:
1670 case BOOK3S_INTERRUPT_H_VIRT:
1671 vcpu->stat.ext_intr_exits++;
1674 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
1675 case BOOK3S_INTERRUPT_HMI:
1676 case BOOK3S_INTERRUPT_PERFMON:
1677 case BOOK3S_INTERRUPT_SYSTEM_RESET:
1680 case BOOK3S_INTERRUPT_MACHINE_CHECK: {
1681 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
1682 DEFAULT_RATELIMIT_BURST);
1684 * Print the MCE event to host console. Ratelimit so the guest
1685 * can't flood the host log.
1687 if (__ratelimit(&rs))
1688 machine_check_print_event_info(&vcpu->arch.mce_evt,false, true);
1691 * If the guest can do FWNMI, exit to userspace so it can
1692 * deliver a FWNMI to the guest.
1693 * Otherwise we synthesize a machine check for the guest
1694 * so that it knows that the machine check occurred.
1696 if (!vcpu->kvm->arch.fwnmi_enabled) {
1697 ulong flags = (__kvmppc_get_msr_hv(vcpu) & 0x083c0000) |
1698 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED);
1699 kvmppc_core_queue_machine_check(vcpu, flags);
1704 /* Exit to guest with KVM_EXIT_NMI as exit reason */
1705 run->exit_reason = KVM_EXIT_NMI;
1706 run->hw.hardware_exit_reason = vcpu->arch.trap;
1707 /* Clear out the old NMI status from run->flags */
1708 run->flags &= ~KVM_RUN_PPC_NMI_DISP_MASK;
1709 /* Now set the NMI status */
1710 if (vcpu->arch.mce_evt.disposition == MCE_DISPOSITION_RECOVERED)
1711 run->flags |= KVM_RUN_PPC_NMI_DISP_FULLY_RECOV;
1713 run->flags |= KVM_RUN_PPC_NMI_DISP_NOT_RECOV;
1718 case BOOK3S_INTERRUPT_PROGRAM:
1722 * Normally program interrupts are delivered directly
1723 * to the guest by the hardware, but we can get here
1724 * as a result of a hypervisor emulation interrupt
1725 * (e40) getting turned into a 700 by BML RTAS.
1727 flags = (__kvmppc_get_msr_hv(vcpu) & 0x1f0000ull) |
1728 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED);
1729 kvmppc_core_queue_program(vcpu, flags);
1733 case BOOK3S_INTERRUPT_SYSCALL:
1737 if (!kvmhv_is_nestedv2() && unlikely(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) {
1739 * Guest userspace executed sc 1. This can only be
1740 * reached by the P9 path because the old path
1741 * handles this case in realmode hcall handlers.
1743 if (!kvmhv_vcpu_is_radix(vcpu)) {
1745 * A guest could be running PR KVM, so this
1746 * may be a PR KVM hcall. It must be reflected
1747 * to the guest kernel as a sc interrupt.
1749 kvmppc_core_queue_syscall(vcpu);
1752 * Radix guests can not run PR KVM or nested HV
1753 * hash guests which might run PR KVM, so this
1754 * is always a privilege fault. Send a program
1755 * check to guest kernel.
1757 kvmppc_core_queue_program(vcpu, SRR1_PROGPRIV);
1764 * hcall - gather args and set exit_reason. This will next be
1765 * handled by kvmppc_pseries_do_hcall which may be able to deal
1766 * with it and resume guest, or may punt to userspace.
1768 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
1769 for (i = 0; i < 9; ++i)
1770 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
1771 run->exit_reason = KVM_EXIT_PAPR_HCALL;
1772 vcpu->arch.hcall_needed = 1;
1777 * We get these next two if the guest accesses a page which it thinks
1778 * it has mapped but which is not actually present, either because
1779 * it is for an emulated I/O device or because the corresonding
1780 * host page has been paged out.
1782 * Any other HDSI/HISI interrupts have been handled already for P7/8
1783 * guests. For POWER9 hash guests not using rmhandlers, basic hash
1784 * fault handling is done here.
1786 case BOOK3S_INTERRUPT_H_DATA_STORAGE: {
1790 if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) &&
1791 unlikely(vcpu->arch.fault_dsisr == HDSISR_CANARY)) {
1792 r = RESUME_GUEST; /* Just retry if it's the canary */
1796 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
1798 * Radix doesn't require anything, and pre-ISAv3.0 hash
1799 * already attempted to handle this in rmhandlers. The
1800 * hash fault handling below is v3 only (it uses ASDR
1803 r = RESUME_PAGE_FAULT;
1807 if (!(vcpu->arch.fault_dsisr & (DSISR_NOHPTE | DSISR_PROTFAULT))) {
1808 kvmppc_core_queue_data_storage(vcpu,
1809 kvmppc_get_msr(vcpu) & SRR1_PREFIXED,
1810 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1815 if (!(__kvmppc_get_msr_hv(vcpu) & MSR_DR))
1816 vsid = vcpu->kvm->arch.vrma_slb_v;
1818 vsid = vcpu->arch.fault_gpa;
1820 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
1821 vsid, vcpu->arch.fault_dsisr, true);
1824 } else if (err == -1 || err == -2) {
1825 r = RESUME_PAGE_FAULT;
1827 kvmppc_core_queue_data_storage(vcpu,
1828 kvmppc_get_msr(vcpu) & SRR1_PREFIXED,
1829 vcpu->arch.fault_dar, err);
1834 case BOOK3S_INTERRUPT_H_INST_STORAGE: {
1838 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
1839 vcpu->arch.fault_dsisr = __kvmppc_get_msr_hv(vcpu) &
1840 DSISR_SRR1_MATCH_64S;
1841 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
1843 * Radix doesn't require anything, and pre-ISAv3.0 hash
1844 * already attempted to handle this in rmhandlers. The
1845 * hash fault handling below is v3 only (it uses ASDR
1848 if (__kvmppc_get_msr_hv(vcpu) & HSRR1_HISI_WRITE)
1849 vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
1850 r = RESUME_PAGE_FAULT;
1854 if (!(vcpu->arch.fault_dsisr & SRR1_ISI_NOPT)) {
1855 kvmppc_core_queue_inst_storage(vcpu,
1856 vcpu->arch.fault_dsisr |
1857 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
1862 if (!(__kvmppc_get_msr_hv(vcpu) & MSR_IR))
1863 vsid = vcpu->kvm->arch.vrma_slb_v;
1865 vsid = vcpu->arch.fault_gpa;
1867 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
1868 vsid, vcpu->arch.fault_dsisr, false);
1871 } else if (err == -1) {
1872 r = RESUME_PAGE_FAULT;
1874 kvmppc_core_queue_inst_storage(vcpu,
1875 err | (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
1882 * This occurs if the guest executes an illegal instruction.
1883 * If the guest debug is disabled, generate a program interrupt
1884 * to the guest. If guest debug is enabled, we need to check
1885 * whether the instruction is a software breakpoint instruction.
1886 * Accordingly return to Guest or Host.
1888 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
1889 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
1890 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
1891 swab32(vcpu->arch.emul_inst) :
1892 vcpu->arch.emul_inst;
1893 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
1894 r = kvmppc_emulate_debug_inst(vcpu);
1896 kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
1897 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
1902 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1903 case BOOK3S_INTERRUPT_HV_SOFTPATCH:
1905 * This occurs for various TM-related instructions that
1906 * we need to emulate on POWER9 DD2.2. We have already
1907 * handled the cases where the guest was in real-suspend
1908 * mode and was transitioning to transactional state.
1910 r = kvmhv_p9_tm_emulation(vcpu);
1913 fallthrough; /* go to facility unavailable handler */
1917 * This occurs if the guest (kernel or userspace), does something that
1918 * is prohibited by HFSCR.
1919 * On POWER9, this could be a doorbell instruction that we need
1921 * Otherwise, we just generate a program interrupt to the guest.
1923 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: {
1924 u64 cause = kvmppc_get_hfscr_hv(vcpu) >> 56;
1927 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1930 r = kvmppc_emulate_doorbell_instr(vcpu);
1933 r = kvmppc_pmu_unavailable(vcpu);
1936 r = kvmppc_ebb_unavailable(vcpu);
1939 r = kvmppc_tm_unavailable(vcpu);
1945 if (r == EMULATE_FAIL) {
1946 kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
1947 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
1953 case BOOK3S_INTERRUPT_HV_RM_HARD:
1954 r = RESUME_PASSTHROUGH;
1957 kvmppc_dump_regs(vcpu);
1958 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1959 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1960 __kvmppc_get_msr_hv(vcpu));
1961 run->hw.hardware_exit_reason = vcpu->arch.trap;
1969 static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)
1974 vcpu->stat.sum_exits++;
1977 * This can happen if an interrupt occurs in the last stages
1978 * of guest entry or the first stages of guest exit (i.e. after
1979 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
1980 * and before setting it to KVM_GUEST_MODE_HOST_HV).
1981 * That can happen due to a bug, or due to a machine check
1982 * occurring at just the wrong time.
1984 if (__kvmppc_get_msr_hv(vcpu) & MSR_HV) {
1985 pr_emerg("KVM trap in HV mode while nested!\n");
1986 pr_emerg("trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1987 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1988 __kvmppc_get_msr_hv(vcpu));
1989 kvmppc_dump_regs(vcpu);
1992 switch (vcpu->arch.trap) {
1993 /* We're good on these - the host merely wanted to get our attention */
1994 case BOOK3S_INTERRUPT_HV_DECREMENTER:
1995 vcpu->stat.dec_exits++;
1998 case BOOK3S_INTERRUPT_EXTERNAL:
1999 vcpu->stat.ext_intr_exits++;
2002 case BOOK3S_INTERRUPT_H_DOORBELL:
2003 case BOOK3S_INTERRUPT_H_VIRT:
2004 vcpu->stat.ext_intr_exits++;
2007 /* These need to go to the nested HV */
2008 case BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER:
2009 vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER;
2010 vcpu->stat.dec_exits++;
2013 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
2014 case BOOK3S_INTERRUPT_HMI:
2015 case BOOK3S_INTERRUPT_PERFMON:
2016 case BOOK3S_INTERRUPT_SYSTEM_RESET:
2019 case BOOK3S_INTERRUPT_MACHINE_CHECK:
2021 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
2022 DEFAULT_RATELIMIT_BURST);
2023 /* Pass the machine check to the L1 guest */
2025 /* Print the MCE event to host console. */
2026 if (__ratelimit(&rs))
2027 machine_check_print_event_info(&vcpu->arch.mce_evt, false, true);
2031 * We get these next two if the guest accesses a page which it thinks
2032 * it has mapped but which is not actually present, either because
2033 * it is for an emulated I/O device or because the corresonding
2034 * host page has been paged out.
2036 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
2037 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2038 r = kvmhv_nested_page_fault(vcpu);
2039 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2041 case BOOK3S_INTERRUPT_H_INST_STORAGE:
2042 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
2043 vcpu->arch.fault_dsisr = kvmppc_get_msr(vcpu) &
2044 DSISR_SRR1_MATCH_64S;
2045 if (__kvmppc_get_msr_hv(vcpu) & HSRR1_HISI_WRITE)
2046 vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
2047 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2048 r = kvmhv_nested_page_fault(vcpu);
2049 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2052 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2053 case BOOK3S_INTERRUPT_HV_SOFTPATCH:
2055 * This occurs for various TM-related instructions that
2056 * we need to emulate on POWER9 DD2.2. We have already
2057 * handled the cases where the guest was in real-suspend
2058 * mode and was transitioning to transactional state.
2060 r = kvmhv_p9_tm_emulation(vcpu);
2063 fallthrough; /* go to facility unavailable handler */
2066 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
2070 case BOOK3S_INTERRUPT_HV_RM_HARD:
2071 vcpu->arch.trap = 0;
2073 if (!xics_on_xive())
2074 kvmppc_xics_rm_complete(vcpu, 0);
2076 case BOOK3S_INTERRUPT_SYSCALL:
2078 unsigned long req = kvmppc_get_gpr(vcpu, 3);
2081 * The H_RPT_INVALIDATE hcalls issued by nested
2082 * guests for process-scoped invalidations when
2083 * GTSE=0, are handled here in L0.
2085 if (req == H_RPT_INVALIDATE) {
2086 r = kvmppc_nested_h_rpt_invalidate(vcpu);
2101 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
2102 struct kvm_sregs *sregs)
2106 memset(sregs, 0, sizeof(struct kvm_sregs));
2107 sregs->pvr = vcpu->arch.pvr;
2108 for (i = 0; i < vcpu->arch.slb_max; i++) {
2109 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
2110 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
2116 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
2117 struct kvm_sregs *sregs)
2121 /* Only accept the same PVR as the host's, since we can't spoof it */
2122 if (sregs->pvr != vcpu->arch.pvr)
2126 for (i = 0; i < vcpu->arch.slb_nr; i++) {
2127 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
2128 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
2129 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
2133 vcpu->arch.slb_max = j;
2139 * Enforce limits on guest LPCR values based on hardware availability,
2140 * guest configuration, and possibly hypervisor support and security
2143 unsigned long kvmppc_filter_lpcr_hv(struct kvm *kvm, unsigned long lpcr)
2145 /* LPCR_TC only applies to HPT guests */
2146 if (kvm_is_radix(kvm))
2149 /* On POWER8 and above, userspace can modify AIL */
2150 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2152 if ((lpcr & LPCR_AIL) != LPCR_AIL_3)
2153 lpcr &= ~LPCR_AIL; /* LPCR[AIL]=1/2 is disallowed */
2155 * On some POWER9s we force AIL off for radix guests to prevent
2156 * executing in MSR[HV]=1 mode with the MMU enabled and PIDR set to
2157 * guest, which can result in Q0 translations with LPID=0 PID=PIDR to
2158 * be cached, which the host TLB management does not expect.
2160 if (kvm_is_radix(kvm) && cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG))
2164 * On POWER9, allow userspace to enable large decrementer for the
2165 * guest, whether or not the host has it enabled.
2167 if (!cpu_has_feature(CPU_FTR_ARCH_300))
2173 static void verify_lpcr(struct kvm *kvm, unsigned long lpcr)
2175 if (lpcr != kvmppc_filter_lpcr_hv(kvm, lpcr)) {
2176 WARN_ONCE(1, "lpcr 0x%lx differs from filtered 0x%lx\n",
2177 lpcr, kvmppc_filter_lpcr_hv(kvm, lpcr));
2181 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
2182 bool preserve_top32)
2184 struct kvm *kvm = vcpu->kvm;
2185 struct kvmppc_vcore *vc = vcpu->arch.vcore;
2188 spin_lock(&vc->lock);
2191 * Userspace can only modify
2192 * DPFD (default prefetch depth), ILE (interrupt little-endian),
2193 * TC (translation control), AIL (alternate interrupt location),
2194 * LD (large decrementer).
2195 * These are subject to restrictions from kvmppc_filter_lcpr_hv().
2197 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD;
2199 /* Broken 32-bit version of LPCR must not clear top bits */
2203 new_lpcr = kvmppc_filter_lpcr_hv(kvm,
2204 (vc->lpcr & ~mask) | (new_lpcr & mask));
2207 * If ILE (interrupt little-endian) has changed, update the
2208 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
2210 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
2211 struct kvm_vcpu *vcpu;
2214 kvm_for_each_vcpu(i, vcpu, kvm) {
2215 if (vcpu->arch.vcore != vc)
2217 if (new_lpcr & LPCR_ILE)
2218 vcpu->arch.intr_msr |= MSR_LE;
2220 vcpu->arch.intr_msr &= ~MSR_LE;
2224 vc->lpcr = new_lpcr;
2225 kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LPCR);
2227 spin_unlock(&vc->lock);
2230 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
2231 union kvmppc_one_reg *val)
2237 case KVM_REG_PPC_DEBUG_INST:
2238 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
2240 case KVM_REG_PPC_HIOR:
2241 *val = get_reg_val(id, 0);
2243 case KVM_REG_PPC_DABR:
2244 *val = get_reg_val(id, vcpu->arch.dabr);
2246 case KVM_REG_PPC_DABRX:
2247 *val = get_reg_val(id, vcpu->arch.dabrx);
2249 case KVM_REG_PPC_DSCR:
2250 *val = get_reg_val(id, kvmppc_get_dscr_hv(vcpu));
2252 case KVM_REG_PPC_PURR:
2253 *val = get_reg_val(id, kvmppc_get_purr_hv(vcpu));
2255 case KVM_REG_PPC_SPURR:
2256 *val = get_reg_val(id, kvmppc_get_spurr_hv(vcpu));
2258 case KVM_REG_PPC_AMR:
2259 *val = get_reg_val(id, kvmppc_get_amr_hv(vcpu));
2261 case KVM_REG_PPC_UAMOR:
2262 *val = get_reg_val(id, kvmppc_get_uamor_hv(vcpu));
2264 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
2265 i = id - KVM_REG_PPC_MMCR0;
2266 *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, i));
2268 case KVM_REG_PPC_MMCR2:
2269 *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, 2));
2271 case KVM_REG_PPC_MMCRA:
2272 *val = get_reg_val(id, kvmppc_get_mmcra_hv(vcpu));
2274 case KVM_REG_PPC_MMCRS:
2275 *val = get_reg_val(id, vcpu->arch.mmcrs);
2277 case KVM_REG_PPC_MMCR3:
2278 *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, 3));
2280 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
2281 i = id - KVM_REG_PPC_PMC1;
2282 *val = get_reg_val(id, kvmppc_get_pmc_hv(vcpu, i));
2284 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
2285 i = id - KVM_REG_PPC_SPMC1;
2286 *val = get_reg_val(id, vcpu->arch.spmc[i]);
2288 case KVM_REG_PPC_SIAR:
2289 *val = get_reg_val(id, kvmppc_get_siar_hv(vcpu));
2291 case KVM_REG_PPC_SDAR:
2292 *val = get_reg_val(id, kvmppc_get_sdar_hv(vcpu));
2294 case KVM_REG_PPC_SIER:
2295 *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 0));
2297 case KVM_REG_PPC_SIER2:
2298 *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 1));
2300 case KVM_REG_PPC_SIER3:
2301 *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 2));
2303 case KVM_REG_PPC_IAMR:
2304 *val = get_reg_val(id, kvmppc_get_iamr_hv(vcpu));
2306 case KVM_REG_PPC_PSPB:
2307 *val = get_reg_val(id, kvmppc_get_pspb_hv(vcpu));
2309 case KVM_REG_PPC_DPDES:
2311 * On POWER9, where we are emulating msgsndp etc.,
2312 * we return 1 bit for each vcpu, which can come from
2313 * either vcore->dpdes or doorbell_request.
2314 * On POWER8, doorbell_request is 0.
2316 if (cpu_has_feature(CPU_FTR_ARCH_300))
2317 *val = get_reg_val(id, vcpu->arch.doorbell_request);
2319 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
2321 case KVM_REG_PPC_VTB:
2322 *val = get_reg_val(id, kvmppc_get_vtb(vcpu));
2324 case KVM_REG_PPC_DAWR:
2325 *val = get_reg_val(id, kvmppc_get_dawr0_hv(vcpu));
2327 case KVM_REG_PPC_DAWRX:
2328 *val = get_reg_val(id, kvmppc_get_dawrx0_hv(vcpu));
2330 case KVM_REG_PPC_DAWR1:
2331 *val = get_reg_val(id, kvmppc_get_dawr1_hv(vcpu));
2333 case KVM_REG_PPC_DAWRX1:
2334 *val = get_reg_val(id, kvmppc_get_dawrx1_hv(vcpu));
2336 case KVM_REG_PPC_DEXCR:
2337 *val = get_reg_val(id, kvmppc_get_dexcr_hv(vcpu));
2339 case KVM_REG_PPC_HASHKEYR:
2340 *val = get_reg_val(id, kvmppc_get_hashkeyr_hv(vcpu));
2342 case KVM_REG_PPC_HASHPKEYR:
2343 *val = get_reg_val(id, kvmppc_get_hashpkeyr_hv(vcpu));
2345 case KVM_REG_PPC_CIABR:
2346 *val = get_reg_val(id, kvmppc_get_ciabr_hv(vcpu));
2348 case KVM_REG_PPC_CSIGR:
2349 *val = get_reg_val(id, vcpu->arch.csigr);
2351 case KVM_REG_PPC_TACR:
2352 *val = get_reg_val(id, vcpu->arch.tacr);
2354 case KVM_REG_PPC_TCSCR:
2355 *val = get_reg_val(id, vcpu->arch.tcscr);
2357 case KVM_REG_PPC_PID:
2358 *val = get_reg_val(id, kvmppc_get_pid(vcpu));
2360 case KVM_REG_PPC_ACOP:
2361 *val = get_reg_val(id, vcpu->arch.acop);
2363 case KVM_REG_PPC_WORT:
2364 *val = get_reg_val(id, kvmppc_get_wort_hv(vcpu));
2366 case KVM_REG_PPC_TIDR:
2367 *val = get_reg_val(id, vcpu->arch.tid);
2369 case KVM_REG_PPC_PSSCR:
2370 *val = get_reg_val(id, vcpu->arch.psscr);
2372 case KVM_REG_PPC_VPA_ADDR:
2373 spin_lock(&vcpu->arch.vpa_update_lock);
2374 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
2375 spin_unlock(&vcpu->arch.vpa_update_lock);
2377 case KVM_REG_PPC_VPA_SLB:
2378 spin_lock(&vcpu->arch.vpa_update_lock);
2379 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
2380 val->vpaval.length = vcpu->arch.slb_shadow.len;
2381 spin_unlock(&vcpu->arch.vpa_update_lock);
2383 case KVM_REG_PPC_VPA_DTL:
2384 spin_lock(&vcpu->arch.vpa_update_lock);
2385 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
2386 val->vpaval.length = vcpu->arch.dtl.len;
2387 spin_unlock(&vcpu->arch.vpa_update_lock);
2389 case KVM_REG_PPC_TB_OFFSET:
2390 *val = get_reg_val(id, kvmppc_get_tb_offset(vcpu));
2392 case KVM_REG_PPC_LPCR:
2393 case KVM_REG_PPC_LPCR_64:
2394 *val = get_reg_val(id, kvmppc_get_lpcr(vcpu));
2396 case KVM_REG_PPC_PPR:
2397 *val = get_reg_val(id, kvmppc_get_ppr_hv(vcpu));
2399 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2400 case KVM_REG_PPC_TFHAR:
2401 *val = get_reg_val(id, vcpu->arch.tfhar);
2403 case KVM_REG_PPC_TFIAR:
2404 *val = get_reg_val(id, vcpu->arch.tfiar);
2406 case KVM_REG_PPC_TEXASR:
2407 *val = get_reg_val(id, vcpu->arch.texasr);
2409 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
2410 i = id - KVM_REG_PPC_TM_GPR0;
2411 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
2413 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
2416 i = id - KVM_REG_PPC_TM_VSR0;
2418 for (j = 0; j < TS_FPRWIDTH; j++)
2419 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
2421 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2422 val->vval = vcpu->arch.vr_tm.vr[i-32];
2428 case KVM_REG_PPC_TM_CR:
2429 *val = get_reg_val(id, vcpu->arch.cr_tm);
2431 case KVM_REG_PPC_TM_XER:
2432 *val = get_reg_val(id, vcpu->arch.xer_tm);
2434 case KVM_REG_PPC_TM_LR:
2435 *val = get_reg_val(id, vcpu->arch.lr_tm);
2437 case KVM_REG_PPC_TM_CTR:
2438 *val = get_reg_val(id, vcpu->arch.ctr_tm);
2440 case KVM_REG_PPC_TM_FPSCR:
2441 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
2443 case KVM_REG_PPC_TM_AMR:
2444 *val = get_reg_val(id, vcpu->arch.amr_tm);
2446 case KVM_REG_PPC_TM_PPR:
2447 *val = get_reg_val(id, vcpu->arch.ppr_tm);
2449 case KVM_REG_PPC_TM_VRSAVE:
2450 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
2452 case KVM_REG_PPC_TM_VSCR:
2453 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2454 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
2458 case KVM_REG_PPC_TM_DSCR:
2459 *val = get_reg_val(id, vcpu->arch.dscr_tm);
2461 case KVM_REG_PPC_TM_TAR:
2462 *val = get_reg_val(id, vcpu->arch.tar_tm);
2465 case KVM_REG_PPC_ARCH_COMPAT:
2466 *val = get_reg_val(id, kvmppc_get_arch_compat(vcpu));
2468 case KVM_REG_PPC_DEC_EXPIRY:
2469 *val = get_reg_val(id, kvmppc_get_dec_expires(vcpu));
2471 case KVM_REG_PPC_ONLINE:
2472 *val = get_reg_val(id, vcpu->arch.online);
2474 case KVM_REG_PPC_PTCR:
2475 *val = get_reg_val(id, vcpu->kvm->arch.l1_ptcr);
2477 case KVM_REG_PPC_FSCR:
2478 *val = get_reg_val(id, kvmppc_get_fscr_hv(vcpu));
2488 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
2489 union kvmppc_one_reg *val)
2493 unsigned long addr, len;
2496 case KVM_REG_PPC_HIOR:
2497 /* Only allow this to be set to zero */
2498 if (set_reg_val(id, *val))
2501 case KVM_REG_PPC_DABR:
2502 vcpu->arch.dabr = set_reg_val(id, *val);
2504 case KVM_REG_PPC_DABRX:
2505 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
2507 case KVM_REG_PPC_DSCR:
2508 kvmppc_set_dscr_hv(vcpu, set_reg_val(id, *val));
2510 case KVM_REG_PPC_PURR:
2511 kvmppc_set_purr_hv(vcpu, set_reg_val(id, *val));
2513 case KVM_REG_PPC_SPURR:
2514 kvmppc_set_spurr_hv(vcpu, set_reg_val(id, *val));
2516 case KVM_REG_PPC_AMR:
2517 kvmppc_set_amr_hv(vcpu, set_reg_val(id, *val));
2519 case KVM_REG_PPC_UAMOR:
2520 kvmppc_set_uamor_hv(vcpu, set_reg_val(id, *val));
2522 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
2523 i = id - KVM_REG_PPC_MMCR0;
2524 kvmppc_set_mmcr_hv(vcpu, i, set_reg_val(id, *val));
2526 case KVM_REG_PPC_MMCR2:
2527 kvmppc_set_mmcr_hv(vcpu, 2, set_reg_val(id, *val));
2529 case KVM_REG_PPC_MMCRA:
2530 kvmppc_set_mmcra_hv(vcpu, set_reg_val(id, *val));
2532 case KVM_REG_PPC_MMCRS:
2533 vcpu->arch.mmcrs = set_reg_val(id, *val);
2535 case KVM_REG_PPC_MMCR3:
2536 kvmppc_set_mmcr_hv(vcpu, 3, set_reg_val(id, *val));
2538 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
2539 i = id - KVM_REG_PPC_PMC1;
2540 kvmppc_set_pmc_hv(vcpu, i, set_reg_val(id, *val));
2542 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
2543 i = id - KVM_REG_PPC_SPMC1;
2544 vcpu->arch.spmc[i] = set_reg_val(id, *val);
2546 case KVM_REG_PPC_SIAR:
2547 kvmppc_set_siar_hv(vcpu, set_reg_val(id, *val));
2549 case KVM_REG_PPC_SDAR:
2550 kvmppc_set_sdar_hv(vcpu, set_reg_val(id, *val));
2552 case KVM_REG_PPC_SIER:
2553 kvmppc_set_sier_hv(vcpu, 0, set_reg_val(id, *val));
2555 case KVM_REG_PPC_SIER2:
2556 kvmppc_set_sier_hv(vcpu, 1, set_reg_val(id, *val));
2558 case KVM_REG_PPC_SIER3:
2559 kvmppc_set_sier_hv(vcpu, 2, set_reg_val(id, *val));
2561 case KVM_REG_PPC_IAMR:
2562 kvmppc_set_iamr_hv(vcpu, set_reg_val(id, *val));
2564 case KVM_REG_PPC_PSPB:
2565 kvmppc_set_pspb_hv(vcpu, set_reg_val(id, *val));
2567 case KVM_REG_PPC_DPDES:
2568 if (cpu_has_feature(CPU_FTR_ARCH_300))
2569 vcpu->arch.doorbell_request = set_reg_val(id, *val) & 1;
2571 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
2573 case KVM_REG_PPC_VTB:
2574 kvmppc_set_vtb(vcpu, set_reg_val(id, *val));
2576 case KVM_REG_PPC_DAWR:
2577 kvmppc_set_dawr0_hv(vcpu, set_reg_val(id, *val));
2579 case KVM_REG_PPC_DAWRX:
2580 kvmppc_set_dawrx0_hv(vcpu, set_reg_val(id, *val) & ~DAWRX_HYP);
2582 case KVM_REG_PPC_DAWR1:
2583 kvmppc_set_dawr1_hv(vcpu, set_reg_val(id, *val));
2585 case KVM_REG_PPC_DAWRX1:
2586 kvmppc_set_dawrx1_hv(vcpu, set_reg_val(id, *val) & ~DAWRX_HYP);
2588 case KVM_REG_PPC_DEXCR:
2589 kvmppc_set_dexcr_hv(vcpu, set_reg_val(id, *val));
2591 case KVM_REG_PPC_HASHKEYR:
2592 kvmppc_set_hashkeyr_hv(vcpu, set_reg_val(id, *val));
2594 case KVM_REG_PPC_HASHPKEYR:
2595 kvmppc_set_hashpkeyr_hv(vcpu, set_reg_val(id, *val));
2597 case KVM_REG_PPC_CIABR:
2598 kvmppc_set_ciabr_hv(vcpu, set_reg_val(id, *val));
2599 /* Don't allow setting breakpoints in hypervisor code */
2600 if ((kvmppc_get_ciabr_hv(vcpu) & CIABR_PRIV) == CIABR_PRIV_HYPER)
2601 kvmppc_set_ciabr_hv(vcpu, kvmppc_get_ciabr_hv(vcpu) & ~CIABR_PRIV);
2603 case KVM_REG_PPC_CSIGR:
2604 vcpu->arch.csigr = set_reg_val(id, *val);
2606 case KVM_REG_PPC_TACR:
2607 vcpu->arch.tacr = set_reg_val(id, *val);
2609 case KVM_REG_PPC_TCSCR:
2610 vcpu->arch.tcscr = set_reg_val(id, *val);
2612 case KVM_REG_PPC_PID:
2613 kvmppc_set_pid(vcpu, set_reg_val(id, *val));
2615 case KVM_REG_PPC_ACOP:
2616 vcpu->arch.acop = set_reg_val(id, *val);
2618 case KVM_REG_PPC_WORT:
2619 kvmppc_set_wort_hv(vcpu, set_reg_val(id, *val));
2621 case KVM_REG_PPC_TIDR:
2622 vcpu->arch.tid = set_reg_val(id, *val);
2624 case KVM_REG_PPC_PSSCR:
2625 vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
2627 case KVM_REG_PPC_VPA_ADDR:
2628 addr = set_reg_val(id, *val);
2630 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
2631 vcpu->arch.dtl.next_gpa))
2633 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
2635 case KVM_REG_PPC_VPA_SLB:
2636 addr = val->vpaval.addr;
2637 len = val->vpaval.length;
2639 if (addr && !vcpu->arch.vpa.next_gpa)
2641 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
2643 case KVM_REG_PPC_VPA_DTL:
2644 addr = val->vpaval.addr;
2645 len = val->vpaval.length;
2647 if (addr && (len < sizeof(struct dtl_entry) ||
2648 !vcpu->arch.vpa.next_gpa))
2650 len -= len % sizeof(struct dtl_entry);
2651 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
2653 case KVM_REG_PPC_TB_OFFSET:
2655 /* round up to multiple of 2^24 */
2656 u64 tb_offset = ALIGN(set_reg_val(id, *val), 1UL << 24);
2659 * Now that we know the timebase offset, update the
2660 * decrementer expiry with a guest timebase value. If
2661 * the userspace does not set DEC_EXPIRY, this ensures
2662 * a migrated vcpu at least starts with an expired
2663 * decrementer, which is better than a large one that
2666 kvmppc_set_tb_offset(vcpu, tb_offset);
2667 if (!kvmppc_get_dec_expires(vcpu) && tb_offset)
2668 kvmppc_set_dec_expires(vcpu, get_tb() + tb_offset);
2670 kvmppc_set_tb_offset(vcpu, tb_offset);
2673 case KVM_REG_PPC_LPCR:
2674 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
2676 case KVM_REG_PPC_LPCR_64:
2677 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
2679 case KVM_REG_PPC_PPR:
2680 kvmppc_set_ppr_hv(vcpu, set_reg_val(id, *val));
2682 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2683 case KVM_REG_PPC_TFHAR:
2684 vcpu->arch.tfhar = set_reg_val(id, *val);
2686 case KVM_REG_PPC_TFIAR:
2687 vcpu->arch.tfiar = set_reg_val(id, *val);
2689 case KVM_REG_PPC_TEXASR:
2690 vcpu->arch.texasr = set_reg_val(id, *val);
2692 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
2693 i = id - KVM_REG_PPC_TM_GPR0;
2694 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
2696 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
2699 i = id - KVM_REG_PPC_TM_VSR0;
2701 for (j = 0; j < TS_FPRWIDTH; j++)
2702 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
2704 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2705 vcpu->arch.vr_tm.vr[i-32] = val->vval;
2710 case KVM_REG_PPC_TM_CR:
2711 vcpu->arch.cr_tm = set_reg_val(id, *val);
2713 case KVM_REG_PPC_TM_XER:
2714 vcpu->arch.xer_tm = set_reg_val(id, *val);
2716 case KVM_REG_PPC_TM_LR:
2717 vcpu->arch.lr_tm = set_reg_val(id, *val);
2719 case KVM_REG_PPC_TM_CTR:
2720 vcpu->arch.ctr_tm = set_reg_val(id, *val);
2722 case KVM_REG_PPC_TM_FPSCR:
2723 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
2725 case KVM_REG_PPC_TM_AMR:
2726 vcpu->arch.amr_tm = set_reg_val(id, *val);
2728 case KVM_REG_PPC_TM_PPR:
2729 vcpu->arch.ppr_tm = set_reg_val(id, *val);
2731 case KVM_REG_PPC_TM_VRSAVE:
2732 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
2734 case KVM_REG_PPC_TM_VSCR:
2735 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2736 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
2740 case KVM_REG_PPC_TM_DSCR:
2741 vcpu->arch.dscr_tm = set_reg_val(id, *val);
2743 case KVM_REG_PPC_TM_TAR:
2744 vcpu->arch.tar_tm = set_reg_val(id, *val);
2747 case KVM_REG_PPC_ARCH_COMPAT:
2748 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
2750 case KVM_REG_PPC_DEC_EXPIRY:
2751 kvmppc_set_dec_expires(vcpu, set_reg_val(id, *val));
2753 case KVM_REG_PPC_ONLINE:
2754 i = set_reg_val(id, *val);
2755 if (i && !vcpu->arch.online)
2756 atomic_inc(&vcpu->arch.vcore->online_count);
2757 else if (!i && vcpu->arch.online)
2758 atomic_dec(&vcpu->arch.vcore->online_count);
2759 vcpu->arch.online = i;
2761 case KVM_REG_PPC_PTCR:
2762 vcpu->kvm->arch.l1_ptcr = set_reg_val(id, *val);
2764 case KVM_REG_PPC_FSCR:
2765 kvmppc_set_fscr_hv(vcpu, set_reg_val(id, *val));
2776 * On POWER9, threads are independent and can be in different partitions.
2777 * Therefore we consider each thread to be a subcore.
2778 * There is a restriction that all threads have to be in the same
2779 * MMU mode (radix or HPT), unfortunately, but since we only support
2780 * HPT guests on a HPT host so far, that isn't an impediment yet.
2782 static int threads_per_vcore(struct kvm *kvm)
2784 if (cpu_has_feature(CPU_FTR_ARCH_300))
2786 return threads_per_subcore;
2789 static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int id)
2791 struct kvmppc_vcore *vcore;
2793 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
2798 spin_lock_init(&vcore->lock);
2799 spin_lock_init(&vcore->stoltb_lock);
2800 rcuwait_init(&vcore->wait);
2801 vcore->preempt_tb = TB_NIL;
2802 vcore->lpcr = kvm->arch.lpcr;
2803 vcore->first_vcpuid = id;
2805 INIT_LIST_HEAD(&vcore->preempt_list);
2810 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2811 static struct debugfs_timings_element {
2815 #ifdef CONFIG_KVM_BOOK3S_HV_P9_TIMING
2816 {"vcpu_entry", offsetof(struct kvm_vcpu, arch.vcpu_entry)},
2817 {"guest_entry", offsetof(struct kvm_vcpu, arch.guest_entry)},
2818 {"in_guest", offsetof(struct kvm_vcpu, arch.in_guest)},
2819 {"guest_exit", offsetof(struct kvm_vcpu, arch.guest_exit)},
2820 {"vcpu_exit", offsetof(struct kvm_vcpu, arch.vcpu_exit)},
2821 {"hypercall", offsetof(struct kvm_vcpu, arch.hcall)},
2822 {"page_fault", offsetof(struct kvm_vcpu, arch.pg_fault)},
2824 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
2825 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
2826 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
2827 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
2828 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
2832 #define N_TIMINGS (ARRAY_SIZE(timings))
2834 struct debugfs_timings_state {
2835 struct kvm_vcpu *vcpu;
2836 unsigned int buflen;
2837 char buf[N_TIMINGS * 100];
2840 static int debugfs_timings_open(struct inode *inode, struct file *file)
2842 struct kvm_vcpu *vcpu = inode->i_private;
2843 struct debugfs_timings_state *p;
2845 p = kzalloc(sizeof(*p), GFP_KERNEL);
2849 kvm_get_kvm(vcpu->kvm);
2851 file->private_data = p;
2853 return nonseekable_open(inode, file);
2856 static int debugfs_timings_release(struct inode *inode, struct file *file)
2858 struct debugfs_timings_state *p = file->private_data;
2860 kvm_put_kvm(p->vcpu->kvm);
2865 static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
2866 size_t len, loff_t *ppos)
2868 struct debugfs_timings_state *p = file->private_data;
2869 struct kvm_vcpu *vcpu = p->vcpu;
2871 struct kvmhv_tb_accumulator tb;
2880 buf_end = s + sizeof(p->buf);
2881 for (i = 0; i < N_TIMINGS; ++i) {
2882 struct kvmhv_tb_accumulator *acc;
2884 acc = (struct kvmhv_tb_accumulator *)
2885 ((unsigned long)vcpu + timings[i].offset);
2887 for (loops = 0; loops < 1000; ++loops) {
2888 count = acc->seqcount;
2893 if (count == acc->seqcount) {
2901 snprintf(s, buf_end - s, "%s: stuck\n",
2904 snprintf(s, buf_end - s,
2905 "%s: %llu %llu %llu %llu\n",
2906 timings[i].name, count / 2,
2907 tb_to_ns(tb.tb_total),
2908 tb_to_ns(tb.tb_min),
2909 tb_to_ns(tb.tb_max));
2912 p->buflen = s - p->buf;
2916 if (pos >= p->buflen)
2918 if (len > p->buflen - pos)
2919 len = p->buflen - pos;
2920 n = copy_to_user(buf, p->buf + pos, len);
2930 static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
2931 size_t len, loff_t *ppos)
2936 static const struct file_operations debugfs_timings_ops = {
2937 .owner = THIS_MODULE,
2938 .open = debugfs_timings_open,
2939 .release = debugfs_timings_release,
2940 .read = debugfs_timings_read,
2941 .write = debugfs_timings_write,
2942 .llseek = generic_file_llseek,
2945 /* Create a debugfs directory for the vcpu */
2946 static int kvmppc_arch_create_vcpu_debugfs_hv(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry)
2948 if (cpu_has_feature(CPU_FTR_ARCH_300) == IS_ENABLED(CONFIG_KVM_BOOK3S_HV_P9_TIMING))
2949 debugfs_create_file("timings", 0444, debugfs_dentry, vcpu,
2950 &debugfs_timings_ops);
2954 #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
2955 static int kvmppc_arch_create_vcpu_debugfs_hv(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry)
2959 #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
2961 static int kvmppc_core_vcpu_create_hv(struct kvm_vcpu *vcpu)
2965 struct kvmppc_vcore *vcore;
2972 vcpu->arch.shared = &vcpu->arch.shregs;
2973 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
2975 * The shared struct is never shared on HV,
2976 * so we can always use host endianness
2978 #ifdef __BIG_ENDIAN__
2979 vcpu->arch.shared_big_endian = true;
2981 vcpu->arch.shared_big_endian = false;
2985 if (kvmhv_is_nestedv2()) {
2986 err = kvmhv_nestedv2_vcpu_create(vcpu, &vcpu->arch.nestedv2_io);
2991 kvmppc_set_mmcr_hv(vcpu, 0, MMCR0_FC);
2992 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
2993 kvmppc_set_mmcr_hv(vcpu, 0, kvmppc_get_mmcr_hv(vcpu, 0) | MMCR0_PMCCEXT);
2994 kvmppc_set_mmcra_hv(vcpu, MMCRA_BHRB_DISABLE);
2997 kvmppc_set_ctrl_hv(vcpu, CTRL_RUNLATCH);
2998 /* default to host PVR, since we can't spoof it */
2999 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
3000 spin_lock_init(&vcpu->arch.vpa_update_lock);
3001 spin_lock_init(&vcpu->arch.tbacct_lock);
3002 vcpu->arch.busy_preempt = TB_NIL;
3003 __kvmppc_set_msr_hv(vcpu, MSR_ME);
3004 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
3007 * Set the default HFSCR for the guest from the host value.
3008 * This value is only used on POWER9 and later.
3009 * On >= POWER9, we want to virtualize the doorbell facility, so we
3010 * don't set the HFSCR_MSGP bit, and that causes those instructions
3011 * to trap and then we emulate them.
3013 kvmppc_set_hfscr_hv(vcpu, HFSCR_TAR | HFSCR_EBB | HFSCR_PM | HFSCR_BHRB |
3014 HFSCR_DSCR | HFSCR_VECVSX | HFSCR_FP);
3016 /* On POWER10 and later, allow prefixed instructions */
3017 if (cpu_has_feature(CPU_FTR_ARCH_31))
3018 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_PREFIX);
3020 if (cpu_has_feature(CPU_FTR_HVMODE)) {
3021 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) & mfspr(SPRN_HFSCR));
3023 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
3024 if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
3025 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_TM);
3028 if (cpu_has_feature(CPU_FTR_TM_COMP))
3029 vcpu->arch.hfscr |= HFSCR_TM;
3031 vcpu->arch.hfscr_permitted = kvmppc_get_hfscr_hv(vcpu);
3034 * PM, EBB, TM are demand-faulted so start with it clear.
3036 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) & ~(HFSCR_PM | HFSCR_EBB | HFSCR_TM));
3038 kvmppc_mmu_book3s_hv_init(vcpu);
3040 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
3042 init_waitqueue_head(&vcpu->arch.cpu_run);
3044 mutex_lock(&kvm->lock);
3047 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
3048 if (id >= (KVM_MAX_VCPUS * kvm->arch.emul_smt_mode)) {
3049 pr_devel("KVM: VCPU ID too high\n");
3050 core = KVM_MAX_VCORES;
3052 BUG_ON(kvm->arch.smt_mode != 1);
3053 core = kvmppc_pack_vcpu_id(kvm, id);
3056 core = id / kvm->arch.smt_mode;
3058 if (core < KVM_MAX_VCORES) {
3059 vcore = kvm->arch.vcores[core];
3060 if (vcore && cpu_has_feature(CPU_FTR_ARCH_300)) {
3061 pr_devel("KVM: collision on id %u", id);
3063 } else if (!vcore) {
3065 * Take mmu_setup_lock for mutual exclusion
3066 * with kvmppc_update_lpcr().
3069 vcore = kvmppc_vcore_create(kvm,
3070 id & ~(kvm->arch.smt_mode - 1));
3071 mutex_lock(&kvm->arch.mmu_setup_lock);
3072 kvm->arch.vcores[core] = vcore;
3073 kvm->arch.online_vcores++;
3074 mutex_unlock(&kvm->arch.mmu_setup_lock);
3077 mutex_unlock(&kvm->lock);
3082 spin_lock(&vcore->lock);
3083 ++vcore->num_threads;
3084 spin_unlock(&vcore->lock);
3085 vcpu->arch.vcore = vcore;
3086 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
3087 vcpu->arch.thread_cpu = -1;
3088 vcpu->arch.prev_cpu = -1;
3090 vcpu->arch.cpu_type = KVM_CPU_3S_64;
3091 kvmppc_sanity_check(vcpu);
3096 static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode,
3097 unsigned long flags)
3104 if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode))
3106 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
3108 * On POWER8 (or POWER7), the threading mode is "strict",
3109 * so we pack smt_mode vcpus per vcore.
3111 if (smt_mode > threads_per_subcore)
3115 * On POWER9, the threading mode is "loose",
3116 * so each vcpu gets its own vcore.
3121 mutex_lock(&kvm->lock);
3123 if (!kvm->arch.online_vcores) {
3124 kvm->arch.smt_mode = smt_mode;
3125 kvm->arch.emul_smt_mode = esmt;
3128 mutex_unlock(&kvm->lock);
3133 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
3135 if (vpa->pinned_addr)
3136 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
3140 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
3142 spin_lock(&vcpu->arch.vpa_update_lock);
3143 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
3144 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
3145 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
3146 spin_unlock(&vcpu->arch.vpa_update_lock);
3147 if (kvmhv_is_nestedv2())
3148 kvmhv_nestedv2_vcpu_free(vcpu, &vcpu->arch.nestedv2_io);
3151 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
3153 /* Indicate we want to get back into the guest */
3157 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
3159 unsigned long dec_nsec, now;
3162 if (now > kvmppc_dec_expires_host_tb(vcpu)) {
3163 /* decrementer has already gone negative */
3164 kvmppc_core_queue_dec(vcpu);
3165 kvmppc_core_prepare_to_enter(vcpu);
3168 dec_nsec = tb_to_ns(kvmppc_dec_expires_host_tb(vcpu) - now);
3169 hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
3170 vcpu->arch.timer_running = 1;
3173 extern int __kvmppc_vcore_entry(void);
3175 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
3176 struct kvm_vcpu *vcpu, u64 tb)
3180 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
3182 spin_lock_irq(&vcpu->arch.tbacct_lock);
3184 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
3185 vcpu->arch.stolen_logged;
3186 vcpu->arch.busy_preempt = now;
3187 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
3188 spin_unlock_irq(&vcpu->arch.tbacct_lock);
3190 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
3193 static int kvmppc_grab_hwthread(int cpu)
3195 struct paca_struct *tpaca;
3196 long timeout = 10000;
3198 tpaca = paca_ptrs[cpu];
3200 /* Ensure the thread won't go into the kernel if it wakes */
3201 tpaca->kvm_hstate.kvm_vcpu = NULL;
3202 tpaca->kvm_hstate.kvm_vcore = NULL;
3203 tpaca->kvm_hstate.napping = 0;
3205 tpaca->kvm_hstate.hwthread_req = 1;
3208 * If the thread is already executing in the kernel (e.g. handling
3209 * a stray interrupt), wait for it to get back to nap mode.
3210 * The smp_mb() is to ensure that our setting of hwthread_req
3211 * is visible before we look at hwthread_state, so if this
3212 * races with the code at system_reset_pSeries and the thread
3213 * misses our setting of hwthread_req, we are sure to see its
3214 * setting of hwthread_state, and vice versa.
3217 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
3218 if (--timeout <= 0) {
3219 pr_err("KVM: couldn't grab cpu %d\n", cpu);
3227 static void kvmppc_release_hwthread(int cpu)
3229 struct paca_struct *tpaca;
3231 tpaca = paca_ptrs[cpu];
3232 tpaca->kvm_hstate.hwthread_req = 0;
3233 tpaca->kvm_hstate.kvm_vcpu = NULL;
3234 tpaca->kvm_hstate.kvm_vcore = NULL;
3235 tpaca->kvm_hstate.kvm_split_mode = NULL;
3238 static DEFINE_PER_CPU(struct kvm *, cpu_in_guest);
3240 static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
3242 struct kvm_nested_guest *nested = vcpu->arch.nested;
3243 cpumask_t *need_tlb_flush;
3247 need_tlb_flush = &nested->need_tlb_flush;
3249 need_tlb_flush = &kvm->arch.need_tlb_flush;
3251 cpu = cpu_first_tlb_thread_sibling(cpu);
3252 for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu);
3253 i += cpu_tlb_thread_sibling_step())
3254 cpumask_set_cpu(i, need_tlb_flush);
3257 * Make sure setting of bit in need_tlb_flush precedes testing of
3258 * cpu_in_guest. The matching barrier on the other side is hwsync
3259 * when switching to guest MMU mode, which happens between
3260 * cpu_in_guest being set to the guest kvm, and need_tlb_flush bit
3265 for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu);
3266 i += cpu_tlb_thread_sibling_step()) {
3267 struct kvm *running = *per_cpu_ptr(&cpu_in_guest, i);
3270 smp_call_function_single(i, do_nothing, NULL, 1);
3274 static void do_migrate_away_vcpu(void *arg)
3276 struct kvm_vcpu *vcpu = arg;
3277 struct kvm *kvm = vcpu->kvm;
3280 * If the guest has GTSE, it may execute tlbie, so do a eieio; tlbsync;
3281 * ptesync sequence on the old CPU before migrating to a new one, in
3282 * case we interrupted the guest between a tlbie ; eieio ;
3283 * tlbsync; ptesync sequence.
3285 * Otherwise, ptesync is sufficient for ordering tlbiel sequences.
3287 if (kvm->arch.lpcr & LPCR_GTSE)
3288 asm volatile("eieio; tlbsync; ptesync");
3290 asm volatile("ptesync");
3293 static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
3295 struct kvm_nested_guest *nested = vcpu->arch.nested;
3296 struct kvm *kvm = vcpu->kvm;
3299 if (!cpu_has_feature(CPU_FTR_HVMODE))
3303 prev_cpu = nested->prev_cpu[vcpu->arch.nested_vcpu_id];
3305 prev_cpu = vcpu->arch.prev_cpu;
3308 * With radix, the guest can do TLB invalidations itself,
3309 * and it could choose to use the local form (tlbiel) if
3310 * it is invalidating a translation that has only ever been
3311 * used on one vcpu. However, that doesn't mean it has
3312 * only ever been used on one physical cpu, since vcpus
3313 * can move around between pcpus. To cope with this, when
3314 * a vcpu moves from one pcpu to another, we need to tell
3315 * any vcpus running on the same core as this vcpu previously
3316 * ran to flush the TLB.
3318 if (prev_cpu != pcpu) {
3319 if (prev_cpu >= 0) {
3320 if (cpu_first_tlb_thread_sibling(prev_cpu) !=
3321 cpu_first_tlb_thread_sibling(pcpu))
3322 radix_flush_cpu(kvm, prev_cpu, vcpu);
3324 smp_call_function_single(prev_cpu,
3325 do_migrate_away_vcpu, vcpu, 1);
3328 nested->prev_cpu[vcpu->arch.nested_vcpu_id] = pcpu;
3330 vcpu->arch.prev_cpu = pcpu;
3334 static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
3337 struct paca_struct *tpaca;
3341 if (vcpu->arch.timer_running) {
3342 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
3343 vcpu->arch.timer_running = 0;
3345 cpu += vcpu->arch.ptid;
3346 vcpu->cpu = vc->pcpu;
3347 vcpu->arch.thread_cpu = cpu;
3349 tpaca = paca_ptrs[cpu];
3350 tpaca->kvm_hstate.kvm_vcpu = vcpu;
3351 tpaca->kvm_hstate.ptid = cpu - vc->pcpu;
3352 tpaca->kvm_hstate.fake_suspend = 0;
3353 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
3355 tpaca->kvm_hstate.kvm_vcore = vc;
3356 if (cpu != smp_processor_id())
3357 kvmppc_ipi_thread(cpu);
3360 static void kvmppc_wait_for_nap(int n_threads)
3362 int cpu = smp_processor_id();
3367 for (loops = 0; loops < 1000000; ++loops) {
3369 * Check if all threads are finished.
3370 * We set the vcore pointer when starting a thread
3371 * and the thread clears it when finished, so we look
3372 * for any threads that still have a non-NULL vcore ptr.
3374 for (i = 1; i < n_threads; ++i)
3375 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
3377 if (i == n_threads) {
3384 for (i = 1; i < n_threads; ++i)
3385 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
3386 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
3390 * Check that we are on thread 0 and that any other threads in
3391 * this core are off-line. Then grab the threads so they can't
3394 static int on_primary_thread(void)
3396 int cpu = smp_processor_id();
3399 /* Are we on a primary subcore? */
3400 if (cpu_thread_in_subcore(cpu))
3404 while (++thr < threads_per_subcore)
3405 if (cpu_online(cpu + thr))
3408 /* Grab all hw threads so they can't go into the kernel */
3409 for (thr = 1; thr < threads_per_subcore; ++thr) {
3410 if (kvmppc_grab_hwthread(cpu + thr)) {
3411 /* Couldn't grab one; let the others go */
3413 kvmppc_release_hwthread(cpu + thr);
3414 } while (--thr > 0);
3422 * A list of virtual cores for each physical CPU.
3423 * These are vcores that could run but their runner VCPU tasks are
3424 * (or may be) preempted.
3426 struct preempted_vcore_list {
3427 struct list_head list;
3431 static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
3433 static void init_vcore_lists(void)
3437 for_each_possible_cpu(cpu) {
3438 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
3439 spin_lock_init(&lp->lock);
3440 INIT_LIST_HEAD(&lp->list);
3444 static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
3446 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
3448 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
3450 vc->vcore_state = VCORE_PREEMPT;
3451 vc->pcpu = smp_processor_id();
3452 if (vc->num_threads < threads_per_vcore(vc->kvm)) {
3453 spin_lock(&lp->lock);
3454 list_add_tail(&vc->preempt_list, &lp->list);
3455 spin_unlock(&lp->lock);
3458 /* Start accumulating stolen time */
3459 kvmppc_core_start_stolen(vc, mftb());
3462 static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
3464 struct preempted_vcore_list *lp;
3466 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
3468 kvmppc_core_end_stolen(vc, mftb());
3469 if (!list_empty(&vc->preempt_list)) {
3470 lp = &per_cpu(preempted_vcores, vc->pcpu);
3471 spin_lock(&lp->lock);
3472 list_del_init(&vc->preempt_list);
3473 spin_unlock(&lp->lock);
3475 vc->vcore_state = VCORE_INACTIVE;
3479 * This stores information about the virtual cores currently
3480 * assigned to a physical core.
3484 int max_subcore_threads;
3486 int subcore_threads[MAX_SUBCORES];
3487 struct kvmppc_vcore *vc[MAX_SUBCORES];
3491 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
3492 * respectively in 2-way micro-threading (split-core) mode on POWER8.
3494 static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
3496 static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
3498 memset(cip, 0, sizeof(*cip));
3499 cip->n_subcores = 1;
3500 cip->max_subcore_threads = vc->num_threads;
3501 cip->total_threads = vc->num_threads;
3502 cip->subcore_threads[0] = vc->num_threads;
3506 static bool subcore_config_ok(int n_subcores, int n_threads)
3509 * POWER9 "SMT4" cores are permanently in what is effectively a 4-way
3510 * split-core mode, with one thread per subcore.
3512 if (cpu_has_feature(CPU_FTR_ARCH_300))
3513 return n_subcores <= 4 && n_threads == 1;
3515 /* On POWER8, can only dynamically split if unsplit to begin with */
3516 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
3518 if (n_subcores > MAX_SUBCORES)
3520 if (n_subcores > 1) {
3521 if (!(dynamic_mt_modes & 2))
3523 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
3527 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
3530 static void init_vcore_to_run(struct kvmppc_vcore *vc)
3532 vc->entry_exit_map = 0;
3534 vc->napping_threads = 0;
3535 vc->conferring_threads = 0;
3536 vc->tb_offset_applied = 0;
3539 static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
3541 int n_threads = vc->num_threads;
3544 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
3547 /* In one_vm_per_core mode, require all vcores to be from the same vm */
3548 if (one_vm_per_core && vc->kvm != cip->vc[0]->kvm)
3551 if (n_threads < cip->max_subcore_threads)
3552 n_threads = cip->max_subcore_threads;
3553 if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
3555 cip->max_subcore_threads = n_threads;
3557 sub = cip->n_subcores;
3559 cip->total_threads += vc->num_threads;
3560 cip->subcore_threads[sub] = vc->num_threads;
3562 init_vcore_to_run(vc);
3563 list_del_init(&vc->preempt_list);
3569 * Work out whether it is possible to piggyback the execution of
3570 * vcore *pvc onto the execution of the other vcores described in *cip.
3572 static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
3575 if (cip->total_threads + pvc->num_threads > target_threads)
3578 return can_dynamic_split(pvc, cip);
3581 static void prepare_threads(struct kvmppc_vcore *vc)
3584 struct kvm_vcpu *vcpu;
3586 for_each_runnable_thread(i, vcpu, vc) {
3587 if (signal_pending(vcpu->arch.run_task))
3588 vcpu->arch.ret = -EINTR;
3589 else if (vcpu->arch.vpa.update_pending ||
3590 vcpu->arch.slb_shadow.update_pending ||
3591 vcpu->arch.dtl.update_pending)
3592 vcpu->arch.ret = RESUME_GUEST;
3595 kvmppc_remove_runnable(vc, vcpu, mftb());
3596 wake_up(&vcpu->arch.cpu_run);
3600 static void collect_piggybacks(struct core_info *cip, int target_threads)
3602 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
3603 struct kvmppc_vcore *pvc, *vcnext;
3605 spin_lock(&lp->lock);
3606 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
3607 if (!spin_trylock(&pvc->lock))
3609 prepare_threads(pvc);
3610 if (!pvc->n_runnable || !pvc->kvm->arch.mmu_ready) {
3611 list_del_init(&pvc->preempt_list);
3612 if (pvc->runner == NULL) {
3613 pvc->vcore_state = VCORE_INACTIVE;
3614 kvmppc_core_end_stolen(pvc, mftb());
3616 spin_unlock(&pvc->lock);
3619 if (!can_piggyback(pvc, cip, target_threads)) {
3620 spin_unlock(&pvc->lock);
3623 kvmppc_core_end_stolen(pvc, mftb());
3624 pvc->vcore_state = VCORE_PIGGYBACK;
3625 if (cip->total_threads >= target_threads)
3628 spin_unlock(&lp->lock);
3631 static bool recheck_signals_and_mmu(struct core_info *cip)
3634 struct kvm_vcpu *vcpu;
3635 struct kvmppc_vcore *vc;
3637 for (sub = 0; sub < cip->n_subcores; ++sub) {
3639 if (!vc->kvm->arch.mmu_ready)
3641 for_each_runnable_thread(i, vcpu, vc)
3642 if (signal_pending(vcpu->arch.run_task))
3648 static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
3650 int still_running = 0, i;
3653 struct kvm_vcpu *vcpu;
3655 spin_lock(&vc->lock);
3657 for_each_runnable_thread(i, vcpu, vc) {
3659 * It's safe to unlock the vcore in the loop here, because
3660 * for_each_runnable_thread() is safe against removal of
3661 * the vcpu, and the vcore state is VCORE_EXITING here,
3662 * so any vcpus becoming runnable will have their arch.trap
3663 * set to zero and can't actually run in the guest.
3665 spin_unlock(&vc->lock);
3666 /* cancel pending dec exception if dec is positive */
3667 if (now < kvmppc_dec_expires_host_tb(vcpu) &&
3668 kvmppc_core_pending_dec(vcpu))
3669 kvmppc_core_dequeue_dec(vcpu);
3671 trace_kvm_guest_exit(vcpu);
3674 if (vcpu->arch.trap)
3675 ret = kvmppc_handle_exit_hv(vcpu,
3676 vcpu->arch.run_task);
3678 vcpu->arch.ret = ret;
3679 vcpu->arch.trap = 0;
3681 spin_lock(&vc->lock);
3682 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
3683 if (vcpu->arch.pending_exceptions)
3684 kvmppc_core_prepare_to_enter(vcpu);
3685 if (vcpu->arch.ceded)
3686 kvmppc_set_timer(vcpu);
3690 kvmppc_remove_runnable(vc, vcpu, mftb());
3691 wake_up(&vcpu->arch.cpu_run);
3695 if (still_running > 0) {
3696 kvmppc_vcore_preempt(vc);
3697 } else if (vc->runner) {
3698 vc->vcore_state = VCORE_PREEMPT;
3699 kvmppc_core_start_stolen(vc, mftb());
3701 vc->vcore_state = VCORE_INACTIVE;
3703 if (vc->n_runnable > 0 && vc->runner == NULL) {
3704 /* make sure there's a candidate runner awake */
3706 vcpu = next_runnable_thread(vc, &i);
3707 wake_up(&vcpu->arch.cpu_run);
3710 spin_unlock(&vc->lock);
3714 * Clear core from the list of active host cores as we are about to
3715 * enter the guest. Only do this if it is the primary thread of the
3716 * core (not if a subcore) that is entering the guest.
3718 static inline int kvmppc_clear_host_core(unsigned int cpu)
3722 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
3725 * Memory barrier can be omitted here as we will do a smp_wmb()
3726 * later in kvmppc_start_thread and we need ensure that state is
3727 * visible to other CPUs only after we enter guest.
3729 core = cpu >> threads_shift;
3730 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
3735 * Advertise this core as an active host core since we exited the guest
3736 * Only need to do this if it is the primary thread of the core that is
3739 static inline int kvmppc_set_host_core(unsigned int cpu)
3743 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
3747 * Memory barrier can be omitted here because we do a spin_unlock
3748 * immediately after this which provides the memory barrier.
3750 core = cpu >> threads_shift;
3751 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
3755 static void set_irq_happened(int trap)
3758 case BOOK3S_INTERRUPT_EXTERNAL:
3759 local_paca->irq_happened |= PACA_IRQ_EE;
3761 case BOOK3S_INTERRUPT_H_DOORBELL:
3762 local_paca->irq_happened |= PACA_IRQ_DBELL;
3764 case BOOK3S_INTERRUPT_HMI:
3765 local_paca->irq_happened |= PACA_IRQ_HMI;
3767 case BOOK3S_INTERRUPT_SYSTEM_RESET:
3768 replay_system_reset();
3774 * Run a set of guest threads on a physical core.
3775 * Called with vc->lock held.
3777 static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
3779 struct kvm_vcpu *vcpu;
3782 struct core_info core_info;
3783 struct kvmppc_vcore *pvc;
3784 struct kvm_split_mode split_info, *sip;
3785 int split, subcore_size, active;
3788 unsigned long cmd_bit, stat_bit;
3791 int controlled_threads;
3795 if (WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)))
3799 * Remove from the list any threads that have a signal pending
3800 * or need a VPA update done
3802 prepare_threads(vc);
3804 /* if the runner is no longer runnable, let the caller pick a new one */
3805 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
3811 init_vcore_to_run(vc);
3812 vc->preempt_tb = TB_NIL;
3815 * Number of threads that we will be controlling: the same as
3816 * the number of threads per subcore, except on POWER9,
3817 * where it's 1 because the threads are (mostly) independent.
3819 controlled_threads = threads_per_vcore(vc->kvm);
3822 * Make sure we are running on primary threads, and that secondary
3823 * threads are offline. Also check if the number of threads in this
3824 * guest are greater than the current system threads per guest.
3826 if ((controlled_threads > 1) &&
3827 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
3828 for_each_runnable_thread(i, vcpu, vc) {
3829 vcpu->arch.ret = -EBUSY;
3830 kvmppc_remove_runnable(vc, vcpu, mftb());
3831 wake_up(&vcpu->arch.cpu_run);
3837 * See if we could run any other vcores on the physical core
3838 * along with this one.
3840 init_core_info(&core_info, vc);
3841 pcpu = smp_processor_id();
3842 target_threads = controlled_threads;
3843 if (target_smt_mode && target_smt_mode < target_threads)
3844 target_threads = target_smt_mode;
3845 if (vc->num_threads < target_threads)
3846 collect_piggybacks(&core_info, target_threads);
3849 * Hard-disable interrupts, and check resched flag and signals.
3850 * If we need to reschedule or deliver a signal, clean up
3851 * and return without going into the guest(s).
3852 * If the mmu_ready flag has been cleared, don't go into the
3853 * guest because that means a HPT resize operation is in progress.
3855 local_irq_disable();
3857 if (lazy_irq_pending() || need_resched() ||
3858 recheck_signals_and_mmu(&core_info)) {
3860 vc->vcore_state = VCORE_INACTIVE;
3861 /* Unlock all except the primary vcore */
3862 for (sub = 1; sub < core_info.n_subcores; ++sub) {
3863 pvc = core_info.vc[sub];
3864 /* Put back on to the preempted vcores list */
3865 kvmppc_vcore_preempt(pvc);
3866 spin_unlock(&pvc->lock);
3868 for (i = 0; i < controlled_threads; ++i)
3869 kvmppc_release_hwthread(pcpu + i);
3873 kvmppc_clear_host_core(pcpu);
3875 /* Decide on micro-threading (split-core) mode */
3876 subcore_size = threads_per_subcore;
3877 cmd_bit = stat_bit = 0;
3878 split = core_info.n_subcores;
3880 is_power8 = cpu_has_feature(CPU_FTR_ARCH_207S);
3884 memset(&split_info, 0, sizeof(split_info));
3885 for (sub = 0; sub < core_info.n_subcores; ++sub)
3886 split_info.vc[sub] = core_info.vc[sub];
3889 if (split == 2 && (dynamic_mt_modes & 2)) {
3890 cmd_bit = HID0_POWER8_1TO2LPAR;
3891 stat_bit = HID0_POWER8_2LPARMODE;
3894 cmd_bit = HID0_POWER8_1TO4LPAR;
3895 stat_bit = HID0_POWER8_4LPARMODE;
3897 subcore_size = MAX_SMT_THREADS / split;
3898 split_info.rpr = mfspr(SPRN_RPR);
3899 split_info.pmmar = mfspr(SPRN_PMMAR);
3900 split_info.ldbar = mfspr(SPRN_LDBAR);
3901 split_info.subcore_size = subcore_size;
3903 split_info.subcore_size = 1;
3906 /* order writes to split_info before kvm_split_mode pointer */
3910 for (thr = 0; thr < controlled_threads; ++thr) {
3911 struct paca_struct *paca = paca_ptrs[pcpu + thr];
3913 paca->kvm_hstate.napping = 0;
3914 paca->kvm_hstate.kvm_split_mode = sip;
3917 /* Initiate micro-threading (split-core) on POWER8 if required */
3919 unsigned long hid0 = mfspr(SPRN_HID0);
3921 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
3923 mtspr(SPRN_HID0, hid0);
3926 hid0 = mfspr(SPRN_HID0);
3927 if (hid0 & stat_bit)
3934 * On POWER8, set RWMR register.
3935 * Since it only affects PURR and SPURR, it doesn't affect
3936 * the host, so we don't save/restore the host value.
3939 unsigned long rwmr_val = RWMR_RPA_P8_8THREAD;
3940 int n_online = atomic_read(&vc->online_count);
3943 * Use the 8-thread value if we're doing split-core
3944 * or if the vcore's online count looks bogus.
3946 if (split == 1 && threads_per_subcore == MAX_SMT_THREADS &&
3947 n_online >= 1 && n_online <= MAX_SMT_THREADS)
3948 rwmr_val = p8_rwmr_values[n_online];
3949 mtspr(SPRN_RWMR, rwmr_val);
3952 /* Start all the threads */
3954 for (sub = 0; sub < core_info.n_subcores; ++sub) {
3955 thr = is_power8 ? subcore_thread_map[sub] : sub;
3958 pvc = core_info.vc[sub];
3959 pvc->pcpu = pcpu + thr;
3960 for_each_runnable_thread(i, vcpu, pvc) {
3962 * XXX: is kvmppc_start_thread called too late here?
3963 * It updates vcpu->cpu and vcpu->arch.thread_cpu
3964 * which are used by kvmppc_fast_vcpu_kick_hv(), but
3965 * kick is called after new exceptions become available
3966 * and exceptions are checked earlier than here, by
3967 * kvmppc_core_prepare_to_enter.
3969 kvmppc_start_thread(vcpu, pvc);
3970 kvmppc_update_vpa_dispatch(vcpu, pvc);
3971 trace_kvm_guest_enter(vcpu);
3972 if (!vcpu->arch.ptid)
3974 active |= 1 << (thr + vcpu->arch.ptid);
3977 * We need to start the first thread of each subcore
3978 * even if it doesn't have a vcpu.
3981 kvmppc_start_thread(NULL, pvc);
3985 * Ensure that split_info.do_nap is set after setting
3986 * the vcore pointer in the PACA of the secondaries.
3991 * When doing micro-threading, poke the inactive threads as well.
3992 * This gets them to the nap instruction after kvm_do_nap,
3993 * which reduces the time taken to unsplit later.
3996 split_info.do_nap = 1; /* ask secondaries to nap when done */
3997 for (thr = 1; thr < threads_per_subcore; ++thr)
3998 if (!(active & (1 << thr)))
3999 kvmppc_ipi_thread(pcpu + thr);
4002 vc->vcore_state = VCORE_RUNNING;
4005 trace_kvmppc_run_core(vc, 0);
4007 for (sub = 0; sub < core_info.n_subcores; ++sub)
4008 spin_unlock(&core_info.vc[sub]->lock);
4010 guest_timing_enter_irqoff();
4012 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
4014 guest_state_enter_irqoff();
4015 this_cpu_disable_ftrace();
4017 trap = __kvmppc_vcore_entry();
4019 this_cpu_enable_ftrace();
4020 guest_state_exit_irqoff();
4022 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
4024 set_irq_happened(trap);
4026 spin_lock(&vc->lock);
4027 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
4028 vc->vcore_state = VCORE_EXITING;
4030 /* wait for secondary threads to finish writing their state to memory */
4031 kvmppc_wait_for_nap(controlled_threads);
4033 /* Return to whole-core mode if we split the core earlier */
4035 unsigned long hid0 = mfspr(SPRN_HID0);
4037 hid0 &= ~HID0_POWER8_DYNLPARDIS;
4038 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
4040 mtspr(SPRN_HID0, hid0);
4043 hid0 = mfspr(SPRN_HID0);
4044 if (!(hid0 & stat_bit))
4048 split_info.do_nap = 0;
4051 kvmppc_set_host_core(pcpu);
4053 if (!vtime_accounting_enabled_this_cpu()) {
4056 * Service IRQs here before guest_timing_exit_irqoff() so any
4057 * ticks that occurred while running the guest are accounted to
4058 * the guest. If vtime accounting is enabled, accounting uses
4059 * TB rather than ticks, so it can be done without enabling
4060 * interrupts here, which has the problem that it accounts
4061 * interrupt processing overhead to the host.
4063 local_irq_disable();
4065 guest_timing_exit_irqoff();
4069 /* Let secondaries go back to the offline loop */
4070 for (i = 0; i < controlled_threads; ++i) {
4071 kvmppc_release_hwthread(pcpu + i);
4072 if (sip && sip->napped[i])
4073 kvmppc_ipi_thread(pcpu + i);
4076 spin_unlock(&vc->lock);
4078 /* make sure updates to secondary vcpu structs are visible now */
4083 for (sub = 0; sub < core_info.n_subcores; ++sub) {
4084 pvc = core_info.vc[sub];
4085 post_guest_process(pvc, pvc == vc);
4088 spin_lock(&vc->lock);
4091 vc->vcore_state = VCORE_INACTIVE;
4092 trace_kvmppc_run_core(vc, 1);
4095 static inline bool hcall_is_xics(unsigned long req)
4097 return req == H_EOI || req == H_CPPR || req == H_IPI ||
4098 req == H_IPOLL || req == H_XIRR || req == H_XIRR_X;
4101 static void vcpu_vpa_increment_dispatch(struct kvm_vcpu *vcpu)
4103 struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
4105 u32 yield_count = be32_to_cpu(lp->yield_count) + 1;
4106 lp->yield_count = cpu_to_be32(yield_count);
4107 vcpu->arch.vpa.dirty = 1;
4111 /* Helper functions for reading L2's stats from L1's VPA */
4112 #ifdef CONFIG_PPC_PSERIES
4113 static DEFINE_PER_CPU(u64, l1_to_l2_cs);
4114 static DEFINE_PER_CPU(u64, l2_to_l1_cs);
4115 static DEFINE_PER_CPU(u64, l2_runtime_agg);
4117 int kvmhv_get_l2_counters_status(void)
4119 return firmware_has_feature(FW_FEATURE_LPAR) &&
4120 get_lppaca()->l2_counters_enable;
4123 void kvmhv_set_l2_counters_status(int cpu, bool status)
4125 if (!firmware_has_feature(FW_FEATURE_LPAR))
4128 lppaca_of(cpu).l2_counters_enable = 1;
4130 lppaca_of(cpu).l2_counters_enable = 0;
4132 EXPORT_SYMBOL(kvmhv_set_l2_counters_status);
4134 int kvmhv_counters_tracepoint_regfunc(void)
4138 for_each_present_cpu(cpu) {
4139 kvmhv_set_l2_counters_status(cpu, true);
4144 void kvmhv_counters_tracepoint_unregfunc(void)
4148 for_each_present_cpu(cpu) {
4149 kvmhv_set_l2_counters_status(cpu, false);
4153 static void do_trace_nested_cs_time(struct kvm_vcpu *vcpu)
4155 struct lppaca *lp = get_lppaca();
4156 u64 l1_to_l2_ns, l2_to_l1_ns, l2_runtime_ns;
4157 u64 *l1_to_l2_cs_ptr = this_cpu_ptr(&l1_to_l2_cs);
4158 u64 *l2_to_l1_cs_ptr = this_cpu_ptr(&l2_to_l1_cs);
4159 u64 *l2_runtime_agg_ptr = this_cpu_ptr(&l2_runtime_agg);
4161 l1_to_l2_ns = tb_to_ns(be64_to_cpu(lp->l1_to_l2_cs_tb));
4162 l2_to_l1_ns = tb_to_ns(be64_to_cpu(lp->l2_to_l1_cs_tb));
4163 l2_runtime_ns = tb_to_ns(be64_to_cpu(lp->l2_runtime_tb));
4164 trace_kvmppc_vcpu_stats(vcpu, l1_to_l2_ns - *l1_to_l2_cs_ptr,
4165 l2_to_l1_ns - *l2_to_l1_cs_ptr,
4166 l2_runtime_ns - *l2_runtime_agg_ptr);
4167 *l1_to_l2_cs_ptr = l1_to_l2_ns;
4168 *l2_to_l1_cs_ptr = l2_to_l1_ns;
4169 *l2_runtime_agg_ptr = l2_runtime_ns;
4170 vcpu->arch.l1_to_l2_cs = l1_to_l2_ns;
4171 vcpu->arch.l2_to_l1_cs = l2_to_l1_ns;
4172 vcpu->arch.l2_runtime_agg = l2_runtime_ns;
4175 u64 kvmhv_get_l1_to_l2_cs_time(void)
4177 return tb_to_ns(be64_to_cpu(get_lppaca()->l1_to_l2_cs_tb));
4179 EXPORT_SYMBOL(kvmhv_get_l1_to_l2_cs_time);
4181 u64 kvmhv_get_l2_to_l1_cs_time(void)
4183 return tb_to_ns(be64_to_cpu(get_lppaca()->l2_to_l1_cs_tb));
4185 EXPORT_SYMBOL(kvmhv_get_l2_to_l1_cs_time);
4187 u64 kvmhv_get_l2_runtime_agg(void)
4189 return tb_to_ns(be64_to_cpu(get_lppaca()->l2_runtime_tb));
4191 EXPORT_SYMBOL(kvmhv_get_l2_runtime_agg);
4193 u64 kvmhv_get_l1_to_l2_cs_time_vcpu(void)
4195 struct kvm_vcpu *vcpu;
4196 struct kvm_vcpu_arch *arch;
4198 vcpu = local_paca->kvm_hstate.kvm_vcpu;
4201 return arch->l1_to_l2_cs;
4206 EXPORT_SYMBOL(kvmhv_get_l1_to_l2_cs_time_vcpu);
4208 u64 kvmhv_get_l2_to_l1_cs_time_vcpu(void)
4210 struct kvm_vcpu *vcpu;
4211 struct kvm_vcpu_arch *arch;
4213 vcpu = local_paca->kvm_hstate.kvm_vcpu;
4216 return arch->l2_to_l1_cs;
4221 EXPORT_SYMBOL(kvmhv_get_l2_to_l1_cs_time_vcpu);
4223 u64 kvmhv_get_l2_runtime_agg_vcpu(void)
4225 struct kvm_vcpu *vcpu;
4226 struct kvm_vcpu_arch *arch;
4228 vcpu = local_paca->kvm_hstate.kvm_vcpu;
4231 return arch->l2_runtime_agg;
4236 EXPORT_SYMBOL(kvmhv_get_l2_runtime_agg_vcpu);
4239 int kvmhv_get_l2_counters_status(void)
4244 static void do_trace_nested_cs_time(struct kvm_vcpu *vcpu)
4249 static int kvmhv_vcpu_entry_nestedv2(struct kvm_vcpu *vcpu, u64 time_limit,
4250 unsigned long lpcr, u64 *tb)
4252 struct kvmhv_nestedv2_io *io;
4253 unsigned long msr, i;
4257 if (vcpu->arch.doorbell_request) {
4258 vcpu->arch.doorbell_request = 0;
4259 kvmppc_set_dpdes(vcpu, 1);
4262 io = &vcpu->arch.nestedv2_io;
4265 kvmppc_msr_hard_disable_set_facilities(vcpu, msr);
4266 if (lazy_irq_pending())
4269 rc = kvmhv_nestedv2_flush_vcpu(vcpu, time_limit);
4273 kvmppc_gse_put_u64(io->vcpu_run_input, KVMPPC_GSID_LPCR, lpcr);
4275 accumulate_time(vcpu, &vcpu->arch.in_guest);
4276 rc = plpar_guest_run_vcpu(0, vcpu->kvm->arch.lpid, vcpu->vcpu_id,
4279 if (rc != H_SUCCESS) {
4280 pr_err("KVM Guest Run VCPU hcall failed\n");
4281 if (rc == H_INVALID_ELEMENT_ID)
4282 pr_err("KVM: Guest Run VCPU invalid element id at %ld\n", i);
4283 else if (rc == H_INVALID_ELEMENT_SIZE)
4284 pr_err("KVM: Guest Run VCPU invalid element size at %ld\n", i);
4285 else if (rc == H_INVALID_ELEMENT_VALUE)
4286 pr_err("KVM: Guest Run VCPU invalid element value at %ld\n", i);
4289 accumulate_time(vcpu, &vcpu->arch.guest_exit);
4292 kvmppc_gsm_reset(io->vcpu_message);
4293 kvmppc_gsm_reset(io->vcore_message);
4294 kvmppc_gsbm_zero(&io->valids);
4296 rc = kvmhv_nestedv2_parse_output(vcpu);
4300 timer_rearm_host_dec(*tb);
4302 /* Record context switch and guest_run_time data */
4303 if (kvmhv_get_l2_counters_status())
4304 do_trace_nested_cs_time(vcpu);
4309 /* call our hypervisor to load up HV regs and go */
4310 static int kvmhv_vcpu_entry_p9_nested(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcr, u64 *tb)
4312 unsigned long host_psscr;
4314 struct hv_guest_state hvregs;
4315 struct p9_host_os_sprs host_os_sprs;
4321 save_p9_host_os_sprs(&host_os_sprs);
4324 * We need to save and restore the guest visible part of the
4325 * psscr (i.e. using SPRN_PSSCR_PR) since the hypervisor
4326 * doesn't do this for us. Note only required if pseries since
4327 * this is done in kvmhv_vcpu_entry_p9() below otherwise.
4329 host_psscr = mfspr(SPRN_PSSCR_PR);
4331 kvmppc_msr_hard_disable_set_facilities(vcpu, msr);
4332 if (lazy_irq_pending())
4335 if (unlikely(load_vcpu_state(vcpu, &host_os_sprs)))
4336 msr = mfmsr(); /* TM restore can update msr */
4338 if (vcpu->arch.psscr != host_psscr)
4339 mtspr(SPRN_PSSCR_PR, vcpu->arch.psscr);
4341 kvmhv_save_hv_regs(vcpu, &hvregs);
4344 vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
4345 hvregs.version = HV_GUEST_STATE_VERSION;
4346 if (vcpu->arch.nested) {
4347 hvregs.lpid = vcpu->arch.nested->shadow_lpid;
4348 hvregs.vcpu_token = vcpu->arch.nested_vcpu_id;
4350 hvregs.lpid = vcpu->kvm->arch.lpid;
4351 hvregs.vcpu_token = vcpu->vcpu_id;
4353 hvregs.hdec_expiry = time_limit;
4356 * hvregs has the doorbell status, so zero it here which
4357 * enables us to receive doorbells when H_ENTER_NESTED is
4358 * in progress for this vCPU
4361 if (vcpu->arch.doorbell_request)
4362 vcpu->arch.doorbell_request = 0;
4365 * When setting DEC, we must always deal with irq_work_raise
4366 * via NMI vs setting DEC. The problem occurs right as we
4367 * switch into guest mode if a NMI hits and sets pending work
4368 * and sets DEC, then that will apply to the guest and not
4369 * bring us back to the host.
4371 * irq_work_raise could check a flag (or possibly LPCR[HDICE]
4372 * for example) and set HDEC to 1? That wouldn't solve the
4373 * nested hv case which needs to abort the hcall or zero the
4376 * XXX: Another day's problem.
4378 mtspr(SPRN_DEC, kvmppc_dec_expires_host_tb(vcpu) - *tb);
4380 mtspr(SPRN_DAR, vcpu->arch.shregs.dar);
4381 mtspr(SPRN_DSISR, vcpu->arch.shregs.dsisr);
4382 switch_pmu_to_guest(vcpu, &host_os_sprs);
4383 accumulate_time(vcpu, &vcpu->arch.in_guest);
4384 trap = plpar_hcall_norets(H_ENTER_NESTED, __pa(&hvregs),
4385 __pa(&vcpu->arch.regs));
4386 accumulate_time(vcpu, &vcpu->arch.guest_exit);
4387 kvmhv_restore_hv_return_state(vcpu, &hvregs);
4388 switch_pmu_to_host(vcpu, &host_os_sprs);
4389 vcpu->arch.shregs.msr = vcpu->arch.regs.msr;
4390 vcpu->arch.shregs.dar = mfspr(SPRN_DAR);
4391 vcpu->arch.shregs.dsisr = mfspr(SPRN_DSISR);
4392 vcpu->arch.psscr = mfspr(SPRN_PSSCR_PR);
4394 store_vcpu_state(vcpu);
4396 dec = mfspr(SPRN_DEC);
4397 if (!(lpcr & LPCR_LD)) /* Sign extend if not using large decrementer */
4400 vcpu->arch.dec_expires = dec + (*tb + kvmppc_get_tb_offset(vcpu));
4402 timer_rearm_host_dec(*tb);
4404 restore_p9_host_os_sprs(vcpu, &host_os_sprs);
4405 if (vcpu->arch.psscr != host_psscr)
4406 mtspr(SPRN_PSSCR_PR, host_psscr);
4412 * Guest entry for POWER9 and later CPUs.
4414 static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,
4415 unsigned long lpcr, u64 *tb)
4417 struct kvm *kvm = vcpu->kvm;
4418 struct kvm_nested_guest *nested = vcpu->arch.nested;
4422 next_timer = timer_get_next_tb();
4423 if (*tb >= next_timer)
4424 return BOOK3S_INTERRUPT_HV_DECREMENTER;
4425 if (next_timer < time_limit)
4426 time_limit = next_timer;
4427 else if (*tb >= time_limit) /* nested time limit */
4428 return BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER;
4430 vcpu->arch.ceded = 0;
4432 vcpu_vpa_increment_dispatch(vcpu);
4434 if (kvmhv_on_pseries()) {
4435 if (kvmhv_is_nestedv1())
4436 trap = kvmhv_vcpu_entry_p9_nested(vcpu, time_limit, lpcr, tb);
4438 trap = kvmhv_vcpu_entry_nestedv2(vcpu, time_limit, lpcr, tb);
4440 /* H_CEDE has to be handled now, not later */
4441 if (trap == BOOK3S_INTERRUPT_SYSCALL && !nested &&
4442 kvmppc_get_gpr(vcpu, 3) == H_CEDE) {
4444 kvmppc_set_gpr(vcpu, 3, 0);
4448 } else if (nested) {
4449 __this_cpu_write(cpu_in_guest, kvm);
4450 trap = kvmhv_vcpu_entry_p9(vcpu, time_limit, lpcr, tb);
4451 __this_cpu_write(cpu_in_guest, NULL);
4454 kvmppc_xive_push_vcpu(vcpu);
4456 __this_cpu_write(cpu_in_guest, kvm);
4457 trap = kvmhv_vcpu_entry_p9(vcpu, time_limit, lpcr, tb);
4458 __this_cpu_write(cpu_in_guest, NULL);
4460 if (trap == BOOK3S_INTERRUPT_SYSCALL &&
4461 !(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) {
4462 unsigned long req = kvmppc_get_gpr(vcpu, 3);
4465 * XIVE rearm and XICS hcalls must be handled
4466 * before xive context is pulled (is this
4469 if (req == H_CEDE) {
4470 /* H_CEDE has to be handled now */
4472 if (!kvmppc_xive_rearm_escalation(vcpu)) {
4474 * Pending escalation so abort
4477 vcpu->arch.ceded = 0;
4479 kvmppc_set_gpr(vcpu, 3, 0);
4482 } else if (req == H_ENTER_NESTED) {
4484 * L2 should not run with the L1
4485 * context so rearm and pull it.
4487 if (!kvmppc_xive_rearm_escalation(vcpu)) {
4489 * Pending escalation so abort
4492 kvmppc_set_gpr(vcpu, 3, 0);
4496 } else if (hcall_is_xics(req)) {
4499 ret = kvmppc_xive_xics_hcall(vcpu, req);
4500 if (ret != H_TOO_HARD) {
4501 kvmppc_set_gpr(vcpu, 3, ret);
4506 kvmppc_xive_pull_vcpu(vcpu);
4508 if (kvm_is_radix(kvm))
4509 vcpu->arch.slb_max = 0;
4512 vcpu_vpa_increment_dispatch(vcpu);
4518 * Wait for some other vcpu thread to execute us, and
4519 * wake us up when we need to handle something in the host.
4521 static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
4522 struct kvm_vcpu *vcpu, int wait_state)
4526 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
4527 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
4528 spin_unlock(&vc->lock);
4530 spin_lock(&vc->lock);
4532 finish_wait(&vcpu->arch.cpu_run, &wait);
4535 static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
4537 if (!halt_poll_ns_grow)
4540 vc->halt_poll_ns *= halt_poll_ns_grow;
4541 if (vc->halt_poll_ns < halt_poll_ns_grow_start)
4542 vc->halt_poll_ns = halt_poll_ns_grow_start;
4545 static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
4547 if (halt_poll_ns_shrink == 0)
4548 vc->halt_poll_ns = 0;
4550 vc->halt_poll_ns /= halt_poll_ns_shrink;
4553 #ifdef CONFIG_KVM_XICS
4554 static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
4556 if (!xics_on_xive())
4558 return vcpu->arch.irq_pending || vcpu->arch.xive_saved_state.pipr <
4559 vcpu->arch.xive_saved_state.cppr;
4562 static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
4566 #endif /* CONFIG_KVM_XICS */
4568 static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu)
4570 if (vcpu->arch.pending_exceptions || vcpu->arch.prodded ||
4571 kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu))
4577 static bool kvmppc_vcpu_check_block(struct kvm_vcpu *vcpu)
4579 if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu))
4585 * Check to see if any of the runnable vcpus on the vcore have pending
4586 * exceptions or are no longer ceded
4588 static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
4590 struct kvm_vcpu *vcpu;
4593 for_each_runnable_thread(i, vcpu, vc) {
4594 if (kvmppc_vcpu_check_block(vcpu))
4602 * All the vcpus in this vcore are idle, so wait for a decrementer
4603 * or external interrupt to one of the vcpus. vc->lock is held.
4605 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
4607 ktime_t cur, start_poll, start_wait;
4611 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
4613 /* Poll for pending exceptions and ceded state */
4614 cur = start_poll = ktime_get();
4615 if (vc->halt_poll_ns) {
4616 ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
4617 ++vc->runner->stat.generic.halt_attempted_poll;
4619 vc->vcore_state = VCORE_POLLING;
4620 spin_unlock(&vc->lock);
4623 if (kvmppc_vcore_check_block(vc)) {
4628 } while (kvm_vcpu_can_poll(cur, stop));
4630 spin_lock(&vc->lock);
4631 vc->vcore_state = VCORE_INACTIVE;
4634 ++vc->runner->stat.generic.halt_successful_poll;
4639 prepare_to_rcuwait(&vc->wait);
4640 set_current_state(TASK_INTERRUPTIBLE);
4641 if (kvmppc_vcore_check_block(vc)) {
4642 finish_rcuwait(&vc->wait);
4644 /* If we polled, count this as a successful poll */
4645 if (vc->halt_poll_ns)
4646 ++vc->runner->stat.generic.halt_successful_poll;
4650 start_wait = ktime_get();
4652 vc->vcore_state = VCORE_SLEEPING;
4653 trace_kvmppc_vcore_blocked(vc->runner, 0);
4654 spin_unlock(&vc->lock);
4656 finish_rcuwait(&vc->wait);
4657 spin_lock(&vc->lock);
4658 vc->vcore_state = VCORE_INACTIVE;
4659 trace_kvmppc_vcore_blocked(vc->runner, 1);
4660 ++vc->runner->stat.halt_successful_wait;
4665 block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
4667 /* Attribute wait time */
4669 vc->runner->stat.generic.halt_wait_ns +=
4670 ktime_to_ns(cur) - ktime_to_ns(start_wait);
4671 KVM_STATS_LOG_HIST_UPDATE(
4672 vc->runner->stat.generic.halt_wait_hist,
4673 ktime_to_ns(cur) - ktime_to_ns(start_wait));
4674 /* Attribute failed poll time */
4675 if (vc->halt_poll_ns) {
4676 vc->runner->stat.generic.halt_poll_fail_ns +=
4677 ktime_to_ns(start_wait) -
4678 ktime_to_ns(start_poll);
4679 KVM_STATS_LOG_HIST_UPDATE(
4680 vc->runner->stat.generic.halt_poll_fail_hist,
4681 ktime_to_ns(start_wait) -
4682 ktime_to_ns(start_poll));
4685 /* Attribute successful poll time */
4686 if (vc->halt_poll_ns) {
4687 vc->runner->stat.generic.halt_poll_success_ns +=
4689 ktime_to_ns(start_poll);
4690 KVM_STATS_LOG_HIST_UPDATE(
4691 vc->runner->stat.generic.halt_poll_success_hist,
4692 ktime_to_ns(cur) - ktime_to_ns(start_poll));
4696 /* Adjust poll time */
4698 if (block_ns <= vc->halt_poll_ns)
4700 /* We slept and blocked for longer than the max halt time */
4701 else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
4702 shrink_halt_poll_ns(vc);
4703 /* We slept and our poll time is too small */
4704 else if (vc->halt_poll_ns < halt_poll_ns &&
4705 block_ns < halt_poll_ns)
4706 grow_halt_poll_ns(vc);
4707 if (vc->halt_poll_ns > halt_poll_ns)
4708 vc->halt_poll_ns = halt_poll_ns;
4710 vc->halt_poll_ns = 0;
4712 trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
4716 * This never fails for a radix guest, as none of the operations it does
4717 * for a radix guest can fail or have a way to report failure.
4719 static int kvmhv_setup_mmu(struct kvm_vcpu *vcpu)
4722 struct kvm *kvm = vcpu->kvm;
4724 mutex_lock(&kvm->arch.mmu_setup_lock);
4725 if (!kvm->arch.mmu_ready) {
4726 if (!kvm_is_radix(kvm))
4727 r = kvmppc_hv_setup_htab_rma(vcpu);
4729 if (cpu_has_feature(CPU_FTR_ARCH_300))
4730 kvmppc_setup_partition_table(kvm);
4731 kvm->arch.mmu_ready = 1;
4734 mutex_unlock(&kvm->arch.mmu_setup_lock);
4738 static int kvmppc_run_vcpu(struct kvm_vcpu *vcpu)
4740 struct kvm_run *run = vcpu->run;
4742 struct kvmppc_vcore *vc;
4745 trace_kvmppc_run_vcpu_enter(vcpu);
4747 run->exit_reason = 0;
4748 vcpu->arch.ret = RESUME_GUEST;
4749 vcpu->arch.trap = 0;
4750 kvmppc_update_vpas(vcpu);
4753 * Synchronize with other threads in this virtual core
4755 vc = vcpu->arch.vcore;
4756 spin_lock(&vc->lock);
4757 vcpu->arch.ceded = 0;
4758 vcpu->arch.run_task = current;
4759 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
4760 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
4761 vcpu->arch.busy_preempt = TB_NIL;
4762 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
4766 * This happens the first time this is called for a vcpu.
4767 * If the vcore is already running, we may be able to start
4768 * this thread straight away and have it join in.
4770 if (!signal_pending(current)) {
4771 if ((vc->vcore_state == VCORE_PIGGYBACK ||
4772 vc->vcore_state == VCORE_RUNNING) &&
4773 !VCORE_IS_EXITING(vc)) {
4774 kvmppc_update_vpa_dispatch(vcpu, vc);
4775 kvmppc_start_thread(vcpu, vc);
4776 trace_kvm_guest_enter(vcpu);
4777 } else if (vc->vcore_state == VCORE_SLEEPING) {
4778 rcuwait_wake_up(&vc->wait);
4783 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
4784 !signal_pending(current)) {
4785 /* See if the MMU is ready to go */
4786 if (!vcpu->kvm->arch.mmu_ready) {
4787 spin_unlock(&vc->lock);
4788 r = kvmhv_setup_mmu(vcpu);
4789 spin_lock(&vc->lock);
4791 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4793 hardware_entry_failure_reason = 0;
4799 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
4800 kvmppc_vcore_end_preempt(vc);
4802 if (vc->vcore_state != VCORE_INACTIVE) {
4803 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
4806 for_each_runnable_thread(i, v, vc) {
4807 kvmppc_core_prepare_to_enter(v);
4808 if (signal_pending(v->arch.run_task)) {
4809 kvmppc_remove_runnable(vc, v, mftb());
4810 v->stat.signal_exits++;
4811 v->run->exit_reason = KVM_EXIT_INTR;
4812 v->arch.ret = -EINTR;
4813 wake_up(&v->arch.cpu_run);
4816 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
4819 for_each_runnable_thread(i, v, vc) {
4820 if (!kvmppc_vcpu_woken(v))
4821 n_ceded += v->arch.ceded;
4826 if (n_ceded == vc->n_runnable) {
4827 kvmppc_vcore_blocked(vc);
4828 } else if (need_resched()) {
4829 kvmppc_vcore_preempt(vc);
4830 /* Let something else run */
4831 cond_resched_lock(&vc->lock);
4832 if (vc->vcore_state == VCORE_PREEMPT)
4833 kvmppc_vcore_end_preempt(vc);
4835 kvmppc_run_core(vc);
4840 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
4841 (vc->vcore_state == VCORE_RUNNING ||
4842 vc->vcore_state == VCORE_EXITING ||
4843 vc->vcore_state == VCORE_PIGGYBACK))
4844 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
4846 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
4847 kvmppc_vcore_end_preempt(vc);
4849 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
4850 kvmppc_remove_runnable(vc, vcpu, mftb());
4851 vcpu->stat.signal_exits++;
4852 run->exit_reason = KVM_EXIT_INTR;
4853 vcpu->arch.ret = -EINTR;
4856 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
4857 /* Wake up some vcpu to run the core */
4859 v = next_runnable_thread(vc, &i);
4860 wake_up(&v->arch.cpu_run);
4863 trace_kvmppc_run_vcpu_exit(vcpu);
4864 spin_unlock(&vc->lock);
4865 return vcpu->arch.ret;
4868 int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
4871 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
4872 struct kvm_run *run = vcpu->run;
4875 struct kvmppc_vcore *vc;
4876 struct kvm *kvm = vcpu->kvm;
4877 struct kvm_nested_guest *nested = vcpu->arch.nested;
4878 unsigned long flags;
4881 trace_kvmppc_run_vcpu_enter(vcpu);
4883 run->exit_reason = 0;
4884 vcpu->arch.ret = RESUME_GUEST;
4885 vcpu->arch.trap = 0;
4887 vc = vcpu->arch.vcore;
4888 vcpu->arch.ceded = 0;
4889 vcpu->arch.run_task = current;
4890 vcpu->arch.last_inst = KVM_INST_FETCH_FAILED;
4892 /* See if the MMU is ready to go */
4893 if (unlikely(!kvm->arch.mmu_ready)) {
4894 r = kvmhv_setup_mmu(vcpu);
4896 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4897 run->fail_entry.hardware_entry_failure_reason = 0;
4906 kvmppc_update_vpas(vcpu);
4909 pcpu = smp_processor_id();
4910 if (kvm_is_radix(kvm))
4911 kvmppc_prepare_radix_vcpu(vcpu, pcpu);
4913 /* flags save not required, but irq_pmu has no disable/enable API */
4914 powerpc_local_irq_pmu_save(flags);
4916 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
4918 if (signal_pending(current))
4920 if (need_resched() || !kvm->arch.mmu_ready)
4924 vcpu->arch.thread_cpu = pcpu;
4926 local_paca->kvm_hstate.kvm_vcpu = vcpu;
4927 local_paca->kvm_hstate.ptid = 0;
4928 local_paca->kvm_hstate.fake_suspend = 0;
4931 * Orders set cpu/thread_cpu vs testing for pending interrupts and
4932 * doorbells below. The other side is when these fields are set vs
4933 * kvmppc_fast_vcpu_kick_hv reading the cpu/thread_cpu fields to
4934 * kick a vCPU to notice the pending interrupt.
4939 kvmppc_core_prepare_to_enter(vcpu);
4940 if (test_bit(BOOK3S_IRQPRIO_EXTERNAL,
4941 &vcpu->arch.pending_exceptions) ||
4942 xive_interrupt_pending(vcpu)) {
4944 * For nested HV, don't synthesize but always pass MER,
4945 * the L0 will be able to optimise that more
4946 * effectively than manipulating registers directly.
4948 if (!kvmhv_on_pseries() && (__kvmppc_get_msr_hv(vcpu) & MSR_EE))
4949 kvmppc_inject_interrupt_hv(vcpu,
4950 BOOK3S_INTERRUPT_EXTERNAL, 0);
4955 * L1's copy of L2's LPCR (vcpu->arch.vcore->lpcr) can get its MER bit
4956 * unexpectedly set - for e.g. during NMI handling when all register
4957 * states are synchronized from L0 to L1. L1 needs to inform L0 about
4958 * MER=1 only when there are pending external interrupts.
4959 * In the above if check, MER bit is set if there are pending
4960 * external interrupts. Hence, explicity mask off MER bit
4961 * here as otherwise it may generate spurious interrupts in L2 KVM
4962 * causing an endless loop, which results in L2 guest getting hung.
4966 } else if (vcpu->arch.pending_exceptions ||
4967 xive_interrupt_pending(vcpu)) {
4968 vcpu->arch.ret = RESUME_HOST;
4972 if (vcpu->arch.timer_running) {
4973 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
4974 vcpu->arch.timer_running = 0;
4979 kvmppc_update_vpa_dispatch_p9(vcpu, vc, tb + kvmppc_get_tb_offset(vcpu));
4981 trace_kvm_guest_enter(vcpu);
4983 guest_timing_enter_irqoff();
4985 srcu_idx = srcu_read_lock(&kvm->srcu);
4987 guest_state_enter_irqoff();
4988 this_cpu_disable_ftrace();
4990 trap = kvmhv_p9_guest_entry(vcpu, time_limit, lpcr, &tb);
4991 vcpu->arch.trap = trap;
4993 this_cpu_enable_ftrace();
4994 guest_state_exit_irqoff();
4996 srcu_read_unlock(&kvm->srcu, srcu_idx);
4998 set_irq_happened(trap);
5001 vcpu->arch.thread_cpu = -1;
5002 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
5004 if (!vtime_accounting_enabled_this_cpu()) {
5005 powerpc_local_irq_pmu_restore(flags);
5007 * Service IRQs here before guest_timing_exit_irqoff() so any
5008 * ticks that occurred while running the guest are accounted to
5009 * the guest. If vtime accounting is enabled, accounting uses
5010 * TB rather than ticks, so it can be done without enabling
5011 * interrupts here, which has the problem that it accounts
5012 * interrupt processing overhead to the host.
5014 powerpc_local_irq_pmu_save(flags);
5016 guest_timing_exit_irqoff();
5018 powerpc_local_irq_pmu_restore(flags);
5023 * cancel pending decrementer exception if DEC is now positive, or if
5024 * entering a nested guest in which case the decrementer is now owned
5025 * by L2 and the L1 decrementer is provided in hdec_expires
5027 if (kvmppc_core_pending_dec(vcpu) &&
5028 ((tb < kvmppc_dec_expires_host_tb(vcpu)) ||
5029 (trap == BOOK3S_INTERRUPT_SYSCALL &&
5030 kvmppc_get_gpr(vcpu, 3) == H_ENTER_NESTED)))
5031 kvmppc_core_dequeue_dec(vcpu);
5033 trace_kvm_guest_exit(vcpu);
5037 r = kvmppc_handle_exit_hv(vcpu, current);
5039 r = kvmppc_handle_nested_exit(vcpu);
5043 if (is_kvmppc_resume_guest(r) && !kvmppc_vcpu_check_block(vcpu)) {
5044 kvmppc_set_timer(vcpu);
5046 prepare_to_rcuwait(wait);
5048 set_current_state(TASK_INTERRUPTIBLE);
5049 if (signal_pending(current)) {
5050 vcpu->stat.signal_exits++;
5051 run->exit_reason = KVM_EXIT_INTR;
5052 vcpu->arch.ret = -EINTR;
5056 if (kvmppc_vcpu_check_block(vcpu))
5059 trace_kvmppc_vcore_blocked(vcpu, 0);
5061 trace_kvmppc_vcore_blocked(vcpu, 1);
5063 finish_rcuwait(wait);
5065 vcpu->arch.ceded = 0;
5068 trace_kvmppc_run_vcpu_exit(vcpu);
5070 return vcpu->arch.ret;
5073 vcpu->stat.signal_exits++;
5074 run->exit_reason = KVM_EXIT_INTR;
5075 vcpu->arch.ret = -EINTR;
5078 vcpu->arch.thread_cpu = -1;
5079 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
5080 powerpc_local_irq_pmu_restore(flags);
5085 static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)
5087 struct kvm_run *run = vcpu->run;
5093 start_timing(vcpu, &vcpu->arch.vcpu_entry);
5095 if (!vcpu->arch.sane) {
5096 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5100 /* No need to go into the guest when all we'll do is come back out */
5101 if (signal_pending(current)) {
5102 run->exit_reason = KVM_EXIT_INTR;
5106 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
5108 * Don't allow entry with a suspended transaction, because
5109 * the guest entry/exit code will lose it.
5111 if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
5112 (current->thread.regs->msr & MSR_TM)) {
5113 if (MSR_TM_ACTIVE(current->thread.regs->msr)) {
5114 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5115 run->fail_entry.hardware_entry_failure_reason = 0;
5122 * Force online to 1 for the sake of old userspace which doesn't
5125 if (!vcpu->arch.online) {
5126 atomic_inc(&vcpu->arch.vcore->online_count);
5127 vcpu->arch.online = 1;
5130 kvmppc_core_prepare_to_enter(vcpu);
5133 atomic_inc(&kvm->arch.vcpus_running);
5134 /* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */
5138 if (IS_ENABLED(CONFIG_PPC_FPU))
5140 if (cpu_has_feature(CPU_FTR_ALTIVEC))
5142 if (cpu_has_feature(CPU_FTR_VSX))
5144 if ((cpu_has_feature(CPU_FTR_TM) ||
5145 cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) &&
5146 (kvmppc_get_hfscr_hv(vcpu) & HFSCR_TM))
5148 msr = msr_check_and_set(msr);
5150 kvmppc_save_user_regs();
5152 kvmppc_save_current_sprs();
5154 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5155 vcpu->arch.waitp = &vcpu->arch.vcore->wait;
5156 vcpu->arch.pgdir = kvm->mm->pgd;
5157 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
5160 accumulate_time(vcpu, &vcpu->arch.guest_entry);
5161 if (cpu_has_feature(CPU_FTR_ARCH_300))
5162 r = kvmhv_run_single_vcpu(vcpu, ~(u64)0,
5163 vcpu->arch.vcore->lpcr);
5165 r = kvmppc_run_vcpu(vcpu);
5167 if (run->exit_reason == KVM_EXIT_PAPR_HCALL) {
5168 accumulate_time(vcpu, &vcpu->arch.hcall);
5170 if (!kvmhv_is_nestedv2() && WARN_ON_ONCE(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) {
5172 * These should have been caught reflected
5173 * into the guest by now. Final sanity check:
5174 * don't allow userspace to execute hcalls in
5180 trace_kvm_hcall_enter(vcpu);
5181 r = kvmppc_pseries_do_hcall(vcpu);
5182 trace_kvm_hcall_exit(vcpu, r);
5183 kvmppc_core_prepare_to_enter(vcpu);
5184 } else if (r == RESUME_PAGE_FAULT) {
5185 accumulate_time(vcpu, &vcpu->arch.pg_fault);
5186 srcu_idx = srcu_read_lock(&kvm->srcu);
5187 r = kvmppc_book3s_hv_page_fault(vcpu,
5188 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
5189 srcu_read_unlock(&kvm->srcu, srcu_idx);
5190 } else if (r == RESUME_PASSTHROUGH) {
5191 if (WARN_ON(xics_on_xive()))
5194 r = kvmppc_xics_rm_complete(vcpu, 0);
5196 } while (is_kvmppc_resume_guest(r));
5197 accumulate_time(vcpu, &vcpu->arch.vcpu_exit);
5199 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
5200 atomic_dec(&kvm->arch.vcpus_running);
5202 srr_regs_clobbered();
5209 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
5210 int shift, int sllp)
5212 (*sps)->page_shift = shift;
5213 (*sps)->slb_enc = sllp;
5214 (*sps)->enc[0].page_shift = shift;
5215 (*sps)->enc[0].pte_enc = kvmppc_pgsize_lp_encoding(shift, shift);
5217 * Add 16MB MPSS support (may get filtered out by userspace)
5220 int penc = kvmppc_pgsize_lp_encoding(shift, 24);
5222 (*sps)->enc[1].page_shift = 24;
5223 (*sps)->enc[1].pte_enc = penc;
5229 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
5230 struct kvm_ppc_smmu_info *info)
5232 struct kvm_ppc_one_seg_page_size *sps;
5235 * POWER7, POWER8 and POWER9 all support 32 storage keys for data.
5236 * POWER7 doesn't support keys for instruction accesses,
5237 * POWER8 and POWER9 do.
5239 info->data_keys = 32;
5240 info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0;
5242 /* POWER7, 8 and 9 all have 1T segments and 32-entry SLB */
5243 info->flags = KVM_PPC_PAGE_SIZES_REAL | KVM_PPC_1T_SEGMENTS;
5244 info->slb_size = 32;
5246 /* We only support these sizes for now, and no muti-size segments */
5247 sps = &info->sps[0];
5248 kvmppc_add_seg_page_size(&sps, 12, 0);
5249 kvmppc_add_seg_page_size(&sps, 16, SLB_VSID_L | SLB_VSID_LP_01);
5250 kvmppc_add_seg_page_size(&sps, 24, SLB_VSID_L);
5252 /* If running as a nested hypervisor, we don't support HPT guests */
5253 if (kvmhv_on_pseries())
5254 info->flags |= KVM_PPC_NO_HASH;
5260 * Get (and clear) the dirty memory log for a memory slot.
5262 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
5263 struct kvm_dirty_log *log)
5265 struct kvm_memslots *slots;
5266 struct kvm_memory_slot *memslot;
5269 unsigned long *buf, *p;
5270 struct kvm_vcpu *vcpu;
5272 mutex_lock(&kvm->slots_lock);
5275 if (log->slot >= KVM_USER_MEM_SLOTS)
5278 slots = kvm_memslots(kvm);
5279 memslot = id_to_memslot(slots, log->slot);
5281 if (!memslot || !memslot->dirty_bitmap)
5285 * Use second half of bitmap area because both HPT and radix
5286 * accumulate bits in the first half.
5288 n = kvm_dirty_bitmap_bytes(memslot);
5289 buf = memslot->dirty_bitmap + n / sizeof(long);
5292 if (kvm_is_radix(kvm))
5293 r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
5295 r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
5300 * We accumulate dirty bits in the first half of the
5301 * memslot's dirty_bitmap area, for when pages are paged
5302 * out or modified by the host directly. Pick up these
5303 * bits and add them to the map.
5305 p = memslot->dirty_bitmap;
5306 for (i = 0; i < n / sizeof(long); ++i)
5307 buf[i] |= xchg(&p[i], 0);
5309 /* Harvest dirty bits from VPA and DTL updates */
5310 /* Note: we never modify the SLB shadow buffer areas */
5311 kvm_for_each_vcpu(i, vcpu, kvm) {
5312 spin_lock(&vcpu->arch.vpa_update_lock);
5313 kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
5314 kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
5315 spin_unlock(&vcpu->arch.vpa_update_lock);
5319 if (copy_to_user(log->dirty_bitmap, buf, n))
5324 mutex_unlock(&kvm->slots_lock);
5328 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *slot)
5330 vfree(slot->arch.rmap);
5331 slot->arch.rmap = NULL;
5334 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
5335 const struct kvm_memory_slot *old,
5336 struct kvm_memory_slot *new,
5337 enum kvm_mr_change change)
5339 if (change == KVM_MR_CREATE) {
5340 unsigned long size = array_size(new->npages, sizeof(*new->arch.rmap));
5342 if ((size >> PAGE_SHIFT) > totalram_pages())
5345 new->arch.rmap = vzalloc(size);
5346 if (!new->arch.rmap)
5348 } else if (change != KVM_MR_DELETE) {
5349 new->arch.rmap = old->arch.rmap;
5355 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
5356 struct kvm_memory_slot *old,
5357 const struct kvm_memory_slot *new,
5358 enum kvm_mr_change change)
5361 * If we are creating or modifying a memslot, it might make
5362 * some address that was previously cached as emulated
5363 * MMIO be no longer emulated MMIO, so invalidate
5364 * all the caches of emulated MMIO translations.
5366 if (change != KVM_MR_DELETE)
5367 atomic64_inc(&kvm->arch.mmio_update);
5370 * For change == KVM_MR_MOVE or KVM_MR_DELETE, higher levels
5371 * have already called kvm_arch_flush_shadow_memslot() to
5372 * flush shadow mappings. For KVM_MR_CREATE we have no
5373 * previous mappings. So the only case to handle is
5374 * KVM_MR_FLAGS_ONLY when the KVM_MEM_LOG_DIRTY_PAGES bit
5376 * For radix guests, we flush on setting KVM_MEM_LOG_DIRTY_PAGES
5377 * to get rid of any THP PTEs in the partition-scoped page tables
5378 * so we can track dirtiness at the page level; we flush when
5379 * clearing KVM_MEM_LOG_DIRTY_PAGES so that we can go back to
5382 if (change == KVM_MR_FLAGS_ONLY && kvm_is_radix(kvm) &&
5383 ((new->flags ^ old->flags) & KVM_MEM_LOG_DIRTY_PAGES))
5384 kvmppc_radix_flush_memslot(kvm, old);
5386 * If UV hasn't yet called H_SVM_INIT_START, don't register memslots.
5388 if (!kvm->arch.secure_guest)
5394 * @TODO kvmppc_uvmem_memslot_create() can fail and
5395 * return error. Fix this.
5397 kvmppc_uvmem_memslot_create(kvm, new);
5400 kvmppc_uvmem_memslot_delete(kvm, old);
5403 /* TODO: Handle KVM_MR_MOVE */
5409 * Update LPCR values in kvm->arch and in vcores.
5410 * Caller must hold kvm->arch.mmu_setup_lock (for mutual exclusion
5411 * of kvm->arch.lpcr update).
5413 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
5418 if ((kvm->arch.lpcr & mask) == lpcr)
5421 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
5423 for (i = 0; i < KVM_MAX_VCORES; ++i) {
5424 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
5428 spin_lock(&vc->lock);
5429 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
5430 verify_lpcr(kvm, vc->lpcr);
5431 spin_unlock(&vc->lock);
5432 if (++cores_done >= kvm->arch.online_vcores)
5436 if (kvmhv_is_nestedv2()) {
5437 struct kvm_vcpu *vcpu;
5439 kvm_for_each_vcpu(i, vcpu, kvm) {
5440 kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LPCR);
5445 void kvmppc_setup_partition_table(struct kvm *kvm)
5447 unsigned long dw0, dw1;
5449 if (!kvm_is_radix(kvm)) {
5450 /* PS field - page size for VRMA */
5451 dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
5452 ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
5453 /* HTABSIZE and HTABORG fields */
5454 dw0 |= kvm->arch.sdr1;
5456 /* Second dword as set by userspace */
5457 dw1 = kvm->arch.process_table;
5459 dw0 = PATB_HR | radix__get_tree_size() |
5460 __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
5461 dw1 = PATB_GR | kvm->arch.process_table;
5463 kvmhv_set_ptbl_entry(kvm->arch.lpid, dw0, dw1);
5467 * Set up HPT (hashed page table) and RMA (real-mode area).
5468 * Must be called with kvm->arch.mmu_setup_lock held.
5470 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
5473 struct kvm *kvm = vcpu->kvm;
5475 struct kvm_memory_slot *memslot;
5476 struct vm_area_struct *vma;
5477 unsigned long lpcr = 0, senc;
5478 unsigned long psize, porder;
5481 /* Allocate hashed page table (if not done already) and reset it */
5482 if (!kvm->arch.hpt.virt) {
5483 int order = KVM_DEFAULT_HPT_ORDER;
5484 struct kvm_hpt_info info;
5486 err = kvmppc_allocate_hpt(&info, order);
5487 /* If we get here, it means userspace didn't specify a
5488 * size explicitly. So, try successively smaller
5489 * sizes if the default failed. */
5490 while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
5491 err = kvmppc_allocate_hpt(&info, order);
5494 pr_err("KVM: Couldn't alloc HPT\n");
5498 kvmppc_set_hpt(kvm, &info);
5501 /* Look up the memslot for guest physical address 0 */
5502 srcu_idx = srcu_read_lock(&kvm->srcu);
5503 memslot = gfn_to_memslot(kvm, 0);
5505 /* We must have some memory at 0 by now */
5507 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
5510 /* Look up the VMA for the start of this memory slot */
5511 hva = memslot->userspace_addr;
5512 mmap_read_lock(kvm->mm);
5513 vma = vma_lookup(kvm->mm, hva);
5514 if (!vma || (vma->vm_flags & VM_IO))
5517 psize = vma_kernel_pagesize(vma);
5519 mmap_read_unlock(kvm->mm);
5521 /* We can handle 4k, 64k or 16M pages in the VRMA */
5522 if (psize >= 0x1000000)
5524 else if (psize >= 0x10000)
5528 porder = __ilog2(psize);
5530 senc = slb_pgsize_encoding(psize);
5531 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
5532 (VRMA_VSID << SLB_VSID_SHIFT_1T);
5533 /* Create HPTEs in the hash page table for the VRMA */
5534 kvmppc_map_vrma(vcpu, memslot, porder);
5536 /* Update VRMASD field in the LPCR */
5537 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
5538 /* the -4 is to account for senc values starting at 0x10 */
5539 lpcr = senc << (LPCR_VRMASD_SH - 4);
5540 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
5543 /* Order updates to kvm->arch.lpcr etc. vs. mmu_ready */
5547 srcu_read_unlock(&kvm->srcu, srcu_idx);
5552 mmap_read_unlock(kvm->mm);
5557 * Must be called with kvm->arch.mmu_setup_lock held and
5558 * mmu_ready = 0 and no vcpus running.
5560 int kvmppc_switch_mmu_to_hpt(struct kvm *kvm)
5562 unsigned long lpcr, lpcr_mask;
5564 if (nesting_enabled(kvm))
5565 kvmhv_release_all_nested(kvm);
5566 kvmppc_rmap_reset(kvm);
5567 kvm->arch.process_table = 0;
5568 /* Mutual exclusion with kvm_unmap_gfn_range etc. */
5569 spin_lock(&kvm->mmu_lock);
5570 kvm->arch.radix = 0;
5571 spin_unlock(&kvm->mmu_lock);
5572 kvmppc_free_radix(kvm);
5575 lpcr_mask = LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5576 if (cpu_has_feature(CPU_FTR_ARCH_31))
5577 lpcr_mask |= LPCR_HAIL;
5578 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
5584 * Must be called with kvm->arch.mmu_setup_lock held and
5585 * mmu_ready = 0 and no vcpus running.
5587 int kvmppc_switch_mmu_to_radix(struct kvm *kvm)
5589 unsigned long lpcr, lpcr_mask;
5592 err = kvmppc_init_vm_radix(kvm);
5595 kvmppc_rmap_reset(kvm);
5596 /* Mutual exclusion with kvm_unmap_gfn_range etc. */
5597 spin_lock(&kvm->mmu_lock);
5598 kvm->arch.radix = 1;
5599 spin_unlock(&kvm->mmu_lock);
5600 kvmppc_free_hpt(&kvm->arch.hpt);
5602 lpcr = LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5603 lpcr_mask = LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5604 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
5605 lpcr_mask |= LPCR_HAIL;
5606 if (cpu_has_feature(CPU_FTR_HVMODE) &&
5607 (kvm->arch.host_lpcr & LPCR_HAIL))
5610 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
5615 #ifdef CONFIG_KVM_XICS
5617 * Allocate a per-core structure for managing state about which cores are
5618 * running in the host versus the guest and for exchanging data between
5619 * real mode KVM and CPU running in the host.
5620 * This is only done for the first VM.
5621 * The allocated structure stays even if all VMs have stopped.
5622 * It is only freed when the kvm-hv module is unloaded.
5623 * It's OK for this routine to fail, we just don't support host
5624 * core operations like redirecting H_IPI wakeups.
5626 void kvmppc_alloc_host_rm_ops(void)
5628 struct kvmppc_host_rm_ops *ops;
5629 unsigned long l_ops;
5633 if (cpu_has_feature(CPU_FTR_ARCH_300))
5636 /* Not the first time here ? */
5637 if (kvmppc_host_rm_ops_hv != NULL)
5640 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
5644 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
5645 ops->rm_core = kzalloc(size, GFP_KERNEL);
5647 if (!ops->rm_core) {
5654 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
5655 if (!cpu_online(cpu))
5658 core = cpu >> threads_shift;
5659 ops->rm_core[core].rm_state.in_host = 1;
5662 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
5665 * Make the contents of the kvmppc_host_rm_ops structure visible
5666 * to other CPUs before we assign it to the global variable.
5667 * Do an atomic assignment (no locks used here), but if someone
5668 * beats us to it, just free our copy and return.
5671 l_ops = (unsigned long) ops;
5673 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
5675 kfree(ops->rm_core);
5680 cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE,
5681 "ppc/kvm_book3s:prepare",
5682 kvmppc_set_host_core,
5683 kvmppc_clear_host_core);
5687 void kvmppc_free_host_rm_ops(void)
5689 if (kvmppc_host_rm_ops_hv) {
5690 cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
5691 kfree(kvmppc_host_rm_ops_hv->rm_core);
5692 kfree(kvmppc_host_rm_ops_hv);
5693 kvmppc_host_rm_ops_hv = NULL;
5698 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
5700 unsigned long lpcr, lpid;
5703 mutex_init(&kvm->arch.uvmem_lock);
5704 INIT_LIST_HEAD(&kvm->arch.uvmem_pfns);
5705 mutex_init(&kvm->arch.mmu_setup_lock);
5707 /* Allocate the guest's logical partition ID */
5709 if (!kvmhv_is_nestedv2()) {
5710 lpid = kvmppc_alloc_lpid();
5713 kvm->arch.lpid = lpid;
5716 kvmppc_alloc_host_rm_ops();
5718 kvmhv_vm_nested_init(kvm);
5720 if (kvmhv_is_nestedv2()) {
5722 unsigned long guest_id;
5724 rc = plpar_guest_create(0, &guest_id);
5726 if (rc != H_SUCCESS)
5727 pr_err("KVM: Create Guest hcall failed, rc=%ld\n", rc);
5734 case H_NOT_ENOUGH_RESOURCES:
5739 case H_NOT_AVAILABLE:
5742 kvm->arch.lpid = guest_id;
5747 * Since we don't flush the TLB when tearing down a VM,
5748 * and this lpid might have previously been used,
5749 * make sure we flush on each core before running the new VM.
5750 * On POWER9, the tlbie in mmu_partition_table_set_entry()
5751 * does this flush for us.
5753 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5754 cpumask_setall(&kvm->arch.need_tlb_flush);
5756 /* Start out with the default set of hcalls enabled */
5757 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
5758 sizeof(kvm->arch.enabled_hcalls));
5760 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5761 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
5763 /* Init LPCR for virtual RMA mode */
5764 if (cpu_has_feature(CPU_FTR_HVMODE)) {
5765 kvm->arch.host_lpid = mfspr(SPRN_LPID);
5766 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
5767 lpcr &= LPCR_PECE | LPCR_LPES;
5770 * The L2 LPES mode will be set by the L0 according to whether
5771 * or not it needs to take external interrupts in HV mode.
5775 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
5776 LPCR_VPM0 | LPCR_VPM1;
5777 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
5778 (VRMA_VSID << SLB_VSID_SHIFT_1T);
5779 /* On POWER8 turn on online bit to enable PURR/SPURR */
5780 if (cpu_has_feature(CPU_FTR_ARCH_207S))
5783 * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
5784 * Set HVICE bit to enable hypervisor virtualization interrupts.
5785 * Set HEIC to prevent OS interrupts to go to hypervisor (should
5786 * be unnecessary but better safe than sorry in case we re-enable
5787 * EE in HV mode with this LPCR still set)
5789 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
5791 lpcr |= LPCR_HVICE | LPCR_HEIC;
5794 * If xive is enabled, we route 0x500 interrupts directly
5802 * If the host uses radix, the guest starts out as radix.
5804 if (radix_enabled()) {
5805 kvm->arch.radix = 1;
5806 kvm->arch.mmu_ready = 1;
5808 lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5809 if (cpu_has_feature(CPU_FTR_HVMODE) &&
5810 cpu_has_feature(CPU_FTR_ARCH_31) &&
5811 (kvm->arch.host_lpcr & LPCR_HAIL))
5813 ret = kvmppc_init_vm_radix(kvm);
5815 if (kvmhv_is_nestedv2())
5816 plpar_guest_delete(0, kvm->arch.lpid);
5818 kvmppc_free_lpid(kvm->arch.lpid);
5821 kvmppc_setup_partition_table(kvm);
5824 verify_lpcr(kvm, lpcr);
5825 kvm->arch.lpcr = lpcr;
5827 /* Initialization for future HPT resizes */
5828 kvm->arch.resize_hpt = NULL;
5831 * Work out how many sets the TLB has, for the use of
5832 * the TLB invalidation loop in book3s_hv_rmhandlers.S.
5834 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
5836 * P10 will flush all the congruence class with a single tlbiel
5838 kvm->arch.tlb_sets = 1;
5839 } else if (radix_enabled())
5840 kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
5841 else if (cpu_has_feature(CPU_FTR_ARCH_300))
5842 kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
5843 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
5844 kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
5846 kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
5849 * Track that we now have a HV mode VM active. This blocks secondary
5850 * CPU threads from coming online.
5852 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5853 kvm_hv_vm_activated();
5856 * Initialize smt_mode depending on processor.
5857 * POWER8 and earlier have to use "strict" threading, where
5858 * all vCPUs in a vcore have to run on the same (sub)core,
5859 * whereas on POWER9 the threads can each run a different
5862 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5863 kvm->arch.smt_mode = threads_per_subcore;
5865 kvm->arch.smt_mode = 1;
5866 kvm->arch.emul_smt_mode = 1;
5871 static int kvmppc_arch_create_vm_debugfs_hv(struct kvm *kvm)
5873 kvmppc_mmu_debugfs_init(kvm);
5874 if (radix_enabled())
5875 kvmhv_radix_debugfs_init(kvm);
5879 static void kvmppc_free_vcores(struct kvm *kvm)
5883 for (i = 0; i < KVM_MAX_VCORES; ++i)
5884 kfree(kvm->arch.vcores[i]);
5885 kvm->arch.online_vcores = 0;
5888 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
5890 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5891 kvm_hv_vm_deactivated();
5893 kvmppc_free_vcores(kvm);
5896 if (kvm_is_radix(kvm))
5897 kvmppc_free_radix(kvm);
5899 kvmppc_free_hpt(&kvm->arch.hpt);
5901 /* Perform global invalidation and return lpid to the pool */
5902 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
5903 if (nesting_enabled(kvm))
5904 kvmhv_release_all_nested(kvm);
5905 kvm->arch.process_table = 0;
5906 if (kvm->arch.secure_guest)
5907 uv_svm_terminate(kvm->arch.lpid);
5908 if (!kvmhv_is_nestedv2())
5909 kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0);
5912 if (kvmhv_is_nestedv2()) {
5913 kvmhv_flush_lpid(kvm->arch.lpid);
5914 plpar_guest_delete(0, kvm->arch.lpid);
5916 kvmppc_free_lpid(kvm->arch.lpid);
5919 kvmppc_free_pimap(kvm);
5922 /* We don't need to emulate any privileged instructions or dcbz */
5923 static int kvmppc_core_emulate_op_hv(struct kvm_vcpu *vcpu,
5924 unsigned int inst, int *advance)
5926 return EMULATE_FAIL;
5929 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
5932 return EMULATE_FAIL;
5935 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
5938 return EMULATE_FAIL;
5941 static int kvmppc_core_check_processor_compat_hv(void)
5943 if (cpu_has_feature(CPU_FTR_HVMODE) &&
5944 cpu_has_feature(CPU_FTR_ARCH_206))
5947 /* POWER9 in radix mode is capable of being a nested hypervisor. */
5948 if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled())
5954 #ifdef CONFIG_KVM_XICS
5956 void kvmppc_free_pimap(struct kvm *kvm)
5958 kfree(kvm->arch.pimap);
5961 static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
5963 return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
5966 static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
5968 struct irq_desc *desc;
5969 struct kvmppc_irq_map *irq_map;
5970 struct kvmppc_passthru_irqmap *pimap;
5971 struct irq_chip *chip;
5973 struct irq_data *host_data;
5975 if (!kvm_irq_bypass)
5978 desc = irq_to_desc(host_irq);
5982 mutex_lock(&kvm->lock);
5984 pimap = kvm->arch.pimap;
5985 if (pimap == NULL) {
5986 /* First call, allocate structure to hold IRQ map */
5987 pimap = kvmppc_alloc_pimap();
5988 if (pimap == NULL) {
5989 mutex_unlock(&kvm->lock);
5992 kvm->arch.pimap = pimap;
5996 * For now, we only support interrupts for which the EOI operation
5997 * is an OPAL call followed by a write to XIRR, since that's
5998 * what our real-mode EOI code does, or a XIVE interrupt
6000 chip = irq_data_get_irq_chip(&desc->irq_data);
6001 if (!chip || !is_pnv_opal_msi(chip)) {
6002 pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
6003 host_irq, guest_gsi);
6004 mutex_unlock(&kvm->lock);
6009 * See if we already have an entry for this guest IRQ number.
6010 * If it's mapped to a hardware IRQ number, that's an error,
6011 * otherwise re-use this entry.
6013 for (i = 0; i < pimap->n_mapped; i++) {
6014 if (guest_gsi == pimap->mapped[i].v_hwirq) {
6015 if (pimap->mapped[i].r_hwirq) {
6016 mutex_unlock(&kvm->lock);
6023 if (i == KVMPPC_PIRQ_MAPPED) {
6024 mutex_unlock(&kvm->lock);
6025 return -EAGAIN; /* table is full */
6028 irq_map = &pimap->mapped[i];
6030 irq_map->v_hwirq = guest_gsi;
6031 irq_map->desc = desc;
6034 * Order the above two stores before the next to serialize with
6035 * the KVM real mode handler.
6040 * The 'host_irq' number is mapped in the PCI-MSI domain but
6041 * the underlying calls, which will EOI the interrupt in real
6042 * mode, need an HW IRQ number mapped in the XICS IRQ domain.
6044 host_data = irq_domain_get_irq_data(irq_get_default_host(), host_irq);
6045 irq_map->r_hwirq = (unsigned int)irqd_to_hwirq(host_data);
6047 if (i == pimap->n_mapped)
6051 rc = kvmppc_xive_set_mapped(kvm, guest_gsi, host_irq);
6053 kvmppc_xics_set_mapped(kvm, guest_gsi, irq_map->r_hwirq);
6055 irq_map->r_hwirq = 0;
6057 mutex_unlock(&kvm->lock);
6062 static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
6064 struct irq_desc *desc;
6065 struct kvmppc_passthru_irqmap *pimap;
6068 if (!kvm_irq_bypass)
6071 desc = irq_to_desc(host_irq);
6075 mutex_lock(&kvm->lock);
6076 if (!kvm->arch.pimap)
6079 pimap = kvm->arch.pimap;
6081 for (i = 0; i < pimap->n_mapped; i++) {
6082 if (guest_gsi == pimap->mapped[i].v_hwirq)
6086 if (i == pimap->n_mapped) {
6087 mutex_unlock(&kvm->lock);
6092 rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, host_irq);
6094 kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
6096 /* invalidate the entry (what to do on error from the above ?) */
6097 pimap->mapped[i].r_hwirq = 0;
6100 * We don't free this structure even when the count goes to
6101 * zero. The structure is freed when we destroy the VM.
6104 mutex_unlock(&kvm->lock);
6108 static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
6109 struct irq_bypass_producer *prod)
6112 struct kvm_kernel_irqfd *irqfd =
6113 container_of(cons, struct kvm_kernel_irqfd, consumer);
6115 irqfd->producer = prod;
6117 ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
6119 pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
6120 prod->irq, irqfd->gsi, ret);
6125 static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
6126 struct irq_bypass_producer *prod)
6129 struct kvm_kernel_irqfd *irqfd =
6130 container_of(cons, struct kvm_kernel_irqfd, consumer);
6132 irqfd->producer = NULL;
6135 * When producer of consumer is unregistered, we change back to
6136 * default external interrupt handling mode - KVM real mode
6137 * will switch back to host.
6139 ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
6141 pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
6142 prod->irq, irqfd->gsi, ret);
6146 static int kvm_arch_vm_ioctl_hv(struct file *filp,
6147 unsigned int ioctl, unsigned long arg)
6149 struct kvm *kvm __maybe_unused = filp->private_data;
6150 void __user *argp = (void __user *)arg;
6155 case KVM_PPC_ALLOCATE_HTAB: {
6158 /* If we're a nested hypervisor, we currently only support radix */
6159 if (kvmhv_on_pseries()) {
6165 if (get_user(htab_order, (u32 __user *)argp))
6167 r = kvmppc_alloc_reset_hpt(kvm, htab_order);
6174 case KVM_PPC_GET_HTAB_FD: {
6175 struct kvm_get_htab_fd ghf;
6178 if (copy_from_user(&ghf, argp, sizeof(ghf)))
6180 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
6184 case KVM_PPC_RESIZE_HPT_PREPARE: {
6185 struct kvm_ppc_resize_hpt rhpt;
6188 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
6191 r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
6195 case KVM_PPC_RESIZE_HPT_COMMIT: {
6196 struct kvm_ppc_resize_hpt rhpt;
6199 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
6202 r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
6214 * List of hcall numbers to enable by default.
6215 * For compatibility with old userspace, we enable by default
6216 * all hcalls that were implemented before the hcall-enabling
6217 * facility was added. Note this list should not include H_RTAS.
6219 static unsigned int default_hcall_list[] = {
6225 #ifdef CONFIG_SPAPR_TCE_IOMMU
6235 #ifdef CONFIG_KVM_XICS
6246 static void init_default_hcalls(void)
6251 for (i = 0; default_hcall_list[i]; ++i) {
6252 hcall = default_hcall_list[i];
6253 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
6254 __set_bit(hcall / 4, default_enabled_hcalls);
6258 static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
6264 /* If not on a POWER9, reject it */
6265 if (!cpu_has_feature(CPU_FTR_ARCH_300))
6268 /* If any unknown flags set, reject it */
6269 if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
6272 /* GR (guest radix) bit in process_table field must match */
6273 radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
6274 if (!!(cfg->process_table & PATB_GR) != radix)
6277 /* Process table size field must be reasonable, i.e. <= 24 */
6278 if ((cfg->process_table & PRTS_MASK) > 24)
6281 /* We can change a guest to/from radix now, if the host is radix */
6282 if (radix && !radix_enabled())
6285 /* If we're a nested hypervisor, we currently only support radix */
6286 if (kvmhv_on_pseries() && !radix)
6289 mutex_lock(&kvm->arch.mmu_setup_lock);
6290 if (radix != kvm_is_radix(kvm)) {
6291 if (kvm->arch.mmu_ready) {
6292 kvm->arch.mmu_ready = 0;
6293 /* order mmu_ready vs. vcpus_running */
6295 if (atomic_read(&kvm->arch.vcpus_running)) {
6296 kvm->arch.mmu_ready = 1;
6302 err = kvmppc_switch_mmu_to_radix(kvm);
6304 err = kvmppc_switch_mmu_to_hpt(kvm);
6309 kvm->arch.process_table = cfg->process_table;
6310 kvmppc_setup_partition_table(kvm);
6312 lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
6313 kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
6317 mutex_unlock(&kvm->arch.mmu_setup_lock);
6321 static int kvmhv_enable_nested(struct kvm *kvm)
6325 if (!cpu_has_feature(CPU_FTR_ARCH_300))
6327 if (!radix_enabled())
6329 if (kvmhv_is_nestedv2())
6332 /* kvm == NULL means the caller is testing if the capability exists */
6334 kvm->arch.nested_enable = true;
6338 static int kvmhv_load_from_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
6343 if (kvmhv_vcpu_is_radix(vcpu)) {
6344 rc = kvmhv_copy_from_guest_radix(vcpu, *eaddr, ptr, size);
6350 /* For now quadrants are the only way to access nested guest memory */
6351 if (rc && vcpu->arch.nested)
6357 static int kvmhv_store_to_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
6362 if (kvmhv_vcpu_is_radix(vcpu)) {
6363 rc = kvmhv_copy_to_guest_radix(vcpu, *eaddr, ptr, size);
6369 /* For now quadrants are the only way to access nested guest memory */
6370 if (rc && vcpu->arch.nested)
6376 static void unpin_vpa_reset(struct kvm *kvm, struct kvmppc_vpa *vpa)
6378 unpin_vpa(kvm, vpa);
6380 vpa->pinned_addr = NULL;
6382 vpa->update_pending = 0;
6386 * Enable a guest to become a secure VM, or test whether
6387 * that could be enabled.
6388 * Called when the KVM_CAP_PPC_SECURE_GUEST capability is
6389 * tested (kvm == NULL) or enabled (kvm != NULL).
6391 static int kvmhv_enable_svm(struct kvm *kvm)
6393 if (!kvmppc_uvmem_available())
6396 kvm->arch.svm_enabled = 1;
6401 * IOCTL handler to turn off secure mode of guest
6403 * - Release all device pages
6404 * - Issue ucall to terminate the guest on the UV side
6405 * - Unpin the VPA pages.
6406 * - Reinit the partition scoped page tables
6408 static int kvmhv_svm_off(struct kvm *kvm)
6410 struct kvm_vcpu *vcpu;
6416 if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
6419 mutex_lock(&kvm->arch.mmu_setup_lock);
6420 mmu_was_ready = kvm->arch.mmu_ready;
6421 if (kvm->arch.mmu_ready) {
6422 kvm->arch.mmu_ready = 0;
6423 /* order mmu_ready vs. vcpus_running */
6425 if (atomic_read(&kvm->arch.vcpus_running)) {
6426 kvm->arch.mmu_ready = 1;
6432 srcu_idx = srcu_read_lock(&kvm->srcu);
6433 for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
6434 struct kvm_memory_slot *memslot;
6435 struct kvm_memslots *slots = __kvm_memslots(kvm, i);
6441 kvm_for_each_memslot(memslot, bkt, slots) {
6442 kvmppc_uvmem_drop_pages(memslot, kvm, true);
6443 uv_unregister_mem_slot(kvm->arch.lpid, memslot->id);
6446 srcu_read_unlock(&kvm->srcu, srcu_idx);
6448 ret = uv_svm_terminate(kvm->arch.lpid);
6449 if (ret != U_SUCCESS) {
6455 * When secure guest is reset, all the guest pages are sent
6456 * to UV via UV_PAGE_IN before the non-boot vcpus get a
6457 * chance to run and unpin their VPA pages. Unpinning of all
6458 * VPA pages is done here explicitly so that VPA pages
6459 * can be migrated to the secure side.
6461 * This is required to for the secure SMP guest to reboot
6464 kvm_for_each_vcpu(i, vcpu, kvm) {
6465 spin_lock(&vcpu->arch.vpa_update_lock);
6466 unpin_vpa_reset(kvm, &vcpu->arch.dtl);
6467 unpin_vpa_reset(kvm, &vcpu->arch.slb_shadow);
6468 unpin_vpa_reset(kvm, &vcpu->arch.vpa);
6469 spin_unlock(&vcpu->arch.vpa_update_lock);
6472 kvmppc_setup_partition_table(kvm);
6473 kvm->arch.secure_guest = 0;
6474 kvm->arch.mmu_ready = mmu_was_ready;
6476 mutex_unlock(&kvm->arch.mmu_setup_lock);
6480 static int kvmhv_enable_dawr1(struct kvm *kvm)
6482 if (!cpu_has_feature(CPU_FTR_DAWR1))
6485 /* kvm == NULL means the caller is testing if the capability exists */
6487 kvm->arch.dawr1_enabled = true;
6491 static bool kvmppc_hash_v3_possible(void)
6493 if (!cpu_has_feature(CPU_FTR_ARCH_300))
6496 if (!cpu_has_feature(CPU_FTR_HVMODE))
6500 * POWER9 chips before version 2.02 can't have some threads in
6501 * HPT mode and some in radix mode on the same core.
6503 if (radix_enabled()) {
6504 unsigned int pvr = mfspr(SPRN_PVR);
6505 if ((pvr >> 16) == PVR_POWER9 &&
6506 (((pvr & 0xe000) == 0 && (pvr & 0xfff) < 0x202) ||
6507 ((pvr & 0xe000) == 0x2000 && (pvr & 0xfff) < 0x101)))
6514 static struct kvmppc_ops kvm_ops_hv = {
6515 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
6516 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
6517 .get_one_reg = kvmppc_get_one_reg_hv,
6518 .set_one_reg = kvmppc_set_one_reg_hv,
6519 .vcpu_load = kvmppc_core_vcpu_load_hv,
6520 .vcpu_put = kvmppc_core_vcpu_put_hv,
6521 .inject_interrupt = kvmppc_inject_interrupt_hv,
6522 .set_msr = kvmppc_set_msr_hv,
6523 .vcpu_run = kvmppc_vcpu_run_hv,
6524 .vcpu_create = kvmppc_core_vcpu_create_hv,
6525 .vcpu_free = kvmppc_core_vcpu_free_hv,
6526 .check_requests = kvmppc_core_check_requests_hv,
6527 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
6528 .flush_memslot = kvmppc_core_flush_memslot_hv,
6529 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
6530 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
6531 .unmap_gfn_range = kvm_unmap_gfn_range_hv,
6532 .age_gfn = kvm_age_gfn_hv,
6533 .test_age_gfn = kvm_test_age_gfn_hv,
6534 .free_memslot = kvmppc_core_free_memslot_hv,
6535 .init_vm = kvmppc_core_init_vm_hv,
6536 .destroy_vm = kvmppc_core_destroy_vm_hv,
6537 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
6538 .emulate_op = kvmppc_core_emulate_op_hv,
6539 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
6540 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
6541 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
6542 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
6543 .hcall_implemented = kvmppc_hcall_impl_hv,
6544 #ifdef CONFIG_KVM_XICS
6545 .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
6546 .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
6548 .configure_mmu = kvmhv_configure_mmu,
6549 .get_rmmu_info = kvmhv_get_rmmu_info,
6550 .set_smt_mode = kvmhv_set_smt_mode,
6551 .enable_nested = kvmhv_enable_nested,
6552 .load_from_eaddr = kvmhv_load_from_eaddr,
6553 .store_to_eaddr = kvmhv_store_to_eaddr,
6554 .enable_svm = kvmhv_enable_svm,
6555 .svm_off = kvmhv_svm_off,
6556 .enable_dawr1 = kvmhv_enable_dawr1,
6557 .hash_v3_possible = kvmppc_hash_v3_possible,
6558 .create_vcpu_debugfs = kvmppc_arch_create_vcpu_debugfs_hv,
6559 .create_vm_debugfs = kvmppc_arch_create_vm_debugfs_hv,
6562 static int kvm_init_subcore_bitmap(void)
6565 int nr_cores = cpu_nr_cores();
6566 struct sibling_subcore_state *sibling_subcore_state;
6568 for (i = 0; i < nr_cores; i++) {
6569 int first_cpu = i * threads_per_core;
6570 int node = cpu_to_node(first_cpu);
6572 /* Ignore if it is already allocated. */
6573 if (paca_ptrs[first_cpu]->sibling_subcore_state)
6576 sibling_subcore_state =
6577 kzalloc_node(sizeof(struct sibling_subcore_state),
6579 if (!sibling_subcore_state)
6583 for (j = 0; j < threads_per_core; j++) {
6584 int cpu = first_cpu + j;
6586 paca_ptrs[cpu]->sibling_subcore_state =
6587 sibling_subcore_state;
6593 static int kvmppc_radix_possible(void)
6595 return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
6598 static int kvmppc_book3s_init_hv(void)
6602 if (!tlbie_capable) {
6603 pr_err("KVM-HV: Host does not support TLBIE\n");
6608 * FIXME!! Do we need to check on all cpus ?
6610 r = kvmppc_core_check_processor_compat_hv();
6614 r = kvmhv_nested_init();
6618 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
6619 r = kvm_init_subcore_bitmap();
6625 * We need a way of accessing the XICS interrupt controller,
6626 * either directly, via paca_ptrs[cpu]->kvm_hstate.xics_phys, or
6627 * indirectly, via OPAL.
6630 if (!xics_on_xive() && !kvmhv_on_pseries() &&
6631 !local_paca->kvm_hstate.xics_phys) {
6632 struct device_node *np;
6634 np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
6636 pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
6640 /* presence of intc confirmed - node can be dropped again */
6645 init_default_hcalls();
6649 r = kvmppc_mmu_hv_init();
6653 if (kvmppc_radix_possible()) {
6654 r = kvmppc_radix_init();
6659 r = kvmppc_uvmem_init();
6661 pr_err("KVM-HV: kvmppc_uvmem_init failed %d\n", r);
6665 kvm_ops_hv.owner = THIS_MODULE;
6666 kvmppc_hv_ops = &kvm_ops_hv;
6671 kvmhv_nested_exit();
6672 kvmppc_radix_exit();
6677 static void kvmppc_book3s_exit_hv(void)
6679 kvmppc_uvmem_free();
6680 kvmppc_free_host_rm_ops();
6681 if (kvmppc_radix_possible())
6682 kvmppc_radix_exit();
6683 kvmppc_hv_ops = NULL;
6684 kvmhv_nested_exit();
6687 module_init(kvmppc_book3s_init_hv);
6688 module_exit(kvmppc_book3s_exit_hv);
6689 MODULE_DESCRIPTION("KVM on Book3S (POWER8 and later) in hypervisor mode");
6690 MODULE_LICENSE("GPL");
6691 MODULE_ALIAS_MISCDEV(KVM_MINOR);
6692 MODULE_ALIAS("devname:kvm");