diff --git a/src/memory/page.c b/src/memory/page.c index 3172bb6..a77d419 100644 --- a/src/memory/page.c +++ b/src/memory/page.c @@ -9,22 +9,19 @@ #define MAX_TLB_ENTRIES 32 -static uint32_t *current_page_table; -static uint16_t current_pd_index; - -static int16_t find_next_block(size_t nb_pages) +static int16_t find_next_block(size_t nb_pages, uint16_t *pd_index_ptr, + uint32_t **page_table_ptr) { - for (uint16_t pd_index = 1; pd_index < 768; pd_index++) { - if (page_directory[pd_index] == 0x02) { - if (add_page_table(pd_index) < 0) + for (*pd_index_ptr = 1; *pd_index_ptr < 768; (*pd_index_ptr)++) { + if (page_directory[(*pd_index_ptr)] == 0x02) { + if (add_page_table(*pd_index_ptr) < 0) return -2; } - current_pd_index = pd_index; - current_page_table = - (uint32_t *)GET_PAGE_ADDR(0, pd_index + PT_START); + *page_table_ptr = + (uint32_t *)GET_PAGE_ADDR(0, *pd_index_ptr + PT_START); for (uint16_t i = 0; i + nb_pages < PT_SIZE; i++) { uint16_t j; - for (j = 0; current_page_table[i + j] >> 12 == i + j && + for (j = 0; (*page_table_ptr)[i + j] >> 12 == i + j && j < nb_pages; j++) ; @@ -39,7 +36,9 @@ static int16_t find_next_block(size_t nb_pages) void *alloc_pages(size_t size) { const uint32_t nb_pages = CEIL(size, PAGE_SIZE); - const int16_t index = find_next_block(nb_pages); + uint16_t pd_index; + uint32_t *page_table; + const int16_t index = find_next_block(nb_pages, &pd_index, &page_table); if (index < 0) { kprintf(KERN_CRIT "%d: Not enough pages (max: %d)\n", index, @@ -50,15 +49,13 @@ void *alloc_pages(size_t size) void *frame = alloc_frames(PAGE_SIZE); if (!frame) { for (size_t j = index; j < i; j++) - free_frames( - (void *)(current_page_table[j] >> 12), - PAGE_SIZE); + free_frames((void *)(page_table[j] >> 12), + PAGE_SIZE); return NULL; } - current_page_table[i] = - ((uint32_t)frame & PAGE_MASK) | INIT_FLAGS; + page_table[i] = ((uint32_t)frame & PAGE_MASK) | INIT_FLAGS; } - return (void *)GET_PAGE_ADDR(current_pd_index, index); + return (void *)GET_PAGE_ADDR(pd_index, index); } int free_pages(void *page_ptr, size_t size) @@ -77,8 +74,8 @@ int free_pages(void *page_ptr, size_t size) return -1; } for (size_t i = page_index; i < page_index + nb_pages; i++) { - free_frames((void *)(current_page_table[i] >> 12), PAGE_SIZE); - current_page_table[i] = i << 12 | INIT_FLAGS; + free_frames((void *)(i >> 12), PAGE_SIZE); + i = i << 12 | INIT_FLAGS; } return 0; }