From patchwork Tue Mar 2 14:59:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12113759 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B4FBEC433DB for ; Wed, 3 Mar 2021 16:08:44 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DB0BA64EDC for ; Wed, 3 Mar 2021 16:08:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DB0BA64EDC Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; 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=kWpPA8mNOQZhumyjDgQ3juUflnh779v7B8cYS0RJwk8=; b=bn7iYSRZJUd9u7 h9UFrcSfHYhvUhSE0GvDkIv99DvHdBDRw8Z81s+dJ4yi2SJtslk9oaudc2aZEluFKaR0zEJsxZdlH i4Ble/VlrcYy2kJmWRLAI3B27kf8nF5HRZJJoEpKfNv8Yqz9LnF2y4BcYQNbGuAriZ0v7k2VfTiks vu5QER8I828/exMDSls/O3J6OimUnwsVqz/Pj3GKFROyXmXoQsMcf9w4VT4JqhuQJvXU/AyCHcfL1 PC5AB3dnL331CT9MQ25om479ilZhnKWmgOBj3DP7LqU4SlDEAmUYYJOoFP1kb9jvfz2Av0mZS2OTM v0InRdkHUN1HUF28F6zw==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lHU1B-005Wsa-7L; Wed, 03 Mar 2021 16:06:33 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lHSoY-005Dd3-OH for linux-arm-kernel@desiato.infradead.org; Wed, 03 Mar 2021 14:49:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:Cc:To:From:Subject: References:Mime-Version:Message-Id:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=fnu4kZLmantAPLgmfgZ7PZzVHqXbW8DjkSliDC/e8I8=; b=ah1HJcdACzBCBM2MffK3Nco3se uPqoM6vYhTz4DHaLCwzyii20db6Yj1wBVWVZLeEmd8dm3DedF8vTMnRfha6hZRVz2FzSSbPCXiyA/ fEYbeveBpFMUKonxAT+MWoDCpmjqTfvtguVF6Xig7Xh80mif53pbNnWWAPGsyAvHQGhYI5QkZiNai hh0sVhQtx3sNE0rptHeOZ3FCq42mIOgKI+5k6G3MWrl8i9s4ErZy3wY4DMrDnO1hx4jYGKx0278Jb zjq5eY5sEG3aDDmc97mq3OBrnR16OUcTwvTsA0J7qZGlCU/6+SMWH21uFIRiy8IKbrZhVBzzQgNMc ZiH/TyGw==; Received: from mail-qk1-x749.google.com ([2607:f8b0:4864:20::749]) by casper.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lH6XX-00HJLu-Cb for linux-arm-kernel@lists.infradead.org; Tue, 02 Mar 2021 15:02:26 +0000 Received: by mail-qk1-x749.google.com with SMTP id b78so13585499qkg.13 for ; Tue, 02 Mar 2021 07:02:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=fnu4kZLmantAPLgmfgZ7PZzVHqXbW8DjkSliDC/e8I8=; b=uUNW1RSwz/PPPl1Ua1IuQ64hOM0R+lTodrWU3UbAefyenAEai8RA0+im6R8w77uKKJ g4DVj/KqEe1TwMkcz/Ygt9jjIn5f38pgXuncyWnVA/+aa/N++uGeKO34r+o678oP9Mq8 67RAjq0NNwnf+jsn3QGIHhc250LRidVe1EfBP+5OIoO/7rtNLutaZrtEM1c24NPrdROO AFGrLf0EhJQzjScHxY2yPE6vMV/MjHb4X61lyzoWiVZ4RX2UG8RcNll2OE8OrY1N1eKg 6/SG5RQNSa0TOnAV1pRMMAOGJJqvxbtGjwYBUsH85FOGQKa/2mvXOYVgOo4Aa+FxfQOa yteA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=fnu4kZLmantAPLgmfgZ7PZzVHqXbW8DjkSliDC/e8I8=; b=tvvUJjAwUc5ewED5sVBrLP7djOJZtfw+PDz7PYTVqojOIGmsIpqE9VVNdMAXPhsYQ8 mz1tbGleq90Kd7CFTGUsc2NtqHqLUgVt8ZdKpCzQTsfp/AaWhD1OFgb1RbDErAmdxlJA KiLEZrcerNmJ5KtX11o3L1mTwjrYx4ekaXf5MJjiLJnH5L9NGF8mY0lqhRWLSf4WUQ++ v9YzalxRz90Ti1KU7pDhFsgtqm3UyZcS7k/CocYqnoQa+4qa+yKjI16PWEHsMPg5e5Ck f68nAvCsQvCQPqbpgEUYKKk2ibXQmOR2b0tQMzaDLUPlQqfi3rAd7YisPdzBwlCX6FM2 6IyA== X-Gm-Message-State: AOAM533/DWTdwS8wdjUZ+cWolKS72k1nItpYklpSrZzP9wYWHfEbdqRO s2f/pf0MUOjoeEjwqXFHAyo4AWrFz6yT X-Google-Smtp-Source: ABdhPJw8Dq6/V08jN1owoo4yABfxU5wp1FwJVtfeIaUa56rS2EjFaWfxxNpGQxARvmsP7DWrgv0nk/HKhje6 X-Received: from r2d2-qp.c.googlers.com ([fda3:e722:ac3:10:28:9cb1:c0a8:1652]) (user=qperret job=sendgmr) by 2002:a0c:c78c:: with SMTP id k12mr3964748qvj.47.1614697265404; Tue, 02 Mar 2021 07:01:05 -0800 (PST) Date: Tue, 2 Mar 2021 14:59:56 +0000 In-Reply-To: <20210302150002.3685113-1-qperret@google.com> Message-Id: <20210302150002.3685113-27-qperret@google.com> Mime-Version: 1.0 References: <20210302150002.3685113-1-qperret@google.com> X-Mailer: git-send-email 2.30.1.766.gb4fecdf3b7-goog Subject: [PATCH v3 26/32] KVM: arm64: Introduce PROT_NONE mappings for stage 2 From: Quentin Perret To: catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com Cc: android-kvm@google.com, linux-kernel@vger.kernel.org, kernel-team@android.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, tabba@google.com, mark.rutland@arm.com, dbrazdil@google.com, mate.toth-pal@arm.com, seanjc@google.com, qperret@google.com, robh+dt@kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210302_150226_026567_7F10C96B X-CRM114-Status: GOOD ( 15.65 ) 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 Once we start unmapping portions of memory from the host stage 2 (such as e.g. the hypervisor memory sections, or pages that belong to protected guests), we will need a way to track page ownership. And given that all mappings in the host stage 2 will be identity-mapped, we can use the host stage 2 page-table itself as a simplistic rmap. As a first step towards this, introduce a new protection attribute in the stage 2 page table code, called KVM_PGTABLE_PROT_NONE, which allows to annotate portions of the IPA space as inaccessible. For simplicity, PROT_NONE mappings are created as invalid mappings with a software bit set. Signed-off-by: Quentin Perret --- arch/arm64/include/asm/kvm_pgtable.h | 2 ++ arch/arm64/kvm/hyp/pgtable.c | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index 9935dbae2cc1..c9f6ed76e0ad 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -80,6 +80,7 @@ struct kvm_pgtable { * @KVM_PGTABLE_PROT_W: Write permission. * @KVM_PGTABLE_PROT_R: Read permission. * @KVM_PGTABLE_PROT_DEVICE: Device attributes. + * @KVM_PGTABLE_PROT_NONE: No permission. */ enum kvm_pgtable_prot { KVM_PGTABLE_PROT_X = BIT(0), @@ -87,6 +88,7 @@ enum kvm_pgtable_prot { KVM_PGTABLE_PROT_R = BIT(2), KVM_PGTABLE_PROT_DEVICE = BIT(3), + KVM_PGTABLE_PROT_NONE = BIT(4), }; #define PAGE_HYP (KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W) diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index bdd6e3d4eeb6..8e7059fcfd40 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -48,6 +48,8 @@ KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ KVM_PTE_LEAF_ATTR_HI_S2_XN) +#define KVM_PTE_LEAF_SW_BIT_PROT_NONE BIT(55) + struct kvm_pgtable_walk_data { struct kvm_pgtable *pgt; struct kvm_pgtable_walker *walker; @@ -120,6 +122,16 @@ static bool kvm_pte_valid(kvm_pte_t pte) return pte & KVM_PTE_VALID; } +static bool kvm_pte_prot_none(kvm_pte_t pte) +{ + return pte & KVM_PTE_LEAF_SW_BIT_PROT_NONE; +} + +static inline bool stage2_is_permanent_mapping(kvm_pte_t pte) +{ + return kvm_pte_prot_none(pte); +} + static bool kvm_pte_table(kvm_pte_t pte, u32 level) { if (level == KVM_PGTABLE_MAX_LEVELS - 1) @@ -182,7 +194,8 @@ static kvm_pte_t kvm_init_valid_leaf_pte(u64 pa, kvm_pte_t attr, u32 level) pte |= attr & (KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI); pte |= FIELD_PREP(KVM_PTE_TYPE, type); - pte |= KVM_PTE_VALID; + if (!kvm_pte_prot_none(pte)) + pte |= KVM_PTE_VALID; return pte; } @@ -317,7 +330,7 @@ static int hyp_map_set_prot_attr(enum kvm_pgtable_prot prot, u32 ap = (prot & KVM_PGTABLE_PROT_W) ? KVM_PTE_LEAF_ATTR_LO_S1_AP_RW : KVM_PTE_LEAF_ATTR_LO_S1_AP_RO; - if (!(prot & KVM_PGTABLE_PROT_R)) + if (!(prot & KVM_PGTABLE_PROT_R) || (prot & KVM_PGTABLE_PROT_NONE)) return -EINVAL; if (prot & KVM_PGTABLE_PROT_X) { @@ -489,6 +502,13 @@ static int stage2_map_set_prot_attr(enum kvm_pgtable_prot prot, PAGE_S2_MEMATTR(NORMAL); u32 sh = KVM_PTE_LEAF_ATTR_LO_S2_SH_IS; + if (prot & KVM_PGTABLE_PROT_NONE) { + if (prot != KVM_PGTABLE_PROT_NONE) + return -EINVAL; + attr |= KVM_PTE_LEAF_SW_BIT_PROT_NONE; + goto out; + } + if (!(prot & KVM_PGTABLE_PROT_X)) attr |= KVM_PTE_LEAF_ATTR_HI_S2_XN; else if (device) @@ -502,6 +522,8 @@ static int stage2_map_set_prot_attr(enum kvm_pgtable_prot prot, attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S2_SH, sh); attr |= KVM_PTE_LEAF_ATTR_LO_S2_AF; + +out: data->attr = attr; return 0; }