Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1817654pxm; Thu, 24 Feb 2022 09:53:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJwTDjqrR0NpVMvoLV0Dl1xR80Kr1C+53cqoYRlmcYDdBo0nmeIx7txbgF82V+71Qwd9tfcu X-Received: by 2002:a17:906:2bd7:b0:6cd:f89d:c828 with SMTP id n23-20020a1709062bd700b006cdf89dc828mr3290914ejg.232.1645725201103; Thu, 24 Feb 2022 09:53:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645725201; cv=none; d=google.com; s=arc-20160816; b=XDBO+wUK0VkBVjgyH8wZtOeL8W5eZ67U2LDsHmj9jYM7lEkgsjTl372zIskFBFJHP6 +f9zlLMwQKGbmFxkwJKq67LaJu5MovGTyZ6AxmAGQ1yM2il3IRTaeywY2jEgrMaQKigS BXkDHYhn5uSPnPvMiJDHFFuhD7YWmzr939jddnQg8b61X4uV6UoAYcrh/PbNSUNJkeVN xeNKkbg0CiFTb5S4bp9EdkEhbWmezYuUhdF6U2/V1qSRQ/B98/vERZvs0OP0BKaZtAqC 7LZJfbmLLDQ4VnkwCc1I0EnL+m7abbm4TDLxRZd07Wlau8uw2RP68339ggPEmpwle0Rm Vqvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=KaCBuwIyBaYjU3K7NXFsCjZcUQNiR1GDNYO7qs8JETY=; b=QtSn2CkeJ9u+UTicYEuqO8wrbIZuWUFX85z0gT8O5wTxC46JIIU3PhKAw5DwA2O7hm 5vpE0V2TmLY8i9FYTbihc53IxsOD9hiJTnG9sDFglvG5d0n/45BGktVTzBidb0DprpOx bl5eg7SjKCcsMuYLR0n0sKlFJ1tVjiPgdqyDv+2WeCYH94xW9CKd8nfur72KTE9pX9Tt 6mJ2JRKOexUstvTYEzTdnHl8L1zL8ZCk+r7n+FJepo1bo1xew5fqIlAP3DOrabkeePNP Po0EUjoLNmupbUjG6gjU7CV5cKYgvqz2ztPNOdkKwVMbAU2o9qAtSdwiNqaWnhUUZz5h bXag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=fZLUwv+j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c22-20020a056402101600b00412ec6653dbsi160923edu.276.2022.02.24.09.52.57; Thu, 24 Feb 2022 09:53:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=fZLUwv+j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231666AbiBXR07 (ORCPT + 99 others); Thu, 24 Feb 2022 12:26:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231358AbiBXR0o (ORCPT ); Thu, 24 Feb 2022 12:26:44 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7956278CA5 for ; Thu, 24 Feb 2022 09:26:13 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id n12-20020a170902968c00b001500c6029b0so1419846plp.5 for ; Thu, 24 Feb 2022 09:26:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=KaCBuwIyBaYjU3K7NXFsCjZcUQNiR1GDNYO7qs8JETY=; b=fZLUwv+joWr/KbhO5KrM6woBx5STObKXzVBlyItFKgfFX2ppVopErWfQyAyqmXKCOx 7jRwCDUOzx9jtL9EJrqyC/h3OjKVMzzyFz22XYWAHaCiZM7gegqLJnyaBcr8T5zHpdMc WxFEKpGgcJlSYHmcbY8TWDHaP1af46W4E8eFFpT51tXxeM+DuvDCrZzLSr9DJPPkwsQS T5eMRo5XWoO6q/1IOYfMQqIBfbOTzUbuS539VeFrhKfo6c8/nzkGGzldC/mEabJedqZ9 yaiXtwieY8sApFSm6D1pDeaUqDdNWzspj3NWfIR4fKioNKKnUVMBh9ogvyH0g+Rk1pJ1 zbHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=KaCBuwIyBaYjU3K7NXFsCjZcUQNiR1GDNYO7qs8JETY=; b=IeJcyeAvrqo83/k3Jv5OE+z1uf2/IO1xr2JKzoZxMY4I38hV20jGIO9m3+cKeKuYte zVcQz+1o9i1MEIhbkP32WgVGRaTIYzoAEQ0LASj6zWaAxd/vRI7a5RLr0cuf+gg7TRIj q+On01PR0/t3AznX0u6vp6xx9c+xFzVthVuvtleg34Oi9D7HEr4PJtR5qS21X2mn7iNY XTa0K0RDvQdXSALuXqPZ5K2VJUhQt3p6AaHnF8CGNT+fnv171C+4tP7eIZhcueo2OmDA vJQ1s6/7FSFkz7JjIH/IhP2NLQArXI0ajmApoiaGcODYCjxopNWW8v42n/0SbOic+JkK 6b4g== X-Gm-Message-State: AOAM530UlaabrFliT6kbOxZe8VJnBw3cg206acZdR4wxOfGYadltqabs 4DtPGqFUwvgrQQmPr8WGSFIknLBEcw1H X-Received: from rananta-virt.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1bcc]) (user=rananta job=sendgmr) by 2002:a62:5ac6:0:b0:4df:34dc:d6c5 with SMTP id o189-20020a625ac6000000b004df34dcd6c5mr3940421pfb.9.1645723573162; Thu, 24 Feb 2022 09:26:13 -0800 (PST) Date: Thu, 24 Feb 2022 17:25:49 +0000 In-Reply-To: <20220224172559.4170192-1-rananta@google.com> Message-Id: <20220224172559.4170192-4-rananta@google.com> Mime-Version: 1.0 References: <20220224172559.4170192-1-rananta@google.com> X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH v4 03/13] KVM: arm64: Encode the scope for firmware registers From: Raghavendra Rao Ananta To: Marc Zyngier , Andrew Jones , James Morse , Alexandru Elisei , Suzuki K Poulose Cc: Paolo Bonzini , Catalin Marinas , Will Deacon , Peter Shier , Ricardo Koller , Oliver Upton , Reiji Watanabe , Jing Zhang , Raghavendra Rao Anata , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The psuedo-firmware registers, KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 and KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, can be scopped as per-VM registers. Hence, during the KVM_GET_REG_LIST call, encode KVM_REG_ARM_SCOPE_VM into the registers, but during KVM_[GET|SET]_ONE_REG calls, clear the scope information such that they can be processed like before. For future expansion, helper functions such as kvm_arm_reg_id_encode_scope() and kvm_arm_reg_id_clear_scope() are introduced. Signed-off-by: Raghavendra Rao Ananta --- arch/arm64/include/asm/kvm_host.h | 2 + arch/arm64/kvm/guest.c | 77 +++++++++++++++++++++++++++++++ arch/arm64/kvm/hypercalls.c | 31 +++++++++---- 3 files changed, 100 insertions(+), 10 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 8132de6bd718..657733554d98 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -794,6 +794,8 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu); (test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features)) int kvm_trng_call(struct kvm_vcpu *vcpu); +int kvm_arm_reg_id_encode_scope(struct kvm_vcpu *vcpu, u64 *reg_id); +void kvm_arm_reg_id_clear_scope(struct kvm_vcpu *vcpu, u64 *reg_id); #ifdef CONFIG_KVM extern phys_addr_t hyp_mem_base; extern phys_addr_t hyp_mem_size; diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index 8238e52d890d..eb061e64a7a5 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -61,6 +61,83 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { sizeof(kvm_vcpu_stats_desc), }; +/* Registers that are VM scopped */ +static const u64 kvm_arm_vm_scope_fw_regs[] = { + KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, + KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, +}; + +/** + * kvm_arm_reg_id_encode_scope - Encode the KVM_REG_ARM_SCOPE info into the + * register-id + * @vcpu: The vcpu pointer + * @reg_id: Pointer to the register + * + * The function adds the register's scoping information into its encoding. + * If it's explicitly marked as a per-VM register, it's encoded with + * KVM_REG_ARM_SCOPE_VM. Else, it's marked as KVM_REG_ARM_SCOPE_VCPU, which + * is also the default if KVM_CAP_ARM_REG_SCOPE is disabled. + * + * For any error cases, the function returns an error code, else it returns + * the integer value of the encoding. + */ +int kvm_arm_reg_id_encode_scope(struct kvm_vcpu *vcpu, u64 *reg_id) +{ + const u64 *vm_scope_reg_arr; + unsigned int arr_size, idx; + + if (!READ_ONCE(vcpu->kvm->arch.reg_scope_enabled)) + return KVM_REG_ARM_SCOPE_VCPU; + + if (!reg_id) + return -EINVAL; + + switch (*reg_id & KVM_REG_ARM_COPROC_MASK) { + case KVM_REG_ARM_FW: + vm_scope_reg_arr = kvm_arm_vm_scope_fw_regs; + arr_size = ARRAY_SIZE(kvm_arm_vm_scope_fw_regs); + break; + default: + /* All the other register classes are currently + * treated as per-vCPU registers. + */ + return KVM_REG_ARM_SCOPE_VCPU; + } + + /* By default, all the registers encodings are scoped as vCPU. + * Modify the scope only if a register is marked as per-VM. + */ + for (idx = 0; idx < arr_size; idx++) { + if (vm_scope_reg_arr[idx] == *reg_id) { + *reg_id |= + KVM_REG_ARM_SCOPE_VM << KVM_REG_ARM_SCOPE_SHIFT; + return KVM_REG_ARM_SCOPE_VM; + } + } + + return KVM_REG_ARM_SCOPE_VCPU; +} + +/** + * kvm_arm_reg_id_clear_scope - Clear the KVM_REG_ARM_SCOPE info from the + * register-id + * @vcpu: The vcpu pointer + * @reg_id: Pointer to the register + * + * The function clears the register's scoping information, which ultimately + * is the raw encoding of the register. Note that the result is same as that + * of re-encoding the register as KVM_REG_ARM_SCOPE_VCPU. + * The function can be helpful to the existing code that uses the original + * register encoding to operate on the register. + */ +void kvm_arm_reg_id_clear_scope(struct kvm_vcpu *vcpu, u64 *reg_id) +{ + if (!READ_ONCE(vcpu->kvm->arch.reg_scope_enabled) || !reg_id) + return; + + *reg_id &= ~(1 << KVM_REG_ARM_SCOPE_SHIFT); +} + static bool core_reg_offset_is_vreg(u64 off) { return off >= KVM_REG_ARM_CORE_REG(fp_regs.vregs) && diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index 3c2fcf31ad3d..8624e6964940 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -160,10 +160,17 @@ int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu) int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) { - int i; + int i, ret; + u64 reg_id; for (i = 0; i < ARRAY_SIZE(kvm_arm_fw_reg_ids); i++) { - if (put_user(kvm_arm_fw_reg_ids[i], uindices++)) + reg_id = kvm_arm_fw_reg_ids[i]; + + ret = kvm_arm_reg_id_encode_scope(vcpu, ®_id); + if (ret < 0) + return ret; + + if (put_user(reg_id, uindices++)) return -EFAULT; } @@ -214,21 +221,23 @@ static int get_kernel_wa_level(u64 regid) int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) { void __user *uaddr = (void __user *)(long)reg->addr; - u64 val; + u64 val, reg_id = reg->id; - switch (reg->id) { + kvm_arm_reg_id_clear_scope(vcpu, ®_id); + + switch (reg_id) { case KVM_REG_ARM_PSCI_VERSION: val = kvm_psci_version(vcpu, vcpu->kvm); break; case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: - val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK; + val = get_kernel_wa_level(reg_id) & KVM_REG_FEATURE_LEVEL_MASK; break; default: return -ENOENT; } - if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id))) + if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg_id))) return -EFAULT; return 0; @@ -237,13 +246,15 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) { void __user *uaddr = (void __user *)(long)reg->addr; - u64 val; + u64 val, reg_id = reg->id; int wa_level; if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id))) return -EFAULT; - switch (reg->id) { + kvm_arm_reg_id_clear_scope(vcpu, ®_id); + + switch (reg_id) { case KVM_REG_ARM_PSCI_VERSION: { bool wants_02; @@ -270,7 +281,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) if (val & ~KVM_REG_FEATURE_LEVEL_MASK) return -EINVAL; - if (get_kernel_wa_level(reg->id) < val) + if (get_kernel_wa_level(reg_id) < val) return -EINVAL; return 0; @@ -306,7 +317,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) * We can deal with NOT_AVAIL on NOT_REQUIRED, but not the * other way around. */ - if (get_kernel_wa_level(reg->id) < wa_level) + if (get_kernel_wa_level(reg_id) < wa_level) return -EINVAL; return 0; -- 2.35.1.473.g83b2b277ed-goog