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