Commit 3e9db9e5 by triazo

Redo the concept of page allocation

parent 9006ccf6
Showing with 46 additions and 8 deletions
...@@ -24,9 +24,14 @@ ...@@ -24,9 +24,14 @@
// The parameters passed here ultimately come through the bootloader // The parameters passed here ultimately come through the bootloader
void kernel_main(multiboot_info* mbt, uint32_t magic) void kernel_main(multiboot_info* mbt, uint32_t magic)
{ {
/* Initialize terminal and serial interfaces */ /* Initialize terminal and serial interfaces */
terminal_initialize(); terminal_initialize();
serial_init(); serial_init();
// For some reason 'myos.bin ' is here. remove it.
memset(&_kernel_end, 0, 20);
gdt_init(); gdt_init();
idt_init(0x8); idt_init(0x8);
serial_writestring("IDT initialized\n"); serial_writestring("IDT initialized\n");
......
...@@ -46,21 +46,45 @@ void populate_page_table(page_table_entry* table, void* start, int num, int offs ...@@ -46,21 +46,45 @@ void populate_page_table(page_table_entry* table, void* start, int num, int offs
} }
} }
// page_allocate returns the address of a new page in real memory
void* page_allocate()
{
// The starting address of the map should always be the same: &_kernel_end
// Find the correct bit to flip and page number
uint8_t* index = (uint8_t*) &_kernel_end;
while (*index == 0xff) index++;
uint8_t i = 7;
while ((*index & (1<<i)) != 0) i--;
// bitfilp
*index = *index | (1<<i);
uint32_t page_number = ((uint32_t)(index - (uint8_t*)_kernel_end) * 8) + (8-i);
return (void*)&_kernel_end + 0x1000 * page_number;
}
// This goes through all available memory, and maps 1 to 1 virtual memory to it.
page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot_memory_map* mmap_end) page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot_memory_map* mmap_end)
{ {
// Allocate enough space for the biggest possible allocation bitmap. Assigning 1 bit to each
for (int i = 0; i < 32; i++)
{
page_allocate();
}
// Make a page directory structure easily // Make a page directory structure easily
page_directory_entry* kern_page_dir = initiate_directory(); page_directory_entry* kern_page_dir = initiate_directory();
// _kern_end is defined in the linker script // _kern_end is defined in the linker script
page_table_entry* next_table = (page_table_entry*) &_kernel_end; /* page_table_entry* next_table = (page_table_entry*) &_kernel_end; */
int current_dir_entry = 0; int current_dir_entry = 0;
// For each entry in the memory map // For each entry in the memory map
while (mmap < mmap_end) while (mmap < mmap_end)
{ {
void* next_table = page_allocate();
// Special case for the first memory segment - it gets its own directory entry // Special case for the first memory segment - it gets its own directory entry
if (mmap->base_addr == 0) if (mmap->base_addr == 0)
{ {
...@@ -71,7 +95,7 @@ page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot ...@@ -71,7 +95,7 @@ page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot
page_directory[0] |= PAGE_WRITABLE | PAGE_PRESENT; page_directory[0] |= PAGE_WRITABLE | PAGE_PRESENT;
current_dir_entry = 1; current_dir_entry = 1;
next_table += 0x1000; /* next_table += 0x1000; */
} }
else if (mmap->type == 1) else if (mmap->type == 1)
{ {
...@@ -83,7 +107,7 @@ page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot ...@@ -83,7 +107,7 @@ page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot
page_directory[current_dir_entry] |= PAGE_WRITABLE | PAGE_PRESENT; page_directory[current_dir_entry] |= PAGE_WRITABLE | PAGE_PRESENT;
current_dir_entry += 1; current_dir_entry += 1;
next_table += 0x1000; /* next_table += 0x1000; */
} }
// There will be an edge case where there is less than a // There will be an edge case where there is less than a
// full page table to be mapped. This handlis this case. // full page table to be mapped. This handlis this case.
...@@ -94,7 +118,7 @@ page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot ...@@ -94,7 +118,7 @@ page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot
page_directory[current_dir_entry] |= PAGE_WRITABLE | PAGE_PRESENT; page_directory[current_dir_entry] |= PAGE_WRITABLE | PAGE_PRESENT;
current_dir_entry += 1; current_dir_entry += 1;
next_table += 0x1000; /* next_table += 0x1000; */
} }
} }
...@@ -102,7 +126,7 @@ page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot ...@@ -102,7 +126,7 @@ page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot
} }
// let the memory allocator work freely with all remaining memory // let the memory allocator work freely with all remaining memory
memory_mark = next_table; memory_mark = page_allocate();
return kern_page_dir; return kern_page_dir;
} }
......
...@@ -30,9 +30,18 @@ extern void enablePaging(); ...@@ -30,9 +30,18 @@ extern void enablePaging();
// Defined in linker.ld // Defined in linker.ld
extern char _kernel_end[]; extern char _kernel_end[];
// Allocation of pages
void* page_allocate();
// Allocatoion of memory
// Initilization
page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot_memory_map* mmap_end);
// Defined in the file mem.c // Defined in the file mem.c
page_directory_entry* initiate_directory(); page_directory_entry* initiate_directory();
page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot_memory_map* mmap_end);
// memory mark stores a counter of bytes for the most recently used // memory mark stores a counter of bytes for the most recently used
// memory. Everything before it is used and all valid memory after it // memory. Everything before it is used and all valid memory after it
...@@ -43,8 +52,6 @@ void* malloc(uint32_t bytes); ...@@ -43,8 +52,6 @@ void* malloc(uint32_t bytes);
// Required for elf loading - virtual memory and stuff // Required for elf loading - virtual memory and stuff
#endif
// TODO: // TODO:
/* /*
Two stage memory thing. Two stage memory thing.
...@@ -68,3 +75,5 @@ void* malloc(uint32_t bytes); ...@@ -68,3 +75,5 @@ void* malloc(uint32_t bytes);
Problems - how to determine when new pages are needed Problems - how to determine when new pages are needed
- how to load in elf stuff at arbitrary locations - how to load in elf stuff at arbitrary locations
*/ */
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment