From patchwork Fri Feb 25 03:34:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12759609 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 2A7B8C433F5 for ; Fri, 25 Feb 2022 03:42:48 +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=vLhV68+MLbzs2gyXfzsTxlt1aWzqQ1rc9Q/jmWsw9Yo=; b=DBU6esuUu6fteu /7MPm0i//+UBgsRXjaSmdT+oiO7akcJ8hDiRV2lq2HJuQpkI96QTZysGaGeWQKOInkNzEks+QRBZd +n361DOpgVwy9OnNai2xVIIUU5T250Zi9ezw8DXpxI+lJhc5CavowpjLk0K84Q/DuGhMo+zf55EBC uJktT5NJWZrLqXfJB4Rdu77U7QIZaPw2g2xtHqEvU8nOxTQquk5G0L09GCsTV2MgVu8t8nVPIg7GE CImI2ADbmy7W31SKxs7XMnotsZSbQsOQVlrlYvleJ0JXKri3luhlgX+eHKYiNld+gfULibvPKHzt6 ClVTDorTBpLYcE+dX9iA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nNRUC-0037dj-IH; Fri, 25 Feb 2022 03:41:40 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nNRTy-0037Z0-8Q for linux-arm-kernel@lists.infradead.org; Fri, 25 Feb 2022 03:41:27 +0000 Received: by mail-yb1-xb49.google.com with SMTP id b64-20020a256743000000b0061e169a5f19so2009805ybc.11 for ; Thu, 24 Feb 2022 19:41:25 -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=0x2wn/o2gYOT1QepQCECViMH+fFbe42VFIEzpGyKYPU=; b=QkaH0+9lm1XnIrQtwef1MIRtU3vdqUyeYauQ0QclzvBFRyMKXRAZNfP3AZ/75tEW5v A/6NnwPNuvAR3YAXjwR9nz9ER16n3AS9LGKJ6T5MVkI0G3eIGG77lex3qyvyySWRAxYP EBkLjFYGzLCoUPdTl11vZrKFSCMErH2Qw2+YLlEKzwoydyx3eocXRUhidIJ6uIYL4bnp T11wZIEVRrdm112A26FKY6O2yMrUH+5sVdRssrItX8YytwEM/FQe6kwVASO+f2v4ABm7 zso4UKV+++MlDgyKOHD6e10/RNFllFf5Tpi4cdHh5fA9bCQb81HdtqrF4zV0WiFsrINw BVtw== 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=0x2wn/o2gYOT1QepQCECViMH+fFbe42VFIEzpGyKYPU=; b=yyunyXJdZzjYhwUsRqNvQjlCLje2/Mq0nAr1L5bqPe98LyuHbf+1Him5GkioIhYhJF GVdmJ3TJYo3dLCjjdKE4zCHxfNtp0edpuX3ayS1mtKg/K2sidN5dBzWcYqiysEW6Nctb MR8BRGv9NtY/l23qmhivk3NE3NBrTRXjdvKTFjMj0hlA01fFvWlkoyNK8DbN0+58RsxB ZPXao3fV4ym5tbCXa8pyRwP49XetzykRT0Col6FOSQPwdIPO4brn7zTGDaxF8nEnqNGM NsLD3AaqikQWYC5FUMmSxB6DTj59Ot5pZiUjDZJnnsqYC53v2oMBs2R5IYLqdL/7F9Yd 4jSw== X-Gm-Message-State: AOAM532MX+WG5UTxHQEoIEfQeRmiGChkWroZV0Z0etZqS3k34fFRe9QP c1z1+Zz25TdRR22aoKrN8vfuuz1vVb4jV09lOQ== X-Google-Smtp-Source: ABdhPJxzcgpZCUdamSFGzYXAXUsCWNCBw73C8lXSgDIh3yMJ8Ub57T3zRTVv6FqPeC0H1pLaruUzeJ4yQJBOn00kXw== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:3a50:90b2:e6a2:9060]) (user=kaleshsingh job=sendgmr) by 2002:a0d:fb45:0:b0:2d0:d09a:576c with SMTP id l66-20020a0dfb45000000b002d0d09a576cmr5778027ywf.447.1645760484556; Thu, 24 Feb 2022 19:41:24 -0800 (PST) Date: Thu, 24 Feb 2022 19:34:48 -0800 In-Reply-To: <20220225033548.1912117-1-kaleshsingh@google.com> Message-Id: <20220225033548.1912117-4-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220225033548.1912117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH v4 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 , 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-20220224_194126_340404_4F0AEBF4 X-CRM114-Status: GOOD ( 16.73 ) 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 v4: - Replace IS_ERR_OR_NULL check with IS_ERR check now that hyp_alloc_private_va_range() returns an error for null pointer, per Fuad - Format comments to < 80 cols, per Fuad 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..0a83c0e7f838 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((void *)guard_hyp_va)) { + err = PTR_ERR((void *)guard_hyp_va); + 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) {