thread.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <Kernel/Memory/MemoryManager.h>
  10. #include <Kernel/PerformanceManager.h>
  11. #include <Kernel/Process.h>
  12. namespace Kernel {
  13. KResultOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<const Syscall::SC_create_thread_params*> user_params)
  14. {
  15. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  16. REQUIRE_PROMISE(thread);
  17. Syscall::SC_create_thread_params params;
  18. if (!copy_from_user(&params, user_params))
  19. return EFAULT;
  20. unsigned detach_state = params.detach_state;
  21. int schedule_priority = params.schedule_priority;
  22. unsigned stack_size = params.stack_size;
  23. auto user_sp = Checked<FlatPtr>((FlatPtr)params.stack_location);
  24. user_sp += stack_size;
  25. if (user_sp.has_overflow())
  26. return EOVERFLOW;
  27. if (!MM.validate_user_stack(this->address_space(), VirtualAddress(user_sp.value() - 4)))
  28. return EFAULT;
  29. // FIXME: return EAGAIN if Thread::all_threads().size() is greater than PTHREAD_THREADS_MAX
  30. int requested_thread_priority = schedule_priority;
  31. if (requested_thread_priority < THREAD_PRIORITY_MIN || requested_thread_priority > THREAD_PRIORITY_MAX)
  32. return EINVAL;
  33. bool is_thread_joinable = (0 == detach_state);
  34. // FIXME: Do something with guard pages?
  35. auto thread_or_error = Thread::try_create(*this);
  36. if (thread_or_error.is_error())
  37. return thread_or_error.error();
  38. auto& thread = thread_or_error.value();
  39. // We know this thread is not the main_thread,
  40. // So give it a unique name until the user calls $set_thread_name on it
  41. // length + 4 to give space for our extra junk at the end
  42. StringBuilder builder(m_name.length() + 4);
  43. thread->set_name(KString::try_create(String::formatted("{} [{}]", m_name, thread->tid().value())));
  44. if (!is_thread_joinable)
  45. thread->detach();
  46. auto& regs = thread->regs();
  47. regs.set_ip((FlatPtr)entry);
  48. regs.set_flags(0x0202);
  49. regs.set_sp(user_sp.value());
  50. #if ARCH(X86_64)
  51. regs.rdi = params.rdi;
  52. regs.rsi = params.rsi;
  53. regs.rdx = params.rdx;
  54. regs.rcx = params.rcx;
  55. #endif
  56. regs.cr3 = address_space().page_directory().cr3();
  57. auto tsr_result = thread->make_thread_specific_region({});
  58. if (tsr_result.is_error())
  59. return tsr_result.error();
  60. PerformanceManager::add_thread_created_event(*thread);
  61. SpinlockLocker lock(g_scheduler_lock);
  62. thread->set_priority(requested_thread_priority);
  63. thread->set_state(Thread::State::Runnable);
  64. return thread->tid().value();
  65. }
  66. void Process::sys$exit_thread(Userspace<void*> exit_value, Userspace<void*> stack_location, size_t stack_size)
  67. {
  68. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  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 = address_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<FlatPtr> Process::sys$detach_thread(pid_t tid)
  86. {
  87. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  88. REQUIRE_PROMISE(thread);
  89. auto thread = Thread::from_tid(tid);
  90. if (!thread || thread->pid() != pid())
  91. return ESRCH;
  92. if (!thread->is_joinable())
  93. return EINVAL;
  94. thread->detach();
  95. return 0;
  96. }
  97. KResultOr<FlatPtr> Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
  98. {
  99. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  100. REQUIRE_PROMISE(thread);
  101. auto thread = Thread::from_tid(tid);
  102. if (!thread || thread->pid() != pid())
  103. return ESRCH;
  104. auto current_thread = Thread::current();
  105. if (thread == current_thread)
  106. return EDEADLK;
  107. void* joinee_exit_value = nullptr;
  108. // NOTE: pthread_join() cannot be interrupted by signals. Only by death.
  109. for (;;) {
  110. KResult try_join_result(KSuccess);
  111. auto result = current_thread->block<Thread::JoinBlocker>({}, *thread, try_join_result, joinee_exit_value);
  112. if (result == Thread::BlockResult::NotBlocked) {
  113. if (try_join_result.is_error())
  114. return try_join_result.error();
  115. break;
  116. }
  117. if (result == Thread::BlockResult::InterruptedByDeath)
  118. break;
  119. dbgln("join_thread: retrying");
  120. }
  121. if (exit_value && !copy_to_user(exit_value, &joinee_exit_value))
  122. return EFAULT;
  123. return 0;
  124. }
  125. KResultOr<FlatPtr> Process::sys$kill_thread(pid_t tid, int signal)
  126. {
  127. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  128. REQUIRE_PROMISE(thread);
  129. if (signal < 0 || signal >= 32)
  130. return EINVAL;
  131. auto thread = Thread::from_tid(tid);
  132. if (!thread || thread->pid() != pid())
  133. return ESRCH;
  134. if (signal != 0)
  135. thread->send_signal(signal, &Process::current());
  136. return 0;
  137. }
  138. KResultOr<FlatPtr> Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
  139. {
  140. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  141. REQUIRE_PROMISE(stdio);
  142. auto name_or_error = try_copy_kstring_from_user(user_name, user_name_length);
  143. if (name_or_error.is_error())
  144. return name_or_error.error();
  145. auto name = name_or_error.release_value();
  146. const size_t max_thread_name_size = 64;
  147. if (name->length() > max_thread_name_size)
  148. return EINVAL;
  149. auto thread = Thread::from_tid(tid);
  150. if (!thread || thread->pid() != pid())
  151. return ESRCH;
  152. thread->set_name(move(name));
  153. return 0;
  154. }
  155. KResultOr<FlatPtr> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
  156. {
  157. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  158. REQUIRE_PROMISE(thread);
  159. if (buffer_size == 0)
  160. return EINVAL;
  161. auto thread = Thread::from_tid(tid);
  162. if (!thread || thread->pid() != pid())
  163. return ESRCH;
  164. SpinlockLocker locker(thread->get_lock());
  165. auto thread_name = thread->name();
  166. if (thread_name.is_null()) {
  167. char null_terminator = '\0';
  168. if (!copy_to_user(buffer, &null_terminator, sizeof(null_terminator)))
  169. return EFAULT;
  170. return 0;
  171. }
  172. if (thread_name.length() + 1 > buffer_size)
  173. return ENAMETOOLONG;
  174. if (!copy_to_user(buffer, thread_name.characters_without_null_termination(), thread_name.length() + 1))
  175. return EFAULT;
  176. return 0;
  177. }
  178. KResultOr<FlatPtr> Process::sys$gettid()
  179. {
  180. VERIFY_NO_PROCESS_BIG_LOCK(this)
  181. REQUIRE_PROMISE(stdio);
  182. return Thread::current()->tid().value();
  183. }
  184. }