core: use ptr param instead of global

This commit is contained in:
Starnakin 2024-10-30 20:48:59 +01:00
parent 04211e2773
commit ae3cfa7647

View File

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