mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-27 12:48:22 +00:00
user thread/process and elf loader stub
This commit is contained in:
Executable
BIN
Binary file not shown.
@@ -22,5 +22,7 @@ void* arch_mmu_getphysicaladdr(arch_mmu_tableptr, void*);
|
||||
void arch_mmu_unmap(arch_mmu_tableptr, void*);
|
||||
void arch_mmu_init();
|
||||
void arch_mmu_apinit();
|
||||
arch_mmu_tableptr arch_mmu_newcontext();
|
||||
void arch_mmu_switchcontext(arch_mmu_tableptr);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -9,5 +9,6 @@ typedef struct{
|
||||
} arch_regs;
|
||||
|
||||
void arch_regs_setupkernel(arch_regs* regs, void* ip, void* stack, bool interrupts);
|
||||
void arch_regs_setupuser(arch_regs* regs, void* ip, void* stack, bool interrupts);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -27,8 +27,20 @@ struct limine_terminal_response* liminetty;
|
||||
|
||||
int ttylock = 0;
|
||||
|
||||
static vmm_context* liminettyctx;
|
||||
|
||||
void liminewrite(char* str, size_t c){
|
||||
|
||||
vmm_context* old = arch_getcls()->context;
|
||||
|
||||
if(liminettyctx)
|
||||
vmm_switchcontext(liminettyctx);
|
||||
|
||||
liminetty->write(liminetty->terminals[0], str, c);
|
||||
|
||||
if(liminettyctx)
|
||||
vmm_switchcontext(old);
|
||||
|
||||
}
|
||||
|
||||
void kmain(){
|
||||
@@ -52,6 +64,8 @@ void kmain(){
|
||||
arch_mmu_init();
|
||||
|
||||
vmm_init();
|
||||
|
||||
liminettyctx = arch_getcls()->context;
|
||||
|
||||
alloc_init();
|
||||
|
||||
@@ -82,6 +96,8 @@ void kmain(){
|
||||
|
||||
smp_init();
|
||||
|
||||
sched_runinit();
|
||||
|
||||
_panic("End of kmain()", 0);
|
||||
|
||||
}
|
||||
|
||||
+19
-5
@@ -26,8 +26,8 @@ static void changeentry(uint64_t* entry, void* paddr, uint64_t flags){
|
||||
|
||||
#define PTR_FLAGS ARCH_MMU_MAP_READ | ARCH_MMU_MAP_WRITE | ARCH_MMU_MAP_USER | ARCH_MMU_MAP_NOEXEC
|
||||
|
||||
static void switchcontext(arch_mmu_tableptr context){
|
||||
asm("mov %%rax, %%cr3" : : "a"(context));
|
||||
void arch_mmu_switchcontext(arch_mmu_tableptr context){
|
||||
asm("mov %%rax, %%cr3" : : "a"(context) : "memory");
|
||||
}
|
||||
|
||||
void* inv = NULL;
|
||||
@@ -162,7 +162,21 @@ void arch_mmu_unmap(arch_mmu_tableptr context, void* vaddr){
|
||||
|
||||
}
|
||||
|
||||
static arch_mmu_tableptr context; //boostrap context
|
||||
static arch_mmu_tableptr context; //boostrap context; also holds the kernel page tables
|
||||
|
||||
arch_mmu_tableptr arch_mmu_newcontext(){
|
||||
|
||||
arch_mmu_tableptr newcontext = pmm_alloc(1);
|
||||
if(!newcontext)
|
||||
return NULL;
|
||||
|
||||
memcpy((void*)newcontext + (size_t)limine_hhdm_offset, context, PAGE_SIZE);
|
||||
|
||||
memset((void*)newcontext + (size_t)limine_hhdm_offset, 0, PAGE_SIZE / 2);
|
||||
|
||||
return newcontext;
|
||||
|
||||
}
|
||||
|
||||
void arch_mmu_init(){
|
||||
|
||||
@@ -252,7 +266,7 @@ void arch_mmu_init(){
|
||||
}
|
||||
|
||||
|
||||
switchcontext(context);
|
||||
arch_mmu_switchcontext(context);
|
||||
arch_getcls()->context->context = context;
|
||||
|
||||
printf("In bootstrap context\n");
|
||||
@@ -261,7 +275,7 @@ void arch_mmu_init(){
|
||||
|
||||
void arch_mmu_apinit(){
|
||||
|
||||
switchcontext(context);
|
||||
arch_mmu_switchcontext(context);
|
||||
|
||||
arch_getcls()->context = pmm_hhdmalloc(1);
|
||||
|
||||
|
||||
@@ -11,3 +11,16 @@ void arch_regs_setupkernel(arch_regs* regs, void* ip, void* stack, bool interrup
|
||||
regs->ss = 0x30;
|
||||
regs->rflags = interrupts ? 0x200 : 0;
|
||||
}
|
||||
|
||||
void arch_regs_setupuser(arch_regs* regs, void* ip, void* stack, bool interrupts){
|
||||
regs->rip = ip;
|
||||
regs->rsp = stack;
|
||||
regs->cs = 0x38 | 3;
|
||||
regs->ds = 0x40 | 3;
|
||||
regs->es = 0x40 | 3;
|
||||
regs->fs = 0x40 | 3;
|
||||
regs->gs = 0x40 | 3;
|
||||
regs->ss = 0x40 | 3;
|
||||
regs->rflags = interrupts ? 0x200 : 0;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
#ifndef _ELF_H_INCLUDE
|
||||
#define _ELF_H_INCLUDE
|
||||
|
||||
#include <kernel/sched.h>
|
||||
#include <kernel/vfs.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define ELF_MAGIC 0x464c457f // 0x7F ELF
|
||||
#define ELF_BITS_64 2
|
||||
#define ELF_ISA_X86_64 0x3E
|
||||
#define ELF_ENDIANNESS_LITTLE 1
|
||||
|
||||
#define ELF_RELOCATABLE 1
|
||||
#define ELF_EXECUTABLE 2
|
||||
#define ELF_SHARED 3
|
||||
#define ELF_CORE 4
|
||||
|
||||
#define ELF_SEGMENT_NULL 0
|
||||
#define ELF_SEGMENT_LOAD 1
|
||||
#define ELF_SEGMENT_DYNAMIC 2
|
||||
#define ELF_SEGMENT_INTERP 3
|
||||
#define ELF_SEGMENT_NOTE 4
|
||||
|
||||
#define ELF_FLAG_EXECUTABLE 1
|
||||
#define ELF_FLAG_WRITABLE 2
|
||||
#define ELF_FLAG_READABLE 4
|
||||
|
||||
|
||||
typedef struct{
|
||||
uint32_t magic;
|
||||
uint8_t bits;
|
||||
uint8_t endianness;
|
||||
uint8_t headversion;
|
||||
uint8_t abi;
|
||||
uint64_t padding;
|
||||
uint16_t type;
|
||||
uint16_t isa;
|
||||
uint32_t elfver;
|
||||
uint64_t entry;
|
||||
uint64_t ph_pos;
|
||||
uint64_t sh_pos;
|
||||
uint32_t flags;
|
||||
uint16_t headsize;
|
||||
uint16_t ph_size;
|
||||
uint16_t ph_count;
|
||||
uint16_t sh_size;
|
||||
uint16_t sh_count;
|
||||
uint16_t sh_names;
|
||||
} __attribute__((packed)) elf_header64;
|
||||
|
||||
#define PH_TYPE_LOAD 1
|
||||
#define PH_TYPE_DYNAMIC 2
|
||||
#define PH_TYPE_INTERPRETER 3
|
||||
#define PH_TYPE_NOTE 4
|
||||
|
||||
typedef struct{
|
||||
uint32_t type;
|
||||
uint32_t flags;
|
||||
uint64_t offset;
|
||||
uint64_t memaddr;
|
||||
uint64_t undefined;
|
||||
uint64_t fsize;
|
||||
uint64_t msize;
|
||||
uint64_t alignment;
|
||||
} elf_ph64;
|
||||
|
||||
// loads an elf executable from file, setting target thread with appropriate
|
||||
// values for it
|
||||
// also sets up the stack for said executable
|
||||
|
||||
int elf_load(thread_t* thread, vnode_t* node, char** argv, char** env);
|
||||
|
||||
#endif
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <arch/regs.h>
|
||||
#include <kernel/vfs.h>
|
||||
#include <kernel/vmm.h>
|
||||
|
||||
#define THREAD_PRIORITY_INTERRUPT 0
|
||||
#define THREAD_PRIORITY_KERNEL 1
|
||||
@@ -37,6 +38,7 @@ typedef struct _proc_t{
|
||||
pid_t pid;
|
||||
gid_t gid;
|
||||
uid_t uid;
|
||||
vmm_context* context;
|
||||
thread_t** threads;
|
||||
size_t threadcount;
|
||||
dirnode_t* root;
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#define KERNEL_SPACE_START 0xFFFF800000000000
|
||||
#define KERNEL_SPACE_END 0xFFFFFFFFFFFFFFFF
|
||||
#define USER_SPACE_START 0
|
||||
#define USER_SPACE_END 0x00007FFFFFFFFFFF
|
||||
|
||||
#define VMM_TYPE_FREE 0
|
||||
#define VMM_TYPE_ANON 1
|
||||
@@ -57,4 +59,5 @@ bool vmm_unmap(void* addr, size_t pagec);
|
||||
bool vmm_map(void* paddr, void* vaddr, size_t pagec, size_t mmuflags);
|
||||
void* vmm_alloc(size_t pagec, size_t mmuflags);
|
||||
bool vmm_setfree(void* addr, size_t pagec);
|
||||
vmm_context* vmm_newcontext();
|
||||
#endif
|
||||
|
||||
@@ -10,6 +10,6 @@ void *memcpy(void* dest, void* src, size_t size);
|
||||
void *memset(void* dest, unsigned long what, size_t size);
|
||||
int strcmp(const char *a, const char* b);
|
||||
int strncmp(const char* a, const char* b, size_t c);
|
||||
|
||||
char* strerror(int errnum);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <string.h>
|
||||
|
||||
static char* strs[] = {
|
||||
"No error",
|
||||
"Not a directory",
|
||||
"No such device",
|
||||
"Out of memory",
|
||||
"Invalid",
|
||||
"Function unimplemented",
|
||||
"File already exists",
|
||||
"No such file or directory"
|
||||
};
|
||||
|
||||
char* strerror(int errnum){
|
||||
return strs[errnum];
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <limine.h>
|
||||
#include <arch/cls.h>
|
||||
#include <kernel/alloc.h>
|
||||
|
||||
vmm_cache* caches;
|
||||
int klock;
|
||||
@@ -489,6 +490,38 @@ bool vmm_dealwithrequest(void* addr){
|
||||
|
||||
}
|
||||
|
||||
void vmm_switchcontext(vmm_context* context){
|
||||
arch_getcls()->context = context;
|
||||
arch_mmu_switchcontext(context->context);
|
||||
}
|
||||
|
||||
vmm_context* vmm_newcontext(){
|
||||
|
||||
vmm_context* context = alloc(sizeof(vmm_context));
|
||||
if(!context)
|
||||
return NULL;
|
||||
vmm_mapping* map = allocatefirst(caches);
|
||||
if(!map){
|
||||
free(context);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
context->context = arch_mmu_newcontext();
|
||||
if(!context->context){
|
||||
free(context);
|
||||
freeentry(map);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
map->start = USER_SPACE_START;
|
||||
map->end = USER_SPACE_END;
|
||||
|
||||
context->userstart = map;
|
||||
|
||||
return context;
|
||||
|
||||
}
|
||||
|
||||
void vmm_init(){
|
||||
printf("%lu mappings per cache\n", VMM_CACHE_ENTRY_COUNT);
|
||||
caches = newcache();
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
#include <kernel/elf.h>
|
||||
#include <kernel/alloc.h>
|
||||
#include <errno.h>
|
||||
|
||||
static bool check_header64(elf_header64* header){
|
||||
// XXX throw this into arch
|
||||
|
||||
if(header->magic != ELF_MAGIC)
|
||||
return false;
|
||||
|
||||
if(header->bits != ELF_BITS_64)
|
||||
return false;
|
||||
|
||||
if(header->endianness != ELF_ENDIANNESS_LITTLE)
|
||||
return false;
|
||||
|
||||
if(header->isa != ELF_ISA_X86_64)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
int elf_load(thread_t* thread, vnode_t* node, char** argv, char** env){
|
||||
|
||||
elf_header64 header;
|
||||
elf_ph64 ph;
|
||||
int err = 0;
|
||||
int readc = vfs_read(&err, node, &header, sizeof(elf_header64), 0);
|
||||
char* interp = NULL;
|
||||
|
||||
if(err)
|
||||
return err;
|
||||
|
||||
|
||||
if(readc != sizeof(elf_header64) || (!check_header64(&header)))
|
||||
return EINVAL;
|
||||
|
||||
|
||||
// find out if this needs an interpreter
|
||||
|
||||
for(size_t currentph = 0; currentph < header.ph_count; ++currentph){
|
||||
readc = vfs_read(&err, node, &ph, header.ph_size, header.ph_pos + currentph*header.ph_size);
|
||||
if(err)
|
||||
return err;
|
||||
if(readc != header.ph_size)
|
||||
return EINVAL;
|
||||
|
||||
if(ph.type == PH_TYPE_INTERPRETER){
|
||||
|
||||
// load the name and elf header of the interpreter
|
||||
|
||||
char* interp = alloc(ph.fsize + 1);
|
||||
if(!interp)
|
||||
return ENOMEM;
|
||||
|
||||
readc = vfs_read(&err, node, interp, ph.fsize, ph.offset);
|
||||
if(err){
|
||||
free(interp);
|
||||
return err;
|
||||
}
|
||||
if(ph.fsize != readc){
|
||||
free(interp);
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
err = vfs_open(&node, thread->proc->root, interp);
|
||||
|
||||
if(err){
|
||||
free(interp);
|
||||
return err;
|
||||
}
|
||||
|
||||
readc = vfs_read(&err, node, &header, sizeof(elf_header64), 0);
|
||||
|
||||
if(err)
|
||||
goto _fail;
|
||||
|
||||
if(readc != sizeof(elf_header64) || (!check_header64(&header))){
|
||||
err = EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// load program into memory
|
||||
|
||||
for(size_t currentph = 0; currentph < header.ph_count; ++currentph){
|
||||
|
||||
readc = vfs_read(&err, node, &ph, header.ph_size, header.ph_pos + currentph*header.ph_size);
|
||||
|
||||
if(err) goto _fail;
|
||||
|
||||
if(readc != header.ph_size){
|
||||
err = EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
if(ph.type != PH_TYPE_LOAD)
|
||||
continue;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
_fail:
|
||||
if(interp){
|
||||
free(interp);
|
||||
vfs_close(node);
|
||||
}
|
||||
|
||||
return err;
|
||||
|
||||
}
|
||||
@@ -6,6 +6,8 @@
|
||||
#include <arch/mmu.h>
|
||||
#include <kernel/pmm.h>
|
||||
#include <kernel/timer.h>
|
||||
#include <string.h>
|
||||
#include <kernel/elf.h>
|
||||
|
||||
#define THREAD_QUANTUM 10000
|
||||
|
||||
@@ -19,6 +21,31 @@ sched_queue queues[QUEUE_COUNT];
|
||||
sched_queue running;
|
||||
sched_queue blocked;
|
||||
|
||||
static proc_t* allocproc(size_t threadcount){
|
||||
|
||||
proc_t* proc = alloc(sizeof(proc_t));
|
||||
if(!proc)
|
||||
return NULL;
|
||||
|
||||
proc->threads = alloc(sizeof(thread_t*) * threadcount);
|
||||
|
||||
if(!proc->threads){
|
||||
free(proc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
proc->context = vmm_newcontext();
|
||||
|
||||
if(!proc->context){
|
||||
free(proc->threads);
|
||||
free(proc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return proc;
|
||||
|
||||
}
|
||||
|
||||
static thread_t* allocthread(proc_t* proc, state_t state, pid_t tid, size_t kstacksize){
|
||||
|
||||
thread_t* thread = alloc(sizeof(thread_t));
|
||||
@@ -57,6 +84,13 @@ static thread_t* allocthread(proc_t* proc, state_t state, pid_t tid, size_t ksta
|
||||
|
||||
}
|
||||
|
||||
static thread_t* freethread(thread_t* thread){
|
||||
free(thread->regs);
|
||||
// XXX use free() when it's better
|
||||
pmm_free(thread->kernelstackbase, thread->stacksize / PAGE_SIZE + 1);
|
||||
free(thread);
|
||||
}
|
||||
|
||||
static void queue_add(sched_queue* queue, thread_t* thread){
|
||||
thread->next = NULL;
|
||||
thread->prev = queue->end;
|
||||
@@ -158,7 +192,7 @@ static void switch_thread(thread_t* thread){
|
||||
|
||||
if(current->proc != thread->proc){
|
||||
|
||||
// TODO change proc specific stuff
|
||||
vmm_switchcontext(thread->proc->context);
|
||||
|
||||
}
|
||||
|
||||
@@ -170,6 +204,37 @@ static void switch_thread(thread_t* thread){
|
||||
|
||||
}
|
||||
|
||||
thread_t* sched_newuthread(void* ip, size_t kstacksize, void* stack, proc_t* proc, bool run, int prio){
|
||||
|
||||
thread_t* thread = allocthread(NULL, THREAD_STATE_WAITING, 0, kstacksize);
|
||||
|
||||
if(!proc){
|
||||
proc = allocproc(1);
|
||||
if(!proc){
|
||||
freethread(thread);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else{
|
||||
thread_t** tmp = realloc(proc->threads, sizeof(thread_t*) * (proc->threadcount + 1));
|
||||
if(!tmp){
|
||||
freethread(thread);
|
||||
return NULL;
|
||||
}
|
||||
proc->threads = tmp;
|
||||
++proc->threadcount;
|
||||
}
|
||||
|
||||
thread->priority = prio;
|
||||
arch_regs_setupuser(thread->regs, ip, stack, true);
|
||||
|
||||
if(run)
|
||||
queue_add(&queues[prio], thread);
|
||||
|
||||
return thread;
|
||||
|
||||
}
|
||||
|
||||
thread_t* sched_newkthread(void* ip, size_t stacksize, bool run, int prio){
|
||||
thread_t* thread = allocthread(NULL, THREAD_STATE_WAITING, 0, stacksize);
|
||||
|
||||
@@ -195,3 +260,33 @@ void sched_init(){
|
||||
|
||||
}
|
||||
|
||||
void sched_runinit(){
|
||||
|
||||
printf("Loading /sbin/init\n");
|
||||
|
||||
thread_t* thread = sched_newuthread(NULL, PAGE_SIZE*3, NULL, NULL, true, THREAD_PRIORITY_USER);
|
||||
|
||||
proc_t* proc = thread->proc;
|
||||
|
||||
proc->root = vfs_root();
|
||||
proc->cwd = vfs_root();
|
||||
proc->pid = 1;
|
||||
|
||||
vnode_t* node;
|
||||
size_t ret = vfs_open(&node, vfs_root(), "sbin/init");
|
||||
if(ret){
|
||||
printf("Open failed: %s\n", strerror(ret));
|
||||
_panic("Could not load init", 0);
|
||||
}
|
||||
char** argv = {"/sbin/init", ""};
|
||||
char** env = {""};
|
||||
|
||||
ret = elf_load(thread, node, argv, env);
|
||||
|
||||
if(ret){
|
||||
printf("ELF load error: %s\n", strerror(ret));
|
||||
_panic("Could not load init", 0);
|
||||
}
|
||||
switch_thread(thread);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user