2020-06-05 18:51:33 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2020-09-20 13:03:44 +03:00
|
|
|
#include <mm/pmm.h>
|
2020-09-18 21:02:10 +03:00
|
|
|
#include <sys/e820.h>
|
2021-05-05 01:12:20 +03:00
|
|
|
#include <lib/acpi.h>
|
2020-06-05 18:51:33 +03:00
|
|
|
#include <lib/blib.h>
|
2020-09-20 13:03:44 +03:00
|
|
|
#include <lib/libc.h>
|
2020-06-05 21:57:09 +03:00
|
|
|
#include <lib/print.h>
|
2021-03-02 12:23:43 +03:00
|
|
|
#if defined (uefi)
|
|
|
|
# include <efi.h>
|
|
|
|
#endif
|
2020-06-05 18:51:33 +03:00
|
|
|
|
|
|
|
#define PAGE_SIZE 4096
|
|
|
|
#define MEMMAP_MAX_ENTRIES 256
|
|
|
|
|
2021-03-07 06:10:44 +03:00
|
|
|
#if defined (bios)
|
|
|
|
extern symbol bss_end;
|
|
|
|
#endif
|
|
|
|
|
2021-04-15 03:21:38 +03:00
|
|
|
static bool allocations_disallowed = true;
|
|
|
|
static void sanitise_entries(bool align_entries);
|
2021-03-07 06:10:44 +03:00
|
|
|
|
|
|
|
void *conv_mem_alloc(size_t count) {
|
2021-04-15 03:21:38 +03:00
|
|
|
static uintptr_t base = 4096;
|
2021-03-07 06:10:44 +03:00
|
|
|
|
2021-04-15 03:21:38 +03:00
|
|
|
if (allocations_disallowed)
|
|
|
|
panic("Memory allocations disallowed");
|
2021-03-07 06:10:44 +03:00
|
|
|
|
2021-04-15 03:21:38 +03:00
|
|
|
count = ALIGN_UP(count, 4096);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (base + count > 0x100000)
|
|
|
|
panic("Conventional memory allocation failed");
|
|
|
|
|
|
|
|
if (memmap_alloc_range(base, count, MEMMAP_BOOTLOADER_RECLAIMABLE, true, false, false, false)) {
|
|
|
|
void *ret = (void *)base;
|
|
|
|
// Zero out allocated space
|
|
|
|
memset(ret, 0, count);
|
|
|
|
base += count;
|
|
|
|
|
|
|
|
sanitise_entries(false);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2021-03-07 06:10:44 +03:00
|
|
|
|
2021-04-15 03:21:38 +03:00
|
|
|
base += 4096;
|
|
|
|
}
|
2021-03-07 06:10:44 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 10:47:37 +03:00
|
|
|
struct e820_entry_t memmap[MEMMAP_MAX_ENTRIES];
|
|
|
|
size_t memmap_entries = 0;
|
2020-06-05 18:51:33 +03:00
|
|
|
|
2020-06-05 21:57:09 +03:00
|
|
|
static const char *memmap_type(uint32_t type) {
|
|
|
|
switch (type) {
|
2020-08-11 18:43:39 +03:00
|
|
|
case MEMMAP_USABLE:
|
2020-06-05 21:57:09 +03:00
|
|
|
return "Usable RAM";
|
2020-08-11 18:43:39 +03:00
|
|
|
case MEMMAP_RESERVED:
|
2020-06-05 21:57:09 +03:00
|
|
|
return "Reserved";
|
2020-08-11 18:43:39 +03:00
|
|
|
case MEMMAP_ACPI_RECLAIMABLE:
|
2020-06-05 21:57:09 +03:00
|
|
|
return "ACPI reclaimable";
|
2020-08-11 18:43:39 +03:00
|
|
|
case MEMMAP_ACPI_NVS:
|
2020-06-05 21:57:09 +03:00
|
|
|
return "ACPI NVS";
|
2020-08-11 18:43:39 +03:00
|
|
|
case MEMMAP_BAD_MEMORY:
|
2020-06-05 21:57:09 +03:00
|
|
|
return "Bad memory";
|
2020-08-11 18:43:39 +03:00
|
|
|
case MEMMAP_BOOTLOADER_RECLAIMABLE:
|
|
|
|
return "Bootloader reclaimable";
|
|
|
|
case MEMMAP_KERNEL_AND_MODULES:
|
2020-06-05 21:57:09 +03:00
|
|
|
return "Kernel/Modules";
|
2021-03-05 08:10:58 +03:00
|
|
|
case MEMMAP_EFI_RECLAIMABLE:
|
|
|
|
return "EFI reclaimable";
|
2021-05-06 05:31:05 +03:00
|
|
|
case MEMMAP_EFI_LOADER:
|
|
|
|
return "EFI loader";
|
2020-06-05 21:57:09 +03:00
|
|
|
default:
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_memmap(struct e820_entry_t *mm, size_t size) {
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
2021-05-11 07:46:42 +03:00
|
|
|
printv("[%X -> %X] : %X <%s>\n",
|
|
|
|
mm[i].base,
|
|
|
|
mm[i].base + mm[i].length,
|
|
|
|
mm[i].length,
|
|
|
|
memmap_type(mm[i].type));
|
2020-06-05 21:57:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:29:30 +03:00
|
|
|
static bool align_entry(uint64_t *base, uint64_t *length) {
|
2020-06-06 17:52:21 +03:00
|
|
|
if (*length < PAGE_SIZE)
|
2020-11-24 02:19:00 +03:00
|
|
|
return false;
|
2020-06-05 22:34:11 +03:00
|
|
|
|
2020-06-06 17:52:21 +03:00
|
|
|
uint64_t orig_base = *base;
|
2020-06-05 22:34:11 +03:00
|
|
|
|
2020-06-06 17:52:21 +03:00
|
|
|
*base = ALIGN_UP(*base, PAGE_SIZE);
|
2020-06-05 18:51:33 +03:00
|
|
|
|
2020-06-06 17:52:21 +03:00
|
|
|
*length -= (*base - orig_base);
|
|
|
|
*length = ALIGN_DOWN(*length, PAGE_SIZE);
|
2020-06-05 18:51:33 +03:00
|
|
|
|
|
|
|
if (!length)
|
2020-11-14 15:29:30 +03:00
|
|
|
return false;
|
2020-06-05 18:51:33 +03:00
|
|
|
|
2020-11-14 15:29:30 +03:00
|
|
|
return true;
|
2020-06-05 18:51:33 +03:00
|
|
|
}
|
|
|
|
|
2020-11-14 15:29:30 +03:00
|
|
|
static void sanitise_entries(bool align_entries) {
|
2020-06-05 18:51:33 +03:00
|
|
|
for (size_t i = 0; i < memmap_entries; i++) {
|
2020-09-30 23:51:56 +03:00
|
|
|
if (memmap[i].type != MEMMAP_USABLE)
|
2020-06-05 18:51:33 +03:00
|
|
|
continue;
|
|
|
|
|
2020-06-06 17:52:21 +03:00
|
|
|
// Check if the entry overlaps other entries
|
|
|
|
for (size_t j = 0; j < memmap_entries; j++) {
|
|
|
|
if (j == i)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uint64_t base = memmap[i].base;
|
|
|
|
uint64_t length = memmap[i].length;
|
|
|
|
uint64_t top = base + length;
|
|
|
|
|
|
|
|
uint64_t res_base = memmap[j].base;
|
|
|
|
uint64_t res_length = memmap[j].length;
|
|
|
|
uint64_t res_top = res_base + res_length;
|
|
|
|
|
|
|
|
// TODO actually handle splitting off usable chunks
|
|
|
|
if ( (res_base >= base && res_base < top)
|
|
|
|
&& (res_top >= base && res_top < top) ) {
|
|
|
|
panic("A non-usable e820 entry is inside a usable section.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res_base >= base && res_base < top) {
|
|
|
|
top = res_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res_top >= base && res_top < top) {
|
|
|
|
base = res_top;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmap[i].base = base;
|
|
|
|
memmap[i].length = top - base;
|
|
|
|
}
|
|
|
|
|
2020-11-24 02:19:00 +03:00
|
|
|
if (!memmap[i].length
|
|
|
|
|| (align_entries && !align_entry(&memmap[i].base, &memmap[i].length))) {
|
2020-06-05 18:51:33 +03:00
|
|
|
// Eradicate from memmap
|
|
|
|
for (size_t j = i; j < memmap_entries - 1; j++) {
|
|
|
|
memmap[j] = memmap[j+1];
|
|
|
|
}
|
|
|
|
memmap_entries--;
|
2020-06-06 17:52:21 +03:00
|
|
|
i--;
|
2020-06-05 18:51:33 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 20:09:57 +03:00
|
|
|
|
|
|
|
// Sort the entries
|
|
|
|
for (size_t p = 0; p < memmap_entries - 1; p++) {
|
|
|
|
uint64_t min = memmap[p].base;
|
|
|
|
size_t min_index = p;
|
|
|
|
for (size_t i = p; i < memmap_entries; i++) {
|
|
|
|
if (memmap[i].base < min) {
|
|
|
|
min = memmap[i].base;
|
|
|
|
min_index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct e820_entry_t min_e = memmap[min_index];
|
|
|
|
memmap[min_index] = memmap[p];
|
|
|
|
memmap[p] = min_e;
|
|
|
|
}
|
2020-09-30 23:51:56 +03:00
|
|
|
|
2021-03-10 03:23:44 +03:00
|
|
|
// Merge contiguous bootloader-reclaimable and usable entries
|
2020-09-30 23:51:56 +03:00
|
|
|
for (size_t i = 0; i < memmap_entries - 1; i++) {
|
2021-03-10 03:23:44 +03:00
|
|
|
if (memmap[i].type != MEMMAP_BOOTLOADER_RECLAIMABLE
|
|
|
|
&& memmap[i].type != MEMMAP_USABLE)
|
2020-09-30 23:51:56 +03:00
|
|
|
continue;
|
|
|
|
|
2021-03-10 03:23:44 +03:00
|
|
|
if (memmap[i+1].type == memmap[i].type
|
2020-09-30 23:51:56 +03:00
|
|
|
&& memmap[i+1].base == memmap[i].base + memmap[i].length) {
|
|
|
|
memmap[i].length += memmap[i+1].length;
|
|
|
|
|
|
|
|
// Eradicate from memmap
|
|
|
|
for (size_t j = i+1; j < memmap_entries - 1; j++) {
|
|
|
|
memmap[j] = memmap[j+1];
|
|
|
|
}
|
|
|
|
memmap_entries--;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 17:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct e820_entry_t *get_memmap(size_t *entries) {
|
2020-11-14 15:29:30 +03:00
|
|
|
sanitise_entries(true);
|
2020-06-05 20:09:57 +03:00
|
|
|
|
2020-06-05 18:51:33 +03:00
|
|
|
*entries = memmap_entries;
|
2020-06-05 21:57:09 +03:00
|
|
|
|
2020-12-10 10:47:37 +03:00
|
|
|
allocations_disallowed = true;
|
|
|
|
|
2020-06-05 18:51:33 +03:00
|
|
|
return memmap;
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
#if defined (bios)
|
2020-06-05 18:51:33 +03:00
|
|
|
void init_memmap(void) {
|
|
|
|
for (size_t i = 0; i < e820_entries; i++) {
|
|
|
|
if (memmap_entries == MEMMAP_MAX_ENTRIES) {
|
|
|
|
panic("Memory map exhausted.");
|
|
|
|
}
|
|
|
|
|
2021-04-15 04:44:25 +03:00
|
|
|
memmap[memmap_entries] = e820_map[i];
|
|
|
|
|
2021-05-05 01:12:20 +03:00
|
|
|
uint64_t top = memmap[memmap_entries].base + memmap[memmap_entries].length;
|
|
|
|
|
|
|
|
if (memmap[memmap_entries].type == MEMMAP_USABLE) {
|
|
|
|
if (memmap[memmap_entries].base < 0x1000) {
|
|
|
|
if (top <= 0x1000) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmap[memmap_entries].length -= 0x1000 - memmap[memmap_entries].base;
|
|
|
|
memmap[memmap_entries].base = 0x1000;
|
2021-04-15 04:44:25 +03:00
|
|
|
}
|
|
|
|
|
2021-05-05 01:12:20 +03:00
|
|
|
if (memmap[memmap_entries].base >= EBDA && memmap[memmap_entries].base < 0x100000) {
|
|
|
|
if (top <= 0x100000)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
memmap[memmap_entries].length -= 0x100000 - memmap[memmap_entries].base;
|
|
|
|
memmap[memmap_entries].base = 0x100000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (top > EBDA && top <= 0x100000) {
|
|
|
|
memmap[memmap_entries].length -= top - EBDA;
|
|
|
|
}
|
2021-04-15 04:44:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
memmap_entries++;
|
2020-06-05 18:51:33 +03:00
|
|
|
}
|
2020-06-06 17:52:21 +03:00
|
|
|
|
2021-04-15 03:21:38 +03:00
|
|
|
// Allocate bootloader itself
|
|
|
|
memmap_alloc_range(4096,
|
|
|
|
ALIGN_UP((uintptr_t)bss_end, 4096) - 4096, MEMMAP_BOOTLOADER_RECLAIMABLE, true, true, false, false);
|
|
|
|
|
2020-11-14 15:29:30 +03:00
|
|
|
sanitise_entries(false);
|
2020-12-10 10:47:37 +03:00
|
|
|
|
|
|
|
allocations_disallowed = false;
|
2020-06-05 18:51:33 +03:00
|
|
|
}
|
2021-03-02 12:23:43 +03:00
|
|
|
#endif
|
2020-06-05 18:51:33 +03:00
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
#if defined (uefi)
|
|
|
|
void init_memmap(void) {
|
|
|
|
EFI_STATUS status;
|
2020-09-19 16:29:19 +03:00
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
EFI_MEMORY_DESCRIPTOR tmp_mmap[1];
|
2021-04-29 00:41:34 +03:00
|
|
|
efi_mmap_size = sizeof(tmp_mmap);
|
|
|
|
UINTN mmap_key = 0;
|
2020-09-19 16:29:19 +03:00
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
status = uefi_call_wrapper(gBS->GetMemoryMap, 5,
|
2021-04-29 00:41:34 +03:00
|
|
|
&efi_mmap_size, tmp_mmap, &mmap_key, &efi_desc_size, &efi_desc_ver);
|
2020-09-19 16:29:19 +03:00
|
|
|
|
2021-04-29 00:41:34 +03:00
|
|
|
efi_mmap_size += 4096;
|
2020-09-20 13:03:44 +03:00
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
status = uefi_call_wrapper(gBS->AllocatePool, 3,
|
2021-04-29 00:41:34 +03:00
|
|
|
EfiLoaderData, efi_mmap_size, &efi_mmap);
|
2020-09-20 13:03:44 +03:00
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
status = uefi_call_wrapper(gBS->GetMemoryMap, 5,
|
2021-04-29 00:41:34 +03:00
|
|
|
&efi_mmap_size, efi_mmap, &mmap_key, &efi_desc_size, &efi_desc_ver);
|
2020-09-30 23:51:56 +03:00
|
|
|
|
2021-04-29 00:41:34 +03:00
|
|
|
size_t entry_count = efi_mmap_size / efi_desc_size;
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2021-03-05 06:20:58 +03:00
|
|
|
for (size_t i = 0; i < entry_count; i++) {
|
2021-04-29 00:41:34 +03:00
|
|
|
EFI_MEMORY_DESCRIPTOR *entry = (void *)efi_mmap + i * efi_desc_size;
|
2021-03-05 06:20:58 +03:00
|
|
|
|
|
|
|
uint32_t our_type;
|
|
|
|
switch (entry->Type) {
|
|
|
|
case EfiReservedMemoryType:
|
|
|
|
case EfiRuntimeServicesCode:
|
|
|
|
case EfiRuntimeServicesData:
|
|
|
|
case EfiUnusableMemory:
|
|
|
|
case EfiMemoryMappedIO:
|
|
|
|
case EfiMemoryMappedIOPortSpace:
|
|
|
|
case EfiPalCode:
|
|
|
|
default:
|
|
|
|
our_type = MEMMAP_RESERVED; break;
|
|
|
|
case EfiBootServicesCode:
|
|
|
|
case EfiBootServicesData:
|
2021-03-10 03:23:44 +03:00
|
|
|
our_type = MEMMAP_EFI_RECLAIMABLE; break;
|
2021-03-05 06:20:58 +03:00
|
|
|
case EfiLoaderCode:
|
|
|
|
case EfiLoaderData:
|
2021-05-06 05:31:05 +03:00
|
|
|
our_type = MEMMAP_EFI_LOADER; break;
|
2021-03-05 06:20:58 +03:00
|
|
|
case EfiACPIReclaimMemory:
|
|
|
|
our_type = MEMMAP_ACPI_RECLAIMABLE; break;
|
|
|
|
case EfiACPIMemoryNVS:
|
|
|
|
our_type = MEMMAP_ACPI_NVS; break;
|
|
|
|
case EfiConventionalMemory:
|
|
|
|
our_type = MEMMAP_USABLE; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmap[memmap_entries].type = our_type;
|
|
|
|
memmap[memmap_entries].base = entry->PhysicalStart;
|
|
|
|
memmap[memmap_entries].length = entry->NumberOfPages * 4096;
|
|
|
|
|
2021-04-15 04:44:25 +03:00
|
|
|
if (memmap[memmap_entries].base < 0x1000) {
|
|
|
|
if (memmap[memmap_entries].base + memmap[memmap_entries].length <= 0x1000) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmap[memmap_entries].length -= 0x1000 - memmap[memmap_entries].base;
|
|
|
|
memmap[memmap_entries].base = 0x1000;
|
|
|
|
}
|
|
|
|
|
2021-03-10 03:23:44 +03:00
|
|
|
memmap_entries++;
|
2021-03-05 06:20:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sanitise_entries(false);
|
|
|
|
|
|
|
|
allocations_disallowed = false;
|
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
// Let's leave 64MiB to the firmware
|
2021-04-15 03:21:38 +03:00
|
|
|
ext_mem_alloc_type(65536, MEMMAP_EFI_RECLAIMABLE);
|
2021-03-05 08:10:58 +03:00
|
|
|
|
|
|
|
// Now own all the usable entries
|
|
|
|
for (size_t i = 0; i < memmap_entries; i++) {
|
|
|
|
if (memmap[i].type != MEMMAP_USABLE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
EFI_PHYSICAL_ADDRESS base = memmap[i].base;
|
|
|
|
|
|
|
|
status = uefi_call_wrapper(gBS->AllocatePages, 4,
|
|
|
|
AllocateAddress, EfiLoaderData, memmap[i].length / 4096, &base);
|
|
|
|
|
|
|
|
if (status)
|
2021-05-06 05:31:05 +03:00
|
|
|
panic("pmm: AllocatePages failure (%x)", status);
|
2021-03-05 08:10:58 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-10 03:23:44 +03:00
|
|
|
|
|
|
|
void pmm_reclaim_uefi_mem(void) {
|
|
|
|
for (size_t i = 0; i < memmap_entries; i++) {
|
2021-05-06 05:31:05 +03:00
|
|
|
if (memmap[i].type != MEMMAP_EFI_RECLAIMABLE
|
|
|
|
&& memmap[i].type != MEMMAP_EFI_LOADER)
|
2021-03-10 03:23:44 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
memmap[i].type = MEMMAP_USABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sanitise_entries(false);
|
|
|
|
}
|
2021-05-06 05:31:05 +03:00
|
|
|
|
|
|
|
void pmm_release_uefi_mem(void) {
|
|
|
|
EFI_STATUS status;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < memmap_entries; i++) {
|
|
|
|
if (memmap[i].type != MEMMAP_USABLE
|
|
|
|
&& memmap[i].type != MEMMAP_BOOTLOADER_RECLAIMABLE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = uefi_call_wrapper(gBS->FreePages, 2,
|
|
|
|
memmap[i].base, memmap[i].length / 4096);
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
panic("pmm: FreePages failure (%x)", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
allocations_disallowed = true;
|
|
|
|
}
|
2021-03-05 08:10:58 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void *ext_mem_alloc(size_t count) {
|
|
|
|
return ext_mem_alloc_type(count, MEMMAP_BOOTLOADER_RECLAIMABLE);
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:21:38 +03:00
|
|
|
// Allocate memory top down, hopefully without bumping into kernel or modules
|
2021-03-05 08:10:58 +03:00
|
|
|
void *ext_mem_alloc_type(size_t count, uint32_t type) {
|
2021-04-15 03:21:38 +03:00
|
|
|
count = ALIGN_UP(count, 4096);
|
2021-03-05 08:10:58 +03:00
|
|
|
|
|
|
|
if (allocations_disallowed)
|
2021-04-15 03:21:38 +03:00
|
|
|
panic("Memory allocations disallowed");
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
for (int i = memmap_entries - 1; i >= 0; i--) {
|
|
|
|
if (memmap[i].type != 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int64_t entry_base = (int64_t)(memmap[i].base);
|
|
|
|
int64_t entry_top = (int64_t)(memmap[i].base + memmap[i].length);
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
// Let's make sure the entry is not > 4GiB
|
2021-03-17 08:53:24 +03:00
|
|
|
if (entry_top >= 0x100000000) {
|
|
|
|
entry_top = 0x100000000;
|
|
|
|
if (entry_base >= entry_top)
|
|
|
|
continue;
|
2021-03-05 08:10:58 +03:00
|
|
|
}
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2021-04-15 03:21:38 +03:00
|
|
|
int64_t alloc_base = ALIGN_DOWN(entry_top - (int64_t)count, 4096);
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
// This entry is too small for us.
|
|
|
|
if (alloc_base < entry_base)
|
|
|
|
continue;
|
2021-03-05 06:20:58 +03:00
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
// We now reserve the range we need.
|
|
|
|
int64_t aligned_length = entry_top - alloc_base;
|
2021-04-07 03:10:28 +03:00
|
|
|
memmap_alloc_range((uint64_t)alloc_base, (uint64_t)aligned_length, type, true, true, false, false);
|
2021-03-05 08:10:58 +03:00
|
|
|
|
|
|
|
void *ret = (void *)(size_t)alloc_base;
|
|
|
|
|
|
|
|
// Zero out allocated space
|
|
|
|
memset(ret, 0, count);
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
sanitise_entries(false);
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("High memory allocator: Out of memory");
|
2021-03-02 12:23:43 +03:00
|
|
|
}
|
2020-09-02 06:33:22 +03:00
|
|
|
|
2021-04-07 03:10:28 +03:00
|
|
|
bool memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type, bool free_only, bool do_panic, bool simulation, bool new_entry) {
|
2021-03-13 11:08:01 +03:00
|
|
|
if (length == 0)
|
|
|
|
return true;
|
|
|
|
|
2020-06-05 18:51:33 +03:00
|
|
|
uint64_t top = base + length;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < memmap_entries; i++) {
|
2021-03-17 08:53:24 +03:00
|
|
|
if (free_only && memmap[i].type != MEMMAP_USABLE)
|
2020-06-05 18:51:33 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
uint64_t entry_base = memmap[i].base;
|
|
|
|
uint64_t entry_top = memmap[i].base + memmap[i].length;
|
2021-03-17 19:56:36 +03:00
|
|
|
uint32_t entry_type = memmap[i].type;
|
2021-03-17 08:53:24 +03:00
|
|
|
|
2021-04-20 14:52:42 +03:00
|
|
|
if (base >= entry_base && base < entry_top && top <= entry_top) {
|
2021-03-26 17:47:59 +03:00
|
|
|
if (simulation)
|
|
|
|
return true;
|
|
|
|
|
2021-03-17 08:53:24 +03:00
|
|
|
struct e820_entry_t *target;
|
2020-06-05 18:51:33 +03:00
|
|
|
|
2021-03-17 08:53:24 +03:00
|
|
|
memmap[i].length -= entry_top - base;
|
2020-06-05 18:51:33 +03:00
|
|
|
|
2021-04-20 14:52:42 +03:00
|
|
|
if (memmap[i].length == 0) {
|
|
|
|
target = &memmap[i];
|
|
|
|
} else {
|
|
|
|
if (memmap_entries >= MEMMAP_MAX_ENTRIES)
|
|
|
|
panic("Memory map exhausted.");
|
2020-06-05 18:51:33 +03:00
|
|
|
|
2021-04-20 14:52:42 +03:00
|
|
|
target = &memmap[memmap_entries++];
|
2021-03-19 11:36:49 +03:00
|
|
|
}
|
2020-06-05 18:51:33 +03:00
|
|
|
|
2021-04-20 14:52:42 +03:00
|
|
|
target->type = type;
|
|
|
|
target->base = base;
|
|
|
|
target->length = length;
|
|
|
|
|
2021-03-17 08:53:24 +03:00
|
|
|
if (top < entry_top) {
|
2021-04-20 14:52:42 +03:00
|
|
|
if (memmap_entries >= MEMMAP_MAX_ENTRIES)
|
|
|
|
panic("Memory map exhausted.");
|
2020-06-05 18:51:33 +03:00
|
|
|
|
2021-04-20 14:52:42 +03:00
|
|
|
target = &memmap[memmap_entries++];
|
2020-06-05 18:51:33 +03:00
|
|
|
|
2021-03-17 19:56:36 +03:00
|
|
|
target->type = entry_type;
|
2021-03-17 08:53:24 +03:00
|
|
|
target->base = top;
|
|
|
|
target->length = entry_top - top;
|
2020-06-05 18:51:33 +03:00
|
|
|
}
|
|
|
|
|
2021-04-20 14:52:42 +03:00
|
|
|
sanitise_entries(false);
|
|
|
|
|
2021-01-27 18:16:36 +03:00
|
|
|
return true;
|
2020-06-05 18:51:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 03:10:28 +03:00
|
|
|
if (!new_entry && do_panic)
|
2021-04-20 14:52:42 +03:00
|
|
|
panic("Memory allocation failure.");
|
2021-01-27 18:16:36 +03:00
|
|
|
|
2021-04-07 03:10:28 +03:00
|
|
|
if (new_entry) {
|
|
|
|
if (memmap_entries >= MEMMAP_MAX_ENTRIES)
|
|
|
|
panic("Memory map exhausted.");
|
|
|
|
|
|
|
|
struct e820_entry_t *target = &memmap[memmap_entries++];
|
|
|
|
|
|
|
|
target->type = type;
|
|
|
|
target->base = base;
|
|
|
|
target->length = length;
|
2021-04-20 14:52:42 +03:00
|
|
|
|
|
|
|
sanitise_entries(false);
|
|
|
|
|
|
|
|
return true;
|
2021-04-07 03:10:28 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:16:36 +03:00
|
|
|
return false;
|
2020-06-05 21:57:09 +03:00
|
|
|
}
|