aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Mackerras <paulus@ozlabs.org>2018-09-21 06:02:01 -0400
committerPaul Mackerras <paulus@ozlabs.org>2018-10-09 01:14:47 -0400
commitaa069a996951f3e2e38437ef0316685a5893fc7e (patch)
tree40de54d39eeb75cb318b221a3d497a032ca72ab4
parent9d67121a4fce20e0f7f127d40cd824fbbf5297dc (diff)
KVM: PPC: Book3S HV: Add a VM capability to enable nested virtualization
With this, userspace can enable a KVM-HV guest to run nested guests under it. The administrator can control whether any nested guests can be run; setting the "nested" module parameter to false prevents any guests becoming nested hypervisors (that is, any attempt to enable the nested capability on a guest will fail). Guests which are already nested hypervisors will continue to be so. Reviewed-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
-rw-r--r--Documentation/virtual/kvm/api.txt14
-rw-r--r--arch/powerpc/include/asm/kvm_ppc.h1
-rw-r--r--arch/powerpc/kvm/book3s_hv.c39
-rw-r--r--arch/powerpc/kvm/powerpc.c12
-rw-r--r--include/uapi/linux/kvm.h1
5 files changed, 58 insertions, 9 deletions
diff --git a/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt
index 2f5f9b743bff..fde48b6708f1 100644
--- a/Documentation/virtual/kvm/api.txt
+++ b/Documentation/virtual/kvm/api.txt
@@ -4532,6 +4532,20 @@ With this capability, a guest may read the MSR_PLATFORM_INFO MSR. Otherwise,
4532a #GP would be raised when the guest tries to access. Currently, this 4532a #GP would be raised when the guest tries to access. Currently, this
4533capability does not enable write permissions of this MSR for the guest. 4533capability does not enable write permissions of this MSR for the guest.
4534 4534
45357.16 KVM_CAP_PPC_NESTED_HV
4536
4537Architectures: ppc
4538Parameters: none
4539Returns: 0 on success, -EINVAL when the implementation doesn't support
4540 nested-HV virtualization.
4541
4542HV-KVM on POWER9 and later systems allows for "nested-HV"
4543virtualization, which provides a way for a guest VM to run guests that
4544can run using the CPU's supervisor mode (privileged non-hypervisor
4545state). Enabling this capability on a VM depends on the CPU having
4546the necessary functionality and on the facility being enabled with a
4547kvm-hv module parameter.
4548
45358. Other capabilities. 45498. Other capabilities.
4536---------------------- 4550----------------------
4537 4551
diff --git a/arch/powerpc/include/asm/kvm_ppc.h b/arch/powerpc/include/asm/kvm_ppc.h
index 88362ccda549..9b89b1918dfc 100644
--- a/arch/powerpc/include/asm/kvm_ppc.h
+++ b/arch/powerpc/include/asm/kvm_ppc.h
@@ -325,6 +325,7 @@ struct kvmppc_ops {
325 int (*set_smt_mode)(struct kvm *kvm, unsigned long mode, 325 int (*set_smt_mode)(struct kvm *kvm, unsigned long mode,
326 unsigned long flags); 326 unsigned long flags);
327 void (*giveup_ext)(struct kvm_vcpu *vcpu, ulong msr); 327 void (*giveup_ext)(struct kvm_vcpu *vcpu, ulong msr);
328 int (*enable_nested)(struct kvm *kvm);
328}; 329};
329 330
330extern struct kvmppc_ops *kvmppc_hv_ops; 331extern struct kvmppc_ops *kvmppc_hv_ops;
diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
index f3cdf51d0191..89bcf923d542 100644
--- a/arch/powerpc/kvm/book3s_hv.c
+++ b/arch/powerpc/kvm/book3s_hv.c
@@ -122,6 +122,16 @@ module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644);
122MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core"); 122MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
123#endif 123#endif
124 124
125/* If set, guests are allowed to create and control nested guests */
126static bool nested = true;
127module_param(nested, bool, S_IRUGO | S_IWUSR);
128MODULE_PARM_DESC(nested, "Enable nested virtualization (only on POWER9)");
129
130static inline bool nesting_enabled(struct kvm *kvm)
131{
132 return kvm->arch.nested_enable && kvm_is_radix(kvm);
133}
134
125/* If set, the threads on each CPU core have to be in the same MMU mode */ 135/* If set, the threads on each CPU core have to be in the same MMU mode */
126static bool no_mixing_hpt_and_radix; 136static bool no_mixing_hpt_and_radix;
127 137
@@ -963,12 +973,12 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
963 973
964 case H_SET_PARTITION_TABLE: 974 case H_SET_PARTITION_TABLE:
965 ret = H_FUNCTION; 975 ret = H_FUNCTION;
966 if (vcpu->kvm->arch.nested_enable) 976 if (nesting_enabled(vcpu->kvm))
967 ret = kvmhv_set_partition_table(vcpu); 977 ret = kvmhv_set_partition_table(vcpu);
968 break; 978 break;
969 case H_ENTER_NESTED: 979 case H_ENTER_NESTED:
970 ret = H_FUNCTION; 980 ret = H_FUNCTION;
971 if (!vcpu->kvm->arch.nested_enable) 981 if (!nesting_enabled(vcpu->kvm))
972 break; 982 break;
973 ret = kvmhv_enter_nested_guest(vcpu); 983 ret = kvmhv_enter_nested_guest(vcpu);
974 if (ret == H_INTERRUPT) { 984 if (ret == H_INTERRUPT) {
@@ -978,9 +988,8 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
978 break; 988 break;
979 case H_TLB_INVALIDATE: 989 case H_TLB_INVALIDATE:
980 ret = H_FUNCTION; 990 ret = H_FUNCTION;
981 if (!vcpu->kvm->arch.nested_enable) 991 if (nesting_enabled(vcpu->kvm))
982 break; 992 ret = kvmhv_do_nested_tlbie(vcpu);
983 ret = kvmhv_do_nested_tlbie(vcpu);
984 break; 993 break;
985 994
986 default: 995 default:
@@ -4508,10 +4517,8 @@ static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
4508/* Must be called with kvm->lock held and mmu_ready = 0 and no vcpus running */ 4517/* Must be called with kvm->lock held and mmu_ready = 0 and no vcpus running */
4509int kvmppc_switch_mmu_to_hpt(struct kvm *kvm) 4518int kvmppc_switch_mmu_to_hpt(struct kvm *kvm)
4510{ 4519{
4511 if (kvm->arch.nested_enable) { 4520 if (nesting_enabled(kvm))
4512 kvm->arch.nested_enable = false;
4513 kvmhv_release_all_nested(kvm); 4521 kvmhv_release_all_nested(kvm);
4514 }
4515 kvmppc_free_radix(kvm); 4522 kvmppc_free_radix(kvm);
4516 kvmppc_update_lpcr(kvm, LPCR_VPM1, 4523 kvmppc_update_lpcr(kvm, LPCR_VPM1,
4517 LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR); 4524 LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
@@ -4788,7 +4795,7 @@ static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
4788 4795
4789 /* Perform global invalidation and return lpid to the pool */ 4796 /* Perform global invalidation and return lpid to the pool */
4790 if (cpu_has_feature(CPU_FTR_ARCH_300)) { 4797 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
4791 if (kvm->arch.nested_enable) 4798 if (nesting_enabled(kvm))
4792 kvmhv_release_all_nested(kvm); 4799 kvmhv_release_all_nested(kvm);
4793 kvm->arch.process_table = 0; 4800 kvm->arch.process_table = 0;
4794 kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0); 4801 kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0);
@@ -5181,6 +5188,19 @@ static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
5181 return err; 5188 return err;
5182} 5189}
5183 5190
5191static int kvmhv_enable_nested(struct kvm *kvm)
5192{
5193 if (!nested)
5194 return -EPERM;
5195 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5196 return -ENODEV;
5197
5198 /* kvm == NULL means the caller is testing if the capability exists */
5199 if (kvm)
5200 kvm->arch.nested_enable = true;
5201 return 0;
5202}
5203
5184static struct kvmppc_ops kvm_ops_hv = { 5204static struct kvmppc_ops kvm_ops_hv = {
5185 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, 5205 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
5186 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv, 5206 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
@@ -5220,6 +5240,7 @@ static struct kvmppc_ops kvm_ops_hv = {
5220 .configure_mmu = kvmhv_configure_mmu, 5240 .configure_mmu = kvmhv_configure_mmu,
5221 .get_rmmu_info = kvmhv_get_rmmu_info, 5241 .get_rmmu_info = kvmhv_get_rmmu_info,
5222 .set_smt_mode = kvmhv_set_smt_mode, 5242 .set_smt_mode = kvmhv_set_smt_mode,
5243 .enable_nested = kvmhv_enable_nested,
5223}; 5244};
5224 5245
5225static int kvm_init_subcore_bitmap(void) 5246static int kvm_init_subcore_bitmap(void)
diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c
index 1f4b128894a0..2869a299c4ed 100644
--- a/arch/powerpc/kvm/powerpc.c
+++ b/arch/powerpc/kvm/powerpc.c
@@ -597,6 +597,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
597 r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300) && 597 r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300) &&
598 cpu_has_feature(CPU_FTR_HVMODE)); 598 cpu_has_feature(CPU_FTR_HVMODE));
599 break; 599 break;
600 case KVM_CAP_PPC_NESTED_HV:
601 r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&
602 !kvmppc_hv_ops->enable_nested(NULL));
603 break;
600#endif 604#endif
601 case KVM_CAP_SYNC_MMU: 605 case KVM_CAP_SYNC_MMU:
602#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE 606#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
@@ -2115,6 +2119,14 @@ static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
2115 r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags); 2119 r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
2116 break; 2120 break;
2117 } 2121 }
2122
2123 case KVM_CAP_PPC_NESTED_HV:
2124 r = -EINVAL;
2125 if (!is_kvmppc_hv_enabled(kvm) ||
2126 !kvm->arch.kvm_ops->enable_nested)
2127 break;
2128 r = kvm->arch.kvm_ops->enable_nested(kvm);
2129 break;
2118#endif 2130#endif
2119 default: 2131 default:
2120 r = -EINVAL; 2132 r = -EINVAL;
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 251be353f950..d9cec6b5cb37 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -953,6 +953,7 @@ struct kvm_ppc_resize_hpt {
953#define KVM_CAP_NESTED_STATE 157 953#define KVM_CAP_NESTED_STATE 157
954#define KVM_CAP_ARM_INJECT_SERROR_ESR 158 954#define KVM_CAP_ARM_INJECT_SERROR_ESR 158
955#define KVM_CAP_MSR_PLATFORM_INFO 159 955#define KVM_CAP_MSR_PLATFORM_INFO 159
956#define KVM_CAP_PPC_NESTED_HV 160
956 957
957#ifdef KVM_CAP_IRQ_ROUTING 958#ifdef KVM_CAP_IRQ_ROUTING
958 959