MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / kernel / compat.c
blob33880d2240775b359dcc52c0017b5e6484a804b8
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/futex.h> /* for FUTEX_WAIT */
21 #include <linux/syscalls.h>
22 #include <linux/unistd.h>
24 #include <asm/uaccess.h>
26 int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
28 return (verify_area(VERIFY_READ, cts, sizeof(*cts)) ||
29 __get_user(ts->tv_sec, &cts->tv_sec) ||
30 __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
33 int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
35 return (verify_area(VERIFY_WRITE, cts, sizeof(*cts)) ||
36 __put_user(ts->tv_sec, &cts->tv_sec) ||
37 __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
40 static long compat_nanosleep_restart(struct restart_block *restart)
42 unsigned long expire = restart->arg0, now = jiffies;
43 struct compat_timespec __user *rmtp;
45 /* Did it expire while we handled signals? */
46 if (!time_after(expire, now))
47 return 0;
49 current->state = TASK_INTERRUPTIBLE;
50 expire = schedule_timeout(expire - now);
51 if (expire == 0)
52 return 0;
54 rmtp = (struct compat_timespec __user *)restart->arg1;
55 if (rmtp) {
56 struct compat_timespec ct;
57 struct timespec t;
59 jiffies_to_timespec(expire, &t);
60 ct.tv_sec = t.tv_sec;
61 ct.tv_nsec = t.tv_nsec;
62 if (copy_to_user(rmtp, &ct, sizeof(ct)))
63 return -EFAULT;
65 /* The 'restart' block is already filled in */
66 return -ERESTART_RESTARTBLOCK;
69 asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
70 struct compat_timespec __user *rmtp)
72 struct timespec t;
73 struct restart_block *restart;
74 unsigned long expire;
76 if (get_compat_timespec(&t, rqtp))
77 return -EFAULT;
79 if ((t.tv_nsec >= 1000000000L) || (t.tv_nsec < 0) || (t.tv_sec < 0))
80 return -EINVAL;
82 expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
83 current->state = TASK_INTERRUPTIBLE;
84 expire = schedule_timeout(expire);
85 if (expire == 0)
86 return 0;
88 if (rmtp) {
89 jiffies_to_timespec(expire, &t);
90 if (put_compat_timespec(&t, rmtp))
91 return -EFAULT;
93 restart = &current_thread_info()->restart_block;
94 restart->fn = compat_nanosleep_restart;
95 restart->arg0 = jiffies + expire;
96 restart->arg1 = (unsigned long) rmtp;
97 return -ERESTART_RESTARTBLOCK;
100 static inline long get_compat_itimerval(struct itimerval *o,
101 struct compat_itimerval __user *i)
103 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
104 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
105 __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
106 __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
107 __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
110 static inline long put_compat_itimerval(struct compat_itimerval __user *o,
111 struct itimerval *i)
113 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
114 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
115 __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
116 __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
117 __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
120 asmlinkage long compat_sys_getitimer(int which,
121 struct compat_itimerval __user *it)
123 struct itimerval kit;
124 int error;
126 error = do_getitimer(which, &kit);
127 if (!error && put_compat_itimerval(it, &kit))
128 error = -EFAULT;
129 return error;
132 asmlinkage long compat_sys_setitimer(int which,
133 struct compat_itimerval __user *in,
134 struct compat_itimerval __user *out)
136 struct itimerval kin, kout;
137 int error;
139 if (in) {
140 if (get_compat_itimerval(&kin, in))
141 return -EFAULT;
142 } else
143 memset(&kin, 0, sizeof(kin));
145 error = do_setitimer(which, &kin, out ? &kout : NULL);
146 if (error || !out)
147 return error;
148 if (put_compat_itimerval(out, &kout))
149 return -EFAULT;
150 return 0;
153 asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
156 * In the SMP world we might just be unlucky and have one of
157 * the times increment as we use it. Since the value is an
158 * atomically safe type this is just fine. Conceptually its
159 * as if the syscall took an instant longer to occur.
161 if (tbuf) {
162 struct compat_tms tmp;
163 struct task_struct *tsk = current;
164 struct task_struct *t;
165 unsigned long utime, stime, cutime, cstime;
167 read_lock(&tasklist_lock);
168 utime = tsk->signal->utime;
169 stime = tsk->signal->stime;
170 t = tsk;
171 do {
172 utime += t->utime;
173 stime += t->stime;
174 t = next_thread(t);
175 } while (t != tsk);
178 * While we have tasklist_lock read-locked, no dying thread
179 * can be updating current->signal->[us]time. Instead,
180 * we got their counts included in the live thread loop.
181 * However, another thread can come in right now and
182 * do a wait call that updates current->signal->c[us]time.
183 * To make sure we always see that pair updated atomically,
184 * we take the siglock around fetching them.
186 spin_lock_irq(&tsk->sighand->siglock);
187 cutime = tsk->signal->cutime;
188 cstime = tsk->signal->cstime;
189 spin_unlock_irq(&tsk->sighand->siglock);
190 read_unlock(&tasklist_lock);
192 tmp.tms_utime = compat_jiffies_to_clock_t(utime);
193 tmp.tms_stime = compat_jiffies_to_clock_t(stime);
194 tmp.tms_cutime = compat_jiffies_to_clock_t(cutime);
195 tmp.tms_cstime = compat_jiffies_to_clock_t(cstime);
196 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
197 return -EFAULT;
199 return compat_jiffies_to_clock_t(jiffies);
203 * Assumption: old_sigset_t and compat_old_sigset_t are both
204 * types that can be passed to put_user()/get_user().
207 asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
209 old_sigset_t s;
210 long ret;
211 mm_segment_t old_fs = get_fs();
213 set_fs(KERNEL_DS);
214 ret = sys_sigpending((old_sigset_t __user *) &s);
215 set_fs(old_fs);
216 if (ret == 0)
217 ret = put_user(s, set);
218 return ret;
221 asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set,
222 compat_old_sigset_t __user *oset)
224 old_sigset_t s;
225 long ret;
226 mm_segment_t old_fs;
228 if (set && get_user(s, set))
229 return -EFAULT;
230 old_fs = get_fs();
231 set_fs(KERNEL_DS);
232 ret = sys_sigprocmask(how,
233 set ? (old_sigset_t __user *) &s : NULL,
234 oset ? (old_sigset_t __user *) &s : NULL);
235 set_fs(old_fs);
236 if (ret == 0)
237 if (oset)
238 ret = put_user(s, oset);
239 return ret;
242 #ifdef CONFIG_FUTEX
243 asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, int val,
244 struct compat_timespec __user *utime, u32 __user *uaddr2,
245 int val3)
247 struct timespec t;
248 unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
249 int val2 = 0;
251 if ((op == FUTEX_WAIT) && utime) {
252 if (get_compat_timespec(&t, utime))
253 return -EFAULT;
254 timeout = timespec_to_jiffies(&t) + 1;
256 if (op >= FUTEX_REQUEUE)
257 val2 = (int) (unsigned long) utime;
259 return do_futex((unsigned long)uaddr, op, val, timeout,
260 (unsigned long)uaddr2, val2, val3);
262 #endif
264 asmlinkage long compat_sys_setrlimit(unsigned int resource,
265 struct compat_rlimit __user *rlim)
267 struct rlimit r;
268 int ret;
269 mm_segment_t old_fs = get_fs ();
271 if (resource >= RLIM_NLIMITS)
272 return -EINVAL;
274 if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
275 __get_user(r.rlim_cur, &rlim->rlim_cur) ||
276 __get_user(r.rlim_max, &rlim->rlim_max))
277 return -EFAULT;
279 if (r.rlim_cur == COMPAT_RLIM_INFINITY)
280 r.rlim_cur = RLIM_INFINITY;
281 if (r.rlim_max == COMPAT_RLIM_INFINITY)
282 r.rlim_max = RLIM_INFINITY;
283 set_fs(KERNEL_DS);
284 ret = sys_setrlimit(resource, (struct rlimit __user *) &r);
285 set_fs(old_fs);
286 return ret;
289 #ifdef COMPAT_RLIM_OLD_INFINITY
291 asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
292 struct compat_rlimit __user *rlim)
294 struct rlimit r;
295 int ret;
296 mm_segment_t old_fs = get_fs();
298 set_fs(KERNEL_DS);
299 ret = sys_old_getrlimit(resource, &r);
300 set_fs(old_fs);
302 if (!ret) {
303 if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
304 r.rlim_cur = COMPAT_RLIM_INFINITY;
305 if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
306 r.rlim_max = COMPAT_RLIM_INFINITY;
308 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
309 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
310 __put_user(r.rlim_max, &rlim->rlim_max))
311 return -EFAULT;
313 return ret;
316 #endif
318 asmlinkage long compat_sys_getrlimit (unsigned int resource,
319 struct compat_rlimit __user *rlim)
321 struct rlimit r;
322 int ret;
323 mm_segment_t old_fs = get_fs();
325 set_fs(KERNEL_DS);
326 ret = sys_getrlimit(resource, (struct rlimit __user *) &r);
327 set_fs(old_fs);
328 if (!ret) {
329 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
330 r.rlim_cur = COMPAT_RLIM_INFINITY;
331 if (r.rlim_max > COMPAT_RLIM_INFINITY)
332 r.rlim_max = COMPAT_RLIM_INFINITY;
334 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
335 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
336 __put_user(r.rlim_max, &rlim->rlim_max))
337 return -EFAULT;
339 return ret;
342 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
344 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
345 __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
346 __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
347 __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
348 __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
349 __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
350 __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
351 __put_user(r->ru_idrss, &ru->ru_idrss) ||
352 __put_user(r->ru_isrss, &ru->ru_isrss) ||
353 __put_user(r->ru_minflt, &ru->ru_minflt) ||
354 __put_user(r->ru_majflt, &ru->ru_majflt) ||
355 __put_user(r->ru_nswap, &ru->ru_nswap) ||
356 __put_user(r->ru_inblock, &ru->ru_inblock) ||
357 __put_user(r->ru_oublock, &ru->ru_oublock) ||
358 __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
359 __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
360 __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
361 __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
362 __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
363 return -EFAULT;
364 return 0;
367 asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
369 struct rusage r;
370 int ret;
371 mm_segment_t old_fs = get_fs();
373 set_fs(KERNEL_DS);
374 ret = sys_getrusage(who, (struct rusage __user *) &r);
375 set_fs(old_fs);
377 if (ret)
378 return ret;
380 if (put_compat_rusage(&r, ru))
381 return -EFAULT;
383 return 0;
386 asmlinkage long
387 compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
388 struct compat_rusage __user *ru)
390 if (!ru) {
391 return sys_wait4(pid, stat_addr, options, NULL);
392 } else {
393 struct rusage r;
394 int ret;
395 unsigned int status;
396 mm_segment_t old_fs = get_fs();
398 set_fs (KERNEL_DS);
399 ret = sys_wait4(pid,
400 (stat_addr ?
401 (unsigned int __user *) &status : NULL),
402 options, (struct rusage __user *) &r);
403 set_fs (old_fs);
405 if (ret > 0) {
406 if (put_compat_rusage(&r, ru))
407 return -EFAULT;
408 if (stat_addr && put_user(status, stat_addr))
409 return -EFAULT;
411 return ret;
415 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
416 unsigned len, cpumask_t *new_mask)
418 unsigned long *k;
420 if (len < sizeof(cpumask_t))
421 memset(new_mask, 0, sizeof(cpumask_t));
422 else if (len > sizeof(cpumask_t))
423 len = sizeof(cpumask_t);
425 k = cpus_addr(*new_mask);
426 return compat_get_bitmap(k, user_mask_ptr, len * 8);
429 asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
430 unsigned int len,
431 compat_ulong_t __user *user_mask_ptr)
433 cpumask_t new_mask;
434 int retval;
436 retval = compat_get_user_cpu_mask(user_mask_ptr, len, &new_mask);
437 if (retval)
438 return retval;
440 return sched_setaffinity(pid, new_mask);
443 asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
444 compat_ulong_t __user *user_mask_ptr)
446 int ret;
447 cpumask_t mask;
448 unsigned long *k;
449 unsigned int min_length = sizeof(cpumask_t);
451 if (NR_CPUS <= BITS_PER_COMPAT_LONG)
452 min_length = sizeof(compat_ulong_t);
454 if (len < min_length)
455 return -EINVAL;
457 ret = sched_getaffinity(pid, &mask);
458 if (ret < 0)
459 return ret;
461 k = cpus_addr(mask);
462 ret = compat_put_bitmap(user_mask_ptr, k, min_length * 8);
463 if (ret)
464 return ret;
466 return min_length;
469 static int get_compat_itimerspec(struct itimerspec *dst,
470 struct compat_itimerspec __user *src)
472 if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
473 get_compat_timespec(&dst->it_value, &src->it_value))
474 return -EFAULT;
475 return 0;
478 static int put_compat_itimerspec(struct compat_itimerspec __user *dst,
479 struct itimerspec *src)
481 if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
482 put_compat_timespec(&src->it_value, &dst->it_value))
483 return -EFAULT;
484 return 0;
487 long compat_timer_settime(timer_t timer_id, int flags,
488 struct compat_itimerspec __user *new,
489 struct compat_itimerspec __user *old)
491 long err;
492 mm_segment_t oldfs;
493 struct itimerspec newts, oldts;
495 if (!new)
496 return -EINVAL;
497 if (get_compat_itimerspec(&newts, new))
498 return -EFAULT;
499 oldfs = get_fs();
500 set_fs(KERNEL_DS);
501 err = sys_timer_settime(timer_id, flags,
502 (struct itimerspec __user *) &newts,
503 (struct itimerspec __user *) &oldts);
504 set_fs(oldfs);
505 if (!err && old && put_compat_itimerspec(old, &oldts))
506 return -EFAULT;
507 return err;
510 long compat_timer_gettime(timer_t timer_id,
511 struct compat_itimerspec __user *setting)
513 long err;
514 mm_segment_t oldfs;
515 struct itimerspec ts;
517 oldfs = get_fs();
518 set_fs(KERNEL_DS);
519 err = sys_timer_gettime(timer_id,
520 (struct itimerspec __user *) &ts);
521 set_fs(oldfs);
522 if (!err && put_compat_itimerspec(setting, &ts))
523 return -EFAULT;
524 return err;
527 long compat_clock_settime(clockid_t which_clock,
528 struct compat_timespec __user *tp)
530 long err;
531 mm_segment_t oldfs;
532 struct timespec ts;
534 if (get_compat_timespec(&ts, tp))
535 return -EFAULT;
536 oldfs = get_fs();
537 set_fs(KERNEL_DS);
538 err = sys_clock_settime(which_clock,
539 (struct timespec __user *) &ts);
540 set_fs(oldfs);
541 return err;
544 long compat_clock_gettime(clockid_t which_clock,
545 struct compat_timespec __user *tp)
547 long err;
548 mm_segment_t oldfs;
549 struct timespec ts;
551 oldfs = get_fs();
552 set_fs(KERNEL_DS);
553 err = sys_clock_gettime(which_clock,
554 (struct timespec __user *) &ts);
555 set_fs(oldfs);
556 if (!err && put_compat_timespec(&ts, tp))
557 return -EFAULT;
558 return err;
561 long compat_clock_getres(clockid_t which_clock,
562 struct compat_timespec __user *tp)
564 long err;
565 mm_segment_t oldfs;
566 struct timespec ts;
568 oldfs = get_fs();
569 set_fs(KERNEL_DS);
570 err = sys_clock_getres(which_clock,
571 (struct timespec __user *) &ts);
572 set_fs(oldfs);
573 if (!err && tp && put_compat_timespec(&ts, tp))
574 return -EFAULT;
575 return err;
578 long compat_clock_nanosleep(clockid_t which_clock, int flags,
579 struct compat_timespec __user *rqtp,
580 struct compat_timespec __user *rmtp)
582 long err;
583 mm_segment_t oldfs;
584 struct timespec in, out;
586 if (get_compat_timespec(&in, rqtp))
587 return -EFAULT;
589 oldfs = get_fs();
590 set_fs(KERNEL_DS);
591 err = sys_clock_nanosleep(which_clock, flags,
592 (struct timespec __user *) &in,
593 (struct timespec __user *) &out);
594 set_fs(oldfs);
595 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
596 put_compat_timespec(&out, rmtp))
597 return -EFAULT;
598 return err;
601 /* timer_create is architecture specific because it needs sigevent conversion */
603 long compat_get_bitmap(unsigned long *mask, compat_ulong_t __user *umask,
604 unsigned long bitmap_size)
606 int i, j;
607 unsigned long m;
608 compat_ulong_t um;
609 unsigned long nr_compat_longs;
611 /* align bitmap up to nearest compat_long_t boundary */
612 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
614 if (verify_area(VERIFY_READ, umask, bitmap_size / 8))
615 return -EFAULT;
617 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
619 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
620 m = 0;
622 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
624 * We dont want to read past the end of the userspace
625 * bitmap. We must however ensure the end of the
626 * kernel bitmap is zeroed.
628 if (nr_compat_longs-- > 0) {
629 if (__get_user(um, umask))
630 return -EFAULT;
631 } else {
632 um = 0;
635 umask++;
636 m |= (long)um << (j * BITS_PER_COMPAT_LONG);
638 *mask++ = m;
641 return 0;
644 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
645 unsigned long bitmap_size)
647 int i, j;
648 unsigned long m;
649 compat_ulong_t um;
650 unsigned long nr_compat_longs;
652 /* align bitmap up to nearest compat_long_t boundary */
653 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
655 if (verify_area(VERIFY_WRITE, umask, bitmap_size / 8))
656 return -EFAULT;
658 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
660 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
661 m = *mask++;
663 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
664 um = m;
667 * We dont want to write past the end of the userspace
668 * bitmap.
670 if (nr_compat_longs-- > 0) {
671 if (__put_user(um, umask))
672 return -EFAULT;
675 umask++;
676 m >>= 4*sizeof(um);
677 m >>= 4*sizeof(um);
681 return 0;