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