bhyve: merge adjacent E820 entries

EDKII can allocate adjacent E820 entries only if they are at a page
boundary.  For some unknown and probably strange reasons, Intel puts
it's OpRegion at an offset of 0x18 bytes. If the VBT lays directly
behind the OpRegion, we're going to try allocating two adjacent E820
sharing the same page. This causes EDKII to do not properly allocate
those entries. A Linux guest then isn't able to map the VBT and those
fails to find it.

Reviewed by:		markj
MFC after:		1 week
Sponsored by:		Beckhoff Automation GmbH & Co. KG
Differential Revision:	https://reviews.freebsd.org/D45336

(cherry picked from commit 08139140c5)
This commit is contained in:
Corvin Köhne 2023-12-19 15:01:45 +01:00
parent b3180bea9b
commit 660e7182db
No known key found for this signature in database
GPG key ID: D854DA56315E026A

View file

@ -156,6 +156,7 @@ e820_add_entry(const uint64_t base, const uint64_t end,
{
struct e820_element *new_element;
struct e820_element *element;
struct e820_element *sib_element;
struct e820_element *ram_element;
assert(end >= base);
@ -273,6 +274,33 @@ e820_add_entry(const uint64_t base, const uint64_t end,
element->base = end;
}
/*
* If the previous element has the same type and ends at our base
* boundary, we can merge both entries.
*/
sib_element = TAILQ_PREV(new_element, e820_table, chain);
if (sib_element != NULL &&
sib_element->type == new_element->type &&
sib_element->end == new_element->base) {
new_element->base = sib_element->base;
TAILQ_REMOVE(&e820_table, sib_element, chain);
free(sib_element);
}
/*
* If the next element has the same type and starts at our end
* boundary, we can merge both entries.
*/
sib_element = TAILQ_NEXT(new_element, chain);
if (sib_element != NULL &&
sib_element->type == new_element->type &&
sib_element->base == new_element->end) {
/* Merge new element into subsequent one. */
new_element->end = sib_element->end;
TAILQ_REMOVE(&e820_table, sib_element, chain);
free(sib_element);
}
return (0);
}