Syscall.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2018-2020, 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/Arch/i386/CPU.h>
  27. #include <Kernel/Process.h>
  28. #include <Kernel/Random.h>
  29. #include <Kernel/Syscall.h>
  30. #include <Kernel/ThreadTracer.h>
  31. #include <Kernel/VM/MemoryManager.h>
  32. namespace Kernel {
  33. extern "C" void syscall_handler(RegisterState&);
  34. extern "C" void syscall_asm_entry();
  35. asm(
  36. ".globl syscall_asm_entry\n"
  37. "syscall_asm_entry:\n"
  38. " pushl $0x0\n"
  39. " pusha\n"
  40. " pushl %ds\n"
  41. " pushl %es\n"
  42. " pushl %fs\n"
  43. " pushl %gs\n"
  44. " pushl %ss\n"
  45. " mov $0x10, %ax\n"
  46. " mov %ax, %ds\n"
  47. " mov %ax, %es\n"
  48. " cld\n"
  49. " xor %esi, %esi\n"
  50. " xor %edi, %edi\n"
  51. " push %esp\n"
  52. " call syscall_handler\n"
  53. " add $0x8, %esp\n"
  54. " popl %gs\n"
  55. " popl %fs\n"
  56. " popl %es\n"
  57. " popl %ds\n"
  58. " popa\n"
  59. " add $0x4, %esp\n"
  60. " iret\n");
  61. namespace Syscall {
  62. static int handle(RegisterState&, u32 function, u32 arg1, u32 arg2, u32 arg3);
  63. void initialize()
  64. {
  65. register_user_callable_interrupt_handler(syscall_vector, syscall_asm_entry);
  66. klog() << "Syscall: int 0x82 handler installed";
  67. }
  68. #pragma GCC diagnostic ignored "-Wcast-function-type"
  69. typedef int (Process::*Handler)(u32, u32, u32);
  70. #define __ENUMERATE_REMOVED_SYSCALL(x) nullptr,
  71. #define __ENUMERATE_SYSCALL(x) reinterpret_cast<Handler>(&Process::sys$##x),
  72. static Handler s_syscall_table[] = {
  73. ENUMERATE_SYSCALLS
  74. };
  75. #undef __ENUMERATE_SYSCALL
  76. #undef __ENUMERATE_REMOVED_SYSCALL
  77. int handle(RegisterState& regs, u32 function, u32 arg1, u32 arg2, u32 arg3)
  78. {
  79. ASSERT_INTERRUPTS_ENABLED();
  80. auto& process = *Process::current;
  81. Thread::current->did_syscall();
  82. if (function == SC_exit || function == SC_exit_thread) {
  83. // These syscalls need special handling since they never return to the caller.
  84. cli();
  85. if (function == SC_exit)
  86. process.sys$exit((int)arg1);
  87. else
  88. process.sys$exit_thread((void*)arg1);
  89. ASSERT_NOT_REACHED();
  90. return 0;
  91. }
  92. if (function == SC_fork)
  93. return process.sys$fork(regs);
  94. if (function == SC_sigreturn)
  95. return process.sys$sigreturn(regs);
  96. if (function >= Function::__Count) {
  97. dbg() << process << ": Unknown syscall %u requested (" << arg1 << ", " << arg2 << ", " << arg3 << ")";
  98. return -ENOSYS;
  99. }
  100. if (s_syscall_table[function] == nullptr) {
  101. dbg() << process << ": Null syscall " << function << " requested: \"" << to_string((Function)function) << "\", you probably need to rebuild this program.";
  102. return -ENOSYS;
  103. }
  104. return (process.*(s_syscall_table[function]))(arg1, arg2, arg3);
  105. }
  106. }
  107. void syscall_handler(RegisterState& regs)
  108. {
  109. // Special handling of the "gettid" syscall since it's extremely hot.
  110. // FIXME: Remove this hack once userspace locks stop calling it so damn much.
  111. if (regs.eax == SC_gettid) {
  112. regs.eax = Process::current->sys$gettid();
  113. Thread::current->did_syscall();
  114. return;
  115. }
  116. if (Thread::current->tracer() && Thread::current->tracer()->is_tracing_syscalls()) {
  117. Thread::current->tracer()->set_trace_syscalls(false);
  118. Thread::current->tracer_trap(regs);
  119. }
  120. // Make sure SMAP protection is enabled on syscall entry.
  121. clac();
  122. // Apply a random offset in the range 0-255 to the stack pointer,
  123. // to make kernel stacks a bit less deterministic.
  124. auto* ptr = (char*)__builtin_alloca(get_fast_random<u8>());
  125. asm volatile(""
  126. : "=m"(*ptr));
  127. auto& process = *Process::current;
  128. if (!MM.validate_user_stack(process, VirtualAddress(regs.userspace_esp))) {
  129. dbg() << "Invalid stack pointer: " << String::format("%p", regs.userspace_esp);
  130. handle_crash(regs, "Bad stack on syscall entry", SIGSTKFLT);
  131. ASSERT_NOT_REACHED();
  132. }
  133. auto* calling_region = MM.region_from_vaddr(process, VirtualAddress(regs.eip));
  134. if (!calling_region) {
  135. dbg() << "Syscall from " << String::format("%p", regs.eip) << " which has no region";
  136. handle_crash(regs, "Syscall from unknown region", SIGSEGV);
  137. ASSERT_NOT_REACHED();
  138. }
  139. if (calling_region->is_writable()) {
  140. dbg() << "Syscall from writable memory at " << String::format("%p", regs.eip);
  141. handle_crash(regs, "Syscall from writable memory", SIGSEGV);
  142. ASSERT_NOT_REACHED();
  143. }
  144. process.big_lock().lock();
  145. u32 function = regs.eax;
  146. u32 arg1 = regs.edx;
  147. u32 arg2 = regs.ecx;
  148. u32 arg3 = regs.ebx;
  149. regs.eax = (u32)Syscall::handle(regs, function, arg1, arg2, arg3);
  150. if (Thread::current->tracer() && Thread::current->tracer()->is_tracing_syscalls()) {
  151. Thread::current->tracer()->set_trace_syscalls(false);
  152. Thread::current->tracer_trap(regs);
  153. }
  154. process.big_lock().unlock();
  155. // Check if we're supposed to return to userspace or just die.
  156. Thread::current->die_if_needed();
  157. if (Thread::current->has_unmasked_pending_signals())
  158. (void)Thread::current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal);
  159. }
  160. }