[USER] Init libc with arguments and environment
MESS - execve - Clean up and move earlier
This commit is contained in:
parent
8d1e693884
commit
1693baaf6b
@ -4,7 +4,6 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
(void) argc;
|
(void) argc;
|
||||||
(void) argv;
|
(void) argv;
|
||||||
fork();
|
|
||||||
for(;;);
|
for(;;);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include <syscall.h>
|
#include <syscall.h>
|
||||||
#include <cpuid.h>
|
#include <cpuid.h>
|
||||||
|
|
||||||
|
int kernel_execve(process_t *p, void *image, char *argv[], char *envp[]);
|
||||||
int kmain(uint64_t multiboot_magic, void *multiboot_data)
|
int kmain(uint64_t multiboot_magic, void *multiboot_data)
|
||||||
{
|
{
|
||||||
debug_init();
|
debug_init();
|
||||||
@ -29,12 +30,13 @@ int kmain(uint64_t multiboot_magic, void *multiboot_data)
|
|||||||
debug_info("Syscall enabled:%d\n", CPUID_FEATURE_SYSCALL);
|
debug_info("Syscall enabled:%d\n", CPUID_FEATURE_SYSCALL);
|
||||||
|
|
||||||
process_t *p1 = process_spawn(0);
|
process_t *p1 = process_spawn(0);
|
||||||
thread_t *th = exec_elf(p1, mboot_data.init);
|
char *args[] = {"init", "Hello, arg", "5", 0};
|
||||||
|
char *env[] = {"OS=mittos64", 0};
|
||||||
|
thread_t *th = exec_elf(p1, mboot_data.init, args, env);
|
||||||
scheduler_insert(th);
|
scheduler_insert(th);
|
||||||
|
|
||||||
procmm_print_map(p1->mmap);
|
procmm_print_map(p1->mmap);
|
||||||
|
|
||||||
|
|
||||||
asm("sti");
|
asm("sti");
|
||||||
debug_info("BOOT COMPLETE\n");
|
debug_info("BOOT COMPLETE\n");
|
||||||
schedule();
|
schedule();
|
||||||
|
@ -4,4 +4,4 @@
|
|||||||
#include <thread.h>
|
#include <thread.h>
|
||||||
|
|
||||||
void *load_elf(process_t *p, void *data);
|
void *load_elf(process_t *p, void *data);
|
||||||
thread_t *exec_elf(process_t *p, void *image);
|
thread_t *exec_elf(process_t *p, void *image, char *argv[], char *envp[]);
|
||||||
|
@ -62,5 +62,5 @@ void procmm_free_map(process_t *proc);
|
|||||||
void procmm_print_map(procmm_mmap_t *map);
|
void procmm_print_map(procmm_mmap_t *map);
|
||||||
procmm_area_t *procmm_map(procmm_mmap_t *map, uintptr_t start, uintptr_t end, uint64_t flags);
|
procmm_area_t *procmm_map(procmm_mmap_t *map, uintptr_t start, uintptr_t end, uint64_t flags);
|
||||||
void procmm_unmap(procmm_area_t *a);
|
void procmm_unmap(procmm_area_t *a);
|
||||||
uintptr_t procmm_setup(process_t *proc);
|
int procmm_setup(process_t *proc, size_t brk_size);
|
||||||
registers_t *procmm_page_fault(registers_t *r);
|
registers_t *procmm_page_fault(registers_t *r);
|
||||||
|
@ -1,14 +1,80 @@
|
|||||||
#include <elf.h>
|
#include <elf.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
|
#include <mem.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <process.h>
|
||||||
|
|
||||||
thread_t *exec_elf(process_t *p, void *image)
|
thread_t *exec_elf(process_t *p, void *image, char *argv[], char *envp[])
|
||||||
{
|
{
|
||||||
|
int argc = 0, envc=0;
|
||||||
|
// Count strings and save their lengths
|
||||||
|
int len = 0;
|
||||||
|
while(argv[argc]){
|
||||||
|
len += strlen(argv[argc]) + 1;
|
||||||
|
argc++;
|
||||||
|
}
|
||||||
|
len += (argc+1)*sizeof(char *);
|
||||||
|
while(envp[envc])
|
||||||
|
{
|
||||||
|
len += strlen(envp[envc]) + 1;
|
||||||
|
envc++;
|
||||||
|
}
|
||||||
|
len += (envc+1)*sizeof(char *);
|
||||||
|
len += sizeof(size_t)*2;
|
||||||
|
len += sizeof(char);
|
||||||
|
|
||||||
|
// Replace process memory space with new image
|
||||||
void *entry = load_elf(p, image);
|
void *entry = load_elf(p, image);
|
||||||
debug("Address:%x\n", entry);
|
procmm_setup(p, len);
|
||||||
procmm_setup(p);
|
|
||||||
|
// We will write to process memory, so let's switch to it
|
||||||
|
vmm_set_P4(p->mmap->P4);
|
||||||
|
|
||||||
|
|
||||||
|
// Memory layout of area before BRK
|
||||||
|
// argv[]
|
||||||
|
// 0
|
||||||
|
// envp[]
|
||||||
|
// 0
|
||||||
|
// auxv[]
|
||||||
|
// arguments
|
||||||
|
// env strings
|
||||||
|
// 0
|
||||||
|
// BRK points here
|
||||||
|
void *pos = (void *)p->mmap->brk->start;
|
||||||
|
char **_argv = pos;
|
||||||
|
pos = incptr(pos, (argc+1)*sizeof(char *));
|
||||||
|
char **_envp = pos;
|
||||||
|
pos = incptr(pos, (envc+1)*sizeof(char *));
|
||||||
|
size_t *_auxv = pos;
|
||||||
|
pos = incptr(pos, sizeof(size_t)*2);
|
||||||
|
|
||||||
|
// Copy arguments
|
||||||
|
for(int i = 0; i < argc; i++)
|
||||||
|
{
|
||||||
|
size_t len = strlen(argv[i]) + 1;
|
||||||
|
_argv[i] = pos;
|
||||||
|
memcpy(_argv[i], argv[i], len);
|
||||||
|
pos = incptr(pos, len);
|
||||||
|
}
|
||||||
|
_argv[argc] = 0;
|
||||||
|
// Copy environment strings
|
||||||
|
for(int i = 0; i < envc; i++)
|
||||||
|
{
|
||||||
|
size_t len = strlen(envp[i]) + 1;
|
||||||
|
_envp[i] = pos;
|
||||||
|
memcpy(_envp[i], envp[i], len);
|
||||||
|
pos = incptr(pos, len);
|
||||||
|
}
|
||||||
|
_envp[envc] = 0;
|
||||||
|
// No auxiliary vectors for now
|
||||||
|
_auxv[0] = 0;
|
||||||
|
|
||||||
thread_t *th = new_thread((void *)entry, 1);
|
thread_t *th = new_thread((void *)entry, 1);
|
||||||
process_attach(p, th);
|
process_attach(p, th);
|
||||||
|
th->r.rsi = (uint64_t)_argv;
|
||||||
|
th->r.rdi = argc;
|
||||||
|
|
||||||
return th;
|
return th;
|
||||||
}
|
}
|
||||||
|
@ -103,13 +103,13 @@ void procmm_unmap(procmm_area_t *a)
|
|||||||
kfree(a);
|
kfree(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
uintptr_t procmm_setup(process_t *proc)
|
int procmm_setup(process_t *proc, size_t brk_size)
|
||||||
{
|
{
|
||||||
procmm_mmap_t *map = proc->mmap;
|
procmm_mmap_t *map = proc->mmap;
|
||||||
procmm_area_t *last_a = map->areas.prev;
|
procmm_area_t *last_a = map->areas.prev;
|
||||||
|
|
||||||
uintptr_t brk_start = (last_a->end + PAGE_SIZE) & ~(PAGE_SIZE-1);
|
uintptr_t brk_start = (last_a->end + PAGE_SIZE) & ~(PAGE_SIZE-1);
|
||||||
map->brk = procmm_map(map, brk_start, brk_start, 0);
|
map->brk = procmm_map(map, brk_start, brk_start + brk_size, 0);
|
||||||
|
|
||||||
map->stack = procmm_map(map, USERSPACE_TOP, USERSPACE_TOP, 0);
|
map->stack = procmm_map(map, USERSPACE_TOP, USERSPACE_TOP, 0);
|
||||||
|
|
||||||
|
@ -13,10 +13,12 @@
|
|||||||
|
|
||||||
call _init
|
call _init
|
||||||
|
|
||||||
pop rdi
|
|
||||||
pop rsi
|
pop rsi
|
||||||
|
pop rdx
|
||||||
|
movabs rax, offset main
|
||||||
|
mov rdi, rax
|
||||||
|
|
||||||
call main
|
call __libc_start_main
|
||||||
|
|
||||||
call _fini
|
call _fini
|
||||||
|
|
||||||
|
@ -46,8 +46,3 @@ long __syscall_common(long num, ...)
|
|||||||
while(1);
|
while(1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct pthread *__pthread_self()
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
21
libc/thread.c
Normal file
21
libc/thread.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include "syscalls.h"
|
||||||
|
|
||||||
|
struct pthread *__pt;
|
||||||
|
|
||||||
|
struct pthread *__pthread_self()
|
||||||
|
{
|
||||||
|
return __pt;
|
||||||
|
}
|
||||||
|
|
||||||
|
SYSCALL_DEF(set_thread_area)
|
||||||
|
{
|
||||||
|
SYSCALL_INIT(void *, ptr);
|
||||||
|
__pt = ptr;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SYSCALL_DEF(set_tid_address)
|
||||||
|
{
|
||||||
|
SYSCALL_INIT();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user