From patchwork Mon Feb 14 06:57:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reiji Watanabe X-Patchwork-Id: 12745045 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 91CE5C433EF for ; Mon, 14 Feb 2022 07:03:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=CvBWgAh7hVIKA+qrU+D+wmKV8iGc9UbBTZ7CQc6ajWA=; b=3gaRmfTH/zW11ujf5/aYxCxLsP nLQW7NGQMDwENbr3mkHIr8wWSNNxnPjA4TEKirnpi7i85HBpkmTbe+YFbJm1btAsIC5khtuqxvfaQ B49WBgRUy/xT7a9XsGEjPQ7TNlKiAG0KoKyF7hXJbih+ROncHaUG/v5KJ3MaPNMF0nhRxAc5LTjxb 1ZjvIs575ZRhswbr8jvtVbH4vpYcqG+bUs8+KE832LaN6OTZnSM6dhj2XdsNrA+mxH7xervQT9i8g 3x6YvKZdmM3BIGeWa3f6PBRcpEwBQXju027+bcoSLsPHuwOtUOyIKDpIwWVg72p5qB8ujDs5KE1Lm T6hbb7Ag==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nJVNW-00DXy0-TY; Mon, 14 Feb 2022 07:02:32 +0000 Received: from mail-pj1-x1049.google.com ([2607:f8b0:4864:20::1049]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nJVKx-00DWgA-Ne for linux-arm-kernel@lists.infradead.org; Mon, 14 Feb 2022 06:59:53 +0000 Received: by mail-pj1-x1049.google.com with SMTP id c6-20020a17090a020600b001b9b44c18aaso3095933pjc.4 for ; Sun, 13 Feb 2022 22:59:50 -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=Brz6Y3j0MZViYAVMVNSgTmSSQnU2BlZWdtLqRqNaGsU=; b=Zu/UInFm39+Vm/bojUEccM94nZ9lA0DMX2QsEIaUyNi8fhkgvXbwYgWeczj5b+togF OMZJQ6q0qT+ld4tcgGrXQqv4ToFMTZ2Y9L/bIMZGz6WCDzumtbAA6qh+dQhCaKD8U1Fc cUTF8pUdBeTFJgl/6rtcjojOSrcXYXYnlWZF9OiwZE8qdd9uihGFSmBUwXW+YpTykR0w do5vJatra5x1BNWBA02xXjlt/ghW9hNKqzsVg8YLdRuoEMKUWLmDdsNZUnR7NvAdwQlt +kQLPlttHiIFiijR9O85L0qGSS2/OsSZMNIuhy5no5X7OsBRsee+pRBI0k8KPonH4m0z QRIg== 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=Brz6Y3j0MZViYAVMVNSgTmSSQnU2BlZWdtLqRqNaGsU=; b=nEZQULIHdJGhc3AklSztjfd4qeNalbfLOw67dHWPknqc7g/FlEgx51wsDfkqoiQpkH d9Fm3lYJjM592ge/2Ho2v8283Tp8hm5wOGLGSgaDHtdarIdXxmbyebpqOBpmW3FHKl4Y 88KvBrbl+CVD4YuS3H0S6W2ff2AHk0BWnKWlw28tMsLYiqVOT7zt0FkHkamf5R3TQf31 PQexWj7xvavv2Jlm8bg5bdmLqlDrLrbVw7Cj+4ulNBbCYEhpGI9TsadcV6OIqG+w0YDQ PI+gUrvzOIXde79khqHiV5T0nplohx8YRf9FA+jzIDglEne3QCaZ2v1pWIT5wZToA2Wl +krw== X-Gm-Message-State: AOAM532bcN5Js/yZjGoyLHC38TGfqU0YLBw66wINaVwthGAZvEDmixPu YXl4BI0nGT7PjjYRDEyKfsOLVmObaRc= X-Google-Smtp-Source: ABdhPJwWwbe9hH9AKFYWngQ18eSzKL4vUbmfnE2yQiNzgqPynQfz0rkiJhzI+TM01pY5PO5th91tCagghrU= X-Received: from reiji-vws-sp.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3d59]) (user=reijiw job=sendgmr) by 2002:a05:6a00:894:: with SMTP id q20mr12856682pfj.79.1644821989848; Sun, 13 Feb 2022 22:59:49 -0800 (PST) Date: Sun, 13 Feb 2022 22:57:28 -0800 In-Reply-To: <20220214065746.1230608-1-reijiw@google.com> Message-Id: <20220214065746.1230608-10-reijiw@google.com> Mime-Version: 1.0 References: <20220214065746.1230608-1-reijiw@google.com> X-Mailer: git-send-email 2.35.1.265.g69c8d7142f-goog Subject: [PATCH v5 09/27] KVM: arm64: Make ID_AA64MMFR1_EL1 writable From: Reiji Watanabe To: Marc Zyngier , kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, James Morse , Alexandru Elisei , Suzuki K Poulose , Paolo Bonzini , Will Deacon , Andrew Jones , Fuad Tabba , Peng Liang , Peter Shier , Ricardo Koller , Oliver Upton , Jing Zhang , Raghavendra Rao Anata , Reiji Watanabe X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220213_225951_815324_9736E729 X-CRM114-Status: GOOD ( 15.13 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This patch adds id_reg_info for ID_AA64MMFR1_EL1 to make it writable by userspace. Hardware update of Access flag and/or Dirty state in translation table needs to be disabled for the guest to let userspace set ID_AA64MMFR1_EL1.HAFDBS field to a lower value. It requires trapping the guest's accessing TCR_EL1, which KVM doesn't always do (in order to trap it without FEAT_FGT, HCR_EL2.TVM needs to be set to 1, which also traps many other virtual memory control registers). So, userspace won't be allowed to modify the value of the HAFDBS field. Signed-off-by: Reiji Watanabe --- arch/arm64/kvm/sys_regs.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 4ed15ae7f160..1c137f8c236f 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -570,6 +570,30 @@ static int validate_id_aa64mmfr0_el1(struct kvm_vcpu *vcpu, return 0; } +static int validate_id_aa64mmfr1_el1(struct kvm_vcpu *vcpu, + const struct id_reg_info *id_reg, u64 val) +{ + u64 limit = id_reg->vcpu_limit_val; + unsigned int hafdbs, lim_hafdbs; + + hafdbs = cpuid_feature_extract_unsigned_field(val, ID_AA64MMFR1_HADBS_SHIFT); + lim_hafdbs = cpuid_feature_extract_unsigned_field(limit, ID_AA64MMFR1_HADBS_SHIFT); + + /* + * Don't allow userspace to modify the value of HAFDBS. + * Hardware update of Access flag and/or Dirty state in translation + * table needs to be disabled for the guest to let userspace set + * HAFDBS field to a lower value. It requires trapping the guest's + * accessing TCR_EL1, which KVM doesn't always do (in order to trap + * it without FEAT_FGT, HCR_EL2.TVM needs to be set to 1, which also + * traps many other virtual memory control registers). + */ + if (hafdbs != lim_hafdbs) + return -EINVAL; + + return 0; +} + static void init_id_aa64pfr0_el1_info(struct id_reg_info *id_reg) { u64 limit = id_reg->vcpu_limit_val; @@ -675,6 +699,11 @@ static struct id_reg_info id_aa64mmfr0_el1_info = { .validate = validate_id_aa64mmfr0_el1, }; +static struct id_reg_info id_aa64mmfr1_el1_info = { + .sys_reg = SYS_ID_AA64MMFR1_EL1, + .validate = validate_id_aa64mmfr1_el1, +}; + /* * An ID register that needs special handling to control the value for the * guest must have its own id_reg_info in id_reg_info_table. @@ -689,6 +718,7 @@ static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM] = { [IDREG_IDX(SYS_ID_AA64ISAR0_EL1)] = &id_aa64isar0_el1_info, [IDREG_IDX(SYS_ID_AA64ISAR1_EL1)] = &id_aa64isar1_el1_info, [IDREG_IDX(SYS_ID_AA64MMFR0_EL1)] = &id_aa64mmfr0_el1_info, + [IDREG_IDX(SYS_ID_AA64MMFR1_EL1)] = &id_aa64mmfr1_el1_info, }; static int validate_id_reg(struct kvm_vcpu *vcpu, u32 id, u64 val)