Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2140395pxa; Mon, 17 Aug 2020 01:43:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxufuTYCsfRqKSK6ShhNBBNY3GnV0vu53UReVWfKnR6u3Lq5Pf0Iy3LQh6ba5TnrjKYKOBT X-Received: by 2002:a05:6402:1f0:: with SMTP id i16mr13645408edy.309.1597653831345; Mon, 17 Aug 2020 01:43:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597653831; cv=none; d=google.com; s=arc-20160816; b=IFUGSmx/5y0X3G00cmEA5j9P68BzkFZiKOmfe8v3a7PUv+nH/Xb8GyNM2youpMszwy T2Hg1kIgWVMCUxdpBglhSLbJuHFqjXUUw3Ho/yk00eWR7p3HoUfGgaBfJt04dkPg4ILB tdM5Q03ecOWbZtz2R4oD8XbRRaE2tLDBJBLFazywOPI6oQ3yTUUVNrKm2oDibPn4P6jn A98k2BMB4zy5E8oLICpSwC4K8Qvne7FiQJkhxTlo2Xfr04TFrwOcoAzR45nXYhVgq3Z0 cj4RFy7YH5fK5iwDD5BvZ6mAFAlmRS9wd5G+ZZmnqo6KhttHAYIIbz9fqpFAJwZdC9b2 N21Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=FIT3qoMH5JvGo2ge3stSfiKyB85X2dWQY5DxxflYCnQ=; b=jSYuUKFYFk2Lydstm+ONj2XxeBdbTntLstrq5Xwk1ZRTo6xVXttnrUrEl2OmypJhnZ jmg580IOI7/oauw65MCDkpJvMD/AyAbHHGI7KrNTjuWkiNen3dt4bl+FI0fTxFWbSSib oRtnZXishQ6kXxAoxsobZjUV7U3/DVih6afxG0IfNActM3vprvvlQHPJxAFnhlkmilP2 5vz1dISmYPCUlZUYRJx35jUL3tAhzin+CRnRWQdmb32dJin97i4B7ZMdd9Smg7Qi31mu s+gSrZUcePKHlKGMZ0PuhoSGjFjp0TllK6S0d/O5j5xHTyUJMcKdYxQbhAVxP71wqA8J 47kA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n5si10648043ejc.171.2020.08.17.01.43.28; Mon, 17 Aug 2020 01:43:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726703AbgHQIl7 (ORCPT + 99 others); Mon, 17 Aug 2020 04:41:59 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:45198 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726287AbgHQIl3 (ORCPT ); Mon, 17 Aug 2020 04:41:29 -0400 Received: from DGGEMS411-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 6AFD1575E938EDF8A34A; Mon, 17 Aug 2020 16:41:22 +0800 (CST) Received: from DESKTOP-5IS4806.china.huawei.com (10.174.187.22) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.487.0; Mon, 17 Aug 2020 16:41:14 +0800 From: Keqian Zhu To: , , , CC: Marc Zyngier , Steven Price , Catalin Marinas , Will Deacon , James Morse , Suzuki K Poulose , , Keqian Zhu Subject: [RFC PATCH 3/5] KVM: arm64: Provide VM device attributes for LPT time Date: Mon, 17 Aug 2020 16:41:08 +0800 Message-ID: <20200817084110.2672-4-zhukeqian1@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20200817084110.2672-1-zhukeqian1@huawei.com> References: <20200817084110.2672-1-zhukeqian1@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.174.187.22] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Allow user space to inform the KVM host what the PV frequency is and where in the physical memory map the paravirtualized LPT time structures should be located. User space can set attributes on the VM for that guest. The address is given in terms of the physical address visible to the guest and must be 64 byte aligned. The guest will discover the address via a hypercall. PV frequency is 32 bits and must not be 0. Signed-off-by: Steven Price Signed-off-by: Keqian Zhu --- arch/arm64/include/asm/kvm_host.h | 4 ++ arch/arm64/include/uapi/asm/kvm.h | 5 +++ arch/arm64/kvm/arm.c | 64 ++++++++++++++++++++++++++++ arch/arm64/kvm/pvtime.c | 87 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 0c6a564..cbe330c 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -523,6 +523,10 @@ int kvm_arm_pvtime_get_attr(struct kvm_vcpu *vcpu, int kvm_arm_pvtime_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr); +int kvm_arm_pvtime_lpt_set_attr(struct kvm *kvm, struct kvm_device_attr *attr); +int kvm_arm_pvtime_lpt_get_attr(struct kvm *kvm, struct kvm_device_attr *attr); +int kvm_arm_pvtime_lpt_has_attr(struct kvm *kvm, struct kvm_device_attr *attr); + static inline void kvm_arm_pvtime_vcpu_init(struct kvm_vcpu_arch *vcpu_arch) { vcpu_arch->steal.base = GPA_INVALID; diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h index ba85bb2..7b045c7 100644 --- a/arch/arm64/include/uapi/asm/kvm.h +++ b/arch/arm64/include/uapi/asm/kvm.h @@ -325,6 +325,11 @@ struct kvm_vcpu_events { #define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3 #define KVM_DEV_ARM_ITS_CTRL_RESET 4 +/* Device Control API on kvm fd */ +#define KVM_ARM_VM_PVTIME_LPT_CTRL 0 +#define KVM_ARM_VM_PVTIME_LPT_IPA 0 +#define KVM_ARM_VM_PVTIME_LPT_FREQ 1 + /* Device Control API on vcpu fd */ #define KVM_ARM_VCPU_PMU_V3_CTRL 0 #define KVM_ARM_VCPU_PMU_V3_IRQ 0 diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 671f1461..4a867e5 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -1235,11 +1235,60 @@ static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, } } +static int kvm_arm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) +{ + int ret; + + switch (attr->group) { + case KVM_ARM_VM_PVTIME_LPT_CTRL: + ret = kvm_arm_pvtime_lpt_set_attr(kvm, attr); + break; + default: + ret = -ENXIO; + break; + } + + return ret; +} + +static int kvm_arm_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) +{ + int ret; + + switch (attr->group) { + case KVM_ARM_VM_PVTIME_LPT_CTRL: + ret = kvm_arm_pvtime_lpt_get_attr(kvm, attr); + break; + default: + ret = -ENXIO; + break; + } + + return ret; +} + +static int kvm_arm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) +{ + int ret; + + switch (attr->group) { + case KVM_ARM_VM_PVTIME_LPT_CTRL: + ret = kvm_arm_pvtime_lpt_has_attr(kvm, attr); + break; + default: + ret = -ENXIO; + break; + } + + return ret; +} + long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { struct kvm *kvm = filp->private_data; void __user *argp = (void __user *)arg; + struct kvm_device_attr attr; switch (ioctl) { case KVM_CREATE_IRQCHIP: { @@ -1271,6 +1320,21 @@ long kvm_arch_vm_ioctl(struct file *filp, return 0; } + case KVM_SET_DEVICE_ATTR: { + if (copy_from_user(&attr, argp, sizeof(attr))) + return -EFAULT; + return kvm_arm_vm_set_attr(kvm, &attr); + } + case KVM_GET_DEVICE_ATTR: { + if (copy_from_user(&attr, argp, sizeof(attr))) + return -EFAULT; + return kvm_arm_vm_get_attr(kvm, &attr); + } + case KVM_HAS_DEVICE_ATTR: { + if (copy_from_user(&attr, argp, sizeof(attr))) + return -EFAULT; + return kvm_arm_vm_has_attr(kvm, &attr); + } default: return -EINVAL; } diff --git a/arch/arm64/kvm/pvtime.c b/arch/arm64/kvm/pvtime.c index 24131ca..3f93473 100644 --- a/arch/arm64/kvm/pvtime.c +++ b/arch/arm64/kvm/pvtime.c @@ -257,3 +257,90 @@ gpa_t kvm_init_lpt_time(struct kvm *kvm) { return kvm->arch.lpt.base; } + +int kvm_arm_pvtime_lpt_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) +{ + u64 __user *user = (u64 __user *)attr->addr; + u64 ipa; + u32 freq; + int idx; + int ret = 0; + + switch (attr->attr) { + case KVM_ARM_VM_PVTIME_LPT_IPA: + if (get_user(ipa, user)) { + ret = -EFAULT; + break; + } + if (!IS_ALIGNED(ipa, 64)) { + ret = -EINVAL; + break; + } + if (kvm->arch.lpt.base != GPA_INVALID) { + ret = -EEXIST; + break; + } + /* Check the address is in a valid memslot */ + idx = srcu_read_lock(&kvm->srcu); + if (kvm_is_error_hva(gfn_to_hva(kvm, ipa >> PAGE_SHIFT))) + ret = -EINVAL; + srcu_read_unlock(&kvm->srcu, idx); + if (ret) + break; + + kvm->arch.lpt.base = ipa; + break; + case KVM_ARM_VM_PVTIME_LPT_FREQ: + if (get_user(freq, user)) { + ret = -EFAULT; + break; + } + if (freq == 0) { + ret = -EINVAL; + break; + } + if (kvm->arch.lpt.fpv != 0) { + ret = -EEXIST; + break; + } + kvm->arch.lpt.fpv = freq; + break; + default: + ret = -ENXIO; + break; + } + + return ret; +} + +int kvm_arm_pvtime_lpt_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) +{ + u64 __user *user = (u64 __user *)attr->addr; + int ret = 0; + + switch (attr->attr) { + case KVM_ARM_VM_PVTIME_LPT_IPA: + if (put_user(kvm->arch.lpt.base, user)) + ret = -EFAULT; + break; + case KVM_ARM_VM_PVTIME_LPT_FREQ: + if (put_user(kvm->arch.lpt.fpv, user)) + ret = -EFAULT; + break; + default: + ret = -ENXIO; + break; + } + + return ret; +} + +int kvm_arm_pvtime_lpt_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) +{ + switch (attr->attr) { + case KVM_ARM_VM_PVTIME_LPT_IPA: + case KVM_ARM_VM_PVTIME_LPT_FREQ: + return 0; + } + return -ENXIO; +} -- 1.8.3.1