wip: start to add multiple pd for each task
This commit is contained in:
parent
3b798e5daa
commit
8dd5373e7f
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "multiboot.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@ -16,9 +17,8 @@
|
||||
#define PAGE_MASK 0xFFFFF000
|
||||
#define HEAP_END 0xC0000000
|
||||
#define HEAP_START ((uint32_t)&_kernel_end - HEAP_END)
|
||||
#define KERNEL_START \
|
||||
((uint32_t)&_kernel_start) #define KERNEL_END((uint32_t)&_kernel_end - \
|
||||
HEAP_END)
|
||||
#define KERNEL_START ((uint32_t)&_kernel_start)
|
||||
#define KERNEL_END ((uint32_t)&_kernel_end - HEAP_END)
|
||||
#define PT_START 256
|
||||
|
||||
#define GET_PAGE_ADDR(pd_index, pt_index) \
|
||||
|
@ -66,7 +66,7 @@ void kernel_main(multiboot_info_t *mbd, uint32_t magic)
|
||||
*/
|
||||
/* "Martin 03:50, 22 March 2009 (UTC)\n"); */
|
||||
create_kernel_task();
|
||||
exec_fn(awa);
|
||||
exec_fn(uwu);
|
||||
/* exec_fn(owo); */
|
||||
/* exec_fn(owo); */
|
||||
/* exec_fn(owo); */
|
||||
|
@ -4,10 +4,10 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
u32 *page_directory = &boot_page_directory;
|
||||
u32 page_table_default[1024] __attribute__((aligned(PAGE_SIZE)));
|
||||
u32 frame_zones_page_table[1024] __attribute__((aligned(PAGE_SIZE)));
|
||||
u32 mem_size;
|
||||
uint32_t *page_directory = &boot_page_directory;
|
||||
uint32_t page_table_default[1024] __attribute__((aligned(PAGE_SIZE)));
|
||||
uint32_t frame_zones_page_table[1024] __attribute__((aligned(PAGE_SIZE)));
|
||||
uint32_t mem_size;
|
||||
struct frame_zone *head;
|
||||
|
||||
static void lst_add_back(struct frame_zone **root, struct frame_zone *element)
|
||||
@ -24,16 +24,16 @@ static void lst_add_back(struct frame_zone **root, struct frame_zone *element)
|
||||
|
||||
static void add_frame_node(multiboot_memory_map_t *mmmt)
|
||||
{
|
||||
static u32 index;
|
||||
static uint32_t index;
|
||||
|
||||
/**
|
||||
* # = kernel code
|
||||
* - = blank
|
||||
*/
|
||||
|
||||
u64 start_addr = mmmt->addr;
|
||||
u64 end_addr = mmmt->addr + mmmt->len;
|
||||
u64 len = mmmt->len;
|
||||
uint64_t start_addr = mmmt->addr;
|
||||
uint64_t end_addr = mmmt->addr + mmmt->len;
|
||||
uint64_t len = mmmt->len;
|
||||
|
||||
/** Kernel code cover all the block
|
||||
* this situation:
|
||||
@ -61,22 +61,21 @@ static void add_frame_node(multiboot_memory_map_t *mmmt)
|
||||
end_addr = ROUND_CEIL(start_addr + len, PAGE_SIZE);
|
||||
|
||||
init_page_table(frame_zones_page_table, 0);
|
||||
page_directory[1022] =
|
||||
((u32)frame_zones_page_table - HEAP_END) | 0x03;
|
||||
page_directory[1022] = ((uint32_t)frame_zones_page_table - HEAP_END) | 0x03;
|
||||
frame_zones_page_table[index] =
|
||||
((u32)start_addr & PAGE_MASK) | INIT_FLAGS;
|
||||
((uint32_t)start_addr & PAGE_MASK) | INIT_FLAGS;
|
||||
|
||||
struct frame_zone *current =
|
||||
(struct frame_zone *)GET_PAGE_ADDR(1022, index++);
|
||||
|
||||
current->frame_table = (u8 *)current + sizeof(struct frame_zone);
|
||||
current->frame_table = (uint8_t *)current + sizeof(struct frame_zone);
|
||||
|
||||
/** 8 is cause we are using u8
|
||||
/** 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))
|
||||
*/
|
||||
const u32 nb_frame = ((len * 8) / (PAGE_SIZE * 8 + 1));
|
||||
const uint32_t nb_frame = ((len * 8) / (PAGE_SIZE * 8 + 1));
|
||||
current->first_free_frame = 0;
|
||||
current->next = NULL;
|
||||
current->remaining_frames = nb_frame;
|
||||
@ -85,10 +84,10 @@ static void add_frame_node(multiboot_memory_map_t *mmmt)
|
||||
memset(current->frame_table, 0,
|
||||
nb_frame * sizeof(*current->frame_table));
|
||||
|
||||
u32 i = 1;
|
||||
uint32_t i = 1;
|
||||
for (; i < CEIL(nb_frame, PAGE_SIZE); i++)
|
||||
frame_zones_page_table[index + i] =
|
||||
((u32)(start_addr + i * PAGE_SIZE) & PAGE_MASK) |
|
||||
((uint32_t)(start_addr + i * PAGE_SIZE) & PAGE_MASK) |
|
||||
INIT_FLAGS;
|
||||
current->addr = (void *)start_addr + i * PAGE_SIZE;
|
||||
index += i - 1;
|
||||
@ -97,7 +96,7 @@ static void add_frame_node(multiboot_memory_map_t *mmmt)
|
||||
|
||||
static void init_frame_zones(void)
|
||||
{
|
||||
for (u32 i = 0; i < mmap_length; i++) {
|
||||
for (uint32_t i = 0; i < mmap_length; i++) {
|
||||
multiboot_memory_map_t *mmmt =
|
||||
(multiboot_memory_map_t *)mmap_addr + i;
|
||||
if (mmmt->type == MULTIBOOT_MEMORY_AVAILABLE)
|
||||
@ -105,12 +104,12 @@ static void init_frame_zones(void)
|
||||
}
|
||||
}
|
||||
|
||||
void init_memory(multiboot_info_t *mbd, u32 magic)
|
||||
void init_memory(multiboot_info_t *mbd, uint32_t magic)
|
||||
{
|
||||
for (u16 i = 0; i < 0x300; i++)
|
||||
for (uint16_t i = 0; i < 0x300; i++)
|
||||
page_directory[i] = 0x02;
|
||||
init_page_table(page_table_default, 0);
|
||||
page_directory[0] = ((u32)page_table_default - HEAP_END) | 0x03;
|
||||
page_directory[0] = ((uint32_t)page_table_default - HEAP_END) | 0x03;
|
||||
init_multiboot(mbd, magic);
|
||||
init_frame_zones();
|
||||
}
|
||||
|
@ -6,20 +6,21 @@
|
||||
#include "kprintf.h"
|
||||
#include "memory.h"
|
||||
#include "string.h"
|
||||
#include "task.h"
|
||||
#include "utils.h"
|
||||
|
||||
static i16 find_next_block(size_t nb_pages, u16 *pd_index_ptr,
|
||||
u32 **page_table_ptr)
|
||||
static int16_t find_next_block(size_t nb_pages, uint16_t *pd_index_ptr,
|
||||
uint32_t **page_table_ptr)
|
||||
{
|
||||
for (*pd_index_ptr = 1; *pd_index_ptr < 768; (*pd_index_ptr)++) {
|
||||
if (page_directory[(*pd_index_ptr)] == 0x02) {
|
||||
if (current_task->heap[(*pd_index_ptr)] == 0x02) {
|
||||
if (add_page_table(*pd_index_ptr) < 0)
|
||||
return -2;
|
||||
}
|
||||
*page_table_ptr =
|
||||
(u32 *)GET_PAGE_ADDR(0, *pd_index_ptr + PT_START);
|
||||
for (u16 i = 0; i + nb_pages - 1 < PT_SIZE; i++) {
|
||||
u16 j;
|
||||
(uint32_t *)GET_PAGE_ADDR(0, *pd_index_ptr + PT_START);
|
||||
for (uint16_t i = 0; i + nb_pages - 1 < PT_SIZE; i++) {
|
||||
uint16_t j;
|
||||
for (j = 0; (*page_table_ptr)[i + j] >> 12 == i + j &&
|
||||
j < nb_pages;
|
||||
j++)
|
||||
@ -32,26 +33,26 @@ static i16 find_next_block(size_t nb_pages, u16 *pd_index_ptr,
|
||||
return -1;
|
||||
}
|
||||
|
||||
i8 add_single_page(void *frame)
|
||||
int8_t add_single_page(void *frame)
|
||||
{
|
||||
u16 pd_index;
|
||||
u32 *page_table;
|
||||
const i16 i = find_next_block(1, &pd_index, &page_table);
|
||||
uint16_t pd_index;
|
||||
uint32_t *page_table;
|
||||
const int16_t i = find_next_block(1, &pd_index, &page_table);
|
||||
|
||||
if (i < 0) {
|
||||
kprintf(KERN_CRIT "impossible to add page to page directory\n");
|
||||
return -1;
|
||||
}
|
||||
page_table[i] = ((u32)frame & PAGE_MASK) | INIT_FLAGS;
|
||||
page_table[i] = ((uint32_t)frame & PAGE_MASK) | INIT_FLAGS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *alloc_pages(size_t size, void **phys_addr)
|
||||
{
|
||||
const u32 nb_pages = CEIL(size, PAGE_SIZE);
|
||||
u16 pd_index;
|
||||
u32 *page_table;
|
||||
const i16 index = find_next_block(nb_pages, &pd_index, &page_table);
|
||||
const uint32_t nb_pages = CEIL(size, PAGE_SIZE);
|
||||
uint16_t pd_index;
|
||||
uint32_t *page_table;
|
||||
const int16_t index = find_next_block(nb_pages, &pd_index, &page_table);
|
||||
|
||||
if (index < 0) {
|
||||
kprintf(KERN_CRIT "%d: Not enough pages (max: %d)\n", index,
|
||||
@ -67,7 +68,7 @@ void *alloc_pages(size_t size, void **phys_addr)
|
||||
}
|
||||
if (phys_addr)
|
||||
*phys_addr = frame;
|
||||
page_table[i] = ((u32)frame & PAGE_MASK) | INIT_FLAGS;
|
||||
page_table[i] = ((uint32_t)frame & PAGE_MASK) | INIT_FLAGS;
|
||||
}
|
||||
memset((void *)GET_PAGE_ADDR(pd_index, index), 0, nb_pages * PAGE_SIZE);
|
||||
return (void *)GET_PAGE_ADDR(pd_index, index);
|
||||
@ -75,13 +76,13 @@ void *alloc_pages(size_t size, void **phys_addr)
|
||||
|
||||
int free_pages(void *page_ptr, size_t size)
|
||||
{
|
||||
const u32 page_addr = (u32)page_ptr;
|
||||
const u32 nb_pages = CEIL(size, PAGE_SIZE);
|
||||
const u32 page_index = page_addr / PAGE_SIZE;
|
||||
const u32 pd_index = page_index / PD_SIZE;
|
||||
const u32 pt_index = page_index % PD_SIZE;
|
||||
const uint32_t page_addr = (uint32_t)page_ptr;
|
||||
const uint32_t nb_pages = CEIL(size, PAGE_SIZE);
|
||||
const uint32_t page_index = page_addr / PAGE_SIZE;
|
||||
const uint32_t pd_index = page_index / PD_SIZE;
|
||||
const uint32_t pt_index = page_index % PD_SIZE;
|
||||
|
||||
if ((u32)pd_index > 0x300) {
|
||||
if ((uint32_t)pd_index > 0x300) {
|
||||
kprintf(KERN_WARNING "Address out of range\n");
|
||||
return -1;
|
||||
} else if (page_addr % PAGE_SIZE) {
|
||||
@ -91,9 +92,8 @@ int free_pages(void *page_ptr, size_t size)
|
||||
kprintf(KERN_WARNING "Invalid number of frames\n");
|
||||
return -1;
|
||||
}
|
||||
u32 *page_table =
|
||||
(u32 *)GET_PAGE_ADDR(0, PT_START + pd_index);
|
||||
for (u16 i = pt_index; i < pt_index + nb_pages; i++) {
|
||||
uint32_t *page_table = (uint32_t *)GET_PAGE_ADDR(0, PT_START + pd_index);
|
||||
for (uint16_t i = pt_index; i < pt_index + nb_pages; i++) {
|
||||
if (page_table[i] >> 12 == i) {
|
||||
kprintf(KERN_WARNING "Page already free\n");
|
||||
return -2;
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include "kpanic.h"
|
||||
#include "kprintf.h"
|
||||
#include "memory.h"
|
||||
#include "task.h"
|
||||
|
||||
Zone *zones[3];
|
||||
|
||||
@ -17,15 +18,15 @@ static void add_zone(Zone *zone, block_type_t type)
|
||||
zones[type] = zone;
|
||||
}
|
||||
|
||||
static void new_block(Zone *zone, u32 zone_size)
|
||||
static void new_block(Zone *zone, uint32_t zone_size)
|
||||
{
|
||||
Block *new_block = (Block *)align_mem((u32)zone + sizeof(Zone));
|
||||
Block *new_block = (Block *)align_mem((uint32_t)zone + sizeof(Zone));
|
||||
|
||||
// Metadata
|
||||
new_block->in_use = false;
|
||||
new_block->size = zone_size - sizeof(Zone) - sizeof(Block);
|
||||
new_block->sub_size = new_block->size;
|
||||
new_block->ptr = (Block *)((u32)new_block + sizeof(Block));
|
||||
new_block->ptr = (Block *)((uint32_t)new_block + sizeof(Block));
|
||||
new_block->zone = zone;
|
||||
|
||||
// Init future linked lists
|
||||
@ -46,8 +47,9 @@ static void new_block(Zone *zone, u32 zone_size)
|
||||
assert(zone->free == new_block);
|
||||
}
|
||||
|
||||
int new_vzone(block_type_t type, u32 size)
|
||||
int new_vzone(block_type_t type, uint32_t size)
|
||||
{
|
||||
assert(current_task->pid);
|
||||
void *heap = alloc_pages(size, NULL);
|
||||
if (heap == NULL) {
|
||||
kprintf(KERN_ERR "error: alloc_pages failed\n");
|
||||
|
@ -19,6 +19,8 @@ void scheduler(void)
|
||||
if (!current_task) // || current_task->next == current_task)
|
||||
return;
|
||||
cli();
|
||||
uint32_t *old_pd = current_task->heap;
|
||||
current_task->heap = page_directory;
|
||||
struct task *it = current_task->next;
|
||||
while (it && it->status != RUN) {
|
||||
if (it != current_task && func[it->status]) {
|
||||
|
@ -10,36 +10,49 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
u32 esp_backup;
|
||||
uint32_t esp_backup;
|
||||
|
||||
struct task *create_task(u8 uid)
|
||||
struct task *create_task(uint8_t uid)
|
||||
{
|
||||
static u32 pid = 1;
|
||||
static uint32_t pid = 1;
|
||||
uint32_t *old_pd = current_task->heap;
|
||||
current_task->heap = page_directory; // kernel pd
|
||||
struct task *new_task = vmalloc(sizeof(struct task));
|
||||
current_task->heap = old_pd;
|
||||
if (!new_task)
|
||||
return NULL;
|
||||
new_task->status = RUN;
|
||||
new_task->uid = uid;
|
||||
new_task->esp = new_task->esp0 + STACK_SIZE;
|
||||
new_task->pid = pid++;
|
||||
|
||||
// Allocate new pd
|
||||
old_pd = current_task->heap;
|
||||
current_task->heap = alloc_pages(4096, (void **)&new_task->cr3);
|
||||
if (!new_task->heap) {
|
||||
current_task->heap = old_pd;
|
||||
vfree(new_task);
|
||||
return NULL;
|
||||
}
|
||||
new_task->heap = current_task->heap;
|
||||
|
||||
// Allocate new stack on the newly allocated pd
|
||||
new_task->esp0 = alloc_pages(STACK_SIZE, NULL);
|
||||
current_task->heap = old_pd;
|
||||
if (!new_task->esp0) {
|
||||
vfree(new_task);
|
||||
free_pages(new_task->heap, 4096);
|
||||
return NULL;
|
||||
}
|
||||
new_task->heap[768] = ((uint32_t)boot_page_table1 - HEAP_END) | 0x03;
|
||||
|
||||
new_task->next = current_task->next;
|
||||
new_task->prev = current_task;
|
||||
current_task->next = new_task;
|
||||
new_task->status = RUN;
|
||||
new_task->uid = uid;
|
||||
new_task->esp0 = alloc_pages(STACK_SIZE, NULL);
|
||||
if (!new_task->esp0) {
|
||||
vfree(new_task);
|
||||
return NULL;
|
||||
}
|
||||
new_task->esp = new_task->esp0 + STACK_SIZE;
|
||||
new_task->pid = pid++;
|
||||
new_task->heap = alloc_pages(4096, (void **)&new_task->cr3);
|
||||
if (!new_task->heap) {
|
||||
free_pages(new_task->esp0, STACK_SIZE);
|
||||
vfree(new_task);
|
||||
return NULL;
|
||||
}
|
||||
return new_task;
|
||||
}
|
||||
|
||||
i8 create_kernel_task(void)
|
||||
int8_t create_kernel_task(void)
|
||||
{
|
||||
struct task *new_task = vmalloc(sizeof(struct task));
|
||||
if (!new_task)
|
||||
@ -60,7 +73,7 @@ void exec_fn(void (*fn)(void))
|
||||
struct task *new_task = create_task(OWNER_KERNEL);
|
||||
if (!new_task)
|
||||
kpanic("failed to create new task");
|
||||
new_task->eip = (u32 *)fn;
|
||||
new_task->eip = (uint32_t *)fn;
|
||||
}
|
||||
|
||||
void zombify_task(struct task *task)
|
||||
|
Loading…
Reference in New Issue
Block a user