From nobody Mon Mar 03 16:12:24 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 4Z63k06yyjz5pjW5; Mon, 03 Mar 2025 16:12:24 +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 4Z63k06HlDz40St; Mon, 03 Mar 2025 16:12:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1741018344; 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=FsJY5TkH9DF19kD5RapnFnBBDZxHvkyyNncpHJlpWmo=; b=BJsU9s+dqfz0uGqA+sOv+SYUPKkWvroh4Seit4G14ECBqfvDhl2F4JfkH8IfPMCOeo1QVU Od3v1WeY+g2pnSg0Lcdi3DFUbDqPouB+g88u4hZuN/cnDWWs0PQYN+jiO7FMClbxJIB0Pp DYtaj10MhGE0GXqmTimKCo7XtpKfe+JVPZW+TPDR57vD+VTkzYbDlL6uuRcvpkBQXTwjlP tp3rjadNwQbQExiMn6hVP99KaPZy4/XGU/NDxcWWMUGYwsg+9XnmU3fwhw0WQpDZD/qK4J xiE/hZ70kFtfHNbielW5oHbK9htdbF14uEXSuy7en+pXJ/UPvpqBWbnPDTlXyw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1741018344; a=rsa-sha256; cv=none; b=bS/uNZ7A3bGr+mokufOfzMoDb3GQsXCJhah5tvbs7Ihmf1Fkzb4K2I8oWZkhjHKRUTTn4p bNrw15NkKV+qhzWRbv6yEk3+28FPQpF6RHnFH8nJs4ulZKqHxU4a6isl15985B4HZ4v7M8 vpaZ2jY8L6/L0BtOKd3l1ONCCywfP4ZLRnXWjL/+LtghKD4EhVABEn5bG1DdrWg5fGtbdm Myaro7fLPbxhaaX6ox+SW8Mc92POn9kXk+CP7uGXtVKHO5mUYQbxDAdWYzlJRUuyOxL05g MxP4c5aqDSPgle9ty8W6FkdhoxWs1/QQPeXt8HUDZxHJApBgDpJKqtKly4Y41A== 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=1741018344; 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=FsJY5TkH9DF19kD5RapnFnBBDZxHvkyyNncpHJlpWmo=; b=xC1BsZ5xpujAXNQZtCkLjoNJ4l/JwgPcKpVL5xIX8EKAL0kZ7v/cUGO+MPXbTLxpqknNQ3 dAekwTjLYiYUZX0OYkzZhpJkRQRqYi2OtWlP8Zc8pr/z+tQ/69/IsFZEST6QZ4fQvPGcPI RRTEFzClGHYBQp1+0oj2gMiuqE/WAT9KWNkoHy1aii1qkuZ9YXu7t7EKV3p0TNLej+A4w3 PbIdJXdmQDtPWJ3NLF+jnCm8UZsZfZVKcgzJiUSSg23oCSUcEX1RMSU9uuw4vQxs2/9tBy qPXlZpDAuhITp62/AqgwReT6oKqFRU3JD9XXHAwW0lSwadZSxOR4ZZCk0P2dBQ== 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 4Z63k05Rfjzv8n; Mon, 03 Mar 2025 16:12:24 +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 523GCOEu007142; Mon, 3 Mar 2025 16:12:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 523GCOm4007139; Mon, 3 Mar 2025 16:12:24 GMT (envelope-from git) Date: Mon, 3 Mar 2025 16:12:24 GMT Message-Id: <202503031612.523GCOm4007139@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: 125877c92d16 - main - arm64: Split EFI map parsing code to a common file 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: 125877c92d1661c6a1f567aa9a3e9c399a514789 Auto-Submitted: auto-generated The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=125877c92d1661c6a1f567aa9a3e9c399a514789 commit 125877c92d1661c6a1f567aa9a3e9c399a514789 Author: Mitchell Horne AuthorDate: 2025-03-01 00:04:38 +0000 Commit: Mitchell Horne CommitDate: 2025-03-03 16:12:15 +0000 arm64: Split EFI map parsing code to a common file Motivation: I would like to enable the EFI memory map parsing on riscv, which is preferable to parsing memory regions from the device tree. I could easily duplicate the functionality, but it can be shared without much extra effort. There are a couple of similar implementations existing already. In this commit, the arm64 version of the code is moved to the new file, and enabled for this platform. No functional change intended. armv7 and riscv will follow. amd64 could be converted to use this interface too, but it doesn't use the common "physmem" interfaces for managing physical memory, and would therefore require further tweaks. I am not inclined to make this change at this time. Reviewed by: imp, andrew Discussed with: emaste (copyright attribution) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47746 --- sys/arm64/arm64/machdep.c | 175 ++------------------------------------------- sys/conf/files.arm64 | 1 + sys/kern/subr_efi_map.c | 178 ++++++++++++++++++++++++++++++++++++++++++++++ sys/sys/efi_map.h | 22 ++++++ 4 files changed, 206 insertions(+), 170 deletions(-) diff --git a/sys/arm64/arm64/machdep.c b/sys/arm64/arm64/machdep.c index 22c74b21cf48..a94cdaa61775 100644 --- a/sys/arm64/arm64/machdep.c +++ b/sys/arm64/arm64/machdep.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -457,172 +458,6 @@ arm64_get_writable_addr(void *addr, void **out) return (false); } -typedef void (*efi_map_entry_cb)(struct efi_md *, void *argp); - -static void -foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb, void *argp) -{ - struct efi_md *map, *p; - size_t efisz; - int ndesc, i; - - /* - * Memory map data provided by UEFI via the GetMemoryMap - * Boot Services API. - */ - efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; - map = (struct efi_md *)((uint8_t *)efihdr + efisz); - - if (efihdr->descriptor_size == 0) - return; - ndesc = efihdr->memory_size / efihdr->descriptor_size; - - for (i = 0, p = map; i < ndesc; i++, - p = efi_next_descriptor(p, efihdr->descriptor_size)) { - cb(p, argp); - } -} - -/* - * Handle the EFI memory map list. - * - * We will make two passes at this, the first (exclude == false) to populate - * physmem with valid physical memory ranges from recognized map entry types. - * In the second pass we will exclude memory ranges from physmem which must not - * be used for general allocations, either because they are used by runtime - * firmware or otherwise reserved. - * - * Adding the runtime-reserved memory ranges to physmem and excluding them - * later ensures that they are included in the DMAP, but excluded from - * phys_avail[]. - * - * Entry types not explicitly listed here are ignored and not mapped. - */ -static void -handle_efi_map_entry(struct efi_md *p, void *argp) -{ - bool exclude = *(bool *)argp; - - switch (p->md_type) { - case EFI_MD_TYPE_RECLAIM: - /* - * The recomended location for ACPI tables. Map into the - * DMAP so we can access them from userspace via /dev/mem. - */ - case EFI_MD_TYPE_RT_CODE: - /* - * Some UEFI implementations put the system table in the - * runtime code section. Include it in the DMAP, but will - * be excluded from phys_avail. - */ - case EFI_MD_TYPE_RT_DATA: - /* - * Runtime data will be excluded after the DMAP - * region is created to stop it from being added - * to phys_avail. - */ - if (exclude) { - physmem_exclude_region(p->md_phys, - p->md_pages * EFI_PAGE_SIZE, EXFLAG_NOALLOC); - break; - } - /* FALLTHROUGH */ - case EFI_MD_TYPE_CODE: - case EFI_MD_TYPE_DATA: - case EFI_MD_TYPE_BS_CODE: - case EFI_MD_TYPE_BS_DATA: - case EFI_MD_TYPE_FREE: - /* - * We're allowed to use any entry with these types. - */ - if (!exclude) - physmem_hardware_region(p->md_phys, - p->md_pages * EFI_PAGE_SIZE); - break; - default: - /* Other types shall not be handled by physmem. */ - break; - } -} - -static void -add_efi_map_entries(struct efi_map_header *efihdr) -{ - bool exclude = false; - foreach_efi_map_entry(efihdr, handle_efi_map_entry, &exclude); -} - -static void -exclude_efi_map_entries(struct efi_map_header *efihdr) -{ - bool exclude = true; - foreach_efi_map_entry(efihdr, handle_efi_map_entry, &exclude); -} - -static void -print_efi_map_entry(struct efi_md *p, void *argp __unused) -{ - const char *type; - static const char *types[] = { - "Reserved", - "LoaderCode", - "LoaderData", - "BootServicesCode", - "BootServicesData", - "RuntimeServicesCode", - "RuntimeServicesData", - "ConventionalMemory", - "UnusableMemory", - "ACPIReclaimMemory", - "ACPIMemoryNVS", - "MemoryMappedIO", - "MemoryMappedIOPortSpace", - "PalCode", - "PersistentMemory" - }; - - if (p->md_type < nitems(types)) - type = types[p->md_type]; - else - type = ""; - printf("%23s %012lx %012lx %08lx ", type, p->md_phys, - p->md_virt, p->md_pages); - if (p->md_attr & EFI_MD_ATTR_UC) - printf("UC "); - if (p->md_attr & EFI_MD_ATTR_WC) - printf("WC "); - if (p->md_attr & EFI_MD_ATTR_WT) - printf("WT "); - if (p->md_attr & EFI_MD_ATTR_WB) - printf("WB "); - if (p->md_attr & EFI_MD_ATTR_UCE) - printf("UCE "); - if (p->md_attr & EFI_MD_ATTR_WP) - printf("WP "); - if (p->md_attr & EFI_MD_ATTR_RP) - printf("RP "); - if (p->md_attr & EFI_MD_ATTR_XP) - printf("XP "); - if (p->md_attr & EFI_MD_ATTR_NV) - printf("NV "); - if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE) - printf("MORE_RELIABLE "); - if (p->md_attr & EFI_MD_ATTR_RO) - printf("RO "); - if (p->md_attr & EFI_MD_ATTR_RT) - printf("RUNTIME"); - printf("\n"); -} - -static void -print_efi_map_entries(struct efi_map_header *efihdr) -{ - - printf("%23s %12s %12s %8s %4s\n", - "Type", "Physical", "Virtual", "#Pages", "Attr"); - foreach_efi_map_entry(efihdr, print_efi_map_entry, NULL); -} - /* * Map the passed in VA in EFI space to a void * using the efi memory table to * find the PA and return it in the DMAP, if it exists. We're used between the @@ -659,7 +494,7 @@ efi_early_map(vm_offset_t va) { struct early_map_data emd = { .va = va }; - foreach_efi_map_entry(efihdr, efi_early_map_entry, &emd); + efi_map_foreach_entry(efihdr, efi_early_map_entry, &emd); if (emd.pa == 0) return NULL; return (void *)PHYS_TO_DMAP(emd.pa); @@ -942,7 +777,7 @@ initarm(struct arm64_bootparams *abp) efihdr = (struct efi_map_header *)preload_search_info(preload_kmdp, MODINFO_METADATA | MODINFOMD_EFI_MAP); if (efihdr != NULL) - add_efi_map_entries(efihdr); + efi_map_add_entries(efihdr); #ifdef FDT else { /* Grab physical memory regions information from device tree. */ @@ -972,7 +807,7 @@ initarm(struct arm64_bootparams *abp) pmap_bootstrap(lastaddr - KERNBASE); /* Exclude entries needed in the DMAP region, but not phys_avail */ if (efihdr != NULL) - exclude_efi_map_entries(efihdr); + efi_map_exclude_entries(efihdr); /* Do the same for reserve entries in the EFI MEMRESERVE table */ if (efi_systbl_phys != 0) exclude_efi_memreserve(efi_systbl_phys); @@ -1051,7 +886,7 @@ initarm(struct arm64_bootparams *abp) if (boothowto & RB_VERBOSE) { if (efihdr != NULL) - print_efi_map_entries(efihdr); + efi_map_print_entries(efihdr); physmem_print_tables(); } diff --git a/sys/conf/files.arm64 b/sys/conf/files.arm64 index 13c7d9b1875d..de3134ad9124 100644 --- a/sys/conf/files.arm64 +++ b/sys/conf/files.arm64 @@ -6,6 +6,7 @@ kern/msi_if.m optional intrng kern/pic_if.m optional intrng kern/subr_devmap.c standard +kern/subr_efi_map.c standard kern/subr_intr.c optional intrng kern/subr_physmem.c standard libkern/strlen.c standard diff --git a/sys/kern/subr_efi_map.c b/sys/kern/subr_efi_map.c new file mode 100644 index 000000000000..29a94858f6c1 --- /dev/null +++ b/sys/kern/subr_efi_map.c @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2014 The FreeBSD Foundation + * Copyright (c) 2018 Andrew Turner + * + * SPDX-License-Identifier: BSD-2-Clause + */ +#include +#include +#include +#include +#include + +#include +#include + +void +efi_map_foreach_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb, void *argp) +{ + struct efi_md *map, *p; + size_t efisz; + int ndesc, i; + + /* + * Memory map data provided by UEFI via the GetMemoryMap + * Boot Services API. + */ + efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; + map = (struct efi_md *)((uint8_t *)efihdr + efisz); + + if (efihdr->descriptor_size == 0) + return; + ndesc = efihdr->memory_size / efihdr->descriptor_size; + + for (i = 0, p = map; i < ndesc; i++, + p = efi_next_descriptor(p, efihdr->descriptor_size)) { + cb(p, argp); + } +} + +/* + * Handle the EFI memory map list. + * + * We will make two passes at this, the first (exclude == false) to populate + * physmem with valid physical memory ranges from recognized map entry types. + * In the second pass we will exclude memory ranges from physmem which must not + * be used for general allocations, either because they are used by runtime + * firmware or otherwise reserved. + * + * Adding the runtime-reserved memory ranges to physmem and excluding them + * later ensures that they are included in the DMAP, but excluded from + * phys_avail[]. + * + * Entry types not explicitly listed here are ignored and not mapped. + */ +static void +handle_efi_map_entry(struct efi_md *p, void *argp) +{ + bool exclude = *(bool *)argp; + + switch (p->md_type) { + case EFI_MD_TYPE_RECLAIM: + /* + * The recomended location for ACPI tables. Map into the + * DMAP so we can access them from userspace via /dev/mem. + */ + case EFI_MD_TYPE_RT_CODE: + /* + * Some UEFI implementations put the system table in the + * runtime code section. Include it in the DMAP, but will + * be excluded from phys_avail. + */ + case EFI_MD_TYPE_RT_DATA: + /* + * Runtime data will be excluded after the DMAP + * region is created to stop it from being added + * to phys_avail. + */ + if (exclude) { + physmem_exclude_region(p->md_phys, + p->md_pages * EFI_PAGE_SIZE, EXFLAG_NOALLOC); + break; + } + /* FALLTHROUGH */ + case EFI_MD_TYPE_CODE: + case EFI_MD_TYPE_DATA: + case EFI_MD_TYPE_BS_CODE: + case EFI_MD_TYPE_BS_DATA: + case EFI_MD_TYPE_FREE: + /* + * We're allowed to use any entry with these types. + */ + if (!exclude) + physmem_hardware_region(p->md_phys, + p->md_pages * EFI_PAGE_SIZE); + break; + default: + /* Other types shall not be handled by physmem. */ + break; + } +} + +void +efi_map_add_entries(struct efi_map_header *efihdr) +{ + bool exclude = false; + efi_map_foreach_entry(efihdr, handle_efi_map_entry, &exclude); +} + +void +efi_map_exclude_entries(struct efi_map_header *efihdr) +{ + bool exclude = true; + efi_map_foreach_entry(efihdr, handle_efi_map_entry, &exclude); +} + +static void +print_efi_map_entry(struct efi_md *p, void *argp __unused) +{ + const char *type; + static const char *types[] = { + "Reserved", + "LoaderCode", + "LoaderData", + "BootServicesCode", + "BootServicesData", + "RuntimeServicesCode", + "RuntimeServicesData", + "ConventionalMemory", + "UnusableMemory", + "ACPIReclaimMemory", + "ACPIMemoryNVS", + "MemoryMappedIO", + "MemoryMappedIOPortSpace", + "PalCode", + "PersistentMemory" + }; + + if (p->md_type < nitems(types)) + type = types[p->md_type]; + else + type = ""; + printf("%23s %012lx %012lx %08lx ", type, p->md_phys, + p->md_virt, p->md_pages); + if (p->md_attr & EFI_MD_ATTR_UC) + printf("UC "); + if (p->md_attr & EFI_MD_ATTR_WC) + printf("WC "); + if (p->md_attr & EFI_MD_ATTR_WT) + printf("WT "); + if (p->md_attr & EFI_MD_ATTR_WB) + printf("WB "); + if (p->md_attr & EFI_MD_ATTR_UCE) + printf("UCE "); + if (p->md_attr & EFI_MD_ATTR_WP) + printf("WP "); + if (p->md_attr & EFI_MD_ATTR_RP) + printf("RP "); + if (p->md_attr & EFI_MD_ATTR_XP) + printf("XP "); + if (p->md_attr & EFI_MD_ATTR_NV) + printf("NV "); + if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE) + printf("MORE_RELIABLE "); + if (p->md_attr & EFI_MD_ATTR_RO) + printf("RO "); + if (p->md_attr & EFI_MD_ATTR_RT) + printf("RUNTIME"); + printf("\n"); +} + +void +efi_map_print_entries(struct efi_map_header *efihdr) +{ + + printf("%23s %12s %12s %8s %4s\n", + "Type", "Physical", "Virtual", "#Pages", "Attr"); + efi_map_foreach_entry(efihdr, print_efi_map_entry, NULL); +} diff --git a/sys/sys/efi_map.h b/sys/sys/efi_map.h new file mode 100644 index 000000000000..4edabd4e5812 --- /dev/null +++ b/sys/sys/efi_map.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2014 The FreeBSD Foundation + * Copyright (c) 2018 Andrew Turner + * + * SPDX-License-Identifier: BSD-2-Clause + */ +#ifndef _SYS_EFI_MAP_H_ +#define _SYS_EFI_MAP_H_ + +#include +#include + +typedef void (*efi_map_entry_cb)(struct efi_md *, void *argp); + +void efi_map_foreach_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb, + void *argp); + +void efi_map_add_entries(struct efi_map_header *efihdr); +void efi_map_exclude_entries(struct efi_map_header *efihdr); +void efi_map_print_entries(struct efi_map_header *efihdr); + +#endif /* !_SYS_EFI_MAP_H_ */