thread.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. // FIXME: Don't make a temporary String here
  35. auto new_thread_name = TRY(KString::try_create(String::formatted("{} [{}]", m_name, thread->tid().value())));
  36. thread->set_name(move(new_thread_name));
  37. if (!is_thread_joinable)
  38. thread->detach();
  39. auto& regs = thread->regs();
  40. regs.set_ip((FlatPtr)entry);
  41. regs.set_flags(0x0202);
  42. regs.set_sp(user_sp.value());
  43. #if ARCH(X86_64)
  44. regs.rdi = params.rdi;
  45. regs.rsi = params.rsi;
  46. regs.rdx = params.rdx;
  47. regs.rcx = params.rcx;
  48. #endif
  49. regs.cr3 = address_space().page_directory().cr3();
  50. TRY(thread->make_thread_specific_region({}));
  51. PerformanceManager::add_thread_created_event(*thread);
  52. SpinlockLocker lock(g_scheduler_lock);
  53. thread->set_priority(requested_thread_priority);
  54. thread->set_state(Thread::State::Runnable);
  55. return thread->tid().value();
  56. }
  57. void Process::sys$exit_thread(Userspace<void*> exit_value, Userspace<void*> stack_location, size_t stack_size)
  58. {
  59. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  60. REQUIRE_PROMISE(thread);
  61. if (this->thread_count() == 1) {
  62. // If this is the last thread, instead kill the process.
  63. this->sys$exit(0);
  64. }
  65. auto current_thread = Thread::current();
  66. current_thread->set_profiling_suppressed();
  67. PerformanceManager::add_thread_exit_event(*current_thread);
  68. if (stack_location) {
  69. auto unmap_result = address_space().unmap_mmap_range(stack_location.vaddr(), stack_size);
  70. if (unmap_result.is_error())
  71. dbgln("Failed to unmap thread stack, terminating thread anyway. Error code: {}", unmap_result.error());
  72. }
  73. current_thread->exit(reinterpret_cast<void*>(exit_value.ptr()));
  74. VERIFY_NOT_REACHED();
  75. }
  76. ErrorOr<FlatPtr> Process::sys$detach_thread(pid_t tid)
  77. {
  78. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  79. REQUIRE_PROMISE(thread);
  80. auto thread = Thread::from_tid(tid);
  81. if (!thread || thread->pid() != pid())
  82. return ESRCH;
  83. if (!thread->is_joinable())
  84. return EINVAL;
  85. thread->detach();
  86. return 0;
  87. }
  88. ErrorOr<FlatPtr> Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
  89. {
  90. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  91. REQUIRE_PROMISE(thread);
  92. auto thread = Thread::from_tid(tid);
  93. if (!thread || thread->pid() != pid())
  94. return ESRCH;
  95. auto current_thread = Thread::current();
  96. if (thread == current_thread)
  97. return EDEADLK;
  98. void* joinee_exit_value = nullptr;
  99. // NOTE: pthread_join() cannot be interrupted by signals. Only by death.
  100. for (;;) {
  101. ErrorOr<void> try_join_result;
  102. auto result = current_thread->block<Thread::JoinBlocker>({}, *thread, try_join_result, joinee_exit_value);
  103. if (result == Thread::BlockResult::NotBlocked) {
  104. if (try_join_result.is_error())
  105. return try_join_result.release_error();
  106. break;
  107. }
  108. if (result == Thread::BlockResult::InterruptedByDeath)
  109. break;
  110. dbgln("join_thread: retrying");
  111. }
  112. if (exit_value)
  113. TRY(copy_to_user(exit_value, &joinee_exit_value));
  114. return 0;
  115. }
  116. ErrorOr<FlatPtr> Process::sys$kill_thread(pid_t tid, int signal)
  117. {
  118. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  119. REQUIRE_PROMISE(thread);
  120. if (signal < 0 || signal >= 32)
  121. return EINVAL;
  122. auto thread = Thread::from_tid(tid);
  123. if (!thread || thread->pid() != pid())
  124. return ESRCH;
  125. if (signal != 0)
  126. thread->send_signal(signal, &Process::current());
  127. return 0;
  128. }
  129. ErrorOr<FlatPtr> Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
  130. {
  131. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  132. REQUIRE_PROMISE(stdio);
  133. auto name = TRY(try_copy_kstring_from_user(user_name, user_name_length));
  134. const size_t max_thread_name_size = 64;
  135. if (name->length() > max_thread_name_size)
  136. return EINVAL;
  137. auto thread = Thread::from_tid(tid);
  138. if (!thread || thread->pid() != pid())
  139. return ESRCH;
  140. thread->set_name(move(name));
  141. return 0;
  142. }
  143. ErrorOr<FlatPtr> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
  144. {
  145. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  146. REQUIRE_PROMISE(thread);
  147. if (buffer_size == 0)
  148. return EINVAL;
  149. auto thread = Thread::from_tid(tid);
  150. if (!thread || thread->pid() != pid())
  151. return ESRCH;
  152. SpinlockLocker locker(thread->get_lock());
  153. auto thread_name = thread->name();
  154. if (thread_name.is_null()) {
  155. char null_terminator = '\0';
  156. TRY(copy_to_user(buffer, &null_terminator, sizeof(null_terminator)));
  157. return 0;
  158. }
  159. if (thread_name.length() + 1 > buffer_size)
  160. return ENAMETOOLONG;
  161. TRY(copy_to_user(buffer, thread_name.characters_without_null_termination(), thread_name.length() + 1));
  162. return 0;
  163. }
  164. ErrorOr<FlatPtr> Process::sys$gettid()
  165. {
  166. VERIFY_NO_PROCESS_BIG_LOCK(this)
  167. REQUIRE_PROMISE(stdio);
  168. return Thread::current()->tid().value();
  169. }
  170. }