thread.cpp 6.6 KB

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