diff --git a/orbis-kernel/include/orbis/sys/sysproto.hpp b/orbis-kernel/include/orbis/sys/sysproto.hpp index 513367f3..9e9bdd99 100644 --- a/orbis-kernel/include/orbis/sys/sysproto.hpp +++ b/orbis-kernel/include/orbis/sys/sysproto.hpp @@ -676,8 +676,8 @@ SysResult sys_obs_eport_close(Thread *thread /* TODO */); SysResult sys_is_in_sandbox(Thread *thread /* TODO */); SysResult sys_dmem_container(Thread *thread); SysResult sys_get_authinfo(Thread *thread, pid_t pid, ptr info); -SysResult sys_mname(Thread *thread, ptr address, uint64_t length, - ptr name); +SysResult sys_mname(Thread *thread, uint64_t address, uint64_t length, + ptr name); SysResult sys_dynlib_dlopen(Thread *thread /* TODO */); SysResult sys_dynlib_dlclose(Thread *thread /* TODO */); SysResult sys_dynlib_dlsym(Thread *thread, SceKernelModule handle, diff --git a/orbis-kernel/include/orbis/thread/Process.hpp b/orbis-kernel/include/orbis/thread/Process.hpp index e3947b36..89856de0 100644 --- a/orbis-kernel/include/orbis/thread/Process.hpp +++ b/orbis-kernel/include/orbis/thread/Process.hpp @@ -22,6 +22,18 @@ struct NamedObjInfo { uint16_t ty; }; +struct NamedMemoryRange { + uint64_t begin, end; + + constexpr bool operator<(const NamedMemoryRange &rhs) const { + return end <= rhs.begin; + } + + constexpr bool operator<(const void *ptr) const { + return end <= reinterpret_cast(ptr); + } +}; + struct Process final { KernelContext *context = nullptr; pid_t pid = -1; @@ -49,5 +61,9 @@ struct Process final { utils::shared_mutex namedObjMutex; utils::kmap namedObjNames; utils::OwningIdMap namedObjIds; + + // Named memory ranges for debugging + utils::shared_mutex namedMemMutex; + utils::kmap namedMem; }; } // namespace orbis diff --git a/orbis-kernel/src/KernelContext.cpp b/orbis-kernel/src/KernelContext.cpp index 1ffbda47..c88814ad 100644 --- a/orbis-kernel/src/KernelContext.cpp +++ b/orbis-kernel/src/KernelContext.cpp @@ -1,5 +1,6 @@ #include "orbis/KernelContext.hpp" #include "orbis/thread/Process.hpp" +#include "orbis/utils/Logs.hpp" #include #include @@ -182,4 +183,11 @@ void *kalloc(std::size_t size, std::size_t align) { return g_context.kalloc(size, align); } } // namespace utils + +inline namespace logs { +template <> +void log_class_string::format(std::string &out, const void *arg) { + out += get_object(arg); +} +} // namespace logs } // namespace orbis diff --git a/orbis-kernel/src/sys/sys_sce.cpp b/orbis-kernel/src/sys/sys_sce.cpp index b1be4c45..e8880609 100644 --- a/orbis-kernel/src/sys/sys_sce.cpp +++ b/orbis-kernel/src/sys/sys_sce.cpp @@ -460,10 +460,32 @@ orbis::SysResult orbis::sys_get_authinfo(Thread *thread, pid_t pid, return {}; } -orbis::SysResult orbis::sys_mname(Thread *thread, ptr address, - uint64_t length, ptr name) { - std::printf("sys_mname(%p, %p, '%s')\n", address, (char *)address + length, - name); +orbis::SysResult orbis::sys_mname(Thread *thread, uint64_t addr, uint64_t len, + ptr name) { + ORBIS_LOG_NOTICE(__FUNCTION__, addr, len, name); + if (addr < 0x40000 || addr >= 0x100'0000'0000 || 0x100'0000'0000 - addr < len) + return ErrorCode::INVAL; + char _name[32]; + if (auto result = ureadString(_name, sizeof(_name), (const char *)name); + result != ErrorCode{}) { + return result; + } + + NamedMemoryRange range; + range.begin = addr & ~0x3fffull; + range.end = range.begin; + range.end += ((addr & 0x3fff) + 0x3fff + len) & ~0x3fffull; + + std::lock_guard lock(thread->tproc->namedMemMutex); + auto [it, end] = thread->tproc->namedMem.equal_range(range); + while (it != end) { + auto [addr2, end2] = it->first; + auto len2 = end2 - addr2; + ORBIS_LOG_NOTICE("sys_mname: removed overlapped", it->second, addr2, len2); + it = thread->tproc->namedMem.erase(it); + } + if (!thread->tproc->namedMem.try_emplace(range, _name).second) + std::abort(); return {}; } orbis::SysResult orbis::sys_dynlib_dlopen(Thread *thread /* TODO */) {