Syscall.cpp 7.0 KB

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