root/fs/select.c

Revision 20, 12.4 kB (checked in by dkaplan1, 7 years ago)

Regression tests passed
Fixed printk implementation
Added lots of callgates to support rtc.c
rtc.c is UNTESTED
Added calldriver calls in for /proc support and for timers
Also added possibility of calling driver method asynchronously

Line 
1 /*
2  * This file contains the procedures for the handling of select and poll
3  *
4  * Created for Linux based loosely upon Mathius Lattner's minix
5  * patches by Peter MacDonald. Heavily edited by Linus.
6  *
7  *  4 February 1994
8  *     COFF/ELF binary emulation. If the process has the STICKY_TIMEOUTS
9  *     flag set in its personality we do *not* modify the given timeout
10  *     parameter to reflect time remaining.
11  *
12  *  24 January 2000
13  *     Changed sys_poll()/do_poll() to use PAGE_SIZE chunk-based allocation
14  *     of fds to overcome nfds < 16390 descriptors limit (Tigran Aivazian).
15  */
16
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/smp_lock.h>
20 #include <linux/poll.h>
21 #include <linux/personality.h> /* for STICKY_TIMEOUTS */
22 #include <linux/file.h>
23 #include <linux/fs.h>
24
25 #include <asm/uaccess.h>
26
27 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
28 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
29
30 struct poll_table_entry {
31         struct file * filp;
32         wait_queue_t wait;
33         wait_queue_head_t * wait_address;
34 };
35
36 struct poll_table_page {
37         struct poll_table_page * next;
38         struct poll_table_entry * entry;
39         struct poll_table_entry entries[0];
40 };
41
42 #define POLL_TABLE_FULL(table) \
43         ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
44
45 /*
46  * Ok, Peter made a complicated, but straightforward multiple_wait() function.
47  * I have rewritten this, taking some shortcuts: This code may not be easy to
48  * follow, but it should be free of race-conditions, and it's practical. If you
49  * understand what I'm doing here, then you understand how the linux
50  * sleep/wakeup mechanism works.
51  *
52  * Two very simple procedures, poll_wait() and poll_freewait() make all the
53  * work.  poll_wait() is an inline-function defined in <linux/poll.h>,
54  * as all select/poll functions have to call it to add an entry to the
55  * poll table.
56  */
57 void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p);
58
59 void poll_initwait(struct poll_wqueues *pwq)
60 {
61         init_poll_funcptr(&pwq->pt, __pollwait);
62         pwq->error = 0;
63         pwq->table = NULL;
64 }
65
66 EXPORT_SYMBOL(poll_initwait);
67
68 void poll_freewait(struct poll_wqueues *pwq)
69 {
70         struct poll_table_page * p = pwq->table;
71         while (p) {
72                 struct poll_table_entry * entry;
73                 struct poll_table_page *old;
74
75                 entry = p->entry;
76                 do {
77                         entry--;
78                         remove_wait_queue(entry->wait_address,&entry->wait);
79                         fput(entry->filp);
80                 } while (entry > p->entries);
81                 old = p;
82                 p = p->next;
83                 free_page((unsigned long) old);
84         }
85 }
86
87 EXPORT_SYMBOL(poll_freewait);
88
89 //NEW FOR RING CYCLE
90 void mod_poll_wait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
91 {
92     poll_wait(filp, wait_address, p);
93 }
94
95
96 void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *_p)
97 {
98         struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
99         struct poll_table_page *table = p->table;
100
101         if (!table || POLL_TABLE_FULL(table)) {
102                 struct poll_table_page *new_table;
103
104                 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
105                 if (!new_table) {
106                         p->error = -ENOMEM;
107                         __set_current_state(TASK_RUNNING);
108                         return;
109                 }
110                 new_table->entry = new_table->entries;
111                 new_table->next = table;
112                 p->table = new_table;
113                 table = new_table;
114         }
115
116         /* Add a new entry */
117         {
118                 struct poll_table_entry * entry = table->entry;
119                 table->entry = entry+1;
120                 get_file(filp);
121                 entry->filp = filp;
122                 entry->wait_address = wait_address;
123                 init_waitqueue_entry(&entry->wait, current);
124                 add_wait_queue(wait_address,&entry->wait);
125         }
126 }
127
128
129 #define __IN(fds, n)            (fds->in + n)
130 #define __OUT(fds, n)           (fds->out + n)
131 #define __EX(fds, n)            (fds->ex + n)
132 #define __RES_IN(fds, n)        (fds->res_in + n)
133 #define __RES_OUT(fds, n)       (fds->res_out + n)
134 #define __RES_EX(fds, n)        (fds->res_ex + n)
135
136 #define BITS(fds, n)            (*__IN(fds, n)|*__OUT(fds, n)|*__EX(fds, n))
137
138 static int max_select_fd(unsigned long n, fd_set_bits *fds)
139 {
140         unsigned long *open_fds;
141         unsigned long set;
142         int max;
143
144         /* handle last in-complete long-word first */
145         set = ~(~0UL << (n & (__NFDBITS-1)));
146         n /= __NFDBITS;
147         open_fds = current->files->open_fds->fds_bits+n;
148         max = 0;
149         if (set) {
150                 set &= BITS(fds, n);
151                 if (set) {
152                         if (!(set & ~*open_fds))
153                                 goto get_max;
154                         return -EBADF;
155                 }
156         }
157         while (n) {
158                 open_fds--;
159                 n--;
160                 set = BITS(fds, n);
161                 if (!set)
162                         continue;
163                 if (set & ~*open_fds)
164                         return -EBADF;
165                 if (max)
166                         continue;
167 get_max:
168                 do {
169                         max++;
170                         set >>= 1;
171                 } while (set);
172                 max += n * __NFDBITS;
173         }
174
175         return max;
176 }
177
178 #define BIT(i)          (1UL << ((i)&(__NFDBITS-1)))
179 #define MEM(i,m)        ((m)+(unsigned)(i)/__NFDBITS)
180 #define ISSET(i,m)      (((i)&*(m)) != 0)
181 #define SET(i,m)        (*(m) |= (i))
182
183 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
184 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
185 #define POLLEX_SET (POLLPRI)
186
187 int do_select(int n, fd_set_bits *fds, long *timeout)
188 {
189         struct poll_wqueues table;
190         poll_table *wait;
191         int retval, i;
192         long __timeout = *timeout;
193
194         spin_lock(&current->files->file_lock);
195         retval = max_select_fd(n, fds);
196         spin_unlock(&current->files->file_lock);
197
198         if (retval < 0)
199                 return retval;
200         n = retval;
201
202         poll_initwait(&table);
203         wait = &table.pt;
204         if (!__timeout)
205                 wait = NULL;
206         retval = 0;
207         for (;;) {
208                 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
209
210                 set_current_state(TASK_INTERRUPTIBLE);
211
212                 inp = fds->in; outp = fds->out; exp = fds->ex;
213                 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
214
215                 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
216                         unsigned long in, out, ex, all_bits, bit = 1, mask, j;
217                         unsigned long res_in = 0, res_out = 0, res_ex = 0;
218                         struct file_operations *f_op = NULL;
219                         struct file *file = NULL;
220
221                         in = *inp++; out = *outp++; ex = *exp++;
222                         all_bits = in | out | ex;
223                         if (all_bits == 0) {
224                                 i += __NFDBITS;
225                                 continue;
226                         }
227
228                         for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
229                                 if (i >= n)
230                                         break;
231                                 if (!(bit & all_bits))
232                                         continue;
233                                 file = fget(i);
234                                 if (file) {
235                                         f_op = file->f_op;
236                                         mask = DEFAULT_POLLMASK;
237                                         if (f_op && f_op->poll)
238                                                 mask = (*f_op->poll)(file, retval ? NULL : wait);
239                                         fput(file);
240                                         if ((mask & POLLIN_SET) && (in & bit)) {
241                                                 res_in |= bit;
242                                                 retval++;
243                                         }
244                                         if ((mask & POLLOUT_SET) && (out & bit)) {
245                                                 res_out |= bit;
246                                                 retval++;
247                                         }
248                                         if ((mask & POLLEX_SET) && (ex & bit)) {
249                                                 res_ex |= bit;
250                                                 retval++;
251                                         }
252                                 }
253                         }
254                         if (res_in)
255                                 *rinp = res_in;
256                         if (res_out)
257                                 *routp = res_out;
258                         if (res_ex)
259                                 *rexp = res_ex;
260                 }
261                 wait = NULL;
262                 if (retval || !__timeout || signal_pending(current))
263                         break;
264                 if(table.error) {
265                         retval = table.error;
266                         break;
267                 }
268                 __timeout = schedule_timeout(__timeout);
269         }
270         __set_current_state(TASK_RUNNING);
271
272         poll_freewait(&table);
273
274         /*
275          * Up-to-date the caller timeout.
276          */
277         *timeout = __timeout;
278         return retval;
279 }
280
281 EXPORT_SYMBOL(do_select);
282
283 static void *select_bits_alloc(int size)
284 {
285         return kmalloc(6 * size, GFP_KERNEL);
286 }
287
288 static void select_bits_free(void *bits, int size)
289 {
290         kfree(bits);
291 }
292
293 /*
294  * We can actually return ERESTARTSYS instead of EINTR, but I'd
295  * like to be certain this leads to no problems. So I return
296  * EINTR just for safety.
297  *
298  * Update: ERESTARTSYS breaks at least the xview clock binary, so
299  * I'm trying ERESTARTNOHAND which restart only when you want to.
300  */
301 #define MAX_SELECT_SECONDS \
302         ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
303
304 asmlinkage long
305 sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
306 {
307         fd_set_bits fds;
308         char *bits;
309         long timeout;
310         int ret, size, max_fdset;
311
312         timeout = MAX_SCHEDULE_TIMEOUT;
313         if (tvp) {
314                 time_t sec, usec;
315
316                 if ((ret = verify_area(VERIFY_READ, tvp, sizeof(*tvp)))
317                     || (ret = __get_user(sec, &tvp->tv_sec))
318                     || (ret = __get_user(usec, &tvp->tv_usec)))
319                         goto out_nofds;
320
321                 ret = -EINVAL;
322                 if (sec < 0 || usec < 0)
323                         goto out_nofds;
324
325                 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
326                         timeout = ROUND_UP(usec, 1000000/HZ);
327                         timeout += sec * (unsigned long) HZ;
328                 }
329         }
330
331         ret = -EINVAL;
332         if (n < 0)
333                 goto out_nofds;
334
335         /* max_fdset can increase, so grab it once to avoid race */
336         max_fdset = current->files->max_fdset;
337         if (n > max_fdset)
338                 n = max_fdset;
339
340         /*
341          * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
342          * since we used fdset we need to allocate memory in units of
343          * long-words.
344          */
345         ret = -ENOMEM;
346         size = FDS_BYTES(n);
347         bits = select_bits_alloc(size);
348         if (!bits)
349                 goto out_nofds;
350         fds.in      = (unsigned long *)  bits;
351         fds.out     = (unsigned long *) (bits +   size);
352         fds.ex      = (unsigned long *) (bits + 2*size);
353         fds.res_in  = (unsigned long *) (bits + 3*size);
354         fds.res_out = (unsigned long *) (bits + 4*size);
355         fds.res_ex  = (unsigned long *) (bits + 5*size);
356
357         if ((ret = get_fd_set(n, inp, fds.in)) ||
358             (ret = get_fd_set(n, outp, fds.out)) ||
359             (ret = get_fd_set(n, exp, fds.ex)))
360                 goto out;
361         zero_fd_set(n, fds.res_in);
362         zero_fd_set(n, fds.res_out);
363         zero_fd_set(n, fds.res_ex);
364
365         ret = do_select(n, &fds, &timeout);
366
367         if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
368                 time_t sec = 0, usec = 0;
369                 if (timeout) {
370                         sec = timeout / HZ;
371                         usec = timeout % HZ;
372                         usec *= (1000000/HZ);
373                 }
374                 put_user(sec, &tvp->tv_sec);
375                 put_user(usec, &tvp->tv_usec);
376         }
377
378         if (ret < 0)
379                 goto out;
380         if (!ret) {
381                 ret = -ERESTARTNOHAND;
382                 if (signal_pending(current))
383                         goto out;
384                 ret = 0;
385         }
386
387         set_fd_set(n, inp, fds.res_in);
388         set_fd_set(n, outp, fds.res_out);
389         set_fd_set(n, exp, fds.res_ex);
390
391 out:
392         select_bits_free(bits, size);
393 out_nofds:
394         return ret;
395 }
396
397 struct poll_list {
398         struct poll_list *next;
399         int len;
400         struct pollfd entries[0];
401 };
402
403 #define POLLFD_PER_PAGE  ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
404
405 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
406         poll_table ** pwait, int *count)
407 {
408         int i;
409
410         for (i = 0; i < num; i++) {
411                 int fd;
412                 unsigned int mask;
413                 struct pollfd *fdp;
414
415                 mask = 0;
416                 fdp = fdpage+i;
417                 fd = fdp->fd;
418                 if (fd >= 0) {
419                         struct file * file = fget(fd);
420                         mask = POLLNVAL;
421                         if (file != NULL) {
422                                 mask = DEFAULT_POLLMASK;
423                                 if (file->f_op && file->f_op->poll)
424                                         mask = file->f_op->poll(file, *pwait);
425                                 mask &= fdp->events | POLLERR | POLLHUP;
426                                 fput(file);
427                         }
428                         if (mask) {
429                                 *pwait = NULL;
430                                 (*count)++;
431                         }
432                 }
433                 fdp->revents = mask;
434         }
435 }
436
437 static int do_poll(unsigned int nfds,  struct poll_list *list,
438                         struct poll_wqueues *wait, long timeout)
439 {
440         int count = 0;
441         poll_table* pt = &wait->pt;
442
443         if (!timeout)
444                 pt = NULL;
445  
446         for (;;) {
447                 struct poll_list *walk;
448                 set_current_state(TASK_INTERRUPTIBLE);
449                 walk = list;
450                 while(walk != NULL) {
451                         do_pollfd( walk->len, walk->entries, &pt, &count);
452                         walk = walk->next;
453                 }
454                 pt = NULL;
455                 if (count || !timeout || signal_pending(current))
456                         break;
457                 count = wait->error;
458                 if (count)
459                         break;
460                 timeout = schedule_timeout(timeout);
461         }
462         __set_current_state(TASK_RUNNING);
463         return count;
464 }
465
466 asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long timeout)
467 {
468         struct poll_wqueues table;
469         int fdcount, err;
470         unsigned int i;
471         struct poll_list *head;
472         struct poll_list *walk;
473
474         /* Do a sanity check on nfds ... */
475         if (nfds > current->files->max_fdset && nfds > OPEN_MAX)
476                 return -EINVAL;
477
478         if (timeout) {
479                 /* Careful about overflow in the intermediate values */
480                 if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
481                         timeout = (unsigned long)(timeout*HZ+999)/1000+1;
482                 else /* Negative or overflow */
483                         timeout = MAX_SCHEDULE_TIMEOUT;
484         }
485
486         poll_initwait(&table);
487
488         head = NULL;
489         walk = NULL;
490         i = nfds;
491         err = -ENOMEM;
492         while(i!=0) {
493                 struct poll_list *pp;
494                 pp = kmalloc(sizeof(struct poll_list)+
495                                 sizeof(struct pollfd)*
496                                 (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
497                                         GFP_KERNEL);
498                 if(pp==NULL)
499                         goto out_fds;
500                 pp->next=NULL;
501                 pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
502                 if (head == NULL)
503                         head = pp;
504                 else
505                         walk->next = pp;
506
507                 walk = pp;
508                 if (copy_from_user(pp->entries, ufds + nfds-i,
509                                 sizeof(struct pollfd)*pp->len)) {
510                         err = -EFAULT;
511                         goto out_fds;
512                 }
513                 i -= pp->len;
514         }
515         fdcount = do_poll(nfds, head, &table, timeout);
516
517         /* OK, now copy the revents fields back to user space. */
518         walk = head;
519         err = -EFAULT;
520         while(walk != NULL) {
521                 struct pollfd *fds = walk->entries;
522                 int j;
523
524                 for (j=0; j < walk->len; j++, ufds++) {
525                         if(__put_user(fds[j].revents, &ufds->revents))
526                                 goto out_fds;
527                 }
528                 walk = walk->next;
529         }
530         err = fdcount;
531         if (!fdcount && signal_pending(current))
532                 err = -EINTR;
533 out_fds:
534         walk = head;
535         while(walk!=NULL) {
536                 struct poll_list *pp = walk->next;
537                 kfree(walk);
538                 walk = pp;
539         }
540         poll_freewait(&table);
541         return err;
542 }
Note: See TracBrowser for help on using the browser.