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