thread.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Checked.h>
  27. #include <AK/String.h>
  28. #include <AK/StringBuilder.h>
  29. #include <AK/StringView.h>
  30. #include <Kernel/Process.h>
  31. #include <Kernel/VM/MemoryManager.h>
  32. #include <Kernel/VM/PageDirectory.h>
  33. namespace Kernel {
  34. int Process::sys$create_thread(void* (*entry)(void*), Userspace<const Syscall::SC_create_thread_params*> user_params)
  35. {
  36. REQUIRE_PROMISE(thread);
  37. Syscall::SC_create_thread_params params;
  38. if (!copy_from_user(&params, user_params))
  39. return -EFAULT;
  40. unsigned detach_state = params.m_detach_state;
  41. int schedule_priority = params.m_schedule_priority;
  42. unsigned stack_size = params.m_stack_size;
  43. if (Checked<FlatPtr>::addition_would_overflow((FlatPtr)params.m_stack_location, stack_size))
  44. return -EOVERFLOW;
  45. auto user_stack_address = (u8*)params.m_stack_location + stack_size;
  46. if (!MM.validate_user_stack(*this, VirtualAddress(user_stack_address - 4)))
  47. return -EFAULT;
  48. // FIXME: return EAGAIN if Thread::all_threads().size() is greater than PTHREAD_THREADS_MAX
  49. int requested_thread_priority = schedule_priority;
  50. if (requested_thread_priority < THREAD_PRIORITY_MIN || requested_thread_priority > THREAD_PRIORITY_MAX)
  51. return -EINVAL;
  52. bool is_thread_joinable = (0 == detach_state);
  53. // FIXME: Do something with guard pages?
  54. auto thread_or_error = Thread::try_create(*this);
  55. if (thread_or_error.is_error())
  56. return thread_or_error.error();
  57. auto& thread = thread_or_error.value();
  58. // We know this thread is not the main_thread,
  59. // So give it a unique name until the user calls $set_thread_name on it
  60. // length + 4 to give space for our extra junk at the end
  61. StringBuilder builder(m_name.length() + 4);
  62. thread->set_name(String::formatted("{} [{}]", m_name, thread->tid().value()));
  63. if (!is_thread_joinable)
  64. thread->detach();
  65. auto& tss = thread->tss();
  66. tss.eip = (FlatPtr)entry;
  67. tss.eflags = 0x0202;
  68. tss.cr3 = space().page_directory().cr3();
  69. tss.esp = (FlatPtr)user_stack_address;
  70. auto tsr_result = thread->make_thread_specific_region({});
  71. if (tsr_result.is_error())
  72. return tsr_result.error();
  73. ScopedSpinLock lock(g_scheduler_lock);
  74. thread->set_priority(requested_thread_priority);
  75. thread->set_state(Thread::State::Runnable);
  76. return thread->tid().value();
  77. }
  78. void Process::sys$exit_thread(Userspace<void*> exit_value)
  79. {
  80. REQUIRE_PROMISE(thread);
  81. cli();
  82. if (this->thread_count() == 1) {
  83. // If this is the last thread, instead kill the process.
  84. this->sys$exit(0);
  85. }
  86. Thread::current()->exit(reinterpret_cast<void*>(exit_value.ptr()));
  87. VERIFY_NOT_REACHED();
  88. }
  89. int Process::sys$detach_thread(pid_t tid)
  90. {
  91. REQUIRE_PROMISE(thread);
  92. auto thread = Thread::from_tid(tid);
  93. if (!thread || thread->pid() != pid())
  94. return -ESRCH;
  95. if (!thread->is_joinable())
  96. return -EINVAL;
  97. thread->detach();
  98. return 0;
  99. }
  100. int Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
  101. {
  102. REQUIRE_PROMISE(thread);
  103. auto thread = Thread::from_tid(tid);
  104. if (!thread || thread->pid() != pid())
  105. return -ESRCH;
  106. auto current_thread = Thread::current();
  107. if (thread == current_thread)
  108. return -EDEADLK;
  109. void* joinee_exit_value = nullptr;
  110. // NOTE: pthread_join() cannot be interrupted by signals. Only by death.
  111. for (;;) {
  112. KResult try_join_result(KSuccess);
  113. auto result = current_thread->block<Thread::JoinBlocker>({}, *thread, try_join_result, joinee_exit_value);
  114. if (result == Thread::BlockResult::NotBlocked) {
  115. if (try_join_result.is_error())
  116. return try_join_result.error();
  117. break;
  118. }
  119. if (result == Thread::BlockResult::InterruptedByDeath)
  120. break;
  121. dbgln("join_thread: retrying");
  122. }
  123. if (exit_value && !copy_to_user(exit_value, &joinee_exit_value))
  124. return -EFAULT;
  125. return 0;
  126. }
  127. int Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
  128. {
  129. REQUIRE_PROMISE(stdio);
  130. auto name = copy_string_from_user(user_name, user_name_length);
  131. if (name.is_null())
  132. return -EFAULT;
  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. int Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
  143. {
  144. REQUIRE_PROMISE(thread);
  145. if (buffer_size == 0)
  146. return -EINVAL;
  147. auto thread = Thread::from_tid(tid);
  148. if (!thread || thread->pid() != pid())
  149. return -ESRCH;
  150. // We must make a temporary copy here to avoid a race with sys$set_thread_name
  151. auto thread_name = thread->name();
  152. if (thread_name.length() + 1 > (size_t)buffer_size)
  153. return -ENAMETOOLONG;
  154. if (!copy_to_user(buffer, thread_name.characters(), thread_name.length() + 1))
  155. return -EFAULT;
  156. return 0;
  157. }
  158. int Process::sys$gettid()
  159. {
  160. REQUIRE_PROMISE(stdio);
  161. return Thread::current()->tid().value();
  162. }
  163. }