From nobody Mon Mar 03 16:12:28 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 4Z63k46HCxz5pjW9; Mon, 03 Mar 2025 16:12:28 +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 4Z63k426Ljz40N2; Mon, 03 Mar 2025 16:12:28 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1741018348; 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=IPKM4IO85K7Iws4zUOAgHxoXF2XFAtM+5Z8VJyYSeOo=; b=qOUza8b5BcGkb726fRNDipMuf+BrWoQzKZjXNqnR0xFKTX9afy1R/1XU4E1C5SssS1r8zz FrvkaXCRMpBpzMBwyoa8iOig4zW1ey/vcWcv6a2OAB3JjEeCM8cPtIXJO4zfqwiPmhYQAh nTqpDO87iuADNrUdu2v9jFfemorpg0wyfO40kDXUzuCM0IYQlJHZGetafPbpZI2PgfGBUw m+DZRL4QvO4ARE+c9PV1rZnBHayFkv/i/9lYxO+OXM8s5ym7JLGDwWIBbOxkak4Wwcldut FsMVMtfL3Cu84Smc27FL1wQRYRi9SzXF/Z47F632JENjFPTzUXV41OU6k2mrJg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1741018348; a=rsa-sha256; cv=none; b=w3m/BdewKj8FZhMLMBIlgAvQBXvQyjkMA4NlzKEIPuTyn6BO5tkg9CpWG3h3Hwq6mQmls5 Dt03vitnVQXBlZZYAmBTLC1yYrPDPGcTT7AKFUcBJssZTUCZszHEiPlbZmkoGU7YWow33M el5n4GmElL+jIEZ8yboY+EvOTh3dINAkC8E3AxW20RnAPn5gDvHlcSzR1MYABgcxCOOPvH 54O0+DpsYHxlL7mtRz/67Wp648ewE+gXDyl0lIEQtoG62KRBuE9AnQLFfEo2i0POKF4TkB 5wZNoZFnuZMV1whcC89Yxhb+3gwEVuksue3fWOM3XXeV+QN3TjfB8tgP5SrU6g== 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=1741018348; 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=IPKM4IO85K7Iws4zUOAgHxoXF2XFAtM+5Z8VJyYSeOo=; b=snrRvIh5tlqq4b4CUPCa5Q+nkihCPAf/o4rSTi4qrD2cyTUUt81NNEpE/pFZ1MR8K0Rntc D4oOMtwiLUsuJj/Q7fefrPrvbOzsziLaPsCxv/2Z/2urqTT28M9uismrWDssLF+eXwK3Zd xqJ8ZSk2lGb/T4mnSnbEZUPAVr9TnrFHAJdVPi6toXBNqy1pXW1nFNU5DvK/EdRNyquxgE YFYnjhiDzV2aGp1b4yxnQP7bWRvfucgdDELWp8tc+gd7xLiwPKIj0Ae8W6quyyYdq+aWjo BdavsQO9orj9TiBOWQo+303H9cAg8uCyq/XGG6RCS1GrgNjvLekvbw+R4dLB0Q== 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 4Z63k41HsBzvYV; Mon, 03 Mar 2025 16:12:28 +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 523GCSLE007246; Mon, 3 Mar 2025 16:12:28 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 523GCSQJ007243; Mon, 3 Mar 2025 16:12:28 GMT (envelope-from git) Date: Mon, 3 Mar 2025 16:12:28 GMT Message-Id: <202503031612.523GCSQJ007243@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mitchell Horne Subject: git: a2e2178402af - main - riscv: parse memory regions from EFI map header 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: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a2e2178402afff939cb90e75c65ded0a9da8ac5c Auto-Submitted: auto-generated The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=a2e2178402afff939cb90e75c65ded0a9da8ac5c commit a2e2178402afff939cb90e75c65ded0a9da8ac5c Author: Mitchell Horne AuthorDate: 2025-03-03 15:49:17 +0000 Commit: Mitchell Horne CommitDate: 2025-03-03 16:12:15 +0000 riscv: parse memory regions from EFI map header Using the newly isolated efi_map interface (subr_efi_map.c). The primary goal is a greater adherence to the EFI firmware's view of memory, and protection of its reserved regions. This is a better source of truth than the device tree. For example, regions marked as RuntimeServicesData must be excluded from general memory allocations for EFI runtime services to eventually be enabled on the platform (planned but not currently in progress). The secondary impact is when loading a new/custom device tree via loader(8); use of the EFI map ensures that reservations made by firmware (OpenSBI, u-boot) are maintained. Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D49131 --- sys/conf/files.riscv | 1 + sys/riscv/riscv/machdep.c | 63 ++++++++++++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/sys/conf/files.riscv b/sys/conf/files.riscv index cc18ecb6eb36..0d782239c89c 100644 --- a/sys/conf/files.riscv +++ b/sys/conf/files.riscv @@ -30,6 +30,7 @@ kern/msi_if.m standard kern/pic_if.m standard kern/subr_devmap.c standard kern/subr_dummy_vdso_tc.c standard +kern/subr_efi_map.c standard kern/subr_intr.c standard kern/subr_physmem.c standard libkern/bcopy.c standard diff --git a/sys/riscv/riscv/machdep.c b/sys/riscv/riscv/machdep.c index 8d32d348df7d..03f5926c3739 100644 --- a/sys/riscv/riscv/machdep.c +++ b/sys/riscv/riscv/machdep.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -468,6 +469,7 @@ void initriscv(struct riscv_bootparams *rvbp) { struct mem_region mem_regions[FDT_MEM_REGIONS]; + struct efi_map_header *efihdr; struct pcpu *pcpup; int mem_regions_sz; vm_offset_t lastaddr; @@ -516,21 +518,40 @@ initriscv(struct riscv_bootparams *rvbp) } pcpup->pc_hart = boot_hart; -#ifdef FDT - /* - * Exclude reserved memory specified by the device tree. Typically, - * this contains an entry for memory used by the runtime SBI firmware. - */ - if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0) { - physmem_exclude_regions(mem_regions, mem_regions_sz, - EXFLAG_NODUMP | EXFLAG_NOALLOC); + efihdr = (struct efi_map_header *)preload_search_info(preload_kmdp, + MODINFO_METADATA | MODINFOMD_EFI_MAP); + if (efihdr != NULL) { + efi_map_add_entries(efihdr); + efi_map_exclude_entries(efihdr); } +#ifdef FDT + else { + /* Exclude reserved memory specified by the device tree. */ + if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0) { + physmem_exclude_regions(mem_regions, mem_regions_sz, + EXFLAG_NODUMP | EXFLAG_NOALLOC); + } - /* Grab physical memory regions information from device tree. */ - if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0) { - panic("Cannot get physical memory regions"); + /* Grab physical memory regions information from device tree. */ + if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0) + panic("Cannot get physical memory regions"); + physmem_hardware_regions(mem_regions, mem_regions_sz); + + /* + * XXX: Unconditionally exclude the lowest 2MB of physical + * memory, as this area is assumed to contain the SBI firmware, + * and this is not properly reserved in all cases (e.g. in + * older firmware like BBL). + * + * This is a little fragile, but it is consistent with the + * platforms we support so far. + * + * TODO: remove this when the all regular booting methods + * properly report their reserved memory in the device tree. + */ + physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE, + EXFLAG_NODUMP | EXFLAG_NOALLOC); } - physmem_hardware_regions(mem_regions, mem_regions_sz); #endif /* @@ -541,19 +562,6 @@ initriscv(struct riscv_bootparams *rvbp) /* Do basic tuning, hz etc */ init_param1(); -#ifdef FDT - /* - * XXX: Unconditionally exclude the lowest 2MB of physical memory, as - * this area is assumed to contain the SBI firmware. This is a little - * fragile, but it is consistent with the platforms we support so far. - * - * TODO: remove this when the all regular booting methods properly - * report their reserved memory in the device tree. - */ - physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE, - EXFLAG_NODUMP | EXFLAG_NOALLOC); -#endif - /* Bootstrap enough of pmap to enter the kernel proper */ kernlen = (lastaddr - KERNBASE); pmap_bootstrap(rvbp->kern_phys, kernlen); @@ -588,8 +596,11 @@ initriscv(struct riscv_bootparams *rvbp) if (env != NULL) strlcpy(kernelname, env, sizeof(kernelname)); - if (boothowto & RB_VERBOSE) + if (boothowto & RB_VERBOSE) { + if (efihdr != NULL) + efi_map_print_entries(efihdr); physmem_print_tables(); + } early_boot = 0;