9dccddd331a39f191dd3378b9769bdc7b8dc3d8d
[linux-2.6.git] / kernel / compat.c
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
24 #include <asm/uaccess.h>
25
26 int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
27 {
28         return (verify_area(VERIFY_READ, cts, sizeof(*cts)) ||
29                         __get_user(ts->tv_sec, &cts->tv_sec) ||
30                         __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
31 }
32
33 int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
34 {
35         return (verify_area(VERIFY_WRITE, cts, sizeof(*cts)) ||
36                         __put_user(ts->tv_sec, &cts->tv_sec) ||
37                         __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
38 }
39
40 static long compat_nanosleep_restart(struct restart_block *restart)
41 {
42         unsigned long expire = restart->arg0, now = jiffies;
43         struct compat_timespec __user *rmtp;
44
45         /* Did it expire while we handled signals? */
46         if (!time_after(expire, now))
47                 return 0;
48
49         current->state = TASK_INTERRUPTIBLE;
50         expire = schedule_timeout(expire - now);
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
69 asmlinkage 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);
83         current->state = TASK_INTERRUPTIBLE;
84         expire = schedule_timeout(expire);
85         if (expire == 0)
86                 return 0;
87
88         if (rmtp) {
89                 jiffies_to_timespec(expire, &t);
90                 if (put_compat_timespec(&t, rmtp))
91                         return -EFAULT;
92         }
93         restart = &current_thread_info()->restart_block;
94         restart->fn = compat_nanosleep_restart;
95         restart->arg0 = jiffies + expire;
96         restart->arg1 = (unsigned long) rmtp;
97         return -ERESTART_RESTARTBLOCK;
98 }
99
100 static inline long get_compat_itimerval(struct itimerval *o,
101                 struct compat_itimerval __user *i)
102 {
103         return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
104                 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
105                  __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
106                  __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
107                  __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
108 }
109
110 static inline long put_compat_itimerval(struct compat_itimerval __user *o,
111                 struct itimerval *i)
112 {
113         return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
114                 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
115                  __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
116                  __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
117                  __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
118 }
119
120 asmlinkage long compat_sys_getitimer(int which,
121                 struct compat_itimerval __user *it)
122 {
123         struct itimerval kit;
124         int error;
125
126         error = do_getitimer(which, &kit);
127         if (!error && put_compat_itimerval(it, &kit))
128                 error = -EFAULT;
129         return error;
130 }
131
132 asmlinkage long compat_sys_setitimer(int which,
133                 struct compat_itimerval __user *in,
134                 struct compat_itimerval __user *out)
135 {
136         struct itimerval kin, kout;
137         int error;
138
139         if (in) {
140                 if (get_compat_itimerval(&kin, in))
141                         return -EFAULT;
142         } else
143                 memset(&kin, 0, sizeof(kin));
144
145         error = do_setitimer(which, &kin, out ? &kout : NULL);
146         if (error || !out)
147                 return error;
148         if (put_compat_itimerval(out, &kout))
149                 return -EFAULT;
150         return 0;
151 }
152
153 asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
154 {
155         /*
156          *      In the SMP world we might just be unlucky and have one of
157          *      the times increment as we use it. Since the value is an
158          *      atomically safe type this is just fine. Conceptually its
159          *      as if the syscall took an instant longer to occur.
160          */
161         if (tbuf) {
162                 struct compat_tms tmp;
163                 tmp.tms_utime = compat_jiffies_to_clock_t(current->utime);
164                 tmp.tms_stime = compat_jiffies_to_clock_t(current->stime);
165                 tmp.tms_cutime = compat_jiffies_to_clock_t(current->cutime);
166                 tmp.tms_cstime = compat_jiffies_to_clock_t(current->cstime);
167                 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
168                         return -EFAULT;
169         }
170         return compat_jiffies_to_clock_t(jiffies);
171 }
172
173 /*
174  * Assumption: old_sigset_t and compat_old_sigset_t are both
175  * types that can be passed to put_user()/get_user().
176  */
177
178 asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
179 {
180         old_sigset_t s;
181         long ret;
182         mm_segment_t old_fs = get_fs();
183
184         set_fs(KERNEL_DS);
185         ret = sys_sigpending(&s);
186         set_fs(old_fs);
187         if (ret == 0)
188                 ret = put_user(s, set);
189         return ret;
190 }
191
192 asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set,
193                 compat_old_sigset_t __user *oset)
194 {
195         old_sigset_t s;
196         long ret;
197         mm_segment_t old_fs;
198
199         if (set && get_user(s, set))
200                 return -EFAULT;
201         old_fs = get_fs();
202         set_fs(KERNEL_DS);
203         ret = sys_sigprocmask(how, set ? &s : NULL, oset ? &s : NULL);
204         set_fs(old_fs);
205         if (ret == 0)
206                 if (oset)
207                         ret = put_user(s, oset);
208         return ret;
209 }
210
211 #ifdef CONFIG_FUTEX
212 asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, int val,
213                 struct compat_timespec __user *utime, u32 __user *uaddr2)
214 {
215         struct timespec t;
216         unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
217         int val2 = 0;
218
219         if ((op == FUTEX_WAIT) && utime) {
220                 if (get_compat_timespec(&t, utime))
221                         return -EFAULT;
222                 timeout = timespec_to_jiffies(&t) + 1;
223         }
224         if (op == FUTEX_REQUEUE)
225                 val2 = (int) (long) utime;
226
227         return do_futex((unsigned long)uaddr, op, val, timeout,
228                         (unsigned long)uaddr2, val2);
229 }
230 #endif
231
232 asmlinkage long compat_sys_setrlimit(unsigned int resource,
233                 struct compat_rlimit __user *rlim)
234 {
235         struct rlimit r;
236         int ret;
237         mm_segment_t old_fs = get_fs ();
238
239         if (resource >= RLIM_NLIMITS) 
240                 return -EINVAL; 
241
242         if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
243             __get_user(r.rlim_cur, &rlim->rlim_cur) ||
244             __get_user(r.rlim_max, &rlim->rlim_max))
245                 return -EFAULT;
246
247         if (r.rlim_cur == COMPAT_RLIM_INFINITY)
248                 r.rlim_cur = RLIM_INFINITY;
249         if (r.rlim_max == COMPAT_RLIM_INFINITY)
250                 r.rlim_max = RLIM_INFINITY;
251         set_fs(KERNEL_DS);
252         ret = sys_setrlimit(resource, &r);
253         set_fs(old_fs);
254         return ret;
255 }
256
257 #ifdef COMPAT_RLIM_OLD_INFINITY
258
259 asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
260                 struct compat_rlimit __user *rlim)
261 {
262         struct rlimit r;
263         int ret;
264         mm_segment_t old_fs = get_fs();
265
266         set_fs(KERNEL_DS);
267         ret = sys_old_getrlimit(resource, &r);
268         set_fs(old_fs);
269
270         if (!ret) {
271                 if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
272                         r.rlim_cur = COMPAT_RLIM_INFINITY;
273                 if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
274                         r.rlim_max = COMPAT_RLIM_INFINITY;
275
276                 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
277                     __put_user(r.rlim_cur, &rlim->rlim_cur) ||
278                     __put_user(r.rlim_max, &rlim->rlim_max))
279                         return -EFAULT;
280         }
281         return ret;
282 }
283
284 #endif
285
286 asmlinkage long compat_sys_getrlimit (unsigned int resource,
287                 struct compat_rlimit __user *rlim)
288 {
289         struct rlimit r;
290         int ret;
291         mm_segment_t old_fs = get_fs();
292
293         set_fs(KERNEL_DS);
294         ret = sys_getrlimit(resource, &r);
295         set_fs(old_fs);
296         if (!ret) {
297                 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
298                         r.rlim_cur = COMPAT_RLIM_INFINITY;
299                 if (r.rlim_max > COMPAT_RLIM_INFINITY)
300                         r.rlim_max = COMPAT_RLIM_INFINITY;
301
302                 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
303                     __put_user(r.rlim_cur, &rlim->rlim_cur) ||
304                     __put_user(r.rlim_max, &rlim->rlim_max))
305                         return -EFAULT;
306         }
307         return ret;
308 }
309
310 static long put_compat_rusage(struct compat_rusage __user *ru, struct rusage *r)
311 {
312         if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
313             __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
314             __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
315             __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
316             __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
317             __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
318             __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
319             __put_user(r->ru_idrss, &ru->ru_idrss) ||
320             __put_user(r->ru_isrss, &ru->ru_isrss) ||
321             __put_user(r->ru_minflt, &ru->ru_minflt) ||
322             __put_user(r->ru_majflt, &ru->ru_majflt) ||
323             __put_user(r->ru_nswap, &ru->ru_nswap) ||
324             __put_user(r->ru_inblock, &ru->ru_inblock) ||
325             __put_user(r->ru_oublock, &ru->ru_oublock) ||
326             __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
327             __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
328             __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
329             __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
330             __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
331                 return -EFAULT;
332         return 0;
333 }
334
335 asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
336 {
337         struct rusage r;
338         int ret;
339         mm_segment_t old_fs = get_fs();
340
341         set_fs(KERNEL_DS);
342         ret = sys_getrusage(who, &r);
343         set_fs(old_fs);
344
345         if (ret)
346                 return ret;
347
348         if (put_compat_rusage(ru, &r))
349                 return -EFAULT;
350
351         return 0;
352 }
353
354 asmlinkage long
355 compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
356         struct compat_rusage __user *ru)
357 {
358         if (!ru) {
359                 return sys_wait4(pid, stat_addr, options, NULL);
360         } else {
361                 struct rusage r;
362                 int ret;
363                 unsigned int status;
364                 mm_segment_t old_fs = get_fs();
365
366                 set_fs (KERNEL_DS);
367                 ret = sys_wait4(pid, stat_addr ? &status : NULL, options, &r);
368                 set_fs (old_fs);
369
370                 if (ret > 0) {
371                         if (put_compat_rusage(ru, &r)) 
372                                 return -EFAULT;
373                         if (stat_addr && put_user(status, stat_addr))
374                                 return -EFAULT;
375                 }
376                 return ret;
377         }
378 }
379
380 asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid, 
381                                              unsigned int len,
382                                              compat_ulong_t __user *user_mask_ptr)
383 {
384         unsigned long kernel_mask;
385         mm_segment_t old_fs;
386         int ret;
387
388         if (get_user(kernel_mask, user_mask_ptr))
389                 return -EFAULT;
390
391         old_fs = get_fs();
392         set_fs(KERNEL_DS);
393         ret = sys_sched_setaffinity(pid,
394                                     sizeof(kernel_mask),
395                                     &kernel_mask);
396         set_fs(old_fs);
397
398         return ret;
399 }
400
401 asmlinkage int compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
402                                            compat_ulong_t __user *user_mask_ptr)
403 {
404         unsigned long kernel_mask;
405         mm_segment_t old_fs;
406         int ret;
407
408         old_fs = get_fs();
409         set_fs(KERNEL_DS);
410         ret = sys_sched_getaffinity(pid,
411                                     sizeof(kernel_mask),
412                                     &kernel_mask);
413         set_fs(old_fs);
414
415         if (ret > 0) {
416                 ret = sizeof(compat_ulong_t);
417                 if (put_user(kernel_mask, user_mask_ptr))
418                         return -EFAULT;
419         }
420
421         return ret;
422 }
423
424 static int get_compat_itimerspec(struct itimerspec *dst, 
425                                  struct compat_itimerspec __user *src)
426
427         if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
428             get_compat_timespec(&dst->it_value, &src->it_value))
429                 return -EFAULT;
430         return 0;
431
432
433 static int put_compat_itimerspec(struct compat_itimerspec __user *dst, 
434                                  struct itimerspec *src)
435
436         if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
437             put_compat_timespec(&src->it_value, &dst->it_value))
438                 return -EFAULT;
439         return 0;
440
441
442 long compat_timer_settime(timer_t timer_id, int flags, 
443                           struct compat_itimerspec __user *new, 
444                           struct compat_itimerspec __user *old)
445
446         long err;
447         mm_segment_t oldfs;
448         struct itimerspec newts, oldts;
449
450         if (!new)
451                 return -EINVAL;
452         if (get_compat_itimerspec(&newts, new))
453                 return -EFAULT; 
454         oldfs = get_fs();
455         set_fs(KERNEL_DS);
456         err = sys_timer_settime(timer_id, flags, &newts, &oldts);
457         set_fs(oldfs); 
458         if (!err && old && put_compat_itimerspec(old, &oldts))
459                 return -EFAULT;
460         return err;
461
462
463 long compat_timer_gettime(timer_t timer_id,
464                 struct compat_itimerspec __user *setting)
465
466         long err;
467         mm_segment_t oldfs;
468         struct itimerspec ts; 
469         oldfs = get_fs();
470         set_fs(KERNEL_DS);
471         err = sys_timer_gettime(timer_id, &ts); 
472         set_fs(oldfs); 
473         if (!err && put_compat_itimerspec(setting, &ts))
474                 return -EFAULT;
475         return err;
476
477
478 long compat_clock_settime(clockid_t which_clock,
479                 struct compat_timespec __user *tp)
480 {
481         long err;
482         mm_segment_t oldfs;
483         struct timespec ts; 
484         if (get_compat_timespec(&ts, tp))
485                 return -EFAULT; 
486         oldfs = get_fs();
487         set_fs(KERNEL_DS);      
488         err = sys_clock_settime(which_clock, &ts); 
489         set_fs(oldfs);
490         return err;
491
492
493 long compat_clock_gettime(clockid_t which_clock,
494                 struct compat_timespec __user *tp)
495 {
496         long err;
497         mm_segment_t oldfs;
498         struct timespec ts; 
499         oldfs = get_fs();
500         set_fs(KERNEL_DS);
501         err = sys_clock_gettime(which_clock, &ts); 
502         set_fs(oldfs);
503         if (!err && put_compat_timespec(&ts, tp))
504                 return -EFAULT; 
505         return err;
506
507
508 long compat_clock_getres(clockid_t which_clock,
509                 struct compat_timespec __user *tp)
510 {
511         long err;
512         mm_segment_t oldfs;
513         struct timespec ts; 
514         oldfs = get_fs();
515         set_fs(KERNEL_DS);
516         err = sys_clock_getres(which_clock, &ts); 
517         set_fs(oldfs);
518         if (!err && put_compat_timespec(&ts, tp))
519                 return -EFAULT; 
520         return err;
521
522
523 long compat_clock_nanosleep(clockid_t which_clock, int flags,
524                             struct compat_timespec __user *rqtp,
525                             struct compat_timespec __user *rmtp)
526 {
527         long err;
528         mm_segment_t oldfs;
529         struct timespec in, out; 
530         if (get_compat_timespec(&in, rqtp)) 
531                 return -EFAULT;
532         oldfs = get_fs();
533         set_fs(KERNEL_DS);      
534         err = sys_clock_nanosleep(which_clock, flags, &in, &out);  
535         set_fs(oldfs);
536         if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
537             put_compat_timespec(&out, rmtp))
538                 return -EFAULT;
539         return err;     
540
541
542 /* timer_create is architecture specific because it needs sigevent conversion */
543