The Pedigree Project 0.1
epoll-syscalls.h
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 */
8
9#ifndef EPOLL_SYSCALLS_H
10#define EPOLL_SYSCALLS_H
11
12#include "pedigree/kernel/compiler.h"
13#include "pedigree/kernel/process/Readiness.h"
14#include "pedigree/kernel/processor/types.h"
15
18 uint32_t events;
19 uint64_t data;
20} PACKED;
21
22static_assert(sizeof(LinuxEpollEvent) == 12, "Linux x86-64 epoll_event must be packed");
23
24namespace LinuxEpoll {
25constexpr uint32_t In = 0x00000001U;
26constexpr uint32_t Priority = 0x00000002U;
27constexpr uint32_t Out = 0x00000004U;
28constexpr uint32_t Error = 0x00000008U;
29constexpr uint32_t Hangup = 0x00000010U;
30constexpr uint32_t Invalid = 0x00000020U;
31constexpr uint32_t ReadNormal = 0x00000040U;
32constexpr uint32_t ReadBand = 0x00000080U;
33constexpr uint32_t WriteNormal = 0x00000100U;
34constexpr uint32_t WriteBand = 0x00000200U;
35constexpr uint32_t Message = 0x00000400U;
36constexpr uint32_t ReadHangup = 0x00002000U;
37constexpr uint32_t Exclusive = 1U << 28;
38constexpr uint32_t Wakeup = 1U << 29;
39constexpr uint32_t OneShot = 1U << 30;
40constexpr uint32_t EdgeTriggered = 1U << 31;
41
42constexpr int ControlAdd = 1;
43constexpr int ControlDelete = 2;
44constexpr int ControlModify = 3;
45
46// Linux defines EPOLL_CLOEXEC as O_CLOEXEC. Keep the raw ABI value here so a
47// hosted build's fcntl constants cannot accidentally alter the guest ABI.
48constexpr int CloseOnExec = 0x00080000;
49} // namespace LinuxEpoll
50
51class EpollState;
53
55class EXPORTED_PUBLIC EpollInstance final : public ReadinessSource {
56 public:
58 ~EpollInstance() override;
59
61 int control(int operation, int targetFd, const LinuxEpollEvent* event);
62
64 int wait(LinuxEpollEvent* events, int maxEvents, int timeoutMilliseconds);
65
67 ReadyMask queryReady();
68
69 private:
70 friend class EpollReadinessObserver;
71
72 EpollInstance(const EpollInstance&) = delete;
73 EpollInstance& operator=(const EpollInstance&) = delete;
74
75 int collectEvents(LinuxEpollEvent* events, int maxEvents, bool consumeOneShot);
76 void wakeWaiter();
77 void sourceReadinessChanged(ReadyMask mask);
78
79 EpollState* m_State;
80};
81
82int posix_epoll_create1(int flags);
83int posix_epoll_create(int size);
84int posix_epoll_ctl(int epollFd, int operation, int targetFd, const LinuxEpollEvent* event);
85int posix_epoll_wait(int epollFd, LinuxEpollEvent* events, int maxEvents, int timeoutMilliseconds);
86int posix_epoll_pwait(int epollFd, LinuxEpollEvent* events, int maxEvents, int timeoutMilliseconds,
87 const void* signalMask, size_t signalMaskSize);
88
89#endif