From d7a34f09048117b3db0dcae88f8fe260904329de Mon Sep 17 00:00:00 2001 From: Ivan Chikish Date: Tue, 4 Jul 2023 19:19:17 +0300 Subject: [PATCH] Initial kernel allocator --- orbis-kernel/CMakeLists.txt | 1 + .../include/orbis/KernelAllocator.hpp | 57 ++++++++++ orbis-kernel/include/orbis/KernelContext.hpp | 106 +++++------------- orbis-kernel/include/orbis/module/Module.hpp | 20 ++-- orbis-kernel/include/orbis/utils/Rc.hpp | 13 ++- orbis-kernel/src/KernelContext.cpp | 91 +++++++++++++++ orbis-kernel/src/module.cpp | 1 - rpcsx-os/io-device.cpp | 5 +- rpcsx-os/iodev/dce.cpp | 5 +- rpcsx-os/iodev/dipsw.cpp | 7 +- rpcsx-os/iodev/dmem.cpp | 5 +- rpcsx-os/iodev/gc.cpp | 5 +- rpcsx-os/iodev/hid.cpp | 5 +- rpcsx-os/iodev/hmd_3da.cpp | 5 +- rpcsx-os/iodev/hmd_cmd.cpp | 5 +- rpcsx-os/iodev/hmd_mmap.cpp | 5 +- rpcsx-os/iodev/hmd_snsr.cpp | 6 +- rpcsx-os/iodev/null.cpp | 5 +- rpcsx-os/iodev/rng.cpp | 5 +- rpcsx-os/iodev/stderr.cpp | 5 +- rpcsx-os/iodev/stdin.cpp | 5 +- rpcsx-os/iodev/stdout.cpp | 5 +- rpcsx-os/iodev/zero.cpp | 5 +- rpcsx-os/linker.cpp | 5 +- rpcsx-os/main.cpp | 14 +-- 25 files changed, 247 insertions(+), 144 deletions(-) create mode 100644 orbis-kernel/include/orbis/KernelAllocator.hpp create mode 100644 orbis-kernel/src/KernelContext.cpp diff --git a/orbis-kernel/CMakeLists.txt b/orbis-kernel/CMakeLists.txt index 1c373699b..1a445145c 100644 --- a/orbis-kernel/CMakeLists.txt +++ b/orbis-kernel/CMakeLists.txt @@ -3,6 +3,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE on) add_library(obj.orbis-kernel OBJECT src/module.cpp src/sysvec.cpp + src/KernelContext.cpp src/sys/sys_acct.cpp src/sys/sys_audit.cpp src/sys/sys_capability.cpp diff --git a/orbis-kernel/include/orbis/KernelAllocator.hpp b/orbis-kernel/include/orbis/KernelAllocator.hpp new file mode 100644 index 000000000..22537bda0 --- /dev/null +++ b/orbis-kernel/include/orbis/KernelAllocator.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include "utils/Rc.hpp" +#include +#include +#include +#include +#include + +namespace orbis { +inline namespace utils { +void *kalloc(std::size_t size, std::size_t align); +void kfree(void* ptr, std::size_t size); +template struct kallocator { + using value_type = T; + + template struct rebind { using other = kallocator; }; + + T *allocate(std::size_t n) { + return static_cast(kalloc(sizeof(T) * n, alignof(T))); + } + + void deallocate(T *p, std::size_t n) { + kfree(p, sizeof(T) * n); + } + + template + friend constexpr bool operator==(const kallocator &, + const kallocator &) noexcept { + return true; + } +}; + +template using kvector = std::vector>; +template using kdeque = std::deque>; +template > +using kmap = std::map>>; +template , + typename Pred = std::equal_to> +using kunmap = + std::unordered_map>>; +} // namespace utils + +template T *knew(Args &&...args) { + auto loc = static_cast(utils::kalloc(sizeof(T), alignof(T))); + auto res = std::construct_at(loc, std::forward(args)...); + if constexpr (WithRc) + res->_total_size = sizeof(T); + return res; +} + +template void kdelete(T *ptr) { + ptr->~T(); + utils::kfree(ptr, sizeof(T)); +} + +} // namespace orbis diff --git a/orbis-kernel/include/orbis/KernelContext.hpp b/orbis-kernel/include/orbis/KernelContext.hpp index 864660c18..6b2b7e535 100644 --- a/orbis-kernel/include/orbis/KernelContext.hpp +++ b/orbis-kernel/include/orbis/KernelContext.hpp @@ -1,97 +1,41 @@ #pragma once -#include "orbis/thread/Process.hpp" #include "utils/LinkedNode.hpp" #include "utils/SharedMutex.hpp" +#include "orbis/thread/types.hpp" +#include "KernelAllocator.hpp" #include -#include #include -#include namespace orbis { -class KernelContext { +struct Process; + +class alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__) KernelContext final { public: - struct EventListener { - virtual ~EventListener() = default; - virtual void onProcessCreated(Process *) = 0; - virtual void onProcessDeleted(pid_t pid) = 0; - }; + KernelContext(); + ~KernelContext(); - ~KernelContext() { - while (m_processes != nullptr) { - deleteProcess(&m_processes->object); - } - } + Process *createProcess(pid_t pid); + void deleteProcess(Process *proc); + Process *findProcessById(pid_t pid) const; - void addEventListener(EventListener *listener) { - m_event_listeners.push_back(listener); - } - - void removeEventListener(EventListener *listener) { - auto it = - std::find(m_event_listeners.begin(), m_event_listeners.end(), listener); - - if (it != m_event_listeners.end()) { - m_event_listeners.erase(it); - } - } - - Process *createProcess(pid_t pid) { - auto newProcess = new utils::LinkedNode(); - newProcess->object.context = this; - newProcess->object.pid = pid; - newProcess->object.state = ProcessState::NEW; - - { - std::lock_guard lock(m_proc_mtx); - if (m_processes != nullptr) { - m_processes->insertPrev(*newProcess); - } - - m_processes = newProcess; - } - - for (auto listener : m_event_listeners) { - listener->onProcessCreated(&newProcess->object); - } - - return &newProcess->object; - } - - void deleteProcess(Process *proc) { - auto procNode = reinterpret_cast *>(proc); - auto pid = proc->pid; - - { - std::lock_guard lock(m_proc_mtx); - auto next = procNode->erase(); - - if (procNode == m_processes) { - m_processes = next; - } - } - - delete procNode; - - for (auto listener : m_event_listeners) { - listener->onProcessDeleted(pid); - } - } - - Process *findProcessById(pid_t pid) const { - std::lock_guard lock(m_proc_mtx); - for (auto proc = m_processes; proc != nullptr; proc = proc->next) { - if (proc->object.pid == pid) { - return &proc->object; - } - } - - return nullptr; - } + static void *kalloc(std::size_t size, + std::size_t align = __STDCPP_DEFAULT_NEW_ALIGNMENT__); + static void kfree(void *ptr, std::size_t size); private: mutable shared_mutex m_proc_mtx; utils::LinkedNode *m_processes = nullptr; - std::vector m_event_listeners; + + struct node { + std::size_t size; + node *next; + }; + + mutable shared_mutex m_heap_mtx; + void *m_heap_next = this + 1; + node *m_free_list = nullptr; }; -} // namespace orbis \ No newline at end of file + +extern KernelContext &g_context; +} // namespace orbis diff --git a/orbis-kernel/include/orbis/module/Module.hpp b/orbis-kernel/include/orbis/module/Module.hpp index 29c6613ef..1e67bf029 100644 --- a/orbis-kernel/include/orbis/module/Module.hpp +++ b/orbis-kernel/include/orbis/module/Module.hpp @@ -4,6 +4,7 @@ #include "ModuleSegment.hpp" #include "../utils/Rc.hpp" +#include "../KernelAllocator.hpp" #include "orbis-config.hpp" #include @@ -65,7 +66,7 @@ struct Relocation { std::int64_t addend; }; -struct Module { +struct Module final { Process *proc{}; std::string vfsPath; char moduleName[256]{}; @@ -108,16 +109,17 @@ struct Module { bool isTlsDone = false; - std::vector symbols; - std::vector pltRelocations; - std::vector nonPltRelocations; - std::vector neededModules; - std::vector neededLibraries; - std::vector> importedModules; - std::vector> namespaceModules; - std::vector needed; + utils::kvector symbols; + utils::kvector pltRelocations; + utils::kvector nonPltRelocations; + utils::kvector neededModules; + utils::kvector neededLibraries; + utils::kvector> importedModules; + utils::kvector> namespaceModules; + utils::kvector needed; std::atomic references{0}; + unsigned _total_size = 0; void incRef() { if (references.fetch_add(1, std::memory_order::relaxed) > 512) { diff --git a/orbis-kernel/include/orbis/utils/Rc.hpp b/orbis-kernel/include/orbis/utils/Rc.hpp index 8eb4b25a4..2790772ea 100644 --- a/orbis-kernel/include/orbis/utils/Rc.hpp +++ b/orbis-kernel/include/orbis/utils/Rc.hpp @@ -6,9 +6,13 @@ #include namespace orbis { +template T *knew(Args &&...args); inline namespace utils { +void kfree(void* ptr, std::size_t size); + struct RcBase { std::atomic references{0}; + unsigned _total_size = 0; // Set by knew/kcreate virtual ~RcBase() = default; @@ -21,7 +25,9 @@ struct RcBase { // returns true if object was destroyed bool decRef() { if (references.fetch_sub(1, std::memory_order::relaxed) == 1) { - delete this; + auto size = _total_size; + this->~RcBase(); + orbis::utils::kfree(this, size); return true; } @@ -116,9 +122,8 @@ public: template requires(std::is_constructible_v) -Ref create(ArgsT &&...args) { - auto result = new T(std::forward(args)...); - return Ref(result); +Ref kcreate(ArgsT &&...args) { + return Ref(knew(std::forward(args)...)); } } // namespace utils } // namespace orbis diff --git a/orbis-kernel/src/KernelContext.cpp b/orbis-kernel/src/KernelContext.cpp new file mode 100644 index 000000000..9a5de8699 --- /dev/null +++ b/orbis-kernel/src/KernelContext.cpp @@ -0,0 +1,91 @@ +#include "orbis/KernelContext.hpp" +#include "orbis/thread/Process.hpp" +#include +#include + +namespace orbis { +KernelContext &g_context = *[]() -> KernelContext * { + // Allocate global shared kernel memory + // TODO: randomize for hardening and reduce size + auto ptr = mmap(reinterpret_cast(0x200'0000'0000), 0x1'0000'0000, + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0); + if (!ptr) + std::abort(); + + return new (ptr) KernelContext; +}(); + +KernelContext::KernelContext() {} +KernelContext::~KernelContext() {} + +Process *KernelContext::createProcess(pid_t pid) { + auto newProcess = knew>(); + newProcess->object.context = this; + newProcess->object.pid = pid; + newProcess->object.state = ProcessState::NEW; + + { + std::lock_guard lock(m_proc_mtx); + if (m_processes != nullptr) { + m_processes->insertPrev(*newProcess); + } + + m_processes = newProcess; + } + + return &newProcess->object; +} + +void KernelContext::deleteProcess(Process *proc) { + auto procNode = reinterpret_cast *>(proc); + auto pid = proc->pid; + + { + std::lock_guard lock(m_proc_mtx); + auto next = procNode->erase(); + + if (procNode == m_processes) { + m_processes = next; + } + } + + kdelete(procNode); +} + +Process *KernelContext::findProcessById(pid_t pid) const { + std::lock_guard lock(m_proc_mtx); + for (auto proc = m_processes; proc != nullptr; proc = proc->next) { + if (proc->object.pid == pid) { + return &proc->object; + } + } + + return nullptr; +} + +void *KernelContext::kalloc(std::size_t size, std::size_t align) { + std::lock_guard lock(g_context.m_heap_mtx); + align = std::max(align, sizeof(node)); + auto heap = reinterpret_cast(g_context.m_heap_next); + heap = (heap + (align - 1)) & ~(align - 1); + auto result = reinterpret_cast(heap); + g_context.m_heap_next = reinterpret_cast(heap + size); + return result; +} + +void KernelContext::kfree(void *ptr, std::size_t size) { + std::lock_guard lock(g_context.m_heap_mtx); + if (!size) + std::abort(); + // TODO: create node and put it into +} + +inline namespace utils { +void kfree(void *ptr, std::size_t size) { + return KernelContext::kfree(ptr, size); +} +void *kalloc(std::size_t size, std::size_t align) { + return KernelContext::kalloc(size, align); +} +} // namespace utils +} // namespace orbis diff --git a/orbis-kernel/src/module.cpp b/orbis-kernel/src/module.cpp index bc695bc1e..731ebccfb 100644 --- a/orbis-kernel/src/module.cpp +++ b/orbis-kernel/src/module.cpp @@ -2,7 +2,6 @@ #include "thread.hpp" #include -#include "module/Module.hpp" #include "thread/Process.hpp" #include diff --git a/rpcsx-os/io-device.cpp b/rpcsx-os/io-device.cpp index b0cdd730e..c3b4bab6d 100644 --- a/rpcsx-os/io-device.cpp +++ b/rpcsx-os/io-device.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include #include #include @@ -108,7 +109,7 @@ static std::int32_t host_io_open(IoDevice *device, orbis::Ref return 1; // TODO: convert errno } - auto newInstance = new HostIoDeviceInstance(); + auto newInstance = orbis::knew(); newInstance->hostFd = hostFd; @@ -124,7 +125,7 @@ static std::int32_t host_io_open(IoDevice *device, orbis::Ref } IoDevice *createHostIoDevice(const char *hostPath) { - auto result = new HostIoDevice(); + auto result = orbis::knew(); result->open = host_io_open; result->hostPath = hostPath; return result; diff --git a/rpcsx-os/iodev/dce.cpp b/rpcsx-os/iodev/dce.cpp index 9d383df1c..5c628a48e 100644 --- a/rpcsx-os/iodev/dce.cpp +++ b/rpcsx-os/iodev/dce.cpp @@ -1,5 +1,6 @@ #include "bridge.hpp" #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include #include #include @@ -245,7 +246,7 @@ static std::int32_t dce_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new DceInstance(); + auto *newInstance = orbis::knew(); newInstance->ioctl = dce_instance_ioctl; newInstance->mmap = dce_instance_mmap; io_device_instance_init(device, newInstance); @@ -254,7 +255,7 @@ static std::int32_t dce_device_open(IoDevice *device, } IoDevice *createDceCharacterDevice() { - auto *newDevice = new DceDevice(); + auto *newDevice = orbis::knew(); newDevice->open = dce_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/dipsw.cpp b/rpcsx-os/iodev/dipsw.cpp index 8cf05ff9d..41c1f55e7 100644 --- a/rpcsx-os/iodev/dipsw.cpp +++ b/rpcsx-os/iodev/dipsw.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include struct DmemDevice : public IoDevice { @@ -26,7 +27,7 @@ static std::int64_t dipsw_instance_ioctl(IoDeviceInstance *instance, } - // 0x8010880a + // 0x8010880a if (request == 0x8010880a) { // write data? used on initilization struct Args { std::uint64_t address; @@ -50,7 +51,7 @@ static std::int32_t dipsw_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new DmemInstance(); + auto *newInstance = orbis::knew(); newInstance->ioctl = dipsw_instance_ioctl; io_device_instance_init(device, newInstance); *instance = newInstance; @@ -58,7 +59,7 @@ static std::int32_t dipsw_device_open(IoDevice *device, } IoDevice *createDipswCharacterDevice() { - auto *newDevice = new DmemDevice(); + auto *newDevice = orbis::knew(); newDevice->open = dipsw_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/dmem.cpp b/rpcsx-os/iodev/dmem.cpp index b16a98d6e..d94e85012 100644 --- a/rpcsx-os/iodev/dmem.cpp +++ b/rpcsx-os/iodev/dmem.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include #include #include "vm.hpp" @@ -105,7 +106,7 @@ static std::int32_t dmem_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new DmemInstance{}; + auto *newInstance = orbis::knew(); newInstance->ioctl = dmem_instance_ioctl; newInstance->mmap = dmem_instance_mmap; @@ -115,7 +116,7 @@ static std::int32_t dmem_device_open(IoDevice *device, } IoDevice *createDmemCharacterDevice(int index) { - auto *newDevice = new DmemDevice(); + auto *newDevice = orbis::knew(); newDevice->open = dmem_device_open; newDevice->index = index; newDevice->nextOffset = 0; diff --git a/rpcsx-os/iodev/gc.cpp b/rpcsx-os/iodev/gc.cpp index 5714160cb..7b7ac1c98 100644 --- a/rpcsx-os/iodev/gc.cpp +++ b/rpcsx-os/iodev/gc.cpp @@ -1,5 +1,6 @@ #include "bridge.hpp" #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include #include #include @@ -251,7 +252,7 @@ static std::int32_t gc_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new GcInstance{}; + auto *newInstance = orbis::knew(); newInstance->ioctl = gc_instance_ioctl; newInstance->mmap = gc_instance_mmap; io_device_instance_init(device, newInstance); @@ -260,7 +261,7 @@ static std::int32_t gc_device_open(IoDevice *device, } IoDevice *createGcCharacterDevice() { - auto *newDevice = new GcDevice; + auto *newDevice = orbis::knew(); newDevice->open = gc_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/hid.cpp b/rpcsx-os/iodev/hid.cpp index 67597f6bf..dee41fc88 100644 --- a/rpcsx-os/iodev/hid.cpp +++ b/rpcsx-os/iodev/hid.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include "vm.hpp" #include #include @@ -25,7 +26,7 @@ static std::int32_t hid_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new HidInstance{}; + auto *newInstance = orbis::knew(); newInstance->ioctl = hid_instance_ioctl; newInstance->mmap = hid_instance_mmap; @@ -35,7 +36,7 @@ static std::int32_t hid_device_open(IoDevice *device, } IoDevice *createHidCharacterDevice() { - auto *newDevice = new HidDevice; + auto *newDevice = orbis::knew(); newDevice->open = hid_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/hmd_3da.cpp b/rpcsx-os/iodev/hmd_3da.cpp index 074af744c..7043929e3 100644 --- a/rpcsx-os/iodev/hmd_3da.cpp +++ b/rpcsx-os/iodev/hmd_3da.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include struct Hmd3daDevice : public IoDevice { @@ -19,7 +20,7 @@ static std::int32_t hmd_3da_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new Hmd3daInstance{}; + auto *newInstance = orbis::knew(); newInstance->ioctl = hmd_3da_instance_ioctl; io_device_instance_init(device, newInstance); @@ -28,7 +29,7 @@ static std::int32_t hmd_3da_device_open(IoDevice *device, } IoDevice *createHmd3daCharacterDevice() { - auto *newDevice = new Hmd3daDevice(); + auto *newDevice = orbis::knew(); newDevice->open = hmd_3da_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/hmd_cmd.cpp b/rpcsx-os/iodev/hmd_cmd.cpp index e22f17650..bf7dcd2e9 100644 --- a/rpcsx-os/iodev/hmd_cmd.cpp +++ b/rpcsx-os/iodev/hmd_cmd.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include struct HmdCmdDevice : public IoDevice { @@ -19,7 +20,7 @@ static std::int32_t hmd_cmd_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new HmdCmdInstance{}; + auto *newInstance = orbis::knew(); newInstance->ioctl = hmd_cmd_instance_ioctl; io_device_instance_init(device, newInstance); @@ -29,7 +30,7 @@ static std::int32_t hmd_cmd_device_open(IoDevice *device, } IoDevice *createHmdCmdCharacterDevice() { - auto *newDevice = new HmdCmdDevice(); + auto *newDevice = orbis::knew(); newDevice->open = hmd_cmd_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/hmd_mmap.cpp b/rpcsx-os/iodev/hmd_mmap.cpp index 0a8953138..d7666655a 100644 --- a/rpcsx-os/iodev/hmd_mmap.cpp +++ b/rpcsx-os/iodev/hmd_mmap.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include "vm.hpp" #include #include @@ -31,7 +32,7 @@ static std::int32_t hmd_mmap_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new HmdMmapInstance{}; + auto *newInstance = orbis::knew(); newInstance->ioctl = hmd_mmap_instance_ioctl; newInstance->mmap = hmd_mmap_instance_mmap; @@ -41,7 +42,7 @@ static std::int32_t hmd_mmap_device_open(IoDevice *device, } IoDevice *createHmdMmapCharacterDevice() { - auto *newDevice = new HmdMmapDevice(); + auto *newDevice = orbis::knew(); newDevice->open = hmd_mmap_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/hmd_snsr.cpp b/rpcsx-os/iodev/hmd_snsr.cpp index 1d1e2753e..6ae0d3875 100644 --- a/rpcsx-os/iodev/hmd_snsr.cpp +++ b/rpcsx-os/iodev/hmd_snsr.cpp @@ -1,5 +1,5 @@ #include "io-device.hpp" - +#include "orbis/KernelAllocator.hpp" #include struct HmdSnsrDevice : public IoDevice {}; @@ -19,7 +19,7 @@ static std::int32_t smd_snr_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new HmdSnsrInstance{}; + auto *newInstance = orbis::knew(); newInstance->ioctl = smd_snr_instance_ioctl; io_device_instance_init(device, newInstance); *instance = newInstance; @@ -27,7 +27,7 @@ static std::int32_t smd_snr_device_open(IoDevice *device, } IoDevice *createHmdSnsrCharacterDevice() { - auto *newDevice = new HmdSnsrDevice(); + auto *newDevice = orbis::knew(); newDevice->open = smd_snr_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/null.cpp b/rpcsx-os/iodev/null.cpp index 3bab3ae0c..3eae1f622 100644 --- a/rpcsx-os/iodev/null.cpp +++ b/rpcsx-os/iodev/null.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" struct NullDevice : public IoDevice {}; @@ -13,7 +14,7 @@ static std::int64_t null_instance_write(IoDeviceInstance *instance, static std::int32_t null_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new NullInstance{}; + auto *newInstance = orbis::knew(); newInstance->write = null_instance_write; io_device_instance_init(device, newInstance); @@ -22,7 +23,7 @@ static std::int32_t null_device_open(IoDevice *device, orbis::Ref(); newDevice->open = null_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/rng.cpp b/rpcsx-os/iodev/rng.cpp index 62053a807..f28875779 100644 --- a/rpcsx-os/iodev/rng.cpp +++ b/rpcsx-os/iodev/rng.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include "vm.hpp" #include #include @@ -23,7 +24,7 @@ static std::int32_t rng_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new RngInstance{}; + auto *newInstance = orbis::knew(); newInstance->ioctl = rng_instance_ioctl; newInstance->mmap = rng_instance_mmap; @@ -33,7 +34,7 @@ static std::int32_t rng_device_open(IoDevice *device, } IoDevice *createRngCharacterDevice() { - auto *newDevice = new RngDevice{}; + auto *newDevice = orbis::knew(); newDevice->open = rng_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/stderr.cpp b/rpcsx-os/iodev/stderr.cpp index be49c06ad..7b0fd02d5 100644 --- a/rpcsx-os/iodev/stderr.cpp +++ b/rpcsx-os/iodev/stderr.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include struct StderrInstance : public IoDeviceInstance {}; @@ -25,7 +26,7 @@ static std::int32_t stderr_device_open(IoDevice *device, std::uint32_t mode) { auto stderrDevice = static_cast(device); if (stderrDevice->instance == nullptr) { - auto *newInstance = new StderrInstance{}; + auto *newInstance = orbis::knew(); newInstance->write = stderr_instance_write; newInstance->close = stderr_instance_close; @@ -42,7 +43,7 @@ static std::int32_t stderr_device_open(IoDevice *device, } IoDevice *createStderrCharacterDevice() { - auto *newDevice = new StderrDevice(); + auto *newDevice = orbis::knew(); newDevice->open = stderr_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/stdin.cpp b/rpcsx-os/iodev/stdin.cpp index c7a86d546..d497c0774 100644 --- a/rpcsx-os/iodev/stdin.cpp +++ b/rpcsx-os/iodev/stdin.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" struct StdinDevice : public IoDevice { }; @@ -14,7 +15,7 @@ static std::int64_t stdin_instance_read(IoDeviceInstance *instance, void *data, static std::int32_t open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new StdinInstance{}; + auto *newInstance = orbis::knew(); newInstance->read = stdin_instance_read; io_device_instance_init(device, newInstance); *instance = newInstance; @@ -22,7 +23,7 @@ static std::int32_t open(IoDevice *device, orbis::Ref *instanc } IoDevice *createStdinCharacterDevice() { - auto *newDevice = new StdinDevice(); + auto *newDevice = orbis::knew(); newDevice->open = open; return newDevice; } diff --git a/rpcsx-os/iodev/stdout.cpp b/rpcsx-os/iodev/stdout.cpp index 5cf92c2fd..0e42709fe 100644 --- a/rpcsx-os/iodev/stdout.cpp +++ b/rpcsx-os/iodev/stdout.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include @@ -28,7 +29,7 @@ static std::int32_t stdout_device_open(IoDevice *device, std::uint32_t mode) { auto stdoutDevice = static_cast(device); if (stdoutDevice->instance == nullptr) { - auto *newInstance = new StdoutInstance{}; + auto *newInstance = orbis::knew(); newInstance->write = stdout_instance_write; newInstance->close = stdout_instance_close; io_device_instance_init(device, newInstance); @@ -43,7 +44,7 @@ static std::int32_t stdout_device_open(IoDevice *device, } IoDevice *createStdoutCharacterDevice() { - auto *newDevice = new StdoutDevice(); + auto *newDevice = orbis::knew(); newDevice->open = stdout_device_open; return newDevice; } diff --git a/rpcsx-os/iodev/zero.cpp b/rpcsx-os/iodev/zero.cpp index 6bc907580..1d8bb42c5 100644 --- a/rpcsx-os/iodev/zero.cpp +++ b/rpcsx-os/iodev/zero.cpp @@ -1,4 +1,5 @@ #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include struct ZeroDevice : public IoDevice { @@ -17,7 +18,7 @@ static std::int32_t zero_device_open(IoDevice *device, orbis::Ref *instance, const char *path, std::uint32_t flags, std::uint32_t mode) { - auto *newInstance = new ZeroInstance{}; + auto *newInstance = orbis::knew(); newInstance->read = zero_device_read; io_device_instance_init(device, newInstance); @@ -26,7 +27,7 @@ static std::int32_t zero_device_open(IoDevice *device, } IoDevice *createZeroCharacterDevice() { - auto *newDevice = new ZeroDevice(); + auto *newDevice = orbis::knew(); newDevice->open = zero_device_open; return newDevice; } diff --git a/rpcsx-os/linker.cpp b/rpcsx-os/linker.cpp index 3cc17984d..2c864edf7 100644 --- a/rpcsx-os/linker.cpp +++ b/rpcsx-os/linker.cpp @@ -1,6 +1,7 @@ #include "linker.hpp" #include "align.hpp" #include "io-device.hpp" +#include "orbis/KernelAllocator.hpp" #include "orbis/module/Module.hpp" #include "vfs.hpp" #include "vm.hpp" @@ -280,7 +281,7 @@ void rx::linker::override(std::string originalModuleName, Ref rx::linker::loadModule(std::span image, orbis::Process *process) { - Ref result{new orbis::Module{}}; + Ref result{orbis::knew()}; Elf64_Ehdr header; std::memcpy(&header, image.data(), sizeof(Elf64_Ehdr)); @@ -842,7 +843,7 @@ Ref rx::linker::loadModuleFile(const char *path, } static Ref createSceFreeTypeFull(orbis::Process *process) { - auto result = orbis::create(); + auto result = orbis::knew(); std::strncpy(result->soName, "libSceFreeTypeFull.prx", sizeof(result->soName) - 1); diff --git a/rpcsx-os/main.cpp b/rpcsx-os/main.cpp index ccd13a937..9320653f7 100644 --- a/rpcsx-os/main.cpp +++ b/rpcsx-os/main.cpp @@ -521,15 +521,6 @@ static int ps4Exec(orbis::Process *mainProcess, return 0; } -struct KernelEventLogger : public orbis::KernelContext::EventListener { - void onProcessCreated(orbis::Process *process) override { - std::printf("process %u was created\n", (unsigned)process->pid); - } - void onProcessDeleted(orbis::pid_t pid) override { - std::printf("process %u was deleted\n", (unsigned)pid); - } -}; - static void usage(const char *argv0) { std::printf("%s [...] [args...]\n", argv0); std::printf(" options:\n"); @@ -653,9 +644,6 @@ int main(int argc, const char *argv[]) { setupSigHandlers(); // rx::vm::printHostStats(); - KernelEventLogger eventLogger; - orbis::KernelContext context; - context.addEventListener(&eventLogger); rx::vfs::initialize(); @@ -712,7 +700,7 @@ int main(int argc, const char *argv[]) { runRpsxGpu(); // rx::vm::printHostStats(); - auto initProcess = context.createProcess(10); + auto initProcess = orbis::g_context.createProcess(10); initProcess->sysent = &orbis::ps4_sysvec; initProcess->onSysEnter = onSysEnter; initProcess->onSysExit = onSysExit;