Skip to content

Commit

Permalink
[orbis-kernel] initial kqueue implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DHrpcs3 committed Oct 30, 2023
1 parent 563c5ea commit 95f1a6f
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 18 deletions.
2 changes: 1 addition & 1 deletion orbis-kernel/include/orbis/sys/sysproto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ SysResult sys_socketex(Thread *thread, ptr<const char> name, sint domain,
sint type, sint protocol);
SysResult sys_socketclose(Thread *thread, sint fd);
SysResult sys_netgetiflist(Thread *thread /* TODO */);
SysResult sys_kqueueex(Thread *thread /* TODO */);
SysResult sys_kqueueex(Thread *thread, ptr<char> name, sint flags);
SysResult sys_mtypeprotect(Thread *thread /* TODO */);
SysResult sys_regmgr_call(Thread *thread, uint32_t op, uint32_t id,
ptr<void> result, ptr<void> value, uint64_t type);
Expand Down
140 changes: 135 additions & 5 deletions orbis-kernel/src/sys/sys_event.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,156 @@
#include "KernelAllocator.hpp"
#include "sys/sysproto.hpp"

#include "thread/Process.hpp"
#include "utils/Logs.hpp"
#include "utils/SharedMutex.hpp"
#include <list>
#include <span>

struct KQueue : orbis::File {};
namespace orbis {
struct KEvent {
uintptr_t ident;
sshort filter;
ushort flags;
uint fflags;
intptr_t data;
ptr<void> udata;
};

struct KNote {
KEvent event;
bool enabled;
};

struct KQueue : orbis::File {
std::list<KNote, kallocator<KNote>> notes;
};

static constexpr auto kEvFiltRead = -1;
static constexpr auto kEvFiltWrite = -2;
static constexpr auto kEvFiltAio = -3;
static constexpr auto kEvFiltVnode = -4;
static constexpr auto kEvFiltProc = -5;
static constexpr auto kEvFiltSignal = -6;
static constexpr auto kEvFiltTimer = -7;
static constexpr auto kEvFiltFs = -9;
static constexpr auto kEvFiltLio = -10;
static constexpr auto kEvFiltUser = -11;
static constexpr auto kEvFiltPolling = -12;
static constexpr auto kEvFiltDisplay = -13;
static constexpr auto kEvFiltGraphicsCore = -14;
static constexpr auto kEvFiltHrTimer = -15;
static constexpr auto kEvFiltUvdTrap = -16;
static constexpr auto kEvFiltVceTrap = -17;
static constexpr auto kEvFiltSdmaTrap = -18;
static constexpr auto kEvFiltRegEv = -19;
static constexpr auto kEvFiltGpuException = -20;
static constexpr auto kEvFiltGpuSystemException = -21;
static constexpr auto kEvFiltGpuDbgGcEv = -22;
static constexpr auto kEvFiltSysCount = 22;

// actions
static constexpr auto kEvAdd = 0x0001;
static constexpr auto kEvDelete = 0x0002;
static constexpr auto kEvEnable = 0x0004;
static constexpr auto kEvDisable = 0x0008;

// flags
static constexpr auto kEvOneshot = 0x0010;
static constexpr auto kEvClear = 0x0020;
static constexpr auto kEvReceipt = 0x0040;
static constexpr auto kEvDispatch = 0x0080;
static constexpr auto kEvSysFlags = 0xf000;
static constexpr auto kEvFlag1 = 0x2000;

static constexpr auto kEvEof = 0x8000;
static constexpr auto kEvError = 0x4000;
} // namespace orbis

orbis::SysResult orbis::sys_kqueue(Thread *thread) {
ORBIS_LOG_TODO(__FUNCTION__);
auto queue = knew<KQueue>();
if (queue == nullptr) {
return ErrorCode::NOMEM;
}

thread->retval[0] = thread->tproc->fileDescriptors.insert(queue);
auto fd = thread->tproc->fileDescriptors.insert(queue);
ORBIS_LOG_TODO(__FUNCTION__, fd);
thread->retval[0] = fd;
return {};
}

orbis::SysResult orbis::sys_kqueueex(Thread *thread, ptr<char> name, sint flags) {
auto queue = knew<KQueue>();
if (queue == nullptr) {
return ErrorCode::NOMEM;
}

auto fd = thread->tproc->fileDescriptors.insert(queue);
ORBIS_LOG_TODO(__FUNCTION__, name, flags, fd);
thread->retval[0] = fd;
return {};
}

orbis::SysResult orbis::sys_kevent(Thread *thread, sint fd,
ptr<KEvent> changelist, sint nchanges,
ptr<KEvent> eventlist, sint nevents,
ptr<const timespec> timeout) {
// ORBIS_LOG_TODO(__FUNCTION__, fd, changelist, nchanges, eventlist, nevents, timeout);
thread->retval[0] = nevents;
// ORBIS_LOG_TODO(__FUNCTION__, fd, changelist, nchanges, eventlist, nevents,
// timeout);
Ref<File> kqf = thread->tproc->fileDescriptors.get(fd);
if (kqf == nullptr) {
return orbis::ErrorCode::BADF;
}

std::lock_guard lock(kqf->mtx);

auto kq = dynamic_cast<KQueue *>(kqf.get());

if (kq == nullptr) {
return orbis::ErrorCode::BADF;
}


if (nchanges != 0) {
for (auto change : std::span(changelist, nchanges)) {
ORBIS_LOG_TODO(__FUNCTION__, change.ident, change.filter, change.flags,
change.fflags, change.data, change.udata);

if (change.flags & kEvAdd) {
if (change.filter != kEvFiltDisplay && change.filter != kEvFiltGraphicsCore) {
std::abort();
}

kq->notes.push_back({
.event = change,
.enabled = (change.flags & kEvDisable) == 0
});

kq->notes.back().event.flags &= ~(kEvAdd | kEvClear | kEvDelete | kEvDisable);
}

// TODO
}
}

std::vector<KEvent> result;
result.reserve(nevents);

for (auto &note : kq->notes) {
if (result.size() >= nevents) {
break;
}

if (!note.enabled) {
continue;
}

if (note.event.filter == kEvFiltDisplay || note.event.filter == kEvFiltGraphicsCore) {
result.push_back(note.event);
}
}

std::memcpy(eventlist, result.data(), result.size() * sizeof(KEvent));
thread->retval[0] = result.size();
return {};
}
12 changes: 0 additions & 12 deletions orbis-kernel/src/sys/sys_sce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,7 @@ orbis::SysResult orbis::sys_socketclose(Thread *thread, sint fd) {
orbis::SysResult orbis::sys_netgetiflist(Thread *thread /* TODO */) {
return ErrorCode::NOSYS;
}
struct KQueueEx : orbis::File {};

orbis::SysResult orbis::sys_kqueueex(Thread *thread /* TODO */) {
ORBIS_LOG_TODO(__FUNCTION__);
auto queue = knew<KQueueEx>();
if (queue == nullptr) {
return ErrorCode::NOMEM;
}

thread->retval[0] = thread->tproc->fileDescriptors.insert(queue);
thread->where();
return {};
}
orbis::SysResult orbis::sys_mtypeprotect(Thread *thread /* TODO */) {
return ErrorCode::NOSYS;
}
Expand Down

0 comments on commit 95f1a6f

Please sign in to comment.