Compare commits

...

2 Commits

4 changed files with 59 additions and 34 deletions

View File

@ -30,22 +30,17 @@
frame_table[i / 8] &= ~(1 << (i % 8)); \
} while (0)
/*
* len is the total size of the zone (ratio starnakin)
* size is the remaining usable size after allocating
* the struct for the linked list
*/
struct frame_zone {
void *addr;
uint32_t first_free_frame;
uint32_t *frame_table;
uint32_t len;
uint32_t size;
uint8_t *frame_table;
uint32_t total_frames;
uint32_t remaining_frames;
struct frame_zone *next;
};
extern uint32_t _kernel_end;
extern uint32_t _kernel_start;
extern uint32_t boot_page_directory;
extern uint32_t *page_directory;
extern uint32_t page_table_default[1024];

View File

@ -66,7 +66,13 @@ void kernel_main(multiboot_info_t *mbd, uint32_t magic)
"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");
/* alloc_frame(); */
// PRINT_PTR(alloc_frame());
/*void *ptr;
while ((ptr = alloc_pages(PAGE_SIZE * 1020))) {
if (ptr)
memset(ptr, ~0, PAGE_SIZE * 1020);
}*/
/* vmalloc(10); */
while (vmalloc(10))
;

View File

@ -30,11 +30,11 @@ void *alloc_frame(void)
int free_frame(void *frame_ptr)
{
struct frame_zone *it = head;
while (it && (frame_ptr < it->addr || frame_ptr >= it->addr + it->len))
while (it && (frame_ptr < it->addr ||
frame_ptr >= it->addr + it->total_frames * PAGE_SIZE))
it = it->next;
uint32_t index =
(frame_ptr + (it->len - it->size) - it->addr) / PAGE_SIZE;
uint32_t index = ((frame_ptr - it->addr) / PAGE_SIZE);
SET_FRAME(it->frame_table, index, 0);
if (it->first_free_frame > index)
it->first_free_frame = index;

View File

@ -45,11 +45,37 @@ static void init_multiboot(multiboot_info_t *mbd, uint32_t magic)
mmap_length = mbd_virt->mmap_length / sizeof(multiboot_memory_map_t);
}
static void lst_add_back(struct frame_zone **root, struct frame_zone *element)
{
if (!*root) {
*root = element;
return;
}
struct frame_zone *it = *root;
while (it->next)
it = it->next;
it->next = element;
}
static void add_frame_node(multiboot_memory_map_t *mmmt)
{
static uint32_t index;
void *zone = (void *)mmmt->addr;
// Kernel code on the block
if (HEAP_START >= zone + mmmt->len)
return;
// KERNEL code partially on the block
if (HEAP_START >= zone) {
const uint32_t len = mmmt->len -
((uint64_t)&_kernel_end - HEAP_END) -
(uint64_t)zone;
mmmt->len = CEIL(len, PAGE_SIZE) * PAGE_SIZE;
zone =
(void *)((uint32_t)CEIL(HEAP_START, PAGE_SIZE) * PAGE_SIZE);
}
init_page_table(frame_zones_page_table, 0);
page_directory[1022] =
((uint32_t)frame_zones_page_table - HEAP_END) | 0x03;
@ -58,32 +84,30 @@ static void add_frame_node(multiboot_memory_map_t *mmmt)
struct frame_zone *current =
(struct frame_zone *)GET_PAGE_ADDR(1022, index++);
memset(current, 0, sizeof(struct frame_zone));
current->addr = (void *)mmmt->addr;
current->frame_table = (uint32_t *)current + sizeof(struct frame_zone);
const size_t frame_table_size =
CEIL(current->size - sizeof(struct frame_zone), PAGE_SIZE * 32);
for (uint32_t i = index; index - i < CEIL(frame_table_size, PAGE_SIZE);
index++)
frame_zones_page_table[index] =
((uint32_t)zone + PAGE_SIZE & PAGE_MASK) | INIT_FLAGS;
// glhf reading this bozo
current->len = mmmt->len;
current->size = (mmmt->len - (sizeof(struct frame_zone) +
(mmmt->len / PAGE_SIZE) / 32));
current->remaining_frames = current->size / PAGE_SIZE;
current->frame_table = (uint8_t *)current + sizeof(struct frame_zone);
/** 8 is cause we are using uint8_t
nb_frame = size / (PAGE_SIZE + 1 / 8)
cause we are using non decimal number
nb_frame = ((size * 8) / (PAGE_SIZE * 8 + 1)) / 8
*/
const uint32_t nb_frame = ((mmmt->len * 8) / (PAGE_SIZE * 8 + 1)) - 1;
current->first_free_frame = 0;
current->next = NULL;
current->remaining_frames = nb_frame;
current->total_frames = nb_frame;
if (!head) {
head = current;
return;
}
struct frame_zone *it = head;
while (it->next)
it = it->next;
it->next = current;
memset(current->frame_table, 0, nb_frame * 4);
uint32_t i = 1;
for (; i < CEIL(nb_frame, PAGE_SIZE); i++)
frame_zones_page_table[index + i] =
((uint32_t)zone + i * PAGE_SIZE & PAGE_MASK) | INIT_FLAGS;
current->addr = zone + i * PAGE_SIZE;
index += i - 1;
lst_add_back(&head, current);
}
static void init_frame_zones(void)