Syscall.cpp 8.1 KB

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