|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-2018 Linaro Limited |
| 3 | + * Copyright (c) 2021 Nordic Semiconductor |
| 4 | + * Copyright (c) 2023 Arm Limited (or its affiliates). All rights reserved. |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +#include <zephyr/kernel.h> |
| 10 | +#include <zephyr/internal/syscall_handler.h> |
| 11 | +#include <zephyr/sys/fdtable.h> |
| 12 | + |
| 13 | +#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) |
| 14 | +bool net_socket_is_tls(void *obj); |
| 15 | +#else |
| 16 | +#define net_socket_is_tls(obj) false |
| 17 | +#endif |
| 18 | + |
| 19 | +int zvfs_poll_internal(struct zvfs_pollfd *fds, int nfds, k_timeout_t timeout) |
| 20 | +{ |
| 21 | + bool retry; |
| 22 | + int ret = 0; |
| 23 | + int i; |
| 24 | + struct zvfs_pollfd *pfd; |
| 25 | + struct k_poll_event poll_events[CONFIG_ZVFS_POLL_MAX]; |
| 26 | + struct k_poll_event *pev; |
| 27 | + struct k_poll_event *pev_end = poll_events + ARRAY_SIZE(poll_events); |
| 28 | + const struct fd_op_vtable *vtable; |
| 29 | + struct k_mutex *lock; |
| 30 | + k_timepoint_t end; |
| 31 | + bool offload = false; |
| 32 | + const struct fd_op_vtable *offl_vtable = NULL; |
| 33 | + void *offl_ctx = NULL; |
| 34 | + |
| 35 | + end = sys_timepoint_calc(timeout); |
| 36 | + |
| 37 | + pev = poll_events; |
| 38 | + for (pfd = fds, i = nfds; i--; pfd++) { |
| 39 | + void *ctx; |
| 40 | + int result; |
| 41 | + |
| 42 | + /* Per POSIX, negative fd's are just ignored */ |
| 43 | + if (pfd->fd < 0) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + ctx = zvfs_get_fd_obj_and_vtable(pfd->fd, &vtable, &lock); |
| 48 | + if (ctx == NULL) { |
| 49 | + /* Will set POLLNVAL in return loop */ |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + (void)k_mutex_lock(lock, K_FOREVER); |
| 54 | + |
| 55 | + result = zvfs_fdtable_call_ioctl(vtable, ctx, ZFD_IOCTL_POLL_PREPARE, pfd, &pev, |
| 56 | + pev_end); |
| 57 | + if (result == -EALREADY) { |
| 58 | + /* If POLL_PREPARE returned with EALREADY, it means |
| 59 | + * it already detected that some socket is ready. In |
| 60 | + * this case, we still perform a k_poll to pick up |
| 61 | + * as many events as possible, but without any wait. |
| 62 | + */ |
| 63 | + timeout = K_NO_WAIT; |
| 64 | + end = sys_timepoint_calc(timeout); |
| 65 | + result = 0; |
| 66 | + } else if (result == -EXDEV) { |
| 67 | + /* If POLL_PREPARE returned EXDEV, it means |
| 68 | + * it detected an offloaded socket. |
| 69 | + * If offloaded socket is used with native TLS, the TLS |
| 70 | + * wrapper for the offloaded poll will be used. |
| 71 | + * In case the fds array contains a mixup of offloaded |
| 72 | + * and non-offloaded sockets, the offloaded poll handler |
| 73 | + * shall return an error. |
| 74 | + */ |
| 75 | + offload = true; |
| 76 | + if (offl_vtable == NULL || net_socket_is_tls(ctx)) { |
| 77 | + offl_vtable = vtable; |
| 78 | + offl_ctx = ctx; |
| 79 | + } |
| 80 | + |
| 81 | + result = 0; |
| 82 | + } |
| 83 | + |
| 84 | + k_mutex_unlock(lock); |
| 85 | + |
| 86 | + if (result < 0) { |
| 87 | + errno = -result; |
| 88 | + return -1; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (offload) { |
| 93 | + int poll_timeout; |
| 94 | + |
| 95 | + if (K_TIMEOUT_EQ(timeout, K_FOREVER)) { |
| 96 | + poll_timeout = SYS_FOREVER_MS; |
| 97 | + } else { |
| 98 | + poll_timeout = k_ticks_to_ms_floor32(timeout.ticks); |
| 99 | + } |
| 100 | + |
| 101 | + return zvfs_fdtable_call_ioctl(offl_vtable, offl_ctx, ZFD_IOCTL_POLL_OFFLOAD, fds, |
| 102 | + nfds, poll_timeout); |
| 103 | + } |
| 104 | + |
| 105 | + timeout = sys_timepoint_timeout(end); |
| 106 | + |
| 107 | + do { |
| 108 | + ret = k_poll(poll_events, pev - poll_events, timeout); |
| 109 | + /* EAGAIN when timeout expired, EINTR when cancelled (i.e. EOF) */ |
| 110 | + if (ret != 0 && ret != -EAGAIN && ret != -EINTR) { |
| 111 | + errno = -ret; |
| 112 | + return -1; |
| 113 | + } |
| 114 | + |
| 115 | + retry = false; |
| 116 | + ret = 0; |
| 117 | + |
| 118 | + pev = poll_events; |
| 119 | + for (pfd = fds, i = nfds; i--; pfd++) { |
| 120 | + void *ctx; |
| 121 | + int result; |
| 122 | + |
| 123 | + pfd->revents = 0; |
| 124 | + |
| 125 | + if (pfd->fd < 0) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + ctx = zvfs_get_fd_obj_and_vtable(pfd->fd, &vtable, &lock); |
| 130 | + if (ctx == NULL) { |
| 131 | + pfd->revents = ZVFS_POLLNVAL; |
| 132 | + ret++; |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + (void)k_mutex_lock(lock, K_FOREVER); |
| 137 | + |
| 138 | + result = zvfs_fdtable_call_ioctl(vtable, ctx, ZFD_IOCTL_POLL_UPDATE, pfd, |
| 139 | + &pev); |
| 140 | + k_mutex_unlock(lock); |
| 141 | + |
| 142 | + if (result == -EAGAIN) { |
| 143 | + retry = true; |
| 144 | + continue; |
| 145 | + } else if (result != 0) { |
| 146 | + errno = -result; |
| 147 | + return -1; |
| 148 | + } |
| 149 | + |
| 150 | + if (pfd->revents != 0) { |
| 151 | + ret++; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (retry) { |
| 156 | + if (ret > 0) { |
| 157 | + break; |
| 158 | + } |
| 159 | + |
| 160 | + timeout = sys_timepoint_timeout(end); |
| 161 | + |
| 162 | + if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
| 163 | + break; |
| 164 | + } |
| 165 | + } |
| 166 | + } while (retry); |
| 167 | + |
| 168 | + return ret; |
| 169 | +} |
| 170 | + |
| 171 | +int z_impl_zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout) |
| 172 | +{ |
| 173 | + k_timeout_t timeout; |
| 174 | + |
| 175 | + if (poll_timeout < 0) { |
| 176 | + timeout = K_FOREVER; |
| 177 | + } else { |
| 178 | + timeout = K_MSEC(poll_timeout); |
| 179 | + } |
| 180 | + |
| 181 | + return zvfs_poll_internal(fds, nfds, timeout); |
| 182 | +} |
| 183 | + |
| 184 | +#ifdef CONFIG_USERSPACE |
| 185 | +static inline int z_vrfy_zvfs_poll(struct zvfs_pollfd *fds, int nfds, int timeout) |
| 186 | +{ |
| 187 | + struct zvfs_pollfd *fds_copy; |
| 188 | + size_t fds_size; |
| 189 | + int ret; |
| 190 | + |
| 191 | + /* Copy fds array from user mode */ |
| 192 | + if (size_mul_overflow(nfds, sizeof(struct zvfs_pollfd), &fds_size)) { |
| 193 | + errno = EFAULT; |
| 194 | + return -1; |
| 195 | + } |
| 196 | + fds_copy = k_usermode_alloc_from_copy((void *)fds, fds_size); |
| 197 | + if (!fds_copy) { |
| 198 | + errno = ENOMEM; |
| 199 | + return -1; |
| 200 | + } |
| 201 | + |
| 202 | + ret = z_impl_zvfs_poll(fds_copy, nfds, timeout); |
| 203 | + |
| 204 | + if (ret >= 0) { |
| 205 | + k_usermode_to_copy((void *)fds, fds_copy, fds_size); |
| 206 | + } |
| 207 | + k_free(fds_copy); |
| 208 | + |
| 209 | + return ret; |
| 210 | +} |
| 211 | +#include <zephyr/syscalls/zvfs_poll_mrsh.c> |
| 212 | +#endif |
0 commit comments