Merge branches 'x86-rwsem-for-linus' and 'x86-gcc46-for-linus' of git://git.kernel...
[linux-2.6/libata-dev.git] / kernel / compat.c
blob5adab05a3172c0c3e81e7a42afbe10b2a37e3a2d
1 /*
2 * linux/kernel/compat.c
4 * Kernel compatibililty routines for e.g. 32 bit syscall support
5 * on 64 bit kernels.
7 * Copyright (C) 2002-2003 Stephen Rothwell, IBM Corporation
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.
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 #include <linux/times.h>
27 #include <linux/ptrace.h>
28 #include <linux/gfp.h>
30 #include <asm/uaccess.h>
33 * Note that the native side is already converted to a timespec, because
34 * that's what we want anyway.
36 static int compat_get_timeval(struct timespec *o,
37 struct compat_timeval __user *i)
39 long usec;
41 if (get_user(o->tv_sec, &i->tv_sec) ||
42 get_user(usec, &i->tv_usec))
43 return -EFAULT;
44 o->tv_nsec = usec * 1000;
45 return 0;
48 static int compat_put_timeval(struct compat_timeval __user *o,
49 struct timeval *i)
51 return (put_user(i->tv_sec, &o->tv_sec) ||
52 put_user(i->tv_usec, &o->tv_usec)) ? -EFAULT : 0;
55 asmlinkage long compat_sys_gettimeofday(struct compat_timeval __user *tv,
56 struct timezone __user *tz)
58 if (tv) {
59 struct timeval ktv;
60 do_gettimeofday(&ktv);
61 if (compat_put_timeval(tv, &ktv))
62 return -EFAULT;
64 if (tz) {
65 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
66 return -EFAULT;
69 return 0;
72 asmlinkage long compat_sys_settimeofday(struct compat_timeval __user *tv,
73 struct timezone __user *tz)
75 struct timespec kts;
76 struct timezone ktz;
78 if (tv) {
79 if (compat_get_timeval(&kts, tv))
80 return -EFAULT;
82 if (tz) {
83 if (copy_from_user(&ktz, tz, sizeof(ktz)))
84 return -EFAULT;
87 return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
90 int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
92 return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
93 __get_user(ts->tv_sec, &cts->tv_sec) ||
94 __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
97 int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
99 return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
100 __put_user(ts->tv_sec, &cts->tv_sec) ||
101 __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
104 static long compat_nanosleep_restart(struct restart_block *restart)
106 struct compat_timespec __user *rmtp;
107 struct timespec rmt;
108 mm_segment_t oldfs;
109 long ret;
111 restart->nanosleep.rmtp = (struct timespec __user *) &rmt;
112 oldfs = get_fs();
113 set_fs(KERNEL_DS);
114 ret = hrtimer_nanosleep_restart(restart);
115 set_fs(oldfs);
117 if (ret) {
118 rmtp = restart->nanosleep.compat_rmtp;
120 if (rmtp && put_compat_timespec(&rmt, rmtp))
121 return -EFAULT;
124 return ret;
127 asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
128 struct compat_timespec __user *rmtp)
130 struct timespec tu, rmt;
131 mm_segment_t oldfs;
132 long ret;
134 if (get_compat_timespec(&tu, rqtp))
135 return -EFAULT;
137 if (!timespec_valid(&tu))
138 return -EINVAL;
140 oldfs = get_fs();
141 set_fs(KERNEL_DS);
142 ret = hrtimer_nanosleep(&tu,
143 rmtp ? (struct timespec __user *)&rmt : NULL,
144 HRTIMER_MODE_REL, CLOCK_MONOTONIC);
145 set_fs(oldfs);
147 if (ret) {
148 struct restart_block *restart
149 = &current_thread_info()->restart_block;
151 restart->fn = compat_nanosleep_restart;
152 restart->nanosleep.compat_rmtp = rmtp;
154 if (rmtp && put_compat_timespec(&rmt, rmtp))
155 return -EFAULT;
158 return ret;
161 static inline long get_compat_itimerval(struct itimerval *o,
162 struct compat_itimerval __user *i)
164 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
165 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
166 __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
167 __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
168 __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
171 static inline long put_compat_itimerval(struct compat_itimerval __user *o,
172 struct itimerval *i)
174 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
175 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
176 __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
177 __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
178 __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
181 asmlinkage long compat_sys_getitimer(int which,
182 struct compat_itimerval __user *it)
184 struct itimerval kit;
185 int error;
187 error = do_getitimer(which, &kit);
188 if (!error && put_compat_itimerval(it, &kit))
189 error = -EFAULT;
190 return error;
193 asmlinkage long compat_sys_setitimer(int which,
194 struct compat_itimerval __user *in,
195 struct compat_itimerval __user *out)
197 struct itimerval kin, kout;
198 int error;
200 if (in) {
201 if (get_compat_itimerval(&kin, in))
202 return -EFAULT;
203 } else
204 memset(&kin, 0, sizeof(kin));
206 error = do_setitimer(which, &kin, out ? &kout : NULL);
207 if (error || !out)
208 return error;
209 if (put_compat_itimerval(out, &kout))
210 return -EFAULT;
211 return 0;
214 static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
216 return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
219 asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
221 if (tbuf) {
222 struct tms tms;
223 struct compat_tms tmp;
225 do_sys_times(&tms);
226 /* Convert our struct tms to the compat version. */
227 tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
228 tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
229 tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
230 tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
231 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
232 return -EFAULT;
234 force_successful_syscall_return();
235 return compat_jiffies_to_clock_t(jiffies);
239 * Assumption: old_sigset_t and compat_old_sigset_t are both
240 * types that can be passed to put_user()/get_user().
243 asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
245 old_sigset_t s;
246 long ret;
247 mm_segment_t old_fs = get_fs();
249 set_fs(KERNEL_DS);
250 ret = sys_sigpending((old_sigset_t __user *) &s);
251 set_fs(old_fs);
252 if (ret == 0)
253 ret = put_user(s, set);
254 return ret;
257 asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set,
258 compat_old_sigset_t __user *oset)
260 old_sigset_t s;
261 long ret;
262 mm_segment_t old_fs;
264 if (set && get_user(s, set))
265 return -EFAULT;
266 old_fs = get_fs();
267 set_fs(KERNEL_DS);
268 ret = sys_sigprocmask(how,
269 set ? (old_sigset_t __user *) &s : NULL,
270 oset ? (old_sigset_t __user *) &s : NULL);
271 set_fs(old_fs);
272 if (ret == 0)
273 if (oset)
274 ret = put_user(s, oset);
275 return ret;
278 asmlinkage long compat_sys_setrlimit(unsigned int resource,
279 struct compat_rlimit __user *rlim)
281 struct rlimit r;
282 int ret;
283 mm_segment_t old_fs = get_fs ();
285 if (resource >= RLIM_NLIMITS)
286 return -EINVAL;
288 if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
289 __get_user(r.rlim_cur, &rlim->rlim_cur) ||
290 __get_user(r.rlim_max, &rlim->rlim_max))
291 return -EFAULT;
293 if (r.rlim_cur == COMPAT_RLIM_INFINITY)
294 r.rlim_cur = RLIM_INFINITY;
295 if (r.rlim_max == COMPAT_RLIM_INFINITY)
296 r.rlim_max = RLIM_INFINITY;
297 set_fs(KERNEL_DS);
298 ret = sys_setrlimit(resource, (struct rlimit __user *) &r);
299 set_fs(old_fs);
300 return ret;
303 #ifdef COMPAT_RLIM_OLD_INFINITY
305 asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
306 struct compat_rlimit __user *rlim)
308 struct rlimit r;
309 int ret;
310 mm_segment_t old_fs = get_fs();
312 set_fs(KERNEL_DS);
313 ret = sys_old_getrlimit(resource, &r);
314 set_fs(old_fs);
316 if (!ret) {
317 if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
318 r.rlim_cur = COMPAT_RLIM_INFINITY;
319 if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
320 r.rlim_max = COMPAT_RLIM_INFINITY;
322 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
323 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
324 __put_user(r.rlim_max, &rlim->rlim_max))
325 return -EFAULT;
327 return ret;
330 #endif
332 asmlinkage long compat_sys_getrlimit (unsigned int resource,
333 struct compat_rlimit __user *rlim)
335 struct rlimit r;
336 int ret;
337 mm_segment_t old_fs = get_fs();
339 set_fs(KERNEL_DS);
340 ret = sys_getrlimit(resource, (struct rlimit __user *) &r);
341 set_fs(old_fs);
342 if (!ret) {
343 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
344 r.rlim_cur = COMPAT_RLIM_INFINITY;
345 if (r.rlim_max > COMPAT_RLIM_INFINITY)
346 r.rlim_max = COMPAT_RLIM_INFINITY;
348 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
349 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
350 __put_user(r.rlim_max, &rlim->rlim_max))
351 return -EFAULT;
353 return ret;
356 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
358 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
359 __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
360 __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
361 __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
362 __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
363 __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
364 __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
365 __put_user(r->ru_idrss, &ru->ru_idrss) ||
366 __put_user(r->ru_isrss, &ru->ru_isrss) ||
367 __put_user(r->ru_minflt, &ru->ru_minflt) ||
368 __put_user(r->ru_majflt, &ru->ru_majflt) ||
369 __put_user(r->ru_nswap, &ru->ru_nswap) ||
370 __put_user(r->ru_inblock, &ru->ru_inblock) ||
371 __put_user(r->ru_oublock, &ru->ru_oublock) ||
372 __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
373 __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
374 __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
375 __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
376 __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
377 return -EFAULT;
378 return 0;
381 asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
383 struct rusage r;
384 int ret;
385 mm_segment_t old_fs = get_fs();
387 set_fs(KERNEL_DS);
388 ret = sys_getrusage(who, (struct rusage __user *) &r);
389 set_fs(old_fs);
391 if (ret)
392 return ret;
394 if (put_compat_rusage(&r, ru))
395 return -EFAULT;
397 return 0;
400 asmlinkage long
401 compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
402 struct compat_rusage __user *ru)
404 if (!ru) {
405 return sys_wait4(pid, stat_addr, options, NULL);
406 } else {
407 struct rusage r;
408 int ret;
409 unsigned int status;
410 mm_segment_t old_fs = get_fs();
412 set_fs (KERNEL_DS);
413 ret = sys_wait4(pid,
414 (stat_addr ?
415 (unsigned int __user *) &status : NULL),
416 options, (struct rusage __user *) &r);
417 set_fs (old_fs);
419 if (ret > 0) {
420 if (put_compat_rusage(&r, ru))
421 return -EFAULT;
422 if (stat_addr && put_user(status, stat_addr))
423 return -EFAULT;
425 return ret;
429 asmlinkage long compat_sys_waitid(int which, compat_pid_t pid,
430 struct compat_siginfo __user *uinfo, int options,
431 struct compat_rusage __user *uru)
433 siginfo_t info;
434 struct rusage ru;
435 long ret;
436 mm_segment_t old_fs = get_fs();
438 memset(&info, 0, sizeof(info));
440 set_fs(KERNEL_DS);
441 ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
442 uru ? (struct rusage __user *)&ru : NULL);
443 set_fs(old_fs);
445 if ((ret < 0) || (info.si_signo == 0))
446 return ret;
448 if (uru) {
449 ret = put_compat_rusage(&ru, uru);
450 if (ret)
451 return ret;
454 BUG_ON(info.si_code & __SI_MASK);
455 info.si_code |= __SI_CHLD;
456 return copy_siginfo_to_user32(uinfo, &info);
459 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
460 unsigned len, struct cpumask *new_mask)
462 unsigned long *k;
464 if (len < cpumask_size())
465 memset(new_mask, 0, cpumask_size());
466 else if (len > cpumask_size())
467 len = cpumask_size();
469 k = cpumask_bits(new_mask);
470 return compat_get_bitmap(k, user_mask_ptr, len * 8);
473 asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
474 unsigned int len,
475 compat_ulong_t __user *user_mask_ptr)
477 cpumask_var_t new_mask;
478 int retval;
480 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
481 return -ENOMEM;
483 retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
484 if (retval)
485 goto out;
487 retval = sched_setaffinity(pid, new_mask);
488 out:
489 free_cpumask_var(new_mask);
490 return retval;
493 asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
494 compat_ulong_t __user *user_mask_ptr)
496 int ret;
497 cpumask_var_t mask;
499 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
500 return -EINVAL;
501 if (len & (sizeof(compat_ulong_t)-1))
502 return -EINVAL;
504 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
505 return -ENOMEM;
507 ret = sched_getaffinity(pid, mask);
508 if (ret == 0) {
509 size_t retlen = min_t(size_t, len, cpumask_size());
511 if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
512 ret = -EFAULT;
513 else
514 ret = retlen;
516 free_cpumask_var(mask);
518 return ret;
521 int get_compat_itimerspec(struct itimerspec *dst,
522 const struct compat_itimerspec __user *src)
524 if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
525 get_compat_timespec(&dst->it_value, &src->it_value))
526 return -EFAULT;
527 return 0;
530 int put_compat_itimerspec(struct compat_itimerspec __user *dst,
531 const struct itimerspec *src)
533 if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
534 put_compat_timespec(&src->it_value, &dst->it_value))
535 return -EFAULT;
536 return 0;
539 long compat_sys_timer_create(clockid_t which_clock,
540 struct compat_sigevent __user *timer_event_spec,
541 timer_t __user *created_timer_id)
543 struct sigevent __user *event = NULL;
545 if (timer_event_spec) {
546 struct sigevent kevent;
548 event = compat_alloc_user_space(sizeof(*event));
549 if (get_compat_sigevent(&kevent, timer_event_spec) ||
550 copy_to_user(event, &kevent, sizeof(*event)))
551 return -EFAULT;
554 return sys_timer_create(which_clock, event, created_timer_id);
557 long compat_sys_timer_settime(timer_t timer_id, int flags,
558 struct compat_itimerspec __user *new,
559 struct compat_itimerspec __user *old)
561 long err;
562 mm_segment_t oldfs;
563 struct itimerspec newts, oldts;
565 if (!new)
566 return -EINVAL;
567 if (get_compat_itimerspec(&newts, new))
568 return -EFAULT;
569 oldfs = get_fs();
570 set_fs(KERNEL_DS);
571 err = sys_timer_settime(timer_id, flags,
572 (struct itimerspec __user *) &newts,
573 (struct itimerspec __user *) &oldts);
574 set_fs(oldfs);
575 if (!err && old && put_compat_itimerspec(old, &oldts))
576 return -EFAULT;
577 return err;
580 long compat_sys_timer_gettime(timer_t timer_id,
581 struct compat_itimerspec __user *setting)
583 long err;
584 mm_segment_t oldfs;
585 struct itimerspec ts;
587 oldfs = get_fs();
588 set_fs(KERNEL_DS);
589 err = sys_timer_gettime(timer_id,
590 (struct itimerspec __user *) &ts);
591 set_fs(oldfs);
592 if (!err && put_compat_itimerspec(setting, &ts))
593 return -EFAULT;
594 return err;
597 long compat_sys_clock_settime(clockid_t which_clock,
598 struct compat_timespec __user *tp)
600 long err;
601 mm_segment_t oldfs;
602 struct timespec ts;
604 if (get_compat_timespec(&ts, tp))
605 return -EFAULT;
606 oldfs = get_fs();
607 set_fs(KERNEL_DS);
608 err = sys_clock_settime(which_clock,
609 (struct timespec __user *) &ts);
610 set_fs(oldfs);
611 return err;
614 long compat_sys_clock_gettime(clockid_t which_clock,
615 struct compat_timespec __user *tp)
617 long err;
618 mm_segment_t oldfs;
619 struct timespec ts;
621 oldfs = get_fs();
622 set_fs(KERNEL_DS);
623 err = sys_clock_gettime(which_clock,
624 (struct timespec __user *) &ts);
625 set_fs(oldfs);
626 if (!err && put_compat_timespec(&ts, tp))
627 return -EFAULT;
628 return err;
631 long compat_sys_clock_getres(clockid_t which_clock,
632 struct compat_timespec __user *tp)
634 long err;
635 mm_segment_t oldfs;
636 struct timespec ts;
638 oldfs = get_fs();
639 set_fs(KERNEL_DS);
640 err = sys_clock_getres(which_clock,
641 (struct timespec __user *) &ts);
642 set_fs(oldfs);
643 if (!err && tp && put_compat_timespec(&ts, tp))
644 return -EFAULT;
645 return err;
648 static long compat_clock_nanosleep_restart(struct restart_block *restart)
650 long err;
651 mm_segment_t oldfs;
652 struct timespec tu;
653 struct compat_timespec *rmtp = restart->nanosleep.compat_rmtp;
655 restart->nanosleep.rmtp = (struct timespec __user *) &tu;
656 oldfs = get_fs();
657 set_fs(KERNEL_DS);
658 err = clock_nanosleep_restart(restart);
659 set_fs(oldfs);
661 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
662 put_compat_timespec(&tu, rmtp))
663 return -EFAULT;
665 if (err == -ERESTART_RESTARTBLOCK) {
666 restart->fn = compat_clock_nanosleep_restart;
667 restart->nanosleep.compat_rmtp = rmtp;
669 return err;
672 long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
673 struct compat_timespec __user *rqtp,
674 struct compat_timespec __user *rmtp)
676 long err;
677 mm_segment_t oldfs;
678 struct timespec in, out;
679 struct restart_block *restart;
681 if (get_compat_timespec(&in, rqtp))
682 return -EFAULT;
684 oldfs = get_fs();
685 set_fs(KERNEL_DS);
686 err = sys_clock_nanosleep(which_clock, flags,
687 (struct timespec __user *) &in,
688 (struct timespec __user *) &out);
689 set_fs(oldfs);
691 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
692 put_compat_timespec(&out, rmtp))
693 return -EFAULT;
695 if (err == -ERESTART_RESTARTBLOCK) {
696 restart = &current_thread_info()->restart_block;
697 restart->fn = compat_clock_nanosleep_restart;
698 restart->nanosleep.compat_rmtp = rmtp;
700 return err;
704 * We currently only need the following fields from the sigevent
705 * structure: sigev_value, sigev_signo, sig_notify and (sometimes
706 * sigev_notify_thread_id). The others are handled in user mode.
707 * We also assume that copying sigev_value.sival_int is sufficient
708 * to keep all the bits of sigev_value.sival_ptr intact.
710 int get_compat_sigevent(struct sigevent *event,
711 const struct compat_sigevent __user *u_event)
713 memset(event, 0, sizeof(*event));
714 return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
715 __get_user(event->sigev_value.sival_int,
716 &u_event->sigev_value.sival_int) ||
717 __get_user(event->sigev_signo, &u_event->sigev_signo) ||
718 __get_user(event->sigev_notify, &u_event->sigev_notify) ||
719 __get_user(event->sigev_notify_thread_id,
720 &u_event->sigev_notify_thread_id))
721 ? -EFAULT : 0;
724 long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
725 unsigned long bitmap_size)
727 int i, j;
728 unsigned long m;
729 compat_ulong_t um;
730 unsigned long nr_compat_longs;
732 /* align bitmap up to nearest compat_long_t boundary */
733 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
735 if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
736 return -EFAULT;
738 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
740 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
741 m = 0;
743 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
745 * We dont want to read past the end of the userspace
746 * bitmap. We must however ensure the end of the
747 * kernel bitmap is zeroed.
749 if (nr_compat_longs-- > 0) {
750 if (__get_user(um, umask))
751 return -EFAULT;
752 } else {
753 um = 0;
756 umask++;
757 m |= (long)um << (j * BITS_PER_COMPAT_LONG);
759 *mask++ = m;
762 return 0;
765 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
766 unsigned long bitmap_size)
768 int i, j;
769 unsigned long m;
770 compat_ulong_t um;
771 unsigned long nr_compat_longs;
773 /* align bitmap up to nearest compat_long_t boundary */
774 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
776 if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
777 return -EFAULT;
779 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
781 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
782 m = *mask++;
784 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
785 um = m;
788 * We dont want to write past the end of the userspace
789 * bitmap.
791 if (nr_compat_longs-- > 0) {
792 if (__put_user(um, umask))
793 return -EFAULT;
796 umask++;
797 m >>= 4*sizeof(um);
798 m >>= 4*sizeof(um);
802 return 0;
805 void
806 sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
808 switch (_NSIG_WORDS) {
809 case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
810 case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
811 case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
812 case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
816 asmlinkage long
817 compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
818 struct compat_siginfo __user *uinfo,
819 struct compat_timespec __user *uts, compat_size_t sigsetsize)
821 compat_sigset_t s32;
822 sigset_t s;
823 int sig;
824 struct timespec t;
825 siginfo_t info;
826 long ret, timeout = 0;
828 if (sigsetsize != sizeof(sigset_t))
829 return -EINVAL;
831 if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
832 return -EFAULT;
833 sigset_from_compat(&s, &s32);
834 sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
835 signotset(&s);
837 if (uts) {
838 if (get_compat_timespec (&t, uts))
839 return -EFAULT;
840 if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0
841 || t.tv_sec < 0)
842 return -EINVAL;
845 spin_lock_irq(&current->sighand->siglock);
846 sig = dequeue_signal(current, &s, &info);
847 if (!sig) {
848 timeout = MAX_SCHEDULE_TIMEOUT;
849 if (uts)
850 timeout = timespec_to_jiffies(&t)
851 +(t.tv_sec || t.tv_nsec);
852 if (timeout) {
853 current->real_blocked = current->blocked;
854 sigandsets(&current->blocked, &current->blocked, &s);
856 recalc_sigpending();
857 spin_unlock_irq(&current->sighand->siglock);
859 timeout = schedule_timeout_interruptible(timeout);
861 spin_lock_irq(&current->sighand->siglock);
862 sig = dequeue_signal(current, &s, &info);
863 current->blocked = current->real_blocked;
864 siginitset(&current->real_blocked, 0);
865 recalc_sigpending();
868 spin_unlock_irq(&current->sighand->siglock);
870 if (sig) {
871 ret = sig;
872 if (uinfo) {
873 if (copy_siginfo_to_user32(uinfo, &info))
874 ret = -EFAULT;
876 }else {
877 ret = timeout?-EINTR:-EAGAIN;
879 return ret;
883 asmlinkage long
884 compat_sys_rt_tgsigqueueinfo(compat_pid_t tgid, compat_pid_t pid, int sig,
885 struct compat_siginfo __user *uinfo)
887 siginfo_t info;
889 if (copy_siginfo_from_user32(&info, uinfo))
890 return -EFAULT;
891 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
894 #ifdef __ARCH_WANT_COMPAT_SYS_TIME
896 /* compat_time_t is a 32 bit "long" and needs to get converted. */
898 asmlinkage long compat_sys_time(compat_time_t __user * tloc)
900 compat_time_t i;
901 struct timeval tv;
903 do_gettimeofday(&tv);
904 i = tv.tv_sec;
906 if (tloc) {
907 if (put_user(i,tloc))
908 return -EFAULT;
910 force_successful_syscall_return();
911 return i;
914 asmlinkage long compat_sys_stime(compat_time_t __user *tptr)
916 struct timespec tv;
917 int err;
919 if (get_user(tv.tv_sec, tptr))
920 return -EFAULT;
922 tv.tv_nsec = 0;
924 err = security_settime(&tv, NULL);
925 if (err)
926 return err;
928 do_settimeofday(&tv);
929 return 0;
932 #endif /* __ARCH_WANT_COMPAT_SYS_TIME */
934 #ifdef __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND
935 asmlinkage long compat_sys_rt_sigsuspend(compat_sigset_t __user *unewset, compat_size_t sigsetsize)
937 sigset_t newset;
938 compat_sigset_t newset32;
940 /* XXX: Don't preclude handling different sized sigset_t's. */
941 if (sigsetsize != sizeof(sigset_t))
942 return -EINVAL;
944 if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
945 return -EFAULT;
946 sigset_from_compat(&newset, &newset32);
947 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
949 spin_lock_irq(&current->sighand->siglock);
950 current->saved_sigmask = current->blocked;
951 current->blocked = newset;
952 recalc_sigpending();
953 spin_unlock_irq(&current->sighand->siglock);
955 current->state = TASK_INTERRUPTIBLE;
956 schedule();
957 set_restore_sigmask();
958 return -ERESTARTNOHAND;
960 #endif /* __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND */
962 asmlinkage long compat_sys_adjtimex(struct compat_timex __user *utp)
964 struct timex txc;
965 int ret;
967 memset(&txc, 0, sizeof(struct timex));
969 if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
970 __get_user(txc.modes, &utp->modes) ||
971 __get_user(txc.offset, &utp->offset) ||
972 __get_user(txc.freq, &utp->freq) ||
973 __get_user(txc.maxerror, &utp->maxerror) ||
974 __get_user(txc.esterror, &utp->esterror) ||
975 __get_user(txc.status, &utp->status) ||
976 __get_user(txc.constant, &utp->constant) ||
977 __get_user(txc.precision, &utp->precision) ||
978 __get_user(txc.tolerance, &utp->tolerance) ||
979 __get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
980 __get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
981 __get_user(txc.tick, &utp->tick) ||
982 __get_user(txc.ppsfreq, &utp->ppsfreq) ||
983 __get_user(txc.jitter, &utp->jitter) ||
984 __get_user(txc.shift, &utp->shift) ||
985 __get_user(txc.stabil, &utp->stabil) ||
986 __get_user(txc.jitcnt, &utp->jitcnt) ||
987 __get_user(txc.calcnt, &utp->calcnt) ||
988 __get_user(txc.errcnt, &utp->errcnt) ||
989 __get_user(txc.stbcnt, &utp->stbcnt))
990 return -EFAULT;
992 ret = do_adjtimex(&txc);
994 if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
995 __put_user(txc.modes, &utp->modes) ||
996 __put_user(txc.offset, &utp->offset) ||
997 __put_user(txc.freq, &utp->freq) ||
998 __put_user(txc.maxerror, &utp->maxerror) ||
999 __put_user(txc.esterror, &utp->esterror) ||
1000 __put_user(txc.status, &utp->status) ||
1001 __put_user(txc.constant, &utp->constant) ||
1002 __put_user(txc.precision, &utp->precision) ||
1003 __put_user(txc.tolerance, &utp->tolerance) ||
1004 __put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
1005 __put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
1006 __put_user(txc.tick, &utp->tick) ||
1007 __put_user(txc.ppsfreq, &utp->ppsfreq) ||
1008 __put_user(txc.jitter, &utp->jitter) ||
1009 __put_user(txc.shift, &utp->shift) ||
1010 __put_user(txc.stabil, &utp->stabil) ||
1011 __put_user(txc.jitcnt, &utp->jitcnt) ||
1012 __put_user(txc.calcnt, &utp->calcnt) ||
1013 __put_user(txc.errcnt, &utp->errcnt) ||
1014 __put_user(txc.stbcnt, &utp->stbcnt) ||
1015 __put_user(txc.tai, &utp->tai))
1016 ret = -EFAULT;
1018 return ret;
1021 #ifdef CONFIG_NUMA
1022 asmlinkage long compat_sys_move_pages(pid_t pid, unsigned long nr_pages,
1023 compat_uptr_t __user *pages32,
1024 const int __user *nodes,
1025 int __user *status,
1026 int flags)
1028 const void __user * __user *pages;
1029 int i;
1031 pages = compat_alloc_user_space(nr_pages * sizeof(void *));
1032 for (i = 0; i < nr_pages; i++) {
1033 compat_uptr_t p;
1035 if (get_user(p, pages32 + i) ||
1036 put_user(compat_ptr(p), pages + i))
1037 return -EFAULT;
1039 return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
1042 asmlinkage long compat_sys_migrate_pages(compat_pid_t pid,
1043 compat_ulong_t maxnode,
1044 const compat_ulong_t __user *old_nodes,
1045 const compat_ulong_t __user *new_nodes)
1047 unsigned long __user *old = NULL;
1048 unsigned long __user *new = NULL;
1049 nodemask_t tmp_mask;
1050 unsigned long nr_bits;
1051 unsigned long size;
1053 nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
1054 size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1055 if (old_nodes) {
1056 if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
1057 return -EFAULT;
1058 old = compat_alloc_user_space(new_nodes ? size * 2 : size);
1059 if (new_nodes)
1060 new = old + size / sizeof(unsigned long);
1061 if (copy_to_user(old, nodes_addr(tmp_mask), size))
1062 return -EFAULT;
1064 if (new_nodes) {
1065 if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
1066 return -EFAULT;
1067 if (new == NULL)
1068 new = compat_alloc_user_space(size);
1069 if (copy_to_user(new, nodes_addr(tmp_mask), size))
1070 return -EFAULT;
1072 return sys_migrate_pages(pid, nr_bits + 1, old, new);
1074 #endif
1076 struct compat_sysinfo {
1077 s32 uptime;
1078 u32 loads[3];
1079 u32 totalram;
1080 u32 freeram;
1081 u32 sharedram;
1082 u32 bufferram;
1083 u32 totalswap;
1084 u32 freeswap;
1085 u16 procs;
1086 u16 pad;
1087 u32 totalhigh;
1088 u32 freehigh;
1089 u32 mem_unit;
1090 char _f[20-2*sizeof(u32)-sizeof(int)];
1093 asmlinkage long
1094 compat_sys_sysinfo(struct compat_sysinfo __user *info)
1096 struct sysinfo s;
1098 do_sysinfo(&s);
1100 /* Check to see if any memory value is too large for 32-bit and scale
1101 * down if needed
1103 if ((s.totalram >> 32) || (s.totalswap >> 32)) {
1104 int bitcount = 0;
1106 while (s.mem_unit < PAGE_SIZE) {
1107 s.mem_unit <<= 1;
1108 bitcount++;
1111 s.totalram >>= bitcount;
1112 s.freeram >>= bitcount;
1113 s.sharedram >>= bitcount;
1114 s.bufferram >>= bitcount;
1115 s.totalswap >>= bitcount;
1116 s.freeswap >>= bitcount;
1117 s.totalhigh >>= bitcount;
1118 s.freehigh >>= bitcount;
1121 if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
1122 __put_user (s.uptime, &info->uptime) ||
1123 __put_user (s.loads[0], &info->loads[0]) ||
1124 __put_user (s.loads[1], &info->loads[1]) ||
1125 __put_user (s.loads[2], &info->loads[2]) ||
1126 __put_user (s.totalram, &info->totalram) ||
1127 __put_user (s.freeram, &info->freeram) ||
1128 __put_user (s.sharedram, &info->sharedram) ||
1129 __put_user (s.bufferram, &info->bufferram) ||
1130 __put_user (s.totalswap, &info->totalswap) ||
1131 __put_user (s.freeswap, &info->freeswap) ||
1132 __put_user (s.procs, &info->procs) ||
1133 __put_user (s.totalhigh, &info->totalhigh) ||
1134 __put_user (s.freehigh, &info->freehigh) ||
1135 __put_user (s.mem_unit, &info->mem_unit))
1136 return -EFAULT;
1138 return 0;