From nobody Wed Apr 16 14:21:51 2025 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 4Zd3B82fxRz5slCB; Wed, 16 Apr 2025 14:21:52 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Zd3B76vXrz3vrR; Wed, 16 Apr 2025 14:21:51 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1744813312; 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=3yidWfObfzyMzUF3GQz4s+R31SSA3wyKXVIH1AQctEI=; b=hwsd+oryyyZXjcAdxXYZVgMib5PPBkWDetVPce+jxeoXtxewCSF0AWotOmMWeEgZaisKTt +OQL3yb1BIWaLI7Bq9iQzKO1j9H7Z0fy/amP4p5Fz1fkKJV6PKb2E1kh9ZWyG81PwiB5QI 9z7qCG+ykXds5WJ5q8/BEaikHmZa64L9HHjmvbloiGhQv9OTFNw0ixDHkG3xgaEa3vVYDE KIS0pUubEUiEWPbP3n4PAK4h3u+U/riM3JOPG6wSXYVZXejF1/cca02JGuy28ObZd0w94m JDvvSZbnBRlfw9c9xxBR3c7jYJNGxSjnbewHu4OBgyU4OS9B9Ys+TUuDhtFNFQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1744813312; a=rsa-sha256; cv=none; b=Ad7hYn9wobF8h1UJpIPgduGib7ka0mAK2R4H9mcfz3jPyhEnEG0WS3+balysg50vR0Y2Ct /Xr5dw79nuPHCNL5gwW6lOEOKbwr+lNlAetIgZiTiSRP22tgSD+U9UVbQoxfSuL05iwCUF NfTn8oh3fTWOBJoIe5arFH6+MWoQguEHQLeaIoX35EIpNb+0fLk4Ay0PdMIxUHlY3mI3Pf yVHviZPbfO421jPqGKsWnHNnXpWjO9Dx/IXFBymWDt7UEaekP+PirtD9hC988tu2JeZ1dF kp3ftfw5VaQrXuuksucFN5p3BgVqBm75HUVkgJFj4xgKIJGkaV9eeX95IoA40A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1744813312; 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=3yidWfObfzyMzUF3GQz4s+R31SSA3wyKXVIH1AQctEI=; b=uDMFXf7rQQFcjfeBUXZ5E7OF7Ar2mw8HMaxtrGn1i3SMB5czfhO33PfCUauBoB8li2aWiL mLLwfVxW48TD4BhCkmlZBp9Z6SiSAWNr9SHS8TTrw5xHILAvR5opKRRnVTCr22Fr5Um/J5 pPT26c8foEee8Iyj60VJg83jOWfuIYTEU2LYNemEHrvGwanHDrwFe5gN/0qgdNKfWPiz5x h+KrXbCJmzsehTJbNfAFDv/6U+Bh+JGVPb8xycCnmRAVp/n2BU3c0Kxjx9O5UkYnLenbBD j8+eJkRrPLBb0LeHWRpOknyeHgLi5s6an/5UUemr1Ogm/VfqeJXfTSGGV7NSiw== 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 4Zd3B76DZ4z1L6l; Wed, 16 Apr 2025 14:21:51 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 53GELpSl059589; Wed, 16 Apr 2025 14:21:51 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 53GELphv059586; Wed, 16 Apr 2025 14:21:51 GMT (envelope-from git) Date: Wed, 16 Apr 2025 14:21:51 GMT Message-Id: <202504161421.53GELphv059586@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Bojan =?utf-8?Q?Novkovi=C4=87?= Subject: git: f1e18f331923 - main - riscv: Exclude OpenSBI memory regions when booting with EFI 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: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bnovkov X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f1e18f331923041980149fef46cdb2736e61debb Auto-Submitted: auto-generated The branch main has been updated by bnovkov: URL: https://cgit.FreeBSD.org/src/commit/?id=f1e18f331923041980149fef46cdb2736e61debb commit f1e18f331923041980149fef46cdb2736e61debb Author: Bojan Novković AuthorDate: 2025-04-15 16:28:05 +0000 Commit: Bojan Novković CommitDate: 2025-04-16 14:20:13 +0000 riscv: Exclude OpenSBI memory regions when booting with EFI OpenSBI uses the first PMP entry to prevent buggy supervisor software from overwriting the firmware [1]. However, this region may not be properly marked as reserved in the EFI map, leading to an access violation exception whenever the kernel attempts to write to a page from that region. Fix this by preemptively excluding first EFI memory map entry if it is marked as "BootServicesData". [1] https://github.com/riscv-non-isa/riscv-sbi-doc/pull/37 Reported by: tuexen Tested by: tuexen Fixes: a2e2178402af Reviewed by: imp, jrtc27 Differential Revision: https://reviews.freebsd.org/D49839 --- sys/riscv/riscv/machdep.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/sys/riscv/riscv/machdep.c b/sys/riscv/riscv/machdep.c index 516dbde5ffaa..f253bc9a853b 100644 --- a/sys/riscv/riscv/machdep.c +++ b/sys/riscv/riscv/machdep.c @@ -541,6 +541,22 @@ fdt_physmem_exclude_region_cb(const struct mem_region *mr, void *arg __unused) } #endif +static void +efi_exclude_sbi_pmp_cb(struct efi_md *p, void *argp) +{ + bool *first = (bool *)argp; + + if (!*first) + return; + + *first = false; + if (p->md_type == EFI_MD_TYPE_BS_DATA) { + physmem_exclude_region(p->md_phys, + min(p->md_pages * EFI_PAGE_SIZE, L2_SIZE), + EXFLAG_NOALLOC); + } +} + void initriscv(struct riscv_bootparams *rvbp) { @@ -548,6 +564,7 @@ initriscv(struct riscv_bootparams *rvbp) struct pcpu *pcpup; vm_offset_t lastaddr; vm_size_t kernlen; + bool first; char *env; TSRAW(&thread0, TS_ENTER, __func__, NULL); @@ -577,11 +594,22 @@ initriscv(struct riscv_bootparams *rvbp) if (efihdr != NULL) { efi_map_add_entries(efihdr); efi_map_exclude_entries(efihdr); + + /* + * OpenSBI uses the first PMP entry to prevent buggy supervisor + * software from overwriting the firmware. However, this + * region may not be properly marked as reserved, leading + * to an access violation exception whenever the kernel + * attempts to write to a page from that region. + * + * Fix this by excluding first EFI memory map entry + * if it is marked as "BootServicesData". + */ + first = true; + efi_map_foreach_entry(efihdr, efi_exclude_sbi_pmp_cb, &first); } #ifdef FDT else { - bool first; - /* Exclude reserved memory specified by the device tree. */ fdt_foreach_reserved_mem(fdt_physmem_exclude_region_cb, NULL);