Merge orbis-kernel submodule

This commit is contained in:
Ivan Chikish 2023-07-03 14:10:16 +03:00
parent 91f48cdf77
commit 1ee6b7c970
97 changed files with 8134 additions and 1 deletions

View file

@ -0,0 +1,38 @@
#pragma once
#include "ProcessState.hpp"
#include "orbis-config.hpp"
#include "orbis/module/Module.hpp"
#include "orbis/utils/IdMap.hpp"
#include "orbis/utils/SharedMutex.hpp"
#include "../thread/types.hpp"
#include "../thread/Thread.hpp"
#include <mutex>
namespace orbis {
class KernelContext;
struct Thread;
struct ProcessOps;
struct sysentvec;
struct Process {
KernelContext *context = nullptr;
pid_t pid = -1;
sysentvec *sysent = nullptr;
ProcessState state = ProcessState::NEW;
Process *parentProcess = nullptr;
shared_mutex mtx;
void (*onSysEnter)(Thread *thread, int id, uint64_t *args, int argsCount) = nullptr;
void (*onSysExit)(Thread *thread, int id, uint64_t *args, int argsCount, SysResult result) = nullptr;
ptr<void> processParam = nullptr;
uint64_t processParamSize = 0;
const ProcessOps *ops = nullptr;
std::uint64_t nextTlsSlot = 1;
std::uint64_t lastTlsOffset = 0;
utils::RcIdMap<Module, ModuleHandle> modulesMap;
utils::OwningIdMap<Thread, lwpid_t> threadsMap;
utils::RcIdMap<utils::RcBase, sint> fileDescriptors;
};
} // namespace orbis

View file

@ -0,0 +1,56 @@
#pragma once
#include "../error/SysResult.hpp"
#include "../module/ModuleHandle.hpp"
#include "orbis-config.hpp"
#include "../thread/types.hpp"
namespace orbis {
struct Thread;
struct Module;
struct ProcessOps {
SysResult (*mmap)(Thread *thread, caddr_t addr, size_t len, sint prot, sint flags, sint fd, off_t pos);
SysResult (*munmap)(Thread *thread, ptr<void> addr, size_t len);
SysResult (*msync)(Thread *thread, ptr<void> addr, size_t len, sint flags);
SysResult (*mprotect)(Thread *thread, ptr<const void> addr, size_t len, sint prot);
SysResult (*minherit)(Thread *thread, ptr<void> addr, size_t len, sint inherit);
SysResult (*madvise)(Thread *thread, ptr<void> addr, size_t len, sint behav);
SysResult (*mincore)(Thread *thread, ptr<const void> addr, size_t len, ptr<char> vec);
SysResult (*mlock)(Thread *thread, ptr<const void> addr, size_t len);
SysResult (*mlockall)(Thread *thread, sint how);
SysResult (*munlockall)(Thread *thread);
SysResult (*munlock)(Thread *thread, ptr<const void> addr, size_t len);
SysResult (*virtual_query)(Thread *thread, ptr<const void> addr, sint flags, ptr<void> info, ulong infoSize);
SysResult (*open)(Thread *thread, ptr<const char> path, sint flags, sint mode);
SysResult (*close)(Thread *thread, sint fd);
SysResult (*ioctl)(Thread *thread, sint fd, ulong com, caddr_t argp);
SysResult (*write)(Thread *thread, sint fd, ptr<const void> data, ulong size);
SysResult (*read)(Thread *thread, sint fd, ptr<void> data, ulong size);
SysResult (*pread)(Thread *thread, sint fd, ptr<void> data, ulong size, ulong offset);
SysResult (*pwrite)(Thread *thread, sint fd, ptr<const void> data, ulong size, ulong offset);
SysResult (*lseek)(Thread *thread, sint fd, ulong offset, sint whence);
SysResult (*ftruncate)(Thread *thread, sint fd, off_t length);
SysResult (*truncate)(Thread *thread, ptr<const char> path, off_t length);
SysResult (*dynlib_get_obj_member)(Thread *thread, ModuleHandle handle, uint64_t index, ptr<ptr<void>> addrp);
SysResult (*dynlib_dlsym)(Thread *thread, ModuleHandle handle, ptr<const char> symbol, ptr<ptr<void>> addrp);
SysResult (*dynlib_do_copy_relocations)(Thread *thread);
SysResult (*dynlib_load_prx)(Thread *thread, ptr<const char> name, uint64_t arg1, ptr<ModuleHandle> pHandle, uint64_t arg3);
SysResult (*dynlib_unload_prx)(Thread *thread, ModuleHandle handle);
SysResult (*thr_create)(Thread *thread, ptr<struct ucontext> ctxt, ptr<slong> arg, sint flags);
SysResult (*thr_new)(Thread *thread, ptr<struct thr_param> param, sint param_size);
SysResult (*thr_exit)(Thread *thread, ptr<slong> state);
SysResult (*thr_kill)(Thread *thread, slong id, sint sig);
SysResult (*thr_kill2)(Thread *thread, pid_t pid, slong id, sint sig);
SysResult (*thr_suspend)(Thread *thread, ptr<const struct timespec> timeout);
SysResult (*thr_wake)(Thread *thread, slong id);
SysResult (*thr_set_name)(Thread *thread, slong id, ptr<const char> name);
SysResult (*exit)(Thread *thread, sint status);
SysResult (*processNeeded)(Thread *thread);
SysResult (*registerEhFrames)(Thread *thread);
};
} // namespace orbis

View file

@ -0,0 +1,11 @@
#pragma once
#include <cstdint>
namespace orbis {
enum class ProcessState : std::uint32_t {
NEW, // In creation
NORMAL, // threads can be run
ZOMBIE
};
} // namespace orbis

View file

@ -0,0 +1,23 @@
#pragma once
namespace orbis {
enum class RegisterId {
r15,
r14,
r13,
r12,
r11,
r10,
r9,
r8,
rdi,
rsi,
rbp,
rbx,
rdx,
rcx,
rax,
rsp,
rflags,
};
} // namespace orbis

View file

@ -0,0 +1,30 @@
#pragma once
#include "orbis-config.hpp"
#include "types.hpp"
#include "ThreadState.hpp"
#include <mutex>
namespace orbis {
struct Process;
struct Thread {
std::mutex lock;
Process *tproc = nullptr;
uint64_t retval[2]{};
void *context{};
ptr<void> stackStart;
ptr<void> stackEnd;
uint64_t fsBase{};
uint64_t gsBase{};
char name[32];
uint64_t sigMask[4] = {
0x7fff'ffff,
0
};
lwpid_t tid = -1;
ThreadState state = ThreadState::INACTIVE;
};
} // namespace orbis

View file

@ -0,0 +1,7 @@
#pragma once
#include <cstdint>
namespace orbis {
enum class ThreadState : std::uint32_t { INACTIVE, INHIBITED, CAN_RUN, RUNQ, RUNNING };
} // namespace orbis

View file

@ -0,0 +1,12 @@
#pragma once
#include "orbis-config.hpp"
namespace orbis {
static constexpr auto NCPUBITS = sizeof(slong) * 8;
static constexpr auto NCPUWORDS = 128 / NCPUBITS;
struct cpuset {
slong bits[NCPUWORDS];
};
} // namespace orbis

View file

@ -0,0 +1,18 @@
#pragma once
#include "orbis-config.hpp"
namespace orbis {
struct Thread;
using sy_call_t = SysResult(Thread *, uint64_t *);
struct sysent {
sint narg;
sy_call_t *call;
};
struct sysentvec {
sint size;
const sysent *table;
};
} // namespace orbis

View file

@ -0,0 +1,9 @@
#pragma once
#include "orbis-config.hpp"
namespace orbis {
using lwpid_t = int32_t;
using pid_t = int64_t;
using uid_t = uint32_t;
using gid_t = uint32_t;
} // namespace orbis