Kernel: Factor our PreviousMode into RegisterState::previous_mode

Various places in the kernel were manually checking the cs register for
x86_64, however to share this with aarch64 a function in RegisterState
is added, and the call-sites are updated. While we're here the
PreviousMode enum is renamed to ExecutionMode.
This commit is contained in:
Timon Kruiper 2023-01-08 16:16:08 +01:00 committed by Andreas Kling
parent 247109cee6
commit fb10774862
Notes: sideshowbarker 2024-07-17 07:14:09 +09:00
11 changed files with 53 additions and 29 deletions

View file

@ -292,10 +292,9 @@ void Processor::enter_trap(TrapFrame& trap, bool raise_irq)
auto& current_trap = current_thread->current_trap();
trap.next_trap = current_trap;
current_trap = &trap;
// FIXME: Determine PreviousMode from TrapFrame when userspace programs can run on aarch64
auto new_previous_mode = Thread::PreviousMode::KernelMode;
auto new_previous_mode = trap.regs->previous_mode();
if (current_thread->set_previous_mode(new_previous_mode)) {
current_thread->update_time_scheduled(TimeManagement::scheduler_current_time(), new_previous_mode == Thread::PreviousMode::KernelMode, false);
current_thread->update_time_scheduled(TimeManagement::scheduler_current_time(), new_previous_mode == ExecutionMode::Kernel, false);
}
} else {
trap.next_trap = nullptr;
@ -321,15 +320,14 @@ void Processor::exit_trap(TrapFrame& trap)
if (current_thread) {
auto& current_trap = current_thread->current_trap();
current_trap = trap.next_trap;
Thread::PreviousMode new_previous_mode;
ExecutionMode new_previous_mode;
if (current_trap) {
VERIFY(current_trap->regs);
// FIXME: Determine PreviousMode from TrapFrame when userspace programs can run on aarch64
new_previous_mode = Thread::PreviousMode::KernelMode;
new_previous_mode = current_trap->regs->previous_mode();
} else {
// If we don't have a higher level trap then we're back in user mode.
// Which means that the previous mode prior to being back in user mode was kernel mode
new_previous_mode = Thread::PreviousMode::KernelMode;
new_previous_mode = ExecutionMode::Kernel;
}
if (current_thread->set_previous_mode(new_previous_mode))

View file

@ -8,6 +8,8 @@
#include <LibC/sys/arch/aarch64/regs.h>
#include <Kernel/ExecutionMode.h>
#include <AK/Platform.h>
VALIDATE_IS_AARCH64()
@ -33,6 +35,11 @@ struct RegisterState {
TODO_AARCH64();
}
FlatPtr bp() const { TODO_AARCH64(); }
ExecutionMode previous_mode() const
{
return ((spsr_el1 & 0b1111) == 0) ? ExecutionMode::User : ExecutionMode::Kernel;
}
};
inline void copy_kernel_registers_into_ptrace_registers(PtraceRegisters& ptrace_regs, RegisterState const& kernel_regs)

View file

@ -19,7 +19,7 @@ void handle_crash(Kernel::RegisterState const& regs, char const* description, in
if (!current_thread)
PANIC("{} with !Thread::current()", description);
auto crashed_in_kernel = (regs.cs & 3) == 0;
auto crashed_in_kernel = regs.previous_mode() == ExecutionMode::Kernel;
if (!crashed_in_kernel && current_thread->has_signal_handler(signal) && !current_thread->should_ignore_signal(signal) && !current_thread->is_signal_masked(signal)) {
current_thread->send_urgent_signal_to_self(signal);
return;
@ -31,7 +31,7 @@ void handle_crash(Kernel::RegisterState const& regs, char const* description, in
// make sure we switch back to the right page tables.
Memory::MemoryManager::enter_process_address_space(process);
dmesgln("CRASH: CPU #{} {} in ring {}", Processor::current_id(), description, (regs.cs & 3));
dmesgln("CRASH: CPU #{} {} in {}", Processor::current_id(), description, regs.previous_mode() == ExecutionMode::Kernel ? "kernel"sv : "userspace"sv);
dump_registers(regs);
if (crashed_in_kernel) {

View file

@ -918,10 +918,10 @@ void Processor::enter_trap(TrapFrame& trap, bool raise_irq)
auto& current_trap = current_thread->current_trap();
trap.next_trap = current_trap;
current_trap = &trap;
// The cs register of this trap tells us where we will return back to
auto new_previous_mode = ((trap.regs->cs & 3) != 0) ? Thread::PreviousMode::UserMode : Thread::PreviousMode::KernelMode;
auto new_previous_mode = trap.regs->previous_mode();
if (current_thread->set_previous_mode(new_previous_mode) && trap.prev_irq_level == 0) {
current_thread->update_time_scheduled(TimeManagement::scheduler_current_time(), new_previous_mode == Thread::PreviousMode::KernelMode, false);
current_thread->update_time_scheduled(TimeManagement::scheduler_current_time(), new_previous_mode == ExecutionMode::Kernel, false);
}
} else {
trap.next_trap = nullptr;
@ -954,17 +954,16 @@ void Processor::exit_trap(TrapFrame& trap)
if (current_thread) {
auto& current_trap = current_thread->current_trap();
current_trap = trap.next_trap;
Thread::PreviousMode new_previous_mode;
ExecutionMode new_previous_mode;
if (current_trap) {
VERIFY(current_trap->regs);
// If we have another higher level trap then we probably returned
// from an interrupt or irq handler. The cs register of the
// new/higher level trap tells us what the mode prior to it was
new_previous_mode = ((current_trap->regs->cs & 3) != 0) ? Thread::PreviousMode::UserMode : Thread::PreviousMode::KernelMode;
// from an interrupt or irq handler.
new_previous_mode = current_trap->regs->previous_mode();
} else {
// If we don't have a higher level trap then we're back in user mode.
// Which means that the previous mode prior to being back in user mode was kernel mode
new_previous_mode = Thread::PreviousMode::KernelMode;
new_previous_mode = ExecutionMode::Kernel;
}
if (current_thread->set_previous_mode(new_previous_mode))

View file

@ -11,6 +11,7 @@
#include <Kernel/Arch/CPU.h>
#include <Kernel/Arch/x86_64/ASM_wrapper.h>
#include <Kernel/ExecutionMode.h>
#include <AK/Platform.h>
VALIDATE_IS_X86()
@ -68,6 +69,11 @@ struct [[gnu::packed]] RegisterState {
arg3 = rbx;
arg4 = rsi;
}
ExecutionMode previous_mode() const
{
return ((cs & 3) != 0) ? ExecutionMode::User : ExecutionMode::Kernel;
}
};
#define REGISTER_STATE_SIZE (22 * 8)

18
Kernel/ExecutionMode.h Normal file
View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
namespace Kernel {
enum class ExecutionMode : u8 {
Kernel = 0,
User,
};
}

View file

@ -427,7 +427,7 @@ void Scheduler::timer_tick(RegisterState const& regs)
current_thread->update_time_scheduled(TimeManagement::scheduler_current_time(), true, false);
}
if (current_thread->previous_mode() == Thread::PreviousMode::UserMode && current_thread->should_die() && !current_thread->is_blocked()) {
if (current_thread->previous_mode() == ExecutionMode::User && current_thread->should_die() && !current_thread->is_blocked()) {
SpinlockLocker scheduler_lock(g_scheduler_lock);
dbgln_if(SCHEDULER_DEBUG, "Scheduler[{}]: Terminating user mode thread {}", Processor::current_id(), *current_thread);
current_thread->set_state(Thread::State::Dying);

View file

@ -145,7 +145,7 @@ NEVER_INLINE void syscall_handler(TrapFrame* trap)
auto& regs = *trap->regs;
auto* current_thread = Thread::current();
VERIFY(current_thread->previous_mode() == Thread::PreviousMode::UserMode);
VERIFY(current_thread->previous_mode() == ExecutionMode::User);
auto& process = current_thread->process();
if (process.is_dying()) {
// It's possible this thread is just about to make a syscall while another is
@ -205,7 +205,7 @@ NEVER_INLINE void syscall_handler(TrapFrame* trap)
current_thread->check_dispatch_pending_signal();
// If the previous mode somehow changed something is seriously messed up...
VERIFY(current_thread->previous_mode() == Thread::PreviousMode::UserMode);
VERIFY(current_thread->previous_mode() == ExecutionMode::User);
// Check if we're supposed to return to userspace or just die.
current_thread->die_if_needed();

View file

@ -14,7 +14,7 @@ namespace Kernel {
void Process::sys$exit(int status)
{
// FIXME: We have callers from kernel which don't acquire the big process lock.
if (Thread::current()->previous_mode() == Thread::PreviousMode::UserMode) {
if (Thread::current()->previous_mode() == ExecutionMode::User) {
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
}

View file

@ -631,7 +631,7 @@ void Thread::update_time_scheduled(u64 current_scheduler_time, bool is_kernel, b
bool Thread::tick()
{
if (previous_mode() == PreviousMode::KernelMode) {
if (previous_mode() == ExecutionMode::Kernel) {
++m_process->m_ticks_in_kernel;
++m_ticks_in_kernel;
} else {

View file

@ -987,12 +987,8 @@ public:
u64 time_in_user() const { return m_total_time_scheduled_user.load(AK::MemoryOrder::memory_order_relaxed); }
u64 time_in_kernel() const { return m_total_time_scheduled_kernel.load(AK::MemoryOrder::memory_order_relaxed); }
enum class PreviousMode : u8 {
KernelMode = 0,
UserMode
};
PreviousMode previous_mode() const { return m_previous_mode; }
bool set_previous_mode(PreviousMode mode)
ExecutionMode previous_mode() const { return m_previous_mode; }
bool set_previous_mode(ExecutionMode mode)
{
if (m_previous_mode == mode)
return false;
@ -1215,7 +1211,7 @@ private:
Atomic<bool, AK::MemoryOrder::memory_order_relaxed> m_is_active { false };
bool m_is_joinable { true };
bool m_handling_page_fault { false };
PreviousMode m_previous_mode { PreviousMode::KernelMode }; // We always start out in kernel mode
ExecutionMode m_previous_mode { ExecutionMode::Kernel }; // We always start out in kernel mode
unsigned m_syscall_count { 0 };
unsigned m_inode_faults { 0 };