thread.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2018-2020, 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/String.h>
  27. #include <AK/StringBuilder.h>
  28. #include <AK/StringView.h>
  29. #include <Kernel/Process.h>
  30. #include <Kernel/VM/MemoryManager.h>
  31. #include <Kernel/VM/PageDirectory.h>
  32. namespace Kernel {
  33. int Process::sys$create_thread(void* (*entry)(void*), Userspace<const Syscall::SC_create_thread_params*> user_params)
  34. {
  35. REQUIRE_PROMISE(thread);
  36. if (!validate_read((const void*)entry, sizeof(void*)))
  37. return -EFAULT;
  38. Syscall::SC_create_thread_params params;
  39. if (!validate_read_and_copy_typed(&params, user_params))
  40. return -EFAULT;
  41. unsigned detach_state = params.m_detach_state;
  42. int schedule_priority = params.m_schedule_priority;
  43. Userspace<void*> stack_location = params.m_stack_location;
  44. unsigned stack_size = params.m_stack_size;
  45. if (!validate_write(stack_location, stack_size))
  46. return -EFAULT;
  47. u32 user_stack_address = reinterpret_cast<u32>(stack_location.ptr()) + stack_size;
  48. if (!MM.validate_user_stack(*this, VirtualAddress(user_stack_address - 4)))
  49. return -EFAULT;
  50. // FIXME: return EAGAIN if Thread::all_threads().size() is greater than PTHREAD_THREADS_MAX
  51. int requested_thread_priority = schedule_priority;
  52. if (requested_thread_priority < THREAD_PRIORITY_MIN || requested_thread_priority > THREAD_PRIORITY_MAX)
  53. return -EINVAL;
  54. bool is_thread_joinable = (0 == detach_state);
  55. // FIXME: Do something with guard pages?
  56. auto* thread = new Thread(*this);
  57. // We know this thread is not the main_thread,
  58. // So give it a unique name until the user calls $set_thread_name on it
  59. // length + 4 to give space for our extra junk at the end
  60. StringBuilder builder(m_name.length() + 4);
  61. builder.append(m_name);
  62. builder.appendf("[%d]", thread->tid().value());
  63. thread->set_name(builder.to_string());
  64. thread->set_priority(requested_thread_priority);
  65. thread->set_joinable(is_thread_joinable);
  66. auto& tss = thread->tss();
  67. tss.eip = (FlatPtr)entry;
  68. tss.eflags = 0x0202;
  69. tss.cr3 = page_directory().cr3();
  70. tss.esp = user_stack_address;
  71. thread->make_thread_specific_region({});
  72. thread->set_state(Thread::State::Runnable);
  73. return thread->tid().value();
  74. }
  75. void Process::sys$exit_thread(void* exit_value)
  76. {
  77. REQUIRE_PROMISE(thread);
  78. cli();
  79. auto current_thread = Thread::current();
  80. current_thread->m_exit_value = exit_value;
  81. current_thread->set_should_die();
  82. big_lock().force_unlock_if_locked();
  83. current_thread->die_if_needed();
  84. ASSERT_NOT_REACHED();
  85. }
  86. int Process::sys$detach_thread(pid_t tid)
  87. {
  88. REQUIRE_PROMISE(thread);
  89. InterruptDisabler disabler;
  90. auto* thread = Thread::from_tid(tid);
  91. if (!thread || thread->pid() != pid())
  92. return -ESRCH;
  93. if (!thread->is_joinable())
  94. return -EINVAL;
  95. thread->set_joinable(false);
  96. return 0;
  97. }
  98. int Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
  99. {
  100. REQUIRE_PROMISE(thread);
  101. if (exit_value && !validate_write_typed(exit_value))
  102. return -EFAULT;
  103. InterruptDisabler disabler;
  104. auto* thread = Thread::from_tid(tid);
  105. if (!thread || thread->pid() != pid())
  106. return -ESRCH;
  107. auto current_thread = Thread::current();
  108. if (thread == current_thread)
  109. return -EDEADLK;
  110. if (thread->m_joinee == current_thread)
  111. return -EDEADLK;
  112. ASSERT(thread->m_joiner != current_thread);
  113. if (thread->m_joiner)
  114. return -EINVAL;
  115. if (!thread->is_joinable())
  116. return -EINVAL;
  117. void* joinee_exit_value = nullptr;
  118. // NOTE: pthread_join() cannot be interrupted by signals. Only by death.
  119. for (;;) {
  120. auto result = current_thread->block<Thread::JoinBlocker>(nullptr, *thread, joinee_exit_value);
  121. if (result == Thread::BlockResult::InterruptedByDeath) {
  122. // NOTE: This cleans things up so that Thread::finalize() won't
  123. // get confused about a missing joiner when finalizing the joinee.
  124. InterruptDisabler disabler_t;
  125. if (current_thread->m_joinee) {
  126. current_thread->m_joinee->m_joiner = nullptr;
  127. current_thread->m_joinee = nullptr;
  128. }
  129. break;
  130. }
  131. }
  132. // NOTE: 'thread' is very possibly deleted at this point. Clear it just to be safe.
  133. thread = nullptr;
  134. if (exit_value)
  135. copy_to_user(exit_value, &joinee_exit_value);
  136. return 0;
  137. }
  138. int Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
  139. {
  140. REQUIRE_PROMISE(thread);
  141. auto name = validate_and_copy_string_from_user(user_name, user_name_length);
  142. if (name.is_null())
  143. return -EFAULT;
  144. const size_t max_thread_name_size = 64;
  145. if (name.length() > max_thread_name_size)
  146. return -EINVAL;
  147. InterruptDisabler disabler;
  148. auto* thread = Thread::from_tid(tid);
  149. if (!thread || thread->pid() != pid())
  150. return -ESRCH;
  151. thread->set_name(name);
  152. return 0;
  153. }
  154. int Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
  155. {
  156. REQUIRE_PROMISE(thread);
  157. if (buffer_size == 0)
  158. return -EINVAL;
  159. if (!validate_write(buffer, buffer_size))
  160. return -EFAULT;
  161. InterruptDisabler disabler;
  162. auto* thread = Thread::from_tid(tid);
  163. if (!thread || thread->pid() != pid())
  164. return -ESRCH;
  165. if (thread->name().length() + 1 > (size_t)buffer_size)
  166. return -ENAMETOOLONG;
  167. copy_to_user(buffer, thread->name().characters(), thread->name().length() + 1);
  168. return 0;
  169. }
  170. int Process::sys$gettid()
  171. {
  172. REQUIRE_PROMISE(stdio);
  173. return Thread::current()->tid().value();
  174. }
  175. }