This commit was manufactured by cvs2svn to create tag
[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                 int val3)
215 {
216         struct timespec t;
217         unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
218         int val2 = 0;
219
220         if ((op == FUTEX_WAIT) && utime) {
221                 if (get_compat_timespec(&t, utime))
222                         return -EFAULT;
223                 timeout = timespec_to_jiffies(&t) + 1;
224         }
225         if (op >= FUTEX_REQUEUE)
226                 val2 = (int) (long) utime;
227
228         return do_futex((unsigned long)uaddr, op, val, timeout,
229                         (unsigned long)uaddr2, val2, val3);
230 }
231 #endif
232
233 asmlinkage long compat_sys_setrlimit(unsigned int resource,
234                 struct compat_rlimit __user *rlim)
235 {
236         struct rlimit r;
237         int ret;
238         mm_segment_t old_fs = get_fs ();
239
240         if (resource >= RLIM_NLIMITS) 
241                 return -EINVAL; 
242
243         if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
244             __get_user(r.rlim_cur, &rlim->rlim_cur) ||
245             __get_user(r.rlim_max, &rlim->rlim_max))
246                 return -EFAULT;
247
248         if (r.rlim_cur == COMPAT_RLIM_INFINITY)
249                 r.rlim_cur = RLIM_INFINITY;
250         if (r.rlim_max == COMPAT_RLIM_INFINITY)
251                 r.rlim_max = RLIM_INFINITY;
252         set_fs(KERNEL_DS);
253         ret = sys_setrlimit(resource, &r);
254         set_fs(old_fs);
255         return ret;
256 }
257
258 #ifdef COMPAT_RLIM_OLD_INFINITY
259
260 asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
261                 struct compat_rlimit __user *rlim)
262 {
263         struct rlimit r;
264         int ret;
265         mm_segment_t old_fs = get_fs();
266
267         set_fs(KERNEL_DS);
268         ret = sys_old_getrlimit(resource, &r);
269         set_fs(old_fs);
270
271         if (!ret) {
272                 if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
273                         r.rlim_cur = COMPAT_RLIM_INFINITY;
274                 if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
275                         r.rlim_max = COMPAT_RLIM_INFINITY;
276
277                 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
278                     __put_user(r.rlim_cur, &rlim->rlim_cur) ||
279                     __put_user(r.rlim_max, &rlim->rlim_max))
280                         return -EFAULT;
281         }
282         return ret;
283 }
284
285 #endif
286
287 asmlinkage long compat_sys_getrlimit (unsigned int resource,
288                 struct compat_rlimit __user *rlim)
289 {
290         struct rlimit r;
291         int ret;
292         mm_segment_t old_fs = get_fs();
293
294         set_fs(KERNEL_DS);
295         ret = sys_getrlimit(resource, &r);
296         set_fs(old_fs);
297         if (!ret) {
298                 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
299                         r.rlim_cur = COMPAT_RLIM_INFINITY;
300                 if (r.rlim_max > COMPAT_RLIM_INFINITY)
301                         r.rlim_max = COMPAT_RLIM_INFINITY;
302
303                 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
304                     __put_user(r.rlim_cur, &rlim->rlim_cur) ||
305                     __put_user(r.rlim_max, &rlim->rlim_max))
306                         return -EFAULT;
307         }
308         return ret;
309 }
310
311 static long put_compat_rusage(struct compat_rusage __user *ru, struct rusage *r)
312 {
313         if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
314             __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
315             __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
316             __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
317             __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
318             __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
319             __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
320             __put_user(r->ru_idrss, &ru->ru_idrss) ||
321             __put_user(r->ru_isrss, &ru->ru_isrss) ||
322             __put_user(r->ru_minflt, &ru->ru_minflt) ||
323             __put_user(r->ru_majflt, &ru->ru_majflt) ||
324             __put_user(r->ru_nswap, &ru->ru_nswap) ||
325             __put_user(r->ru_inblock, &ru->ru_inblock) ||
326             __put_user(r->ru_oublock, &ru->ru_oublock) ||
327             __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
328             __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
329             __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
330             __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
331             __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
332                 return -EFAULT;
333         return 0;
334 }
335
336 asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
337 {
338         struct rusage r;
339         int ret;
340         mm_segment_t old_fs = get_fs();
341
342         set_fs(KERNEL_DS);
343         ret = sys_getrusage(who, &r);
344         set_fs(old_fs);
345
346         if (ret)
347                 return ret;
348
349         if (put_compat_rusage(ru, &r))
350                 return -EFAULT;
351
352         return 0;
353 }
354
355 asmlinkage long
356 compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
357         struct compat_rusage __user *ru)
358 {
359         if (!ru) {
360                 return sys_wait4(pid, stat_addr, options, NULL);
361         } else {
362                 struct rusage r;
363                 int ret;
364                 unsigned int status;
365                 mm_segment_t old_fs = get_fs();
366
367                 set_fs (KERNEL_DS);
368                 ret = sys_wait4(pid, stat_addr ? &status : NULL, options, &r);
369                 set_fs (old_fs);
370
371                 if (ret > 0) {
372                         if (put_compat_rusage(ru, &r)) 
373                                 return -EFAULT;
374                         if (stat_addr && put_user(status, stat_addr))
375                                 return -EFAULT;
376                 }
377                 return ret;
378         }
379 }
380
381 asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid, 
382                                              unsigned int len,
383                                              compat_ulong_t __user *user_mask_ptr)
384 {
385         unsigned long kernel_mask;
386         mm_segment_t old_fs;
387         int ret;
388
389         if (get_user(kernel_mask, user_mask_ptr))
390                 return -EFAULT;
391
392         old_fs = get_fs();
393         set_fs(KERNEL_DS);
394         ret = sys_sched_setaffinity(pid,
395                                     sizeof(kernel_mask),
396                                     &kernel_mask);
397         set_fs(old_fs);
398
399         return ret;
400 }
401
402 asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
403                                              compat_ulong_t __user *user_mask_ptr)
404 {
405         unsigned long kernel_mask;
406         mm_segment_t old_fs;
407         int ret;
408
409         old_fs = get_fs();
410         set_fs(KERNEL_DS);
411         ret = sys_sched_getaffinity(pid,
412                                     sizeof(kernel_mask),
413                                     &kernel_mask);
414         set_fs(old_fs);
415
416         if (ret > 0) {
417                 ret = sizeof(compat_ulong_t);
418                 if (put_user(kernel_mask, user_mask_ptr))
419                         return -EFAULT;
420         }
421
422         return ret;
423 }
424
425 static int get_compat_itimerspec(struct itimerspec *dst, 
426                                  struct compat_itimerspec __user *src)
427
428         if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
429             get_compat_timespec(&dst->it_value, &src->it_value))
430                 return -EFAULT;
431         return 0;
432
433
434 static int put_compat_itimerspec(struct compat_itimerspec __user *dst, 
435                                  struct itimerspec *src)
436
437         if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
438             put_compat_timespec(&src->it_value, &dst->it_value))
439                 return -EFAULT;
440         return 0;
441
442
443 long compat_timer_settime(timer_t timer_id, int flags, 
444                           struct compat_itimerspec __user *new, 
445                           struct compat_itimerspec __user *old)
446
447         long err;
448         mm_segment_t oldfs;
449         struct itimerspec newts, oldts;
450
451         if (!new)
452                 return -EINVAL;
453         if (get_compat_itimerspec(&newts, new))
454                 return -EFAULT; 
455         oldfs = get_fs();
456         set_fs(KERNEL_DS);
457         err = sys_timer_settime(timer_id, flags, &newts, &oldts);
458         set_fs(oldfs); 
459         if (!err && old && put_compat_itimerspec(old, &oldts))
460                 return -EFAULT;
461         return err;
462
463
464 long compat_timer_gettime(timer_t timer_id,
465                 struct compat_itimerspec __user *setting)
466
467         long err;
468         mm_segment_t oldfs;
469         struct itimerspec ts; 
470         oldfs = get_fs();
471         set_fs(KERNEL_DS);
472         err = sys_timer_gettime(timer_id, &ts); 
473         set_fs(oldfs); 
474         if (!err && put_compat_itimerspec(setting, &ts))
475                 return -EFAULT;
476         return err;
477
478
479 long compat_clock_settime(clockid_t which_clock,
480                 struct compat_timespec __user *tp)
481 {
482         long err;
483         mm_segment_t oldfs;
484         struct timespec ts; 
485         if (get_compat_timespec(&ts, tp))
486                 return -EFAULT; 
487         oldfs = get_fs();
488         set_fs(KERNEL_DS);      
489         err = sys_clock_settime(which_clock, &ts); 
490         set_fs(oldfs);
491         return err;
492
493
494 long compat_clock_gettime(clockid_t which_clock,
495                 struct compat_timespec __user *tp)
496 {
497         long err;
498         mm_segment_t oldfs;
499         struct timespec ts; 
500         oldfs = get_fs();
501         set_fs(KERNEL_DS);
502         err = sys_clock_gettime(which_clock, &ts); 
503         set_fs(oldfs);
504         if (!err && put_compat_timespec(&ts, tp))
505                 return -EFAULT; 
506         return err;
507
508
509 long compat_clock_getres(clockid_t which_clock,
510                 struct compat_timespec __user *tp)
511 {
512         long err;
513         mm_segment_t oldfs;
514         struct timespec ts; 
515         oldfs = get_fs();
516         set_fs(KERNEL_DS);
517         err = sys_clock_getres(which_clock, &ts); 
518         set_fs(oldfs);
519         if (!err && put_compat_timespec(&ts, tp))
520                 return -EFAULT; 
521         return err;
522
523
524 long compat_clock_nanosleep(clockid_t which_clock, int flags,
525                             struct compat_timespec __user *rqtp,
526                             struct compat_timespec __user *rmtp)
527 {
528         long err;
529         mm_segment_t oldfs;
530         struct timespec in, out; 
531         if (get_compat_timespec(&in, rqtp)) 
532                 return -EFAULT;
533         oldfs = get_fs();
534         set_fs(KERNEL_DS);      
535         err = sys_clock_nanosleep(which_clock, flags, &in, &out);  
536         set_fs(oldfs);
537         if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
538             put_compat_timespec(&out, rmtp))
539                 return -EFAULT;
540         return err;     
541
542
543 /* timer_create is architecture specific because it needs sigevent conversion */
544