vserver 1.9.5.x5
[linux-2.6.git] / kernel / compat.c
1 /*
2  *  linux/kernel/compat.c
3  *
4  *  Kernel compatibililty routines for e.g. 32 bit syscall support
5  *  on 64 bit kernels.
6  *
7  *  Copyright (C) 2002-2003 Stephen Rothwell, IBM Corporation
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 as
11  *  published by the Free Software Foundation.
12  */
13
14 #include <linux/linkage.h>
15 #include <linux/compat.h>
16 #include <linux/errno.h>
17 #include <linux/time.h>
18 #include <linux/signal.h>
19 #include <linux/sched.h>        /* for MAX_SCHEDULE_TIMEOUT */
20 #include <linux/futex.h>        /* for FUTEX_WAIT */
21 #include <linux/syscalls.h>
22 #include <linux/unistd.h>
23 #include <linux/security.h>
24
25 #include <asm/uaccess.h>
26
27 int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
28 {
29         return (verify_area(VERIFY_READ, cts, sizeof(*cts)) ||
30                         __get_user(ts->tv_sec, &cts->tv_sec) ||
31                         __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
32 }
33
34 int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
35 {
36         return (verify_area(VERIFY_WRITE, cts, sizeof(*cts)) ||
37                         __put_user(ts->tv_sec, &cts->tv_sec) ||
38                         __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
39 }
40
41 static long compat_nanosleep_restart(struct restart_block *restart)
42 {
43         unsigned long expire = restart->arg0, now = jiffies;
44         struct compat_timespec __user *rmtp;
45
46         /* Did it expire while we handled signals? */
47         if (!time_after(expire, now))
48                 return 0;
49
50         current->state = TASK_INTERRUPTIBLE;
51         expire = schedule_timeout(expire - now);
52         if (expire == 0)
53                 return 0;
54
55         rmtp = (struct compat_timespec __user *)restart->arg1;
56         if (rmtp) {
57                 struct compat_timespec ct;
58                 struct timespec t;
59
60                 jiffies_to_timespec(expire, &t);
61                 ct.tv_sec = t.tv_sec;
62                 ct.tv_nsec = t.tv_nsec;
63                 if (copy_to_user(rmtp, &ct, sizeof(ct)))
64                         return -EFAULT;
65         }
66         /* The 'restart' block is already filled in */
67         return -ERESTART_RESTARTBLOCK;
68 }
69
70 asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
71                 struct compat_timespec __user *rmtp)
72 {
73         struct timespec t;
74         struct restart_block *restart;
75         unsigned long expire;
76
77         if (get_compat_timespec(&t, rqtp))
78                 return -EFAULT;
79
80         if ((t.tv_nsec >= 1000000000L) || (t.tv_nsec < 0) || (t.tv_sec < 0))
81                 return -EINVAL;
82
83         expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
84         current->state = TASK_INTERRUPTIBLE;
85         expire = schedule_timeout(expire);
86         if (expire == 0)
87                 return 0;
88
89         if (rmtp) {
90                 jiffies_to_timespec(expire, &t);
91                 if (put_compat_timespec(&t, rmtp))
92                         return -EFAULT;
93         }
94         restart = &current_thread_info()->restart_block;
95         restart->fn = compat_nanosleep_restart;
96         restart->arg0 = jiffies + expire;
97         restart->arg1 = (unsigned long) rmtp;
98         return -ERESTART_RESTARTBLOCK;
99 }
100
101 static inline long get_compat_itimerval(struct itimerval *o,
102                 struct compat_itimerval __user *i)
103 {
104         return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
105                 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
106                  __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
107                  __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
108                  __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
109 }
110
111 static inline long put_compat_itimerval(struct compat_itimerval __user *o,
112                 struct itimerval *i)
113 {
114         return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
115                 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
116                  __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
117                  __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
118                  __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
119 }
120
121 asmlinkage long compat_sys_getitimer(int which,
122                 struct compat_itimerval __user *it)
123 {
124         struct itimerval kit;
125         int error;
126
127         error = do_getitimer(which, &kit);
128         if (!error && put_compat_itimerval(it, &kit))
129                 error = -EFAULT;
130         return error;
131 }
132
133 asmlinkage long compat_sys_setitimer(int which,
134                 struct compat_itimerval __user *in,
135                 struct compat_itimerval __user *out)
136 {
137         struct itimerval kin, kout;
138         int error;
139
140         if (in) {
141                 if (get_compat_itimerval(&kin, in))
142                         return -EFAULT;
143         } else
144                 memset(&kin, 0, sizeof(kin));
145
146         error = do_setitimer(which, &kin, out ? &kout : NULL);
147         if (error || !out)
148                 return error;
149         if (put_compat_itimerval(out, &kout))
150                 return -EFAULT;
151         return 0;
152 }
153
154 asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
155 {
156         /*
157          *      In the SMP world we might just be unlucky and have one of
158          *      the times increment as we use it. Since the value is an
159          *      atomically safe type this is just fine. Conceptually its
160          *      as if the syscall took an instant longer to occur.
161          */
162         if (tbuf) {
163                 struct compat_tms tmp;
164                 struct task_struct *tsk = current;
165                 struct task_struct *t;
166                 cputime_t utime, stime, cutime, cstime;
167
168                 read_lock(&tasklist_lock);
169                 utime = tsk->signal->utime;
170                 stime = tsk->signal->stime;
171                 t = tsk;
172                 do {
173                         utime = cputime_add(utime, t->utime);
174                         stime = cputime_add(stime, t->stime);
175                         t = next_thread(t);
176                 } while (t != tsk);
177
178                 /*
179                  * While we have tasklist_lock read-locked, no dying thread
180                  * can be updating current->signal->[us]time.  Instead,
181                  * we got their counts included in the live thread loop.
182                  * However, another thread can come in right now and
183                  * do a wait call that updates current->signal->c[us]time.
184                  * To make sure we always see that pair updated atomically,
185                  * we take the siglock around fetching them.
186                  */
187                 spin_lock_irq(&tsk->sighand->siglock);
188                 cutime = tsk->signal->cutime;
189                 cstime = tsk->signal->cstime;
190                 spin_unlock_irq(&tsk->sighand->siglock);
191                 read_unlock(&tasklist_lock);
192
193                 tmp.tms_utime = compat_jiffies_to_clock_t(cputime_to_jiffies(utime));
194                 tmp.tms_stime = compat_jiffies_to_clock_t(cputime_to_jiffies(stime));
195                 tmp.tms_cutime = compat_jiffies_to_clock_t(cputime_to_jiffies(cutime));
196                 tmp.tms_cstime = compat_jiffies_to_clock_t(cputime_to_jiffies(cstime));
197                 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
198                         return -EFAULT;
199         }
200         return compat_jiffies_to_clock_t(jiffies);
201 }
202
203 /*
204  * Assumption: old_sigset_t and compat_old_sigset_t are both
205  * types that can be passed to put_user()/get_user().
206  */
207
208 asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
209 {
210         old_sigset_t s;
211         long ret;
212         mm_segment_t old_fs = get_fs();
213
214         set_fs(KERNEL_DS);
215         ret = sys_sigpending((old_sigset_t __user *) &s);
216         set_fs(old_fs);
217         if (ret == 0)
218                 ret = put_user(s, set);
219         return ret;
220 }
221
222 asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set,
223                 compat_old_sigset_t __user *oset)
224 {
225         old_sigset_t s;
226         long ret;
227         mm_segment_t old_fs;
228
229         if (set && get_user(s, set))
230                 return -EFAULT;
231         old_fs = get_fs();
232         set_fs(KERNEL_DS);
233         ret = sys_sigprocmask(how,
234                               set ? (old_sigset_t __user *) &s : NULL,
235                               oset ? (old_sigset_t __user *) &s : NULL);
236         set_fs(old_fs);
237         if (ret == 0)
238                 if (oset)
239                         ret = put_user(s, oset);
240         return ret;
241 }
242
243 #ifdef CONFIG_FUTEX
244 asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, int val,
245                 struct compat_timespec __user *utime, u32 __user *uaddr2,
246                 int val3)
247 {
248         struct timespec t;
249         unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
250         int val2 = 0;
251
252         if ((op == FUTEX_WAIT) && utime) {
253                 if (get_compat_timespec(&t, utime))
254                         return -EFAULT;
255                 timeout = timespec_to_jiffies(&t) + 1;
256         }
257         if (op >= FUTEX_REQUEUE)
258                 val2 = (int) (unsigned long) utime;
259
260         return do_futex((unsigned long)uaddr, op, val, timeout,
261                         (unsigned long)uaddr2, val2, val3);
262 }
263 #endif
264
265 asmlinkage long compat_sys_setrlimit(unsigned int resource,
266                 struct compat_rlimit __user *rlim)
267 {
268         struct rlimit r;
269         int ret;
270         mm_segment_t old_fs = get_fs ();
271
272         if (resource >= RLIM_NLIMITS) 
273                 return -EINVAL; 
274
275         if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
276             __get_user(r.rlim_cur, &rlim->rlim_cur) ||
277             __get_user(r.rlim_max, &rlim->rlim_max))
278                 return -EFAULT;
279
280         if (r.rlim_cur == COMPAT_RLIM_INFINITY)
281                 r.rlim_cur = RLIM_INFINITY;
282         if (r.rlim_max == COMPAT_RLIM_INFINITY)
283                 r.rlim_max = RLIM_INFINITY;
284         set_fs(KERNEL_DS);
285         ret = sys_setrlimit(resource, (struct rlimit __user *) &r);
286         set_fs(old_fs);
287         return ret;
288 }
289
290 #ifdef COMPAT_RLIM_OLD_INFINITY
291
292 asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
293                 struct compat_rlimit __user *rlim)
294 {
295         struct rlimit r;
296         int ret;
297         mm_segment_t old_fs = get_fs();
298
299         set_fs(KERNEL_DS);
300         ret = sys_old_getrlimit(resource, &r);
301         set_fs(old_fs);
302
303         if (!ret) {
304                 if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
305                         r.rlim_cur = COMPAT_RLIM_INFINITY;
306                 if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
307                         r.rlim_max = COMPAT_RLIM_INFINITY;
308
309                 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
310                     __put_user(r.rlim_cur, &rlim->rlim_cur) ||
311                     __put_user(r.rlim_max, &rlim->rlim_max))
312                         return -EFAULT;
313         }
314         return ret;
315 }
316
317 #endif
318
319 asmlinkage long compat_sys_getrlimit (unsigned int resource,
320                 struct compat_rlimit __user *rlim)
321 {
322         struct rlimit r;
323         int ret;
324         mm_segment_t old_fs = get_fs();
325
326         set_fs(KERNEL_DS);
327         ret = sys_getrlimit(resource, (struct rlimit __user *) &r);
328         set_fs(old_fs);
329         if (!ret) {
330                 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
331                         r.rlim_cur = COMPAT_RLIM_INFINITY;
332                 if (r.rlim_max > COMPAT_RLIM_INFINITY)
333                         r.rlim_max = COMPAT_RLIM_INFINITY;
334
335                 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
336                     __put_user(r.rlim_cur, &rlim->rlim_cur) ||
337                     __put_user(r.rlim_max, &rlim->rlim_max))
338                         return -EFAULT;
339         }
340         return ret;
341 }
342
343 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
344 {
345         if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
346             __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
347             __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
348             __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
349             __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
350             __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
351             __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
352             __put_user(r->ru_idrss, &ru->ru_idrss) ||
353             __put_user(r->ru_isrss, &ru->ru_isrss) ||
354             __put_user(r->ru_minflt, &ru->ru_minflt) ||
355             __put_user(r->ru_majflt, &ru->ru_majflt) ||
356             __put_user(r->ru_nswap, &ru->ru_nswap) ||
357             __put_user(r->ru_inblock, &ru->ru_inblock) ||
358             __put_user(r->ru_oublock, &ru->ru_oublock) ||
359             __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
360             __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
361             __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
362             __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
363             __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
364                 return -EFAULT;
365         return 0;
366 }
367
368 asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
369 {
370         struct rusage r;
371         int ret;
372         mm_segment_t old_fs = get_fs();
373
374         set_fs(KERNEL_DS);
375         ret = sys_getrusage(who, (struct rusage __user *) &r);
376         set_fs(old_fs);
377
378         if (ret)
379                 return ret;
380
381         if (put_compat_rusage(&r, ru))
382                 return -EFAULT;
383
384         return 0;
385 }
386
387 asmlinkage long
388 compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
389         struct compat_rusage __user *ru)
390 {
391         if (!ru) {
392                 return sys_wait4(pid, stat_addr, options, NULL);
393         } else {
394                 struct rusage r;
395                 int ret;
396                 unsigned int status;
397                 mm_segment_t old_fs = get_fs();
398
399                 set_fs (KERNEL_DS);
400                 ret = sys_wait4(pid,
401                                 (stat_addr ?
402                                  (unsigned int __user *) &status : NULL),
403                                 options, (struct rusage __user *) &r);
404                 set_fs (old_fs);
405
406                 if (ret > 0) {
407                         if (put_compat_rusage(&r, ru))
408                                 return -EFAULT;
409                         if (stat_addr && put_user(status, stat_addr))
410                                 return -EFAULT;
411                 }
412                 return ret;
413         }
414 }
415
416 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
417                                     unsigned len, cpumask_t *new_mask)
418 {
419         unsigned long *k;
420
421         if (len < sizeof(cpumask_t))
422                 memset(new_mask, 0, sizeof(cpumask_t));
423         else if (len > sizeof(cpumask_t))
424                 len = sizeof(cpumask_t);
425
426         k = cpus_addr(*new_mask);
427         return compat_get_bitmap(k, user_mask_ptr, len * 8);
428 }
429
430 asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
431                                              unsigned int len,
432                                              compat_ulong_t __user *user_mask_ptr)
433 {
434         cpumask_t new_mask;
435         int retval;
436
437         retval = compat_get_user_cpu_mask(user_mask_ptr, len, &new_mask);
438         if (retval)
439                 return retval;
440
441         return sched_setaffinity(pid, new_mask);
442 }
443
444 asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
445                                              compat_ulong_t __user *user_mask_ptr)
446 {
447         int ret;
448         cpumask_t mask;
449         unsigned long *k;
450         unsigned int min_length = sizeof(cpumask_t);
451
452         if (NR_CPUS <= BITS_PER_COMPAT_LONG)
453                 min_length = sizeof(compat_ulong_t);
454
455         if (len < min_length)
456                 return -EINVAL;
457
458         ret = sched_getaffinity(pid, &mask);
459         if (ret < 0)
460                 return ret;
461
462         k = cpus_addr(mask);
463         ret = compat_put_bitmap(user_mask_ptr, k, min_length * 8);
464         if (ret)
465                 return ret;
466
467         return min_length;
468 }
469
470 static int get_compat_itimerspec(struct itimerspec *dst, 
471                                  struct compat_itimerspec __user *src)
472
473         if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
474             get_compat_timespec(&dst->it_value, &src->it_value))
475                 return -EFAULT;
476         return 0;
477
478
479 static int put_compat_itimerspec(struct compat_itimerspec __user *dst, 
480                                  struct itimerspec *src)
481
482         if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
483             put_compat_timespec(&src->it_value, &dst->it_value))
484                 return -EFAULT;
485         return 0;
486
487
488 long compat_sys_timer_settime(timer_t timer_id, int flags,
489                           struct compat_itimerspec __user *new, 
490                           struct compat_itimerspec __user *old)
491
492         long err;
493         mm_segment_t oldfs;
494         struct itimerspec newts, oldts;
495
496         if (!new)
497                 return -EINVAL;
498         if (get_compat_itimerspec(&newts, new))
499                 return -EFAULT; 
500         oldfs = get_fs();
501         set_fs(KERNEL_DS);
502         err = sys_timer_settime(timer_id, flags,
503                                 (struct itimerspec __user *) &newts,
504                                 (struct itimerspec __user *) &oldts);
505         set_fs(oldfs); 
506         if (!err && old && put_compat_itimerspec(old, &oldts))
507                 return -EFAULT;
508         return err;
509
510
511 long compat_sys_timer_gettime(timer_t timer_id,
512                 struct compat_itimerspec __user *setting)
513
514         long err;
515         mm_segment_t oldfs;
516         struct itimerspec ts; 
517
518         oldfs = get_fs();
519         set_fs(KERNEL_DS);
520         err = sys_timer_gettime(timer_id,
521                                 (struct itimerspec __user *) &ts); 
522         set_fs(oldfs); 
523         if (!err && put_compat_itimerspec(setting, &ts))
524                 return -EFAULT;
525         return err;
526
527
528 long compat_sys_clock_settime(clockid_t which_clock,
529                 struct compat_timespec __user *tp)
530 {
531         long err;
532         mm_segment_t oldfs;
533         struct timespec ts; 
534
535         if (get_compat_timespec(&ts, tp))
536                 return -EFAULT; 
537         oldfs = get_fs();
538         set_fs(KERNEL_DS);      
539         err = sys_clock_settime(which_clock,
540                                 (struct timespec __user *) &ts);
541         set_fs(oldfs);
542         return err;
543
544
545 long compat_sys_clock_gettime(clockid_t which_clock,
546                 struct compat_timespec __user *tp)
547 {
548         long err;
549         mm_segment_t oldfs;
550         struct timespec ts; 
551
552         oldfs = get_fs();
553         set_fs(KERNEL_DS);
554         err = sys_clock_gettime(which_clock,
555                                 (struct timespec __user *) &ts);
556         set_fs(oldfs);
557         if (!err && put_compat_timespec(&ts, tp))
558                 return -EFAULT; 
559         return err;
560
561
562 long compat_sys_clock_getres(clockid_t which_clock,
563                 struct compat_timespec __user *tp)
564 {
565         long err;
566         mm_segment_t oldfs;
567         struct timespec ts; 
568
569         oldfs = get_fs();
570         set_fs(KERNEL_DS);
571         err = sys_clock_getres(which_clock,
572                                (struct timespec __user *) &ts);
573         set_fs(oldfs);
574         if (!err && tp && put_compat_timespec(&ts, tp))
575                 return -EFAULT; 
576         return err;
577
578
579 long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
580                             struct compat_timespec __user *rqtp,
581                             struct compat_timespec __user *rmtp)
582 {
583         long err;
584         mm_segment_t oldfs;
585         struct timespec in, out; 
586
587         if (get_compat_timespec(&in, rqtp)) 
588                 return -EFAULT;
589
590         oldfs = get_fs();
591         set_fs(KERNEL_DS);
592         err = sys_clock_nanosleep(which_clock, flags,
593                                   (struct timespec __user *) &in,
594                                   (struct timespec __user *) &out);
595         set_fs(oldfs);
596         if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
597             put_compat_timespec(&out, rmtp))
598                 return -EFAULT;
599         return err;     
600
601
602 /* timer_create is architecture specific because it needs sigevent conversion */
603
604 long compat_get_bitmap(unsigned long *mask, compat_ulong_t __user *umask,
605                        unsigned long bitmap_size)
606 {
607         int i, j;
608         unsigned long m;
609         compat_ulong_t um;
610         unsigned long nr_compat_longs;
611
612         /* align bitmap up to nearest compat_long_t boundary */
613         bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
614
615         if (verify_area(VERIFY_READ, umask, bitmap_size / 8))
616                 return -EFAULT;
617
618         nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
619
620         for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
621                 m = 0;
622
623                 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
624                         /*
625                          * We dont want to read past the end of the userspace
626                          * bitmap. We must however ensure the end of the
627                          * kernel bitmap is zeroed.
628                          */
629                         if (nr_compat_longs-- > 0) {
630                                 if (__get_user(um, umask))
631                                         return -EFAULT;
632                         } else {
633                                 um = 0;
634                         }
635
636                         umask++;
637                         m |= (long)um << (j * BITS_PER_COMPAT_LONG);
638                 }
639                 *mask++ = m;
640         }
641
642         return 0;
643 }
644
645 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
646                        unsigned long bitmap_size)
647 {
648         int i, j;
649         unsigned long m;
650         compat_ulong_t um;
651         unsigned long nr_compat_longs;
652
653         /* align bitmap up to nearest compat_long_t boundary */
654         bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
655
656         if (verify_area(VERIFY_WRITE, umask, bitmap_size / 8))
657                 return -EFAULT;
658
659         nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
660
661         for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
662                 m = *mask++;
663
664                 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
665                         um = m;
666
667                         /*
668                          * We dont want to write past the end of the userspace
669                          * bitmap.
670                          */
671                         if (nr_compat_longs-- > 0) {
672                                 if (__put_user(um, umask))
673                                         return -EFAULT;
674                         }
675
676                         umask++;
677                         m >>= 4*sizeof(um);
678                         m >>= 4*sizeof(um);
679                 }
680         }
681
682         return 0;
683 }
684
685 void
686 sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
687 {
688         switch (_NSIG_WORDS) {
689 #if defined (__COMPAT_ENDIAN_SWAP__)
690         case 4: set->sig[3] = compat->sig[7] | (((long)compat->sig[6]) << 32 );
691         case 3: set->sig[2] = compat->sig[5] | (((long)compat->sig[4]) << 32 );
692         case 2: set->sig[1] = compat->sig[3] | (((long)compat->sig[2]) << 32 );
693         case 1: set->sig[0] = compat->sig[1] | (((long)compat->sig[0]) << 32 );
694 #else
695         case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
696         case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
697         case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
698         case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
699 #endif
700         }
701 }
702
703 asmlinkage long
704 compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
705                 struct compat_siginfo __user *uinfo,
706                 struct compat_timespec __user *uts, compat_size_t sigsetsize)
707 {
708         compat_sigset_t s32;
709         sigset_t s;
710         int sig;
711         struct timespec t;
712         siginfo_t info;
713         long ret, timeout = 0;
714
715         if (sigsetsize != sizeof(sigset_t))
716                 return -EINVAL;
717
718         if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
719                 return -EFAULT;
720         sigset_from_compat(&s, &s32);
721         sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
722         signotset(&s);
723
724         if (uts) {
725                 if (get_compat_timespec (&t, uts))
726                         return -EFAULT;
727                 if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0
728                                 || t.tv_sec < 0)
729                         return -EINVAL;
730         }
731
732         spin_lock_irq(&current->sighand->siglock);
733         sig = dequeue_signal(current, &s, &info);
734         if (!sig) {
735                 timeout = MAX_SCHEDULE_TIMEOUT;
736                 if (uts)
737                         timeout = timespec_to_jiffies(&t)
738                                 +(t.tv_sec || t.tv_nsec);
739                 if (timeout) {
740                         current->real_blocked = current->blocked;
741                         sigandsets(&current->blocked, &current->blocked, &s);
742
743                         recalc_sigpending();
744                         spin_unlock_irq(&current->sighand->siglock);
745
746                         current->state = TASK_INTERRUPTIBLE;
747                         timeout = schedule_timeout(timeout);
748
749                         spin_lock_irq(&current->sighand->siglock);
750                         sig = dequeue_signal(current, &s, &info);
751                         current->blocked = current->real_blocked;
752                         siginitset(&current->real_blocked, 0);
753                         recalc_sigpending();
754                 }
755         }
756         spin_unlock_irq(&current->sighand->siglock);
757
758         if (sig) {
759                 ret = sig;
760                 if (uinfo) {
761                         if (copy_siginfo_to_user32(uinfo, &info))
762                                 ret = -EFAULT;
763                 }
764         }else {
765                 ret = timeout?-EINTR:-EAGAIN;
766         }
767         return ret;
768
769 }
770
771 #ifdef __ARCH_WANT_COMPAT_SYS_TIME
772
773 /* compat_time_t is a 32 bit "long" and needs to get converted. */
774
775 asmlinkage long compat_sys_time(compat_time_t __user * tloc)
776 {
777         compat_time_t i;
778         struct timeval tv;
779
780         do_gettimeofday(&tv);
781         i = tv.tv_sec;
782
783         if (tloc) {
784                 if (put_user(i,tloc))
785                         i = -EFAULT;
786         }
787         return i;
788 }
789
790 asmlinkage long compat_sys_stime(compat_time_t __user *tptr)
791 {
792         struct timespec tv;
793         int err;
794
795         if (get_user(tv.tv_sec, tptr))
796                 return -EFAULT;
797
798         tv.tv_nsec = 0;
799
800         err = security_settime(&tv, NULL);
801         if (err)
802                 return err;
803
804         do_settimeofday(&tv);
805         return 0;
806 }
807
808 #endif /* __ARCH_WANT_COMPAT_SYS_TIME */