thread.cpp 5.8 KB

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