2020-07-31 00:38:15 +03:00
|
|
|
/*
|
2021-01-31 01:38:57 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-07-31 00:38:15 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 00:38:15 +03:00
|
|
|
*/
|
|
|
|
|
2021-02-24 17:02:51 +03:00
|
|
|
#include <AK/Checked.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2023-02-24 21:25:52 +03:00
|
|
|
#include <Kernel/Tasks/PerformanceManager.h>
|
2023-02-24 20:45:37 +03:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
|
|
|
#include <Kernel/Tasks/Scheduler.h>
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<Syscall::SC_create_thread_params const*> user_params)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-17 23:03:04 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::thread));
|
2021-09-05 18:51:37 +03:00
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-07-01 12:20:27 +03:00
|
|
|
unsigned detach_state = params.detach_state;
|
|
|
|
int schedule_priority = params.schedule_priority;
|
|
|
|
unsigned stack_size = params.stack_size;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-07-01 12:20:27 +03:00
|
|
|
auto user_sp = Checked<FlatPtr>((FlatPtr)params.stack_location);
|
2021-06-29 18:48:10 +03:00
|
|
|
user_sp += stack_size;
|
|
|
|
if (user_sp.has_overflow())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EOVERFLOW;
|
2021-02-24 17:02:51 +03:00
|
|
|
|
2022-08-23 18:58:05 +03:00
|
|
|
TRY(address_space().with([&](auto& space) -> ErrorOr<void> {
|
|
|
|
if (!MM.validate_user_stack(*space, VirtualAddress(user_sp.value() - 4)))
|
|
|
|
return EFAULT;
|
|
|
|
return {};
|
|
|
|
}));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
// FIXME: return EAGAIN if Thread::all_threads().size() is greater than PTHREAD_THREADS_MAX
|
|
|
|
|
|
|
|
int requested_thread_priority = schedule_priority;
|
|
|
|
if (requested_thread_priority < THREAD_PRIORITY_MIN || requested_thread_priority > THREAD_PRIORITY_MAX)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
bool is_thread_joinable = (0 == detach_state);
|
|
|
|
|
|
|
|
// FIXME: Do something with guard pages?
|
|
|
|
|
2023-04-02 21:40:47 +03:00
|
|
|
auto thread = TRY(Thread::create(*this));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
// We know this thread is not the main_thread,
|
|
|
|
// So give it a unique name until the user calls $set_thread_name on it
|
2023-02-04 16:01:46 +03:00
|
|
|
auto new_thread_name = TRY(name().with([&](auto& process_name) {
|
2023-07-17 18:34:19 +03:00
|
|
|
return KString::formatted("{} [{}]", process_name.representable_view(), thread->tid().value());
|
2023-02-04 16:01:46 +03:00
|
|
|
}));
|
2023-07-17 18:34:19 +03:00
|
|
|
thread->set_name(new_thread_name->view());
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-09-26 06:44:43 +03:00
|
|
|
if (!is_thread_joinable)
|
|
|
|
thread->detach();
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-06-26 20:57:16 +03:00
|
|
|
auto& regs = thread->regs();
|
2021-08-19 22:53:53 +03:00
|
|
|
regs.set_ip((FlatPtr)entry);
|
|
|
|
regs.set_sp(user_sp.value());
|
2022-12-27 16:04:07 +03:00
|
|
|
|
2021-08-19 22:53:53 +03:00
|
|
|
#if ARCH(X86_64)
|
2022-12-27 16:04:07 +03:00
|
|
|
regs.set_flags(0x0202);
|
|
|
|
regs.cr3 = address_space().with([](auto& space) { return space->page_directory().cr3(); });
|
|
|
|
|
2023-04-22 17:29:17 +03:00
|
|
|
// Set up the argument registers expected by pthread_create_helper.
|
|
|
|
regs.rdi = (FlatPtr)params.entry;
|
|
|
|
regs.rsi = (FlatPtr)params.entry_argument;
|
|
|
|
regs.rdx = (FlatPtr)params.stack_location;
|
|
|
|
regs.rcx = (FlatPtr)params.stack_size;
|
|
|
|
#elif ARCH(AARCH64)
|
|
|
|
regs.ttbr0_el1 = address_space().with([](auto& space) { return space->page_directory().ttbr0(); });
|
|
|
|
|
|
|
|
// Set up the argument registers expected by pthread_create_helper.
|
|
|
|
regs.x[0] = (FlatPtr)params.entry;
|
|
|
|
regs.x[1] = (FlatPtr)params.entry_argument;
|
|
|
|
regs.x[2] = (FlatPtr)params.stack_location;
|
|
|
|
regs.x[3] = (FlatPtr)params.stack_size;
|
|
|
|
#else
|
|
|
|
# error Unknown architecture
|
2021-06-23 22:54:41 +03:00
|
|
|
#endif
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-09-05 19:12:10 +03:00
|
|
|
TRY(thread->make_thread_specific_region({}));
|
2020-10-26 05:22:59 +03:00
|
|
|
|
2021-05-07 08:29:19 +03:00
|
|
|
PerformanceManager::add_thread_created_event(*thread);
|
2021-04-26 00:42:36 +03:00
|
|
|
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(g_scheduler_lock);
|
2020-10-26 05:22:59 +03:00
|
|
|
thread->set_priority(requested_thread_priority);
|
2020-07-31 00:38:15 +03:00
|
|
|
thread->set_state(Thread::State::Runnable);
|
2020-08-08 18:32:34 +03:00
|
|
|
return thread->tid().value();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-05-28 12:20:22 +03:00
|
|
|
void Process::sys$exit_thread(Userspace<void*> exit_value, Userspace<void*> stack_location, size_t stack_size)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-17 23:03:04 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
|
|
|
|
auto result = require_promise(Pledge::thread);
|
|
|
|
if (result.is_error()) {
|
|
|
|
// Crash now, as we will never reach back to the syscall handler.
|
2023-01-30 14:10:29 +03:00
|
|
|
crash(SIGABRT, {});
|
2021-12-29 12:11:45 +03:00
|
|
|
}
|
2021-02-10 23:17:30 +03:00
|
|
|
|
|
|
|
if (this->thread_count() == 1) {
|
|
|
|
// If this is the last thread, instead kill the process.
|
|
|
|
this->sys$exit(0);
|
|
|
|
}
|
|
|
|
|
2021-12-18 20:37:21 +03:00
|
|
|
auto* current_thread = Thread::current();
|
2021-05-30 17:24:53 +03:00
|
|
|
current_thread->set_profiling_suppressed();
|
2021-05-07 08:29:19 +03:00
|
|
|
PerformanceManager::add_thread_exit_event(*current_thread);
|
2021-04-26 00:42:36 +03:00
|
|
|
|
2021-05-28 12:20:22 +03:00
|
|
|
if (stack_location) {
|
2022-08-23 18:58:05 +03:00
|
|
|
auto unmap_result = address_space().with([&](auto& space) {
|
|
|
|
return space->unmap_mmap_range(stack_location.vaddr(), stack_size);
|
|
|
|
});
|
2021-05-28 12:20:22 +03:00
|
|
|
if (unmap_result.is_error())
|
|
|
|
dbgln("Failed to unmap thread stack, terminating thread anyway. Error code: {}", unmap_result.error());
|
|
|
|
}
|
|
|
|
|
2021-05-07 08:29:19 +03:00
|
|
|
current_thread->exit(reinterpret_cast<void*>(exit_value.ptr()));
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$detach_thread(pid_t tid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-17 23:03:04 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::thread));
|
2020-09-27 17:53:35 +03:00
|
|
|
auto thread = Thread::from_tid(tid);
|
2020-07-31 00:38:15 +03:00
|
|
|
if (!thread || thread->pid() != pid())
|
2021-03-01 15:49:16 +03:00
|
|
|
return ESRCH;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
if (!thread->is_joinable())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-09-26 06:44:43 +03:00
|
|
|
thread->detach();
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-17 23:03:04 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::thread));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-09-27 17:53:35 +03:00
|
|
|
auto thread = Thread::from_tid(tid);
|
2020-07-31 00:38:15 +03:00
|
|
|
if (!thread || thread->pid() != pid())
|
2021-03-01 15:49:16 +03:00
|
|
|
return ESRCH;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-12-18 20:37:21 +03:00
|
|
|
auto* current_thread = Thread::current();
|
2020-07-31 00:38:15 +03:00
|
|
|
if (thread == current_thread)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EDEADLK;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
void* joinee_exit_value = nullptr;
|
|
|
|
|
|
|
|
// NOTE: pthread_join() cannot be interrupted by signals. Only by death.
|
|
|
|
for (;;) {
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> try_join_result;
|
2021-01-11 02:29:28 +03:00
|
|
|
auto result = current_thread->block<Thread::JoinBlocker>({}, *thread, try_join_result, joinee_exit_value);
|
2020-09-26 06:44:43 +03:00
|
|
|
if (result == Thread::BlockResult::NotBlocked) {
|
|
|
|
if (try_join_result.is_error())
|
2021-11-08 02:51:39 +03:00
|
|
|
return try_join_result.release_error();
|
2020-09-26 06:44:43 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-09-27 17:53:35 +03:00
|
|
|
if (result == Thread::BlockResult::InterruptedByDeath)
|
2020-10-14 21:47:19 +03:00
|
|
|
break;
|
2021-01-09 20:51:44 +03:00
|
|
|
dbgln("join_thread: retrying");
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-09-05 18:38:37 +03:00
|
|
|
if (exit_value)
|
|
|
|
TRY(copy_to_user(exit_value, &joinee_exit_value));
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
return 0;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$kill_thread(pid_t tid, int signal)
|
2021-07-09 04:56:29 +03:00
|
|
|
{
|
2022-08-17 23:03:04 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::thread));
|
2021-07-09 04:56:29 +03:00
|
|
|
|
2022-07-22 00:08:07 +03:00
|
|
|
if (signal < 0 || signal >= NSIG)
|
2021-07-09 04:56:29 +03:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
auto thread = Thread::from_tid(tid);
|
|
|
|
if (!thread || thread->pid() != pid())
|
|
|
|
return ESRCH;
|
|
|
|
|
|
|
|
if (signal != 0)
|
2021-08-19 22:45:07 +03:00
|
|
|
thread->send_signal(signal, &Process::current());
|
2021-07-09 04:56:29 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$set_thread_name(pid_t tid, Userspace<char const*> user_name, size_t user_name_length)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2023-02-04 17:11:35 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2021-08-05 23:22:26 +03:00
|
|
|
|
2023-07-17 18:34:19 +03:00
|
|
|
Thread::Name thread_name {};
|
|
|
|
TRY(try_copy_name_from_user_into_fixed_string_buffer<64>(user_name, thread_name, user_name_length));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-09-27 17:53:35 +03:00
|
|
|
auto thread = Thread::from_tid(tid);
|
2020-07-31 00:38:15 +03:00
|
|
|
if (!thread || thread->pid() != pid())
|
2021-03-01 15:49:16 +03:00
|
|
|
return ESRCH;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2023-07-17 18:34:19 +03:00
|
|
|
thread->set_name(thread_name.representable_view());
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2020-08-10 01:27:23 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2023-02-04 17:11:35 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::thread));
|
2020-07-31 00:38:15 +03:00
|
|
|
if (buffer_size == 0)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-09-27 17:53:35 +03:00
|
|
|
auto thread = Thread::from_tid(tid);
|
2020-07-31 00:38:15 +03:00
|
|
|
if (!thread || thread->pid() != pid())
|
2021-03-01 15:49:16 +03:00
|
|
|
return ESRCH;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2023-02-04 17:11:35 +03:00
|
|
|
TRY(thread->name().with([&](auto& thread_name) -> ErrorOr<void> {
|
2023-07-17 18:34:19 +03:00
|
|
|
VERIFY(!thread_name.representable_view().is_null());
|
|
|
|
auto thread_name_view = thread_name.representable_view();
|
|
|
|
if (thread_name_view.length() + 1 > buffer_size)
|
2023-02-04 17:11:35 +03:00
|
|
|
return ENAMETOOLONG;
|
2021-08-05 23:22:26 +03:00
|
|
|
|
2023-07-17 18:34:19 +03:00
|
|
|
return copy_to_user(buffer, thread_name_view.characters_without_null_termination(), thread_name_view.length() + 1);
|
2023-02-04 17:11:35 +03:00
|
|
|
}));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
return 0;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$gettid()
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-17 23:03:04 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2020-08-08 18:32:34 +03:00
|
|
|
return Thread::current()->tid().value();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|