ladybird/Kernel/Syscalls/get_stack_bounds.cpp
Gunnar Beutner 2a78bf8596 Kernel: Fix the return type for syscalls
The Process::Handler type has KResultOr<FlatPtr> as its return type.
Using a different return type with an equally-sized template parameter
sort of works but breaks once that condition is no longer true, e.g.
for KResultOr<int> on x86_64.

Ideally the syscall handlers would also take FlatPtrs as their args
so we can get rid of the reinterpret_cast for the function pointer
but I didn't quite feel like cleaning that up as well.
2021-06-28 22:29:28 +02:00

36 lines
1 KiB
C++

/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Panic.h>
#include <Kernel/Process.h>
#include <Kernel/VM/Region.h>
namespace Kernel {
KResultOr<FlatPtr> Process::sys$get_stack_bounds(Userspace<FlatPtr*> user_stack_base, Userspace<size_t*> user_stack_size)
{
auto& regs = Thread::current()->get_register_dump_from_stack();
FlatPtr stack_pointer;
#if ARCH(I386)
stack_pointer = regs.userspace_esp;
#else
stack_pointer = regs.userspace_rsp;
#endif
auto* stack_region = space().find_region_containing(Range { VirtualAddress(stack_pointer), 1 });
// The syscall handler should have killed us if we had an invalid stack pointer.
VERIFY(stack_region);
FlatPtr stack_base = stack_region->range().base().get();
size_t stack_size = stack_region->size();
if (!copy_to_user(user_stack_base, &stack_base))
return EFAULT;
if (!copy_to_user(user_stack_size, &stack_size))
return EFAULT;
return 0;
}
}