Skip to content

Commit

Permalink
pmm: Only align memory map when explicitly requested as not to fragme…
Browse files Browse the repository at this point in the history
…nt ext_mem allocations too much
  • Loading branch information
mintsuki committed Nov 14, 2020
1 parent 7a03b32 commit ee1aee3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
Binary file modified limine-pxe.bin
Binary file not shown.
Binary file modified limine.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion stage2/fs/ext2.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ static int ext2_get_inode(struct ext2_inode *ret, struct part *part,
}

static uint32_t *create_alloc_map(struct ext2_file_handle *fd,
struct ext2_inode *inode) {
struct ext2_inode *inode) {
if (inode->i_flags & EXT4_EXTENTS_FLAG)
return NULL;

Expand Down
22 changes: 13 additions & 9 deletions stage2/mm/pmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void print_memmap(struct e820_entry_t *mm, size_t size) {
}
}

static int align_entry(uint64_t *base, uint64_t *length) {
static bool align_entry(uint64_t *base, uint64_t *length) {
if (*length < PAGE_SIZE)
return -1;

Expand All @@ -57,7 +57,7 @@ static int align_entry(uint64_t *base, uint64_t *length) {
*length = ALIGN_DOWN(*length, PAGE_SIZE);

if (!length)
return -1;
return false;

uint64_t top = *base + *length;

Expand All @@ -66,14 +66,14 @@ static int align_entry(uint64_t *base, uint64_t *length) {
*length -= MEMMAP_BASE - *base;
*base = MEMMAP_BASE;
} else {
return -1;
return false;
}
}

return 0;
return true;
}

static void sanitise_entries(void) {
static void sanitise_entries(bool align_entries) {
for (size_t i = 0; i < memmap_entries; i++) {
if (memmap[i].type != MEMMAP_USABLE)
continue;
Expand Down Expand Up @@ -109,7 +109,11 @@ static void sanitise_entries(void) {
memmap[i].length = top - base;
}

if (!memmap[i].length || align_entry(&memmap[i].base, &memmap[i].length)) {
bool success = true;
if (align_entries)
success = align_entry(&memmap[i].base, &memmap[i].length);

if (!memmap[i].length || !success) {
// Eradicate from memmap
for (size_t j = i; j < memmap_entries - 1; j++) {
memmap[j] = memmap[j+1];
Expand Down Expand Up @@ -154,7 +158,7 @@ static void sanitise_entries(void) {
}

struct e820_entry_t *get_memmap(size_t *entries) {
sanitise_entries();
sanitise_entries(true);

*entries = memmap_entries;

Expand All @@ -170,7 +174,7 @@ void init_memmap(void) {
memmap[memmap_entries++] = e820_map[i];
}

sanitise_entries();
sanitise_entries(false);
}

void *ext_mem_alloc(size_t count) {
Expand Down Expand Up @@ -217,7 +221,7 @@ void *ext_mem_alloc_aligned_type(size_t count, size_t alignment, uint32_t type)
// Zero out allocated space
memset(ret, 0, count);

sanitise_entries();
sanitise_entries(false);

return ret;
}
Expand Down
40 changes: 20 additions & 20 deletions stage2/protos/stivale2.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,26 +293,6 @@ void stivale2_load(char *cmdline) {
if (bits == 64)
pagemap = stivale_build_pagemap(level5pg && level5pg_requested);

//////////////////////////////////////////////
// Create memmap struct tag
//////////////////////////////////////////////
{
size_t memmap_entries;
struct e820_entry_t *memmap = get_memmap(&memmap_entries);

struct stivale2_struct_tag_memmap *tag =
conv_mem_alloc(sizeof(struct stivale2_struct_tag_memmap) +
sizeof(struct e820_entry_t) * memmap_entries);

tag->tag.identifier = STIVALE2_STRUCT_TAG_MEMMAP_ID;
tag->entries = (uint64_t)memmap_entries;

memcpy((void*)tag + sizeof(struct stivale2_struct_tag_memmap),
memmap, sizeof(struct e820_entry_t) * memmap_entries);

append_tag(&stivale2_struct, (struct stivale2_tag *)tag);
}

//////////////////////////////////////////////
// Create SMP struct tag
//////////////////////////////////////////////
Expand Down Expand Up @@ -349,6 +329,26 @@ void stivale2_load(char *cmdline) {
break;
}

//////////////////////////////////////////////
// Create memmap struct tag
//////////////////////////////////////////////
{
size_t memmap_entries;
struct e820_entry_t *memmap = get_memmap(&memmap_entries);

struct stivale2_struct_tag_memmap *tag =
conv_mem_alloc(sizeof(struct stivale2_struct_tag_memmap) +
sizeof(struct e820_entry_t) * memmap_entries);

tag->tag.identifier = STIVALE2_STRUCT_TAG_MEMMAP_ID;
tag->entries = (uint64_t)memmap_entries;

memcpy((void*)tag + sizeof(struct stivale2_struct_tag_memmap),
memmap, sizeof(struct e820_entry_t) * memmap_entries);

append_tag(&stivale2_struct, (struct stivale2_tag *)tag);
}

stivale_spinup(bits, level5pg && level5pg_requested, pagemap,
entry_point, &stivale2_struct, stivale2_hdr.stack);
}

0 comments on commit ee1aee3

Please sign in to comment.