From 84725ef3a5dfe9fb52f61af770b59d56de0b6f25 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 9 Mar 2021 08:16:00 +0100 Subject: [PATCH] Kernel+UserspaceEmulator: Add sys$emuctl() system call This returns ENOSYS if you are running in the real kernel, and some other result if you are running in UserspaceEmulator. There are other ways we could check if we're inside an emulator, but it seemed easier to just ask. :^) --- Kernel/API/Syscall.h | 3 +- Kernel/CMakeLists.txt | 1 + Kernel/Process.h | 1 + Kernel/Syscalls/emuctl.cpp | 36 +++++++++++++++++++ .../DevTools/UserspaceEmulator/Emulator.cpp | 7 ++++ .../DevTools/UserspaceEmulator/Emulator.h | 1 + 6 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 Kernel/Syscalls/emuctl.cpp diff --git a/Kernel/API/Syscall.h b/Kernel/API/Syscall.h index 0a67957f2e..4c873e2688 100644 --- a/Kernel/API/Syscall.h +++ b/Kernel/API/Syscall.h @@ -192,7 +192,8 @@ namespace Kernel { S(abort) \ S(anon_create) \ S(msyscall) \ - S(readv) + S(readv) \ + S(emuctl) namespace Syscall { diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index 9283a1740c..5347de34c9 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -137,6 +137,7 @@ set(KERNEL_SOURCES Syscalls/debug.cpp Syscalls/disown.cpp Syscalls/dup2.cpp + Syscalls/emuctl.cpp Syscalls/execve.cpp Syscalls/exit.cpp Syscalls/fcntl.cpp diff --git a/Kernel/Process.h b/Kernel/Process.h index 626072baa8..ad8e8a3f82 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -211,6 +211,7 @@ public: void stop_tracing(); void tracer_trap(Thread&, const RegisterState&); + KResultOr sys$emuctl(); KResultOr sys$yield(); KResultOr sys$sync(); KResultOr sys$beep(); diff --git a/Kernel/Syscalls/emuctl.cpp b/Kernel/Syscalls/emuctl.cpp new file mode 100644 index 0000000000..872d7aeead --- /dev/null +++ b/Kernel/Syscalls/emuctl.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021, Andreas Kling + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +namespace Kernel { + +KResultOr Process::sys$emuctl() +{ + return ENOSYS; +} + +} diff --git a/Userland/DevTools/UserspaceEmulator/Emulator.cpp b/Userland/DevTools/UserspaceEmulator/Emulator.cpp index 77ba4a383c..2db1d880a0 100644 --- a/Userland/DevTools/UserspaceEmulator/Emulator.cpp +++ b/Userland/DevTools/UserspaceEmulator/Emulator.cpp @@ -518,6 +518,8 @@ u32 Emulator::virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3) return virt$getrandom(arg1, arg2, arg3); case SC_fork: return virt$fork(); + case SC_emuctl: + return virt$emuctl(); case SC_sched_getparam: return virt$sched_getparam(arg1, arg2); case SC_sched_setparam: @@ -1265,6 +1267,11 @@ int Emulator::virt$ioctl([[maybe_unused]] int fd, unsigned request, [[maybe_unus TODO(); } +int Emulator::virt$emuctl() +{ + return 0; +} + int Emulator::virt$fork() { int rc = fork(); diff --git a/Userland/DevTools/UserspaceEmulator/Emulator.h b/Userland/DevTools/UserspaceEmulator/Emulator.h index b0bde0304a..78943d019f 100644 --- a/Userland/DevTools/UserspaceEmulator/Emulator.h +++ b/Userland/DevTools/UserspaceEmulator/Emulator.h @@ -82,6 +82,7 @@ private: void register_signal_handlers(); void setup_signal_trampoline(); + int virt$emuctl(); int virt$fork(); int virt$execve(FlatPtr); int virt$access(FlatPtr, size_t, int);