[orbis-kernel] Implement sys_mname

This commit is contained in:
Ivan Chikish 2023-07-16 18:31:12 +03:00
parent 71444c236a
commit a1a91bb557
4 changed files with 58 additions and 6 deletions

View File

@ -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,

View File

@ -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;
@ -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

View File

@ -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>
@ -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

View File

@ -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<NamedMemoryRange>(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 */) {