2 * linux/kernel/compat.c
4 * Kernel compatibililty routines for e.g. 32 bit syscall support
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/futex.h> /* for FUTEX_WAIT */
21 #include <linux/syscalls.h>
22 #include <linux/unistd.h>
23 #include <linux/security.h>
25 #include <asm/uaccess.h>
27 int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
29 return (verify_area(VERIFY_READ, cts, sizeof(*cts)) ||
30 __get_user(ts->tv_sec, &cts->tv_sec) ||
31 __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
34 int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
36 return (verify_area(VERIFY_WRITE, cts, sizeof(*cts)) ||
37 __put_user(ts->tv_sec, &cts->tv_sec) ||
38 __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
41 static long compat_nanosleep_restart(struct restart_block *restart)
43 unsigned long expire = restart->arg0, now = jiffies;
44 struct compat_timespec __user *rmtp;
46 /* Did it expire while we handled signals? */
47 if (!time_after(expire, now))
50 current->state = TASK_INTERRUPTIBLE;
51 expire = schedule_timeout(expire - now);
55 rmtp = (struct compat_timespec __user *)restart->arg1;
57 struct compat_timespec ct;
60 jiffies_to_timespec(expire, &t);
62 ct.tv_nsec = t.tv_nsec;
63 if (copy_to_user(rmtp, &ct, sizeof(ct)))
66 /* The 'restart' block is already filled in */
67 return -ERESTART_RESTARTBLOCK;
70 asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
71 struct compat_timespec __user *rmtp)
74 struct restart_block *restart;
77 if (get_compat_timespec(&t, rqtp))
80 if ((t.tv_nsec >= 1000000000L) || (t.tv_nsec < 0) || (t.tv_sec < 0))
83 expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
84 current->state = TASK_INTERRUPTIBLE;
85 expire = schedule_timeout(expire);
90 jiffies_to_timespec(expire, &t);
91 if (put_compat_timespec(&t, rmtp))
94 restart = ¤t_thread_info()->restart_block;
95 restart->fn = compat_nanosleep_restart;
96 restart->arg0 = jiffies + expire;
97 restart->arg1 = (unsigned long) rmtp;
98 return -ERESTART_RESTARTBLOCK;
101 static inline long get_compat_itimerval(struct itimerval *o,
102 struct compat_itimerval __user *i)
104 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
105 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
106 __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
107 __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
108 __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
111 static inline long put_compat_itimerval(struct compat_itimerval __user *o,
114 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
115 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
116 __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
117 __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
118 __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
121 asmlinkage long compat_sys_getitimer(int which,
122 struct compat_itimerval __user *it)
124 struct itimerval kit;
127 error = do_getitimer(which, &kit);
128 if (!error && put_compat_itimerval(it, &kit))
133 asmlinkage long compat_sys_setitimer(int which,
134 struct compat_itimerval __user *in,
135 struct compat_itimerval __user *out)
137 struct itimerval kin, kout;
141 if (get_compat_itimerval(&kin, in))
144 memset(&kin, 0, sizeof(kin));
146 error = do_setitimer(which, &kin, out ? &kout : NULL);
149 if (put_compat_itimerval(out, &kout))
154 asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
157 * In the SMP world we might just be unlucky and have one of
158 * the times increment as we use it. Since the value is an
159 * atomically safe type this is just fine. Conceptually its
160 * as if the syscall took an instant longer to occur.
163 struct compat_tms tmp;
164 struct task_struct *tsk = current;
165 struct task_struct *t;
166 cputime_t utime, stime, cutime, cstime;
168 read_lock(&tasklist_lock);
169 utime = tsk->signal->utime;
170 stime = tsk->signal->stime;
173 utime = cputime_add(utime, t->utime);
174 stime = cputime_add(stime, t->stime);
179 * While we have tasklist_lock read-locked, no dying thread
180 * can be updating current->signal->[us]time. Instead,
181 * we got their counts included in the live thread loop.
182 * However, another thread can come in right now and
183 * do a wait call that updates current->signal->c[us]time.
184 * To make sure we always see that pair updated atomically,
185 * we take the siglock around fetching them.
187 spin_lock_irq(&tsk->sighand->siglock);
188 cutime = tsk->signal->cutime;
189 cstime = tsk->signal->cstime;
190 spin_unlock_irq(&tsk->sighand->siglock);
191 read_unlock(&tasklist_lock);
193 tmp.tms_utime = compat_jiffies_to_clock_t(cputime_to_jiffies(utime));
194 tmp.tms_stime = compat_jiffies_to_clock_t(cputime_to_jiffies(stime));
195 tmp.tms_cutime = compat_jiffies_to_clock_t(cputime_to_jiffies(cutime));
196 tmp.tms_cstime = compat_jiffies_to_clock_t(cputime_to_jiffies(cstime));
197 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
200 return compat_jiffies_to_clock_t(jiffies);
204 * Assumption: old_sigset_t and compat_old_sigset_t are both
205 * types that can be passed to put_user()/get_user().
208 asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
212 mm_segment_t old_fs = get_fs();
215 ret = sys_sigpending((old_sigset_t __user *) &s);
218 ret = put_user(s, set);
222 asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set,
223 compat_old_sigset_t __user *oset)
229 if (set && get_user(s, set))
233 ret = sys_sigprocmask(how,
234 set ? (old_sigset_t __user *) &s : NULL,
235 oset ? (old_sigset_t __user *) &s : NULL);
239 ret = put_user(s, oset);
244 asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, int val,
245 struct compat_timespec __user *utime, u32 __user *uaddr2,
249 unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
252 if ((op == FUTEX_WAIT) && utime) {
253 if (get_compat_timespec(&t, utime))
255 timeout = timespec_to_jiffies(&t) + 1;
257 if (op >= FUTEX_REQUEUE)
258 val2 = (int) (unsigned long) utime;
260 return do_futex((unsigned long)uaddr, op, val, timeout,
261 (unsigned long)uaddr2, val2, val3);
265 asmlinkage long compat_sys_setrlimit(unsigned int resource,
266 struct compat_rlimit __user *rlim)
270 mm_segment_t old_fs = get_fs ();
272 if (resource >= RLIM_NLIMITS)
275 if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
276 __get_user(r.rlim_cur, &rlim->rlim_cur) ||
277 __get_user(r.rlim_max, &rlim->rlim_max))
280 if (r.rlim_cur == COMPAT_RLIM_INFINITY)
281 r.rlim_cur = RLIM_INFINITY;
282 if (r.rlim_max == COMPAT_RLIM_INFINITY)
283 r.rlim_max = RLIM_INFINITY;
285 ret = sys_setrlimit(resource, (struct rlimit __user *) &r);
290 #ifdef COMPAT_RLIM_OLD_INFINITY
292 asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
293 struct compat_rlimit __user *rlim)
297 mm_segment_t old_fs = get_fs();
300 ret = sys_old_getrlimit(resource, &r);
304 if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
305 r.rlim_cur = COMPAT_RLIM_INFINITY;
306 if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
307 r.rlim_max = COMPAT_RLIM_INFINITY;
309 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
310 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
311 __put_user(r.rlim_max, &rlim->rlim_max))
319 asmlinkage long compat_sys_getrlimit (unsigned int resource,
320 struct compat_rlimit __user *rlim)
324 mm_segment_t old_fs = get_fs();
327 ret = sys_getrlimit(resource, (struct rlimit __user *) &r);
330 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
331 r.rlim_cur = COMPAT_RLIM_INFINITY;
332 if (r.rlim_max > COMPAT_RLIM_INFINITY)
333 r.rlim_max = COMPAT_RLIM_INFINITY;
335 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
336 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
337 __put_user(r.rlim_max, &rlim->rlim_max))
343 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
345 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
346 __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
347 __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
348 __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
349 __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
350 __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
351 __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
352 __put_user(r->ru_idrss, &ru->ru_idrss) ||
353 __put_user(r->ru_isrss, &ru->ru_isrss) ||
354 __put_user(r->ru_minflt, &ru->ru_minflt) ||
355 __put_user(r->ru_majflt, &ru->ru_majflt) ||
356 __put_user(r->ru_nswap, &ru->ru_nswap) ||
357 __put_user(r->ru_inblock, &ru->ru_inblock) ||
358 __put_user(r->ru_oublock, &ru->ru_oublock) ||
359 __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
360 __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
361 __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
362 __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
363 __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
368 asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
372 mm_segment_t old_fs = get_fs();
375 ret = sys_getrusage(who, (struct rusage __user *) &r);
381 if (put_compat_rusage(&r, ru))
388 compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
389 struct compat_rusage __user *ru)
392 return sys_wait4(pid, stat_addr, options, NULL);
397 mm_segment_t old_fs = get_fs();
402 (unsigned int __user *) &status : NULL),
403 options, (struct rusage __user *) &r);
407 if (put_compat_rusage(&r, ru))
409 if (stat_addr && put_user(status, stat_addr))
416 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
417 unsigned len, cpumask_t *new_mask)
421 if (len < sizeof(cpumask_t))
422 memset(new_mask, 0, sizeof(cpumask_t));
423 else if (len > sizeof(cpumask_t))
424 len = sizeof(cpumask_t);
426 k = cpus_addr(*new_mask);
427 return compat_get_bitmap(k, user_mask_ptr, len * 8);
430 asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
432 compat_ulong_t __user *user_mask_ptr)
437 retval = compat_get_user_cpu_mask(user_mask_ptr, len, &new_mask);
441 return sched_setaffinity(pid, new_mask);
444 asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
445 compat_ulong_t __user *user_mask_ptr)
450 unsigned int min_length = sizeof(cpumask_t);
452 if (NR_CPUS <= BITS_PER_COMPAT_LONG)
453 min_length = sizeof(compat_ulong_t);
455 if (len < min_length)
458 ret = sched_getaffinity(pid, &mask);
463 ret = compat_put_bitmap(user_mask_ptr, k, min_length * 8);
470 static int get_compat_itimerspec(struct itimerspec *dst,
471 struct compat_itimerspec __user *src)
473 if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
474 get_compat_timespec(&dst->it_value, &src->it_value))
479 static int put_compat_itimerspec(struct compat_itimerspec __user *dst,
480 struct itimerspec *src)
482 if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
483 put_compat_timespec(&src->it_value, &dst->it_value))
488 long compat_sys_timer_settime(timer_t timer_id, int flags,
489 struct compat_itimerspec __user *new,
490 struct compat_itimerspec __user *old)
494 struct itimerspec newts, oldts;
498 if (get_compat_itimerspec(&newts, new))
502 err = sys_timer_settime(timer_id, flags,
503 (struct itimerspec __user *) &newts,
504 (struct itimerspec __user *) &oldts);
506 if (!err && old && put_compat_itimerspec(old, &oldts))
511 long compat_sys_timer_gettime(timer_t timer_id,
512 struct compat_itimerspec __user *setting)
516 struct itimerspec ts;
520 err = sys_timer_gettime(timer_id,
521 (struct itimerspec __user *) &ts);
523 if (!err && put_compat_itimerspec(setting, &ts))
528 long compat_sys_clock_settime(clockid_t which_clock,
529 struct compat_timespec __user *tp)
535 if (get_compat_timespec(&ts, tp))
539 err = sys_clock_settime(which_clock,
540 (struct timespec __user *) &ts);
545 long compat_sys_clock_gettime(clockid_t which_clock,
546 struct compat_timespec __user *tp)
554 err = sys_clock_gettime(which_clock,
555 (struct timespec __user *) &ts);
557 if (!err && put_compat_timespec(&ts, tp))
562 long compat_sys_clock_getres(clockid_t which_clock,
563 struct compat_timespec __user *tp)
571 err = sys_clock_getres(which_clock,
572 (struct timespec __user *) &ts);
574 if (!err && tp && put_compat_timespec(&ts, tp))
579 long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
580 struct compat_timespec __user *rqtp,
581 struct compat_timespec __user *rmtp)
585 struct timespec in, out;
587 if (get_compat_timespec(&in, rqtp))
592 err = sys_clock_nanosleep(which_clock, flags,
593 (struct timespec __user *) &in,
594 (struct timespec __user *) &out);
596 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
597 put_compat_timespec(&out, rmtp))
602 /* timer_create is architecture specific because it needs sigevent conversion */
604 long compat_get_bitmap(unsigned long *mask, compat_ulong_t __user *umask,
605 unsigned long bitmap_size)
610 unsigned long nr_compat_longs;
612 /* align bitmap up to nearest compat_long_t boundary */
613 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
615 if (verify_area(VERIFY_READ, umask, bitmap_size / 8))
618 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
620 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
623 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
625 * We dont want to read past the end of the userspace
626 * bitmap. We must however ensure the end of the
627 * kernel bitmap is zeroed.
629 if (nr_compat_longs-- > 0) {
630 if (__get_user(um, umask))
637 m |= (long)um << (j * BITS_PER_COMPAT_LONG);
645 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
646 unsigned long bitmap_size)
651 unsigned long nr_compat_longs;
653 /* align bitmap up to nearest compat_long_t boundary */
654 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
656 if (verify_area(VERIFY_WRITE, umask, bitmap_size / 8))
659 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
661 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
664 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
668 * We dont want to write past the end of the userspace
671 if (nr_compat_longs-- > 0) {
672 if (__put_user(um, umask))
686 sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
688 switch (_NSIG_WORDS) {
689 #if defined (__COMPAT_ENDIAN_SWAP__)
690 case 4: set->sig[3] = compat->sig[7] | (((long)compat->sig[6]) << 32 );
691 case 3: set->sig[2] = compat->sig[5] | (((long)compat->sig[4]) << 32 );
692 case 2: set->sig[1] = compat->sig[3] | (((long)compat->sig[2]) << 32 );
693 case 1: set->sig[0] = compat->sig[1] | (((long)compat->sig[0]) << 32 );
695 case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
696 case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
697 case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
698 case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
704 compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
705 struct compat_siginfo __user *uinfo,
706 struct compat_timespec __user *uts, compat_size_t sigsetsize)
713 long ret, timeout = 0;
715 if (sigsetsize != sizeof(sigset_t))
718 if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
720 sigset_from_compat(&s, &s32);
721 sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
725 if (get_compat_timespec (&t, uts))
727 if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0
732 spin_lock_irq(¤t->sighand->siglock);
733 sig = dequeue_signal(current, &s, &info);
735 timeout = MAX_SCHEDULE_TIMEOUT;
737 timeout = timespec_to_jiffies(&t)
738 +(t.tv_sec || t.tv_nsec);
740 current->real_blocked = current->blocked;
741 sigandsets(¤t->blocked, ¤t->blocked, &s);
744 spin_unlock_irq(¤t->sighand->siglock);
746 current->state = TASK_INTERRUPTIBLE;
747 timeout = schedule_timeout(timeout);
749 spin_lock_irq(¤t->sighand->siglock);
750 sig = dequeue_signal(current, &s, &info);
751 current->blocked = current->real_blocked;
752 siginitset(¤t->real_blocked, 0);
756 spin_unlock_irq(¤t->sighand->siglock);
761 if (copy_siginfo_to_user32(uinfo, &info))
765 ret = timeout?-EINTR:-EAGAIN;
771 #ifdef __ARCH_WANT_COMPAT_SYS_TIME
773 /* compat_time_t is a 32 bit "long" and needs to get converted. */
775 asmlinkage long compat_sys_time(compat_time_t __user * tloc)
780 do_gettimeofday(&tv);
784 if (put_user(i,tloc))
790 asmlinkage long compat_sys_stime(compat_time_t __user *tptr)
795 if (get_user(tv.tv_sec, tptr))
800 err = security_settime(&tv, NULL);
804 do_settimeofday(&tv);
808 #endif /* __ARCH_WANT_COMPAT_SYS_TIME */