From bd5969fc876a10b18613302db7087ef3c40f18e1 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 7 Mar 2024 17:28:00 -0500 Subject: lib: Add mlibc Signed-off-by: Ian Moffett --- lib/mlibc/sysdeps/managarm/generic/sched.cpp | 102 +++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 lib/mlibc/sysdeps/managarm/generic/sched.cpp (limited to 'lib/mlibc/sysdeps/managarm/generic/sched.cpp') diff --git a/lib/mlibc/sysdeps/managarm/generic/sched.cpp b/lib/mlibc/sysdeps/managarm/generic/sched.cpp new file mode 100644 index 0000000..bce8db8 --- /dev/null +++ b/lib/mlibc/sysdeps/managarm/generic/sched.cpp @@ -0,0 +1,102 @@ +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace mlibc { + +int sys_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask) { + return sys_getthreadaffinity(pid, cpusetsize, mask); +} + +int sys_getthreadaffinity(pid_t tid, size_t cpusetsize, cpu_set_t *mask) { + SignalGuard sguard; + + managarm::posix::GetAffinityRequest req(getSysdepsAllocator()); + + req.set_pid(tid); + req.set_size(cpusetsize); + + auto [offer, send_head, recv_resp, recv_data] = + exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline(), + helix_ng::recvBuffer(mask, cpusetsize) + ) + ); + + HEL_CHECK(offer.error()); + HEL_CHECK(send_head.error()); + HEL_CHECK(recv_resp.error()); + + managarm::posix::SvrResponse resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + + if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + return EINVAL; + } else if(resp.error() != managarm::posix::Errors::SUCCESS) { + mlibc::infoLogger() << "mlibc: got unexpected error from posix in sys_getaffinity!" << frg::endlog; + return EIEIO; + } + HEL_CHECK(recv_data.error()); + + return 0; +} + +int sys_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask) { + return sys_setthreadaffinity(pid, cpusetsize, mask); +} + +int sys_setthreadaffinity(pid_t tid, size_t cpusetsize, const cpu_set_t *mask) { + SignalGuard sguard; + + frg::vector affinity_mask(getSysdepsAllocator()); + affinity_mask.resize(cpusetsize); + memcpy(affinity_mask.data(), mask, cpusetsize); + managarm::posix::SetAffinityRequest req(getSysdepsAllocator()); + + req.set_pid(tid); + req.set_mask(affinity_mask); + + auto [offer, send_head, send_tail, recv_resp] = + exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + + HEL_CHECK(offer.error()); + HEL_CHECK(send_head.error()); + HEL_CHECK(send_tail.error()); + HEL_CHECK(recv_resp.error()); + + managarm::posix::SvrResponse resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + + if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + return EINVAL; + } else if(resp.error() != managarm::posix::Errors::SUCCESS) { + mlibc::infoLogger() << "mlibc: got unexpected error from posix in sys_getaffinity!" << frg::endlog; + return EIEIO; + } + + return 0; +} + +int sys_getcpu(int *cpu) { + HEL_CHECK(helGetCurrentCpu(cpu)); + return 0; +} + +} -- cgit v1.2.3