[PATCH] create struct compat_timex and use it everywhere
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / compat.c
CommitLineData
1da177e4
LT
1/*
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>
24
25#include <asm/uaccess.h>
1da177e4
LT
26
27int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
28{
29 return (!access_ok(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;
32}
33
34int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
35{
36 return (!access_ok(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;
39}
40
41static long compat_nanosleep_restart(struct restart_block *restart)
42{
43 unsigned long expire = restart->arg0, now = jiffies;
44 struct compat_timespec __user *rmtp;
45
46 /* Did it expire while we handled signals? */
47 if (!time_after(expire, now))
48 return 0;
49
75bcc8c5 50 expire = schedule_timeout_interruptible(expire - now);
1da177e4
LT
51 if (expire == 0)
52 return 0;
53
54 rmtp = (struct compat_timespec __user *)restart->arg1;
55 if (rmtp) {
56 struct compat_timespec ct;
57 struct timespec t;
58
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;
64 }
65 /* The 'restart' block is already filled in */
66 return -ERESTART_RESTARTBLOCK;
67}
68
69asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
70 struct compat_timespec __user *rmtp)
71{
72 struct timespec t;
73 struct restart_block *restart;
74 unsigned long expire;
75
76 if (get_compat_timespec(&t, rqtp))
77 return -EFAULT;
78
79 if ((t.tv_nsec >= 1000000000L) || (t.tv_nsec < 0) || (t.tv_sec < 0))
80 return -EINVAL;
81
82 expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
75bcc8c5 83 expire = schedule_timeout_interruptible(expire);
1da177e4
LT
84 if (expire == 0)
85 return 0;
86
87 if (rmtp) {
88 jiffies_to_timespec(expire, &t);
89 if (put_compat_timespec(&t, rmtp))
90 return -EFAULT;
91 }
92 restart = &current_thread_info()->restart_block;
93 restart->fn = compat_nanosleep_restart;
94 restart->arg0 = jiffies + expire;
95 restart->arg1 = (unsigned long) rmtp;
96 return -ERESTART_RESTARTBLOCK;
97}
98
99static inline long get_compat_itimerval(struct itimerval *o,
100 struct compat_itimerval __user *i)
101{
102 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
103 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
104 __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
105 __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
106 __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
107}
108
109static inline long put_compat_itimerval(struct compat_itimerval __user *o,
110 struct itimerval *i)
111{
112 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
113 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
114 __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
115 __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
116 __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
117}
118
119asmlinkage long compat_sys_getitimer(int which,
120 struct compat_itimerval __user *it)
121{
122 struct itimerval kit;
123 int error;
124
125 error = do_getitimer(which, &kit);
126 if (!error && put_compat_itimerval(it, &kit))
127 error = -EFAULT;
128 return error;
129}
130
131asmlinkage long compat_sys_setitimer(int which,
132 struct compat_itimerval __user *in,
133 struct compat_itimerval __user *out)
134{
135 struct itimerval kin, kout;
136 int error;
137
138 if (in) {
139 if (get_compat_itimerval(&kin, in))
140 return -EFAULT;
141 } else
142 memset(&kin, 0, sizeof(kin));
143
144 error = do_setitimer(which, &kin, out ? &kout : NULL);
145 if (error || !out)
146 return error;
147 if (put_compat_itimerval(out, &kout))
148 return -EFAULT;
149 return 0;
150}
151
152asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
153{
154 /*
155 * In the SMP world we might just be unlucky and have one of
156 * the times increment as we use it. Since the value is an
157 * atomically safe type this is just fine. Conceptually its
158 * as if the syscall took an instant longer to occur.
159 */
160 if (tbuf) {
161 struct compat_tms tmp;
162 struct task_struct *tsk = current;
163 struct task_struct *t;
164 cputime_t utime, stime, cutime, cstime;
165
166 read_lock(&tasklist_lock);
167 utime = tsk->signal->utime;
168 stime = tsk->signal->stime;
169 t = tsk;
170 do {
171 utime = cputime_add(utime, t->utime);
172 stime = cputime_add(stime, t->stime);
173 t = next_thread(t);
174 } while (t != tsk);
175
176 /*
177 * While we have tasklist_lock read-locked, no dying thread
178 * can be updating current->signal->[us]time. Instead,
179 * we got their counts included in the live thread loop.
180 * However, another thread can come in right now and
181 * do a wait call that updates current->signal->c[us]time.
182 * To make sure we always see that pair updated atomically,
183 * we take the siglock around fetching them.
184 */
185 spin_lock_irq(&tsk->sighand->siglock);
186 cutime = tsk->signal->cutime;
187 cstime = tsk->signal->cstime;
188 spin_unlock_irq(&tsk->sighand->siglock);
189 read_unlock(&tasklist_lock);
190
191 tmp.tms_utime = compat_jiffies_to_clock_t(cputime_to_jiffies(utime));
192 tmp.tms_stime = compat_jiffies_to_clock_t(cputime_to_jiffies(stime));
193 tmp.tms_cutime = compat_jiffies_to_clock_t(cputime_to_jiffies(cutime));
194 tmp.tms_cstime = compat_jiffies_to_clock_t(cputime_to_jiffies(cstime));
195 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
196 return -EFAULT;
197 }
198 return compat_jiffies_to_clock_t(jiffies);
199}
200
201/*
202 * Assumption: old_sigset_t and compat_old_sigset_t are both
203 * types that can be passed to put_user()/get_user().
204 */
205
206asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
207{
208 old_sigset_t s;
209 long ret;
210 mm_segment_t old_fs = get_fs();
211
212 set_fs(KERNEL_DS);
213 ret = sys_sigpending((old_sigset_t __user *) &s);
214 set_fs(old_fs);
215 if (ret == 0)
216 ret = put_user(s, set);
217 return ret;
218}
219
220asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set,
221 compat_old_sigset_t __user *oset)
222{
223 old_sigset_t s;
224 long ret;
225 mm_segment_t old_fs;
226
227 if (set && get_user(s, set))
228 return -EFAULT;
229 old_fs = get_fs();
230 set_fs(KERNEL_DS);
231 ret = sys_sigprocmask(how,
232 set ? (old_sigset_t __user *) &s : NULL,
233 oset ? (old_sigset_t __user *) &s : NULL);
234 set_fs(old_fs);
235 if (ret == 0)
236 if (oset)
237 ret = put_user(s, oset);
238 return ret;
239}
240
241#ifdef CONFIG_FUTEX
242asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, int val,
243 struct compat_timespec __user *utime, u32 __user *uaddr2,
244 int val3)
245{
246 struct timespec t;
247 unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
248 int val2 = 0;
249
250 if ((op == FUTEX_WAIT) && utime) {
251 if (get_compat_timespec(&t, utime))
252 return -EFAULT;
253 timeout = timespec_to_jiffies(&t) + 1;
254 }
255 if (op >= FUTEX_REQUEUE)
256 val2 = (int) (unsigned long) utime;
257
258 return do_futex((unsigned long)uaddr, op, val, timeout,
259 (unsigned long)uaddr2, val2, val3);
260}
261#endif
262
263asmlinkage long compat_sys_setrlimit(unsigned int resource,
264 struct compat_rlimit __user *rlim)
265{
266 struct rlimit r;
267 int ret;
268 mm_segment_t old_fs = get_fs ();
269
270 if (resource >= RLIM_NLIMITS)
271 return -EINVAL;
272
273 if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
274 __get_user(r.rlim_cur, &rlim->rlim_cur) ||
275 __get_user(r.rlim_max, &rlim->rlim_max))
276 return -EFAULT;
277
278 if (r.rlim_cur == COMPAT_RLIM_INFINITY)
279 r.rlim_cur = RLIM_INFINITY;
280 if (r.rlim_max == COMPAT_RLIM_INFINITY)
281 r.rlim_max = RLIM_INFINITY;
282 set_fs(KERNEL_DS);
283 ret = sys_setrlimit(resource, (struct rlimit __user *) &r);
284 set_fs(old_fs);
285 return ret;
286}
287
288#ifdef COMPAT_RLIM_OLD_INFINITY
289
290asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
291 struct compat_rlimit __user *rlim)
292{
293 struct rlimit r;
294 int ret;
295 mm_segment_t old_fs = get_fs();
296
297 set_fs(KERNEL_DS);
298 ret = sys_old_getrlimit(resource, &r);
299 set_fs(old_fs);
300
301 if (!ret) {
302 if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
303 r.rlim_cur = COMPAT_RLIM_INFINITY;
304 if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
305 r.rlim_max = COMPAT_RLIM_INFINITY;
306
307 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
308 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
309 __put_user(r.rlim_max, &rlim->rlim_max))
310 return -EFAULT;
311 }
312 return ret;
313}
314
315#endif
316
317asmlinkage long compat_sys_getrlimit (unsigned int resource,
318 struct compat_rlimit __user *rlim)
319{
320 struct rlimit r;
321 int ret;
322 mm_segment_t old_fs = get_fs();
323
324 set_fs(KERNEL_DS);
325 ret = sys_getrlimit(resource, (struct rlimit __user *) &r);
326 set_fs(old_fs);
327 if (!ret) {
328 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
329 r.rlim_cur = COMPAT_RLIM_INFINITY;
330 if (r.rlim_max > COMPAT_RLIM_INFINITY)
331 r.rlim_max = COMPAT_RLIM_INFINITY;
332
333 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
334 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
335 __put_user(r.rlim_max, &rlim->rlim_max))
336 return -EFAULT;
337 }
338 return ret;
339}
340
341int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
342{
343 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
344 __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
345 __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
346 __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
347 __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
348 __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
349 __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
350 __put_user(r->ru_idrss, &ru->ru_idrss) ||
351 __put_user(r->ru_isrss, &ru->ru_isrss) ||
352 __put_user(r->ru_minflt, &ru->ru_minflt) ||
353 __put_user(r->ru_majflt, &ru->ru_majflt) ||
354 __put_user(r->ru_nswap, &ru->ru_nswap) ||
355 __put_user(r->ru_inblock, &ru->ru_inblock) ||
356 __put_user(r->ru_oublock, &ru->ru_oublock) ||
357 __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
358 __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
359 __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
360 __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
361 __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
362 return -EFAULT;
363 return 0;
364}
365
366asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
367{
368 struct rusage r;
369 int ret;
370 mm_segment_t old_fs = get_fs();
371
372 set_fs(KERNEL_DS);
373 ret = sys_getrusage(who, (struct rusage __user *) &r);
374 set_fs(old_fs);
375
376 if (ret)
377 return ret;
378
379 if (put_compat_rusage(&r, ru))
380 return -EFAULT;
381
382 return 0;
383}
384
385asmlinkage long
386compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
387 struct compat_rusage __user *ru)
388{
389 if (!ru) {
390 return sys_wait4(pid, stat_addr, options, NULL);
391 } else {
392 struct rusage r;
393 int ret;
394 unsigned int status;
395 mm_segment_t old_fs = get_fs();
396
397 set_fs (KERNEL_DS);
398 ret = sys_wait4(pid,
399 (stat_addr ?
400 (unsigned int __user *) &status : NULL),
401 options, (struct rusage __user *) &r);
402 set_fs (old_fs);
403
404 if (ret > 0) {
405 if (put_compat_rusage(&r, ru))
406 return -EFAULT;
407 if (stat_addr && put_user(status, stat_addr))
408 return -EFAULT;
409 }
410 return ret;
411 }
412}
413
414asmlinkage long compat_sys_waitid(int which, compat_pid_t pid,
415 struct compat_siginfo __user *uinfo, int options,
416 struct compat_rusage __user *uru)
417{
418 siginfo_t info;
419 struct rusage ru;
420 long ret;
421 mm_segment_t old_fs = get_fs();
422
423 memset(&info, 0, sizeof(info));
424
425 set_fs(KERNEL_DS);
426 ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
427 uru ? (struct rusage __user *)&ru : NULL);
428 set_fs(old_fs);
429
430 if ((ret < 0) || (info.si_signo == 0))
431 return ret;
432
433 if (uru) {
434 ret = put_compat_rusage(&ru, uru);
435 if (ret)
436 return ret;
437 }
438
439 BUG_ON(info.si_code & __SI_MASK);
440 info.si_code |= __SI_CHLD;
441 return copy_siginfo_to_user32(uinfo, &info);
442}
443
444static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
445 unsigned len, cpumask_t *new_mask)
446{
447 unsigned long *k;
448
449 if (len < sizeof(cpumask_t))
450 memset(new_mask, 0, sizeof(cpumask_t));
451 else if (len > sizeof(cpumask_t))
452 len = sizeof(cpumask_t);
453
454 k = cpus_addr(*new_mask);
455 return compat_get_bitmap(k, user_mask_ptr, len * 8);
456}
457
458asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
459 unsigned int len,
460 compat_ulong_t __user *user_mask_ptr)
461{
462 cpumask_t new_mask;
463 int retval;
464
465 retval = compat_get_user_cpu_mask(user_mask_ptr, len, &new_mask);
466 if (retval)
467 return retval;
468
469 return sched_setaffinity(pid, new_mask);
470}
471
472asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
473 compat_ulong_t __user *user_mask_ptr)
474{
475 int ret;
476 cpumask_t mask;
477 unsigned long *k;
478 unsigned int min_length = sizeof(cpumask_t);
479
480 if (NR_CPUS <= BITS_PER_COMPAT_LONG)
481 min_length = sizeof(compat_ulong_t);
482
483 if (len < min_length)
484 return -EINVAL;
485
486 ret = sched_getaffinity(pid, &mask);
487 if (ret < 0)
488 return ret;
489
490 k = cpus_addr(mask);
491 ret = compat_put_bitmap(user_mask_ptr, k, min_length * 8);
492 if (ret)
493 return ret;
494
495 return min_length;
496}
497
498static int get_compat_itimerspec(struct itimerspec *dst,
499 struct compat_itimerspec __user *src)
500{
501 if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
502 get_compat_timespec(&dst->it_value, &src->it_value))
503 return -EFAULT;
504 return 0;
505}
506
507static int put_compat_itimerspec(struct compat_itimerspec __user *dst,
508 struct itimerspec *src)
509{
510 if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
511 put_compat_timespec(&src->it_value, &dst->it_value))
512 return -EFAULT;
513 return 0;
514}
515
3a0f69d5
CH
516long compat_sys_timer_create(clockid_t which_clock,
517 struct compat_sigevent __user *timer_event_spec,
518 timer_t __user *created_timer_id)
519{
520 struct sigevent __user *event = NULL;
521
522 if (timer_event_spec) {
523 struct sigevent kevent;
524
525 event = compat_alloc_user_space(sizeof(*event));
526 if (get_compat_sigevent(&kevent, timer_event_spec) ||
527 copy_to_user(event, &kevent, sizeof(*event)))
528 return -EFAULT;
529 }
530
531 return sys_timer_create(which_clock, event, created_timer_id);
532}
533
1da177e4
LT
534long compat_sys_timer_settime(timer_t timer_id, int flags,
535 struct compat_itimerspec __user *new,
536 struct compat_itimerspec __user *old)
537{
538 long err;
539 mm_segment_t oldfs;
540 struct itimerspec newts, oldts;
541
542 if (!new)
543 return -EINVAL;
544 if (get_compat_itimerspec(&newts, new))
545 return -EFAULT;
546 oldfs = get_fs();
547 set_fs(KERNEL_DS);
548 err = sys_timer_settime(timer_id, flags,
549 (struct itimerspec __user *) &newts,
550 (struct itimerspec __user *) &oldts);
551 set_fs(oldfs);
552 if (!err && old && put_compat_itimerspec(old, &oldts))
553 return -EFAULT;
554 return err;
555}
556
557long compat_sys_timer_gettime(timer_t timer_id,
558 struct compat_itimerspec __user *setting)
559{
560 long err;
561 mm_segment_t oldfs;
562 struct itimerspec ts;
563
564 oldfs = get_fs();
565 set_fs(KERNEL_DS);
566 err = sys_timer_gettime(timer_id,
567 (struct itimerspec __user *) &ts);
568 set_fs(oldfs);
569 if (!err && put_compat_itimerspec(setting, &ts))
570 return -EFAULT;
571 return err;
572}
573
574long compat_sys_clock_settime(clockid_t which_clock,
575 struct compat_timespec __user *tp)
576{
577 long err;
578 mm_segment_t oldfs;
579 struct timespec ts;
580
581 if (get_compat_timespec(&ts, tp))
582 return -EFAULT;
583 oldfs = get_fs();
584 set_fs(KERNEL_DS);
585 err = sys_clock_settime(which_clock,
586 (struct timespec __user *) &ts);
587 set_fs(oldfs);
588 return err;
589}
590
591long compat_sys_clock_gettime(clockid_t which_clock,
592 struct compat_timespec __user *tp)
593{
594 long err;
595 mm_segment_t oldfs;
596 struct timespec ts;
597
598 oldfs = get_fs();
599 set_fs(KERNEL_DS);
600 err = sys_clock_gettime(which_clock,
601 (struct timespec __user *) &ts);
602 set_fs(oldfs);
603 if (!err && put_compat_timespec(&ts, tp))
604 return -EFAULT;
605 return err;
606}
607
608long compat_sys_clock_getres(clockid_t which_clock,
609 struct compat_timespec __user *tp)
610{
611 long err;
612 mm_segment_t oldfs;
613 struct timespec ts;
614
615 oldfs = get_fs();
616 set_fs(KERNEL_DS);
617 err = sys_clock_getres(which_clock,
618 (struct timespec __user *) &ts);
619 set_fs(oldfs);
620 if (!err && tp && put_compat_timespec(&ts, tp))
621 return -EFAULT;
622 return err;
623}
624
625long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
626 struct compat_timespec __user *rqtp,
627 struct compat_timespec __user *rmtp)
628{
629 long err;
630 mm_segment_t oldfs;
631 struct timespec in, out;
632
633 if (get_compat_timespec(&in, rqtp))
634 return -EFAULT;
635
636 oldfs = get_fs();
637 set_fs(KERNEL_DS);
638 err = sys_clock_nanosleep(which_clock, flags,
639 (struct timespec __user *) &in,
640 (struct timespec __user *) &out);
641 set_fs(oldfs);
642 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
643 put_compat_timespec(&out, rmtp))
644 return -EFAULT;
645 return err;
646}
647
648/*
649 * We currently only need the following fields from the sigevent
650 * structure: sigev_value, sigev_signo, sig_notify and (sometimes
651 * sigev_notify_thread_id). The others are handled in user mode.
652 * We also assume that copying sigev_value.sival_int is sufficient
653 * to keep all the bits of sigev_value.sival_ptr intact.
654 */
655int get_compat_sigevent(struct sigevent *event,
656 const struct compat_sigevent __user *u_event)
657{
51410d3c 658 memset(event, 0, sizeof(*event));
1da177e4
LT
659 return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
660 __get_user(event->sigev_value.sival_int,
661 &u_event->sigev_value.sival_int) ||
662 __get_user(event->sigev_signo, &u_event->sigev_signo) ||
663 __get_user(event->sigev_notify, &u_event->sigev_notify) ||
664 __get_user(event->sigev_notify_thread_id,
665 &u_event->sigev_notify_thread_id))
666 ? -EFAULT : 0;
667}
668
1da177e4
LT
669long compat_get_bitmap(unsigned long *mask, compat_ulong_t __user *umask,
670 unsigned long bitmap_size)
671{
672 int i, j;
673 unsigned long m;
674 compat_ulong_t um;
675 unsigned long nr_compat_longs;
676
677 /* align bitmap up to nearest compat_long_t boundary */
678 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
679
680 if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
681 return -EFAULT;
682
683 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
684
685 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
686 m = 0;
687
688 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
689 /*
690 * We dont want to read past the end of the userspace
691 * bitmap. We must however ensure the end of the
692 * kernel bitmap is zeroed.
693 */
694 if (nr_compat_longs-- > 0) {
695 if (__get_user(um, umask))
696 return -EFAULT;
697 } else {
698 um = 0;
699 }
700
701 umask++;
702 m |= (long)um << (j * BITS_PER_COMPAT_LONG);
703 }
704 *mask++ = m;
705 }
706
707 return 0;
708}
709
710long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
711 unsigned long bitmap_size)
712{
713 int i, j;
714 unsigned long m;
715 compat_ulong_t um;
716 unsigned long nr_compat_longs;
717
718 /* align bitmap up to nearest compat_long_t boundary */
719 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
720
721 if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
722 return -EFAULT;
723
724 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
725
726 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
727 m = *mask++;
728
729 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
730 um = m;
731
732 /*
733 * We dont want to write past the end of the userspace
734 * bitmap.
735 */
736 if (nr_compat_longs-- > 0) {
737 if (__put_user(um, umask))
738 return -EFAULT;
739 }
740
741 umask++;
742 m >>= 4*sizeof(um);
743 m >>= 4*sizeof(um);
744 }
745 }
746
747 return 0;
748}
749
750void
751sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
752{
753 switch (_NSIG_WORDS) {
754#if defined (__COMPAT_ENDIAN_SWAP__)
755 case 4: set->sig[3] = compat->sig[7] | (((long)compat->sig[6]) << 32 );
756 case 3: set->sig[2] = compat->sig[5] | (((long)compat->sig[4]) << 32 );
757 case 2: set->sig[1] = compat->sig[3] | (((long)compat->sig[2]) << 32 );
758 case 1: set->sig[0] = compat->sig[1] | (((long)compat->sig[0]) << 32 );
759#else
760 case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
761 case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
762 case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
763 case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
764#endif
765 }
766}
767
768asmlinkage long
769compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
770 struct compat_siginfo __user *uinfo,
771 struct compat_timespec __user *uts, compat_size_t sigsetsize)
772{
773 compat_sigset_t s32;
774 sigset_t s;
775 int sig;
776 struct timespec t;
777 siginfo_t info;
778 long ret, timeout = 0;
779
780 if (sigsetsize != sizeof(sigset_t))
781 return -EINVAL;
782
783 if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
784 return -EFAULT;
785 sigset_from_compat(&s, &s32);
786 sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
787 signotset(&s);
788
789 if (uts) {
790 if (get_compat_timespec (&t, uts))
791 return -EFAULT;
792 if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0
793 || t.tv_sec < 0)
794 return -EINVAL;
795 }
796
797 spin_lock_irq(&current->sighand->siglock);
798 sig = dequeue_signal(current, &s, &info);
799 if (!sig) {
800 timeout = MAX_SCHEDULE_TIMEOUT;
801 if (uts)
802 timeout = timespec_to_jiffies(&t)
803 +(t.tv_sec || t.tv_nsec);
804 if (timeout) {
805 current->real_blocked = current->blocked;
806 sigandsets(&current->blocked, &current->blocked, &s);
807
808 recalc_sigpending();
809 spin_unlock_irq(&current->sighand->siglock);
810
75bcc8c5 811 timeout = schedule_timeout_interruptible(timeout);
1da177e4
LT
812
813 spin_lock_irq(&current->sighand->siglock);
814 sig = dequeue_signal(current, &s, &info);
815 current->blocked = current->real_blocked;
816 siginitset(&current->real_blocked, 0);
817 recalc_sigpending();
818 }
819 }
820 spin_unlock_irq(&current->sighand->siglock);
821
822 if (sig) {
823 ret = sig;
824 if (uinfo) {
825 if (copy_siginfo_to_user32(uinfo, &info))
826 ret = -EFAULT;
827 }
828 }else {
829 ret = timeout?-EINTR:-EAGAIN;
830 }
831 return ret;
832
833}
834
835#ifdef __ARCH_WANT_COMPAT_SYS_TIME
836
837/* compat_time_t is a 32 bit "long" and needs to get converted. */
838
839asmlinkage long compat_sys_time(compat_time_t __user * tloc)
840{
841 compat_time_t i;
842 struct timeval tv;
843
844 do_gettimeofday(&tv);
845 i = tv.tv_sec;
846
847 if (tloc) {
848 if (put_user(i,tloc))
849 i = -EFAULT;
850 }
851 return i;
852}
853
854asmlinkage long compat_sys_stime(compat_time_t __user *tptr)
855{
856 struct timespec tv;
857 int err;
858
859 if (get_user(tv.tv_sec, tptr))
860 return -EFAULT;
861
862 tv.tv_nsec = 0;
863
864 err = security_settime(&tv, NULL);
865 if (err)
866 return err;
867
868 do_settimeofday(&tv);
869 return 0;
870}
871
872#endif /* __ARCH_WANT_COMPAT_SYS_TIME */
150256d8
DW
873
874#ifdef __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND
875asmlinkage long compat_sys_rt_sigsuspend(compat_sigset_t __user *unewset, compat_size_t sigsetsize)
876{
877 sigset_t newset;
878 compat_sigset_t newset32;
879
880 /* XXX: Don't preclude handling different sized sigset_t's. */
881 if (sigsetsize != sizeof(sigset_t))
882 return -EINVAL;
883
884 if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
885 return -EFAULT;
886 sigset_from_compat(&newset, &newset32);
887 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
888
889 spin_lock_irq(&current->sighand->siglock);
890 current->saved_sigmask = current->blocked;
891 current->blocked = newset;
892 recalc_sigpending();
893 spin_unlock_irq(&current->sighand->siglock);
894
895 current->state = TASK_INTERRUPTIBLE;
896 schedule();
897 set_thread_flag(TIF_RESTORE_SIGMASK);
898 return -ERESTARTNOHAND;
899}
900#endif /* __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND */