Syscall.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/API/Syscall.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. 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 $" __STRINGIFY(GDT_SELECTOR_DATA0) ", %ax\n"
  46. " mov %ax, %ds\n"
  47. " mov %ax, %es\n"
  48. " mov $" __STRINGIFY(GDT_SELECTOR_PROC) ", %ax\n"
  49. " mov %ax, %fs\n"
  50. " cld\n"
  51. " xor %esi, %esi\n"
  52. " xor %edi, %edi\n"
  53. " pushl %esp \n" // set TrapFrame::regs
  54. " subl $" __STRINGIFY(TRAP_FRAME_SIZE - 4) ", %esp \n"
  55. " movl %esp, %ebx \n"
  56. " pushl %ebx \n" // push pointer to TrapFrame
  57. " call enter_trap_no_irq \n"
  58. " movl %ebx, 0(%esp) \n" // push pointer to TrapFrame
  59. " call syscall_handler \n"
  60. " movl %ebx, 0(%esp) \n" // push pointer to TrapFrame
  61. " jmp common_trap_exit \n");
  62. namespace Syscall {
  63. static int handle(RegisterState&, u32 function, u32 arg1, u32 arg2, u32 arg3);
  64. void initialize()
  65. {
  66. register_user_callable_interrupt_handler(syscall_vector, syscall_asm_entry);
  67. klog() << "Syscall: int 0x82 handler installed";
  68. }
  69. #pragma GCC diagnostic ignored "-Wcast-function-type"
  70. typedef int (Process::*Handler)(u32, u32, u32);
  71. #define __ENUMERATE_REMOVED_SYSCALL(x) nullptr,
  72. #define __ENUMERATE_SYSCALL(x) reinterpret_cast<Handler>(&Process::sys$##x),
  73. static Handler s_syscall_table[] = {
  74. ENUMERATE_SYSCALLS
  75. };
  76. #undef __ENUMERATE_SYSCALL
  77. #undef __ENUMERATE_REMOVED_SYSCALL
  78. int handle(RegisterState& regs, u32 function, u32 arg1, u32 arg2, u32 arg3)
  79. {
  80. ASSERT_INTERRUPTS_ENABLED();
  81. auto current_thread = Thread::current();
  82. auto& process = current_thread->process();
  83. current_thread->did_syscall();
  84. if (function == SC_exit || function == SC_exit_thread) {
  85. // These syscalls need special handling since they never return to the caller.
  86. cli();
  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(TrapFrame* trap)
  110. {
  111. auto& regs = *trap->regs;
  112. auto current_thread = Thread::current();
  113. if (current_thread->tracer() && current_thread->tracer()->is_tracing_syscalls()) {
  114. current_thread->tracer()->set_trace_syscalls(false);
  115. current_thread->tracer_trap(regs);
  116. }
  117. // Make sure SMAP protection is enabled on syscall entry.
  118. clac();
  119. // Apply a random offset in the range 0-255 to the stack pointer,
  120. // to make kernel stacks a bit less deterministic.
  121. auto* ptr = (char*)__builtin_alloca(get_fast_random<u8>());
  122. asm volatile(""
  123. : "=m"(*ptr));
  124. auto& process = current_thread->process();
  125. if (!MM.validate_user_stack(process, VirtualAddress(regs.userspace_esp))) {
  126. dbg() << "Invalid stack pointer: " << String::format("%p", regs.userspace_esp);
  127. handle_crash(regs, "Bad stack on syscall entry", SIGSTKFLT);
  128. ASSERT_NOT_REACHED();
  129. }
  130. auto* calling_region = MM.find_region_from_vaddr(process, VirtualAddress(regs.eip));
  131. if (!calling_region) {
  132. dbg() << "Syscall from " << String::format("%p", regs.eip) << " which has no region";
  133. handle_crash(regs, "Syscall from unknown region", SIGSEGV);
  134. ASSERT_NOT_REACHED();
  135. }
  136. if (calling_region->is_writable()) {
  137. dbg() << "Syscall from writable memory at " << String::format("%p", regs.eip);
  138. handle_crash(regs, "Syscall from writable memory", SIGSEGV);
  139. ASSERT_NOT_REACHED();
  140. }
  141. process.big_lock().lock();
  142. u32 function = regs.eax;
  143. u32 arg1 = regs.edx;
  144. u32 arg2 = regs.ecx;
  145. u32 arg3 = regs.ebx;
  146. regs.eax = (u32)Syscall::handle(regs, function, arg1, arg2, arg3);
  147. if (current_thread->tracer() && current_thread->tracer()->is_tracing_syscalls()) {
  148. current_thread->tracer()->set_trace_syscalls(false);
  149. current_thread->tracer_trap(regs);
  150. }
  151. process.big_lock().unlock();
  152. // Check if we're supposed to return to userspace or just die.
  153. current_thread->die_if_needed();
  154. if (current_thread->has_unmasked_pending_signals())
  155. (void)current_thread->block<Thread::SemiPermanentBlocker>(nullptr, Thread::SemiPermanentBlocker::Reason::Signal);
  156. }
  157. }