diff --git a/kernel/arch/x86_64/Thread.S b/kernel/arch/x86_64/Thread.S index 3a5a934e..e5c55ff6 100644 --- a/kernel/arch/x86_64/Thread.S +++ b/kernel/arch/x86_64/Thread.S @@ -25,7 +25,7 @@ continue_thread: movq $0, %rax jmp *%rsi -# void thread_userspace_trampoline(uint64_t rsp, uint64_t rip, int argc, char** argv) +# void thread_userspace_trampoline(uint64_t rsp, uint64_t rip, int argc, char** argv, char** envp) .global thread_userspace_trampoline thread_userspace_trampoline: pushq $0x23 @@ -35,4 +35,5 @@ thread_userspace_trampoline: pushq %rsi movq %rdx, %rdi movq %rcx, %rsi + movq %r8, %rdx iretq diff --git a/kernel/arch/x86_64/crt0.S b/kernel/arch/x86_64/crt0.S index 1f083260..dc3fdffc 100644 --- a/kernel/arch/x86_64/crt0.S +++ b/kernel/arch/x86_64/crt0.S @@ -9,10 +9,12 @@ _start: movq %rsp, %rbp # We need those in a moment when we call main. + pushq %rdx pushq %rsi pushq %rdi # Prepare signals, memory allocation, stdio and such. + movq %rdx, %rdi call _init_libc # Run the global constructors. @@ -21,6 +23,7 @@ _start: # Restore argc and argv. popq %rdi popq %rsi + popq %rdx # Run main call main diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index a124e6b3..dbfd0991 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -31,7 +31,8 @@ namespace Kernel { uintptr_t entry { 0 }; int argc { 0 }; - char** argv { 0 }; + char** argv { nullptr }; + char** envp { nullptr }; }; public: diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 8c0d6c63..3d03fce1 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -78,6 +78,7 @@ namespace Kernel process->m_userspace_entry.argc = 1; process->m_userspace_entry.argv = argv; + process->m_userspace_entry.envp = (char**)0x69696969; process->m_userspace_entry.entry = elf->file_header_native().e_entry; delete elf; diff --git a/kernel/kernel/Thread.cpp b/kernel/kernel/Thread.cpp index 68ea0dc9..8e2d892e 100644 --- a/kernel/kernel/Thread.cpp +++ b/kernel/kernel/Thread.cpp @@ -9,7 +9,7 @@ namespace Kernel { - extern "C" void thread_userspace_trampoline(uint64_t rsp, uint64_t rip, int argc, char** argv); + extern "C" void thread_userspace_trampoline(uint64_t rsp, uint64_t rip, int argc, char** argv, char** envp); extern "C" uintptr_t read_rip(); template @@ -131,7 +131,7 @@ namespace Kernel [](void*) { const auto& entry = Process::current().userspace_entry(); - thread_userspace_trampoline(Thread::current().rsp(), entry.entry, entry.argc, entry.argv); + thread_userspace_trampoline(Thread::current().rsp(), entry.entry, entry.argc, entry.argv, entry.envp); ASSERT_NOT_REACHED(); } ); diff --git a/libc/unistd.cpp b/libc/unistd.cpp index 0d7dd6c4..1aa6f9da 100644 --- a/libc/unistd.cpp +++ b/libc/unistd.cpp @@ -9,10 +9,13 @@ #include #include +char** environ; + extern "C" void _init_stdio(); -extern "C" void _init_libc() +extern "C" void _init_libc(char** _environ) { + environ = _environ; _init_stdio(); }