Syscall.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/ProcessTracer.h>
  29. #include <Kernel/Random.h>
  30. #include <Kernel/Syscall.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(0x82, 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 (auto* tracer = process.tracer())
  86. tracer->did_syscall(function, arg1, arg2, arg3, 0);
  87. if (function == SC_exit)
  88. process.sys$exit((int)arg1);
  89. else
  90. process.sys$exit_thread((void*)arg1);
  91. ASSERT_NOT_REACHED();
  92. return 0;
  93. }
  94. if (function == SC_fork)
  95. return process.sys$fork(regs);
  96. if (function == SC_sigreturn)
  97. return process.sys$sigreturn(regs);
  98. if (function >= Function::__Count) {
  99. dbg() << process << ": Unknown syscall %u requested (" << arg1 << ", " << arg2 << ", " << arg3 << ")";
  100. return -ENOSYS;
  101. }
  102. if (s_syscall_table[function] == nullptr) {
  103. dbg() << process << ": Null syscall " << function << " requested: \"" << to_string((Function)function) << "\", you probably need to rebuild this program.";
  104. return -ENOSYS;
  105. }
  106. return (process.*(s_syscall_table[function]))(arg1, arg2, arg3);
  107. }
  108. }
  109. void syscall_handler(RegisterState& regs)
  110. {
  111. // Special handling of the "gettid" syscall since it's extremely hot.
  112. // FIXME: Remove this hack once userspace locks stop calling it so damn much.
  113. if (regs.eax == SC_gettid) {
  114. regs.eax = Process::current->sys$gettid();
  115. Thread::current->did_syscall();
  116. return;
  117. }
  118. // Make sure SMAP protection is enabled on syscall entry.
  119. clac();
  120. // Apply a random offset in the range 0-255 to the stack pointer,
  121. // to make kernel stacks a bit less deterministic.
  122. auto* ptr = (char*)__builtin_alloca(get_fast_random<u8>());
  123. asm volatile(""
  124. : "=m"(*ptr));
  125. auto& process = *Process::current;
  126. if (!MM.validate_user_stack(process, VirtualAddress(regs.userspace_esp))) {
  127. dbg() << "Invalid stack pointer: " << String::format("%p", regs.userspace_esp);
  128. handle_crash(regs, "Bad stack on syscall entry", SIGSTKFLT);
  129. ASSERT_NOT_REACHED();
  130. }
  131. auto* calling_region = MM.region_from_vaddr(process, VirtualAddress(regs.eip));
  132. if (!calling_region) {
  133. dbg() << "Syscall from " << String::format("%p", regs.eip) << " which has no region";
  134. handle_crash(regs, "Syscall from unknown region", SIGSEGV);
  135. ASSERT_NOT_REACHED();
  136. }
  137. if (calling_region->is_writable()) {
  138. dbg() << "Syscall from writable memory at " << String::format("%p", regs.eip);
  139. handle_crash(regs, "Syscall from writable memory", SIGSEGV);
  140. ASSERT_NOT_REACHED();
  141. }
  142. process.big_lock().lock();
  143. u32 function = regs.eax;
  144. u32 arg1 = regs.edx;
  145. u32 arg2 = regs.ecx;
  146. u32 arg3 = regs.ebx;
  147. regs.eax = (u32)Syscall::handle(regs, function, arg1, arg2, arg3);
  148. if (auto* tracer = process.tracer())
  149. tracer->did_syscall(function, arg1, arg2, arg3, regs.eax);
  150. process.big_lock().unlock();
  151. // Check if we're supposed to return to userspace or just die.
  152. Thread::current->die_if_needed();
  153. if (Thread::current->has_unmasked_pending_signals())
  154. (void)Thread::current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal);
  155. }
  156. }