[kernel] Stop creating user stacks in the kernel

Stop creating stacks in user space for user threads, that should be done
by the thread's creator. This change adds process and stack_top
arguments to the thread_create syscall, so that threads can be created
in other processes, and given a stack address.

Also included is a fix in add_thunk_user due to the r11/flags change.

THIS COMMIT BREAKS USERSPACE. See subsequent commits for the user side
changes related to this change.
This commit is contained in:
Justin C. Miller
2021-12-26 15:36:59 -08:00
parent cade24a7ce
commit 300bf9c2c5
6 changed files with 24 additions and 27 deletions

View File

@@ -86,7 +86,7 @@ process::update()
}
thread *
process::create_thread(uint8_t priority, bool user)
process::create_thread(uintptr_t rsp3, uint8_t priority)
{
if (priority == default_priority)
priority = scheduler::default_priority;
@@ -94,17 +94,8 @@ process::create_thread(uint8_t priority, bool user)
thread *th = new thread(*this, priority);
kassert(th, "Failed to create thread!");
if (user) {
uintptr_t stack_top = stacks_top - (m_threads.count() * stack_size);
vm_flags flags = vm_flags::zero|vm_flags::write;
vm_area *vma = new vm_area_open(stack_size, flags);
m_space.add(stack_top - stack_size, vma);
// Space for null frame - because the page gets zeroed on
// allocation, just pointing rsp here does the trick
th->tcb()->rsp3 = stack_top - 2 * sizeof(uint64_t);
}
if (rsp3)
th->tcb()->rsp3 = rsp3;
m_threads.append(th);
scheduler::get().add_thread(th->tcb());

View File

@@ -43,10 +43,10 @@ public:
vm_space & space() { return m_space; }
/// Create a new thread in this process
/// \args rsp3 If non-zero, sets the ring3 stack pointer to this value
/// \args priority The new thread's scheduling priority
/// \args user If true, create a userspace stack for this thread
/// \returns The newly created thread object
thread * create_thread(uint8_t priorty = default_priority, bool user = true);
thread * create_thread(uintptr_t rsp3 = 0, uint8_t priorty = default_priority);
/// Start tracking an object with a handle.
/// \args obj The object this handle refers to

View File

@@ -172,14 +172,15 @@ thread::add_thunk_user(uintptr_t rip3, uintptr_t rip0, uint64_t flags)
// b) come out of the kernel/user trampoline and start executing
// in user mode at rip
m_tcb.rsp -= sizeof(uintptr_t) * 8;
uintptr_t *stack = reinterpret_cast<uintptr_t*>(m_tcb.rsp);
flags |= 0x200;
m_tcb.rflags3 = flags;
stack[7] = rip3; // return rip in rcx
stack[6] = m_tcb.rsp3; // rbp
stack[5] = 0xbbbbbbbb; // rbx
stack[4] = flags; // r11 sets RFLAGS
m_tcb.rsp -= sizeof(uintptr_t) * 7;
uintptr_t *stack = reinterpret_cast<uintptr_t*>(m_tcb.rsp);
stack[6] = rip3; // return rip in rcx
stack[5] = m_tcb.rsp3; // rbp
stack[4] = 0xbbbbbbbb; // rbx
stack[3] = 0x12121212; // r12
stack[2] = 0x13131313; // r13
stack[1] = 0x14141414; // r14