blob: b9bdd1271f4497f07c587f9f1d6fa78f5c76292e [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 */
20#include <linux/futex.h> /* for FUTEX_WAIT */
21#include <linux/syscalls.h>
22#include <linux/unistd.h>
23#include <linux/security.h>
Stephen Rothwell3158e942006-03-26 01:37:29 -080024#include <linux/timex.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
242#ifdef CONFIG_FUTEX
243asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, int val,
244 struct compat_timespec __user *utime, u32 __user *uaddr2,
245 int val3)
246{
247 struct timespec t;
248 unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
249 int val2 = 0;
250
251 if ((op == FUTEX_WAIT) && utime) {
252 if (get_compat_timespec(&t, utime))
253 return -EFAULT;
254 timeout = timespec_to_jiffies(&t) + 1;
255 }
256 if (op >= FUTEX_REQUEUE)
257 val2 = (int) (unsigned long) utime;
258
259 return do_futex((unsigned long)uaddr, op, val, timeout,
260 (unsigned long)uaddr2, val2, val3);
261}
262#endif
263
264asmlinkage long compat_sys_setrlimit(unsigned int resource,
265 struct compat_rlimit __user *rlim)
266{
267 struct rlimit r;
268 int ret;
269 mm_segment_t old_fs = get_fs ();
270
271 if (resource >= RLIM_NLIMITS)
272 return -EINVAL;
273
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;
278
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;
287}
288
289#ifdef COMPAT_RLIM_OLD_INFINITY
290
291asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
292 struct compat_rlimit __user *rlim)
293{
294 struct rlimit r;
295 int ret;
296 mm_segment_t old_fs = get_fs();
297
298 set_fs(KERNEL_DS);
299 ret = sys_old_getrlimit(resource, &r);
300 set_fs(old_fs);
301
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;
307
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;
312 }
313 return ret;
314}
315
316#endif
317
318asmlinkage long compat_sys_getrlimit (unsigned int resource,
319 struct compat_rlimit __user *rlim)
320{
321 struct rlimit r;
322 int ret;
323 mm_segment_t old_fs = get_fs();
324
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;
333
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;
338 }
339 return ret;
340}
341
342int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
343{
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;
365}
366
367asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
368{
369 struct rusage r;
370 int ret;
371 mm_segment_t old_fs = get_fs();
372
373 set_fs(KERNEL_DS);
374 ret = sys_getrusage(who, (struct rusage __user *) &r);
375 set_fs(old_fs);
376
377 if (ret)
378 return ret;
379
380 if (put_compat_rusage(&r, ru))
381 return -EFAULT;
382
383 return 0;
384}
385
386asmlinkage long
387compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
388 struct compat_rusage __user *ru)
389{
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();
397
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);
404
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;
410 }
411 return ret;
412 }
413}
414
415asmlinkage long compat_sys_waitid(int which, compat_pid_t pid,
416 struct compat_siginfo __user *uinfo, int options,
417 struct compat_rusage __user *uru)
418{
419 siginfo_t info;
420 struct rusage ru;
421 long ret;
422 mm_segment_t old_fs = get_fs();
423
424 memset(&info, 0, sizeof(info));
425
426 set_fs(KERNEL_DS);
427 ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
428 uru ? (struct rusage __user *)&ru : NULL);
429 set_fs(old_fs);
430
431 if ((ret < 0) || (info.si_signo == 0))
432 return ret;
433
434 if (uru) {
435 ret = put_compat_rusage(&ru, uru);
436 if (ret)
437 return ret;
438 }
439
440 BUG_ON(info.si_code & __SI_MASK);
441 info.si_code |= __SI_CHLD;
442 return copy_siginfo_to_user32(uinfo, &info);
443}
444
445static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
446 unsigned len, cpumask_t *new_mask)
447{
448 unsigned long *k;
449
450 if (len < sizeof(cpumask_t))
451 memset(new_mask, 0, sizeof(cpumask_t));
452 else if (len > sizeof(cpumask_t))
453 len = sizeof(cpumask_t);
454
455 k = cpus_addr(*new_mask);
456 return compat_get_bitmap(k, user_mask_ptr, len * 8);
457}
458
459asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
460 unsigned int len,
461 compat_ulong_t __user *user_mask_ptr)
462{
463 cpumask_t new_mask;
464 int retval;
465
466 retval = compat_get_user_cpu_mask(user_mask_ptr, len, &new_mask);
467 if (retval)
468 return retval;
469
470 return sched_setaffinity(pid, new_mask);
471}
472
473asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
474 compat_ulong_t __user *user_mask_ptr)
475{
476 int ret;
477 cpumask_t mask;
478 unsigned long *k;
479 unsigned int min_length = sizeof(cpumask_t);
480
481 if (NR_CPUS <= BITS_PER_COMPAT_LONG)
482 min_length = sizeof(compat_ulong_t);
483
484 if (len < min_length)
485 return -EINVAL;
486
487 ret = sched_getaffinity(pid, &mask);
488 if (ret < 0)
489 return ret;
490
491 k = cpus_addr(mask);
492 ret = compat_put_bitmap(user_mask_ptr, k, min_length * 8);
493 if (ret)
494 return ret;
495
496 return min_length;
497}
498
499static int get_compat_itimerspec(struct itimerspec *dst,
500 struct compat_itimerspec __user *src)
501{
502 if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
503 get_compat_timespec(&dst->it_value, &src->it_value))
504 return -EFAULT;
505 return 0;
506}
507
508static int put_compat_itimerspec(struct compat_itimerspec __user *dst,
509 struct itimerspec *src)
510{
511 if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
512 put_compat_timespec(&src->it_value, &dst->it_value))
513 return -EFAULT;
514 return 0;
515}
516
Christoph Hellwig3a0f69d2006-01-09 20:52:08 -0800517long compat_sys_timer_create(clockid_t which_clock,
518 struct compat_sigevent __user *timer_event_spec,
519 timer_t __user *created_timer_id)
520{
521 struct sigevent __user *event = NULL;
522
523 if (timer_event_spec) {
524 struct sigevent kevent;
525
526 event = compat_alloc_user_space(sizeof(*event));
527 if (get_compat_sigevent(&kevent, timer_event_spec) ||
528 copy_to_user(event, &kevent, sizeof(*event)))
529 return -EFAULT;
530 }
531
532 return sys_timer_create(which_clock, event, created_timer_id);
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535long compat_sys_timer_settime(timer_t timer_id, int flags,
536 struct compat_itimerspec __user *new,
537 struct compat_itimerspec __user *old)
538{
539 long err;
540 mm_segment_t oldfs;
541 struct itimerspec newts, oldts;
542
543 if (!new)
544 return -EINVAL;
545 if (get_compat_itimerspec(&newts, new))
546 return -EFAULT;
547 oldfs = get_fs();
548 set_fs(KERNEL_DS);
549 err = sys_timer_settime(timer_id, flags,
550 (struct itimerspec __user *) &newts,
551 (struct itimerspec __user *) &oldts);
552 set_fs(oldfs);
553 if (!err && old && put_compat_itimerspec(old, &oldts))
554 return -EFAULT;
555 return err;
556}
557
558long compat_sys_timer_gettime(timer_t timer_id,
559 struct compat_itimerspec __user *setting)
560{
561 long err;
562 mm_segment_t oldfs;
563 struct itimerspec ts;
564
565 oldfs = get_fs();
566 set_fs(KERNEL_DS);
567 err = sys_timer_gettime(timer_id,
568 (struct itimerspec __user *) &ts);
569 set_fs(oldfs);
570 if (!err && put_compat_itimerspec(setting, &ts))
571 return -EFAULT;
572 return err;
573}
574
575long compat_sys_clock_settime(clockid_t which_clock,
576 struct compat_timespec __user *tp)
577{
578 long err;
579 mm_segment_t oldfs;
580 struct timespec ts;
581
582 if (get_compat_timespec(&ts, tp))
583 return -EFAULT;
584 oldfs = get_fs();
585 set_fs(KERNEL_DS);
586 err = sys_clock_settime(which_clock,
587 (struct timespec __user *) &ts);
588 set_fs(oldfs);
589 return err;
590}
591
592long compat_sys_clock_gettime(clockid_t which_clock,
593 struct compat_timespec __user *tp)
594{
595 long err;
596 mm_segment_t oldfs;
597 struct timespec ts;
598
599 oldfs = get_fs();
600 set_fs(KERNEL_DS);
601 err = sys_clock_gettime(which_clock,
602 (struct timespec __user *) &ts);
603 set_fs(oldfs);
604 if (!err && put_compat_timespec(&ts, tp))
605 return -EFAULT;
606 return err;
607}
608
609long compat_sys_clock_getres(clockid_t which_clock,
610 struct compat_timespec __user *tp)
611{
612 long err;
613 mm_segment_t oldfs;
614 struct timespec ts;
615
616 oldfs = get_fs();
617 set_fs(KERNEL_DS);
618 err = sys_clock_getres(which_clock,
619 (struct timespec __user *) &ts);
620 set_fs(oldfs);
621 if (!err && tp && put_compat_timespec(&ts, tp))
622 return -EFAULT;
623 return err;
624}
625
626long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
627 struct compat_timespec __user *rqtp,
628 struct compat_timespec __user *rmtp)
629{
630 long err;
631 mm_segment_t oldfs;
632 struct timespec in, out;
633
634 if (get_compat_timespec(&in, rqtp))
635 return -EFAULT;
636
637 oldfs = get_fs();
638 set_fs(KERNEL_DS);
639 err = sys_clock_nanosleep(which_clock, flags,
640 (struct timespec __user *) &in,
641 (struct timespec __user *) &out);
642 set_fs(oldfs);
643 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
644 put_compat_timespec(&out, rmtp))
645 return -EFAULT;
646 return err;
647}
648
649/*
650 * We currently only need the following fields from the sigevent
651 * structure: sigev_value, sigev_signo, sig_notify and (sometimes
652 * sigev_notify_thread_id). The others are handled in user mode.
653 * We also assume that copying sigev_value.sival_int is sufficient
654 * to keep all the bits of sigev_value.sival_ptr intact.
655 */
656int get_compat_sigevent(struct sigevent *event,
657 const struct compat_sigevent __user *u_event)
658{
David S. Miller51410d32005-04-16 15:24:01 -0700659 memset(event, 0, sizeof(*event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
661 __get_user(event->sigev_value.sival_int,
662 &u_event->sigev_value.sival_int) ||
663 __get_user(event->sigev_signo, &u_event->sigev_signo) ||
664 __get_user(event->sigev_notify, &u_event->sigev_notify) ||
665 __get_user(event->sigev_notify_thread_id,
666 &u_event->sigev_notify_thread_id))
667 ? -EFAULT : 0;
668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670long compat_get_bitmap(unsigned long *mask, compat_ulong_t __user *umask,
671 unsigned long bitmap_size)
672{
673 int i, j;
674 unsigned long m;
675 compat_ulong_t um;
676 unsigned long nr_compat_longs;
677
678 /* align bitmap up to nearest compat_long_t boundary */
679 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
680
681 if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
682 return -EFAULT;
683
684 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
685
686 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
687 m = 0;
688
689 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
690 /*
691 * We dont want to read past the end of the userspace
692 * bitmap. We must however ensure the end of the
693 * kernel bitmap is zeroed.
694 */
695 if (nr_compat_longs-- > 0) {
696 if (__get_user(um, umask))
697 return -EFAULT;
698 } else {
699 um = 0;
700 }
701
702 umask++;
703 m |= (long)um << (j * BITS_PER_COMPAT_LONG);
704 }
705 *mask++ = m;
706 }
707
708 return 0;
709}
710
711long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
712 unsigned long bitmap_size)
713{
714 int i, j;
715 unsigned long m;
716 compat_ulong_t um;
717 unsigned long nr_compat_longs;
718
719 /* align bitmap up to nearest compat_long_t boundary */
720 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
721
722 if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
723 return -EFAULT;
724
725 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
726
727 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
728 m = *mask++;
729
730 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
731 um = m;
732
733 /*
734 * We dont want to write past the end of the userspace
735 * bitmap.
736 */
737 if (nr_compat_longs-- > 0) {
738 if (__put_user(um, umask))
739 return -EFAULT;
740 }
741
742 umask++;
743 m >>= 4*sizeof(um);
744 m >>= 4*sizeof(um);
745 }
746 }
747
748 return 0;
749}
750
751void
752sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
753{
754 switch (_NSIG_WORDS) {
755#if defined (__COMPAT_ENDIAN_SWAP__)
756 case 4: set->sig[3] = compat->sig[7] | (((long)compat->sig[6]) << 32 );
757 case 3: set->sig[2] = compat->sig[5] | (((long)compat->sig[4]) << 32 );
758 case 2: set->sig[1] = compat->sig[3] | (((long)compat->sig[2]) << 32 );
759 case 1: set->sig[0] = compat->sig[1] | (((long)compat->sig[0]) << 32 );
760#else
761 case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
762 case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
763 case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
764 case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
765#endif
766 }
767}
768
769asmlinkage long
770compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
771 struct compat_siginfo __user *uinfo,
772 struct compat_timespec __user *uts, compat_size_t sigsetsize)
773{
774 compat_sigset_t s32;
775 sigset_t s;
776 int sig;
777 struct timespec t;
778 siginfo_t info;
779 long ret, timeout = 0;
780
781 if (sigsetsize != sizeof(sigset_t))
782 return -EINVAL;
783
784 if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
785 return -EFAULT;
786 sigset_from_compat(&s, &s32);
787 sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
788 signotset(&s);
789
790 if (uts) {
791 if (get_compat_timespec (&t, uts))
792 return -EFAULT;
793 if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0
794 || t.tv_sec < 0)
795 return -EINVAL;
796 }
797
798 spin_lock_irq(&current->sighand->siglock);
799 sig = dequeue_signal(current, &s, &info);
800 if (!sig) {
801 timeout = MAX_SCHEDULE_TIMEOUT;
802 if (uts)
803 timeout = timespec_to_jiffies(&t)
804 +(t.tv_sec || t.tv_nsec);
805 if (timeout) {
806 current->real_blocked = current->blocked;
807 sigandsets(&current->blocked, &current->blocked, &s);
808
809 recalc_sigpending();
810 spin_unlock_irq(&current->sighand->siglock);
811
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -0700812 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 spin_lock_irq(&current->sighand->siglock);
815 sig = dequeue_signal(current, &s, &info);
816 current->blocked = current->real_blocked;
817 siginitset(&current->real_blocked, 0);
818 recalc_sigpending();
819 }
820 }
821 spin_unlock_irq(&current->sighand->siglock);
822
823 if (sig) {
824 ret = sig;
825 if (uinfo) {
826 if (copy_siginfo_to_user32(uinfo, &info))
827 ret = -EFAULT;
828 }
829 }else {
830 ret = timeout?-EINTR:-EAGAIN;
831 }
832 return ret;
833
834}
835
836#ifdef __ARCH_WANT_COMPAT_SYS_TIME
837
838/* compat_time_t is a 32 bit "long" and needs to get converted. */
839
840asmlinkage long compat_sys_time(compat_time_t __user * tloc)
841{
842 compat_time_t i;
843 struct timeval tv;
844
845 do_gettimeofday(&tv);
846 i = tv.tv_sec;
847
848 if (tloc) {
849 if (put_user(i,tloc))
850 i = -EFAULT;
851 }
852 return i;
853}
854
855asmlinkage long compat_sys_stime(compat_time_t __user *tptr)
856{
857 struct timespec tv;
858 int err;
859
860 if (get_user(tv.tv_sec, tptr))
861 return -EFAULT;
862
863 tv.tv_nsec = 0;
864
865 err = security_settime(&tv, NULL);
866 if (err)
867 return err;
868
869 do_settimeofday(&tv);
870 return 0;
871}
872
873#endif /* __ARCH_WANT_COMPAT_SYS_TIME */
David Woodhouse150256d2006-01-18 17:43:57 -0800874
875#ifdef __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND
876asmlinkage long compat_sys_rt_sigsuspend(compat_sigset_t __user *unewset, compat_size_t sigsetsize)
877{
878 sigset_t newset;
879 compat_sigset_t newset32;
880
881 /* XXX: Don't preclude handling different sized sigset_t's. */
882 if (sigsetsize != sizeof(sigset_t))
883 return -EINVAL;
884
885 if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
886 return -EFAULT;
887 sigset_from_compat(&newset, &newset32);
888 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
889
890 spin_lock_irq(&current->sighand->siglock);
891 current->saved_sigmask = current->blocked;
892 current->blocked = newset;
893 recalc_sigpending();
894 spin_unlock_irq(&current->sighand->siglock);
895
896 current->state = TASK_INTERRUPTIBLE;
897 schedule();
898 set_thread_flag(TIF_RESTORE_SIGMASK);
899 return -ERESTARTNOHAND;
900}
901#endif /* __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND */
Stephen Rothwell3158e942006-03-26 01:37:29 -0800902
903asmlinkage long compat_sys_adjtimex(struct compat_timex __user *utp)
904{
905 struct timex txc;
906 int ret;
907
908 memset(&txc, 0, sizeof(struct timex));
909
910 if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
911 __get_user(txc.modes, &utp->modes) ||
912 __get_user(txc.offset, &utp->offset) ||
913 __get_user(txc.freq, &utp->freq) ||
914 __get_user(txc.maxerror, &utp->maxerror) ||
915 __get_user(txc.esterror, &utp->esterror) ||
916 __get_user(txc.status, &utp->status) ||
917 __get_user(txc.constant, &utp->constant) ||
918 __get_user(txc.precision, &utp->precision) ||
919 __get_user(txc.tolerance, &utp->tolerance) ||
920 __get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
921 __get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
922 __get_user(txc.tick, &utp->tick) ||
923 __get_user(txc.ppsfreq, &utp->ppsfreq) ||
924 __get_user(txc.jitter, &utp->jitter) ||
925 __get_user(txc.shift, &utp->shift) ||
926 __get_user(txc.stabil, &utp->stabil) ||
927 __get_user(txc.jitcnt, &utp->jitcnt) ||
928 __get_user(txc.calcnt, &utp->calcnt) ||
929 __get_user(txc.errcnt, &utp->errcnt) ||
930 __get_user(txc.stbcnt, &utp->stbcnt))
931 return -EFAULT;
932
933 ret = do_adjtimex(&txc);
934
935 if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
936 __put_user(txc.modes, &utp->modes) ||
937 __put_user(txc.offset, &utp->offset) ||
938 __put_user(txc.freq, &utp->freq) ||
939 __put_user(txc.maxerror, &utp->maxerror) ||
940 __put_user(txc.esterror, &utp->esterror) ||
941 __put_user(txc.status, &utp->status) ||
942 __put_user(txc.constant, &utp->constant) ||
943 __put_user(txc.precision, &utp->precision) ||
944 __put_user(txc.tolerance, &utp->tolerance) ||
945 __put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
946 __put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
947 __put_user(txc.tick, &utp->tick) ||
948 __put_user(txc.ppsfreq, &utp->ppsfreq) ||
949 __put_user(txc.jitter, &utp->jitter) ||
950 __put_user(txc.shift, &utp->shift) ||
951 __put_user(txc.stabil, &utp->stabil) ||
952 __put_user(txc.jitcnt, &utp->jitcnt) ||
953 __put_user(txc.calcnt, &utp->calcnt) ||
954 __put_user(txc.errcnt, &utp->errcnt) ||
955 __put_user(txc.stbcnt, &utp->stbcnt))
956 ret = -EFAULT;
957
958 return ret;
959}