1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
4 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
7 * Paul Mackerras <paulus@au1.ibm.com>
8 * Alexander Graf <agraf@suse.de>
9 * Kevin Wolf <mail@kevin-wolf.de>
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,
15 * by Alexander Graf <agraf@suse.de>.
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>
46 #include <asm/ftrace.h>
48 #include <asm/ppc-opcode.h>
49 #include <asm/asm-prototypes.h>
50 #include <asm/archrandom.h>
51 #include <asm/debug.h>
52 #include <asm/disassemble.h>
53 #include <asm/cputable.h>
54 #include <asm/cacheflush.h>
55 #include <linux/uaccess.h>
56 #include <asm/interrupt.h>
58 #include <asm/kvm_ppc.h>
59 #include <asm/kvm_book3s.h>
60 #include <asm/mmu_context.h>
61 #include <asm/lppaca.h>
63 #include <asm/processor.h>
64 #include <asm/cputhreads.h>
66 #include <asm/hvcall.h>
67 #include <asm/switch_to.h>
69 #include <asm/dbell.h>
71 #include <asm/pnv-pci.h>
76 #include <asm/hw_breakpoint.h>
77 #include <asm/kvm_book3s_uvmem.h>
78 #include <asm/ultravisor.h>
80 #include <asm/plpar_wrappers.h>
84 #define CREATE_TRACE_POINTS
87 /* #define EXIT_DEBUG */
88 /* #define EXIT_DEBUG_SIMPLE */
89 /* #define EXIT_DEBUG_INT */
91 /* Used to indicate that a guest page fault needs to be handled */
92 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
93 /* Used to indicate that a guest passthrough interrupt needs to be handled */
94 #define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
96 /* Used as a "null" value for timebase values */
97 #define TB_NIL (~(u64)0)
99 static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
101 static int dynamic_mt_modes = 6;
102 module_param(dynamic_mt_modes, int, 0644);
103 MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
104 static int target_smt_mode;
105 module_param(target_smt_mode, int, 0644);
106 MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
108 static bool one_vm_per_core;
109 module_param(one_vm_per_core, bool, S_IRUGO | S_IWUSR);
110 MODULE_PARM_DESC(one_vm_per_core, "Only run vCPUs from the same VM on a core (requires POWER8 or older)");
112 #ifdef CONFIG_KVM_XICS
113 static const struct kernel_param_ops module_param_ops = {
114 .set = param_set_int,
115 .get = param_get_int,
118 module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass, 0644);
119 MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
121 module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644);
122 MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
125 /* If set, guests are allowed to create and control nested guests */
126 static bool nested = true;
127 module_param(nested, bool, S_IRUGO | S_IWUSR);
128 MODULE_PARM_DESC(nested, "Enable nested virtualization (only on POWER9)");
130 static inline bool nesting_enabled(struct kvm *kvm)
132 return kvm->arch.nested_enable && kvm_is_radix(kvm);
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;
232 waitp = kvm_arch_vcpu_get_wait(vcpu);
233 if (rcuwait_wake_up(waitp))
234 ++vcpu->stat.generic.halt_wakeup;
236 cpu = READ_ONCE(vcpu->arch.thread_cpu);
237 if (cpu >= 0 && kvmppc_ipi_thread(cpu))
240 /* CPU points to the first thread of the core */
242 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
243 smp_send_reschedule(cpu);
247 * We use the vcpu_load/put functions to measure stolen time.
248 * Stolen time is counted as time when either the vcpu is able to
249 * run as part of a virtual core, but the task running the vcore
250 * is preempted or sleeping, or when the vcpu needs something done
251 * in the kernel by the task running the vcpu, but that task is
252 * preempted or sleeping. Those two things have to be counted
253 * separately, since one of the vcpu tasks will take on the job
254 * of running the core, and the other vcpu tasks in the vcore will
255 * sleep waiting for it to do that, but that sleep shouldn't count
258 * Hence we accumulate stolen time when the vcpu can run as part of
259 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
260 * needs its task to do other things in the kernel (for example,
261 * service a page fault) in busy_stolen. We don't accumulate
262 * stolen time for a vcore when it is inactive, or for a vcpu
263 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
264 * a misnomer; it means that the vcpu task is not executing in
265 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
266 * the kernel. We don't have any way of dividing up that time
267 * between time that the vcpu is genuinely stopped, time that
268 * the task is actively working on behalf of the vcpu, and time
269 * that the task is preempted, so we don't count any of it as
272 * Updates to busy_stolen are protected by arch.tbacct_lock;
273 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
274 * lock. The stolen times are measured in units of timebase ticks.
275 * (Note that the != TB_NIL checks below are purely defensive;
276 * they should never fail.)
279 static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
283 spin_lock_irqsave(&vc->stoltb_lock, flags);
284 vc->preempt_tb = mftb();
285 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
288 static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
292 spin_lock_irqsave(&vc->stoltb_lock, flags);
293 if (vc->preempt_tb != TB_NIL) {
294 vc->stolen_tb += mftb() - vc->preempt_tb;
295 vc->preempt_tb = TB_NIL;
297 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
300 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
302 struct kvmppc_vcore *vc = vcpu->arch.vcore;
306 * We can test vc->runner without taking the vcore lock,
307 * because only this task ever sets vc->runner to this
308 * vcpu, and once it is set to this vcpu, only this task
309 * ever sets it to NULL.
311 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
312 kvmppc_core_end_stolen(vc);
314 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
315 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
316 vcpu->arch.busy_preempt != TB_NIL) {
317 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
318 vcpu->arch.busy_preempt = TB_NIL;
320 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
323 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
325 struct kvmppc_vcore *vc = vcpu->arch.vcore;
328 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
329 kvmppc_core_start_stolen(vc);
331 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
332 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
333 vcpu->arch.busy_preempt = mftb();
334 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
337 static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
339 vcpu->arch.pvr = pvr;
342 /* Dummy value used in computing PCR value below */
343 #define PCR_ARCH_31 (PCR_ARCH_300 << 1)
345 static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
347 unsigned long host_pcr_bit = 0, guest_pcr_bit = 0;
348 struct kvmppc_vcore *vc = vcpu->arch.vcore;
350 /* We can (emulate) our own architecture version and anything older */
351 if (cpu_has_feature(CPU_FTR_ARCH_31))
352 host_pcr_bit = PCR_ARCH_31;
353 else if (cpu_has_feature(CPU_FTR_ARCH_300))
354 host_pcr_bit = PCR_ARCH_300;
355 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
356 host_pcr_bit = PCR_ARCH_207;
357 else if (cpu_has_feature(CPU_FTR_ARCH_206))
358 host_pcr_bit = PCR_ARCH_206;
360 host_pcr_bit = PCR_ARCH_205;
362 /* Determine lowest PCR bit needed to run guest in given PVR level */
363 guest_pcr_bit = host_pcr_bit;
365 switch (arch_compat) {
367 guest_pcr_bit = PCR_ARCH_205;
371 guest_pcr_bit = PCR_ARCH_206;
374 guest_pcr_bit = PCR_ARCH_207;
377 guest_pcr_bit = PCR_ARCH_300;
380 guest_pcr_bit = PCR_ARCH_31;
387 /* Check requested PCR bits don't exceed our capabilities */
388 if (guest_pcr_bit > host_pcr_bit)
391 spin_lock(&vc->lock);
392 vc->arch_compat = arch_compat;
394 * Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit
395 * Also set all reserved PCR bits
397 vc->pcr = (host_pcr_bit - guest_pcr_bit) | PCR_MASK;
398 spin_unlock(&vc->lock);
403 static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
407 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
408 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
409 vcpu->arch.regs.nip, vcpu->arch.shregs.msr, vcpu->arch.trap);
410 for (r = 0; r < 16; ++r)
411 pr_err("r%2d = %.16lx r%d = %.16lx\n",
412 r, kvmppc_get_gpr(vcpu, r),
413 r+16, kvmppc_get_gpr(vcpu, r+16));
414 pr_err("ctr = %.16lx lr = %.16lx\n",
415 vcpu->arch.regs.ctr, vcpu->arch.regs.link);
416 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
417 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
418 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
419 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
420 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
421 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
422 pr_err("cr = %.8lx xer = %.16lx dsisr = %.8x\n",
423 vcpu->arch.regs.ccr, vcpu->arch.regs.xer, vcpu->arch.shregs.dsisr);
424 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
425 pr_err("fault dar = %.16lx dsisr = %.8x\n",
426 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
427 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
428 for (r = 0; r < vcpu->arch.slb_max; ++r)
429 pr_err(" ESID = %.16llx VSID = %.16llx\n",
430 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
431 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
432 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
433 vcpu->arch.last_inst);
436 static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
438 return kvm_get_vcpu_by_id(kvm, id);
441 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
443 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
444 vpa->yield_count = cpu_to_be32(1);
447 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
448 unsigned long addr, unsigned long len)
450 /* check address is cacheline aligned */
451 if (addr & (L1_CACHE_BYTES - 1))
453 spin_lock(&vcpu->arch.vpa_update_lock);
454 if (v->next_gpa != addr || v->len != len) {
456 v->len = addr ? len : 0;
457 v->update_pending = 1;
459 spin_unlock(&vcpu->arch.vpa_update_lock);
463 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
472 static int vpa_is_registered(struct kvmppc_vpa *vpap)
474 if (vpap->update_pending)
475 return vpap->next_gpa != 0;
476 return vpap->pinned_addr != NULL;
479 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
481 unsigned long vcpuid, unsigned long vpa)
483 struct kvm *kvm = vcpu->kvm;
484 unsigned long len, nb;
486 struct kvm_vcpu *tvcpu;
489 struct kvmppc_vpa *vpap;
491 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
495 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
496 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
497 subfunc == H_VPA_REG_SLB) {
498 /* Registering new area - address must be cache-line aligned */
499 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
502 /* convert logical addr to kernel addr and read length */
503 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
506 if (subfunc == H_VPA_REG_VPA)
507 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
509 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
510 kvmppc_unpin_guest_page(kvm, va, vpa, false);
513 if (len > nb || len < sizeof(struct reg_vpa))
522 spin_lock(&tvcpu->arch.vpa_update_lock);
525 case H_VPA_REG_VPA: /* register VPA */
527 * The size of our lppaca is 1kB because of the way we align
528 * it for the guest to avoid crossing a 4kB boundary. We only
529 * use 640 bytes of the structure though, so we should accept
530 * clients that set a size of 640.
532 BUILD_BUG_ON(sizeof(struct lppaca) != 640);
533 if (len < sizeof(struct lppaca))
535 vpap = &tvcpu->arch.vpa;
539 case H_VPA_REG_DTL: /* register DTL */
540 if (len < sizeof(struct dtl_entry))
542 len -= len % sizeof(struct dtl_entry);
544 /* Check that they have previously registered a VPA */
546 if (!vpa_is_registered(&tvcpu->arch.vpa))
549 vpap = &tvcpu->arch.dtl;
553 case H_VPA_REG_SLB: /* register SLB shadow buffer */
554 /* Check that they have previously registered a VPA */
556 if (!vpa_is_registered(&tvcpu->arch.vpa))
559 vpap = &tvcpu->arch.slb_shadow;
563 case H_VPA_DEREG_VPA: /* deregister VPA */
564 /* Check they don't still have a DTL or SLB buf registered */
566 if (vpa_is_registered(&tvcpu->arch.dtl) ||
567 vpa_is_registered(&tvcpu->arch.slb_shadow))
570 vpap = &tvcpu->arch.vpa;
574 case H_VPA_DEREG_DTL: /* deregister DTL */
575 vpap = &tvcpu->arch.dtl;
579 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
580 vpap = &tvcpu->arch.slb_shadow;
586 vpap->next_gpa = vpa;
588 vpap->update_pending = 1;
591 spin_unlock(&tvcpu->arch.vpa_update_lock);
596 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
598 struct kvm *kvm = vcpu->kvm;
604 * We need to pin the page pointed to by vpap->next_gpa,
605 * but we can't call kvmppc_pin_guest_page under the lock
606 * as it does get_user_pages() and down_read(). So we
607 * have to drop the lock, pin the page, then get the lock
608 * again and check that a new area didn't get registered
612 gpa = vpap->next_gpa;
613 spin_unlock(&vcpu->arch.vpa_update_lock);
617 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
618 spin_lock(&vcpu->arch.vpa_update_lock);
619 if (gpa == vpap->next_gpa)
621 /* sigh... unpin that one and try again */
623 kvmppc_unpin_guest_page(kvm, va, gpa, false);
626 vpap->update_pending = 0;
627 if (va && nb < vpap->len) {
629 * If it's now too short, it must be that userspace
630 * has changed the mappings underlying guest memory,
631 * so unregister the region.
633 kvmppc_unpin_guest_page(kvm, va, gpa, false);
636 if (vpap->pinned_addr)
637 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
640 vpap->pinned_addr = va;
643 vpap->pinned_end = va + vpap->len;
646 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
648 if (!(vcpu->arch.vpa.update_pending ||
649 vcpu->arch.slb_shadow.update_pending ||
650 vcpu->arch.dtl.update_pending))
653 spin_lock(&vcpu->arch.vpa_update_lock);
654 if (vcpu->arch.vpa.update_pending) {
655 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
656 if (vcpu->arch.vpa.pinned_addr)
657 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
659 if (vcpu->arch.dtl.update_pending) {
660 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
661 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
662 vcpu->arch.dtl_index = 0;
664 if (vcpu->arch.slb_shadow.update_pending)
665 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
666 spin_unlock(&vcpu->arch.vpa_update_lock);
670 * Return the accumulated stolen time for the vcore up until `now'.
671 * The caller should hold the vcore lock.
673 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
678 spin_lock_irqsave(&vc->stoltb_lock, flags);
680 if (vc->vcore_state != VCORE_INACTIVE &&
681 vc->preempt_tb != TB_NIL)
682 p += now - vc->preempt_tb;
683 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
687 static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
688 struct kvmppc_vcore *vc)
690 struct dtl_entry *dt;
692 unsigned long stolen;
693 unsigned long core_stolen;
697 dt = vcpu->arch.dtl_ptr;
698 vpa = vcpu->arch.vpa.pinned_addr;
700 core_stolen = vcore_stolen_time(vc, now);
701 stolen = core_stolen - vcpu->arch.stolen_logged;
702 vcpu->arch.stolen_logged = core_stolen;
703 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
704 stolen += vcpu->arch.busy_stolen;
705 vcpu->arch.busy_stolen = 0;
706 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
709 memset(dt, 0, sizeof(struct dtl_entry));
710 dt->dispatch_reason = 7;
711 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
712 dt->timebase = cpu_to_be64(now + vc->tb_offset);
713 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
714 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
715 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
717 if (dt == vcpu->arch.dtl.pinned_end)
718 dt = vcpu->arch.dtl.pinned_addr;
719 vcpu->arch.dtl_ptr = dt;
720 /* order writing *dt vs. writing vpa->dtl_idx */
722 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
723 vcpu->arch.dtl.dirty = true;
726 /* See if there is a doorbell interrupt pending for a vcpu */
727 static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu)
730 struct kvmppc_vcore *vc;
732 if (vcpu->arch.doorbell_request)
735 * Ensure that the read of vcore->dpdes comes after the read
736 * of vcpu->doorbell_request. This barrier matches the
737 * smp_wmb() in kvmppc_guest_entry_inject().
740 vc = vcpu->arch.vcore;
741 thr = vcpu->vcpu_id - vc->first_vcpuid;
742 return !!(vc->dpdes & (1 << thr));
745 static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
747 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
749 if ((!vcpu->arch.vcore->arch_compat) &&
750 cpu_has_feature(CPU_FTR_ARCH_207S))
755 static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
756 unsigned long resource, unsigned long value1,
757 unsigned long value2)
760 case H_SET_MODE_RESOURCE_SET_CIABR:
761 if (!kvmppc_power8_compatible(vcpu))
766 return H_UNSUPPORTED_FLAG_START;
767 /* Guests can't breakpoint the hypervisor */
768 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
770 vcpu->arch.ciabr = value1;
772 case H_SET_MODE_RESOURCE_SET_DAWR0:
773 if (!kvmppc_power8_compatible(vcpu))
775 if (!ppc_breakpoint_available())
778 return H_UNSUPPORTED_FLAG_START;
779 if (value2 & DABRX_HYP)
781 vcpu->arch.dawr0 = value1;
782 vcpu->arch.dawrx0 = value2;
784 case H_SET_MODE_RESOURCE_SET_DAWR1:
785 if (!kvmppc_power8_compatible(vcpu))
787 if (!ppc_breakpoint_available())
789 if (!cpu_has_feature(CPU_FTR_DAWR1))
791 if (!vcpu->kvm->arch.dawr1_enabled)
794 return H_UNSUPPORTED_FLAG_START;
795 if (value2 & DABRX_HYP)
797 vcpu->arch.dawr1 = value1;
798 vcpu->arch.dawrx1 = value2;
800 case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
802 * KVM does not support mflags=2 (AIL=2) and AIL=1 is reserved.
803 * Keep this in synch with kvmppc_filter_guest_lpcr_hv.
805 if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) &&
806 kvmhv_vcpu_is_radix(vcpu) && mflags == 3)
807 return H_UNSUPPORTED_FLAG_START;
814 /* Copy guest memory in place - must reside within a single memslot */
815 static int kvmppc_copy_guest(struct kvm *kvm, gpa_t to, gpa_t from,
818 struct kvm_memory_slot *to_memslot = NULL;
819 struct kvm_memory_slot *from_memslot = NULL;
820 unsigned long to_addr, from_addr;
823 /* Get HPA for from address */
824 from_memslot = gfn_to_memslot(kvm, from >> PAGE_SHIFT);
827 if ((from + len) >= ((from_memslot->base_gfn + from_memslot->npages)
830 from_addr = gfn_to_hva_memslot(from_memslot, from >> PAGE_SHIFT);
831 if (kvm_is_error_hva(from_addr))
833 from_addr |= (from & (PAGE_SIZE - 1));
835 /* Get HPA for to address */
836 to_memslot = gfn_to_memslot(kvm, to >> PAGE_SHIFT);
839 if ((to + len) >= ((to_memslot->base_gfn + to_memslot->npages)
842 to_addr = gfn_to_hva_memslot(to_memslot, to >> PAGE_SHIFT);
843 if (kvm_is_error_hva(to_addr))
845 to_addr |= (to & (PAGE_SIZE - 1));
848 r = raw_copy_in_user((void __user *)to_addr, (void __user *)from_addr,
852 mark_page_dirty(kvm, to >> PAGE_SHIFT);
856 static long kvmppc_h_page_init(struct kvm_vcpu *vcpu, unsigned long flags,
857 unsigned long dest, unsigned long src)
859 u64 pg_sz = SZ_4K; /* 4K page size */
860 u64 pg_mask = SZ_4K - 1;
863 /* Check for invalid flags (H_PAGE_SET_LOANED covers all CMO flags) */
864 if (flags & ~(H_ICACHE_INVALIDATE | H_ICACHE_SYNCHRONIZE |
865 H_ZERO_PAGE | H_COPY_PAGE | H_PAGE_SET_LOANED))
868 /* dest (and src if copy_page flag set) must be page aligned */
869 if ((dest & pg_mask) || ((flags & H_COPY_PAGE) && (src & pg_mask)))
872 /* zero and/or copy the page as determined by the flags */
873 if (flags & H_COPY_PAGE) {
874 ret = kvmppc_copy_guest(vcpu->kvm, dest, src, pg_sz);
877 } else if (flags & H_ZERO_PAGE) {
878 ret = kvm_clear_guest(vcpu->kvm, dest, pg_sz);
883 /* We can ignore the remaining flags */
888 static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
890 struct kvmppc_vcore *vcore = target->arch.vcore;
893 * We expect to have been called by the real mode handler
894 * (kvmppc_rm_h_confer()) which would have directly returned
895 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
896 * have useful work to do and should not confer) so we don't
899 * In the case of the P9 single vcpu per vcore case, the real
900 * mode handler is not called but no other threads are in the
904 spin_lock(&vcore->lock);
905 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
906 vcore->vcore_state != VCORE_INACTIVE &&
908 target = vcore->runner;
909 spin_unlock(&vcore->lock);
911 return kvm_vcpu_yield_to(target);
914 static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
917 struct lppaca *lppaca;
919 spin_lock(&vcpu->arch.vpa_update_lock);
920 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
922 yield_count = be32_to_cpu(lppaca->yield_count);
923 spin_unlock(&vcpu->arch.vpa_update_lock);
928 * H_RPT_INVALIDATE hcall handler for nested guests.
930 * Handles only nested process-scoped invalidation requests in L0.
932 static int kvmppc_nested_h_rpt_invalidate(struct kvm_vcpu *vcpu)
934 unsigned long type = kvmppc_get_gpr(vcpu, 6);
935 unsigned long pid, pg_sizes, start, end;
938 * The partition-scoped invalidations aren't handled here in L0.
940 if (type & H_RPTI_TYPE_NESTED)
943 pid = kvmppc_get_gpr(vcpu, 4);
944 pg_sizes = kvmppc_get_gpr(vcpu, 7);
945 start = kvmppc_get_gpr(vcpu, 8);
946 end = kvmppc_get_gpr(vcpu, 9);
948 do_h_rpt_invalidate_prt(pid, vcpu->arch.nested->shadow_lpid,
949 type, pg_sizes, start, end);
951 kvmppc_set_gpr(vcpu, 3, H_SUCCESS);
955 static long kvmppc_h_rpt_invalidate(struct kvm_vcpu *vcpu,
956 unsigned long id, unsigned long target,
957 unsigned long type, unsigned long pg_sizes,
958 unsigned long start, unsigned long end)
960 if (!kvm_is_radix(vcpu->kvm))
961 return H_UNSUPPORTED;
967 * Partition-scoped invalidation for nested guests.
969 if (type & H_RPTI_TYPE_NESTED) {
970 if (!nesting_enabled(vcpu->kvm))
973 /* Support only cores as target */
974 if (target != H_RPTI_TARGET_CMMU)
977 return do_h_rpt_invalidate_pat(vcpu, id, type, pg_sizes,
982 * Process-scoped invalidation for L1 guests.
984 do_h_rpt_invalidate_prt(id, vcpu->kvm->arch.lpid,
985 type, pg_sizes, start, end);
989 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
991 struct kvm *kvm = vcpu->kvm;
992 unsigned long req = kvmppc_get_gpr(vcpu, 3);
993 unsigned long target, ret = H_SUCCESS;
995 struct kvm_vcpu *tvcpu;
998 if (req <= MAX_HCALL_OPCODE &&
999 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
1004 ret = kvmppc_h_remove(vcpu, kvmppc_get_gpr(vcpu, 4),
1005 kvmppc_get_gpr(vcpu, 5),
1006 kvmppc_get_gpr(vcpu, 6));
1007 if (ret == H_TOO_HARD)
1011 ret = kvmppc_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
1012 kvmppc_get_gpr(vcpu, 5),
1013 kvmppc_get_gpr(vcpu, 6),
1014 kvmppc_get_gpr(vcpu, 7));
1015 if (ret == H_TOO_HARD)
1019 ret = kvmppc_h_read(vcpu, kvmppc_get_gpr(vcpu, 4),
1020 kvmppc_get_gpr(vcpu, 5));
1021 if (ret == H_TOO_HARD)
1025 ret = kvmppc_h_clear_mod(vcpu, kvmppc_get_gpr(vcpu, 4),
1026 kvmppc_get_gpr(vcpu, 5));
1027 if (ret == H_TOO_HARD)
1031 ret = kvmppc_h_clear_ref(vcpu, kvmppc_get_gpr(vcpu, 4),
1032 kvmppc_get_gpr(vcpu, 5));
1033 if (ret == H_TOO_HARD)
1037 ret = kvmppc_h_protect(vcpu, kvmppc_get_gpr(vcpu, 4),
1038 kvmppc_get_gpr(vcpu, 5),
1039 kvmppc_get_gpr(vcpu, 6));
1040 if (ret == H_TOO_HARD)
1044 ret = kvmppc_h_bulk_remove(vcpu);
1045 if (ret == H_TOO_HARD)
1052 target = kvmppc_get_gpr(vcpu, 4);
1053 tvcpu = kvmppc_find_vcpu(kvm, target);
1058 tvcpu->arch.prodded = 1;
1060 if (tvcpu->arch.ceded)
1061 kvmppc_fast_vcpu_kick_hv(tvcpu);
1064 target = kvmppc_get_gpr(vcpu, 4);
1067 tvcpu = kvmppc_find_vcpu(kvm, target);
1072 yield_count = kvmppc_get_gpr(vcpu, 5);
1073 if (kvmppc_get_yield_count(tvcpu) != yield_count)
1075 kvm_arch_vcpu_yield_to(tvcpu);
1077 case H_REGISTER_VPA:
1078 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
1079 kvmppc_get_gpr(vcpu, 5),
1080 kvmppc_get_gpr(vcpu, 6));
1083 if (list_empty(&kvm->arch.rtas_tokens))
1086 idx = srcu_read_lock(&kvm->srcu);
1087 rc = kvmppc_rtas_hcall(vcpu);
1088 srcu_read_unlock(&kvm->srcu, idx);
1095 /* Send the error out to userspace via KVM_RUN */
1097 case H_LOGICAL_CI_LOAD:
1098 ret = kvmppc_h_logical_ci_load(vcpu);
1099 if (ret == H_TOO_HARD)
1102 case H_LOGICAL_CI_STORE:
1103 ret = kvmppc_h_logical_ci_store(vcpu);
1104 if (ret == H_TOO_HARD)
1108 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
1109 kvmppc_get_gpr(vcpu, 5),
1110 kvmppc_get_gpr(vcpu, 6),
1111 kvmppc_get_gpr(vcpu, 7));
1112 if (ret == H_TOO_HARD)
1121 if (kvmppc_xics_enabled(vcpu)) {
1122 if (xics_on_xive()) {
1123 ret = H_NOT_AVAILABLE;
1124 return RESUME_GUEST;
1126 ret = kvmppc_xics_hcall(vcpu, req);
1131 ret = kvmppc_h_set_dabr(vcpu, kvmppc_get_gpr(vcpu, 4));
1134 ret = kvmppc_h_set_xdabr(vcpu, kvmppc_get_gpr(vcpu, 4),
1135 kvmppc_get_gpr(vcpu, 5));
1137 #ifdef CONFIG_SPAPR_TCE_IOMMU
1139 ret = kvmppc_h_get_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1140 kvmppc_get_gpr(vcpu, 5));
1141 if (ret == H_TOO_HARD)
1145 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1146 kvmppc_get_gpr(vcpu, 5),
1147 kvmppc_get_gpr(vcpu, 6));
1148 if (ret == H_TOO_HARD)
1151 case H_PUT_TCE_INDIRECT:
1152 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
1153 kvmppc_get_gpr(vcpu, 5),
1154 kvmppc_get_gpr(vcpu, 6),
1155 kvmppc_get_gpr(vcpu, 7));
1156 if (ret == H_TOO_HARD)
1160 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1161 kvmppc_get_gpr(vcpu, 5),
1162 kvmppc_get_gpr(vcpu, 6),
1163 kvmppc_get_gpr(vcpu, 7));
1164 if (ret == H_TOO_HARD)
1169 if (!arch_get_random_seed_long(&vcpu->arch.regs.gpr[4]))
1172 case H_RPT_INVALIDATE:
1173 ret = kvmppc_h_rpt_invalidate(vcpu, kvmppc_get_gpr(vcpu, 4),
1174 kvmppc_get_gpr(vcpu, 5),
1175 kvmppc_get_gpr(vcpu, 6),
1176 kvmppc_get_gpr(vcpu, 7),
1177 kvmppc_get_gpr(vcpu, 8),
1178 kvmppc_get_gpr(vcpu, 9));
1181 case H_SET_PARTITION_TABLE:
1183 if (nesting_enabled(kvm))
1184 ret = kvmhv_set_partition_table(vcpu);
1186 case H_ENTER_NESTED:
1188 if (!nesting_enabled(kvm))
1190 ret = kvmhv_enter_nested_guest(vcpu);
1191 if (ret == H_INTERRUPT) {
1192 kvmppc_set_gpr(vcpu, 3, 0);
1193 vcpu->arch.hcall_needed = 0;
1195 } else if (ret == H_TOO_HARD) {
1196 kvmppc_set_gpr(vcpu, 3, 0);
1197 vcpu->arch.hcall_needed = 0;
1201 case H_TLB_INVALIDATE:
1203 if (nesting_enabled(kvm))
1204 ret = kvmhv_do_nested_tlbie(vcpu);
1206 case H_COPY_TOFROM_GUEST:
1208 if (nesting_enabled(kvm))
1209 ret = kvmhv_copy_tofrom_guest_nested(vcpu);
1212 ret = kvmppc_h_page_init(vcpu, kvmppc_get_gpr(vcpu, 4),
1213 kvmppc_get_gpr(vcpu, 5),
1214 kvmppc_get_gpr(vcpu, 6));
1217 ret = H_UNSUPPORTED;
1218 if (kvmppc_get_srr1(vcpu) & MSR_S)
1219 ret = kvmppc_h_svm_page_in(kvm,
1220 kvmppc_get_gpr(vcpu, 4),
1221 kvmppc_get_gpr(vcpu, 5),
1222 kvmppc_get_gpr(vcpu, 6));
1224 case H_SVM_PAGE_OUT:
1225 ret = H_UNSUPPORTED;
1226 if (kvmppc_get_srr1(vcpu) & MSR_S)
1227 ret = kvmppc_h_svm_page_out(kvm,
1228 kvmppc_get_gpr(vcpu, 4),
1229 kvmppc_get_gpr(vcpu, 5),
1230 kvmppc_get_gpr(vcpu, 6));
1232 case H_SVM_INIT_START:
1233 ret = H_UNSUPPORTED;
1234 if (kvmppc_get_srr1(vcpu) & MSR_S)
1235 ret = kvmppc_h_svm_init_start(kvm);
1237 case H_SVM_INIT_DONE:
1238 ret = H_UNSUPPORTED;
1239 if (kvmppc_get_srr1(vcpu) & MSR_S)
1240 ret = kvmppc_h_svm_init_done(kvm);
1242 case H_SVM_INIT_ABORT:
1244 * Even if that call is made by the Ultravisor, the SSR1 value
1245 * is the guest context one, with the secure bit clear as it has
1246 * not yet been secured. So we can't check it here.
1247 * Instead the kvm->arch.secure_guest flag is checked inside
1248 * kvmppc_h_svm_init_abort().
1250 ret = kvmppc_h_svm_init_abort(kvm);
1256 WARN_ON_ONCE(ret == H_TOO_HARD);
1257 kvmppc_set_gpr(vcpu, 3, ret);
1258 vcpu->arch.hcall_needed = 0;
1259 return RESUME_GUEST;
1263 * Handle H_CEDE in the P9 path where we don't call the real-mode hcall
1264 * handlers in book3s_hv_rmhandlers.S.
1266 * This has to be done early, not in kvmppc_pseries_do_hcall(), so
1267 * that the cede logic in kvmppc_run_single_vcpu() works properly.
1269 static void kvmppc_cede(struct kvm_vcpu *vcpu)
1271 vcpu->arch.shregs.msr |= MSR_EE;
1272 vcpu->arch.ceded = 1;
1274 if (vcpu->arch.prodded) {
1275 vcpu->arch.prodded = 0;
1277 vcpu->arch.ceded = 0;
1281 static int kvmppc_hcall_impl_hv(unsigned long cmd)
1287 case H_REGISTER_VPA:
1289 case H_LOGICAL_CI_LOAD:
1290 case H_LOGICAL_CI_STORE:
1291 #ifdef CONFIG_KVM_XICS
1300 case H_RPT_INVALIDATE:
1304 /* See if it's in the real-mode table */
1305 return kvmppc_hcall_impl_hv_realmode(cmd);
1308 static int kvmppc_emulate_debug_inst(struct kvm_vcpu *vcpu)
1312 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
1315 * Fetch failed, so return to guest and
1316 * try executing it again.
1318 return RESUME_GUEST;
1321 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
1322 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
1323 vcpu->run->debug.arch.address = kvmppc_get_pc(vcpu);
1326 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1327 return RESUME_GUEST;
1331 static void do_nothing(void *x)
1335 static unsigned long kvmppc_read_dpdes(struct kvm_vcpu *vcpu)
1337 int thr, cpu, pcpu, nthreads;
1339 unsigned long dpdes;
1341 nthreads = vcpu->kvm->arch.emul_smt_mode;
1343 cpu = vcpu->vcpu_id & ~(nthreads - 1);
1344 for (thr = 0; thr < nthreads; ++thr, ++cpu) {
1345 v = kvmppc_find_vcpu(vcpu->kvm, cpu);
1349 * If the vcpu is currently running on a physical cpu thread,
1350 * interrupt it in order to pull it out of the guest briefly,
1351 * which will update its vcore->dpdes value.
1353 pcpu = READ_ONCE(v->cpu);
1355 smp_call_function_single(pcpu, do_nothing, NULL, 1);
1356 if (kvmppc_doorbell_pending(v))
1363 * On POWER9, emulate doorbell-related instructions in order to
1364 * give the guest the illusion of running on a multi-threaded core.
1365 * The instructions emulated are msgsndp, msgclrp, mfspr TIR,
1368 static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)
1372 struct kvm *kvm = vcpu->kvm;
1373 struct kvm_vcpu *tvcpu;
1375 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst) != EMULATE_DONE)
1376 return RESUME_GUEST;
1377 if (get_op(inst) != 31)
1378 return EMULATE_FAIL;
1380 thr = vcpu->vcpu_id & (kvm->arch.emul_smt_mode - 1);
1381 switch (get_xop(inst)) {
1382 case OP_31_XOP_MSGSNDP:
1383 arg = kvmppc_get_gpr(vcpu, rb);
1384 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
1387 if (arg >= kvm->arch.emul_smt_mode)
1389 tvcpu = kvmppc_find_vcpu(kvm, vcpu->vcpu_id - thr + arg);
1392 if (!tvcpu->arch.doorbell_request) {
1393 tvcpu->arch.doorbell_request = 1;
1394 kvmppc_fast_vcpu_kick_hv(tvcpu);
1397 case OP_31_XOP_MSGCLRP:
1398 arg = kvmppc_get_gpr(vcpu, rb);
1399 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
1401 vcpu->arch.vcore->dpdes = 0;
1402 vcpu->arch.doorbell_request = 0;
1404 case OP_31_XOP_MFSPR:
1405 switch (get_sprn(inst)) {
1410 arg = kvmppc_read_dpdes(vcpu);
1413 return EMULATE_FAIL;
1415 kvmppc_set_gpr(vcpu, get_rt(inst), arg);
1418 return EMULATE_FAIL;
1420 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
1421 return RESUME_GUEST;
1424 static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,
1425 struct task_struct *tsk)
1427 struct kvm_run *run = vcpu->run;
1428 int r = RESUME_HOST;
1430 vcpu->stat.sum_exits++;
1433 * This can happen if an interrupt occurs in the last stages
1434 * of guest entry or the first stages of guest exit (i.e. after
1435 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
1436 * and before setting it to KVM_GUEST_MODE_HOST_HV).
1437 * That can happen due to a bug, or due to a machine check
1438 * occurring at just the wrong time.
1440 if (vcpu->arch.shregs.msr & MSR_HV) {
1441 printk(KERN_EMERG "KVM trap in HV mode!\n");
1442 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1443 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1444 vcpu->arch.shregs.msr);
1445 kvmppc_dump_regs(vcpu);
1446 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1447 run->hw.hardware_exit_reason = vcpu->arch.trap;
1450 run->exit_reason = KVM_EXIT_UNKNOWN;
1451 run->ready_for_interrupt_injection = 1;
1452 switch (vcpu->arch.trap) {
1453 /* We're good on these - the host merely wanted to get our attention */
1454 case BOOK3S_INTERRUPT_HV_DECREMENTER:
1455 vcpu->stat.dec_exits++;
1458 case BOOK3S_INTERRUPT_EXTERNAL:
1459 case BOOK3S_INTERRUPT_H_DOORBELL:
1460 case BOOK3S_INTERRUPT_H_VIRT:
1461 vcpu->stat.ext_intr_exits++;
1464 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
1465 case BOOK3S_INTERRUPT_HMI:
1466 case BOOK3S_INTERRUPT_PERFMON:
1467 case BOOK3S_INTERRUPT_SYSTEM_RESET:
1470 case BOOK3S_INTERRUPT_MACHINE_CHECK: {
1471 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
1472 DEFAULT_RATELIMIT_BURST);
1474 * Print the MCE event to host console. Ratelimit so the guest
1475 * can't flood the host log.
1477 if (__ratelimit(&rs))
1478 machine_check_print_event_info(&vcpu->arch.mce_evt,false, true);
1481 * If the guest can do FWNMI, exit to userspace so it can
1482 * deliver a FWNMI to the guest.
1483 * Otherwise we synthesize a machine check for the guest
1484 * so that it knows that the machine check occurred.
1486 if (!vcpu->kvm->arch.fwnmi_enabled) {
1487 ulong flags = vcpu->arch.shregs.msr & 0x083c0000;
1488 kvmppc_core_queue_machine_check(vcpu, flags);
1493 /* Exit to guest with KVM_EXIT_NMI as exit reason */
1494 run->exit_reason = KVM_EXIT_NMI;
1495 run->hw.hardware_exit_reason = vcpu->arch.trap;
1496 /* Clear out the old NMI status from run->flags */
1497 run->flags &= ~KVM_RUN_PPC_NMI_DISP_MASK;
1498 /* Now set the NMI status */
1499 if (vcpu->arch.mce_evt.disposition == MCE_DISPOSITION_RECOVERED)
1500 run->flags |= KVM_RUN_PPC_NMI_DISP_FULLY_RECOV;
1502 run->flags |= KVM_RUN_PPC_NMI_DISP_NOT_RECOV;
1507 case BOOK3S_INTERRUPT_PROGRAM:
1511 * Normally program interrupts are delivered directly
1512 * to the guest by the hardware, but we can get here
1513 * as a result of a hypervisor emulation interrupt
1514 * (e40) getting turned into a 700 by BML RTAS.
1516 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
1517 kvmppc_core_queue_program(vcpu, flags);
1521 case BOOK3S_INTERRUPT_SYSCALL:
1525 if (unlikely(vcpu->arch.shregs.msr & MSR_PR)) {
1527 * Guest userspace executed sc 1. This can only be
1528 * reached by the P9 path because the old path
1529 * handles this case in realmode hcall handlers.
1531 if (!kvmhv_vcpu_is_radix(vcpu)) {
1533 * A guest could be running PR KVM, so this
1534 * may be a PR KVM hcall. It must be reflected
1535 * to the guest kernel as a sc interrupt.
1537 kvmppc_core_queue_syscall(vcpu);
1540 * Radix guests can not run PR KVM or nested HV
1541 * hash guests which might run PR KVM, so this
1542 * is always a privilege fault. Send a program
1543 * check to guest kernel.
1545 kvmppc_core_queue_program(vcpu, SRR1_PROGPRIV);
1552 * hcall - gather args and set exit_reason. This will next be
1553 * handled by kvmppc_pseries_do_hcall which may be able to deal
1554 * with it and resume guest, or may punt to userspace.
1556 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
1557 for (i = 0; i < 9; ++i)
1558 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
1559 run->exit_reason = KVM_EXIT_PAPR_HCALL;
1560 vcpu->arch.hcall_needed = 1;
1565 * We get these next two if the guest accesses a page which it thinks
1566 * it has mapped but which is not actually present, either because
1567 * it is for an emulated I/O device or because the corresonding
1568 * host page has been paged out.
1570 * Any other HDSI/HISI interrupts have been handled already for P7/8
1571 * guests. For POWER9 hash guests not using rmhandlers, basic hash
1572 * fault handling is done here.
1574 case BOOK3S_INTERRUPT_H_DATA_STORAGE: {
1578 if (vcpu->arch.fault_dsisr == HDSISR_CANARY) {
1579 r = RESUME_GUEST; /* Just retry if it's the canary */
1583 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
1585 * Radix doesn't require anything, and pre-ISAv3.0 hash
1586 * already attempted to handle this in rmhandlers. The
1587 * hash fault handling below is v3 only (it uses ASDR
1590 r = RESUME_PAGE_FAULT;
1594 if (!(vcpu->arch.fault_dsisr & (DSISR_NOHPTE | DSISR_PROTFAULT))) {
1595 kvmppc_core_queue_data_storage(vcpu,
1596 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1601 if (!(vcpu->arch.shregs.msr & MSR_DR))
1602 vsid = vcpu->kvm->arch.vrma_slb_v;
1604 vsid = vcpu->arch.fault_gpa;
1606 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
1607 vsid, vcpu->arch.fault_dsisr, true);
1610 } else if (err == -1 || err == -2) {
1611 r = RESUME_PAGE_FAULT;
1613 kvmppc_core_queue_data_storage(vcpu,
1614 vcpu->arch.fault_dar, err);
1619 case BOOK3S_INTERRUPT_H_INST_STORAGE: {
1623 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
1624 vcpu->arch.fault_dsisr = vcpu->arch.shregs.msr &
1625 DSISR_SRR1_MATCH_64S;
1626 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
1628 * Radix doesn't require anything, and pre-ISAv3.0 hash
1629 * already attempted to handle this in rmhandlers. The
1630 * hash fault handling below is v3 only (it uses ASDR
1633 if (vcpu->arch.shregs.msr & HSRR1_HISI_WRITE)
1634 vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
1635 r = RESUME_PAGE_FAULT;
1639 if (!(vcpu->arch.fault_dsisr & SRR1_ISI_NOPT)) {
1640 kvmppc_core_queue_inst_storage(vcpu,
1641 vcpu->arch.fault_dsisr);
1646 if (!(vcpu->arch.shregs.msr & MSR_IR))
1647 vsid = vcpu->kvm->arch.vrma_slb_v;
1649 vsid = vcpu->arch.fault_gpa;
1651 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
1652 vsid, vcpu->arch.fault_dsisr, false);
1655 } else if (err == -1) {
1656 r = RESUME_PAGE_FAULT;
1658 kvmppc_core_queue_inst_storage(vcpu, err);
1665 * This occurs if the guest executes an illegal instruction.
1666 * If the guest debug is disabled, generate a program interrupt
1667 * to the guest. If guest debug is enabled, we need to check
1668 * whether the instruction is a software breakpoint instruction.
1669 * Accordingly return to Guest or Host.
1671 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
1672 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
1673 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
1674 swab32(vcpu->arch.emul_inst) :
1675 vcpu->arch.emul_inst;
1676 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
1677 r = kvmppc_emulate_debug_inst(vcpu);
1679 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1684 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1685 case BOOK3S_INTERRUPT_HV_SOFTPATCH:
1687 * This occurs for various TM-related instructions that
1688 * we need to emulate on POWER9 DD2.2. We have already
1689 * handled the cases where the guest was in real-suspend
1690 * mode and was transitioning to transactional state.
1692 r = kvmhv_p9_tm_emulation(vcpu);
1695 fallthrough; /* go to facility unavailable handler */
1699 * This occurs if the guest (kernel or userspace), does something that
1700 * is prohibited by HFSCR.
1701 * On POWER9, this could be a doorbell instruction that we need
1703 * Otherwise, we just generate a program interrupt to the guest.
1705 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
1707 if (((vcpu->arch.hfscr >> 56) == FSCR_MSGP_LG) &&
1708 cpu_has_feature(CPU_FTR_ARCH_300))
1709 r = kvmppc_emulate_doorbell_instr(vcpu);
1710 if (r == EMULATE_FAIL) {
1711 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1716 case BOOK3S_INTERRUPT_HV_RM_HARD:
1717 r = RESUME_PASSTHROUGH;
1720 kvmppc_dump_regs(vcpu);
1721 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1722 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1723 vcpu->arch.shregs.msr);
1724 run->hw.hardware_exit_reason = vcpu->arch.trap;
1732 static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)
1737 vcpu->stat.sum_exits++;
1740 * This can happen if an interrupt occurs in the last stages
1741 * of guest entry or the first stages of guest exit (i.e. after
1742 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
1743 * and before setting it to KVM_GUEST_MODE_HOST_HV).
1744 * That can happen due to a bug, or due to a machine check
1745 * occurring at just the wrong time.
1747 if (vcpu->arch.shregs.msr & MSR_HV) {
1748 pr_emerg("KVM trap in HV mode while nested!\n");
1749 pr_emerg("trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1750 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1751 vcpu->arch.shregs.msr);
1752 kvmppc_dump_regs(vcpu);
1755 switch (vcpu->arch.trap) {
1756 /* We're good on these - the host merely wanted to get our attention */
1757 case BOOK3S_INTERRUPT_HV_DECREMENTER:
1758 vcpu->stat.dec_exits++;
1761 case BOOK3S_INTERRUPT_EXTERNAL:
1762 vcpu->stat.ext_intr_exits++;
1765 case BOOK3S_INTERRUPT_H_DOORBELL:
1766 case BOOK3S_INTERRUPT_H_VIRT:
1767 vcpu->stat.ext_intr_exits++;
1770 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
1771 case BOOK3S_INTERRUPT_HMI:
1772 case BOOK3S_INTERRUPT_PERFMON:
1773 case BOOK3S_INTERRUPT_SYSTEM_RESET:
1776 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1778 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
1779 DEFAULT_RATELIMIT_BURST);
1780 /* Pass the machine check to the L1 guest */
1782 /* Print the MCE event to host console. */
1783 if (__ratelimit(&rs))
1784 machine_check_print_event_info(&vcpu->arch.mce_evt, false, true);
1788 * We get these next two if the guest accesses a page which it thinks
1789 * it has mapped but which is not actually present, either because
1790 * it is for an emulated I/O device or because the corresonding
1791 * host page has been paged out.
1793 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
1794 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1795 r = kvmhv_nested_page_fault(vcpu);
1796 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1798 case BOOK3S_INTERRUPT_H_INST_STORAGE:
1799 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
1800 vcpu->arch.fault_dsisr = kvmppc_get_msr(vcpu) &
1801 DSISR_SRR1_MATCH_64S;
1802 if (vcpu->arch.shregs.msr & HSRR1_HISI_WRITE)
1803 vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
1804 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1805 r = kvmhv_nested_page_fault(vcpu);
1806 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1809 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1810 case BOOK3S_INTERRUPT_HV_SOFTPATCH:
1812 * This occurs for various TM-related instructions that
1813 * we need to emulate on POWER9 DD2.2. We have already
1814 * handled the cases where the guest was in real-suspend
1815 * mode and was transitioning to transactional state.
1817 r = kvmhv_p9_tm_emulation(vcpu);
1820 fallthrough; /* go to facility unavailable handler */
1823 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: {
1824 u64 cause = vcpu->arch.hfscr >> 56;
1827 * Only pass HFU interrupts to the L1 if the facility is
1828 * permitted but disabled by the L1's HFSCR, otherwise
1829 * the interrupt does not make sense to the L1 so turn
1832 if (!(vcpu->arch.hfscr_permitted & (1UL << cause)) ||
1833 (vcpu->arch.nested_hfscr & (1UL << cause))) {
1834 vcpu->arch.trap = BOOK3S_INTERRUPT_H_EMUL_ASSIST;
1837 * If the fetch failed, return to guest and
1838 * try executing it again.
1840 r = kvmppc_get_last_inst(vcpu, INST_GENERIC,
1841 &vcpu->arch.emul_inst);
1842 if (r != EMULATE_DONE)
1853 case BOOK3S_INTERRUPT_HV_RM_HARD:
1854 vcpu->arch.trap = 0;
1856 if (!xics_on_xive())
1857 kvmppc_xics_rm_complete(vcpu, 0);
1859 case BOOK3S_INTERRUPT_SYSCALL:
1861 unsigned long req = kvmppc_get_gpr(vcpu, 3);
1864 * The H_RPT_INVALIDATE hcalls issued by nested
1865 * guests for process-scoped invalidations when
1866 * GTSE=0, are handled here in L0.
1868 if (req == H_RPT_INVALIDATE) {
1869 r = kvmppc_nested_h_rpt_invalidate(vcpu);
1884 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
1885 struct kvm_sregs *sregs)
1889 memset(sregs, 0, sizeof(struct kvm_sregs));
1890 sregs->pvr = vcpu->arch.pvr;
1891 for (i = 0; i < vcpu->arch.slb_max; i++) {
1892 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
1893 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1899 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
1900 struct kvm_sregs *sregs)
1904 /* Only accept the same PVR as the host's, since we can't spoof it */
1905 if (sregs->pvr != vcpu->arch.pvr)
1909 for (i = 0; i < vcpu->arch.slb_nr; i++) {
1910 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
1911 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
1912 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
1916 vcpu->arch.slb_max = j;
1922 * Enforce limits on guest LPCR values based on hardware availability,
1923 * guest configuration, and possibly hypervisor support and security
1926 unsigned long kvmppc_filter_lpcr_hv(struct kvm *kvm, unsigned long lpcr)
1928 /* LPCR_TC only applies to HPT guests */
1929 if (kvm_is_radix(kvm))
1932 /* On POWER8 and above, userspace can modify AIL */
1933 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
1935 if ((lpcr & LPCR_AIL) != LPCR_AIL_3)
1936 lpcr &= ~LPCR_AIL; /* LPCR[AIL]=1/2 is disallowed */
1938 * On some POWER9s we force AIL off for radix guests to prevent
1939 * executing in MSR[HV]=1 mode with the MMU enabled and PIDR set to
1940 * guest, which can result in Q0 translations with LPID=0 PID=PIDR to
1941 * be cached, which the host TLB management does not expect.
1943 if (kvm_is_radix(kvm) && cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG))
1947 * On POWER9, allow userspace to enable large decrementer for the
1948 * guest, whether or not the host has it enabled.
1950 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1956 static void verify_lpcr(struct kvm *kvm, unsigned long lpcr)
1958 if (lpcr != kvmppc_filter_lpcr_hv(kvm, lpcr)) {
1959 WARN_ONCE(1, "lpcr 0x%lx differs from filtered 0x%lx\n",
1960 lpcr, kvmppc_filter_lpcr_hv(kvm, lpcr));
1964 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
1965 bool preserve_top32)
1967 struct kvm *kvm = vcpu->kvm;
1968 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1971 spin_lock(&vc->lock);
1974 * Userspace can only modify
1975 * DPFD (default prefetch depth), ILE (interrupt little-endian),
1976 * TC (translation control), AIL (alternate interrupt location),
1977 * LD (large decrementer).
1978 * These are subject to restrictions from kvmppc_filter_lcpr_hv().
1980 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD;
1982 /* Broken 32-bit version of LPCR must not clear top bits */
1986 new_lpcr = kvmppc_filter_lpcr_hv(kvm,
1987 (vc->lpcr & ~mask) | (new_lpcr & mask));
1990 * If ILE (interrupt little-endian) has changed, update the
1991 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1993 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
1994 struct kvm_vcpu *vcpu;
1997 kvm_for_each_vcpu(i, vcpu, kvm) {
1998 if (vcpu->arch.vcore != vc)
2000 if (new_lpcr & LPCR_ILE)
2001 vcpu->arch.intr_msr |= MSR_LE;
2003 vcpu->arch.intr_msr &= ~MSR_LE;
2007 vc->lpcr = new_lpcr;
2009 spin_unlock(&vc->lock);
2012 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
2013 union kvmppc_one_reg *val)
2019 case KVM_REG_PPC_DEBUG_INST:
2020 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
2022 case KVM_REG_PPC_HIOR:
2023 *val = get_reg_val(id, 0);
2025 case KVM_REG_PPC_DABR:
2026 *val = get_reg_val(id, vcpu->arch.dabr);
2028 case KVM_REG_PPC_DABRX:
2029 *val = get_reg_val(id, vcpu->arch.dabrx);
2031 case KVM_REG_PPC_DSCR:
2032 *val = get_reg_val(id, vcpu->arch.dscr);
2034 case KVM_REG_PPC_PURR:
2035 *val = get_reg_val(id, vcpu->arch.purr);
2037 case KVM_REG_PPC_SPURR:
2038 *val = get_reg_val(id, vcpu->arch.spurr);
2040 case KVM_REG_PPC_AMR:
2041 *val = get_reg_val(id, vcpu->arch.amr);
2043 case KVM_REG_PPC_UAMOR:
2044 *val = get_reg_val(id, vcpu->arch.uamor);
2046 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
2047 i = id - KVM_REG_PPC_MMCR0;
2048 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
2050 case KVM_REG_PPC_MMCR2:
2051 *val = get_reg_val(id, vcpu->arch.mmcr[2]);
2053 case KVM_REG_PPC_MMCRA:
2054 *val = get_reg_val(id, vcpu->arch.mmcra);
2056 case KVM_REG_PPC_MMCRS:
2057 *val = get_reg_val(id, vcpu->arch.mmcrs);
2059 case KVM_REG_PPC_MMCR3:
2060 *val = get_reg_val(id, vcpu->arch.mmcr[3]);
2062 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
2063 i = id - KVM_REG_PPC_PMC1;
2064 *val = get_reg_val(id, vcpu->arch.pmc[i]);
2066 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
2067 i = id - KVM_REG_PPC_SPMC1;
2068 *val = get_reg_val(id, vcpu->arch.spmc[i]);
2070 case KVM_REG_PPC_SIAR:
2071 *val = get_reg_val(id, vcpu->arch.siar);
2073 case KVM_REG_PPC_SDAR:
2074 *val = get_reg_val(id, vcpu->arch.sdar);
2076 case KVM_REG_PPC_SIER:
2077 *val = get_reg_val(id, vcpu->arch.sier[0]);
2079 case KVM_REG_PPC_SIER2:
2080 *val = get_reg_val(id, vcpu->arch.sier[1]);
2082 case KVM_REG_PPC_SIER3:
2083 *val = get_reg_val(id, vcpu->arch.sier[2]);
2085 case KVM_REG_PPC_IAMR:
2086 *val = get_reg_val(id, vcpu->arch.iamr);
2088 case KVM_REG_PPC_PSPB:
2089 *val = get_reg_val(id, vcpu->arch.pspb);
2091 case KVM_REG_PPC_DPDES:
2093 * On POWER9, where we are emulating msgsndp etc.,
2094 * we return 1 bit for each vcpu, which can come from
2095 * either vcore->dpdes or doorbell_request.
2096 * On POWER8, doorbell_request is 0.
2098 *val = get_reg_val(id, vcpu->arch.vcore->dpdes |
2099 vcpu->arch.doorbell_request);
2101 case KVM_REG_PPC_VTB:
2102 *val = get_reg_val(id, vcpu->arch.vcore->vtb);
2104 case KVM_REG_PPC_DAWR:
2105 *val = get_reg_val(id, vcpu->arch.dawr0);
2107 case KVM_REG_PPC_DAWRX:
2108 *val = get_reg_val(id, vcpu->arch.dawrx0);
2110 case KVM_REG_PPC_DAWR1:
2111 *val = get_reg_val(id, vcpu->arch.dawr1);
2113 case KVM_REG_PPC_DAWRX1:
2114 *val = get_reg_val(id, vcpu->arch.dawrx1);
2116 case KVM_REG_PPC_CIABR:
2117 *val = get_reg_val(id, vcpu->arch.ciabr);
2119 case KVM_REG_PPC_CSIGR:
2120 *val = get_reg_val(id, vcpu->arch.csigr);
2122 case KVM_REG_PPC_TACR:
2123 *val = get_reg_val(id, vcpu->arch.tacr);
2125 case KVM_REG_PPC_TCSCR:
2126 *val = get_reg_val(id, vcpu->arch.tcscr);
2128 case KVM_REG_PPC_PID:
2129 *val = get_reg_val(id, vcpu->arch.pid);
2131 case KVM_REG_PPC_ACOP:
2132 *val = get_reg_val(id, vcpu->arch.acop);
2134 case KVM_REG_PPC_WORT:
2135 *val = get_reg_val(id, vcpu->arch.wort);
2137 case KVM_REG_PPC_TIDR:
2138 *val = get_reg_val(id, vcpu->arch.tid);
2140 case KVM_REG_PPC_PSSCR:
2141 *val = get_reg_val(id, vcpu->arch.psscr);
2143 case KVM_REG_PPC_VPA_ADDR:
2144 spin_lock(&vcpu->arch.vpa_update_lock);
2145 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
2146 spin_unlock(&vcpu->arch.vpa_update_lock);
2148 case KVM_REG_PPC_VPA_SLB:
2149 spin_lock(&vcpu->arch.vpa_update_lock);
2150 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
2151 val->vpaval.length = vcpu->arch.slb_shadow.len;
2152 spin_unlock(&vcpu->arch.vpa_update_lock);
2154 case KVM_REG_PPC_VPA_DTL:
2155 spin_lock(&vcpu->arch.vpa_update_lock);
2156 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
2157 val->vpaval.length = vcpu->arch.dtl.len;
2158 spin_unlock(&vcpu->arch.vpa_update_lock);
2160 case KVM_REG_PPC_TB_OFFSET:
2161 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
2163 case KVM_REG_PPC_LPCR:
2164 case KVM_REG_PPC_LPCR_64:
2165 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
2167 case KVM_REG_PPC_PPR:
2168 *val = get_reg_val(id, vcpu->arch.ppr);
2170 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2171 case KVM_REG_PPC_TFHAR:
2172 *val = get_reg_val(id, vcpu->arch.tfhar);
2174 case KVM_REG_PPC_TFIAR:
2175 *val = get_reg_val(id, vcpu->arch.tfiar);
2177 case KVM_REG_PPC_TEXASR:
2178 *val = get_reg_val(id, vcpu->arch.texasr);
2180 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
2181 i = id - KVM_REG_PPC_TM_GPR0;
2182 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
2184 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
2187 i = id - KVM_REG_PPC_TM_VSR0;
2189 for (j = 0; j < TS_FPRWIDTH; j++)
2190 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
2192 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2193 val->vval = vcpu->arch.vr_tm.vr[i-32];
2199 case KVM_REG_PPC_TM_CR:
2200 *val = get_reg_val(id, vcpu->arch.cr_tm);
2202 case KVM_REG_PPC_TM_XER:
2203 *val = get_reg_val(id, vcpu->arch.xer_tm);
2205 case KVM_REG_PPC_TM_LR:
2206 *val = get_reg_val(id, vcpu->arch.lr_tm);
2208 case KVM_REG_PPC_TM_CTR:
2209 *val = get_reg_val(id, vcpu->arch.ctr_tm);
2211 case KVM_REG_PPC_TM_FPSCR:
2212 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
2214 case KVM_REG_PPC_TM_AMR:
2215 *val = get_reg_val(id, vcpu->arch.amr_tm);
2217 case KVM_REG_PPC_TM_PPR:
2218 *val = get_reg_val(id, vcpu->arch.ppr_tm);
2220 case KVM_REG_PPC_TM_VRSAVE:
2221 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
2223 case KVM_REG_PPC_TM_VSCR:
2224 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2225 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
2229 case KVM_REG_PPC_TM_DSCR:
2230 *val = get_reg_val(id, vcpu->arch.dscr_tm);
2232 case KVM_REG_PPC_TM_TAR:
2233 *val = get_reg_val(id, vcpu->arch.tar_tm);
2236 case KVM_REG_PPC_ARCH_COMPAT:
2237 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
2239 case KVM_REG_PPC_DEC_EXPIRY:
2240 *val = get_reg_val(id, vcpu->arch.dec_expires +
2241 vcpu->arch.vcore->tb_offset);
2243 case KVM_REG_PPC_ONLINE:
2244 *val = get_reg_val(id, vcpu->arch.online);
2246 case KVM_REG_PPC_PTCR:
2247 *val = get_reg_val(id, vcpu->kvm->arch.l1_ptcr);
2257 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
2258 union kvmppc_one_reg *val)
2262 unsigned long addr, len;
2265 case KVM_REG_PPC_HIOR:
2266 /* Only allow this to be set to zero */
2267 if (set_reg_val(id, *val))
2270 case KVM_REG_PPC_DABR:
2271 vcpu->arch.dabr = set_reg_val(id, *val);
2273 case KVM_REG_PPC_DABRX:
2274 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
2276 case KVM_REG_PPC_DSCR:
2277 vcpu->arch.dscr = set_reg_val(id, *val);
2279 case KVM_REG_PPC_PURR:
2280 vcpu->arch.purr = set_reg_val(id, *val);
2282 case KVM_REG_PPC_SPURR:
2283 vcpu->arch.spurr = set_reg_val(id, *val);
2285 case KVM_REG_PPC_AMR:
2286 vcpu->arch.amr = set_reg_val(id, *val);
2288 case KVM_REG_PPC_UAMOR:
2289 vcpu->arch.uamor = set_reg_val(id, *val);
2291 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
2292 i = id - KVM_REG_PPC_MMCR0;
2293 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
2295 case KVM_REG_PPC_MMCR2:
2296 vcpu->arch.mmcr[2] = set_reg_val(id, *val);
2298 case KVM_REG_PPC_MMCRA:
2299 vcpu->arch.mmcra = set_reg_val(id, *val);
2301 case KVM_REG_PPC_MMCRS:
2302 vcpu->arch.mmcrs = set_reg_val(id, *val);
2304 case KVM_REG_PPC_MMCR3:
2305 *val = get_reg_val(id, vcpu->arch.mmcr[3]);
2307 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
2308 i = id - KVM_REG_PPC_PMC1;
2309 vcpu->arch.pmc[i] = set_reg_val(id, *val);
2311 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
2312 i = id - KVM_REG_PPC_SPMC1;
2313 vcpu->arch.spmc[i] = set_reg_val(id, *val);
2315 case KVM_REG_PPC_SIAR:
2316 vcpu->arch.siar = set_reg_val(id, *val);
2318 case KVM_REG_PPC_SDAR:
2319 vcpu->arch.sdar = set_reg_val(id, *val);
2321 case KVM_REG_PPC_SIER:
2322 vcpu->arch.sier[0] = set_reg_val(id, *val);
2324 case KVM_REG_PPC_SIER2:
2325 vcpu->arch.sier[1] = set_reg_val(id, *val);
2327 case KVM_REG_PPC_SIER3:
2328 vcpu->arch.sier[2] = set_reg_val(id, *val);
2330 case KVM_REG_PPC_IAMR:
2331 vcpu->arch.iamr = set_reg_val(id, *val);
2333 case KVM_REG_PPC_PSPB:
2334 vcpu->arch.pspb = set_reg_val(id, *val);
2336 case KVM_REG_PPC_DPDES:
2337 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
2339 case KVM_REG_PPC_VTB:
2340 vcpu->arch.vcore->vtb = set_reg_val(id, *val);
2342 case KVM_REG_PPC_DAWR:
2343 vcpu->arch.dawr0 = set_reg_val(id, *val);
2345 case KVM_REG_PPC_DAWRX:
2346 vcpu->arch.dawrx0 = set_reg_val(id, *val) & ~DAWRX_HYP;
2348 case KVM_REG_PPC_DAWR1:
2349 vcpu->arch.dawr1 = set_reg_val(id, *val);
2351 case KVM_REG_PPC_DAWRX1:
2352 vcpu->arch.dawrx1 = set_reg_val(id, *val) & ~DAWRX_HYP;
2354 case KVM_REG_PPC_CIABR:
2355 vcpu->arch.ciabr = set_reg_val(id, *val);
2356 /* Don't allow setting breakpoints in hypervisor code */
2357 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
2358 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
2360 case KVM_REG_PPC_CSIGR:
2361 vcpu->arch.csigr = set_reg_val(id, *val);
2363 case KVM_REG_PPC_TACR:
2364 vcpu->arch.tacr = set_reg_val(id, *val);
2366 case KVM_REG_PPC_TCSCR:
2367 vcpu->arch.tcscr = set_reg_val(id, *val);
2369 case KVM_REG_PPC_PID:
2370 vcpu->arch.pid = set_reg_val(id, *val);
2372 case KVM_REG_PPC_ACOP:
2373 vcpu->arch.acop = set_reg_val(id, *val);
2375 case KVM_REG_PPC_WORT:
2376 vcpu->arch.wort = set_reg_val(id, *val);
2378 case KVM_REG_PPC_TIDR:
2379 vcpu->arch.tid = set_reg_val(id, *val);
2381 case KVM_REG_PPC_PSSCR:
2382 vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
2384 case KVM_REG_PPC_VPA_ADDR:
2385 addr = set_reg_val(id, *val);
2387 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
2388 vcpu->arch.dtl.next_gpa))
2390 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
2392 case KVM_REG_PPC_VPA_SLB:
2393 addr = val->vpaval.addr;
2394 len = val->vpaval.length;
2396 if (addr && !vcpu->arch.vpa.next_gpa)
2398 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
2400 case KVM_REG_PPC_VPA_DTL:
2401 addr = val->vpaval.addr;
2402 len = val->vpaval.length;
2404 if (addr && (len < sizeof(struct dtl_entry) ||
2405 !vcpu->arch.vpa.next_gpa))
2407 len -= len % sizeof(struct dtl_entry);
2408 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
2410 case KVM_REG_PPC_TB_OFFSET:
2411 /* round up to multiple of 2^24 */
2412 vcpu->arch.vcore->tb_offset =
2413 ALIGN(set_reg_val(id, *val), 1UL << 24);
2415 case KVM_REG_PPC_LPCR:
2416 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
2418 case KVM_REG_PPC_LPCR_64:
2419 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
2421 case KVM_REG_PPC_PPR:
2422 vcpu->arch.ppr = set_reg_val(id, *val);
2424 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2425 case KVM_REG_PPC_TFHAR:
2426 vcpu->arch.tfhar = set_reg_val(id, *val);
2428 case KVM_REG_PPC_TFIAR:
2429 vcpu->arch.tfiar = set_reg_val(id, *val);
2431 case KVM_REG_PPC_TEXASR:
2432 vcpu->arch.texasr = set_reg_val(id, *val);
2434 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
2435 i = id - KVM_REG_PPC_TM_GPR0;
2436 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
2438 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
2441 i = id - KVM_REG_PPC_TM_VSR0;
2443 for (j = 0; j < TS_FPRWIDTH; j++)
2444 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
2446 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2447 vcpu->arch.vr_tm.vr[i-32] = val->vval;
2452 case KVM_REG_PPC_TM_CR:
2453 vcpu->arch.cr_tm = set_reg_val(id, *val);
2455 case KVM_REG_PPC_TM_XER:
2456 vcpu->arch.xer_tm = set_reg_val(id, *val);
2458 case KVM_REG_PPC_TM_LR:
2459 vcpu->arch.lr_tm = set_reg_val(id, *val);
2461 case KVM_REG_PPC_TM_CTR:
2462 vcpu->arch.ctr_tm = set_reg_val(id, *val);
2464 case KVM_REG_PPC_TM_FPSCR:
2465 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
2467 case KVM_REG_PPC_TM_AMR:
2468 vcpu->arch.amr_tm = set_reg_val(id, *val);
2470 case KVM_REG_PPC_TM_PPR:
2471 vcpu->arch.ppr_tm = set_reg_val(id, *val);
2473 case KVM_REG_PPC_TM_VRSAVE:
2474 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
2476 case KVM_REG_PPC_TM_VSCR:
2477 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2478 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
2482 case KVM_REG_PPC_TM_DSCR:
2483 vcpu->arch.dscr_tm = set_reg_val(id, *val);
2485 case KVM_REG_PPC_TM_TAR:
2486 vcpu->arch.tar_tm = set_reg_val(id, *val);
2489 case KVM_REG_PPC_ARCH_COMPAT:
2490 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
2492 case KVM_REG_PPC_DEC_EXPIRY:
2493 vcpu->arch.dec_expires = set_reg_val(id, *val) -
2494 vcpu->arch.vcore->tb_offset;
2496 case KVM_REG_PPC_ONLINE:
2497 i = set_reg_val(id, *val);
2498 if (i && !vcpu->arch.online)
2499 atomic_inc(&vcpu->arch.vcore->online_count);
2500 else if (!i && vcpu->arch.online)
2501 atomic_dec(&vcpu->arch.vcore->online_count);
2502 vcpu->arch.online = i;
2504 case KVM_REG_PPC_PTCR:
2505 vcpu->kvm->arch.l1_ptcr = set_reg_val(id, *val);
2516 * On POWER9, threads are independent and can be in different partitions.
2517 * Therefore we consider each thread to be a subcore.
2518 * There is a restriction that all threads have to be in the same
2519 * MMU mode (radix or HPT), unfortunately, but since we only support
2520 * HPT guests on a HPT host so far, that isn't an impediment yet.
2522 static int threads_per_vcore(struct kvm *kvm)
2524 if (cpu_has_feature(CPU_FTR_ARCH_300))
2526 return threads_per_subcore;
2529 static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int id)
2531 struct kvmppc_vcore *vcore;
2533 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
2538 spin_lock_init(&vcore->lock);
2539 spin_lock_init(&vcore->stoltb_lock);
2540 rcuwait_init(&vcore->wait);
2541 vcore->preempt_tb = TB_NIL;
2542 vcore->lpcr = kvm->arch.lpcr;
2543 vcore->first_vcpuid = id;
2545 INIT_LIST_HEAD(&vcore->preempt_list);
2550 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2551 static struct debugfs_timings_element {
2555 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
2556 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
2557 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
2558 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
2559 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
2562 #define N_TIMINGS (ARRAY_SIZE(timings))
2564 struct debugfs_timings_state {
2565 struct kvm_vcpu *vcpu;
2566 unsigned int buflen;
2567 char buf[N_TIMINGS * 100];
2570 static int debugfs_timings_open(struct inode *inode, struct file *file)
2572 struct kvm_vcpu *vcpu = inode->i_private;
2573 struct debugfs_timings_state *p;
2575 p = kzalloc(sizeof(*p), GFP_KERNEL);
2579 kvm_get_kvm(vcpu->kvm);
2581 file->private_data = p;
2583 return nonseekable_open(inode, file);
2586 static int debugfs_timings_release(struct inode *inode, struct file *file)
2588 struct debugfs_timings_state *p = file->private_data;
2590 kvm_put_kvm(p->vcpu->kvm);
2595 static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
2596 size_t len, loff_t *ppos)
2598 struct debugfs_timings_state *p = file->private_data;
2599 struct kvm_vcpu *vcpu = p->vcpu;
2601 struct kvmhv_tb_accumulator tb;
2610 buf_end = s + sizeof(p->buf);
2611 for (i = 0; i < N_TIMINGS; ++i) {
2612 struct kvmhv_tb_accumulator *acc;
2614 acc = (struct kvmhv_tb_accumulator *)
2615 ((unsigned long)vcpu + timings[i].offset);
2617 for (loops = 0; loops < 1000; ++loops) {
2618 count = acc->seqcount;
2623 if (count == acc->seqcount) {
2631 snprintf(s, buf_end - s, "%s: stuck\n",
2634 snprintf(s, buf_end - s,
2635 "%s: %llu %llu %llu %llu\n",
2636 timings[i].name, count / 2,
2637 tb_to_ns(tb.tb_total),
2638 tb_to_ns(tb.tb_min),
2639 tb_to_ns(tb.tb_max));
2642 p->buflen = s - p->buf;
2646 if (pos >= p->buflen)
2648 if (len > p->buflen - pos)
2649 len = p->buflen - pos;
2650 n = copy_to_user(buf, p->buf + pos, len);
2660 static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
2661 size_t len, loff_t *ppos)
2666 static const struct file_operations debugfs_timings_ops = {
2667 .owner = THIS_MODULE,
2668 .open = debugfs_timings_open,
2669 .release = debugfs_timings_release,
2670 .read = debugfs_timings_read,
2671 .write = debugfs_timings_write,
2672 .llseek = generic_file_llseek,
2675 /* Create a debugfs directory for the vcpu */
2676 static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
2679 struct kvm *kvm = vcpu->kvm;
2681 snprintf(buf, sizeof(buf), "vcpu%u", id);
2682 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
2683 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir, vcpu,
2684 &debugfs_timings_ops);
2687 #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
2688 static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
2691 #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
2693 static int kvmppc_core_vcpu_create_hv(struct kvm_vcpu *vcpu)
2697 struct kvmppc_vcore *vcore;
2704 vcpu->arch.shared = &vcpu->arch.shregs;
2705 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
2707 * The shared struct is never shared on HV,
2708 * so we can always use host endianness
2710 #ifdef __BIG_ENDIAN__
2711 vcpu->arch.shared_big_endian = true;
2713 vcpu->arch.shared_big_endian = false;
2716 vcpu->arch.mmcr[0] = MMCR0_FC;
2717 vcpu->arch.ctrl = CTRL_RUNLATCH;
2718 /* default to host PVR, since we can't spoof it */
2719 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
2720 spin_lock_init(&vcpu->arch.vpa_update_lock);
2721 spin_lock_init(&vcpu->arch.tbacct_lock);
2722 vcpu->arch.busy_preempt = TB_NIL;
2723 vcpu->arch.shregs.msr = MSR_ME;
2724 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
2727 * Set the default HFSCR for the guest from the host value.
2728 * This value is only used on POWER9.
2729 * On POWER9, we want to virtualize the doorbell facility, so we
2730 * don't set the HFSCR_MSGP bit, and that causes those instructions
2731 * to trap and then we emulate them.
2733 vcpu->arch.hfscr = HFSCR_TAR | HFSCR_EBB | HFSCR_PM | HFSCR_BHRB |
2734 HFSCR_DSCR | HFSCR_VECVSX | HFSCR_FP | HFSCR_PREFIX;
2735 if (cpu_has_feature(CPU_FTR_HVMODE)) {
2736 vcpu->arch.hfscr &= mfspr(SPRN_HFSCR);
2737 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2738 if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
2739 vcpu->arch.hfscr |= HFSCR_TM;
2742 if (cpu_has_feature(CPU_FTR_TM_COMP))
2743 vcpu->arch.hfscr |= HFSCR_TM;
2745 vcpu->arch.hfscr_permitted = vcpu->arch.hfscr;
2747 kvmppc_mmu_book3s_hv_init(vcpu);
2749 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
2751 init_waitqueue_head(&vcpu->arch.cpu_run);
2753 mutex_lock(&kvm->lock);
2756 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
2757 if (id >= (KVM_MAX_VCPUS * kvm->arch.emul_smt_mode)) {
2758 pr_devel("KVM: VCPU ID too high\n");
2759 core = KVM_MAX_VCORES;
2761 BUG_ON(kvm->arch.smt_mode != 1);
2762 core = kvmppc_pack_vcpu_id(kvm, id);
2765 core = id / kvm->arch.smt_mode;
2767 if (core < KVM_MAX_VCORES) {
2768 vcore = kvm->arch.vcores[core];
2769 if (vcore && cpu_has_feature(CPU_FTR_ARCH_300)) {
2770 pr_devel("KVM: collision on id %u", id);
2772 } else if (!vcore) {
2774 * Take mmu_setup_lock for mutual exclusion
2775 * with kvmppc_update_lpcr().
2778 vcore = kvmppc_vcore_create(kvm,
2779 id & ~(kvm->arch.smt_mode - 1));
2780 mutex_lock(&kvm->arch.mmu_setup_lock);
2781 kvm->arch.vcores[core] = vcore;
2782 kvm->arch.online_vcores++;
2783 mutex_unlock(&kvm->arch.mmu_setup_lock);
2786 mutex_unlock(&kvm->lock);
2791 spin_lock(&vcore->lock);
2792 ++vcore->num_threads;
2793 spin_unlock(&vcore->lock);
2794 vcpu->arch.vcore = vcore;
2795 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
2796 vcpu->arch.thread_cpu = -1;
2797 vcpu->arch.prev_cpu = -1;
2799 vcpu->arch.cpu_type = KVM_CPU_3S_64;
2800 kvmppc_sanity_check(vcpu);
2802 debugfs_vcpu_init(vcpu, id);
2807 static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode,
2808 unsigned long flags)
2815 if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode))
2817 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
2819 * On POWER8 (or POWER7), the threading mode is "strict",
2820 * so we pack smt_mode vcpus per vcore.
2822 if (smt_mode > threads_per_subcore)
2826 * On POWER9, the threading mode is "loose",
2827 * so each vcpu gets its own vcore.
2832 mutex_lock(&kvm->lock);
2834 if (!kvm->arch.online_vcores) {
2835 kvm->arch.smt_mode = smt_mode;
2836 kvm->arch.emul_smt_mode = esmt;
2839 mutex_unlock(&kvm->lock);
2844 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
2846 if (vpa->pinned_addr)
2847 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
2851 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
2853 spin_lock(&vcpu->arch.vpa_update_lock);
2854 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
2855 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
2856 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
2857 spin_unlock(&vcpu->arch.vpa_update_lock);
2860 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
2862 /* Indicate we want to get back into the guest */
2866 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
2868 unsigned long dec_nsec, now;
2871 if (now > vcpu->arch.dec_expires) {
2872 /* decrementer has already gone negative */
2873 kvmppc_core_queue_dec(vcpu);
2874 kvmppc_core_prepare_to_enter(vcpu);
2877 dec_nsec = tb_to_ns(vcpu->arch.dec_expires - now);
2878 hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
2879 vcpu->arch.timer_running = 1;
2882 extern int __kvmppc_vcore_entry(void);
2884 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
2885 struct kvm_vcpu *vcpu)
2889 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2891 spin_lock_irq(&vcpu->arch.tbacct_lock);
2893 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
2894 vcpu->arch.stolen_logged;
2895 vcpu->arch.busy_preempt = now;
2896 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
2897 spin_unlock_irq(&vcpu->arch.tbacct_lock);
2899 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
2902 static int kvmppc_grab_hwthread(int cpu)
2904 struct paca_struct *tpaca;
2905 long timeout = 10000;
2907 tpaca = paca_ptrs[cpu];
2909 /* Ensure the thread won't go into the kernel if it wakes */
2910 tpaca->kvm_hstate.kvm_vcpu = NULL;
2911 tpaca->kvm_hstate.kvm_vcore = NULL;
2912 tpaca->kvm_hstate.napping = 0;
2914 tpaca->kvm_hstate.hwthread_req = 1;
2917 * If the thread is already executing in the kernel (e.g. handling
2918 * a stray interrupt), wait for it to get back to nap mode.
2919 * The smp_mb() is to ensure that our setting of hwthread_req
2920 * is visible before we look at hwthread_state, so if this
2921 * races with the code at system_reset_pSeries and the thread
2922 * misses our setting of hwthread_req, we are sure to see its
2923 * setting of hwthread_state, and vice versa.
2926 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
2927 if (--timeout <= 0) {
2928 pr_err("KVM: couldn't grab cpu %d\n", cpu);
2936 static void kvmppc_release_hwthread(int cpu)
2938 struct paca_struct *tpaca;
2940 tpaca = paca_ptrs[cpu];
2941 tpaca->kvm_hstate.hwthread_req = 0;
2942 tpaca->kvm_hstate.kvm_vcpu = NULL;
2943 tpaca->kvm_hstate.kvm_vcore = NULL;
2944 tpaca->kvm_hstate.kvm_split_mode = NULL;
2947 static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
2949 struct kvm_nested_guest *nested = vcpu->arch.nested;
2950 cpumask_t *cpu_in_guest;
2953 cpu = cpu_first_tlb_thread_sibling(cpu);
2955 cpumask_set_cpu(cpu, &nested->need_tlb_flush);
2956 cpu_in_guest = &nested->cpu_in_guest;
2958 cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush);
2959 cpu_in_guest = &kvm->arch.cpu_in_guest;
2962 * Make sure setting of bit in need_tlb_flush precedes
2963 * testing of cpu_in_guest bits. The matching barrier on
2964 * the other side is the first smp_mb() in kvmppc_run_core().
2967 for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu);
2968 i += cpu_tlb_thread_sibling_step())
2969 if (cpumask_test_cpu(i, cpu_in_guest))
2970 smp_call_function_single(i, do_nothing, NULL, 1);
2973 static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
2975 struct kvm_nested_guest *nested = vcpu->arch.nested;
2976 struct kvm *kvm = vcpu->kvm;
2979 if (!cpu_has_feature(CPU_FTR_HVMODE))
2983 prev_cpu = nested->prev_cpu[vcpu->arch.nested_vcpu_id];
2985 prev_cpu = vcpu->arch.prev_cpu;
2988 * With radix, the guest can do TLB invalidations itself,
2989 * and it could choose to use the local form (tlbiel) if
2990 * it is invalidating a translation that has only ever been
2991 * used on one vcpu. However, that doesn't mean it has
2992 * only ever been used on one physical cpu, since vcpus
2993 * can move around between pcpus. To cope with this, when
2994 * a vcpu moves from one pcpu to another, we need to tell
2995 * any vcpus running on the same core as this vcpu previously
2996 * ran to flush the TLB. The TLB is shared between threads,
2997 * so we use a single bit in .need_tlb_flush for all 4 threads.
2999 if (prev_cpu != pcpu) {
3000 if (prev_cpu >= 0 &&
3001 cpu_first_tlb_thread_sibling(prev_cpu) !=
3002 cpu_first_tlb_thread_sibling(pcpu))
3003 radix_flush_cpu(kvm, prev_cpu, vcpu);
3005 nested->prev_cpu[vcpu->arch.nested_vcpu_id] = pcpu;
3007 vcpu->arch.prev_cpu = pcpu;
3011 static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
3014 struct paca_struct *tpaca;
3015 struct kvm *kvm = vc->kvm;
3019 if (vcpu->arch.timer_running) {
3020 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
3021 vcpu->arch.timer_running = 0;
3023 cpu += vcpu->arch.ptid;
3024 vcpu->cpu = vc->pcpu;
3025 vcpu->arch.thread_cpu = cpu;
3026 cpumask_set_cpu(cpu, &kvm->arch.cpu_in_guest);
3028 tpaca = paca_ptrs[cpu];
3029 tpaca->kvm_hstate.kvm_vcpu = vcpu;
3030 tpaca->kvm_hstate.ptid = cpu - vc->pcpu;
3031 tpaca->kvm_hstate.fake_suspend = 0;
3032 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
3034 tpaca->kvm_hstate.kvm_vcore = vc;
3035 if (cpu != smp_processor_id())
3036 kvmppc_ipi_thread(cpu);
3039 static void kvmppc_wait_for_nap(int n_threads)
3041 int cpu = smp_processor_id();
3046 for (loops = 0; loops < 1000000; ++loops) {
3048 * Check if all threads are finished.
3049 * We set the vcore pointer when starting a thread
3050 * and the thread clears it when finished, so we look
3051 * for any threads that still have a non-NULL vcore ptr.
3053 for (i = 1; i < n_threads; ++i)
3054 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
3056 if (i == n_threads) {
3063 for (i = 1; i < n_threads; ++i)
3064 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
3065 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
3069 * Check that we are on thread 0 and that any other threads in
3070 * this core are off-line. Then grab the threads so they can't
3073 static int on_primary_thread(void)
3075 int cpu = smp_processor_id();
3078 /* Are we on a primary subcore? */
3079 if (cpu_thread_in_subcore(cpu))
3083 while (++thr < threads_per_subcore)
3084 if (cpu_online(cpu + thr))
3087 /* Grab all hw threads so they can't go into the kernel */
3088 for (thr = 1; thr < threads_per_subcore; ++thr) {
3089 if (kvmppc_grab_hwthread(cpu + thr)) {
3090 /* Couldn't grab one; let the others go */
3092 kvmppc_release_hwthread(cpu + thr);
3093 } while (--thr > 0);
3101 * A list of virtual cores for each physical CPU.
3102 * These are vcores that could run but their runner VCPU tasks are
3103 * (or may be) preempted.
3105 struct preempted_vcore_list {
3106 struct list_head list;
3110 static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
3112 static void init_vcore_lists(void)
3116 for_each_possible_cpu(cpu) {
3117 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
3118 spin_lock_init(&lp->lock);
3119 INIT_LIST_HEAD(&lp->list);
3123 static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
3125 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
3127 vc->vcore_state = VCORE_PREEMPT;
3128 vc->pcpu = smp_processor_id();
3129 if (vc->num_threads < threads_per_vcore(vc->kvm)) {
3130 spin_lock(&lp->lock);
3131 list_add_tail(&vc->preempt_list, &lp->list);
3132 spin_unlock(&lp->lock);
3135 /* Start accumulating stolen time */
3136 kvmppc_core_start_stolen(vc);
3139 static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
3141 struct preempted_vcore_list *lp;
3143 kvmppc_core_end_stolen(vc);
3144 if (!list_empty(&vc->preempt_list)) {
3145 lp = &per_cpu(preempted_vcores, vc->pcpu);
3146 spin_lock(&lp->lock);
3147 list_del_init(&vc->preempt_list);
3148 spin_unlock(&lp->lock);
3150 vc->vcore_state = VCORE_INACTIVE;
3154 * This stores information about the virtual cores currently
3155 * assigned to a physical core.
3159 int max_subcore_threads;
3161 int subcore_threads[MAX_SUBCORES];
3162 struct kvmppc_vcore *vc[MAX_SUBCORES];
3166 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
3167 * respectively in 2-way micro-threading (split-core) mode on POWER8.
3169 static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
3171 static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
3173 memset(cip, 0, sizeof(*cip));
3174 cip->n_subcores = 1;
3175 cip->max_subcore_threads = vc->num_threads;
3176 cip->total_threads = vc->num_threads;
3177 cip->subcore_threads[0] = vc->num_threads;
3181 static bool subcore_config_ok(int n_subcores, int n_threads)
3184 * POWER9 "SMT4" cores are permanently in what is effectively a 4-way
3185 * split-core mode, with one thread per subcore.
3187 if (cpu_has_feature(CPU_FTR_ARCH_300))
3188 return n_subcores <= 4 && n_threads == 1;
3190 /* On POWER8, can only dynamically split if unsplit to begin with */
3191 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
3193 if (n_subcores > MAX_SUBCORES)
3195 if (n_subcores > 1) {
3196 if (!(dynamic_mt_modes & 2))
3198 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
3202 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
3205 static void init_vcore_to_run(struct kvmppc_vcore *vc)
3207 vc->entry_exit_map = 0;
3209 vc->napping_threads = 0;
3210 vc->conferring_threads = 0;
3211 vc->tb_offset_applied = 0;
3214 static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
3216 int n_threads = vc->num_threads;
3219 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
3222 /* In one_vm_per_core mode, require all vcores to be from the same vm */
3223 if (one_vm_per_core && vc->kvm != cip->vc[0]->kvm)
3226 if (n_threads < cip->max_subcore_threads)
3227 n_threads = cip->max_subcore_threads;
3228 if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
3230 cip->max_subcore_threads = n_threads;
3232 sub = cip->n_subcores;
3234 cip->total_threads += vc->num_threads;
3235 cip->subcore_threads[sub] = vc->num_threads;
3237 init_vcore_to_run(vc);
3238 list_del_init(&vc->preempt_list);
3244 * Work out whether it is possible to piggyback the execution of
3245 * vcore *pvc onto the execution of the other vcores described in *cip.
3247 static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
3250 if (cip->total_threads + pvc->num_threads > target_threads)
3253 return can_dynamic_split(pvc, cip);
3256 static void prepare_threads(struct kvmppc_vcore *vc)
3259 struct kvm_vcpu *vcpu;
3261 for_each_runnable_thread(i, vcpu, vc) {
3262 if (signal_pending(vcpu->arch.run_task))
3263 vcpu->arch.ret = -EINTR;
3264 else if (vcpu->arch.vpa.update_pending ||
3265 vcpu->arch.slb_shadow.update_pending ||
3266 vcpu->arch.dtl.update_pending)
3267 vcpu->arch.ret = RESUME_GUEST;
3270 kvmppc_remove_runnable(vc, vcpu);
3271 wake_up(&vcpu->arch.cpu_run);
3275 static void collect_piggybacks(struct core_info *cip, int target_threads)
3277 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
3278 struct kvmppc_vcore *pvc, *vcnext;
3280 spin_lock(&lp->lock);
3281 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
3282 if (!spin_trylock(&pvc->lock))
3284 prepare_threads(pvc);
3285 if (!pvc->n_runnable || !pvc->kvm->arch.mmu_ready) {
3286 list_del_init(&pvc->preempt_list);
3287 if (pvc->runner == NULL) {
3288 pvc->vcore_state = VCORE_INACTIVE;
3289 kvmppc_core_end_stolen(pvc);
3291 spin_unlock(&pvc->lock);
3294 if (!can_piggyback(pvc, cip, target_threads)) {
3295 spin_unlock(&pvc->lock);
3298 kvmppc_core_end_stolen(pvc);
3299 pvc->vcore_state = VCORE_PIGGYBACK;
3300 if (cip->total_threads >= target_threads)
3303 spin_unlock(&lp->lock);
3306 static bool recheck_signals_and_mmu(struct core_info *cip)
3309 struct kvm_vcpu *vcpu;
3310 struct kvmppc_vcore *vc;
3312 for (sub = 0; sub < cip->n_subcores; ++sub) {
3314 if (!vc->kvm->arch.mmu_ready)
3316 for_each_runnable_thread(i, vcpu, vc)
3317 if (signal_pending(vcpu->arch.run_task))
3323 static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
3325 int still_running = 0, i;
3328 struct kvm_vcpu *vcpu;
3330 spin_lock(&vc->lock);
3332 for_each_runnable_thread(i, vcpu, vc) {
3334 * It's safe to unlock the vcore in the loop here, because
3335 * for_each_runnable_thread() is safe against removal of
3336 * the vcpu, and the vcore state is VCORE_EXITING here,
3337 * so any vcpus becoming runnable will have their arch.trap
3338 * set to zero and can't actually run in the guest.
3340 spin_unlock(&vc->lock);
3341 /* cancel pending dec exception if dec is positive */
3342 if (now < vcpu->arch.dec_expires &&
3343 kvmppc_core_pending_dec(vcpu))
3344 kvmppc_core_dequeue_dec(vcpu);
3346 trace_kvm_guest_exit(vcpu);
3349 if (vcpu->arch.trap)
3350 ret = kvmppc_handle_exit_hv(vcpu,
3351 vcpu->arch.run_task);
3353 vcpu->arch.ret = ret;
3354 vcpu->arch.trap = 0;
3356 spin_lock(&vc->lock);
3357 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
3358 if (vcpu->arch.pending_exceptions)
3359 kvmppc_core_prepare_to_enter(vcpu);
3360 if (vcpu->arch.ceded)
3361 kvmppc_set_timer(vcpu);
3365 kvmppc_remove_runnable(vc, vcpu);
3366 wake_up(&vcpu->arch.cpu_run);
3370 if (still_running > 0) {
3371 kvmppc_vcore_preempt(vc);
3372 } else if (vc->runner) {
3373 vc->vcore_state = VCORE_PREEMPT;
3374 kvmppc_core_start_stolen(vc);
3376 vc->vcore_state = VCORE_INACTIVE;
3378 if (vc->n_runnable > 0 && vc->runner == NULL) {
3379 /* make sure there's a candidate runner awake */
3381 vcpu = next_runnable_thread(vc, &i);
3382 wake_up(&vcpu->arch.cpu_run);
3385 spin_unlock(&vc->lock);
3389 * Clear core from the list of active host cores as we are about to
3390 * enter the guest. Only do this if it is the primary thread of the
3391 * core (not if a subcore) that is entering the guest.
3393 static inline int kvmppc_clear_host_core(unsigned int cpu)
3397 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
3400 * Memory barrier can be omitted here as we will do a smp_wmb()
3401 * later in kvmppc_start_thread and we need ensure that state is
3402 * visible to other CPUs only after we enter guest.
3404 core = cpu >> threads_shift;
3405 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
3410 * Advertise this core as an active host core since we exited the guest
3411 * Only need to do this if it is the primary thread of the core that is
3414 static inline int kvmppc_set_host_core(unsigned int cpu)
3418 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
3422 * Memory barrier can be omitted here because we do a spin_unlock
3423 * immediately after this which provides the memory barrier.
3425 core = cpu >> threads_shift;
3426 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
3430 static void set_irq_happened(int trap)
3433 case BOOK3S_INTERRUPT_EXTERNAL:
3434 local_paca->irq_happened |= PACA_IRQ_EE;
3436 case BOOK3S_INTERRUPT_H_DOORBELL:
3437 local_paca->irq_happened |= PACA_IRQ_DBELL;
3439 case BOOK3S_INTERRUPT_HMI:
3440 local_paca->irq_happened |= PACA_IRQ_HMI;
3442 case BOOK3S_INTERRUPT_SYSTEM_RESET:
3443 replay_system_reset();
3449 * Run a set of guest threads on a physical core.
3450 * Called with vc->lock held.
3452 static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
3454 struct kvm_vcpu *vcpu;
3457 struct core_info core_info;
3458 struct kvmppc_vcore *pvc;
3459 struct kvm_split_mode split_info, *sip;
3460 int split, subcore_size, active;
3463 unsigned long cmd_bit, stat_bit;
3466 int controlled_threads;
3470 if (WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)))
3474 * Remove from the list any threads that have a signal pending
3475 * or need a VPA update done
3477 prepare_threads(vc);
3479 /* if the runner is no longer runnable, let the caller pick a new one */
3480 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
3486 init_vcore_to_run(vc);
3487 vc->preempt_tb = TB_NIL;
3490 * Number of threads that we will be controlling: the same as
3491 * the number of threads per subcore, except on POWER9,
3492 * where it's 1 because the threads are (mostly) independent.
3494 controlled_threads = threads_per_vcore(vc->kvm);
3497 * Make sure we are running on primary threads, and that secondary
3498 * threads are offline. Also check if the number of threads in this
3499 * guest are greater than the current system threads per guest.
3501 if ((controlled_threads > 1) &&
3502 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
3503 for_each_runnable_thread(i, vcpu, vc) {
3504 vcpu->arch.ret = -EBUSY;
3505 kvmppc_remove_runnable(vc, vcpu);
3506 wake_up(&vcpu->arch.cpu_run);
3512 * See if we could run any other vcores on the physical core
3513 * along with this one.
3515 init_core_info(&core_info, vc);
3516 pcpu = smp_processor_id();
3517 target_threads = controlled_threads;
3518 if (target_smt_mode && target_smt_mode < target_threads)
3519 target_threads = target_smt_mode;
3520 if (vc->num_threads < target_threads)
3521 collect_piggybacks(&core_info, target_threads);
3524 * Hard-disable interrupts, and check resched flag and signals.
3525 * If we need to reschedule or deliver a signal, clean up
3526 * and return without going into the guest(s).
3527 * If the mmu_ready flag has been cleared, don't go into the
3528 * guest because that means a HPT resize operation is in progress.
3530 local_irq_disable();
3532 if (lazy_irq_pending() || need_resched() ||
3533 recheck_signals_and_mmu(&core_info)) {
3535 vc->vcore_state = VCORE_INACTIVE;
3536 /* Unlock all except the primary vcore */
3537 for (sub = 1; sub < core_info.n_subcores; ++sub) {
3538 pvc = core_info.vc[sub];
3539 /* Put back on to the preempted vcores list */
3540 kvmppc_vcore_preempt(pvc);
3541 spin_unlock(&pvc->lock);
3543 for (i = 0; i < controlled_threads; ++i)
3544 kvmppc_release_hwthread(pcpu + i);
3548 kvmppc_clear_host_core(pcpu);
3550 /* Decide on micro-threading (split-core) mode */
3551 subcore_size = threads_per_subcore;
3552 cmd_bit = stat_bit = 0;
3553 split = core_info.n_subcores;
3555 is_power8 = cpu_has_feature(CPU_FTR_ARCH_207S);
3559 memset(&split_info, 0, sizeof(split_info));
3560 for (sub = 0; sub < core_info.n_subcores; ++sub)
3561 split_info.vc[sub] = core_info.vc[sub];
3564 if (split == 2 && (dynamic_mt_modes & 2)) {
3565 cmd_bit = HID0_POWER8_1TO2LPAR;
3566 stat_bit = HID0_POWER8_2LPARMODE;
3569 cmd_bit = HID0_POWER8_1TO4LPAR;
3570 stat_bit = HID0_POWER8_4LPARMODE;
3572 subcore_size = MAX_SMT_THREADS / split;
3573 split_info.rpr = mfspr(SPRN_RPR);
3574 split_info.pmmar = mfspr(SPRN_PMMAR);
3575 split_info.ldbar = mfspr(SPRN_LDBAR);
3576 split_info.subcore_size = subcore_size;
3578 split_info.subcore_size = 1;
3581 /* order writes to split_info before kvm_split_mode pointer */
3585 for (thr = 0; thr < controlled_threads; ++thr) {
3586 struct paca_struct *paca = paca_ptrs[pcpu + thr];
3588 paca->kvm_hstate.napping = 0;
3589 paca->kvm_hstate.kvm_split_mode = sip;
3592 /* Initiate micro-threading (split-core) on POWER8 if required */
3594 unsigned long hid0 = mfspr(SPRN_HID0);
3596 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
3598 mtspr(SPRN_HID0, hid0);
3601 hid0 = mfspr(SPRN_HID0);
3602 if (hid0 & stat_bit)
3609 * On POWER8, set RWMR register.
3610 * Since it only affects PURR and SPURR, it doesn't affect
3611 * the host, so we don't save/restore the host value.
3614 unsigned long rwmr_val = RWMR_RPA_P8_8THREAD;
3615 int n_online = atomic_read(&vc->online_count);
3618 * Use the 8-thread value if we're doing split-core
3619 * or if the vcore's online count looks bogus.
3621 if (split == 1 && threads_per_subcore == MAX_SMT_THREADS &&
3622 n_online >= 1 && n_online <= MAX_SMT_THREADS)
3623 rwmr_val = p8_rwmr_values[n_online];
3624 mtspr(SPRN_RWMR, rwmr_val);
3627 /* Start all the threads */
3629 for (sub = 0; sub < core_info.n_subcores; ++sub) {
3630 thr = is_power8 ? subcore_thread_map[sub] : sub;
3633 pvc = core_info.vc[sub];
3634 pvc->pcpu = pcpu + thr;
3635 for_each_runnable_thread(i, vcpu, pvc) {
3636 kvmppc_start_thread(vcpu, pvc);
3637 kvmppc_create_dtl_entry(vcpu, pvc);
3638 trace_kvm_guest_enter(vcpu);
3639 if (!vcpu->arch.ptid)
3641 active |= 1 << (thr + vcpu->arch.ptid);
3644 * We need to start the first thread of each subcore
3645 * even if it doesn't have a vcpu.
3648 kvmppc_start_thread(NULL, pvc);
3652 * Ensure that split_info.do_nap is set after setting
3653 * the vcore pointer in the PACA of the secondaries.
3658 * When doing micro-threading, poke the inactive threads as well.
3659 * This gets them to the nap instruction after kvm_do_nap,
3660 * which reduces the time taken to unsplit later.
3663 split_info.do_nap = 1; /* ask secondaries to nap when done */
3664 for (thr = 1; thr < threads_per_subcore; ++thr)
3665 if (!(active & (1 << thr)))
3666 kvmppc_ipi_thread(pcpu + thr);
3669 vc->vcore_state = VCORE_RUNNING;
3672 trace_kvmppc_run_core(vc, 0);
3674 for (sub = 0; sub < core_info.n_subcores; ++sub)
3675 spin_unlock(&core_info.vc[sub]->lock);
3677 guest_enter_irqoff();
3679 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
3681 this_cpu_disable_ftrace();
3684 * Interrupts will be enabled once we get into the guest,
3685 * so tell lockdep that we're about to enable interrupts.
3687 trace_hardirqs_on();
3689 trap = __kvmppc_vcore_entry();
3691 trace_hardirqs_off();
3693 this_cpu_enable_ftrace();
3695 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
3697 set_irq_happened(trap);
3699 spin_lock(&vc->lock);
3700 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
3701 vc->vcore_state = VCORE_EXITING;
3703 /* wait for secondary threads to finish writing their state to memory */
3704 kvmppc_wait_for_nap(controlled_threads);
3706 /* Return to whole-core mode if we split the core earlier */
3708 unsigned long hid0 = mfspr(SPRN_HID0);
3709 unsigned long loops = 0;
3711 hid0 &= ~HID0_POWER8_DYNLPARDIS;
3712 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
3714 mtspr(SPRN_HID0, hid0);
3717 hid0 = mfspr(SPRN_HID0);
3718 if (!(hid0 & stat_bit))
3723 split_info.do_nap = 0;
3726 kvmppc_set_host_core(pcpu);
3728 context_tracking_guest_exit();
3729 if (!vtime_accounting_enabled_this_cpu()) {
3732 * Service IRQs here before vtime_account_guest_exit() so any
3733 * ticks that occurred while running the guest are accounted to
3734 * the guest. If vtime accounting is enabled, accounting uses
3735 * TB rather than ticks, so it can be done without enabling
3736 * interrupts here, which has the problem that it accounts
3737 * interrupt processing overhead to the host.
3739 local_irq_disable();
3741 vtime_account_guest_exit();
3745 /* Let secondaries go back to the offline loop */
3746 for (i = 0; i < controlled_threads; ++i) {
3747 kvmppc_release_hwthread(pcpu + i);
3748 if (sip && sip->napped[i])
3749 kvmppc_ipi_thread(pcpu + i);
3750 cpumask_clear_cpu(pcpu + i, &vc->kvm->arch.cpu_in_guest);
3753 spin_unlock(&vc->lock);
3755 /* make sure updates to secondary vcpu structs are visible now */
3760 for (sub = 0; sub < core_info.n_subcores; ++sub) {
3761 pvc = core_info.vc[sub];
3762 post_guest_process(pvc, pvc == vc);
3765 spin_lock(&vc->lock);
3768 vc->vcore_state = VCORE_INACTIVE;
3769 trace_kvmppc_run_core(vc, 1);
3772 static void load_spr_state(struct kvm_vcpu *vcpu)
3774 mtspr(SPRN_DSCR, vcpu->arch.dscr);
3775 mtspr(SPRN_IAMR, vcpu->arch.iamr);
3776 mtspr(SPRN_PSPB, vcpu->arch.pspb);
3777 mtspr(SPRN_FSCR, vcpu->arch.fscr);
3778 mtspr(SPRN_TAR, vcpu->arch.tar);
3779 mtspr(SPRN_EBBHR, vcpu->arch.ebbhr);
3780 mtspr(SPRN_EBBRR, vcpu->arch.ebbrr);
3781 mtspr(SPRN_BESCR, vcpu->arch.bescr);
3782 mtspr(SPRN_TIDR, vcpu->arch.tid);
3783 mtspr(SPRN_AMR, vcpu->arch.amr);
3784 mtspr(SPRN_UAMOR, vcpu->arch.uamor);
3787 * DAR, DSISR, and for nested HV, SPRGs must be set with MSR[RI]
3788 * clear (or hstate set appropriately to catch those registers
3789 * being clobbered if we take a MCE or SRESET), so those are done
3793 if (!(vcpu->arch.ctrl & 1))
3794 mtspr(SPRN_CTRLT, mfspr(SPRN_CTRLF) & ~1);
3797 static void store_spr_state(struct kvm_vcpu *vcpu)
3799 vcpu->arch.ctrl = mfspr(SPRN_CTRLF);
3801 vcpu->arch.iamr = mfspr(SPRN_IAMR);
3802 vcpu->arch.pspb = mfspr(SPRN_PSPB);
3803 vcpu->arch.fscr = mfspr(SPRN_FSCR);
3804 vcpu->arch.tar = mfspr(SPRN_TAR);
3805 vcpu->arch.ebbhr = mfspr(SPRN_EBBHR);
3806 vcpu->arch.ebbrr = mfspr(SPRN_EBBRR);
3807 vcpu->arch.bescr = mfspr(SPRN_BESCR);
3808 vcpu->arch.tid = mfspr(SPRN_TIDR);
3809 vcpu->arch.amr = mfspr(SPRN_AMR);
3810 vcpu->arch.uamor = mfspr(SPRN_UAMOR);
3811 vcpu->arch.dscr = mfspr(SPRN_DSCR);
3815 * Privileged (non-hypervisor) host registers to save.
3817 struct p9_host_os_sprs {
3825 static void save_p9_host_os_sprs(struct p9_host_os_sprs *host_os_sprs)
3827 host_os_sprs->dscr = mfspr(SPRN_DSCR);
3828 host_os_sprs->tidr = mfspr(SPRN_TIDR);
3829 host_os_sprs->iamr = mfspr(SPRN_IAMR);
3830 host_os_sprs->amr = mfspr(SPRN_AMR);
3831 host_os_sprs->fscr = mfspr(SPRN_FSCR);
3834 /* vcpu guest regs must already be saved */
3835 static void restore_p9_host_os_sprs(struct kvm_vcpu *vcpu,
3836 struct p9_host_os_sprs *host_os_sprs)
3838 mtspr(SPRN_PSPB, 0);
3839 mtspr(SPRN_UAMOR, 0);
3841 mtspr(SPRN_DSCR, host_os_sprs->dscr);
3842 mtspr(SPRN_TIDR, host_os_sprs->tidr);
3843 mtspr(SPRN_IAMR, host_os_sprs->iamr);
3845 if (host_os_sprs->amr != vcpu->arch.amr)
3846 mtspr(SPRN_AMR, host_os_sprs->amr);
3848 if (host_os_sprs->fscr != vcpu->arch.fscr)
3849 mtspr(SPRN_FSCR, host_os_sprs->fscr);
3851 /* Save guest CTRL register, set runlatch to 1 */
3852 if (!(vcpu->arch.ctrl & 1))
3853 mtspr(SPRN_CTRLT, 1);
3856 static inline bool hcall_is_xics(unsigned long req)
3858 return req == H_EOI || req == H_CPPR || req == H_IPI ||
3859 req == H_IPOLL || req == H_XIRR || req == H_XIRR_X;
3863 * Guest entry for POWER9 and later CPUs.
3865 static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,
3868 struct kvmppc_vcore *vc = vcpu->arch.vcore;
3869 struct p9_host_os_sprs host_os_sprs;
3874 WARN_ON_ONCE(vcpu->arch.ceded);
3876 dec = mfspr(SPRN_DEC);
3879 return BOOK3S_INTERRUPT_HV_DECREMENTER;
3880 local_paca->kvm_hstate.dec_expires = dec + tb;
3881 if (local_paca->kvm_hstate.dec_expires < time_limit)
3882 time_limit = local_paca->kvm_hstate.dec_expires;
3884 save_p9_host_os_sprs(&host_os_sprs);
3886 kvmhv_save_host_pmu(); /* saves it to PACA kvm_hstate */
3888 kvmppc_subcore_enter_guest();
3890 vc->entry_exit_map = 1;
3893 if (vcpu->arch.vpa.pinned_addr) {
3894 struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
3895 u32 yield_count = be32_to_cpu(lp->yield_count) + 1;
3896 lp->yield_count = cpu_to_be32(yield_count);
3897 vcpu->arch.vpa.dirty = 1;
3900 if (cpu_has_feature(CPU_FTR_TM) ||
3901 cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
3902 kvmppc_restore_tm_hv(vcpu, vcpu->arch.shregs.msr, true);
3904 #ifdef CONFIG_PPC_PSERIES
3905 if (kvmhv_on_pseries()) {
3907 if (vcpu->arch.vpa.pinned_addr) {
3908 struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
3909 get_lppaca()->pmcregs_in_use = lp->pmcregs_in_use;
3911 get_lppaca()->pmcregs_in_use = 1;
3916 kvmhv_load_guest_pmu(vcpu);
3918 msr_check_and_set(MSR_FP | MSR_VEC | MSR_VSX);
3919 load_fp_state(&vcpu->arch.fp);
3920 #ifdef CONFIG_ALTIVEC
3921 load_vr_state(&vcpu->arch.vr);
3923 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
3925 load_spr_state(vcpu);
3928 * When setting DEC, we must always deal with irq_work_raise via NMI vs
3929 * setting DEC. The problem occurs right as we switch into guest mode
3930 * if a NMI hits and sets pending work and sets DEC, then that will
3931 * apply to the guest and not bring us back to the host.
3933 * irq_work_raise could check a flag (or possibly LPCR[HDICE] for
3934 * example) and set HDEC to 1? That wouldn't solve the nested hv
3935 * case which needs to abort the hcall or zero the time limit.
3937 * XXX: Another day's problem.
3939 mtspr(SPRN_DEC, vcpu->arch.dec_expires - mftb());
3941 if (kvmhv_on_pseries()) {
3943 * We need to save and restore the guest visible part of the
3944 * psscr (i.e. using SPRN_PSSCR_PR) since the hypervisor
3945 * doesn't do this for us. Note only required if pseries since
3946 * this is done in kvmhv_vcpu_entry_p9() below otherwise.
3948 unsigned long host_psscr;
3949 /* call our hypervisor to load up HV regs and go */
3950 struct hv_guest_state hvregs;
3952 host_psscr = mfspr(SPRN_PSSCR_PR);
3953 mtspr(SPRN_PSSCR_PR, vcpu->arch.psscr);
3954 kvmhv_save_hv_regs(vcpu, &hvregs);
3956 vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
3957 hvregs.version = HV_GUEST_STATE_VERSION;
3958 if (vcpu->arch.nested) {
3959 hvregs.lpid = vcpu->arch.nested->shadow_lpid;
3960 hvregs.vcpu_token = vcpu->arch.nested_vcpu_id;
3962 hvregs.lpid = vcpu->kvm->arch.lpid;
3963 hvregs.vcpu_token = vcpu->vcpu_id;
3965 hvregs.hdec_expiry = time_limit;
3966 mtspr(SPRN_DAR, vcpu->arch.shregs.dar);
3967 mtspr(SPRN_DSISR, vcpu->arch.shregs.dsisr);
3968 trap = plpar_hcall_norets(H_ENTER_NESTED, __pa(&hvregs),
3969 __pa(&vcpu->arch.regs));
3970 kvmhv_restore_hv_return_state(vcpu, &hvregs);
3971 vcpu->arch.shregs.msr = vcpu->arch.regs.msr;
3972 vcpu->arch.shregs.dar = mfspr(SPRN_DAR);
3973 vcpu->arch.shregs.dsisr = mfspr(SPRN_DSISR);
3974 vcpu->arch.psscr = mfspr(SPRN_PSSCR_PR);
3975 mtspr(SPRN_PSSCR_PR, host_psscr);
3977 /* H_CEDE has to be handled now, not later */
3978 if (trap == BOOK3S_INTERRUPT_SYSCALL && !vcpu->arch.nested &&
3979 kvmppc_get_gpr(vcpu, 3) == H_CEDE) {
3981 kvmppc_set_gpr(vcpu, 3, 0);
3985 kvmppc_xive_push_vcpu(vcpu);
3986 trap = kvmhv_vcpu_entry_p9(vcpu, time_limit, lpcr);
3987 if (trap == BOOK3S_INTERRUPT_SYSCALL && !vcpu->arch.nested &&
3988 !(vcpu->arch.shregs.msr & MSR_PR)) {
3989 unsigned long req = kvmppc_get_gpr(vcpu, 3);
3991 /* H_CEDE has to be handled now, not later */
3992 if (req == H_CEDE) {
3994 kvmppc_xive_rearm_escalation(vcpu); /* may un-cede */
3995 kvmppc_set_gpr(vcpu, 3, 0);
3998 /* XICS hcalls must be handled before xive is pulled */
3999 } else if (hcall_is_xics(req)) {
4002 ret = kvmppc_xive_xics_hcall(vcpu, req);
4003 if (ret != H_TOO_HARD) {
4004 kvmppc_set_gpr(vcpu, 3, ret);
4009 kvmppc_xive_pull_vcpu(vcpu);
4011 if (kvm_is_radix(vcpu->kvm))
4012 vcpu->arch.slb_max = 0;
4015 dec = mfspr(SPRN_DEC);
4016 if (!(lpcr & LPCR_LD)) /* Sign extend if not using large decrementer */
4019 vcpu->arch.dec_expires = dec + tb;
4021 vcpu->arch.thread_cpu = -1;
4023 store_spr_state(vcpu);
4025 restore_p9_host_os_sprs(vcpu, &host_os_sprs);
4027 msr_check_and_set(MSR_FP | MSR_VEC | MSR_VSX);
4028 store_fp_state(&vcpu->arch.fp);
4029 #ifdef CONFIG_ALTIVEC
4030 store_vr_state(&vcpu->arch.vr);
4032 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
4034 if (cpu_has_feature(CPU_FTR_TM) ||
4035 cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
4036 kvmppc_save_tm_hv(vcpu, vcpu->arch.shregs.msr, true);
4039 if (vcpu->arch.vpa.pinned_addr) {
4040 struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
4041 u32 yield_count = be32_to_cpu(lp->yield_count) + 1;
4042 lp->yield_count = cpu_to_be32(yield_count);
4043 vcpu->arch.vpa.dirty = 1;
4044 save_pmu = lp->pmcregs_in_use;
4046 /* Must save pmu if this guest is capable of running nested guests */
4047 save_pmu |= nesting_enabled(vcpu->kvm);
4049 kvmhv_save_guest_pmu(vcpu, save_pmu);
4050 #ifdef CONFIG_PPC_PSERIES
4051 if (kvmhv_on_pseries()) {
4053 get_lppaca()->pmcregs_in_use = ppc_get_pmu_inuse();
4058 vc->entry_exit_map = 0x101;
4061 mtspr(SPRN_DEC, local_paca->kvm_hstate.dec_expires - mftb());
4062 /* We may have raced with new irq work */
4063 if (test_irq_work_pending())
4065 mtspr(SPRN_SPRG_VDSO_WRITE, local_paca->sprg_vdso);
4067 kvmhv_load_host_pmu();
4069 kvmppc_subcore_exit_guest();
4075 * Wait for some other vcpu thread to execute us, and
4076 * wake us up when we need to handle something in the host.
4078 static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
4079 struct kvm_vcpu *vcpu, int wait_state)
4083 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
4084 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
4085 spin_unlock(&vc->lock);
4087 spin_lock(&vc->lock);
4089 finish_wait(&vcpu->arch.cpu_run, &wait);
4092 static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
4094 if (!halt_poll_ns_grow)
4097 vc->halt_poll_ns *= halt_poll_ns_grow;
4098 if (vc->halt_poll_ns < halt_poll_ns_grow_start)
4099 vc->halt_poll_ns = halt_poll_ns_grow_start;
4102 static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
4104 if (halt_poll_ns_shrink == 0)
4105 vc->halt_poll_ns = 0;
4107 vc->halt_poll_ns /= halt_poll_ns_shrink;
4110 #ifdef CONFIG_KVM_XICS
4111 static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
4113 if (!xics_on_xive())
4115 return vcpu->arch.irq_pending || vcpu->arch.xive_saved_state.pipr <
4116 vcpu->arch.xive_saved_state.cppr;
4119 static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
4123 #endif /* CONFIG_KVM_XICS */
4125 static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu)
4127 if (vcpu->arch.pending_exceptions || vcpu->arch.prodded ||
4128 kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu))
4135 * Check to see if any of the runnable vcpus on the vcore have pending
4136 * exceptions or are no longer ceded
4138 static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
4140 struct kvm_vcpu *vcpu;
4143 for_each_runnable_thread(i, vcpu, vc) {
4144 if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu))
4152 * All the vcpus in this vcore are idle, so wait for a decrementer
4153 * or external interrupt to one of the vcpus. vc->lock is held.
4155 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
4157 ktime_t cur, start_poll, start_wait;
4161 /* Poll for pending exceptions and ceded state */
4162 cur = start_poll = ktime_get();
4163 if (vc->halt_poll_ns) {
4164 ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
4165 ++vc->runner->stat.generic.halt_attempted_poll;
4167 vc->vcore_state = VCORE_POLLING;
4168 spin_unlock(&vc->lock);
4171 if (kvmppc_vcore_check_block(vc)) {
4176 } while (kvm_vcpu_can_poll(cur, stop));
4178 spin_lock(&vc->lock);
4179 vc->vcore_state = VCORE_INACTIVE;
4182 ++vc->runner->stat.generic.halt_successful_poll;
4187 prepare_to_rcuwait(&vc->wait);
4188 set_current_state(TASK_INTERRUPTIBLE);
4189 if (kvmppc_vcore_check_block(vc)) {
4190 finish_rcuwait(&vc->wait);
4192 /* If we polled, count this as a successful poll */
4193 if (vc->halt_poll_ns)
4194 ++vc->runner->stat.generic.halt_successful_poll;
4198 start_wait = ktime_get();
4200 vc->vcore_state = VCORE_SLEEPING;
4201 trace_kvmppc_vcore_blocked(vc, 0);
4202 spin_unlock(&vc->lock);
4204 finish_rcuwait(&vc->wait);
4205 spin_lock(&vc->lock);
4206 vc->vcore_state = VCORE_INACTIVE;
4207 trace_kvmppc_vcore_blocked(vc, 1);
4208 ++vc->runner->stat.halt_successful_wait;
4213 block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
4215 /* Attribute wait time */
4217 vc->runner->stat.generic.halt_wait_ns +=
4218 ktime_to_ns(cur) - ktime_to_ns(start_wait);
4219 KVM_STATS_LOG_HIST_UPDATE(
4220 vc->runner->stat.generic.halt_wait_hist,
4221 ktime_to_ns(cur) - ktime_to_ns(start_wait));
4222 /* Attribute failed poll time */
4223 if (vc->halt_poll_ns) {
4224 vc->runner->stat.generic.halt_poll_fail_ns +=
4225 ktime_to_ns(start_wait) -
4226 ktime_to_ns(start_poll);
4227 KVM_STATS_LOG_HIST_UPDATE(
4228 vc->runner->stat.generic.halt_poll_fail_hist,
4229 ktime_to_ns(start_wait) -
4230 ktime_to_ns(start_poll));
4233 /* Attribute successful poll time */
4234 if (vc->halt_poll_ns) {
4235 vc->runner->stat.generic.halt_poll_success_ns +=
4237 ktime_to_ns(start_poll);
4238 KVM_STATS_LOG_HIST_UPDATE(
4239 vc->runner->stat.generic.halt_poll_success_hist,
4240 ktime_to_ns(cur) - ktime_to_ns(start_poll));
4244 /* Adjust poll time */
4246 if (block_ns <= vc->halt_poll_ns)
4248 /* We slept and blocked for longer than the max halt time */
4249 else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
4250 shrink_halt_poll_ns(vc);
4251 /* We slept and our poll time is too small */
4252 else if (vc->halt_poll_ns < halt_poll_ns &&
4253 block_ns < halt_poll_ns)
4254 grow_halt_poll_ns(vc);
4255 if (vc->halt_poll_ns > halt_poll_ns)
4256 vc->halt_poll_ns = halt_poll_ns;
4258 vc->halt_poll_ns = 0;
4260 trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
4264 * This never fails for a radix guest, as none of the operations it does
4265 * for a radix guest can fail or have a way to report failure.
4267 static int kvmhv_setup_mmu(struct kvm_vcpu *vcpu)
4270 struct kvm *kvm = vcpu->kvm;
4272 mutex_lock(&kvm->arch.mmu_setup_lock);
4273 if (!kvm->arch.mmu_ready) {
4274 if (!kvm_is_radix(kvm))
4275 r = kvmppc_hv_setup_htab_rma(vcpu);
4277 if (cpu_has_feature(CPU_FTR_ARCH_300))
4278 kvmppc_setup_partition_table(kvm);
4279 kvm->arch.mmu_ready = 1;
4282 mutex_unlock(&kvm->arch.mmu_setup_lock);
4286 static int kvmppc_run_vcpu(struct kvm_vcpu *vcpu)
4288 struct kvm_run *run = vcpu->run;
4290 struct kvmppc_vcore *vc;
4293 trace_kvmppc_run_vcpu_enter(vcpu);
4295 run->exit_reason = 0;
4296 vcpu->arch.ret = RESUME_GUEST;
4297 vcpu->arch.trap = 0;
4298 kvmppc_update_vpas(vcpu);
4301 * Synchronize with other threads in this virtual core
4303 vc = vcpu->arch.vcore;
4304 spin_lock(&vc->lock);
4305 vcpu->arch.ceded = 0;
4306 vcpu->arch.run_task = current;
4307 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
4308 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
4309 vcpu->arch.busy_preempt = TB_NIL;
4310 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
4314 * This happens the first time this is called for a vcpu.
4315 * If the vcore is already running, we may be able to start
4316 * this thread straight away and have it join in.
4318 if (!signal_pending(current)) {
4319 if ((vc->vcore_state == VCORE_PIGGYBACK ||
4320 vc->vcore_state == VCORE_RUNNING) &&
4321 !VCORE_IS_EXITING(vc)) {
4322 kvmppc_create_dtl_entry(vcpu, vc);
4323 kvmppc_start_thread(vcpu, vc);
4324 trace_kvm_guest_enter(vcpu);
4325 } else if (vc->vcore_state == VCORE_SLEEPING) {
4326 rcuwait_wake_up(&vc->wait);
4331 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
4332 !signal_pending(current)) {
4333 /* See if the MMU is ready to go */
4334 if (!vcpu->kvm->arch.mmu_ready) {
4335 spin_unlock(&vc->lock);
4336 r = kvmhv_setup_mmu(vcpu);
4337 spin_lock(&vc->lock);
4339 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4341 hardware_entry_failure_reason = 0;
4347 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
4348 kvmppc_vcore_end_preempt(vc);
4350 if (vc->vcore_state != VCORE_INACTIVE) {
4351 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
4354 for_each_runnable_thread(i, v, vc) {
4355 kvmppc_core_prepare_to_enter(v);
4356 if (signal_pending(v->arch.run_task)) {
4357 kvmppc_remove_runnable(vc, v);
4358 v->stat.signal_exits++;
4359 v->run->exit_reason = KVM_EXIT_INTR;
4360 v->arch.ret = -EINTR;
4361 wake_up(&v->arch.cpu_run);
4364 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
4367 for_each_runnable_thread(i, v, vc) {
4368 if (!kvmppc_vcpu_woken(v))
4369 n_ceded += v->arch.ceded;
4374 if (n_ceded == vc->n_runnable) {
4375 kvmppc_vcore_blocked(vc);
4376 } else if (need_resched()) {
4377 kvmppc_vcore_preempt(vc);
4378 /* Let something else run */
4379 cond_resched_lock(&vc->lock);
4380 if (vc->vcore_state == VCORE_PREEMPT)
4381 kvmppc_vcore_end_preempt(vc);
4383 kvmppc_run_core(vc);
4388 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
4389 (vc->vcore_state == VCORE_RUNNING ||
4390 vc->vcore_state == VCORE_EXITING ||
4391 vc->vcore_state == VCORE_PIGGYBACK))
4392 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
4394 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
4395 kvmppc_vcore_end_preempt(vc);
4397 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
4398 kvmppc_remove_runnable(vc, vcpu);
4399 vcpu->stat.signal_exits++;
4400 run->exit_reason = KVM_EXIT_INTR;
4401 vcpu->arch.ret = -EINTR;
4404 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
4405 /* Wake up some vcpu to run the core */
4407 v = next_runnable_thread(vc, &i);
4408 wake_up(&v->arch.cpu_run);
4411 trace_kvmppc_run_vcpu_exit(vcpu);
4412 spin_unlock(&vc->lock);
4413 return vcpu->arch.ret;
4416 int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
4419 struct kvm_run *run = vcpu->run;
4422 struct kvmppc_vcore *vc;
4423 struct kvm *kvm = vcpu->kvm;
4424 struct kvm_nested_guest *nested = vcpu->arch.nested;
4426 trace_kvmppc_run_vcpu_enter(vcpu);
4428 run->exit_reason = 0;
4429 vcpu->arch.ret = RESUME_GUEST;
4430 vcpu->arch.trap = 0;
4432 vc = vcpu->arch.vcore;
4433 vcpu->arch.ceded = 0;
4434 vcpu->arch.run_task = current;
4435 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
4436 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
4437 vcpu->arch.busy_preempt = TB_NIL;
4438 vcpu->arch.last_inst = KVM_INST_FETCH_FAILED;
4439 vc->runnable_threads[0] = vcpu;
4443 /* See if the MMU is ready to go */
4444 if (!kvm->arch.mmu_ready) {
4445 r = kvmhv_setup_mmu(vcpu);
4447 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4448 run->fail_entry.hardware_entry_failure_reason = 0;
4457 kvmppc_update_vpas(vcpu);
4459 init_vcore_to_run(vc);
4460 vc->preempt_tb = TB_NIL;
4463 pcpu = smp_processor_id();
4465 if (kvm_is_radix(kvm))
4466 kvmppc_prepare_radix_vcpu(vcpu, pcpu);
4468 local_irq_disable();
4470 if (signal_pending(current))
4472 if (lazy_irq_pending() || need_resched() || !kvm->arch.mmu_ready)
4476 kvmppc_core_prepare_to_enter(vcpu);
4477 if (vcpu->arch.doorbell_request) {
4480 vcpu->arch.doorbell_request = 0;
4482 if (test_bit(BOOK3S_IRQPRIO_EXTERNAL,
4483 &vcpu->arch.pending_exceptions))
4485 } else if (vcpu->arch.pending_exceptions ||
4486 vcpu->arch.doorbell_request ||
4487 xive_interrupt_pending(vcpu)) {
4488 vcpu->arch.ret = RESUME_HOST;
4492 kvmppc_clear_host_core(pcpu);
4494 local_paca->kvm_hstate.napping = 0;
4495 local_paca->kvm_hstate.kvm_split_mode = NULL;
4496 kvmppc_start_thread(vcpu, vc);
4497 kvmppc_create_dtl_entry(vcpu, vc);
4498 trace_kvm_guest_enter(vcpu);
4500 vc->vcore_state = VCORE_RUNNING;
4501 trace_kvmppc_run_core(vc, 0);
4503 guest_enter_irqoff();
4505 srcu_idx = srcu_read_lock(&kvm->srcu);
4507 this_cpu_disable_ftrace();
4509 /* Tell lockdep that we're about to enable interrupts */
4510 trace_hardirqs_on();
4512 trap = kvmhv_p9_guest_entry(vcpu, time_limit, lpcr);
4513 vcpu->arch.trap = trap;
4515 trace_hardirqs_off();
4517 this_cpu_enable_ftrace();
4519 srcu_read_unlock(&kvm->srcu, srcu_idx);
4521 set_irq_happened(trap);
4523 kvmppc_set_host_core(pcpu);
4525 context_tracking_guest_exit();
4526 if (!vtime_accounting_enabled_this_cpu()) {
4529 * Service IRQs here before vtime_account_guest_exit() so any
4530 * ticks that occurred while running the guest are accounted to
4531 * the guest. If vtime accounting is enabled, accounting uses
4532 * TB rather than ticks, so it can be done without enabling
4533 * interrupts here, which has the problem that it accounts
4534 * interrupt processing overhead to the host.
4536 local_irq_disable();
4538 vtime_account_guest_exit();
4542 cpumask_clear_cpu(pcpu, &kvm->arch.cpu_in_guest);
4547 * cancel pending decrementer exception if DEC is now positive, or if
4548 * entering a nested guest in which case the decrementer is now owned
4549 * by L2 and the L1 decrementer is provided in hdec_expires
4551 if (kvmppc_core_pending_dec(vcpu) &&
4552 ((get_tb() < vcpu->arch.dec_expires) ||
4553 (trap == BOOK3S_INTERRUPT_SYSCALL &&
4554 kvmppc_get_gpr(vcpu, 3) == H_ENTER_NESTED)))
4555 kvmppc_core_dequeue_dec(vcpu);
4557 trace_kvm_guest_exit(vcpu);
4561 r = kvmppc_handle_exit_hv(vcpu, current);
4563 r = kvmppc_handle_nested_exit(vcpu);
4567 if (is_kvmppc_resume_guest(r) && vcpu->arch.ceded &&
4568 !kvmppc_vcpu_woken(vcpu)) {
4569 kvmppc_set_timer(vcpu);
4570 while (vcpu->arch.ceded && !kvmppc_vcpu_woken(vcpu)) {
4571 if (signal_pending(current)) {
4572 vcpu->stat.signal_exits++;
4573 run->exit_reason = KVM_EXIT_INTR;
4574 vcpu->arch.ret = -EINTR;
4577 spin_lock(&vc->lock);
4578 kvmppc_vcore_blocked(vc);
4579 spin_unlock(&vc->lock);
4582 vcpu->arch.ceded = 0;
4584 vc->vcore_state = VCORE_INACTIVE;
4585 trace_kvmppc_run_core(vc, 1);
4588 kvmppc_remove_runnable(vc, vcpu);
4589 trace_kvmppc_run_vcpu_exit(vcpu);
4591 return vcpu->arch.ret;
4594 vcpu->stat.signal_exits++;
4595 run->exit_reason = KVM_EXIT_INTR;
4596 vcpu->arch.ret = -EINTR;
4603 static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)
4605 struct kvm_run *run = vcpu->run;
4608 unsigned long ebb_regs[3] = {}; /* shut up GCC */
4609 unsigned long user_tar = 0;
4610 unsigned int user_vrsave;
4613 if (!vcpu->arch.sane) {
4614 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4619 * Don't allow entry with a suspended transaction, because
4620 * the guest entry/exit code will lose it.
4621 * If the guest has TM enabled, save away their TM-related SPRs
4622 * (they will get restored by the TM unavailable interrupt).
4624 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
4625 if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
4626 (current->thread.regs->msr & MSR_TM)) {
4627 if (MSR_TM_ACTIVE(current->thread.regs->msr)) {
4628 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4629 run->fail_entry.hardware_entry_failure_reason = 0;
4632 /* Enable TM so we can read the TM SPRs */
4633 mtmsr(mfmsr() | MSR_TM);
4634 current->thread.tm_tfhar = mfspr(SPRN_TFHAR);
4635 current->thread.tm_tfiar = mfspr(SPRN_TFIAR);
4636 current->thread.tm_texasr = mfspr(SPRN_TEXASR);
4637 current->thread.regs->msr &= ~MSR_TM;
4642 * Force online to 1 for the sake of old userspace which doesn't
4645 if (!vcpu->arch.online) {
4646 atomic_inc(&vcpu->arch.vcore->online_count);
4647 vcpu->arch.online = 1;
4650 kvmppc_core_prepare_to_enter(vcpu);
4652 /* No need to go into the guest when all we'll do is come back out */
4653 if (signal_pending(current)) {
4654 run->exit_reason = KVM_EXIT_INTR;
4659 atomic_inc(&kvm->arch.vcpus_running);
4660 /* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */
4663 flush_all_to_thread(current);
4665 /* Save userspace EBB and other register values */
4666 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
4667 ebb_regs[0] = mfspr(SPRN_EBBHR);
4668 ebb_regs[1] = mfspr(SPRN_EBBRR);
4669 ebb_regs[2] = mfspr(SPRN_BESCR);
4670 user_tar = mfspr(SPRN_TAR);
4672 user_vrsave = mfspr(SPRN_VRSAVE);
4674 vcpu->arch.waitp = &vcpu->arch.vcore->wait;
4675 vcpu->arch.pgdir = kvm->mm->pgd;
4676 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
4679 if (cpu_has_feature(CPU_FTR_ARCH_300))
4680 r = kvmhv_run_single_vcpu(vcpu, ~(u64)0,
4681 vcpu->arch.vcore->lpcr);
4683 r = kvmppc_run_vcpu(vcpu);
4685 if (run->exit_reason == KVM_EXIT_PAPR_HCALL) {
4686 if (WARN_ON_ONCE(vcpu->arch.shregs.msr & MSR_PR)) {
4688 * These should have been caught reflected
4689 * into the guest by now. Final sanity check:
4690 * don't allow userspace to execute hcalls in
4696 trace_kvm_hcall_enter(vcpu);
4697 r = kvmppc_pseries_do_hcall(vcpu);
4698 trace_kvm_hcall_exit(vcpu, r);
4699 kvmppc_core_prepare_to_enter(vcpu);
4700 } else if (r == RESUME_PAGE_FAULT) {
4701 srcu_idx = srcu_read_lock(&kvm->srcu);
4702 r = kvmppc_book3s_hv_page_fault(vcpu,
4703 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
4704 srcu_read_unlock(&kvm->srcu, srcu_idx);
4705 } else if (r == RESUME_PASSTHROUGH) {
4706 if (WARN_ON(xics_on_xive()))
4709 r = kvmppc_xics_rm_complete(vcpu, 0);
4711 } while (is_kvmppc_resume_guest(r));
4713 /* Restore userspace EBB and other register values */
4714 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
4715 mtspr(SPRN_EBBHR, ebb_regs[0]);
4716 mtspr(SPRN_EBBRR, ebb_regs[1]);
4717 mtspr(SPRN_BESCR, ebb_regs[2]);
4718 mtspr(SPRN_TAR, user_tar);
4720 mtspr(SPRN_VRSAVE, user_vrsave);
4722 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
4723 atomic_dec(&kvm->arch.vcpus_running);
4725 srr_regs_clobbered();
4730 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
4731 int shift, int sllp)
4733 (*sps)->page_shift = shift;
4734 (*sps)->slb_enc = sllp;
4735 (*sps)->enc[0].page_shift = shift;
4736 (*sps)->enc[0].pte_enc = kvmppc_pgsize_lp_encoding(shift, shift);
4738 * Add 16MB MPSS support (may get filtered out by userspace)
4741 int penc = kvmppc_pgsize_lp_encoding(shift, 24);
4743 (*sps)->enc[1].page_shift = 24;
4744 (*sps)->enc[1].pte_enc = penc;
4750 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
4751 struct kvm_ppc_smmu_info *info)
4753 struct kvm_ppc_one_seg_page_size *sps;
4756 * POWER7, POWER8 and POWER9 all support 32 storage keys for data.
4757 * POWER7 doesn't support keys for instruction accesses,
4758 * POWER8 and POWER9 do.
4760 info->data_keys = 32;
4761 info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0;
4763 /* POWER7, 8 and 9 all have 1T segments and 32-entry SLB */
4764 info->flags = KVM_PPC_PAGE_SIZES_REAL | KVM_PPC_1T_SEGMENTS;
4765 info->slb_size = 32;
4767 /* We only support these sizes for now, and no muti-size segments */
4768 sps = &info->sps[0];
4769 kvmppc_add_seg_page_size(&sps, 12, 0);
4770 kvmppc_add_seg_page_size(&sps, 16, SLB_VSID_L | SLB_VSID_LP_01);
4771 kvmppc_add_seg_page_size(&sps, 24, SLB_VSID_L);
4773 /* If running as a nested hypervisor, we don't support HPT guests */
4774 if (kvmhv_on_pseries())
4775 info->flags |= KVM_PPC_NO_HASH;
4781 * Get (and clear) the dirty memory log for a memory slot.
4783 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
4784 struct kvm_dirty_log *log)
4786 struct kvm_memslots *slots;
4787 struct kvm_memory_slot *memslot;
4790 unsigned long *buf, *p;
4791 struct kvm_vcpu *vcpu;
4793 mutex_lock(&kvm->slots_lock);
4796 if (log->slot >= KVM_USER_MEM_SLOTS)
4799 slots = kvm_memslots(kvm);
4800 memslot = id_to_memslot(slots, log->slot);
4802 if (!memslot || !memslot->dirty_bitmap)
4806 * Use second half of bitmap area because both HPT and radix
4807 * accumulate bits in the first half.
4809 n = kvm_dirty_bitmap_bytes(memslot);
4810 buf = memslot->dirty_bitmap + n / sizeof(long);
4813 if (kvm_is_radix(kvm))
4814 r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
4816 r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
4821 * We accumulate dirty bits in the first half of the
4822 * memslot's dirty_bitmap area, for when pages are paged
4823 * out or modified by the host directly. Pick up these
4824 * bits and add them to the map.
4826 p = memslot->dirty_bitmap;
4827 for (i = 0; i < n / sizeof(long); ++i)
4828 buf[i] |= xchg(&p[i], 0);
4830 /* Harvest dirty bits from VPA and DTL updates */
4831 /* Note: we never modify the SLB shadow buffer areas */
4832 kvm_for_each_vcpu(i, vcpu, kvm) {
4833 spin_lock(&vcpu->arch.vpa_update_lock);
4834 kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
4835 kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
4836 spin_unlock(&vcpu->arch.vpa_update_lock);
4840 if (copy_to_user(log->dirty_bitmap, buf, n))
4845 mutex_unlock(&kvm->slots_lock);
4849 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *slot)
4851 vfree(slot->arch.rmap);
4852 slot->arch.rmap = NULL;
4855 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
4856 struct kvm_memory_slot *slot,
4857 const struct kvm_userspace_memory_region *mem,
4858 enum kvm_mr_change change)
4860 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
4862 if (change == KVM_MR_CREATE) {
4863 unsigned long size = array_size(npages, sizeof(*slot->arch.rmap));
4865 if ((size >> PAGE_SHIFT) > totalram_pages())
4868 slot->arch.rmap = vzalloc(size);
4869 if (!slot->arch.rmap)
4876 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
4877 const struct kvm_userspace_memory_region *mem,
4878 const struct kvm_memory_slot *old,
4879 const struct kvm_memory_slot *new,
4880 enum kvm_mr_change change)
4882 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
4885 * If we are making a new memslot, it might make
4886 * some address that was previously cached as emulated
4887 * MMIO be no longer emulated MMIO, so invalidate
4888 * all the caches of emulated MMIO translations.
4891 atomic64_inc(&kvm->arch.mmio_update);
4894 * For change == KVM_MR_MOVE or KVM_MR_DELETE, higher levels
4895 * have already called kvm_arch_flush_shadow_memslot() to
4896 * flush shadow mappings. For KVM_MR_CREATE we have no
4897 * previous mappings. So the only case to handle is
4898 * KVM_MR_FLAGS_ONLY when the KVM_MEM_LOG_DIRTY_PAGES bit
4900 * For radix guests, we flush on setting KVM_MEM_LOG_DIRTY_PAGES
4901 * to get rid of any THP PTEs in the partition-scoped page tables
4902 * so we can track dirtiness at the page level; we flush when
4903 * clearing KVM_MEM_LOG_DIRTY_PAGES so that we can go back to
4906 if (change == KVM_MR_FLAGS_ONLY && kvm_is_radix(kvm) &&
4907 ((new->flags ^ old->flags) & KVM_MEM_LOG_DIRTY_PAGES))
4908 kvmppc_radix_flush_memslot(kvm, old);
4910 * If UV hasn't yet called H_SVM_INIT_START, don't register memslots.
4912 if (!kvm->arch.secure_guest)
4918 * @TODO kvmppc_uvmem_memslot_create() can fail and
4919 * return error. Fix this.
4921 kvmppc_uvmem_memslot_create(kvm, new);
4924 kvmppc_uvmem_memslot_delete(kvm, old);
4927 /* TODO: Handle KVM_MR_MOVE */
4933 * Update LPCR values in kvm->arch and in vcores.
4934 * Caller must hold kvm->arch.mmu_setup_lock (for mutual exclusion
4935 * of kvm->arch.lpcr update).
4937 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
4942 if ((kvm->arch.lpcr & mask) == lpcr)
4945 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
4947 for (i = 0; i < KVM_MAX_VCORES; ++i) {
4948 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
4952 spin_lock(&vc->lock);
4953 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
4954 verify_lpcr(kvm, vc->lpcr);
4955 spin_unlock(&vc->lock);
4956 if (++cores_done >= kvm->arch.online_vcores)
4961 void kvmppc_setup_partition_table(struct kvm *kvm)
4963 unsigned long dw0, dw1;
4965 if (!kvm_is_radix(kvm)) {
4966 /* PS field - page size for VRMA */
4967 dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
4968 ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
4969 /* HTABSIZE and HTABORG fields */
4970 dw0 |= kvm->arch.sdr1;
4972 /* Second dword as set by userspace */
4973 dw1 = kvm->arch.process_table;
4975 dw0 = PATB_HR | radix__get_tree_size() |
4976 __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
4977 dw1 = PATB_GR | kvm->arch.process_table;
4979 kvmhv_set_ptbl_entry(kvm->arch.lpid, dw0, dw1);
4983 * Set up HPT (hashed page table) and RMA (real-mode area).
4984 * Must be called with kvm->arch.mmu_setup_lock held.
4986 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
4989 struct kvm *kvm = vcpu->kvm;
4991 struct kvm_memory_slot *memslot;
4992 struct vm_area_struct *vma;
4993 unsigned long lpcr = 0, senc;
4994 unsigned long psize, porder;
4997 /* Allocate hashed page table (if not done already) and reset it */
4998 if (!kvm->arch.hpt.virt) {
4999 int order = KVM_DEFAULT_HPT_ORDER;
5000 struct kvm_hpt_info info;
5002 err = kvmppc_allocate_hpt(&info, order);
5003 /* If we get here, it means userspace didn't specify a
5004 * size explicitly. So, try successively smaller
5005 * sizes if the default failed. */
5006 while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
5007 err = kvmppc_allocate_hpt(&info, order);
5010 pr_err("KVM: Couldn't alloc HPT\n");
5014 kvmppc_set_hpt(kvm, &info);
5017 /* Look up the memslot for guest physical address 0 */
5018 srcu_idx = srcu_read_lock(&kvm->srcu);
5019 memslot = gfn_to_memslot(kvm, 0);
5021 /* We must have some memory at 0 by now */
5023 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
5026 /* Look up the VMA for the start of this memory slot */
5027 hva = memslot->userspace_addr;
5028 mmap_read_lock(kvm->mm);
5029 vma = vma_lookup(kvm->mm, hva);
5030 if (!vma || (vma->vm_flags & VM_IO))
5033 psize = vma_kernel_pagesize(vma);
5035 mmap_read_unlock(kvm->mm);
5037 /* We can handle 4k, 64k or 16M pages in the VRMA */
5038 if (psize >= 0x1000000)
5040 else if (psize >= 0x10000)
5044 porder = __ilog2(psize);
5046 senc = slb_pgsize_encoding(psize);
5047 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
5048 (VRMA_VSID << SLB_VSID_SHIFT_1T);
5049 /* Create HPTEs in the hash page table for the VRMA */
5050 kvmppc_map_vrma(vcpu, memslot, porder);
5052 /* Update VRMASD field in the LPCR */
5053 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
5054 /* the -4 is to account for senc values starting at 0x10 */
5055 lpcr = senc << (LPCR_VRMASD_SH - 4);
5056 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
5059 /* Order updates to kvm->arch.lpcr etc. vs. mmu_ready */
5063 srcu_read_unlock(&kvm->srcu, srcu_idx);
5068 mmap_read_unlock(kvm->mm);
5073 * Must be called with kvm->arch.mmu_setup_lock held and
5074 * mmu_ready = 0 and no vcpus running.
5076 int kvmppc_switch_mmu_to_hpt(struct kvm *kvm)
5078 if (nesting_enabled(kvm))
5079 kvmhv_release_all_nested(kvm);
5080 kvmppc_rmap_reset(kvm);
5081 kvm->arch.process_table = 0;
5082 /* Mutual exclusion with kvm_unmap_gfn_range etc. */
5083 spin_lock(&kvm->mmu_lock);
5084 kvm->arch.radix = 0;
5085 spin_unlock(&kvm->mmu_lock);
5086 kvmppc_free_radix(kvm);
5087 kvmppc_update_lpcr(kvm, LPCR_VPM1,
5088 LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
5093 * Must be called with kvm->arch.mmu_setup_lock held and
5094 * mmu_ready = 0 and no vcpus running.
5096 int kvmppc_switch_mmu_to_radix(struct kvm *kvm)
5100 err = kvmppc_init_vm_radix(kvm);
5103 kvmppc_rmap_reset(kvm);
5104 /* Mutual exclusion with kvm_unmap_gfn_range etc. */
5105 spin_lock(&kvm->mmu_lock);
5106 kvm->arch.radix = 1;
5107 spin_unlock(&kvm->mmu_lock);
5108 kvmppc_free_hpt(&kvm->arch.hpt);
5109 kvmppc_update_lpcr(kvm, LPCR_UPRT | LPCR_GTSE | LPCR_HR,
5110 LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
5114 #ifdef CONFIG_KVM_XICS
5116 * Allocate a per-core structure for managing state about which cores are
5117 * running in the host versus the guest and for exchanging data between
5118 * real mode KVM and CPU running in the host.
5119 * This is only done for the first VM.
5120 * The allocated structure stays even if all VMs have stopped.
5121 * It is only freed when the kvm-hv module is unloaded.
5122 * It's OK for this routine to fail, we just don't support host
5123 * core operations like redirecting H_IPI wakeups.
5125 void kvmppc_alloc_host_rm_ops(void)
5127 struct kvmppc_host_rm_ops *ops;
5128 unsigned long l_ops;
5132 /* Not the first time here ? */
5133 if (kvmppc_host_rm_ops_hv != NULL)
5136 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
5140 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
5141 ops->rm_core = kzalloc(size, GFP_KERNEL);
5143 if (!ops->rm_core) {
5150 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
5151 if (!cpu_online(cpu))
5154 core = cpu >> threads_shift;
5155 ops->rm_core[core].rm_state.in_host = 1;
5158 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
5161 * Make the contents of the kvmppc_host_rm_ops structure visible
5162 * to other CPUs before we assign it to the global variable.
5163 * Do an atomic assignment (no locks used here), but if someone
5164 * beats us to it, just free our copy and return.
5167 l_ops = (unsigned long) ops;
5169 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
5171 kfree(ops->rm_core);
5176 cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE,
5177 "ppc/kvm_book3s:prepare",
5178 kvmppc_set_host_core,
5179 kvmppc_clear_host_core);
5183 void kvmppc_free_host_rm_ops(void)
5185 if (kvmppc_host_rm_ops_hv) {
5186 cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
5187 kfree(kvmppc_host_rm_ops_hv->rm_core);
5188 kfree(kvmppc_host_rm_ops_hv);
5189 kvmppc_host_rm_ops_hv = NULL;
5194 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
5196 unsigned long lpcr, lpid;
5200 mutex_init(&kvm->arch.uvmem_lock);
5201 INIT_LIST_HEAD(&kvm->arch.uvmem_pfns);
5202 mutex_init(&kvm->arch.mmu_setup_lock);
5204 /* Allocate the guest's logical partition ID */
5206 lpid = kvmppc_alloc_lpid();
5209 kvm->arch.lpid = lpid;
5211 kvmppc_alloc_host_rm_ops();
5213 kvmhv_vm_nested_init(kvm);
5216 * Since we don't flush the TLB when tearing down a VM,
5217 * and this lpid might have previously been used,
5218 * make sure we flush on each core before running the new VM.
5219 * On POWER9, the tlbie in mmu_partition_table_set_entry()
5220 * does this flush for us.
5222 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5223 cpumask_setall(&kvm->arch.need_tlb_flush);
5225 /* Start out with the default set of hcalls enabled */
5226 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
5227 sizeof(kvm->arch.enabled_hcalls));
5229 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5230 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
5232 /* Init LPCR for virtual RMA mode */
5233 if (cpu_has_feature(CPU_FTR_HVMODE)) {
5234 kvm->arch.host_lpid = mfspr(SPRN_LPID);
5235 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
5236 lpcr &= LPCR_PECE | LPCR_LPES;
5239 * The L2 LPES mode will be set by the L0 according to whether
5240 * or not it needs to take external interrupts in HV mode.
5244 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
5245 LPCR_VPM0 | LPCR_VPM1;
5246 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
5247 (VRMA_VSID << SLB_VSID_SHIFT_1T);
5248 /* On POWER8 turn on online bit to enable PURR/SPURR */
5249 if (cpu_has_feature(CPU_FTR_ARCH_207S))
5252 * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
5253 * Set HVICE bit to enable hypervisor virtualization interrupts.
5254 * Set HEIC to prevent OS interrupts to go to hypervisor (should
5255 * be unnecessary but better safe than sorry in case we re-enable
5256 * EE in HV mode with this LPCR still set)
5258 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
5260 lpcr |= LPCR_HVICE | LPCR_HEIC;
5263 * If xive is enabled, we route 0x500 interrupts directly
5271 * If the host uses radix, the guest starts out as radix.
5273 if (radix_enabled()) {
5274 kvm->arch.radix = 1;
5275 kvm->arch.mmu_ready = 1;
5277 lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5278 ret = kvmppc_init_vm_radix(kvm);
5280 kvmppc_free_lpid(kvm->arch.lpid);
5283 kvmppc_setup_partition_table(kvm);
5286 verify_lpcr(kvm, lpcr);
5287 kvm->arch.lpcr = lpcr;
5289 /* Initialization for future HPT resizes */
5290 kvm->arch.resize_hpt = NULL;
5293 * Work out how many sets the TLB has, for the use of
5294 * the TLB invalidation loop in book3s_hv_rmhandlers.S.
5296 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
5298 * P10 will flush all the congruence class with a single tlbiel
5300 kvm->arch.tlb_sets = 1;
5301 } else if (radix_enabled())
5302 kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
5303 else if (cpu_has_feature(CPU_FTR_ARCH_300))
5304 kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
5305 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
5306 kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
5308 kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
5311 * Track that we now have a HV mode VM active. This blocks secondary
5312 * CPU threads from coming online.
5314 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5315 kvm_hv_vm_activated();
5318 * Initialize smt_mode depending on processor.
5319 * POWER8 and earlier have to use "strict" threading, where
5320 * all vCPUs in a vcore have to run on the same (sub)core,
5321 * whereas on POWER9 the threads can each run a different
5324 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5325 kvm->arch.smt_mode = threads_per_subcore;
5327 kvm->arch.smt_mode = 1;
5328 kvm->arch.emul_smt_mode = 1;
5331 * Create a debugfs directory for the VM
5333 snprintf(buf, sizeof(buf), "vm%d", current->pid);
5334 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
5335 kvmppc_mmu_debugfs_init(kvm);
5336 if (radix_enabled())
5337 kvmhv_radix_debugfs_init(kvm);
5342 static void kvmppc_free_vcores(struct kvm *kvm)
5346 for (i = 0; i < KVM_MAX_VCORES; ++i)
5347 kfree(kvm->arch.vcores[i]);
5348 kvm->arch.online_vcores = 0;
5351 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
5353 debugfs_remove_recursive(kvm->arch.debugfs_dir);
5355 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5356 kvm_hv_vm_deactivated();
5358 kvmppc_free_vcores(kvm);
5361 if (kvm_is_radix(kvm))
5362 kvmppc_free_radix(kvm);
5364 kvmppc_free_hpt(&kvm->arch.hpt);
5366 /* Perform global invalidation and return lpid to the pool */
5367 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
5368 if (nesting_enabled(kvm))
5369 kvmhv_release_all_nested(kvm);
5370 kvm->arch.process_table = 0;
5371 if (kvm->arch.secure_guest)
5372 uv_svm_terminate(kvm->arch.lpid);
5373 kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0);
5376 kvmppc_free_lpid(kvm->arch.lpid);
5378 kvmppc_free_pimap(kvm);
5381 /* We don't need to emulate any privileged instructions or dcbz */
5382 static int kvmppc_core_emulate_op_hv(struct kvm_vcpu *vcpu,
5383 unsigned int inst, int *advance)
5385 return EMULATE_FAIL;
5388 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
5391 return EMULATE_FAIL;
5394 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
5397 return EMULATE_FAIL;
5400 static int kvmppc_core_check_processor_compat_hv(void)
5402 if (cpu_has_feature(CPU_FTR_HVMODE) &&
5403 cpu_has_feature(CPU_FTR_ARCH_206))
5406 /* POWER9 in radix mode is capable of being a nested hypervisor. */
5407 if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled())
5413 #ifdef CONFIG_KVM_XICS
5415 void kvmppc_free_pimap(struct kvm *kvm)
5417 kfree(kvm->arch.pimap);
5420 static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
5422 return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
5425 static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
5427 struct irq_desc *desc;
5428 struct kvmppc_irq_map *irq_map;
5429 struct kvmppc_passthru_irqmap *pimap;
5430 struct irq_chip *chip;
5432 struct irq_data *host_data;
5434 if (!kvm_irq_bypass)
5437 desc = irq_to_desc(host_irq);
5441 mutex_lock(&kvm->lock);
5443 pimap = kvm->arch.pimap;
5444 if (pimap == NULL) {
5445 /* First call, allocate structure to hold IRQ map */
5446 pimap = kvmppc_alloc_pimap();
5447 if (pimap == NULL) {
5448 mutex_unlock(&kvm->lock);
5451 kvm->arch.pimap = pimap;
5455 * For now, we only support interrupts for which the EOI operation
5456 * is an OPAL call followed by a write to XIRR, since that's
5457 * what our real-mode EOI code does, or a XIVE interrupt
5459 chip = irq_data_get_irq_chip(&desc->irq_data);
5460 if (!chip || !is_pnv_opal_msi(chip)) {
5461 pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
5462 host_irq, guest_gsi);
5463 mutex_unlock(&kvm->lock);
5468 * See if we already have an entry for this guest IRQ number.
5469 * If it's mapped to a hardware IRQ number, that's an error,
5470 * otherwise re-use this entry.
5472 for (i = 0; i < pimap->n_mapped; i++) {
5473 if (guest_gsi == pimap->mapped[i].v_hwirq) {
5474 if (pimap->mapped[i].r_hwirq) {
5475 mutex_unlock(&kvm->lock);
5482 if (i == KVMPPC_PIRQ_MAPPED) {
5483 mutex_unlock(&kvm->lock);
5484 return -EAGAIN; /* table is full */
5487 irq_map = &pimap->mapped[i];
5489 irq_map->v_hwirq = guest_gsi;
5490 irq_map->desc = desc;
5493 * Order the above two stores before the next to serialize with
5494 * the KVM real mode handler.
5499 * The 'host_irq' number is mapped in the PCI-MSI domain but
5500 * the underlying calls, which will EOI the interrupt in real
5501 * mode, need an HW IRQ number mapped in the XICS IRQ domain.
5503 host_data = irq_domain_get_irq_data(irq_get_default_host(), host_irq);
5504 irq_map->r_hwirq = (unsigned int)irqd_to_hwirq(host_data);
5506 if (i == pimap->n_mapped)
5510 rc = kvmppc_xive_set_mapped(kvm, guest_gsi, host_irq);
5512 kvmppc_xics_set_mapped(kvm, guest_gsi, irq_map->r_hwirq);
5514 irq_map->r_hwirq = 0;
5516 mutex_unlock(&kvm->lock);
5521 static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
5523 struct irq_desc *desc;
5524 struct kvmppc_passthru_irqmap *pimap;
5527 if (!kvm_irq_bypass)
5530 desc = irq_to_desc(host_irq);
5534 mutex_lock(&kvm->lock);
5535 if (!kvm->arch.pimap)
5538 pimap = kvm->arch.pimap;
5540 for (i = 0; i < pimap->n_mapped; i++) {
5541 if (guest_gsi == pimap->mapped[i].v_hwirq)
5545 if (i == pimap->n_mapped) {
5546 mutex_unlock(&kvm->lock);
5551 rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, host_irq);
5553 kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
5555 /* invalidate the entry (what do do on error from the above ?) */
5556 pimap->mapped[i].r_hwirq = 0;
5559 * We don't free this structure even when the count goes to
5560 * zero. The structure is freed when we destroy the VM.
5563 mutex_unlock(&kvm->lock);
5567 static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
5568 struct irq_bypass_producer *prod)
5571 struct kvm_kernel_irqfd *irqfd =
5572 container_of(cons, struct kvm_kernel_irqfd, consumer);
5574 irqfd->producer = prod;
5576 ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
5578 pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
5579 prod->irq, irqfd->gsi, ret);
5584 static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
5585 struct irq_bypass_producer *prod)
5588 struct kvm_kernel_irqfd *irqfd =
5589 container_of(cons, struct kvm_kernel_irqfd, consumer);
5591 irqfd->producer = NULL;
5594 * When producer of consumer is unregistered, we change back to
5595 * default external interrupt handling mode - KVM real mode
5596 * will switch back to host.
5598 ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
5600 pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
5601 prod->irq, irqfd->gsi, ret);
5605 static long kvm_arch_vm_ioctl_hv(struct file *filp,
5606 unsigned int ioctl, unsigned long arg)
5608 struct kvm *kvm __maybe_unused = filp->private_data;
5609 void __user *argp = (void __user *)arg;
5614 case KVM_PPC_ALLOCATE_HTAB: {
5617 /* If we're a nested hypervisor, we currently only support radix */
5618 if (kvmhv_on_pseries()) {
5624 if (get_user(htab_order, (u32 __user *)argp))
5626 r = kvmppc_alloc_reset_hpt(kvm, htab_order);
5633 case KVM_PPC_GET_HTAB_FD: {
5634 struct kvm_get_htab_fd ghf;
5637 if (copy_from_user(&ghf, argp, sizeof(ghf)))
5639 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
5643 case KVM_PPC_RESIZE_HPT_PREPARE: {
5644 struct kvm_ppc_resize_hpt rhpt;
5647 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
5650 r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
5654 case KVM_PPC_RESIZE_HPT_COMMIT: {
5655 struct kvm_ppc_resize_hpt rhpt;
5658 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
5661 r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
5673 * List of hcall numbers to enable by default.
5674 * For compatibility with old userspace, we enable by default
5675 * all hcalls that were implemented before the hcall-enabling
5676 * facility was added. Note this list should not include H_RTAS.
5678 static unsigned int default_hcall_list[] = {
5684 #ifdef CONFIG_SPAPR_TCE_IOMMU
5694 #ifdef CONFIG_KVM_XICS
5705 static void init_default_hcalls(void)
5710 for (i = 0; default_hcall_list[i]; ++i) {
5711 hcall = default_hcall_list[i];
5712 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
5713 __set_bit(hcall / 4, default_enabled_hcalls);
5717 static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
5723 /* If not on a POWER9, reject it */
5724 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5727 /* If any unknown flags set, reject it */
5728 if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
5731 /* GR (guest radix) bit in process_table field must match */
5732 radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
5733 if (!!(cfg->process_table & PATB_GR) != radix)
5736 /* Process table size field must be reasonable, i.e. <= 24 */
5737 if ((cfg->process_table & PRTS_MASK) > 24)
5740 /* We can change a guest to/from radix now, if the host is radix */
5741 if (radix && !radix_enabled())
5744 /* If we're a nested hypervisor, we currently only support radix */
5745 if (kvmhv_on_pseries() && !radix)
5748 mutex_lock(&kvm->arch.mmu_setup_lock);
5749 if (radix != kvm_is_radix(kvm)) {
5750 if (kvm->arch.mmu_ready) {
5751 kvm->arch.mmu_ready = 0;
5752 /* order mmu_ready vs. vcpus_running */
5754 if (atomic_read(&kvm->arch.vcpus_running)) {
5755 kvm->arch.mmu_ready = 1;
5761 err = kvmppc_switch_mmu_to_radix(kvm);
5763 err = kvmppc_switch_mmu_to_hpt(kvm);
5768 kvm->arch.process_table = cfg->process_table;
5769 kvmppc_setup_partition_table(kvm);
5771 lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
5772 kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
5776 mutex_unlock(&kvm->arch.mmu_setup_lock);
5780 static int kvmhv_enable_nested(struct kvm *kvm)
5784 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5786 if (!radix_enabled())
5789 /* kvm == NULL means the caller is testing if the capability exists */
5791 kvm->arch.nested_enable = true;
5795 static int kvmhv_load_from_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
5800 if (kvmhv_vcpu_is_radix(vcpu)) {
5801 rc = kvmhv_copy_from_guest_radix(vcpu, *eaddr, ptr, size);
5807 /* For now quadrants are the only way to access nested guest memory */
5808 if (rc && vcpu->arch.nested)
5814 static int kvmhv_store_to_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
5819 if (kvmhv_vcpu_is_radix(vcpu)) {
5820 rc = kvmhv_copy_to_guest_radix(vcpu, *eaddr, ptr, size);
5826 /* For now quadrants are the only way to access nested guest memory */
5827 if (rc && vcpu->arch.nested)
5833 static void unpin_vpa_reset(struct kvm *kvm, struct kvmppc_vpa *vpa)
5835 unpin_vpa(kvm, vpa);
5837 vpa->pinned_addr = NULL;
5839 vpa->update_pending = 0;
5843 * Enable a guest to become a secure VM, or test whether
5844 * that could be enabled.
5845 * Called when the KVM_CAP_PPC_SECURE_GUEST capability is
5846 * tested (kvm == NULL) or enabled (kvm != NULL).
5848 static int kvmhv_enable_svm(struct kvm *kvm)
5850 if (!kvmppc_uvmem_available())
5853 kvm->arch.svm_enabled = 1;
5858 * IOCTL handler to turn off secure mode of guest
5860 * - Release all device pages
5861 * - Issue ucall to terminate the guest on the UV side
5862 * - Unpin the VPA pages.
5863 * - Reinit the partition scoped page tables
5865 static int kvmhv_svm_off(struct kvm *kvm)
5867 struct kvm_vcpu *vcpu;
5873 if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
5876 mutex_lock(&kvm->arch.mmu_setup_lock);
5877 mmu_was_ready = kvm->arch.mmu_ready;
5878 if (kvm->arch.mmu_ready) {
5879 kvm->arch.mmu_ready = 0;
5880 /* order mmu_ready vs. vcpus_running */
5882 if (atomic_read(&kvm->arch.vcpus_running)) {
5883 kvm->arch.mmu_ready = 1;
5889 srcu_idx = srcu_read_lock(&kvm->srcu);
5890 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
5891 struct kvm_memory_slot *memslot;
5892 struct kvm_memslots *slots = __kvm_memslots(kvm, i);
5897 kvm_for_each_memslot(memslot, slots) {
5898 kvmppc_uvmem_drop_pages(memslot, kvm, true);
5899 uv_unregister_mem_slot(kvm->arch.lpid, memslot->id);
5902 srcu_read_unlock(&kvm->srcu, srcu_idx);
5904 ret = uv_svm_terminate(kvm->arch.lpid);
5905 if (ret != U_SUCCESS) {
5911 * When secure guest is reset, all the guest pages are sent
5912 * to UV via UV_PAGE_IN before the non-boot vcpus get a
5913 * chance to run and unpin their VPA pages. Unpinning of all
5914 * VPA pages is done here explicitly so that VPA pages
5915 * can be migrated to the secure side.
5917 * This is required to for the secure SMP guest to reboot
5920 kvm_for_each_vcpu(i, vcpu, kvm) {
5921 spin_lock(&vcpu->arch.vpa_update_lock);
5922 unpin_vpa_reset(kvm, &vcpu->arch.dtl);
5923 unpin_vpa_reset(kvm, &vcpu->arch.slb_shadow);
5924 unpin_vpa_reset(kvm, &vcpu->arch.vpa);
5925 spin_unlock(&vcpu->arch.vpa_update_lock);
5928 kvmppc_setup_partition_table(kvm);
5929 kvm->arch.secure_guest = 0;
5930 kvm->arch.mmu_ready = mmu_was_ready;
5932 mutex_unlock(&kvm->arch.mmu_setup_lock);
5936 static int kvmhv_enable_dawr1(struct kvm *kvm)
5938 if (!cpu_has_feature(CPU_FTR_DAWR1))
5941 /* kvm == NULL means the caller is testing if the capability exists */
5943 kvm->arch.dawr1_enabled = true;
5947 static bool kvmppc_hash_v3_possible(void)
5949 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5952 if (!cpu_has_feature(CPU_FTR_HVMODE))
5956 * POWER9 chips before version 2.02 can't have some threads in
5957 * HPT mode and some in radix mode on the same core.
5959 if (radix_enabled()) {
5960 unsigned int pvr = mfspr(SPRN_PVR);
5961 if ((pvr >> 16) == PVR_POWER9 &&
5962 (((pvr & 0xe000) == 0 && (pvr & 0xfff) < 0x202) ||
5963 ((pvr & 0xe000) == 0x2000 && (pvr & 0xfff) < 0x101)))
5970 static struct kvmppc_ops kvm_ops_hv = {
5971 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
5972 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
5973 .get_one_reg = kvmppc_get_one_reg_hv,
5974 .set_one_reg = kvmppc_set_one_reg_hv,
5975 .vcpu_load = kvmppc_core_vcpu_load_hv,
5976 .vcpu_put = kvmppc_core_vcpu_put_hv,
5977 .inject_interrupt = kvmppc_inject_interrupt_hv,
5978 .set_msr = kvmppc_set_msr_hv,
5979 .vcpu_run = kvmppc_vcpu_run_hv,
5980 .vcpu_create = kvmppc_core_vcpu_create_hv,
5981 .vcpu_free = kvmppc_core_vcpu_free_hv,
5982 .check_requests = kvmppc_core_check_requests_hv,
5983 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
5984 .flush_memslot = kvmppc_core_flush_memslot_hv,
5985 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
5986 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
5987 .unmap_gfn_range = kvm_unmap_gfn_range_hv,
5988 .age_gfn = kvm_age_gfn_hv,
5989 .test_age_gfn = kvm_test_age_gfn_hv,
5990 .set_spte_gfn = kvm_set_spte_gfn_hv,
5991 .free_memslot = kvmppc_core_free_memslot_hv,
5992 .init_vm = kvmppc_core_init_vm_hv,
5993 .destroy_vm = kvmppc_core_destroy_vm_hv,
5994 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
5995 .emulate_op = kvmppc_core_emulate_op_hv,
5996 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
5997 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
5998 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
5999 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
6000 .hcall_implemented = kvmppc_hcall_impl_hv,
6001 #ifdef CONFIG_KVM_XICS
6002 .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
6003 .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
6005 .configure_mmu = kvmhv_configure_mmu,
6006 .get_rmmu_info = kvmhv_get_rmmu_info,
6007 .set_smt_mode = kvmhv_set_smt_mode,
6008 .enable_nested = kvmhv_enable_nested,
6009 .load_from_eaddr = kvmhv_load_from_eaddr,
6010 .store_to_eaddr = kvmhv_store_to_eaddr,
6011 .enable_svm = kvmhv_enable_svm,
6012 .svm_off = kvmhv_svm_off,
6013 .enable_dawr1 = kvmhv_enable_dawr1,
6014 .hash_v3_possible = kvmppc_hash_v3_possible,
6017 static int kvm_init_subcore_bitmap(void)
6020 int nr_cores = cpu_nr_cores();
6021 struct sibling_subcore_state *sibling_subcore_state;
6023 for (i = 0; i < nr_cores; i++) {
6024 int first_cpu = i * threads_per_core;
6025 int node = cpu_to_node(first_cpu);
6027 /* Ignore if it is already allocated. */
6028 if (paca_ptrs[first_cpu]->sibling_subcore_state)
6031 sibling_subcore_state =
6032 kzalloc_node(sizeof(struct sibling_subcore_state),
6034 if (!sibling_subcore_state)
6038 for (j = 0; j < threads_per_core; j++) {
6039 int cpu = first_cpu + j;
6041 paca_ptrs[cpu]->sibling_subcore_state =
6042 sibling_subcore_state;
6048 static int kvmppc_radix_possible(void)
6050 return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
6053 static int kvmppc_book3s_init_hv(void)
6057 if (!tlbie_capable) {
6058 pr_err("KVM-HV: Host does not support TLBIE\n");
6063 * FIXME!! Do we need to check on all cpus ?
6065 r = kvmppc_core_check_processor_compat_hv();
6069 r = kvmhv_nested_init();
6073 r = kvm_init_subcore_bitmap();
6078 * We need a way of accessing the XICS interrupt controller,
6079 * either directly, via paca_ptrs[cpu]->kvm_hstate.xics_phys, or
6080 * indirectly, via OPAL.
6083 if (!xics_on_xive() && !kvmhv_on_pseries() &&
6084 !local_paca->kvm_hstate.xics_phys) {
6085 struct device_node *np;
6087 np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
6089 pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
6092 /* presence of intc confirmed - node can be dropped again */
6097 kvm_ops_hv.owner = THIS_MODULE;
6098 kvmppc_hv_ops = &kvm_ops_hv;
6100 init_default_hcalls();
6104 r = kvmppc_mmu_hv_init();
6108 if (kvmppc_radix_possible()) {
6109 r = kvmppc_radix_init();
6114 r = kvmppc_uvmem_init();
6116 pr_err("KVM-HV: kvmppc_uvmem_init failed %d\n", r);
6121 static void kvmppc_book3s_exit_hv(void)
6123 kvmppc_uvmem_free();
6124 kvmppc_free_host_rm_ops();
6125 if (kvmppc_radix_possible())
6126 kvmppc_radix_exit();
6127 kvmppc_hv_ops = NULL;
6128 kvmhv_nested_exit();
6131 module_init(kvmppc_book3s_init_hv);
6132 module_exit(kvmppc_book3s_exit_hv);
6133 MODULE_LICENSE("GPL");
6134 MODULE_ALIAS_MISCDEV(KVM_MINOR);
6135 MODULE_ALIAS("devname:kvm");