From 896b919c9d00d568443f42da39cae83dd4eca6fb Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 5 Jun 2023 20:34:08 +0300 Subject: [PATCH] Kernel/LibC: pass environ pointer to process --- kernel/arch/x86_64/Thread.S | 3 ++- kernel/arch/x86_64/crt0.S | 3 +++ kernel/include/kernel/Process.h | 3 ++- kernel/kernel/Process.cpp | 1 + kernel/kernel/Thread.cpp | 4 ++-- libc/unistd.cpp | 5 ++++- 6 files changed, 14 insertions(+), 5 deletions(-) diff --git a/kernel/arch/x86_64/Thread.S b/kernel/arch/x86_64/Thread.S index 3a5a934e90..e5c55ff6ee 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 1f0832603d..dc3fdffc7d 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 a124e6b3ec..dbfd09910d 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 8c0d6c63a4..3d03fce173 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 68ea0dc9a1..8e2d892ec7 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 0d7dd6c4f6..1aa6f9da67 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(); }