blob: 72650e39b3e6bc296368913b71dae42893844acd [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>
Toyo Abe1711ef32006-09-29 02:00:28 -070025#include <linux/posix-timers.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070026#include <linux/times.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
31{
32 return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
33 __get_user(ts->tv_sec, &cts->tv_sec) ||
34 __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
35}
36
37int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
38{
39 return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
40 __put_user(ts->tv_sec, &cts->tv_sec) ||
41 __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
42}
43
Oleg Nesterov41652932008-02-01 20:35:31 +030044static long compat_nanosleep_restart(struct restart_block *restart)
45{
46 struct compat_timespec __user *rmtp;
47 struct timespec rmt;
48 mm_segment_t oldfs;
49 long ret;
50
Thomas Gleixner029a07e2008-02-10 09:17:43 +010051 restart->nanosleep.rmtp = (struct timespec __user *) &rmt;
Oleg Nesterov41652932008-02-01 20:35:31 +030052 oldfs = get_fs();
53 set_fs(KERNEL_DS);
54 ret = hrtimer_nanosleep_restart(restart);
55 set_fs(oldfs);
56
57 if (ret) {
Thomas Gleixner029a07e2008-02-10 09:17:43 +010058 rmtp = restart->nanosleep.compat_rmtp;
Oleg Nesterov41652932008-02-01 20:35:31 +030059
60 if (rmtp && put_compat_timespec(&rmt, rmtp))
61 return -EFAULT;
62 }
63
64 return ret;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
Anton Blanchardc70878b2007-10-15 16:13:56 -050068 struct compat_timespec __user *rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Anton Blanchardc70878b2007-10-15 16:13:56 -050070 struct timespec tu, rmt;
Oleg Nesterov41652932008-02-01 20:35:31 +030071 mm_segment_t oldfs;
Anton Blanchardc70878b2007-10-15 16:13:56 -050072 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Anton Blanchardc70878b2007-10-15 16:13:56 -050074 if (get_compat_timespec(&tu, rqtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EFAULT;
76
Anton Blanchardc70878b2007-10-15 16:13:56 -050077 if (!timespec_valid(&tu))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EINVAL;
79
Oleg Nesterov41652932008-02-01 20:35:31 +030080 oldfs = get_fs();
81 set_fs(KERNEL_DS);
82 ret = hrtimer_nanosleep(&tu,
83 rmtp ? (struct timespec __user *)&rmt : NULL,
84 HRTIMER_MODE_REL, CLOCK_MONOTONIC);
85 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Oleg Nesterov41652932008-02-01 20:35:31 +030087 if (ret) {
88 struct restart_block *restart
89 = &current_thread_info()->restart_block;
90
91 restart->fn = compat_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +010092 restart->nanosleep.compat_rmtp = rmtp;
Oleg Nesterov41652932008-02-01 20:35:31 +030093
94 if (rmtp && put_compat_timespec(&rmt, rmtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return -EFAULT;
96 }
Anton Blanchardc70878b2007-10-15 16:13:56 -050097
98 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static inline long get_compat_itimerval(struct itimerval *o,
102 struct compat_itimerval __user *i)
103{
104 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
105 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
106 __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
107 __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
108 __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
109}
110
111static inline long put_compat_itimerval(struct compat_itimerval __user *o,
112 struct itimerval *i)
113{
114 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
115 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
116 __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
117 __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
118 __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
119}
120
121asmlinkage long compat_sys_getitimer(int which,
122 struct compat_itimerval __user *it)
123{
124 struct itimerval kit;
125 int error;
126
127 error = do_getitimer(which, &kit);
128 if (!error && put_compat_itimerval(it, &kit))
129 error = -EFAULT;
130 return error;
131}
132
133asmlinkage long compat_sys_setitimer(int which,
134 struct compat_itimerval __user *in,
135 struct compat_itimerval __user *out)
136{
137 struct itimerval kin, kout;
138 int error;
139
140 if (in) {
141 if (get_compat_itimerval(&kin, in))
142 return -EFAULT;
143 } else
144 memset(&kin, 0, sizeof(kin));
145
146 error = do_setitimer(which, &kin, out ? &kout : NULL);
147 if (error || !out)
148 return error;
149 if (put_compat_itimerval(out, &kout))
150 return -EFAULT;
151 return 0;
152}
153
Frank Mayharf06febc2008-09-12 09:54:39 -0700154static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
155{
156 return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
160{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (tbuf) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700162 struct tms tms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct compat_tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Frank Mayharf06febc2008-09-12 09:54:39 -0700165 do_sys_times(&tms);
166 /* Convert our struct tms to the compat version. */
167 tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
168 tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
169 tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
170 tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
172 return -EFAULT;
173 }
174 return compat_jiffies_to_clock_t(jiffies);
175}
176
177/*
178 * Assumption: old_sigset_t and compat_old_sigset_t are both
179 * types that can be passed to put_user()/get_user().
180 */
181
182asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
183{
184 old_sigset_t s;
185 long ret;
186 mm_segment_t old_fs = get_fs();
187
188 set_fs(KERNEL_DS);
189 ret = sys_sigpending((old_sigset_t __user *) &s);
190 set_fs(old_fs);
191 if (ret == 0)
192 ret = put_user(s, set);
193 return ret;
194}
195
196asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set,
197 compat_old_sigset_t __user *oset)
198{
199 old_sigset_t s;
200 long ret;
201 mm_segment_t old_fs;
202
203 if (set && get_user(s, set))
204 return -EFAULT;
205 old_fs = get_fs();
206 set_fs(KERNEL_DS);
207 ret = sys_sigprocmask(how,
208 set ? (old_sigset_t __user *) &s : NULL,
209 oset ? (old_sigset_t __user *) &s : NULL);
210 set_fs(old_fs);
211 if (ret == 0)
212 if (oset)
213 ret = put_user(s, oset);
214 return ret;
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217asmlinkage long compat_sys_setrlimit(unsigned int resource,
218 struct compat_rlimit __user *rlim)
219{
220 struct rlimit r;
221 int ret;
222 mm_segment_t old_fs = get_fs ();
223
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700224 if (resource >= RLIM_NLIMITS)
225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
228 __get_user(r.rlim_cur, &rlim->rlim_cur) ||
229 __get_user(r.rlim_max, &rlim->rlim_max))
230 return -EFAULT;
231
232 if (r.rlim_cur == COMPAT_RLIM_INFINITY)
233 r.rlim_cur = RLIM_INFINITY;
234 if (r.rlim_max == COMPAT_RLIM_INFINITY)
235 r.rlim_max = RLIM_INFINITY;
236 set_fs(KERNEL_DS);
237 ret = sys_setrlimit(resource, (struct rlimit __user *) &r);
238 set_fs(old_fs);
239 return ret;
240}
241
242#ifdef COMPAT_RLIM_OLD_INFINITY
243
244asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
245 struct compat_rlimit __user *rlim)
246{
247 struct rlimit r;
248 int ret;
249 mm_segment_t old_fs = get_fs();
250
251 set_fs(KERNEL_DS);
252 ret = sys_old_getrlimit(resource, &r);
253 set_fs(old_fs);
254
255 if (!ret) {
256 if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
257 r.rlim_cur = COMPAT_RLIM_INFINITY;
258 if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
259 r.rlim_max = COMPAT_RLIM_INFINITY;
260
261 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
262 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
263 __put_user(r.rlim_max, &rlim->rlim_max))
264 return -EFAULT;
265 }
266 return ret;
267}
268
269#endif
270
271asmlinkage long compat_sys_getrlimit (unsigned int resource,
272 struct compat_rlimit __user *rlim)
273{
274 struct rlimit r;
275 int ret;
276 mm_segment_t old_fs = get_fs();
277
278 set_fs(KERNEL_DS);
279 ret = sys_getrlimit(resource, (struct rlimit __user *) &r);
280 set_fs(old_fs);
281 if (!ret) {
282 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
283 r.rlim_cur = COMPAT_RLIM_INFINITY;
284 if (r.rlim_max > COMPAT_RLIM_INFINITY)
285 r.rlim_max = COMPAT_RLIM_INFINITY;
286
287 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
288 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
289 __put_user(r.rlim_max, &rlim->rlim_max))
290 return -EFAULT;
291 }
292 return ret;
293}
294
295int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
296{
297 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
298 __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
299 __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
300 __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
301 __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
302 __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
303 __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
304 __put_user(r->ru_idrss, &ru->ru_idrss) ||
305 __put_user(r->ru_isrss, &ru->ru_isrss) ||
306 __put_user(r->ru_minflt, &ru->ru_minflt) ||
307 __put_user(r->ru_majflt, &ru->ru_majflt) ||
308 __put_user(r->ru_nswap, &ru->ru_nswap) ||
309 __put_user(r->ru_inblock, &ru->ru_inblock) ||
310 __put_user(r->ru_oublock, &ru->ru_oublock) ||
311 __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
312 __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
313 __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
314 __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
315 __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
316 return -EFAULT;
317 return 0;
318}
319
320asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
321{
322 struct rusage r;
323 int ret;
324 mm_segment_t old_fs = get_fs();
325
326 set_fs(KERNEL_DS);
327 ret = sys_getrusage(who, (struct rusage __user *) &r);
328 set_fs(old_fs);
329
330 if (ret)
331 return ret;
332
333 if (put_compat_rusage(&r, ru))
334 return -EFAULT;
335
336 return 0;
337}
338
339asmlinkage long
340compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
341 struct compat_rusage __user *ru)
342{
343 if (!ru) {
344 return sys_wait4(pid, stat_addr, options, NULL);
345 } else {
346 struct rusage r;
347 int ret;
348 unsigned int status;
349 mm_segment_t old_fs = get_fs();
350
351 set_fs (KERNEL_DS);
352 ret = sys_wait4(pid,
353 (stat_addr ?
354 (unsigned int __user *) &status : NULL),
355 options, (struct rusage __user *) &r);
356 set_fs (old_fs);
357
358 if (ret > 0) {
359 if (put_compat_rusage(&r, ru))
360 return -EFAULT;
361 if (stat_addr && put_user(status, stat_addr))
362 return -EFAULT;
363 }
364 return ret;
365 }
366}
367
368asmlinkage long compat_sys_waitid(int which, compat_pid_t pid,
369 struct compat_siginfo __user *uinfo, int options,
370 struct compat_rusage __user *uru)
371{
372 siginfo_t info;
373 struct rusage ru;
374 long ret;
375 mm_segment_t old_fs = get_fs();
376
377 memset(&info, 0, sizeof(info));
378
379 set_fs(KERNEL_DS);
380 ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
381 uru ? (struct rusage __user *)&ru : NULL);
382 set_fs(old_fs);
383
384 if ((ret < 0) || (info.si_signo == 0))
385 return ret;
386
387 if (uru) {
388 ret = put_compat_rusage(&ru, uru);
389 if (ret)
390 return ret;
391 }
392
393 BUG_ON(info.si_code & __SI_MASK);
394 info.si_code |= __SI_CHLD;
395 return copy_siginfo_to_user32(uinfo, &info);
396}
397
398static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
399 unsigned len, cpumask_t *new_mask)
400{
401 unsigned long *k;
402
403 if (len < sizeof(cpumask_t))
404 memset(new_mask, 0, sizeof(cpumask_t));
405 else if (len > sizeof(cpumask_t))
406 len = sizeof(cpumask_t);
407
408 k = cpus_addr(*new_mask);
409 return compat_get_bitmap(k, user_mask_ptr, len * 8);
410}
411
412asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
413 unsigned int len,
414 compat_ulong_t __user *user_mask_ptr)
415{
416 cpumask_t new_mask;
417 int retval;
418
419 retval = compat_get_user_cpu_mask(user_mask_ptr, len, &new_mask);
420 if (retval)
421 return retval;
422
Mike Travisb53e9212008-04-04 18:11:08 -0700423 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
427 compat_ulong_t __user *user_mask_ptr)
428{
429 int ret;
430 cpumask_t mask;
431 unsigned long *k;
432 unsigned int min_length = sizeof(cpumask_t);
433
434 if (NR_CPUS <= BITS_PER_COMPAT_LONG)
435 min_length = sizeof(compat_ulong_t);
436
437 if (len < min_length)
438 return -EINVAL;
439
440 ret = sched_getaffinity(pid, &mask);
441 if (ret < 0)
442 return ret;
443
444 k = cpus_addr(mask);
445 ret = compat_put_bitmap(user_mask_ptr, k, min_length * 8);
446 if (ret)
447 return ret;
448
449 return min_length;
450}
451
Davide Libenzi83f5d122007-05-10 22:23:18 -0700452int get_compat_itimerspec(struct itimerspec *dst,
453 const struct compat_itimerspec __user *src)
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700454{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
456 get_compat_timespec(&dst->it_value, &src->it_value))
457 return -EFAULT;
458 return 0;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700459}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Davide Libenzi83f5d122007-05-10 22:23:18 -0700461int put_compat_itimerspec(struct compat_itimerspec __user *dst,
462 const struct itimerspec *src)
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700463{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
465 put_compat_timespec(&src->it_value, &dst->it_value))
466 return -EFAULT;
467 return 0;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700468}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Christoph Hellwig3a0f69d2006-01-09 20:52:08 -0800470long compat_sys_timer_create(clockid_t which_clock,
471 struct compat_sigevent __user *timer_event_spec,
472 timer_t __user *created_timer_id)
473{
474 struct sigevent __user *event = NULL;
475
476 if (timer_event_spec) {
477 struct sigevent kevent;
478
479 event = compat_alloc_user_space(sizeof(*event));
480 if (get_compat_sigevent(&kevent, timer_event_spec) ||
481 copy_to_user(event, &kevent, sizeof(*event)))
482 return -EFAULT;
483 }
484
485 return sys_timer_create(which_clock, event, created_timer_id);
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488long compat_sys_timer_settime(timer_t timer_id, int flags,
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700489 struct compat_itimerspec __user *new,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct compat_itimerspec __user *old)
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700491{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 long err;
493 mm_segment_t oldfs;
494 struct itimerspec newts, oldts;
495
496 if (!new)
497 return -EINVAL;
498 if (get_compat_itimerspec(&newts, new))
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700499 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 oldfs = get_fs();
501 set_fs(KERNEL_DS);
502 err = sys_timer_settime(timer_id, flags,
503 (struct itimerspec __user *) &newts,
504 (struct itimerspec __user *) &oldts);
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700505 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (!err && old && put_compat_itimerspec(old, &oldts))
507 return -EFAULT;
508 return err;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511long compat_sys_timer_gettime(timer_t timer_id,
512 struct compat_itimerspec __user *setting)
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700513{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 long err;
515 mm_segment_t oldfs;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700516 struct itimerspec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 oldfs = get_fs();
519 set_fs(KERNEL_DS);
520 err = sys_timer_gettime(timer_id,
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700521 (struct itimerspec __user *) &ts);
522 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (!err && put_compat_itimerspec(setting, &ts))
524 return -EFAULT;
525 return err;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700526}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528long compat_sys_clock_settime(clockid_t which_clock,
529 struct compat_timespec __user *tp)
530{
531 long err;
532 mm_segment_t oldfs;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700533 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (get_compat_timespec(&ts, tp))
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700536 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 oldfs = get_fs();
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700538 set_fs(KERNEL_DS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 err = sys_clock_settime(which_clock,
540 (struct timespec __user *) &ts);
541 set_fs(oldfs);
542 return err;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700543}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545long compat_sys_clock_gettime(clockid_t which_clock,
546 struct compat_timespec __user *tp)
547{
548 long err;
549 mm_segment_t oldfs;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700550 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 oldfs = get_fs();
553 set_fs(KERNEL_DS);
554 err = sys_clock_gettime(which_clock,
555 (struct timespec __user *) &ts);
556 set_fs(oldfs);
557 if (!err && put_compat_timespec(&ts, tp))
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700558 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return err;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700560}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562long compat_sys_clock_getres(clockid_t which_clock,
563 struct compat_timespec __user *tp)
564{
565 long err;
566 mm_segment_t oldfs;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700567 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 oldfs = get_fs();
570 set_fs(KERNEL_DS);
571 err = sys_clock_getres(which_clock,
572 (struct timespec __user *) &ts);
573 set_fs(oldfs);
574 if (!err && tp && put_compat_timespec(&ts, tp))
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700575 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return err;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700577}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Toyo Abe1711ef32006-09-29 02:00:28 -0700579static long compat_clock_nanosleep_restart(struct restart_block *restart)
580{
581 long err;
582 mm_segment_t oldfs;
583 struct timespec tu;
Thomas Gleixner029a07e2008-02-10 09:17:43 +0100584 struct compat_timespec *rmtp = restart->nanosleep.compat_rmtp;
Toyo Abe1711ef32006-09-29 02:00:28 -0700585
Thomas Gleixner029a07e2008-02-10 09:17:43 +0100586 restart->nanosleep.rmtp = (struct timespec __user *) &tu;
Toyo Abe1711ef32006-09-29 02:00:28 -0700587 oldfs = get_fs();
588 set_fs(KERNEL_DS);
589 err = clock_nanosleep_restart(restart);
590 set_fs(oldfs);
591
592 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
593 put_compat_timespec(&tu, rmtp))
594 return -EFAULT;
595
596 if (err == -ERESTART_RESTARTBLOCK) {
597 restart->fn = compat_clock_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +0100598 restart->nanosleep.compat_rmtp = rmtp;
Toyo Abe1711ef32006-09-29 02:00:28 -0700599 }
600 return err;
601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
604 struct compat_timespec __user *rqtp,
605 struct compat_timespec __user *rmtp)
606{
607 long err;
608 mm_segment_t oldfs;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700609 struct timespec in, out;
Toyo Abe1711ef32006-09-29 02:00:28 -0700610 struct restart_block *restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700612 if (get_compat_timespec(&in, rqtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 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);
Toyo Abe1711ef32006-09-29 02:00:28 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
623 put_compat_timespec(&out, rmtp))
624 return -EFAULT;
Toyo Abe1711ef32006-09-29 02:00:28 -0700625
626 if (err == -ERESTART_RESTARTBLOCK) {
627 restart = &current_thread_info()->restart_block;
628 restart->fn = compat_clock_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +0100629 restart->nanosleep.compat_rmtp = rmtp;
Toyo Abe1711ef32006-09-29 02:00:28 -0700630 }
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700631 return err;
632}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634/*
635 * We currently only need the following fields from the sigevent
636 * structure: sigev_value, sigev_signo, sig_notify and (sometimes
637 * sigev_notify_thread_id). The others are handled in user mode.
638 * We also assume that copying sigev_value.sival_int is sufficient
639 * to keep all the bits of sigev_value.sival_ptr intact.
640 */
641int get_compat_sigevent(struct sigevent *event,
642 const struct compat_sigevent __user *u_event)
643{
David S. Miller51410d32005-04-16 15:24:01 -0700644 memset(event, 0, sizeof(*event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
646 __get_user(event->sigev_value.sival_int,
647 &u_event->sigev_value.sival_int) ||
648 __get_user(event->sigev_signo, &u_event->sigev_signo) ||
649 __get_user(event->sigev_notify, &u_event->sigev_notify) ||
650 __get_user(event->sigev_notify_thread_id,
651 &u_event->sigev_notify_thread_id))
652 ? -EFAULT : 0;
653}
654
Stephen Rothwell5fa38392006-10-28 10:38:46 -0700655long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 unsigned long bitmap_size)
657{
658 int i, j;
659 unsigned long m;
660 compat_ulong_t um;
661 unsigned long nr_compat_longs;
662
663 /* align bitmap up to nearest compat_long_t boundary */
664 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
665
666 if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
667 return -EFAULT;
668
669 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
670
671 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
672 m = 0;
673
674 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
675 /*
676 * We dont want to read past the end of the userspace
677 * bitmap. We must however ensure the end of the
678 * kernel bitmap is zeroed.
679 */
680 if (nr_compat_longs-- > 0) {
681 if (__get_user(um, umask))
682 return -EFAULT;
683 } else {
684 um = 0;
685 }
686
687 umask++;
688 m |= (long)um << (j * BITS_PER_COMPAT_LONG);
689 }
690 *mask++ = m;
691 }
692
693 return 0;
694}
695
696long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
697 unsigned long bitmap_size)
698{
699 int i, j;
700 unsigned long m;
701 compat_ulong_t um;
702 unsigned long nr_compat_longs;
703
704 /* align bitmap up to nearest compat_long_t boundary */
705 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
706
707 if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
708 return -EFAULT;
709
710 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
711
712 for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
713 m = *mask++;
714
715 for (j = 0; j < sizeof(m)/sizeof(um); j++) {
716 um = m;
717
718 /*
719 * We dont want to write past the end of the userspace
720 * bitmap.
721 */
722 if (nr_compat_longs-- > 0) {
723 if (__put_user(um, umask))
724 return -EFAULT;
725 }
726
727 umask++;
728 m >>= 4*sizeof(um);
729 m >>= 4*sizeof(um);
730 }
731 }
732
733 return 0;
734}
735
736void
737sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
738{
739 switch (_NSIG_WORDS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
741 case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
742 case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
743 case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
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();
Roland McGrath4e4c22c2008-04-30 00:53:06 -0700876 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -0800877 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) ||
Roman Zippel153b5d02008-05-01 04:34:37 -0700933 __put_user(txc.stbcnt, &utp->stbcnt) ||
934 __put_user(txc.tai, &utp->tai))
Stephen Rothwell3158e942006-03-26 01:37:29 -0800935 ret = -EFAULT;
936
937 return ret;
938}
Christoph Lameter1b2db9f2006-06-23 02:03:56 -0700939
940#ifdef CONFIG_NUMA
941asmlinkage long compat_sys_move_pages(pid_t pid, unsigned long nr_pages,
Christoph Lameter9216dfa2006-06-23 02:03:57 -0700942 compat_uptr_t __user *pages32,
Christoph Lameter1b2db9f2006-06-23 02:03:56 -0700943 const int __user *nodes,
944 int __user *status,
945 int flags)
946{
947 const void __user * __user *pages;
948 int i;
949
950 pages = compat_alloc_user_space(nr_pages * sizeof(void *));
951 for (i = 0; i < nr_pages; i++) {
952 compat_uptr_t p;
953
Christoph Lameter9216dfa2006-06-23 02:03:57 -0700954 if (get_user(p, pages32 + i) ||
Christoph Lameter1b2db9f2006-06-23 02:03:56 -0700955 put_user(compat_ptr(p), pages + i))
956 return -EFAULT;
957 }
958 return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
959}
Stephen Rothwell3fd59392006-11-02 22:07:24 -0800960
961asmlinkage long compat_sys_migrate_pages(compat_pid_t pid,
962 compat_ulong_t maxnode,
963 const compat_ulong_t __user *old_nodes,
964 const compat_ulong_t __user *new_nodes)
965{
966 unsigned long __user *old = NULL;
967 unsigned long __user *new = NULL;
968 nodemask_t tmp_mask;
969 unsigned long nr_bits;
970 unsigned long size;
971
972 nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
973 size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
974 if (old_nodes) {
975 if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
976 return -EFAULT;
977 old = compat_alloc_user_space(new_nodes ? size * 2 : size);
978 if (new_nodes)
979 new = old + size / sizeof(unsigned long);
980 if (copy_to_user(old, nodes_addr(tmp_mask), size))
981 return -EFAULT;
982 }
983 if (new_nodes) {
984 if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
985 return -EFAULT;
986 if (new == NULL)
987 new = compat_alloc_user_space(size);
988 if (copy_to_user(new, nodes_addr(tmp_mask), size))
989 return -EFAULT;
990 }
991 return sys_migrate_pages(pid, nr_bits + 1, old, new);
992}
Christoph Lameter1b2db9f2006-06-23 02:03:56 -0700993#endif
Kyle McMartind4d23ad2007-02-10 01:46:00 -0800994
995struct compat_sysinfo {
996 s32 uptime;
997 u32 loads[3];
998 u32 totalram;
999 u32 freeram;
1000 u32 sharedram;
1001 u32 bufferram;
1002 u32 totalswap;
1003 u32 freeswap;
1004 u16 procs;
1005 u16 pad;
1006 u32 totalhigh;
1007 u32 freehigh;
1008 u32 mem_unit;
1009 char _f[20-2*sizeof(u32)-sizeof(int)];
1010};
1011
1012asmlinkage long
1013compat_sys_sysinfo(struct compat_sysinfo __user *info)
1014{
1015 struct sysinfo s;
1016
1017 do_sysinfo(&s);
1018
1019 /* Check to see if any memory value is too large for 32-bit and scale
1020 * down if needed
1021 */
1022 if ((s.totalram >> 32) || (s.totalswap >> 32)) {
1023 int bitcount = 0;
1024
1025 while (s.mem_unit < PAGE_SIZE) {
1026 s.mem_unit <<= 1;
1027 bitcount++;
1028 }
1029
1030 s.totalram >>= bitcount;
1031 s.freeram >>= bitcount;
1032 s.sharedram >>= bitcount;
1033 s.bufferram >>= bitcount;
1034 s.totalswap >>= bitcount;
1035 s.freeswap >>= bitcount;
1036 s.totalhigh >>= bitcount;
1037 s.freehigh >>= bitcount;
1038 }
1039
1040 if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
1041 __put_user (s.uptime, &info->uptime) ||
1042 __put_user (s.loads[0], &info->loads[0]) ||
1043 __put_user (s.loads[1], &info->loads[1]) ||
1044 __put_user (s.loads[2], &info->loads[2]) ||
1045 __put_user (s.totalram, &info->totalram) ||
1046 __put_user (s.freeram, &info->freeram) ||
1047 __put_user (s.sharedram, &info->sharedram) ||
1048 __put_user (s.bufferram, &info->bufferram) ||
1049 __put_user (s.totalswap, &info->totalswap) ||
1050 __put_user (s.freeswap, &info->freeswap) ||
1051 __put_user (s.procs, &info->procs) ||
1052 __put_user (s.totalhigh, &info->totalhigh) ||
1053 __put_user (s.freehigh, &info->freehigh) ||
1054 __put_user (s.mem_unit, &info->mem_unit))
1055 return -EFAULT;
1056
1057 return 0;
1058}