diff --git a/headers/memory.h b/headers/memory.h index 635dcf7..16c334f 100644 --- a/headers/memory.h +++ b/headers/memory.h @@ -30,22 +30,17 @@ frame_table[i / 8] &= ~(1 << (i % 8)); \ } 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 { void *addr; uint32_t first_free_frame; - uint32_t *frame_table; - uint32_t len; - uint32_t size; + uint8_t *frame_table; + uint32_t total_frames; uint32_t remaining_frames; struct frame_zone *next; }; extern uint32_t _kernel_end; +extern uint32_t _kernel_start; extern uint32_t boot_page_directory; extern uint32_t *page_directory; extern uint32_t page_table_default[1024]; diff --git a/src/kernel.c b/src/kernel.c index c637694..5f34f6a 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -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 " "complex 8*unknown quantity -byte descriptor table. -- Troy " "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); */ while (vmalloc(10)) ; diff --git a/src/memory/frame.c b/src/memory/frame.c index c571c2a..2ac70f8 100644 --- a/src/memory/frame.c +++ b/src/memory/frame.c @@ -30,7 +30,8 @@ void *alloc_frame(void) int free_frame(void *frame_ptr) { 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; uint32_t index = ((frame_ptr - it->addr) / PAGE_SIZE); diff --git a/src/memory/memory.c b/src/memory/memory.c index b395641..3e307bd 100644 --- a/src/memory/memory.c +++ b/src/memory/memory.c @@ -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); } +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 uint32_t index; 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); page_directory[1022] = ((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 *)GET_PAGE_ADDR(1022, index++); - current->frame_table = (uint32_t *)current + sizeof(struct frame_zone); - current->len = mmmt->len; - // 32 is cause we are using uint32_t - current->size = (mmmt->len - (sizeof(struct frame_zone) + - (mmmt->len / PAGE_SIZE) / 32)); - current->remaining_frames = current->size / PAGE_SIZE; + current->frame_table = (uint8_t *)current + sizeof(struct frame_zone); + + /** 8 is cause we are using uint8_t + nb_frame = size / (PAGE_SIZE + 1 / 8) + cause we are using non decimal number + 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->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, frame_table_size * 4); + memset(current->frame_table, 0, nb_frame * 4); - uint32_t i = index; - for (; index - i < CEIL(frame_table_size, PAGE_SIZE); index++) - frame_zones_page_table[index] = - ((uint32_t)zone + PAGE_SIZE & PAGE_MASK) | INIT_FLAGS; - current->addr = (void *)mmmt->addr + index * PAGE_SIZE; - - if (!head) { - head = current; - return; - } - struct frame_zone *it = head; - while (it->next) - it = it->next; - it->next = current; + uint32_t i = 1; + for (; i < CEIL(nb_frame, PAGE_SIZE); i++) + frame_zones_page_table[index + i] = + ((uint32_t)zone + i * PAGE_SIZE & PAGE_MASK) | INIT_FLAGS; + current->addr = zone + i * PAGE_SIZE; + index += i - 1; + lst_add_back(&head, current); } static void init_frame_zones(void)