fix: free: use the new pt system

This commit is contained in:
Starnakin 2024-10-30 21:26:00 +01:00
parent ae3cfa7647
commit 6533334824
3 changed files with 30 additions and 22 deletions

View File

@ -36,10 +36,11 @@ void kernel_main(void)
"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");
while (true) {
for (uint16_t i = 0;; i++) {
void *ptr = alloc_pages(4096);
memset(ptr, ~0, 4096);
PRINT_PTR(ptr);
memset(ptr, ~0, 4096);
free_pages(ptr, 4096);
}
/* int i = 0; */
/* while (vmalloc(10)) */

View File

@ -9,15 +9,19 @@
#define MAX_FRAMES (HEAP_END / PAGE_SIZE)
enum {
FREE,
USED,
};
static uint8_t frame_table[MAX_FRAMES];
static uint32_t remaining_frames = MAX_FRAMES;
static int32_t find_next_block(size_t nb_frames)
{
for (uint32_t i = CEIL(HEAP_START, PAGE_SIZE);
i + nb_frames < MAX_FRAMES; i++) {
uint32_t j;
for (j = 0; frame_table[i + j] == 0 && j < nb_frames; j++)
for (j = 0; frame_table[i + j] == FREE && j < nb_frames; j++)
;
if (j == nb_frames)
return i;
@ -29,28 +33,22 @@ static int32_t find_next_block(size_t nb_frames)
void *alloc_frames(size_t size)
{
const uint32_t nb_frames = CEIL(size, PAGE_SIZE);
if (nb_frames > remaining_frames) {
kprintf(KERN_CRIT "Not enough frames (max: %d)\n", MAX_FRAMES);
return NULL;
}
int i = find_next_block(nb_frames);
const int i = find_next_block(nb_frames);
if (i < 0) {
kprintf(KERN_WARNING "Not enough frames available\n");
return NULL;
}
for (size_t j = 0; j < nb_frames; j++) {
assert(j + i < MAX_FRAMES);
frame_table[j + i] = 1;
frame_table[j + i] = USED;
}
assert(remaining_frames >= nb_frames);
remaining_frames -= nb_frames;
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 + HEAP_END) / PAGE_SIZE;
const uint32_t start = (uint32_t)frame_ptr / PAGE_SIZE;
if (start > MAX_FRAMES) {
kprintf(KERN_WARNING "Address out of range\n");
@ -63,7 +61,6 @@ int free_frames(void *frame_ptr, size_t size)
return -1;
}
for (size_t i = start; i < start + nb_frames; i++)
frame_table[i] = 0;
remaining_frames += nb_frames;
frame_table[i] = FREE;
return 0;
}

View File

@ -60,22 +60,32 @@ void *alloc_pages(size_t size)
int free_pages(void *page_ptr, size_t size)
{
const uint32_t page_addr = (uint32_t)page_ptr;
const uint32_t nb_pages = CEIL(size, PAGE_SIZE);
const uint32_t page_index = (uint32_t)page_ptr / PAGE_SIZE;
const uint32_t page_index = page_addr / PAGE_SIZE;
const uint32_t pd_index = page_index / PD_SIZE;
const uint32_t pt_index = page_index % PD_SIZE;
if ((uint32_t)page_ptr > PT_SIZE * PAGE_SIZE) {
if ((uint32_t)pd_index > 0x300) {
kprintf(KERN_WARNING "Address out of range\n");
return -1;
} else if ((uint32_t)page_ptr % PAGE_SIZE) {
} else if (page_addr % PAGE_SIZE) {
kprintf(KERN_WARNING "Invalid address\n");
return -1;
} else if (page_index + nb_pages > PT_SIZE) {
} else if (pt_index + nb_pages > PT_SIZE) {
kprintf(KERN_WARNING "Invalid number of frames\n");
return -1;
}
for (size_t i = page_index; i < page_index + nb_pages; i++) {
free_frames((void *)(i >> 12), PAGE_SIZE);
i = i << 12 | INIT_FLAGS;
uint32_t *page_table =
(uint32_t *)GET_PAGE_ADDR(0, PT_START + pd_index);
for (uint16_t i = pt_index; i < pt_index + nb_pages; i++) {
if (page_table[i] >> 12 == i) {
kprintf(KERN_WARNING "Page already free\n");
return -2;
}
free_frames((void *)(page_table[i] & PAGE_MASK), PAGE_SIZE);
page_table[i] = i << 12;
}
return 0;
}