2 * linux/kernel/compat.c
4 * Kernel compatibililty routines for e.g. 32 bit syscall support
7 * Copyright (C) 2002-2003 Stephen Rothwell, IBM Corporation
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.
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/syscalls.h>
21 #include <linux/unistd.h>
22 #include <linux/security.h>
23 #include <linux/export.h>
24 #include <linux/migrate.h>
25 #include <linux/posix-timers.h>
26 #include <linux/times.h>
27 #include <linux/ptrace.h>
28 #include <linux/gfp.h>
30 #include <linux/uaccess.h>
32 static int __compat_get_timeval(struct timeval
*tv
, const struct old_timeval32 __user
*ctv
)
34 return (!access_ok(ctv
, sizeof(*ctv
)) ||
35 __get_user(tv
->tv_sec
, &ctv
->tv_sec
) ||
36 __get_user(tv
->tv_usec
, &ctv
->tv_usec
)) ? -EFAULT
: 0;
39 static int __compat_put_timeval(const struct timeval
*tv
, struct old_timeval32 __user
*ctv
)
41 return (!access_ok(ctv
, sizeof(*ctv
)) ||
42 __put_user(tv
->tv_sec
, &ctv
->tv_sec
) ||
43 __put_user(tv
->tv_usec
, &ctv
->tv_usec
)) ? -EFAULT
: 0;
46 static int __compat_get_timespec(struct timespec
*ts
, const struct old_timespec32 __user
*cts
)
48 return (!access_ok(cts
, sizeof(*cts
)) ||
49 __get_user(ts
->tv_sec
, &cts
->tv_sec
) ||
50 __get_user(ts
->tv_nsec
, &cts
->tv_nsec
)) ? -EFAULT
: 0;
53 static int __compat_put_timespec(const struct timespec
*ts
, struct old_timespec32 __user
*cts
)
55 return (!access_ok(cts
, sizeof(*cts
)) ||
56 __put_user(ts
->tv_sec
, &cts
->tv_sec
) ||
57 __put_user(ts
->tv_nsec
, &cts
->tv_nsec
)) ? -EFAULT
: 0;
60 int compat_get_timeval(struct timeval
*tv
, const void __user
*utv
)
62 if (COMPAT_USE_64BIT_TIME
)
63 return copy_from_user(tv
, utv
, sizeof(*tv
)) ? -EFAULT
: 0;
65 return __compat_get_timeval(tv
, utv
);
67 EXPORT_SYMBOL_GPL(compat_get_timeval
);
69 int compat_put_timeval(const struct timeval
*tv
, void __user
*utv
)
71 if (COMPAT_USE_64BIT_TIME
)
72 return copy_to_user(utv
, tv
, sizeof(*tv
)) ? -EFAULT
: 0;
74 return __compat_put_timeval(tv
, utv
);
76 EXPORT_SYMBOL_GPL(compat_put_timeval
);
78 int compat_get_timespec(struct timespec
*ts
, const void __user
*uts
)
80 if (COMPAT_USE_64BIT_TIME
)
81 return copy_from_user(ts
, uts
, sizeof(*ts
)) ? -EFAULT
: 0;
83 return __compat_get_timespec(ts
, uts
);
85 EXPORT_SYMBOL_GPL(compat_get_timespec
);
87 int compat_put_timespec(const struct timespec
*ts
, void __user
*uts
)
89 if (COMPAT_USE_64BIT_TIME
)
90 return copy_to_user(uts
, ts
, sizeof(*ts
)) ? -EFAULT
: 0;
92 return __compat_put_timespec(ts
, uts
);
94 EXPORT_SYMBOL_GPL(compat_put_timespec
);
96 int get_compat_itimerval(struct itimerval
*o
, const struct compat_itimerval __user
*i
)
98 struct compat_itimerval v32
;
100 if (copy_from_user(&v32
, i
, sizeof(struct compat_itimerval
)))
102 o
->it_interval
.tv_sec
= v32
.it_interval
.tv_sec
;
103 o
->it_interval
.tv_usec
= v32
.it_interval
.tv_usec
;
104 o
->it_value
.tv_sec
= v32
.it_value
.tv_sec
;
105 o
->it_value
.tv_usec
= v32
.it_value
.tv_usec
;
109 int put_compat_itimerval(struct compat_itimerval __user
*o
, const struct itimerval
*i
)
111 struct compat_itimerval v32
;
113 v32
.it_interval
.tv_sec
= i
->it_interval
.tv_sec
;
114 v32
.it_interval
.tv_usec
= i
->it_interval
.tv_usec
;
115 v32
.it_value
.tv_sec
= i
->it_value
.tv_sec
;
116 v32
.it_value
.tv_usec
= i
->it_value
.tv_usec
;
117 return copy_to_user(o
, &v32
, sizeof(struct compat_itimerval
)) ? -EFAULT
: 0;
120 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
123 * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
124 * blocked set of signals to the supplied signal set
126 static inline void compat_sig_setmask(sigset_t
*blocked
, compat_sigset_word set
)
128 memcpy(blocked
->sig
, &set
, sizeof(set
));
131 COMPAT_SYSCALL_DEFINE3(sigprocmask
, int, how
,
132 compat_old_sigset_t __user
*, nset
,
133 compat_old_sigset_t __user
*, oset
)
135 old_sigset_t old_set
, new_set
;
136 sigset_t new_blocked
;
138 old_set
= current
->blocked
.sig
[0];
141 if (get_user(new_set
, nset
))
143 new_set
&= ~(sigmask(SIGKILL
) | sigmask(SIGSTOP
));
145 new_blocked
= current
->blocked
;
149 sigaddsetmask(&new_blocked
, new_set
);
152 sigdelsetmask(&new_blocked
, new_set
);
155 compat_sig_setmask(&new_blocked
, new_set
);
161 set_current_blocked(&new_blocked
);
165 if (put_user(old_set
, oset
))
174 int put_compat_rusage(const struct rusage
*r
, struct compat_rusage __user
*ru
)
176 struct compat_rusage r32
;
177 memset(&r32
, 0, sizeof(r32
));
178 r32
.ru_utime
.tv_sec
= r
->ru_utime
.tv_sec
;
179 r32
.ru_utime
.tv_usec
= r
->ru_utime
.tv_usec
;
180 r32
.ru_stime
.tv_sec
= r
->ru_stime
.tv_sec
;
181 r32
.ru_stime
.tv_usec
= r
->ru_stime
.tv_usec
;
182 r32
.ru_maxrss
= r
->ru_maxrss
;
183 r32
.ru_ixrss
= r
->ru_ixrss
;
184 r32
.ru_idrss
= r
->ru_idrss
;
185 r32
.ru_isrss
= r
->ru_isrss
;
186 r32
.ru_minflt
= r
->ru_minflt
;
187 r32
.ru_majflt
= r
->ru_majflt
;
188 r32
.ru_nswap
= r
->ru_nswap
;
189 r32
.ru_inblock
= r
->ru_inblock
;
190 r32
.ru_oublock
= r
->ru_oublock
;
191 r32
.ru_msgsnd
= r
->ru_msgsnd
;
192 r32
.ru_msgrcv
= r
->ru_msgrcv
;
193 r32
.ru_nsignals
= r
->ru_nsignals
;
194 r32
.ru_nvcsw
= r
->ru_nvcsw
;
195 r32
.ru_nivcsw
= r
->ru_nivcsw
;
196 if (copy_to_user(ru
, &r32
, sizeof(r32
)))
201 static int compat_get_user_cpu_mask(compat_ulong_t __user
*user_mask_ptr
,
202 unsigned len
, struct cpumask
*new_mask
)
206 if (len
< cpumask_size())
207 memset(new_mask
, 0, cpumask_size());
208 else if (len
> cpumask_size())
209 len
= cpumask_size();
211 k
= cpumask_bits(new_mask
);
212 return compat_get_bitmap(k
, user_mask_ptr
, len
* 8);
215 COMPAT_SYSCALL_DEFINE3(sched_setaffinity
, compat_pid_t
, pid
,
217 compat_ulong_t __user
*, user_mask_ptr
)
219 cpumask_var_t new_mask
;
222 if (!alloc_cpumask_var(&new_mask
, GFP_KERNEL
))
225 retval
= compat_get_user_cpu_mask(user_mask_ptr
, len
, new_mask
);
229 retval
= sched_setaffinity(pid
, new_mask
);
231 free_cpumask_var(new_mask
);
235 COMPAT_SYSCALL_DEFINE3(sched_getaffinity
, compat_pid_t
, pid
, unsigned int, len
,
236 compat_ulong_t __user
*, user_mask_ptr
)
241 if ((len
* BITS_PER_BYTE
) < nr_cpu_ids
)
243 if (len
& (sizeof(compat_ulong_t
)-1))
246 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
249 ret
= sched_getaffinity(pid
, mask
);
251 unsigned int retlen
= min(len
, cpumask_size());
253 if (compat_put_bitmap(user_mask_ptr
, cpumask_bits(mask
), retlen
* 8))
258 free_cpumask_var(mask
);
264 * We currently only need the following fields from the sigevent
265 * structure: sigev_value, sigev_signo, sig_notify and (sometimes
266 * sigev_notify_thread_id). The others are handled in user mode.
267 * We also assume that copying sigev_value.sival_int is sufficient
268 * to keep all the bits of sigev_value.sival_ptr intact.
270 int get_compat_sigevent(struct sigevent
*event
,
271 const struct compat_sigevent __user
*u_event
)
273 memset(event
, 0, sizeof(*event
));
274 return (!access_ok(u_event
, sizeof(*u_event
)) ||
275 __get_user(event
->sigev_value
.sival_int
,
276 &u_event
->sigev_value
.sival_int
) ||
277 __get_user(event
->sigev_signo
, &u_event
->sigev_signo
) ||
278 __get_user(event
->sigev_notify
, &u_event
->sigev_notify
) ||
279 __get_user(event
->sigev_notify_thread_id
,
280 &u_event
->sigev_notify_thread_id
))
284 long compat_get_bitmap(unsigned long *mask
, const compat_ulong_t __user
*umask
,
285 unsigned long bitmap_size
)
287 unsigned long nr_compat_longs
;
289 /* align bitmap up to nearest compat_long_t boundary */
290 bitmap_size
= ALIGN(bitmap_size
, BITS_PER_COMPAT_LONG
);
291 nr_compat_longs
= BITS_TO_COMPAT_LONGS(bitmap_size
);
293 if (!user_access_begin(umask
, bitmap_size
/ 8))
296 while (nr_compat_longs
> 1) {
297 compat_ulong_t l1
, l2
;
298 unsafe_get_user(l1
, umask
++, Efault
);
299 unsafe_get_user(l2
, umask
++, Efault
);
300 *mask
++ = ((unsigned long)l2
<< BITS_PER_COMPAT_LONG
) | l1
;
301 nr_compat_longs
-= 2;
304 unsafe_get_user(*mask
, umask
++, Efault
);
313 long compat_put_bitmap(compat_ulong_t __user
*umask
, unsigned long *mask
,
314 unsigned long bitmap_size
)
316 unsigned long nr_compat_longs
;
318 /* align bitmap up to nearest compat_long_t boundary */
319 bitmap_size
= ALIGN(bitmap_size
, BITS_PER_COMPAT_LONG
);
320 nr_compat_longs
= BITS_TO_COMPAT_LONGS(bitmap_size
);
322 if (!user_access_begin(umask
, bitmap_size
/ 8))
325 while (nr_compat_longs
> 1) {
326 unsigned long m
= *mask
++;
327 unsafe_put_user((compat_ulong_t
)m
, umask
++, Efault
);
328 unsafe_put_user(m
>> BITS_PER_COMPAT_LONG
, umask
++, Efault
);
329 nr_compat_longs
-= 2;
332 unsafe_put_user((compat_ulong_t
)*mask
, umask
++, Efault
);
341 get_compat_sigset(sigset_t
*set
, const compat_sigset_t __user
*compat
)
345 if (copy_from_user(&v
, compat
, sizeof(compat_sigset_t
)))
347 switch (_NSIG_WORDS
) {
348 case 4: set
->sig
[3] = v
.sig
[6] | (((long)v
.sig
[7]) << 32 );
349 case 3: set
->sig
[2] = v
.sig
[4] | (((long)v
.sig
[5]) << 32 );
350 case 2: set
->sig
[1] = v
.sig
[2] | (((long)v
.sig
[3]) << 32 );
351 case 1: set
->sig
[0] = v
.sig
[0] | (((long)v
.sig
[1]) << 32 );
354 if (copy_from_user(set
, compat
, sizeof(compat_sigset_t
)))
359 EXPORT_SYMBOL_GPL(get_compat_sigset
);
362 * Allocate user-space memory for the duration of a single system call,
363 * in order to marshall parameters inside a compat thunk.
365 void __user
*compat_alloc_user_space(unsigned long len
)
369 /* If len would occupy more than half of the entire compat space... */
370 if (unlikely(len
> (((compat_uptr_t
)~0) >> 1)))
373 ptr
= arch_compat_alloc_user_space(len
);
375 if (unlikely(!access_ok(ptr
, len
)))
380 EXPORT_SYMBOL_GPL(compat_alloc_user_space
);