Syscall.cpp 8.1 KB

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