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