Skip to content

Commit

Permalink
[orbis-kernel] Implement sys_mname
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekotekina committed Jul 16, 2023
1 parent 71444c2 commit 94797df
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
4 changes: 2 additions & 2 deletions orbis-kernel/include/orbis/sys/sysproto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> info);
SysResult sys_mname(Thread *thread, ptr<void> address, uint64_t length,
ptr<const char> name);
SysResult sys_mname(Thread *thread, uint64_t address, uint64_t length,
ptr<const char[32]> name);
SysResult sys_dynlib_dlopen(Thread *thread /* TODO */);
SysResult sys_dynlib_dlclose(Thread *thread /* TODO */);
SysResult sys_dynlib_dlsym(Thread *thread, SceKernelModule handle,
Expand Down
20 changes: 20 additions & 0 deletions orbis-kernel/include/orbis/thread/Process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ struct NamedObjInfo {
uint16_t ty;
};

struct NamedMemoryRange {
uint64_t begin, end;

constexpr bool operator<(const NamedMemoryRange &rhs) const {
return end <= rhs.begin;
}

friend constexpr bool operator<(const NamedMemoryRange &lhs, uint64_t ptr) {
return lhs.end <= ptr;
}

friend constexpr bool operator<(uint64_t ptr, const NamedMemoryRange &rhs) {
return ptr < rhs.begin;
}
};

struct Process final {
KernelContext *context = nullptr;
pid_t pid = -1;
Expand Down Expand Up @@ -49,5 +65,9 @@ struct Process final {
utils::shared_mutex namedObjMutex;
utils::kmap<void *, utils::kstring> namedObjNames;
utils::OwningIdMap<NamedObjInfo, uint, 65535, 1> namedObjIds;

// Named memory ranges for debugging
utils::shared_mutex namedMemMutex;
utils::kmap<NamedMemoryRange, utils::kstring> namedMem;
};
} // namespace orbis
8 changes: 8 additions & 0 deletions orbis-kernel/src/KernelContext.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "orbis/KernelContext.hpp"
#include "orbis/thread/Process.hpp"
#include "orbis/utils/Logs.hpp"
#include <sys/mman.h>
#include <sys/unistd.h>

Expand Down Expand Up @@ -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<kstring>::format(std::string &out, const void *arg) {
out += get_object(arg);
}
} // namespace logs
} // namespace orbis
32 changes: 28 additions & 4 deletions orbis-kernel/src/sys/sys_sce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,34 @@ orbis::SysResult orbis::sys_get_authinfo(Thread *thread, pid_t pid,

return {};
}
orbis::SysResult orbis::sys_mname(Thread *thread, ptr<void> address,
uint64_t length, ptr<const char> 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<const char[32]> 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();
if (!thread->tproc->namedMem.count(addr))
std::abort();
return {};
}
orbis::SysResult orbis::sys_dynlib_dlopen(Thread *thread /* TODO */) {
Expand Down

0 comments on commit 94797df

Please sign in to comment.