From patchwork Thu Feb 24 05:13:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12757843 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 D386FC433EF for ; Thu, 24 Feb 2022 05:19:37 +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:From:Subject:References:Mime-Version :Message-Id:In-Reply-To:Date:Reply-To:To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=7fJyezx1Adqt6SzlQGXsCzf/5L9vheSKuEEwN+4/fbg=; b=f5Yld92DvIzqMb aXxfL2JK3RwHNCKzu2avlQWcDsv03gu/yc/+0PjTcw6IzFSiCA9k/nGHDhu6rjFQsRXmEl2tZPHAZ 2ugCjIamnwMCZZT1A4L0pUz28xVlSCXTQr+swTUplM9afvN9HzpDgpiFgate8S/35+vcLli23fAKc pmklGBgaWlH6ad+v52ZDrPobKtKSgT8Tc/XZy4pAdc8DkyQWYCLDXkKTGB+rQQYx7/g1PLKCofRET wg21iab6z4Mpe3m6A5jksn4PZqCrBHTiMxb/6mhpYQMARSARULB9UbXJqQGsu9uzXraNmmA+1sitr ICf+lbizNU/8I1C567/A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nN6WE-00GqCN-SW; Thu, 24 Feb 2022 05:18:23 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nN6WB-00GqB8-8R for linux-arm-kernel@lists.infradead.org; Thu, 24 Feb 2022 05:18:20 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id k7-20020a255607000000b00621afc793b8so949402ybb.1 for ; Wed, 23 Feb 2022 21:18:18 -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:cc; bh=WjbUGG0xKbMDb5kZ3phtYyC+0rOONMmeQOjqy/50x/c=; b=FHuVkJYODfDfl0B/txzDiDC6eLLacjGAf43dVMqRJLxMGNm+ImeYaDBNJD5gfJcv21 Z9ohbr5zf1QC8/F1bTBF+ZtWze2p25FmnoYimvvp8tGfx9BVPZudUp/YbHnPxZvAYx55 BqQBFSvFlWYpbE2iAz3wRpB2MQIzUJu+vUzz+Lh96Jvt7QNRq+kZQU99bHlUuJuxphka SBWOmouo+ZUbL8PlVbbewShLA/QY6RIkqnU5TnnahSBctuqAIesOkiMO0fGPQRCh9HHP 62goySNV+LLta5Vhbshz1OV5YtgqzaIrnFy1jWIOuZFsvFmAUGNSod5QivSRYUMFNxUt nAHw== 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:cc; bh=WjbUGG0xKbMDb5kZ3phtYyC+0rOONMmeQOjqy/50x/c=; b=FhorO7yHGtIakJJdFvoBx0MtuKkiIrGzjPNvurYyA7crmh6A4QjS9FUSB7LDz19Jte MlxBnZxMWOIACWrhS6n1LM5Ne/xgxruRCJEvz+YC0fZtajCCX+USq6sAoaUVJjY84Maz +ai2kpS9N5Lmf9J7V6f8x2Gbh+FCSvng5jxJcHDRPNlV7BmKUlXspC7WvQP3jaOfHE1d YPs5RdZqRCwa2S2jWwp+o9afMeAZqSL3QPZuv1BMQEd4zk7RZGNUE+FWMI73FbVHtQIy svfNXTbht5uuKAIGvayP9Dh1clX/vCYybSvBdIJ1BQC0xUaW3MrHfTRZ9KDy1KYUzsJL spcA== X-Gm-Message-State: AOAM5322+C2EqhBHKhbuvihGrXo/w75r1pr5dsHukN66T+/CPX3GauRM H/reNqjxg/ndp7+ZDExr9O3HPG0BkgbB3VYJoA== X-Google-Smtp-Source: ABdhPJx3WSBr6Ord/trpGYMYJOGDBeI1xnyUZY7Sz4G+U3zbWcId4VvxdhF5xjfG8cDbo/CMBLseJWAynlqRPGyAbg== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:415c:9be0:c461:440a]) (user=kaleshsingh job=sendgmr) by 2002:a25:ae94:0:b0:624:4253:a8b with SMTP id b20-20020a25ae94000000b0062442530a8bmr999641ybj.529.1645679897623; Wed, 23 Feb 2022 21:18:17 -0800 (PST) Date: Wed, 23 Feb 2022 21:13:38 -0800 In-Reply-To: <20220224051439.640768-1-kaleshsingh@google.com> Message-Id: <20220224051439.640768-4-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220224051439.640768-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.35.1.473.g83b2b277ed-goog Subject: [PATCH v3 3/8] KVM: arm64: Add guard pages for KVM nVHE hypervisor stack From: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Kalesh Singh , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Mark Rutland , Mark Brown , Masami Hiramatsu , Peter Collingbourne , "Madhavan T. Venkataraman" , Andrew Walbran , Andrew Scull , Paolo Bonzini , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220223_211819_321970_B11F6431 X-CRM114-Status: GOOD ( 17.05 ) 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 Maps the stack pages in the flexible private VA range and allocates guard pages below the stack as unbacked VA space. The stack is aligned to twice its size to aid overflow detection (implemented in a subsequent patch in the series). Signed-off-by: Kalesh Singh --- Changes in v3: - Handle null ptr in IS_ERR_OR_NULL checks, per Mark arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/arm.c | 32 +++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h index d5b0386ef765..2e277f2ed671 100644 --- a/arch/arm64/include/asm/kvm_asm.h +++ b/arch/arm64/include/asm/kvm_asm.h @@ -169,6 +169,7 @@ struct kvm_nvhe_init_params { unsigned long tcr_el2; unsigned long tpidr_el2; unsigned long stack_hyp_va; + unsigned long stack_pa; phys_addr_t pgd_pa; unsigned long hcr_el2; unsigned long vttbr; diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index ecc5958e27fe..7a23630c4a7f 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -1541,7 +1541,6 @@ static void cpu_prepare_hyp_mode(int cpu) tcr |= (idmap_t0sz & GENMASK(TCR_TxSZ_WIDTH - 1, 0)) << TCR_T0SZ_OFFSET; params->tcr_el2 = tcr; - params->stack_hyp_va = kern_hyp_va(per_cpu(kvm_arm_hyp_stack_page, cpu) + PAGE_SIZE); params->pgd_pa = kvm_mmu_get_httbr(); if (is_protected_kvm_enabled()) params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS; @@ -1990,14 +1989,41 @@ static int init_hyp_mode(void) * Map the Hyp stack pages */ for_each_possible_cpu(cpu) { + struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); - err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE, - PAGE_HYP); + unsigned long stack_hyp_va, guard_hyp_va; + /* + * Private mappings are allocated downwards from io_map_base + * so allocate the stack first then the guard page. + * + * The stack is aligned to twice its size to facilitate overflow + * detection. + */ + err = __create_hyp_private_mapping(__pa(stack_page), PAGE_SIZE, + PAGE_SIZE * 2, &stack_hyp_va, PAGE_HYP); if (err) { kvm_err("Cannot map hyp stack\n"); goto out_err; } + + /* Allocate unbacked private VA range for stack guard page */ + guard_hyp_va = hyp_alloc_private_va_range(PAGE_SIZE, PAGE_SIZE); + if (IS_ERR_OR_NULL((void *)guard_hyp_va)) { + err = guard_hyp_va ? PTR_ERR((void *)guard_hyp_va) : -ENOMEM; + kvm_err("Cannot allocate hyp stack guard page\n"); + goto out_err; + } + + /* + * Save the stack PA in nvhe_init_params. This will be needed to recreate + * the stack mapping in protected nVHE mode. __hyp_pa() won't do the right + * thing there, since the stack has been mapped in the flexible private + * VA space. + */ + params->stack_pa = __pa(stack_page) + PAGE_SIZE; + + params->stack_hyp_va = stack_hyp_va + PAGE_SIZE; } for_each_possible_cpu(cpu) {