Syscall.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/API/Syscall.h>
  7. #include <Kernel/Arch/x86/Interrupts.h>
  8. #include <Kernel/Arch/x86/TrapFrame.h>
  9. #include <Kernel/Memory/MemoryManager.h>
  10. #include <Kernel/Panic.h>
  11. #include <Kernel/PerformanceManager.h>
  12. #include <Kernel/Process.h>
  13. #include <Kernel/Sections.h>
  14. #include <Kernel/ThreadTracer.h>
  15. namespace Kernel {
  16. extern "C" void syscall_handler(TrapFrame*) __attribute__((used));
  17. extern "C" void syscall_asm_entry();
  18. NEVER_INLINE NAKED void syscall_asm_entry()
  19. {
  20. // clang-format off
  21. #if ARCH(I386)
  22. asm(
  23. " pushl $0x0\n"
  24. " pusha\n"
  25. " pushl %ds\n"
  26. " pushl %es\n"
  27. " pushl %fs\n"
  28. " pushl %gs\n"
  29. " pushl %ss\n"
  30. " mov $" __STRINGIFY(GDT_SELECTOR_DATA0) ", %ax\n"
  31. " mov %ax, %ds\n"
  32. " mov %ax, %es\n"
  33. " mov $" __STRINGIFY(GDT_SELECTOR_PROC) ", %ax\n"
  34. " mov %ax, %gs\n"
  35. " cld\n"
  36. " xor %esi, %esi\n"
  37. " xor %edi, %edi\n"
  38. " pushl %esp \n" // set TrapFrame::regs
  39. " subl $" __STRINGIFY(TRAP_FRAME_SIZE - 4) ", %esp \n"
  40. " movl %esp, %ebx \n"
  41. " pushl %ebx \n" // push pointer to TrapFrame
  42. " call enter_trap_no_irq \n"
  43. " movl %ebx, 0(%esp) \n" // push pointer to TrapFrame
  44. " call syscall_handler \n"
  45. " movl %ebx, 0(%esp) \n" // push pointer to TrapFrame
  46. " jmp common_trap_exit \n");
  47. #elif ARCH(X86_64)
  48. asm(
  49. " pushq $0x0\n"
  50. " pushq %r15\n"
  51. " pushq %r14\n"
  52. " pushq %r13\n"
  53. " pushq %r12\n"
  54. " pushq %r11\n"
  55. " pushq %r10\n"
  56. " pushq %r9\n"
  57. " pushq %r8\n"
  58. " pushq %rax\n"
  59. " pushq %rcx\n"
  60. " pushq %rdx\n"
  61. " pushq %rbx\n"
  62. " pushq %rsp\n"
  63. " pushq %rbp\n"
  64. " pushq %rsi\n"
  65. " pushq %rdi\n"
  66. " pushq %rsp \n" /* set TrapFrame::regs */
  67. " subq $" __STRINGIFY(TRAP_FRAME_SIZE - 8) ", %rsp \n"
  68. " movq %rsp, %rdi \n"
  69. " cld\n"
  70. " call enter_trap_no_irq \n"
  71. " movq %rsp, %rdi \n"
  72. " call syscall_handler\n"
  73. " jmp common_trap_exit \n");
  74. #endif
  75. // clang-format on
  76. }
  77. namespace Syscall {
  78. static KResultOr<FlatPtr> handle(RegisterState&, FlatPtr function, FlatPtr arg1, FlatPtr arg2, FlatPtr arg3, FlatPtr arg4);
  79. UNMAP_AFTER_INIT void initialize()
  80. {
  81. register_user_callable_interrupt_handler(syscall_vector, syscall_asm_entry);
  82. }
  83. #pragma GCC diagnostic ignored "-Wcast-function-type"
  84. typedef KResultOr<FlatPtr> (Process::*Handler)(FlatPtr, FlatPtr, FlatPtr, FlatPtr);
  85. typedef KResultOr<FlatPtr> (Process::*HandlerWithRegisterState)(RegisterState&);
  86. struct HandlerMetadata {
  87. Handler handler;
  88. NeedsBigProcessLock needs_lock;
  89. };
  90. #define __ENUMERATE_SYSCALL(sys_call, needs_lock) { reinterpret_cast<Handler>(&Process::sys$##sys_call), needs_lock },
  91. static const HandlerMetadata s_syscall_table[] = {
  92. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  93. };
  94. #undef __ENUMERATE_SYSCALL
  95. KResultOr<FlatPtr> handle(RegisterState& regs, FlatPtr function, FlatPtr arg1, FlatPtr arg2, FlatPtr arg3, FlatPtr arg4)
  96. {
  97. VERIFY_INTERRUPTS_ENABLED();
  98. auto current_thread = Thread::current();
  99. auto& process = current_thread->process();
  100. current_thread->did_syscall();
  101. PerformanceManager::add_syscall_event(*current_thread, regs);
  102. if (function >= Function::__Count) {
  103. dbgln("Unknown syscall {} requested ({:p}, {:p}, {:p}, {:p})", function, arg1, arg2, arg3, arg4);
  104. return ENOSYS;
  105. }
  106. const auto syscall_metadata = s_syscall_table[function];
  107. if (syscall_metadata.handler == nullptr) {
  108. dbgln("Null syscall {} requested, you probably need to rebuild this program!", function);
  109. return ENOSYS;
  110. }
  111. MutexLocker mutex_locker;
  112. const auto needs_big_lock = syscall_metadata.needs_lock == NeedsBigProcessLock::Yes;
  113. if (needs_big_lock) {
  114. mutex_locker.attach_and_lock(process.big_lock());
  115. };
  116. if (function == SC_exit || function == SC_exit_thread) {
  117. // These syscalls need special handling since they never return to the caller.
  118. // In these cases the process big lock will get released on the exit of the thread.
  119. if (auto* tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  120. regs.set_return_reg(0);
  121. tracer->set_trace_syscalls(false);
  122. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  123. }
  124. switch (function) {
  125. case SC_exit:
  126. process.sys$exit(arg1);
  127. break;
  128. case SC_exit_thread:
  129. process.sys$exit_thread(arg1, arg2, arg3);
  130. break;
  131. default:
  132. VERIFY_NOT_REACHED();
  133. }
  134. }
  135. KResultOr<FlatPtr> result { FlatPtr(nullptr) };
  136. if (function == SC_fork || function == SC_sigreturn) {
  137. // These syscalls want the RegisterState& rather than individual parameters.
  138. auto handler = (HandlerWithRegisterState)syscall_metadata.handler;
  139. result = (process.*(handler))(regs);
  140. } else {
  141. result = (process.*(syscall_metadata.handler))(arg1, arg2, arg3, arg4);
  142. }
  143. return result;
  144. }
  145. }
  146. NEVER_INLINE void syscall_handler(TrapFrame* trap)
  147. {
  148. auto& regs = *trap->regs;
  149. auto current_thread = Thread::current();
  150. VERIFY(current_thread->previous_mode() == Thread::PreviousMode::UserMode);
  151. auto& process = current_thread->process();
  152. if (process.is_dying()) {
  153. // It's possible this thread is just about to make a syscall while another is
  154. // is killing our process.
  155. current_thread->die_if_needed();
  156. return;
  157. }
  158. if (auto tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  159. tracer->set_trace_syscalls(false);
  160. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  161. }
  162. current_thread->yield_if_stopped();
  163. // Make sure SMAP protection is enabled on syscall entry.
  164. clac();
  165. // Apply a random offset in the range 0-255 to the stack pointer,
  166. // to make kernel stacks a bit less deterministic.
  167. u32 lsw;
  168. u32 msw;
  169. read_tsc(lsw, msw);
  170. auto* ptr = (char*)__builtin_alloca(lsw & 0xff);
  171. asm volatile(""
  172. : "=m"(*ptr));
  173. static constexpr FlatPtr iopl_mask = 3u << 12;
  174. FlatPtr flags = regs.flags();
  175. if ((flags & (iopl_mask)) != 0) {
  176. PANIC("Syscall from process with IOPL != 0");
  177. }
  178. MM.validate_syscall_preconditions(process.address_space(), regs);
  179. FlatPtr function;
  180. FlatPtr arg1;
  181. FlatPtr arg2;
  182. FlatPtr arg3;
  183. FlatPtr arg4;
  184. regs.capture_syscall_params(function, arg1, arg2, arg3, arg4);
  185. auto result = Syscall::handle(regs, function, arg1, arg2, arg3, arg4);
  186. if (result.is_error()) {
  187. regs.set_return_reg(result.error().error());
  188. } else {
  189. regs.set_return_reg(result.value());
  190. }
  191. if (auto tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  192. tracer->set_trace_syscalls(false);
  193. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  194. }
  195. current_thread->yield_if_stopped();
  196. current_thread->check_dispatch_pending_signal();
  197. // If the previous mode somehow changed something is seriously messed up...
  198. VERIFY(current_thread->previous_mode() == Thread::PreviousMode::UserMode);
  199. // Check if we're supposed to return to userspace or just die.
  200. current_thread->die_if_needed();
  201. VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
  202. }
  203. }