fix: start frame allocator after kernel code

This commit is contained in:
Starnakin 2024-11-20 01:51:07 +01:00
parent b7dd7761d5
commit 3bc05604db
4 changed files with 57 additions and 32 deletions

View File

@ -30,22 +30,17 @@
frame_table[i / 8] &= ~(1 << (i % 8)); \ frame_table[i / 8] &= ~(1 << (i % 8)); \
} while (0) } while (0)
/*
* len is the total size of the zone (ratio starnakin)
* size is the remaining usable size after allocating
* the struct for the linked list
*/
struct frame_zone { struct frame_zone {
void *addr; void *addr;
uint32_t first_free_frame; uint32_t first_free_frame;
uint32_t *frame_table; uint8_t *frame_table;
uint32_t len; uint32_t total_frames;
uint32_t size;
uint32_t remaining_frames; uint32_t remaining_frames;
struct frame_zone *next; struct frame_zone *next;
}; };
extern uint32_t _kernel_end; extern uint32_t _kernel_end;
extern uint32_t _kernel_start;
extern uint32_t boot_page_directory; extern uint32_t boot_page_directory;
extern uint32_t *page_directory; extern uint32_t *page_directory;
extern uint32_t page_table_default[1024]; extern uint32_t page_table_default[1024];

View File

@ -66,7 +66,13 @@ 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 " "I see no way to confuse an array of 256 seg:off pairs with a "
"complex 8*unknown quantity -byte descriptor table. -- Troy " "complex 8*unknown quantity -byte descriptor table. -- Troy "
"Martin 03:50, 22 March 2009 (UTC)\n"); "Martin 03:50, 22 March 2009 (UTC)\n");
/* alloc_frame(); */
// PRINT_PTR(alloc_frame());
/*void *ptr;
while ((ptr = alloc_pages(PAGE_SIZE * 1020))) {
if (ptr)
memset(ptr, ~0, PAGE_SIZE * 1020);
}*/
/* vmalloc(10); */ /* vmalloc(10); */
while (vmalloc(10)) while (vmalloc(10))
; ;

View File

@ -30,7 +30,8 @@ void *alloc_frame(void)
int free_frame(void *frame_ptr) int free_frame(void *frame_ptr)
{ {
struct frame_zone *it = head; struct frame_zone *it = head;
while (it && (frame_ptr < it->addr || frame_ptr >= it->addr + it->len)) while (it && (frame_ptr < it->addr ||
frame_ptr >= it->addr + it->total_frames * PAGE_SIZE))
it = it->next; it = it->next;
uint32_t index = ((frame_ptr - it->addr) / PAGE_SIZE); uint32_t index = ((frame_ptr - it->addr) / PAGE_SIZE);

View File

@ -45,11 +45,37 @@ static void init_multiboot(multiboot_info_t *mbd, uint32_t magic)
mmap_length = mbd_virt->mmap_length / sizeof(multiboot_memory_map_t); mmap_length = mbd_virt->mmap_length / sizeof(multiboot_memory_map_t);
} }
static void lst_add_back(struct frame_zone **root, struct frame_zone *element)
{
if (!*root) {
*root = element;
return;
}
struct frame_zone *it = *root;
while (it->next)
it = it->next;
it->next = element;
}
static void add_frame_node(multiboot_memory_map_t *mmmt) static void add_frame_node(multiboot_memory_map_t *mmmt)
{ {
static uint32_t index; static uint32_t index;
void *zone = (void *)mmmt->addr; void *zone = (void *)mmmt->addr;
// Kernel code on the block
if (HEAP_START >= zone + mmmt->len)
return;
// KERNEL code partially on the block
if (HEAP_START >= zone) {
const uint32_t len = mmmt->len -
((uint64_t)&_kernel_end - HEAP_END) -
(uint64_t)zone;
mmmt->len = CEIL(len, PAGE_SIZE) * PAGE_SIZE;
zone =
(void *)((uint32_t)CEIL(HEAP_START, PAGE_SIZE) * PAGE_SIZE);
}
init_page_table(frame_zones_page_table, 0); init_page_table(frame_zones_page_table, 0);
page_directory[1022] = page_directory[1022] =
((uint32_t)frame_zones_page_table - HEAP_END) | 0x03; ((uint32_t)frame_zones_page_table - HEAP_END) | 0x03;
@ -59,32 +85,29 @@ static void add_frame_node(multiboot_memory_map_t *mmmt)
struct frame_zone *current = struct frame_zone *current =
(struct frame_zone *)GET_PAGE_ADDR(1022, index++); (struct frame_zone *)GET_PAGE_ADDR(1022, index++);
current->frame_table = (uint32_t *)current + sizeof(struct frame_zone); current->frame_table = (uint8_t *)current + sizeof(struct frame_zone);
current->len = mmmt->len;
// 32 is cause we are using uint32_t /** 8 is cause we are using uint8_t
current->size = (mmmt->len - (sizeof(struct frame_zone) + nb_frame = size / (PAGE_SIZE + 1 / 8)
(mmmt->len / PAGE_SIZE) / 32)); cause we are using non decimal number
current->remaining_frames = current->size / PAGE_SIZE; nb_frame = ((size * 8) / (PAGE_SIZE * 8 + 1)) / 8
*/
const uint32_t nb_frame = ((mmmt->len * 8) / (PAGE_SIZE * 8 + 1)) - 1;
current->first_free_frame = 0; current->first_free_frame = 0;
current->next = NULL; current->next = NULL;
current->remaining_frames = nb_frame;
current->total_frames = nb_frame;
const size_t frame_table_size = current->size / (PAGE_SIZE * 32); memset(current->frame_table, 0, nb_frame * 4);
memset(current->frame_table, 0, frame_table_size * 4);
uint32_t i = index; uint32_t i = 1;
for (; index - i < CEIL(frame_table_size, PAGE_SIZE); index++) for (; i < CEIL(nb_frame, PAGE_SIZE); i++)
frame_zones_page_table[index] = frame_zones_page_table[index + i] =
((uint32_t)zone + PAGE_SIZE & PAGE_MASK) | INIT_FLAGS; ((uint32_t)zone + i * PAGE_SIZE & PAGE_MASK) | INIT_FLAGS;
current->addr = (void *)mmmt->addr + index * PAGE_SIZE; current->addr = zone + i * PAGE_SIZE;
index += i - 1;
if (!head) { lst_add_back(&head, current);
head = current;
return;
}
struct frame_zone *it = head;
while (it->next)
it = it->next;
it->next = current;
} }
static void init_frame_zones(void) static void init_frame_zones(void)