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