2020-07-30 21:38:15 +00:00
|
|
|
/*
|
2021-01-30 22:38:57 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-07-30 21:38:15 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
2021-02-24 14:02:51 +00:00
|
|
|
#include <AK/Checked.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <AK/StringView.h>
|
2021-04-25 21:42:36 +00:00
|
|
|
#include <Kernel/PerformanceEventBuffer.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
|
|
|
#include <Kernel/VM/PageDirectory.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-03-01 12:49:16 +00:00
|
|
|
KResultOr<int> Process::sys$create_thread(void* (*entry)(void*), Userspace<const Syscall::SC_create_thread_params*> user_params)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(thread);
|
|
|
|
|
|
|
|
Syscall::SC_create_thread_params params;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_from_user(¶ms, user_params))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
unsigned detach_state = params.m_detach_state;
|
|
|
|
int schedule_priority = params.m_schedule_priority;
|
|
|
|
unsigned stack_size = params.m_stack_size;
|
|
|
|
|
2021-03-07 12:21:03 +00:00
|
|
|
auto user_esp = Checked<FlatPtr>((FlatPtr)params.m_stack_location);
|
|
|
|
user_esp += stack_size;
|
|
|
|
if (user_esp.has_overflow())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EOVERFLOW;
|
2021-02-24 14:02:51 +00:00
|
|
|
|
2021-03-07 12:21:03 +00:00
|
|
|
if (!MM.validate_user_stack(*this, VirtualAddress(user_esp.value() - 4)))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +00: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 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
bool is_thread_joinable = (0 == detach_state);
|
|
|
|
|
|
|
|
// FIXME: Do something with guard pages?
|
|
|
|
|
2021-02-07 17:13:51 +00:00
|
|
|
auto thread_or_error = Thread::try_create(*this);
|
|
|
|
if (thread_or_error.is_error())
|
|
|
|
return thread_or_error.error();
|
|
|
|
|
|
|
|
auto& thread = thread_or_error.value();
|
2020-07-30 21:38:15 +00: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
|
|
|
|
// length + 4 to give space for our extra junk at the end
|
|
|
|
StringBuilder builder(m_name.length() + 4);
|
2021-02-09 15:08:43 +00:00
|
|
|
thread->set_name(String::formatted("{} [{}]", m_name, thread->tid().value()));
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-09-26 03:44:43 +00:00
|
|
|
if (!is_thread_joinable)
|
|
|
|
thread->detach();
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
auto& tss = thread->tss();
|
|
|
|
tss.eip = (FlatPtr)entry;
|
|
|
|
tss.eflags = 0x0202;
|
2021-02-08 14:45:40 +00:00
|
|
|
tss.cr3 = space().page_directory().cr3();
|
2021-03-07 12:21:03 +00:00
|
|
|
tss.esp = user_esp.value();
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-09-16 17:47:47 +00:00
|
|
|
auto tsr_result = thread->make_thread_specific_region({});
|
|
|
|
if (tsr_result.is_error())
|
|
|
|
return tsr_result.error();
|
2020-10-26 02:22:59 +00:00
|
|
|
|
2021-04-25 21:42:36 +00:00
|
|
|
if (m_perf_event_buffer) {
|
|
|
|
[[maybe_unused]] auto rc = m_perf_event_buffer->append(PERF_EVENT_THREAD_CREATE, thread->tid().value(), 0, nullptr);
|
|
|
|
}
|
|
|
|
|
2020-10-26 02:22:59 +00:00
|
|
|
ScopedSpinLock lock(g_scheduler_lock);
|
|
|
|
thread->set_priority(requested_thread_priority);
|
2020-07-30 21:38:15 +00:00
|
|
|
thread->set_state(Thread::State::Runnable);
|
2020-08-08 15:32:34 +00:00
|
|
|
return thread->tid().value();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:45:51 +00:00
|
|
|
void Process::sys$exit_thread(Userspace<void*> exit_value)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(thread);
|
2021-02-10 20:17:30 +00:00
|
|
|
|
|
|
|
if (this->thread_count() == 1) {
|
|
|
|
// If this is the last thread, instead kill the process.
|
|
|
|
this->sys$exit(0);
|
|
|
|
}
|
|
|
|
|
2021-04-26 21:07:30 +00:00
|
|
|
if (auto* event_buffer = current_perf_events_buffer()) {
|
2021-04-26 21:08:09 +00:00
|
|
|
[[maybe_unused]] auto rc = event_buffer->append(PERF_EVENT_THREAD_EXIT, 0, 0, nullptr);
|
2021-04-25 21:42:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 03:51:34 +00:00
|
|
|
Thread::current()->exit(reinterpret_cast<void*>(exit_value.ptr()));
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 12:49:16 +00:00
|
|
|
KResultOr<int> Process::sys$detach_thread(pid_t tid)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(thread);
|
2020-09-27 14:53:35 +00:00
|
|
|
auto thread = Thread::from_tid(tid);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!thread || thread->pid() != pid())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ESRCH;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
if (!thread->is_joinable())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-09-26 03:44:43 +00:00
|
|
|
thread->detach();
|
2020-07-30 21:38:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-01 12:49:16 +00:00
|
|
|
KResultOr<int> Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(thread);
|
|
|
|
|
2020-09-27 14:53:35 +00:00
|
|
|
auto thread = Thread::from_tid(tid);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!thread || thread->pid() != pid())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ESRCH;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
auto current_thread = Thread::current();
|
|
|
|
if (thread == current_thread)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EDEADLK;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
void* joinee_exit_value = nullptr;
|
|
|
|
|
|
|
|
// NOTE: pthread_join() cannot be interrupted by signals. Only by death.
|
|
|
|
for (;;) {
|
2020-09-26 03:44:43 +00:00
|
|
|
KResult try_join_result(KSuccess);
|
2021-01-10 23:29:28 +00:00
|
|
|
auto result = current_thread->block<Thread::JoinBlocker>({}, *thread, try_join_result, joinee_exit_value);
|
2020-09-26 03:44:43 +00:00
|
|
|
if (result == Thread::BlockResult::NotBlocked) {
|
|
|
|
if (try_join_result.is_error())
|
|
|
|
return try_join_result.error();
|
|
|
|
break;
|
|
|
|
}
|
2020-09-27 14:53:35 +00:00
|
|
|
if (result == Thread::BlockResult::InterruptedByDeath)
|
2020-10-14 18:47:19 +00:00
|
|
|
break;
|
2021-01-09 17:51:44 +00:00
|
|
|
dbgln("join_thread: retrying");
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:11:07 +00:00
|
|
|
if (exit_value && !copy_to_user(exit_value, &joinee_exit_value))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-01 12:49:16 +00:00
|
|
|
KResultOr<int> Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-01-30 22:38:57 +00:00
|
|
|
REQUIRE_PROMISE(stdio);
|
2020-09-12 03:11:07 +00:00
|
|
|
auto name = copy_string_from_user(user_name, user_name_length);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (name.is_null())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
const size_t max_thread_name_size = 64;
|
|
|
|
if (name.length() > max_thread_name_size)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-09-27 14:53:35 +00:00
|
|
|
auto thread = Thread::from_tid(tid);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!thread || thread->pid() != pid())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ESRCH;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-09-12 03:11:07 +00:00
|
|
|
thread->set_name(move(name));
|
2020-07-30 21:38:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-08-09 22:27:23 +00:00
|
|
|
|
2021-03-01 12:49:16 +00:00
|
|
|
KResultOr<int> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(thread);
|
|
|
|
if (buffer_size == 0)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-09-27 14:53:35 +00:00
|
|
|
auto thread = Thread::from_tid(tid);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!thread || thread->pid() != pid())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ESRCH;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-09-27 14:53:35 +00:00
|
|
|
// We must make a temporary copy here to avoid a race with sys$set_thread_name
|
|
|
|
auto thread_name = thread->name();
|
|
|
|
if (thread_name.length() + 1 > (size_t)buffer_size)
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENAMETOOLONG;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-09-27 14:53:35 +00:00
|
|
|
if (!copy_to_user(buffer, thread_name.characters(), thread_name.length() + 1))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-01 12:49:16 +00:00
|
|
|
KResultOr<int> Process::sys$gettid()
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
2020-08-08 15:32:34 +00:00
|
|
|
return Thread::current()->tid().value();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|