mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:37:35 +00:00
Kernel: Push ARCH specific ifdef's down into RegisterState functions
The non CPU specific code of the kernel shouldn't need to deal with architecture specific registers, and should instead deal with an abstract view of the machine. This allows us to remove a variety of architecture specific ifdefs and helps keep the code slightly more portable. We do this by exposing the abstract representation of instruction pointer, stack pointer, base pointer, return register, etc on the RegisterState struct.
This commit is contained in:
parent
ff8429a749
commit
1cffecbe8d
9 changed files with 119 additions and 97 deletions
|
@ -67,6 +67,75 @@ struct [[gnu::packed]] RegisterState {
|
|||
FlatPtr userspace_rsp;
|
||||
FlatPtr userspace_ss;
|
||||
#endif
|
||||
|
||||
FlatPtr userspace_sp() const
|
||||
{
|
||||
#if ARCH(I386)
|
||||
return userspace_esp;
|
||||
#else
|
||||
return userspace_rsp;
|
||||
#endif
|
||||
}
|
||||
|
||||
FlatPtr ip() const
|
||||
{
|
||||
#if ARCH(I386)
|
||||
return eip;
|
||||
#else
|
||||
return rip;
|
||||
#endif
|
||||
}
|
||||
|
||||
FlatPtr bp() const
|
||||
{
|
||||
#if ARCH(I386)
|
||||
return ebp;
|
||||
#else
|
||||
return rbp;
|
||||
#endif
|
||||
}
|
||||
|
||||
FlatPtr flags() const
|
||||
{
|
||||
#if ARCH(I386)
|
||||
return eflags;
|
||||
#else
|
||||
return rflags;
|
||||
#endif
|
||||
}
|
||||
|
||||
void capture_syscall_params(FlatPtr& function, FlatPtr& arg1, FlatPtr& arg2, FlatPtr& arg3) const
|
||||
{
|
||||
#if ARCH(I386)
|
||||
function = eax;
|
||||
arg1 = edx;
|
||||
arg2 = ecx;
|
||||
arg3 = ebx;
|
||||
#else
|
||||
function = rax;
|
||||
arg1 = rdx;
|
||||
arg2 = rcx;
|
||||
arg3 = rbx;
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_ip_reg(FlatPtr value)
|
||||
{
|
||||
#if ARCH(I386)
|
||||
eip = value;
|
||||
#else
|
||||
rip = value;
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_return_reg(FlatPtr value)
|
||||
{
|
||||
#if ARCH(I386)
|
||||
eax = value;
|
||||
#else
|
||||
rax = value;
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
#if ARCH(I386)
|
||||
|
|
|
@ -228,13 +228,7 @@ void handle_crash(RegisterState& regs, const char* description, int signal, bool
|
|||
PANIC("Crash in ring 0");
|
||||
}
|
||||
|
||||
FlatPtr ip;
|
||||
#if ARCH(I386)
|
||||
ip = regs.eip;
|
||||
#else
|
||||
ip = regs.rip;
|
||||
#endif
|
||||
process->crash(signal, ip, out_of_memory);
|
||||
process->crash(signal, regs.ip(), out_of_memory);
|
||||
}
|
||||
|
||||
EH_ENTRY_NO_CODE(6, illegal_instruction);
|
||||
|
@ -316,12 +310,7 @@ void page_fault_handler(TrapFrame* trap)
|
|||
current_thread->set_handling_page_fault(false);
|
||||
};
|
||||
|
||||
VirtualAddress userspace_sp;
|
||||
#if ARCH(I386)
|
||||
userspace_sp = VirtualAddress { regs.userspace_esp };
|
||||
#else
|
||||
userspace_sp = VirtualAddress { regs.userspace_rsp };
|
||||
#endif
|
||||
VirtualAddress userspace_sp = VirtualAddress { regs.userspace_sp() };
|
||||
if (!faulted_in_kernel && !MM.validate_user_stack(current_thread->process(), userspace_sp)) {
|
||||
dbgln("Invalid stack pointer: {}", userspace_sp);
|
||||
handle_crash(regs, "Bad stack on page fault", SIGSTKFLT);
|
||||
|
|
|
@ -546,14 +546,7 @@ Vector<FlatPtr> Processor::capture_stack_trace(Thread& thread, size_t max_frames
|
|||
// to be ebp.
|
||||
ProcessPagingScope paging_scope(thread.process());
|
||||
auto& regs = thread.regs();
|
||||
FlatPtr* stack_top;
|
||||
FlatPtr sp;
|
||||
#if ARCH(I386)
|
||||
sp = regs.esp;
|
||||
#else
|
||||
sp = regs.rsp;
|
||||
#endif
|
||||
stack_top = reinterpret_cast<FlatPtr*>(sp);
|
||||
FlatPtr* stack_top = reinterpret_cast<FlatPtr*>(regs.sp());
|
||||
if (is_user_range(VirtualAddress(stack_top), sizeof(FlatPtr))) {
|
||||
if (!copy_from_user(&frame_ptr, &((FlatPtr*)stack_top)[0]))
|
||||
frame_ptr = 0;
|
||||
|
@ -562,11 +555,9 @@ Vector<FlatPtr> Processor::capture_stack_trace(Thread& thread, size_t max_frames
|
|||
if (!safe_memcpy(&frame_ptr, &((FlatPtr*)stack_top)[0], sizeof(FlatPtr), fault_at))
|
||||
frame_ptr = 0;
|
||||
}
|
||||
#if ARCH(I386)
|
||||
ip = regs.eip;
|
||||
#else
|
||||
ip = regs.rip;
|
||||
#endif
|
||||
|
||||
ip = regs.ip();
|
||||
|
||||
// TODO: We need to leave the scheduler lock here, but we also
|
||||
// need to prevent the target thread from being run while
|
||||
// we walk the stack
|
||||
|
@ -1222,12 +1213,7 @@ extern "C" void context_first_init([[maybe_unused]] Thread* from_thread, [[maybe
|
|||
// the scheduler lock. We don't want to enable interrupts at this point
|
||||
// as we're still in the middle of a context switch. Doing so could
|
||||
// trigger a context switch within a context switch, leading to a crash.
|
||||
FlatPtr flags;
|
||||
#if ARCH(I386)
|
||||
flags = trap->regs->eflags;
|
||||
#else
|
||||
flags = trap->regs->rflags;
|
||||
#endif
|
||||
FlatPtr flags = trap->regs->flags();
|
||||
Scheduler::leave_on_first_switch(flags & ~0x200);
|
||||
}
|
||||
|
||||
|
|
|
@ -261,12 +261,8 @@ NEVER_INLINE Optional<bool> safe_atomic_compare_exchange_relaxed(volatile u32* v
|
|||
|
||||
bool handle_safe_access_fault(RegisterState& regs, FlatPtr fault_address)
|
||||
{
|
||||
FlatPtr ip;
|
||||
#if ARCH(I386)
|
||||
ip = regs.eip;
|
||||
#else
|
||||
ip = regs.rip;
|
||||
#endif
|
||||
FlatPtr ip = regs.ip();
|
||||
;
|
||||
if (ip >= (FlatPtr)&start_of_safemem_text && ip < (FlatPtr)&end_of_safemem_text) {
|
||||
// If we detect that the fault happened in safe_memcpy() safe_strnlen(),
|
||||
// or safe_memset() then resume at the appropriate _faulted label
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue