Skip to content

Commit

Permalink
Kernel: Add sys$get_stack_bounds() for finding the stack base & size
Browse files Browse the repository at this point in the history
This will be useful when implementing conservative garbage collection.
  • Loading branch information
awesomekling committed Mar 16, 2020
1 parent 086f68e commit ad92a1e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4838,4 +4838,25 @@ OwnPtr<Process::ELFBundle> Process::elf_bundle() const
return bundle;
}

int Process::sys$get_stack_bounds(FlatPtr* user_stack_base, size_t* user_stack_size)
{
if (!validate_write_typed(user_stack_base))
return -EFAULT;
if (!validate_write_typed(user_stack_size))
return -EFAULT;

FlatPtr stack_pointer = Thread::current->get_register_dump_from_stack().userspace_esp;
auto* stack_region = MM.region_from_vaddr(*this, VirtualAddress(stack_pointer));
if (!stack_region) {
ASSERT_NOT_REACHED();
return -EINVAL;
}

FlatPtr stack_base = stack_region->range().base().get();
size_t stack_size = stack_region->size();
copy_to_user(user_stack_base, &stack_base);
copy_to_user(user_stack_size, &stack_size);
return 0;
}

}
1 change: 1 addition & 0 deletions Kernel/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class Process : public InlineLinkedListNode<Process> {
int sys$pledge(const Syscall::SC_pledge_params*);
int sys$unveil(const Syscall::SC_unveil_params*);
int sys$perf_event(int type, FlatPtr arg1, FlatPtr arg2);
int sys$get_stack_bounds(FlatPtr* stack_base, size_t* stack_size);

template<bool sockname, typename Params>
int get_sock_or_peer_name(const Params&);
Expand Down
3 changes: 2 additions & 1 deletion Kernel/Syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ namespace Kernel {
__ENUMERATE_SYSCALL(pledge) \
__ENUMERATE_SYSCALL(unveil) \
__ENUMERATE_SYSCALL(perf_event) \
__ENUMERATE_SYSCALL(shutdown)
__ENUMERATE_SYSCALL(shutdown) \
__ENUMERATE_SYSCALL(get_stack_bounds)

namespace Syscall {

Expand Down
7 changes: 7 additions & 0 deletions Libraries/LibC/serenity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,11 @@ int shbuf_allow_all(int shbuf_id)
int rc = syscall(SC_shbuf_allow_all, shbuf_id);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

int get_stack_bounds(uintptr_t* user_stack_base, size_t* user_stack_size)
{
int rc = syscall(SC_get_stack_bounds, user_stack_base, user_stack_size);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

}
2 changes: 2 additions & 0 deletions Libraries/LibC/serenity.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ int purge(int mode);

int perf_event(int type, uintptr_t arg1, uintptr_t arg2);

int get_stack_bounds(uintptr_t* user_stack_base, size_t* user_stack_size);

__END_DECLS

0 comments on commit ad92a1e

Please sign in to comment.