Kernel: Rename rsp->sp and rip->ip

This makes more sense if we support i386
This commit is contained in:
Bananymous 2024-03-22 14:48:33 +02:00
parent 3e4d410646
commit fe17958b9f
13 changed files with 108 additions and 112 deletions

View File

@ -3,7 +3,7 @@ sys_fork_trampoline:
subl $4, %esp
pushl %ebx
pushl %ebp
call read_rip
call read_ip
testl %eax, %eax
je .done
subl $8, %esp

View File

@ -1,6 +1,6 @@
# uint32_t read_rip()
.global read_rip
read_rip:
# uint32_t read_ip()
.global read_ip
read_ip:
popl %eax
jmp *%eax
@ -9,7 +9,7 @@ exit_thread_trampoline:
pushl (%esp)
ret
# void start_thread(uint32_t esp, uint32_t eip)
# void start_thread(uint32_t sp, uint32_t ip)
.global start_thread
start_thread:
movl 8(%esp), %ecx
@ -19,7 +19,7 @@ start_thread:
sti
jmp *%ecx
# void continue_thread(uint32_t rsp, uint32_t rip)
# void continue_thread(uint32_t sp, uint32_t ip)
.global continue_thread
continue_thread:
movl 8(%esp), %ecx
@ -27,7 +27,7 @@ continue_thread:
movl $0, %eax
jmp *%ecx
# void thread_jump_userspace(uint32_t rsp, uint32_t rip)
# void thread_jump_userspace(uint32_t sp, uint32_t ip)
.global thread_jump_userspace
thread_jump_userspace:
movl $0x23, %eax

View File

@ -161,24 +161,24 @@ namespace Kernel
if (tid)
{
Thread::current().set_return_rsp(interrupt_stack.rsp);
Thread::current().set_return_rip(interrupt_stack.rip);
Thread::current().set_return_sp(interrupt_stack.sp);
Thread::current().set_return_ip(interrupt_stack.ip);
if (isr == ISR::PageFault)
{
// Check if stack is OOB
auto& stack = Thread::current().stack();
auto& istack = Thread::current().interrupt_stack();
if (stack.vaddr() < interrupt_stack.rsp && interrupt_stack.rsp <= stack.vaddr() + stack.size())
if (stack.vaddr() < interrupt_stack.sp && interrupt_stack.sp <= stack.vaddr() + stack.size())
; // using normal stack
else if (istack.vaddr() < interrupt_stack.rsp && interrupt_stack.rsp <= istack.vaddr() + istack.size())
else if (istack.vaddr() < interrupt_stack.sp && interrupt_stack.sp <= istack.vaddr() + istack.size())
; // using interrupt stack
else
{
derrorln("Stack pointer out of bounds!");
derrorln("rip {H}", interrupt_stack.rip);
derrorln("rip {H}", interrupt_stack.ip);
derrorln("rsp {H}, stack {H}->{H}, istack {H}->{H}",
interrupt_stack.rsp,
interrupt_stack.sp,
stack.vaddr(), stack.vaddr() + stack.size(),
istack.vaddr(), istack.vaddr() + istack.size()
);
@ -225,9 +225,9 @@ namespace Kernel
#endif
}
if (PageTable::current().get_page_flags(interrupt_stack.rip & PAGE_ADDR_MASK) & PageTable::Flags::Present)
if (PageTable::current().get_page_flags(interrupt_stack.ip & PAGE_ADDR_MASK) & PageTable::Flags::Present)
{
auto* machine_code = (const uint8_t*)interrupt_stack.rip;
auto* machine_code = (const uint8_t*)interrupt_stack.ip;
dwarnln("While executing: {2H}{2H}{2H}{2H}{2H}{2H}{2H}{2H}",
machine_code[0],
machine_code[1],
@ -308,8 +308,8 @@ done:
if (Scheduler::current_tid())
{
Thread::current().set_return_rsp(interrupt_stack.rsp);
Thread::current().set_return_rip(interrupt_stack.rip);
Thread::current().set_return_sp(interrupt_stack.sp);
Thread::current().set_return_ip(interrupt_stack.ip);
}
if (!InterruptController::get().is_in_service(irq))

View File

@ -6,7 +6,7 @@ sys_fork_trampoline:
pushq %r13
pushq %r14
pushq %r15
call read_rip
call read_ip
testq %rax, %rax
je .done
movq %rax, %rsi

View File

@ -1,6 +1,6 @@
# uint64_t read_rip()
.global read_rip
read_rip:
# uint64_t read_()
.global read_ip
read_ip:
popq %rax
jmp *%rax
@ -8,7 +8,7 @@ exit_thread_trampoline:
movq 8(%rsp), %rdi
ret
# void start_thread(uint64_t rsp, uint64_t rip)
# void start_thread(uint64_t sp, uint64_t ip)
.global start_thread
start_thread:
movq %rdi, %rsp
@ -18,14 +18,14 @@ start_thread:
sti
jmp *%rsi
# void continue_thread(uint64_t rsp, uint64_t rip)
# void continue_thread(uint64_t sp, uint64_t ip)
.global continue_thread
continue_thread:
movq %rdi, %rsp
movq $0, %rax
jmp *%rsi
# void thread_userspace_trampoline(uint64_t rsp, uint64_t rip, int argc, char** argv, char** envp)
# void thread_userspace_trampoline(uint64_t sp, uint64_t ip, int argc, char** argv, char** envp)
.global thread_userspace_trampoline
thread_userspace_trampoline:
pushq $0x23

View File

@ -21,8 +21,4 @@
#include <stdint.h>
#ifdef __cplusplus
extern "C" uintptr_t read_rip();
#else
extern uintptr_t read_rip();
#endif
extern "C" uintptr_t read_ip();

View File

@ -7,11 +7,11 @@ namespace Kernel
struct InterruptStack
{
uint64_t rip;
uint64_t cs;
uint64_t flags;
uint64_t rsp;
uint64_t ss;
uintptr_t ip;
uintptr_t cs;
uintptr_t flags;
uintptr_t sp;
uintptr_t ss;
};
}

View File

@ -33,7 +33,7 @@ namespace Kernel
static BAN::ErrorOr<Thread*> create_userspace(Process*);
~Thread();
BAN::ErrorOr<Thread*> clone(Process*, uintptr_t rsp, uintptr_t rip);
BAN::ErrorOr<Thread*> clone(Process*, uintptr_t sp, uintptr_t ip);
void setup_exec();
void setup_process_cleanup();
@ -52,17 +52,17 @@ namespace Kernel
BAN::ErrorOr<void> block_or_eintr_or_timeout(Semaphore& semaphore, uint64_t timeout_ms, bool etimedout);
BAN::ErrorOr<void> block_or_eintr_or_waketime(Semaphore& semaphore, uint64_t wake_time_ms, bool etimedout);
void set_return_rsp(uintptr_t& rsp) { m_return_rsp = &rsp; }
void set_return_rip(uintptr_t& rip) { m_return_rip = &rip; }
uintptr_t return_rsp() { ASSERT(m_return_rsp); return *m_return_rsp; }
uintptr_t return_rip() { ASSERT(m_return_rip); return *m_return_rip; }
void set_return_sp(uintptr_t& sp) { m_return_sp = &sp; }
void set_return_ip(uintptr_t& ip) { m_return_ip = &ip; }
uintptr_t return_sp() { ASSERT(m_return_sp); return *m_return_sp; }
uintptr_t return_ip() { ASSERT(m_return_ip); return *m_return_ip; }
pid_t tid() const { return m_tid; }
void set_rsp(uintptr_t rsp) { m_rsp = rsp; validate_stack(); }
void set_rip(uintptr_t rip) { m_rip = rip; }
uintptr_t rsp() const { return m_rsp; }
uintptr_t rip() const { return m_rip; }
void set_sp(uintptr_t sp) { m_sp = sp; validate_stack(); }
void set_ip(uintptr_t ip) { m_ip = ip; }
uintptr_t sp() const { return m_sp; }
uintptr_t ip() const { return m_ip; }
void set_started() { ASSERT(m_state == State::NotStarted); m_state = State::Executing; }
State state() const { return m_state; }
@ -104,15 +104,15 @@ namespace Kernel
static constexpr size_t m_interrupt_stack_size = PAGE_SIZE * 2;
BAN::UniqPtr<VirtualRange> m_interrupt_stack;
BAN::UniqPtr<VirtualRange> m_stack;
uintptr_t m_rip { 0 };
uintptr_t m_rsp { 0 };
uintptr_t m_ip { 0 };
uintptr_t m_sp { 0 };
const pid_t m_tid { 0 };
State m_state { State::NotStarted };
Process* m_process { nullptr };
bool m_is_userspace { false };
uintptr_t* m_return_rsp { nullptr };
uintptr_t* m_return_rip { nullptr };
uintptr_t* m_return_sp { nullptr };
uintptr_t* m_return_ip { nullptr };
uint64_t m_signal_pending_mask { 0 };
uint64_t m_signal_block_mask { 0 };

View File

@ -21,8 +21,8 @@ namespace Debug
struct stackframe
{
stackframe* rbp;
uintptr_t rip;
stackframe* bp;
uintptr_t ip;
};
SpinLockGuard _(s_debug_lock);
@ -33,8 +33,8 @@ namespace Debug
dprintln("Could not get frame address");
return;
}
uintptr_t first_rip = frame->rip;
uintptr_t last_rip = 0;
uintptr_t first_ip = frame->ip;
uintptr_t last_ip = 0;
bool first = true;
BAN::Formatter::print(Debug::putchar, "\e[36mStack trace:\r\n");
@ -46,21 +46,21 @@ namespace Debug
break;
}
BAN::Formatter::print(Debug::putchar, " {}\r\n", (void*)frame->rip);
BAN::Formatter::print(Debug::putchar, " {}\r\n", (void*)frame->ip);
if (!first && frame->rip == first_rip)
if (!first && frame->ip == first_ip)
{
derrorln("looping kernel panic :(");
break;
}
else if (!first && frame->rip == last_rip)
else if (!first && frame->ip == last_ip)
{
derrorln("repeating stack trace");
break;
}
last_rip = frame->rip;
frame = frame->rbp;
last_ip = frame->ip;
frame = frame->bp;
first = false;
}
BAN::Formatter::print(Debug::putchar, "\e[m");

View File

@ -390,7 +390,7 @@ namespace Kernel
return TRY(LibELF::LoadableELF::load_from_inode(page_table, file.inode));
}
BAN::ErrorOr<long> Process::sys_fork(uintptr_t rsp, uintptr_t rip)
BAN::ErrorOr<long> Process::sys_fork(uintptr_t sp, uintptr_t ip)
{
auto page_table = BAN::UniqPtr<PageTable>::adopt(TRY(PageTable::create_userspace()));
@ -423,7 +423,7 @@ namespace Kernel
ASSERT(this == &Process::current());
// FIXME: this should be able to fail
Thread* thread = MUST(Thread::current().clone(forked, rsp, rip));
Thread* thread = MUST(Thread::current().clone(forked, sp, ip));
forked->add_thread(thread);
forked->register_to_scheduler();

View File

@ -11,8 +11,8 @@
namespace Kernel
{
extern "C" [[noreturn]] void start_thread(uintptr_t rsp, uintptr_t rip);
extern "C" [[noreturn]] void continue_thread(uintptr_t rsp, uintptr_t rip);
extern "C" [[noreturn]] void start_thread(uintptr_t sp, uintptr_t ip);
extern "C" [[noreturn]] void continue_thread(uintptr_t sp, uintptr_t ip);
static Scheduler* s_instance = nullptr;
static BAN::Atomic<bool> s_started { false };
@ -144,18 +144,18 @@ namespace Kernel
{
ASSERT(m_lock.current_processor_has_lock());
uintptr_t rsp, rip;
uintptr_t sp, ip;
push_callee_saved();
if (!(rip = read_rip()))
if (!(ip = read_ip()))
{
pop_callee_saved();
return true;
}
read_rsp(rsp);
read_rsp(sp);
Thread& current = current_thread();
current.set_rip(rip);
current.set_rsp(rsp);
current.set_ip(ip);
current.set_sp(sp);
load_temp_stack();
@ -261,12 +261,12 @@ namespace Kernel
case Thread::State::NotStarted:
current->set_started();
m_lock.unlock(InterruptState::Disabled);
start_thread(current->rsp(), current->rip());
start_thread(current->sp(), current->ip());
case Thread::State::Executing:
m_lock.unlock(InterruptState::Disabled);
while (current->can_add_signal_to_execute())
current->handle_signal();
continue_thread(current->rsp(), current->rip());
continue_thread(current->sp(), current->ip());
case Thread::State::Terminated:
ASSERT_NOT_REACHED();
}

View File

@ -10,9 +10,9 @@
namespace Kernel
{
extern "C" long sys_fork(uintptr_t rsp, uintptr_t rip)
extern "C" long sys_fork(uintptr_t sp, uintptr_t ip)
{
auto ret = Process::current().sys_fork(rsp, rip);
auto ret = Process::current().sys_fork(sp, ip);
if (ret.is_error())
return -ret.error().get_error_code();
return ret.value();
@ -32,8 +32,8 @@ namespace Kernel
{
ASSERT((interrupt_stack.cs & 0b11) == 0b11);
Thread::current().set_return_rsp(interrupt_stack.rsp);
Thread::current().set_return_rip(interrupt_stack.rip);
Thread::current().set_return_sp(interrupt_stack.sp);
Thread::current().set_return_ip(interrupt_stack.ip);
asm volatile("sti");

View File

@ -12,8 +12,8 @@
namespace Kernel
{
extern "C" void thread_userspace_trampoline(uint64_t rsp, uint64_t rip, int argc, char** argv, char** envp);
extern "C" uintptr_t read_rip();
extern "C" void thread_userspace_trampoline(uint64_t sp, uint64_t ip, int argc, char** argv, char** envp);
extern "C" uintptr_t read_ip();
extern "C" void signal_trampoline();
@ -46,14 +46,14 @@ namespace Kernel
PageTable::Flags::ReadWrite | PageTable::Flags::Present,
true
));
thread->m_rsp = thread->stack_base() + thread->stack_size();
thread->m_rip = (uintptr_t)entry;
thread->m_sp = thread->stack_base() + thread->stack_size();
thread->m_ip = (uintptr_t)entry;
// Initialize stack for returning
write_to_stack(thread->m_rsp, nullptr); // alignment
write_to_stack(thread->m_rsp, thread);
write_to_stack(thread->m_rsp, &Thread::on_exit);
write_to_stack(thread->m_rsp, data);
write_to_stack(thread->m_sp, nullptr); // alignment
write_to_stack(thread->m_sp, thread);
write_to_stack(thread->m_sp, &Thread::on_exit);
write_to_stack(thread->m_sp, data);
thread_deleter.disable();
@ -144,7 +144,7 @@ namespace Kernel
{
}
BAN::ErrorOr<Thread*> Thread::clone(Process* new_process, uintptr_t rsp, uintptr_t rip)
BAN::ErrorOr<Thread*> Thread::clone(Process* new_process, uintptr_t sp, uintptr_t ip)
{
ASSERT(m_is_userspace);
ASSERT(m_state == State::Executing);
@ -161,8 +161,8 @@ namespace Kernel
thread->m_state = State::Executing;
thread->m_rip = rip;
thread->m_rsp = rsp;
thread->m_ip = ip;
thread->m_sp = sp;
thread_deleter.disable();
@ -177,24 +177,24 @@ namespace Kernel
[](void*)
{
const auto& info = Process::current().userspace_info();
thread_userspace_trampoline(Thread::current().rsp(), info.entry, info.argc, info.argv, info.envp);
thread_userspace_trampoline(Thread::current().sp(), info.entry, info.argc, info.argv, info.envp);
ASSERT_NOT_REACHED();
}
);
m_rsp = stack_base() + stack_size();
m_rip = (uintptr_t)entry_trampoline;
m_sp = stack_base() + stack_size();
m_ip = (uintptr_t)entry_trampoline;
// Signal mask is inherited
// Setup stack for returning
ASSERT(m_rsp % PAGE_SIZE == 0);
PageTable::with_fast_page(process().page_table().physical_address_of(m_rsp - PAGE_SIZE), [&] {
uintptr_t rsp = PageTable::fast_page() + PAGE_SIZE;
write_to_stack(rsp, nullptr); // alignment
write_to_stack(rsp, this);
write_to_stack(rsp, &Thread::on_exit);
write_to_stack(rsp, nullptr);
m_rsp -= 4 * sizeof(uintptr_t);
ASSERT(m_sp % PAGE_SIZE == 0);
PageTable::with_fast_page(process().page_table().physical_address_of(m_sp - PAGE_SIZE), [&] {
uintptr_t sp = PageTable::fast_page() + PAGE_SIZE;
write_to_stack(sp, nullptr); // alignment
write_to_stack(sp, this);
write_to_stack(sp, &Thread::on_exit);
write_to_stack(sp, nullptr);
m_sp -= 4 * sizeof(uintptr_t);
});
}
@ -210,20 +210,20 @@ namespace Kernel
ASSERT_NOT_REACHED();
}
);
m_rsp = stack_base() + stack_size();
m_rip = (uintptr_t)entry;
m_sp = stack_base() + stack_size();
m_ip = (uintptr_t)entry;
m_signal_pending_mask = 0;
m_signal_block_mask = ~0ull;
ASSERT(m_rsp % PAGE_SIZE == 0);
PageTable::with_fast_page(process().page_table().physical_address_of(m_rsp - PAGE_SIZE), [&] {
uintptr_t rsp = PageTable::fast_page() + PAGE_SIZE;
write_to_stack(rsp, nullptr); // alignment
write_to_stack(rsp, this);
write_to_stack(rsp, &Thread::on_exit);
write_to_stack(rsp, m_process);
m_rsp -= 4 * sizeof(uintptr_t);
ASSERT(m_sp % PAGE_SIZE == 0);
PageTable::with_fast_page(process().page_table().physical_address_of(m_sp - PAGE_SIZE), [&] {
uintptr_t sp = PageTable::fast_page() + PAGE_SIZE;
write_to_stack(sp, nullptr); // alignment
write_to_stack(sp, this);
write_to_stack(sp, &Thread::on_exit);
write_to_stack(sp, m_process);
m_sp -= 4 * sizeof(uintptr_t);
});
}
@ -250,7 +250,7 @@ namespace Kernel
if (!is_userspace() || m_state != State::Executing)
return false;
auto& interrupt_stack = *reinterpret_cast<InterruptStack*>(interrupt_stack_base() + interrupt_stack_size() - sizeof(InterruptStack));
return interrupt_stack.rip == (uintptr_t)signal_trampoline;
return interrupt_stack.ip == (uintptr_t)signal_trampoline;
}
void Thread::handle_signal(int signal)
@ -290,11 +290,11 @@ namespace Kernel
else if (signal_handler != (vaddr_t)SIG_DFL)
{
// call userspace signal handlers
interrupt_stack.rsp -= 128; // skip possible red-zone
write_to_stack(interrupt_stack.rsp, interrupt_stack.rip);
write_to_stack(interrupt_stack.rsp, signal);
write_to_stack(interrupt_stack.rsp, signal_handler);
interrupt_stack.rip = (uintptr_t)signal_trampoline;
interrupt_stack.sp -= 128; // skip possible red-zone
write_to_stack(interrupt_stack.sp, interrupt_stack.ip);
write_to_stack(interrupt_stack.sp, signal);
write_to_stack(interrupt_stack.sp, signal_handler);
interrupt_stack.ip = (uintptr_t)signal_trampoline;
}
else
{
@ -392,11 +392,11 @@ namespace Kernel
void Thread::validate_stack() const
{
if (stack_base() <= m_rsp && m_rsp <= stack_base() + stack_size())
if (stack_base() <= m_sp && m_sp <= stack_base() + stack_size())
return;
if (interrupt_stack_base() <= m_rsp && m_rsp <= interrupt_stack_base() + interrupt_stack_size())
if (interrupt_stack_base() <= m_sp && m_sp <= interrupt_stack_base() + interrupt_stack_size())
return;
Kernel::panic("rsp {8H}, stack {8H}->{8H}, interrupt_stack {8H}->{8H}", m_rsp,
Kernel::panic("sp {8H}, stack {8H}->{8H}, interrupt_stack {8H}->{8H}", m_sp,
stack_base(), stack_base() + stack_size(),
interrupt_stack_base(), interrupt_stack_base() + interrupt_stack_size()
);