Skip to content

Commit

Permalink
Merge pull request SysRay#50 from SysRay/features
Browse files Browse the repository at this point in the history
Features -> main
  • Loading branch information
SysRay authored Mar 30, 2024
2 parents c8bb397 + bf722f1 commit 6a0f459
Show file tree
Hide file tree
Showing 169 changed files with 29,934 additions and 1,370 deletions.
2 changes: 2 additions & 0 deletions .clang-format-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/third_party/**
**/vk_mem_alloc.h
21 changes: 14 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run clang-format
uses: jidicula/clang-format-action@v4.11.0
- uses: DoozyX/clang-format-lint-action@v0.17
with:
clang-format-version: 17
exclude-regex: (third_party|vk_mem_alloc)
source: ./
extensions: h,hpp,cpp,c,cc
clangFormatVersion: 17

build:
needs: formatting-check
Expand Down Expand Up @@ -77,13 +77,20 @@ jobs:
cache: true

- name: Configure
run: cmake.exe -S. -B_build/_Release -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=_build/_Install -DCMAKE_C_COMPILER=clang-cl.exe -DCMAKE_CXX_COMPILER=clang-cl.exe
run: |
echo "::add-matcher::tools/matchers/cmake.json"
cmake.exe -S. -B_build/_Release -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=_build/_Install -DCMAKE_C_COMPILER=clang-cl.exe -DCMAKE_CXX_COMPILER=clang-cl.exe
- name: Build
run: cmake --build _build/_Release -j4
run: |
echo "::add-matcher::tools/matchers/cmake.json"
echo "::add-matcher::tools/matchers/compilers.json"
cmake --build _build/_Release -j4
- name: Install
run: cmake.exe --install _build/_Release
run: |
echo "::add-matcher::tools/matchers/cmake.json"
cmake.exe --install _build/_Release
- name: Upload artifacts
uses: actions/upload-artifact@v4
Expand Down
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"ms-vscode.cpptools",
"ms-vscode.cpptools-extension-pack",
"ms-vscode.cmake-tools",
"jeff-hykin.better-cpp-syntax"
]
}
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "cppvsdbg",
"request": "launch",
"program": "${config:psoff.root-path}/emulator.exe",
"args": ["--file", "${config:psoff.game-path}/eboot.bin"],
"args": ["--file", "${config:psoff.game-path}/eboot.bin", "--d"],
"stopAtEntry": false,
"cwd": "${config:psoff.root-path}",
"environment": [],
Expand Down
24 changes: 15 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@
],
"ccls.index.threads": 0,

"cmake.buildDirectory": "${workspaceFolder}/_build",
"cmake.installPrefix": "_Install",
"cmake.sourceDirectory": "${workspaceFolder}",
"C_Cpp.default.intelliSenseMode": "windows-clang-x64",
"C_Cpp.default.cppStandard": "c++20",
"C_Cpp.autoAddFileAssociations": false,
"cmake.generator": "Ninja",
"cmake.generator": "Ninja",
"cmake.sourceDirectory": "${workspaceFolder}",
"cmake.buildDirectory": "${workspaceFolder}/_build/_Release",
"cmake.configureSettings": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_INSTALL_PREFIX": "${workspaceFolder}/_build/_Install",
"CMAKE_C_COMPILER": "clang-cl.exe",
"CMAKE_CXX_COMPILER": "clang-cl.exe"
},
"C_Cpp.default.compilerPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\Llvm\\bin\\clang-cl.exe",
"C_Cpp.default.intelliSenseMode": "windows-clang-x64",
"C_Cpp.default.cppStandard": "c++20",
"C_Cpp.autoAddFileAssociations": false,
"editor.tabSize": 2,
"editor.insertSpaces": true,
"git.ignoredRepositories": [
"third_party/boost",
"third_party/optick",
"third_party/magic_enum",
"third_part/SDL2"
]
"third_part/SDL2"
]
}
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/lib) # Move all libs here post-build, ex
#
include_directories(BEFORE
${CMAKE_INSTALL_PREFIX}/development/include
${PRJ_SRC_DIR}/third_party/SDL2/include
${PRJ_SRC_DIR}/modules_include
${PRJ_SRC_DIR}/tools/logging
${PRJ_SRC_DIR}/tools/config_emu
${PRJ_SRC_DIR}
${CMAKE_BINARY_DIR}/third_party/install/include
)
Expand All @@ -52,6 +52,7 @@ link_directories(BEFORE
)

add_subdirectory(tools/logging) # include before link_libraries
add_subdirectory(tools/config_emu) # include before link_libraries
add_subdirectory(tools/dll2Nids)
add_dependencies(dll2Nids third_party)

Expand Down
2 changes: 2 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_subdirectory(fileManager)
add_subdirectory(imports)
add_subdirectory(memory)
add_subdirectory(dmem)
add_subdirectory(unwinding)

# Build
add_library(core SHARED
Expand All @@ -25,6 +26,7 @@ add_library(core SHARED
$<TARGET_OBJECTS:imports>
$<TARGET_OBJECTS:memory>
$<TARGET_OBJECTS:dmem>
$<TARGET_OBJECTS:unwinding>
)

add_dependencies(core logging boost)
Expand Down
2 changes: 1 addition & 1 deletion core/dmem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ add_library(dmem OBJECT
dmem.cpp
)

add_dependencies(dmem third_party psOff_utility)
add_dependencies(dmem third_party boost psOff_utility)

target_include_directories(dmem PRIVATE
${PRJ_SRC_DIR}/third_party/magic_enum/include
Expand Down
101 changes: 87 additions & 14 deletions core/dmem/dmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,61 @@
#include "dmem.h"
#undef __APICALL_EXTERN

#include "core/imports/imports_gpuMemory.h"
#include "core/memory/memory.h"
#include "core/videoout/videoout.h"
#include "logging.h"
#include "utility/utility.h"

#include <algorithm>
#include <boost/thread.hpp>
#include <magic_enum/magic_enum.hpp>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>

LOG_DEFINE_MODULE(MemoryManager);

namespace {

static uint64_t getAligned(uint64_t pos, size_t align) {
return (align != 0 ? (pos + (align - 1)) & ~(align - 1) : pos);
struct MemoryInfo {
bool isGpu = false;
size_t size = 0;
};

struct pImpl {
boost::mutex mutex_;

std::unordered_map<uint64_t, MappingType> mappings_;
};

auto getData() {
static pImpl obj;
return &obj;
}
} // namespace

// Allocates the sections (memory pool is done internally (in app))
class PhysicalMemory: public IPysicalMemory {
class PhysicalMemory: public IPhysicalMemory {
std::mutex m_mutex_int;

std::unordered_map<uint64_t, MemoryInfo> m_objects;

public:
PhysicalMemory() = default;

virtual ~PhysicalMemory() { deinit(); }

uint64_t alloc(uint64_t vaddr, size_t len, int memoryTye) final;
bool reserve(uint64_t start, size_t len, size_t alignment, uint64_t* outAddr, int memoryType) final;
uintptr_t commit(uint64_t base, uint64_t offset, size_t len, size_t alignment, int prot) final;
bool Map(uint64_t vaddr, uint64_t physAddr, size_t len, int prot, bool allocFixed, size_t alignment, uint64_t* outAddr) final;
bool Release(uint64_t start, size_t len, uint64_t* vaddr, uint64_t* size) final;
bool Unmap(uint64_t vaddr, uint64_t size) final;
void deinit() final;
};

IPysicalMemory& accessPysicalMemory() {
IPhysicalMemory& accessPysicalMemory() {
static PhysicalMemory inst;
return inst;
}
Expand Down Expand Up @@ -66,16 +86,20 @@ bool PhysicalMemory::reserve(uint64_t start, size_t len, size_t alignment, uint6
uintptr_t PhysicalMemory::commit(uint64_t base, uint64_t vaddr, size_t len, size_t alignment, int prot) {
LOG_USE_MODULE(MemoryManager);

uintptr_t addr = 0;
auto const isGpu = (prot & 0xF0) > 0;
if (isGpu & ((prot & 0xF) > 0)) {
uintptr_t addr = 0;

auto [protCPU, protGPU] = util::getMemoryProtection(prot);

if (protGPU != 0) {
addr = memory::allocGPUMemory(base, 0, len, alignment);
} else {
addr = memory::commit(base, 0, len, alignment, prot);
}

if (isGpu) {
if (!gpuMemory::notify_allocHeap(addr, len, prot)) {
m_objects[addr] = MemoryInfo {.isGpu = protGPU != 0, .size = len};

if (protGPU != 0) {
if (!accessVideoOut().notify_allocHeap(addr, len, prot)) {
LOG_ERR(L"Commit| Couldn't allocHeap| base:0x%08llx offset:0x%08llx size:%llu alignment:%llu prot:%d -> @%08llx", base, vaddr, len, alignment, prot,
addr);
return 0;
Expand Down Expand Up @@ -103,8 +127,10 @@ bool PhysicalMemory::Map(uint64_t vaddr, uint64_t physAddr, size_t len, int prot
*outAddr = memory::allocAligned(physAddr, len, prot, alignment);
}

m_objects[*outAddr] = MemoryInfo {.isGpu = protGPU != 0, .size = len};

if (protGPU != 0) {
if (!gpuMemory::notify_allocHeap(*outAddr, len, prot)) {
if (!accessVideoOut().notify_allocHeap(*outAddr, len, prot)) {
LOG_ERR(L"Map| Couldn't allocHeap vaddr:0x%08llx physAddr:0x%08llx len:0x%08llx prot:0x%x -> out:0x%08llx", vaddr, physAddr, len, prot, *outAddr);
return false;
}
Expand All @@ -130,23 +156,46 @@ bool PhysicalMemory::Release(uint64_t start, size_t len, uint64_t* vaddr, uint64
bool PhysicalMemory::Unmap(uint64_t vaddr, uint64_t size) {
LOG_USE_MODULE(MemoryManager);

memory::free(vaddr);
if (auto it = m_objects.find(vaddr); it != m_objects.end()) {
if (it->second.isGpu) {
// if(isGPU) accessGpuMemory().freeHeap(vaddr); // todo, notify free (should free host memory aswell)
memory::free(vaddr);
} else {
memory::free(vaddr);
}
} else {
LOG_ERR(L"Unmap not in map: vaddr:0x%08llx len:%lld", vaddr, size);
memory::free(vaddr);
}

{
std::unique_lock const lock(m_mutex_int);
m_availableSize += size;
}
// if(isGPU) accessGpuMemory().freeHeap(vaddr); // todo

LOG_INFO(L"Unmap: vaddr:0x%08llx len:%lld", vaddr, size);
return true;
}

void PhysicalMemory::deinit() {
for (auto& item: m_objects) {
if (item.second.isGpu) {
// done by gpuMemoryManager
} else {
memory::free(item.first);
}
}

m_objects.clear();
}

class FlexibleMemory: public IFlexibleMemory {
uint64_t m_totalAllocated = 0;
std::mutex m_mutex_int;

public:
FlexibleMemory() = default;
FlexibleMemory() = default;
virtual ~FlexibleMemory() = default;

uint64_t alloc(uint64_t vaddr, size_t len, int prot) final;
bool destroy(uint64_t vaddr, uint64_t size) final;
Expand All @@ -171,6 +220,8 @@ uint64_t FlexibleMemory::alloc(uint64_t vaddr, size_t len, int prot) {
m_totalAllocated += len;

auto const outAddr = memory::alloc(vaddr, len, prot);

registerMapping(outAddr, MappingType::Flexible);
LOG_INFO(L"--> Heap| vaddr:0x%08llx len:%llu prot:0x%x total:0x%08llx -> @0x%08llx", vaddr, len, prot, m_totalAllocated, outAddr);
return outAddr;
}
Expand All @@ -190,4 +241,26 @@ bool FlexibleMemory::destroy(uint64_t vaddr, uint64_t size) {
void FlexibleMemory::release(uint64_t start, size_t len) {
LOG_USE_MODULE(MemoryManager);
LOG_ERR(L"todo %S", __FUNCTION__);
}

void registerMapping(uint64_t vaddr, MappingType type) {
auto impl = getData();

boost::unique_lock lock(impl->mutex_);

impl->mappings_.emplace(std::make_pair(vaddr, type));
}

MappingType unregisterMapping(uint64_t vaddr) {
auto impl = getData();

boost::unique_lock lock(impl->mutex_);

if (auto it = impl->mappings_.find((uint64_t)vaddr); it != impl->mappings_.end()) {
auto type = it->second;
impl->mappings_.erase(it);
return it->second;
}

return MappingType::None;
}
32 changes: 28 additions & 4 deletions core/dmem/dmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@

enum class GpuMemoryMode { NoAccess, Read, Write, ReadWrite };

class IPysicalMemory {
CLASS_NO_COPY(IPysicalMemory);
enum class MappingType { None, File, Flexible, Fixed };

class IPhysicalMemory {
CLASS_NO_COPY(IPhysicalMemory);

protected:
IPysicalMemory() = default;
IPhysicalMemory() = default;
uint64_t m_availableSize = 5000000000llu; // todo get from system memory
size_t m_allocSize = 0;

public:
virtual ~IPhysicalMemory() = default;

void getAvailableSize(uint32_t start, uint32_t end, size_t alignment, uint32_t* startOut, size_t* sizeOut) {
*startOut = size() - m_availableSize;
*sizeOut = m_availableSize;
Expand All @@ -26,6 +30,7 @@ class IPysicalMemory {
virtual bool Map(uint64_t vaddr, uint64_t physAddr, size_t len, int prot, bool allocFixed, size_t alignment, uint64_t* outAddr) = 0;
virtual bool Release(uint64_t start, size_t len, uint64_t* vaddr, uint64_t* size) = 0;
virtual bool Unmap(uint64_t vaddr, uint64_t size) = 0;
virtual void deinit() = 0;

uint64_t const size() const { return m_allocSize; } // use system ram
};
Expand All @@ -39,6 +44,8 @@ class IFlexibleMemory {
IFlexibleMemory() = default;

public:
virtual ~IFlexibleMemory() = default;

void setConfiguredSize(uint64_t size) { m_configuresSize = size; }

uint64_t size() const { return m_configuresSize; }
Expand All @@ -58,6 +65,23 @@ class IFlexibleMemory {
#define __APICALL
#endif

__APICALL IPysicalMemory& accessPysicalMemory();
/**
* @brief registers mapping (for mmap, mumap)
*
* @param vaddr
* @param type != None
* @return __APICALL
*/
__APICALL void registerMapping(uint64_t vaddr, MappingType type);

/**
* @brief Unregisters mapping and returns the type of the mappin
*
* @param vaddr
* @return None: Mapping didn't exist
*/
__APICALL MappingType unregisterMapping(uint64_t vaddr);

__APICALL IPhysicalMemory& accessPysicalMemory();
__APICALL IFlexibleMemory& accessFlexibleMemory();
#undef __APICALL
Loading

0 comments on commit 6a0f459

Please sign in to comment.