thread.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <Kernel/Memory/MemoryManager.h>
  8. #include <Kernel/PerformanceManager.h>
  9. #include <Kernel/Process.h>
  10. #include <Kernel/Scheduler.h>
  11. namespace Kernel {
  12. ErrorOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<Syscall::SC_create_thread_params const*> user_params)
  13. {
  14. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  15. TRY(require_promise(Pledge::thread));
  16. auto params = TRY(copy_typed_from_user(user_params));
  17. unsigned detach_state = params.detach_state;
  18. int schedule_priority = params.schedule_priority;
  19. unsigned stack_size = params.stack_size;
  20. auto user_sp = Checked<FlatPtr>((FlatPtr)params.stack_location);
  21. user_sp += stack_size;
  22. if (user_sp.has_overflow())
  23. return EOVERFLOW;
  24. TRY(address_space().with([&](auto& space) -> ErrorOr<void> {
  25. if (!MM.validate_user_stack(*space, VirtualAddress(user_sp.value() - 4)))
  26. return EFAULT;
  27. return {};
  28. }));
  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 = TRY(Thread::create(*this));
  36. // We know this thread is not the main_thread,
  37. // So give it a unique name until the user calls $set_thread_name on it
  38. auto new_thread_name = TRY(name().with([&](auto& process_name) {
  39. return KString::formatted("{} [{}]", process_name->view(), thread->tid().value());
  40. }));
  41. thread->set_name(move(new_thread_name));
  42. if (!is_thread_joinable)
  43. thread->detach();
  44. auto& regs = thread->regs();
  45. regs.set_ip((FlatPtr)entry);
  46. regs.set_sp(user_sp.value());
  47. #if ARCH(X86_64)
  48. regs.set_flags(0x0202);
  49. regs.cr3 = address_space().with([](auto& space) { return space->page_directory().cr3(); });
  50. regs.rdi = params.rdi;
  51. regs.rsi = params.rsi;
  52. regs.rdx = params.rdx;
  53. regs.rcx = params.rcx;
  54. #endif
  55. TRY(thread->make_thread_specific_region({}));
  56. PerformanceManager::add_thread_created_event(*thread);
  57. SpinlockLocker lock(g_scheduler_lock);
  58. thread->set_priority(requested_thread_priority);
  59. thread->set_state(Thread::State::Runnable);
  60. return thread->tid().value();
  61. }
  62. void Process::sys$exit_thread(Userspace<void*> exit_value, Userspace<void*> stack_location, size_t stack_size)
  63. {
  64. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  65. auto result = require_promise(Pledge::thread);
  66. if (result.is_error()) {
  67. // Crash now, as we will never reach back to the syscall handler.
  68. crash(SIGABRT, {});
  69. }
  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().with([&](auto& space) {
  79. return space->unmap_mmap_range(stack_location.vaddr(), stack_size);
  80. });
  81. if (unmap_result.is_error())
  82. dbgln("Failed to unmap thread stack, terminating thread anyway. Error code: {}", unmap_result.error());
  83. }
  84. current_thread->exit(reinterpret_cast<void*>(exit_value.ptr()));
  85. VERIFY_NOT_REACHED();
  86. }
  87. ErrorOr<FlatPtr> Process::sys$detach_thread(pid_t tid)
  88. {
  89. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  90. TRY(require_promise(Pledge::thread));
  91. auto thread = Thread::from_tid(tid);
  92. if (!thread || thread->pid() != pid())
  93. return ESRCH;
  94. if (!thread->is_joinable())
  95. return EINVAL;
  96. thread->detach();
  97. return 0;
  98. }
  99. ErrorOr<FlatPtr> Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
  100. {
  101. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  102. TRY(require_promise(Pledge::thread));
  103. auto thread = Thread::from_tid(tid);
  104. if (!thread || thread->pid() != pid())
  105. return ESRCH;
  106. auto* current_thread = Thread::current();
  107. if (thread == current_thread)
  108. return EDEADLK;
  109. void* joinee_exit_value = nullptr;
  110. // NOTE: pthread_join() cannot be interrupted by signals. Only by death.
  111. for (;;) {
  112. ErrorOr<void> try_join_result;
  113. auto result = current_thread->block<Thread::JoinBlocker>({}, *thread, try_join_result, joinee_exit_value);
  114. if (result == Thread::BlockResult::NotBlocked) {
  115. if (try_join_result.is_error())
  116. return try_join_result.release_error();
  117. break;
  118. }
  119. if (result == Thread::BlockResult::InterruptedByDeath)
  120. break;
  121. dbgln("join_thread: retrying");
  122. }
  123. if (exit_value)
  124. TRY(copy_to_user(exit_value, &joinee_exit_value));
  125. return 0;
  126. }
  127. ErrorOr<FlatPtr> Process::sys$kill_thread(pid_t tid, int signal)
  128. {
  129. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  130. TRY(require_promise(Pledge::thread));
  131. if (signal < 0 || signal >= NSIG)
  132. return EINVAL;
  133. auto thread = Thread::from_tid(tid);
  134. if (!thread || thread->pid() != pid())
  135. return ESRCH;
  136. if (signal != 0)
  137. thread->send_signal(signal, &Process::current());
  138. return 0;
  139. }
  140. ErrorOr<FlatPtr> Process::sys$set_thread_name(pid_t tid, Userspace<char const*> user_name, size_t user_name_length)
  141. {
  142. VERIFY_NO_PROCESS_BIG_LOCK(this);
  143. TRY(require_promise(Pledge::stdio));
  144. auto name = TRY(try_copy_kstring_from_user(user_name, user_name_length));
  145. const size_t max_thread_name_size = 64;
  146. if (name->length() > max_thread_name_size)
  147. return ENAMETOOLONG;
  148. auto thread = Thread::from_tid(tid);
  149. if (!thread || thread->pid() != pid())
  150. return ESRCH;
  151. thread->set_name(move(name));
  152. return 0;
  153. }
  154. ErrorOr<FlatPtr> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
  155. {
  156. VERIFY_NO_PROCESS_BIG_LOCK(this);
  157. TRY(require_promise(Pledge::thread));
  158. if (buffer_size == 0)
  159. return EINVAL;
  160. auto thread = Thread::from_tid(tid);
  161. if (!thread || thread->pid() != pid())
  162. return ESRCH;
  163. TRY(thread->name().with([&](auto& thread_name) -> ErrorOr<void> {
  164. if (thread_name->view().is_null()) {
  165. char null_terminator = '\0';
  166. TRY(copy_to_user(buffer, &null_terminator, sizeof(null_terminator)));
  167. return {};
  168. }
  169. if (thread_name->length() + 1 > buffer_size)
  170. return ENAMETOOLONG;
  171. return copy_to_user(buffer, thread_name->characters(), thread_name->length() + 1);
  172. }));
  173. return 0;
  174. }
  175. ErrorOr<FlatPtr> Process::sys$gettid()
  176. {
  177. VERIFY_NO_PROCESS_BIG_LOCK(this);
  178. TRY(require_promise(Pledge::stdio));
  179. return Thread::current()->tid().value();
  180. }
  181. }