mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-07 00:19:27 +00:00
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. :^)
This commit is contained in:
parent
2cd16778b5
commit
bf0719092f
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -1,168 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* 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 <AK/SharedBuffer.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(__serenity__)
|
||||
# include <Kernel/API/Syscall.h>
|
||||
# include <serenity.h>
|
||||
#else
|
||||
# include <AK/String.h>
|
||||
# include <fcntl.h>
|
||||
# include <sys/mman.h>
|
||||
|
||||
static String shbuf_shm_name(int shbuf_id)
|
||||
{
|
||||
return String::formatted("/serenity-shm:{}", shbuf_id);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
namespace AK {
|
||||
|
||||
RefPtr<SharedBuffer> 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<size_t*>(data);
|
||||
*size_data = size;
|
||||
data = reinterpret_cast<u8*>(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> 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<size_t*>(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<u8*>(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<u8*>(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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,68 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* 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 <AK/RefCounted.h>
|
||||
#include <AK/RefPtr.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
class SharedBuffer : public RefCounted<SharedBuffer> {
|
||||
public:
|
||||
static RefPtr<SharedBuffer> create_with_size(int);
|
||||
static RefPtr<SharedBuffer> 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<typename T>
|
||||
T* data()
|
||||
{
|
||||
static_assert(IsVoid<T>::value || is_trivial<T>());
|
||||
return (T*)m_data;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
const T* data() const
|
||||
{
|
||||
static_assert(IsVoid<T>::value || is_trivial<T>());
|
||||
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;
|
|
@ -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
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
## Name
|
||||
|
||||
shbuf\_allow\_pid - allow another process to map a shareable buffer
|
||||
|
||||
## Synopsis
|
||||
```**c++
|
||||
#include <SharedBuffer.h>
|
||||
|
||||
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)
|
|
@ -1,27 +0,0 @@
|
|||
## Name
|
||||
|
||||
shbuf\_create - create a shareable memory buffer
|
||||
|
||||
## Synopsis
|
||||
```**c++
|
||||
#include <SharedBuffer.h>
|
||||
|
||||
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)
|
|
@ -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) \
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -60,7 +60,6 @@ class RangeAllocator;
|
|||
class Region;
|
||||
class Scheduler;
|
||||
class SchedulerPerProcessorData;
|
||||
class SharedBuffer;
|
||||
class Socket;
|
||||
template<typename BaseType>
|
||||
class SpinLock;
|
||||
|
|
|
@ -45,10 +45,10 @@
|
|||
#include <Kernel/PerformanceEventBuffer.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <Kernel/RTC.h>
|
||||
#include <Kernel/SharedBuffer.h>
|
||||
#include <Kernel/StdLib.h>
|
||||
#include <Kernel/TTY/TTY.h>
|
||||
#include <Kernel/Thread.h>
|
||||
#include <Kernel/VM/AnonymousVMObject.h>
|
||||
#include <Kernel/VM/PageDirectory.h>
|
||||
#include <Kernel/VM/PrivateInodeVMObject.h>
|
||||
#include <Kernel/VM/ProcessPagingScope.h>
|
||||
|
@ -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())) {
|
||||
|
|
|
@ -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<char*> buffer, size_t buffer_size);
|
||||
int sys$rename(Userspace<const Syscall::SC_rename_params*>);
|
||||
int sys$mknod(Userspace<const Syscall::SC_mknod_params*>);
|
||||
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_t*> size);
|
||||
int sys$shbuf_release(int shbuf_id);
|
||||
int sys$halt();
|
||||
int sys$reboot();
|
||||
int sys$realpath(Userspace<const Syscall::SC_realpath_params*>);
|
||||
|
@ -530,7 +525,6 @@ private:
|
|||
KResultOr<RefPtr<FileDescription>> 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);
|
||||
|
|
|
@ -1,188 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* 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 <AK/Debug.h>
|
||||
#include <AK/Singleton.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <Kernel/SharedBuffer.h>
|
||||
|
||||
//#define SHARED_BUFFER_DEBUG
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
static AK::Singleton<Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>> s_map;
|
||||
|
||||
Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>& 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<debug_shared_buffer>("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<debug_shared_buffer>("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<debug_shared_buffer>("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<debug_shared_buffer>("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<debug_shared_buffer>("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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,87 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* 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 <AK/Debug.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/WeakPtr.h>
|
||||
#include <Kernel/VM/AnonymousVMObject.h>
|
||||
#include <Kernel/VM/MemoryManager.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
class SharedBuffer {
|
||||
private:
|
||||
struct Reference {
|
||||
Reference(ProcessID pid, unsigned count = 0)
|
||||
: pid(pid)
|
||||
, count(count)
|
||||
{
|
||||
}
|
||||
|
||||
ProcessID pid;
|
||||
unsigned count { 0 };
|
||||
WeakPtr<Region> region;
|
||||
};
|
||||
|
||||
public:
|
||||
SharedBuffer(int id, NonnullRefPtr<AnonymousVMObject>&& vmobject)
|
||||
: m_shbuf_id(id)
|
||||
, m_vmobject(move(vmobject))
|
||||
{
|
||||
dbgln<debug_shared_buffer>("Created shared buffer {} of size {}", m_shbuf_id, size());
|
||||
}
|
||||
|
||||
~SharedBuffer()
|
||||
{
|
||||
dbgln<debug_shared_buffer>("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<AnonymousVMObject> m_vmobject;
|
||||
Vector<Reference, 2> m_refs;
|
||||
unsigned m_total_refs { 0 };
|
||||
};
|
||||
|
||||
Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>& shared_buffers();
|
||||
|
||||
}
|
|
@ -522,8 +522,6 @@ int Process::do_exec(NonnullRefPtr<FileDescription> 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) {
|
||||
|
|
|
@ -27,7 +27,6 @@
|
|||
#include <Kernel/FileSystem/Custody.h>
|
||||
#include <Kernel/FileSystem/FileDescription.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <Kernel/SharedBuffer.h>
|
||||
#include <Kernel/VM/Region.h>
|
||||
|
||||
//#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) {
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
|
||||
#include <AK/Types.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <Kernel/SharedBuffer.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
|
|
|
@ -1,137 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* 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 <Kernel/Process.h>
|
||||
#include <Kernel/SharedBuffer.h>
|
||||
|
||||
//#define SHARED_BUFFER_DEBUG
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
void Process::disown_all_shared_buffers()
|
||||
{
|
||||
LOCKER(shared_buffers().lock());
|
||||
Vector<SharedBuffer*, 32> 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<SharedBuffer>(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<size_t*> 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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")
|
||||
|
|
|
@ -3,7 +3,6 @@ set(SOURCES
|
|||
MallocTracer.cpp
|
||||
MmapRegion.cpp
|
||||
Region.cpp
|
||||
SharedBufferRegion.cpp
|
||||
SimpleRegion.cpp
|
||||
SoftCPU.cpp
|
||||
SoftMMU.cpp
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
|
||||
#include "Emulator.h"
|
||||
#include "MmapRegion.h"
|
||||
#include "SharedBufferRegion.h"
|
||||
#include "SimpleRegion.h"
|
||||
#include "SoftCPU.h"
|
||||
#include <AK/Format.h>
|
||||
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -1,117 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* 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 <Kernel/API/Syscall.h>
|
||||
#include <serenity.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
namespace UserspaceEmulator {
|
||||
|
||||
NonnullOwnPtr<SharedBufferRegion> 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<u8> SharedBufferRegion::read8(FlatPtr offset)
|
||||
{
|
||||
ASSERT(offset < size());
|
||||
return { *reinterpret_cast<const u8*>(m_data + offset), *reinterpret_cast<const u8*>(m_shadow_data + offset) };
|
||||
}
|
||||
|
||||
ValueWithShadow<u16> SharedBufferRegion::read16(u32 offset)
|
||||
{
|
||||
ASSERT(offset + 1 < size());
|
||||
return { *reinterpret_cast<const u16*>(m_data + offset), *reinterpret_cast<const u16*>(m_shadow_data + offset) };
|
||||
}
|
||||
|
||||
ValueWithShadow<u32> SharedBufferRegion::read32(u32 offset)
|
||||
{
|
||||
ASSERT(offset + 3 < size());
|
||||
return { *reinterpret_cast<const u32*>(m_data + offset), *reinterpret_cast<const u32*>(m_shadow_data + offset) };
|
||||
}
|
||||
|
||||
ValueWithShadow<u64> SharedBufferRegion::read64(u32 offset)
|
||||
{
|
||||
ASSERT(offset + 7 < size());
|
||||
return { *reinterpret_cast<const u64*>(m_data + offset), *reinterpret_cast<const u64*>(m_shadow_data + offset) };
|
||||
}
|
||||
|
||||
void SharedBufferRegion::write8(u32 offset, ValueWithShadow<u8> value)
|
||||
{
|
||||
ASSERT(offset < size());
|
||||
*reinterpret_cast<u8*>(m_data + offset) = value.value();
|
||||
*reinterpret_cast<u8*>(m_shadow_data + offset) = value.shadow();
|
||||
}
|
||||
|
||||
void SharedBufferRegion::write16(u32 offset, ValueWithShadow<u16> value)
|
||||
{
|
||||
ASSERT(offset + 1 < size());
|
||||
*reinterpret_cast<u16*>(m_data + offset) = value.value();
|
||||
*reinterpret_cast<u16*>(m_shadow_data + offset) = value.shadow();
|
||||
}
|
||||
|
||||
void SharedBufferRegion::write32(u32 offset, ValueWithShadow<u32> value)
|
||||
{
|
||||
ASSERT(offset + 3 < size());
|
||||
*reinterpret_cast<u32*>(m_data + offset) = value.value();
|
||||
*reinterpret_cast<u32*>(m_shadow_data + offset) = value.shadow();
|
||||
}
|
||||
|
||||
void SharedBufferRegion::write64(u32 offset, ValueWithShadow<u64> value)
|
||||
{
|
||||
ASSERT(offset + 7 < size());
|
||||
*reinterpret_cast<u64*>(m_data + offset) = value.value();
|
||||
*reinterpret_cast<u64*>(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);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* 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 <sys/mman.h>
|
||||
|
||||
namespace UserspaceEmulator {
|
||||
|
||||
class SharedBufferRegion final : public Region {
|
||||
public:
|
||||
static NonnullOwnPtr<SharedBufferRegion> create_with_shbuf_id(u32 base, u32 size, int shbuf_id, u8* shbuf_data);
|
||||
virtual ~SharedBufferRegion() override;
|
||||
|
||||
virtual ValueWithShadow<u8> read8(u32 offset) override;
|
||||
virtual ValueWithShadow<u16> read16(u32 offset) override;
|
||||
virtual ValueWithShadow<u32> read32(u32 offset) override;
|
||||
virtual ValueWithShadow<u64> read64(u32 offset) override;
|
||||
|
||||
virtual void write8(u32 offset, ValueWithShadow<u8>) override;
|
||||
virtual void write16(u32 offset, ValueWithShadow<u16>) override;
|
||||
virtual void write32(u32 offset, ValueWithShadow<u32>) override;
|
||||
virtual void write64(u32 offset, ValueWithShadow<u64>) 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 };
|
||||
};
|
||||
|
||||
}
|
|
@ -28,7 +28,6 @@
|
|||
#include "Emulator.h"
|
||||
#include "MmapRegion.h"
|
||||
#include "Report.h"
|
||||
#include "SharedBufferRegion.h"
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/Memory.h>
|
||||
|
||||
|
@ -43,10 +42,6 @@ void SoftMMU::add_region(NonnullOwnPtr<Region> region)
|
|||
{
|
||||
ASSERT(!find_region({ 0x23, region->base() }));
|
||||
|
||||
// FIXME: More sanity checks pls
|
||||
if (is<SharedBufferRegion>(*region))
|
||||
m_shbuf_regions.set(static_cast<SharedBufferRegion*>(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<SharedBufferRegion>(region))
|
||||
m_shbuf_regions.remove(static_cast<SharedBufferRegion&>(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<u8> value)
|
||||
{
|
||||
if (!size)
|
||||
|
|
|
@ -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<typename Callback>
|
||||
void for_each_region(Callback callback)
|
||||
{
|
||||
|
@ -96,7 +93,6 @@ private:
|
|||
|
||||
OwnPtr<Region> m_tls_region;
|
||||
NonnullOwnPtrVector<Region> m_regions;
|
||||
HashMap<int, Region*> m_shbuf_regions;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue