Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 1 | /* |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 2 | * Copyright (C) 2017 - Cambridge Greys Ltd |
| 3 | * Copyright (C) 2011 - 2014 Cisco Systems Inc |
Jeff Dike | fee64d3 | 2008-02-04 22:31:02 -0800 | [diff] [blame] | 4 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 5 | * Licensed under the GPL |
| 6 | */ |
| 7 | |
| 8 | #include <stdlib.h> |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 9 | #include <errno.h> |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 10 | #include <sys/epoll.h> |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 11 | #include <signal.h> |
| 12 | #include <string.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 13 | #include <irq_user.h> |
| 14 | #include <os.h> |
| 15 | #include <um_malloc.h> |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 16 | |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 17 | /* Epoll support */ |
| 18 | |
| 19 | static int epollfd = -1; |
| 20 | |
| 21 | #define MAX_EPOLL_EVENTS 64 |
| 22 | |
| 23 | static 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 |
Jeff Dike | f2e6299 | 2007-02-10 01:44:23 -0800 | [diff] [blame] | 27 | */ |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 28 | |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 29 | void *os_epoll_get_data_pointer(int index) |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 30 | { |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 31 | return epoll_events[index].data.ptr; |
| 32 | } |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 33 | |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 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 | |
| 39 | int 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 | |
| 48 | int os_event_mask(int irq_type) |
| 49 | { |
| 50 | if (irq_type == IRQ_READ) |
| 51 | return EPOLLIN | EPOLLPRI; |
| 52 | if (irq_type == IRQ_WRITE) |
| 53 | return EPOLLOUT; |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Initial Epoll Setup |
| 59 | */ |
| 60 | int 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 | */ |
| 69 | int 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); |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 75 | if (n < 0) { |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 76 | err = -errno; |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 77 | if (errno != EINTR) |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 78 | printk( |
| 79 | UM_KERN_ERR "os_waiting_for_events:" |
| 80 | " epoll returned %d, error = %s\n", n, |
| 81 | strerror(errno) |
| 82 | ); |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 83 | return err; |
| 84 | } |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 85 | return n; |
| 86 | } |
| 87 | |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * Helper to add a fd to epoll |
| 91 | */ |
| 92 | int os_add_epoll_fd(int events, int fd, void *data) |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 93 | { |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 94 | struct epoll_event event; |
| 95 | int result; |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 96 | |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 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; |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 107 | /* |
| 108 | * Helper to mod the fd event mask and/or data backreference |
| 109 | */ |
| 110 | int os_mod_epoll_fd(int events, int fd, void *data) |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 111 | { |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 112 | struct epoll_event event; |
| 113 | int result; |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 114 | |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 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; |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 124 | /* |
| 125 | * Helper to delete the epoll fd |
| 126 | */ |
| 127 | int os_del_epoll_fd(int fd) |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 128 | { |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 129 | struct epoll_event event; |
| 130 | int result; |
| 131 | /* This is quiet as we use this as IO ON/OFF - so it is often |
| 132 | * invoked on a non-existent fd |
| 133 | */ |
| 134 | result = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &event); |
| 135 | return result; |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void os_set_ioignore(void) |
| 139 | { |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 140 | signal(SIGIO, SIG_IGN); |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 141 | } |
Anton Ivanov | ff6a179 | 2017-11-20 21:17:58 +0000 | [diff] [blame] | 142 | |
| 143 | void os_close_epoll_fd(void) |
| 144 | { |
| 145 | /* Needed so we do not leak an fd when rebooting */ |
| 146 | os_close_file(epollfd); |
| 147 | } |