Syscall.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/CPU.h>
  8. #include <Kernel/Panic.h>
  9. #include <Kernel/Process.h>
  10. #include <Kernel/ThreadTracer.h>
  11. #include <Kernel/VM/MemoryManager.h>
  12. namespace Kernel {
  13. extern "C" void syscall_handler(TrapFrame*) __attribute__((used));
  14. extern "C" void syscall_asm_entry();
  15. static void syscall_asm_entry_dummy() __attribute__((used));
  16. NEVER_INLINE void syscall_asm_entry_dummy()
  17. {
  18. // clang-format off
  19. #if ARCH(I386)
  20. asm(
  21. ".globl syscall_asm_entry\n"
  22. "syscall_asm_entry:\n"
  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, %fs\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. ".globl syscall_asm_entry\n"
  50. "syscall_asm_entry:\n"
  51. " cli\n"
  52. " hlt\n");
  53. #endif
  54. // clang-format on
  55. }
  56. namespace Syscall {
  57. static KResultOr<FlatPtr> handle(RegisterState&, FlatPtr function, FlatPtr arg1, FlatPtr arg2, FlatPtr arg3);
  58. UNMAP_AFTER_INIT void initialize()
  59. {
  60. register_user_callable_interrupt_handler(syscall_vector, syscall_asm_entry);
  61. }
  62. #pragma GCC diagnostic ignored "-Wcast-function-type"
  63. typedef KResultOr<FlatPtr> (Process::*Handler)(FlatPtr, FlatPtr, FlatPtr);
  64. typedef KResultOr<FlatPtr> (Process::*HandlerWithRegisterState)(RegisterState&);
  65. #define __ENUMERATE_SYSCALL(x) reinterpret_cast<Handler>(&Process::sys$##x),
  66. static const Handler s_syscall_table[] = {
  67. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  68. };
  69. #undef __ENUMERATE_SYSCALL
  70. KResultOr<FlatPtr> handle(RegisterState& regs, FlatPtr function, FlatPtr arg1, FlatPtr arg2, FlatPtr arg3)
  71. {
  72. VERIFY_INTERRUPTS_ENABLED();
  73. auto current_thread = Thread::current();
  74. auto& process = current_thread->process();
  75. current_thread->did_syscall();
  76. if (function == SC_exit || function == SC_exit_thread) {
  77. // These syscalls need special handling since they never return to the caller.
  78. if (auto* tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  79. regs.eax = 0;
  80. tracer->set_trace_syscalls(false);
  81. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  82. }
  83. switch (function) {
  84. case SC_exit:
  85. process.sys$exit(arg1);
  86. break;
  87. case SC_exit_thread:
  88. process.sys$exit_thread(arg1, arg2, arg3);
  89. break;
  90. default:
  91. VERIFY_NOT_REACHED();
  92. }
  93. }
  94. if (function == SC_fork || function == SC_sigreturn) {
  95. // These syscalls want the RegisterState& rather than individual parameters.
  96. auto handler = (HandlerWithRegisterState)s_syscall_table[function];
  97. return (process.*(handler))(regs);
  98. }
  99. if (function >= Function::__Count) {
  100. dbgln("Unknown syscall {} requested ({:08x}, {:08x}, {:08x})", function, arg1, arg2, arg3);
  101. return ENOSYS;
  102. }
  103. if (s_syscall_table[function] == nullptr) {
  104. dbgln("Null syscall {} requested, you probably need to rebuild this program!", function);
  105. return ENOSYS;
  106. }
  107. // This appears to be a bogus warning, as s_syscall_table is always
  108. // initialized, and the index (function) is always bounded.
  109. // TODO: Figure out how to avoid the suppression.
  110. #pragma GCC diagnostic push
  111. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  112. return (process.*(s_syscall_table[function]))(arg1, arg2, arg3);
  113. #pragma GCC diagnostic pop
  114. }
  115. }
  116. NEVER_INLINE void syscall_handler(TrapFrame* trap)
  117. {
  118. auto& regs = *trap->regs;
  119. auto current_thread = Thread::current();
  120. VERIFY(current_thread->previous_mode() == Thread::PreviousMode::UserMode);
  121. auto& process = current_thread->process();
  122. if (auto tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  123. tracer->set_trace_syscalls(false);
  124. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  125. }
  126. current_thread->yield_if_stopped();
  127. // Make sure SMAP protection is enabled on syscall entry.
  128. clac();
  129. // Apply a random offset in the range 0-255 to the stack pointer,
  130. // to make kernel stacks a bit less deterministic.
  131. u32 lsw;
  132. u32 msw;
  133. read_tsc(lsw, msw);
  134. auto* ptr = (char*)__builtin_alloca(lsw & 0xff);
  135. asm volatile(""
  136. : "=m"(*ptr));
  137. static constexpr FlatPtr iopl_mask = 3u << 12;
  138. if ((regs.eflags & (iopl_mask)) != 0) {
  139. PANIC("Syscall from process with IOPL != 0");
  140. }
  141. // NOTE: We take the big process lock before inspecting memory regions.
  142. process.big_lock().lock();
  143. if (!MM.validate_user_stack(process, VirtualAddress(regs.userspace_esp))) {
  144. dbgln("Invalid stack pointer: {:p}", regs.userspace_esp);
  145. handle_crash(regs, "Bad stack on syscall entry", SIGSTKFLT);
  146. }
  147. auto* calling_region = MM.find_user_region_from_vaddr(process.space(), VirtualAddress(regs.eip));
  148. if (!calling_region) {
  149. dbgln("Syscall from {:p} which has no associated region", regs.eip);
  150. handle_crash(regs, "Syscall from unknown region", SIGSEGV);
  151. }
  152. if (calling_region->is_writable()) {
  153. dbgln("Syscall from writable memory at {:p}", regs.eip);
  154. handle_crash(regs, "Syscall from writable memory", SIGSEGV);
  155. }
  156. if (process.space().enforces_syscall_regions() && !calling_region->is_syscall_region()) {
  157. dbgln("Syscall from non-syscall region");
  158. handle_crash(regs, "Syscall from non-syscall region", SIGSEGV);
  159. }
  160. auto function = regs.eax;
  161. auto arg1 = regs.edx;
  162. auto arg2 = regs.ecx;
  163. auto arg3 = regs.ebx;
  164. auto result = Syscall::handle(regs, function, arg1, arg2, arg3);
  165. if (result.is_error())
  166. regs.eax = result.error();
  167. else
  168. regs.eax = result.value();
  169. process.big_lock().unlock();
  170. if (auto tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  171. tracer->set_trace_syscalls(false);
  172. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  173. }
  174. current_thread->yield_if_stopped();
  175. current_thread->check_dispatch_pending_signal();
  176. // If the previous mode somehow changed something is seriously messed up...
  177. VERIFY(current_thread->previous_mode() == Thread::PreviousMode::UserMode);
  178. // Check if we're supposed to return to userspace or just die.
  179. current_thread->die_if_needed();
  180. VERIFY(!g_scheduler_lock.own_lock());
  181. }
  182. }