Syscall.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 ErrorOr<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. using Handler = auto (Process::*)(FlatPtr, FlatPtr, FlatPtr, FlatPtr) -> ErrorOr<FlatPtr>;
  84. using HandlerWithRegisterState = auto (Process::*)(RegisterState&) -> ErrorOr<FlatPtr>;
  85. struct HandlerMetadata {
  86. Handler handler;
  87. NeedsBigProcessLock needs_lock;
  88. };
  89. #define __ENUMERATE_SYSCALL(sys_call, needs_lock) { bit_cast<Handler>(&Process::sys$##sys_call), needs_lock },
  90. static const HandlerMetadata s_syscall_table[] = {
  91. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  92. };
  93. #undef __ENUMERATE_SYSCALL
  94. ErrorOr<FlatPtr> handle(RegisterState& regs, FlatPtr function, FlatPtr arg1, FlatPtr arg2, FlatPtr arg3, FlatPtr arg4)
  95. {
  96. VERIFY_INTERRUPTS_ENABLED();
  97. auto* current_thread = Thread::current();
  98. auto& process = current_thread->process();
  99. current_thread->did_syscall();
  100. PerformanceManager::add_syscall_event(*current_thread, regs);
  101. if (function >= Function::__Count) {
  102. dbgln("Unknown syscall {} requested ({:p}, {:p}, {:p}, {:p})", function, arg1, arg2, arg3, arg4);
  103. return ENOSYS;
  104. }
  105. const auto syscall_metadata = s_syscall_table[function];
  106. if (syscall_metadata.handler == nullptr) {
  107. dbgln("Null syscall {} requested, you probably need to rebuild this program!", function);
  108. return ENOSYS;
  109. }
  110. MutexLocker mutex_locker;
  111. const auto needs_big_lock = syscall_metadata.needs_lock == NeedsBigProcessLock::Yes;
  112. if (needs_big_lock) {
  113. mutex_locker.attach_and_lock(process.big_lock());
  114. };
  115. if (function == SC_exit || function == SC_exit_thread) {
  116. // These syscalls need special handling since they never return to the caller.
  117. // In these cases the process big lock will get released on the exit of the thread.
  118. if (auto* tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  119. regs.set_return_reg(0);
  120. tracer->set_trace_syscalls(false);
  121. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  122. }
  123. switch (function) {
  124. case SC_exit:
  125. process.sys$exit(arg1);
  126. case SC_exit_thread:
  127. process.sys$exit_thread(arg1, arg2, arg3);
  128. default:
  129. VERIFY_NOT_REACHED();
  130. }
  131. }
  132. ErrorOr<FlatPtr> result { FlatPtr(nullptr) };
  133. if (function == SC_fork || function == SC_sigreturn) {
  134. // These syscalls want the RegisterState& rather than individual parameters.
  135. auto handler = bit_cast<HandlerWithRegisterState>(syscall_metadata.handler);
  136. result = (process.*(handler))(regs);
  137. } else {
  138. result = (process.*(syscall_metadata.handler))(arg1, arg2, arg3, arg4);
  139. }
  140. return result;
  141. }
  142. }
  143. NEVER_INLINE void syscall_handler(TrapFrame* trap)
  144. {
  145. // Make sure SMAP protection is enabled on syscall entry.
  146. clac();
  147. auto& regs = *trap->regs;
  148. auto* current_thread = Thread::current();
  149. VERIFY(current_thread->previous_mode() == Thread::PreviousMode::UserMode);
  150. auto& process = current_thread->process();
  151. if (process.is_dying()) {
  152. // It's possible this thread is just about to make a syscall while another is
  153. // is killing our process.
  154. current_thread->die_if_needed();
  155. return;
  156. }
  157. if (auto* tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  158. tracer->set_trace_syscalls(false);
  159. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  160. }
  161. current_thread->yield_if_stopped();
  162. // Apply a random offset in the range 0-255 to the stack pointer,
  163. // to make kernel stacks a bit less deterministic.
  164. u32 lsw;
  165. u32 msw;
  166. read_tsc(lsw, msw);
  167. auto* ptr = (char*)__builtin_alloca(lsw & 0xff);
  168. asm volatile(""
  169. : "=m"(*ptr));
  170. static constexpr FlatPtr iopl_mask = 3u << 12;
  171. FlatPtr flags = regs.flags();
  172. if ((flags & (iopl_mask)) != 0) {
  173. PANIC("Syscall from process with IOPL != 0");
  174. }
  175. Memory::MemoryManager::validate_syscall_preconditions(process.address_space(), regs);
  176. FlatPtr function;
  177. FlatPtr arg1;
  178. FlatPtr arg2;
  179. FlatPtr arg3;
  180. FlatPtr arg4;
  181. regs.capture_syscall_params(function, arg1, arg2, arg3, arg4);
  182. auto result = Syscall::handle(regs, function, arg1, arg2, arg3, arg4);
  183. if (result.is_error()) {
  184. regs.set_return_reg(-result.error().code());
  185. } else {
  186. regs.set_return_reg(result.value());
  187. }
  188. if (auto* tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  189. tracer->set_trace_syscalls(false);
  190. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  191. }
  192. current_thread->yield_if_stopped();
  193. current_thread->check_dispatch_pending_signal();
  194. // If the previous mode somehow changed something is seriously messed up...
  195. VERIFY(current_thread->previous_mode() == Thread::PreviousMode::UserMode);
  196. // Check if we're supposed to return to userspace or just die.
  197. current_thread->die_if_needed();
  198. // Crash any processes which have commited a promise violation during syscall handling.
  199. if (result.is_error() && result.error().code() == EPROMISEVIOLATION) {
  200. VERIFY(current_thread->is_promise_violation_pending());
  201. current_thread->set_promise_violation_pending(false);
  202. process.crash(SIGABRT, 0);
  203. } else {
  204. VERIFY(!current_thread->is_promise_violation_pending());
  205. }
  206. VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
  207. }
  208. }