mirror of
https://github.com/RPCSX/rpcsx.git
synced 2026-04-05 14:37:08 +00:00
orbis: remove process list from context & initial serialization support
modernize kenv add LockableKernelObject utility
This commit is contained in:
parent
f71e3410c1
commit
014012c219
22 changed files with 372 additions and 215 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "rx/Rc.hpp"
|
||||
#include "rx/Serializer.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include "rx/TypeId.hpp"
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
|
|
@ -44,6 +45,28 @@ struct KernelObject : KernelObjectBase, StateT {
|
|||
}
|
||||
};
|
||||
|
||||
template <rx::Serializable StateT>
|
||||
struct LockableKernelObject : KernelObjectBase, StateT {
|
||||
mutable rx::shared_mutex mtx;
|
||||
|
||||
template <typename... Args>
|
||||
LockableKernelObject(Args &&...args)
|
||||
: KernelObjectBase(rx::TypeId::get<StateT>()),
|
||||
StateT(std::forward<Args>(args)...) {}
|
||||
|
||||
virtual void serialize(rx::Serializer &s) const {
|
||||
std::lock_guard lock(*this);
|
||||
s.serialize(static_cast<const StateT &>(*this));
|
||||
}
|
||||
|
||||
virtual void deserialize(rx::Deserializer &s) {
|
||||
s.deserialize(static_cast<StateT &>(*this));
|
||||
}
|
||||
|
||||
void lock() const { mtx.lock(); }
|
||||
void unlock() const { mtx.unlock(); }
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
struct StaticObjectCtl {
|
||||
std::size_t offset = -1ull;
|
||||
|
|
|
|||
|
|
@ -65,6 +65,9 @@ add_library(obj.orbis-kernel OBJECT
|
|||
src/sys/sys_vm_mmap.cpp
|
||||
src/sys/sys_vm_unix.cpp
|
||||
|
||||
src/thread/Process.cpp
|
||||
src/thread/Thread.cpp
|
||||
|
||||
src/utils/Logs.cpp
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -8,9 +8,7 @@
|
|||
#include "orbis/note.hpp"
|
||||
#include "osem.hpp"
|
||||
#include "rx/IdMap.hpp"
|
||||
#include "rx/LinkedNode.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include "thread/types.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
|
|
@ -37,18 +35,6 @@ public:
|
|||
KernelContext();
|
||||
~KernelContext();
|
||||
|
||||
Process *createProcess(pid_t pid);
|
||||
void deleteProcess(Process *proc);
|
||||
Process *findProcessById(pid_t pid) const;
|
||||
Process *findProcessByHostId(std::uint64_t pid) const;
|
||||
|
||||
rx::LinkedNode<Process> *getProcessList() { return m_processes; }
|
||||
|
||||
long allocatePid() {
|
||||
std::lock_guard lock(m_thread_id_mtx);
|
||||
return m_thread_id_map.emplace(0).first;
|
||||
}
|
||||
|
||||
long getTscFreq();
|
||||
|
||||
std::pair<EventFlag *, bool> createEventFlag(kstring name, std::int32_t flags,
|
||||
|
|
@ -123,17 +109,15 @@ public:
|
|||
return {};
|
||||
}
|
||||
|
||||
std::tuple<kmap<kstring, char[128]> &, std::unique_lock<rx::shared_mutex>>
|
||||
std::tuple<kmap<kstring, rx::StaticString<128>> &, std::unique_lock<rx::shared_mutex>>
|
||||
getKernelEnv() {
|
||||
std::unique_lock lock(m_kenv_mtx);
|
||||
return {m_kenv, std::move(lock)};
|
||||
}
|
||||
|
||||
void setKernelEnv(std::string_view key, std::string_view value) {
|
||||
auto &kenvValue = m_kenv[kstring(key)];
|
||||
auto len = std::min(sizeof(kenvValue) - 1, value.size());
|
||||
std::memcpy(kenvValue, value.data(), len);
|
||||
kenvValue[len] = '0';
|
||||
std::unique_lock lock(m_kenv_mtx);
|
||||
m_kenv[kstring(key)] = value;
|
||||
}
|
||||
|
||||
rx::Ref<EventEmitter> deviceEventEmitter;
|
||||
|
|
@ -177,11 +161,6 @@ public:
|
|||
private:
|
||||
std::atomic<long> m_tsc_freq{0};
|
||||
|
||||
rx::shared_mutex m_thread_id_mtx;
|
||||
rx::OwningIdMap<char, long, 256, 0> m_thread_id_map;
|
||||
mutable rx::shared_mutex m_proc_mtx;
|
||||
rx::LinkedNode<Process> *m_processes = nullptr;
|
||||
|
||||
rx::shared_mutex m_evf_mtx;
|
||||
kmap<kstring, rx::Ref<EventFlag>> m_event_flags;
|
||||
|
||||
|
|
@ -192,7 +171,7 @@ private:
|
|||
kmap<kstring, rx::Ref<IpmiServer>> mIpmiServers;
|
||||
|
||||
rx::shared_mutex m_kenv_mtx;
|
||||
kmap<kstring, char[128]> m_kenv; // max size: 127 + '\0'
|
||||
kmap<kstring, rx::StaticString<128>> m_kenv; // max size: 127 + '\0'
|
||||
};
|
||||
|
||||
extern GlobalObjectRef<KernelContext> g_context;
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ public:
|
|||
explicit GlobalObjectRef(std::uint32_t offset) : mOffset(offset) {}
|
||||
T *get() { return reinterpret_cast<T *>(g_globalStorage + mOffset); }
|
||||
T *operator->() { return get(); }
|
||||
T &operator*() { return *get(); }
|
||||
};
|
||||
|
||||
template <rx::Serializable StateT>
|
||||
|
|
|
|||
|
|
@ -59,6 +59,8 @@ struct Process final {
|
|||
kernel::StaticKernelObjectStorage<OrbisNamespace,
|
||||
kernel::detail::ProcessScope>;
|
||||
|
||||
~Process();
|
||||
|
||||
KernelContext *context = nullptr;
|
||||
std::byte *storage = nullptr;
|
||||
|
||||
|
|
@ -97,7 +99,7 @@ struct Process final {
|
|||
rx::RcIdMap<EventFlag, sint, 4097, 1> evfMap;
|
||||
rx::RcIdMap<Semaphore, sint, 4097, 1> semMap;
|
||||
rx::RcIdMap<Module, ModuleHandle> modulesMap;
|
||||
rx::OwningIdMap<Thread, lwpid_t> threadsMap;
|
||||
rx::RcIdMap<Thread, lwpid_t> threadsMap;
|
||||
rx::RcIdMap<orbis::File, sint> fileDescriptors;
|
||||
|
||||
// Named objects for debugging
|
||||
|
|
@ -115,18 +117,8 @@ struct Process final {
|
|||
void incRef() {}
|
||||
void decRef() {}
|
||||
|
||||
void allocate() {
|
||||
if (auto size = Storage::GetSize()) {
|
||||
storage = (std::byte *)kalloc(size, Storage::GetAlignment());
|
||||
}
|
||||
}
|
||||
|
||||
void deallocate() {
|
||||
if (auto size = Storage::GetSize()) {
|
||||
kfree(storage, size);
|
||||
storage = nullptr;
|
||||
}
|
||||
}
|
||||
void serialize(rx::Serializer &) const;
|
||||
void deserialize(rx::Deserializer &);
|
||||
|
||||
template <rx::Serializable T>
|
||||
T *get(
|
||||
|
|
@ -135,4 +127,10 @@ struct Process final {
|
|||
return ref.get(storage);
|
||||
}
|
||||
};
|
||||
|
||||
pid_t allocatePid();
|
||||
Process *createProcess(Process *parentProcess = nullptr, pid_t pid = -1);
|
||||
void deleteProcess(Process *proc);
|
||||
Process *findProcessById(pid_t pid);
|
||||
Process *findProcessByHostId(std::uint64_t pid);
|
||||
} // namespace orbis
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "cpuset.hpp"
|
||||
#include "orbis-config.hpp"
|
||||
#include "rx/Serializer.hpp"
|
||||
#include "rx/StaticString.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
#include "../KernelAllocator.hpp"
|
||||
|
|
@ -23,6 +24,8 @@ struct Thread final {
|
|||
kernel::StaticKernelObjectStorage<OrbisNamespace,
|
||||
kernel::detail::ThreadScope>;
|
||||
|
||||
~Thread();
|
||||
|
||||
rx::shared_mutex mtx;
|
||||
Process *tproc = nullptr;
|
||||
std::byte *storage = nullptr;
|
||||
|
|
@ -33,7 +36,7 @@ struct Thread final {
|
|||
ptr<void> stackEnd;
|
||||
uint64_t fsBase{};
|
||||
uint64_t gsBase{};
|
||||
char name[32]{};
|
||||
rx::StaticString<32> name;
|
||||
|
||||
cpuset affinity{~0u};
|
||||
SigSet sigMask = {0x7fff'ffff, ~0u, ~0u, ~0u};
|
||||
|
|
@ -82,19 +85,6 @@ struct Thread final {
|
|||
void notifyUnblockedSignal(int signo);
|
||||
void setSigMask(SigSet newSigMask);
|
||||
|
||||
void allocate() {
|
||||
if (auto size = Storage::GetSize()) {
|
||||
storage = (std::byte *)kalloc(size, Storage::GetAlignment());
|
||||
}
|
||||
}
|
||||
|
||||
void deallocate() {
|
||||
if (auto size = Storage::GetSize()) {
|
||||
kfree(storage, size);
|
||||
storage = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template <rx::Serializable T>
|
||||
T *get(kernel::StaticObjectRef<OrbisNamespace, kernel::detail::ThreadScope, T>
|
||||
ref) {
|
||||
|
|
@ -106,6 +96,8 @@ struct Thread final {
|
|||
void decRef() {}
|
||||
};
|
||||
|
||||
Thread *createThread(Process *process, std::string_view name);
|
||||
|
||||
extern thread_local Thread *g_currentThread;
|
||||
|
||||
struct scoped_unblock {
|
||||
|
|
|
|||
|
|
@ -24,71 +24,6 @@ KernelContext::KernelContext() {
|
|||
}
|
||||
KernelContext::~KernelContext() {}
|
||||
|
||||
Process *KernelContext::createProcess(pid_t pid) {
|
||||
auto newProcess = knew<rx::LinkedNode<Process>>();
|
||||
newProcess->object.context = this;
|
||||
newProcess->object.pid = pid;
|
||||
newProcess->object.state = ProcessState::NEW;
|
||||
|
||||
{
|
||||
std::lock_guard lock(m_proc_mtx);
|
||||
if (m_processes != nullptr) {
|
||||
m_processes->insertPrev(*newProcess);
|
||||
}
|
||||
|
||||
m_processes = newProcess;
|
||||
}
|
||||
|
||||
return &newProcess->object;
|
||||
}
|
||||
|
||||
void KernelContext::deleteProcess(Process *proc) {
|
||||
auto procNode = reinterpret_cast<rx::LinkedNode<Process> *>(proc);
|
||||
|
||||
{
|
||||
std::lock_guard lock(m_proc_mtx);
|
||||
auto next = procNode->erase();
|
||||
|
||||
if (procNode == m_processes) {
|
||||
m_processes = next;
|
||||
}
|
||||
}
|
||||
|
||||
kdelete(procNode);
|
||||
}
|
||||
|
||||
Process *KernelContext::findProcessById(pid_t pid) const {
|
||||
for (std::size_t i = 0; i < 20; ++i) {
|
||||
{
|
||||
std::lock_guard lock(m_proc_mtx);
|
||||
for (auto proc = m_processes; proc != nullptr; proc = proc->next) {
|
||||
if (proc->object.pid == pid) {
|
||||
return &proc->object;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(50));
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Process *KernelContext::findProcessByHostId(std::uint64_t pid) const {
|
||||
for (std::size_t i = 0; i < 20; ++i) {
|
||||
{
|
||||
std::lock_guard lock(m_proc_mtx);
|
||||
for (auto proc = m_processes; proc != nullptr; proc = proc->next) {
|
||||
if (proc->object.hostPid == pid) {
|
||||
return &proc->object;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(50));
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
long KernelContext::getTscFreq() {
|
||||
auto cal_tsc = []() -> long {
|
||||
const long timer_freq = 1'000'000'000;
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ static orbis::ErrorCode pipe_read(orbis::File *file, orbis::Uio *uio,
|
|||
while (true) {
|
||||
if (pipe->data.empty()) {
|
||||
// pipe->cv.wait(file->mtx);
|
||||
// ORBIS_LOG_ERROR(__FUNCTION__, "wakeup", thread->name, thread->tid,
|
||||
// file); continue;
|
||||
// ORBIS_LOG_ERROR(__FUNCTION__, "wakeup", thread->name.c_str(),
|
||||
// thread->tid, file); continue;
|
||||
return orbis::ErrorCode::WOULDBLOCK;
|
||||
}
|
||||
|
||||
|
|
@ -32,8 +32,8 @@ static orbis::ErrorCode pipe_read(orbis::File *file, orbis::Uio *uio,
|
|||
uio->offset += size;
|
||||
std::memcpy(vec.base, pipe->data.data(), size);
|
||||
|
||||
ORBIS_LOG_ERROR(__FUNCTION__, thread->name, thread->tid, file, size,
|
||||
pipe->data.size(), uio->offset, file->nextOff);
|
||||
ORBIS_LOG_ERROR(__FUNCTION__, thread->name.c_str(), thread->tid, file,
|
||||
size, pipe->data.size(), uio->offset, file->nextOff);
|
||||
|
||||
if (pipe->data.size() == size) {
|
||||
pipe->data.clear();
|
||||
|
|
@ -55,7 +55,7 @@ static orbis::ErrorCode pipe_read(orbis::File *file, orbis::Uio *uio,
|
|||
static orbis::ErrorCode pipe_write(orbis::File *file, orbis::Uio *uio,
|
||||
orbis::Thread *thread) {
|
||||
auto pipe = static_cast<orbis::Pipe *>(file)->other;
|
||||
ORBIS_LOG_ERROR(__FUNCTION__, thread->name, thread->tid, file);
|
||||
ORBIS_LOG_ERROR(__FUNCTION__, thread->name.c_str(), thread->tid, file);
|
||||
|
||||
std::size_t cnt = 0;
|
||||
for (auto vec : std::span(uio->iov, uio->iovcnt)) {
|
||||
|
|
@ -70,8 +70,8 @@ static orbis::ErrorCode pipe_write(orbis::File *file, orbis::Uio *uio,
|
|||
uio->resid -= cnt;
|
||||
uio->offset += cnt;
|
||||
|
||||
ORBIS_LOG_ERROR(__FUNCTION__, thread->name, thread->tid, file, uio->resid,
|
||||
uio->offset, file->nextOff, cnt);
|
||||
ORBIS_LOG_ERROR(__FUNCTION__, thread->name.c_str(), thread->tid, file,
|
||||
uio->resid, uio->offset, file->nextOff, cnt);
|
||||
thread->where();
|
||||
return {};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ orbis::SysResult orbis::sys_cpuset_getaffinity(Thread *thread, cpulevel_t level,
|
|||
case CpuLevel::Which:
|
||||
switch (CpuWhich(which)) {
|
||||
case CpuWhich::Tid: {
|
||||
Thread *whichThread = nullptr;
|
||||
rx::Ref<Thread> whichThread = nullptr;
|
||||
if (id == ~id_t(0) || thread->tid == id) {
|
||||
whichThread = thread;
|
||||
} else {
|
||||
|
|
@ -97,7 +97,7 @@ orbis::SysResult orbis::sys_cpuset_getaffinity(Thread *thread, cpulevel_t level,
|
|||
if (id == ~id_t(0) || id == thread->tproc->pid) {
|
||||
whichProcess = thread->tproc;
|
||||
} else {
|
||||
whichProcess = g_context->findProcessById(id);
|
||||
whichProcess = findProcessById(id);
|
||||
|
||||
if (whichProcess == nullptr) {
|
||||
return ErrorCode::SRCH;
|
||||
|
|
@ -132,7 +132,7 @@ orbis::SysResult orbis::sys_cpuset_setaffinity(Thread *thread, cpulevel_t level,
|
|||
case CpuLevel::Which:
|
||||
switch (CpuWhich(which)) {
|
||||
case CpuWhich::Tid: {
|
||||
Thread *whichThread = nullptr;
|
||||
rx::Ref<Thread> whichThread = nullptr;
|
||||
if (id == ~id_t(0) || thread->tid == id) {
|
||||
whichThread = thread;
|
||||
} else {
|
||||
|
|
@ -163,7 +163,7 @@ orbis::SysResult orbis::sys_cpuset_setaffinity(Thread *thread, cpulevel_t level,
|
|||
} else {
|
||||
ORBIS_LOG_ERROR(__FUNCTION__, "process not found", level, which, id,
|
||||
cpusetsize);
|
||||
whichProcess = g_context->findProcessById(id);
|
||||
whichProcess = findProcessById(id);
|
||||
|
||||
if (whichProcess == nullptr) {
|
||||
return ErrorCode::SRCH;
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ orbis::SysResult orbis::sys_kenv(Thread *thread, sint what,
|
|||
size_t entry = 0;
|
||||
// Entry: size of both full buffers, the '=' and the '\0' at the end
|
||||
if (value == nullptr || len == 0) {
|
||||
entry = key.size() + 1 + strnlen(env_value, 128) + 1;
|
||||
entry = key.size() + 1 + env_value.size() + 1;
|
||||
} else {
|
||||
char buf[128 * 2 + 2];
|
||||
|
||||
|
|
@ -28,9 +28,8 @@ orbis::SysResult orbis::sys_kenv(Thread *thread, sint what,
|
|||
|
||||
*_buf++ = '=';
|
||||
|
||||
const size_t value_size = strnlen(env_value, 128);
|
||||
std::strncpy(_buf, env_value, value_size);
|
||||
_buf += value_size;
|
||||
std::strncpy(_buf, env_value.data(), env_value.size());
|
||||
_buf += env_value.size();
|
||||
|
||||
*_buf++ = '\0';
|
||||
|
||||
|
|
@ -58,16 +57,16 @@ orbis::SysResult orbis::sys_kenv(Thread *thread, sint what,
|
|||
if (it == kenv.end()) {
|
||||
return ErrorCode::NOENT;
|
||||
}
|
||||
const char *buf = it->second;
|
||||
ORBIS_RET_ON_ERROR(uwriteRaw(value, buf, std::min(len, 128)));
|
||||
ORBIS_RET_ON_ERROR(uwriteRaw(value, it->second.data(), it->second.size()));
|
||||
break;
|
||||
}
|
||||
case kenv_set: {
|
||||
if (len < 1) {
|
||||
return ErrorCode::INVAL;
|
||||
}
|
||||
char *_value_buf = kenv[kstring(_name)];
|
||||
ORBIS_RET_ON_ERROR(ureadString(_value_buf, 128, value));
|
||||
auto &_value_buf = kenv[kstring(_name)];
|
||||
ORBIS_RET_ON_ERROR(
|
||||
ureadString(_value_buf.data(), _value_buf.max_size() + 1, value));
|
||||
break;
|
||||
}
|
||||
case kenv_unset: {
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@ static SysResult keventChange(KQueue *kq, KEvent &change, Thread *thread) {
|
|||
nodeIt = kq->notes.begin();
|
||||
|
||||
if (change.filter == kEvFiltProc) {
|
||||
auto process = g_context->findProcessById(change.ident);
|
||||
auto process = findProcessById(change.ident);
|
||||
if (process == nullptr) {
|
||||
return ErrorCode::SRCH;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ orbis::SysResult orbis::sys_wait4(Thread *thread, sint pid, ptr<sint> status,
|
|||
|
||||
int hostPid = pid;
|
||||
if (pid > 0) {
|
||||
auto process = g_context->findProcessById(pid);
|
||||
auto process = findProcessById(pid);
|
||||
if (process == nullptr) {
|
||||
return ErrorCode::SRCH;
|
||||
}
|
||||
|
|
@ -42,7 +42,7 @@ orbis::SysResult orbis::sys_wait4(Thread *thread, sint pid, ptr<sint> status,
|
|||
|
||||
ORBIS_LOG_ERROR(__FUNCTION__, pid, status, options, rusage, result, stat);
|
||||
|
||||
auto process = g_context->findProcessByHostId(result);
|
||||
auto process = findProcessByHostId(result);
|
||||
if (process == nullptr) {
|
||||
ORBIS_LOG_ERROR("host process not found", result);
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ orbis::SysResult orbis::sys_rtprio_thread(Thread *thread, sint function,
|
|||
ORBIS_LOG_ERROR(__FUNCTION__, function, lwpid, rtp->prio, rtp->type);
|
||||
thread->where();
|
||||
|
||||
Thread *targetThread;
|
||||
rx::Ref<Thread> targetThread;
|
||||
if (lwpid == thread->tid || lwpid == -1) {
|
||||
targetThread = thread;
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -940,7 +940,7 @@ orbis::SysResult orbis::sys_dmem_container(Thread *thread, uint id) {
|
|||
orbis::SysResult orbis::sys_get_authinfo(Thread *thread, pid_t pid,
|
||||
ptr<AuthInfo> info) {
|
||||
|
||||
auto process = pid > 0 ? g_context->findProcessById(pid) : thread->tproc;
|
||||
auto process = pid > 0 ? findProcessById(pid) : thread->tproc;
|
||||
if (process == nullptr) {
|
||||
return ErrorCode::SRCH;
|
||||
}
|
||||
|
|
@ -1188,21 +1188,20 @@ orbis::SysResult orbis::sys_randomized_path(Thread *thread, sint type,
|
|||
}
|
||||
orbis::SysResult orbis::sys_rdup(Thread *thread, sint pid, sint fd) {
|
||||
ORBIS_LOG_TODO(__FUNCTION__, pid, fd);
|
||||
for (auto it = g_context->getProcessList(); it != nullptr; it = it->next) {
|
||||
auto &p = it->object;
|
||||
if (p.pid != pid) {
|
||||
continue;
|
||||
}
|
||||
auto process = pid == -1 || pid == thread->tproc->pid ? thread->tproc
|
||||
: findProcessById(pid);
|
||||
|
||||
auto file = p.fileDescriptors.get(fd);
|
||||
if (file == nullptr) {
|
||||
return ErrorCode::BADF;
|
||||
}
|
||||
|
||||
thread->retval[0] = thread->tproc->fileDescriptors.insert(std::move(file));
|
||||
return {};
|
||||
if (!process) {
|
||||
return ErrorCode::SRCH;
|
||||
}
|
||||
return ErrorCode::SRCH;
|
||||
|
||||
auto file = process->fileDescriptors.get(fd);
|
||||
if (file == nullptr) {
|
||||
return ErrorCode::BADF;
|
||||
}
|
||||
|
||||
thread->retval[0] = thread->tproc->fileDescriptors.insert(std::move(file));
|
||||
return {};
|
||||
}
|
||||
orbis::SysResult orbis::sys_dl_get_metadata(Thread *thread /* TODO */) {
|
||||
return ErrorCode::NOSYS;
|
||||
|
|
@ -1297,7 +1296,7 @@ orbis::SysResult orbis::sys_budget_get_ptype(Thread *thread, sint pid) {
|
|||
if (pid < 0 || pid == thread->tproc->pid) {
|
||||
process = thread->tproc;
|
||||
} else {
|
||||
process = g_context->findProcessById(pid);
|
||||
process = findProcessById(pid);
|
||||
|
||||
if (!process) {
|
||||
return ErrorCode::SRCH;
|
||||
|
|
@ -1337,14 +1336,14 @@ orbis::SysResult orbis::sys_get_resident_fmem_count(Thread *thread, pid_t pid) {
|
|||
}
|
||||
orbis::SysResult orbis::sys_thr_get_name(Thread *thread, lwpid_t lwpid,
|
||||
char *buf, size_t buflen) {
|
||||
Thread *searchThread;
|
||||
rx::Ref<Thread> searchThread;
|
||||
if (thread->tid == lwpid || lwpid == -1) {
|
||||
searchThread = thread;
|
||||
} else {
|
||||
searchThread = thread->tproc->threadsMap.get(lwpid - thread->tproc->pid);
|
||||
|
||||
if (searchThread == nullptr) {
|
||||
if (auto process = g_context->findProcessById(lwpid)) {
|
||||
if (auto process = findProcessById(lwpid)) {
|
||||
searchThread = process->threadsMap.get(lwpid - process->pid);
|
||||
}
|
||||
}
|
||||
|
|
@ -1354,11 +1353,11 @@ orbis::SysResult orbis::sys_thr_get_name(Thread *thread, lwpid_t lwpid,
|
|||
}
|
||||
}
|
||||
|
||||
auto namelen = std::strlen(searchThread->name);
|
||||
auto namelen = searchThread->name.length();
|
||||
|
||||
auto writeLen = std::min(namelen + 1, buflen);
|
||||
if (writeLen > 0) {
|
||||
ORBIS_RET_ON_ERROR(uwriteRaw(buf, searchThread->name, writeLen - 1));
|
||||
ORBIS_RET_ON_ERROR(uwriteRaw(buf, searchThread->name.data(), writeLen - 1));
|
||||
buf[writeLen] = 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ orbis::SysResult orbis::sys_kill(Thread *thread, sint pid, sint signum) {
|
|||
|
||||
int hostPid = pid;
|
||||
if (pid > 0) {
|
||||
auto process = g_context->findProcessById(pid);
|
||||
auto process = findProcessById(pid);
|
||||
if (process == nullptr) {
|
||||
return ErrorCode::SRCH;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -225,7 +225,7 @@ SysResult kern_sysctl(Thread *thread, ptr<sint> name, uint namelen,
|
|||
ORBIS_LOG_ERROR("KERN_PROC_PROC 2");
|
||||
|
||||
if (namelen >= 4) {
|
||||
auto process = g_context->findProcessById(name[3]);
|
||||
auto process = findProcessById(name[3]);
|
||||
if (process == nullptr || process->exitStatus.has_value()) {
|
||||
return ErrorCode::SRCH;
|
||||
}
|
||||
|
|
@ -259,7 +259,7 @@ SysResult kern_sysctl(Thread *thread, ptr<sint> name, uint namelen,
|
|||
if (name[0] == kern && name[1] == proc && name[2] == 36) {
|
||||
Process *process = thread->tproc;
|
||||
if (process->pid != name[3]) {
|
||||
process = g_context->findProcessById(name[3]);
|
||||
process = findProcessById(name[3]);
|
||||
if (process == nullptr) {
|
||||
ORBIS_LOG_ERROR("get sdk version by pid: process not found", name[3],
|
||||
thread->tproc->pid);
|
||||
|
|
@ -290,7 +290,7 @@ SysResult kern_sysctl(Thread *thread, ptr<sint> name, uint namelen,
|
|||
// 1 - 14 - 35 - pid
|
||||
Process *process = thread->tproc;
|
||||
if (process->pid != name[3] && name[3] != -1) {
|
||||
process = g_context->findProcessById(name[3]);
|
||||
process = findProcessById(name[3]);
|
||||
if (process == nullptr) {
|
||||
ORBIS_LOG_ERROR("appinfo process not found", name[3],
|
||||
thread->tproc->pid);
|
||||
|
|
@ -461,7 +461,7 @@ SysResult kern_sysctl(Thread *thread, ptr<sint> name, uint namelen,
|
|||
if (name[0] == kern && name[1] == proc && name[2] == 68) {
|
||||
Process *process = thread->tproc;
|
||||
if (process->pid != name[3]) {
|
||||
process = g_context->findProcessById(name[3]);
|
||||
process = findProcessById(name[3]);
|
||||
if (process == nullptr) {
|
||||
ORBIS_LOG_ERROR("get ps5 sdk version by pid: process not found",
|
||||
name[3], thread->tproc->pid);
|
||||
|
|
|
|||
179
kernel/orbis/src/thread/Process.cpp
Normal file
179
kernel/orbis/src/thread/Process.cpp
Normal file
|
|
@ -0,0 +1,179 @@
|
|||
#include "thread/Process.hpp"
|
||||
#include "KernelAllocator.hpp"
|
||||
#include "KernelContext.hpp"
|
||||
#include "KernelObject.hpp"
|
||||
#include "kernel/KernelObject.hpp"
|
||||
#include "rx/LinkedNode.hpp"
|
||||
#include "rx/Serializer.hpp"
|
||||
#include "rx/align.hpp"
|
||||
#include "thread/Thread.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
struct ProcessIdList {
|
||||
rx::OwningIdMap<std::uint8_t, orbis::pid_t, 256, 0> pidMap;
|
||||
|
||||
void serialize(rx::Serializer &s) const {
|
||||
pidMap.walk([&s](std::uint8_t id, orbis::pid_t value) {
|
||||
s.serialize(id);
|
||||
s.serialize(value);
|
||||
});
|
||||
|
||||
s.serialize<std::uint8_t>(-1);
|
||||
}
|
||||
|
||||
void deserialize(rx::Deserializer &s) {
|
||||
while (true) {
|
||||
auto id = s.deserialize<std::uint8_t>();
|
||||
if (id == static_cast<std::uint8_t>(-1)) {
|
||||
break;
|
||||
}
|
||||
|
||||
auto value = s.deserialize<orbis::pid_t>();
|
||||
|
||||
if (s.failure()) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!pidMap.emplace_at(id, value)) {
|
||||
s.setFailure();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static auto g_processIdList =
|
||||
orbis::createGlobalObject<kernel::LockableKernelObject<ProcessIdList>>();
|
||||
|
||||
orbis::pid_t orbis::allocatePid() {
|
||||
std::lock_guard lock(*g_processIdList);
|
||||
return g_processIdList->pidMap.emplace(0).first * 10000 + 1;
|
||||
}
|
||||
|
||||
struct ProcessList {
|
||||
rx::LinkedNode<orbis::Process> *list = nullptr;
|
||||
|
||||
void serialize(rx::Serializer &s) const {
|
||||
for (auto proc = list; proc != nullptr; proc = proc->next) {
|
||||
s.serialize(proc->object.pid);
|
||||
s.serialize(proc->object);
|
||||
}
|
||||
|
||||
s.serialize<pid_t>(-1);
|
||||
}
|
||||
|
||||
void deserialize(rx::Deserializer &s) {
|
||||
while (true) {
|
||||
auto pid = s.deserialize<pid_t>();
|
||||
if (pid == static_cast<pid_t>(-1) || s.failure()) {
|
||||
break;
|
||||
}
|
||||
|
||||
auto process = orbis::createProcess(nullptr, pid);
|
||||
s.deserialize(*process);
|
||||
|
||||
if (s.failure()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static auto g_processList =
|
||||
orbis::createGlobalObject<kernel::LockableKernelObject<ProcessList>>();
|
||||
|
||||
void orbis::deleteProcess(orbis::Process *proc) {
|
||||
auto procNode = reinterpret_cast<rx::LinkedNode<Process> *>(proc);
|
||||
|
||||
{
|
||||
std::lock_guard lock(*g_processList);
|
||||
auto next = procNode->erase();
|
||||
|
||||
if (procNode == g_processList->list) {
|
||||
g_processList->list = next;
|
||||
}
|
||||
}
|
||||
|
||||
kdelete(procNode);
|
||||
}
|
||||
|
||||
orbis::Process *orbis::findProcessById(pid_t pid) {
|
||||
for (std::size_t i = 0; i < 20; ++i) {
|
||||
{
|
||||
std::lock_guard lock(*g_processList);
|
||||
for (auto proc = g_processList->list; proc != nullptr;
|
||||
proc = proc->next) {
|
||||
if (proc->object.pid == pid) {
|
||||
return &proc->object;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(50));
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
orbis::Process *orbis::findProcessByHostId(std::uint64_t pid) {
|
||||
for (std::size_t i = 0; i < 20; ++i) {
|
||||
{
|
||||
std::lock_guard lock(*g_processList);
|
||||
for (auto proc = g_processList->list; proc != nullptr;
|
||||
proc = proc->next) {
|
||||
if (proc->object.hostPid == pid) {
|
||||
return &proc->object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(50));
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void orbis::Process::serialize(rx::Serializer &s) const {
|
||||
Process::Storage::SerializeAll(storage, s);
|
||||
}
|
||||
void orbis::Process::deserialize(rx::Deserializer &s) {
|
||||
Process::Storage::DeserializeAll(storage, s);
|
||||
}
|
||||
|
||||
orbis::Process::~Process() {
|
||||
Process::Storage::DestructAll(storage);
|
||||
|
||||
auto size = sizeof(Process);
|
||||
size = rx::alignUp(size, Process::Storage::GetAlignment());
|
||||
size += Process::Storage::GetSize();
|
||||
|
||||
kfree(this, size);
|
||||
}
|
||||
|
||||
orbis::Process *orbis::createProcess(Process *parentProcess, pid_t pid) {
|
||||
if (pid == static_cast<pid_t>(-1)) {
|
||||
pid = allocatePid();
|
||||
}
|
||||
|
||||
auto size = sizeof(rx::LinkedNode<Process>);
|
||||
size = rx::alignUp(size, Process::Storage::GetAlignment());
|
||||
auto storageOffset = size;
|
||||
size += Process::Storage::GetSize();
|
||||
|
||||
auto memory = (std::byte *)kalloc(
|
||||
size, std::max<std::size_t>(alignof(rx::LinkedNode<Process>),
|
||||
Process::Storage::GetAlignment()));
|
||||
|
||||
auto result = new (memory) rx::LinkedNode<Process>();
|
||||
result->object.context = g_context.get();
|
||||
result->object.storage = memory + storageOffset;
|
||||
result->object.parentProcess = parentProcess;
|
||||
result->object.pid = pid;
|
||||
Process::Storage::ConstructAll(result->object.storage);
|
||||
|
||||
std::lock_guard lock(*g_processList);
|
||||
if (auto list = g_processList->list) {
|
||||
list->insertPrev(*result);
|
||||
}
|
||||
g_processList->list = result;
|
||||
return &result->object;
|
||||
}
|
||||
38
kernel/orbis/src/thread/Thread.cpp
Normal file
38
kernel/orbis/src/thread/Thread.cpp
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include "thread/Thread.hpp"
|
||||
#include "thread/Process.hpp"
|
||||
|
||||
orbis::Thread::~Thread() {
|
||||
Thread::Storage::DestructAll(storage);
|
||||
|
||||
auto size = sizeof(Thread);
|
||||
size = rx::alignUp(size, Thread::Storage::GetAlignment());
|
||||
size += Thread::Storage::GetSize();
|
||||
|
||||
kfree(this, size);
|
||||
}
|
||||
|
||||
orbis::Thread *orbis::createThread(Process *process, std::string_view name) {
|
||||
auto size = sizeof(Thread);
|
||||
size = rx::alignUp(size, Thread::Storage::GetAlignment());
|
||||
auto storageOffset = size;
|
||||
size += Thread::Storage::GetSize();
|
||||
|
||||
auto memory = (std::byte *)kalloc(
|
||||
size,
|
||||
std::max<std::size_t>(alignof(Thread), Thread::Storage::GetAlignment()));
|
||||
|
||||
auto result = new (memory) Thread();
|
||||
result->storage = memory + storageOffset;
|
||||
result->tproc = process;
|
||||
result->name = name;
|
||||
|
||||
Thread::Storage::ConstructAll(result->storage);
|
||||
|
||||
std::lock_guard lock(process->mtx);
|
||||
auto baseId = process->threadsMap.insert(result);
|
||||
result->tproc = process;
|
||||
result->tid = process->pid + baseId;
|
||||
result->state = orbis::ThreadState::RUNNING;
|
||||
|
||||
return result;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue