wip: alloc_femmes now uses multiboot memory mapping

This commit is contained in:
0x35c 2024-11-13 13:32:14 +01:00
parent 1e35f3b710
commit 09ea386b21
4 changed files with 22 additions and 57 deletions

View File

@ -26,6 +26,8 @@ extern uint32_t boot_page_directory;
extern uint32_t *page_directory;
extern uint32_t page_table_default[1024];
extern uint32_t mem_size;
extern multiboot_uint32_t *mmap_addr;
extern multiboot_uint32_t mmap_length;
uint32_t *virt_to_phys(uint32_t *virt_addr);
void init_memory(multiboot_info_t *mbd, uint32_t magic);

View File

@ -50,8 +50,8 @@ static void set_mem_size(multiboot_info_t *mbd, uint32_t magic)
i += sizeof(multiboot_memory_map_t)) {
multiboot_memory_map_t *mmmt =
(multiboot_memory_map_t *)(mbd->mmap_addr + i);
if (mmmt->type == MULTIBOOT_MEMORY_AVAILABLE)
mem_size += mmmt->len;
/* if (mmmt->type == MULTIBOOT_MEMORY_AVAILABLE) */
/* mem_size += mmmt->len; */
}
}
@ -66,6 +66,7 @@ void kernel_main(multiboot_info_t *mbd, uint32_t magic)
"I see no way to confuse an array of 256 seg:off pairs with a "
"complex 8*unknown quantity -byte descriptor table. -- Troy "
"Martin 03:50, 22 March 2009 (UTC)\n");
alloc_frames(1);
/* vmalloc(10); */
/* while (vmalloc(10)) */
/* ; */

View File

@ -7,61 +7,29 @@
#include "memory.h"
#include "utils.h"
#define MAX_FRAMES (HEAP_END / PAGE_SIZE)
#define NB_FRAMES (mem_size / PAGE_SIZE)
enum {
FREE,
USED,
};
static uint8_t frame_table[MAX_FRAMES];
static int32_t find_next_block(size_t nb_frames)
{
for (uint32_t i = CEIL(HEAP_START, PAGE_SIZE);
i + nb_frames < NB_FRAMES; i++) {
uint32_t j;
for (j = 0; frame_table[i + j] == FREE && j < nb_frames; j++)
;
if (j == nb_frames)
return i;
i += j;
}
return -1;
}
void *alloc_frames(size_t size)
{
const uint32_t nb_frames = CEIL(size, PAGE_SIZE);
const int i = find_next_block(nb_frames);
if (i < 0) {
kprintf(KERN_WARNING "Not enough frames available\n");
/* PRINT_PTR(((multiboot_memory_map_t *)mmap_addr)->addr); */
/* PRINT_UINT(((multiboot_memory_map_t *)mmap_addr)->len); */
/* PRINT_UINT(((multiboot_memory_map_t *)mmap_addr)->type); */
for (uint32_t i = 0; i < mmap_length;
i += sizeof(multiboot_memory_map_t)) {
multiboot_memory_map_t *mmmt =
(multiboot_memory_map_t *)(mmap_addr + i);
if (mmmt->type == MULTIBOOT_MEMORY_AVAILABLE) {
}
PRINT_UINT(mmmt->len);
/* kprintf("[%d] available %p: %u bytes\n", i, mmmt->addr, */
/* mmmt->len); */
/* PRINT_UINT(mmmt->type); */
}
/* for (size_t i = 0; i < mmap_length; i++) { */
/* } */
return NULL;
}
for (size_t j = 0; j < nb_frames; j++) {
assert(j + i < NB_FRAMES);
frame_table[j + i] = USED;
}
return (void *)(i * PAGE_SIZE);
}
int free_frames(void *frame_ptr, size_t size)
{
const uint32_t nb_frames = CEIL(size, PAGE_SIZE);
const uint32_t start = (uint32_t)frame_ptr / PAGE_SIZE;
if (start > NB_FRAMES) {
kprintf(KERN_WARNING "Address out of range\n");
return -1;
} else if ((uint32_t)frame_ptr % PAGE_SIZE) {
kprintf(KERN_WARNING "Invalid address\n");
return -1;
} else if (start + nb_frames > NB_FRAMES) {
kprintf(KERN_WARNING "Invalid number of frames\n");
return -1;
}
for (size_t i = start; i < start + nb_frames; i++)
frame_table[i] = FREE;
return 0;
}

View File

@ -23,28 +23,22 @@ static void init_multiboot(multiboot_info_t *mbd, uint32_t magic)
const size_t mbd_size = CEIL(
(uint32_t)mbd % PAGE_SIZE + sizeof(multiboot_info_t), PAGE_SIZE);
kprintf("cramptes0\n");
// Index multiboot_info_t struct
for (uint32_t i = 0; i < mbd_size; i++)
multiboot_page_table[i] =
((uint32_t)mbd + PAGE_SIZE * i) & PAGE_MASK | INIT_FLAGS;
(((uint32_t)mbd + PAGE_SIZE * i) & PAGE_MASK) | INIT_FLAGS;
multiboot_info_t *mbd_virt =
(multiboot_info_t *)(GET_PAGE_ADDR(1023, 0) +
(uint32_t)mbd % PAGE_SIZE);
kprintf("cramptes1\n");
PRINT_PTR(mbd_virt);
/* PRINT_UINT(mbd_virt->mmap_length); */
// Index mbd->mmap_addr pointers
for (uint32_t i = 0; i < mbd_virt->mmap_length; i++)
multiboot_page_table[i + mbd_size] =
((mbd_virt->mmap_addr + i * PAGE_SIZE) & PAGE_MASK) |
INIT_FLAGS;
kprintf("cramptes2\n");
mmap_addr =
(multiboot_uint32_t *)(GET_PAGE_ADDR(1023, mbd_size) +
(uint32_t)mbd_virt->mmap_addr % PAGE_SIZE);
kprintf("cramptes3\n");
mmap_length = mbd_virt->mmap_length;
}