Syscall.cpp 6.0 KB

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