From patchwork Mon Mar 14 20:01:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12780746 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 A86CEC4332F for ; Mon, 14 Mar 2022 20:05:36 +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=ARJifowxiM/I/Oj5WW1SlJOw4CG1RTXHhaGKS1bR6ZM=; b=qeagyptLbEEbtk g2VnhmOR7sDpQz6w65Z1GhnUCXuFcT0zNxvegqt6YhW6F8TR+muGZAvoCM6ffBcW+Lu5GJ7sRrvQs aAVUzccDVrqLIo7Ql0eaq7XnKDnF8gvJ8uq+IEdC2p+1sSwD+Bu1HwZytSPtvB65PrYYPXERMhpoa NZAF6Ywyz5nEfk8aUBmbUjz7nSGONHXzWIZgMFUE0qJfKgB1lHD2IQ251ci8vAPg/k5fX9a/d+edY NIJY3Svo7c+ZljPCZmegj4taSKfwZfqfeIs4gfZ5j1WIdDIlDkRN1wOTK6bP3hsMpvxL73p8/IFMd J80SZy0egcAtN2EQUy4A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nTqvG-006hek-48; Mon, 14 Mar 2022 20:04:06 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nTqv7-006hbS-9W for linux-arm-kernel@lists.infradead.org; Mon, 14 Mar 2022 20:04:00 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2d7eaa730d9so141851887b3.13 for ; Mon, 14 Mar 2022 13:03:55 -0700 (PDT) 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=KBKuM8nQbHEsk5KoB9KDoIyUXnj0iRI96x05Zc5GVwU=; b=pSlak9X+zf0pVcMyDGHXsdj/0ZvCoyfAdmZkLL2IoXQ/eoplgnCHVzUHV6625bNCMO 5W/FfGYImrd/5arptPi/Rh1DZ3oNGT2WHcLtPHwLYae1RG+2+WzzpSLpx2IBnWYENwKv olSKeenA96Zm6AESUkoS6AaJro4cv3oA8G58W7nRuRrabP+0XkTX3+FgmM2KLrYrKfxI fh3CE8qwaMgsfxjjWdwl2sVShwIJXNUkC9SCrJe9gGVTROtWlcut+zWSPLRbuIgzhnlp LaMbeaJkLAlGGeT8FDnOUeP6d721ULqiYxQkoP6I2jelyXGKa7OrDJwlWFpyCLR4N3P3 sZSA== 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=KBKuM8nQbHEsk5KoB9KDoIyUXnj0iRI96x05Zc5GVwU=; b=kGkIbRMIBo2lf6pZQJ0RCZkc6RWdyly3kj07e7Nf/dFNH96ByxYGatik7m6F/oC9rL RKwBHARbuhQN5r24gLf6MgqZnTC6eXKn3HQEImRFp0tARxI58mMlfVNYYr6USeRTiRxl Ci6h/hQXSwBd9Lg+3TtCyDXc9kSCW2ZDWpZqyRzxv6I7ze4sCtCCO8u2rmpvL28UHqsU QwjlM4ZvyrCt+ZVtdGN6nWDcV5eOP8doNTjDasmoqxW9MdOycrMKFxKktedfAt9qDmwE nqprvRD0JeDa3alc6BWwxb5Z15En1JDpMqGASWKn4EFSQKx+TRI77TxDi3JGQTL2rLDE 6/QQ== X-Gm-Message-State: AOAM531GrJb1hWTbbJ9vTfmZcfrqgF4ntu4LMHmUU/egBpa71shZz4qf c810i/KvipBEM/dqen4Cje8EcfiqzqBTsJL7gw== X-Google-Smtp-Source: ABdhPJy9MIn39nszadJy4TGmciC+Phtwg3irGAJ8Z0MmmzIQBmJ0g8Fd8p3i/9X7ZdC1H/f6r8RSPw1B8z+qXReY+g== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:500a:9ac2:e73e:69e3]) (user=kaleshsingh job=sendgmr) by 2002:a0d:e244:0:b0:2db:d6a8:14c9 with SMTP id l65-20020a0de244000000b002dbd6a814c9mr21314233ywe.402.1647288235164; Mon, 14 Mar 2022 13:03:55 -0700 (PDT) Date: Mon, 14 Mar 2022 13:01:12 -0700 In-Reply-To: <20220314200148.2695206-1-kaleshsingh@google.com> Message-Id: <20220314200148.2695206-4-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220314200148.2695206-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.35.1.723.g4982287a31-goog Subject: [PATCH v6 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" , Stephen Boyd , 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-20220314_130357_387187_72F2E37B X-CRM114-Status: GOOD ( 20.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 Map the stack pages in the flexible private VA range and allocate guard pages below the stack as unbacked VA space. The stack is aligned so that any valid stack address has PAGE_SHIFT bit as 1 - this is used for overflow detection (implemented in a subsequent patch in the series). Signed-off-by: Kalesh Singh Tested-by: Fuad Tabba Reviewed-by: Fuad Tabba --- Changes in v6: - Update call to hyp_alloc_private_va_range() (return val and params) Changes in v5: - Use a single allocation for stack and guard pages to ensure they are contiguous, per Marc 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/include/asm/kvm_mmu.h | 3 +++ arch/arm64/kvm/arm.c | 39 +++++++++++++++++++++++++++++--- arch/arm64/kvm/mmu.c | 4 ++-- 4 files changed, 42 insertions(+), 5 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/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h index 3cc9aa25f510..967a9ea9a47b 100644 --- a/arch/arm64/include/asm/kvm_mmu.h +++ b/arch/arm64/include/asm/kvm_mmu.h @@ -116,6 +116,9 @@ alternative_cb_end #include #include +extern struct kvm_pgtable *hyp_pgtable; +extern struct mutex kvm_hyp_pgd_mutex; + void kvm_update_va_mask(struct alt_instr *alt, __le32 *origptr, __le32 *updptr, int nr_inst); void kvm_compute_layout(void); diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 4dca6ffd03d4..72be7e695d8d 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -1538,7 +1538,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; @@ -1987,14 +1986,48 @@ 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 hyp_addr; + /* + * Allocate a contiguous HYP private VA range for the stack + * and guard page. The allocation is also aligned based on + * the order of its size. + */ + err = hyp_alloc_private_va_range(PAGE_SIZE * 2, &hyp_addr); + if (err) { + kvm_err("Cannot allocate hyp stack guard page\n"); + goto out_err; + } + + /* + * Since the stack grows downwards, map the stack to the page + * at the higher address and leave the lower guard page + * unbacked. + * + * Any valid stack address now has the PAGE_SHIFT bit as 1 + * and addresses corresponding to the guard page have the + * PAGE_SHIFT bit as 0 - this is used for overflow detection. + */ + mutex_lock(&kvm_hyp_pgd_mutex); + err = kvm_pgtable_hyp_map(hyp_pgtable, hyp_addr + PAGE_SIZE, + PAGE_SIZE, __pa(stack_page), PAGE_HYP); + mutex_unlock(&kvm_hyp_pgd_mutex); if (err) { kvm_err("Cannot map hyp stack\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); + + params->stack_hyp_va = hyp_addr + (2 * PAGE_SIZE); } for_each_possible_cpu(cpu) { diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 7326d683c500..9bfc6d8f3c49 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -22,8 +22,8 @@ #include "trace.h" -static struct kvm_pgtable *hyp_pgtable; -static DEFINE_MUTEX(kvm_hyp_pgd_mutex); +struct kvm_pgtable *hyp_pgtable; +DEFINE_MUTEX(kvm_hyp_pgd_mutex); static unsigned long hyp_idmap_start; static unsigned long hyp_idmap_end;