Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
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)
6 */
7
8#include <stdlib.h>
9#include <errno.h>
10#include <sys/epoll.h>
11#include <signal.h>
12#include <string.h>
13#include <irq_user.h>
14#include <os.h>
15#include <um_malloc.h>
16
17/* Epoll support */
18
19static int epollfd = -1;
20
21#define MAX_EPOLL_EVENTS 64
22
23static struct epoll_event epoll_events[MAX_EPOLL_EVENTS];
24
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
27 */
28
29void *os_epoll_get_data_pointer(int index)
30{
31 return epoll_events[index].data.ptr;
32}
33
34/* Helper to compare events versus the events in the epoll structure.
35 * Same as above - needs to be on the userspace side
36 */
37
38
39int os_epoll_triggered(int index, int events)
40{
41 return epoll_events[index].events & events;
42}
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.
46 */
47
48int os_event_mask(enum um_irq_type irq_type)
49{
50 if (irq_type == IRQ_READ)
51 return EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP | EPOLLRDHUP;
52 if (irq_type == IRQ_WRITE)
53 return EPOLLOUT;
54 return 0;
55}
56
57/*
58 * Initial Epoll Setup
59 */
60int os_setup_epoll(void)
61{
62 epollfd = epoll_create(MAX_EPOLL_EVENTS);
63 return epollfd;
64}
65
66/*
67 * Helper to run the actual epoll_wait
68 */
69int os_waiting_for_events_epoll(void)
70{
71 int n, err;
72
73 n = epoll_wait(epollfd,
74 (struct epoll_event *) &epoll_events, MAX_EPOLL_EVENTS, 0);
75 if (n < 0) {
76 err = -errno;
77 if (errno != EINTR)
78 printk(
79 UM_KERN_ERR "os_waiting_for_events:"
80 " epoll returned %d, error = %s\n", n,
81 strerror(errno)
82 );
83 return err;
84 }
85 return n;
86}
87
88
89/*
90 * Helper to add a fd to epoll
91 */
92int os_add_epoll_fd(int events, int fd, void *data)
93{
94 struct epoll_event event;
95 int result;
96
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);
102 if (result)
103 printk("epollctl add err fd %d, %s\n", fd, strerror(errno));
104 return result;
105}
106
107/*
108 * Helper to mod the fd event mask and/or data backreference
109 */
110int os_mod_epoll_fd(int events, int fd, void *data)
111{
112 struct epoll_event event;
113 int result;
114
115 event.data.ptr = data;
116 event.events = events;
117 result = epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &event);
118 if (result)
119 printk(UM_KERN_ERR
120 "epollctl mod err fd %d, %s\n", fd, strerror(errno));
121 return result;
122}
123
124/*
125 * Helper to delete the epoll fd
126 */
127int os_del_epoll_fd(int fd)
128{
129 struct epoll_event event;
130 /* This is quiet as we use this as IO ON/OFF - so it is often
131 * invoked on a non-existent fd
132 */
133 return epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &event);
134}
135
136void os_set_ioignore(void)
137{
138 signal(SIGIO, SIG_IGN);
139}
140
141void os_close_epoll_fd(void)
142{
143 /* Needed so we do not leak an fd when rebooting */
144 os_close_file(epollfd);
145}
1/*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
6#include <stdlib.h>
7#include <errno.h>
8#include <poll.h>
9#include <signal.h>
10#include <string.h>
11#include "irq_user.h"
12#include "kern_constants.h"
13#include "os.h"
14#include "process.h"
15#include "um_malloc.h"
16#include "user.h"
17
18/*
19 * Locked by irq_lock in arch/um/kernel/irq.c. Changed by os_create_pollfd
20 * and os_free_irq_by_cb, which are called under irq_lock.
21 */
22static struct pollfd *pollfds = NULL;
23static int pollfds_num = 0;
24static int pollfds_size = 0;
25
26int os_waiting_for_events(struct irq_fd *active_fds)
27{
28 struct irq_fd *irq_fd;
29 int i, n, err;
30
31 n = poll(pollfds, pollfds_num, 0);
32 if (n < 0) {
33 err = -errno;
34 if (errno != EINTR)
35 printk(UM_KERN_ERR "os_waiting_for_events:"
36 " poll returned %d, errno = %d\n", n, errno);
37 return err;
38 }
39
40 if (n == 0)
41 return 0;
42
43 irq_fd = active_fds;
44
45 for (i = 0; i < pollfds_num; i++) {
46 if (pollfds[i].revents != 0) {
47 irq_fd->current_events = pollfds[i].revents;
48 pollfds[i].fd = -1;
49 }
50 irq_fd = irq_fd->next;
51 }
52 return n;
53}
54
55int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
56{
57 if (pollfds_num == pollfds_size) {
58 if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
59 /* return min size needed for new pollfds area */
60 return (pollfds_size + 1) * sizeof(pollfds[0]);
61 }
62
63 if (pollfds != NULL) {
64 memcpy(tmp_pfd, pollfds,
65 sizeof(pollfds[0]) * pollfds_size);
66 /* remove old pollfds */
67 kfree(pollfds);
68 }
69 pollfds = tmp_pfd;
70 pollfds_size++;
71 } else
72 kfree(tmp_pfd); /* remove not used tmp_pfd */
73
74 pollfds[pollfds_num] = ((struct pollfd) { .fd = fd,
75 .events = events,
76 .revents = 0 });
77 pollfds_num++;
78
79 return 0;
80}
81
82void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
83 struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
84{
85 struct irq_fd **prev;
86 int i = 0;
87
88 prev = &active_fds;
89 while (*prev != NULL) {
90 if ((*test)(*prev, arg)) {
91 struct irq_fd *old_fd = *prev;
92 if ((pollfds[i].fd != -1) &&
93 (pollfds[i].fd != (*prev)->fd)) {
94 printk(UM_KERN_ERR "os_free_irq_by_cb - "
95 "mismatch between active_fds and "
96 "pollfds, fd %d vs %d\n",
97 (*prev)->fd, pollfds[i].fd);
98 goto out;
99 }
100
101 pollfds_num--;
102
103 /*
104 * This moves the *whole* array after pollfds[i]
105 * (though it doesn't spot as such)!
106 */
107 memmove(&pollfds[i], &pollfds[i + 1],
108 (pollfds_num - i) * sizeof(pollfds[0]));
109 if (*last_irq_ptr2 == &old_fd->next)
110 *last_irq_ptr2 = prev;
111
112 *prev = (*prev)->next;
113 if (old_fd->type == IRQ_WRITE)
114 ignore_sigio_fd(old_fd->fd);
115 kfree(old_fd);
116 continue;
117 }
118 prev = &(*prev)->next;
119 i++;
120 }
121 out:
122 return;
123}
124
125int os_get_pollfd(int i)
126{
127 return pollfds[i].fd;
128}
129
130void os_set_pollfd(int i, int fd)
131{
132 pollfds[i].fd = fd;
133}
134
135void os_set_ioignore(void)
136{
137 signal(SIGIO, SIG_IGN);
138}