thread.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. REQUIRE_PROMISE(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. REQUIRE_PROMISE(thread);
  60. if (this->thread_count() == 1) {
  61. // If this is the last thread, instead kill the process.
  62. this->sys$exit(0);
  63. }
  64. auto* current_thread = Thread::current();
  65. current_thread->set_profiling_suppressed();
  66. PerformanceManager::add_thread_exit_event(*current_thread);
  67. if (stack_location) {
  68. auto unmap_result = address_space().unmap_mmap_range(stack_location.vaddr(), stack_size);
  69. if (unmap_result.is_error())
  70. dbgln("Failed to unmap thread stack, terminating thread anyway. Error code: {}", unmap_result.error());
  71. }
  72. current_thread->exit(reinterpret_cast<void*>(exit_value.ptr()));
  73. VERIFY_NOT_REACHED();
  74. }
  75. ErrorOr<FlatPtr> Process::sys$detach_thread(pid_t tid)
  76. {
  77. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  78. REQUIRE_PROMISE(thread);
  79. auto thread = Thread::from_tid(tid);
  80. if (!thread || thread->pid() != pid())
  81. return ESRCH;
  82. if (!thread->is_joinable())
  83. return EINVAL;
  84. thread->detach();
  85. return 0;
  86. }
  87. ErrorOr<FlatPtr> Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
  88. {
  89. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  90. REQUIRE_PROMISE(thread);
  91. auto thread = Thread::from_tid(tid);
  92. if (!thread || thread->pid() != pid())
  93. return ESRCH;
  94. auto* current_thread = Thread::current();
  95. if (thread == current_thread)
  96. return EDEADLK;
  97. void* joinee_exit_value = nullptr;
  98. // NOTE: pthread_join() cannot be interrupted by signals. Only by death.
  99. for (;;) {
  100. ErrorOr<void> try_join_result;
  101. auto result = current_thread->block<Thread::JoinBlocker>({}, *thread, try_join_result, joinee_exit_value);
  102. if (result == Thread::BlockResult::NotBlocked) {
  103. if (try_join_result.is_error())
  104. return try_join_result.release_error();
  105. break;
  106. }
  107. if (result == Thread::BlockResult::InterruptedByDeath)
  108. break;
  109. dbgln("join_thread: retrying");
  110. }
  111. if (exit_value)
  112. TRY(copy_to_user(exit_value, &joinee_exit_value));
  113. return 0;
  114. }
  115. ErrorOr<FlatPtr> Process::sys$kill_thread(pid_t tid, int signal)
  116. {
  117. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  118. REQUIRE_PROMISE(thread);
  119. if (signal < 0 || signal >= 32)
  120. return EINVAL;
  121. auto thread = Thread::from_tid(tid);
  122. if (!thread || thread->pid() != pid())
  123. return ESRCH;
  124. if (signal != 0)
  125. thread->send_signal(signal, &Process::current());
  126. return 0;
  127. }
  128. ErrorOr<FlatPtr> Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
  129. {
  130. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  131. REQUIRE_PROMISE(stdio);
  132. auto name = TRY(try_copy_kstring_from_user(user_name, user_name_length));
  133. const size_t max_thread_name_size = 64;
  134. if (name->length() > max_thread_name_size)
  135. return EINVAL;
  136. auto thread = Thread::from_tid(tid);
  137. if (!thread || thread->pid() != pid())
  138. return ESRCH;
  139. thread->set_name(move(name));
  140. return 0;
  141. }
  142. ErrorOr<FlatPtr> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
  143. {
  144. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  145. REQUIRE_PROMISE(thread);
  146. if (buffer_size == 0)
  147. return EINVAL;
  148. auto thread = Thread::from_tid(tid);
  149. if (!thread || thread->pid() != pid())
  150. return ESRCH;
  151. SpinlockLocker locker(thread->get_lock());
  152. auto thread_name = thread->name();
  153. if (thread_name.is_null()) {
  154. char null_terminator = '\0';
  155. TRY(copy_to_user(buffer, &null_terminator, sizeof(null_terminator)));
  156. return 0;
  157. }
  158. if (thread_name.length() + 1 > buffer_size)
  159. return ENAMETOOLONG;
  160. TRY(copy_to_user(buffer, thread_name.characters_without_null_termination(), thread_name.length() + 1));
  161. return 0;
  162. }
  163. ErrorOr<FlatPtr> Process::sys$gettid()
  164. {
  165. VERIFY_NO_PROCESS_BIG_LOCK(this)
  166. REQUIRE_PROMISE(stdio);
  167. return Thread::current()->tid().value();
  168. }
  169. }