thread.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <AK/StringView.h>
  10. #include <Kernel/Panic.h>
  11. #include <Kernel/PerformanceManager.h>
  12. #include <Kernel/Process.h>
  13. #include <Kernel/VM/MemoryManager.h>
  14. #include <Kernel/VM/PageDirectory.h>
  15. namespace Kernel {
  16. KResultOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<const Syscall::SC_create_thread_params*> user_params)
  17. {
  18. REQUIRE_PROMISE(thread);
  19. Syscall::SC_create_thread_params params;
  20. if (!copy_from_user(&params, user_params))
  21. return EFAULT;
  22. unsigned detach_state = params.m_detach_state;
  23. int schedule_priority = params.m_schedule_priority;
  24. unsigned stack_size = params.m_stack_size;
  25. auto user_esp = Checked<FlatPtr>((FlatPtr)params.m_stack_location);
  26. user_esp += stack_size;
  27. if (user_esp.has_overflow())
  28. return EOVERFLOW;
  29. if (!MM.validate_user_stack(*this, VirtualAddress(user_esp.value() - 4)))
  30. return EFAULT;
  31. // FIXME: return EAGAIN if Thread::all_threads().size() is greater than PTHREAD_THREADS_MAX
  32. int requested_thread_priority = schedule_priority;
  33. if (requested_thread_priority < THREAD_PRIORITY_MIN || requested_thread_priority > THREAD_PRIORITY_MAX)
  34. return EINVAL;
  35. bool is_thread_joinable = (0 == detach_state);
  36. // FIXME: Do something with guard pages?
  37. auto thread_or_error = Thread::try_create(*this);
  38. if (thread_or_error.is_error())
  39. return thread_or_error.error();
  40. auto& thread = thread_or_error.value();
  41. // We know this thread is not the main_thread,
  42. // So give it a unique name until the user calls $set_thread_name on it
  43. // length + 4 to give space for our extra junk at the end
  44. StringBuilder builder(m_name.length() + 4);
  45. thread->set_name(String::formatted("{} [{}]", m_name, thread->tid().value()));
  46. if (!is_thread_joinable)
  47. thread->detach();
  48. auto& regs = thread->regs();
  49. #if ARCH(I386)
  50. regs.eip = (FlatPtr)entry;
  51. regs.eflags = 0x0202;
  52. regs.esp = user_esp.value();
  53. #else
  54. regs.rip = (FlatPtr)entry;
  55. regs.rflags = 0x0202;
  56. regs.rsp = user_esp.value();
  57. #endif
  58. regs.cr3 = space().page_directory().cr3();
  59. auto tsr_result = thread->make_thread_specific_region({});
  60. if (tsr_result.is_error())
  61. return tsr_result.error();
  62. PerformanceManager::add_thread_created_event(*thread);
  63. ScopedSpinLock lock(g_scheduler_lock);
  64. thread->set_priority(requested_thread_priority);
  65. thread->set_state(Thread::State::Runnable);
  66. return thread->tid().value();
  67. }
  68. void Process::sys$exit_thread(Userspace<void*> exit_value, Userspace<void*> stack_location, size_t stack_size)
  69. {
  70. REQUIRE_PROMISE(thread);
  71. if (this->thread_count() == 1) {
  72. // If this is the last thread, instead kill the process.
  73. this->sys$exit(0);
  74. }
  75. auto current_thread = Thread::current();
  76. current_thread->set_profiling_suppressed();
  77. PerformanceManager::add_thread_exit_event(*current_thread);
  78. if (stack_location) {
  79. auto unmap_result = space().unmap_mmap_range(VirtualAddress { stack_location }, stack_size);
  80. if (unmap_result.is_error())
  81. dbgln("Failed to unmap thread stack, terminating thread anyway. Error code: {}", unmap_result.error());
  82. }
  83. current_thread->exit(reinterpret_cast<void*>(exit_value.ptr()));
  84. VERIFY_NOT_REACHED();
  85. }
  86. KResultOr<FlatPtr> Process::sys$detach_thread(pid_t tid)
  87. {
  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. REQUIRE_PROMISE(thread);
  100. auto thread = Thread::from_tid(tid);
  101. if (!thread || thread->pid() != pid())
  102. return ESRCH;
  103. auto current_thread = Thread::current();
  104. if (thread == current_thread)
  105. return EDEADLK;
  106. void* joinee_exit_value = nullptr;
  107. // NOTE: pthread_join() cannot be interrupted by signals. Only by death.
  108. for (;;) {
  109. KResult try_join_result(KSuccess);
  110. auto result = current_thread->block<Thread::JoinBlocker>({}, *thread, try_join_result, joinee_exit_value);
  111. if (result == Thread::BlockResult::NotBlocked) {
  112. if (try_join_result.is_error())
  113. return try_join_result.error();
  114. break;
  115. }
  116. if (result == Thread::BlockResult::InterruptedByDeath)
  117. break;
  118. dbgln("join_thread: retrying");
  119. }
  120. if (exit_value && !copy_to_user(exit_value, &joinee_exit_value))
  121. return EFAULT;
  122. return 0;
  123. }
  124. KResultOr<FlatPtr> Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
  125. {
  126. REQUIRE_PROMISE(stdio);
  127. auto name = copy_string_from_user(user_name, user_name_length);
  128. if (name.is_null())
  129. return EFAULT;
  130. const size_t max_thread_name_size = 64;
  131. if (name.length() > max_thread_name_size)
  132. return EINVAL;
  133. auto thread = Thread::from_tid(tid);
  134. if (!thread || thread->pid() != pid())
  135. return ESRCH;
  136. thread->set_name(move(name));
  137. return 0;
  138. }
  139. KResultOr<FlatPtr> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
  140. {
  141. REQUIRE_PROMISE(thread);
  142. if (buffer_size == 0)
  143. return EINVAL;
  144. auto thread = Thread::from_tid(tid);
  145. if (!thread || thread->pid() != pid())
  146. return ESRCH;
  147. // We must make a temporary copy here to avoid a race with sys$set_thread_name
  148. auto thread_name = thread->name();
  149. if (thread_name.length() + 1 > buffer_size)
  150. return ENAMETOOLONG;
  151. if (!copy_to_user(buffer, thread_name.characters(), thread_name.length() + 1))
  152. return EFAULT;
  153. return 0;
  154. }
  155. KResultOr<FlatPtr> Process::sys$gettid()
  156. {
  157. REQUIRE_PROMISE(stdio);
  158. return Thread::current()->tid().value();
  159. }
  160. }