From nobody Thu Feb 08 16:36:39 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TW2gX35Q4z5B0b1; Thu, 8 Feb 2024 16:36:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TW2gX09WPz4N2k; Thu, 8 Feb 2024 16:36:40 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1707410200; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mVxFxuz8vFOyzqX5WF7FVFLR/KTY4fMedAas4qmXzc0=; b=oMB5qlZEILK4FOULT5MpqIHIuhFka+k0ZK/GMYXrCZb4RkOjZZfiGn9FmHrnbwKTZk4Nn2 vEmRXqffoq6e3nb9gpd/IHhZGFhIqOgawR4nu9WPOKmirnL9SQjgiO9PPIn9KqYUzJcKhZ U4Xt5dsST6hzQHD4+23BGl/rGFerf8nQnRpBMp0yX9Wah5Ms1byyjqRLYPwSScx2rEoFAw gvdt9mrHidtqcnMrzGySXcJ7RJW5QMVt5t4Lvoq8E/3e5BqC1SW7LA2mXrxUsicsptZPWP +g/Jk5tl0PpyUCl6j61UWMdl+jU4od8NqWV1qf/uP3z6XphPrAG1QpjJfGg9fA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1707410200; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mVxFxuz8vFOyzqX5WF7FVFLR/KTY4fMedAas4qmXzc0=; b=FOKKq42PF5IqbkM+2yFSt+bXdLrTGcVEXwoE55CzstNyQaTZrXuZXPXsshkxtc4/XxP97o N58CMt2Bq/vDi8Lo/8Z7RrJsM3ZvvuCOi8QYPbpLx+f9cNbcjdlVpDxPvBlNSFbEm3a78y QNl1G9R6cPLTr+rJmUA3MK4Dpt2g4pN9VkOMO84hBFtR7xSOPD9dgw8rG+utqhah/rLhmH Lvo4xPQDIFJ1VZCR9A2f95xgBT8fyEwrhTMoBNszcHQRsaZ1MZNW0hmEkF7CQW2yx5PXjn mrvzdtakYdmXOx5kIkb1l/HPOri5tnKNyRp8V/GhdvALF9cES5ZkoqajitI2HQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1707410200; a=rsa-sha256; cv=none; b=grYKq60vBXp8ZEHNKhtaq4jTRJirz/WwaB99Mq/HWKccBKZfCxY9Sp4Ag4+wAOh2xSJA1G l3CIkEsmFTEaj3C4D0FlwdrD7txTiFzfkI4gnbGoQlXm8Wwirv4ydP1i+M6cGStZwjLqEj sNGTVnH40oowVyV28VbkhGIWz4UB435qLDbLUFbh67brP8VEhKKwkR5DYu3dF4LAKjYefN DDnC1F3xX1I/A1mXB/QX6i+jIr4u5gFWSVi6LNlBHYYHwFkxrDOeBP68wZOL0CBQ+lISEy rm+MaT/fhnRrgDLiMIYag8DfIcFJjzJx9t/BuOIktNYWdXCFArBvFYQyVi3Wyw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TW2gW66QZzxFF; Thu, 8 Feb 2024 16:36:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 418GadEZ021032; Thu, 8 Feb 2024 16:36:39 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 418Gadq4021029; Thu, 8 Feb 2024 16:36:39 GMT (envelope-from git) Date: Thu, 8 Feb 2024 16:36:39 GMT Message-Id: <202402081636.418Gadq4021029@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 6631b5894234 - main - arm64: Simplify and improve KASAN shadow map bootstrapping List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6631b589423407c3f05bfebc62ae230ae496e061 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=6631b589423407c3f05bfebc62ae230ae496e061 commit 6631b589423407c3f05bfebc62ae230ae496e061 Author: Mark Johnston AuthorDate: 2024-02-08 16:10:43 +0000 Commit: Mark Johnston CommitDate: 2024-02-08 16:35:11 +0000 arm64: Simplify and improve KASAN shadow map bootstrapping - Move pmap_bootstrap_allocate_kasan_l2() close to the place where it is actually used. - Simplify pmap_bootstrap_allocate_kasan_l2() a bit: eliminate some unneeded variables and zero and exclude each 2MB mapping as we go rather than doing that all at once. Excluded regions will be coalesced. - As a consequence of the previous point, ensure that we do not zero a preexisting 2MB mapping. - Simplify pmap_bootstrap_san() and prepare it to work with KMSAN. MFC after: 2 weeks Sponsored by: Klara, Inc. Sponsored by: Juniper Networks, Inc. Differential Revision: https://reviews.freebsd.org/D43404 --- sys/arm64/arm64/pmap.c | 112 +++++++++++++++++++++---------------------------- 1 file changed, 48 insertions(+), 64 deletions(-) diff --git a/sys/arm64/arm64/pmap.c b/sys/arm64/arm64/pmap.c index 6a84b6bb80f8..93f513832b97 100644 --- a/sys/arm64/arm64/pmap.c +++ b/sys/arm64/arm64/pmap.c @@ -1224,53 +1224,6 @@ pmap_bootstrap_l3(vm_offset_t va) pmap_bootstrap_l2_table(&bs_state); } -#ifdef KASAN -static void -pmap_bootstrap_allocate_kasan_l2(vm_paddr_t start_pa, vm_paddr_t end_pa, - vm_offset_t *start_va, int *nkasan_l2) -{ - int i; - vm_paddr_t pa; - vm_offset_t va; - pd_entry_t *l2; - - va = *start_va; - pa = rounddown2(end_pa - L2_SIZE, L2_SIZE); - l2 = pmap_l2(kernel_pmap, va); - - for (i = 0; pa >= start_pa && i < *nkasan_l2; - i++, va += L2_SIZE, pa -= L2_SIZE, l2++) { - /* - * KASAN stack checking results in us having already allocated - * part of our shadow map, so we can just skip those segments. - */ - if ((pmap_load(l2) & ATTR_DESCR_VALID) != 0) { - pa += L2_SIZE; - continue; - } - - pmap_store(l2, PHYS_TO_PTE(pa) | PMAP_SAN_PTE_BITS | L2_BLOCK); - } - - /* - * Ended the allocation due to start_pa constraint, rather than because - * we allocated everything. Adjust back up to the start_pa and remove - * the invalid L2 block from our accounting. - */ - if (pa < start_pa) { - va += L2_SIZE; - i--; - pa = start_pa; - } - - bzero((void *)PHYS_TO_DMAP(pa), i * L2_SIZE); - physmem_exclude_region(pa, i * L2_SIZE, EXFLAG_NOALLOC); - - *nkasan_l2 -= i; - *start_va = va; -} -#endif - /* * Bootstrap the system enough to run with virtual memory. */ @@ -1368,19 +1321,48 @@ pmap_bootstrap(vm_size_t kernlen) cpu_tlb_flushID(); } -#if defined(KASAN) +#ifdef KASAN +static void +pmap_bootstrap_allocate_kasan_l2(vm_paddr_t start_pa, vm_paddr_t end_pa, + vm_offset_t *vap, vm_offset_t eva) +{ + vm_paddr_t pa; + vm_offset_t va; + pd_entry_t *l2; + + va = *vap; + pa = rounddown2(end_pa - L2_SIZE, L2_SIZE); + for (; pa >= start_pa && va < eva; va += L2_SIZE, pa -= L2_SIZE) { + l2 = pmap_l2(kernel_pmap, va); + + /* + * KASAN stack checking results in us having already allocated + * part of our shadow map, so we can just skip those segments. + */ + if ((pmap_load(l2) & ATTR_DESCR_VALID) != 0) { + pa += L2_SIZE; + continue; + } + + bzero((void *)PHYS_TO_DMAP(pa), L2_SIZE); + physmem_exclude_region(pa, L2_SIZE, EXFLAG_NOALLOC); + pmap_store(l2, PHYS_TO_PTE(pa) | PMAP_SAN_PTE_BITS | L2_BLOCK); + } + *vap = va; +} + /* * Finish constructing the initial shadow map: * - Count how many pages from KERNBASE to virtual_avail (scaled for * shadow map) * - Map that entire range using L2 superpages. */ -void -pmap_bootstrap_san(void) +static void +pmap_bootstrap_san1(vm_offset_t va, int scale) { - vm_offset_t va; + vm_offset_t eva; vm_paddr_t kernstart; - int i, shadow_npages, nkasan_l2; + int i; kernstart = pmap_early_vtophys(KERNBASE); @@ -1392,19 +1374,14 @@ pmap_bootstrap_san(void) physmap_idx = physmem_avail(physmap, nitems(physmap)); physmap_idx /= 2; - shadow_npages = (virtual_avail - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE; - shadow_npages = howmany(shadow_npages, KASAN_SHADOW_SCALE); - nkasan_l2 = howmany(shadow_npages, Ln_ENTRIES); - - /* Map the valid KVA up to this point. */ - va = KASAN_MIN_ADDRESS; + eva = va + (virtual_avail - VM_MIN_KERNEL_ADDRESS) / scale; /* * Find a slot in the physmap large enough for what we needed. We try to put * the shadow map as high up as we can to avoid depleting the lower 4GB in case * it's needed for, e.g., an xhci controller that can only do 32-bit DMA. */ - for (i = (physmap_idx * 2) - 2; i >= 0 && nkasan_l2 > 0; i -= 2) { + for (i = (physmap_idx * 2) - 2; i >= 0; i -= 2) { vm_paddr_t plow, phigh; /* L2 mappings must be backed by memory that is L2-aligned */ @@ -1414,12 +1391,13 @@ pmap_bootstrap_san(void) continue; if (kernstart >= plow && kernstart < phigh) phigh = kernstart; - if (phigh - plow >= L2_SIZE) - pmap_bootstrap_allocate_kasan_l2(plow, phigh, &va, - &nkasan_l2); + if (phigh - plow >= L2_SIZE) { + pmap_bootstrap_allocate_kasan_l2(plow, phigh, &va, eva); + if (va >= eva) + break; + } } - - if (nkasan_l2 != 0) + if (i < 0) panic("Could not find phys region for shadow map"); /* @@ -1431,6 +1409,12 @@ pmap_bootstrap_san(void) * is handled by pmap_san_enter(). */ } + +void +pmap_bootstrap_san(void) +{ + pmap_bootstrap_san1(KASAN_MIN_ADDRESS, KASAN_SHADOW_SCALE); +} #endif /*