Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6-block.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 */
1da177e4
LT
20#include <linux/syscalls.h>
21#include <linux/unistd.h>
22#include <linux/security.h>
3158e941 23#include <linux/timex.h>
1b2db9fb 24#include <linux/migrate.h>
1711ef38 25#include <linux/posix-timers.h>
f06febc9 26#include <linux/times.h>
e3d5a27d 27#include <linux/ptrace.h>
5a0e3ad6 28#include <linux/gfp.h>
1da177e4
LT
29
30#include <asm/uaccess.h>
1da177e4 31
b418da16
CH
32/*
33 * Note that the native side is already converted to a timespec, because
34 * that's what we want anyway.
35 */
36static int compat_get_timeval(struct timespec *o,
37 struct compat_timeval __user *i)
38{
39 long usec;
40
41 if (get_user(o->tv_sec, &i->tv_sec) ||
42 get_user(usec, &i->tv_usec))
43 return -EFAULT;
44 o->tv_nsec = usec * 1000;
45 return 0;
46}
47
48static int compat_put_timeval(struct compat_timeval __user *o,
49 struct timeval *i)
50{
51 return (put_user(i->tv_sec, &o->tv_sec) ||
52 put_user(i->tv_usec, &o->tv_usec)) ? -EFAULT : 0;
53}
54
55asmlinkage long compat_sys_gettimeofday(struct compat_timeval __user *tv,
56 struct timezone __user *tz)
57{
58 if (tv) {
59 struct timeval ktv;
60 do_gettimeofday(&ktv);
61 if (compat_put_timeval(tv, &ktv))
62 return -EFAULT;
63 }
64 if (tz) {
65 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
66 return -EFAULT;
67 }
68
69 return 0;
70}
71
72asmlinkage long compat_sys_settimeofday(struct compat_timeval __user *tv,
73 struct timezone __user *tz)
74{
75 struct timespec kts;
76 struct timezone ktz;
77
78 if (tv) {
79 if (compat_get_timeval(&kts, tv))
80 return -EFAULT;
81 }
82 if (tz) {
83 if (copy_from_user(&ktz, tz, sizeof(ktz)))
84 return -EFAULT;
85 }
86
87 return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
88}
89
1da177e4
LT
90int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
91{
92 return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
93 __get_user(ts->tv_sec, &cts->tv_sec) ||
94 __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
95}
96
97int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
98{
99 return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
100 __put_user(ts->tv_sec, &cts->tv_sec) ||
101 __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
102}
103
41652937
ON
104static long compat_nanosleep_restart(struct restart_block *restart)
105{
106 struct compat_timespec __user *rmtp;
107 struct timespec rmt;
108 mm_segment_t oldfs;
109 long ret;
110
029a07e0 111 restart->nanosleep.rmtp = (struct timespec __user *) &rmt;
41652937
ON
112 oldfs = get_fs();
113 set_fs(KERNEL_DS);
114 ret = hrtimer_nanosleep_restart(restart);
115 set_fs(oldfs);
116
117 if (ret) {
029a07e0 118 rmtp = restart->nanosleep.compat_rmtp;
41652937
ON
119
120 if (rmtp && put_compat_timespec(&rmt, rmtp))
121 return -EFAULT;
122 }
123
124 return ret;
125}
126
1da177e4 127asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
c70878b4 128 struct compat_timespec __user *rmtp)
1da177e4 129{
c70878b4 130 struct timespec tu, rmt;
41652937 131 mm_segment_t oldfs;
c70878b4 132 long ret;
1da177e4 133
c70878b4 134 if (get_compat_timespec(&tu, rqtp))
1da177e4
LT
135 return -EFAULT;
136
c70878b4 137 if (!timespec_valid(&tu))
1da177e4
LT
138 return -EINVAL;
139
41652937
ON
140 oldfs = get_fs();
141 set_fs(KERNEL_DS);
142 ret = hrtimer_nanosleep(&tu,
143 rmtp ? (struct timespec __user *)&rmt : NULL,
144 HRTIMER_MODE_REL, CLOCK_MONOTONIC);
145 set_fs(oldfs);
146
147 if (ret) {
148 struct restart_block *restart
149 = &current_thread_info()->restart_block;
150
151 restart->fn = compat_nanosleep_restart;
029a07e0 152 restart->nanosleep.compat_rmtp = rmtp;
1da177e4 153
41652937 154 if (rmtp && put_compat_timespec(&rmt, rmtp))
1da177e4
LT
155 return -EFAULT;
156 }
c70878b4
AB
157
158 return ret;
1da177e4
LT
159}
160
161static inline long get_compat_itimerval(struct itimerval *o,
162 struct compat_itimerval __user *i)
163{
164 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
165 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
166 __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
167 __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
168 __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
169}
170
171static inline long put_compat_itimerval(struct compat_itimerval __user *o,
172 struct itimerval *i)
173{
174 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
175 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
176 __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
177 __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
178 __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
179}
180
181asmlinkage long compat_sys_getitimer(int which,
182 struct compat_itimerval __user *it)
183{
184 struct itimerval kit;
185 int error;
186
187 error = do_getitimer(which, &kit);
188 if (!error && put_compat_itimerval(it, &kit))
189 error = -EFAULT;
190 return error;
191}
192
193asmlinkage long compat_sys_setitimer(int which,
194 struct compat_itimerval __user *in,
195 struct compat_itimerval __user *out)
196{
197 struct itimerval kin, kout;
198 int error;
199
200 if (in) {
201 if (get_compat_itimerval(&kin, in))
202 return -EFAULT;
203 } else
204 memset(&kin, 0, sizeof(kin));
205
206 error = do_setitimer(which, &kin, out ? &kout : NULL);
207 if (error || !out)
208 return error;
209 if (put_compat_itimerval(out, &kout))
210 return -EFAULT;
211 return 0;
212}
213
f06febc9
FM
214static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
215{
216 return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
217}
218
1da177e4
LT
219asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
220{
1da177e4 221 if (tbuf) {
f06febc9 222 struct tms tms;
1da177e4 223 struct compat_tms tmp;
f06febc9
FM
224
225 do_sys_times(&tms);
226 /* Convert our struct tms to the compat version. */
227 tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
228 tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
229 tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
230 tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
1da177e4
LT
231 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
232 return -EFAULT;
233 }
e3d5a27d 234 force_successful_syscall_return();
1da177e4
LT
235 return compat_jiffies_to_clock_t(jiffies);
236}
237
238/*
239 * Assumption: old_sigset_t and compat_old_sigset_t are both
240 * types that can be passed to put_user()/get_user().
241 */
242
243asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
244{
245 old_sigset_t s;
246 long ret;
247 mm_segment_t old_fs = get_fs();
248
249 set_fs(KERNEL_DS);
250 ret = sys_sigpending((old_sigset_t __user *) &s);
251 set_fs(old_fs);
252 if (ret == 0)
253 ret = put_user(s, set);
254 return ret;
255}
256
257asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set,
258 compat_old_sigset_t __user *oset)
259{
260 old_sigset_t s;
261 long ret;
262 mm_segment_t old_fs;
263
264 if (set && get_user(s, set))
265 return -EFAULT;
266 old_fs = get_fs();
267 set_fs(KERNEL_DS);
268 ret = sys_sigprocmask(how,
269 set ? (old_sigset_t __user *) &s : NULL,
270 oset ? (old_sigset_t __user *) &s : NULL);
271 set_fs(old_fs);
272 if (ret == 0)
273 if (oset)
274 ret = put_user(s, oset);
275 return ret;
276}
277
1da177e4
LT
278asmlinkage long compat_sys_setrlimit(unsigned int resource,
279 struct compat_rlimit __user *rlim)
280{
281 struct rlimit r;
282 int ret;
283 mm_segment_t old_fs = get_fs ();
284
bd3a8492
DW
285 if (resource >= RLIM_NLIMITS)
286 return -EINVAL;
1da177e4
LT
287
288 if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
289 __get_user(r.rlim_cur, &rlim->rlim_cur) ||
290 __get_user(r.rlim_max, &rlim->rlim_max))
291 return -EFAULT;
292
293 if (r.rlim_cur == COMPAT_RLIM_INFINITY)
294 r.rlim_cur = RLIM_INFINITY;
295 if (r.rlim_max == COMPAT_RLIM_INFINITY)
296 r.rlim_max = RLIM_INFINITY;
297 set_fs(KERNEL_DS);
298 ret = sys_setrlimit(resource, (struct rlimit __user *) &r);
299 set_fs(old_fs);
300 return ret;
301}
302
303#ifdef COMPAT_RLIM_OLD_INFINITY
304
305asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
306 struct compat_rlimit __user *rlim)
307{
308 struct rlimit r;
309 int ret;
310 mm_segment_t old_fs = get_fs();
311
312 set_fs(KERNEL_DS);
313 ret = sys_old_getrlimit(resource, &r);
314 set_fs(old_fs);
315
316 if (!ret) {
317 if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
318 r.rlim_cur = COMPAT_RLIM_INFINITY;
319 if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
320 r.rlim_max = COMPAT_RLIM_INFINITY;
321
322 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
323 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
324 __put_user(r.rlim_max, &rlim->rlim_max))
325 return -EFAULT;
326 }
327 return ret;
328}
329
330#endif
331
332asmlinkage long compat_sys_getrlimit (unsigned int resource,
333 struct compat_rlimit __user *rlim)
334{
335 struct rlimit r;
336 int ret;
337 mm_segment_t old_fs = get_fs();
338
339 set_fs(KERNEL_DS);
340 ret = sys_getrlimit(resource, (struct rlimit __user *) &r);
341 set_fs(old_fs);
342 if (!ret) {
343 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
344 r.rlim_cur = COMPAT_RLIM_INFINITY;
345 if (r.rlim_max > COMPAT_RLIM_INFINITY)
346 r.rlim_max = COMPAT_RLIM_INFINITY;
347
348 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
349 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
350 __put_user(r.rlim_max, &rlim->rlim_max))
351 return -EFAULT;
352 }
353 return ret;
354}
355
356int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
357{
358 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
359 __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
360 __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
361 __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
362 __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
363 __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
364 __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
365 __put_user(r->ru_idrss, &ru->ru_idrss) ||
366 __put_user(r->ru_isrss, &ru->ru_isrss) ||
367 __put_user(r->ru_minflt, &ru->ru_minflt) ||
368 __put_user(r->ru_majflt, &ru->ru_majflt) ||
369 __put_user(r->ru_nswap, &ru->ru_nswap) ||
370 __put_user(r->ru_inblock, &ru->ru_inblock) ||
371 __put_user(r->ru_oublock, &ru->ru_oublock) ||
372 __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
373 __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
374 __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
375 __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
376 __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
377 return -EFAULT;
378 return 0;
379}
380
381asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
382{
383 struct rusage r;
384 int ret;
385 mm_segment_t old_fs = get_fs();
386
387 set_fs(KERNEL_DS);
388 ret = sys_getrusage(who, (struct rusage __user *) &r);
389 set_fs(old_fs);
390
391 if (ret)
392 return ret;
393
394 if (put_compat_rusage(&r, ru))
395 return -EFAULT;
396
397 return 0;
398}
399
400asmlinkage long
401compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
402 struct compat_rusage __user *ru)
403{
404 if (!ru) {
405 return sys_wait4(pid, stat_addr, options, NULL);
406 } else {
407 struct rusage r;
408 int ret;
409 unsigned int status;
410 mm_segment_t old_fs = get_fs();
411
412 set_fs (KERNEL_DS);
413 ret = sys_wait4(pid,
414 (stat_addr ?
415 (unsigned int __user *) &status : NULL),
416 options, (struct rusage __user *) &r);
417 set_fs (old_fs);
418
419 if (ret > 0) {
420 if (put_compat_rusage(&r, ru))
421 return -EFAULT;
422 if (stat_addr && put_user(status, stat_addr))
423 return -EFAULT;
424 }
425 return ret;
426 }
427}
428
429asmlinkage long compat_sys_waitid(int which, compat_pid_t pid,
430 struct compat_siginfo __user *uinfo, int options,
431 struct compat_rusage __user *uru)
432{
433 siginfo_t info;
434 struct rusage ru;
435 long ret;
436 mm_segment_t old_fs = get_fs();
437
438 memset(&info, 0, sizeof(info));
439
440 set_fs(KERNEL_DS);
441 ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
442 uru ? (struct rusage __user *)&ru : NULL);
443 set_fs(old_fs);
444
445 if ((ret < 0) || (info.si_signo == 0))
446 return ret;
447
448 if (uru) {
449 ret = put_compat_rusage(&ru, uru);
450 if (ret)
451 return ret;
452 }
453
454 BUG_ON(info.si_code & __SI_MASK);
455 info.si_code |= __SI_CHLD;
456 return copy_siginfo_to_user32(uinfo, &info);
457}
458
459static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
a45185d2 460 unsigned len, struct cpumask *new_mask)
1da177e4
LT
461{
462 unsigned long *k;
463
a45185d2
RR
464 if (len < cpumask_size())
465 memset(new_mask, 0, cpumask_size());
466 else if (len > cpumask_size())
467 len = cpumask_size();
1da177e4 468
a45185d2 469 k = cpumask_bits(new_mask);
1da177e4
LT
470 return compat_get_bitmap(k, user_mask_ptr, len * 8);
471}
472
473asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
474 unsigned int len,
475 compat_ulong_t __user *user_mask_ptr)
476{
a45185d2 477 cpumask_var_t new_mask;
1da177e4
LT
478 int retval;
479
a45185d2
RR
480 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
481 return -ENOMEM;
482
483 retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
1da177e4 484 if (retval)
a45185d2 485 goto out;
1da177e4 486
a45185d2
RR
487 retval = sched_setaffinity(pid, new_mask);
488out:
489 free_cpumask_var(new_mask);
490 return retval;
1da177e4
LT
491}
492
493asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
494 compat_ulong_t __user *user_mask_ptr)
495{
496 int ret;
a45185d2 497 cpumask_var_t mask;
1da177e4 498 unsigned long *k;
a45185d2 499 unsigned int min_length = cpumask_size();
1da177e4 500
a45185d2 501 if (nr_cpu_ids <= BITS_PER_COMPAT_LONG)
1da177e4
LT
502 min_length = sizeof(compat_ulong_t);
503
504 if (len < min_length)
505 return -EINVAL;
506
a45185d2
RR
507 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
508 return -ENOMEM;
509
510 ret = sched_getaffinity(pid, mask);
1da177e4 511 if (ret < 0)
a45185d2 512 goto out;
1da177e4 513
a45185d2 514 k = cpumask_bits(mask);
1da177e4 515 ret = compat_put_bitmap(user_mask_ptr, k, min_length * 8);
a45185d2
RR
516 if (ret == 0)
517 ret = min_length;
1da177e4 518
a45185d2
RR
519out:
520 free_cpumask_var(mask);
521 return ret;
1da177e4
LT
522}
523
83f5d126
DL
524int get_compat_itimerspec(struct itimerspec *dst,
525 const struct compat_itimerspec __user *src)
bd3a8492 526{
1da177e4
LT
527 if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
528 get_compat_timespec(&dst->it_value, &src->it_value))
529 return -EFAULT;
530 return 0;
bd3a8492 531}
1da177e4 532
83f5d126
DL
533int put_compat_itimerspec(struct compat_itimerspec __user *dst,
534 const struct itimerspec *src)
bd3a8492 535{
1da177e4
LT
536 if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
537 put_compat_timespec(&src->it_value, &dst->it_value))
538 return -EFAULT;
539 return 0;
bd3a8492 540}
1da177e4 541
3a0f69d5
CH
542long compat_sys_timer_create(clockid_t which_clock,
543 struct compat_sigevent __user *timer_event_spec,
544 timer_t __user *created_timer_id)
545{
546 struct sigevent __user *event = NULL;
547
548 if (timer_event_spec) {
549 struct sigevent kevent;
550
551 event = compat_alloc_user_space(sizeof(*event));
552 if (get_compat_sigevent(&kevent, timer_event_spec) ||
553 copy_to_user(event, &kevent, sizeof(*event)))
554 return -EFAULT;
555 }
556
557 return sys_timer_create(which_clock, event, created_timer_id);
558}
559
1da177e4 560long compat_sys_timer_settime(timer_t timer_id, int flags,
bd3a8492 561 struct compat_itimerspec __user *new,
1da177e4 562 struct compat_itimerspec __user *old)
bd3a8492 563{
1da177e4
LT
564 long err;
565 mm_segment_t oldfs;
566 struct itimerspec newts, oldts;
567
568 if (!new)
569 return -EINVAL;
570 if (get_compat_itimerspec(&newts, new))
bd3a8492 571 return -EFAULT;
1da177e4
LT
572 oldfs = get_fs();
573 set_fs(KERNEL_DS);
574 err = sys_timer_settime(timer_id, flags,
575 (struct itimerspec __user *) &newts,
576 (struct itimerspec __user *) &oldts);
bd3a8492 577 set_fs(oldfs);
1da177e4
LT
578 if (!err && old && put_compat_itimerspec(old, &oldts))
579 return -EFAULT;
580 return err;
bd3a8492 581}
1da177e4
LT
582
583long compat_sys_timer_gettime(timer_t timer_id,
584 struct compat_itimerspec __user *setting)
bd3a8492 585{
1da177e4
LT
586 long err;
587 mm_segment_t oldfs;
bd3a8492 588 struct itimerspec ts;
1da177e4
LT
589
590 oldfs = get_fs();
591 set_fs(KERNEL_DS);
592 err = sys_timer_gettime(timer_id,
bd3a8492
DW
593 (struct itimerspec __user *) &ts);
594 set_fs(oldfs);
1da177e4
LT
595 if (!err && put_compat_itimerspec(setting, &ts))
596 return -EFAULT;
597 return err;
bd3a8492 598}
1da177e4
LT
599
600long compat_sys_clock_settime(clockid_t which_clock,
601 struct compat_timespec __user *tp)
602{
603 long err;
604 mm_segment_t oldfs;
bd3a8492 605 struct timespec ts;
1da177e4
LT
606
607 if (get_compat_timespec(&ts, tp))
bd3a8492 608 return -EFAULT;
1da177e4 609 oldfs = get_fs();
bd3a8492 610 set_fs(KERNEL_DS);
1da177e4
LT
611 err = sys_clock_settime(which_clock,
612 (struct timespec __user *) &ts);
613 set_fs(oldfs);
614 return err;
bd3a8492 615}
1da177e4
LT
616
617long compat_sys_clock_gettime(clockid_t which_clock,
618 struct compat_timespec __user *tp)
619{
620 long err;
621 mm_segment_t oldfs;
bd3a8492 622 struct timespec ts;
1da177e4
LT
623
624 oldfs = get_fs();
625 set_fs(KERNEL_DS);
626 err = sys_clock_gettime(which_clock,
627 (struct timespec __user *) &ts);
628 set_fs(oldfs);
629 if (!err && put_compat_timespec(&ts, tp))
bd3a8492 630 return -EFAULT;
1da177e4 631 return err;
bd3a8492 632}
1da177e4
LT
633
634long compat_sys_clock_getres(clockid_t which_clock,
635 struct compat_timespec __user *tp)
636{
637 long err;
638 mm_segment_t oldfs;
bd3a8492 639 struct timespec ts;
1da177e4
LT
640
641 oldfs = get_fs();
642 set_fs(KERNEL_DS);
643 err = sys_clock_getres(which_clock,
644 (struct timespec __user *) &ts);
645 set_fs(oldfs);
646 if (!err && tp && put_compat_timespec(&ts, tp))
bd3a8492 647 return -EFAULT;
1da177e4 648 return err;
bd3a8492 649}
1da177e4 650
1711ef38
TA
651static long compat_clock_nanosleep_restart(struct restart_block *restart)
652{
653 long err;
654 mm_segment_t oldfs;
655 struct timespec tu;
029a07e0 656 struct compat_timespec *rmtp = restart->nanosleep.compat_rmtp;
1711ef38 657
029a07e0 658 restart->nanosleep.rmtp = (struct timespec __user *) &tu;
1711ef38
TA
659 oldfs = get_fs();
660 set_fs(KERNEL_DS);
661 err = clock_nanosleep_restart(restart);
662 set_fs(oldfs);
663
664 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
665 put_compat_timespec(&tu, rmtp))
666 return -EFAULT;
667
668 if (err == -ERESTART_RESTARTBLOCK) {
669 restart->fn = compat_clock_nanosleep_restart;
029a07e0 670 restart->nanosleep.compat_rmtp = rmtp;
1711ef38
TA
671 }
672 return err;
673}
674
1da177e4
LT
675long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
676 struct compat_timespec __user *rqtp,
677 struct compat_timespec __user *rmtp)
678{
679 long err;
680 mm_segment_t oldfs;
bd3a8492 681 struct timespec in, out;
1711ef38 682 struct restart_block *restart;
1da177e4 683
bd3a8492 684 if (get_compat_timespec(&in, rqtp))
1da177e4
LT
685 return -EFAULT;
686
687 oldfs = get_fs();
688 set_fs(KERNEL_DS);
689 err = sys_clock_nanosleep(which_clock, flags,
690 (struct timespec __user *) &in,
691 (struct timespec __user *) &out);
692 set_fs(oldfs);
1711ef38 693
1da177e4
LT
694 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
695 put_compat_timespec(&out, rmtp))
696 return -EFAULT;
1711ef38
TA
697
698 if (err == -ERESTART_RESTARTBLOCK) {
699 restart = &current_thread_info()->restart_block;
700 restart->fn = compat_clock_nanosleep_restart;
029a07e0 701 restart->nanosleep.compat_rmtp = rmtp;
1711ef38 702 }
bd3a8492
DW
703 return err;
704}
1da177e4
LT
705
706/*
707 * We currently only need the following fields from the sigevent
708 * structure: sigev_value, sigev_signo, sig_notify and (sometimes
709 * sigev_notify_thread_id). The others are handled in user mode.
710 * We also assume that copying sigev_value.sival_int is sufficient
711 * to keep all the bits of sigev_value.sival_ptr intact.
712 */
713int get_compat_sigevent(struct sigevent *event,
714 const struct compat_sigevent __user *u_event)
715{
51410d3c 716 memset(event, 0, sizeof(*event));
1da177e4
LT
717 return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
718 __get_user(event->sigev_value.sival_int,
719 &u_event->sigev_value.sival_int) ||
720 __get_user(event->sigev_signo, &u_event->sigev_signo) ||
721 __get_user(event->sigev_notify, &u_event->sigev_notify) ||
722 __get_user(event->sigev_notify_thread_id,
723 &u_event->sigev_notify_thread_id))
724 ? -EFAULT : 0;
725}
726
5fa3839a 727long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
1da177e4
LT
728 unsigned long bitmap_size)
729{
730 int i, j;
731 unsigned long m;
732 compat_ulong_t um;
733 unsigned long nr_compat_longs;
734
735 /* align bitmap up to nearest compat_long_t boundary */
736 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
737
738 if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
739 return -EFAULT;
740
741 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
742
743 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
744 m = 0;
745
746 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
747 /*
748 * We dont want to read past the end of the userspace
749 * bitmap. We must however ensure the end of the
750 * kernel bitmap is zeroed.
751 */
752 if (nr_compat_longs-- > 0) {
753 if (__get_user(um, umask))
754 return -EFAULT;
755 } else {
756 um = 0;
757 }
758
759 umask++;
760 m |= (long)um << (j * BITS_PER_COMPAT_LONG);
761 }
762 *mask++ = m;
763 }
764
765 return 0;
766}
767
768long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
769 unsigned long bitmap_size)
770{
771 int i, j;
772 unsigned long m;
773 compat_ulong_t um;
774 unsigned long nr_compat_longs;
775
776 /* align bitmap up to nearest compat_long_t boundary */
777 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
778
779 if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
780 return -EFAULT;
781
782 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
783
784 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
785 m = *mask++;
786
787 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
788 um = m;
789
790 /*
791 * We dont want to write past the end of the userspace
792 * bitmap.
793 */
794 if (nr_compat_longs-- > 0) {
795 if (__put_user(um, umask))
796 return -EFAULT;
797 }
798
799 umask++;
800 m >>= 4*sizeof(um);
801 m >>= 4*sizeof(um);
802 }
803 }
804
805 return 0;
806}
807
808void
809sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
810{
811 switch (_NSIG_WORDS) {
1da177e4
LT
812 case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
813 case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
814 case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
815 case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
1da177e4
LT
816 }
817}
818
819asmlinkage long
820compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
821 struct compat_siginfo __user *uinfo,
822 struct compat_timespec __user *uts, compat_size_t sigsetsize)
823{
824 compat_sigset_t s32;
825 sigset_t s;
826 int sig;
827 struct timespec t;
828 siginfo_t info;
829 long ret, timeout = 0;
830
831 if (sigsetsize != sizeof(sigset_t))
832 return -EINVAL;
833
834 if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
835 return -EFAULT;
836 sigset_from_compat(&s, &s32);
837 sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
838 signotset(&s);
839
840 if (uts) {
841 if (get_compat_timespec (&t, uts))
842 return -EFAULT;
843 if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0
844 || t.tv_sec < 0)
845 return -EINVAL;
846 }
847
848 spin_lock_irq(&current->sighand->siglock);
849 sig = dequeue_signal(current, &s, &info);
850 if (!sig) {
851 timeout = MAX_SCHEDULE_TIMEOUT;
852 if (uts)
853 timeout = timespec_to_jiffies(&t)
854 +(t.tv_sec || t.tv_nsec);
855 if (timeout) {
856 current->real_blocked = current->blocked;
857 sigandsets(&current->blocked, &current->blocked, &s);
858
859 recalc_sigpending();
860 spin_unlock_irq(&current->sighand->siglock);
861
75bcc8c5 862 timeout = schedule_timeout_interruptible(timeout);
1da177e4
LT
863
864 spin_lock_irq(&current->sighand->siglock);
865 sig = dequeue_signal(current, &s, &info);
866 current->blocked = current->real_blocked;
867 siginitset(&current->real_blocked, 0);
868 recalc_sigpending();
869 }
870 }
871 spin_unlock_irq(&current->sighand->siglock);
872
873 if (sig) {
874 ret = sig;
875 if (uinfo) {
876 if (copy_siginfo_to_user32(uinfo, &info))
877 ret = -EFAULT;
878 }
879 }else {
880 ret = timeout?-EINTR:-EAGAIN;
881 }
882 return ret;
883
884}
885
62ab4505
TG
886asmlinkage long
887compat_sys_rt_tgsigqueueinfo(compat_pid_t tgid, compat_pid_t pid, int sig,
888 struct compat_siginfo __user *uinfo)
889{
890 siginfo_t info;
891
892 if (copy_siginfo_from_user32(&info, uinfo))
893 return -EFAULT;
894 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
895}
896
1da177e4
LT
897#ifdef __ARCH_WANT_COMPAT_SYS_TIME
898
899/* compat_time_t is a 32 bit "long" and needs to get converted. */
900
901asmlinkage long compat_sys_time(compat_time_t __user * tloc)
902{
903 compat_time_t i;
904 struct timeval tv;
905
906 do_gettimeofday(&tv);
907 i = tv.tv_sec;
908
909 if (tloc) {
910 if (put_user(i,tloc))
e3d5a27d 911 return -EFAULT;
1da177e4 912 }
e3d5a27d 913 force_successful_syscall_return();
1da177e4
LT
914 return i;
915}
916
917asmlinkage long compat_sys_stime(compat_time_t __user *tptr)
918{
919 struct timespec tv;
920 int err;
921
922 if (get_user(tv.tv_sec, tptr))
923 return -EFAULT;
924
925 tv.tv_nsec = 0;
926
927 err = security_settime(&tv, NULL);
928 if (err)
929 return err;
930
931 do_settimeofday(&tv);
932 return 0;
933}
934
935#endif /* __ARCH_WANT_COMPAT_SYS_TIME */
150256d8
DW
936
937#ifdef __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND
938asmlinkage long compat_sys_rt_sigsuspend(compat_sigset_t __user *unewset, compat_size_t sigsetsize)
939{
940 sigset_t newset;
941 compat_sigset_t newset32;
942
943 /* XXX: Don't preclude handling different sized sigset_t's. */
944 if (sigsetsize != sizeof(sigset_t))
945 return -EINVAL;
946
947 if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
948 return -EFAULT;
949 sigset_from_compat(&newset, &newset32);
950 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
951
952 spin_lock_irq(&current->sighand->siglock);
953 current->saved_sigmask = current->blocked;
954 current->blocked = newset;
955 recalc_sigpending();
956 spin_unlock_irq(&current->sighand->siglock);
957
958 current->state = TASK_INTERRUPTIBLE;
959 schedule();
4e4c22c7 960 set_restore_sigmask();
150256d8
DW
961 return -ERESTARTNOHAND;
962}
963#endif /* __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND */
3158e941
SR
964
965asmlinkage long compat_sys_adjtimex(struct compat_timex __user *utp)
966{
967 struct timex txc;
968 int ret;
969
970 memset(&txc, 0, sizeof(struct timex));
971
972 if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
973 __get_user(txc.modes, &utp->modes) ||
974 __get_user(txc.offset, &utp->offset) ||
975 __get_user(txc.freq, &utp->freq) ||
976 __get_user(txc.maxerror, &utp->maxerror) ||
977 __get_user(txc.esterror, &utp->esterror) ||
978 __get_user(txc.status, &utp->status) ||
979 __get_user(txc.constant, &utp->constant) ||
980 __get_user(txc.precision, &utp->precision) ||
981 __get_user(txc.tolerance, &utp->tolerance) ||
982 __get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
983 __get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
984 __get_user(txc.tick, &utp->tick) ||
985 __get_user(txc.ppsfreq, &utp->ppsfreq) ||
986 __get_user(txc.jitter, &utp->jitter) ||
987 __get_user(txc.shift, &utp->shift) ||
988 __get_user(txc.stabil, &utp->stabil) ||
989 __get_user(txc.jitcnt, &utp->jitcnt) ||
990 __get_user(txc.calcnt, &utp->calcnt) ||
991 __get_user(txc.errcnt, &utp->errcnt) ||
992 __get_user(txc.stbcnt, &utp->stbcnt))
993 return -EFAULT;
994
995 ret = do_adjtimex(&txc);
996
997 if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
998 __put_user(txc.modes, &utp->modes) ||
999 __put_user(txc.offset, &utp->offset) ||
1000 __put_user(txc.freq, &utp->freq) ||
1001 __put_user(txc.maxerror, &utp->maxerror) ||
1002 __put_user(txc.esterror, &utp->esterror) ||
1003 __put_user(txc.status, &utp->status) ||
1004 __put_user(txc.constant, &utp->constant) ||
1005 __put_user(txc.precision, &utp->precision) ||
1006 __put_user(txc.tolerance, &utp->tolerance) ||
1007 __put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
1008 __put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
1009 __put_user(txc.tick, &utp->tick) ||
1010 __put_user(txc.ppsfreq, &utp->ppsfreq) ||
1011 __put_user(txc.jitter, &utp->jitter) ||
1012 __put_user(txc.shift, &utp->shift) ||
1013 __put_user(txc.stabil, &utp->stabil) ||
1014 __put_user(txc.jitcnt, &utp->jitcnt) ||
1015 __put_user(txc.calcnt, &utp->calcnt) ||
1016 __put_user(txc.errcnt, &utp->errcnt) ||
153b5d05
RZ
1017 __put_user(txc.stbcnt, &utp->stbcnt) ||
1018 __put_user(txc.tai, &utp->tai))
3158e941
SR
1019 ret = -EFAULT;
1020
1021 return ret;
1022}
1b2db9fb
CL
1023
1024#ifdef CONFIG_NUMA
1025asmlinkage long compat_sys_move_pages(pid_t pid, unsigned long nr_pages,
9216dfad 1026 compat_uptr_t __user *pages32,
1b2db9fb
CL
1027 const int __user *nodes,
1028 int __user *status,
1029 int flags)
1030{
1031 const void __user * __user *pages;
1032 int i;
1033
1034 pages = compat_alloc_user_space(nr_pages * sizeof(void *));
1035 for (i = 0; i < nr_pages; i++) {
1036 compat_uptr_t p;
1037
9216dfad 1038 if (get_user(p, pages32 + i) ||
1b2db9fb
CL
1039 put_user(compat_ptr(p), pages + i))
1040 return -EFAULT;
1041 }
1042 return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
1043}
3fd59397
SR
1044
1045asmlinkage long compat_sys_migrate_pages(compat_pid_t pid,
1046 compat_ulong_t maxnode,
1047 const compat_ulong_t __user *old_nodes,
1048 const compat_ulong_t __user *new_nodes)
1049{
1050 unsigned long __user *old = NULL;
1051 unsigned long __user *new = NULL;
1052 nodemask_t tmp_mask;
1053 unsigned long nr_bits;
1054 unsigned long size;
1055
1056 nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
1057 size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1058 if (old_nodes) {
1059 if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
1060 return -EFAULT;
1061 old = compat_alloc_user_space(new_nodes ? size * 2 : size);
1062 if (new_nodes)
1063 new = old + size / sizeof(unsigned long);
1064 if (copy_to_user(old, nodes_addr(tmp_mask), size))
1065 return -EFAULT;
1066 }
1067 if (new_nodes) {
1068 if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
1069 return -EFAULT;
1070 if (new == NULL)
1071 new = compat_alloc_user_space(size);
1072 if (copy_to_user(new, nodes_addr(tmp_mask), size))
1073 return -EFAULT;
1074 }
1075 return sys_migrate_pages(pid, nr_bits + 1, old, new);
1076}
1b2db9fb 1077#endif
d4d23add
KM
1078
1079struct compat_sysinfo {
1080 s32 uptime;
1081 u32 loads[3];
1082 u32 totalram;
1083 u32 freeram;
1084 u32 sharedram;
1085 u32 bufferram;
1086 u32 totalswap;
1087 u32 freeswap;
1088 u16 procs;
1089 u16 pad;
1090 u32 totalhigh;
1091 u32 freehigh;
1092 u32 mem_unit;
1093 char _f[20-2*sizeof(u32)-sizeof(int)];
1094};
1095
1096asmlinkage long
1097compat_sys_sysinfo(struct compat_sysinfo __user *info)
1098{
1099 struct sysinfo s;
1100
1101 do_sysinfo(&s);
1102
1103 /* Check to see if any memory value is too large for 32-bit and scale
1104 * down if needed
1105 */
1106 if ((s.totalram >> 32) || (s.totalswap >> 32)) {
1107 int bitcount = 0;
1108
1109 while (s.mem_unit < PAGE_SIZE) {
1110 s.mem_unit <<= 1;
1111 bitcount++;
1112 }
1113
1114 s.totalram >>= bitcount;
1115 s.freeram >>= bitcount;
1116 s.sharedram >>= bitcount;
1117 s.bufferram >>= bitcount;
1118 s.totalswap >>= bitcount;
1119 s.freeswap >>= bitcount;
1120 s.totalhigh >>= bitcount;
1121 s.freehigh >>= bitcount;
1122 }
1123
1124 if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
1125 __put_user (s.uptime, &info->uptime) ||
1126 __put_user (s.loads[0], &info->loads[0]) ||
1127 __put_user (s.loads[1], &info->loads[1]) ||
1128 __put_user (s.loads[2], &info->loads[2]) ||
1129 __put_user (s.totalram, &info->totalram) ||
1130 __put_user (s.freeram, &info->freeram) ||
1131 __put_user (s.sharedram, &info->sharedram) ||
1132 __put_user (s.bufferram, &info->bufferram) ||
1133 __put_user (s.totalswap, &info->totalswap) ||
1134 __put_user (s.freeswap, &info->freeswap) ||
1135 __put_user (s.procs, &info->procs) ||
1136 __put_user (s.totalhigh, &info->totalhigh) ||
1137 __put_user (s.freehigh, &info->freehigh) ||
1138 __put_user (s.mem_unit, &info->mem_unit))
1139 return -EFAULT;
1140
1141 return 0;
1142}