Kernel: Move ThreadRegisters into arch-specific directory

These are architecture-specific anyway, so they belong in the Arch
directory. This commit also adds ThreadRegisters::set_initial_state to
factor out the logic in Thread.cpp.
This commit is contained in:
Timon Kruiper 2022-12-27 14:04:07 +01:00 committed by Andrew Kaster
parent 0d2dffb95b
commit a3cbaa3449
6 changed files with 133 additions and 75 deletions

View file

@ -0,0 +1,17 @@
/*
* Copyright (c) 2022, Timon Kruiper <timonkruiper@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Platform.h>
#if ARCH(X86_64)
# include <Kernel/Arch/x86_64/ThreadRegisters.h>
#elif ARCH(AARCH64)
# include <Kernel/Arch/aarch64/ThreadRegisters.h>
#else
# error "Unknown architecture"
#endif

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2022, Timon Kruiper <timonkruiper@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
#include <Kernel/Memory/AddressSpace.h>
namespace Kernel {
struct ThreadRegisters {
u64 x[31];
u64 elr_el1;
u64 sp_el0;
FlatPtr ip() const { return elr_el1; }
void set_ip(FlatPtr value) { elr_el1 = value; }
void set_sp(FlatPtr value) { sp_el0 = value; }
void set_initial_state(bool, Memory::AddressSpace&, FlatPtr kernel_stack_top)
{
set_sp(kernel_stack_top);
}
};
}

View file

@ -0,0 +1,78 @@
/*
* Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Platform.h>
#include <AK/Types.h>
#include <Kernel/Memory/AddressSpace.h>
namespace Kernel {
struct ThreadRegisters {
FlatPtr rdi;
FlatPtr rsi;
FlatPtr rbp;
FlatPtr rsp;
FlatPtr rbx;
FlatPtr rdx;
FlatPtr rcx;
FlatPtr rax;
FlatPtr r8;
FlatPtr r9;
FlatPtr r10;
FlatPtr r11;
FlatPtr r12;
FlatPtr r13;
FlatPtr r14;
FlatPtr r15;
FlatPtr rip;
FlatPtr rsp0;
FlatPtr cs;
FlatPtr rflags;
FlatPtr flags() const { return rflags; }
void set_flags(FlatPtr value) { rflags = value; }
void set_sp(FlatPtr value) { rsp = value; }
void set_sp0(FlatPtr value) { rsp0 = value; }
void set_ip(FlatPtr value) { rip = value; }
FlatPtr cr3;
FlatPtr ip() const
{
return rip;
}
FlatPtr sp() const
{
return rsp;
}
void set_initial_state(bool is_kernel_process, Memory::AddressSpace& space, FlatPtr kernel_stack_top)
{
// Only IF is set when a process boots.
set_flags(0x0202);
if (is_kernel_process)
cs = GDT_SELECTOR_CODE0;
else
cs = GDT_SELECTOR_CODE3 | 3;
cr3 = space.page_directory().cr3();
if (is_kernel_process) {
set_sp(kernel_stack_top);
set_sp0(kernel_stack_top);
} else {
// Ring 3 processes get a separate stack for ring 0.
// The ring 3 stack will be assigned by exec().
set_sp0(kernel_stack_top);
}
}
};
}

View file

@ -55,15 +55,17 @@ ErrorOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<Sys
auto& regs = thread->regs();
regs.set_ip((FlatPtr)entry);
regs.set_flags(0x0202);
regs.set_sp(user_sp.value());
#if ARCH(X86_64)
regs.set_flags(0x0202);
regs.cr3 = address_space().with([](auto& space) { return space->page_directory().cr3(); });
regs.rdi = params.rdi;
regs.rsi = params.rsi;
regs.rdx = params.rdx;
regs.rcx = params.rcx;
#endif
regs.cr3 = address_space().with([](auto& space) { return space->page_directory().cr3(); });
TRY(thread->make_thread_specific_region({}));

View file

@ -77,33 +77,12 @@ Thread::Thread(NonnullLockRefPtr<Process> process, NonnullOwnPtr<Memory::Region>
reset_fpu_state();
// Only IF is set when a process boots.
m_regs.set_flags(0x0202);
#if ARCH(X86_64)
if (m_process->is_kernel_process())
m_regs.cs = GDT_SELECTOR_CODE0;
else
m_regs.cs = GDT_SELECTOR_CODE3 | 3;
#elif ARCH(AARCH64)
TODO_AARCH64();
#else
# error Unknown architecture
#endif
m_regs.cr3 = m_process->address_space().with([](auto& space) { return space->page_directory().cr3(); });
m_kernel_stack_base = m_kernel_stack_region->vaddr().get();
m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & ~(FlatPtr)0x7u;
if (m_process->is_kernel_process()) {
m_regs.set_sp(m_kernel_stack_top);
m_regs.set_sp0(m_kernel_stack_top);
} else {
// Ring 3 processes get a separate stack for ring 0.
// The ring 3 stack will be assigned by exec().
m_regs.set_sp0(m_kernel_stack_top);
}
m_process->address_space().with([&](auto& space) {
m_regs.set_initial_state(m_process->is_kernel_process(), *space, m_kernel_stack_top);
});
// We need to add another reference if we could successfully create
// all the resources needed for this thread. The reason for this is that

View file

@ -17,6 +17,7 @@
#include <AK/Vector.h>
#include <Kernel/API/POSIX/sched.h>
#include <Kernel/Arch/RegisterState.h>
#include <Kernel/Arch/ThreadRegisters.h>
#include <Kernel/Debug.h>
#include <Kernel/Forward.h>
#include <Kernel/KString.h>
@ -49,55 +50,6 @@ struct ThreadSpecificData {
#define THREAD_AFFINITY_DEFAULT 0xffffffff
struct ThreadRegisters {
#if ARCH(X86_64)
FlatPtr rdi;
FlatPtr rsi;
FlatPtr rbp;
FlatPtr rsp;
FlatPtr rbx;
FlatPtr rdx;
FlatPtr rcx;
FlatPtr rax;
FlatPtr r8;
FlatPtr r9;
FlatPtr r10;
FlatPtr r11;
FlatPtr r12;
FlatPtr r13;
FlatPtr r14;
FlatPtr r15;
FlatPtr rip;
FlatPtr rsp0;
#endif
FlatPtr cs;
#if ARCH(X86_64)
FlatPtr rflags;
FlatPtr flags() const { return rflags; }
void set_flags(FlatPtr value) { rflags = value; }
void set_sp(FlatPtr value) { rsp = value; }
void set_sp0(FlatPtr value) { rsp0 = value; }
void set_ip(FlatPtr value) { rip = value; }
#endif
FlatPtr cr3;
FlatPtr ip() const
{
#if ARCH(X86_64)
return rip;
#endif
}
FlatPtr sp() const
{
#if ARCH(X86_64)
return rsp;
#endif
}
};
class Thread
: public ListedRefCounted<Thread, LockType::Spinlock>
, public LockWeakable<Thread> {