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