1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017 - Cambridge Greys Ltd
4 * Copyright (C) 2011 - 2014 Cisco Systems Inc
5 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
10 #include <sys/epoll.h>
15 #include <um_malloc.h>
19 static int epollfd
= -1;
21 #define MAX_EPOLL_EVENTS 64
23 static struct epoll_event epoll_events
[MAX_EPOLL_EVENTS
];
25 /* Helper to return an Epoll data pointer from an epoll event structure.
26 * We need to keep this one on the userspace side to keep includes separate
29 void *os_epoll_get_data_pointer(int index
)
31 return epoll_events
[index
].data
.ptr
;
34 /* Helper to compare events versus the events in the epoll structure.
35 * Same as above - needs to be on the userspace side
39 int os_epoll_triggered(int index
, int events
)
41 return epoll_events
[index
].events
& events
;
43 /* Helper to set the event mask.
44 * The event mask is opaque to the kernel side, because it does not have
45 * access to the right includes/defines for EPOLL constants.
48 int os_event_mask(int irq_type
)
50 if (irq_type
== IRQ_READ
)
51 return EPOLLIN
| EPOLLPRI
;
52 if (irq_type
== IRQ_WRITE
)
60 int os_setup_epoll(void)
62 epollfd
= epoll_create(MAX_EPOLL_EVENTS
);
67 * Helper to run the actual epoll_wait
69 int os_waiting_for_events_epoll(void)
73 n
= epoll_wait(epollfd
,
74 (struct epoll_event
*) &epoll_events
, MAX_EPOLL_EVENTS
, 0);
79 UM_KERN_ERR
"os_waiting_for_events:"
80 " epoll returned %d, error = %s\n", n
,
90 * Helper to add a fd to epoll
92 int os_add_epoll_fd(int events
, int fd
, void *data
)
94 struct epoll_event event
;
97 event
.data
.ptr
= data
;
98 event
.events
= events
| EPOLLET
;
99 result
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, fd
, &event
);
100 if ((result
) && (errno
== EEXIST
))
101 result
= os_mod_epoll_fd(events
, fd
, data
);
103 printk("epollctl add err fd %d, %s\n", fd
, strerror(errno
));
108 * Helper to mod the fd event mask and/or data backreference
110 int os_mod_epoll_fd(int events
, int fd
, void *data
)
112 struct epoll_event event
;
115 event
.data
.ptr
= data
;
116 event
.events
= events
;
117 result
= epoll_ctl(epollfd
, EPOLL_CTL_MOD
, fd
, &event
);
120 "epollctl mod err fd %d, %s\n", fd
, strerror(errno
));
125 * Helper to delete the epoll fd
127 int os_del_epoll_fd(int fd
)
129 struct epoll_event event
;
131 /* This is quiet as we use this as IO ON/OFF - so it is often
132 * invoked on a non-existent fd
134 result
= epoll_ctl(epollfd
, EPOLL_CTL_DEL
, fd
, &event
);
138 void os_set_ioignore(void)
140 signal(SIGIO
, SIG_IGN
);
143 void os_close_epoll_fd(void)
145 /* Needed so we do not leak an fd when rebooting */
146 os_close_file(epollfd
);