Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_POLL_H
0003 #define _LINUX_POLL_H
0004 
0005 
0006 #include <linux/compiler.h>
0007 #include <linux/ktime.h>
0008 #include <linux/wait.h>
0009 #include <linux/string.h>
0010 #include <linux/fs.h>
0011 #include <linux/uaccess.h>
0012 #include <uapi/linux/poll.h>
0013 #include <uapi/linux/eventpoll.h>
0014 
0015 /* ~832 bytes of stack space used max in sys_select/sys_poll before allocating
0016    additional memory. */
0017 #ifdef __clang__
0018 #define MAX_STACK_ALLOC 768
0019 #else
0020 #define MAX_STACK_ALLOC 832
0021 #endif
0022 #define FRONTEND_STACK_ALLOC    256
0023 #define SELECT_STACK_ALLOC  FRONTEND_STACK_ALLOC
0024 #define POLL_STACK_ALLOC    FRONTEND_STACK_ALLOC
0025 #define WQUEUES_STACK_ALLOC (MAX_STACK_ALLOC - FRONTEND_STACK_ALLOC)
0026 #define N_INLINE_POLL_ENTRIES   (WQUEUES_STACK_ALLOC / sizeof(struct poll_table_entry))
0027 
0028 #define DEFAULT_POLLMASK (EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM)
0029 
0030 struct poll_table_struct;
0031 
0032 /* 
0033  * structures and helpers for f_op->poll implementations
0034  */
0035 typedef void (*poll_queue_proc)(struct file *, wait_queue_head_t *, struct poll_table_struct *);
0036 
0037 /*
0038  * Do not touch the structure directly, use the access functions
0039  * poll_does_not_wait() and poll_requested_events() instead.
0040  */
0041 typedef struct poll_table_struct {
0042     poll_queue_proc _qproc;
0043     __poll_t _key;
0044 } poll_table;
0045 
0046 static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
0047 {
0048     if (p && p->_qproc && wait_address)
0049         p->_qproc(filp, wait_address, p);
0050 }
0051 
0052 /*
0053  * Return true if it is guaranteed that poll will not wait. This is the case
0054  * if the poll() of another file descriptor in the set got an event, so there
0055  * is no need for waiting.
0056  */
0057 static inline bool poll_does_not_wait(const poll_table *p)
0058 {
0059     return p == NULL || p->_qproc == NULL;
0060 }
0061 
0062 /*
0063  * Return the set of events that the application wants to poll for.
0064  * This is useful for drivers that need to know whether a DMA transfer has
0065  * to be started implicitly on poll(). You typically only want to do that
0066  * if the application is actually polling for POLLIN and/or POLLOUT.
0067  */
0068 static inline __poll_t poll_requested_events(const poll_table *p)
0069 {
0070     return p ? p->_key : ~(__poll_t)0;
0071 }
0072 
0073 static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
0074 {
0075     pt->_qproc = qproc;
0076     pt->_key   = ~(__poll_t)0; /* all events enabled */
0077 }
0078 
0079 static inline bool file_can_poll(struct file *file)
0080 {
0081     return file->f_op->poll;
0082 }
0083 
0084 static inline __poll_t vfs_poll(struct file *file, struct poll_table_struct *pt)
0085 {
0086     if (unlikely(!file->f_op->poll))
0087         return DEFAULT_POLLMASK;
0088     return file->f_op->poll(file, pt);
0089 }
0090 
0091 struct poll_table_entry {
0092     struct file *filp;
0093     __poll_t key;
0094     wait_queue_entry_t wait;
0095     wait_queue_head_t *wait_address;
0096 };
0097 
0098 /*
0099  * Structures and helpers for select/poll syscall
0100  */
0101 struct poll_wqueues {
0102     poll_table pt;
0103     struct poll_table_page *table;
0104     struct task_struct *polling_task;
0105     int triggered;
0106     int error;
0107     int inline_index;
0108     struct poll_table_entry inline_entries[N_INLINE_POLL_ENTRIES];
0109 };
0110 
0111 extern void poll_initwait(struct poll_wqueues *pwq);
0112 extern void poll_freewait(struct poll_wqueues *pwq);
0113 extern u64 select_estimate_accuracy(struct timespec64 *tv);
0114 
0115 #define MAX_INT64_SECONDS (((s64)(~((u64)0)>>1)/HZ)-1)
0116 
0117 extern int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
0118                fd_set __user *exp, struct timespec64 *end_time);
0119 
0120 extern int poll_select_set_timeout(struct timespec64 *to, time64_t sec,
0121                    long nsec);
0122 
0123 #define __MAP(v, from, to) \
0124     (from < to ? (v & from) * (to/from) : (v & from) / (from/to))
0125 
0126 static inline __u16 mangle_poll(__poll_t val)
0127 {
0128     __u16 v = (__force __u16)val;
0129 #define M(X) __MAP(v, (__force __u16)EPOLL##X, POLL##X)
0130     return M(IN) | M(OUT) | M(PRI) | M(ERR) | M(NVAL) |
0131         M(RDNORM) | M(RDBAND) | M(WRNORM) | M(WRBAND) |
0132         M(HUP) | M(RDHUP) | M(MSG);
0133 #undef M
0134 }
0135 
0136 static inline __poll_t demangle_poll(u16 val)
0137 {
0138 #define M(X) (__force __poll_t)__MAP(val, POLL##X, (__force __u16)EPOLL##X)
0139     return M(IN) | M(OUT) | M(PRI) | M(ERR) | M(NVAL) |
0140         M(RDNORM) | M(RDBAND) | M(WRNORM) | M(WRBAND) |
0141         M(HUP) | M(RDHUP) | M(MSG);
0142 #undef M
0143 }
0144 #undef __MAP
0145 
0146 
0147 #endif /* _LINUX_POLL_H */