1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:57:44 +00:00

Everywhere: Make the codebase more architecture aware

This commit is contained in:
Undefine 2022-07-22 20:48:24 +02:00 committed by Brian Gianforcaro
parent 6c4b5775e1
commit 97cc33ca47
22 changed files with 108 additions and 36 deletions

View file

@ -97,11 +97,13 @@ static int create_thread(pthread_t* thread, void* (*entry)(void*), void* argumen
push_on_stack(thread_params->stack_location);
push_on_stack(argument);
push_on_stack((void*)entry);
#else
#elif ARCH(X86_64)
thread_params->rdi = (FlatPtr)entry;
thread_params->rsi = (FlatPtr)argument;
thread_params->rdx = (FlatPtr)thread_params->stack_location;
thread_params->rcx = thread_params->stack_size;
#else
# error Unknown architecture
#endif
VERIFY((uintptr_t)stack % 16 == 0);

View file

@ -130,17 +130,19 @@ int timingsafe_memcmp(void const* b1, void const* b2, size_t len)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/memcpy.html
void* memcpy(void* dest_ptr, void const* src_ptr, size_t n)
{
#if ARCH(I386) || ARCH(X86_64)
void* original_dest = dest_ptr;
asm volatile(
"rep movsb"
: "+D"(dest_ptr), "+S"(src_ptr), "+c"(n)::"memory");
return original_dest;
#else
# error Unknown architecture
#endif
}
#if ARCH(I386)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/memset.html
//
// For x86-64, an optimized ASM implementation is found in ./arch/x86_64/memset.S
void* memset(void* dest_ptr, int c, size_t n)
{
size_t dest = (size_t)dest_ptr;
@ -164,6 +166,10 @@ void* memset(void* dest_ptr, int c, size_t n)
: "memory");
return dest_ptr;
}
#elif ARCH(X86_64)
// For x86-64, an optimized ASM implementation is found in ./arch/x86_64/memset.S
#else
# error Unknown architecture
#endif
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/memmove.html

View file

@ -47,9 +47,11 @@ Backtrace::Backtrace(Reader const& coredump, const ELF::Core::ThreadInfo& thread
#if ARCH(I386)
auto start_bp = m_thread_info.regs.ebp;
auto start_ip = m_thread_info.regs.eip;
#else
#elif ARCH(X86_64)
auto start_bp = m_thread_info.regs.rbp;
auto start_ip = m_thread_info.regs.rip;
#else
# error Unknown architecture
#endif
// In order to provide progress updates, we first have to walk the

View file

@ -169,8 +169,10 @@ NonnullOwnPtrVector<DebugInfo::VariableInfo> DebugInfo::get_variables_in_current
FlatPtr ip;
#if ARCH(I386)
ip = regs.eip;
#else
#elif ARCH(X86_64)
ip = regs.rip;
#else
# error Unknown architecture
#endif
if (ip - m_base_address < scope.address_low || ip - m_base_address >= scope.address_high)
continue;

View file

@ -346,8 +346,10 @@ FlatPtr DebugSession::single_step()
constexpr u32 TRAP_FLAG = 0x100;
#if ARCH(I386)
regs.eflags |= TRAP_FLAG;
#else
#elif ARCH(X86_64)
regs.rflags |= TRAP_FLAG;
#else
# error Unknown architecture
#endif
set_registers(regs);
@ -361,8 +363,10 @@ FlatPtr DebugSession::single_step()
regs = get_registers();
#if ARCH(I386)
regs.eflags &= ~(TRAP_FLAG);
#else
#elif ARCH(X86_64)
regs.rflags &= ~(TRAP_FLAG);
#else
# error Unknown architecture
#endif
set_registers(regs);
return regs.ip();

View file

@ -186,8 +186,10 @@ void DebugSession::run(DesiredInitialDebugeeState initial_debugee_state, Callbac
#if ARCH(I386)
FlatPtr current_instruction = regs.eip;
#else
#elif ARCH(X86_64)
FlatPtr current_instruction = regs.rip;
#else
# error Unknown architecture
#endif
auto debug_status = peek_debug(DEBUG_STATUS_REGISTER);
@ -207,8 +209,10 @@ void DebugSession::run(DesiredInitialDebugeeState initial_debugee_state, Callbac
#if ARCH(I386)
FlatPtr current_ebp = regs.ebp;
#else
#elif ARCH(X86_64)
FlatPtr current_ebp = regs.rbp;
#else
# error Unknown architecture
#endif
do {
@ -253,8 +257,10 @@ void DebugSession::run(DesiredInitialDebugeeState initial_debugee_state, Callbac
auto breakpoint_addr = bit_cast<FlatPtr>(current_breakpoint.value().address);
#if ARCH(I386)
regs.eip = breakpoint_addr;
#else
#elif ARCH(X86_64)
regs.rip = breakpoint_addr;
#else
# error Unknown architecture
#endif
set_registers(regs);
disable_breakpoint(current_breakpoint.value().address);

View file

@ -45,8 +45,10 @@ Optional<FlatPtr> kernel_base()
auto kernel_base_str = String { file.value()->read_all(), NoChomp };
#if ARCH(I386)
using AddressType = u32;
#else
#elif ARCH(X86_64) || ARCH(AARCH64)
using AddressType = u64;
#else
# error Unknown architecture
#endif
auto maybe_kernel_base = kernel_base_str.to_uint<AddressType>();
if (!maybe_kernel_base.has_value()) {

View file

@ -20,8 +20,10 @@ namespace Web {
#if ARCH(I386)
# define CPU_STRING "x86"
#else
#elif ARCH(X86_64)
# define CPU_STRING "x86_64"
#elif ARCH(AARCH64)
# define CPU_STRING "AArch64"
#endif
constexpr auto default_user_agent = "Mozilla/5.0 (SerenityOS; " CPU_STRING ") LibWeb+LibJS/1.0 Browser/1.0";