set dev usb
[cascardo/linux.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/syscalls.h>
21 #include <linux/unistd.h>
22 #include <linux/security.h>
23 #include <linux/timex.h>
24 #include <linux/export.h>
25 #include <linux/migrate.h>
26 #include <linux/posix-timers.h>
27 #include <linux/times.h>
28 #include <linux/ptrace.h>
29 #include <linux/prctl.h>
30 #include <linux/gfp.h>
31
32 #include <asm/uaccess.h>
33
34 /*
35  * Get/set struct timeval with struct timespec on the native side
36  */
37 static int compat_get_timeval_convert(struct timespec *o,
38                                       struct compat_timeval __user *i)
39 {
40         long usec;
41
42         if (get_user(o->tv_sec, &i->tv_sec) ||
43             get_user(usec, &i->tv_usec))
44                 return -EFAULT;
45         o->tv_nsec = usec * 1000;
46         return 0;
47 }
48
49 static int compat_put_timeval_convert(struct compat_timeval __user *o,
50                                       struct timeval *i)
51 {
52         return (put_user(i->tv_sec, &o->tv_sec) ||
53                 put_user(i->tv_usec, &o->tv_usec)) ? -EFAULT : 0;
54 }
55
56 static int compat_get_timex(struct timex *txc, struct compat_timex __user *utp)
57 {
58         memset(txc, 0, sizeof(struct timex));
59
60         if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
61                         __get_user(txc->modes, &utp->modes) ||
62                         __get_user(txc->offset, &utp->offset) ||
63                         __get_user(txc->freq, &utp->freq) ||
64                         __get_user(txc->maxerror, &utp->maxerror) ||
65                         __get_user(txc->esterror, &utp->esterror) ||
66                         __get_user(txc->status, &utp->status) ||
67                         __get_user(txc->constant, &utp->constant) ||
68                         __get_user(txc->precision, &utp->precision) ||
69                         __get_user(txc->tolerance, &utp->tolerance) ||
70                         __get_user(txc->time.tv_sec, &utp->time.tv_sec) ||
71                         __get_user(txc->time.tv_usec, &utp->time.tv_usec) ||
72                         __get_user(txc->tick, &utp->tick) ||
73                         __get_user(txc->ppsfreq, &utp->ppsfreq) ||
74                         __get_user(txc->jitter, &utp->jitter) ||
75                         __get_user(txc->shift, &utp->shift) ||
76                         __get_user(txc->stabil, &utp->stabil) ||
77                         __get_user(txc->jitcnt, &utp->jitcnt) ||
78                         __get_user(txc->calcnt, &utp->calcnt) ||
79                         __get_user(txc->errcnt, &utp->errcnt) ||
80                         __get_user(txc->stbcnt, &utp->stbcnt))
81                 return -EFAULT;
82
83         return 0;
84 }
85
86 static int compat_put_timex(struct compat_timex __user *utp, struct timex *txc)
87 {
88         if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
89                         __put_user(txc->modes, &utp->modes) ||
90                         __put_user(txc->offset, &utp->offset) ||
91                         __put_user(txc->freq, &utp->freq) ||
92                         __put_user(txc->maxerror, &utp->maxerror) ||
93                         __put_user(txc->esterror, &utp->esterror) ||
94                         __put_user(txc->status, &utp->status) ||
95                         __put_user(txc->constant, &utp->constant) ||
96                         __put_user(txc->precision, &utp->precision) ||
97                         __put_user(txc->tolerance, &utp->tolerance) ||
98                         __put_user(txc->time.tv_sec, &utp->time.tv_sec) ||
99                         __put_user(txc->time.tv_usec, &utp->time.tv_usec) ||
100                         __put_user(txc->tick, &utp->tick) ||
101                         __put_user(txc->ppsfreq, &utp->ppsfreq) ||
102                         __put_user(txc->jitter, &utp->jitter) ||
103                         __put_user(txc->shift, &utp->shift) ||
104                         __put_user(txc->stabil, &utp->stabil) ||
105                         __put_user(txc->jitcnt, &utp->jitcnt) ||
106                         __put_user(txc->calcnt, &utp->calcnt) ||
107                         __put_user(txc->errcnt, &utp->errcnt) ||
108                         __put_user(txc->stbcnt, &utp->stbcnt) ||
109                         __put_user(txc->tai, &utp->tai))
110                 return -EFAULT;
111         return 0;
112 }
113
114 asmlinkage long compat_sys_gettimeofday(struct compat_timeval __user *tv,
115                 struct timezone __user *tz)
116 {
117         if (tv) {
118                 struct timeval ktv;
119                 do_gettimeofday(&ktv);
120                 if (compat_put_timeval_convert(tv, &ktv))
121                         return -EFAULT;
122         }
123         if (tz) {
124                 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
125                         return -EFAULT;
126         }
127
128         return 0;
129 }
130
131 asmlinkage long compat_sys_settimeofday(struct compat_timeval __user *tv,
132                 struct timezone __user *tz)
133 {
134         struct timespec kts;
135         struct timezone ktz;
136
137         if (tv) {
138                 if (compat_get_timeval_convert(&kts, tv))
139                         return -EFAULT;
140         }
141         if (tz) {
142                 if (copy_from_user(&ktz, tz, sizeof(ktz)))
143                         return -EFAULT;
144         }
145
146         return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
147 }
148
149 int get_compat_timeval(struct timeval *tv, const struct compat_timeval __user *ctv)
150 {
151         return (!access_ok(VERIFY_READ, ctv, sizeof(*ctv)) ||
152                         __get_user(tv->tv_sec, &ctv->tv_sec) ||
153                         __get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
154 }
155 EXPORT_SYMBOL_GPL(get_compat_timeval);
156
157 int put_compat_timeval(const struct timeval *tv, struct compat_timeval __user *ctv)
158 {
159         return (!access_ok(VERIFY_WRITE, ctv, sizeof(*ctv)) ||
160                         __put_user(tv->tv_sec, &ctv->tv_sec) ||
161                         __put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
162 }
163 EXPORT_SYMBOL_GPL(put_compat_timeval);
164
165 int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
166 {
167         return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
168                         __get_user(ts->tv_sec, &cts->tv_sec) ||
169                         __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
170 }
171 EXPORT_SYMBOL_GPL(get_compat_timespec);
172
173 int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
174 {
175         return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
176                         __put_user(ts->tv_sec, &cts->tv_sec) ||
177                         __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
178 }
179 EXPORT_SYMBOL_GPL(put_compat_timespec);
180
181 int compat_get_timeval(struct timeval *tv, const void __user *utv)
182 {
183         if (COMPAT_USE_64BIT_TIME)
184                 return copy_from_user(tv, utv, sizeof *tv) ? -EFAULT : 0;
185         else
186                 return get_compat_timeval(tv, utv);
187 }
188 EXPORT_SYMBOL_GPL(compat_get_timeval);
189
190 int compat_put_timeval(const struct timeval *tv, void __user *utv)
191 {
192         if (COMPAT_USE_64BIT_TIME)
193                 return copy_to_user(utv, tv, sizeof *tv) ? -EFAULT : 0;
194         else
195                 return put_compat_timeval(tv, utv);
196 }
197 EXPORT_SYMBOL_GPL(compat_put_timeval);
198
199 int compat_get_timespec(struct timespec *ts, const void __user *uts)
200 {
201         if (COMPAT_USE_64BIT_TIME)
202                 return copy_from_user(ts, uts, sizeof *ts) ? -EFAULT : 0;
203         else
204                 return get_compat_timespec(ts, uts);
205 }
206 EXPORT_SYMBOL_GPL(compat_get_timespec);
207
208 int compat_put_timespec(const struct timespec *ts, void __user *uts)
209 {
210         if (COMPAT_USE_64BIT_TIME)
211                 return copy_to_user(uts, ts, sizeof *ts) ? -EFAULT : 0;
212         else
213                 return put_compat_timespec(ts, uts);
214 }
215 EXPORT_SYMBOL_GPL(compat_put_timespec);
216
217 static long compat_nanosleep_restart(struct restart_block *restart)
218 {
219         struct compat_timespec __user *rmtp;
220         struct timespec rmt;
221         mm_segment_t oldfs;
222         long ret;
223
224         restart->nanosleep.rmtp = (struct timespec __user *) &rmt;
225         oldfs = get_fs();
226         set_fs(KERNEL_DS);
227         ret = hrtimer_nanosleep_restart(restart);
228         set_fs(oldfs);
229
230         if (ret) {
231                 rmtp = restart->nanosleep.compat_rmtp;
232
233                 if (rmtp && put_compat_timespec(&rmt, rmtp))
234                         return -EFAULT;
235         }
236
237         return ret;
238 }
239
240 asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
241                                      struct compat_timespec __user *rmtp)
242 {
243         struct timespec tu, rmt;
244         mm_segment_t oldfs;
245         long ret;
246
247         if (get_compat_timespec(&tu, rqtp))
248                 return -EFAULT;
249
250         if (!timespec_valid(&tu))
251                 return -EINVAL;
252
253         oldfs = get_fs();
254         set_fs(KERNEL_DS);
255         ret = hrtimer_nanosleep(&tu,
256                                 rmtp ? (struct timespec __user *)&rmt : NULL,
257                                 HRTIMER_MODE_REL, CLOCK_MONOTONIC);
258         set_fs(oldfs);
259
260         if (ret) {
261                 struct restart_block *restart
262                         = &current_thread_info()->restart_block;
263
264                 restart->fn = compat_nanosleep_restart;
265                 restart->nanosleep.compat_rmtp = rmtp;
266
267                 if (rmtp && put_compat_timespec(&rmt, rmtp))
268                         return -EFAULT;
269         }
270
271         return ret;
272 }
273
274 static inline long get_compat_itimerval(struct itimerval *o,
275                 struct compat_itimerval __user *i)
276 {
277         return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
278                 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
279                  __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
280                  __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
281                  __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
282 }
283
284 static inline long put_compat_itimerval(struct compat_itimerval __user *o,
285                 struct itimerval *i)
286 {
287         return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
288                 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
289                  __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
290                  __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
291                  __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
292 }
293
294 asmlinkage long compat_sys_getitimer(int which,
295                 struct compat_itimerval __user *it)
296 {
297         struct itimerval kit;
298         int error;
299
300         error = do_getitimer(which, &kit);
301         if (!error && put_compat_itimerval(it, &kit))
302                 error = -EFAULT;
303         return error;
304 }
305
306 asmlinkage long compat_sys_setitimer(int which,
307                 struct compat_itimerval __user *in,
308                 struct compat_itimerval __user *out)
309 {
310         struct itimerval kin, kout;
311         int error;
312
313         if (in) {
314                 if (get_compat_itimerval(&kin, in))
315                         return -EFAULT;
316         } else
317                 memset(&kin, 0, sizeof(kin));
318
319         error = do_setitimer(which, &kin, out ? &kout : NULL);
320         if (error || !out)
321                 return error;
322         if (put_compat_itimerval(out, &kout))
323                 return -EFAULT;
324         return 0;
325 }
326
327 static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
328 {
329         return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
330 }
331
332 asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
333 {
334         if (tbuf) {
335                 struct tms tms;
336                 struct compat_tms tmp;
337
338                 do_sys_times(&tms);
339                 /* Convert our struct tms to the compat version. */
340                 tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
341                 tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
342                 tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
343                 tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
344                 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
345                         return -EFAULT;
346         }
347         force_successful_syscall_return();
348         return compat_jiffies_to_clock_t(jiffies);
349 }
350
351 #ifdef __ARCH_WANT_SYS_SIGPENDING
352
353 /*
354  * Assumption: old_sigset_t and compat_old_sigset_t are both
355  * types that can be passed to put_user()/get_user().
356  */
357
358 asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
359 {
360         old_sigset_t s;
361         long ret;
362         mm_segment_t old_fs = get_fs();
363
364         set_fs(KERNEL_DS);
365         ret = sys_sigpending((old_sigset_t __user *) &s);
366         set_fs(old_fs);
367         if (ret == 0)
368                 ret = put_user(s, set);
369         return ret;
370 }
371
372 #endif
373
374 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
375
376 /*
377  * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
378  * blocked set of signals to the supplied signal set
379  */
380 static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
381 {
382         memcpy(blocked->sig, &set, sizeof(set));
383 }
384
385 asmlinkage long compat_sys_sigprocmask(int how,
386                                        compat_old_sigset_t __user *nset,
387                                        compat_old_sigset_t __user *oset)
388 {
389         old_sigset_t old_set, new_set;
390         sigset_t new_blocked;
391
392         old_set = current->blocked.sig[0];
393
394         if (nset) {
395                 if (get_user(new_set, nset))
396                         return -EFAULT;
397                 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
398
399                 new_blocked = current->blocked;
400
401                 switch (how) {
402                 case SIG_BLOCK:
403                         sigaddsetmask(&new_blocked, new_set);
404                         break;
405                 case SIG_UNBLOCK:
406                         sigdelsetmask(&new_blocked, new_set);
407                         break;
408                 case SIG_SETMASK:
409                         compat_sig_setmask(&new_blocked, new_set);
410                         break;
411                 default:
412                         return -EINVAL;
413                 }
414
415                 set_current_blocked(&new_blocked);
416         }
417
418         if (oset) {
419                 if (put_user(old_set, oset))
420                         return -EFAULT;
421         }
422
423         return 0;
424 }
425
426 #endif
427
428 asmlinkage long compat_sys_setrlimit(unsigned int resource,
429                 struct compat_rlimit __user *rlim)
430 {
431         struct rlimit r;
432
433         if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
434             __get_user(r.rlim_cur, &rlim->rlim_cur) ||
435             __get_user(r.rlim_max, &rlim->rlim_max))
436                 return -EFAULT;
437
438         if (r.rlim_cur == COMPAT_RLIM_INFINITY)
439                 r.rlim_cur = RLIM_INFINITY;
440         if (r.rlim_max == COMPAT_RLIM_INFINITY)
441                 r.rlim_max = RLIM_INFINITY;
442         return do_prlimit(current, resource, &r, NULL);
443 }
444
445 #ifdef COMPAT_RLIM_OLD_INFINITY
446
447 asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
448                 struct compat_rlimit __user *rlim)
449 {
450         struct rlimit r;
451         int ret;
452         mm_segment_t old_fs = get_fs();
453
454         set_fs(KERNEL_DS);
455         ret = sys_old_getrlimit(resource, &r);
456         set_fs(old_fs);
457
458         if (!ret) {
459                 if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
460                         r.rlim_cur = COMPAT_RLIM_INFINITY;
461                 if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
462                         r.rlim_max = COMPAT_RLIM_INFINITY;
463
464                 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
465                     __put_user(r.rlim_cur, &rlim->rlim_cur) ||
466                     __put_user(r.rlim_max, &rlim->rlim_max))
467                         return -EFAULT;
468         }
469         return ret;
470 }
471
472 #endif
473
474 asmlinkage long compat_sys_getrlimit(unsigned int resource,
475                 struct compat_rlimit __user *rlim)
476 {
477         struct rlimit r;
478         int ret;
479
480         ret = do_prlimit(current, resource, NULL, &r);
481         if (!ret) {
482                 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
483                         r.rlim_cur = COMPAT_RLIM_INFINITY;
484                 if (r.rlim_max > COMPAT_RLIM_INFINITY)
485                         r.rlim_max = COMPAT_RLIM_INFINITY;
486
487                 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
488                     __put_user(r.rlim_cur, &rlim->rlim_cur) ||
489                     __put_user(r.rlim_max, &rlim->rlim_max))
490                         return -EFAULT;
491         }
492         return ret;
493 }
494
495 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
496 {
497         if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
498             __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
499             __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
500             __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
501             __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
502             __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
503             __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
504             __put_user(r->ru_idrss, &ru->ru_idrss) ||
505             __put_user(r->ru_isrss, &ru->ru_isrss) ||
506             __put_user(r->ru_minflt, &ru->ru_minflt) ||
507             __put_user(r->ru_majflt, &ru->ru_majflt) ||
508             __put_user(r->ru_nswap, &ru->ru_nswap) ||
509             __put_user(r->ru_inblock, &ru->ru_inblock) ||
510             __put_user(r->ru_oublock, &ru->ru_oublock) ||
511             __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
512             __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
513             __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
514             __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
515             __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
516                 return -EFAULT;
517         return 0;
518 }
519
520 asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
521 {
522         struct rusage r;
523         int ret;
524         mm_segment_t old_fs = get_fs();
525
526         set_fs(KERNEL_DS);
527         ret = sys_getrusage(who, (struct rusage __user *) &r);
528         set_fs(old_fs);
529
530         if (ret)
531                 return ret;
532
533         if (put_compat_rusage(&r, ru))
534                 return -EFAULT;
535
536         return 0;
537 }
538
539 asmlinkage long
540 compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
541         struct compat_rusage __user *ru)
542 {
543         if (!ru) {
544                 return sys_wait4(pid, stat_addr, options, NULL);
545         } else {
546                 struct rusage r;
547                 int ret;
548                 unsigned int status;
549                 mm_segment_t old_fs = get_fs();
550
551                 set_fs (KERNEL_DS);
552                 ret = sys_wait4(pid,
553                                 (stat_addr ?
554                                  (unsigned int __user *) &status : NULL),
555                                 options, (struct rusage __user *) &r);
556                 set_fs (old_fs);
557
558                 if (ret > 0) {
559                         if (put_compat_rusage(&r, ru))
560                                 return -EFAULT;
561                         if (stat_addr && put_user(status, stat_addr))
562                                 return -EFAULT;
563                 }
564                 return ret;
565         }
566 }
567
568 asmlinkage long compat_sys_waitid(int which, compat_pid_t pid,
569                 struct compat_siginfo __user *uinfo, int options,
570                 struct compat_rusage __user *uru)
571 {
572         siginfo_t info;
573         struct rusage ru;
574         long ret;
575         mm_segment_t old_fs = get_fs();
576
577         memset(&info, 0, sizeof(info));
578
579         set_fs(KERNEL_DS);
580         ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
581                          uru ? (struct rusage __user *)&ru : NULL);
582         set_fs(old_fs);
583
584         if ((ret < 0) || (info.si_signo == 0))
585                 return ret;
586
587         if (uru) {
588                 ret = put_compat_rusage(&ru, uru);
589                 if (ret)
590                         return ret;
591         }
592
593         BUG_ON(info.si_code & __SI_MASK);
594         info.si_code |= __SI_CHLD;
595         return copy_siginfo_to_user32(uinfo, &info);
596 }
597
598 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
599                                     unsigned len, struct cpumask *new_mask)
600 {
601         unsigned long *k;
602
603         if (len < cpumask_size())
604                 memset(new_mask, 0, cpumask_size());
605         else if (len > cpumask_size())
606                 len = cpumask_size();
607
608         k = cpumask_bits(new_mask);
609         return compat_get_bitmap(k, user_mask_ptr, len * 8);
610 }
611
612 asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
613                                              unsigned int len,
614                                              compat_ulong_t __user *user_mask_ptr)
615 {
616         cpumask_var_t new_mask;
617         int retval;
618
619         if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
620                 return -ENOMEM;
621
622         retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
623         if (retval)
624                 goto out;
625
626         retval = sched_setaffinity(pid, new_mask);
627 out:
628         free_cpumask_var(new_mask);
629         return retval;
630 }
631
632 asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
633                                              compat_ulong_t __user *user_mask_ptr)
634 {
635         int ret;
636         cpumask_var_t mask;
637
638         if ((len * BITS_PER_BYTE) < nr_cpu_ids)
639                 return -EINVAL;
640         if (len & (sizeof(compat_ulong_t)-1))
641                 return -EINVAL;
642
643         if (!alloc_cpumask_var(&mask, GFP_KERNEL))
644                 return -ENOMEM;
645
646         ret = sched_getaffinity(pid, mask);
647         if (ret == 0) {
648                 size_t retlen = min_t(size_t, len, cpumask_size());
649
650                 if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
651                         ret = -EFAULT;
652                 else
653                         ret = retlen;
654         }
655         free_cpumask_var(mask);
656
657         return ret;
658 }
659
660 int get_compat_itimerspec(struct itimerspec *dst,
661                           const struct compat_itimerspec __user *src)
662 {
663         if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
664             get_compat_timespec(&dst->it_value, &src->it_value))
665                 return -EFAULT;
666         return 0;
667 }
668
669 int put_compat_itimerspec(struct compat_itimerspec __user *dst,
670                           const struct itimerspec *src)
671 {
672         if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
673             put_compat_timespec(&src->it_value, &dst->it_value))
674                 return -EFAULT;
675         return 0;
676 }
677
678 long compat_sys_timer_create(clockid_t which_clock,
679                         struct compat_sigevent __user *timer_event_spec,
680                         timer_t __user *created_timer_id)
681 {
682         struct sigevent __user *event = NULL;
683
684         if (timer_event_spec) {
685                 struct sigevent kevent;
686
687                 event = compat_alloc_user_space(sizeof(*event));
688                 if (get_compat_sigevent(&kevent, timer_event_spec) ||
689                     copy_to_user(event, &kevent, sizeof(*event)))
690                         return -EFAULT;
691         }
692
693         return sys_timer_create(which_clock, event, created_timer_id);
694 }
695
696 long compat_sys_timer_settime(timer_t timer_id, int flags,
697                           struct compat_itimerspec __user *new,
698                           struct compat_itimerspec __user *old)
699 {
700         long err;
701         mm_segment_t oldfs;
702         struct itimerspec newts, oldts;
703
704         if (!new)
705                 return -EINVAL;
706         if (get_compat_itimerspec(&newts, new))
707                 return -EFAULT;
708         oldfs = get_fs();
709         set_fs(KERNEL_DS);
710         err = sys_timer_settime(timer_id, flags,
711                                 (struct itimerspec __user *) &newts,
712                                 (struct itimerspec __user *) &oldts);
713         set_fs(oldfs);
714         if (!err && old && put_compat_itimerspec(old, &oldts))
715                 return -EFAULT;
716         return err;
717 }
718
719 long compat_sys_timer_gettime(timer_t timer_id,
720                 struct compat_itimerspec __user *setting)
721 {
722         long err;
723         mm_segment_t oldfs;
724         struct itimerspec ts;
725
726         oldfs = get_fs();
727         set_fs(KERNEL_DS);
728         err = sys_timer_gettime(timer_id,
729                                 (struct itimerspec __user *) &ts);
730         set_fs(oldfs);
731         if (!err && put_compat_itimerspec(setting, &ts))
732                 return -EFAULT;
733         return err;
734 }
735
736 long compat_sys_clock_settime(clockid_t which_clock,
737                 struct compat_timespec __user *tp)
738 {
739         long err;
740         mm_segment_t oldfs;
741         struct timespec ts;
742
743         if (get_compat_timespec(&ts, tp))
744                 return -EFAULT;
745         oldfs = get_fs();
746         set_fs(KERNEL_DS);
747         err = sys_clock_settime(which_clock,
748                                 (struct timespec __user *) &ts);
749         set_fs(oldfs);
750         return err;
751 }
752
753 long compat_sys_clock_gettime(clockid_t which_clock,
754                 struct compat_timespec __user *tp)
755 {
756         long err;
757         mm_segment_t oldfs;
758         struct timespec ts;
759
760         oldfs = get_fs();
761         set_fs(KERNEL_DS);
762         err = sys_clock_gettime(which_clock,
763                                 (struct timespec __user *) &ts);
764         set_fs(oldfs);
765         if (!err && put_compat_timespec(&ts, tp))
766                 return -EFAULT;
767         return err;
768 }
769
770 long compat_sys_clock_adjtime(clockid_t which_clock,
771                 struct compat_timex __user *utp)
772 {
773         struct timex txc;
774         mm_segment_t oldfs;
775         int err, ret;
776
777         err = compat_get_timex(&txc, utp);
778         if (err)
779                 return err;
780
781         oldfs = get_fs();
782         set_fs(KERNEL_DS);
783         ret = sys_clock_adjtime(which_clock, (struct timex __user *) &txc);
784         set_fs(oldfs);
785
786         err = compat_put_timex(utp, &txc);
787         if (err)
788                 return err;
789
790         return ret;
791 }
792
793 long compat_sys_clock_getres(clockid_t which_clock,
794                 struct compat_timespec __user *tp)
795 {
796         long err;
797         mm_segment_t oldfs;
798         struct timespec ts;
799
800         oldfs = get_fs();
801         set_fs(KERNEL_DS);
802         err = sys_clock_getres(which_clock,
803                                (struct timespec __user *) &ts);
804         set_fs(oldfs);
805         if (!err && tp && put_compat_timespec(&ts, tp))
806                 return -EFAULT;
807         return err;
808 }
809
810 static long compat_clock_nanosleep_restart(struct restart_block *restart)
811 {
812         long err;
813         mm_segment_t oldfs;
814         struct timespec tu;
815         struct compat_timespec *rmtp = restart->nanosleep.compat_rmtp;
816
817         restart->nanosleep.rmtp = (struct timespec __user *) &tu;
818         oldfs = get_fs();
819         set_fs(KERNEL_DS);
820         err = clock_nanosleep_restart(restart);
821         set_fs(oldfs);
822
823         if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
824             put_compat_timespec(&tu, rmtp))
825                 return -EFAULT;
826
827         if (err == -ERESTART_RESTARTBLOCK) {
828                 restart->fn = compat_clock_nanosleep_restart;
829                 restart->nanosleep.compat_rmtp = rmtp;
830         }
831         return err;
832 }
833
834 long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
835                             struct compat_timespec __user *rqtp,
836                             struct compat_timespec __user *rmtp)
837 {
838         long err;
839         mm_segment_t oldfs;
840         struct timespec in, out;
841         struct restart_block *restart;
842
843         if (get_compat_timespec(&in, rqtp))
844                 return -EFAULT;
845
846         oldfs = get_fs();
847         set_fs(KERNEL_DS);
848         err = sys_clock_nanosleep(which_clock, flags,
849                                   (struct timespec __user *) &in,
850                                   (struct timespec __user *) &out);
851         set_fs(oldfs);
852
853         if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
854             put_compat_timespec(&out, rmtp))
855                 return -EFAULT;
856
857         if (err == -ERESTART_RESTARTBLOCK) {
858                 restart = &current_thread_info()->restart_block;
859                 restart->fn = compat_clock_nanosleep_restart;
860                 restart->nanosleep.compat_rmtp = rmtp;
861         }
862         return err;
863 }
864
865 /*
866  * We currently only need the following fields from the sigevent
867  * structure: sigev_value, sigev_signo, sig_notify and (sometimes
868  * sigev_notify_thread_id).  The others are handled in user mode.
869  * We also assume that copying sigev_value.sival_int is sufficient
870  * to keep all the bits of sigev_value.sival_ptr intact.
871  */
872 int get_compat_sigevent(struct sigevent *event,
873                 const struct compat_sigevent __user *u_event)
874 {
875         memset(event, 0, sizeof(*event));
876         return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
877                 __get_user(event->sigev_value.sival_int,
878                         &u_event->sigev_value.sival_int) ||
879                 __get_user(event->sigev_signo, &u_event->sigev_signo) ||
880                 __get_user(event->sigev_notify, &u_event->sigev_notify) ||
881                 __get_user(event->sigev_notify_thread_id,
882                         &u_event->sigev_notify_thread_id))
883                 ? -EFAULT : 0;
884 }
885
886 long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
887                        unsigned long bitmap_size)
888 {
889         int i, j;
890         unsigned long m;
891         compat_ulong_t um;
892         unsigned long nr_compat_longs;
893
894         /* align bitmap up to nearest compat_long_t boundary */
895         bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
896
897         if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
898                 return -EFAULT;
899
900         nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
901
902         for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
903                 m = 0;
904
905                 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
906                         /*
907                          * We dont want to read past the end of the userspace
908                          * bitmap. We must however ensure the end of the
909                          * kernel bitmap is zeroed.
910                          */
911                         if (nr_compat_longs-- > 0) {
912                                 if (__get_user(um, umask))
913                                         return -EFAULT;
914                         } else {
915                                 um = 0;
916                         }
917
918                         umask++;
919                         m |= (long)um << (j * BITS_PER_COMPAT_LONG);
920                 }
921                 *mask++ = m;
922         }
923
924         return 0;
925 }
926
927 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
928                        unsigned long bitmap_size)
929 {
930         int i, j;
931         unsigned long m;
932         compat_ulong_t um;
933         unsigned long nr_compat_longs;
934
935         /* align bitmap up to nearest compat_long_t boundary */
936         bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
937
938         if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
939                 return -EFAULT;
940
941         nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
942
943         for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
944                 m = *mask++;
945
946                 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
947                         um = m;
948
949                         /*
950                          * We dont want to write past the end of the userspace
951                          * bitmap.
952                          */
953                         if (nr_compat_longs-- > 0) {
954                                 if (__put_user(um, umask))
955                                         return -EFAULT;
956                         }
957
958                         umask++;
959                         m >>= 4*sizeof(um);
960                         m >>= 4*sizeof(um);
961                 }
962         }
963
964         return 0;
965 }
966
967 void
968 sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
969 {
970         switch (_NSIG_WORDS) {
971         case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
972         case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
973         case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
974         case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
975         }
976 }
977 EXPORT_SYMBOL_GPL(sigset_from_compat);
978
979 asmlinkage long
980 compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
981                 struct compat_siginfo __user *uinfo,
982                 struct compat_timespec __user *uts, compat_size_t sigsetsize)
983 {
984         compat_sigset_t s32;
985         sigset_t s;
986         struct timespec t;
987         siginfo_t info;
988         long ret;
989
990         if (sigsetsize != sizeof(sigset_t))
991                 return -EINVAL;
992
993         if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
994                 return -EFAULT;
995         sigset_from_compat(&s, &s32);
996
997         if (uts) {
998                 if (get_compat_timespec(&t, uts))
999                         return -EFAULT;
1000         }
1001
1002         ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
1003
1004         if (ret > 0 && uinfo) {
1005                 if (copy_siginfo_to_user32(uinfo, &info))
1006                         ret = -EFAULT;
1007         }
1008
1009         return ret;
1010
1011 }
1012
1013 asmlinkage long
1014 compat_sys_rt_tgsigqueueinfo(compat_pid_t tgid, compat_pid_t pid, int sig,
1015                              struct compat_siginfo __user *uinfo)
1016 {
1017         siginfo_t info;
1018
1019         if (copy_siginfo_from_user32(&info, uinfo))
1020                 return -EFAULT;
1021         return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
1022 }
1023
1024 #ifdef __ARCH_WANT_COMPAT_SYS_TIME
1025
1026 /* compat_time_t is a 32 bit "long" and needs to get converted. */
1027
1028 asmlinkage long compat_sys_time(compat_time_t __user * tloc)
1029 {
1030         compat_time_t i;
1031         struct timeval tv;
1032
1033         do_gettimeofday(&tv);
1034         i = tv.tv_sec;
1035
1036         if (tloc) {
1037                 if (put_user(i,tloc))
1038                         return -EFAULT;
1039         }
1040         force_successful_syscall_return();
1041         return i;
1042 }
1043
1044 asmlinkage long compat_sys_stime(compat_time_t __user *tptr)
1045 {
1046         struct timespec tv;
1047         int err;
1048
1049         if (get_user(tv.tv_sec, tptr))
1050                 return -EFAULT;
1051
1052         tv.tv_nsec = 0;
1053
1054         err = security_settime(&tv, NULL);
1055         if (err)
1056                 return err;
1057
1058         do_settimeofday(&tv);
1059         return 0;
1060 }
1061
1062 #endif /* __ARCH_WANT_COMPAT_SYS_TIME */
1063
1064 #ifdef __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND
1065 asmlinkage long compat_sys_rt_sigsuspend(compat_sigset_t __user *unewset, compat_size_t sigsetsize)
1066 {
1067         sigset_t newset;
1068         compat_sigset_t newset32;
1069
1070         /* XXX: Don't preclude handling different sized sigset_t's.  */
1071         if (sigsetsize != sizeof(sigset_t))
1072                 return -EINVAL;
1073
1074         if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
1075                 return -EFAULT;
1076         sigset_from_compat(&newset, &newset32);
1077         sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1078
1079         current->saved_sigmask = current->blocked;
1080         set_current_blocked(&newset);
1081
1082         current->state = TASK_INTERRUPTIBLE;
1083         schedule();
1084         set_restore_sigmask();
1085         return -ERESTARTNOHAND;
1086 }
1087 #endif /* __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND */
1088
1089 asmlinkage long compat_sys_adjtimex(struct compat_timex __user *utp)
1090 {
1091         struct timex txc;
1092         int err, ret;
1093
1094         err = compat_get_timex(&txc, utp);
1095         if (err)
1096                 return err;
1097
1098         ret = do_adjtimex(&txc);
1099
1100         err = compat_put_timex(utp, &txc);
1101         if (err)
1102                 return err;
1103
1104         return ret;
1105 }
1106
1107 #ifdef CONFIG_NUMA
1108 asmlinkage long compat_sys_move_pages(pid_t pid, unsigned long nr_pages,
1109                 compat_uptr_t __user *pages32,
1110                 const int __user *nodes,
1111                 int __user *status,
1112                 int flags)
1113 {
1114         const void __user * __user *pages;
1115         int i;
1116
1117         pages = compat_alloc_user_space(nr_pages * sizeof(void *));
1118         for (i = 0; i < nr_pages; i++) {
1119                 compat_uptr_t p;
1120
1121                 if (get_user(p, pages32 + i) ||
1122                         put_user(compat_ptr(p), pages + i))
1123                         return -EFAULT;
1124         }
1125         return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
1126 }
1127
1128 asmlinkage long compat_sys_migrate_pages(compat_pid_t pid,
1129                         compat_ulong_t maxnode,
1130                         const compat_ulong_t __user *old_nodes,
1131                         const compat_ulong_t __user *new_nodes)
1132 {
1133         unsigned long __user *old = NULL;
1134         unsigned long __user *new = NULL;
1135         nodemask_t tmp_mask;
1136         unsigned long nr_bits;
1137         unsigned long size;
1138
1139         nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
1140         size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1141         if (old_nodes) {
1142                 if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
1143                         return -EFAULT;
1144                 old = compat_alloc_user_space(new_nodes ? size * 2 : size);
1145                 if (new_nodes)
1146                         new = old + size / sizeof(unsigned long);
1147                 if (copy_to_user(old, nodes_addr(tmp_mask), size))
1148                         return -EFAULT;
1149         }
1150         if (new_nodes) {
1151                 if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
1152                         return -EFAULT;
1153                 if (new == NULL)
1154                         new = compat_alloc_user_space(size);
1155                 if (copy_to_user(new, nodes_addr(tmp_mask), size))
1156                         return -EFAULT;
1157         }
1158         return sys_migrate_pages(pid, nr_bits + 1, old, new);
1159 }
1160 #endif
1161
1162 struct compat_sysinfo {
1163         s32 uptime;
1164         u32 loads[3];
1165         u32 totalram;
1166         u32 freeram;
1167         u32 sharedram;
1168         u32 bufferram;
1169         u32 totalswap;
1170         u32 freeswap;
1171         u16 procs;
1172         u16 pad;
1173         u32 totalhigh;
1174         u32 freehigh;
1175         u32 mem_unit;
1176         char _f[20-2*sizeof(u32)-sizeof(int)];
1177 };
1178
1179 asmlinkage long
1180 compat_sys_sysinfo(struct compat_sysinfo __user *info)
1181 {
1182         struct sysinfo s;
1183
1184         do_sysinfo(&s);
1185
1186         /* Check to see if any memory value is too large for 32-bit and scale
1187          *  down if needed
1188          */
1189         if ((s.totalram >> 32) || (s.totalswap >> 32)) {
1190                 int bitcount = 0;
1191
1192                 while (s.mem_unit < PAGE_SIZE) {
1193                         s.mem_unit <<= 1;
1194                         bitcount++;
1195                 }
1196
1197                 s.totalram >>= bitcount;
1198                 s.freeram >>= bitcount;
1199                 s.sharedram >>= bitcount;
1200                 s.bufferram >>= bitcount;
1201                 s.totalswap >>= bitcount;
1202                 s.freeswap >>= bitcount;
1203                 s.totalhigh >>= bitcount;
1204                 s.freehigh >>= bitcount;
1205         }
1206
1207         if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
1208             __put_user (s.uptime, &info->uptime) ||
1209             __put_user (s.loads[0], &info->loads[0]) ||
1210             __put_user (s.loads[1], &info->loads[1]) ||
1211             __put_user (s.loads[2], &info->loads[2]) ||
1212             __put_user (s.totalram, &info->totalram) ||
1213             __put_user (s.freeram, &info->freeram) ||
1214             __put_user (s.sharedram, &info->sharedram) ||
1215             __put_user (s.bufferram, &info->bufferram) ||
1216             __put_user (s.totalswap, &info->totalswap) ||
1217             __put_user (s.freeswap, &info->freeswap) ||
1218             __put_user (s.procs, &info->procs) ||
1219             __put_user (s.totalhigh, &info->totalhigh) ||
1220             __put_user (s.freehigh, &info->freehigh) ||
1221             __put_user (s.mem_unit, &info->mem_unit))
1222                 return -EFAULT;
1223
1224         return 0;
1225 }
1226
1227 /* Note: it is necessary to treat option as an unsigned int,
1228  * with the corresponding cast to a signed int to ensure that the
1229  * proper conversion (sign extension) between the register representation
1230  * of a signed int (msr in 32-bit mode) and the register representation
1231  * of a signed int (msr in 64-bit mode) is performed.
1232  */
1233 asmlinkage long compat_sys_prctl(u32 option, u32 arg2, u32 arg3,
1234                                  u32 arg4, u32 arg5)
1235 {
1236         unsigned long a2 = arg2;
1237
1238         /* PR_SET_PTRACER_ANY is -1, so can't do an unsigned extension */
1239         if (option == PR_SET_PTRACER && arg2 == (u32) PR_SET_PTRACER_ANY)
1240                 a2 = PR_SET_PTRACER_ANY;
1241
1242         return sys_prctl((int)option,
1243                          (unsigned long) a2,
1244                          (unsigned long) arg3,
1245                          (unsigned long) arg4,
1246                          (unsigned long) arg5);
1247 }
1248
1249 /*
1250  * Allocate user-space memory for the duration of a single system call,
1251  * in order to marshall parameters inside a compat thunk.
1252  */
1253 void __user *compat_alloc_user_space(unsigned long len)
1254 {
1255         void __user *ptr;
1256
1257         /* If len would occupy more than half of the entire compat space... */
1258         if (unlikely(len > (((compat_uptr_t)~0) >> 1)))
1259                 return NULL;
1260
1261         ptr = arch_compat_alloc_user_space(len);
1262
1263         if (unlikely(!access_ok(VERIFY_WRITE, ptr, len)))
1264                 return NULL;
1265
1266         return ptr;
1267 }
1268 EXPORT_SYMBOL_GPL(compat_alloc_user_space);