From bf0719092fc5769f568c87671172b5a43d17c038 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 17 Jan 2021 08:51:41 +0100 Subject: [PATCH] Kernel+Userland: Remove shared buffers (shbufs) All users of this mechanism have been switched to anonymous files and passing file descriptors with sendfd()/recvfd(). Shbufs got us where we are today, but it's time we say good-bye to them and welcome a much more idiomatic replacement. :^) --- AK/Debug.h | 6 - AK/Forward.h | 2 - AK/SharedBuffer.cpp | 168 ---------------- AK/SharedBuffer.h | 68 ------- Base/usr/share/man/man2/pledge.md | 1 - Base/usr/share/man/man2/shbuf_allow_pid.md | 28 --- Base/usr/share/man/man2/shbuf_create.md | 27 --- Kernel/API/Syscall.h | 4 - Kernel/CMakeLists.txt | 2 - Kernel/Forward.h | 1 - Kernel/Process.cpp | 3 +- Kernel/Process.h | 8 +- Kernel/SharedBuffer.cpp | 188 ------------------ Kernel/SharedBuffer.h | 87 -------- Kernel/Syscalls/execve.cpp | 2 - Kernel/Syscalls/fork.cpp | 3 - Kernel/Syscalls/process.cpp | 1 - Kernel/Syscalls/shbuf.cpp | 137 ------------- Meta/CMake/all_the_debug_macros.cmake | 1 - .../DevTools/UserspaceEmulator/CMakeLists.txt | 1 - .../DevTools/UserspaceEmulator/Emulator.cpp | 51 ----- .../DevTools/UserspaceEmulator/Emulator.h | 4 - .../UserspaceEmulator/SharedBufferRegion.cpp | 117 ----------- .../UserspaceEmulator/SharedBufferRegion.h | 65 ------ .../DevTools/UserspaceEmulator/SoftMMU.cpp | 12 -- Userland/DevTools/UserspaceEmulator/SoftMMU.h | 4 - Userland/Libraries/LibC/serenity.cpp | 28 --- Userland/Libraries/LibC/serenity.h | 5 - 28 files changed, 2 insertions(+), 1022 deletions(-) delete mode 100644 AK/SharedBuffer.cpp delete mode 100644 AK/SharedBuffer.h delete mode 100644 Base/usr/share/man/man2/shbuf_allow_pid.md delete mode 100644 Base/usr/share/man/man2/shbuf_create.md delete mode 100644 Kernel/SharedBuffer.cpp delete mode 100644 Kernel/SharedBuffer.h delete mode 100644 Kernel/Syscalls/shbuf.cpp delete mode 100644 Userland/DevTools/UserspaceEmulator/SharedBufferRegion.cpp delete mode 100644 Userland/DevTools/UserspaceEmulator/SharedBufferRegion.h diff --git a/AK/Debug.h b/AK/Debug.h index 46c0b20a9f..d8fac0c9df 100644 --- a/AK/Debug.h +++ b/AK/Debug.h @@ -46,12 +46,6 @@ constexpr bool debug_scheduler_runnable = true; constexpr bool debug_scheduler_runnable = false; #endif -#ifdef SHARED_BUFFER_DEBUG -constexpr bool debug_shared_buffer = true; -#else -constexpr bool debug_shared_buffer = false; -#endif - #ifdef THREAD_DEBUG constexpr bool debug_thread = true; #else diff --git a/AK/Forward.h b/AK/Forward.h index 1257a7ff7d..c15a275e9e 100644 --- a/AK/Forward.h +++ b/AK/Forward.h @@ -38,7 +38,6 @@ class JsonArray; class JsonObject; class JsonValue; class LogStream; -class SharedBuffer; class StackInfo; class String; class StringBuilder; @@ -169,7 +168,6 @@ using AK::OutputStream; using AK::OwnPtr; using AK::ReadonlyBytes; using AK::RefPtr; -using AK::SharedBuffer; using AK::SinglyLinkedList; using AK::Span; using AK::StackInfo; diff --git a/AK/SharedBuffer.cpp b/AK/SharedBuffer.cpp deleted file mode 100644 index 3be2c2edd8..0000000000 --- a/AK/SharedBuffer.cpp +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (c) 2018-2020, 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 -#include - -#if defined(__serenity__) -# include -# include -#else -# include -# include -# include - -static String shbuf_shm_name(int shbuf_id) -{ - return String::formatted("/serenity-shm:{}", shbuf_id); -} - -#endif - -namespace AK { - -RefPtr SharedBuffer::create_with_size(int size) -{ -#if defined(__serenity__) - void* data; - int shbuf_id = shbuf_create(size, &data); - if (shbuf_id < 0) { - perror("shbuf_create"); - return nullptr; - } -#else - // Not atomic, so don't create shared buffers from many threads too hard under lagom. - static unsigned g_shm_id = 0; - - int shbuf_id = (getpid() << 8) | (g_shm_id++); - int fd = shm_open(shbuf_shm_name(shbuf_id).characters(), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); - if (fd < 0) { - perror("shm_open"); - return nullptr; - } - - if (ftruncate(fd, size) < 0) { - perror("ftruncate"); - return nullptr; - } - - void* data = mmap(nullptr, size + sizeof(size_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (data == MAP_FAILED) { - perror("mmap"); - return nullptr; - } - size_t* size_data = reinterpret_cast(data); - *size_data = size; - data = reinterpret_cast(data) + sizeof(size_t); - - if (close(fd) < 0) { - perror("close"); - return nullptr; - } -#endif - return adopt(*new SharedBuffer(shbuf_id, size, data)); -} - -bool SharedBuffer::share_with([[maybe_unused]] pid_t peer) -{ -#if defined(__serenity__) - int ret = shbuf_allow_pid(shbuf_id(), peer); - if (ret < 0) { - perror("shbuf_allow_pid"); - return false; - } -#endif - return true; -} - -RefPtr SharedBuffer::create_from_shbuf_id(int shbuf_id) -{ -#if defined(__serenity__) - size_t size = 0; - void* data = shbuf_get(shbuf_id, &size); - if (data == (void*)-1) - return nullptr; -#else - int fd = shm_open(shbuf_shm_name(shbuf_id).characters(), O_RDWR, S_IRUSR | S_IWUSR); - if (fd < 0) { - perror("shm_open"); - return nullptr; - } - - void* data = mmap(nullptr, sizeof(size_t), PROT_READ, MAP_SHARED, fd, 0); - if (data == MAP_FAILED) { - perror("mmap"); - return nullptr; - } - size_t* size_data = reinterpret_cast(data); - size_t size = *size_data; - if (munmap(data, sizeof(size_t)) < 0) { - perror("munmap"); - return nullptr; - } - - data = mmap(nullptr, size + sizeof(size_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (data == MAP_FAILED) { - perror("mmap"); - return nullptr; - } - - data = reinterpret_cast(data) + sizeof(size_t); - - if (close(fd) < 0) { - perror("close"); - return nullptr; - } -#endif - - return adopt(*new SharedBuffer(shbuf_id, size, data)); -} - -SharedBuffer::SharedBuffer(int shbuf_id, int size, void* data) - : m_shbuf_id(shbuf_id) - , m_size(size) - , m_data(data) -{ -} - -SharedBuffer::~SharedBuffer() -{ - if (m_shbuf_id >= 0) { -#if defined(__serenity__) - int rc = shbuf_release(m_shbuf_id); - if (rc < 0) { - perror("shbuf_release"); - } -#else - if (munmap(reinterpret_cast(m_data) - sizeof(size_t), m_size + sizeof(size_t)) < 0) - perror("munmap"); - if (shm_unlink(shbuf_shm_name(m_shbuf_id).characters()) < 0) - perror("unlink"); -#endif - } -} - -} diff --git a/AK/SharedBuffer.h b/AK/SharedBuffer.h deleted file mode 100644 index da9c1be3db..0000000000 --- a/AK/SharedBuffer.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2018-2020, 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. - */ - -#pragma once - -#include -#include - -namespace AK { - -class SharedBuffer : public RefCounted { -public: - static RefPtr create_with_size(int); - static RefPtr create_from_shbuf_id(int); - ~SharedBuffer(); - - bool share_with(pid_t); - int shbuf_id() const { return m_shbuf_id; } - int size() const { return m_size; } - - template - T* data() - { - static_assert(IsVoid::value || is_trivial()); - return (T*)m_data; - } - - template - const T* data() const - { - static_assert(IsVoid::value || is_trivial()); - return (const T*)m_data; - } - -private: - SharedBuffer(int shbuf_id, int size, void*); - - int m_shbuf_id { -1 }; - int m_size { 0 }; - void* m_data { nullptr }; -}; - -} - -using AK::SharedBuffer; diff --git a/Base/usr/share/man/man2/pledge.md b/Base/usr/share/man/man2/pledge.md index 1a1a5a4aaf..8558f464ed 100644 --- a/Base/usr/share/man/man2/pledge.md +++ b/Base/usr/share/man/man2/pledge.md @@ -45,7 +45,6 @@ If the process later attempts to use any system functionality it has previously * `dpath`: Creating new device files * `chown`: Changing file owner/group * `fattr`: Changing file attributes/permissions -* `shared_buffer`: Shared memory buffers (\*) * `chroot`: The [`chroot(2)`](chroot.md) syscall (\*) * `video`: May use [`ioctl(2)`](ioctl.md) and [`mmap(2)`](mmap.md) on framebuffer video devices * `settime`: Changing the system time and date diff --git a/Base/usr/share/man/man2/shbuf_allow_pid.md b/Base/usr/share/man/man2/shbuf_allow_pid.md deleted file mode 100644 index 649c908671..0000000000 --- a/Base/usr/share/man/man2/shbuf_allow_pid.md +++ /dev/null @@ -1,28 +0,0 @@ -## Name - -shbuf\_allow\_pid - allow another process to map a shareable buffer - -## Synopsis -```**c++ -#include - -int shbuf_allow_pid(int shbuf_id, pid_t peer_pid); -``` - -## Description - -Gives the process with PID `peer_pid` permission to map the shareable buffer with ID `shbuf_id`. - -## Return value - -On success, returns 0. Otherwise, returns -1 and `errno` is set. - -## Errors - -* `EINVAL`: `peer_pid` is invalid, or `shbuf_id` is not a valid ID. -* `EPERM`: The calling process does not have access to the buffer with `shbuf_id`. -* `ESRCH`: No process with PID `peer_pid` is found. - -## See also - -* [`shbuf_create`(2)](shbuf_create.md) diff --git a/Base/usr/share/man/man2/shbuf_create.md b/Base/usr/share/man/man2/shbuf_create.md deleted file mode 100644 index 1a05a4efe3..0000000000 --- a/Base/usr/share/man/man2/shbuf_create.md +++ /dev/null @@ -1,27 +0,0 @@ -## Name - -shbuf\_create - create a shareable memory buffer - -## Synopsis -```**c++ -#include - -int shbuf_create(int size, void** buffer); -``` - -## Description - -Creates a new memory region that can be shared with other processes. The region is only accessible to the calling process by default. - -## Return value - -If a region is successfully created, `shbuf_create()` stores a pointer to the memory in `buffer` and returns a buffer ID. Otherwise, it returns -1 and sets `errno` to describe the error. - -## Errors - -* `EINVAL`: `size` is zero or negative. -* `EFAULT`: `buffer` is not a valid address. - -## See also - -* [`shbuf_allow_pid`(2)](shbuf_allow_pid.md) diff --git a/Kernel/API/Syscall.h b/Kernel/API/Syscall.h index 86a3439cf6..f236616c41 100644 --- a/Kernel/API/Syscall.h +++ b/Kernel/API/Syscall.h @@ -121,10 +121,6 @@ namespace Kernel { S(accept) \ S(listen) \ S(connect) \ - S(shbuf_create) \ - S(shbuf_allow_pid) \ - S(shbuf_get) \ - S(shbuf_release) \ S(link) \ S(chown) \ S(fchmod) \ diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index a0233ef71a..3f24f86a78 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -101,7 +101,6 @@ set(KERNEL_SOURCES RTC.cpp Random.cpp Scheduler.cpp - SharedBuffer.cpp StdLib.cpp Syscall.cpp Syscalls/anon_create.cpp @@ -157,7 +156,6 @@ set(KERNEL_SOURCES Syscalls/setkeymap.cpp Syscalls/setpgid.cpp Syscalls/setuid.cpp - Syscalls/shbuf.cpp Syscalls/shutdown.cpp Syscalls/sigaction.cpp Syscalls/socket.cpp diff --git a/Kernel/Forward.h b/Kernel/Forward.h index d7cc3dfdd7..f09dc37fa3 100644 --- a/Kernel/Forward.h +++ b/Kernel/Forward.h @@ -60,7 +60,6 @@ class RangeAllocator; class Region; class Scheduler; class SchedulerPerProcessorData; -class SharedBuffer; class Socket; template class SpinLock; diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 5a39ab2d1b..c8102c1646 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -45,10 +45,10 @@ #include #include #include -#include #include #include #include +#include #include #include #include @@ -644,7 +644,6 @@ void Process::finalize() m_dead = true; - disown_all_shared_buffers(); { // FIXME: PID/TID BUG if (auto parent_thread = Thread::from_tid(m_ppid.value())) { diff --git a/Kernel/Process.h b/Kernel/Process.h index 0f46ca1035..72d8be3ba9 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -80,8 +80,7 @@ extern VirtualAddress g_return_to_ring3_from_signal_trampoline; __ENUMERATE_PLEDGE_PROMISE(accept) \ __ENUMERATE_PLEDGE_PROMISE(settime) \ __ENUMERATE_PLEDGE_PROMISE(sigaction) \ - __ENUMERATE_PLEDGE_PROMISE(setkeymap) \ - __ENUMERATE_PLEDGE_PROMISE(shared_buffer) + __ENUMERATE_PLEDGE_PROMISE(setkeymap) enum class Pledge : u32 { #define __ENUMERATE_PLEDGE_PROMISE(x) x, @@ -334,10 +333,6 @@ public: int sys$get_thread_name(pid_t tid, Userspace buffer, size_t buffer_size); int sys$rename(Userspace); int sys$mknod(Userspace); - int sys$shbuf_create(int, void** buffer); - int sys$shbuf_allow_pid(int, pid_t peer_pid); - void* sys$shbuf_get(int shbuf_id, Userspace size); - int sys$shbuf_release(int shbuf_id); int sys$halt(); int sys$reboot(); int sys$realpath(Userspace); @@ -530,7 +525,6 @@ private: KResultOr> find_elf_interpreter_for_executable(const String& path, const Elf32_Ehdr& elf_header, int nread, size_t file_size); int alloc_fd(int first_candidate_fd = 0); - void disown_all_shared_buffers(); KResult do_kill(Process&, int signal); KResult do_killpg(ProcessGroupID pgrp, int signal); diff --git a/Kernel/SharedBuffer.cpp b/Kernel/SharedBuffer.cpp deleted file mode 100644 index caa32d2a43..0000000000 --- a/Kernel/SharedBuffer.cpp +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (c) 2018-2020, 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 -#include -#include -#include - -//#define SHARED_BUFFER_DEBUG - -namespace Kernel { - -static AK::Singleton>>> s_map; - -Lockable>>& shared_buffers() -{ - return *s_map; -} - -void SharedBuffer::sanity_check(const char* what) -{ - LOCKER(shared_buffers().lock(), Lock::Mode::Shared); - - unsigned found_refs = 0; - for (const auto& ref : m_refs) - found_refs += ref.count; - - if (found_refs != m_total_refs) { - dbgln("{} sanity -- SharedBuffer({}) id: {} has total refs {} but we found {}", - what, - this, - m_shbuf_id, - m_total_refs, - found_refs); - - for (const auto& ref : m_refs) - dbgln(" ref from pid {}: reference count {}", ref.pid.value(), ref.count); - ASSERT_NOT_REACHED(); - } -} - -bool SharedBuffer::is_shared_with(ProcessID peer_pid) const -{ - LOCKER(shared_buffers().lock(), Lock::Mode::Shared); - for (auto& ref : m_refs) { - if (ref.pid == peer_pid) { - return true; - } - } - - return false; -} - -void* SharedBuffer::ref_for_process_and_get_address(Process& process) -{ - LOCKER(shared_buffers().lock()); - ASSERT(is_shared_with(process.pid())); - for (auto& ref : m_refs) { - if (ref.pid == process.pid()) { - if (!ref.region) { - auto region_or_error = process.allocate_region_with_vmobject(VirtualAddress(), size(), m_vmobject, 0, "SharedBuffer", PROT_READ | (m_writable ? PROT_WRITE : 0), true); - if (region_or_error.is_error()) - return (void*)region_or_error.error().error(); - ref.region = region_or_error.value(); - } - ref.count++; - m_total_refs++; - sanity_check("ref_for_process_and_get_address"); - return ref.region.unsafe_ptr()->vaddr().as_ptr(); // TODO: Region needs to be RefCounted! - } - } - ASSERT_NOT_REACHED(); -} - -void SharedBuffer::share_with(ProcessID peer_pid) -{ - LOCKER(shared_buffers().lock()); - for (auto& ref : m_refs) { - if (ref.pid == peer_pid) { - // don't increment the reference count yet; let them shbuf_get it first. - sanity_check("share_with (old ref)"); - return; - } - } - - m_refs.append(Reference(peer_pid)); - sanity_check("share_with (new ref)"); -} - -void SharedBuffer::share_all_shared_buffers(Process& from_process, Process& with_process) -{ - LOCKER(shared_buffers().lock()); - for (auto& shbuf : shared_buffers().resource()) { - auto& shared_buffer = *shbuf.value; - // We need to clone all references (including for global shared buffers), - // and the reference counts as well. - for (auto& ref : shared_buffer.m_refs) { - if (ref.pid == from_process.pid()) { - auto ref_count = ref.count; - shared_buffer.m_refs.append(Reference(with_process.pid(), ref_count)); - // NOTE: ref may become invalid after we appended! - shared_buffer.m_total_refs += ref_count; - break; - } - } - } -} - -void SharedBuffer::deref_for_process(Process& process) -{ - LOCKER(shared_buffers().lock()); - for (size_t i = 0; i < m_refs.size(); ++i) { - auto& ref = m_refs[i]; - if (ref.pid == process.pid()) { - ASSERT(ref.count > 0); - ref.count--; - ASSERT(m_total_refs > 0); - m_total_refs--; - if (ref.count == 0) { - dbgln("Releasing shared buffer reference on {} of size {} by PID {}", m_shbuf_id, size(), process.pid().value()); - process.deallocate_region(*ref.region.unsafe_ptr()); // TODO: Region needs to be RefCounted! - dbgln("Released shared buffer reference on {} of size {} by PID {}", m_shbuf_id, size(), process.pid().value()); - sanity_check("deref_for_process"); - destroy_if_unused(); - return; - } - return; - } - } - - ASSERT_NOT_REACHED(); -} - -bool SharedBuffer::disown(ProcessID pid) -{ - LOCKER(shared_buffers().lock()); - for (size_t i = 0; i < m_refs.size(); ++i) { - auto& ref = m_refs[i]; - if (ref.pid == pid) { - dbgln("Disowning shared buffer {} of size {} by PID {}", m_shbuf_id, size(), pid.value()); - ASSERT(m_total_refs >= ref.count); - m_total_refs -= ref.count; - m_refs.unstable_take(i); - dbgln("Disowned shared buffer {} of size {} by PID {}", m_shbuf_id, size(), pid.value()); - destroy_if_unused(); - break; - } - } - - return m_total_refs == 0; -} - -void SharedBuffer::destroy_if_unused() -{ - LOCKER(shared_buffers().lock()); - sanity_check("destroy_if_unused"); - if (m_total_refs == 0) { - dbgln("Destroying unused SharedBuffer({}) id={}", this, m_shbuf_id); - auto count_before = shared_buffers().resource().size(); - shared_buffers().resource().remove(m_shbuf_id); - ASSERT(count_before != shared_buffers().resource().size()); - } -} - -} diff --git a/Kernel/SharedBuffer.h b/Kernel/SharedBuffer.h deleted file mode 100644 index 4943a60d06..0000000000 --- a/Kernel/SharedBuffer.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2018-2020, 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. - */ - -#pragma once - -#include -#include -#include -#include -#include - -namespace Kernel { - -class SharedBuffer { -private: - struct Reference { - Reference(ProcessID pid, unsigned count = 0) - : pid(pid) - , count(count) - { - } - - ProcessID pid; - unsigned count { 0 }; - WeakPtr region; - }; - -public: - SharedBuffer(int id, NonnullRefPtr&& vmobject) - : m_shbuf_id(id) - , m_vmobject(move(vmobject)) - { - dbgln("Created shared buffer {} of size {}", m_shbuf_id, size()); - } - - ~SharedBuffer() - { - dbgln("Destroyed shared buffer {} of size {}", m_shbuf_id, size()); - } - - void sanity_check(const char* what); - bool is_shared_with(ProcessID peer_pid) const; - void* ref_for_process_and_get_address(Process& process); - void share_with(ProcessID peer_pid); - void deref_for_process(Process& process); - bool disown(ProcessID pid); - static void share_all_shared_buffers(Process& from_process, Process& with_process); - size_t size() const { return m_vmobject->size(); } - void destroy_if_unused(); - AnonymousVMObject& vmobject() { return m_vmobject; } - const AnonymousVMObject& vmobject() const { return m_vmobject; } - int id() const { return m_shbuf_id; } - -private: - int m_shbuf_id { -1 }; - bool m_writable { true }; - NonnullRefPtr m_vmobject; - Vector m_refs; - unsigned m_total_refs { 0 }; -}; - -Lockable>>& shared_buffers(); - -} diff --git a/Kernel/Syscalls/execve.cpp b/Kernel/Syscalls/execve.cpp index 48cd0c26ae..fbe03a3558 100644 --- a/Kernel/Syscalls/execve.cpp +++ b/Kernel/Syscalls/execve.cpp @@ -522,8 +522,6 @@ int Process::do_exec(NonnullRefPtr main_program_description, Ve m_region_lookup_cache = {}; - disown_all_shared_buffers(); - set_dumpable(!executable_is_setid); for (size_t i = 0; i < m_fds.size(); ++i) { diff --git a/Kernel/Syscalls/fork.cpp b/Kernel/Syscalls/fork.cpp index dbf9cfdeba..05e61b3c58 100644 --- a/Kernel/Syscalls/fork.cpp +++ b/Kernel/Syscalls/fork.cpp @@ -27,7 +27,6 @@ #include #include #include -#include #include //#define FORK_DEBUG @@ -80,8 +79,6 @@ pid_t Process::sys$fork(RegisterState& regs) dbgln("fork: child will begin executing at {:04x}:{:08x} with stack {:04x}:{:08x}, kstack {:04x}:{:08x}", child_tss.cs, child_tss.eip, child_tss.ss, child_tss.esp, child_tss.ss0, child_tss.esp0); #endif - SharedBuffer::share_all_shared_buffers(*this, *child); - { ScopedSpinLock lock(m_lock); for (auto& region : m_regions) { diff --git a/Kernel/Syscalls/process.cpp b/Kernel/Syscalls/process.cpp index a10b506f34..2b2bbccea9 100644 --- a/Kernel/Syscalls/process.cpp +++ b/Kernel/Syscalls/process.cpp @@ -26,7 +26,6 @@ #include #include -#include namespace Kernel { diff --git a/Kernel/Syscalls/shbuf.cpp b/Kernel/Syscalls/shbuf.cpp deleted file mode 100644 index 7b885cd9f2..0000000000 --- a/Kernel/Syscalls/shbuf.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2018-2020, 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 -#include - -//#define SHARED_BUFFER_DEBUG - -namespace Kernel { - -void Process::disown_all_shared_buffers() -{ - LOCKER(shared_buffers().lock()); - Vector buffers_to_disown; - for (auto& it : shared_buffers().resource()) - buffers_to_disown.append(it.value.ptr()); - for (auto* shared_buffer : buffers_to_disown) { - if (shared_buffer->disown(m_pid)) { - shared_buffers().resource().remove(shared_buffer->id()); - delete shared_buffer; - } - } -} - -int Process::sys$shbuf_create(int size, void** buffer) -{ - REQUIRE_PROMISE(shared_buffer); - if (!size || size < 0) - return -EINVAL; - size = PAGE_ROUND_UP(size); - - auto vmobject = AnonymousVMObject::create_with_size(size, AllocationStrategy::Reserve); - if (!vmobject) - return -ENOMEM; - - LOCKER(shared_buffers().lock()); - static int s_next_shbuf_id; - int shbuf_id = ++s_next_shbuf_id; - auto shared_buffer = make(shbuf_id, vmobject.release_nonnull()); - shared_buffer->share_with(m_pid); - - void* address = shared_buffer->ref_for_process_and_get_address(*this); - if (!copy_to_user(buffer, &address)) - return -EFAULT; - ASSERT((int)shared_buffer->size() >= size); -#ifdef SHARED_BUFFER_DEBUG - klog() << "Created shared buffer " << shbuf_id << " @ " << buffer << " (" << size << " bytes, vmobject is " << shared_buffer->size() << ")"; -#endif - shared_buffers().resource().set(shbuf_id, move(shared_buffer)); - - return shbuf_id; -} - -int Process::sys$shbuf_allow_pid(int shbuf_id, pid_t peer_pid) -{ - REQUIRE_PROMISE(shared_buffer); - if (!peer_pid || peer_pid < 0 || ProcessID(peer_pid) == m_pid) - return -EINVAL; - LOCKER(shared_buffers().lock()); - auto it = shared_buffers().resource().find(shbuf_id); - if (it == shared_buffers().resource().end()) - return -EINVAL; - auto& shared_buffer = *(*it).value; - if (!shared_buffer.is_shared_with(m_pid)) - return -EPERM; - { - ScopedSpinLock lock(g_processes_lock); - auto peer = Process::from_pid(peer_pid); - if (!peer) - return -ESRCH; - } - shared_buffer.share_with(peer_pid); - return 0; -} - -int Process::sys$shbuf_release(int shbuf_id) -{ - REQUIRE_PROMISE(shared_buffer); - LOCKER(shared_buffers().lock()); - auto it = shared_buffers().resource().find(shbuf_id); - if (it == shared_buffers().resource().end()) - return -EINVAL; - auto& shared_buffer = *(*it).value; - if (!shared_buffer.is_shared_with(m_pid)) - return -EPERM; -#ifdef SHARED_BUFFER_DEBUG - klog() << "Releasing shared buffer " << shbuf_id << ", buffer count: " << shared_buffers().resource().size(); -#endif - shared_buffer.deref_for_process(*this); - return 0; -} - -void* Process::sys$shbuf_get(int shbuf_id, Userspace user_size) -{ - REQUIRE_PROMISE(shared_buffer); - LOCKER(shared_buffers().lock()); - auto it = shared_buffers().resource().find(shbuf_id); - if (it == shared_buffers().resource().end()) - return (void*)-EINVAL; - auto& shared_buffer = *(*it).value; - if (!shared_buffer.is_shared_with(m_pid)) - return (void*)-EPERM; -#ifdef SHARED_BUFFER_DEBUG - klog() << "Retaining shared buffer " << shbuf_id << ", buffer count: " << shared_buffers().resource().size(); -#endif - if (user_size) { - size_t size = shared_buffer.size(); - if (!copy_to_user(user_size, &size)) - return (void*)-EFAULT; - } - return shared_buffer.ref_for_process_and_get_address(*this); -} - -} diff --git a/Meta/CMake/all_the_debug_macros.cmake b/Meta/CMake/all_the_debug_macros.cmake index e8403fa7e7..769b1a3955 100644 --- a/Meta/CMake/all_the_debug_macros.cmake +++ b/Meta/CMake/all_the_debug_macros.cmake @@ -135,7 +135,6 @@ add_compile_definitions("SCHEDULER_DEBUG") add_compile_definitions("SCHEDULER_RUNNABLE_DEBUG") add_compile_definitions("SELECTION_DEBUG") add_compile_definitions("SERVICE_DEBUG") -add_compile_definitions("SHARED_BUFFER_DEBUG") add_compile_definitions("SH_DEBUG") add_compile_definitions("SIGNAL_DEBUG") add_compile_definitions("SLAVEPTY_DEBUG") diff --git a/Userland/DevTools/UserspaceEmulator/CMakeLists.txt b/Userland/DevTools/UserspaceEmulator/CMakeLists.txt index dd2a2b37ad..43164b0d6d 100644 --- a/Userland/DevTools/UserspaceEmulator/CMakeLists.txt +++ b/Userland/DevTools/UserspaceEmulator/CMakeLists.txt @@ -3,7 +3,6 @@ set(SOURCES MallocTracer.cpp MmapRegion.cpp Region.cpp - SharedBufferRegion.cpp SimpleRegion.cpp SoftCPU.cpp SoftMMU.cpp diff --git a/Userland/DevTools/UserspaceEmulator/Emulator.cpp b/Userland/DevTools/UserspaceEmulator/Emulator.cpp index b4a42a4320..9208f6cb9c 100644 --- a/Userland/DevTools/UserspaceEmulator/Emulator.cpp +++ b/Userland/DevTools/UserspaceEmulator/Emulator.cpp @@ -26,7 +26,6 @@ #include "Emulator.h" #include "MmapRegion.h" -#include "SharedBufferRegion.h" #include "SimpleRegion.h" #include "SoftCPU.h" #include @@ -385,14 +384,6 @@ u32 Emulator::virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3) return virt$ioctl(arg1, arg2, arg3); case SC_get_dir_entries: return virt$get_dir_entries(arg1, arg2, arg3); - case SC_shbuf_create: - return virt$shbuf_create(arg1, arg2); - case SC_shbuf_allow_pid: - return virt$shbuf_allow_pid(arg1, arg2); - case SC_shbuf_get: - return virt$shbuf_get(arg1, arg2); - case SC_shbuf_release: - return virt$shbuf_release(arg1); case SC_profiling_enable: return virt$profiling_enable(arg1); case SC_profiling_disable: @@ -560,48 +551,6 @@ int Emulator::virt$recvfd(int socket) return syscall(SC_recvfd, socket); } -int Emulator::virt$shbuf_create(int size, FlatPtr buffer) -{ - u8* host_data = nullptr; - int shbuf_id = syscall(SC_shbuf_create, size, &host_data); - if (shbuf_id < 0) - return shbuf_id; - FlatPtr address = allocate_vm(size, PAGE_SIZE); - auto region = SharedBufferRegion::create_with_shbuf_id(address, size, shbuf_id, host_data); - m_mmu.add_region(move(region)); - m_mmu.copy_to_vm(buffer, &address, sizeof(address)); - return shbuf_id; -} - -FlatPtr Emulator::virt$shbuf_get(int shbuf_id, FlatPtr size_ptr) -{ - size_t host_size = 0; - void* host_data = (void*)syscall(SC_shbuf_get, shbuf_id, &host_size); - if (host_data == (void*)-1) - return (FlatPtr)host_data; - FlatPtr address = allocate_vm(host_size, PAGE_SIZE); - auto region = SharedBufferRegion::create_with_shbuf_id(address, host_size, shbuf_id, (u8*)host_data); - m_mmu.add_region(move(region)); - m_mmu.copy_to_vm(size_ptr, &host_size, sizeof(host_size)); - return address; -} - -int Emulator::virt$shbuf_allow_pid(int shbuf_id, pid_t peer_pid) -{ - auto* region = m_mmu.shbuf_region(shbuf_id); - ASSERT(region); - return region->allow_pid(peer_pid); -} - -int Emulator::virt$shbuf_release(int shbuf_id) -{ - auto* region = m_mmu.shbuf_region(shbuf_id); - ASSERT(region); - auto rc = region->release(); - m_mmu.remove_region(*region); - return rc; -} - int Emulator::virt$profiling_enable(pid_t pid) { return syscall(SC_profiling_enable, pid); diff --git a/Userland/DevTools/UserspaceEmulator/Emulator.h b/Userland/DevTools/UserspaceEmulator/Emulator.h index dae845187f..c848e27d8e 100644 --- a/Userland/DevTools/UserspaceEmulator/Emulator.h +++ b/Userland/DevTools/UserspaceEmulator/Emulator.h @@ -91,10 +91,6 @@ private: int virt$stat(FlatPtr); int virt$realpath(FlatPtr); int virt$gethostname(FlatPtr, ssize_t); - int virt$shbuf_create(int size, FlatPtr buffer); - int virt$shbuf_allow_pid(int, pid_t peer_pid); - FlatPtr virt$shbuf_get(int shbuf_id, FlatPtr size); - int virt$shbuf_release(int shbuf_id); int virt$profiling_enable(pid_t); int virt$profiling_disable(pid_t); int virt$disown(pid_t); diff --git a/Userland/DevTools/UserspaceEmulator/SharedBufferRegion.cpp b/Userland/DevTools/UserspaceEmulator/SharedBufferRegion.cpp deleted file mode 100644 index 537200c2b3..0000000000 --- a/Userland/DevTools/UserspaceEmulator/SharedBufferRegion.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) 2020, 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 "SharedBufferRegion.h" -#include "Emulator.h" -#include -#include -#include -#include - -namespace UserspaceEmulator { - -NonnullOwnPtr SharedBufferRegion::create_with_shbuf_id(u32 base, u32 size, int shbuf_id, u8* host_data) -{ - return adopt_own(*new SharedBufferRegion(base, size, shbuf_id, host_data)); -} - -SharedBufferRegion::SharedBufferRegion(u32 base, u32 size, int shbuf_id, u8* host_data) - : Region(base, size) - , m_data(host_data) - , m_shbuf_id(shbuf_id) -{ - m_shadow_data = (u8*)malloc(size); - memset(m_shadow_data, 1, size); -} - -SharedBufferRegion::~SharedBufferRegion() -{ - free(m_shadow_data); -} - -ValueWithShadow SharedBufferRegion::read8(FlatPtr offset) -{ - ASSERT(offset < size()); - return { *reinterpret_cast(m_data + offset), *reinterpret_cast(m_shadow_data + offset) }; -} - -ValueWithShadow SharedBufferRegion::read16(u32 offset) -{ - ASSERT(offset + 1 < size()); - return { *reinterpret_cast(m_data + offset), *reinterpret_cast(m_shadow_data + offset) }; -} - -ValueWithShadow SharedBufferRegion::read32(u32 offset) -{ - ASSERT(offset + 3 < size()); - return { *reinterpret_cast(m_data + offset), *reinterpret_cast(m_shadow_data + offset) }; -} - -ValueWithShadow SharedBufferRegion::read64(u32 offset) -{ - ASSERT(offset + 7 < size()); - return { *reinterpret_cast(m_data + offset), *reinterpret_cast(m_shadow_data + offset) }; -} - -void SharedBufferRegion::write8(u32 offset, ValueWithShadow value) -{ - ASSERT(offset < size()); - *reinterpret_cast(m_data + offset) = value.value(); - *reinterpret_cast(m_shadow_data + offset) = value.shadow(); -} - -void SharedBufferRegion::write16(u32 offset, ValueWithShadow value) -{ - ASSERT(offset + 1 < size()); - *reinterpret_cast(m_data + offset) = value.value(); - *reinterpret_cast(m_shadow_data + offset) = value.shadow(); -} - -void SharedBufferRegion::write32(u32 offset, ValueWithShadow value) -{ - ASSERT(offset + 3 < size()); - *reinterpret_cast(m_data + offset) = value.value(); - *reinterpret_cast(m_shadow_data + offset) = value.shadow(); -} - -void SharedBufferRegion::write64(u32 offset, ValueWithShadow value) -{ - ASSERT(offset + 7 < size()); - *reinterpret_cast(m_data + offset) = value.value(); - *reinterpret_cast(m_shadow_data + offset) = value.shadow(); -} - -int SharedBufferRegion::allow_pid(pid_t pid) -{ - return syscall(SC_shbuf_allow_pid, m_shbuf_id, pid); -} - -int SharedBufferRegion::release() -{ - return syscall(SC_shbuf_release, m_shbuf_id); -} - -} diff --git a/Userland/DevTools/UserspaceEmulator/SharedBufferRegion.h b/Userland/DevTools/UserspaceEmulator/SharedBufferRegion.h deleted file mode 100644 index 58636f2643..0000000000 --- a/Userland/DevTools/UserspaceEmulator/SharedBufferRegion.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2020, 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. - */ - -#pragma once - -#include "SoftMMU.h" -#include - -namespace UserspaceEmulator { - -class SharedBufferRegion final : public Region { -public: - static NonnullOwnPtr create_with_shbuf_id(u32 base, u32 size, int shbuf_id, u8* shbuf_data); - virtual ~SharedBufferRegion() override; - - virtual ValueWithShadow read8(u32 offset) override; - virtual ValueWithShadow read16(u32 offset) override; - virtual ValueWithShadow read32(u32 offset) override; - virtual ValueWithShadow read64(u32 offset) override; - - virtual void write8(u32 offset, ValueWithShadow) override; - virtual void write16(u32 offset, ValueWithShadow) override; - virtual void write32(u32 offset, ValueWithShadow) override; - virtual void write64(u32 offset, ValueWithShadow) override; - - virtual u8* data() override { return m_data; } - virtual u8* shadow_data() override { return m_shadow_data; } - - int shbuf_id() const { return m_shbuf_id; } - - int allow_pid(pid_t); - int release(); - -private: - SharedBufferRegion(u32 base, u32 size, int shbuf_id, u8* shbuf_data); - - u8* m_data { nullptr }; - u8* m_shadow_data { nullptr }; - int m_shbuf_id { 0 }; -}; - -} diff --git a/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp b/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp index 50da131f1c..1822cb36d2 100644 --- a/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp +++ b/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp @@ -28,7 +28,6 @@ #include "Emulator.h" #include "MmapRegion.h" #include "Report.h" -#include "SharedBufferRegion.h" #include #include @@ -43,10 +42,6 @@ void SoftMMU::add_region(NonnullOwnPtr region) { ASSERT(!find_region({ 0x23, region->base() })); - // FIXME: More sanity checks pls - if (is(*region)) - m_shbuf_regions.set(static_cast(region.ptr())->shbuf_id(), region.ptr()); - size_t first_page_in_region = region->base() / PAGE_SIZE; size_t last_page_in_region = (region->base() + region->size() - 1) / PAGE_SIZE; for (size_t page = first_page_in_region; page <= last_page_in_region; ++page) { @@ -63,8 +58,6 @@ void SoftMMU::remove_region(Region& region) m_page_to_region_map[first_page_in_region + i] = nullptr; } - if (is(region)) - m_shbuf_regions.remove(static_cast(region).shbuf_id()); m_regions.remove_first_matching([&](auto& entry) { return entry.ptr() == ®ion; }); } @@ -238,11 +231,6 @@ ByteBuffer SoftMMU::copy_buffer_from_vm(const FlatPtr source, size_t size) return buffer; } -SharedBufferRegion* SoftMMU::shbuf_region(int shbuf_id) -{ - return (SharedBufferRegion*)m_shbuf_regions.get(shbuf_id).value_or(nullptr); -} - bool SoftMMU::fast_fill_memory8(X86::LogicalAddress address, size_t size, ValueWithShadow value) { if (!size) diff --git a/Userland/DevTools/UserspaceEmulator/SoftMMU.h b/Userland/DevTools/UserspaceEmulator/SoftMMU.h index 33351ff57b..8490a3e7e2 100644 --- a/Userland/DevTools/UserspaceEmulator/SoftMMU.h +++ b/Userland/DevTools/UserspaceEmulator/SoftMMU.h @@ -37,7 +37,6 @@ namespace UserspaceEmulator { class Emulator; -class SharedBufferRegion; class SoftMMU { public: @@ -74,8 +73,6 @@ public: void copy_from_vm(void* destination, const FlatPtr source, size_t); ByteBuffer copy_buffer_from_vm(const FlatPtr source, size_t); - SharedBufferRegion* shbuf_region(int shbuf_id); - template void for_each_region(Callback callback) { @@ -96,7 +93,6 @@ private: OwnPtr m_tls_region; NonnullOwnPtrVector m_regions; - HashMap m_shbuf_regions; }; } diff --git a/Userland/Libraries/LibC/serenity.cpp b/Userland/Libraries/LibC/serenity.cpp index eb25403262..15587ca6e1 100644 --- a/Userland/Libraries/LibC/serenity.cpp +++ b/Userland/Libraries/LibC/serenity.cpp @@ -79,34 +79,6 @@ int perf_event(int type, uintptr_t arg1, FlatPtr arg2) __RETURN_WITH_ERRNO(rc, rc, -1); } -void* shbuf_get(int shbuf_id, size_t* size) -{ - ssize_t rc = syscall(SC_shbuf_get, shbuf_id, size); - if (rc < 0 && -rc < EMAXERRNO) { - errno = -rc; - return (void*)-1; - } - return (void*)rc; -} - -int shbuf_release(int shbuf_id) -{ - int rc = syscall(SC_shbuf_release, shbuf_id); - __RETURN_WITH_ERRNO(rc, rc, -1); -} - -int shbuf_create(int size, void** buffer) -{ - int rc = syscall(SC_shbuf_create, size, buffer); - __RETURN_WITH_ERRNO(rc, rc, -1); -} - -int shbuf_allow_pid(int shbuf_id, pid_t peer_pid) -{ - int rc = syscall(SC_shbuf_allow_pid, shbuf_id, peer_pid); - __RETURN_WITH_ERRNO(rc, rc, -1); -} - int get_stack_bounds(uintptr_t* user_stack_base, size_t* user_stack_size) { int rc = syscall(SC_get_stack_bounds, user_stack_base, user_stack_size); diff --git a/Userland/Libraries/LibC/serenity.h b/Userland/Libraries/LibC/serenity.h index b3e8e7f3c2..42a726119e 100644 --- a/Userland/Libraries/LibC/serenity.h +++ b/Userland/Libraries/LibC/serenity.h @@ -33,11 +33,6 @@ __BEGIN_DECLS int disown(pid_t); -int shbuf_create(int, void** buffer); -int shbuf_allow_pid(int, pid_t peer_pid); -void* shbuf_get(int shbuf_id, size_t* size); -int shbuf_release(int shbuf_id); - int module_load(const char* path, size_t path_length); int module_unload(const char* name, size_t name_length);