Reimplemented page frame allocator using linked list

This commit is contained in:
g1n 2022-02-13 21:09:00 +02:00
parent 1e6f9083b8
commit f111e459c9
Signed by: g1n
GPG Key ID: 8D352193D65D4E2C
3 changed files with 33 additions and 34 deletions

View File

@ -3,14 +3,18 @@
#include <idt.h> #include <idt.h>
#define NPAGES 4096U // FIXME #define NPAGES 4096 // FIXME
#define ENTRY_WIDTH 32U #define PAGE_SIZE 16
#define PAGE_SIZE 0x1000U
typedef struct page {
struct page *next;
int value;
} page_t;
typedef uint32_t page_addr_t; typedef uint32_t page_addr_t;
page_addr_t kalloc_frame(); page_t *kalloc_frame();
void kfree_frame(page_addr_t addr); void kfree_frame(page_t *page);
void paging_init(); void paging_init();
void page_fault(registers_t regs); void page_fault(registers_t regs);

View File

@ -1,6 +1,6 @@
/* The bootloader will look at this image and start execution at the symbol /* The bootloader will look at this image and start execution at the symbol
designated as the entry point. */ designated as the entry point. */
ENTRY(_start) ENTRY(boot)
/* Tell where the various sections of the object files will be put in the final /* Tell where the various sections of the object files will be put in the final
kernel image. */ kernel image. */
@ -15,6 +15,7 @@ SECTIONS
Next we'll put the .text section. */ Next we'll put the .text section. */
.text BLOCK(4K) : ALIGN(4K) .text BLOCK(4K) : ALIGN(4K)
{ {
text_start = .;
*(.multiboot) *(.multiboot)
*(.text) *(.text)
} }
@ -29,13 +30,17 @@ SECTIONS
.data BLOCK(4K) : ALIGN(4K) .data BLOCK(4K) : ALIGN(4K)
{ {
*(.data) *(.data)
end_data = .;
} }
/* Read-write data (uninitialized) and stack */ /* Read-write data (uninitialized) and stack */
.bss BLOCK(4K) : ALIGN(4K) .bss BLOCK(4K) : ALIGN(4K)
{ {
sbss = .;
*(COMMON) *(COMMON)
*(.bss) *(.bss)
ebss = .;
endkernel = .;
} }
/* The compiler may produce other sections, by default it will put them in /* The compiler may produce other sections, by default it will put them in

View File

@ -5,8 +5,6 @@
uint32_t page_dir[1024] __attribute__((aligned(4096))); uint32_t page_dir[1024] __attribute__((aligned(4096)));
uint32_t page_table[1024] __attribute__((aligned(4096))); uint32_t page_table[1024] __attribute__((aligned(4096)));
static uint32_t bitmap[NPAGES/ENTRY_WIDTH] __attribute__((aligned(4096)));
void page_fault(registers_t regs) { void page_fault(registers_t regs) {
uint32_t faulting_address; uint32_t faulting_address;
__asm__ volatile("mov %%cr2, %0" : "=r" (faulting_address)); __asm__ volatile("mov %%cr2, %0" : "=r" (faulting_address));
@ -38,32 +36,26 @@ void enable_paging() {
__asm__ volatile("mov %0, %%cr0":: "r"(cr0)); __asm__ volatile("mov %0, %%cr0":: "r"(cr0));
} }
page_addr_t kalloc_frame() { extern uint32_t endkernel;
uint32_t bitmap_index;
uint32_t element_index;
uint32_t page_frame_number;
for (bitmap_index = 0; bitmap_index < NPAGES/ENTRY_WIDTH; bitmap_index++)
if (bitmap[bitmap_index] != 0) break; // FIXME
if (bitmap_index >= NPAGES/ENTRY_WIDTH) page_t *next_page;
return -1; page_t *free_pages = 0;
for (element_index = 0; element_index < ENTRY_WIDTH; element_index++) page_t *kalloc_frame(){
if ((bitmap[bitmap_index] & (1U << element_index)) != 0) break; // FIXME page_t *temp;
if (free_pages == 0) {
if ((bitmap[bitmap_index] & (1U << element_index)) == 0) temp = next_page;
return -1; next_page += PAGE_SIZE;
} else {
bitmap[bitmap_index] &= ~(1U << element_index); temp = free_pages;
free_pages = free_pages->next;
page_frame_number = bitmap_index * ENTRY_WIDTH + element_index; }
return page_frame_number * PAGE_SIZE; return temp;
} }
void kfree_frame(page_addr_t addr) { void kfree_frame(page_t *page) {
page_addr_t index = addr / PAGE_SIZE; page->next = free_pages;
bitmap[index / ENTRY_WIDTH] |= (1U << ((index / ENTRY_WIDTH) + (index % ENTRY_WIDTH))); free_pages = page;
} }
void paging_init() { void paging_init() {
@ -71,10 +63,8 @@ void paging_init() {
for(i = 0; i < 1024; i++) { for(i = 0; i < 1024; i++) {
page_dir[i] = 0x00000002; page_dir[i] = 0x00000002;
} }
for(i = 0; i < NPAGES/ENTRY_WIDTH; i++) { next_page = (page_t*)endkernel;
bitmap[i] = ~0;
}
for(i = 0; i < 1024; i++) { for(i = 0; i < 1024; i++) {
page_table[i] = (i * 0x1000) | 3; page_table[i] = (i * 0x1000) | 3;