Skip to content

Commit

Permalink
Merge pull request #169 from SysRay/work
Browse files Browse the repository at this point in the history
fixes
  • Loading branch information
SysRay authored May 27, 2024
2 parents 110f4a3 + 5e09ef1 commit dba7044
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.24)
include(ExternalProject)

set(PSOFF_LIB_VERSION v.0.3)
set(PSOFF_RENDER_VERSION v.0.5-nightly_04.05.24)
set(PSOFF_RENDER_VERSION v.0.5-nightly_27.05.24)

set(ProjectName psOff_${CMAKE_BUILD_TYPE})
project(${ProjectName} VERSION 0.0.1)
Expand Down
52 changes: 31 additions & 21 deletions core/runtime/runtimeLinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ class InternalLib: public Symbols::IResolve {

class RuntimeLinker: public IRuntimeLinker {
private:
mutable std::mutex m_mutex_int;
mutable std::recursive_mutex m_mutex_int;

uint64_t m_invalidMemoryAddr = 0;
uint64_t m_invalidMemoryAddr = 0;
size_t m_countcreatePrograms = 0;

EntryParams m_entryParams = {
.argc = 1,
Expand All @@ -203,7 +204,7 @@ class RuntimeLinker: public IRuntimeLinker {
std::unordered_map<std::string_view, Symbols::SymbolIntercept> m_interceptMap;
mutable std::unordered_map<uintptr_t, uintptr_t> m_interceptOrigVaddrMap;

std::unordered_map<uint32_t, uint64_t> mTlsIndexMap;
std::unordered_map<uint32_t, Program*> mTlsIndexMap;
std::unordered_map<std::string_view, void*> m_libHandles;

std::vector<uint8_t> m_tlsStaticInitBlock;
Expand Down Expand Up @@ -241,10 +242,9 @@ class RuntimeLinker: public IRuntimeLinker {
std::unique_ptr<Program> createProgram(std::filesystem::path const filepath, uint64_t const baseSize, uint64_t const baseSizeAligned, uint64_t const alocSize,
bool useStaticTLS) final {
std::unique_lock const lock(m_mutex_int);

if (useStaticTLS) ++m_countcreatePrograms;
auto inst = std::make_unique<Program>(filepath, baseSize, baseSizeAligned, IMAGE_BASE, alocSize, useStaticTLS);
inst->id = accessFileManager().addFile(createType_lib(inst), filepath);

return inst;
}

Expand Down Expand Up @@ -530,6 +530,16 @@ void RuntimeLinker::loadModules(std::string_view libName) {

int RuntimeLinker::loadStartModule(std::filesystem::path const& path, size_t argc, const void* argp, int* pRes) {
LOG_USE_MODULE(RuntimeLinker);
std::unique_lock const lock(m_mutex_int);

// check if already loaded
auto fileName = path.filename().string();
if (auto it = std::find_if(m_programList.begin(), m_programList.end(),
[&fileName](std::pair<std::unique_ptr<Program>, std::shared_ptr<IFormat>>& rhs) { return rhs.first->filename == fileName; });
it != m_programList.end()) {
return it->first->id;
}
// -

auto ifFile = util::openFile(path);
if (!ifFile) {
Expand Down Expand Up @@ -562,7 +572,7 @@ int RuntimeLinker::loadStartModule(std::filesystem::path const& path, size_t arg

pProgram->moduleInfoEx.tls_index = tlsIndex;

mTlsIndexMap[tlsIndex] = pProgram->id;
mTlsIndexMap[tlsIndex] = pProgram;

// check imports
LOG_DEBUG(L"Load for %S", pProgram->filename.string().c_str());
Expand Down Expand Up @@ -614,24 +624,24 @@ void* RuntimeLinker::getTLSAddr(uint32_t index, uint64_t offset) {

LOG_USE_MODULE(RuntimeLinker);

auto& tlsAddr = pthread::getDTV(pthread::getSelf())[index];
if (index <= m_countcreatePrograms) {
auto& tlsAddr = pthread::getDTV(pthread::getSelf())[index];
LOG_TRACE(L"[%d] getTlsAddr key:%d addr:0x%08llx offset:0x%08llx", pthread::getThreadId(), index, tlsAddr, offset);
return (void*)((uint8_t*)tlsAddr + offset);
}

LOG_TRACE(L"[%d] getTlsAddr key:%d addr:0x%08llx offset:0x%08llx", pthread::getThreadId(), index, tlsAddr, offset);
// Lookup module and get its tls
auto const prog = mTlsIndexMap[index];
auto& tlsAddr = pthread::getDTV(pthread::getSelf())[prog->tls.index];

// Create new for dynamically loaded module
if (tlsAddr == 0) {

LOG_ERR(L"todo init tls module:%d", index);
auto const progIndex = mTlsIndexMap[index];
auto& prog = m_programList[progIndex];

uint8_t* tlsData = new uint8_t[prog.first->tls.sizeImage];
memcpy(tlsData, (void*)prog.first->tls.vaddrImage, prog.first->tls.sizeImage);

tlsAddr = (uint64_t)tlsData;
LOG_TRACE(L"[%d] create tls key:%d", pthread::getThreadId(), index);
auto obj = std::make_unique<uint8_t[]>(prog->tls.sizeImage).release();
memcpy(obj, (void*)prog->tls.vaddrImage, prog->tls.sizeImage);
tlsAddr = (uint64_t)obj;
}
// - create new

LOG_TRACE(L"[%d] getTlsAddr key:%d addr:0x%08llx offset:0x%08llx", pthread::getThreadId(), index, tlsAddr, offset);
return (void*)((uint8_t*)tlsAddr + offset);
}

Expand Down Expand Up @@ -664,7 +674,7 @@ uint32_t RuntimeLinker::createTLSKey(void* destructor) {

std::unique_lock const lock(m_mutex_int);

for (uint64_t n = 0; n < m_dtvKeys.size(); ++n) {
for (uint64_t n = m_countcreatePrograms; n < m_dtvKeys.size(); ++n) {
if (!m_dtvKeys[n].used) {
m_dtvKeys[n].used = true;
m_dtvKeys[n].destructor = (pthread_key_destructor_func_t)destructor;
Expand Down Expand Up @@ -698,7 +708,7 @@ void RuntimeLinker::destroyTLSKeys(uint8_t* obj) {

std::unique_lock const lock(m_mutex_int);

for (uint64_t n = 0; n < m_dtvKeys.size(); ++n, ++pDtvKey) {
for (uint64_t n = m_countcreatePrograms; n < m_dtvKeys.size(); ++n, ++pDtvKey) {
if (m_dtvKeys[n].destructor != nullptr) {
if (pDtvKey[n] != 0) m_dtvKeys[n].destructor((void*)pDtvKey[n]);
}
Expand Down
4 changes: 2 additions & 2 deletions modules/libSceGraphicsDriver/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ int SYSV_ABI sceGnmDrawIndexOffset(uint32_t* cmdOut, uint64_t size, uint32_t ind
LOG_TRACE(L"%S 0x%08llx %u", __FUNCTION__, (uint64_t)cmdOut, size);

cmdOut[0] = Pm4::create(size, Pm4::Custom::R_DRAW_INDEX_OFFSET);
cmdOut[1] = index_offset;
cmdOut[2] = index_count;
cmdOut[1] = index_count;
cmdOut[2] = index_offset;
cmdOut[3] = flags;
cmdOut[4] = 0;
cmdOut[5] = 0;
Expand Down

0 comments on commit dba7044

Please sign in to comment.