thread.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Checked.h>
  7. #include <AK/String.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/StringView.h>
  10. #include <Kernel/Panic.h>
  11. #include <Kernel/PerformanceManager.h>
  12. #include <Kernel/Process.h>
  13. #include <Kernel/VM/MemoryManager.h>
  14. #include <Kernel/VM/PageDirectory.h>
  15. namespace Kernel {
  16. KResultOr<int> Process::sys$create_thread(void* (*entry)(void*), Userspace<const Syscall::SC_create_thread_params*> user_params)
  17. {
  18. REQUIRE_PROMISE(thread);
  19. Syscall::SC_create_thread_params params;
  20. if (!copy_from_user(&params, user_params))
  21. return EFAULT;
  22. unsigned detach_state = params.m_detach_state;
  23. int schedule_priority = params.m_schedule_priority;
  24. unsigned stack_size = params.m_stack_size;
  25. auto user_esp = Checked<FlatPtr>((FlatPtr)params.m_stack_location);
  26. user_esp += stack_size;
  27. if (user_esp.has_overflow())
  28. return EOVERFLOW;
  29. if (!MM.validate_user_stack(*this, VirtualAddress(user_esp.value() - 4)))
  30. return EFAULT;
  31. // FIXME: return EAGAIN if Thread::all_threads().size() is greater than PTHREAD_THREADS_MAX
  32. int requested_thread_priority = schedule_priority;
  33. if (requested_thread_priority < THREAD_PRIORITY_MIN || requested_thread_priority > THREAD_PRIORITY_MAX)
  34. return EINVAL;
  35. bool is_thread_joinable = (0 == detach_state);
  36. // FIXME: Do something with guard pages?
  37. auto thread_or_error = Thread::try_create(*this);
  38. if (thread_or_error.is_error())
  39. return thread_or_error.error();
  40. auto& thread = thread_or_error.value();
  41. // We know this thread is not the main_thread,
  42. // So give it a unique name until the user calls $set_thread_name on it
  43. // length + 4 to give space for our extra junk at the end
  44. StringBuilder builder(m_name.length() + 4);
  45. thread->set_name(String::formatted("{} [{}]", m_name, thread->tid().value()));
  46. if (!is_thread_joinable)
  47. thread->detach();
  48. #if ARCH(I386)
  49. auto& tss = thread->tss();
  50. tss.eip = (FlatPtr)entry;
  51. tss.eflags = 0x0202;
  52. tss.cr3 = space().page_directory().cr3();
  53. tss.esp = user_esp.value();
  54. #else
  55. (void)entry;
  56. PANIC("Process::sys$create_thread() not implemented");
  57. #endif
  58. auto tsr_result = thread->make_thread_specific_region({});
  59. if (tsr_result.is_error())
  60. return tsr_result.error();
  61. PerformanceManager::add_thread_created_event(*thread);
  62. ScopedSpinLock lock(g_scheduler_lock);
  63. thread->set_priority(requested_thread_priority);
  64. thread->set_state(Thread::State::Runnable);
  65. return thread->tid().value();
  66. }
  67. void Process::sys$exit_thread(Userspace<void*> exit_value, Userspace<void*> stack_location, size_t stack_size)
  68. {
  69. REQUIRE_PROMISE(thread);
  70. if (this->thread_count() == 1) {
  71. // If this is the last thread, instead kill the process.
  72. this->sys$exit(0);
  73. }
  74. auto current_thread = Thread::current();
  75. current_thread->set_profiling_suppressed();
  76. PerformanceManager::add_thread_exit_event(*current_thread);
  77. if (stack_location) {
  78. auto unmap_result = space().unmap_mmap_range(VirtualAddress { stack_location }, stack_size);
  79. if (unmap_result.is_error())
  80. dbgln("Failed to unmap thread stack, terminating thread anyway. Error code: {}", unmap_result.error());
  81. }
  82. current_thread->exit(reinterpret_cast<void*>(exit_value.ptr()));
  83. VERIFY_NOT_REACHED();
  84. }
  85. KResultOr<int> Process::sys$detach_thread(pid_t tid)
  86. {
  87. REQUIRE_PROMISE(thread);
  88. auto thread = Thread::from_tid(tid);
  89. if (!thread || thread->pid() != pid())
  90. return ESRCH;
  91. if (!thread->is_joinable())
  92. return EINVAL;
  93. thread->detach();
  94. return 0;
  95. }
  96. KResultOr<int> Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
  97. {
  98. REQUIRE_PROMISE(thread);
  99. auto thread = Thread::from_tid(tid);
  100. if (!thread || thread->pid() != pid())
  101. return ESRCH;
  102. auto current_thread = Thread::current();
  103. if (thread == current_thread)
  104. return EDEADLK;
  105. void* joinee_exit_value = nullptr;
  106. // NOTE: pthread_join() cannot be interrupted by signals. Only by death.
  107. for (;;) {
  108. KResult try_join_result(KSuccess);
  109. auto result = current_thread->block<Thread::JoinBlocker>({}, *thread, try_join_result, joinee_exit_value);
  110. if (result == Thread::BlockResult::NotBlocked) {
  111. if (try_join_result.is_error())
  112. return try_join_result.error();
  113. break;
  114. }
  115. if (result == Thread::BlockResult::InterruptedByDeath)
  116. break;
  117. dbgln("join_thread: retrying");
  118. }
  119. if (exit_value && !copy_to_user(exit_value, &joinee_exit_value))
  120. return EFAULT;
  121. return 0;
  122. }
  123. KResultOr<int> Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
  124. {
  125. REQUIRE_PROMISE(stdio);
  126. auto name = copy_string_from_user(user_name, user_name_length);
  127. if (name.is_null())
  128. return EFAULT;
  129. const size_t max_thread_name_size = 64;
  130. if (name.length() > max_thread_name_size)
  131. return EINVAL;
  132. auto thread = Thread::from_tid(tid);
  133. if (!thread || thread->pid() != pid())
  134. return ESRCH;
  135. thread->set_name(move(name));
  136. return 0;
  137. }
  138. KResultOr<int> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
  139. {
  140. REQUIRE_PROMISE(thread);
  141. if (buffer_size == 0)
  142. return EINVAL;
  143. auto thread = Thread::from_tid(tid);
  144. if (!thread || thread->pid() != pid())
  145. return ESRCH;
  146. // We must make a temporary copy here to avoid a race with sys$set_thread_name
  147. auto thread_name = thread->name();
  148. if (thread_name.length() + 1 > buffer_size)
  149. return ENAMETOOLONG;
  150. if (!copy_to_user(buffer, thread_name.characters(), thread_name.length() + 1))
  151. return EFAULT;
  152. return 0;
  153. }
  154. KResultOr<int> Process::sys$gettid()
  155. {
  156. REQUIRE_PROMISE(stdio);
  157. return Thread::current()->tid().value();
  158. }
  159. }