Syscall.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/API/Syscall.h>
  27. #include <Kernel/Arch/i386/CPU.h>
  28. #include <Kernel/Panic.h>
  29. #include <Kernel/Process.h>
  30. #include <Kernel/ThreadTracer.h>
  31. #include <Kernel/VM/MemoryManager.h>
  32. namespace Kernel {
  33. extern "C" void syscall_handler(TrapFrame*);
  34. extern "C" void syscall_asm_entry();
  35. // clang-format off
  36. asm(
  37. ".globl syscall_asm_entry\n"
  38. "syscall_asm_entry:\n"
  39. " pushl $0x0\n"
  40. " pusha\n"
  41. " pushl %ds\n"
  42. " pushl %es\n"
  43. " pushl %fs\n"
  44. " pushl %gs\n"
  45. " pushl %ss\n"
  46. " mov $" __STRINGIFY(GDT_SELECTOR_DATA0) ", %ax\n"
  47. " mov %ax, %ds\n"
  48. " mov %ax, %es\n"
  49. " mov $" __STRINGIFY(GDT_SELECTOR_PROC) ", %ax\n"
  50. " mov %ax, %fs\n"
  51. " cld\n"
  52. " xor %esi, %esi\n"
  53. " xor %edi, %edi\n"
  54. " pushl %esp \n" // set TrapFrame::regs
  55. " subl $" __STRINGIFY(TRAP_FRAME_SIZE - 4) ", %esp \n"
  56. " movl %esp, %ebx \n"
  57. " pushl %ebx \n" // push pointer to TrapFrame
  58. " call enter_trap_no_irq \n"
  59. " movl %ebx, 0(%esp) \n" // push pointer to TrapFrame
  60. " call syscall_handler \n"
  61. " movl %ebx, 0(%esp) \n" // push pointer to TrapFrame
  62. " jmp common_trap_exit \n");
  63. // clang-format on
  64. namespace Syscall {
  65. static KResultOr<FlatPtr> handle(RegisterState&, FlatPtr function, FlatPtr arg1, FlatPtr arg2, FlatPtr arg3);
  66. UNMAP_AFTER_INIT void initialize()
  67. {
  68. register_user_callable_interrupt_handler(syscall_vector, syscall_asm_entry);
  69. klog() << "Syscall: int 0x82 handler installed";
  70. }
  71. #pragma GCC diagnostic ignored "-Wcast-function-type"
  72. typedef KResultOr<FlatPtr> (Process::*Handler)(FlatPtr, FlatPtr, FlatPtr);
  73. typedef KResultOr<FlatPtr> (Process::*HandlerWithRegisterState)(RegisterState&);
  74. #define __ENUMERATE_SYSCALL(x) reinterpret_cast<Handler>(&Process::sys$##x),
  75. static Handler s_syscall_table[] = {
  76. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  77. };
  78. #undef __ENUMERATE_SYSCALL
  79. KResultOr<FlatPtr> handle(RegisterState& regs, FlatPtr function, FlatPtr arg1, FlatPtr arg2, FlatPtr arg3)
  80. {
  81. VERIFY_INTERRUPTS_ENABLED();
  82. auto current_thread = Thread::current();
  83. auto& process = current_thread->process();
  84. current_thread->did_syscall();
  85. if (function == SC_abort || function == SC_exit || function == SC_exit_thread) {
  86. // These syscalls need special handling since they never return to the caller.
  87. if (auto* tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  88. regs.eax = 0;
  89. tracer->set_trace_syscalls(false);
  90. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  91. }
  92. switch (function) {
  93. case SC_abort:
  94. process.sys$abort();
  95. break;
  96. case SC_exit:
  97. process.sys$exit(arg1);
  98. break;
  99. case SC_exit_thread:
  100. process.sys$exit_thread(arg1);
  101. break;
  102. default:
  103. VERIFY_NOT_REACHED();
  104. }
  105. }
  106. if (function == SC_fork || function == SC_sigreturn) {
  107. // These syscalls want the RegisterState& rather than individual parameters.
  108. auto handler = (HandlerWithRegisterState)s_syscall_table[function];
  109. return (process.*(handler))(regs);
  110. }
  111. if (function >= Function::__Count) {
  112. dbgln("Unknown syscall {} requested ({:08x}, {:08x}, {:08x})", function, arg1, arg2, arg3);
  113. return -ENOSYS;
  114. }
  115. if (s_syscall_table[function] == nullptr) {
  116. dbgln("Null syscall {} requested, you probably need to rebuild this program!", function);
  117. return -ENOSYS;
  118. }
  119. return (process.*(s_syscall_table[function]))(arg1, arg2, arg3);
  120. }
  121. }
  122. void syscall_handler(TrapFrame* trap)
  123. {
  124. auto& regs = *trap->regs;
  125. auto current_thread = Thread::current();
  126. VERIFY(current_thread->previous_mode() == Thread::PreviousMode::UserMode);
  127. auto& process = current_thread->process();
  128. if (auto tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  129. tracer->set_trace_syscalls(false);
  130. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  131. }
  132. current_thread->yield_if_stopped();
  133. // Make sure SMAP protection is enabled on syscall entry.
  134. clac();
  135. // Apply a random offset in the range 0-255 to the stack pointer,
  136. // to make kernel stacks a bit less deterministic.
  137. u32 lsw;
  138. u32 msw;
  139. read_tsc(lsw, msw);
  140. auto* ptr = (char*)__builtin_alloca(lsw & 0xff);
  141. asm volatile(""
  142. : "=m"(*ptr));
  143. static constexpr FlatPtr iopl_mask = 3u << 12;
  144. if ((regs.eflags & (iopl_mask)) != 0) {
  145. PANIC("Syscall from process with IOPL != 0");
  146. }
  147. // NOTE: We take the big process lock before inspecting memory regions.
  148. process.big_lock().lock();
  149. if (!MM.validate_user_stack(process, VirtualAddress(regs.userspace_esp))) {
  150. dbgln("Invalid stack pointer: {:p}", regs.userspace_esp);
  151. handle_crash(regs, "Bad stack on syscall entry", SIGSTKFLT);
  152. }
  153. auto* calling_region = MM.find_region_from_vaddr(process.space(), VirtualAddress(regs.eip));
  154. if (!calling_region) {
  155. dbgln("Syscall from {:p} which has no associated region", regs.eip);
  156. handle_crash(regs, "Syscall from unknown region", SIGSEGV);
  157. }
  158. if (calling_region->is_writable()) {
  159. dbgln("Syscall from writable memory at {:p}", regs.eip);
  160. handle_crash(regs, "Syscall from writable memory", SIGSEGV);
  161. }
  162. if (process.space().enforces_syscall_regions() && !calling_region->is_syscall_region()) {
  163. dbgln("Syscall from non-syscall region");
  164. handle_crash(regs, "Syscall from non-syscall region", SIGSEGV);
  165. }
  166. auto function = regs.eax;
  167. auto arg1 = regs.edx;
  168. auto arg2 = regs.ecx;
  169. auto arg3 = regs.ebx;
  170. auto result = Syscall::handle(regs, function, arg1, arg2, arg3);
  171. if (result.is_error())
  172. regs.eax = result.error();
  173. else
  174. regs.eax = result.value();
  175. process.big_lock().unlock();
  176. if (auto tracer = process.tracer(); tracer && tracer->is_tracing_syscalls()) {
  177. tracer->set_trace_syscalls(false);
  178. process.tracer_trap(*current_thread, regs); // this triggers SIGTRAP and stops the thread!
  179. }
  180. current_thread->yield_if_stopped();
  181. current_thread->check_dispatch_pending_signal();
  182. // If the previous mode somehow changed something is seriously messed up...
  183. VERIFY(current_thread->previous_mode() == Thread::PreviousMode::UserMode);
  184. // Check if we're supposed to return to userspace or just die.
  185. current_thread->die_if_needed();
  186. VERIFY(!g_scheduler_lock.own_lock());
  187. }
  188. }