fix: memory: use rigth index and addr, add page to pt before write it

This commit is contained in:
Starnakin 2024-10-29 15:17:59 +01:00
parent 659ba24f12
commit ff058d0ae1
4 changed files with 17 additions and 11 deletions

View File

@ -16,6 +16,9 @@
#define HEAP_START ((uint32_t) & _kernel_end - HEAP_END)
#define PT_START 256
#define GET_PAGE_ADDR(pd_index, pt_index) \
(((pd_index * 1024) + pt_index) * 4096)
extern uint32_t _kernel_end;
extern uint32_t boot_page_directory;
extern uint32_t *page_directory;
@ -28,4 +31,4 @@ int free_frames(void *frame_ptr, size_t size);
void *alloc_pages(size_t size);
int free_pages(void *page_ptr, size_t size);
void init_page_table(uint32_t page_table[1024], uint16_t start);
int16_t create_page_table(uint16_t pd_index);
int16_t add_page_table(uint16_t pd_index);

View File

@ -36,9 +36,6 @@ 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");
char *str = vmalloc(10);
if (str)
strcpy(str, "hello");
/* int i = 0; */
/* while (vmalloc(10)) */
/* ; */

View File

@ -2,6 +2,7 @@
#include <stddef.h>
#include <stdint.h>
#include "debug.h"
#include "kprintf.h"
#include "memory.h"
#include "utils.h"
@ -15,11 +16,12 @@ static int16_t find_next_block(size_t nb_pages)
{
for (uint16_t pd_index = 1; pd_index < 768; pd_index++) {
if (page_directory[pd_index] == 0x02) {
if (create_page_table(pd_index) < 0)
if (add_page_table(pd_index) < 0)
return -2;
}
current_pd_index = pd_index;
current_page_table = (uint32_t *)((PT_START + pd_index) * 1024);
current_page_table =
(uint32_t *)GET_PAGE_ADDR(0, pd_index + 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 &&
@ -56,7 +58,7 @@ void *alloc_pages(size_t size)
current_page_table[i] =
((uint32_t)frame & PAGE_MASK) | INIT_FLAGS;
}
return (void *)(((current_pd_index * 1024) + index) * 1024);
return (void *)GET_PAGE_ADDR(current_pd_index, index);
}
int free_pages(void *page_ptr, size_t size)

View File

@ -1,18 +1,22 @@
#include "debug.h"
#include "kprintf.h"
#include "memory.h"
void init_page_table(uint32_t page_table[1024], uint16_t start)
{
for (uint16_t i = start; i < 1024; i++)
page_table[i] = (i << 12) | 0x03;
}
int16_t create_page_table(uint16_t pd_index)
int16_t add_page_table(uint16_t pd_index)
{
void *frame = alloc_frames(PAGE_SIZE);
if (!frame)
return -1;
page_directory[pd_index] = ((uint32_t)frame & PAGE_MASK) | 0x03;
uint32_t *page_table = (uint32_t *)((PT_START + pd_index) * 1024);
page_table_default[PT_START + pd_index] =
((uint32_t)frame & PAGE_MASK) | 0x03;
uint32_t *page_table =
(uint32_t *)GET_PAGE_ADDR(0, PT_START + pd_index);
init_page_table(page_table, 0);
page_directory[pd_index] = ((uint32_t)frame & PAGE_MASK) | 0x03;
return 0;
}