KVM: PPC: Book3S HV: Nested HV virtualization
Subsystems (5)
v3 cover KVM: PPC: Book3S HV: Nested HV virtualization
This patch series implements nested virtualization in the KVM-HV module for radix guests on POWER9 systems. Unlike PR KVM, nested guests are able to run in supervisor mode, meaning that performance is much better than with PR KVM, and is very close to the performance of a non-nested guests for most things.
The way this works is that each nested guest is also a guest of the real hypervisor, also known as the level 0 or L0 hypervisor, which runs in the CPU's hypervisor mode. Its guests are at level 1, and when a L1 system wants to run a nested guest, it performs hypercalls to L0 to set up a virtual partition table in its (L1's) memory and to enter the L2 guest. The L0 hypervisor maintains a shadow partition-scoped page table for the L2 guest and demand-faults entries into it by translating the L1 real addresses in the partition-scoped page table in L1 memory into L0 real addresses and puts them in the shadow partition-scoped page table for L2.
Essentially what this is doing is providing L1 with the ability to do (some) hypervisor functions using paravirtualization; optionally, TLB invalidations can be done through emulation of the tlbie instruction rather than a hypercall.
Along the way, this implements a new guest entry/exit path for radix guests on POWER9 systems which is written almost entirely in C and does not do any of the inter-thread coordination that the existing entry/exit path does. It is only used for radix guests and when indep_threads_mode=Y (the default).
The limitations of this scheme are:
- Host and all nested hypervisors and their guests must be in radix mode.
- Nested hypervisors cannot use indep_threads_mode=N.
- If the host (i.e. the L0 hypervisor) has indep_threads_mode=N then only one nested vcpu can be run on any core at any given time; the secondary threads will do nothing.
- A nested hypervisor can't use a smaller page size than the base page size of the hypervisor(s) above it.
- A nested hypervisor is limited to having at most 1023 guests below it, each of which can have at most NR_CPUS virtual CPUs (and the virtual CPU ids have to be < NR_CPUS as well).
This patch series is against my kvm-ppc-fixes branch.
Changes in this version since version 2:
- Restored the patch adding the KVM_CAP_PPC_NESTED_HV capability, which accidentally got dropped from v2.
- Made kvmhv_nested_init() return an error code.
- Made the prev_cpu array be unsigned short[] instead of int[].
- Made LPID flushes done by the nested hypervisor use the hcall instead of the tlbie instruction, and added an LPID flush to kvmhv_flush_nested().
Paul.
Documentation/virtual/kvm/api.txt | 15 + arch/powerpc/include/asm/asm-prototypes.h | 21 + arch/powerpc/include/asm/book3s/64/mmu-hash.h | 12 + .../powerpc/include/asm/book3s/64/tlbflush-radix.h | 1 + arch/powerpc/include/asm/hvcall.h | 41 + arch/powerpc/include/asm/kvm_asm.h | 4 +- arch/powerpc/include/asm/kvm_book3s.h | 49 +- arch/powerpc/include/asm/kvm_book3s_64.h | 119 +- arch/powerpc/include/asm/kvm_book3s_asm.h | 3 + arch/powerpc/include/asm/kvm_booke.h | 4 +- arch/powerpc/include/asm/kvm_host.h | 16 +- arch/powerpc/include/asm/kvm_ppc.h | 4 + arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/include/asm/reg.h | 3 + arch/powerpc/include/uapi/asm/kvm.h | 1 + arch/powerpc/kernel/asm-offsets.c | 5 +- arch/powerpc/kernel/cpu_setup_power.S | 4 +- arch/powerpc/kvm/Makefile | 3 +- arch/powerpc/kvm/book3s.c | 43 +- arch/powerpc/kvm/book3s_64_mmu_hv.c | 7 +- arch/powerpc/kvm/book3s_64_mmu_radix.c | 720 ++++++++--- arch/powerpc/kvm/book3s_emulate.c | 13 +- arch/powerpc/kvm/book3s_hv.c | 927 ++++++++++++-- arch/powerpc/kvm/book3s_hv_builtin.c | 92 +- arch/powerpc/kvm/book3s_hv_interrupts.S | 95 +- arch/powerpc/kvm/book3s_hv_nested.c | 1333 ++++++++++++++++++++ arch/powerpc/kvm/book3s_hv_ras.c | 10 + arch/powerpc/kvm/book3s_hv_rm_xics.c | 13 +- arch/powerpc/kvm/book3s_hv_rmhandlers.S | 809 ++++++------ arch/powerpc/kvm/book3s_hv_tm.c | 6 +- arch/powerpc/kvm/book3s_hv_tm_builtin.c | 5 +- arch/powerpc/kvm/book3s_pr.c | 5 +- arch/powerpc/kvm/book3s_xics.c | 14 +- arch/powerpc/kvm/book3s_xive.c | 63 + arch/powerpc/kvm/book3s_xive_template.c | 8 - arch/powerpc/kvm/bookehv_interrupts.S | 8 +- arch/powerpc/kvm/emulate_loadstore.c | 1 - arch/powerpc/kvm/powerpc.c | 12 + arch/powerpc/kvm/tm.S | 250 ++-- arch/powerpc/kvm/trace_book3s.h | 1 - arch/powerpc/mm/tlb-radix.c | 9 + include/uapi/linux/kvm.h | 1 + tools/perf/arch/powerpc/util/book3s_hv_exits.h | 1 - 43 files changed, 3885 insertions(+), 867 deletions(-)
PATCH v3 1/33 powerpc: Turn off CPU_FTR_P9_TM_HV_ASSIST in non-hypervisor mode
When doing nested virtualization, it is only necessary to do the transactional memory hypervisor assist at level 0, that is, when we are in hypervisor mode. Nested hypervisors can just use the TM facilities as architected. Therefore we should clear the CPU_FTR_P9_TM_HV_ASSIST bit when we are not in hypervisor mode, along with the CPU_FTR_HVMODE bit.
Doing this will not change anything at this stage because the only code that tests CPU_FTR_P9_TM_HV_ASSIST is in HV KVM, which currently can only be used when when CPU_FTR_HVMODE is set.
--- arch/powerpc/kernel/cpu_setup_power.S | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/kernel/cpu_setup_power.S +2 -2
@@ -147,8 +147,8 @@ __init_hvmode_206: rldicl. r0,r3,4,63 bnelr ld r5,CPU_SPEC_FEATURES(r4) - LOAD_REG_IMMEDIATE(r6,CPU_FTR_HVMODE) - xor r5,r5,r6 + LOAD_REG_IMMEDIATE(r6,CPU_FTR_HVMODE | CPU_FTR_P9_TM_HV_ASSIST) + andc r5,r5,r6 std r5,CPU_SPEC_FEATURES(r4) blr
Signature · 1 line
2.7.4
PATCH v3 2/33 KVM: PPC: Book3S: Simplify external interrupt handling
In reply to Paul MackerrasCurrently we use two bits in the vcpu pending_exceptions bitmap to indicate that an external interrupt is pending for the guest, one for "one-shot" interrupts that are cleared when delivered, and one for interrupts that persist until cleared by an explicit action of the OS (e.g. an acknowledge to an interrupt controller). The BOOK3S_IRQPRIO_EXTERNAL bit is used for one-shot interrupt requests and BOOK3S_IRQPRIO_EXTERNAL_LEVEL is used for persisting interrupts.
In practice BOOK3S_IRQPRIO_EXTERNAL never gets used, because our Book3S platforms generally, and pseries in particular, expect external interrupt requests to persist until they are acknowledged at the interrupt controller. That combined with the confusion introduced by having two bits for what is essentially the same thing makes it attractive to simplify things by only using one bit. This patch does that.
With this patch there is only BOOK3S_IRQPRIO_EXTERNAL, and by default it has the semantics of a persisting interrupt. In order to avoid breaking the ABI, we introduce a new "external_oneshot" flag which preserves the behaviour of the KVM_INTERRUPT ioctl with the KVM_INTERRUPT_SET argument.
--- arch/powerpc/include/asm/kvm_asm.h | 4 +-- arch/powerpc/include/asm/kvm_host.h | 1 + arch/powerpc/kvm/book3s.c | 43 ++++++++++++++++++++------ arch/powerpc/kvm/book3s_hv_rm_xics.c | 5 ++- arch/powerpc/kvm/book3s_hv_rmhandlers.S | 4 +-- arch/powerpc/kvm/book3s_pr.c | 1 - arch/powerpc/kvm/book3s_xics.c | 11 +++---- arch/powerpc/kvm/book3s_xive_template.c | 2 +- arch/powerpc/kvm/trace_book3s.h | 1 - tools/perf/arch/powerpc/util/book3s_hv_exits.h | 1 - 10 files changed, 44 insertions(+), 29 deletions(-)
arch/powerpc/include/asm/kvm_asm.h +1 -3
@@ -84,7 +84,6 @@#define BOOK3S_INTERRUPT_INST_STORAGE 0x400#define BOOK3S_INTERRUPT_INST_SEGMENT 0x480#define BOOK3S_INTERRUPT_EXTERNAL 0x500 -#define BOOK3S_INTERRUPT_EXTERNAL_LEVEL 0x501#define BOOK3S_INTERRUPT_EXTERNAL_HV 0x502#define BOOK3S_INTERRUPT_ALIGNMENT 0x600#define BOOK3S_INTERRUPT_PROGRAM 0x700
@@ -134,8 +133,7 @@#define BOOK3S_IRQPRIO_EXTERNAL 14#define BOOK3S_IRQPRIO_DECREMENTER 15#define BOOK3S_IRQPRIO_PERFORMANCE_MONITOR 16 -#define BOOK3S_IRQPRIO_EXTERNAL_LEVEL 17 -#define BOOK3S_IRQPRIO_MAX 18 +#define BOOK3S_IRQPRIO_MAX 17#define BOOK3S_HFLAG_DCBZ32 0x1#define BOOK3S_HFLAG_SLB 0x2
arch/powerpc/include/asm/kvm_host.h +1 -0
@@ -707,6 +707,7 @@ struct kvm_vcpu_arch {u8 hcall_needed;u8 epr_flags;/* KVMPPC_EPR_xxx */ u8 epr_needed; +u8 external_oneshot;/* clear external irq after delivery */ u32 cpr0_cfgaddr;/* holds the last set cpr0_cfgaddr */
arch/powerpc/kvm/book3s.c +33 -10
73 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_rm_xics.c +2 -3
@@ -136,7 +136,7 @@ static void icp_rm_set_vcpu_irq(struct kvm_vcpu *vcpu,/* Mark the target VCPU as having an interrupt pending */ vcpu->stat.queue_intr++; -set_bit (BOOK3S_IRQPRIO_EXTERNAL_LEVEL , &vcpu->arch.pending_exceptions); +set_bit (BOOK3S_IRQPRIO_EXTERNAL , &vcpu->arch.pending_exceptions);/* Kick self ? Just set MER and return */ if (vcpu == this_vcpu) {
@@ -170,8 +170,7 @@ static void icp_rm_set_vcpu_irq(struct kvm_vcpu *vcpu,static void icp_rm_clr_vcpu_irq (struct kvm_vcpu *vcpu) {/* Note: Only called on self ! */ -clear_bit (BOOK3S_IRQPRIO_EXTERNAL_LEVEL , - &vcpu->arch.pending_exceptions); +clear_bit (BOOK3S_IRQPRIO_EXTERNAL , &vcpu->arch.pending_exceptions);mtspr (SPRN_LPCR ,mfspr (SPRN_LPCR ) & ~LPCR_MER ); }
arch/powerpc/kvm/book3s_hv_rmhandlers.S +2 -2
@@ -1122,11 +1122,11 @@ kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */ /* Check if we can deliver an external or decrementer interrupt now */ ld r0, VCPU_PENDING_EXC(r4) - rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63 + rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL, 63 cmpdi cr1, r0, 0 andi. r8, r11, MSR_EE mfspr r8, SPRN_LPCR - /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */ + /* Insert EXTERNAL bit into LPCR at the MER bit position */ rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH mtspr SPRN_LPCR, r8 isync
arch/powerpc/kvm/book3s_pr.c +0 -1
@@ -1246,7 +1246,6 @@ int kvmppc_handle_exit_pr(struct kvm_run *run, struct kvm_vcpu *vcpu, r =RESUME_GUEST ;break ;case BOOK3S_INTERRUPT_EXTERNAL : -case BOOK3S_INTERRUPT_EXTERNAL_LEVEL :case BOOK3S_INTERRUPT_EXTERNAL_HV :case BOOK3S_INTERRUPT_H_VIRT : vcpu->stat.ext_intr_exits++;
arch/powerpc/kvm/book3s_xics.c +4 -7
@@ -310,7 +310,7 @@ static inline bool icp_try_update(struct kvmppc_icp *icp,*/ if (new.out_ee) {kvmppc_book3s_queue_irqprio (icp->vcpu, -BOOK3S_INTERRUPT_EXTERNAL_LEVEL ); +BOOK3S_INTERRUPT_EXTERNAL );if (!change_self)kvmppc_fast_vcpu_kick (icp->vcpu); }
@@ -593,8 +593,7 @@ static noinline unsigned long kvmppc_h_xirr(struct kvm_vcpu *vcpu)u32 xirr;/* First, remove EE from the processor */ -kvmppc_book3s_dequeue_irqprio (icp->vcpu, -BOOK3S_INTERRUPT_EXTERNAL_LEVEL ); +kvmppc_book3s_dequeue_irqprio (icp->vcpu,BOOK3S_INTERRUPT_EXTERNAL );/* * ICP State: Accept_Interrupt
@@ -754,8 +753,7 @@ static noinline void kvmppc_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)* We can remove EE from the current processor, the update * transaction will set it again if needed */ -kvmppc_book3s_dequeue_irqprio (icp->vcpu, -BOOK3S_INTERRUPT_EXTERNAL_LEVEL ); +kvmppc_book3s_dequeue_irqprio (icp->vcpu,BOOK3S_INTERRUPT_EXTERNAL ); do { old_state = new_state = READ_ONCE(icp->state);
@@ -1167,8 +1165,7 @@ int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval)* Deassert the CPU interrupt request. * icp_try_update will reassert it if necessary. */ -kvmppc_book3s_dequeue_irqprio (icp->vcpu, -BOOK3S_INTERRUPT_EXTERNAL_LEVEL ); +kvmppc_book3s_dequeue_irqprio (icp->vcpu,BOOK3S_INTERRUPT_EXTERNAL );/* * Note that if we displace an interrupt from old_state.xisr,
arch/powerpc/kvm/book3s_xive_template.c +1 -1
@@ -285,7 +285,7 @@ X_STATIC unsigned long GLUE(X_PFX,h_xirr)(struct kvm_vcpu *vcpu)* set by pull or an escalation interrupts). */ if (test_bit (BOOK3S_IRQPRIO_EXTERNAL , &vcpu->arch.pending_exceptions)) -clear_bit (BOOK3S_IRQPRIO_EXTERNAL_LEVEL , +clear_bit (BOOK3S_IRQPRIO_EXTERNAL , &vcpu->arch.pending_exceptions); pr_devel(" new pending=0x%02x hw_cppr=%d cppr=%d\n",
arch/powerpc/kvm/trace_book3s.h +0 -1
@@ -14,7 +14,6 @@ {0x400, "INST_STORAGE"}, \ {0x480, "INST_SEGMENT"}, \ {0x500, "EXTERNAL"}, \ - {0x501, "EXTERNAL_LEVEL"}, \ {0x502, "EXTERNAL_HV"}, \ {0x600, "ALIGNMENT"}, \ {0x700, "PROGRAM"}, \
tools/perf/arch/powerpc/util/book3s_hv_exits.h +0 -1
@@ -15,7 +15,6 @@ {0x400, "INST_STORAGE"}, \ {0x480, "INST_SEGMENT"}, \ {0x500, "EXTERNAL"}, \ - {0x501, "EXTERNAL_LEVEL"}, \ {0x502, "EXTERNAL_HV"}, \ {0x600, "ALIGNMENT"}, \ {0x700, "PROGRAM"}, \
Signature · 1 line
2.7.4
PATCH v3 3/33 KVM: PPC: Book3S HV: Remove left-over code in XICS-on-XIVE emulation
In reply to Paul MackerrasThis removes code that clears the external interrupt pending bit in the pending_exceptions bitmap. This is left over from an earlier iteration of the code where this bit was set when an escalation interrupt arrived in order to wake the vcpu from cede. Currently we set the vcpu->arch.irq_pending flag instead for this purpose. Therefore there is no need to do anything with the pending_exceptions bitmap.
--- arch/powerpc/kvm/book3s_xive_template.c | 8 -------- 1 file changed, 8 deletions(-)
arch/powerpc/kvm/book3s_xive_template.c +0 -8
@@ -280,14 +280,6 @@ X_STATIC unsigned long GLUE(X_PFX,h_xirr)(struct kvm_vcpu *vcpu)/* First collect pending bits from HW */ GLUE (X_PFX ,ack_pending)(xc); -/* -* Cleanup the old-style bits if needed (they may have been -* set by pull or an escalation interrupts). -*/ -if (test_bit (BOOK3S_IRQPRIO_EXTERNAL , &vcpu->arch.pending_exceptions)) -clear_bit (BOOK3S_IRQPRIO_EXTERNAL , - &vcpu->arch.pending_exceptions); -pr_devel (" new pending=0x%02x hw_cppr=%d cppr=%d\n" , xc->pending, xc->hw_cppr, xc->cppr);
Signature · 1 line
2.7.4
PATCH v3 4/33 KVM: PPC: Book3S HV: Move interrupt delivery on guest entry to C code
In reply to Paul MackerrasThis is based on a patch by Suraj Jitindar Singh.
This moves the code in book3s_hv_rmhandlers.S that generates an external, decrementer or privileged doorbell interrupt just before entering the guest to C code in book3s_hv_builtin.c. This is to make future maintenance and modification easier. The algorithm expressed in the C code is almost identical to the previous algorithm.
--- arch/powerpc/include/asm/kvm_ppc.h | 1 + arch/powerpc/kvm/book3s_hv.c | 3 +- arch/powerpc/kvm/book3s_hv_builtin.c | 48 ++++++++++++++++++++++ arch/powerpc/kvm/book3s_hv_rmhandlers.S | 70 ++++++++------------------------- 4 files changed, 67 insertions(+), 55 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +1 -0
@@ -652,6 +652,7 @@ int kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server, unsigned long mfrr);int kvmppc_rm_h_cppr (struct kvm_vcpu *vcpu,unsigned long cppr);int kvmppc_rm_h_eoi (struct kvm_vcpu *vcpu,unsigned long xirr); +void kvmppc_guest_entry_inject_int (struct kvm_vcpu *vcpu);/* * Host-side operations we want to set up while running in real
arch/powerpc/kvm/book3s_hv.c +1 -2
@@ -730,8 +730,7 @@ static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu)/* * Ensure that the read of vcore->dpdes comes after the read * of vcpu->doorbell_request. This barrier matches the -* lwsync in book3s_hv_rmhandlers.S just before the -* fast_guest_return label. +* smb_wmb() in kvmppc_guest_entry_inject(). */ smp_rmb (); vc = vcpu->arch.vcore;
arch/powerpc/kvm/book3s_hv_builtin.c +48 -0
51 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_rmhandlers.S +17 -53
83 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 5/33 KVM: PPC: Book3S HV: Extract PMU save/restore operations as C-callable functions
In reply to Paul MackerrasThis pulls out the assembler code that is responsible for saving and restoring the PMU state for the host and guest into separate functions so they can be used from an alternate entry path. The calling convention is made compatible with C.
--- arch/powerpc/include/asm/asm-prototypes.h | 5 + arch/powerpc/kvm/book3s_hv_interrupts.S | 95 ++++---- arch/powerpc/kvm/book3s_hv_rmhandlers.S | 363 ++++++++++++++++-------------- 3 files changed, 253 insertions(+), 210 deletions(-)
arch/powerpc/include/asm/asm-prototypes.h +5 -0
@@ -150,4 +150,9 @@ extern s32 patch__memset_nocache, patch__memcpy_nocache;extern long flush_count_cache; +void kvmhv_save_host_pmu (void ); +void kvmhv_load_host_pmu (void ); +void kvmhv_save_guest_pmu (struct kvm_vcpu *vcpu,bool pmu_in_use); +void kvmhv_load_guest_pmu (struct kvm_vcpu *vcpu); +#endif /* _ASM_POWERPC_ASM_PROTOTYPES_H */
arch/powerpc/kvm/book3s_hv_interrupts.S +49 -46
104 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_rmhandlers.S +199 -164
396 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 6/33 KVM: PPC: Book3S HV: Simplify real-mode interrupt handling
In reply to Paul MackerrasThis streamlines the first part of the code that handles a hypervisor interrupt that occurred in the guest. With this, all of the real-mode handling that occurs is done before the "guest_exit_cont" label; once we get to that label we are committed to exiting to host virtual mode. Thus the machine check and HMI real-mode handling is moved before that label.
Also, the code to handle external interrupts is moved out of line, as is the code that calls kvmppc_realmode_hmi_handler().
--- arch/powerpc/kvm/book3s_hv_ras.c | 8 ++ arch/powerpc/kvm/book3s_hv_rmhandlers.S | 220 ++++++++++++++++---------------- 2 files changed, 119 insertions(+), 109 deletions(-)
arch/powerpc/kvm/book3s_hv_ras.c +8 -0
@@ -331,5 +331,13 @@ long kvmppc_realmode_hmi_handler(void) }else {wait_for_tb_resync (); } + +/* +* Reset tb_offset_applied so the guest exit code won't try +* to subtract the previous timebase offset from the timebase. +*/ +if (local_paca->kvm_hstate.kvm_vcore) + local_paca->kvm_hstate.kvm_vcore->tb_offset_applied =0 ; +return 0 ; }
arch/powerpc/kvm/book3s_hv_rmhandlers.S +111 -109
294 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 7/33 KVM: PPC: Book3S: Rework TM save/restore code and make it C-callable
In reply to Paul MackerrasThis adds a parameter to __kvmppc_save_tm and __kvmppc_restore_tm which allows the caller to indicate whether it wants the nonvolatile register state to be preserved across the call, as required by the C calling conventions. This parameter being non-zero also causes the MSR bits that enable TM, FP, VMX and VSX to be preserved. The condition register and DSCR are now always preserved.
With this, kvmppc_save_tm_hv and kvmppc_restore_tm_hv can be called from C code provided the 3rd parameter is non-zero. So that these functions can be called from modules, they now include code to set the TOC pointer (r2) on entry, as they can call other built-in C functions which will assume the TOC to have been set.
Also, the fake suspend code in kvmppc_save_tm_hv is modified here to assume that treclaim in fake-suspend state does not modify any registers, which is the case on POWER9. This enables the code to be simplified quite a bit.
_kvmppc_save_tm_pr and _kvmppc_restore_tm_pr become much simpler with this change, since they now only need to save and restore TAR and pass 1 for the 3rd argument to __kvmppc_{save,restore}_tm.
--- arch/powerpc/include/asm/asm-prototypes.h | 10 ++ arch/powerpc/kvm/book3s_hv_rmhandlers.S | 49 +++--- arch/powerpc/kvm/tm.S | 250 ++++++++++++++++-------------- 3 files changed, 169 insertions(+), 140 deletions(-)
arch/powerpc/include/asm/asm-prototypes.h +10 -0
@@ -150,6 +150,16 @@ extern s32 patch__memset_nocache, patch__memcpy_nocache;extern long flush_count_cache; +#ifdef CONFIG_PPC_TRANSACTIONAL_MEM +void kvmppc_save_tm_hv (struct kvm_vcpu *vcpu,u64 msr,bool preserve_nv); +void kvmppc_restore_tm_hv (struct kvm_vcpu *vcpu,u64 msr,bool preserve_nv); +#else +static inline void kvmppc_save_tm_hv (struct kvm_vcpu *vcpu,u64 msr, +bool preserve_nv) { } +static inline void kvmppc_restore_tm_hv (struct kvm_vcpu *vcpu,u64 msr, +bool preserve_nv) { } +#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ +void kvmhv_save_host_pmu (void );void kvmhv_load_host_pmu (void );void kvmhv_save_guest_pmu (struct kvm_vcpu *vcpu,bool pmu_in_use);
arch/powerpc/kvm/book3s_hv_rmhandlers.S +23 -26
121 lines not on this page · raw message
arch/powerpc/kvm/tm.S +136 -114
383 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 8/33 KVM: PPC: Book3S HV: Call kvmppc_handle_exit_hv() with vcore unlocked
In reply to Paul MackerrasCurrently kvmppc_handle_exit_hv() is called with the vcore lock held because it is called within a for_each_runnable_thread loop. However, we already unlock the vcore within kvmppc_handle_exit_hv() under certain circumstances, and this is safe because (a) any vcpus that become runnable and are added to the runnable set by kvmppc_run_vcpu() have their vcpu->arch.trap == 0 and can't actually run in the guest (because the vcore state is VCORE_EXITING), and
(b) for_each_runnable_thread is safe against addition or removal of vcpus from the runnable set.
Therefore, in order to simplify things for following patches, let's drop the vcore lock in the for_each_runnable_thread loop, so kvmppc_handle_exit_hv() gets called without the vcore lock held.
--- arch/powerpc/kvm/book3s_hv.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
arch/powerpc/kvm/book3s_hv.c +10 -9
@@ -1084,7 +1084,6 @@ static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)return RESUME_GUEST ; } -/* Called with vcpu->arch.vcore->lock held */ static int kvmppc_handle_exit_hv (struct kvm_run *run,struct kvm_vcpu *vcpu,struct task_struct *tsk) {
@@ -1205,10 +1204,7 @@ static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, swab32(vcpu->arch.emul_inst) : vcpu->arch.emul_inst;if (vcpu->guest_debug &KVM_GUESTDBG_USE_SW_BP ) { -/* Need vcore unlocked to call kvmppc_get_last_inst */ -spin_unlock (&vcpu->arch.vcore->lock); r =kvmppc_emulate_debug_inst (run, vcpu); -spin_lock (&vcpu->arch.vcore->lock); }else {kvmppc_core_queue_program (vcpu,SRR1_PROGILL ); r =RESUME_GUEST ;
@@ -1224,12 +1220,8 @@ static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,case BOOK3S_INTERRUPT_H_FAC_UNAVAIL : r =EMULATE_FAIL ;if (((vcpu->arch.hfscr >>56 ) ==FSCR_MSGP_LG ) && -cpu_has_feature (CPU_FTR_ARCH_300 )) { -/* Need vcore unlocked to call kvmppc_get_last_inst */ -spin_unlock (&vcpu->arch.vcore->lock); +cpu_has_feature (CPU_FTR_ARCH_300 )) r =kvmppc_emulate_doorbell_instr (vcpu); -spin_lock (&vcpu->arch.vcore->lock); - }if (r ==EMULATE_FAIL ) {kvmppc_core_queue_program (vcpu,SRR1_PROGILL ); r =RESUME_GUEST ;
@@ -2599,6 +2591,14 @@ static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)spin_lock (&vc->lock); now =get_tb ();for_each_runnable_thread (i, vcpu, vc) { +/* +* It's safe to unlock the vcore in the loop here, because +* for_each_runnable_thread() is safe against removal of +* the vcpu, and the vcore state is VCORE_EXITING here, +* so any vcpus becoming runnable will have their arch.trap +* set to zero and can't actually run in the guest. +*/ +spin_unlock (&vc->lock);/* cancel pending dec exception if dec is positive */ if (now < vcpu->arch.dec_expires &&kvmppc_core_pending_dec (vcpu))
@@ -2614,6 +2614,7 @@ static void post_guest_process(struct kvmppc_vcore *vc, bool is_master) vcpu->arch.ret = ret; vcpu->arch.trap =0 ; +spin_lock (&vc->lock);if (is_kvmppc_resume_guest (vcpu->arch.ret)) {if (vcpu->arch.pending_exceptions)kvmppc_core_prepare_to_enter (vcpu);
Signature · 1 line
2.7.4
PATCH v3 9/33 KVM: PPC: Book3S HV: Streamlined guest entry/exit path on P9 for radix guests
In reply to Paul MackerrasThis creates an alternative guest entry/exit path which is used for radix guests on POWER9 systems when we have indep_threads_mode=Y. In these circumstances there is exactly one vcpu per vcore and there is no coordination required between vcpus or vcores; the vcpu can enter the guest without needing to synchronize with anything else.
The new fast path is implemented almost entirely in C in book3s_hv.c and runs with the MMU on until the guest is entered. On guest exit we use the existing path until the point where we are committed to exiting the guest (as distinct from handling an interrupt in the low-level code and returning to the guest) and we have pulled the guest context from the XIVE. At that point we check a flag in the stack frame to see whether we came in via the old path and the new path; if we came in via the new path then we go back to C code to do the rest of the process of saving the guest context and restoring the host context.
The C code is split into separate functions for handling the OS-accessible state and the hypervisor state, with the idea that the latter can be replaced by a hypercall when we implement nested virtualization.
--- arch/powerpc/include/asm/asm-prototypes.h | 2 + arch/powerpc/include/asm/kvm_ppc.h | 2 + arch/powerpc/kvm/book3s_hv.c | 425 +++++++++++++++++++++++++++++- arch/powerpc/kvm/book3s_hv_ras.c | 2 + arch/powerpc/kvm/book3s_hv_rmhandlers.S | 95 ++++++- arch/powerpc/kvm/book3s_xive.c | 63 +++++ 6 files changed, 585 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/asm-prototypes.h +2 -0
@@ -165,4 +165,6 @@ void kvmhv_load_host_pmu(void);void kvmhv_save_guest_pmu (struct kvm_vcpu *vcpu,bool pmu_in_use);void kvmhv_load_guest_pmu (struct kvm_vcpu *vcpu); +int __kvmhv_vcpu_entry_p9 (struct kvm_vcpu *vcpu); +#endif /* _ASM_POWERPC_ASM_PROTOTYPES_H */
arch/powerpc/include/asm/kvm_ppc.h +2 -0
@@ -585,6 +585,7 @@ extern int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval);extern int kvmppc_xive_set_irq (struct kvm *kvm,int irq_source_id,u32 irq,int level,bool line_status); +extern void kvmppc_xive_push_vcpu (struct kvm_vcpu *vcpu);#else static inline int kvmppc_xive_set_xive (struct kvm *kvm,u32 irq,u32 server,u32 priority) {return -1 ; }
@@ -607,6 +608,7 @@ static inline int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval) { returstatic inline int kvmppc_xive_set_irq (struct kvm *kvm,int irq_source_id,u32 irq,int level,bool line_status) {return -ENODEV ; } +static inline void kvmppc_xive_push_vcpu (struct kvm_vcpu *vcpu) { }#endif /* CONFIG_KVM_XIVE */ /*
arch/powerpc/kvm/book3s_hv.c +424 -1
443 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_ras.c +2 -0
@@ -177,6 +177,7 @@ void kvmppc_subcore_enter_guest(void) local_paca->sibling_subcore_state->in_guest[subcore_id] =1 ; } +EXPORT_SYMBOL_GPL (kvmppc_subcore_enter_guest);void kvmppc_subcore_exit_guest (void ) {
@@ -187,6 +188,7 @@ void kvmppc_subcore_exit_guest(void) local_paca->sibling_subcore_state->in_guest[subcore_id] =0 ; } +EXPORT_SYMBOL_GPL (kvmppc_subcore_exit_guest);static bool kvmppc_tb_resync_required (void ) {
arch/powerpc/kvm/book3s_hv_rmhandlers.S +92 -3
137 lines not on this page · raw message
arch/powerpc/kvm/book3s_xive.c +63 -0
69 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 10/33 KVM: PPC: Book3S HV: Handle hypervisor instruction faults better
In reply to Paul MackerrasCurrently the code for handling hypervisor instruction page faults passes 0 for the flags indicating the type of fault, which is OK in the usual case that the page is not mapped in the partition-scoped page tables. However, there are other causes for hypervisor instruction page faults, such as not being to update a reference
(R) or change (C) bit. The cause is indicated in bits in HSRR1, including a bit which indicates that the fault is due to not being able to write to a page (for example to update an R or C bit). Not handling these other kinds of faults correctly can lead to a loop of continual faults without forward progress in the guest.
In order to handle these faults better, this patch constructs a "DSISR-like" value from the bits which DSISR and SRR1 (for a HISI) have in common, and passes it to kvmppc_book3s_hv_page_fault() so that it knows what caused the fault.
--- arch/powerpc/include/asm/reg.h | 1 + arch/powerpc/kvm/book3s_hv.c | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/reg.h +1 -0
@@ -766,6 +766,7 @@#define SPRN_HSRR0 0x13A/* Save/Restore Register 0 */ #define SPRN_HSRR1 0x13B/* Save/Restore Register 1 */ #define HSRR1_DENORM 0x00100000/* Denorm exception */ +#define HSRR1_HISI_WRITE 0x00010000/* HISI bcs couldn't update mem */ #define SPRN_TBCTL 0x35f/* PA6T Timebase control register */ #define TBCTL_FREEZE 0x0000000000000000ull/* Freeze all tbs */
arch/powerpc/kvm/book3s_hv.c +4 -1
@@ -1188,7 +1188,10 @@ static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,break ;case BOOK3S_INTERRUPT_H_INST_STORAGE : vcpu->arch.fault_dar =kvmppc_get_pc (vcpu); - vcpu->arch.fault_dsisr =0 ; + vcpu->arch.fault_dsisr = vcpu->arch.shregs.msr & +DSISR_SRR1_MATCH_64S ; +if (vcpu->arch.shregs.msr &HSRR1_HISI_WRITE ) + vcpu->arch.fault_dsisr |=DSISR_ISSTORE ; r =RESUME_PAGE_FAULT ;break ;/*
Signature · 1 line
2.7.4
PATCH v3 11/33 KVM: PPC: Book3S HV: Add a debugfs file to dump radix mappings
In reply to Paul MackerrasThis adds a file called 'radix' in the debugfs directory for the guest, which when read gives all of the valid leaf PTEs in the partition-scoped radix tree for a radix guest, in human-readable format. It is analogous to the existing 'htab' file which dumps the HPT entries for a HPT guest.
--- arch/powerpc/include/asm/kvm_book3s_64.h | 1 + arch/powerpc/include/asm/kvm_host.h | 1 + arch/powerpc/kvm/book3s_64_mmu_radix.c | 179 +++++++++++++++++++++++++++++++ arch/powerpc/kvm/book3s_hv.c | 2 + 4 files changed, 183 insertions(+)
arch/powerpc/include/asm/kvm_book3s_64.h +1 -0
@@ -435,6 +435,7 @@ static inline struct kvm_memslots *kvm_memslots_raw(struct kvm *kvm) }extern void kvmppc_mmu_debugfs_init (struct kvm *kvm); +extern void kvmhv_radix_debugfs_init (struct kvm *kvm);extern void kvmhv_rm_send_ipi (int cpu);
arch/powerpc/include/asm/kvm_host.h +1 -0
@@ -291,6 +291,7 @@ struct kvm_arch {u64 process_table;struct dentry *debugfs_dir;struct dentry *htab_dentry; +struct dentry *radix_dentry;struct kvm_resize_hpt *resize_hpt;/* protected by kvm->lock */ #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */ #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
arch/powerpc/kvm/book3s_64_mmu_radix.c +179 -0
191 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv.c +2 -0
@@ -4477,6 +4477,8 @@ static int kvmppc_core_init_vm_hv(struct kvm *kvm)snprintf (buf,sizeof (buf),"vm%d" , current->pid); kvm->arch.debugfs_dir =debugfs_create_dir (buf, kvm_debugfs_dir);kvmppc_mmu_debugfs_init (kvm); +if (radix_enabled ()) +kvmhv_radix_debugfs_init (kvm);return 0 ; }
Signature · 1 line
2.7.4
PATCH v3 12/33 KVM: PPC: Use ccr field in pt_regs struct embedded in vcpu struct
In reply to Paul MackerrasWhen the 'regs' field was added to struct kvm_vcpu_arch, the code was changed to use several of the fields inside regs (e.g., gpr, lr, etc.) but not the ccr field, because the ccr field in struct pt_regs is 64 bits on 64-bit platforms, but the cr field in kvm_vcpu_arch is only 32 bits. This changes the code to use the regs.ccr field instead of cr, and changes the assembly code on 64-bit platforms to use 64-bit loads and stores instead of 32-bit ones.
--- arch/powerpc/include/asm/kvm_book3s.h | 4 ++-- arch/powerpc/include/asm/kvm_book3s_64.h | 4 ++-- arch/powerpc/include/asm/kvm_booke.h | 4 ++-- arch/powerpc/include/asm/kvm_host.h | 2 -- arch/powerpc/kernel/asm-offsets.c | 4 ++-- arch/powerpc/kvm/book3s_emulate.c | 12 ++++++------ arch/powerpc/kvm/book3s_hv.c | 4 ++-- arch/powerpc/kvm/book3s_hv_rmhandlers.S | 4 ++-- arch/powerpc/kvm/book3s_hv_tm.c | 6 +++--- arch/powerpc/kvm/book3s_hv_tm_builtin.c | 5 +++-- arch/powerpc/kvm/book3s_pr.c | 4 ++-- arch/powerpc/kvm/bookehv_interrupts.S | 8 ++++---- arch/powerpc/kvm/emulate_loadstore.c | 1 - 13 files changed, 30 insertions(+), 32 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +2 -2
@@ -301,12 +301,12 @@ static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)static inline void kvmppc_set_cr (struct kvm_vcpu *vcpu,u32 val) { - vcpu->arch.cr = val; + vcpu->arch.regs.ccr = val; }static inline u32 kvmppc_get_cr (struct kvm_vcpu *vcpu) { -return vcpu->arch.cr; +return vcpu->arch.regs.ccr; }static inline void kvmppc_set_xer (struct kvm_vcpu *vcpu,ulong val)
arch/powerpc/include/asm/kvm_book3s_64.h +2 -2
@@ -483,7 +483,7 @@ static inline u64 sanitize_msr(u64 msr)#ifdef CONFIG_PPC_TRANSACTIONAL_MEM static inline void copy_from_checkpoint (struct kvm_vcpu *vcpu) { - vcpu->arch.cr = vcpu->arch.cr_tm; + vcpu->arch.regs.ccr = vcpu->arch.cr_tm; vcpu->arch.regs.xer = vcpu->arch.xer_tm; vcpu->arch.regs.link = vcpu->arch.lr_tm; vcpu->arch.regs.ctr = vcpu->arch.ctr_tm;
@@ -500,7 +500,7 @@ static inline void copy_from_checkpoint(struct kvm_vcpu *vcpu)static inline void copy_to_checkpoint (struct kvm_vcpu *vcpu) { - vcpu->arch.cr_tm = vcpu->arch.cr; + vcpu->arch.cr_tm = vcpu->arch.regs.ccr; vcpu->arch.xer_tm = vcpu->arch.regs.xer; vcpu->arch.lr_tm = vcpu->arch.regs.link; vcpu->arch.ctr_tm = vcpu->arch.regs.ctr;
arch/powerpc/include/asm/kvm_booke.h +2 -2
@@ -46,12 +46,12 @@ static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)static inline void kvmppc_set_cr (struct kvm_vcpu *vcpu,u32 val) { - vcpu->arch.cr = val; + vcpu->arch.regs.ccr = val; }static inline u32 kvmppc_get_cr (struct kvm_vcpu *vcpu) { -return vcpu->arch.cr; +return vcpu->arch.regs.ccr; }static inline void kvmppc_set_xer (struct kvm_vcpu *vcpu,ulong val)
arch/powerpc/include/asm/kvm_host.h +0 -2
@@ -538,8 +538,6 @@ struct kvm_vcpu_arch {ulong tar;#endif -u32 cr; -#ifdef CONFIG_PPC_BOOK3S ulong hflags;ulong guest_owned_ext;
arch/powerpc/kernel/asm-offsets.c +2 -2
@@ -438,7 +438,7 @@ int main(void)#ifdef CONFIG_PPC_BOOK3S OFFSET (VCPU_TAR , kvm_vcpu, arch.tar);#endif -OFFSET (VCPU_CR , kvm_vcpu, arch.cr); +OFFSET (VCPU_CR , kvm_vcpu, arch.regs.ccr);OFFSET (VCPU_PC , kvm_vcpu, arch.regs.nip);#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE OFFSET (VCPU_MSR , kvm_vcpu, arch.shregs.msr);
@@ -695,7 +695,7 @@ int main(void)#endif /* CONFIG_PPC_BOOK3S_64 */ #else /* CONFIG_PPC_BOOK3S */ -OFFSET (VCPU_CR , kvm_vcpu, arch.cr); +OFFSET (VCPU_CR , kvm_vcpu, arch.regs.ccr);OFFSET (VCPU_XER , kvm_vcpu, arch.regs.xer);OFFSET (VCPU_LR , kvm_vcpu, arch.regs.link);OFFSET (VCPU_CTR , kvm_vcpu, arch.regs.ctr);
arch/powerpc/kvm/book3s_emulate.c +6 -6
@@ -110,7 +110,7 @@ static inline void kvmppc_copyto_vcpu_tm(struct kvm_vcpu *vcpu) vcpu->arch.ctr_tm = vcpu->arch.regs.ctr; vcpu->arch.tar_tm = vcpu->arch.tar; vcpu->arch.lr_tm = vcpu->arch.regs.link; - vcpu->arch.cr_tm = vcpu->arch.cr; + vcpu->arch.cr_tm = vcpu->arch.regs.ccr; vcpu->arch.xer_tm = vcpu->arch.regs.xer; vcpu->arch.vrsave_tm = vcpu->arch.vrsave; }
@@ -129,7 +129,7 @@ static inline void kvmppc_copyfrom_vcpu_tm(struct kvm_vcpu *vcpu) vcpu->arch.regs.ctr = vcpu->arch.ctr_tm; vcpu->arch.tar = vcpu->arch.tar_tm; vcpu->arch.regs.link = vcpu->arch.lr_tm; - vcpu->arch.cr = vcpu->arch.cr_tm; + vcpu->arch.regs.ccr = vcpu->arch.cr_tm; vcpu->arch.regs.xer = vcpu->arch.xer_tm; vcpu->arch.vrsave = vcpu->arch.vrsave_tm; }
@@ -141,7 +141,7 @@ static void kvmppc_emulate_treclaim(struct kvm_vcpu *vcpu, int ra_val)uint64_t texasr;/* CR0 = 0 | MSR[TS] | 0 */ - vcpu->arch.cr = (vcpu->arch.cr & ~(CR0_MASK <<CR0_SHIFT )) | + vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr & ~(CR0_MASK <<CR0_SHIFT )) | (((guest_msr &MSR_TS_MASK ) >> (MSR_TS_S_LG -1 )) <<CR0_SHIFT );
@@ -220,7 +220,7 @@ void kvmppc_emulate_tabort(struct kvm_vcpu *vcpu, int ra_val)tm_abort (ra_val);/* CR0 = 0 | MSR[TS] | 0 */ - vcpu->arch.cr = (vcpu->arch.cr & ~(CR0_MASK <<CR0_SHIFT )) | + vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr & ~(CR0_MASK <<CR0_SHIFT )) | (((guest_msr &MSR_TS_MASK ) >> (MSR_TS_S_LG -1 )) <<CR0_SHIFT );
@@ -494,8 +494,8 @@ int kvmppc_core_emulate_op_pr(struct kvm_run *run, struct kvm_vcpu *vcpu,if (!(kvmppc_get_msr (vcpu) &MSR_PR )) {preempt_disable (); - vcpu->arch.cr = (CR0_TBEGIN_FAILURE | - (vcpu->arch.cr & ~(CR0_MASK <<CR0_SHIFT ))); + vcpu->arch.regs.ccr = (CR0_TBEGIN_FAILURE | + (vcpu->arch.regs.ccr & ~(CR0_MASK <<CR0_SHIFT ))); vcpu->arch.texasr = (TEXASR_FS | TEXASR_EXACT | (((u64)(TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
arch/powerpc/kvm/book3s_hv.c +2 -2
@@ -410,8 +410,8 @@ static void kvmppc_dump_regs(struct kvm_vcpu *vcpu) vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);pr_err ("sprg2 = %.16llx sprg3 = %.16llx\n" , vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3); -pr_err ("cr = %.8x xer = %.16lx dsisr = %.8x\n" , - vcpu->arch.cr, vcpu->arch.regs.xer, vcpu->arch.shregs.dsisr); +pr_err ("cr = %.8lx xer = %.16lx dsisr = %.8x\n" , + vcpu->arch.regs.ccr, vcpu->arch.regs.xer, vcpu->arch.shregs.dsisr);pr_err ("dar = %.16llx\n" , vcpu->arch.shregs.dar);pr_err ("fault dar = %.16lx dsisr = %.8x\n" , vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
arch/powerpc/kvm/book3s_hv_rmhandlers.S +2 -2
@@ -1092,7 +1092,7 @@ BEGIN_FTR_SECTION END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR) ld r5, VCPU_LR(r4) - lwz r6, VCPU_CR(r4) + ld r6, VCPU_CR(r4) mtlr r5 mtcr r6
@@ -1280,7 +1280,7 @@ kvmppc_interrupt_hv: std r3, VCPU_GPR(R12)(r9) /* CR is in the high half of r12 */ srdi r4, r12, 32 - stw r4, VCPU_CR(r9) + std r4, VCPU_CR(r9) BEGIN_FTR_SECTION ld r3, HSTATE_CFAR(r13) std r3, VCPU_CFAR(r9)
arch/powerpc/kvm/book3s_hv_tm.c +3 -3
@@ -130,7 +130,7 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)return RESUME_GUEST ; }/* Set CR0 to indicate previous transactional state */ - vcpu->arch.cr = (vcpu->arch.cr &0x0fffffff ) | + vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr &0x0fffffff ) | (((msr &MSR_TS_MASK ) >>MSR_TS_S_LG ) <<28 );/* L=1 => tresume, L=0 => tsuspend */ if (instr & (1 <<21 )) {
@@ -174,7 +174,7 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)copy_from_checkpoint (vcpu);/* Set CR0 to indicate previous transactional state */ - vcpu->arch.cr = (vcpu->arch.cr &0x0fffffff ) | + vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr &0x0fffffff ) | (((msr &MSR_TS_MASK ) >>MSR_TS_S_LG ) <<28 ); vcpu->arch.shregs.msr &= ~MSR_TS_MASK ;return RESUME_GUEST ;
@@ -204,7 +204,7 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)copy_to_checkpoint (vcpu);/* Set CR0 to indicate previous transactional state */ - vcpu->arch.cr = (vcpu->arch.cr &0x0fffffff ) | + vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr &0x0fffffff ) | (((msr &MSR_TS_MASK ) >>MSR_TS_S_LG ) <<28 ); vcpu->arch.shregs.msr = msr |MSR_TS_S ;return RESUME_GUEST ;
arch/powerpc/kvm/book3s_hv_tm_builtin.c +3 -2
@@ -89,7 +89,8 @@ int kvmhv_p9_tm_emulation_early(struct kvm_vcpu *vcpu)if (instr & (1 <<21 )) vcpu->arch.shregs.msr = (msr & ~MSR_TS_MASK ) |MSR_TS_T ;/* Set CR0 to 0b0010 */ - vcpu->arch.cr = (vcpu->arch.cr &0x0fffffff ) |0x20000000 ; + vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr &0x0fffffff ) | +0x20000000 ;return 1 ; }
@@ -105,5 +106,5 @@ void kvmhv_emulate_tm_rollback(struct kvm_vcpu *vcpu) vcpu->arch.shregs.msr &= ~MSR_TS_MASK ;/* go to N state */ vcpu->arch.regs.nip = vcpu->arch.tfhar;copy_from_checkpoint (vcpu); - vcpu->arch.cr = (vcpu->arch.cr &0x0fffffff ) |0xa0000000 ; + vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr &0x0fffffff ) |0xa0000000 ; }
arch/powerpc/kvm/book3s_pr.c +2 -2
@@ -167,7 +167,7 @@ void kvmppc_copy_to_svcpu(struct kvm_vcpu *vcpu) svcpu->gpr[11 ] = vcpu->arch.regs.gpr[11 ]; svcpu->gpr[12 ] = vcpu->arch.regs.gpr[12 ]; svcpu->gpr[13 ] = vcpu->arch.regs.gpr[13 ]; - svcpu->cr = vcpu->arch.cr; + svcpu->cr = vcpu->arch.regs.ccr; svcpu->xer = vcpu->arch.regs.xer; svcpu->ctr = vcpu->arch.regs.ctr; svcpu->lr = vcpu->arch.regs.link;
@@ -249,7 +249,7 @@ void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu) vcpu->arch.regs.gpr[11 ] = svcpu->gpr[11 ]; vcpu->arch.regs.gpr[12 ] = svcpu->gpr[12 ]; vcpu->arch.regs.gpr[13 ] = svcpu->gpr[13 ]; - vcpu->arch.cr = svcpu->cr; + vcpu->arch.regs.ccr = svcpu->cr; vcpu->arch.regs.xer = svcpu->xer; vcpu->arch.regs.ctr = svcpu->ctr; vcpu->arch.regs.link = svcpu->lr;
arch/powerpc/kvm/bookehv_interrupts.S +4 -4
@@ -182,7 +182,7 @@ */ PPC_LL r4, PACACURRENT(r13) PPC_LL r4, (THREAD + THREAD_KVM_VCPU)(r4) - stw r10, VCPU_CR(r4) + PPC_STL r10, VCPU_CR(r4) PPC_STL r11, VCPU_GPR(R4)(r4) PPC_STL r5, VCPU_GPR(R5)(r4) PPC_STL r6, VCPU_GPR(R6)(r4)
@@ -292,7 +292,7 @@ _GLOBAL(kvmppc_handler_\intno\()_\srr1) PPC_STL r4, VCPU_GPR(R4)(r11) PPC_LL r4, THREAD_NORMSAVE(0)(r10) PPC_STL r5, VCPU_GPR(R5)(r11) - stw r13, VCPU_CR(r11) + PPC_STL r13, VCPU_CR(r11) mfspr r5, \srr0 PPC_STL r3, VCPU_GPR(R10)(r11) PPC_LL r3, THREAD_NORMSAVE(2)(r10)
@@ -319,7 +319,7 @@ _GLOBAL(kvmppc_handler_\intno\()_\srr1) PPC_STL r4, VCPU_GPR(R4)(r11) PPC_LL r4, GPR9(r8) PPC_STL r5, VCPU_GPR(R5)(r11) - stw r9, VCPU_CR(r11) + PPC_STL r9, VCPU_CR(r11) mfspr r5, \srr0 PPC_STL r3, VCPU_GPR(R8)(r11) PPC_LL r3, GPR10(r8)
@@ -643,7 +643,7 @@ lightweight_exit: PPC_LL r3, VCPU_LR(r4) PPC_LL r5, VCPU_XER(r4) PPC_LL r6, VCPU_CTR(r4) - lwz r7, VCPU_CR(r4) + PPC_LL r7, VCPU_CR(r4) PPC_LL r8, VCPU_PC(r4) PPC_LD(r9, VCPU_SHARED_MSR, r11) PPC_LL r0, VCPU_GPR(R0)(r4)
arch/powerpc/kvm/emulate_loadstore.c +0 -1
@@ -117,7 +117,6 @@ int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu) emulated =EMULATE_FAIL ; vcpu->arch.regs.msr = vcpu->arch.shared->msr; - vcpu->arch.regs.ccr = vcpu->arch.cr;if (analyse_instr (&op, &vcpu->arch.regs, inst) ==0 ) {int type = op.type &INSTR_TYPE_MASK ;int size =GETSIZE (op.type);
Signature · 1 line
2.7.4
PATCH v3 13/33 KVM: PPC: Book3S HV: Clear partition table entry on vm teardown
In reply to Paul MackerrasFrom: Suraj Jitindar Singh <[email protected]>
When destroying a VM we return the LPID to the pool, however we never zero the partition table entry. This is instead done when we reallocate the LPID.
Zero the partition table entry on VM teardown before returning the LPID to the pool. This means if we were running as a nested hypervisor the real hypervisor could use this to determine when it can free resources.
--- arch/powerpc/kvm/book3s_hv.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/powerpc/kvm/book3s_hv.c +7 -1
@@ -4501,13 +4501,19 @@ static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)kvmppc_free_vcores (kvm); -kvmppc_free_lpid (kvm->arch.lpid);if (kvm_is_radix (kvm))kvmppc_free_radix (kvm);else kvmppc_free_hpt (&kvm->arch.hpt); +/* Perform global invalidation and return lpid to the pool */ +if (cpu_has_feature (CPU_FTR_ARCH_300 )) { + kvm->arch.process_table =0 ; +kvmppc_setup_partition_table (kvm); + } +kvmppc_free_lpid (kvm->arch.lpid); +kvmppc_free_pimap (kvm); }
Signature · 1 line
2.7.4
PATCH v3 14/33 KVM: PPC: Book3S HV: Make kvmppc_mmu_radix_xlate process/partition table agnostic
In reply to Paul MackerrasFrom: Suraj Jitindar Singh <[email protected]>
kvmppc_mmu_radix_xlate() is used to translate an effective address through the process tables. The process table and partition tables have identical layout. Exploit this fact to make the kvmppc_mmu_radix_xlate() function able to translate either an effective address through the process tables or a guest real address through the partition tables.
[[email protected] - reduced diffs from previous code]
--- arch/powerpc/include/asm/kvm_book3s.h | 3 + arch/powerpc/kvm/book3s_64_mmu_radix.c | 109 +++++++++++++++++++++++---------- 2 files changed, 78 insertions(+), 34 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +3 -0
@@ -188,6 +188,9 @@ extern int kvmppc_book3s_hcall_implemented(struct kvm *kvm, unsigned long hc);extern int kvmppc_book3s_radix_page_fault (struct kvm_run *run,struct kvm_vcpu *vcpu,unsigned long ea,unsigned long dsisr); +extern int kvmppc_mmu_radix_translate_table (struct kvm_vcpu *vcpu,gva_t eaddr, +struct kvmppc_pte *gpte,u64 table, +int table_index,u64 *pte_ret_p);extern int kvmppc_mmu_radix_xlate (struct kvm_vcpu *vcpu,gva_t eaddr,struct kvmppc_pte *gpte,bool data,bool iswrite);extern int kvmppc_init_vm_radix (struct kvm *kvm);
arch/powerpc/kvm/book3s_64_mmu_radix.c +75 -34
164 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 15/33 KVM: PPC: Book3S HV: Refactor radix page fault handler
In reply to Paul MackerrasFrom: Suraj Jitindar Singh <[email protected]>
The radix page fault handler accounts for all cases, including just needing to insert a pte. This breaks it up into separate functions for the two main cases; setting rc and inserting a pte.
This allows us to make the setting of rc and inserting of a pte generic for any pgtable, not specific to the one for this guest.
[[email protected] - reduced diffs from previous code]
--- arch/powerpc/kvm/book3s_64_mmu_radix.c | 210 +++++++++++++++++++-------------- 1 file changed, 123 insertions(+), 87 deletions(-)
arch/powerpc/kvm/book3s_64_mmu_radix.c +123 -87
@@ -400,8 +400,9 @@ static void kvmppc_unmap_free_pud_entry_table(struct kvm *kvm, pud_t *pud,*/ #define PTE_BITS_MUST_MATCH (~(_PAGE_WRITE | _PAGE_DIRTY | _PAGE_ACCESSED)) -static int kvmppc_create_pte (struct kvm *kvm,pte_t pte,unsigned long gpa, -unsigned int level,unsigned long mmu_seq) +static int kvmppc_create_pte (struct kvm *kvm,pgd_t *pgtable,pte_t pte, +unsigned long gpa,unsigned int level, +unsigned long mmu_seq) {pgd_t *pgd;pud_t *pud, *new_pud =NULL ;
@@ -410,7 +411,7 @@ static int kvmppc_create_pte(struct kvm *kvm, pte_t pte, unsigned long gpa,int ret;/* Traverse the guest's 2nd-level tree, allocate new levels needed */ - pgd = kvm->arch.pgtable +pgd_index (gpa); + pgd = pgtable +pgd_index (gpa); pud =NULL ;if (pgd_present (*pgd)) pud =pud_offset (pgd, gpa);
@@ -565,95 +566,49 @@ static int kvmppc_create_pte(struct kvm *kvm, pte_t pte, unsigned long gpa,return ret; } -int kvmppc_book3s_radix_page_fault (struct kvm_run *run,struct kvm_vcpu *vcpu, -unsigned long ea,unsigned long dsisr) +static bool kvmppc_hv_handle_set_rc (struct kvm *kvm,pgd_t *pgtable, +bool writing,unsigned long gpa) +{ +unsigned long pgflags; +unsigned int shift; +pte_t *ptep; + +/* +* Need to set an R or C bit in the 2nd-level tables; +* since we are just helping out the hardware here, +* it is sufficient to do what the hardware does. +*/ + pgflags = _PAGE_ACCESSED; +if (writing) + pgflags |= _PAGE_DIRTY; +/* +* We are walking the secondary (partition-scoped) page table here. +* We can do this without disabling irq because the Linux MM +* subsystem doesn't do THP splits and collapses on this tree. +*/ + ptep =__find_linux_pte (pgtable, gpa,NULL , &shift); +if (ptep &&pte_present (*ptep) && (!writing ||pte_write (*ptep))) { +kvmppc_radix_update_pte (kvm, ptep,0 , pgflags, gpa, shift); +return true; + } +return false; +} + +static int kvmppc_book3s_instantiate_page (struct kvm_vcpu *vcpu, +unsigned long gpa, +struct kvm_memory_slot *memslot, +bool writing,bool kvm_ro, +pte_t *inserted_pte,unsigned int *levelp) {struct kvm *kvm = vcpu->kvm; -unsigned long mmu_seq; -unsigned long gpa, gfn, hva; -struct kvm_memory_slot *memslot;struct page *page =NULL ; -long ret; -bool writing; +unsigned long mmu_seq; +unsigned long hva, gfn = gpa >>PAGE_SHIFT ;bool upgrade_write = false;bool *upgrade_p = &upgrade_write;pte_t pte, *ptep; -unsigned long pgflags;unsigned int shift, level; - -/* Check for unusual errors */ -if (dsisr &DSISR_UNSUPP_MMU ) { -pr_err ("KVM: Got unsupported MMU fault\n" ); -return -EFAULT ; - } -if (dsisr &DSISR_BADACCESS ) { -/* Reflect to the guest as DSI */ -pr_err ("KVM: Got radix HV page fault with DSISR=%lx\n" , dsisr); -kvmppc_core_queue_data_storage (vcpu, ea, dsisr); -return RESUME_GUEST ; - } - -/* Translate the logical address and get the page */ - gpa = vcpu->arch.fault_gpa & ~0xfffUL ; - gpa &= ~0xF000000000000000ul ; - gfn = gpa >>PAGE_SHIFT ; -if (!(dsisr &DSISR_PRTABLE_FAULT )) - gpa |= ea &0xfff ; - memslot =gfn_to_memslot (kvm, gfn); - -/* No memslot means it's an emulated MMIO region */ -if (!memslot || (memslot->flags &KVM_MEMSLOT_INVALID )) { -if (dsisr & (DSISR_PRTABLE_FAULT |DSISR_BADACCESS | -DSISR_SET_RC )) { -/* -* Bad address in guest page table tree, or other -* unusual error - reflect it to the guest as DSI. -*/ -kvmppc_core_queue_data_storage (vcpu, ea, dsisr); -return RESUME_GUEST ; - } -return kvmppc_hv_emulate_mmio (run, vcpu, gpa, ea, - dsisr &DSISR_ISSTORE ); - } - - writing = (dsisr &DSISR_ISSTORE ) !=0 ; -if (memslot->flags &KVM_MEM_READONLY ) { -if (writing) { -/* give the guest a DSI */ - dsisr =DSISR_ISSTORE |DSISR_PROTFAULT ; -kvmppc_core_queue_data_storage (vcpu, ea, dsisr); -return RESUME_GUEST ; - } - upgrade_p =NULL ; - } - -if (dsisr &DSISR_SET_RC ) { -/* -* Need to set an R or C bit in the 2nd-level tables; -* since we are just helping out the hardware here, -* it is sufficient to do what the hardware does. -*/ - pgflags = _PAGE_ACCESSED; -if (writing) - pgflags |= _PAGE_DIRTY; -/* -* We are walking the secondary page table here. We can do this -* without disabling irq. -*/ -spin_lock (&kvm->mmu_lock); - ptep =__find_linux_pte (kvm->arch.pgtable, - gpa,NULL , &shift); -if (ptep &&pte_present (*ptep) && - (!writing ||pte_write (*ptep))) { -kvmppc_radix_update_pte (kvm, ptep,0 , pgflags, - gpa, shift); - dsisr &= ~DSISR_SET_RC ; - } -spin_unlock (&kvm->mmu_lock); -if (!(dsisr & (DSISR_BAD_FAULT_64S |DSISR_NOHPTE | -DSISR_PROTFAULT |DSISR_SET_RC ))) -return RESUME_GUEST ; - } +int ret;/* used to check for invalidations in progress */ mmu_seq = kvm->mmu_notifier_seq;
@@ -666,7 +621,7 @@ int kvmppc_book3s_radix_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,* is that the page is writable. */ hva =gfn_to_hva_memslot (memslot, gfn); -if (upgrade_p &&__get_user_pages_fast (hva,1 ,1 , &page) ==1 ) { +if (!kvm_ro &&__get_user_pages_fast (hva,1 ,1 , &page) ==1 ) { upgrade_write = true; }else {unsigned long pfn;
@@ -724,7 +679,12 @@ int kvmppc_book3s_radix_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu, }/* Allocate space in the tree and write the PTE */ - ret =kvmppc_create_pte (kvm, pte, gpa, level, mmu_seq); + ret =kvmppc_create_pte (kvm, kvm->arch.pgtable, pte, gpa, level, + mmu_seq); +if (inserted_pte) + *inserted_pte = pte; +if (levelp) + *levelp = level;if (page) {if (!ret && (pte_val (pte) & _PAGE_WRITE))
@@ -732,6 +692,82 @@ int kvmppc_book3s_radix_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,put_page (page); } +return ret; +} + +int kvmppc_book3s_radix_page_fault (struct kvm_run *run,struct kvm_vcpu *vcpu, +unsigned long ea,unsigned long dsisr) +{ +struct kvm *kvm = vcpu->kvm; +unsigned long gpa, gfn; +struct kvm_memory_slot *memslot; +long ret; +bool writing = !!(dsisr &DSISR_ISSTORE ); +bool kvm_ro = false; + +/* Check for unusual errors */ +if (dsisr &DSISR_UNSUPP_MMU ) { +pr_err ("KVM: Got unsupported MMU fault\n" ); +return -EFAULT ; + } +if (dsisr &DSISR_BADACCESS ) { +/* Reflect to the guest as DSI */ +pr_err ("KVM: Got radix HV page fault with DSISR=%lx\n" , dsisr); +kvmppc_core_queue_data_storage (vcpu, ea, dsisr); +return RESUME_GUEST ; + } + +/* Translate the logical address */ + gpa = vcpu->arch.fault_gpa & ~0xfffUL ; + gpa &= ~0xF000000000000000ul ; + gfn = gpa >>PAGE_SHIFT ; +if (!(dsisr &DSISR_PRTABLE_FAULT )) + gpa |= ea &0xfff ; + +/* Get the corresponding memslot */ + memslot =gfn_to_memslot (kvm, gfn); + +/* No memslot means it's an emulated MMIO region */ +if (!memslot || (memslot->flags &KVM_MEMSLOT_INVALID )) { +if (dsisr & (DSISR_PRTABLE_FAULT |DSISR_BADACCESS | +DSISR_SET_RC )) { +/* +* Bad address in guest page table tree, or other +* unusual error - reflect it to the guest as DSI. +*/ +kvmppc_core_queue_data_storage (vcpu, ea, dsisr); +return RESUME_GUEST ; + } +return kvmppc_hv_emulate_mmio (run, vcpu, gpa, ea, writing); + } + +if (memslot->flags &KVM_MEM_READONLY ) { +if (writing) { +/* give the guest a DSI */ +kvmppc_core_queue_data_storage (vcpu, ea,DSISR_ISSTORE | +DSISR_PROTFAULT ); +return RESUME_GUEST ; + } + kvm_ro = true; + } + +/* Failed to set the reference/change bits */ +if (dsisr &DSISR_SET_RC ) { +spin_lock (&kvm->mmu_lock); +if (kvmppc_hv_handle_set_rc (kvm, kvm->arch.pgtable, + writing, gpa)) + dsisr &= ~DSISR_SET_RC ; +spin_unlock (&kvm->mmu_lock); + +if (!(dsisr & (DSISR_BAD_FAULT_64S |DSISR_NOHPTE | +DSISR_PROTFAULT |DSISR_SET_RC ))) +return RESUME_GUEST ; + } + +/* Try to insert a pte */ + ret =kvmppc_book3s_instantiate_page (vcpu, gpa, memslot, writing, + kvm_ro,NULL ,NULL ); +if (ret ==0 || ret == -EAGAIN ) ret =RESUME_GUEST ;return ret;
Signature · 1 line
2.7.4
PATCH v3 16/33 KVM: PPC: Book3S HV: Use kvmppc_unmap_pte() in kvm_unmap_radix()
In reply to Paul Mackerraskvmppc_unmap_pte() does a sequence of operations that are open-coded in kvm_unmap_radix(). This extends kvmppc_unmap_pte() a little so that it can be used by kvm_unmap_radix(), and makes kvm_unmap_radix() call it.
--- arch/powerpc/kvm/book3s_64_mmu_radix.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-)
arch/powerpc/kvm/book3s_64_mmu_radix.c +13 -20
@@ -240,19 +240,22 @@ static void kvmppc_pmd_free(pmd_t *pmdp) }static void kvmppc_unmap_pte (struct kvm *kvm,pte_t *pte, -unsigned long gpa,unsigned int shift) +unsigned long gpa,unsigned int shift, +struct kvm_memory_slot *memslot) { -unsigned long page_size =1ul << shift;unsigned long old; old =kvmppc_radix_update_pte (kvm, pte, ~0UL ,0 , gpa, shift);kvmppc_radix_tlbie_page (kvm, gpa, shift);if (old & _PAGE_DIRTY) {unsigned long gfn = gpa >>PAGE_SHIFT ; -struct kvm_memory_slot *memslot; +unsigned long page_size =PAGE_SIZE ; - memslot =gfn_to_memslot (kvm, gfn); +if (shift) + page_size =1ul << shift; +if (!memslot) + memslot =gfn_to_memslot (kvm, gfn);if (memslot && memslot->dirty_bitmap)kvmppc_update_dirty_map (memslot, gfn, page_size); }
@@ -282,7 +285,7 @@ static void kvmppc_unmap_free_pte(struct kvm *kvm, pte_t *pte, bool full)WARN_ON_ONCE (1 );kvmppc_unmap_pte (kvm, p,pte_pfn (*p) <<PAGE_SHIFT , -PAGE_SHIFT ); +PAGE_SHIFT ,NULL ); } }
@@ -304,7 +307,7 @@ static void kvmppc_unmap_free_pmd(struct kvm *kvm, pmd_t *pmd, bool full)WARN_ON_ONCE (1 );kvmppc_unmap_pte (kvm, (pte_t *)p,pte_pfn (*(pte_t *)p) <<PAGE_SHIFT , -PMD_SHIFT ); +PMD_SHIFT ,NULL ); } }else {pte_t *pte;
@@ -468,7 +471,7 @@ static int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte, goto out_unlock; }/* Valid 1GB page here already, remove it */ -kvmppc_unmap_pte (kvm, (pte_t *)pud, hgpa,PUD_SHIFT ); +kvmppc_unmap_pte (kvm, (pte_t *)pud, hgpa,PUD_SHIFT ,NULL ); }if (level ==2 ) {if (!pud_none (*pud)) {
@@ -517,7 +520,7 @@ static int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte, goto out_unlock; }/* Valid 2MB page here already, remove it */ -kvmppc_unmap_pte (kvm,pmdp_ptep (pmd), lgpa,PMD_SHIFT ); +kvmppc_unmap_pte (kvm,pmdp_ptep (pmd), lgpa,PMD_SHIFT ,NULL ); }if (level ==1 ) {if (!pmd_none (*pmd)) {
@@ -780,20 +783,10 @@ int kvm_unmap_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,pte_t *ptep;unsigned long gpa = gfn <<PAGE_SHIFT ;unsigned int shift; -unsigned long old; ptep =__find_linux_pte (kvm->arch.pgtable, gpa,NULL , &shift); -if (ptep &&pte_present (*ptep)) { - old =kvmppc_radix_update_pte (kvm, ptep, ~0UL ,0 , - gpa, shift); -kvmppc_radix_tlbie_page (kvm, gpa, shift); -if ((old & _PAGE_DIRTY) && memslot->dirty_bitmap) { -unsigned long psize =PAGE_SIZE ; -if (shift) - psize =1ul << shift; -kvmppc_update_dirty_map (memslot, gfn, psize); - } - } +if (ptep &&pte_present (*ptep)) +kvmppc_unmap_pte (kvm, ptep, gpa, shift, memslot);return 0 ; }
Signature · 1 line
2.7.4
PATCH v3 17/33 KVM: PPC: Book3S HV: Framework and hcall stubs for nested virtualization
In reply to Paul MackerrasThis starts the process of adding the code to support nested HV-style virtualization. It defines a new H_SET_PARTITION_TABLE hypercall which a nested hypervisor can use to set the base address and size of a partition table in its memory (analogous to the PTCR register). On the host (level 0 hypervisor) side, the H_SET_PARTITION_TABLE hypercall from the guest is handled by code that saves the virtual PTCR value for the guest.
This also adds code for creating and destroying nested guests and for reading the partition table entry for a nested guest from L1 memory. Each nested guest has its own shadow LPID value, different in general from the LPID value used by the nested hypervisor to refer to it. The shadow LPID value is allocated at nested guest creation time.
Nested hypervisor functionality is only available for a radix guest, which therefore means a radix host on a POWER9 (or later) processor.
--- arch/powerpc/include/asm/hvcall.h | 5 + arch/powerpc/include/asm/kvm_book3s.h | 10 +- arch/powerpc/include/asm/kvm_book3s_64.h | 33 ++++ arch/powerpc/include/asm/kvm_book3s_asm.h | 3 + arch/powerpc/include/asm/kvm_host.h | 5 + arch/powerpc/kvm/Makefile | 3 +- arch/powerpc/kvm/book3s_hv.c | 27 ++- arch/powerpc/kvm/book3s_hv_nested.c | 287 ++++++++++++++++++++++++++++++ 8 files changed, 366 insertions(+), 7 deletions(-) create mode 100644 arch/powerpc/kvm/book3s_hv_nested.c
arch/powerpc/include/asm/hvcall.h +5 -0
@@ -322,6 +322,11 @@#define H_GET_24X7_DATA 0xF07C#define H_GET_PERF_COUNTER_INFO 0xF080 +/* Platform-specific hcalls used for nested HV KVM */ +#define H_SET_PARTITION_TABLE 0xF800 +#define H_ENTER_NESTED 0xF804 +#define H_TLB_INVALIDATE 0xF808 +/* Values for 2nd argument to H_SET_MODE */ #define H_SET_MODE_RESOURCE_SET_CIABR 1#define H_SET_MODE_RESOURCE_SET_DAWR 2
arch/powerpc/include/asm/kvm_book3s.h +7 -3
@@ -274,6 +274,13 @@ static inline void kvmppc_save_tm_sprs(struct kvm_vcpu *vcpu) {}static inline void kvmppc_restore_tm_sprs (struct kvm_vcpu *vcpu) {}#endif +long kvmhv_nested_init (void ); +void kvmhv_nested_exit (void ); +void kvmhv_vm_nested_init (struct kvm *kvm); +long kvmhv_set_partition_table (struct kvm_vcpu *vcpu); +void kvmhv_set_ptbl_entry (unsigned int lpid,u64 dw0,u64 dw1); +void kvmhv_release_all_nested (struct kvm *kvm); +void kvmppc_giveup_fac (struct kvm_vcpu *vcpu,ulong fac);extern int kvm_irq_bypass;
@@ -387,9 +394,6 @@ extern int kvmppc_h_logical_ci_store(struct kvm_vcpu *vcpu);/* TO = 31 for unconditional trap */ #define INS_TW 0x7fe00008 -/* LPIDs we support with this build -- runtime limit may be lower */ -#define KVMPPC_NR_LPIDS (LPID_RSVD + 1) -#define SPLIT_HACK_MASK 0xff000000#define SPLIT_HACK_OFFS 0xfb000000
arch/powerpc/include/asm/kvm_book3s_64.h +33 -0
@@ -23,6 +23,39 @@#include <linux/string.h> #include <asm/bitops.h> #include <asm/book3s/64/mmu-hash.h> +#include <asm/cpu_has_feature.h> + +#ifdef CONFIG_PPC_PSERIES +static inline bool kvmhv_on_pseries (void ) +{ +return !cpu_has_feature (CPU_FTR_HVMODE ); +} +#else +static inline bool kvmhv_on_pseries (void ) +{ +return false; +} +#endif + +/* +* Structure for a nested guest, that is, for a guest that is managed by +* one of our guests. +*/ +struct kvm_nested_guest { +struct kvm *l1_host;/* L1 VM that owns this nested guest */ +int l1_lpid;/* lpid L1 guest thinks this guest is */ +int shadow_lpid;/* real lpid of this nested guest */ +pgd_t *shadow_pgtable;/* our page table for this guest */ +u64 l1_gr_to_hr;/* L1's addr of part'n-scoped table */ +u64 process_table;/* process table entry for this guest */ +long refcnt;/* number of pointers to this struct */ +struct mutex tlb_lock;/* serialize page faults and tlbies */ +struct kvm_nested_guest *next; +}; + +struct kvm_nested_guest *kvmhv_get_nested (struct kvm *kvm,int l1_lpid, +bool create); +void kvmhv_put_nested (struct kvm_nested_guest *gp);/* Power architecture requires HPT is at least 256kiB, at most 64TiB */ #define PPC_MIN_HPT_ORDER 18
arch/powerpc/include/asm/kvm_book3s_asm.h +3 -0
@@ -25,6 +25,9 @@#define XICS_MFRR 0xc#define XICS_IPI 2/* interrupt source # for IPIs */ +/* LPIDs we support with this build -- runtime limit may be lower */ +#define KVMPPC_NR_LPIDS (LPID_RSVD + 1) +/* Maximum number of threads per physical core */ #define MAX_SMT_THREADS 8
arch/powerpc/include/asm/kvm_host.h +5 -0
@@ -46,6 +46,7 @@#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE #include <asm/kvm_book3s_asm.h> /* for MAX_SMT_THREADS */ #define KVM_MAX_VCPU_ID (MAX_SMT_THREADS * KVM_MAX_VCORES) +#define KVM_MAX_NESTED_GUESTS KVMPPC_NR_LPIDS#else #define KVM_MAX_VCPU_ID KVM_MAX_VCPUS
@@ -287,6 +288,7 @@ struct kvm_arch {u8 radix;u8 fwnmi_enabled;bool threads_indep; +bool nested_enable;pgd_t *pgtable;u64 process_table;struct dentry *debugfs_dir;
@@ -312,6 +314,9 @@ struct kvm_arch {#endif struct kvmppc_ops *kvm_ops;#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE +u64 l1_ptcr; +int max_nested_lpid; +struct kvm_nested_guest *nested_guests[KVM_MAX_NESTED_GUESTS ];/* This array can grow quite large, keep it at the end */ struct kvmppc_vcore *vcores[KVM_MAX_VCORES ];#endif
arch/powerpc/kvm/Makefile +2 -1
@@ -75,7 +75,8 @@ kvm-hv-y += \ book3s_hv.o \ book3s_hv_interrupts.o \ book3s_64_mmu_hv.o \ - book3s_64_mmu_radix.o + book3s_64_mmu_radix.o \ + book3s_hv_nested.o kvm-hv-$(CONFIG_PPC_TRANSACTIONAL_MEM) += \ book3s_hv_tm.o
arch/powerpc/kvm/book3s_hv.c +24 -3
@@ -934,6 +934,19 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)if (ret ==H_TOO_HARD )return RESUME_HOST ;break ; + +case H_SET_PARTITION_TABLE : + ret =H_FUNCTION ; +if (vcpu->kvm->arch.nested_enable) + ret =kvmhv_set_partition_table (vcpu); +break ; +case H_ENTER_NESTED : + ret =H_FUNCTION ; +break ; +case H_TLB_INVALIDATE : + ret =H_FUNCTION ; +break ; +default :return RESUME_HOST ; }
@@ -4149,8 +4162,7 @@ void kvmppc_setup_partition_table(struct kvm *kvm)__pa (kvm->arch.pgtable) |RADIX_PGD_INDEX_SIZE ; dw1 =PATB_GR | kvm->arch.process_table; } - -mmu_partition_table_set_entry (kvm->arch.lpid, dw0, dw1); +kvmhv_set_ptbl_entry (kvm->arch.lpid, dw0, dw1); }/*
@@ -4366,6 +4378,8 @@ static int kvmppc_core_init_vm_hv(struct kvm *kvm)kvmppc_alloc_host_rm_ops (); +kvmhv_vm_nested_init (kvm); +/* * Since we don't flush the TLB when tearing down a VM, * and this lpid might have previously been used,
@@ -4509,8 +4523,10 @@ static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)/* Perform global invalidation and return lpid to the pool */ if (cpu_has_feature (CPU_FTR_ARCH_300 )) { +if (kvm->arch.nested_enable) +kvmhv_release_all_nested (kvm); kvm->arch.process_table =0 ; -kvmppc_setup_partition_table (kvm); +kvmhv_set_ptbl_entry (kvm->arch.lpid,0 ,0 ); }kvmppc_free_lpid (kvm->arch.lpid);
@@ -4981,6 +4997,10 @@ static int kvmppc_book3s_init_hv(void)if (r <0 )return -ENODEV ; + r =kvmhv_nested_init (); +if (r) +return r; + r =kvm_init_subcore_bitmap ();if (r)return r;
@@ -5039,6 +5059,7 @@ static void kvmppc_book3s_exit_hv(void)if (kvmppc_radix_possible ())kvmppc_radix_exit (); kvmppc_hv_ops =NULL ; +kvmhv_nested_exit (); }module_init (kvmppc_book3s_init_hv);
arch/powerpc/kvm/book3s_hv_nested.c new file +287 -0
287 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH v3 17/33] KVM: PPC: Book3S HV: Framework and hcall stubs for nested virtualization
Quoted 14 lines · Paul Mackerras
This starts the process of adding the code to support nested HV-style virtualization. It defines a new
H_SET_PARTITION_TABLEhypercall which a nested hypervisor can use to set the base address and size of a partition table in its memory (analogous to the PTCR register). On the host (level 0 hypervisor) side, theH_SET_PARTITION_TABLEhypercall from the guest is handled by code that saves the virtual PTCR value for the guest.This also adds code for creating and destroying nested guests and for reading the partition table entry for a nested guest from L1 memory. Each nested guest has its own shadow LPID value, different in general from the LPID value used by the nested hypervisor to refer to it. The shadow LPID value is allocated at nested guest creation time.
Quoted 4 lines
Nested hypervisor functionality is only available for a radix guest, which therefore means a radix host on a POWER9 (or later) processor.
Signed-off-by Paul Mackerras <[email protected]>
Couple of nits noted below
[snip]
arch/powerpc/kvm/book3s_hv_nested.c line 81
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_nested.c new file +10
-0+void kvmhv_set_ptbl_entry (unsigned int lpid,u64 dw0,u64 dw1) +{ +if (cpu_has_feature (CPU_FTR_HVMODE )) { +mmu_partition_table_set_entry (lpid, dw0, dw1); + }else { + pseries_partition_tb[lpid].patb0 =cpu_to_be64 (dw0); + pseries_partition_tb[lpid].patb1 =cpu_to_be64 (dw1); +/* this will be emulated, L0 will do the necessary barriers */ + asm volatile(PPC_TLBIE_5(%0, %1, 2, 0, 1) : : + "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid));
It seems a bit odd to me to introduce this with a mechanism we never actually implement, then replace it. But I guess if refolding the series is painful..
Quoted 1 line
(quoted diff) +1
-0+ }
[snip]
arch/powerpc/kvm/book3s_hv_nested.c line 109
Quoted 11 lines
arch/powerpc/kvm/book3s_hv_nested.c new file +11
-0+/* +* Handle the H_SET_PARTITION_TABLE hcall. +* r4 = guest real address of partition table + log_2(size) - 12 +* (formatted as for the PTCR). +*/ +long kvmhv_set_partition_table (struct kvm_vcpu *vcpu) +{ +struct kvm *kvm = vcpu->kvm; +unsigned long ptcr =kvmppc_get_gpr (vcpu,4 ); + + kvm->arch.l1_ptcr = ptcr;
Still no validation here.
Quoted 2 lines
(quoted diff) +2
-0+ return H_SUCCESS; +}
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v3 18/33 KVM: PPC: Book3S HV: Nested guest entry via hypercall
In reply to Paul MackerrasThis adds a new hypercall, H_ENTER_NESTED, which is used by a nested hypervisor to enter one of its nested guests. The hypercall supplies register values in two structs. Those values are copied by the level 0 (L0) hypervisor (the one which is running in hypervisor mode) into the vcpu struct of the L1 guest, and then the guest is run until an interrupt or error occurs which needs to be reported to L1 via the hypercall return value.
Currently this assumes that the L0 and L1 hypervisors are the same endianness, and the structs passed as arguments are in native endianness. If they are of different endianness, the version number check will fail and the hcall will be rejected.
Nested hypervisors do not support indep_threads_mode=N, so this adds code to print a warning message if the administrator has set indep_threads_mode=N, and treat it as Y.
--- arch/powerpc/include/asm/hvcall.h | 36 +++++ arch/powerpc/include/asm/kvm_book3s.h | 7 + arch/powerpc/include/asm/kvm_host.h | 5 + arch/powerpc/kernel/asm-offsets.c | 1 + arch/powerpc/kvm/book3s_hv.c | 214 ++++++++++++++++++++++++----- arch/powerpc/kvm/book3s_hv_nested.c | 230 ++++++++++++++++++++++++++++++++ arch/powerpc/kvm/book3s_hv_rmhandlers.S | 8 ++ 7 files changed, 471 insertions(+), 30 deletions(-)
arch/powerpc/include/asm/hvcall.h +36 -0
@@ -466,6 +466,42 @@ struct h_cpu_char_result {u64 behaviour; }; +/* Register state for entering a nested guest with H_ENTER_NESTED */ +struct hv_guest_state { +u64 version;/* version of this structure layout */ +u32 lpid; +u32 vcpu_token; +/* These registers are hypervisor privileged (at least for writing) */ +u64 lpcr; +u64 pcr; +u64 amor; +u64 dpdes; +u64 hfscr; +s64 tb_offset; +u64 dawr0; +u64 dawrx0; +u64 ciabr; +u64 hdec_expiry; +u64 purr; +u64 spurr; +u64 ic; +u64 vtb; +u64 hdar; +u64 hdsisr; +u64 heir; +u64 asdr; +/* These are OS privileged but need to be set late in guest entry */ +u64 srr0; +u64 srr1; +u64 sprg[4 ]; +u64 pidr; +u64 cfar; +u64 ppr; +}; + +/* Latest version of hv_guest_state structure */ +#define HV_GUEST_STATE_VERSION 1 +#endif /* __ASSEMBLY__ */ #endif /* __KERNEL__ */ #endif /* _ASM_POWERPC_HVCALL_H */
arch/powerpc/include/asm/kvm_book3s.h +7 -0
@@ -280,6 +280,13 @@ void kvmhv_vm_nested_init(struct kvm *kvm);long kvmhv_set_partition_table (struct kvm_vcpu *vcpu);void kvmhv_set_ptbl_entry (unsigned int lpid,u64 dw0,u64 dw1);void kvmhv_release_all_nested (struct kvm *kvm); +long kvmhv_enter_nested_guest (struct kvm_vcpu *vcpu); +int kvmhv_run_single_vcpu (struct kvm_run *kvm_run,struct kvm_vcpu *vcpu, +u64 time_limit,unsigned long lpcr); +void kvmhv_save_hv_regs (struct kvm_vcpu *vcpu,struct hv_guest_state *hr); +void kvmhv_restore_hv_return_state (struct kvm_vcpu *vcpu, +struct hv_guest_state *hr); +long int kvmhv_nested_page_fault (struct kvm_vcpu *vcpu);void kvmppc_giveup_fac (struct kvm_vcpu *vcpu,ulong fac);
arch/powerpc/include/asm/kvm_host.h +5 -0
@@ -95,6 +95,7 @@ struct dtl_entry;struct kvmppc_vcpu_book3s ;struct kvmppc_book3s_shadow_vcpu ; +struct kvm_nested_guest ;struct kvm_vm_stat {ulong remote_tlb_flush;
@@ -786,6 +787,10 @@ struct kvm_vcpu_arch {u32 emul_inst;u32 online; + +/* For support of nested guests */ +struct kvm_nested_guest *nested; +u32 nested_vcpu_id;#endif #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
arch/powerpc/kernel/asm-offsets.c +1 -0
@@ -503,6 +503,7 @@ int main(void)OFFSET (VCPU_VPA , kvm_vcpu, arch.vpa.pinned_addr);OFFSET (VCPU_VPA_DIRTY , kvm_vcpu, arch.vpa.dirty);OFFSET (VCPU_HEIR , kvm_vcpu, arch.emul_inst); +OFFSET (VCPU_NESTED , kvm_vcpu, arch.nested);OFFSET (VCPU_CPU , kvm_vcpu, cpu);OFFSET (VCPU_THREAD_CPU , kvm_vcpu, arch.thread_cpu);#endif
arch/powerpc/kvm/book3s_hv.c +184 -30
316 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_nested.c +230 -0
239 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_rmhandlers.S +8 -0
@@ -1393,6 +1393,10 @@ END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR) BEGIN_FTR_SECTION PPC_MSGSYNC lwsync + /* always exit if we're running a nested guest */ + ld r0, VCPU_NESTED(r9) + cmpdi r0, 0 + bne guest_exit_cont END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300) lbz r0, HSTATE_HOST_IPI(r13) cmpwi r0, 0
@@ -2199,6 +2203,10 @@ hcall_try_real_mode: andi. r0,r11,MSR_PR /* sc 1 from userspace - reflect to guest syscall */ bne sc_1_fast_return + /* sc 1 from nested guest - give it to L1 to handle */ + ld r0, VCPU_NESTED(r9) + cmpdi r0, 0 + bne guest_exit_cont clrrdi r3,r3,2 cmpldi r3,hcall_real_table_end - hcall_real_table bge guest_exit_cont
Signature · 1 line
2.7.4
Re: [PATCH v3 18/33] KVM: PPC: Book3S HV: Nested guest entry via hypercall
Quoted 13 lines · Paul Mackerras
This adds a new hypercall,
H_ENTER_NESTED, which is used by a nested hypervisor to enter one of its nested guests. The hypercall supplies register values in two structs. Those values are copied by the level 0 (L0) hypervisor (the one which is running in hypervisor mode) into the vcpu struct of the L1 guest, and then the guest is run until an interrupt or error occurs which needs to be reported to L1 via the hypercall return value.Currently this assumes that the L0 and L1 hypervisors are the same endianness, and the structs passed as arguments are in native endianness. If they are of different endianness, the version number check will fail and the hcall will be rejected.
Quoted 5 lines
Nested hypervisors do not support indep_threads_mode=N, so this adds code to print a warning message if the administrator has set indep_threads_mode=N, and treat it as Y.
Signed-off-by Paul Mackerras <[email protected]>
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v3 19/33 KVM: PPC: Book3S HV: Use XICS hypercalls when running as a nested hypervisor
In reply to Paul MackerrasThis adds code to call the H_IPI and H_EOI hypercalls when we are running as a nested hypervisor (i.e. without the CPU_FTR_HVMODE cpu feature) and we would otherwise access the XICS interrupt controller directly or via an OPAL call.
--- arch/powerpc/kvm/book3s_hv.c | 7 +++++- arch/powerpc/kvm/book3s_hv_builtin.c | 44 +++++++++++++++++++++++++++++------- arch/powerpc/kvm/book3s_hv_rm_xics.c | 8 +++++++ 3 files changed, 50 insertions(+), 9 deletions(-)
arch/powerpc/kvm/book3s_hv.c +6 -1
@@ -173,6 +173,10 @@ static bool kvmppc_ipi_thread(int cpu) {unsigned long msg =PPC_DBELL_TYPE (PPC_DBELL_SERVER ); +/* If we're a nested hypervisor, fall back to ordinary IPIs for now */ +if (kvmhv_on_pseries ()) +return false; +/* On POWER9 we can use msgsnd to IPI any cpu */ if (cpu_has_feature (CPU_FTR_ARCH_300 )) { msg |=get_hard_smp_processor_id (cpu);
@@ -5165,7 +5169,8 @@ static int kvmppc_book3s_init_hv(void)* indirectly, via OPAL. */ #ifdef CONFIG_SMP -if (!xive_enabled () && !local_paca->kvm_hstate.xics_phys) { +if (!xive_enabled () && !kvmhv_on_pseries () && + !local_paca->kvm_hstate.xics_phys) {struct device_node *np; np =of_find_compatible_node (NULL ,NULL ,"ibm,opal-intc" );
arch/powerpc/kvm/book3s_hv_builtin.c +36 -8
68 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_rm_xics.c +8 -0
@@ -767,6 +767,14 @@ static void icp_eoi(struct irq_chip *c, u32 hwirq, __be32 xirr, bool *again)void __iomem *xics_phys;int64_t rc; +if (kvmhv_on_pseries ()) { +unsigned long retbuf[PLPAR_HCALL_BUFSIZE ]; + +iosync (); +plpar_hcall_raw (H_EOI , retbuf, hwirq); +return ; + } + rc =pnv_opal_pci_msi_eoi (c, hwirq);if (rc)
Signature · 1 line
2.7.4
PATCH v3 20/33 KVM: PPC: Book3S HV: Handle hypercalls correctly when nested
In reply to Paul MackerrasWhen we are running as a nested hypervisor, we use a hypercall to enter the guest rather than code in book3s_hv_rmhandlers.S. This means that the hypercall handlers listed in hcall_real_table never get called. There are some hypercalls that are handled there and not in kvmppc_pseries_do_hcall(), which therefore won't get processed for a nested guest.
To fix this, we add cases to kvmppc_pseries_do_hcall() to handle those hypercalls, with the following exceptions:
- The HPT hypercalls (H_ENTER, H_REMOVE, etc.) are not handled because we only support radix mode for nested guests.
- H_CEDE has to be handled specially because the cede logic in kvmhv_run_single_vcpu assumes that it has been processed by the time that kvmhv_p9_guest_entry() returns. Therefore we put a special case for H_CEDE in kvmhv_p9_guest_entry().
For the XICS hypercalls, if real-mode processing is enabled, then the virtual-mode handlers assume that they are being called only to finish up the operation. Therefore we turn off the real-mode flag in the XICS code when running as a nested hypervisor.
--- arch/powerpc/include/asm/asm-prototypes.h | 4 +++ arch/powerpc/kvm/book3s_hv.c | 43 +++++++++++++++++++++++++++++++ arch/powerpc/kvm/book3s_hv_rmhandlers.S | 2 ++ arch/powerpc/kvm/book3s_xics.c | 3 ++- 4 files changed, 51 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/asm-prototypes.h +4 -0
@@ -167,4 +167,8 @@ void kvmhv_load_guest_pmu(struct kvm_vcpu *vcpu);int __kvmhv_vcpu_entry_p9 (struct kvm_vcpu *vcpu); +long kvmppc_h_set_dabr (struct kvm_vcpu *vcpu,unsigned long dabr); +long kvmppc_h_set_xdabr (struct kvm_vcpu *vcpu,unsigned long dabr, +unsigned long dabrx); +#endif /* _ASM_POWERPC_ASM_PROTOTYPES_H */
arch/powerpc/kvm/book3s_hv.c +43 -0
73 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_rmhandlers.S +2 -0
@@ -2466,6 +2466,7 @@ hcall_real_table: hcall_real_table_end: _GLOBAL(kvmppc_h_set_xdabr) +EXPORT_SYMBOL_GPL(kvmppc_h_set_xdabr) andi. r0, r5, DABRX_USER | DABRX_KERNEL beq 6f li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
@@ -2475,6 +2476,7 @@ _GLOBAL(kvmppc_h_set_xdabr) blr _GLOBAL(kvmppc_h_set_dabr) +EXPORT_SYMBOL_GPL(kvmppc_h_set_dabr) li r5, DABRX_USER | DABRX_KERNEL 3: BEGIN_FTR_SECTION
arch/powerpc/kvm/book3s_xics.c +2 -1
@@ -1390,7 +1390,8 @@ static int kvmppc_xics_create(struct kvm_device *dev, u32 type) }#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE -if (cpu_has_feature (CPU_FTR_ARCH_206 )) { +if (cpu_has_feature (CPU_FTR_ARCH_206 ) && +cpu_has_feature (CPU_FTR_HVMODE )) {/* Enable real mode support */ xics->real_mode =ENABLE_REALMODE ; xics->real_mode_dbg =DEBUG_REALMODE ;
Signature · 1 line
2.7.4
PATCH v3 21/33 KVM: PPC: Book3S HV: Framework to handle HV Emulation Assist Interrupt
In reply to Paul MackerrasFrom: Suraj Jitindar Singh <[email protected]>
A HEAI (hypervisor emulation assistance interrupt) occurs when a hypervisor resource or instruction is used in a privileged but non-hypervisor state and the LPCR_EVIRT bit is set in LPCR. When this occurs bit 45 is set in HSRR1. Detect the occurrence of this, and if userspace has enabled the nested virtualization capability on the VM, then call the code to handle it accordingly.
With LPCR[EVIRT] set, we also get HEAI (without bit 45 set) for mfspr or mtspr to unimplemented SPR numbers. For these accesses, we emulate the EVIRT=0 behaviour, which is to make the access a no-op for privileged software unless it is accessing SPR 0, 4, 5 or 6. Problem-state accesses and accesses to SPR 0, 4, 5 or 6 generate an illegal-instruction type program interrupt.
--- arch/powerpc/include/asm/kvm_book3s.h | 2 + arch/powerpc/include/asm/reg.h | 1 + arch/powerpc/kvm/book3s_hv.c | 87 ++++++++++++++++++++++------------- arch/powerpc/kvm/book3s_hv_nested.c | 55 ++++++++++++++++++++++ 4 files changed, 113 insertions(+), 32 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +2 -0
@@ -287,6 +287,8 @@ void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr);void kvmhv_restore_hv_return_state (struct kvm_vcpu *vcpu,struct hv_guest_state *hr);long int kvmhv_nested_page_fault (struct kvm_vcpu *vcpu); +int kvmhv_emulate_priv (struct kvm_run *run,struct kvm_vcpu *vcpu, +unsigned int instr);void kvmppc_giveup_fac (struct kvm_vcpu *vcpu,ulong fac);
arch/powerpc/include/asm/reg.h +1 -0
@@ -456,6 +456,7 @@#define LPCR_HVICE ASM_CONST(0x0000000000000002)/* P9: HV interrupt enable */ #define LPCR_HDICE ASM_CONST(0x0000000000000001)/* Hyp Decr enable (HV,PR,EE) */ #define LPCR_UPRT ASM_CONST(0x0000000000400000)/* Use Process Table (ISA 3) */ +#define LPCR_EVIRT ASM_CONST(0x0000000000200000)/* Enhanced Virtualisation */ #define LPCR_HR ASM_CONST(0x0000000000100000)#ifndef SPRN_LPID #define SPRN_LPID 0x13F/* Logical Partition Identifier */
arch/powerpc/kvm/book3s_hv.c +55 -32
117 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_nested.c +55 -0
64 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH v3 21/33] KVM: PPC: Book3S HV: Framework to handle HV Emulation Assist Interrupt
Quoted 16 lines · Paul Mackerras
From: Suraj Jitindar Singh <[email protected]>
A HEAI (hypervisor emulation assistance interrupt) occurs when a hypervisor resource or instruction is used in a privileged but non-hypervisor state and the
LPCR_EVIRTbit is set in LPCR. When this occurs bit 45 is set in HSRR1. Detect the occurrence of this, and if userspace has enabled the nested virtualization capability on the VM, then call the code to handle it accordingly.With LPCR[EVIRT] set, we also get HEAI (without bit 45 set) for mfspr or mtspr to unimplemented SPR numbers. For these accesses, we emulate the EVIRT=0 behaviour, which is to make the access a no-op for privileged software unless it is accessing SPR 0, 4, 5 or 6. Problem-state accesses and accesses to SPR 0, 4, 5 or 6 generate an illegal-instruction type program interrupt.
Quoted 3 lines
Reviewed-by David Gibson <[email protected]>Signed-off-by Suraj Jitindar Singh <[email protected]>Signed-off-by Paul Mackerras <[email protected]>
Do we still need this if we're moving to paravirt tlbie?
Quoted 227 lines
--- arch/powerpc/include/asm/kvm_book3s.h | 2 + arch/powerpc/include/asm/reg.h | 1 + arch/powerpc/kvm/book3s_hv.c | 87 ++++++++++++++++++++++------------- arch/powerpc/kvm/book3s_hv_nested.c | 55 ++++++++++++++++++++++ 4 files changed, 113 insertions(+), 32 deletions(-)arch/powerpc/include/asm/kvm_book3s.h +2
-0@@ -287,6 +287,8 @@ void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr);void kvmhv_restore_hv_return_state (struct kvm_vcpu *vcpu,struct hv_guest_state *hr);long int kvmhv_nested_page_fault (struct kvm_vcpu *vcpu); +int kvmhv_emulate_priv (struct kvm_run *run,struct kvm_vcpu *vcpu, +unsigned int instr);void kvmppc_giveup_fac (struct kvm_vcpu *vcpu,ulong fac);arch/powerpc/include/asm/reg.h +1
-0@@ -456,6 +456,7 @@#define LPCR_HVICE ASM_CONST(0x0000000000000002)/* P9: HV interrupt enable */ #define LPCR_HDICE ASM_CONST(0x0000000000000001)/* Hyp Decr enable (HV,PR,EE) */ #define LPCR_UPRT ASM_CONST(0x0000000000400000)/* Use Process Table (ISA 3) */ +#define LPCR_EVIRT ASM_CONST(0x0000000000200000)/* Enhanced Virtualisation */ #define LPCR_HR ASM_CONST(0x0000000000100000)#ifndef SPRN_LPID #define SPRN_LPID 0x13F/* Logical Partition Identifier */ arch/powerpc/kvm/book3s_hv.c +55
-32@@ -1027,30 +1027,6 @@ static int kvmppc_hcall_impl_hv(unsigned long cmd)return kvmppc_hcall_impl_hv_realmode (cmd); } -static int kvmppc_emulate_debug_inst (struct kvm_run *run, -struct kvm_vcpu *vcpu) -{ -u32 last_inst; - -if (kvmppc_get_last_inst (vcpu,INST_GENERIC , &last_inst) != -EMULATE_DONE ) { -/* -* Fetch failed, so return to guest and -* try executing it again. -*/ -return RESUME_GUEST ; - } - -if (last_inst ==KVMPPC_INST_SW_BREAKPOINT ) { - run->exit_reason =KVM_EXIT_DEBUG ; - run->debug.arch.address =kvmppc_get_pc (vcpu); -return RESUME_HOST ; - }else { -kvmppc_core_queue_program (vcpu,SRR1_PROGILL ); -return RESUME_GUEST ; - } -} -static void do_nothing (void *x) { }@@ -1144,6 +1120,23 @@ static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)return RESUME_GUEST ; } +static int kvmhv_emulate_unknown_spr (struct kvm_vcpu *vcpu,u32 instr) +{ +u32 spr =get_sprn (instr); + +/* +* In privileged state, access to unimplemented SPRs is a no-op +* except for SPR 0, 4, 5 and 6. All other accesses get turned +* into illegal-instruction program interrupts. +*/ +if ((vcpu->arch.shregs.msr &MSR_PR ) || + spr ==0 || (4 <= spr && spr <=6 )) +return EMULATE_FAIL ; + +kvmppc_set_pc (vcpu,kvmppc_get_pc (vcpu) +4 ); +return RESUME_GUEST ; +} +static int kvmppc_handle_exit_hv (struct kvm_run *run,struct kvm_vcpu *vcpu,struct task_struct *tsk) {@@ -1260,19 +1253,49 @@ static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,* to the guest. If guest debug is enabled, we need to check * whether the instruction is a software breakpoint instruction. * Accordingly return to Guest or Host. +* With LPCR[EVIRT] set, we also get these for accesses to +* unknown SPRs and for guests executing hypervisor privileged +* instructions. */ case BOOK3S_INTERRUPT_H_EMUL_ASSIST : -if (vcpu->arch.emul_inst !=KVM_INST_FETCH_FAILED ) - vcpu->arch.last_inst =kvmppc_need_byteswap (vcpu) ? -swab32 (vcpu->arch.emul_inst) : - vcpu->arch.emul_inst; -if (vcpu->guest_debug &KVM_GUESTDBG_USE_SW_BP ) { - r =kvmppc_emulate_debug_inst (run, vcpu); + { +u32 instr = vcpu->arch.emul_inst; +unsigned long srr1_bit =SRR1_PROGILL ; + + vcpu->arch.last_inst =kvmppc_need_byteswap (vcpu) ? +swab32 (instr) : instr; + + r =EMULATE_FAIL ; +if (vcpu->arch.shregs.msr &SRR1_PROGPRIV ) { +/* +* Tried to execute hypervisor privileged instruction +* or mtspr/mfspr on a hypervisor privileged SPR while +* MSR(HV | PR) == 0b00 -> Privileged but !HV state +*/ + srr1_bit =SRR1_PROGPRIV ; +if (vcpu->kvm->arch.nested_enable) + r =kvmhv_emulate_priv (run, vcpu, instr); }else { -kvmppc_core_queue_program (vcpu,SRR1_PROGILL ); +/* Illegal instruction or unknown SPR access */ +if (instr ==KVMPPC_INST_SW_BREAKPOINT && + (vcpu->guest_debug &KVM_GUESTDBG_USE_SW_BP )) { + run->exit_reason =KVM_EXIT_DEBUG ; + run->debug.arch.address =kvmppc_get_pc (vcpu); + r =RESUME_HOST ; +break ; + } +if ((instr &0xfc0006fe ) ==PPC_INST_MFSPR ) +/* mfspr or mtspr to unknown SPR, may be noop */ + r =kvmhv_emulate_unknown_spr (vcpu, instr); + } +if (r ==EMULATE_FAIL ) { +pr_debug ("KVM: Couldn't emulate instruction 0x%.8x\n" , + instr); +kvmppc_core_queue_program (vcpu, srr1_bit); r =RESUME_GUEST ; }break ; + }/* * This occurs if the guest (kernel or userspace), does something that * is prohibited by HFSCR. @@ -4612,7 +4635,7 @@ static int kvmppc_core_init_vm_hv(struct kvm *kvm)*/ if (cpu_has_feature (CPU_FTR_ARCH_300 )) { lpcr &= ~LPCR_VPM0 ; - lpcr |=LPCR_HVICE |LPCR_HEIC ; + lpcr |=LPCR_HVICE |LPCR_HEIC |LPCR_EVIRT ;/* * If xive is enabled, we route 0x500 interrupts directly arch/powerpc/kvm/book3s_hv_nested.c +55
-0@@ -15,6 +15,7 @@#include <asm/mmu.h> #include <asm/pgtable.h> #include <asm/pgalloc.h> +#include <asm/disassemble.h> static struct patb_entry *pseries_partition_tb;@@ -515,3 +516,57 @@ long kvmhv_nested_page_fault(struct kvm_vcpu *vcpu) {return RESUME_HOST ; } + +static int kvmhv_emulate_priv_mtspr (struct kvm_run *run,struct kvm_vcpu *vcpu, +unsigned int instr) +{ +return EMULATE_FAIL ; +} + +static int kvmhv_emulate_priv_mfspr (struct kvm_run *run,struct kvm_vcpu *vcpu, +unsigned int instr) +{ +return EMULATE_FAIL ; +} + +static int kvmhv_emulate_priv_op_31 (struct kvm_run *run,struct kvm_vcpu *vcpu, +unsigned int instr) +{ +return EMULATE_FAIL ; +} + +static int kvmhv_emulate_priv_op (struct kvm_run *run,struct kvm_vcpu *vcpu, +unsigned int instr) +{ +return EMULATE_FAIL ; +} + +int kvmhv_emulate_priv (struct kvm_run *run,struct kvm_vcpu *vcpu, +unsigned int instr) +{ +int rc =EMULATE_FAIL ; + +switch (get_op (instr)) { +case 31 : +switch (get_xop (instr)) { +case OP_31_XOP_MTSPR : + rc =kvmhv_emulate_priv_mtspr (run, vcpu, instr); +break ; +case OP_31_XOP_MFSPR : + rc =kvmhv_emulate_priv_mfspr (run, vcpu, instr); +break ; +default : + rc =kvmhv_emulate_priv_op_31 (run, vcpu, instr); +break ; + } + +if (rc ==EMULATE_DONE ) +kvmppc_set_pc (vcpu,kvmppc_get_pc (vcpu) +4 ); +break ; +default : + rc =kvmhv_emulate_priv_op (run, vcpu, instr); +break ; + } + +return rc; +}
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v3 22/33 KVM: PPC: Book3S HV: Handle page fault for a nested guest
In reply to Paul MackerrasFrom: Suraj Jitindar Singh <[email protected]>
Consider a normal (L1) guest running under the main hypervisor (L0), and then a nested guest (L2) running under the L1 guest which is acting as a nested hypervisor. L0 has page tables to map the address space for L1 providing the translation from L1 real address -> L0 real address;
L1 | | (L1 -> L0) | ----> L0
There are also page tables in L1 used to map the address space for L2 providing the translation from L2 real address -> L1 read address. Since the hardware can only walk a single level of page table, we need to maintain in L0 a "shadow_pgtable" for L2 which provides the translation from L2 real address -> L0 real address. Which looks like;
L2 L2 | | | (L2 -> L1) | | | ----> L1 | (L2 -> L0) | | | (L1 -> L0) | | | ----> L0 --------> L0
When a page fault occurs while running a nested (L2) guest we need to insert a pte into this "shadow_pgtable" for the L2 -> L0 mapping. To do this we need to:
1. Walk the pgtable in L1 memory to find the L2 -> L1 mapping, and provide a page fault to L1 if this mapping doesn't exist.
2. Use our L1 -> L0 pgtable to convert this L1 address to an L0 address, or try to insert a pte for that mapping if it doesn't exist.
3. Now we have a L2 -> L0 mapping, insert this into our shadow_pgtable
Once this mapping exists we can take rc faults when hardware is unable to automatically set the reference and change bits in the pte. On these we need to:
1. Check the rc bits on the L2 -> L1 pte match, and otherwise reflect the fault down to L1.
2. Set the rc bits in the L1 -> L0 pte which corresponds to the same host page.
3. Set the rc bits in the L2 -> L0 pte.
As we reuse a large number of functions in book3s_64_mmu_radix.c for this we also needed to refactor a number of these functions to take an lpid parameter so that the correct lpid is used for tlb invalidations. The functionality however has remained the same.
--- .../powerpc/include/asm/book3s/64/tlbflush-radix.h | 1 + arch/powerpc/include/asm/kvm_book3s.h | 19 ++ arch/powerpc/include/asm/kvm_book3s_64.h | 4 + arch/powerpc/include/asm/kvm_host.h | 2 + arch/powerpc/kvm/book3s_64_mmu_radix.c | 196 +++++++----- arch/powerpc/kvm/book3s_hv_nested.c | 334 ++++++++++++++++++++- arch/powerpc/mm/tlb-radix.c | 9 + 7 files changed, 478 insertions(+), 87 deletions(-)
arch/powerpc/include/asm/book3s/64/tlbflush-radix.h +1 -0
@@ -53,6 +53,7 @@ extern void radix__flush_tlb_lpid_page(unsigned int lpid,unsigned long addr, unsigned long page_size);extern void radix__flush_pwc_lpid (unsigned int lpid); +extern void radix__flush_tlb_lpid (unsigned int lpid);extern void radix__local_flush_tlb_lpid (unsigned int lpid);extern void radix__local_flush_tlb_lpid_guest (unsigned int lpid);
arch/powerpc/include/asm/kvm_book3s.h +19 -0
@@ -188,17 +188,34 @@ extern int kvmppc_book3s_hcall_implemented(struct kvm *kvm, unsigned long hc);extern int kvmppc_book3s_radix_page_fault (struct kvm_run *run,struct kvm_vcpu *vcpu,unsigned long ea,unsigned long dsisr); +extern int kvmppc_mmu_walk_radix_tree (struct kvm_vcpu *vcpu,gva_t eaddr, +struct kvmppc_pte *gpte,u64 root, +u64 *pte_ret_p);extern int kvmppc_mmu_radix_translate_table (struct kvm_vcpu *vcpu,gva_t eaddr,struct kvmppc_pte *gpte,u64 table,int table_index,u64 *pte_ret_p);extern int kvmppc_mmu_radix_xlate (struct kvm_vcpu *vcpu,gva_t eaddr,struct kvmppc_pte *gpte,bool data,bool iswrite); +extern bool kvmppc_hv_handle_set_rc (struct kvm *kvm,pgd_t *pgtable, +bool writing,unsigned long gpa, +unsigned int lpid); +extern int kvmppc_book3s_instantiate_page (struct kvm_vcpu *vcpu, +unsigned long gpa, +struct kvm_memory_slot *memslot, +bool writing,bool kvm_ro, +pte_t *inserted_pte,unsigned int *levelp);extern int kvmppc_init_vm_radix (struct kvm *kvm);extern void kvmppc_free_radix (struct kvm *kvm); +extern void kvmppc_free_pgtable_radix (struct kvm *kvm,pgd_t *pgd, +unsigned int lpid);extern int kvmppc_radix_init (void );extern void kvmppc_radix_exit (void );extern int kvm_unmap_radix (struct kvm *kvm,struct kvm_memory_slot *memslot,unsigned long gfn); +extern void kvmppc_unmap_pte (struct kvm *kvm,pte_t *pte, +unsigned long gpa,unsigned int shift, +struct kvm_memory_slot *memslot, +unsigned int lpid);extern int kvm_age_radix (struct kvm *kvm,struct kvm_memory_slot *memslot,unsigned long gfn); extern int kvm_test_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
@@ -289,6 +306,8 @@ void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,long int kvmhv_nested_page_fault (struct kvm_vcpu *vcpu);int kvmhv_emulate_priv (struct kvm_run *run,struct kvm_vcpu *vcpu,unsigned int instr); +int kvmhv_handle_nested_trap (struct kvm_run *run,struct kvm_vcpu *vcpu, +struct task_struct *tsk);void kvmppc_giveup_fac (struct kvm_vcpu *vcpu,ulong fac);
arch/powerpc/include/asm/kvm_book3s_64.h +4 -0
@@ -549,6 +549,10 @@ static inline void copy_to_checkpoint(struct kvm_vcpu *vcpu) }#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ +extern int kvmppc_create_pte (struct kvm *kvm,pgd_t *pgtable,pte_t pte, +unsigned long gpa,unsigned int level, +unsigned long mmu_seq,unsigned int lpid); +#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */ #endif /* __ASM_KVM_BOOK3S_64_H__ */
arch/powerpc/include/asm/kvm_host.h +2 -0
@@ -367,7 +367,9 @@ struct kvmppc_pte { bool may_write : 1; bool may_execute : 1;unsigned long wimg; +unsigned long rc;u8 page_size;/* MMU_PAGE_xxx */ +u16 page_shift; };struct kvmppc_mmu {
arch/powerpc/kvm/book3s_64_mmu_radix.c +113 -83
422 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_nested.c +330 -4
368 lines not on this page · raw message
arch/powerpc/mm/tlb-radix.c +9 -0
@@ -833,6 +833,15 @@ EXPORT_SYMBOL_GPL(radix__flush_pwc_lpid);/* * Flush partition scoped translations from LPID (=LPIDR) */ +void radix__flush_tlb_lpid (unsigned int lpid) +{ +_tlbie_lpid (lpid,RIC_FLUSH_ALL ); +} +EXPORT_SYMBOL_GPL (radix__flush_tlb_lpid); + +/* +* Flush partition scoped translations from LPID (=LPIDR) +*/ void radix__local_flush_tlb_lpid (unsigned int lpid) {_tlbiel_lpid (lpid,RIC_FLUSH_ALL );
Signature · 1 line
2.7.4
Re: [PATCH v3 22/33] KVM: PPC: Book3S HV: Handle page fault for a nested guest
Quoted 49 lines · Paul Mackerras
From: Suraj Jitindar Singh <[email protected]>
Consider a normal (L1) guest running under the main hypervisor (L0), and then a nested guest (L2) running under the L1 guest which is acting as a nested hypervisor. L0 has page tables to map the address space for L1 providing the translation from L1 real address -> L0 real address;
L1 | | (L1 -> L0) | ----> L0There are also page tables in L1 used to map the address space for L2 providing the translation from L2 real address -> L1 read address. Since the hardware can only walk a single level of page table, we need to maintain in L0 a "
shadow_pgtable" for L2 which provides the translation from L2 real address -> L0 real address. Which looks like;L2 L2 | | | (L2 -> L1) | | | ----> L1 | (L2 -> L0) | | | (L1 -> L0) | | | ----> L0 --------> L0When a page fault occurs while running a nested (L2) guest we need to insert a pte into this "
shadow_pgtable" for the L2 -> L0 mapping. To do this we need to:1. Walk the pgtable in L1 memory to find the L2 -> L1 mapping, and provide a page fault to L1 if this mapping doesn't exist.
2. Use our L1 -> L0 pgtable to convert this L1 address to an L0 address, or try to insert a pte for that mapping if it doesn't exist.
3. Now we have a L2 -> L0 mapping, insert this into ourshadow_pgtableOnce this mapping exists we can take rc faults when hardware is unable to automatically set the reference and change bits in the pte. On these we need to:
1. Check the rc bits on the L2 -> L1 pte match, and otherwise reflect the fault down to L1.
2. Set the rc bits in the L1 -> L0 pte which corresponds to the same host page.
3. Set the rc bits in the L2 -> L0 pte.
Quoted 7 lines
As we reuse a large number of functions in
book3s_64_mmu_radix.cfor this we also needed to refactor a number of these functions to take an lpid parameter so that the correct lpid is used for tlb invalidations. The functionality however has remained the same.Signed-off-by Suraj Jitindar Singh <[email protected]>Signed-off-by Paul Mackerras <[email protected]>
Some comments below, but no showstoppers, so,
Quoted 90 lines
--- .../powerpc/include/asm/book3s/64/tlbflush-radix.h | 1 + arch/powerpc/include/asm/kvm_book3s.h | 19 ++ arch/powerpc/include/asm/kvm_book3s_64.h | 4 + arch/powerpc/include/asm/kvm_host.h | 2 + arch/powerpc/kvm/book3s_64_mmu_radix.c | 196 +++++++----- arch/powerpc/kvm/book3s_hv_nested.c | 334 ++++++++++++++++++++- arch/powerpc/mm/tlb-radix.c | 9 + 7 files changed, 478 insertions(+), 87 deletions(-)arch/powerpc/include/asm/book3s/64/tlbflush-radix.h +1
-0@@ -53,6 +53,7 @@ extern void radix__flush_tlb_lpid_page(unsigned int lpid,unsigned long addr, unsigned long page_size);extern void radix__flush_pwc_lpid (unsigned int lpid); +extern void radix__flush_tlb_lpid (unsigned int lpid);extern void radix__local_flush_tlb_lpid (unsigned int lpid);extern void radix__local_flush_tlb_lpid_guest (unsigned int lpid);arch/powerpc/include/asm/kvm_book3s.h +19
-0@@ -188,17 +188,34 @@ extern int kvmppc_book3s_hcall_implemented(struct kvm *kvm, unsigned long hc);extern int kvmppc_book3s_radix_page_fault (struct kvm_run *run,struct kvm_vcpu *vcpu,unsigned long ea,unsigned long dsisr); +extern int kvmppc_mmu_walk_radix_tree (struct kvm_vcpu *vcpu,gva_t eaddr, +struct kvmppc_pte *gpte,u64 root, +u64 *pte_ret_p);extern int kvmppc_mmu_radix_translate_table (struct kvm_vcpu *vcpu,gva_t eaddr,struct kvmppc_pte *gpte,u64 table,int table_index,u64 *pte_ret_p);extern int kvmppc_mmu_radix_xlate (struct kvm_vcpu *vcpu,gva_t eaddr,struct kvmppc_pte *gpte,bool data,bool iswrite); +extern bool kvmppc_hv_handle_set_rc (struct kvm *kvm,pgd_t *pgtable, +bool writing,unsigned long gpa, +unsigned int lpid); +extern int kvmppc_book3s_instantiate_page (struct kvm_vcpu *vcpu, +unsigned long gpa, +struct kvm_memory_slot *memslot, +bool writing,bool kvm_ro, +pte_t *inserted_pte,unsigned int *levelp);extern int kvmppc_init_vm_radix (struct kvm *kvm);extern void kvmppc_free_radix (struct kvm *kvm); +extern void kvmppc_free_pgtable_radix (struct kvm *kvm,pgd_t *pgd, +unsigned int lpid);extern int kvmppc_radix_init (void );extern void kvmppc_radix_exit (void );extern int kvm_unmap_radix (struct kvm *kvm,struct kvm_memory_slot *memslot,unsigned long gfn); +extern void kvmppc_unmap_pte (struct kvm *kvm,pte_t *pte, +unsigned long gpa,unsigned int shift, +struct kvm_memory_slot *memslot, +unsigned int lpid);extern int kvm_age_radix (struct kvm *kvm,struct kvm_memory_slot *memslot,unsigned long gfn); extern int kvm_test_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,@@ -289,6 +306,8 @@ void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,long int kvmhv_nested_page_fault (struct kvm_vcpu *vcpu);int kvmhv_emulate_priv (struct kvm_run *run,struct kvm_vcpu *vcpu,unsigned int instr); +int kvmhv_handle_nested_trap (struct kvm_run *run,struct kvm_vcpu *vcpu, +struct task_struct *tsk);void kvmppc_giveup_fac (struct kvm_vcpu *vcpu,ulong fac);arch/powerpc/include/asm/kvm_book3s_64.h +4
-0@@ -549,6 +549,10 @@ static inline void copy_to_checkpoint(struct kvm_vcpu *vcpu) }#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ +extern int kvmppc_create_pte (struct kvm *kvm,pgd_t *pgtable,pte_t pte, +unsigned long gpa,unsigned int level, +unsigned long mmu_seq,unsigned int lpid); +#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */ #endif /* __ASM_KVM_BOOK3S_64_H__ */ arch/powerpc/include/asm/kvm_host.h +0
-0
arch/powerpc/include/asm/kvm_host.h struct kvmppc_pte { · line 372
Quoted 6 lines
arch/powerpc/include/asm/kvm_host.h +2
-0@@ -367,7 +367,9 @@ struct kvmppc_pte { bool may_write : 1; bool may_execute : 1;unsigned long wimg; +unsigned long rc;u8 page_size;/* MMU_PAGE_xxx */ +u16 page_shift;
It's a bit ugly that this has both page_size and page_shift, which is redundant information AFAICT. Also, why does page_shift need to be u16 - given that 2^255 bytes is much more than our supported address space, let alone a plausible page size.
Quoted 42 lines
arch/powerpc/include/asm/kvm_host.h +0
-0};struct kvmppc_mmu {arch/powerpc/kvm/book3s_64_mmu_radix.c +4
-21@@ -29,43 +29,16 @@*/ static int p9_supported_radix_bits[4 ] = {5 ,9 ,9 ,13 }; -/* -* Used to walk a partition or process table radix tree in guest memory -* Note: We exploit the fact that a partition table and a process -* table have the same layout, a partition-scoped page table and a -* process-scoped page table have the same layout, and the 2nd -* doubleword of a partition table entry has the same layout as -* the PTCR register. -*/ -int kvmppc_mmu_radix_translate_table (struct kvm_vcpu *vcpu,gva_t eaddr, -struct kvmppc_pte *gpte,u64 table, -int table_index,u64 *pte_ret_p) +int kvmppc_mmu_walk_radix_tree (struct kvm_vcpu *vcpu,gva_t eaddr, +struct kvmppc_pte *gpte,u64 root, +u64 *pte_ret_p) {struct kvm *kvm = vcpu->kvm;int ret, level, ps; -unsigned long ptbl, root; -unsigned long rts, bits, offset; -unsigned long size, index; -struct prtb_entry entry; +unsigned long rts, bits, offset, index;u64 pte, base, gpa;__be64 rpte; -if ((table &PRTS_MASK ) >24 ) -return -EINVAL ; - size =1ul << ((table &PRTS_MASK ) +12 ); - -/* Is the table big enough to contain this entry? */ -if ((table_index *sizeof (entry)) >= size)
arch/powerpc/kvm/book3s_64_mmu_radix.c line 68
Quoted 10 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +0
-10-return -EINVAL ; - -/* Read the table to find the root of the radix tree */ - ptbl = (table &PRTB_MASK ) + (table_index *sizeof (entry)); - ret =kvm_read_guest (kvm, ptbl, &entry,sizeof (entry)); -if (ret) -return ret; - -/* Root is stored in the first double word */ - root =be64_to_cpu (entry.prtb0);
This refactoring somewhat obscures the changes directly relevant to the nested guest handling. Ideally it would be nice to fold some of this into the earlier reworkings.
Quoted 199 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +67
-19rts = ((root &RTS1_MASK ) >> (RTS1_SHIFT -3 )) | ((root &RTS2_MASK ) >>RTS2_SHIFT ); bits = root &RPDS_MASK ;@@ -79,6 +52,7 @@ int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr,/* Walk each level of the radix tree */ for (level =3 ; level >=0 ; --level) { +u64 addr;/* Check a valid size */ if (level && bits != p9_supported_radix_bits[level])return -EINVAL ;@@ -90,10 +64,13 @@ int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr,if (base & ((1UL << (bits +3 )) -1 ))return -EINVAL ;/* Read the entry from guest memory */ - ret =kvm_read_guest (kvm, base + (index *sizeof (rpte)), - &rpte,sizeof (rpte)); -if (ret) + addr = base + (index *sizeof (rpte)); + ret =kvm_read_guest (kvm, addr, &rpte,sizeof (rpte)); +if (ret) { +if (pte_ret_p) + *pte_ret_p = addr;return ret; + } pte =__be64_to_cpu (rpte);if (!(pte & _PAGE_PRESENT))return -ENOENT ;@@ -119,6 +96,7 @@ int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr,if (offset == mmu_psize_defs[ps].shift)break ; gpte->page_size = ps; + gpte->page_shift = offset; gpte->eaddr = eaddr; gpte->raddr = gpa;@@ -128,12 +106,51 @@ int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr, gpte->may_write = !!(pte & _PAGE_WRITE); gpte->may_execute = !!(pte & _PAGE_EXEC); + gpte->rc = pte & (_PAGE_ACCESSED | _PAGE_DIRTY); +if (pte_ret_p) *pte_ret_p = pte;return 0 ; } +/* +* Used to walk a partition or process table radix tree in guest memory +* Note: We exploit the fact that a partition table and a process +* table have the same layout, a partition-scoped page table and a +* process-scoped page table have the same layout, and the 2nd +* doubleword of a partition table entry has the same layout as +* the PTCR register. +*/ +int kvmppc_mmu_radix_translate_table (struct kvm_vcpu *vcpu,gva_t eaddr, +struct kvmppc_pte *gpte,u64 table, +int table_index,u64 *pte_ret_p) +{ +struct kvm *kvm = vcpu->kvm; +int ret; +unsigned long size, ptbl, root; +struct prtb_entry entry; + +if ((table &PRTS_MASK ) >24 ) +return -EINVAL ; + size =1ul << ((table &PRTS_MASK ) +12 ); + +/* Is the table big enough to contain this entry? */ +if ((table_index *sizeof (entry)) >= size) +return -EINVAL ; + +/* Read the table to find the root of the radix tree */ + ptbl = (table &PRTB_MASK ) + (table_index *sizeof (entry)); + ret =kvm_read_guest (kvm, ptbl, &entry,sizeof (entry)); +if (ret) +return ret; + +/* Root is stored in the first double word */ + root =be64_to_cpu (entry.prtb0); + +return kvmppc_mmu_walk_radix_tree (vcpu, eaddr, gpte, root, pte_ret_p); +} +int kvmppc_mmu_radix_xlate (struct kvm_vcpu *vcpu,gva_t eaddr,struct kvmppc_pte *gpte,bool data,bool iswrite) {@@ -181,7 +198,7 @@ int kvmppc_mmu_radix_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, }static void kvmppc_radix_tlbie_page (struct kvm *kvm,unsigned long addr, -unsigned int pshift) +unsigned int pshift,unsigned int lpid) {unsigned long psize =PAGE_SIZE ;@@ -189,12 +206,12 @@ static void kvmppc_radix_tlbie_page(struct kvm *kvm, unsigned long addr, psize =1UL << pshift; addr &= ~(psize -1 ); -radix__flush_tlb_lpid_page (kvm->arch.lpid, addr, psize); +radix__flush_tlb_lpid_page (lpid, addr, psize); } -static void kvmppc_radix_flush_pwc (struct kvm *kvm) +static void kvmppc_radix_flush_pwc (struct kvm *kvm,unsigned int lpid) { -radix__flush_pwc_lpid (kvm->arch.lpid); +radix__flush_pwc_lpid (lpid); } static unsigned long kvmppc_radix_update_pte(struct kvm *kvm, pte_t *ptep,@@ -239,16 +256,17 @@ static void kvmppc_pmd_free(pmd_t *pmdp)kmem_cache_free (kvm_pmd_cache, pmdp); } -static void kvmppc_unmap_pte (struct kvm *kvm,pte_t *pte, -unsigned long gpa,unsigned int shift, -struct kvm_memory_slot *memslot) +void kvmppc_unmap_pte (struct kvm *kvm,pte_t *pte, +unsigned long gpa,unsigned int shift, +struct kvm_memory_slot *memslot, +unsigned int lpid) {unsigned long old; old =kvmppc_radix_update_pte (kvm, pte, ~0UL ,0 , gpa, shift); -kvmppc_radix_tlbie_page (kvm, gpa, shift); -if (old & _PAGE_DIRTY) { +kvmppc_radix_tlbie_page (kvm, gpa, shift, lpid); +if ((old & _PAGE_DIRTY) && (lpid == kvm->arch.lpid)) {unsigned long gfn = gpa >>PAGE_SHIFT ;unsigned long page_size =PAGE_SIZE ;@@ -271,7 +289,8 @@ static void kvmppc_unmap_pte(struct kvm *kvm, pte_t *pte,* and emit a warning if encountered, but there may already be data * corruption due to the unexpected mappings. */ -static void kvmppc_unmap_free_pte (struct kvm *kvm,pte_t *pte,bool full) +static void kvmppc_unmap_free_pte (struct kvm *kvm,pte_t *pte,bool full, +unsigned int lpid) {if (full) {memset (pte,0 ,sizeof (long ) <<PTE_INDEX_SIZE );@@ -285,14 +304,15 @@ static void kvmppc_unmap_free_pte(struct kvm *kvm, pte_t *pte, bool full)WARN_ON_ONCE (1 );kvmppc_unmap_pte (kvm, p,pte_pfn (*p) <<PAGE_SHIFT , -PAGE_SHIFT ,NULL ); +PAGE_SHIFT ,NULL , lpid); } }kvmppc_pte_free (pte); } -static void kvmppc_unmap_free_pmd (struct kvm *kvm,pmd_t *pmd,bool full) +static void kvmppc_unmap_free_pmd (struct kvm *kvm,pmd_t *pmd,bool full, +unsigned int lpid) {unsigned long im;pmd_t *p = pmd;@@ -307,20 +327,21 @@ static void kvmppc_unmap_free_pmd(struct kvm *kvm, pmd_t *pmd, bool full)WARN_ON_ONCE (1 );kvmppc_unmap_pte (kvm, (pte_t *)p,pte_pfn (*(pte_t *)p) <<PAGE_SHIFT , -PMD_SHIFT ,NULL ); +PMD_SHIFT ,NULL , lpid); } }else {pte_t *pte; pte =pte_offset_map (p,0 ); -kvmppc_unmap_free_pte (kvm, pte, full); +kvmppc_unmap_free_pte (kvm, pte, full, lpid);pmd_clear (p); } }kvmppc_pmd_free (pmd); } -static void kvmppc_unmap_free_pud (struct kvm *kvm,pud_t *pud) +static void kvmppc_unmap_free_pud (struct kvm *kvm,pud_t *pud, +unsigned int lpid) {unsigned long iu;pud_t *p = pud;@@ -334,36 +355,42 @@ static void kvmppc_unmap_free_pud(struct kvm *kvm, pud_t *pud)pmd_t *pmd; pmd =pmd_offset (p,0 ); -kvmppc_unmap_free_pmd (kvm, pmd, true); +kvmppc_unmap_free_pmd (kvm, pmd, true, lpid);pud_clear (p); } }pud_free (kvm->mm, pud);
arch/powerpc/kvm/book3s_64_mmu_radix.c static void kvmppc_unmap_free_pud(struct kvm *kvm, pud_t *pud) · line 369
Quoted 10 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +2
-3} -void kvmppc_free_radix (struct kvm *kvm) +void kvmppc_free_pgtable_radix (struct kvm *kvm,pgd_t *pgd,unsigned int lpid) {unsigned long ig; -pgd_t *pgd; -if (!kvm->arch.pgtable) +if (!pgd)
Not sure if this test is worth it, since most of the callers have to test for a NULL pgtable for other reasons anyway.
Quoted 75 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +20
-13return ; - pgd = kvm->arch.pgtable;for (ig =0 ; ig <PTRS_PER_PGD ; ++ig, ++pgd) {pud_t *pud;if (!pgd_present (*pgd))continue ; pud =pud_offset (pgd,0 ); -kvmppc_unmap_free_pud (kvm, pud); +kvmppc_unmap_free_pud (kvm, pud, lpid);pgd_clear (pgd); } -pgd_free (kvm->mm, kvm->arch.pgtable); - kvm->arch.pgtable =NULL ; +} + +void kvmppc_free_radix (struct kvm *kvm) +{ +if (kvm->arch.pgtable) { +kvmppc_free_pgtable_radix (kvm, kvm->arch.pgtable, + kvm->arch.lpid); +pgd_free (kvm->mm, kvm->arch.pgtable); + kvm->arch.pgtable =NULL ; + } }static void kvmppc_unmap_free_pmd_entry_table (struct kvm *kvm,pmd_t *pmd, -unsigned long gpa) +unsigned long gpa,unsigned int lpid) {pte_t *pte =pte_offset_kernel (pmd,0 );@@ -373,13 +400,13 @@ static void kvmppc_unmap_free_pmd_entry_table(struct kvm *kvm, pmd_t *pmd,* flushing the PWC again. */ pmd_clear (pmd); -kvmppc_radix_flush_pwc (kvm); +kvmppc_radix_flush_pwc (kvm, lpid); -kvmppc_unmap_free_pte (kvm, pte, false); +kvmppc_unmap_free_pte (kvm, pte, false, lpid); }static void kvmppc_unmap_free_pud_entry_table (struct kvm *kvm,pud_t *pud, -unsigned long gpa) +unsigned long gpa,unsigned int lpid) {pmd_t *pmd =pmd_offset (pud,0 );@@ -389,9 +416,9 @@ static void kvmppc_unmap_free_pud_entry_table(struct kvm *kvm, pud_t *pud,* so can be freed without flushing the PWC again. */ pud_clear (pud); -kvmppc_radix_flush_pwc (kvm); +kvmppc_radix_flush_pwc (kvm, lpid); -kvmppc_unmap_free_pmd (kvm, pmd, false); +kvmppc_unmap_free_pmd (kvm, pmd, false, lpid); }/* @@ -403,9 +430,9 @@ static void kvmppc_unmap_free_pud_entry_table(struct kvm *kvm, pud_t *pud,*/ #define PTE_BITS_MUST_MATCH (~(_PAGE_WRITE | _PAGE_DIRTY | _PAGE_ACCESSED)) -static int kvmppc_create_pte (struct kvm *kvm,pgd_t *pgtable,pte_t pte, -unsigned long gpa,unsigned int level, -unsigned long mmu_seq) +int kvmppc_create_pte (struct kvm *kvm,pgd_t *pgtable,pte_t pte, +unsigned long gpa,unsigned int level, +unsigned long mmu_seq,unsigned int lpid) {pgd_t *pgd;pud_t *pud, *new_pud =NULL ;
arch/powerpc/kvm/book3s_64_mmu_radix.c static int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte, · line 488
Quoted 5 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +1
-1@@ -458,7 +485,7 @@ static int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,WARN_ON_ONCE ((pud_val (*pud) ^pte_val (pte)) &PTE_BITS_MUST_MATCH );kvmppc_radix_update_pte (kvm, (pte_t *)pud, -0 ,pte_val (pte), hgpa,PUD_SHIFT ); +0 ,pte_val (pte), hgpa,PUD_SHIFT );
This is an unconnected whitespace change, AFAICT.
Quoted 224 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +19
-16ret =0 ; goto out_unlock; }@@ -471,7 +498,8 @@ static int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte, goto out_unlock; }/* Valid 1GB page here already, remove it */ -kvmppc_unmap_pte (kvm, (pte_t *)pud, hgpa,PUD_SHIFT ,NULL ); +kvmppc_unmap_pte (kvm, (pte_t *)pud, hgpa,PUD_SHIFT ,NULL , + lpid); }if (level ==2 ) {if (!pud_none (*pud)) {@@ -480,7 +508,7 @@ static int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,* install a large page, so remove and free the page * table page. */ -kvmppc_unmap_free_pud_entry_table (kvm, pud, gpa); +kvmppc_unmap_free_pud_entry_table (kvm, pud, gpa, lpid); }kvmppc_radix_set_pte_at (kvm, gpa, (pte_t *)pud, pte); ret =0 ;@@ -506,7 +534,7 @@ static int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,WARN_ON_ONCE ((pmd_val (*pmd) ^pte_val (pte)) &PTE_BITS_MUST_MATCH );kvmppc_radix_update_pte (kvm,pmdp_ptep (pmd), -0 ,pte_val (pte), lgpa,PMD_SHIFT ); +0 ,pte_val (pte), lgpa,PMD_SHIFT ); ret =0 ; goto out_unlock; }@@ -520,7 +548,8 @@ static int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte, goto out_unlock; }/* Valid 2MB page here already, remove it */ -kvmppc_unmap_pte (kvm,pmdp_ptep (pmd), lgpa,PMD_SHIFT ,NULL ); +kvmppc_unmap_pte (kvm,pmdp_ptep (pmd), lgpa,PMD_SHIFT ,NULL , + lpid); }if (level ==1 ) {if (!pmd_none (*pmd)) {@@ -529,7 +558,7 @@ static int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,* install a large page, so remove and free the page * table page. */ -kvmppc_unmap_free_pmd_entry_table (kvm, pmd, gpa); +kvmppc_unmap_free_pmd_entry_table (kvm, pmd, gpa, lpid); }kvmppc_radix_set_pte_at (kvm, gpa,pmdp_ptep (pmd), pte); ret =0 ;@@ -569,8 +598,8 @@ static int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,return ret; } -static bool kvmppc_hv_handle_set_rc (struct kvm *kvm,pgd_t *pgtable, -bool writing,unsigned long gpa) +bool kvmppc_hv_handle_set_rc (struct kvm *kvm,pgd_t *pgtable,bool writing, +unsigned long gpa,unsigned int lpid) {unsigned long pgflags;unsigned int shift;@@ -597,11 +626,11 @@ static bool kvmppc_hv_handle_set_rc(struct kvm *kvm, pgd_t *pgtable,return false; } -static int kvmppc_book3s_instantiate_page (struct kvm_vcpu *vcpu, -unsigned long gpa, -struct kvm_memory_slot *memslot, -bool writing,bool kvm_ro, -pte_t *inserted_pte,unsigned int *levelp) +int kvmppc_book3s_instantiate_page (struct kvm_vcpu *vcpu, +unsigned long gpa, +struct kvm_memory_slot *memslot, +bool writing,bool kvm_ro, +pte_t *inserted_pte,unsigned int *levelp) {struct kvm *kvm = vcpu->kvm;struct page *page =NULL ;@@ -683,7 +712,7 @@ static int kvmppc_book3s_instantiate_page(struct kvm_vcpu *vcpu,/* Allocate space in the tree and write the PTE */ ret =kvmppc_create_pte (kvm, kvm->arch.pgtable, pte, gpa, level, - mmu_seq); + mmu_seq, kvm->arch.lpid);if (inserted_pte) *inserted_pte = pte;if (levelp)@@ -758,7 +787,7 @@ int kvmppc_book3s_radix_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,if (dsisr &DSISR_SET_RC ) {spin_lock (&kvm->mmu_lock);if (kvmppc_hv_handle_set_rc (kvm, kvm->arch.pgtable, - writing, gpa)) + writing, gpa, kvm->arch.lpid)) dsisr &= ~DSISR_SET_RC ;spin_unlock (&kvm->mmu_lock);@@ -786,7 +815,8 @@ int kvm_unmap_radix(struct kvm *kvm, struct kvm_memory_slot *memslot, ptep =__find_linux_pte (kvm->arch.pgtable, gpa,NULL , &shift);if (ptep &&pte_present (*ptep)) -kvmppc_unmap_pte (kvm, ptep, gpa, shift, memslot); +kvmppc_unmap_pte (kvm, ptep, gpa, shift, memslot, + kvm->arch.lpid);return 0 ; }@@ -841,7 +871,7 @@ static int kvm_radix_test_clear_dirty(struct kvm *kvm, ret =1 << (shift -PAGE_SHIFT );kvmppc_radix_update_pte (kvm, ptep, _PAGE_DIRTY,0 , gpa, shift); -kvmppc_radix_tlbie_page (kvm, gpa, shift); +kvmppc_radix_tlbie_page (kvm, gpa, shift, kvm->arch.lpid); }return ret; }arch/powerpc/kvm/book3s_hv_nested.c +62
-4@@ -12,10 +12,13 @@#include <linux/kvm_host.h> #include <asm/kvm_ppc.h> +#include <asm/kvm_book3s.h> #include <asm/mmu.h> #include <asm/pgtable.h> #include <asm/pgalloc.h> +#include <asm/pte-walk.h> #include <asm/disassemble.h> +#include <asm/reg.h> static struct patb_entry *pseries_partition_tb;@@ -393,10 +396,20 @@ struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid)*/ static void kvmhv_release_nested (struct kvm_nested_guest *gp) { +struct kvm *kvm = gp->l1_host; + +if (gp->shadow_pgtable) { +/* +* No vcpu is using this struct and no call to +* kvmhv_remove_nest_rmap can find this struct, +* so we don't need to hold kvm->mmu_lock. +*/ +kvmppc_free_pgtable_radix (kvm, gp->shadow_pgtable, + gp->shadow_lpid); +pgd_free (kvm->mm, gp->shadow_pgtable); + }kvmhv_set_ptbl_entry (gp->shadow_lpid,0 ,0 );kvmppc_free_lpid (gp->shadow_lpid); -if (gp->shadow_pgtable) -pgd_free (gp->l1_host->mm, gp->shadow_pgtable);kfree (gp); }@@ -453,6 +466,12 @@ void kvmhv_release_all_nested(struct kvm *kvm)/* caller must hold gp->tlb_lock */ void kvmhv_flush_nested (struct kvm_nested_guest *gp) { +struct kvm *kvm = gp->l1_host; + +spin_lock (&kvm->mmu_lock); +kvmppc_free_pgtable_radix (kvm, gp->shadow_pgtable, gp->shadow_lpid); +spin_unlock (&kvm->mmu_lock); +radix__flush_tlb_lpid (gp->shadow_lpid);kvmhv_update_ptbl_cache (gp);if (gp->l1_gr_to_hr ==0 )kvmhv_remove_nested (gp);@@ -512,9 +531,28 @@ void kvmhv_put_nested(struct kvm_nested_guest *gp)kvmhv_release_nested (gp); } -long kvmhv_nested_page_fault (struct kvm_vcpu *vcpu) +static bool kvmhv_invalidate_shadow_pte (struct kvm_vcpu *vcpu, +struct kvm_nested_guest *gp, +long gpa,int *shift_ret) { -return RESUME_HOST ; +struct kvm *kvm = vcpu->kvm; +bool ret = false; +pte_t *ptep; +int shift; + +spin_lock (&kvm->mmu_lock); + ptep =__find_linux_pte (gp->shadow_pgtable, gpa,NULL , &shift); +if (!shift) + shift =PAGE_SHIFT ; +if (ptep &&pte_present (*ptep)) { +kvmppc_unmap_pte (kvm, ptep, gpa, shift,NULL , gp->shadow_lpid); + ret = true; + } +spin_unlock (&kvm->mmu_lock); + +if (shift_ret) + *shift_ret = shift; +return ret; } static int kvmhv_emulate_priv_mtspr(struct kvm_run *run, struct kvm_vcpu *vcpu,@@ -570,3 +608,291 @@ int kvmhv_emulate_priv(struct kvm_run *run, struct kvm_vcpu *vcpu,return rc; } + +/* Used to convert a nested guest real address to a L1 guest real address */ +static int kvmhv_translate_addr_nested (struct kvm_vcpu *vcpu, +struct kvm_nested_guest *gp, +unsigned long n_gpa,unsigned long dsisr, +struct kvmppc_pte *gpte_p) +{ +u64 fault_addr, flags = dsisr &DSISR_ISSTORE ; +int ret; + + ret =kvmppc_mmu_walk_radix_tree (vcpu, n_gpa, gpte_p, gp->l1_gr_to_hr, + &fault_addr); + +if (ret) { +/* We didn't find a pte */ +if (ret == -EINVAL ) { +/* Unsupported mmu config */ + flags |=DSISR_UNSUPP_MMU ; + }else if (ret == -ENOENT ) { +/* No translation found */
arch/powerpc/kvm/book3s_hv_nested.c int kvmhv_emulate_priv(struct kvm_run *run, struct kvm_vcpu *vcpu, · line 640
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_nested.c +10
-0+ flags |=DSISR_NOHPTE ; + }else if (ret == -EFAULT ) { +/* Couldn't access L1 real address */ + flags |=DSISR_PRTABLE_FAULT ; + vcpu->arch.fault_gpa = fault_addr; + }else { +/* Unknown error */ +return ret; + } + goto resume_host;
This is effectively forwarding the fault to L1, yes? In which case a different name might be better than the ambiguous "resume_host".
Quoted 278 lines
arch/powerpc/kvm/book3s_hv_nested.c +258
-0+ }else { +/* We found a pte -> check permissions */ +if (dsisr &DSISR_ISSTORE ) { +/* Can we write? */ +if (!gpte_p->may_write) { + flags |=DSISR_PROTFAULT ; + goto resume_host; + } + }else if (vcpu->arch.trap ==BOOK3S_INTERRUPT_H_INST_STORAGE ) { +/* Can we execute? */ +if (!gpte_p->may_execute) { + flags |=SRR1_ISI_N_OR_G ; + goto resume_host; + } + }else { +/* Can we read? */ +if (!gpte_p->may_read && !gpte_p->may_write) { + flags |=DSISR_PROTFAULT ; + goto resume_host; + } + } + } + +return 0 ; + +resume_host: + vcpu->arch.fault_dsisr = flags; +if (vcpu->arch.trap ==BOOK3S_INTERRUPT_H_INST_STORAGE ) { + vcpu->arch.shregs.msr &= ~0x783f0000ul ; + vcpu->arch.shregs.msr |= flags; + } +return RESUME_HOST ; +} + +static long kvmhv_handle_nested_set_rc (struct kvm_vcpu *vcpu, +struct kvm_nested_guest *gp, +unsigned long n_gpa, +struct kvmppc_pte gpte, +unsigned long dsisr) +{ +struct kvm *kvm = vcpu->kvm; +bool writing = !!(dsisr &DSISR_ISSTORE ); +u64 pgflags; +bool ret; + +/* Are the rc bits set in the L1 partition scoped pte? */ + pgflags = _PAGE_ACCESSED; +if (writing) + pgflags |= _PAGE_DIRTY; +if (pgflags & ~gpte.rc) +return RESUME_HOST ; + +spin_lock (&kvm->mmu_lock); +/* Set the rc bit in the pte of our (L0) pgtable for the L1 guest */ + ret =kvmppc_hv_handle_set_rc (kvm, kvm->arch.pgtable, writing, + gpte.raddr, kvm->arch.lpid); +spin_unlock (&kvm->mmu_lock); +if (!ret) +return -EINVAL ; + +/* Set the rc bit in the pte of the shadow_pgtable for the nest guest */ + ret =kvmppc_hv_handle_set_rc (kvm, gp->shadow_pgtable, writing, n_gpa, + gp->shadow_lpid); +if (!ret) +return -EINVAL ; +return 0 ; +} + +static inline int kvmppc_radix_level_to_shift (int level) +{ +switch (level) { +case 2 : +return PUD_SHIFT ; +case 1 : +return PMD_SHIFT ; +default : +return PAGE_SHIFT ; + } +} + +static inline int kvmppc_radix_shift_to_level (int shift) +{ +if (shift ==PUD_SHIFT ) +return 2 ; +if (shift ==PMD_SHIFT ) +return 1 ; +if (shift ==PAGE_SHIFT ) +return 0 ; +WARN_ON_ONCE (1 ); +return 0 ; +} + +/* called with gp->tlb_lock held */ +static long int __kvmhv_nested_page_fault (struct kvm_vcpu *vcpu, +struct kvm_nested_guest *gp) +{ +struct kvm *kvm = vcpu->kvm; +struct kvm_memory_slot *memslot; +struct kvmppc_pte gpte; +pte_t pte, *pte_p; +unsigned long mmu_seq; +unsigned long dsisr = vcpu->arch.fault_dsisr; +unsigned long ea = vcpu->arch.fault_dar; +unsigned long n_gpa, gpa, gfn, perm =0UL ; +unsigned int shift, l1_shift, level; +bool writing = !!(dsisr &DSISR_ISSTORE ); +bool kvm_ro = false; +long int ret; + +if (!gp->l1_gr_to_hr) { +kvmhv_update_ptbl_cache (gp); +if (!gp->l1_gr_to_hr) +return RESUME_HOST ; + } + +/* Convert the nested guest real address into a L1 guest real address */ + + n_gpa = vcpu->arch.fault_gpa & ~0xF000000000000FFFULL ; +if (!(dsisr &DSISR_PRTABLE_FAULT )) + n_gpa |= ea &0xFFF ; + ret =kvmhv_translate_addr_nested (vcpu, gp, n_gpa, dsisr, &gpte); + +/* +* If the hardware found a translation but we don't now have a usable +* translation in the l1 partition-scoped tree, remove the shadow pte +* and let the guest retry. +*/ +if (ret ==RESUME_HOST && + (dsisr & (DSISR_PROTFAULT |DSISR_BADACCESS |DSISR_NOEXEC_OR_G | +DSISR_BAD_COPYPASTE ))) + goto inval; +if (ret) +return ret; + +/* Failed to set the reference/change bits */ +if (dsisr &DSISR_SET_RC ) { + ret =kvmhv_handle_nested_set_rc (vcpu, gp, n_gpa, gpte, dsisr); +if (ret ==RESUME_HOST ) +return ret; +if (ret) + goto inval; + dsisr &= ~DSISR_SET_RC ; +if (!(dsisr & (DSISR_BAD_FAULT_64S |DSISR_NOHPTE | +DSISR_PROTFAULT ))) +return RESUME_GUEST ; + } + +/* +* We took an HISI or HDSI while we were running a nested guest which +* means we have no partition scoped translation for that. This means +* we need to insert a pte for the mapping into our shadow_pgtable. +*/ + + l1_shift = gpte.page_shift; +if (l1_shift <PAGE_SHIFT ) { +/* We don't support l1 using a page size smaller than our own */ +pr_err ("KVM: L1 guest page shift (%d) less than our own (%d)\n" , + l1_shift,PAGE_SHIFT ); +return -EINVAL ; + } + gpa = gpte.raddr; + gfn = gpa >>PAGE_SHIFT ; + +/* 1. Get the corresponding host memslot */ + + memslot =gfn_to_memslot (kvm, gfn); +if (!memslot || (memslot->flags &KVM_MEMSLOT_INVALID )) { +if (dsisr & (DSISR_PRTABLE_FAULT |DSISR_BADACCESS )) { +/* unusual error -> reflect to the guest as a DSI */ +kvmppc_core_queue_data_storage (vcpu, ea, dsisr); +return RESUME_GUEST ; + } +/* passthrough of emulated MMIO case... */ +pr_err ("emulated MMIO passthrough?\n" ); +return -EINVAL ; + } +if (memslot->flags &KVM_MEM_READONLY ) { +if (writing) { +/* Give the guest a DSI */ +kvmppc_core_queue_data_storage (vcpu, ea, +DSISR_ISSTORE |DSISR_PROTFAULT ); +return RESUME_GUEST ; + } + kvm_ro = true; + } + +/* 2. Find the host pte for this L1 guest real address */ + +/* Used to check for invalidations in progress */ + mmu_seq = kvm->mmu_notifier_seq; +smp_rmb (); + +/* See if can find translation in our partition scoped tables for L1 */ + pte =__pte (0 ); +spin_lock (&kvm->mmu_lock); + pte_p =__find_linux_pte (kvm->arch.pgtable, gpa,NULL , &shift); +if (!shift) + shift =PAGE_SHIFT ; +if (pte_p) + pte = *pte_p; +spin_unlock (&kvm->mmu_lock); + +if (!pte_present (pte) || (writing && !(pte_val (pte) & _PAGE_WRITE))) { +/* No suitable pte found -> try to insert a mapping */ + ret =kvmppc_book3s_instantiate_page (vcpu, gpa, memslot, + writing, kvm_ro, &pte, &level); +if (ret == -EAGAIN ) +return RESUME_GUEST ; +else if (ret) +return ret; + shift =kvmppc_radix_level_to_shift (level); + } + +/* 3. Compute the pte we need to insert for nest_gpa -> host r_addr */ + +/* The permissions is the combination of the host and l1 guest ptes */ + perm |= gpte.may_read ?0UL : _PAGE_READ; + perm |= gpte.may_write ?0UL : _PAGE_WRITE; + perm |= gpte.may_execute ?0UL : _PAGE_EXEC; + pte =__pte (pte_val (pte) & ~perm); + +/* What size pte can we insert? */ +if (shift > l1_shift) { +u64 mask; +unsigned int actual_shift =PAGE_SHIFT ; +if (PMD_SHIFT < l1_shift) + actual_shift =PMD_SHIFT ; + mask = (1UL << shift) - (1UL << actual_shift); + pte =__pte (pte_val (pte) | (gpa & mask)); + shift = actual_shift; + } + level =kvmppc_radix_shift_to_level (shift); + n_gpa &= ~((1UL << shift) -1 ); + +/* 4. Insert the pte into our shadow_pgtable */ + + ret =kvmppc_create_pte (kvm, gp->shadow_pgtable, pte, n_gpa, level, + mmu_seq, gp->shadow_lpid); +if (ret == -EAGAIN ) + ret =RESUME_GUEST ;/* Let the guest try again */ + +return ret; + + inval: +kvmhv_invalidate_shadow_pte (vcpu, gp, n_gpa,NULL ); +return RESUME_GUEST ; +} + +long int kvmhv_nested_page_fault (struct kvm_vcpu *vcpu) +{ +struct kvm_nested_guest *gp = vcpu->arch.nested; +long int ret; + +mutex_lock (&gp->tlb_lock); + ret =__kvmhv_nested_page_fault (vcpu, gp); +mutex_unlock (&gp->tlb_lock); +return ret; +}arch/powerpc/mm/tlb-radix.c +9
-0@@ -833,6 +833,15 @@ EXPORT_SYMBOL_GPL(radix__flush_pwc_lpid);/* * Flush partition scoped translations from LPID (=LPIDR) */ +void radix__flush_tlb_lpid (unsigned int lpid) +{ +_tlbie_lpid (lpid,RIC_FLUSH_ALL ); +} +EXPORT_SYMBOL_GPL (radix__flush_tlb_lpid); + +/* +* Flush partition scoped translations from LPID (=LPIDR) +*/ void radix__local_flush_tlb_lpid (unsigned int lpid) {_tlbiel_lpid (lpid,RIC_FLUSH_ALL );
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH v3 22/33] KVM: PPC: Book3S HV: Handle page fault for a nested guest
Quoted 55 lines · David Gibson
Quoted 54 lines · Paul Mackerras
From: Suraj Jitindar Singh <[email protected]>
Consider a normal (L1) guest running under the main hypervisor (L0), and then a nested guest (L2) running under the L1 guest which is acting as a nested hypervisor. L0 has page tables to map the address space for L1 providing the translation from L1 real address -> L0 real address;
L1 | | (L1 -> L0) | ----> L0There are also page tables in L1 used to map the address space for L2 providing the translation from L2 real address -> L1 read address. Since the hardware can only walk a single level of page table, we need to maintain in L0 a "
shadow_pgtable" for L2 which provides the translation from L2 real address -> L0 real address. Which looks like;L2 L2 | | | (L2 -> L1) | | | ----> L1 | (L2 -> L0) | | | (L1 -> L0) | | | ----> L0 --------> L0When a page fault occurs while running a nested (L2) guest we need to insert a pte into this "
shadow_pgtable" for the L2 -> L0 mapping. To do this we need to:1. Walk the pgtable in L1 memory to find the L2 -> L1 mapping, and provide a page fault to L1 if this mapping doesn't exist.
2. Use our L1 -> L0 pgtable to convert this L1 address to an L0 address, or try to insert a pte for that mapping if it doesn't exist.
3. Now we have a L2 -> L0 mapping, insert this into ourshadow_pgtableOnce this mapping exists we can take rc faults when hardware is unable to automatically set the reference and change bits in the pte. On these we need to:
1. Check the rc bits on the L2 -> L1 pte match, and otherwise reflect the fault down to L1.
2. Set the rc bits in the L1 -> L0 pte which corresponds to the same host page.
3. Set the rc bits in the L2 -> L0 pte.As we reuse a large number of functions in
book3s_64_mmu_radix.cfor this we also needed to refactor a number of these functions to take an lpid parameter so that the correct lpid is used for tlb invalidations. The functionality however has remained the same.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Suraj Jitindar Singh <[email protected]>Signed-off-by Paul Mackerras <[email protected]>Some comments below, but no showstoppers, so,
Reviewed-by David Gibson <[email protected]>
One more, again not a showstopper:
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_nested.c struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid) · line 404
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_nested.c +6
-0@@ -393,10 +396,20 @@ struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid)*/ static void kvmhv_release_nested (struct kvm_nested_guest *gp) { +struct kvm *kvm = gp->l1_host; + +if (gp->shadow_pgtable) { +/* +* No vcpu is using this struct and no call to +* kvmhv_remove_nest_rmap can find this struct,
It's kind of dubious that you're referring to kvmhv_remove_nest_rmap() a patch before it is introduced.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH v3 22/33] KVM: PPC: Book3S HV: Handle page fault for a nested guest
In reply to David GibsonQuoted 14 lines · David Gibson
arch/powerpc/include/asm/kvm_host.h struct kvmppc_pte { · line 372
Quoted 8 lines · Paul Mackerras
From: Suraj Jitindar Singh <[email protected]>
arch/powerpc/include/asm/kvm_host.h +2
-0@@ -367,7 +367,9 @@ struct kvmppc_pte { bool may_write : 1; bool may_execute : 1;unsigned long wimg; +unsigned long rc;u8 page_size;/* MMU_PAGE_xxx */ +u16 page_shift;It's a bit ugly that this has both
page_sizeandpage_shift, which is redundant information AFAICT. Also, why doespage_shiftneed to be u16 - given that 2^255 bytes is much more than our supported address space, let alone a plausible page size.
These values are all essentially function outputs, so I don't think it's ugly to have the same information in different forms. I actually don't like using the MMU_PAGE_xxx values, because the information in the mmu_psize_defs[] array depends on the MMU mode of the host, but KVM needs to be able to work with guests in both MMU modes. More generally I don't think it's a good idea that the KVM <-> guest interface depends so much on what the host firmware tells us about the physical machine we're on. Thus I'm trying to move away from using MMU_PSIZE_xxx values and mmu_psize_defs[] in KVM code.
I'll change the type to u8.
Quoted 45 lines
Quoted 45 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +4
-27@@ -29,43 +29,16 @@*/ static int p9_supported_radix_bits[4 ] = {5 ,9 ,9 ,13 }; -/* -* Used to walk a partition or process table radix tree in guest memory -* Note: We exploit the fact that a partition table and a process -* table have the same layout, a partition-scoped page table and a -* process-scoped page table have the same layout, and the 2nd -* doubleword of a partition table entry has the same layout as -* the PTCR register. -*/ -int kvmppc_mmu_radix_translate_table (struct kvm_vcpu *vcpu,gva_t eaddr, -struct kvmppc_pte *gpte,u64 table, -int table_index,u64 *pte_ret_p) +int kvmppc_mmu_walk_radix_tree (struct kvm_vcpu *vcpu,gva_t eaddr, +struct kvmppc_pte *gpte,u64 root, +u64 *pte_ret_p) {struct kvm *kvm = vcpu->kvm;int ret, level, ps; -unsigned long ptbl, root; -unsigned long rts, bits, offset; -unsigned long size, index; -struct prtb_entry entry; +unsigned long rts, bits, offset, index;u64 pte, base, gpa;__be64 rpte; -if ((table &PRTS_MASK ) >24 ) -return -EINVAL ; - size =1ul << ((table &PRTS_MASK ) +12 ); - -/* Is the table big enough to contain this entry? */ -if ((table_index *sizeof (entry)) >= size) -return -EINVAL ; - -/* Read the table to find the root of the radix tree */ - ptbl = (table &PRTB_MASK ) + (table_index *sizeof (entry)); - ret =kvm_read_guest (kvm, ptbl, &entry,sizeof (entry)); -if (ret)
Quoted 8 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c line 68
Quoted 4 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +0
-4-return ret; - -/* Root is stored in the first double word */ - root =be64_to_cpu (entry.prtb0);This refactoring somewhat obscures the changes directly relevant to the nested guest handling. Ideally it would be nice to fold some of this into the earlier reworkings.
True, but given the rapidly approaching merge window, I'm not inclined to rework it.
Quoted 12 lines
Quoted 12 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +12
-0+if (ret) { +/* We didn't find a pte */ +if (ret == -EINVAL ) { +/* Unsupported mmu config */ + flags |=DSISR_UNSUPP_MMU ; + }else if (ret == -ENOENT ) { +/* No translation found */ + flags |=DSISR_NOHPTE ; + }else if (ret == -EFAULT ) { +/* Couldn't access L1 real address */ + flags |=DSISR_PRTABLE_FAULT ; + vcpu->arch.fault_gpa = fault_addr;
Quoted 8 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c
Quoted 5 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +5
-0+ }else { +/* Unknown error */ +return ret; + } + goto resume_host;This is effectively forwarding the fault to L1, yes? In which case a different name might be better than the ambiguous "
resume_host".
I'll change it to "forward_to_l1".
Paul.
Re: [PATCH v3 22/33] KVM: PPC: Book3S HV: Handle page fault for a nested guest
Quoted 16 lines · Paul Mackerras
Quoted 14 lines · David Gibson
arch/powerpc/include/asm/kvm_host.h struct kvmppc_pte { · line 372
Quoted 8 lines · Paul Mackerras
From: Suraj Jitindar Singh <[email protected]>
arch/powerpc/include/asm/kvm_host.h +2
-0@@ -367,7 +367,9 @@ struct kvmppc_pte { bool may_write : 1; bool may_execute : 1;unsigned long wimg; +unsigned long rc;u8 page_size;/* MMU_PAGE_xxx */ +u16 page_shift;It's a bit ugly that this has both
page_sizeandpage_shift, which is redundant information AFAICT. Also, why doespage_shiftneed to be u16 - given that 2^255 bytes is much more than our supported address space, let alone a plausible page size.
Quoted 9 lines
These values are all essentially function outputs, so I don't think it's ugly to have the same information in different forms. I actually don't like using the
MMU_PAGE_xxxvalues, because the information in the mmu_psize_defs[] array depends on the MMU mode of the host, but KVM needs to be able to work with guests in both MMU modes. More generally I don't think it's a good idea that the KVM <-> guest interface depends so much on what the host firmware tells us about the physical machine we're on. Thus I'm trying to move away from usingMMU_PSIZE_xxxvalues and mmu_psize_defs[] in KVM code.
Fair enough.
Quoted 56 lines
I'll change the type to u8.
Quoted 45 lines
Quoted 45 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +4
-27@@ -29,43 +29,16 @@*/ static int p9_supported_radix_bits[4 ] = {5 ,9 ,9 ,13 }; -/* -* Used to walk a partition or process table radix tree in guest memory -* Note: We exploit the fact that a partition table and a process -* table have the same layout, a partition-scoped page table and a -* process-scoped page table have the same layout, and the 2nd -* doubleword of a partition table entry has the same layout as -* the PTCR register. -*/ -int kvmppc_mmu_radix_translate_table (struct kvm_vcpu *vcpu,gva_t eaddr, -struct kvmppc_pte *gpte,u64 table, -int table_index,u64 *pte_ret_p) +int kvmppc_mmu_walk_radix_tree (struct kvm_vcpu *vcpu,gva_t eaddr, +struct kvmppc_pte *gpte,u64 root, +u64 *pte_ret_p) {struct kvm *kvm = vcpu->kvm;int ret, level, ps; -unsigned long ptbl, root; -unsigned long rts, bits, offset; -unsigned long size, index; -struct prtb_entry entry; +unsigned long rts, bits, offset, index;u64 pte, base, gpa;__be64 rpte; -if ((table &PRTS_MASK ) >24 ) -return -EINVAL ; - size =1ul << ((table &PRTS_MASK ) +12 ); - -/* Is the table big enough to contain this entry? */ -if ((table_index *sizeof (entry)) >= size) -return -EINVAL ; - -/* Read the table to find the root of the radix tree */ - ptbl = (table &PRTB_MASK ) + (table_index *sizeof (entry)); - ret =kvm_read_guest (kvm, ptbl, &entry,sizeof (entry)); -if (ret)Quoted 8 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c line 68
Quoted 4 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +0
-4-return ret; - -/* Root is stored in the first double word */ - root =be64_to_cpu (entry.prtb0);This refactoring somewhat obscures the changes directly relevant to the nested guest handling. Ideally it would be nice to fold some of this into the earlier reworkings.
Quoted 2 lines
True, but given the rapidly approaching merge window, I'm not inclined to rework it.
Yeah, ok.
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +12
-0+if (ret) { +/* We didn't find a pte */ +if (ret == -EINVAL ) { +/* Unsupported mmu config */ + flags |=DSISR_UNSUPP_MMU ; + }else if (ret == -ENOENT ) { +/* No translation found */ + flags |=DSISR_NOHPTE ; + }else if (ret == -EFAULT ) { +/* Couldn't access L1 real address */ + flags |=DSISR_PRTABLE_FAULT ; + vcpu->arch.fault_gpa = fault_addr;Quoted 8 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c
Quoted 5 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +5
-0+ }else { +/* Unknown error */ +return ret; + } + goto resume_host;This is effectively forwarding the fault to L1, yes? In which case a different name might be better than the ambiguous "
resume_host".
Quoted 1 line
I'll change it to "
forward_to_l1".
Thanks.
Quoted 4 lines
Paul.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v3 23/33 KVM: PPC: Book3S HV: Introduce rmap to track nested guest mappings
In reply to Paul MackerrasFrom: Suraj Jitindar Singh <[email protected]>
When a host (L0) page which is mapped into a (L1) guest is in turn mapped through to a nested (L2) guest we keep a reverse mapping (rmap) so that these mappings can be retrieved later.
Whenever we create an entry in a shadow_pgtable for a nested guest we create a corresponding rmap entry and add it to the list for the L1 guest memslot at the index of the L1 guest page it maps. This means at the L1 guest memslot we end up with lists of rmaps.
When we are notified of a host page being invalidated which has been mapped through to a (L1) guest, we can then walk the rmap list for that guest page, and find and invalidate all of the corresponding shadow_pgtable entries.
In order to reduce memory consumption, we compress the information for each rmap entry down to 52 bits -- 12 bits for the LPID and 40 bits for the guest real page frame number -- which will fit in a single unsigned long. To avoid a scenario where a guest can trigger unbounded memory allocations, we scan the list when adding an entry to see if there is already an entry with the contents we need. This can occur, because we don't ever remove entries from the middle of a list.
A struct nested guest rmap is a list pointer and an rmap entry;
----------------
| next pointer |
----------------
| rmap entry |
----------------
Thus the rmap pointer for each guest frame number in the memslot can be either NULL, a single entry, or a pointer to a list of nested rmap entries.
gfn memslot rmap array
-------------------------
0 | NULL | (no rmap entry)
-------------------------
1 | single rmap entry | (rmap entry with low bit set)
-------------------------
2 | list head pointer | (list of rmap entries)
-------------------------
The final entry always has the lowest bit set and is stored in the next pointer of the last list entry, or as a single rmap entry. With a list of rmap entries looking like;
----------------- ----------------- -------------------------
| list head ptr | ----> | next pointer | ----> | single rmap entry |
----------------- ----------------- -------------------------
| rmap entry | | rmap entry | ----------------- -------------------------
--- arch/powerpc/include/asm/kvm_book3s.h | 3 + arch/powerpc/include/asm/kvm_book3s_64.h | 70 ++++++++++++++++- arch/powerpc/kvm/book3s_64_mmu_radix.c | 44 +++++++---- arch/powerpc/kvm/book3s_hv.c | 1 + arch/powerpc/kvm/book3s_hv_nested.c | 130 ++++++++++++++++++++++++++++++- 5 files changed, 233 insertions(+), 15 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +3 -0
@@ -196,6 +196,9 @@ extern int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr, int table_index, u64 *pte_ret_p);extern int kvmppc_mmu_radix_xlate (struct kvm_vcpu *vcpu,gva_t eaddr,struct kvmppc_pte *gpte,bool data,bool iswrite); +extern void kvmppc_unmap_pte (struct kvm *kvm,pte_t *pte,unsigned long gpa, +unsigned int shift,struct kvm_memory_slot *memslot, +unsigned int lpid);extern bool kvmppc_hv_handle_set_rc (struct kvm *kvm,pgd_t *pgtable,bool writing,unsigned long gpa,unsigned int lpid);
arch/powerpc/include/asm/kvm_book3s_64.h +69 -1
82 lines not on this page · raw message
arch/powerpc/kvm/book3s_64_mmu_radix.c +31 -13
90 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv.c +1 -0
@@ -4292,6 +4292,7 @@ static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free, struct kvm_memory_slot *dont) {if (!dont || free->arch.rmap != dont->arch.rmap) { +kvmhv_free_memslot_nest_rmap (free);vfree (free->arch.rmap); free->arch.rmap =NULL ; }
arch/powerpc/kvm/book3s_hv_nested.c +129 -1
160 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH v3 23/33] KVM: PPC: Book3S HV: Introduce rmap to track nested guest mappings
Quoted 82 lines · Paul Mackerras
From: Suraj Jitindar Singh <[email protected]>
When a host (L0) page which is mapped into a (L1) guest is in turn mapped through to a nested (L2) guest we keep a reverse mapping (rmap) so that these mappings can be retrieved later.
Whenever we create an entry in a
shadow_pgtablefor a nested guest we create a corresponding rmap entry and add it to the list for the L1 guest memslot at the index of the L1 guest page it maps. This means at the L1 guest memslot we end up with lists of rmaps.When we are notified of a host page being invalidated which has been mapped through to a (L1) guest, we can then walk the rmap list for that guest page, and find and invalidate all of the corresponding
shadow_pgtableentries.In order to reduce memory consumption, we compress the information for each rmap entry down to 52 bits -- 12 bits for the LPID and 40 bits for the guest real page frame number -- which will fit in a single unsigned long. To avoid a scenario where a guest can trigger unbounded memory allocations, we scan the list when adding an entry to see if there is already an entry with the contents we need. This can occur, because we don't ever remove entries from the middle of a list.
A struct nested guest rmap is a list pointer and an rmap entry;
----------------
| next pointer |
----------------
| rmap entry |
----------------Thus the rmap pointer for each guest frame number in the memslot can be either NULL, a single entry, or a pointer to a list of nested rmap entries.
gfn memslot rmap array
(quoted diff) +0
-0------------------------- 0 | NULL | (no rmap entry) ------------------------- 1 | single rmap entry | (rmap entry with low bit set) ------------------------- 2 | list head pointer | (list of rmap entries) -------------------------The final entry always has the lowest bit set and is stored in the next pointer of the last list entry, or as a single rmap entry. With a list of rmap entries looking like;
----------------- ----------------- -------------------------
| list head ptr | ----> | next pointer | ----> | single rmap entry |
----------------- ----------------- -------------------------| rmap entry | | rmap entry | ----------------- -------------------------Signed-off-by Suraj Jitindar Singh <[email protected]>Signed-off-by Paul Mackerras <[email protected]>--- arch/powerpc/include/asm/kvm_book3s.h | 3 + arch/powerpc/include/asm/kvm_book3s_64.h | 70 ++++++++++++++++- arch/powerpc/kvm/book3s_64_mmu_radix.c | 44 +++++++---- arch/powerpc/kvm/book3s_hv.c | 1 + arch/powerpc/kvm/book3s_hv_nested.c | 130 ++++++++++++++++++++++++++++++- 5 files changed, 233 insertions(+), 15 deletions(-)arch/powerpc/include/asm/kvm_book3s.h +3
-0@@ -196,6 +196,9 @@ extern int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr, int table_index, u64 *pte_ret_p);extern int kvmppc_mmu_radix_xlate (struct kvm_vcpu *vcpu,gva_t eaddr,struct kvmppc_pte *gpte,bool data,bool iswrite); +extern void kvmppc_unmap_pte (struct kvm *kvm,pte_t *pte,unsigned long gpa, +unsigned int shift,struct kvm_memory_slot *memslot, +unsigned int lpid);extern bool kvmppc_hv_handle_set_rc (struct kvm *kvm,pgd_t *pgtable,bool writing,unsigned long gpa,unsigned int lpid);arch/powerpc/include/asm/kvm_book3s_64.h +0
-0
arch/powerpc/include/asm/kvm_book3s_64.h struct kvm_nested_guest { · line 59
Quoted 7 lines
arch/powerpc/include/asm/kvm_book3s_64.h +4
-0@@ -53,6 +53,66 @@ struct kvm_nested_guest {struct kvm_nested_guest *next; }; +/* +* We define a nested rmap entry as a single 64-bit quantity +* 0xFFF0000000000000 12-bit lpid field +* 0x000FFFFFFFFFF000 40-bit guest physical address field
I thought we could potentially support guests with >1TiB of RAM..?
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH v3 23/33] KVM: PPC: Book3S HV: Introduce rmap to track nested guest mappings
Quoted 86 lines · David Gibson
Quoted 85 lines · Paul Mackerras
From: Suraj Jitindar Singh <[email protected]>
When a host (L0) page which is mapped into a (L1) guest is in turn mapped through to a nested (L2) guest we keep a reverse mapping (rmap) so that these mappings can be retrieved later.
Whenever we create an entry in a
shadow_pgtablefor a nested guest we create a corresponding rmap entry and add it to the list for the L1 guest memslot at the index of the L1 guest page it maps. This means at the L1 guest memslot we end up with lists of rmaps.When we are notified of a host page being invalidated which has been mapped through to a (L1) guest, we can then walk the rmap list for that guest page, and find and invalidate all of the corresponding
shadow_pgtableentries.In order to reduce memory consumption, we compress the information for each rmap entry down to 52 bits -- 12 bits for the LPID and 40 bits for the guest real page frame number -- which will fit in a single unsigned long. To avoid a scenario where a guest can trigger unbounded memory allocations, we scan the list when adding an entry to see if there is already an entry with the contents we need. This can occur, because we don't ever remove entries from the middle of a list.
A struct nested guest rmap is a list pointer and an rmap entry;
----------------
| next pointer |
----------------
| rmap entry |
----------------Thus the rmap pointer for each guest frame number in the memslot can be either NULL, a single entry, or a pointer to a list of nested rmap entries.
gfn memslot rmap array
(quoted diff) +0
-0------------------------- 0 | NULL | (no rmap entry) ------------------------- 1 | single rmap entry | (rmap entry with low bit set) ------------------------- 2 | list head pointer | (list of rmap entries) -------------------------The final entry always has the lowest bit set and is stored in the next pointer of the last list entry, or as a single rmap entry. With a list of rmap entries looking like;
----------------- ----------------- -------------------------
| list head ptr | ----> | next pointer | ----> | single rmap entry |
----------------- ----------------- -------------------------| rmap entry | | rmap entry | ----------------- -------------------------Signed-off-by Suraj Jitindar Singh <[email protected]>Signed-off-by Paul Mackerras <[email protected]>--- arch/powerpc/include/asm/kvm_book3s.h | 3 + arch/powerpc/include/asm/kvm_book3s_64.h | 70 ++++++++++++++++- arch/powerpc/kvm/book3s_64_mmu_radix.c | 44 +++++++---- arch/powerpc/kvm/book3s_hv.c | 1 + arch/powerpc/kvm/book3s_hv_nested.c | 130 ++++++++++++++++++++++++++++++- 5 files changed, 233 insertions(+), 15 deletions(-)arch/powerpc/include/asm/kvm_book3s.h +3
-0@@ -196,6 +196,9 @@ extern int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr, int table_index, u64 *pte_ret_p);extern int kvmppc_mmu_radix_xlate (struct kvm_vcpu *vcpu,gva_t eaddr,struct kvmppc_pte *gpte,bool data,bool iswrite); +extern void kvmppc_unmap_pte (struct kvm *kvm,pte_t *pte,unsigned long gpa, +unsigned int shift,struct kvm_memory_slot *memslot, +unsigned int lpid);extern bool kvmppc_hv_handle_set_rc (struct kvm *kvm,pgd_t *pgtable,bool writing,unsigned long gpa,unsigned int lpid);arch/powerpc/include/asm/kvm_book3s_64.h +0
-0@@ -53,6 +53,66 @@ struct kvm_nested_guest {struct kvm_nested_guest *next; };
Quoted 6 lines
arch/powerpc/include/asm/kvm_book3s_64.h struct kvm_nested_guest { · line 59
Quoted 4 lines
arch/powerpc/include/asm/kvm_book3s_64.h +4
-0+/* +* We define a nested rmap entry as a single 64-bit quantity +* 0xFFF0000000000000 12-bit lpid field +* 0x000FFFFFFFFFF000 40-bit guest physical address field I thought we could potentially support guests with >1TiB of RAM..?
We can, that's really a (4k) page frame number, not a physical address. We can support 52-bit guest physical addresses.
Paul.
PATCH v3 24/33 KVM: PPC: Book3S HV: Emulate Privileged TLBIE for guest hypervisors
In reply to Paul MackerrasFrom: Suraj Jitindar Singh <[email protected]>
When running a nested (L2) guest the guest (L1) hypervisor will use hypervisor privileged tlb invalidation instructions (to manage the partition scoped page tables) which will result in hypervisor emulation assistance interrupts. We emulate these instructions on behalf of the L1 guest.
The tlbie instruction can invalidate different scopes:
Invalidate TLB for a given target address:
- This invalidates a single L2 -> L1 pte
- We need to invalidate any L2 -> L0 shadow_pgtable ptes which map the L2 address space which is being invalidated. This is because a single L2 -> L1 pte may have been mapped with more than one pte in the L2 -> L0 page tables.
Invalidate the entire TLB for a given LPID or for all LPIDs:
- Invalidate the entire shadow_pgtable for a given nested guest, or for all nested guests.
Invalidate the PWC (page walk cache) for a given LPID or for all LPIDs:
- We don't cache the PWC, so nothing to do
Invalidate the entire TLB, PWC and partition table for a given/all LPIDs:
- Here we re-read the partition table entry and remove the nested state for any nested guest for which the first doubleword of the partition table entry is now zero.
This also implements the H_TLB_INVALIDATE hcall. It takes as parameters the tlbie instruction word (of which the RIC, PRS and R fields are used), the rS value (giving the lpid, where required) and the rB value (giving the IS, AP and EPN values).
[[email protected] - adapted to having the partition table in guest memory, added the H_TLB_INVALIDATE implementation.]
--- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 12 ++ arch/powerpc/include/asm/kvm_book3s.h | 1 + arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/kvm/book3s_emulate.c | 1 - arch/powerpc/kvm/book3s_hv.c | 3 + arch/powerpc/kvm/book3s_hv_nested.c | 210 +++++++++++++++++++++++++- 6 files changed, 225 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/book3s/64/mmu-hash.h +12 -0
@@ -203,6 +203,18 @@ static inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)BUG (); } +static inline unsigned int ap_to_shift (unsigned long ap) +{ +int psize; + +for (psize =0 ; psize <MMU_PAGE_COUNT ; psize++) { +if (mmu_psize_defs[psize].ap == ap) +return mmu_psize_defs[psize].shift; + } + +return -1 ; +} +static inline unsigned long get_sllp_encoding (int psize) {unsigned long sllp;
arch/powerpc/include/asm/kvm_book3s.h +1 -0
@@ -301,6 +301,7 @@ long kvmhv_set_partition_table(struct kvm_vcpu *vcpu);void kvmhv_set_ptbl_entry (unsigned int lpid,u64 dw0,u64 dw1);void kvmhv_release_all_nested (struct kvm *kvm);long kvmhv_enter_nested_guest (struct kvm_vcpu *vcpu); +long kvmhv_do_nested_tlbie (struct kvm_vcpu *vcpu);int kvmhv_run_single_vcpu (struct kvm_run *kvm_run,struct kvm_vcpu *vcpu,u64 time_limit,unsigned long lpcr);void kvmhv_save_hv_regs (struct kvm_vcpu *vcpu,struct hv_guest_state *hr);
arch/powerpc/include/asm/ppc-opcode.h +1 -0
@@ -104,6 +104,7 @@#define OP_31_XOP_LHZUX 311#define OP_31_XOP_MSGSNDP 142#define OP_31_XOP_MSGCLRP 174 +#define OP_31_XOP_TLBIE 306#define OP_31_XOP_MFSPR 339#define OP_31_XOP_LWAX 341#define OP_31_XOP_LHAX 343
arch/powerpc/kvm/book3s_emulate.c +0 -1
@@ -36,7 +36,6 @@#define OP_31_XOP_MTSR 210#define OP_31_XOP_MTSRIN 242#define OP_31_XOP_TLBIEL 274 -#define OP_31_XOP_TLBIE 306/* Opcode is officially reserved, reuse it as sc 1 when sc 1 doesn't trap */ #define OP_31_XOP_FAKE_SC1 308#define OP_31_XOP_SLBMTE 402
arch/powerpc/kvm/book3s_hv.c +3 -0
@@ -974,6 +974,9 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)break ;case H_TLB_INVALIDATE : ret =H_FUNCTION ; +if (!vcpu->kvm->arch.nested_enable) +break ; + ret =kvmhv_do_nested_tlbie (vcpu);break ;default :
arch/powerpc/kvm/book3s_hv_nested.c +208 -2
225 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH v3 24/33] KVM: PPC: Book3S HV: Emulate Privileged TLBIE for guest hypervisors
Quoted 34 lines · Paul Mackerras
From: Suraj Jitindar Singh <[email protected]>
When running a nested (L2) guest the guest (L1) hypervisor will use hypervisor privileged tlb invalidation instructions (to manage the partition scoped page tables) which will result in hypervisor emulation assistance interrupts. We emulate these instructions on behalf of the L1 guest.
The tlbie instruction can invalidate different scopes:
Invalidate TLB for a given target address:
- This invalidates a single L2 -> L1 pte
- We need to invalidate any L2 -> L0shadow_pgtableptes which map the L2 address space which is being invalidated. This is because a single L2 -> L1 pte may have been mapped with more than one pte in the L2 -> L0 page tables.Invalidate the entire TLB for a given LPID or for all LPIDs:
- Invalidate the entireshadow_pgtablefor a given nested guest, or for all nested guests.Invalidate the PWC (page walk cache) for a given LPID or for all LPIDs:
- We don't cache the PWC, so nothing to doInvalidate the entire TLB, PWC and partition table for a given/all LPIDs:
- Here we re-read the partition table entry and remove the nested state for any nested guest for which the first doubleword of the partition table entry is now zero.This also implements the
H_TLB_INVALIDATEhcall. It takes as parameters the tlbie instruction word (of which the RIC, PRS and R fields are used), the rS value (giving the lpid, where required) and the rB value (giving the IS, AP and EPN values).
Quoted 5 lines
[[email protected] - adapted to having the partition table in guest memory, added the
H_TLB_INVALIDATEimplementation.]Signed-off-by Suraj Jitindar Singh <[email protected]>Signed-off-by Paul Mackerras <[email protected]>
Again, do we need this if we're moving to a paravirt tlbie?
Quoted 313 lines
--- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 12 ++ arch/powerpc/include/asm/kvm_book3s.h | 1 + arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/kvm/book3s_emulate.c | 1 - arch/powerpc/kvm/book3s_hv.c | 3 + arch/powerpc/kvm/book3s_hv_nested.c | 210 +++++++++++++++++++++++++- 6 files changed, 225 insertions(+), 3 deletions(-)arch/powerpc/include/asm/book3s/64/mmu-hash.h +12
-0@@ -203,6 +203,18 @@ static inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)BUG (); } +static inline unsigned int ap_to_shift (unsigned long ap) +{ +int psize; + +for (psize =0 ; psize <MMU_PAGE_COUNT ; psize++) { +if (mmu_psize_defs[psize].ap == ap) +return mmu_psize_defs[psize].shift; + } + +return -1 ; +} +static inline unsigned long get_sllp_encoding (int psize) {unsigned long sllp;arch/powerpc/include/asm/kvm_book3s.h +1
-0@@ -301,6 +301,7 @@ long kvmhv_set_partition_table(struct kvm_vcpu *vcpu);void kvmhv_set_ptbl_entry (unsigned int lpid,u64 dw0,u64 dw1);void kvmhv_release_all_nested (struct kvm *kvm);long kvmhv_enter_nested_guest (struct kvm_vcpu *vcpu); +long kvmhv_do_nested_tlbie (struct kvm_vcpu *vcpu);int kvmhv_run_single_vcpu (struct kvm_run *kvm_run,struct kvm_vcpu *vcpu,u64 time_limit,unsigned long lpcr);void kvmhv_save_hv_regs (struct kvm_vcpu *vcpu,struct hv_guest_state *hr);arch/powerpc/include/asm/ppc-opcode.h +1
-0@@ -104,6 +104,7 @@#define OP_31_XOP_LHZUX 311#define OP_31_XOP_MSGSNDP 142#define OP_31_XOP_MSGCLRP 174 +#define OP_31_XOP_TLBIE 306#define OP_31_XOP_MFSPR 339#define OP_31_XOP_LWAX 341#define OP_31_XOP_LHAX 343arch/powerpc/kvm/book3s_emulate.c +0
-1@@ -36,7 +36,6 @@#define OP_31_XOP_MTSR 210#define OP_31_XOP_MTSRIN 242#define OP_31_XOP_TLBIEL 274 -#define OP_31_XOP_TLBIE 306/* Opcode is officially reserved, reuse it as sc 1 when sc 1 doesn't trap */ #define OP_31_XOP_FAKE_SC1 308#define OP_31_XOP_SLBMTE 402arch/powerpc/kvm/book3s_hv.c +3
-0@@ -974,6 +974,9 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)break ;case H_TLB_INVALIDATE : ret =H_FUNCTION ; +if (!vcpu->kvm->arch.nested_enable) +break ; + ret =kvmhv_do_nested_tlbie (vcpu);break ;default :arch/powerpc/kvm/book3s_hv_nested.c +208
-2@@ -465,7 +465,7 @@ void kvmhv_release_all_nested(struct kvm *kvm) }/* caller must hold gp->tlb_lock */ -void kvmhv_flush_nested (struct kvm_nested_guest *gp) +static void kvmhv_flush_nested (struct kvm_nested_guest *gp) {struct kvm *kvm = gp->l1_host;@@ -685,10 +685,216 @@ static int kvmhv_emulate_priv_mfspr(struct kvm_run *run, struct kvm_vcpu *vcpu,return EMULATE_FAIL ; } +static inline int get_ric (unsigned int instr) +{ +return (instr >>18 ) &0x3 ; +} + +static inline int get_prs (unsigned int instr) +{ +return (instr >>17 ) &0x1 ; +} + +static inline int get_r (unsigned int instr) +{ +return (instr >>16 ) &0x1 ; +} + +static inline int get_lpid (unsigned long r_val) +{ +return r_val &0xffffffff ; +} + +static inline int get_is (unsigned long r_val) +{ +return (r_val >>10 ) &0x3 ; +} + +static inline int get_ap (unsigned long r_val) +{ +return (r_val >>5 ) &0x7 ; +} + +static inline long get_epn (unsigned long r_val) +{ +return r_val >>12 ; +} + +static int kvmhv_emulate_tlbie_tlb_addr (struct kvm_vcpu *vcpu,int lpid, +int ap,long epn) +{ +struct kvm *kvm = vcpu->kvm; +struct kvm_nested_guest *gp; +long npages; +int shift; +unsigned long addr; + + shift =ap_to_shift (ap); + addr = epn <<12 ; +if (shift <0 ) +/* Invalid ap encoding */ +return -EINVAL ; + + addr &= ~((1UL << shift) -1 ); + npages =1UL << (shift -PAGE_SHIFT ); + + gp =kvmhv_get_nested (kvm, lpid, false); +if (!gp)/* No such guest -> nothing to do */ +return 0 ; +mutex_lock (&gp->tlb_lock); + +/* There may be more than one host page backing this single guest pte */ +do { +kvmhv_invalidate_shadow_pte (vcpu, gp, addr, &shift); + + npages -=1UL << (shift -PAGE_SHIFT ); + addr +=1UL << shift; + }while (npages >0 ); + +mutex_unlock (&gp->tlb_lock); +kvmhv_put_nested (gp); +return 0 ; +} + +static void kvmhv_emulate_tlbie_lpid (struct kvm_vcpu *vcpu, +struct kvm_nested_guest *gp,int ric) +{ +struct kvm *kvm = vcpu->kvm; + +mutex_lock (&gp->tlb_lock); +switch (ric) { +case 0 : +/* Invalidate TLB */ +spin_lock (&kvm->mmu_lock); +kvmppc_free_pgtable_radix (kvm, gp->shadow_pgtable, + gp->shadow_lpid); +radix__flush_tlb_lpid (gp->shadow_lpid); +spin_unlock (&kvm->mmu_lock); +break ; +case 1 : +/* +* Invalidate PWC +* We don't cache this -> nothing to do +*/ +break ; +case 2 : +/* Invalidate TLB, PWC and caching of partition table entries */ +kvmhv_flush_nested (gp); +break ; +default : +break ; + } +mutex_unlock (&gp->tlb_lock); +} + +static void kvmhv_emulate_tlbie_all_lpid (struct kvm_vcpu *vcpu,int ric) +{ +struct kvm *kvm = vcpu->kvm; +struct kvm_nested_guest *gp; +int i; + +spin_lock (&kvm->mmu_lock); +for (i =0 ; i <= kvm->arch.max_nested_lpid; i++) { + gp = kvm->arch.nested_guests[i]; +if (gp) { +spin_unlock (&kvm->mmu_lock); +kvmhv_emulate_tlbie_lpid (vcpu, gp, ric); +spin_lock (&kvm->mmu_lock); + } + } +spin_unlock (&kvm->mmu_lock); +} + +static int kvmhv_emulate_priv_tlbie (struct kvm_vcpu *vcpu,unsigned int instr, +int rs,int rb) +{ +struct kvm *kvm = vcpu->kvm; +struct kvm_nested_guest *gp; +int r, ric, prs, is, ap; +int lpid; +long epn; +int ret =0 ; + + ric =get_ric (instr); + prs =get_prs (instr); + r =get_r (instr); + lpid =get_lpid (kvmppc_get_gpr (vcpu, rs)); + is =get_is (kvmppc_get_gpr (vcpu, rb)); + +/* +* These cases are invalid and __should__ have caused a machine check +* r != 1 -> Only radix supported +* prs == 1 -> Not HV privileged +* ric == 3 -> No clusted bombs for radix +* is == 1 -> Partition scoped translations not associated with pid +* (!is) && (ric == 1 || ric == 2) -> Not supported by ISA +*/ +if ((!r) || (prs) || (ric ==3 ) || (is ==1 ) || + ((!is) && (ric ==1 || ric ==2 ))) +return -EINVAL ; + +switch (is) { +case 0 : +/* +* We know ric == 0 +* Invalidate TLB for a given target address +*/ + epn =get_epn (kvmppc_get_gpr (vcpu, rb)); + ap =get_ap (kvmppc_get_gpr (vcpu, rb)); + ret =kvmhv_emulate_tlbie_tlb_addr (vcpu, lpid, ap, epn); +break ; +case 2 : +/* Invalidate matching LPID */ + gp =kvmhv_get_nested (kvm, lpid, false); +if (gp) { +kvmhv_emulate_tlbie_lpid (vcpu, gp, ric); +kvmhv_put_nested (gp); + } +break ; +case 3 : +/* Invalidate ALL LPIDs */ +kvmhv_emulate_tlbie_all_lpid (vcpu, ric); +break ; +default : + ret = -EINVAL ; +break ; + } + +return ret; +} +static int kvmhv_emulate_priv_op_31 (struct kvm_run *run,struct kvm_vcpu *vcpu,unsigned int instr) { -return EMULATE_FAIL ; +int ret; + +switch (get_xop (instr)) { +case OP_31_XOP_TLBIE : + ret =kvmhv_emulate_priv_tlbie (vcpu, instr,get_rs (instr), +get_rb (instr)); +if (ret) { +kvmppc_core_queue_program (vcpu,SRR1_PROGILL ); +return EMULATE_AGAIN ; + } +return EMULATE_DONE ; +default : +return EMULATE_FAIL ; + } +} + +/* +* This handles the H_TLB_INVALIDATE hcall. +* Parameters are (r4) tlbie instruction code, (r5) rS contents, +* (r6) rB contents. +*/ +long kvmhv_do_nested_tlbie (struct kvm_vcpu *vcpu) +{ +int ret; + + ret =kvmhv_emulate_priv_tlbie (vcpu,kvmppc_get_gpr (vcpu,4 ),5 ,6 ); +if (ret) +return H_PARAMETER ; +return H_SUCCESS ; } static int kvmhv_emulate_priv_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v3 25/33 KVM: PPC: Book3S HV: Invalidate TLB when nested vcpu moves physical cpu
In reply to Paul MackerrasFrom: Suraj Jitindar Singh <[email protected]>
This is only done at level 0, since only level 0 knows which physical CPU a vcpu is running on. This does for nested guests what L0 already did for its own guests, which is to flush the TLB on a pCPU when it goes to run a vCPU there, and there is another vCPU in the same VM which previously ran on this pCPU and has now started to run on another pCPU. This is to handle the situation where the other vCPU touched a mapping, moved to another pCPU and did a tlbiel (local-only tlbie) on that new pCPU and thus left behind a stale TLB entry on this pCPU.
This introduces a limit on the the vcpu_token values used in the H_ENTER_NESTED hcall -- they must now be less than NR_CPUS.
[[email protected] - made prev_cpu array be unsigned short[] to reduce
memory consumption.]
--- arch/powerpc/include/asm/kvm_book3s_64.h | 3 + arch/powerpc/kvm/book3s_hv.c | 101 +++++++++++++++++++------------ arch/powerpc/kvm/book3s_hv_nested.c | 5 ++ 3 files changed, 71 insertions(+), 38 deletions(-)
arch/powerpc/include/asm/kvm_book3s_64.h +3 -0
@@ -51,6 +51,9 @@ struct kvm_nested_guest {long refcnt;/* number of pointers to this struct */ struct mutex tlb_lock;/* serialize page faults and tlbies */ struct kvm_nested_guest *next; +cpumask_t need_tlb_flush; +cpumask_t cpu_in_guest; +unsigned short prev_cpu[NR_CPUS ]; };/*
arch/powerpc/kvm/book3s_hv.c +63 -38
156 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_nested.c +5 -0
@@ -168,6 +168,9 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)if (err)return H_PARAMETER ; +if (l2_hv.vcpu_token >=NR_CPUS ) +return H_PARAMETER ; +/* translate lpid */ l2 =kvmhv_get_nested (vcpu->kvm, l2_hv.lpid, true);if (!l2)
@@ -383,6 +386,8 @@ struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid) goto out_free2; gp->shadow_lpid = shadow_lpid; +memset (gp->prev_cpu,-1 ,sizeof (gp->prev_cpu)); +return gp; out_free2:
Signature · 1 line
2.7.4
PATCH v3 26/33 KVM: PPC: Book3S HV: Don't access HFSCR, LPIDR or LPCR when running nested
In reply to Paul MackerrasWhen running as a nested hypervisor, this avoids reading hypervisor privileged registers (specifically HFSCR, LPIDR and LPCR) at startup; instead reasonable default values are used. This also avoids writing LPIDR in the single-vcpu entry/exit path.
Also, this removes the check for CPU_FTR_HVMODE in kvmppc_mmu_hv_init() since its only caller already checks this.
--- arch/powerpc/kvm/book3s_64_mmu_hv.c | 7 +++---- arch/powerpc/kvm/book3s_hv.c | 33 +++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 16 deletions(-)
arch/powerpc/kvm/book3s_64_mmu_hv.c +3 -4
@@ -268,14 +268,13 @@ int kvmppc_mmu_hv_init(void) {unsigned long host_lpid, rsvd_lpid; -if (!cpu_has_feature (CPU_FTR_HVMODE )) -return -EINVAL ; -if (!mmu_has_feature (MMU_FTR_LOCKLESS_TLBIE ))return -EINVAL ;/* POWER7 has 10-bit LPIDs (12-bit in POWER8) */ - host_lpid =mfspr (SPRN_LPID ); + host_lpid =0 ; +if (cpu_has_feature (CPU_FTR_HVMODE )) + host_lpid =mfspr (SPRN_LPID ); rsvd_lpid =LPID_RSVD ;kvmppc_init_lpid (rsvd_lpid +1 );
arch/powerpc/kvm/book3s_hv.c +21 -12
@@ -2197,15 +2197,18 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,* Set the default HFSCR for the guest from the host value. * This value is only used on POWER9. * On POWER9, we want to virtualize the doorbell facility, so we -* turn off the HFSCR bit, which causes those instructions to trap. +* don't set the HFSCR_MSGP bit, and that causes those instructions +* to trap and then we emulate them. */ - vcpu->arch.hfscr =mfspr (SPRN_HFSCR ); -if (cpu_has_feature (CPU_FTR_P9_TM_HV_ASSIST )) + vcpu->arch.hfscr =HFSCR_TAR |HFSCR_EBB |HFSCR_PM |HFSCR_BHRB | +HFSCR_DSCR |HFSCR_VECVSX |HFSCR_FP ; +if (cpu_has_feature (CPU_FTR_HVMODE )) { + vcpu->arch.hfscr &=mfspr (SPRN_HFSCR ); +if (cpu_has_feature (CPU_FTR_P9_TM_HV_ASSIST )) + vcpu->arch.hfscr |=HFSCR_TM ; + } +if (cpu_has_feature (CPU_FTR_TM_COMP )) vcpu->arch.hfscr |=HFSCR_TM ; -else if (!cpu_has_feature (CPU_FTR_TM_COMP )) - vcpu->arch.hfscr &= ~HFSCR_TM ; -if (cpu_has_feature (CPU_FTR_ARCH_300 )) - vcpu->arch.hfscr &= ~HFSCR_MSGP ;kvmppc_mmu_book3s_hv_init (vcpu);
@@ -4024,8 +4027,10 @@ int kvmhv_run_single_vcpu(struct kvm_run *kvm_run,srcu_read_unlock (&vc->kvm->srcu, srcu_idx); -mtspr (SPRN_LPID , vc->kvm->arch.host_lpid); -isync (); +if (cpu_has_feature (CPU_FTR_HVMODE )) { +mtspr (SPRN_LPID , vc->kvm->arch.host_lpid); +isync (); + }trace_hardirqs_off ();set_irq_happened (trap);
@@ -4645,9 +4650,13 @@ static int kvmppc_core_init_vm_hv(struct kvm *kvm) kvm->arch.host_sdr1 =mfspr (SPRN_SDR1 );/* Init LPCR for virtual RMA mode */ - kvm->arch.host_lpid =mfspr (SPRN_LPID ); - kvm->arch.host_lpcr = lpcr =mfspr (SPRN_LPCR ); - lpcr &=LPCR_PECE |LPCR_LPES ; +if (cpu_has_feature (CPU_FTR_HVMODE )) { + kvm->arch.host_lpid =mfspr (SPRN_LPID ); + kvm->arch.host_lpcr = lpcr =mfspr (SPRN_LPCR ); + lpcr &=LPCR_PECE |LPCR_LPES ; + }else { + lpcr =0 ; + } lpcr |= (4UL <<LPCR_DPFD_SH ) |LPCR_HDICE |LPCR_VPM0 |LPCR_VPM1 ; kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
Signature · 1 line
2.7.4
PATCH v3 27/33 KVM: PPC: Book3S HV: Add one-reg interface to virtual PTCR register
In reply to Paul MackerrasThis adds a one-reg register identifier which can be used to read and set the virtual PTCR for the guest. This register identifies the address and size of the virtual partition table for the guest, which contains information about the nested guests under this guest.
Migrating this value is the only extra requirement for migrating a guest which has nested guests (assuming of course that the destination host supports nested virtualization in the kvm-hv module).
--- Documentation/virtual/kvm/api.txt | 1 + arch/powerpc/include/uapi/asm/kvm.h | 1 + arch/powerpc/kvm/book3s_hv.c | 6 ++++++ 3 files changed, 8 insertions(+)
Documentation/virtual/kvm/api.txt +1 -0
@@ -1922,6 +1922,7 @@ registers, find a list below: PPC | KVM_REG_PPC_TIDR | 64 PPC | KVM_REG_PPC_PSSCR | 64 PPC | KVM_REG_PPC_DEC_EXPIRY | 64 + PPC | KVM_REG_PPC_PTCR | 64 PPC | KVM_REG_PPC_TM_GPR0 | 64 ... PPC | KVM_REG_PPC_TM_GPR31 | 64
arch/powerpc/include/uapi/asm/kvm.h +1 -0
@@ -634,6 +634,7 @@ struct kvm_ppc_cpu_char {#define KVM_REG_PPC_DEC_EXPIRY (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xbe)#define KVM_REG_PPC_ONLINE (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xbf) +#define KVM_REG_PPC_PTCR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xc0)/* Transactional Memory checkpointed state: * This is all GPRs, all VSX regs and a subset of SPRs
arch/powerpc/kvm/book3s_hv.c +6 -0
@@ -1733,6 +1733,9 @@ static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,case KVM_REG_PPC_ONLINE : *val =get_reg_val (id, vcpu->arch.online);break ; +case KVM_REG_PPC_PTCR : + *val =get_reg_val (id, vcpu->kvm->arch.l1_ptcr); +break ;default : r = -EINVAL ;break ;
@@ -1964,6 +1967,9 @@ static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,atomic_dec (&vcpu->arch.vcore->online_count); vcpu->arch.online = i;break ; +case KVM_REG_PPC_PTCR : + vcpu->kvm->arch.l1_ptcr =set_reg_val (id, *val); +break ;default : r = -EINVAL ;break ;
Signature · 1 line
2.7.4
PATCH v3 28/33 KVM: PPC: Book3S HV: Sanitise hv_regs on nested guest entry
In reply to Paul MackerrasFrom: Suraj Jitindar Singh <[email protected]>
restore_hv_regs() is used to copy the hv_regs L1 wants to set to run the nested (L2) guest into the vcpu structure. We need to sanitise these values to ensure we don't let the L1 guest hypervisor do things we don't want it to.
We don't let data address watchpoints or completed instruction address breakpoints be set to match in hypervisor state.
We also don't let L1 enable features in the hypervisor facility status and control register (HFSCR) for L2 which we have disabled for L1. That is L2 will get the subset of features which the L0 hypervisor has enabled for L1 and the features L1 wants to enable for L2. This could mean we give L1 a hypervisor facility unavailable interrupt for a facility it thinks it has enabled, however it shouldn't have enabled a facility it itself doesn't have for the L2 guest.
We sanitise the registers when copying in the L2 hv_regs. We don't need to sanitise when copying back the L1 hv_regs since these shouldn't be able to contain invalid values as they're just what was copied out.
--- arch/powerpc/include/asm/reg.h | 1 + arch/powerpc/kvm/book3s_hv_nested.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+)
arch/powerpc/include/asm/reg.h +1 -0
@@ -415,6 +415,7 @@#define HFSCR_DSCR __MASK(FSCR_DSCR_LG)#define HFSCR_VECVSX __MASK(FSCR_VECVSX_LG)#define HFSCR_FP __MASK(FSCR_FP_LG) +#define HFSCR_INTR_CAUSE (ASM_CONST(0xFF) << 56)/* interrupt cause */ #define SPRN_TAR 0x32f/* Target Address Register */ #define SPRN_LPCR 0x13E/* LPAR Control Register */ #define LPCR_VPM0 ASM_CONST(0x8000000000000000)
arch/powerpc/kvm/book3s_hv_nested.c +17 -0
@@ -86,6 +86,22 @@ static void save_hv_return_state(struct kvm_vcpu *vcpu, int trap, } } +static void sanitise_hv_regs (struct kvm_vcpu *vcpu,struct hv_guest_state *hr) +{ +/* +* Don't let L1 enable features for L2 which we've disabled for L1, +* but preserve the interrupt cause field. +*/ + hr->hfscr &= (HFSCR_INTR_CAUSE | vcpu->arch.hfscr); + +/* Don't let data address watchpoint match in hypervisor state */ + hr->dawrx0 &= ~DAWRX_HYP ; + +/* Don't let completed instruction address breakpt match in HV state */ +if ((hr->ciabr &CIABR_PRIV ) ==CIABR_PRIV_HYPER ) + hr->ciabr &= ~CIABR_PRIV ; +} +static void restore_hv_regs (struct kvm_vcpu *vcpu,struct hv_guest_state *hr) {struct kvmppc_vcore *vc = vcpu->arch.vcore;
@@ -198,6 +214,7 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) mask =LPCR_DPFD |LPCR_ILE |LPCR_TC |LPCR_AIL |LPCR_LD |LPCR_LPES |LPCR_MER ; lpcr = (vc->lpcr & ~mask) | (l2_hv.lpcr & mask); +sanitise_hv_regs (vcpu, &l2_hv);restore_hv_regs (vcpu, &l2_hv); vcpu->arch.ret =RESUME_GUEST ;
Signature · 1 line
2.7.4
Re: [PATCH v3 28/33] KVM: PPC: Book3S HV: Sanitise hv_regs on nested guest entry
Quoted 18 lines · Paul Mackerras
From: Suraj Jitindar Singh <[email protected]>
restore_hv_regs()is used to copy thehv_regsL1 wants to set to run the nested (L2) guest into the vcpu structure. We need to sanitise these values to ensure we don't let the L1 guest hypervisor do things we don't want it to.We don't let data address watchpoints or completed instruction address breakpoints be set to match in hypervisor state.
We also don't let L1 enable features in the hypervisor facility status and control register (HFSCR) for L2 which we have disabled for L1. That is L2 will get the subset of features which the L0 hypervisor has enabled for L1 and the features L1 wants to enable for L2. This could mean we give L1 a hypervisor facility unavailable interrupt for a facility it thinks it has enabled, however it shouldn't have enabled a facility it itself doesn't have for the L2 guest.
Quoted 6 lines
We sanitise the registers when copying in the L2
hv_regs. We don't need to sanitise when copying back the L1hv_regssince these shouldn't be able to contain invalid values as they're just what was copied out.Signed-off-by Suraj Jitindar Singh <[email protected]>Signed-off-by Paul Mackerras <[email protected]>
Quoted 52 lines
--- arch/powerpc/include/asm/reg.h | 1 + arch/powerpc/kvm/book3s_hv_nested.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+)arch/powerpc/include/asm/reg.h +1
-0@@ -415,6 +415,7 @@#define HFSCR_DSCR __MASK(FSCR_DSCR_LG)#define HFSCR_VECVSX __MASK(FSCR_VECVSX_LG)#define HFSCR_FP __MASK(FSCR_FP_LG) +#define HFSCR_INTR_CAUSE (ASM_CONST(0xFF) << 56)/* interrupt cause */ #define SPRN_TAR 0x32f/* Target Address Register */ #define SPRN_LPCR 0x13E/* LPAR Control Register */ #define LPCR_VPM0 ASM_CONST(0x8000000000000000)arch/powerpc/kvm/book3s_hv_nested.c +17
-0@@ -86,6 +86,22 @@ static void save_hv_return_state(struct kvm_vcpu *vcpu, int trap, } } +static void sanitise_hv_regs (struct kvm_vcpu *vcpu,struct hv_guest_state *hr) +{ +/* +* Don't let L1 enable features for L2 which we've disabled for L1, +* but preserve the interrupt cause field. +*/ + hr->hfscr &= (HFSCR_INTR_CAUSE | vcpu->arch.hfscr); + +/* Don't let data address watchpoint match in hypervisor state */ + hr->dawrx0 &= ~DAWRX_HYP ; + +/* Don't let completed instruction address breakpt match in HV state */ +if ((hr->ciabr &CIABR_PRIV ) ==CIABR_PRIV_HYPER ) + hr->ciabr &= ~CIABR_PRIV ; +} +static void restore_hv_regs (struct kvm_vcpu *vcpu,struct hv_guest_state *hr) {struct kvmppc_vcore *vc = vcpu->arch.vcore;@@ -198,6 +214,7 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) mask =LPCR_DPFD |LPCR_ILE |LPCR_TC |LPCR_AIL |LPCR_LD |LPCR_LPES |LPCR_MER ; lpcr = (vc->lpcr & ~mask) | (l2_hv.lpcr & mask); +sanitise_hv_regs (vcpu, &l2_hv);restore_hv_regs (vcpu, &l2_hv); vcpu->arch.ret =RESUME_GUEST ;
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v3 29/33 KVM: PPC: Book3S HV: Handle differing endianness for H_ENTER_NESTED
In reply to Paul MackerrasFrom: Suraj Jitindar Singh <[email protected]>
The hcall H_ENTER_NESTED takes as the two parameters the address in L1 guest memory of a hv_regs struct and a pt_regs struct which the L1 guest would like to use to run a L2 guest and in which are returned the exit state of the L2 guest. For efficiency, these are in the endianness of the L1 guest, rather than being always big-endian as is usually the case for PAPR hypercalls.
When reading/writing these structures, this patch handles the case where the endianness of the L1 guest differs from that of the L0 hypervisor, by byteswapping the structures after reading and before writing them back.
Since all the fields of the pt_regs are of the same type, i.e., unsigned long, we treat it as an array of unsigned longs. The fields of struct hv_guest_state are not all the same, so its fields are byteswapped individually.
--- arch/powerpc/kvm/book3s_hv_nested.c | 51 ++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-)
arch/powerpc/kvm/book3s_hv_nested.c +50 -1
@@ -51,6 +51,48 @@ void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr) hr->ppr = vcpu->arch.ppr; } +static void byteswap_pt_regs (struct pt_regs *regs) +{ +unsigned long *addr = (unsigned long *) regs; + +for (; addr < ((unsigned long *) (regs +1 )); addr++) + *addr =swab64 (*addr); +} + +static void byteswap_hv_regs (struct hv_guest_state *hr) +{ + hr->version =swab64 (hr->version); + hr->lpid =swab32 (hr->lpid); + hr->vcpu_token =swab32 (hr->vcpu_token); + hr->lpcr =swab64 (hr->lpcr); + hr->pcr =swab64 (hr->pcr); + hr->amor =swab64 (hr->amor); + hr->dpdes =swab64 (hr->dpdes); + hr->hfscr =swab64 (hr->hfscr); + hr->tb_offset =swab64 (hr->tb_offset); + hr->dawr0 =swab64 (hr->dawr0); + hr->dawrx0 =swab64 (hr->dawrx0); + hr->ciabr =swab64 (hr->ciabr); + hr->hdec_expiry =swab64 (hr->hdec_expiry); + hr->purr =swab64 (hr->purr); + hr->spurr =swab64 (hr->spurr); + hr->ic =swab64 (hr->ic); + hr->vtb =swab64 (hr->vtb); + hr->hdar =swab64 (hr->hdar); + hr->hdsisr =swab64 (hr->hdsisr); + hr->heir =swab64 (hr->heir); + hr->asdr =swab64 (hr->asdr); + hr->srr0 =swab64 (hr->srr0); + hr->srr1 =swab64 (hr->srr1); + hr->sprg[0 ] =swab64 (hr->sprg[0 ]); + hr->sprg[1 ] =swab64 (hr->sprg[1 ]); + hr->sprg[2 ] =swab64 (hr->sprg[2 ]); + hr->sprg[3 ] =swab64 (hr->sprg[3 ]); + hr->pidr =swab64 (hr->pidr); + hr->cfar =swab64 (hr->cfar); + hr->ppr =swab64 (hr->ppr); +} +static void save_hv_return_state (struct kvm_vcpu *vcpu,int trap,struct hv_guest_state *hr) {
@@ -175,6 +217,8 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) sizeof(struct hv_guest_state));if (err)return H_PARAMETER ; +if (kvmppc_need_byteswap (vcpu)) +byteswap_hv_regs (&l2_hv);if (l2_hv.version !=HV_GUEST_STATE_VERSION )return H_P2 ;
@@ -183,7 +227,8 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) sizeof(struct pt_regs));if (err)return H_PARAMETER ; - +if (kvmppc_need_byteswap (vcpu)) +byteswap_pt_regs (&l2_regs);if (l2_hv.vcpu_token >=NR_CPUS )return H_PARAMETER ;
@@ -255,6 +300,10 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)kvmhv_put_nested (l2);/* copy l2_hv_state and regs back to guest */ +if (kvmppc_need_byteswap (vcpu)) { +byteswap_hv_regs (&l2_hv); +byteswap_pt_regs (&l2_regs); + } err =kvm_vcpu_write_guest (vcpu, hv_ptr, &l2_hv,sizeof (struct hv_guest_state ));if (err)
Signature · 1 line
2.7.4
Re: [PATCH v3 29/33] KVM: PPC: Book3S HV: Handle differing endianness for H_ENTER_NESTED
Quoted 8 lines · Paul Mackerras
From: Suraj Jitindar Singh <[email protected]>
The hcall
H_ENTER_NESTEDtakes as the two parameters the address in L1 guest memory of ahv_regsstruct and apt_regsstruct which the L1 guest would like to use to run a L2 guest and in which are returned the exit state of the L2 guest. For efficiency, these are in the endianness of the L1 guest, rather than being always big-endian as is usually the case for PAPR hypercalls.
Does that actually make a difference for efficiency? I thought the presence of the byte-reversing loads and stores meant byteswapping was basically zero-cost on POWER.
Quoted 5 lines
When reading/writing these structures, this patch handles the case where the endianness of the L1 guest differs from that of the L0 hypervisor, by byteswapping the structures after reading and before writing them back.
Quoted 7 lines
Since all the fields of the
pt_regsare of the same type, i.e., unsigned long, we treat it as an array of unsigned longs. The fields of structhv_guest_stateare not all the same, so its fields are byteswapped individually.Signed-off-by Suraj Jitindar Singh <[email protected]>Signed-off-by Paul Mackerras <[email protected]>
Looks correct, though, so
Quoted 87 lines
--- arch/powerpc/kvm/book3s_hv_nested.c | 51 ++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-)arch/powerpc/kvm/book3s_hv_nested.c +50
-1@@ -51,6 +51,48 @@ void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr) hr->ppr = vcpu->arch.ppr; } +static void byteswap_pt_regs (struct pt_regs *regs) +{ +unsigned long *addr = (unsigned long *) regs; + +for (; addr < ((unsigned long *) (regs +1 )); addr++) + *addr =swab64 (*addr); +} + +static void byteswap_hv_regs (struct hv_guest_state *hr) +{ + hr->version =swab64 (hr->version); + hr->lpid =swab32 (hr->lpid); + hr->vcpu_token =swab32 (hr->vcpu_token); + hr->lpcr =swab64 (hr->lpcr); + hr->pcr =swab64 (hr->pcr); + hr->amor =swab64 (hr->amor); + hr->dpdes =swab64 (hr->dpdes); + hr->hfscr =swab64 (hr->hfscr); + hr->tb_offset =swab64 (hr->tb_offset); + hr->dawr0 =swab64 (hr->dawr0); + hr->dawrx0 =swab64 (hr->dawrx0); + hr->ciabr =swab64 (hr->ciabr); + hr->hdec_expiry =swab64 (hr->hdec_expiry); + hr->purr =swab64 (hr->purr); + hr->spurr =swab64 (hr->spurr); + hr->ic =swab64 (hr->ic); + hr->vtb =swab64 (hr->vtb); + hr->hdar =swab64 (hr->hdar); + hr->hdsisr =swab64 (hr->hdsisr); + hr->heir =swab64 (hr->heir); + hr->asdr =swab64 (hr->asdr); + hr->srr0 =swab64 (hr->srr0); + hr->srr1 =swab64 (hr->srr1); + hr->sprg[0 ] =swab64 (hr->sprg[0 ]); + hr->sprg[1 ] =swab64 (hr->sprg[1 ]); + hr->sprg[2 ] =swab64 (hr->sprg[2 ]); + hr->sprg[3 ] =swab64 (hr->sprg[3 ]); + hr->pidr =swab64 (hr->pidr); + hr->cfar =swab64 (hr->cfar); + hr->ppr =swab64 (hr->ppr); +} +static void save_hv_return_state (struct kvm_vcpu *vcpu,int trap,struct hv_guest_state *hr) {@@ -175,6 +217,8 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) sizeof(struct hv_guest_state));if (err)return H_PARAMETER ; +if (kvmppc_need_byteswap (vcpu)) +byteswap_hv_regs (&l2_hv);if (l2_hv.version !=HV_GUEST_STATE_VERSION )return H_P2 ;@@ -183,7 +227,8 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) sizeof(struct pt_regs));if (err)return H_PARAMETER ; - +if (kvmppc_need_byteswap (vcpu)) +byteswap_pt_regs (&l2_regs);if (l2_hv.vcpu_token >=NR_CPUS )return H_PARAMETER ;@@ -255,6 +300,10 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)kvmhv_put_nested (l2);/* copy l2_hv_state and regs back to guest */ +if (kvmppc_need_byteswap (vcpu)) { +byteswap_hv_regs (&l2_hv); +byteswap_pt_regs (&l2_regs); + } err =kvm_vcpu_write_guest (vcpu, hv_ptr, &l2_hv,sizeof (struct hv_guest_state ));if (err)
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH v3 29/33] KVM: PPC: Book3S HV: Handle differing endianness for H_ENTER_NESTED
Quoted 10 lines · David Gibson
Quoted 8 lines · Paul Mackerras
From: Suraj Jitindar Singh <[email protected]>
The hcall
H_ENTER_NESTEDtakes as the two parameters the address in L1 guest memory of ahv_regsstruct and apt_regsstruct which the L1 guest would like to use to run a L2 guest and in which are returned the exit state of the L2 guest. For efficiency, these are in the endianness of the L1 guest, rather than being always big-endian as is usually the case for PAPR hypercalls.
Quoted 3 lines
Does that actually make a difference for efficiency? I thought the presence of the byte-reversing loads and stores meant byteswapping was basically zero-cost on POWER.
It means that the L1 hypervisor can pass a pointer to the regs struct in the kvm_vcpu struct rather than having to make a copy, and copy the values back after the H_ENTER_NESTED. I'll reword the commit message to say it's mostly about convenience and to a lesser extent about performance.
Paul.
PATCH v3 30/33 KVM: PPC: Book3S HV: Allow HV module to load without hypervisor mode
In reply to Paul MackerrasWith this, the KVM-HV module can be loaded in a guest running under KVM-HV, and if the hypervisor supports nested virtualization, this guest can now act as a nested hypervisor and run nested guests.
This also adds some checks to inform userspace that HPT guests are not supported by nested hypervisors, and to prevent userspace from configuring a guest to use HPT mode.
--- arch/powerpc/kvm/book3s_hv.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
arch/powerpc/kvm/book3s_hv.c +16 -4
@@ -4237,6 +4237,10 @@ static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm, {struct kvm_ppc_one_seg_page_size *sps; +/* If we're a nested hypervisor, we only support radix guests */ +if (kvmhv_on_pseries ()) +return -EINVAL ; +/* * POWER7, POWER8 and POWER9 all support 32 storage keys for data. * POWER7 doesn't support keys for instruction accesses,
@@ -4822,11 +4826,15 @@ static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,static int kvmppc_core_check_processor_compat_hv (void ) { -if (!cpu_has_feature (CPU_FTR_HVMODE ) || - !cpu_has_feature (CPU_FTR_ARCH_206 )) -return -EIO ; +if (cpu_has_feature (CPU_FTR_HVMODE ) && +cpu_has_feature (CPU_FTR_ARCH_206 )) +return 0 ; -return 0 ; +/* Can run as nested hypervisor on POWER9 in radix mode. */ +if (cpu_has_feature (CPU_FTR_ARCH_300 ) &&radix_enabled ()) +return 0 ; + +return -EIO ; }#ifdef CONFIG_KVM_XICS
@@ -5144,6 +5152,10 @@ static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)if (radix && !radix_enabled ())return -EINVAL ; +/* If we're a nested hypervisor, we currently only support radix */ +if (kvmhv_on_pseries () && !radix) +return -EINVAL ; +mutex_lock (&kvm->lock);if (radix !=kvm_is_radix (kvm)) {if (kvm->arch.mmu_ready) {
Signature · 1 line
2.7.4
Re: [PATCH v3 30/33] KVM: PPC: Book3S HV: Allow HV module to load without hypervisor mode
Quoted 32 lines · Paul Mackerras
With this, the KVM-HV module can be loaded in a guest running under KVM-HV, and if the hypervisor supports nested virtualization, this guest can now act as a nested hypervisor and run nested guests.
This also adds some checks to inform userspace that HPT guests are not supported by nested hypervisors, and to prevent userspace from configuring a guest to use HPT mode.
Signed-off-by Paul Mackerras <[email protected]>--- arch/powerpc/kvm/book3s_hv.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)arch/powerpc/kvm/book3s_hv.c +4
-0@@ -4237,6 +4237,10 @@ static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm, {struct kvm_ppc_one_seg_page_size *sps; +/* If we're a nested hypervisor, we only support radix guests */ +if (kvmhv_on_pseries ()) +return -EINVAL ; +/* * POWER7, POWER8 and POWER9 all support 32 storage keys for data. * POWER7 doesn't support keys for instruction accesses, @@ -4822,11 +4826,15 @@ static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,static int kvmppc_core_check_processor_compat_hv (void ) {
arch/powerpc/kvm/book3s_hv.c static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn, · line 4834
Quoted 10 lines
arch/powerpc/kvm/book3s_hv.c +5
-4-if (!cpu_has_feature (CPU_FTR_HVMODE ) || - !cpu_has_feature (CPU_FTR_ARCH_206 )) -return -EIO ; +if (cpu_has_feature (CPU_FTR_HVMODE ) && +cpu_has_feature (CPU_FTR_ARCH_206 )) +return 0 ; -return 0 ; +/* Can run as nested hypervisor on POWER9 in radix mode. */ +if (cpu_has_feature (CPU_FTR_ARCH_300 ) &&radix_enabled ())
Shouldn't we probe the parent hypervisor for ability to support nested guests before we say "yes" here?
Quoted 17 lines
arch/powerpc/kvm/book3s_hv.c +7
-0+return 0 ; + +return -EIO ; }#ifdef CONFIG_KVM_XICS @@ -5144,6 +5152,10 @@ static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)if (radix && !radix_enabled ())return -EINVAL ; +/* If we're a nested hypervisor, we currently only support radix */ +if (kvmhv_on_pseries () && !radix) +return -EINVAL ; +mutex_lock (&kvm->lock);if (radix !=kvm_is_radix (kvm)) {if (kvm->arch.mmu_ready) {
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH v3 30/33] KVM: PPC: Book3S HV: Allow HV module to load without hypervisor mode
Quoted 40 lines · David Gibson
Quoted 39 lines · Paul Mackerras
With this, the KVM-HV module can be loaded in a guest running under KVM-HV, and if the hypervisor supports nested virtualization, this guest can now act as a nested hypervisor and run nested guests.
This also adds some checks to inform userspace that HPT guests are not supported by nested hypervisors, and to prevent userspace from configuring a guest to use HPT mode.
Signed-off-by Paul Mackerras <[email protected]>--- arch/powerpc/kvm/book3s_hv.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)arch/powerpc/kvm/book3s_hv.c +7
-3@@ -4237,6 +4237,10 @@ static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm, {struct kvm_ppc_one_seg_page_size *sps; +/* If we're a nested hypervisor, we only support radix guests */ +if (kvmhv_on_pseries ()) +return -EINVAL ; +/* * POWER7, POWER8 and POWER9 all support 32 storage keys for data. * POWER7 doesn't support keys for instruction accesses, @@ -4822,11 +4826,15 @@ static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,static int kvmppc_core_check_processor_compat_hv (void ) { -if (!cpu_has_feature (CPU_FTR_HVMODE ) || - !cpu_has_feature (CPU_FTR_ARCH_206 )) -return -EIO ; +if (cpu_has_feature (CPU_FTR_HVMODE ) && +cpu_has_feature (CPU_FTR_ARCH_206 )) +return 0 ;
Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn, · line 4834
Quoted 3 lines
arch/powerpc/kvm/book3s_hv.c +2
-1-return 0 ; +/* Can run as nested hypervisor on POWER9 in radix mode. */ +if (cpu_has_feature (CPU_FTR_ARCH_300 ) &&radix_enabled ())Shouldn't we probe the parent hypervisor for ability to support nested guests before we say "yes" here?
Well, we do check that the parent hypervisor can support nested hypervisors, it's just done later on. And to match nitpick with nitpick, this is a function evaluating _processor_ compatibility, and a POWER9 processor in radix mode does have everything necessary to support nested hypervisors -- if the parent hypervisor doesn't support nested hypervisors, that's not a deficiency in the processor.
Paul.
Re: [PATCH v3 30/33] KVM: PPC: Book3S HV: Allow HV module to load without hypervisor mode
Quoted 48 lines · Paul Mackerras
Quoted 40 lines · David Gibson
Quoted 39 lines · Paul Mackerras
With this, the KVM-HV module can be loaded in a guest running under KVM-HV, and if the hypervisor supports nested virtualization, this guest can now act as a nested hypervisor and run nested guests.
This also adds some checks to inform userspace that HPT guests are not supported by nested hypervisors, and to prevent userspace from configuring a guest to use HPT mode.
Signed-off-by Paul Mackerras <[email protected]>--- arch/powerpc/kvm/book3s_hv.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)arch/powerpc/kvm/book3s_hv.c +7
-3@@ -4237,6 +4237,10 @@ static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm, {struct kvm_ppc_one_seg_page_size *sps; +/* If we're a nested hypervisor, we only support radix guests */ +if (kvmhv_on_pseries ()) +return -EINVAL ; +/* * POWER7, POWER8 and POWER9 all support 32 storage keys for data. * POWER7 doesn't support keys for instruction accesses, @@ -4822,11 +4826,15 @@ static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,static int kvmppc_core_check_processor_compat_hv (void ) { -if (!cpu_has_feature (CPU_FTR_HVMODE ) || - !cpu_has_feature (CPU_FTR_ARCH_206 )) -return -EIO ; +if (cpu_has_feature (CPU_FTR_HVMODE ) && +cpu_has_feature (CPU_FTR_ARCH_206 )) +return 0 ;Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn, · line 4834
Quoted 3 lines
arch/powerpc/kvm/book3s_hv.c +2
-1-return 0 ; +/* Can run as nested hypervisor on POWER9 in radix mode. */ +if (cpu_has_feature (CPU_FTR_ARCH_300 ) &&radix_enabled ())Shouldn't we probe the parent hypervisor for ability to support nested guests before we say "yes" here?
Quoted 6 lines
Well, we do check that the parent hypervisor can support nested hypervisors, it's just done later on. And to match nitpick with nitpick, this is a function evaluating _processor_ compatibility, and a POWER9 processor in radix mode does have everything necessary to support nested hypervisors -- if the parent hypervisor doesn't support nested hypervisors, that's not a deficiency in the processor.
Fair enough.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v3 31/33 KVM: PPC: Book3S HV: Add nested shadow page tables to debugfs
In reply to Paul MackerrasThis adds a list of valid shadow PTEs for each nested guest to the 'radix' file for the guest in debugfs. This can be useful for debugging.
--- arch/powerpc/include/asm/kvm_book3s_64.h | 1 + arch/powerpc/kvm/book3s_64_mmu_radix.c | 39 +++++++++++++++++++++++++++++--- arch/powerpc/kvm/book3s_hv_nested.c | 15 ++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/kvm_book3s_64.h +1 -0
@@ -119,6 +119,7 @@ struct rmap_nested {struct kvm_nested_guest *kvmhv_get_nested (struct kvm *kvm,int l1_lpid,bool create);void kvmhv_put_nested (struct kvm_nested_guest *gp); +int kvmhv_nested_next_lpid (struct kvm *kvm,int lpid);/* Power architecture requires HPT is at least 256kiB, at most 64TiB */ #define PPC_MIN_HPT_ORDER 18
arch/powerpc/kvm/book3s_64_mmu_radix.c +36 -3
@@ -978,6 +978,7 @@ struct debugfs_radix_state {struct kvm *kvm;struct mutex mutex;unsigned long gpa; +int lpid;int chars_left;int buf_index;char buf[128 ];
@@ -1019,6 +1020,7 @@ static ssize_t debugfs_radix_read(struct file *file, char __user *buf,struct kvm *kvm;unsigned long gpa;pgd_t *pgt; +struct kvm_nested_guest *nested;pgd_t pgd, *pgdp;pud_t pud, *pudp;pmd_t pmd, *pmdp;
@@ -1053,10 +1055,39 @@ static ssize_t debugfs_radix_read(struct file *file, char __user *buf, } gpa = p->gpa; - pgt = kvm->arch.pgtable; -while (len !=0 && gpa <RADIX_PGTABLE_RANGE ) { + nested =NULL ; + pgt =NULL ; +while (len !=0 && p->lpid >=0 ) { +if (gpa >=RADIX_PGTABLE_RANGE ) { + gpa =0 ; + pgt =NULL ; +if (nested) { +kvmhv_put_nested (nested); + nested =NULL ; + } + p->lpid =kvmhv_nested_next_lpid (kvm, p->lpid); + p->hdr =0 ; +if (p->lpid <0 ) +break ; + } +if (!pgt) { +if (p->lpid ==0 ) { + pgt = kvm->arch.pgtable; + }else { + nested =kvmhv_get_nested (kvm, p->lpid, false); +if (!nested) { + gpa =RADIX_PGTABLE_RANGE ; +continue ; + } + pgt = nested->shadow_pgtable; + } + } + n =0 ;if (!p->hdr) { - n =scnprintf (p->buf,sizeof (p->buf), +if (p->lpid >0 ) + n =scnprintf (p->buf,sizeof (p->buf), +"\nNested LPID %d: " , p->lpid); + n +=scnprintf (p->buf + n,sizeof (p->buf) - n,"pgdir: %lx\n" , (unsigned long )pgt); p->hdr =1 ; goto copy;
@@ -1122,6 +1153,8 @@ static ssize_t debugfs_radix_read(struct file *file, char __user *buf, } } p->gpa = gpa; +if (nested) +kvmhv_put_nested (nested); out:mutex_unlock (&p->mutex);
arch/powerpc/kvm/book3s_hv_nested.c +15 -0
@@ -1301,3 +1301,18 @@ long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu)mutex_unlock (&gp->tlb_lock);return ret; } + +int kvmhv_nested_next_lpid (struct kvm *kvm,int lpid) +{ +int ret =-1 ; + +spin_lock (&kvm->mmu_lock); +while (++lpid <= kvm->arch.max_nested_lpid) { +if (kvm->arch.nested_guests[lpid]) { + ret = lpid; +break ; + } + } +spin_unlock (&kvm->mmu_lock); +return ret; +}
Signature · 1 line
2.7.4
PATCH v3 32/33 KVM: PPC: Book3S HV: Use hypercalls for TLB invalidation when nested
In reply to Paul MackerrasThis adds code to call the H_TLB_INVALIDATE hypercall when running as a guest, in the cases where we need to invalidate TLBs (or other MMU caches) as part of managing the mappings for a nested guest. Calling H_TLB_INVALIDATE is an alternative to doing the tlbie instruction and having it be emulated by our hypervisor.
--- arch/powerpc/include/asm/kvm_book3s_64.h | 5 +++++ arch/powerpc/kvm/book3s_64_mmu_radix.c | 30 +++++++++++++++++++++++++++-- arch/powerpc/kvm/book3s_hv_nested.c | 33 +++++++++++++++++++++++--------- 3 files changed, 57 insertions(+), 11 deletions(-)
arch/powerpc/include/asm/kvm_book3s_64.h +5 -0
@@ -24,6 +24,7 @@#include <asm/bitops.h> #include <asm/book3s/64/mmu-hash.h> #include <asm/cpu_has_feature.h> +#include <asm/ppc-opcode.h> #ifdef CONFIG_PPC_PSERIES static inline bool kvmhv_on_pseries (void )
@@ -121,6 +122,10 @@ struct kvm_nested_guest *kvmhv_get_nested(struct kvm *kvm, int l1_lpid,void kvmhv_put_nested (struct kvm_nested_guest *gp);int kvmhv_nested_next_lpid (struct kvm *kvm,int lpid); +/* Encoding of first parameter for H_TLB_INVALIDATE */ +#define H_TLBIE_P1_ENC (ric, prs, r) (___PPC_RIC(ric) | ___PPC_PRS(prs) | \ + ___PPC_R(r)) +/* Power architecture requires HPT is at least 256kiB, at most 64TiB */ #define PPC_MIN_HPT_ORDER 18#define PPC_MAX_HPT_ORDER 46
arch/powerpc/kvm/book3s_64_mmu_radix.c +28 -2
@@ -201,17 +201,43 @@ static void kvmppc_radix_tlbie_page(struct kvm *kvm, unsigned long addr, unsigned int pshift, unsigned int lpid) {unsigned long psize =PAGE_SIZE ; +int psi; +long rc; +unsigned long rb;if (pshift) psize =1UL << pshift; +else + pshift =PAGE_SHIFT ; addr &= ~(psize -1 ); -radix__flush_tlb_lpid_page (lpid, addr, psize); + +if (!kvmhv_on_pseries ()) { +radix__flush_tlb_lpid_page (lpid, addr, psize); +return ; + } + + psi =shift_to_mmu_psize (pshift); + rb = addr | (mmu_get_ap (psi) <<PPC_BITLSHIFT (58 )); + rc =plpar_hcall_norets (H_TLB_INVALIDATE ,H_TLBIE_P1_ENC (0 ,0 ,1 ), + lpid, rb); +if (rc) +pr_err ("KVM: TLB page invalidation hcall failed, rc=%ld\n" , rc); }static void kvmppc_radix_flush_pwc (struct kvm *kvm,unsigned int lpid) { -radix__flush_pwc_lpid (lpid); +long rc; + +if (!kvmhv_on_pseries ()) { +radix__flush_pwc_lpid (lpid); +return ; + } + + rc =plpar_hcall_norets (H_TLB_INVALIDATE ,H_TLBIE_P1_ENC (1 ,0 ,1 ), + lpid,TLBIEL_INVAL_SET_LPID ); +if (rc) +pr_err ("KVM: TLB PWC invalidation hcall failed, rc=%ld\n" , rc); } static unsigned long kvmppc_radix_update_pte(struct kvm *kvm, pte_t *ptep,
arch/powerpc/kvm/book3s_hv_nested.c +24 -9
@@ -368,17 +368,32 @@ void kvmhv_nested_exit(void) } } +static void kvmhv_flush_lpid (unsigned int lpid) +{ +long rc; + +if (!kvmhv_on_pseries ()) { +radix__flush_tlb_lpid (lpid); +return ; + } + + rc =plpar_hcall_norets (H_TLB_INVALIDATE ,H_TLBIE_P1_ENC (2 ,0 ,1 ), + lpid,TLBIEL_INVAL_SET_LPID ); +if (rc) +pr_err ("KVM: TLB LPID invalidation hcall failed, rc=%ld\n" , rc); +} +void kvmhv_set_ptbl_entry (unsigned int lpid,u64 dw0,u64 dw1) { -if (cpu_has_feature (CPU_FTR_HVMODE )) { +if (!kvmhv_on_pseries ()) {mmu_partition_table_set_entry (lpid, dw0, dw1); - }else { - pseries_partition_tb[lpid].patb0 =cpu_to_be64 (dw0); - pseries_partition_tb[lpid].patb1 =cpu_to_be64 (dw1); -/* this will be emulated, L0 will do the necessary barriers */ - asm volatile(PPC_TLBIE_5(%0, %1, 2, 0, 1) : : - "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid)); +return ; } + + pseries_partition_tb[lpid].patb0 =cpu_to_be64 (dw0); + pseries_partition_tb[lpid].patb1 =cpu_to_be64 (dw1); +/* L0 will do the necessary barriers */ +kvmhv_flush_lpid (lpid); }static void kvmhv_set_nested_ptbl (struct kvm_nested_guest *gp)
@@ -543,7 +558,7 @@ static void kvmhv_flush_nested(struct kvm_nested_guest *gp)spin_lock (&kvm->mmu_lock);kvmppc_free_pgtable_radix (kvm, gp->shadow_pgtable, gp->shadow_lpid);spin_unlock (&kvm->mmu_lock); -radix__flush_tlb_lpid (gp->shadow_lpid); +kvmhv_flush_lpid (gp->shadow_lpid);kvmhv_update_ptbl_cache (gp);if (gp->l1_gr_to_hr ==0 )kvmhv_remove_nested (gp);
@@ -839,7 +854,7 @@ static void kvmhv_emulate_tlbie_lpid(struct kvm_vcpu *vcpu,spin_lock (&kvm->mmu_lock);kvmppc_free_pgtable_radix (kvm, gp->shadow_pgtable, gp->shadow_lpid); -radix__flush_tlb_lpid (gp->shadow_lpid); +kvmhv_flush_lpid (gp->shadow_lpid);spin_unlock (&kvm->mmu_lock);break ;case 1 :
Signature · 1 line
2.7.4
Re: [PATCH v3 32/33] KVM: PPC: Book3S HV: Use hypercalls for TLB invalidation when nested
Quoted 5 lines · Paul Mackerras
This adds code to call the
H_TLB_INVALIDATEhypercall when running as a guest, in the cases where we need to invalidate TLBs (or other MMU caches) as part of managing the mappings for a nested guest. CallingH_TLB_INVALIDATEis an alternative to doing the tlbie instruction and having it be emulated by our hypervisor.
Why is supporting two methods useful, rather than just settling on one? Having both just sounds like asking for untested and therefore broken code paths lying around.
Quoted 142 lines
Signed-off-by Paul Mackerras <[email protected]>--- arch/powerpc/include/asm/kvm_book3s_64.h | 5 +++++ arch/powerpc/kvm/book3s_64_mmu_radix.c | 30 +++++++++++++++++++++++++++-- arch/powerpc/kvm/book3s_hv_nested.c | 33 +++++++++++++++++++++++--------- 3 files changed, 57 insertions(+), 11 deletions(-)arch/powerpc/include/asm/kvm_book3s_64.h +5
-0@@ -24,6 +24,7 @@#include <asm/bitops.h> #include <asm/book3s/64/mmu-hash.h> #include <asm/cpu_has_feature.h> +#include <asm/ppc-opcode.h> #ifdef CONFIG_PPC_PSERIES static inline bool kvmhv_on_pseries (void )@@ -121,6 +122,10 @@ struct kvm_nested_guest *kvmhv_get_nested(struct kvm *kvm, int l1_lpid,void kvmhv_put_nested (struct kvm_nested_guest *gp);int kvmhv_nested_next_lpid (struct kvm *kvm,int lpid); +/* Encoding of first parameter for H_TLB_INVALIDATE */ +#define H_TLBIE_P1_ENC (ric, prs, r) (___PPC_RIC(ric) | ___PPC_PRS(prs) | \ + ___PPC_R(r)) +/* Power architecture requires HPT is at least 256kiB, at most 64TiB */ #define PPC_MIN_HPT_ORDER 18#define PPC_MAX_HPT_ORDER 46arch/powerpc/kvm/book3s_64_mmu_radix.c +28
-2@@ -201,17 +201,43 @@ static void kvmppc_radix_tlbie_page(struct kvm *kvm, unsigned long addr, unsigned int pshift, unsigned int lpid) {unsigned long psize =PAGE_SIZE ; +int psi; +long rc; +unsigned long rb;if (pshift) psize =1UL << pshift; +else + pshift =PAGE_SHIFT ; addr &= ~(psize -1 ); -radix__flush_tlb_lpid_page (lpid, addr, psize); + +if (!kvmhv_on_pseries ()) { +radix__flush_tlb_lpid_page (lpid, addr, psize); +return ; + } + + psi =shift_to_mmu_psize (pshift); + rb = addr | (mmu_get_ap (psi) <<PPC_BITLSHIFT (58 )); + rc =plpar_hcall_norets (H_TLB_INVALIDATE ,H_TLBIE_P1_ENC (0 ,0 ,1 ), + lpid, rb); +if (rc) +pr_err ("KVM: TLB page invalidation hcall failed, rc=%ld\n" , rc); }static void kvmppc_radix_flush_pwc (struct kvm *kvm,unsigned int lpid) { -radix__flush_pwc_lpid (lpid); +long rc; + +if (!kvmhv_on_pseries ()) { +radix__flush_pwc_lpid (lpid); +return ; + } + + rc =plpar_hcall_norets (H_TLB_INVALIDATE ,H_TLBIE_P1_ENC (1 ,0 ,1 ), + lpid,TLBIEL_INVAL_SET_LPID ); +if (rc) +pr_err ("KVM: TLB PWC invalidation hcall failed, rc=%ld\n" , rc); } static unsigned long kvmppc_radix_update_pte(struct kvm *kvm, pte_t *ptep,arch/powerpc/kvm/book3s_hv_nested.c +24
-9@@ -368,17 +368,32 @@ void kvmhv_nested_exit(void) } } +static void kvmhv_flush_lpid (unsigned int lpid) +{ +long rc; + +if (!kvmhv_on_pseries ()) { +radix__flush_tlb_lpid (lpid); +return ; + } + + rc =plpar_hcall_norets (H_TLB_INVALIDATE ,H_TLBIE_P1_ENC (2 ,0 ,1 ), + lpid,TLBIEL_INVAL_SET_LPID ); +if (rc) +pr_err ("KVM: TLB LPID invalidation hcall failed, rc=%ld\n" , rc); +} +void kvmhv_set_ptbl_entry (unsigned int lpid,u64 dw0,u64 dw1) { -if (cpu_has_feature (CPU_FTR_HVMODE )) { +if (!kvmhv_on_pseries ()) {mmu_partition_table_set_entry (lpid, dw0, dw1); - }else { - pseries_partition_tb[lpid].patb0 =cpu_to_be64 (dw0); - pseries_partition_tb[lpid].patb1 =cpu_to_be64 (dw1); -/* this will be emulated, L0 will do the necessary barriers */ - asm volatile(PPC_TLBIE_5(%0, %1, 2, 0, 1) : : - "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid)); +return ; } + + pseries_partition_tb[lpid].patb0 =cpu_to_be64 (dw0); + pseries_partition_tb[lpid].patb1 =cpu_to_be64 (dw1); +/* L0 will do the necessary barriers */ +kvmhv_flush_lpid (lpid); }static void kvmhv_set_nested_ptbl (struct kvm_nested_guest *gp)@@ -543,7 +558,7 @@ static void kvmhv_flush_nested(struct kvm_nested_guest *gp)spin_lock (&kvm->mmu_lock);kvmppc_free_pgtable_radix (kvm, gp->shadow_pgtable, gp->shadow_lpid);spin_unlock (&kvm->mmu_lock); -radix__flush_tlb_lpid (gp->shadow_lpid); +kvmhv_flush_lpid (gp->shadow_lpid);kvmhv_update_ptbl_cache (gp);if (gp->l1_gr_to_hr ==0 )kvmhv_remove_nested (gp);@@ -839,7 +854,7 @@ static void kvmhv_emulate_tlbie_lpid(struct kvm_vcpu *vcpu,spin_lock (&kvm->mmu_lock);kvmppc_free_pgtable_radix (kvm, gp->shadow_pgtable, gp->shadow_lpid); -radix__flush_tlb_lpid (gp->shadow_lpid); +kvmhv_flush_lpid (gp->shadow_lpid);spin_unlock (&kvm->mmu_lock);break ;case 1 :
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v3 33/33 KVM: PPC: Book3S HV: Add a VM capability to enable nested virtualization
In reply to Paul MackerrasWith this, userspace can enable a KVM-HV guest to run nested guests under it.
--- Documentation/virtual/kvm/api.txt | 14 ++++++++++++++ arch/powerpc/include/asm/kvm_ppc.h | 1 + arch/powerpc/kvm/book3s_hv.c | 17 +++++++++++++++++ arch/powerpc/kvm/powerpc.c | 12 ++++++++++++ include/uapi/linux/kvm.h | 1 + 5 files changed, 45 insertions(+)
Documentation/virtual/kvm/api.txt +14 -0
@@ -4522,6 +4522,20 @@ hpage module parameter is not set to 1, -EINVAL is returned. While it is generally possible to create a huge page backed VM without this capability, the VM will not be able to run. +7.15 KVM_CAP_PPC_NESTED_HV + +Architectures: ppc +Parameters: enable flag (0 to disable, non-zero to enable) +Returns: 0 on success, -EINVAL when the implementation doesn't support +nested-HV virtualization. + +HV-KVM on POWER9 and later systems allows for "nested-HV" +virtualization, which provides a way for a guest VM to run guests that +can run using the CPU's supervisor mode (privileged non-hypervisor +state). Enabling this capability on a VM depends on the CPU having +the necessary functionality and on the facility being enabled with a +kvm-hv module parameter. + 8. Other capabilities. ----------------------
arch/powerpc/include/asm/kvm_ppc.h +1 -0
@@ -327,6 +327,7 @@ struct kvmppc_ops {int (*set_smt_mode)(struct kvm *kvm,unsigned long mode,unsigned long flags);void (*giveup_ext)(struct kvm_vcpu *vcpu,ulong msr); +int (*enable_nested)(struct kvm *kvm,bool enable); };extern struct kvmppc_ops *kvmppc_hv_ops;
arch/powerpc/kvm/book3s_hv.c +17 -0
@@ -118,6 +118,11 @@ module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644);MODULE_PARM_DESC (h_ipi_redirect,"Redirect H_IPI wakeup to a free host core" );#endif +/* If set, guests are allowed to create and control nested guests */ +static bool enable_nested = true; +module_param (enable_nested, bool,S_IRUGO |S_IWUSR ); +MODULE_PARM_DESC (enable_nested,"Enable nested virtualization (only on POWER9)" ); +/* If set, the threads on each CPU core have to be in the same MMU mode */ static bool no_mixing_hpt_and_radix;
@@ -5188,6 +5193,17 @@ static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)return err; } +static int kvmhv_enable_nested (struct kvm *kvm,bool enable) +{ +if (!(enable_nested &&cpu_has_feature (CPU_FTR_ARCH_300 ))) +return -EINVAL ; + +/* kvm == NULL means the caller is testing if the capability exists */ +if (kvm) + kvm->arch.nested_enable = enable; +return 0 ; +} +static struct kvmppc_ops kvm_ops_hv = { .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
@@ -5227,6 +5243,7 @@ static struct kvmppc_ops kvm_ops_hv = { .configure_mmu = kvmhv_configure_mmu, .get_rmmu_info = kvmhv_get_rmmu_info, .set_smt_mode = kvmhv_set_smt_mode, + .enable_nested = kvmhv_enable_nested, };static int kvm_init_subcore_bitmap (void )
arch/powerpc/kvm/powerpc.c +12 -0
@@ -596,6 +596,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_PPC_MMU_HASH_V3 : r = !!(hv_enabled &&cpu_has_feature (CPU_FTR_ARCH_300 ));break ; +case KVM_CAP_PPC_NESTED_HV : + r = !!(hv_enabled && kvmppc_hv_ops->enable_nested && + !kvmppc_hv_ops->enable_nested (NULL , false)); +break ;#endif case KVM_CAP_SYNC_MMU :#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
@@ -2114,6 +2118,14 @@ static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r = kvm->arch.kvm_ops->set_smt_mode (kvm, mode, flags); break; } + +case KVM_CAP_PPC_NESTED_HV : + r = -EINVAL ; +if (!is_kvmppc_hv_enabled (kvm) || + !kvm->arch.kvm_ops->enable_nested) +break ; + r = kvm->arch.kvm_ops->enable_nested (kvm, !!cap->args[0 ]); +break ;#endif default : r = -EINVAL ;
include/uapi/linux/kvm.h +1 -0
@@ -952,6 +952,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_S390_HPAGE_1M 156#define KVM_CAP_NESTED_STATE 157#define KVM_CAP_ARM_INJECT_SERROR_ESR 158 +#define KVM_CAP_PPC_NESTED_HV 160#ifdef KVM_CAP_IRQ_ROUTING
Signature · 1 line
2.7.4
Re: [PATCH v3 33/33] KVM: PPC: Book3S HV: Add a VM capability to enable nested virtualization
Quoted 54 lines · Paul Mackerras
With this, userspace can enable a KVM-HV guest to run nested guests under it.
Signed-off-by Paul Mackerras <[email protected]>--- Documentation/virtual/kvm/api.txt | 14 ++++++++++++++ arch/powerpc/include/asm/kvm_ppc.h | 1 + arch/powerpc/kvm/book3s_hv.c | 17 +++++++++++++++++ arch/powerpc/kvm/powerpc.c | 12 ++++++++++++ include/uapi/linux/kvm.h | 1 + 5 files changed, 45 insertions(+)Documentation/virtual/kvm/api.txt +14
-0@@ -4522,6 +4522,20 @@ hpage module parameter is not set to 1, -EINVAL is returned. While it is generally possible to create a huge page backed VM without this capability, the VM will not be able to run. +7.15 KVM_CAP_PPC_NESTED_HV + +Architectures: ppc +Parameters: enable flag (0 to disable, non-zero to enable) +Returns: 0 on success, -EINVAL when the implementation doesn't support +nested-HV virtualization. + +HV-KVM on POWER9 and later systems allows for "nested-HV" +virtualization, which provides a way for a guest VM to run guests that +can run using the CPU's supervisor mode (privileged non-hypervisor +state). Enabling this capability on a VM depends on the CPU having +the necessary functionality and on the facility being enabled with a +kvm-hv module parameter. + 8. Other capabilities. ----------------------arch/powerpc/include/asm/kvm_ppc.h +1
-0@@ -327,6 +327,7 @@ struct kvmppc_ops {int (*set_smt_mode)(struct kvm *kvm,unsigned long mode,unsigned long flags);void (*giveup_ext)(struct kvm_vcpu *vcpu,ulong msr); +int (*enable_nested)(struct kvm *kvm,bool enable); };extern struct kvmppc_ops *kvmppc_hv_ops;arch/powerpc/kvm/book3s_hv.c +0
-0
arch/powerpc/kvm/book3s_hv.c module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644); · line 124
Quoted 7 lines
arch/powerpc/kvm/book3s_hv.c +4
-0@@ -118,6 +118,11 @@ module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644);MODULE_PARM_DESC (h_ipi_redirect,"Redirect H_IPI wakeup to a free host core" );#endif +/* If set, guests are allowed to create and control nested guests */ +static bool enable_nested = true; +module_param (enable_nested, bool,S_IRUGO |S_IWUSR ); +MODULE_PARM_DESC (enable_nested,"Enable nested virtualization (only on POWER9)" );
I'd suggest calling the module parameter just "nested" to match x86.
arch/powerpc/kvm/book3s_hv.c static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg) · line 5199
Quoted 11 lines
arch/powerpc/kvm/book3s_hv.c +4
-0/* If set, the threads on each CPU core have to be in the same MMU mode */ static bool no_mixing_hpt_and_radix;@@ -5188,6 +5193,17 @@ static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)return err; } +static int kvmhv_enable_nested (struct kvm *kvm,bool enable) +{ +if (!(enable_nested &&cpu_has_feature (CPU_FTR_ARCH_300 ))) +return -EINVAL ;
Maybe EPERM, rather than EINVAL. There's nothing invalid about the ioctl() parameters - we just can't do what they want.
Quoted 60 lines
arch/powerpc/kvm/book3s_hv.c +8
-0+ +/* kvm == NULL means the caller is testing if the capability exists */ +if (kvm) + kvm->arch.nested_enable = enable; +return 0 ; +} +static struct kvmppc_ops kvm_ops_hv = { .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,@@ -5227,6 +5243,7 @@ static struct kvmppc_ops kvm_ops_hv = { .configure_mmu = kvmhv_configure_mmu, .get_rmmu_info = kvmhv_get_rmmu_info, .set_smt_mode = kvmhv_set_smt_mode, + .enable_nested = kvmhv_enable_nested, };static int kvm_init_subcore_bitmap (void )arch/powerpc/kvm/powerpc.c +12
-0@@ -596,6 +596,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_PPC_MMU_HASH_V3 : r = !!(hv_enabled &&cpu_has_feature (CPU_FTR_ARCH_300 ));break ; +case KVM_CAP_PPC_NESTED_HV : + r = !!(hv_enabled && kvmppc_hv_ops->enable_nested && + !kvmppc_hv_ops->enable_nested (NULL , false)); +break ;#endif case KVM_CAP_SYNC_MMU :#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE @@ -2114,6 +2118,14 @@ static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r = kvm->arch.kvm_ops->set_smt_mode (kvm, mode, flags); break; } + +case KVM_CAP_PPC_NESTED_HV : + r = -EINVAL ; +if (!is_kvmppc_hv_enabled (kvm) || + !kvm->arch.kvm_ops->enable_nested) +break ; + r = kvm->arch.kvm_ops->enable_nested (kvm, !!cap->args[0 ]); +break ;#endif default : r = -EINVAL ;include/uapi/linux/kvm.h +1
-0@@ -952,6 +952,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_S390_HPAGE_1M 156#define KVM_CAP_NESTED_STATE 157#define KVM_CAP_ARM_INJECT_SERROR_ESR 158 +#define KVM_CAP_PPC_NESTED_HV 160#ifdef KVM_CAP_IRQ_ROUTING
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH v3 33/33] KVM: PPC: Book3S HV: Add a VM capability to enable nested virtualization
Quoted 3 lines · David Gibson
Quoted 2 lines · Paul Mackerras
With this, userspace can enable a KVM-HV guest to run nested guests under it.
[snip]
Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644); · line 124
Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +4
-0+/* If set, guests are allowed to create and control nested guests */ +static bool enable_nested = true; +module_param (enable_nested, bool,S_IRUGO |S_IWUSR ); +MODULE_PARM_DESC (enable_nested,"Enable nested virtualization (only on POWER9)" );I'd suggest calling the module parameter just "nested" to match x86.
OK.
Quoted 7 lines
Quoted 7 lines
/* If set, the threads on each CPU core have to be in the same MMU mode */ static bool
no_mixing_hpt_and_radix;arch/powerpc/kvm/book3s_hv.c +0
-0@@ -5188,6 +5193,17 @@ static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)return err; }
Quoted 7 lines
arch/powerpc/kvm/book3s_hv.c static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg) · line 5199
Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +4
-0+static int kvmhv_enable_nested (struct kvm *kvm,bool enable) +{ +if (!(enable_nested &&cpu_has_feature (CPU_FTR_ARCH_300 ))) +return -EINVAL ;Maybe EPERM, rather than EINVAL. There's nothing invalid about the
ioctl()parameters - we just can't do what they want.
Just for pedantry's sake, I'll make it EPERM for !enable_nested and ENODEV for !POWER9. :)
Paul.
Re: [PATCH v3 33/33] KVM: PPC: Book3S HV: Add a VM capability to enable nested virtualization
Quoted 29 lines · Paul Mackerras
Quoted 3 lines · David Gibson
Quoted 2 lines · Paul Mackerras
With this, userspace can enable a KVM-HV guest to run nested guests under it.
[snip]
Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644); · line 124
Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +4
-0+/* If set, guests are allowed to create and control nested guests */ +static bool enable_nested = true; +module_param (enable_nested, bool,S_IRUGO |S_IWUSR ); +MODULE_PARM_DESC (enable_nested,"Enable nested virtualization (only on POWER9)" );I'd suggest calling the module parameter just "nested" to match x86.
OK.
Quoted 7 lines
Quoted 7 lines
/* If set, the threads on each CPU core have to be in the same MMU mode */ static bool
no_mixing_hpt_and_radix;arch/powerpc/kvm/book3s_hv.c +0
-0@@ -5188,6 +5193,17 @@ static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)return err; }Quoted 7 lines
arch/powerpc/kvm/book3s_hv.c static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg) · line 5199
Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +4
-0+static int kvmhv_enable_nested (struct kvm *kvm,bool enable) +{ +if (!(enable_nested &&cpu_has_feature (CPU_FTR_ARCH_300 ))) +return -EINVAL ;Maybe EPERM, rather than EINVAL. There's nothing invalid about the
ioctl()parameters - we just can't do what they want.
Quoted 2 lines
Just for pedantry's sake, I'll make it EPERM for !enable_nested and ENODEV for !POWER9. :)
Sounds fair.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson