diff options
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/features/load.c | 31 | ||||
-rw-r--r-- | kernel/features/mm.c | 5 | ||||
-rw-r--r-- | kernel/inc/mm.h | 13 |
3 files changed, 40 insertions, 9 deletions
diff --git a/kernel/features/load.c b/kernel/features/load.c index 4b3f8ea..31e22dc 100644 --- a/kernel/features/load.c +++ b/kernel/features/load.c @@ -6,15 +6,21 @@ #include <mm.h> #include <str.h> +// TODO: Fix pdi < 256! +#define PROC_DATA_ADDR 0xc000000 + #define PROC_STACK_SIZE 0x4000 +#define PROC_STACK_ADDR (PROC_DATA_ADDR - 256) void proc_load(struct proc *proc, u32 entry) { - u32 stack; - memory_alloc(proc->page_dir, PROC_STACK_SIZE, MEMORY_CLEAR, &stack); + /* memory_dir_switch(proc->page_dir); */ + u32 paddr = physical_alloc(PROC_STACK_SIZE); + virtual_map(proc->page_dir, PROC_STACK_ADDR, paddr, PROC_STACK_SIZE, + MEMORY_USER | MEMORY_CLEAR); - proc->regs.ebp = stack; - proc->regs.useresp = stack; + proc->regs.ebp = PROC_STACK_ADDR; + proc->regs.useresp = PROC_STACK_ADDR; proc->regs.eip = entry; proc->entry = entry; } @@ -23,13 +29,22 @@ int bin_load(const char *path, struct proc *proc) { struct stat s = { 0 }; vfs_stat(path, &s); - u32 data; - memory_alloc(proc->page_dir, PAGE_ALIGN_UP(s.size), MEMORY_CLEAR, &data); - if (!vfs_read(path, (void *)data, 0, s.size)) + struct proc *current = proc_current(); + struct page_dir *prev = current ? current->page_dir : memory_kernel_dir(); + + u32 size = PAGE_ALIGN_UP(s.size); + memory_dir_switch(proc->page_dir); + u32 paddr = physical_alloc(size); + virtual_map(proc->page_dir, PROC_DATA_ADDR, paddr, size, MEMORY_USER | MEMORY_CLEAR); + + if (!vfs_read(path, (void *)PROC_DATA_ADDR, 0, s.size)) { + memory_dir_switch(prev); return 1; + } strcpy(proc->name, path); - proc_load(proc, data); + proc_load(proc, PROC_DATA_ADDR); + memory_dir_switch(prev); return 0; } diff --git a/kernel/features/mm.c b/kernel/features/mm.c index ad70685..b17d9fc 100644 --- a/kernel/features/mm.c +++ b/kernel/features/mm.c @@ -128,8 +128,13 @@ u32 virtual_to_physical(struct page_dir *dir, u32 vaddr) u32 pti = PTI(vaddr); union page_dir_entry *dir_entry = &dir->entries[pdi]; + if (!dir_entry->bits.present) + return 0; + struct page_table *table = (struct page_table *)(dir_entry->bits.address * PAGE_SIZE); union page_table_entry *table_entry = &table->entries[pti]; + if (!table_entry->bits.present) + return 0; return (table_entry->bits.address * PAGE_SIZE) + (vaddr & (PAGE_SIZE - 1)); } diff --git a/kernel/inc/mm.h b/kernel/inc/mm.h index 00d9e4b..e680ab7 100644 --- a/kernel/inc/mm.h +++ b/kernel/inc/mm.h @@ -10,6 +10,8 @@ * Physical */ +u32 physical_alloc(u32 n); + /** * Virtual */ @@ -63,6 +65,10 @@ struct page_dir { union page_dir_entry entries[PAGE_COUNT]; } PACKED; +u32 virtual_to_physical(struct page_dir *dir, u32 vaddr); +void virtual_map(struct page_dir *dir, u32 vaddr, u32 paddr, u32 n, u8 user); +struct memory_range virtual_alloc(struct page_dir *dir, struct memory_range physical_range, + u32 flags); void paging_install(struct mem_info *mem_info); /** @@ -79,9 +85,14 @@ struct memory_range { u32 size; }; +struct memory_range memory_range_from_address(u32 base, u32 size); +struct memory_range memory_range_around_address(u32 base, u32 size); + struct page_dir *memory_dir_create(void); void memory_dir_switch(struct page_dir *dir); -void memory_alloc(struct page_dir *dir, u32 size, u32 flags, u32 *out); +// TODO: Remove these wrappers completely? +/* void memory_alloc(struct page_dir *dir, u32 size, u32 flags, u32 *out); */ +/* void memory_map(struct page_dir *dir, struct memory_range range, u32 flags); */ struct page_dir *memory_kernel_dir(void); #endif |