Skip to content
This repository has been archived by the owner on Oct 29, 2020. It is now read-only.

Commit

Permalink
hack together support for thread redzone
Browse files Browse the repository at this point in the history
  • Loading branch information
Force67 committed Dec 21, 2019
1 parent afa2f42 commit df647b1
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 131 deletions.
50 changes: 5 additions & 45 deletions .gitignore
@@ -1,14 +1,14 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

bin/
build/
code/vendor/cef
.vscode

# binaries
*.exe
*.elf

# ida
*.i64

# QT generated code
code/delta/core/qtgen/*
Expand Down Expand Up @@ -162,16 +162,6 @@ AutoTest.Net/
# Installshield output folder
[Ee]xpress/

# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html

# Click-Once directory
publish/

Expand Down Expand Up @@ -253,36 +243,6 @@ UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak

# SQL Server files
*.mdf
*.ldf
*.ndf

# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser

# Microsoft Fakes
FakesAssemblies/

# GhostDoc plugin setting file
*.GhostDoc.xml

# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/

# Visual Studio 6 build log
*.plg

# Visual Studio 6 workspace options file
*.opt

# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw

# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
Expand Down
10 changes: 5 additions & 5 deletions code/delta/core/kernel/module.cpp
Expand Up @@ -215,7 +215,7 @@ namespace krnl
codeSize += pcfg.ripZoneSize;

// reserve segment
info.base = process->vmem.mapMemory(nullptr, codeSize, true);
info.base = process->vmem.mapMemory(nullptr, codeSize, utl::pageProtection::rwx);
if (!info.base)
return false;

Expand Down Expand Up @@ -270,10 +270,10 @@ namespace krnl
auto trans_perm = [](uint32_t op)
{
switch (op) {
case (PF_R | PF_X): return utl::pageProtection::rwx;
case (PF_R | PF_W): return utl::pageProtection::write;
case (PF_R): return utl::pageProtection::read;
default: return utl::pageProtection::rwx;
case (PF_R | PF_X): return utl::pageProtection::rx;
case (PF_R | PF_W): return utl::pageProtection::w;
case (PF_R): return utl::pageProtection::r;
default: return utl::pageProtection::priv;
/*todo: invalid parameter bugcheck*/
}
};
Expand Down
2 changes: 2 additions & 0 deletions code/delta/core/kernel/module.h
Expand Up @@ -2,6 +2,8 @@

// Copyright (C) Force67 2019

#include <string>

#include <elf_types.h>
#include <sce_types.h>
#include "proc.h"
Expand Down
13 changes: 6 additions & 7 deletions code/delta/core/kernel/proc.cpp
Expand Up @@ -53,7 +53,8 @@ namespace krnl

static proc* g_activeProc{ nullptr };

proc::proc()
proc::proc() :
vmem(env)
{
g_activeProc = this;
}
Expand All @@ -64,15 +65,13 @@ namespace krnl

bool proc::create(const std::string& path)
{
// register HLE prx modules
/*register HLE prx overrides*/
runtime::vprx_init();

// create a user stack
env.userStack = static_cast<uint8_t*>(
utl::allocMem(nullptr, env.userStackSize,
utl::pageProtection::noaccess,
utl::allocationType::reserve));
/*initialize memory manager*/
LOG_ASSERT(vmem.init());

/*register first main module*/
auto first = modules.emplace_back(std::make_shared<elfModule>(this));
first->getInfo().handle = 0;

Expand Down
47 changes: 34 additions & 13 deletions code/delta/core/kernel/vm_manager.cpp
Expand Up @@ -2,26 +2,47 @@
// Copyright (C) Force67 2019

#include <utl/mem.h>

#include "proc.h"
#include "vm_manager.h"

namespace krnl
{
uint8_t* vmManager::mapMemory(uint8_t* preference, size_t size, bool code)
vmManager::vmManager(procInfo& info) :
pinfo(info)
{}

vmManager::~vmManager()
{
auto prot = code ? utl::pageProtection::rwx : utl::pageProtection::write;
if (pinfo.userStack)
utl::freeMem(pinfo.userStack);

void* ptr = utl::allocMem(static_cast<void*>(preference), size, prot,
utl::allocationType::reservecommit);
if (ptr) {
if (code) {
codeMemTotal += size;
codePages.emplace_back(static_cast<uint8_t*>(ptr), size);
}
else {
rtMemTotal += size;
rtPages.emplace_back(static_cast<uint8_t*>(ptr), size);
}
pinfo.userStack = nullptr;
}

bool vmManager::init()
{
/*reserve address space for the user stack*/
pinfo.userStack = static_cast<uint8_t*>(
utl::allocMem(nullptr, pinfo.userStackSize,
utl::pageProtection::priv,
utl::allocationType::reserve));

return pinfo.userStack;
}

uint8_t* vmManager::mapMemory(uint8_t* preference, size_t size, utl::pageProtection prot)
{
auto allocType = utl::allocationType::reservecommit;

/*this is a bit hacky right now*/
if ((uintptr_t)preference <= (uintptr_t)(pinfo.userStack + pinfo.userStackSize) &&
(uintptr_t)preference >= (uintptr_t)pinfo.userStack) {
allocType = utl::allocationType::commit;
}

void* ptr = utl::allocMem(static_cast<void*>(preference), size, prot, allocType);
if (ptr) {
return static_cast<uint8_t*>(ptr);
}

Expand Down
13 changes: 12 additions & 1 deletion code/delta/core/kernel/vm_manager.h
Expand Up @@ -5,8 +5,12 @@
#include <cstdint>
#include <vector>

#include <utl/mem.h>

namespace krnl
{
struct procInfo;

struct pageInfo
{
uint8_t* ptr;
Expand All @@ -20,10 +24,17 @@ namespace krnl
class vmManager
{
public:
uint8_t* mapMemory(uint8_t* preference, size_t size, bool code = false);
vmManager(procInfo&);
~vmManager();

bool init();

uint8_t* mapMemory(uint8_t* preference, size_t size, utl::pageProtection);
void unmapRtMemory(uint8_t*);

private:
procInfo& pinfo;

size_t codeMemTotal{ 0 };
size_t rtMemTotal{ 0 };

Expand Down
1 change: 1 addition & 0 deletions code/delta/core/runtime/lv2/lv2.cpp
Expand Up @@ -61,6 +61,7 @@ namespace runtime
{340, (void*)&sys_sigprocmask},
{379, (void*)&null_handler}, //sys_mtypeprotect
{432, (void*)&sys_thr_self},
{466, (void*)&sys_rtprio_thread},
{477, (void*)&sys_mmap},
{532, (void*)&sys_regmgr_call},
{533, (void*)&null_handler}, //sys_jitshm_create
Expand Down
1 change: 1 addition & 0 deletions code/delta/core/runtime/lv2/sys_info.cpp
Expand Up @@ -36,6 +36,7 @@ namespace runtime
if (name[0] == 1 && name[1] == 33 && namelen == 2) {
auto& info = proc::getActive()->getEnv();
*static_cast<void**>(oldp) = info.userStack + info.userStackSize;
std::printf("userstack -> base %p, end %p\n", info.userStack, oldp);
return 0;
}

Expand Down
18 changes: 14 additions & 4 deletions code/delta/core/runtime/lv2/sys_mem.cpp
Expand Up @@ -2,6 +2,7 @@
// Copyright (C) Force67 2019

#include <base.h>
#include <utl/mem.h>
#include <logger/logger.h>

#include "kernel/proc.h"
Expand All @@ -19,18 +20,27 @@ namespace runtime
/*TODO: should we allocate aligned?*/

auto &vma = proc::getActive()->getVma();
uint8_t *ptr = vma.mapMemory(static_cast<uint8_t*>(addr), len);
uint8_t *ptr = vma.mapMemory(static_cast<uint8_t*>(addr), len, utl::pageProtection::w);
if (!ptr) {
__debugbreak();
return reinterpret_cast<uint8_t*>(-1);
}

std::printf("mmap: addr=%p, len=%I64x, allocated @ %p\n", addr, len, ptr);

/*ensure memory is zero'd*/
auto tprot = utl::pageProtection::r;
if (prot & mprotFlags::write)
tprot |= utl::pageProtection::w;
if (prot & mprotFlags::exec)
tprot |= utl::pageProtection::x;

if (flags & mFlags::anon)
std::memset(ptr, 0, len);

/*we apply target protection later so we can setup the memory
properly beforehand*/
utl::protectMem(static_cast<void*>(ptr), len, tprot);

std::printf("mmap: addr=%p, len=%I64x, allocated @ %p\n", addr, len, ptr);

if (flags & mFlags::stack)
return &static_cast<uint8_t*>(ptr)[len];

Expand Down
5 changes: 5 additions & 0 deletions code/delta/core/runtime/lv2/sys_thread.cpp
Expand Up @@ -17,4 +17,9 @@ namespace runtime
*arg = (void*)357;
return 135;
}

int PS4ABI sys_rtprio_thread(int, uint64_t, void*)
{
return 0;
}
}
1 change: 1 addition & 0 deletions code/delta/core/runtime/lv2/sys_thread.h
Expand Up @@ -7,4 +7,5 @@
namespace runtime
{
int PS4ABI sys_thr_self(void**);
int PS4ABI sys_rtprio_thread(int, uint64_t, void*);
}
35 changes: 5 additions & 30 deletions code/shared/utl/logger/logger.cpp
Expand Up @@ -20,29 +20,6 @@ namespace utl
return (begin == end) == (*other == '\0');
}

const char* TrimSourcePath(const char* path, const char* root)
{
const char* p = path;

while (*p != '\0') {
const char* next_slash = p;
while (*next_slash != '\0' && *next_slash != '/' && *next_slash != '\\') {
++next_slash;
}

bool is_src = ComparePartialString(p, next_slash, root);
p = next_slash;

if (*p != '\0') {
++p;
}
if (is_src) {
path = p;
}
}
return path;
}

class LogRegistry
{
std::mutex writing_lock;
Expand Down Expand Up @@ -104,7 +81,7 @@ namespace utl
backend_thread.join();
}

void AddEntry(logLevel lvl, const char* filename, uint32_t line, const char* func, std::string msg)
void AddEntry(logLevel lvl, uint32_t line, const char* func, std::string msg)
{

using std::chrono::duration_cast;
Expand All @@ -116,7 +93,6 @@ namespace utl
entry.line_num = line;
entry.function = func;
entry.message = std::move(msg);
entry.filename = TrimSourcePath(filename, "code");

pending.Push(entry);
}
Expand Down Expand Up @@ -174,19 +150,18 @@ namespace utl

const char* level_name = GetLevelName(entry.log_level);

return fmt::format("[{:4d}.{:06d}] <{}> {}:{}:{}: {}", time_seconds, time_fractional,
level_name, entry.filename, entry.function, entry.line_num,
entry.message);
return fmt::format("[{:4d}.{:06d}] <{}> {}:{}: {}", time_seconds, time_fractional,
level_name, entry.function, entry.line_num, entry.message);
}

logBase *addLogSink(std::unique_ptr<logBase> sink) {
return LogRegistry::Instance().AddSink(std::move(sink));
}

void formatLogMsg(logLevel lvl, const char* filename, uint32_t line, const char* func, const char* fmt, const fmt::format_args& args)
void formatLogMsg(logLevel lvl, uint32_t line, const char* func, const char* fmt, const fmt::format_args& args)
{
auto& reg = LogRegistry::Instance();
reg.AddEntry(lvl, filename, line, func, fmt::vformat(fmt, args));
reg.AddEntry(lvl, line, func, fmt::vformat(fmt, args));
}

logBase* getLogSink(std::string_view name)
Expand Down

0 comments on commit df647b1

Please sign in to comment.