Compare commits

...

2 Commits

5 changed files with 17 additions and 13 deletions

View File

@ -16,6 +16,9 @@
#define HEAP_START ((uint32_t) & _kernel_end - HEAP_END) #define HEAP_START ((uint32_t) & _kernel_end - HEAP_END)
#define PT_START 256 #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 _kernel_end;
extern uint32_t boot_page_directory; extern uint32_t boot_page_directory;
extern uint32_t *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); void *alloc_pages(size_t size);
int free_pages(void *page_ptr, size_t size); int free_pages(void *page_ptr, size_t size);
void init_page_table(uint32_t page_table[1024], uint16_t start); 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 " "I see no way to confuse an array of 256 seg:off pairs with a "
"complex 8*unknown quantity -byte descriptor table. -- Troy " "complex 8*unknown quantity -byte descriptor table. -- Troy "
"Martin 03:50, 22 March 2009 (UTC)\n"); "Martin 03:50, 22 March 2009 (UTC)\n");
char *str = vmalloc(10);
if (str)
strcpy(str, "hello");
/* int i = 0; */ /* int i = 0; */
/* while (vmalloc(10)) */ /* while (vmalloc(10)) */
/* ; */ /* ; */

View File

@ -14,10 +14,8 @@ static uint32_t remaining_frames = MAX_FRAMES;
static int32_t find_next_block(size_t nb_frames) static int32_t find_next_block(size_t nb_frames)
{ {
PRINT_UINT(CEIL(HEAP_START, PAGE_SIZE));
for (uint32_t i = CEIL(HEAP_START, PAGE_SIZE); for (uint32_t i = CEIL(HEAP_START, PAGE_SIZE);
i + nb_frames < MAX_FRAMES; i++) { i + nb_frames < MAX_FRAMES; i++) {
PRINT_UINT(i);
uint32_t j; uint32_t j;
for (j = 0; frame_table[i + j] == 0 && j < nb_frames; j++) for (j = 0; frame_table[i + j] == 0 && j < nb_frames; j++)
; ;

View File

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

View File

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