Skip to contentSkip to conversation

EFI improvements for Xen dom0

PATCH In review Demi Marie Obenour 3 participants 34 messages Sep 29, 2022 – Oct 6, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover EFI improvements for Xen dom0

This fixes EFI table processing in Xen dom0 and adds ESRT support in that configuration.

Changes since v3:

- Check location of all configuration tables, not just the ESRT.
- Move most Xen-specific code to drivers/xen/.
- Allow configuration tables to be in EFI_ACPI_RECLAIM_MEMORY.

Demi Marie Obenour (2):
Avoid using EFI tables Xen may have clobbered
Support ESRT in Xen dom0

 drivers/firmware/efi/efi.c  |  8 ++++---
 drivers/firmware/efi/esrt.c | 43 ++++++++++++++++++++++++++-----------
 drivers/xen/efi.c           | 35 ++++++++++++++++++++++++++++++
 include/linux/efi.h         |  9 ++++++++
 4 files changed, 79 insertions(+), 16 deletions(-)
Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
#1 lore raw

PATCH v4 1/2 Avoid using EFI tables Xen may have clobbered

Memory of type EFI_CONVENTIONAL_MEMORY, EFI_LOADER_CODE, EFI_LOADER_DATA, EFI_BOOT_SERVICES_CODE, and EFI_BOOT_SERVICES_DATA may be clobbered by Xen before Linux gets to start using it. Therefore, Linux under Xen must not use EFI tables from such memory. Most of the remaining EFI memory types are not suitable for EFI tables, leaving only EFI_ACPI_RECLAIM_MEMORY, EFI_RUNTIME_SERVICES_DATA, and EFI_RUNTIME_SERVICES_CODE. When running under Xen, Linux should only use tables that are located in one of these types of memory.

This patch ensures this, and also adds a function (xen_config_table_memory_region_max()) that will be used later to replace the usage of the EFI memory map in esrt.c when running under Xen. This function can also be used in mokvar-table.c and efi-bgrt.c, but I have not implemented this.

---
 drivers/firmware/efi/efi.c |  8 +++++---
 drivers/xen/efi.c          | 35 +++++++++++++++++++++++++++++++++++
 include/linux/efi.h        |  9 +++++++++
 3 files changed, 49 insertions(+), 3 deletions(-)
drivers/firmware/efi/efi.c +5 -3
index e4080ad96089abd7f84745dd8461c548bcbb7685..d344f3ff73d1c5ed0c67e3251a9502e66719741d 100644
@@ -574,7 +574,6 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
 	unsigned long table;
 	int i;
 
-	pr_info("");
 	for (i = 0; i < count; i++) {
 		if (!IS_ENABLED(CONFIG_X86)) {
 			guid = &config_tables[i].guid;
@@ -585,7 +584,6 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
 
 			if (IS_ENABLED(CONFIG_X86_32) &&
 			    tbl64[i].table > U32_MAX) {
-				pr_cont("\n");
 				pr_err("Table located above 4GB, disabling EFI.\n");
 				return -EINVAL;
 			}
@@ -594,10 +592,14 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
 			table = tbl32[i].table;
 		}
 
+#ifdef CONFIG_XEN_EFI
+		if (efi_enabled(EFI_PARAVIRT) && !xen_config_table_memory_region_max(table))
+			continue;
+#endif
+
 		if (!match_config_table(guid, table, common_tables) && arch_tables)
 			match_config_table(guid, table, arch_tables);
 	}
-	pr_cont("\n");
 	set_bit(EFI_CONFIG_TABLES, &efi.flags);
 
 	if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) {
drivers/xen/efi.c +35 -0
index d1ff2186ebb48a7c0981ecb6d4afcbbb25ffcea0..c2274ddfcc63304008ef0fd78fd9fa416f75d073 100644
@@ -28,6 +28,7 @@
 #include <xen/interface/platform.h>
 #include <xen/xen.h>
 #include <xen/xen-ops.h>
+#include <xen/page.h>
 
 #include <asm/page.h>
 
@@ -271,6 +272,40 @@ static void xen_efi_reset_system(int reset_type, efi_status_t status,
 	}
 }
 
+__init u64 xen_config_table_memory_region_max(u64 addr)
+{
+	static_assert(XEN_PAGE_SHIFT == EFI_PAGE_SHIFT,
+		      "Mismatch between EFI_PAGE_SHIFT and XEN_PAGE_SHIFT");
+	struct xen_platform_op op = {
+		.cmd = XENPF_firmware_info,
+		.u.firmware_info = {
+			.type = XEN_FW_EFI_INFO,
+			.index = XEN_FW_EFI_MEM_INFO,
+			.u.efi_info.mem.addr = addr,
+			.u.efi_info.mem.size = U64_MAX - addr,
+		}
+	};
+	union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
+	int rc = HYPERVISOR_platform_op(&op);
+
+	if (rc) {
+		pr_warn("Failed to lookup header %llu in Xen memory map: error %d\n",
+			(unsigned long long)addr, rc);
+		return 0;
+	}
+
+	switch (info->mem.type) {
+	case EFI_RUNTIME_SERVICES_CODE:
+	case EFI_RUNTIME_SERVICES_DATA:
+	case EFI_ACPI_RECLAIM_MEMORY:
+		return info->mem.addr + info->mem.size;
+	default:
+		pr_warn("Table %llu is in memory of type %d, ignoring it\n",
+			(unsigned long long)addr, info->mem.type);
+		return 0;
+	}
+}
+
 /*
  * Set XEN EFI runtime services function pointers. Other fields of struct efi,
  * e.g. efi.systab, will be set like normal EFI.
include/linux/efi.h +9 -0
index d2b84c2fec39f0268324d1a38a73ed67786973c9..fc81e4b984398cdb399e7886b2cae7f33bf91613 100644
@@ -1324,4 +1324,13 @@ struct linux_efi_coco_secret_area {
 /* Header of a populated EFI secret area */
 #define EFI_SECRET_TABLE_HEADER_GUID	EFI_GUID(0x1e74f542, 0x71dd, 0x4d66,  0x96, 0x3e, 0xef, 0x42, 0x87, 0xff, 0x17, 0x3b)
 
+#ifdef CONFIG_XEN_EFI
+/*
+ * Returns the end of the memory region containing the given config table,
+ * or 0 if the given address does not reside in memory that can validly
+ * contain EFI configuration tables.
+ */
+__init u64 xen_config_table_memory_region_max(u64 addr);
+#endif
+
 #endif /* _LINUX_EFI_H */
Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
#2 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 9 lines · Demi Marie Obenour

Memory of type EFI_CONVENTIONAL_MEMORY, EFI_LOADER_CODE, EFI_LOADER_DATA, EFI_BOOT_SERVICES_CODE, and EFI_BOOT_SERVICES_DATA may be clobbered by Xen before Linux gets to start using it. Therefore, Linux under Xen must not use EFI tables from such memory. Most of the remaining EFI memory types are not suitable for EFI tables, leaving only EFI_ACPI_RECLAIM_MEMORY, EFI_RUNTIME_SERVICES_DATA, and EFI_RUNTIME_SERVICES_CODE. When running under Xen, Linux should only use tables that are located in one of these types of memory.

Quoted 7 lines

This patch ensures this, and also adds a function (xen_config_table_memory_region_max()) that will be used later to replace the usage of the EFI memory map in esrt.c when running under Xen. This function can also be used in mokvar-table.c and efi-bgrt.c, but I have not implemented this.

In Xen we don't clobber EfiBootServices{Code,Data} when xen.efi was passed "-mapbs". Should we perhaps extend the interface such that Dom0 can then also use tables located in such regions, perhaps by faking EFI_MEMORY_RUNTIME in the attributes returned by XEN_FW_EFI_MEM_INFO?

Jan

maintainer
#3 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 17 lines · Jan Beulich
Quoted 15 lines · Demi Marie Obenour

Memory of type EFI_CONVENTIONAL_MEMORY, EFI_LOADER_CODE, EFI_LOADER_DATA, EFI_BOOT_SERVICES_CODE, and EFI_BOOT_SERVICES_DATA may be clobbered by Xen before Linux gets to start using it. Therefore, Linux under Xen must not use EFI tables from such memory. Most of the remaining EFI memory types are not suitable for EFI tables, leaving only EFI_ACPI_RECLAIM_MEMORY, EFI_RUNTIME_SERVICES_DATA, and EFI_RUNTIME_SERVICES_CODE. When running under Xen, Linux should only use tables that are located in one of these types of memory.

This patch ensures this, and also adds a function (xen_config_table_memory_region_max()) that will be used later to replace the usage of the EFI memory map in esrt.c when running under Xen. This function can also be used in mokvar-table.c and efi-bgrt.c, but I have not implemented this.

Quoted 7 lines
Quoted 1 line

In Xen we don't clobber EfiBootServices{Code,Data} when xen.efi was passed "-mapbs". Should we perhaps extend the interface such that Dom0 can then also use tables located in such regions, perhaps by faking EFI_MEMORY_RUNTIME in the attributes returned by XEN_FW_EFI_MEM_INFO?

I know this ship has sailed for x86, but for the sake of other architectures, I'd strongly recommend leaving the EFI_MEMORY_RUNTIME bits alone, for the same reasons I gave earlier. (Runtime mappings for the firmware code itself, page table fragmentation etc etc)

I know very little about Xen, but based on the context you provided in this thread, I'd say that the best approach from the Xen side is to convert all EfiBootServicesData regions that have configuration tables pointing into them into EfiAcpiReclaimMemory.

I take it XEN_FW_EFI_MEM_INFO is an existing interface? If so, you might do the same for the returned type - EfiBootServicesData -> EfiAcpiReclaimMemory, and not muck about with the EFI_MEMORY_RUNTIME attribute.

#4 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 26 lines · Ard Biesheuvel
Quoted 17 lines · Jan Beulich
Quoted 15 lines · Demi Marie Obenour

Memory of type EFI_CONVENTIONAL_MEMORY, EFI_LOADER_CODE, EFI_LOADER_DATA, EFI_BOOT_SERVICES_CODE, and EFI_BOOT_SERVICES_DATA may be clobbered by Xen before Linux gets to start using it. Therefore, Linux under Xen must not use EFI tables from such memory. Most of the remaining EFI memory types are not suitable for EFI tables, leaving only EFI_ACPI_RECLAIM_MEMORY, EFI_RUNTIME_SERVICES_DATA, and EFI_RUNTIME_SERVICES_CODE. When running under Xen, Linux should only use tables that are located in one of these types of memory.

This patch ensures this, and also adds a function (xen_config_table_memory_region_max()) that will be used later to replace the usage of the EFI memory map in esrt.c when running under Xen. This function can also be used in mokvar-table.c and efi-bgrt.c, but I have not implemented this.

Quoted 7 lines
Quoted 1 line

In Xen we don't clobber EfiBootServices{Code,Data} when xen.efi was passed "-mapbs". Should we perhaps extend the interface such that Dom0 can then also use tables located in such regions, perhaps by faking EFI_MEMORY_RUNTIME in the attributes returned by XEN_FW_EFI_MEM_INFO?

Quoted 4 lines

I know this ship has sailed for x86, but for the sake of other architectures, I'd strongly recommend leaving the EFI_MEMORY_RUNTIME bits alone, for the same reasons I gave earlier. (Runtime mappings for the firmware code itself, page table fragmentation etc etc)

Why do you say that it has sailed for x86?

Quoted 4 lines

I know very little about Xen, but based on the context you provided in this thread, I'd say that the best approach from the Xen side is to convert all EfiBootServicesData regions that have configuration tables pointing into them into EfiAcpiReclaimMemory.

Should Xen convert the entire region, or should it try to reserve only the memory it needs? The latter would require it to parse the configuration tables. Is there a list of configuration tables that can legitimately be in EfiBootServicesData regions?

Quoted 4 lines

I take it XEN_FW_EFI_MEM_INFO is an existing interface? If so, you might do the same for the returned type - EfiBootServicesData -> EfiAcpiReclaimMemory, and not muck about with the EFI_MEMORY_RUNTIME attribute.

It is indeed an existing interface, and this is a much better idea than what you proposed.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
maintainer
#5 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 28 lines · Demi Marie Obenour
Quoted 26 lines · Ard Biesheuvel
Quoted 24 lines · Jan Beulich
Quoted 9 lines · Demi Marie Obenour

Memory of type EFI_CONVENTIONAL_MEMORY, EFI_LOADER_CODE, EFI_LOADER_DATA, EFI_BOOT_SERVICES_CODE, and EFI_BOOT_SERVICES_DATA may be clobbered by Xen before Linux gets to start using it. Therefore, Linux under Xen must not use EFI tables from such memory. Most of the remaining EFI memory types are not suitable for EFI tables, leaving only EFI_ACPI_RECLAIM_MEMORY, EFI_RUNTIME_SERVICES_DATA, and EFI_RUNTIME_SERVICES_CODE. When running under Xen, Linux should only use tables that are located in one of these types of memory.

Quoted 7 lines

This patch ensures this, and also adds a function (xen_config_table_memory_region_max()) that will be used later to replace the usage of the EFI memory map in esrt.c when running under Xen. This function can also be used in mokvar-table.c and efi-bgrt.c, but I have not implemented this.

In Xen we don't clobber EfiBootServices{Code,Data} when xen.efi was passed "-mapbs". Should we perhaps extend the interface such that Dom0 can then also use tables located in such regions, perhaps by faking EFI_MEMORY_RUNTIME in the attributes returned by XEN_FW_EFI_MEM_INFO?

Quoted 7 lines
Quoted 4 lines

I know this ship has sailed for x86, but for the sake of other architectures, I'd strongly recommend leaving the EFI_MEMORY_RUNTIME bits alone, for the same reasons I gave earlier. (Runtime mappings for the firmware code itself, page table fragmentation etc etc)

Why do you say that it has sailed for x86?

The x86 EFI code in Linux makes changes to the EFI memory map in many different places in the code. On other architectures, we have managed to avoid this, so that the EFI memory map is always identical to the one provided by the firmware at boot.

Quoted 10 lines
Quoted 4 lines

I know very little about Xen, but based on the context you provided in this thread, I'd say that the best approach from the Xen side is to convert all EfiBootServicesData regions that have configuration tables pointing into them into EfiAcpiReclaimMemory.

Should Xen convert the entire region, or should it try to reserve only the memory it needs? The latter would require it to parse the configuration tables. Is there a list of configuration tables that can legitimately be in EfiBootServicesData regions?

Not really, no. So you would have to convert the entire region /unless/ Xen knows the GUID, and therefore knows how to derive the size of the table, allowing it to reserve memory more conservatively. However, I doubt whether this is worth it: splitting entries implies rewriting the memory map, which is a thing I'd rather avoid if I were in your shoes.

Quoted 7 lines
Quoted 4 lines

I take it XEN_FW_EFI_MEM_INFO is an existing interface? If so, you might do the same for the returned type - EfiBootServicesData -> EfiAcpiReclaimMemory, and not muck about with the EFI_MEMORY_RUNTIME attribute.

It is indeed an existing interface, and this is a much better idea than what you proposed.

Right.

#6 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 48 lines · Ard Biesheuvel
Quoted 28 lines · Demi Marie Obenour
Quoted 26 lines · Ard Biesheuvel
Quoted 24 lines · Jan Beulich
Quoted 9 lines · Demi Marie Obenour

Memory of type EFI_CONVENTIONAL_MEMORY, EFI_LOADER_CODE, EFI_LOADER_DATA, EFI_BOOT_SERVICES_CODE, and EFI_BOOT_SERVICES_DATA may be clobbered by Xen before Linux gets to start using it. Therefore, Linux under Xen must not use EFI tables from such memory. Most of the remaining EFI memory types are not suitable for EFI tables, leaving only EFI_ACPI_RECLAIM_MEMORY, EFI_RUNTIME_SERVICES_DATA, and EFI_RUNTIME_SERVICES_CODE. When running under Xen, Linux should only use tables that are located in one of these types of memory.

Quoted 7 lines

This patch ensures this, and also adds a function (xen_config_table_memory_region_max()) that will be used later to replace the usage of the EFI memory map in esrt.c when running under Xen. This function can also be used in mokvar-table.c and efi-bgrt.c, but I have not implemented this.

In Xen we don't clobber EfiBootServices{Code,Data} when xen.efi was passed "-mapbs". Should we perhaps extend the interface such that Dom0 can then also use tables located in such regions, perhaps by faking EFI_MEMORY_RUNTIME in the attributes returned by XEN_FW_EFI_MEM_INFO?

Quoted 7 lines
Quoted 4 lines

I know this ship has sailed for x86, but for the sake of other architectures, I'd strongly recommend leaving the EFI_MEMORY_RUNTIME bits alone, for the same reasons I gave earlier. (Runtime mappings for the firmware code itself, page table fragmentation etc etc)

Why do you say that it has sailed for x86?

The x86 EFI code in Linux makes changes to the EFI memory map in many different places in the code. On other architectures, we have managed to avoid this, so that the EFI memory map is always identical to the one provided by the firmware at boot.

Quoted 5 lines
Quoted 4 lines

I know very little about Xen, but based on the context you provided in this thread, I'd say that the best approach from the Xen side is to convert all EfiBootServicesData regions that have configuration tables pointing into them into EfiAcpiReclaimMemory.

Quoted 7 lines
Quoted 5 lines

Should Xen convert the entire region, or should it try to reserve only the memory it needs? The latter would require it to parse the configuration tables. Is there a list of configuration tables that can legitimately be in EfiBootServicesData regions?

Not really, no.

Is there a list of tables that can be in EfiBootServicesData and which Linux cares about?

Quoted 3 lines

So you would have to convert the entire region
/unless/ Xen knows the GUID, and therefore knows how to derive the size of the table, allowing it to reserve memory more conservatively.

My worry is that this will wind up being equivalent to mapping all (or most) of EfiBootServicesData memory.

Quoted 3 lines

However, I doubt whether this is worth it: splitting entries implies rewriting the memory map, which is a thing I'd rather avoid if I were in your shoes.

Xen actually uses a different approach: instead of rewriting the memory map, it uses the EFI pool allocator to allocate memory of the desired type, copies the table to the newly allocated memory, and installs the new table in place of the old one. That only works for tables Xen understands, though.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
#7 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

In reply to Ard Biesheuvel
Quoted 13 lines · Ard Biesheuvel
Quoted 11 lines · Demi Marie Obenour
Quoted 4 lines · Ard Biesheuvel

I know very little about Xen, but based on the context you provided in this thread, I'd say that the best approach from the Xen side is to convert all EfiBootServicesData regions that have configuration tables pointing into them into EfiAcpiReclaimMemory.

Should Xen convert the entire region, or should it try to reserve only the memory it needs? The latter would require it to parse the configuration tables. Is there a list of configuration tables that can legitimately be in EfiBootServicesData regions?

Quoted 6 lines

Not really, no. So you would have to convert the entire region /unless/ Xen knows the GUID, and therefore knows how to derive the size of the table, allowing it to reserve memory more conservatively. However, I doubt whether this is worth it: splitting entries implies rewriting the memory map, which is a thing I'd rather avoid if I were in your shoes.

I actually wonder if Xen needs to reserve *all* of EfiBootServicesData. The reason is that some (probably buggy) firmware may store ACPI tables there, and Xen does not have an ACPI implementation. From my perspective, a much safer approach would be to pass all of EfiBootServicesData memory directly to dom0, and have dom0 give Xen back what it doesn’t wind up using. That allows dom0’s memory reservation code to work properly, which it currently does not.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
#8 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 21 lines · Demi Marie Obenour
Quoted 13 lines · Ard Biesheuvel
Quoted 11 lines · Demi Marie Obenour
Quoted 4 lines · Ard Biesheuvel

I know very little about Xen, but based on the context you provided in this thread, I'd say that the best approach from the Xen side is to convert all EfiBootServicesData regions that have configuration tables pointing into them into EfiAcpiReclaimMemory.

Should Xen convert the entire region, or should it try to reserve only the memory it needs? The latter would require it to parse the configuration tables. Is there a list of configuration tables that can legitimately be in EfiBootServicesData regions?

Quoted 6 lines

Not really, no. So you would have to convert the entire region /unless/ Xen knows the GUID, and therefore knows how to derive the size of the table, allowing it to reserve memory more conservatively. However, I doubt whether this is worth it: splitting entries implies rewriting the memory map, which is a thing I'd rather avoid if I were in your shoes.

Quoted 3 lines

I actually wonder if Xen needs to reserve *all* of EfiBootServicesData. The reason is that some (probably buggy) firmware may store ACPI tables there, and Xen does not have an ACPI implementation.

We already have the -mapbs option as a workaround in such situations.

Quoted 5 lines

From my
perspective, a much safer approach would be to pass all of EfiBootServicesData memory directly to dom0, and have dom0 give Xen back what it doesn’t wind up using. That allows dom0’s memory reservation code to work properly, which it currently does not.

As said already on a different thread: Giving memory to domains (incl Dom0) isn't related to their original memory type (neither EFI's nor E820's); the needed memory is taken from the general page allocator (with one exception for initrd, to avoid unnecessary copying around of data). Hence what you propose would end up as an (imo) awful hack in Xen. I also don't see how this relates to "dom0’s memory reservation code", but I'm sure you can clarify that for me.

Jan

#9 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 22 lines · Jan Beulich
Quoted 21 lines · Demi Marie Obenour
Quoted 19 lines · Ard Biesheuvel
Quoted 11 lines · Demi Marie Obenour
Quoted 4 lines · Ard Biesheuvel

I know very little about Xen, but based on the context you provided in this thread, I'd say that the best approach from the Xen side is to convert all EfiBootServicesData regions that have configuration tables pointing into them into EfiAcpiReclaimMemory.

Should Xen convert the entire region, or should it try to reserve only the memory it needs? The latter would require it to parse the configuration tables. Is there a list of configuration tables that can legitimately be in EfiBootServicesData regions?

Not really, no. So you would have to convert the entire region /unless/ Xen knows the GUID, and therefore knows how to derive the size of the table, allowing it to reserve memory more conservatively. However, I doubt whether this is worth it: splitting entries implies rewriting the memory map, which is a thing I'd rather avoid if I were in your shoes.

Quoted 5 lines
Quoted 3 lines

I actually wonder if Xen needs to reserve *all* of EfiBootServicesData. The reason is that some (probably buggy) firmware may store ACPI tables there, and Xen does not have an ACPI implementation.

We already have the -mapbs option as a workaround in such situations.

The problem is that there is no way for the user to know when it is needed. One option would be for dom0 to print a warning in the kernel log if it needs to ignore a table due to it being in EfiBootServicesData.

Quoted 6 lines
Quoted 5 lines

From my
perspective, a much safer approach would be to pass all of EfiBootServicesData memory directly to dom0, and have dom0 give Xen back what it doesn’t wind up using. That allows dom0’s memory reservation code to work properly, which it currently does not.

Quoted 7 lines

As said already on a different thread: Giving memory to domains (incl Dom0) isn't related to their original memory type (neither EFI's nor E820's); the needed memory is taken from the general page allocator (with one exception for initrd, to avoid unnecessary copying around of data). Hence what you propose would end up as an (imo) awful hack in Xen. I also don't see how this relates to "dom0’s memory reservation code", but I'm sure you can clarify that for me.

Linux has a function called efi_mem_reserve() that is used to reserve EfiBootServicesData memory that contains e.g. EFI configuration tables. This function does not work under Xen because Xen could have already clobbered the memory. efi_mem_reserve() not working is the whole reason for this thread, as it prevents EFI tables that are in EfiBootServicesData from being used under Xen.

A much nicer approach would be for Xen to reserve boot services memory unconditionally, but provide a hypercall that dom0 could used to free the parts of EfiBootServicesData memory that are no longer needed. This would allow efi_mem_reserve() to work normally.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
#10 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 11 lines · Demi Marie Obenour

Linux has a function called efi_mem_reserve() that is used to reserve EfiBootServicesData memory that contains e.g. EFI configuration tables. This function does not work under Xen because Xen could have already clobbered the memory. efi_mem_reserve() not working is the whole reason for this thread, as it prevents EFI tables that are in EfiBootServicesData from being used under Xen.

A much nicer approach would be for Xen to reserve boot services memory unconditionally, but provide a hypercall that dom0 could used to free the parts of EfiBootServicesData memory that are no longer needed. This would allow efi_mem_reserve() to work normally.

efi_mem_reserve() actually working would be a layering violation; controlling the EFI memory map is entirely Xen's job.

As to the hypercall you suggest - I wouldn't mind its addition, but only for the case when -mapbs is used. As I've indicated before, I'm of the opinion that default behavior should be matching the intentions of the spec, and the intention of EfiBootServices* is for the space to be reclaimed. Plus I'm sure you realize there's a caveat with Dom0 using that hypercall: It might use it for regions where data lives which it wouldn't care about itself, but which an eventual kexec-ed (or alike) entity would later want to consume. Code/data potentially usable by _anyone_ between two resets of the system cannot legitimately be freed (and hence imo is wrong to live in EfiBootServices* regions). In a way one could view the Dom0 kernel as an "or alike" entity ...

Jan

#11 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 8 lines · Jan Beulich
Quoted 7 lines · Demi Marie Obenour

Linux has a function called efi_mem_reserve() that is used to reserve EfiBootServicesData memory that contains e.g. EFI configuration tables. This function does not work under Xen because Xen could have already clobbered the memory. efi_mem_reserve() not working is the whole reason for this thread, as it prevents EFI tables that are in EfiBootServicesData from being used under Xen.

Quoted 7 lines
Quoted 4 lines

A much nicer approach would be for Xen to reserve boot services memory unconditionally, but provide a hypercall that dom0 could used to free the parts of EfiBootServicesData memory that are no longer needed. This would allow efi_mem_reserve() to work normally.

efi_mem_reserve() actually working would be a layering violation; controlling the EFI memory map is entirely Xen's job.

Doing this properly would require Xen to understand all of the EFI tables that could validly be in EfiBootServices* and which could be of interest to dom0. It might (at least on some very buggy firmware) require a partial ACPI and/or SMBIOS implementation too, if the firmware decided to put an ACPI or SMBIOS table in EfiBootServices*.

Quoted 10 lines

As to the hypercall you suggest - I wouldn't mind its addition, but only for the case when -mapbs is used. As I've indicated before, I'm of the opinion that default behavior should be matching the intentions of the spec, and the intention of EfiBootServices* is for the space to be reclaimed. Plus I'm sure you realize there's a caveat with Dom0 using that hypercall: It might use it for regions where data lives which it wouldn't care about itself, but which an eventual kexec-ed (or alike) entity would later want to consume. Code/data potentially usable by _anyone_ between two resets of the system cannot legitimately be freed (and hence imo is wrong to live in EfiBootServices* regions).

I agree, but currently some such data *is* in EfiBootServices* regions, sadly. When -mapbs is *not* used, I recommend uninstalling all of the configuration tables that point to EfiBootServicesData memory before freeing that memory.

Quoted 1 line

In a way one could view the Dom0 kernel as an "or alike" entity ...

It is indeed such an entity.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
maintainer
#12 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 35 lines · Demi Marie Obenour
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Demi Marie Obenour

Linux has a function called efi_mem_reserve() that is used to reserve EfiBootServicesData memory that contains e.g. EFI configuration tables. This function does not work under Xen because Xen could have already clobbered the memory. efi_mem_reserve() not working is the whole reason for this thread, as it prevents EFI tables that are in EfiBootServicesData from being used under Xen.

Quoted 7 lines
Quoted 4 lines

A much nicer approach would be for Xen to reserve boot services memory unconditionally, but provide a hypercall that dom0 could used to free the parts of EfiBootServicesData memory that are no longer needed. This would allow efi_mem_reserve() to work normally.

efi_mem_reserve() actually working would be a layering violation; controlling the EFI memory map is entirely Xen's job.

Doing this properly would require Xen to understand all of the EFI tables that could validly be in EfiBootServices* and which could be of interest to dom0. It might (at least on some very buggy firmware) require a partial ACPI and/or SMBIOS implementation too, if the firmware decided to put an ACPI or SMBIOS table in EfiBootServices*.

Quoted 10 lines

As to the hypercall you suggest - I wouldn't mind its addition, but only for the case when -mapbs is used. As I've indicated before, I'm of the opinion that default behavior should be matching the intentions of the spec, and the intention of EfiBootServices* is for the space to be reclaimed. Plus I'm sure you realize there's a caveat with Dom0 using that hypercall: It might use it for regions where data lives which it wouldn't care about itself, but which an eventual kexec-ed (or alike) entity would later want to consume. Code/data potentially usable by _anyone_ between two resets of the system cannot legitimately be freed (and hence imo is wrong to live in EfiBootServices* regions).

Quoted 5 lines

I agree, but currently some such data *is* in EfiBootServices* regions, sadly. When -mapbs is *not* used, I recommend uninstalling all of the configuration tables that point to EfiBootServicesData memory before freeing that memory.

That seems like a reasonable approach to me. Tables like MEMATTR or RT_PROP are mostly relevant for bare metal where the host kernel maps the runtime services, and in general, passing on these tables without knowing what they do is kind of fishy anyway. You might even argue that only known table types should be forwarded in the first place, regardless of the memory type.

#13 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 43 lines · Ard Biesheuvel
Quoted 35 lines · Demi Marie Obenour
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Demi Marie Obenour

Linux has a function called efi_mem_reserve() that is used to reserve EfiBootServicesData memory that contains e.g. EFI configuration tables. This function does not work under Xen because Xen could have already clobbered the memory. efi_mem_reserve() not working is the whole reason for this thread, as it prevents EFI tables that are in EfiBootServicesData from being used under Xen.

Quoted 7 lines
Quoted 4 lines

A much nicer approach would be for Xen to reserve boot services memory unconditionally, but provide a hypercall that dom0 could used to free the parts of EfiBootServicesData memory that are no longer needed. This would allow efi_mem_reserve() to work normally.

efi_mem_reserve() actually working would be a layering violation; controlling the EFI memory map is entirely Xen's job.

Doing this properly would require Xen to understand all of the EFI tables that could validly be in EfiBootServices* and which could be of interest to dom0. It might (at least on some very buggy firmware) require a partial ACPI and/or SMBIOS implementation too, if the firmware decided to put an ACPI or SMBIOS table in EfiBootServices*.

Quoted 10 lines

As to the hypercall you suggest - I wouldn't mind its addition, but only for the case when -mapbs is used. As I've indicated before, I'm of the opinion that default behavior should be matching the intentions of the spec, and the intention of EfiBootServices* is for the space to be reclaimed. Plus I'm sure you realize there's a caveat with Dom0 using that hypercall: It might use it for regions where data lives which it wouldn't care about itself, but which an eventual kexec-ed (or alike) entity would later want to consume. Code/data potentially usable by _anyone_ between two resets of the system cannot legitimately be freed (and hence imo is wrong to live in EfiBootServices* regions).

Quoted 5 lines

I agree, but currently some such data *is* in EfiBootServices* regions, sadly. When -mapbs is *not* used, I recommend uninstalling all of the configuration tables that point to EfiBootServicesData memory before freeing that memory.

Quoted 6 lines

That seems like a reasonable approach to me. Tables like MEMATTR or RT_PROP are mostly relevant for bare metal where the host kernel maps the runtime services, and in general, passing on these tables without knowing what they do is kind of fishy anyway. You might even argue that only known table types should be forwarded in the first place, regardless of the memory type.

Which tables are worth handling in Xen? I know about ACPI, SMBIOS, and ESRT, but I am curious which others Xen should preserve. Currently, Xen does not know about RT_PROP or MEMATTR; could this be a cause of problems?

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
maintainer
#14 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 52 lines · Demi Marie Obenour
Quoted 43 lines · Ard Biesheuvel
Quoted 35 lines · Demi Marie Obenour
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Demi Marie Obenour

Linux has a function called efi_mem_reserve() that is used to reserve EfiBootServicesData memory that contains e.g. EFI configuration tables. This function does not work under Xen because Xen could have already clobbered the memory. efi_mem_reserve() not working is the whole reason for this thread, as it prevents EFI tables that are in EfiBootServicesData from being used under Xen.

Quoted 7 lines
Quoted 4 lines

A much nicer approach would be for Xen to reserve boot services memory unconditionally, but provide a hypercall that dom0 could used to free the parts of EfiBootServicesData memory that are no longer needed. This would allow efi_mem_reserve() to work normally.

efi_mem_reserve() actually working would be a layering violation; controlling the EFI memory map is entirely Xen's job.

Doing this properly would require Xen to understand all of the EFI tables that could validly be in EfiBootServices* and which could be of interest to dom0. It might (at least on some very buggy firmware) require a partial ACPI and/or SMBIOS implementation too, if the firmware decided to put an ACPI or SMBIOS table in EfiBootServices*.

Quoted 10 lines

As to the hypercall you suggest - I wouldn't mind its addition, but only for the case when -mapbs is used. As I've indicated before, I'm of the opinion that default behavior should be matching the intentions of the spec, and the intention of EfiBootServices* is for the space to be reclaimed. Plus I'm sure you realize there's a caveat with Dom0 using that hypercall: It might use it for regions where data lives which it wouldn't care about itself, but which an eventual kexec-ed (or alike) entity would later want to consume. Code/data potentially usable by _anyone_ between two resets of the system cannot legitimately be freed (and hence imo is wrong to live in EfiBootServices* regions).

Quoted 5 lines

I agree, but currently some such data *is* in EfiBootServices* regions, sadly. When -mapbs is *not* used, I recommend uninstalling all of the configuration tables that point to EfiBootServicesData memory before freeing that memory.

Quoted 6 lines

That seems like a reasonable approach to me. Tables like MEMATTR or RT_PROP are mostly relevant for bare metal where the host kernel maps the runtime services, and in general, passing on these tables without knowing what they do is kind of fishy anyway. You might even argue that only known table types should be forwarded in the first place, regardless of the memory type.

Quoted 4 lines

Which tables are worth handling in Xen? I know about ACPI, SMBIOS, and ESRT, but I am curious which others Xen should preserve. Currently, Xen does not know about RT_PROP or MEMATTR; could this be a cause of problems?

dom0 only has access to paravirtualized EFI runtime services, so consuming RT_PROP or MEMATTR should be up to Xen (they describe which runtime services remain available at runtime, and which permission attributes to use for the runtime services memory regions, respectively)

Looking through the kernel code, I don't think there are any that dom0 should care about beyond ACPI, SMBIOS and ESRT. But as you suggest, that means Xen should just mask them in the view of the EFI system table it exposes so dom0. Otherwise, the kernel may still try to map and parse them.

#15 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 59 lines · Ard Biesheuvel
Quoted 52 lines · Demi Marie Obenour
Quoted 43 lines · Ard Biesheuvel
Quoted 35 lines · Demi Marie Obenour
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Demi Marie Obenour

Linux has a function called efi_mem_reserve() that is used to reserve EfiBootServicesData memory that contains e.g. EFI configuration tables. This function does not work under Xen because Xen could have already clobbered the memory. efi_mem_reserve() not working is the whole reason for this thread, as it prevents EFI tables that are in EfiBootServicesData from being used under Xen.

Quoted 7 lines
Quoted 4 lines

A much nicer approach would be for Xen to reserve boot services memory unconditionally, but provide a hypercall that dom0 could used to free the parts of EfiBootServicesData memory that are no longer needed. This would allow efi_mem_reserve() to work normally.

efi_mem_reserve() actually working would be a layering violation; controlling the EFI memory map is entirely Xen's job.

Doing this properly would require Xen to understand all of the EFI tables that could validly be in EfiBootServices* and which could be of interest to dom0. It might (at least on some very buggy firmware) require a partial ACPI and/or SMBIOS implementation too, if the firmware decided to put an ACPI or SMBIOS table in EfiBootServices*.

Quoted 10 lines

As to the hypercall you suggest - I wouldn't mind its addition, but only for the case when -mapbs is used. As I've indicated before, I'm of the opinion that default behavior should be matching the intentions of the spec, and the intention of EfiBootServices* is for the space to be reclaimed. Plus I'm sure you realize there's a caveat with Dom0 using that hypercall: It might use it for regions where data lives which it wouldn't care about itself, but which an eventual kexec-ed (or alike) entity would later want to consume. Code/data potentially usable by _anyone_ between two resets of the system cannot legitimately be freed (and hence imo is wrong to live in EfiBootServices* regions).

Quoted 5 lines

I agree, but currently some such data *is* in EfiBootServices* regions, sadly. When -mapbs is *not* used, I recommend uninstalling all of the configuration tables that point to EfiBootServicesData memory before freeing that memory.

Quoted 6 lines

That seems like a reasonable approach to me. Tables like MEMATTR or RT_PROP are mostly relevant for bare metal where the host kernel maps the runtime services, and in general, passing on these tables without knowing what they do is kind of fishy anyway. You might even argue that only known table types should be forwarded in the first place, regardless of the memory type.

Quoted 4 lines

Which tables are worth handling in Xen? I know about ACPI, SMBIOS, and ESRT, but I am curious which others Xen should preserve. Currently, Xen does not know about RT_PROP or MEMATTR; could this be a cause of problems?

Quoted 5 lines

dom0 only has access to paravirtualized EFI runtime services, so consuming RT_PROP or MEMATTR should be up to Xen (they describe which runtime services remain available at runtime, and which permission attributes to use for the runtime services memory regions, respectively)

Xen does not do this right now. I wonder if this could be the cause of compatibility issues with various firmware implementations.

Quoted 5 lines

Looking through the kernel code, I don't think there are any that dom0 should care about beyond ACPI, SMBIOS and ESRT. But as you suggest, that means Xen should just mask them in the view of the EFI system table it exposes so dom0. Otherwise, the kernel may still try to map and parse them.

What about the BGRT and MOKvar? I agree that Xen should not expose the others. Should it just hide the tables, or should it actually uninstall them? My intuition is that the second would be technically more correct, but also more likely to trigger bugs in various firmware implementations.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
maintainer
#16 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 76 lines · Demi Marie Obenour
Quoted 59 lines · Ard Biesheuvel
Quoted 52 lines · Demi Marie Obenour
Quoted 43 lines · Ard Biesheuvel
Quoted 35 lines · Demi Marie Obenour
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Demi Marie Obenour

Linux has a function called efi_mem_reserve() that is used to reserve EfiBootServicesData memory that contains e.g. EFI configuration tables. This function does not work under Xen because Xen could have already clobbered the memory. efi_mem_reserve() not working is the whole reason for this thread, as it prevents EFI tables that are in EfiBootServicesData from being used under Xen.

Quoted 7 lines
Quoted 4 lines

A much nicer approach would be for Xen to reserve boot services memory unconditionally, but provide a hypercall that dom0 could used to free the parts of EfiBootServicesData memory that are no longer needed. This would allow efi_mem_reserve() to work normally.

efi_mem_reserve() actually working would be a layering violation; controlling the EFI memory map is entirely Xen's job.

Doing this properly would require Xen to understand all of the EFI tables that could validly be in EfiBootServices* and which could be of interest to dom0. It might (at least on some very buggy firmware) require a partial ACPI and/or SMBIOS implementation too, if the firmware decided to put an ACPI or SMBIOS table in EfiBootServices*.

Quoted 10 lines

As to the hypercall you suggest - I wouldn't mind its addition, but only for the case when -mapbs is used. As I've indicated before, I'm of the opinion that default behavior should be matching the intentions of the spec, and the intention of EfiBootServices* is for the space to be reclaimed. Plus I'm sure you realize there's a caveat with Dom0 using that hypercall: It might use it for regions where data lives which it wouldn't care about itself, but which an eventual kexec-ed (or alike) entity would later want to consume. Code/data potentially usable by _anyone_ between two resets of the system cannot legitimately be freed (and hence imo is wrong to live in EfiBootServices* regions).

Quoted 5 lines

I agree, but currently some such data *is* in EfiBootServices* regions, sadly. When -mapbs is *not* used, I recommend uninstalling all of the configuration tables that point to EfiBootServicesData memory before freeing that memory.

Quoted 6 lines

That seems like a reasonable approach to me. Tables like MEMATTR or RT_PROP are mostly relevant for bare metal where the host kernel maps the runtime services, and in general, passing on these tables without knowing what they do is kind of fishy anyway. You might even argue that only known table types should be forwarded in the first place, regardless of the memory type.

Quoted 4 lines

Which tables are worth handling in Xen? I know about ACPI, SMBIOS, and ESRT, but I am curious which others Xen should preserve. Currently, Xen does not know about RT_PROP or MEMATTR; could this be a cause of problems?

Quoted 5 lines

dom0 only has access to paravirtualized EFI runtime services, so consuming RT_PROP or MEMATTR should be up to Xen (they describe which runtime services remain available at runtime, and which permission attributes to use for the runtime services memory regions, respectively)

Xen does not do this right now. I wonder if this could be the cause of compatibility issues with various firmware implementations.

Quoted 5 lines

Looking through the kernel code, I don't think there are any that dom0 should care about beyond ACPI, SMBIOS and ESRT. But as you suggest, that means Xen should just mask them in the view of the EFI system table it exposes so dom0. Otherwise, the kernel may still try to map and parse them.

Quoted 5 lines

What about the BGRT and MOKvar? I agree that Xen should not expose the others. Should it just hide the tables, or should it actually uninstall them? My intuition is that the second would be technically more correct, but also more likely to trigger bugs in various firmware implementations.

BGRT is a ACPI table not a EFI configuration table, so I'd assume it is treated the same way as other ACPI tables

MOKvar is a fallback for systems where it doesn't fit into a volatile variable IIRC but I am not sure if it is actually implemented anywhere, and what type of memory region it is expected to use.

However, I'm not sure if it even matters, given that Xen loads the dom0 kernel not the firmware, right?

#17 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 78 lines · Ard Biesheuvel
Quoted 76 lines · Demi Marie Obenour
Quoted 59 lines · Ard Biesheuvel
Quoted 52 lines · Demi Marie Obenour
Quoted 43 lines · Ard Biesheuvel
Quoted 35 lines · Demi Marie Obenour
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Demi Marie Obenour

Linux has a function called efi_mem_reserve() that is used to reserve EfiBootServicesData memory that contains e.g. EFI configuration tables. This function does not work under Xen because Xen could have already clobbered the memory. efi_mem_reserve() not working is the whole reason for this thread, as it prevents EFI tables that are in EfiBootServicesData from being used under Xen.

Quoted 7 lines
Quoted 4 lines

A much nicer approach would be for Xen to reserve boot services memory unconditionally, but provide a hypercall that dom0 could used to free the parts of EfiBootServicesData memory that are no longer needed. This would allow efi_mem_reserve() to work normally.

efi_mem_reserve() actually working would be a layering violation; controlling the EFI memory map is entirely Xen's job.

Doing this properly would require Xen to understand all of the EFI tables that could validly be in EfiBootServices* and which could be of interest to dom0. It might (at least on some very buggy firmware) require a partial ACPI and/or SMBIOS implementation too, if the firmware decided to put an ACPI or SMBIOS table in EfiBootServices*.

Quoted 10 lines

As to the hypercall you suggest - I wouldn't mind its addition, but only for the case when -mapbs is used. As I've indicated before, I'm of the opinion that default behavior should be matching the intentions of the spec, and the intention of EfiBootServices* is for the space to be reclaimed. Plus I'm sure you realize there's a caveat with Dom0 using that hypercall: It might use it for regions where data lives which it wouldn't care about itself, but which an eventual kexec-ed (or alike) entity would later want to consume. Code/data potentially usable by _anyone_ between two resets of the system cannot legitimately be freed (and hence imo is wrong to live in EfiBootServices* regions).

Quoted 5 lines

I agree, but currently some such data *is* in EfiBootServices* regions, sadly. When -mapbs is *not* used, I recommend uninstalling all of the configuration tables that point to EfiBootServicesData memory before freeing that memory.

Quoted 6 lines

That seems like a reasonable approach to me. Tables like MEMATTR or RT_PROP are mostly relevant for bare metal where the host kernel maps the runtime services, and in general, passing on these tables without knowing what they do is kind of fishy anyway. You might even argue that only known table types should be forwarded in the first place, regardless of the memory type.

Quoted 4 lines

Which tables are worth handling in Xen? I know about ACPI, SMBIOS, and ESRT, but I am curious which others Xen should preserve. Currently, Xen does not know about RT_PROP or MEMATTR; could this be a cause of problems?

Quoted 5 lines

dom0 only has access to paravirtualized EFI runtime services, so consuming RT_PROP or MEMATTR should be up to Xen (they describe which runtime services remain available at runtime, and which permission attributes to use for the runtime services memory regions, respectively)

Xen does not do this right now. I wonder if this could be the cause of compatibility issues with various firmware implementations.

Quoted 5 lines

Looking through the kernel code, I don't think there are any that dom0 should care about beyond ACPI, SMBIOS and ESRT. But as you suggest, that means Xen should just mask them in the view of the EFI system table it exposes so dom0. Otherwise, the kernel may still try to map and parse them.

Quoted 8 lines
Quoted 5 lines

What about the BGRT and MOKvar? I agree that Xen should not expose the others. Should it just hide the tables, or should it actually uninstall them? My intuition is that the second would be technically more correct, but also more likely to trigger bugs in various firmware implementations.

BGRT is a ACPI table not a EFI configuration table, so I'd assume it is treated the same way as other ACPI tables

That actually surprises me. Xen definitely needs to know about its GUID, then. Are there any other EFI configuration tables that Xen needs to know about, or ACPI tables that might be outside of EfiACPIReclaimMemory.

Quoted 6 lines

MOKvar is a fallback for systems where it doesn't fit into a volatile variable IIRC but I am not sure if it is actually implemented anywhere, and what type of memory region it is expected to use.

However, I'm not sure if it even matters, given that Xen loads the dom0 kernel not the firmware, right?

No idea.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
maintainer
#18 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 86 lines · Demi Marie Obenour
Quoted 84 lines · Ard Biesheuvel
Quoted 81 lines · Demi Marie Obenour
Quoted 59 lines · Ard Biesheuvel
Quoted 52 lines · Demi Marie Obenour
Quoted 43 lines · Ard Biesheuvel
Quoted 35 lines · Demi Marie Obenour
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Demi Marie Obenour

Linux has a function called efi_mem_reserve() that is used to reserve EfiBootServicesData memory that contains e.g. EFI configuration tables. This function does not work under Xen because Xen could have already clobbered the memory. efi_mem_reserve() not working is the whole reason for this thread, as it prevents EFI tables that are in EfiBootServicesData from being used under Xen.

Quoted 7 lines
Quoted 4 lines

A much nicer approach would be for Xen to reserve boot services memory unconditionally, but provide a hypercall that dom0 could used to free the parts of EfiBootServicesData memory that are no longer needed. This would allow efi_mem_reserve() to work normally.

efi_mem_reserve() actually working would be a layering violation; controlling the EFI memory map is entirely Xen's job.

Doing this properly would require Xen to understand all of the EFI tables that could validly be in EfiBootServices* and which could be of interest to dom0. It might (at least on some very buggy firmware) require a partial ACPI and/or SMBIOS implementation too, if the firmware decided to put an ACPI or SMBIOS table in EfiBootServices*.

Quoted 10 lines

As to the hypercall you suggest - I wouldn't mind its addition, but only for the case when -mapbs is used. As I've indicated before, I'm of the opinion that default behavior should be matching the intentions of the spec, and the intention of EfiBootServices* is for the space to be reclaimed. Plus I'm sure you realize there's a caveat with Dom0 using that hypercall: It might use it for regions where data lives which it wouldn't care about itself, but which an eventual kexec-ed (or alike) entity would later want to consume. Code/data potentially usable by _anyone_ between two resets of the system cannot legitimately be freed (and hence imo is wrong to live in EfiBootServices* regions).

Quoted 5 lines

I agree, but currently some such data *is* in EfiBootServices* regions, sadly. When -mapbs is *not* used, I recommend uninstalling all of the configuration tables that point to EfiBootServicesData memory before freeing that memory.

Quoted 6 lines

That seems like a reasonable approach to me. Tables like MEMATTR or RT_PROP are mostly relevant for bare metal where the host kernel maps the runtime services, and in general, passing on these tables without knowing what they do is kind of fishy anyway. You might even argue that only known table types should be forwarded in the first place, regardless of the memory type.

Quoted 4 lines

Which tables are worth handling in Xen? I know about ACPI, SMBIOS, and ESRT, but I am curious which others Xen should preserve. Currently, Xen does not know about RT_PROP or MEMATTR; could this be a cause of problems?

Quoted 5 lines

dom0 only has access to paravirtualized EFI runtime services, so consuming RT_PROP or MEMATTR should be up to Xen (they describe which runtime services remain available at runtime, and which permission attributes to use for the runtime services memory regions, respectively)

Xen does not do this right now. I wonder if this could be the cause of compatibility issues with various firmware implementations.

Quoted 5 lines

Looking through the kernel code, I don't think there are any that dom0 should care about beyond ACPI, SMBIOS and ESRT. But as you suggest, that means Xen should just mask them in the view of the EFI system table it exposes so dom0. Otherwise, the kernel may still try to map and parse them.

What about the BGRT and MOKvar? I agree that Xen should not expose the others. Should it just hide the tables, or should it actually uninstall them? My intuition is that the second would be technically more correct, but also more likely to trigger bugs in various firmware implementations.

Quoted 5 lines
Quoted 2 lines

BGRT is a ACPI table not a EFI configuration table, so I'd assume it is treated the same way as other ACPI tables

That actually surprises me. Xen definitely needs to know about its GUID, then.

It doesn't have a GUID. The table itself is a ACPI table that is enumerated in the usual ACPI way. The problem is that the BGRT describes a region in memory (where the OEM boot logo is), and this is usually in EfiBootServicesData memory.

This data is typically used during early boot, to keep the logo intact while the OS loader draws a nice animation below it. Not the end of the world if it gets corrupted or removed.

#19 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

In reply to Demi Marie Obenour
Quoted 14 lines · Demi Marie Obenour
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Demi Marie Obenour

Linux has a function called efi_mem_reserve() that is used to reserve EfiBootServicesData memory that contains e.g. EFI configuration tables. This function does not work under Xen because Xen could have already clobbered the memory. efi_mem_reserve() not working is the whole reason for this thread, as it prevents EFI tables that are in EfiBootServicesData from being used under Xen.

A much nicer approach would be for Xen to reserve boot services memory unconditionally, but provide a hypercall that dom0 could used to free the parts of EfiBootServicesData memory that are no longer needed. This would allow efi_mem_reserve() to work normally.

Quoted 6 lines
Quoted 2 lines

efi_mem_reserve() actually working would be a layering violation; controlling the EFI memory map is entirely Xen's job.

Doing this properly would require Xen to understand all of the EFI tables that could validly be in EfiBootServices* and which could be of interest to dom0.

We don't need to understand the tables as long as none crosses memory map descriptor boundaries, and as long as they don't contain further pointers.

Quoted 3 lines

It might (at least on some very buggy firmware)
require a partial ACPI and/or SMBIOS implementation too, if the firmware decided to put an ACPI or SMBIOS table in EfiBootServices*.

I hope we won't need to go that far; on such systems -mapbs will continue to be needed.

Quoted 11 lines
Quoted 10 lines

As to the hypercall you suggest - I wouldn't mind its addition, but only for the case when -mapbs is used. As I've indicated before, I'm of the opinion that default behavior should be matching the intentions of the spec, and the intention of EfiBootServices* is for the space to be reclaimed. Plus I'm sure you realize there's a caveat with Dom0 using that hypercall: It might use it for regions where data lives which it wouldn't care about itself, but which an eventual kexec-ed (or alike) entity would later want to consume. Code/data potentially usable by _anyone_ between two resets of the system cannot legitimately be freed (and hence imo is wrong to live in EfiBootServices* regions).

Quoted 4 lines

I agree, but currently some such data *is* in EfiBootServices* regions, sadly. When -mapbs is *not* used, I recommend uninstalling all of the configuration tables that point to EfiBootServicesData memory before freeing that memory.

Hmm, uninstalling isn't nice, as it may limit functionality. Instead we might go through all tables and fiddle with memap descriptors in case a pointer references an EfiBootServices* region (regardless of size, as per the first restriction mentioned above). (A more brute force approach might be to simply behave as if -mapbs was specified in such a case, provided we can reliably determine this early enough, i.e. before first checking the "map_bs" variable.) Tables actually known to us could also be relocated (like you've done for ESRT).

Such checking could be extended to the runtime services function pointers. While that wouldn't cover cases where a function entry point is in runtime services space but the function then wrongly calls into or references boot services space, it would cover a few more (broken) systems.

This, unlike behaving by default as if -mapbs was given, would be a workaround I'd accept to be enabled unconditionally, as it wouldn't affect well behaved systems (beyond the time it takes to carry out the checks, and provided the checking logic isn't buggy).

There's one further caveat towards uninstalling (in a way also for your ESRT relocation code): The final memory map is known to us only when we can't call boot services functions anymore (i.e. in particular InstallConfigurationTable()).

Jan

#20 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

In reply to Jan Beulich
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Demi Marie Obenour

Memory of type EFI_CONVENTIONAL_MEMORY, EFI_LOADER_CODE, EFI_LOADER_DATA, EFI_BOOT_SERVICES_CODE, and EFI_BOOT_SERVICES_DATA may be clobbered by Xen before Linux gets to start using it. Therefore, Linux under Xen must not use EFI tables from such memory. Most of the remaining EFI memory types are not suitable for EFI tables, leaving only EFI_ACPI_RECLAIM_MEMORY, EFI_RUNTIME_SERVICES_DATA, and EFI_RUNTIME_SERVICES_CODE. When running under Xen, Linux should only use tables that are located in one of these types of memory.

This patch ensures this, and also adds a function (xen_config_table_memory_region_max()) that will be used later to replace the usage of the EFI memory map in esrt.c when running under Xen. This function can also be used in mokvar-table.c and efi-bgrt.c, but I have not implemented this.

Quoted 6 lines
Quoted 1 line

In Xen we don't clobber EfiBootServices{Code,Data} when xen.efi was passed "-mapbs". Should we perhaps extend the interface such that Dom0 can then also use tables located in such regions, perhaps by faking EFI_MEMORY_RUNTIME in the attributes returned by XEN_FW_EFI_MEM_INFO?

I can add a check for EFI_MEMORY_RUNTIME, but only if I can require a Xen
version with https://lore.kernel.org/xen-devel/[email protected]/t/#u.
This is easy in Qubes OS via RPM dependencies, but I am not sure if it
is suitable for upstream without a mechanism for dom0 to verify that the
patch has been included.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
maintainer
#21 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

In reply to Demi Marie Obenour
Quoted 28 lines · Demi Marie Obenour

Memory of type EFI_CONVENTIONAL_MEMORY, EFI_LOADER_CODE, EFI_LOADER_DATA, EFI_BOOT_SERVICES_CODE, and EFI_BOOT_SERVICES_DATA may be clobbered by Xen before Linux gets to start using it. Therefore, Linux under Xen must not use EFI tables from such memory. Most of the remaining EFI memory types are not suitable for EFI tables, leaving only EFI_ACPI_RECLAIM_MEMORY, EFI_RUNTIME_SERVICES_DATA, and EFI_RUNTIME_SERVICES_CODE. When running under Xen, Linux should only use tables that are located in one of these types of memory.

This patch ensures this, and also adds a function (xen_config_table_memory_region_max()) that will be used later to replace the usage of the EFI memory map in esrt.c when running under Xen. This function can also be used in mokvar-table.c and efi-bgrt.c, but I have not implemented this.

---
 drivers/firmware/efi/efi.c |  8 +++++---
 drivers/xen/efi.c          | 35 +++++++++++++++++++++++++++++++++++
 include/linux/efi.h        |  9 +++++++++
 3 files changed, 49 insertions(+), 3 deletions(-)
drivers/firmware/efi/efi.c +0 -0
index e4080ad96089abd7f84745dd8461c548bcbb7685..d344f3ff73d1c5ed0c67e3251a9502e66719741d 100644

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 577

Quoted 4 lines
drivers/firmware/efi/efi.c +0 -1
@@ -574,7 +574,6 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
        unsigned long table;
        int i;
 
-       pr_info("");

Why are you removing these prints?

Quoted 12 lines
drivers/firmware/efi/efi.c +0 -1
        for (i = 0; i < count; i++) {
                if (!IS_ENABLED(CONFIG_X86)) {
                        guid = &config_tables[i].guid;
@@ -585,7 +584,6 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
 
                        if (IS_ENABLED(CONFIG_X86_32) &&
                            tbl64[i].table > U32_MAX) {
-                               pr_cont("\n");
                                pr_err("Table located above 4GB, disabling EFI.\n");
                                return -EINVAL;
                        }

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 595

Quoted 4 lines
drivers/firmware/efi/efi.c +1 -0
@@ -594,10 +592,14 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
                        table = tbl32[i].table;
                }
 
+#ifdef CONFIG_XEN_EFI

We tend to prefer IS_ENABLED() for cases such as this one. That way, the compiler always gets to see the code inside the conditional block, which gives better build test coverage (even if CONFIG_XEN_EFI is disabled).

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 596

Quoted 1 line
drivers/firmware/efi/efi.c +1 -0
+               if (efi_enabled(EFI_PARAVIRT) && !xen_config_table_memory_region_max(table))

So the question here is whether Xen thinks the table should be disregarded or not. So let's define a prototype that reflects that purpose, and let the implementation reason about how this should be achieved.

So

if (IS_ENABLED(CONFIG_XEN_EFI) &&

    efi_enabled(EFI_PARAVIRT) &&
    xen_efi_config_table_valid(guid, table)
        continue

I should note here, though, that EFI_PARAViRT is only set on x86 not on other architectures that enable CONFIG_XEN_EFI so this will not work anywhere else.

Quoted 23 lines
drivers/firmware/efi/efi.c +3 -1
+                       continue;
+#endif
+
                if (!match_config_table(guid, table, common_tables) && arch_tables)
                        match_config_table(guid, table, arch_tables);
        }
-       pr_cont("\n");
        set_bit(EFI_CONFIG_TABLES, &efi.flags);
 
        if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) {
drivers/xen/efi.c +1 -0
index d1ff2186ebb48a7c0981ecb6d4afcbbb25ffcea0..c2274ddfcc63304008ef0fd78fd9fa416f75d073 100644
@@ -28,6 +28,7 @@
 #include <xen/interface/platform.h>
 #include <xen/xen.h>
 #include <xen/xen-ops.h>
+#include <xen/page.h>
 
 #include <asm/page.h>
 

drivers/xen/efi.c static void xen_efi_reset_system(int reset_type, efi_status_t status, · line 275

Quoted 4 lines
drivers/xen/efi.c +1 -0
@@ -271,6 +272,40 @@ static void xen_efi_reset_system(int reset_type, efi_status_t status,
        }
 }
 
+__init u64 xen_config_table_memory_region_max(u64 addr)

It is more idiomatic for Linux to put __init after the return type. And if we adopt my suggestion above, this becomes

bool __init xen_efi_config_table_valid(const efi_guid_t *guid, u64 table)

Alternatively, you could pass the string identifier of the table instead of the guid (or both) to print in the diagnostic message.

drivers/xen/efi.c static void xen_efi_reset_system(int reset_type, efi_status_t status, · line 278

Quoted 3 lines
drivers/xen/efi.c +3 -0
+{
+       static_assert(XEN_PAGE_SHIFT == EFI_PAGE_SHIFT,
+                     "Mismatch between EFI_PAGE_SHIFT and XEN_PAGE_SHIFT");

Is this the only place where this matters? And this never happens on x86, right?

Quoted 12 lines
drivers/xen/efi.c +12 -0
+       struct xen_platform_op op = {
+               .cmd = XENPF_firmware_info,
+               .u.firmware_info = {
+                       .type = XEN_FW_EFI_INFO,
+                       .index = XEN_FW_EFI_MEM_INFO,
+                       .u.efi_info.mem.addr = addr,
+                       .u.efi_info.mem.size = U64_MAX - addr,
+               }
+       };
+       union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
+       int rc = HYPERVISOR_platform_op(&op);
+

drivers/xen/efi.c static void xen_efi_reset_system(int reset_type, efi_status_t status, · line 300

Quoted 10 lines
drivers/xen/efi.c +10 -0
+       if (rc) {
+               pr_warn("Failed to lookup header %llu in Xen memory map: error %d\n",
+                       (unsigned long long)addr, rc);
+               return 0;
+       }
+
+       switch (info->mem.type) {
+       case EFI_RUNTIME_SERVICES_CODE:
+       case EFI_RUNTIME_SERVICES_DATA:
+       case EFI_ACPI_RECLAIM_MEMORY:

If we are listing all memory types that Xen preserves, you might add EFI_RESERVED_MEMORY here. Otherwise, please only list the ones that you need to permit explicitly.

Quoted 16 lines
drivers/xen/efi.c +8 -0
+               return info->mem.addr + info->mem.size;
+       default:
+               pr_warn("Table %llu is in memory of type %d, ignoring it\n",
+                       (unsigned long long)addr, info->mem.type);
+               return 0;
+       }
+}
+
 /*
  * Set XEN EFI runtime services function pointers. Other fields of struct efi,
  * e.g. efi.systab, will be set like normal EFI.
include/linux/efi.h +0 -0
index d2b84c2fec39f0268324d1a38a73ed67786973c9..fc81e4b984398cdb399e7886b2cae7f33bf91613 100644

include/linux/efi.h struct linux_efi_coco_secret_area { · line 1327

Quoted 4 lines
include/linux/efi.h +1 -0
@@ -1324,4 +1324,13 @@ struct linux_efi_coco_secret_area {
 /* Header of a populated EFI secret area */
 #define EFI_SECRET_TABLE_HEADER_GUID   EFI_GUID(0x1e74f542, 0x71dd, 0x4d66,  0x96, 0x3e, 0xef, 0x42, 0x87, 0xff, 0x17, 0x3b)
 
+#ifdef CONFIG_XEN_EFI

Please drop this #ifdef

include/linux/efi.h struct linux_efi_coco_secret_area { · line 1333

Quoted 6 lines
include/linux/efi.h +6 -0
+/*
+ * Returns the end of the memory region containing the given config table,
+ * or 0 if the given address does not reside in memory that can validly
+ * contain EFI configuration tables.
+ */
+__init u64 xen_config_table_memory_region_max(u64 addr);

You can drop the __init here

Quoted 8 lines
include/linux/efi.h +2 -1
+#endif
+
 #endif /* _LINUX_EFI_H */
--

Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab

#22 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 33 lines · Ard Biesheuvel
Quoted 31 lines · Demi Marie Obenour

Memory of type EFI_CONVENTIONAL_MEMORY, EFI_LOADER_CODE, EFI_LOADER_DATA, EFI_BOOT_SERVICES_CODE, and EFI_BOOT_SERVICES_DATA may be clobbered by Xen before Linux gets to start using it. Therefore, Linux under Xen must not use EFI tables from such memory. Most of the remaining EFI memory types are not suitable for EFI tables, leaving only EFI_ACPI_RECLAIM_MEMORY, EFI_RUNTIME_SERVICES_DATA, and EFI_RUNTIME_SERVICES_CODE. When running under Xen, Linux should only use tables that are located in one of these types of memory.

This patch ensures this, and also adds a function (xen_config_table_memory_region_max()) that will be used later to replace the usage of the EFI memory map in esrt.c when running under Xen. This function can also be used in mokvar-table.c and efi-bgrt.c, but I have not implemented this.

---
 drivers/firmware/efi/efi.c |  8 +++++---
 drivers/xen/efi.c          | 35 +++++++++++++++++++++++++++++++++++
 include/linux/efi.h        |  9 +++++++++
 3 files changed, 49 insertions(+), 3 deletions(-)
drivers/firmware/efi/efi.c +0 -0
index e4080ad96089abd7f84745dd8461c548bcbb7685..d344f3ff73d1c5ed0c67e3251a9502e66719741d 100644
@@ -574,7 +574,6 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
        unsigned long table;
        int i;
 
Quoted 3 lines

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 577

Quoted 1 line
drivers/firmware/efi/efi.c +0 -1
-       pr_info("");

Why are you removing these prints?

If I left them, I would need to include a pr_cont("\n") later. Checkpatch recommends against that. What is the purpose of this print? I assumed that since it prints an empty string it is superfluous.

Quoted 15 lines
Quoted 15 lines
drivers/firmware/efi/efi.c +0 -1
        for (i = 0; i < count; i++) {
                if (!IS_ENABLED(CONFIG_X86)) {
                        guid = &config_tables[i].guid;
@@ -585,7 +584,6 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
 
                        if (IS_ENABLED(CONFIG_X86_32) &&
                            tbl64[i].table > U32_MAX) {
-                               pr_cont("\n");
                                pr_err("Table located above 4GB, disabling EFI.\n");
                                return -EINVAL;
                        }
@@ -594,10 +592,14 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
                        table = tbl32[i].table;
                }
 
Quoted 6 lines

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 595

Quoted 1 line
drivers/firmware/efi/efi.c +1 -0
+#ifdef CONFIG_XEN_EFI

We tend to prefer IS_ENABLED() for cases such as this one. That way, the compiler always gets to see the code inside the conditional block, which gives better build test coverage (even if CONFIG_XEN_EFI is disabled).

Can I count on the compiler eliminating the code as unreachable? With CONFIG_XEN_EFI disabled xen_config_table_memory_region_max() would be an undefined symbol.

Quoted 6 lines

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 596

Quoted 1 line
drivers/firmware/efi/efi.c +1 -0
+               if (efi_enabled(EFI_PARAVIRT) && !xen_config_table_memory_region_max(table))

So the question here is whether Xen thinks the table should be disregarded or not. So let's define a prototype that reflects that purpose, and let the implementation reason about how this should be achieved.

xen_config_table_memory_region_max() doesn’t just return whether the table should be disregarded, but also (if the table should not be ignored) the end of the memory region containing it. I will make xen_efi_config_table_valid() a wrapper around xen_config_table_memory_region_max(), as the former also needs to print a warning if the table is in an invalid location.

Quoted 10 lines

So

if (IS_ENABLED(CONFIG_XEN_EFI) &&

    efi_enabled(EFI_PARAVIRT) &&
    xen_efi_config_table_valid(guid, table)
        continue

I should note here, though, that EFI_PARAViRT is only set on x86 not on other architectures that enable CONFIG_XEN_EFI so this will not work anywhere else.

What should I use instead?

Quoted 31 lines
Quoted 23 lines
drivers/firmware/efi/efi.c +3 -1
+                       continue;
+#endif
+
                if (!match_config_table(guid, table, common_tables) && arch_tables)
                        match_config_table(guid, table, arch_tables);
        }
-       pr_cont("\n");
        set_bit(EFI_CONFIG_TABLES, &efi.flags);
 
        if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) {
drivers/xen/efi.c +1 -0
index d1ff2186ebb48a7c0981ecb6d4afcbbb25ffcea0..c2274ddfcc63304008ef0fd78fd9fa416f75d073 100644
@@ -28,6 +28,7 @@
 #include <xen/interface/platform.h>
 #include <xen/xen.h>
 #include <xen/xen-ops.h>
+#include <xen/page.h>
 
 #include <asm/page.h>
 

drivers/xen/efi.c static void xen_efi_reset_system(int reset_type, efi_status_t status, · line 275

Quoted 4 lines
drivers/xen/efi.c +1 -0
@@ -271,6 +272,40 @@ static void xen_efi_reset_system(int reset_type, efi_status_t status,
        }
 }
 
+__init u64 xen_config_table_memory_region_max(u64 addr)

It is more idiomatic for Linux to put __init after the return type. And if we adopt my suggestion above, this becomes

Quoted 4 lines

bool __init xen_efi_config_table_valid(const efi_guid_t *guid, u64 table)

Alternatively, you could pass the string identifier of the table instead of the guid (or both) to print in the diagnostic message.

Will fix in v5.

Quoted 5 lines

drivers/xen/efi.c static void xen_efi_reset_system(int reset_type, efi_status_t status, · line 278

Quoted 3 lines
drivers/xen/efi.c +3 -0
+{
+       static_assert(XEN_PAGE_SHIFT == EFI_PAGE_SHIFT,
+                     "Mismatch between EFI_PAGE_SHIFT and XEN_PAGE_SHIFT");

Is this the only place where this matters? And this never happens on x86, right?

My understanding is that it should never happen on any architecture. That’s why I static_assert() it. I have no idea if this is the only place it matters, though.

Quoted 18 lines
Quoted 18 lines
drivers/xen/efi.c +18 -0
+       struct xen_platform_op op = {
+               .cmd = XENPF_firmware_info,
+               .u.firmware_info = {
+                       .type = XEN_FW_EFI_INFO,
+                       .index = XEN_FW_EFI_MEM_INFO,
+                       .u.efi_info.mem.addr = addr,
+                       .u.efi_info.mem.size = U64_MAX - addr,
+               }
+       };
+       union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
+       int rc = HYPERVISOR_platform_op(&op);
+
+       if (rc) {
+               pr_warn("Failed to lookup header %llu in Xen memory map: error %d\n",
+                       (unsigned long long)addr, rc);
+               return 0;
+       }
+
Quoted 8 lines

drivers/xen/efi.c static void xen_efi_reset_system(int reset_type, efi_status_t status, · line 300

Quoted 4 lines
drivers/xen/efi.c +4 -0
+       switch (info->mem.type) {
+       case EFI_RUNTIME_SERVICES_CODE:
+       case EFI_RUNTIME_SERVICES_DATA:
+       case EFI_ACPI_RECLAIM_MEMORY:

If we are listing all memory types that Xen preserves, you might add EFI_RESERVED_MEMORY here. Otherwise, please only list the ones that you need to permit explicitly.

My understanding was that EFI_RESERVED_MEMORY should never be touched by the OS, so I left it out. Which types would you permit?

Quoted 19 lines
Quoted 19 lines
drivers/xen/efi.c +8 -0
+               return info->mem.addr + info->mem.size;
+       default:
+               pr_warn("Table %llu is in memory of type %d, ignoring it\n",
+                       (unsigned long long)addr, info->mem.type);
+               return 0;
+       }
+}
+
 /*
  * Set XEN EFI runtime services function pointers. Other fields of struct efi,
  * e.g. efi.systab, will be set like normal EFI.
include/linux/efi.h +0 -0
index d2b84c2fec39f0268324d1a38a73ed67786973c9..fc81e4b984398cdb399e7886b2cae7f33bf91613 100644
@@ -1324,4 +1324,13 @@ struct linux_efi_coco_secret_area {
 /* Header of a populated EFI secret area */
 #define EFI_SECRET_TABLE_HEADER_GUID   EFI_GUID(0x1e74f542, 0x71dd, 0x4d66,  0x96, 0x3e, 0xef, 0x42, 0x87, 0xff, 0x17, 0x3b)
 
Quoted 3 lines

include/linux/efi.h struct linux_efi_coco_secret_area { · line 1327

Quoted 1 line
include/linux/efi.h +1 -0
+#ifdef CONFIG_XEN_EFI

Please drop this #ifdef

Will fix in v5.

Quoted 8 lines

include/linux/efi.h struct linux_efi_coco_secret_area { · line 1333

Quoted 6 lines
include/linux/efi.h +6 -0
+/*
+ * Returns the end of the memory region containing the given config table,
+ * or 0 if the given address does not reside in memory that can validly
+ * contain EFI configuration tables.
+ */
+__init u64 xen_config_table_memory_region_max(u64 addr);

You can drop the __init here

Will fix in v5.

Quoted 8 lines
Quoted 8 lines
include/linux/efi.h +2 -1
+#endif
+
 #endif /* _LINUX_EFI_H */
--

Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
maintainer
#23 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 35 lines · Demi Marie Obenour
Quoted 33 lines · Ard Biesheuvel
Quoted 31 lines · Demi Marie Obenour

Memory of type EFI_CONVENTIONAL_MEMORY, EFI_LOADER_CODE, EFI_LOADER_DATA, EFI_BOOT_SERVICES_CODE, and EFI_BOOT_SERVICES_DATA may be clobbered by Xen before Linux gets to start using it. Therefore, Linux under Xen must not use EFI tables from such memory. Most of the remaining EFI memory types are not suitable for EFI tables, leaving only EFI_ACPI_RECLAIM_MEMORY, EFI_RUNTIME_SERVICES_DATA, and EFI_RUNTIME_SERVICES_CODE. When running under Xen, Linux should only use tables that are located in one of these types of memory.

This patch ensures this, and also adds a function (xen_config_table_memory_region_max()) that will be used later to replace the usage of the EFI memory map in esrt.c when running under Xen. This function can also be used in mokvar-table.c and efi-bgrt.c, but I have not implemented this.

---
 drivers/firmware/efi/efi.c |  8 +++++---
 drivers/xen/efi.c          | 35 +++++++++++++++++++++++++++++++++++
 include/linux/efi.h        |  9 +++++++++
 3 files changed, 49 insertions(+), 3 deletions(-)
drivers/firmware/efi/efi.c +0 -0
index e4080ad96089abd7f84745dd8461c548bcbb7685..d344f3ff73d1c5ed0c67e3251a9502e66719741d 100644
@@ -574,7 +574,6 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
        unsigned long table;
        int i;
 
Quoted 5 lines
Quoted 3 lines

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 577

Quoted 1 line
drivers/firmware/efi/efi.c +0 -1
-       pr_info("");

Why are you removing these prints?

If I left them, I would need to include a pr_cont("\n") later.

There should always be one at the end of the loop, no? Or is this related to the diagnostic that gets printed in your helper?

Quoted 3 lines

Checkpatch recommends against that. What is the purpose of this print? I assumed that since it prints an empty string it is superfluous.

It prints the leading [invisible] loglevel marker, and the 'efi: ' prefix.

Quoted 22 lines
Quoted 15 lines
Quoted 15 lines
drivers/firmware/efi/efi.c +0 -1
        for (i = 0; i < count; i++) {
                if (!IS_ENABLED(CONFIG_X86)) {
                        guid = &config_tables[i].guid;
@@ -585,7 +584,6 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
 
                        if (IS_ENABLED(CONFIG_X86_32) &&
                            tbl64[i].table > U32_MAX) {
-                               pr_cont("\n");
                                pr_err("Table located above 4GB, disabling EFI.\n");
                                return -EINVAL;
                        }
@@ -594,10 +592,14 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
                        table = tbl32[i].table;
                }
 
Quoted 6 lines

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 595

Quoted 1 line
drivers/firmware/efi/efi.c +1 -0
+#ifdef CONFIG_XEN_EFI

We tend to prefer IS_ENABLED() for cases such as this one. That way, the compiler always gets to see the code inside the conditional block, which gives better build test coverage (even if CONFIG_XEN_EFI is disabled).

Quoted 4 lines

Can I count on the compiler eliminating the code as unreachable? With CONFIG_XEN_EFI disabled xen_config_table_memory_region_max() would be an undefined symbol.

If you drop the #ifdef in the .h file (as I suggested below) the code will compile fine, and the symbol reference will not be emitted into the object, so it will link fine even if the Xen objects are not being built.

We rely on this behavior all over the Linux kernel.

Quoted 10 lines
Quoted 6 lines

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 596

Quoted 1 line
drivers/firmware/efi/efi.c +1 -0
+               if (efi_enabled(EFI_PARAVIRT) && !xen_config_table_memory_region_max(table))

So the question here is whether Xen thinks the table should be disregarded or not. So let's define a prototype that reflects that purpose, and let the implementation reason about how this should be achieved.

xen_config_table_memory_region_max() doesn’t just return whether the table should be disregarded, but also (if the table should not be ignored) the end of the memory region containing it.

But the calling code never uses that value, right?

Quoted 56 lines

I will make
xen_efi_config_table_valid() a wrapper around xen_config_table_memory_region_max(), as the former also needs to print a warning if the table is in an invalid location.

Quoted 10 lines

So

if (IS_ENABLED(CONFIG_XEN_EFI) &&

    efi_enabled(EFI_PARAVIRT) &&
    xen_efi_config_table_valid(guid, table)
        continue

I should note here, though, that EFI_PARAViRT is only set on x86 not on other architectures that enable CONFIG_XEN_EFI so this will not work anywhere else.

What should I use instead?

Quoted 31 lines
Quoted 23 lines
drivers/firmware/efi/efi.c +3 -1
+                       continue;
+#endif
+
                if (!match_config_table(guid, table, common_tables) && arch_tables)
                        match_config_table(guid, table, arch_tables);
        }
-       pr_cont("\n");
        set_bit(EFI_CONFIG_TABLES, &efi.flags);
 
        if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) {
drivers/xen/efi.c +1 -0
index d1ff2186ebb48a7c0981ecb6d4afcbbb25ffcea0..c2274ddfcc63304008ef0fd78fd9fa416f75d073 100644
@@ -28,6 +28,7 @@
 #include <xen/interface/platform.h>
 #include <xen/xen.h>
 #include <xen/xen-ops.h>
+#include <xen/page.h>
 
 #include <asm/page.h>
 

drivers/xen/efi.c static void xen_efi_reset_system(int reset_type, efi_status_t status, · line 275

Quoted 4 lines
drivers/xen/efi.c +1 -0
@@ -271,6 +272,40 @@ static void xen_efi_reset_system(int reset_type, efi_status_t status,
        }
 }
 
+__init u64 xen_config_table_memory_region_max(u64 addr)

It is more idiomatic for Linux to put __init after the return type. And if we adopt my suggestion above, this becomes

Quoted 4 lines

bool __init xen_efi_config_table_valid(const efi_guid_t *guid, u64 table)

Alternatively, you could pass the string identifier of the table instead of the guid (or both) to print in the diagnostic message.

Will fix in v5.

Quoted 7 lines
Quoted 5 lines

drivers/xen/efi.c static void xen_efi_reset_system(int reset_type, efi_status_t status, · line 278

Quoted 3 lines
drivers/xen/efi.c +3 -0
+{
+       static_assert(XEN_PAGE_SHIFT == EFI_PAGE_SHIFT,
+                     "Mismatch between EFI_PAGE_SHIFT and XEN_PAGE_SHIFT");

Is this the only place where this matters? And this never happens on x86, right?

My understanding is that it should never happen on any architecture.

EFI_PAGE_SHIFT is always 12, on any architecture and regardless of the page size used by the OS itself. AFAICT, the same applies to XEN_PAGE_SHIFT.

Quoted 3 lines

That’s why I static_assert() it. I have no idea if this is the only place it matters, though.

I don't mind adding this here, but it's kind of orthogonal to the rest of the patch so please make a mention in the commit log why you are adding it.

Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
drivers/xen/efi.c +18 -0
+       struct xen_platform_op op = {
+               .cmd = XENPF_firmware_info,
+               .u.firmware_info = {
+                       .type = XEN_FW_EFI_INFO,
+                       .index = XEN_FW_EFI_MEM_INFO,
+                       .u.efi_info.mem.addr = addr,
+                       .u.efi_info.mem.size = U64_MAX - addr,
+               }
+       };
+       union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
+       int rc = HYPERVISOR_platform_op(&op);
+
+       if (rc) {
+               pr_warn("Failed to lookup header %llu in Xen memory map: error %d\n",
+                       (unsigned long long)addr, rc);
+               return 0;
+       }
+
Quoted 8 lines

drivers/xen/efi.c static void xen_efi_reset_system(int reset_type, efi_status_t status, · line 300

Quoted 4 lines
drivers/xen/efi.c +4 -0
+       switch (info->mem.type) {
+       case EFI_RUNTIME_SERVICES_CODE:
+       case EFI_RUNTIME_SERVICES_DATA:
+       case EFI_ACPI_RECLAIM_MEMORY:

If we are listing all memory types that Xen preserves, you might add EFI_RESERVED_MEMORY here. Otherwise, please only list the ones that you need to permit explicitly.

Quoted 3 lines

My understanding was that EFI_RESERVED_MEMORY should never be touched by the OS, so I left it out. Which types would you permit?

Well, given the purpose of the function (i.e, to reject EfiBootServicesData in spite of the spec), I'd only permit EFI_ACPI_RECLAIM_MEMORY and EFI_RUNTIME_SERVICES_DATA. However, the EFI spec does mention that prior versions permitted the reserved memory type as well for ACPI and SMBIOS tables (although that may be a very long time ago).

Bottom line is that you want to permit only regions that Xen is guaranteed not to clobber, right? In any case, I'm not going to obsess over this so if you prefer to keep it this way, that's also fine.

Quoted 49 lines
Quoted 19 lines
Quoted 19 lines
drivers/xen/efi.c +8 -0
+               return info->mem.addr + info->mem.size;
+       default:
+               pr_warn("Table %llu is in memory of type %d, ignoring it\n",
+                       (unsigned long long)addr, info->mem.type);
+               return 0;
+       }
+}
+
 /*
  * Set XEN EFI runtime services function pointers. Other fields of struct efi,
  * e.g. efi.systab, will be set like normal EFI.
include/linux/efi.h +0 -0
index d2b84c2fec39f0268324d1a38a73ed67786973c9..fc81e4b984398cdb399e7886b2cae7f33bf91613 100644
@@ -1324,4 +1324,13 @@ struct linux_efi_coco_secret_area {
 /* Header of a populated EFI secret area */
 #define EFI_SECRET_TABLE_HEADER_GUID   EFI_GUID(0x1e74f542, 0x71dd, 0x4d66,  0x96, 0x3e, 0xef, 0x42, 0x87, 0xff, 0x17, 0x3b)
 
Quoted 3 lines

include/linux/efi.h struct linux_efi_coco_secret_area { · line 1327

Quoted 1 line
include/linux/efi.h +1 -0
+#ifdef CONFIG_XEN_EFI

Please drop this #ifdef

Will fix in v5.

Quoted 8 lines

include/linux/efi.h struct linux_efi_coco_secret_area { · line 1333

Quoted 6 lines
include/linux/efi.h +6 -0
+/*
+ * Returns the end of the memory region containing the given config table,
+ * or 0 if the given address does not reside in memory that can validly
+ * contain EFI configuration tables.
+ */
+__init u64 xen_config_table_memory_region_max(u64 addr);

You can drop the __init here

Will fix in v5.

Quoted 8 lines
Quoted 8 lines
include/linux/efi.h +2 -1
+#endif
+
 #endif /* _LINUX_EFI_H */
--

Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab

--
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab

#24 lore raw

Re: [PATCH v4 1/2] Avoid using EFI tables Xen may have clobbered

Quoted 37 lines · Ard Biesheuvel
Quoted 35 lines · Demi Marie Obenour
Quoted 33 lines · Ard Biesheuvel
Quoted 31 lines · Demi Marie Obenour

Memory of type EFI_CONVENTIONAL_MEMORY, EFI_LOADER_CODE, EFI_LOADER_DATA, EFI_BOOT_SERVICES_CODE, and EFI_BOOT_SERVICES_DATA may be clobbered by Xen before Linux gets to start using it. Therefore, Linux under Xen must not use EFI tables from such memory. Most of the remaining EFI memory types are not suitable for EFI tables, leaving only EFI_ACPI_RECLAIM_MEMORY, EFI_RUNTIME_SERVICES_DATA, and EFI_RUNTIME_SERVICES_CODE. When running under Xen, Linux should only use tables that are located in one of these types of memory.

This patch ensures this, and also adds a function (xen_config_table_memory_region_max()) that will be used later to replace the usage of the EFI memory map in esrt.c when running under Xen. This function can also be used in mokvar-table.c and efi-bgrt.c, but I have not implemented this.

---
 drivers/firmware/efi/efi.c |  8 +++++---
 drivers/xen/efi.c          | 35 +++++++++++++++++++++++++++++++++++
 include/linux/efi.h        |  9 +++++++++
 3 files changed, 49 insertions(+), 3 deletions(-)
drivers/firmware/efi/efi.c +0 -0
index e4080ad96089abd7f84745dd8461c548bcbb7685..d344f3ff73d1c5ed0c67e3251a9502e66719741d 100644
@@ -574,7 +574,6 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
        unsigned long table;
        int i;
 
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 577

Quoted 1 line
drivers/firmware/efi/efi.c +0 -1
-       pr_info("");

Why are you removing these prints?

If I left them, I would need to include a pr_cont("\n") later.

There should always be one at the end of the loop, no? Or is this related to the diagnostic that gets printed in your helper?

My helper emits a diagnostic (at severity KERN_WARNING) if the table is in memory that Xen has not reserved.

Quoted 5 lines
Quoted 3 lines

Checkpatch recommends against that. What is the purpose of this print? I assumed that since it prints an empty string it is superfluous.

It prints the leading [invisible] loglevel marker, and the 'efi: ' prefix.

Okay, that makes sense.

Quoted 27 lines
Quoted 22 lines
Quoted 15 lines
Quoted 15 lines
drivers/firmware/efi/efi.c +0 -1
        for (i = 0; i < count; i++) {
                if (!IS_ENABLED(CONFIG_X86)) {
                        guid = &config_tables[i].guid;
@@ -585,7 +584,6 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
 
                        if (IS_ENABLED(CONFIG_X86_32) &&
                            tbl64[i].table > U32_MAX) {
-                               pr_cont("\n");
                                pr_err("Table located above 4GB, disabling EFI.\n");
                                return -EINVAL;
                        }
@@ -594,10 +592,14 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
                        table = tbl32[i].table;
                }
 
Quoted 6 lines

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 595

Quoted 1 line
drivers/firmware/efi/efi.c +1 -0
+#ifdef CONFIG_XEN_EFI

We tend to prefer IS_ENABLED() for cases such as this one. That way, the compiler always gets to see the code inside the conditional block, which gives better build test coverage (even if CONFIG_XEN_EFI is disabled).

Quoted 4 lines

Can I count on the compiler eliminating the code as unreachable? With CONFIG_XEN_EFI disabled xen_config_table_memory_region_max() would be an undefined symbol.

Quoted 6 lines

If you drop the #ifdef in the .h file (as I suggested below) the code will compile fine, and the symbol reference will not be emitted into the object, so it will link fine even if the Xen objects are not being built.

We rely on this behavior all over the Linux kernel.

Okay, thanks!

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

drivers/firmware/efi/efi.c int __init efi_config_parse_tables(const efi_config_table_t *config_tables, · line 596

Quoted 1 line
drivers/firmware/efi/efi.c +1 -0
+               if (efi_enabled(EFI_PARAVIRT) && !xen_config_table_memory_region_max(table))

So the question here is whether Xen thinks the table should be disregarded or not. So let's define a prototype that reflects that purpose, and let the implementation reason about how this should be achieved.

Quoted 5 lines
Quoted 3 lines

xen_config_table_memory_region_max() doesn’t just return whether the table should be disregarded, but also (if the table should not be ignored) the end of the memory region containing it.

But the calling code never uses that value, right?

The code in this patch does not use that value. Patch 2 of 2 does use it.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
#25 lore raw

PATCH v4 2/2 Support ESRT in Xen dom0

In reply to Demi Marie Obenour

fwupd requires access to the EFI System Resource Table (ESRT) to discover which firmware can be updated by the OS. Currently, Linux does not expose the ESRT when running as a Xen dom0. Therefore, it is not possible to use fwupd in a Xen dom0, which is a serious problem for e.g. Qubes OS.

Before Xen 4.17, this was not fixable due to hypervisor limitations. The UEFI specification requires the ESRT to be in EfiBootServicesData memory, which Xen will use for whatever purposes it likes. Therefore, Linux cannot safely access the ESRT, as Xen may have overwritten it.

Starting with Xen 4.17, Xen checks if the ESRT is in EfiBootServicesData or EfiRuntimeServicesData memory. If the ESRT is in EfiBootServicesData memory, Xen replaces the ESRT with a copy in memory that it has reserved. Such memory is currently of type EFI_RUNTIME_SERVICES_DATA, but in the future it will be of type EFI_ACPI_RECLAIM_MEMORY. This ensures that the ESRT can safely be accessed by the OS.

When running as a Xen dom0, use the new xen_config_table_memory_region_max() function to determine if Xen has reserved the ESRT and, if so, find the end of the memory region containing it. This allows programs such as fwupd which require the ESRT to run under Xen, and so makes fwupd support in Qubes OS possible.

---
 drivers/firmware/efi/esrt.c | 43 ++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 13 deletions(-)
drivers/firmware/efi/esrt.c +30 -13
index 2a2f52b017e736dd995c69e8aeb5fbd7761732e5..a0642bc161b4b1f94f818b8c9f46511fe2424bb2 100644
@@ -243,27 +243,44 @@ void __init efi_esrt_init(void)
 	void *va;
 	struct efi_system_resource_table tmpesrt;
 	size_t size, max, entry_size, entries_size;
-	efi_memory_desc_t md;
-	int rc;
 	phys_addr_t end;
-
-	if (!efi_enabled(EFI_MEMMAP))
-		return;
+	u32 type;
 
 	pr_debug("esrt-init: loading.\n");
 	if (!esrt_table_exists())
 		return;
 
-	rc = efi_mem_desc_lookup(efi.esrt, &md);
-	if (rc < 0 ||
-	    (!(md.attribute & EFI_MEMORY_RUNTIME) &&
-	     md.type != EFI_BOOT_SERVICES_DATA &&
-	     md.type != EFI_RUNTIME_SERVICES_DATA)) {
-		pr_warn("ESRT header is not in the memory map.\n");
+	if (efi_enabled(EFI_MEMMAP)) {
+		efi_memory_desc_t md;
+
+		if (efi_mem_desc_lookup(efi.esrt, &md) < 0 ||
+		    (!(md.attribute & EFI_MEMORY_RUNTIME) &&
+		     md.type != EFI_BOOT_SERVICES_DATA &&
+		     md.type != EFI_RUNTIME_SERVICES_DATA)) {
+			pr_warn("ESRT header is not in the memory map.\n");
+			return;
+		}
+
+		type = md.type;
+		max = efi_mem_desc_end(&md);
+#ifdef CONFIG_XEN_EFI
+	} else if (efi_enabled(EFI_PARAVIRT)) {
+		max = xen_config_table_memory_region_max(efi.esrt);
+		/*
+		 * This might be wrong, but it doesn't matter.
+		 * xen_config_table_memory_region_max() checks the type
+		 * of the memory region, and if it returns 0, the code
+		 * below will fail without looking at the type.  Choose
+		 * a value that will not cause * subsequent code to try
+		 * to reserve the memory containing the ESRT, as either
+		 * Xen or the firmware has done so already.
+		 */
+		type = EFI_RUNTIME_SERVICES_DATA;
+#endif
+	} else {
 		return;
 	}
 
-	max = efi_mem_desc_end(&md);
 	if (max < efi.esrt) {
 		pr_err("EFI memory descriptor is invalid. (esrt: %p max: %p)\n",
 		       (void *)efi.esrt, (void *)max);
@@ -333,7 +350,7 @@ void __init efi_esrt_init(void)
 
 	end = esrt_data + size;
 	pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end);
-	if (md.type == EFI_BOOT_SERVICES_DATA)
+	if (type == EFI_BOOT_SERVICES_DATA)
 		efi_mem_reserve(esrt_data, esrt_data_size);
 
 	pr_debug("esrt-init: loaded.\n");
Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
maintainer
#26 lore raw

Re: [PATCH v4 2/2] Support ESRT in Xen dom0

Quoted 19 lines · Demi Marie Obenour

fwupd requires access to the EFI System Resource Table (ESRT) to discover which firmware can be updated by the OS. Currently, Linux does not expose the ESRT when running as a Xen dom0. Therefore, it is not possible to use fwupd in a Xen dom0, which is a serious problem for e.g. Qubes OS.

Before Xen 4.17, this was not fixable due to hypervisor limitations. The UEFI specification requires the ESRT to be in EfiBootServicesData memory, which Xen will use for whatever purposes it likes. Therefore, Linux cannot safely access the ESRT, as Xen may have overwritten it.

Starting with Xen 4.17, Xen checks if the ESRT is in EfiBootServicesData or EfiRuntimeServicesData memory. If the ESRT is in EfiBootServicesData memory, Xen replaces the ESRT with a copy in memory that it has reserved. Such memory is currently of type EFI_RUNTIME_SERVICES_DATA, but in the future it will be of type EFI_ACPI_RECLAIM_MEMORY. This ensures that the ESRT can safely be accessed by the OS.

Quoted 7 lines

When running as a Xen dom0, use the new xen_config_table_memory_region_max() function to determine if Xen has reserved the ESRT and, if so, find the end of the memory region containing it. This allows programs such as fwupd which require the ESRT to run under Xen, and so makes fwupd support in Qubes OS possible.

Why do we need this patch? I'd expect esrt_table_exists() to return false when patch 1/2 is applied.

Quoted 79 lines
---
 drivers/firmware/efi/esrt.c | 43 ++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 13 deletions(-)
drivers/firmware/efi/esrt.c +30 -14
index 2a2f52b017e736dd995c69e8aeb5fbd7761732e5..a0642bc161b4b1f94f818b8c9f46511fe2424bb2 100644
@@ -243,27 +243,44 @@ void __init efi_esrt_init(void)
        void *va;
        struct efi_system_resource_table tmpesrt;
        size_t size, max, entry_size, entries_size;
-       efi_memory_desc_t md;
-       int rc;
        phys_addr_t end;
-
-       if (!efi_enabled(EFI_MEMMAP))
-               return;
+       u32 type;
 
        pr_debug("esrt-init: loading.\n");
        if (!esrt_table_exists())
                return;
 
-       rc = efi_mem_desc_lookup(efi.esrt, &md);
-       if (rc < 0 ||
-           (!(md.attribute & EFI_MEMORY_RUNTIME) &&
-            md.type != EFI_BOOT_SERVICES_DATA &&
-            md.type != EFI_RUNTIME_SERVICES_DATA)) {
-               pr_warn("ESRT header is not in the memory map.\n");
+       if (efi_enabled(EFI_MEMMAP)) {
+               efi_memory_desc_t md;
+
+               if (efi_mem_desc_lookup(efi.esrt, &md) < 0 ||
+                   (!(md.attribute & EFI_MEMORY_RUNTIME) &&
+                    md.type != EFI_BOOT_SERVICES_DATA &&
+                    md.type != EFI_RUNTIME_SERVICES_DATA)) {
+                       pr_warn("ESRT header is not in the memory map.\n");
+                       return;
+               }
+
+               type = md.type;
+               max = efi_mem_desc_end(&md);
+#ifdef CONFIG_XEN_EFI
+       } else if (efi_enabled(EFI_PARAVIRT)) {
+               max = xen_config_table_memory_region_max(efi.esrt);
+               /*
+                * This might be wrong, but it doesn't matter.
+                * xen_config_table_memory_region_max() checks the type
+                * of the memory region, and if it returns 0, the code
+                * below will fail without looking at the type.  Choose
+                * a value that will not cause * subsequent code to try
+                * to reserve the memory containing the ESRT, as either
+                * Xen or the firmware has done so already.
+                */
+               type = EFI_RUNTIME_SERVICES_DATA;
+#endif
+       } else {
                return;
        }
 
-       max = efi_mem_desc_end(&md);
        if (max < efi.esrt) {
                pr_err("EFI memory descriptor is invalid. (esrt: %p max: %p)\n",
                       (void *)efi.esrt, (void *)max);
@@ -333,7 +350,7 @@ void __init efi_esrt_init(void)
 
        end = esrt_data + size;
        pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end);
-       if (md.type == EFI_BOOT_SERVICES_DATA)
+       if (type == EFI_BOOT_SERVICES_DATA)
                efi_mem_reserve(esrt_data, esrt_data_size);
 
        pr_debug("esrt-init: loaded.\n");
--

Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab

#27 lore raw

Re: [PATCH v4 2/2] Support ESRT in Xen dom0

Quoted 27 lines · Ard Biesheuvel
Quoted 25 lines · Demi Marie Obenour

fwupd requires access to the EFI System Resource Table (ESRT) to discover which firmware can be updated by the OS. Currently, Linux does not expose the ESRT when running as a Xen dom0. Therefore, it is not possible to use fwupd in a Xen dom0, which is a serious problem for e.g. Qubes OS.

Before Xen 4.17, this was not fixable due to hypervisor limitations. The UEFI specification requires the ESRT to be in EfiBootServicesData memory, which Xen will use for whatever purposes it likes. Therefore, Linux cannot safely access the ESRT, as Xen may have overwritten it.

Starting with Xen 4.17, Xen checks if the ESRT is in EfiBootServicesData or EfiRuntimeServicesData memory. If the ESRT is in EfiBootServicesData memory, Xen replaces the ESRT with a copy in memory that it has reserved. Such memory is currently of type EFI_RUNTIME_SERVICES_DATA, but in the future it will be of type EFI_ACPI_RECLAIM_MEMORY. This ensures that the ESRT can safely be accessed by the OS.

When running as a Xen dom0, use the new xen_config_table_memory_region_max() function to determine if Xen has reserved the ESRT and, if so, find the end of the memory region containing it. This allows programs such as fwupd which require the ESRT to run under Xen, and so makes fwupd support in Qubes OS possible.

Quoted 4 lines
Quoted 1 line

Why do we need this patch? I'd expect esrt_table_exists() to return false when patch 1/2 is applied.

efi_enabled(EFI_MEMMAP) is false under Xen, so there needs to be an alternative way to get the end of the memory region containing the ESRT. That is what this patch provides.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
maintainer
#28 lore raw

Re: [PATCH v4 2/2] Support ESRT in Xen dom0

Quoted 29 lines · Demi Marie Obenour
Quoted 27 lines · Ard Biesheuvel
Quoted 25 lines · Demi Marie Obenour

fwupd requires access to the EFI System Resource Table (ESRT) to discover which firmware can be updated by the OS. Currently, Linux does not expose the ESRT when running as a Xen dom0. Therefore, it is not possible to use fwupd in a Xen dom0, which is a serious problem for e.g. Qubes OS.

Before Xen 4.17, this was not fixable due to hypervisor limitations. The UEFI specification requires the ESRT to be in EfiBootServicesData memory, which Xen will use for whatever purposes it likes. Therefore, Linux cannot safely access the ESRT, as Xen may have overwritten it.

Starting with Xen 4.17, Xen checks if the ESRT is in EfiBootServicesData or EfiRuntimeServicesData memory. If the ESRT is in EfiBootServicesData memory, Xen replaces the ESRT with a copy in memory that it has reserved. Such memory is currently of type EFI_RUNTIME_SERVICES_DATA, but in the future it will be of type EFI_ACPI_RECLAIM_MEMORY. This ensures that the ESRT can safely be accessed by the OS.

When running as a Xen dom0, use the new xen_config_table_memory_region_max() function to determine if Xen has reserved the ESRT and, if so, find the end of the memory region containing it. This allows programs such as fwupd which require the ESRT to run under Xen, and so makes fwupd support in Qubes OS possible.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Why do we need this patch? I'd expect esrt_table_exists() to return false when patch 1/2 is applied.

efi_enabled(EFI_MEMMAP) is false under Xen, so there needs to be an alternative way to get the end of the memory region containing the ESRT. That is what this patch provides.

OK. I don't think we need that to be honest. When running under Xen, we should be able to assume that the ESRT does not span multiple memory regions arbitrarily, so we can just omit this check if !efi_enabled(EFI_MEMMAP)

IIRC (and Peter would know), we are trying to filter out descriptors that are completely bogus here: zero lenght, zero address, etc etc. I don't think we need that for Xen.

#29 lore raw

Re: [PATCH v4 2/2] Support ESRT in Xen dom0

Quoted 40 lines · Ard Biesheuvel
Quoted 29 lines · Demi Marie Obenour
Quoted 27 lines · Ard Biesheuvel
Quoted 25 lines · Demi Marie Obenour

fwupd requires access to the EFI System Resource Table (ESRT) to discover which firmware can be updated by the OS. Currently, Linux does not expose the ESRT when running as a Xen dom0. Therefore, it is not possible to use fwupd in a Xen dom0, which is a serious problem for e.g. Qubes OS.

Before Xen 4.17, this was not fixable due to hypervisor limitations. The UEFI specification requires the ESRT to be in EfiBootServicesData memory, which Xen will use for whatever purposes it likes. Therefore, Linux cannot safely access the ESRT, as Xen may have overwritten it.

Starting with Xen 4.17, Xen checks if the ESRT is in EfiBootServicesData or EfiRuntimeServicesData memory. If the ESRT is in EfiBootServicesData memory, Xen replaces the ESRT with a copy in memory that it has reserved. Such memory is currently of type EFI_RUNTIME_SERVICES_DATA, but in the future it will be of type EFI_ACPI_RECLAIM_MEMORY. This ensures that the ESRT can safely be accessed by the OS.

When running as a Xen dom0, use the new xen_config_table_memory_region_max() function to determine if Xen has reserved the ESRT and, if so, find the end of the memory region containing it. This allows programs such as fwupd which require the ESRT to run under Xen, and so makes fwupd support in Qubes OS possible.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Why do we need this patch? I'd expect esrt_table_exists() to return false when patch 1/2 is applied.

efi_enabled(EFI_MEMMAP) is false under Xen, so there needs to be an alternative way to get the end of the memory region containing the ESRT. That is what this patch provides.

Quoted 8 lines

OK. I don't think we need that to be honest. When running under Xen, we should be able to assume that the ESRT does not span multiple memory regions arbitrarily, so we can just omit this check if !efi_enabled(EFI_MEMMAP)

IIRC (and Peter would know), we are trying to filter out descriptors that are completely bogus here: zero lenght, zero address, etc etc. I don't think we need that for Xen.

Xen doesn’t uninstall bogus ESRTs, so there is no less reason to worry under Xen than on bare hardware.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
maintainer
#30 lore raw

Re: [PATCH v4 2/2] Support ESRT in Xen dom0

Quoted 47 lines · Demi Marie Obenour
Quoted 45 lines · Ard Biesheuvel
Quoted 29 lines · Demi Marie Obenour
Quoted 27 lines · Ard Biesheuvel
Quoted 25 lines · Demi Marie Obenour

fwupd requires access to the EFI System Resource Table (ESRT) to discover which firmware can be updated by the OS. Currently, Linux does not expose the ESRT when running as a Xen dom0. Therefore, it is not possible to use fwupd in a Xen dom0, which is a serious problem for e.g. Qubes OS.

Before Xen 4.17, this was not fixable due to hypervisor limitations. The UEFI specification requires the ESRT to be in EfiBootServicesData memory, which Xen will use for whatever purposes it likes. Therefore, Linux cannot safely access the ESRT, as Xen may have overwritten it.

Starting with Xen 4.17, Xen checks if the ESRT is in EfiBootServicesData or EfiRuntimeServicesData memory. If the ESRT is in EfiBootServicesData memory, Xen replaces the ESRT with a copy in memory that it has reserved. Such memory is currently of type EFI_RUNTIME_SERVICES_DATA, but in the future it will be of type EFI_ACPI_RECLAIM_MEMORY. This ensures that the ESRT can safely be accessed by the OS.

When running as a Xen dom0, use the new xen_config_table_memory_region_max() function to determine if Xen has reserved the ESRT and, if so, find the end of the memory region containing it. This allows programs such as fwupd which require the ESRT to run under Xen, and so makes fwupd support in Qubes OS possible.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Why do we need this patch? I'd expect esrt_table_exists() to return false when patch 1/2 is applied.

efi_enabled(EFI_MEMMAP) is false under Xen, so there needs to be an alternative way to get the end of the memory region containing the ESRT. That is what this patch provides.

OK. I don't think we need that to be honest. When running under Xen, we should be able to assume that the ESRT does not span multiple memory regions arbitrarily, so we can just omit this check if !efi_enabled(EFI_MEMMAP)

Quoted 6 lines
Quoted 3 lines

IIRC (and Peter would know), we are trying to filter out descriptors that are completely bogus here: zero lenght, zero address, etc etc. I don't think we need that for Xen.

Xen doesn’t uninstall bogus ESRTs, so there is no less reason to worry under Xen than on bare hardware.

That may be true. But if Xen needs dom0 to be able to cross reference the EFI memory map, it should provide one (and set EFI_MEMMAP to enabled).

maintainer
#31 lore raw

Re: [PATCH v4 2/2] Support ESRT in Xen dom0

Quoted 54 lines · Ard Biesheuvel
Quoted 51 lines · Demi Marie Obenour
Quoted 48 lines · Ard Biesheuvel
Quoted 29 lines · Demi Marie Obenour
Quoted 27 lines · Ard Biesheuvel
Quoted 25 lines · Demi Marie Obenour

fwupd requires access to the EFI System Resource Table (ESRT) to discover which firmware can be updated by the OS. Currently, Linux does not expose the ESRT when running as a Xen dom0. Therefore, it is not possible to use fwupd in a Xen dom0, which is a serious problem for e.g. Qubes OS.

Before Xen 4.17, this was not fixable due to hypervisor limitations. The UEFI specification requires the ESRT to be in EfiBootServicesData memory, which Xen will use for whatever purposes it likes. Therefore, Linux cannot safely access the ESRT, as Xen may have overwritten it.

Starting with Xen 4.17, Xen checks if the ESRT is in EfiBootServicesData or EfiRuntimeServicesData memory. If the ESRT is in EfiBootServicesData memory, Xen replaces the ESRT with a copy in memory that it has reserved. Such memory is currently of type EFI_RUNTIME_SERVICES_DATA, but in the future it will be of type EFI_ACPI_RECLAIM_MEMORY. This ensures that the ESRT can safely be accessed by the OS.

When running as a Xen dom0, use the new xen_config_table_memory_region_max() function to determine if Xen has reserved the ESRT and, if so, find the end of the memory region containing it. This allows programs such as fwupd which require the ESRT to run under Xen, and so makes fwupd support in Qubes OS possible.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Why do we need this patch? I'd expect esrt_table_exists() to return false when patch 1/2 is applied.

efi_enabled(EFI_MEMMAP) is false under Xen, so there needs to be an alternative way to get the end of the memory region containing the ESRT. That is what this patch provides.

OK. I don't think we need that to be honest. When running under Xen, we should be able to assume that the ESRT does not span multiple memory regions arbitrarily, so we can just omit this check if !efi_enabled(EFI_MEMMAP)

IIRC (and Peter would know), we are trying to filter out descriptors that are completely bogus here: zero lenght, zero address, etc etc. I don't think we need that for Xen.

Quoted 6 lines
Quoted 2 lines

Xen doesn’t uninstall bogus ESRTs, so there is no less reason to worry under Xen than on bare hardware.

That may be true. But if Xen needs dom0 to be able to cross reference the EFI memory map, it should provide one (and set EFI_MEMMAP to enabled).

Btw the efi_mem_reserve() for the ESRT is also redundant if it is guaranteed to be in RT services data or ACPI reclaim memory.

#32 lore raw

Re: [PATCH v4 2/2] Support ESRT in Xen dom0

Quoted 58 lines · Ard Biesheuvel
Quoted 57 lines · Ard Biesheuvel
Quoted 53 lines · Demi Marie Obenour
Quoted 40 lines · Ard Biesheuvel
Quoted 29 lines · Demi Marie Obenour
Quoted 27 lines · Ard Biesheuvel
Quoted 25 lines · Demi Marie Obenour

fwupd requires access to the EFI System Resource Table (ESRT) to discover which firmware can be updated by the OS. Currently, Linux does not expose the ESRT when running as a Xen dom0. Therefore, it is not possible to use fwupd in a Xen dom0, which is a serious problem for e.g. Qubes OS.

Before Xen 4.17, this was not fixable due to hypervisor limitations. The UEFI specification requires the ESRT to be in EfiBootServicesData memory, which Xen will use for whatever purposes it likes. Therefore, Linux cannot safely access the ESRT, as Xen may have overwritten it.

Starting with Xen 4.17, Xen checks if the ESRT is in EfiBootServicesData or EfiRuntimeServicesData memory. If the ESRT is in EfiBootServicesData memory, Xen replaces the ESRT with a copy in memory that it has reserved. Such memory is currently of type EFI_RUNTIME_SERVICES_DATA, but in the future it will be of type EFI_ACPI_RECLAIM_MEMORY. This ensures that the ESRT can safely be accessed by the OS.

When running as a Xen dom0, use the new xen_config_table_memory_region_max() function to determine if Xen has reserved the ESRT and, if so, find the end of the memory region containing it. This allows programs such as fwupd which require the ESRT to run under Xen, and so makes fwupd support in Qubes OS possible.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Why do we need this patch? I'd expect esrt_table_exists() to return false when patch 1/2 is applied.

efi_enabled(EFI_MEMMAP) is false under Xen, so there needs to be an alternative way to get the end of the memory region containing the ESRT. That is what this patch provides.

Quoted 8 lines

OK. I don't think we need that to be honest. When running under Xen, we should be able to assume that the ESRT does not span multiple memory regions arbitrarily, so we can just omit this check if !efi_enabled(EFI_MEMMAP)

IIRC (and Peter would know), we are trying to filter out descriptors that are completely bogus here: zero lenght, zero address, etc etc. I don't think we need that for Xen.

Xen doesn’t uninstall bogus ESRTs, so there is no less reason to worry under Xen than on bare hardware.

Quoted 6 lines
Quoted 3 lines

That may be true. But if Xen needs dom0 to be able to cross reference the EFI memory map, it should provide one (and set EFI_MEMMAP to enabled).

Btw the efi_mem_reserve() for the ESRT is also redundant if it is guaranteed to be in RT services data or ACPI reclaim memory.

It’s needed on bare hardware. On Xen it’s unreachable code.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
#33 lore raw

Re: [PATCH v4 2/2] Support ESRT in Xen dom0

In reply to Ard Biesheuvel
Quoted 53 lines · Ard Biesheuvel
Quoted 51 lines · Demi Marie Obenour
Quoted 48 lines · Ard Biesheuvel
Quoted 29 lines · Demi Marie Obenour
Quoted 27 lines · Ard Biesheuvel
Quoted 25 lines · Demi Marie Obenour

fwupd requires access to the EFI System Resource Table (ESRT) to discover which firmware can be updated by the OS. Currently, Linux does not expose the ESRT when running as a Xen dom0. Therefore, it is not possible to use fwupd in a Xen dom0, which is a serious problem for e.g. Qubes OS.

Before Xen 4.17, this was not fixable due to hypervisor limitations. The UEFI specification requires the ESRT to be in EfiBootServicesData memory, which Xen will use for whatever purposes it likes. Therefore, Linux cannot safely access the ESRT, as Xen may have overwritten it.

Starting with Xen 4.17, Xen checks if the ESRT is in EfiBootServicesData or EfiRuntimeServicesData memory. If the ESRT is in EfiBootServicesData memory, Xen replaces the ESRT with a copy in memory that it has reserved. Such memory is currently of type EFI_RUNTIME_SERVICES_DATA, but in the future it will be of type EFI_ACPI_RECLAIM_MEMORY. This ensures that the ESRT can safely be accessed by the OS.

When running as a Xen dom0, use the new xen_config_table_memory_region_max() function to determine if Xen has reserved the ESRT and, if so, find the end of the memory region containing it. This allows programs such as fwupd which require the ESRT to run under Xen, and so makes fwupd support in Qubes OS possible.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Why do we need this patch? I'd expect esrt_table_exists() to return false when patch 1/2 is applied.

efi_enabled(EFI_MEMMAP) is false under Xen, so there needs to be an alternative way to get the end of the memory region containing the ESRT. That is what this patch provides.

OK. I don't think we need that to be honest. When running under Xen, we should be able to assume that the ESRT does not span multiple memory regions arbitrarily, so we can just omit this check if !efi_enabled(EFI_MEMMAP)

IIRC (and Peter would know), we are trying to filter out descriptors that are completely bogus here: zero lenght, zero address, etc etc. I don't think we need that for Xen.

Quoted 6 lines
Quoted 2 lines

Xen doesn’t uninstall bogus ESRTs, so there is no less reason to worry under Xen than on bare hardware.

That may be true. But if Xen needs dom0 to be able to cross reference the EFI memory map, it should provide one (and set EFI_MEMMAP to enabled).

I agree, but it is also a significant amount of work compared to this patch.

Signature · 3 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab