Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/syscalls.h> |
| 21 | #include <linux/unistd.h> |
| 22 | #include <linux/security.h> |
Stephen Rothwell | 3158e94 | 2006-03-26 01:37:29 -0800 | [diff] [blame] | 23 | #include <linux/timex.h> |
Christoph Lameter | 1b2db9f | 2006-06-23 02:03:56 -0700 | [diff] [blame] | 24 | #include <linux/migrate.h> |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 25 | #include <linux/posix-timers.h> |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 26 | #include <linux/times.h> |
Paul Mackerras | e3d5a27 | 2009-01-06 14:41:02 -0800 | [diff] [blame^] | 27 | #include <linux/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Christoph Hellwig | b418da1 | 2008-10-15 22:02:06 -0700 | [diff] [blame] | 31 | /* |
| 32 | * Note that the native side is already converted to a timespec, because |
| 33 | * that's what we want anyway. |
| 34 | */ |
| 35 | static int compat_get_timeval(struct timespec *o, |
| 36 | struct compat_timeval __user *i) |
| 37 | { |
| 38 | long usec; |
| 39 | |
| 40 | if (get_user(o->tv_sec, &i->tv_sec) || |
| 41 | get_user(usec, &i->tv_usec)) |
| 42 | return -EFAULT; |
| 43 | o->tv_nsec = usec * 1000; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int compat_put_timeval(struct compat_timeval __user *o, |
| 48 | struct timeval *i) |
| 49 | { |
| 50 | return (put_user(i->tv_sec, &o->tv_sec) || |
| 51 | put_user(i->tv_usec, &o->tv_usec)) ? -EFAULT : 0; |
| 52 | } |
| 53 | |
| 54 | asmlinkage long compat_sys_gettimeofday(struct compat_timeval __user *tv, |
| 55 | struct timezone __user *tz) |
| 56 | { |
| 57 | if (tv) { |
| 58 | struct timeval ktv; |
| 59 | do_gettimeofday(&ktv); |
| 60 | if (compat_put_timeval(tv, &ktv)) |
| 61 | return -EFAULT; |
| 62 | } |
| 63 | if (tz) { |
| 64 | if (copy_to_user(tz, &sys_tz, sizeof(sys_tz))) |
| 65 | return -EFAULT; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | asmlinkage long compat_sys_settimeofday(struct compat_timeval __user *tv, |
| 72 | struct timezone __user *tz) |
| 73 | { |
| 74 | struct timespec kts; |
| 75 | struct timezone ktz; |
| 76 | |
| 77 | if (tv) { |
| 78 | if (compat_get_timeval(&kts, tv)) |
| 79 | return -EFAULT; |
| 80 | } |
| 81 | if (tz) { |
| 82 | if (copy_from_user(&ktz, tz, sizeof(ktz))) |
| 83 | return -EFAULT; |
| 84 | } |
| 85 | |
| 86 | return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL); |
| 87 | } |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts) |
| 90 | { |
| 91 | return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) || |
| 92 | __get_user(ts->tv_sec, &cts->tv_sec) || |
| 93 | __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0; |
| 94 | } |
| 95 | |
| 96 | int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts) |
| 97 | { |
| 98 | return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) || |
| 99 | __put_user(ts->tv_sec, &cts->tv_sec) || |
| 100 | __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0; |
| 101 | } |
| 102 | |
Oleg Nesterov | 4165293 | 2008-02-01 20:35:31 +0300 | [diff] [blame] | 103 | static long compat_nanosleep_restart(struct restart_block *restart) |
| 104 | { |
| 105 | struct compat_timespec __user *rmtp; |
| 106 | struct timespec rmt; |
| 107 | mm_segment_t oldfs; |
| 108 | long ret; |
| 109 | |
Thomas Gleixner | 029a07e | 2008-02-10 09:17:43 +0100 | [diff] [blame] | 110 | restart->nanosleep.rmtp = (struct timespec __user *) &rmt; |
Oleg Nesterov | 4165293 | 2008-02-01 20:35:31 +0300 | [diff] [blame] | 111 | oldfs = get_fs(); |
| 112 | set_fs(KERNEL_DS); |
| 113 | ret = hrtimer_nanosleep_restart(restart); |
| 114 | set_fs(oldfs); |
| 115 | |
| 116 | if (ret) { |
Thomas Gleixner | 029a07e | 2008-02-10 09:17:43 +0100 | [diff] [blame] | 117 | rmtp = restart->nanosleep.compat_rmtp; |
Oleg Nesterov | 4165293 | 2008-02-01 20:35:31 +0300 | [diff] [blame] | 118 | |
| 119 | if (rmtp && put_compat_timespec(&rmt, rmtp)) |
| 120 | return -EFAULT; |
| 121 | } |
| 122 | |
| 123 | return ret; |
| 124 | } |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp, |
Anton Blanchard | c70878b | 2007-10-15 16:13:56 -0500 | [diff] [blame] | 127 | struct compat_timespec __user *rmtp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Anton Blanchard | c70878b | 2007-10-15 16:13:56 -0500 | [diff] [blame] | 129 | struct timespec tu, rmt; |
Oleg Nesterov | 4165293 | 2008-02-01 20:35:31 +0300 | [diff] [blame] | 130 | mm_segment_t oldfs; |
Anton Blanchard | c70878b | 2007-10-15 16:13:56 -0500 | [diff] [blame] | 131 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Anton Blanchard | c70878b | 2007-10-15 16:13:56 -0500 | [diff] [blame] | 133 | if (get_compat_timespec(&tu, rqtp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return -EFAULT; |
| 135 | |
Anton Blanchard | c70878b | 2007-10-15 16:13:56 -0500 | [diff] [blame] | 136 | if (!timespec_valid(&tu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return -EINVAL; |
| 138 | |
Oleg Nesterov | 4165293 | 2008-02-01 20:35:31 +0300 | [diff] [blame] | 139 | oldfs = get_fs(); |
| 140 | set_fs(KERNEL_DS); |
| 141 | ret = hrtimer_nanosleep(&tu, |
| 142 | rmtp ? (struct timespec __user *)&rmt : NULL, |
| 143 | HRTIMER_MODE_REL, CLOCK_MONOTONIC); |
| 144 | set_fs(oldfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Oleg Nesterov | 4165293 | 2008-02-01 20:35:31 +0300 | [diff] [blame] | 146 | if (ret) { |
| 147 | struct restart_block *restart |
| 148 | = ¤t_thread_info()->restart_block; |
| 149 | |
| 150 | restart->fn = compat_nanosleep_restart; |
Thomas Gleixner | 029a07e | 2008-02-10 09:17:43 +0100 | [diff] [blame] | 151 | restart->nanosleep.compat_rmtp = rmtp; |
Oleg Nesterov | 4165293 | 2008-02-01 20:35:31 +0300 | [diff] [blame] | 152 | |
| 153 | if (rmtp && put_compat_timespec(&rmt, rmtp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | return -EFAULT; |
| 155 | } |
Anton Blanchard | c70878b | 2007-10-15 16:13:56 -0500 | [diff] [blame] | 156 | |
| 157 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static inline long get_compat_itimerval(struct itimerval *o, |
| 161 | struct compat_itimerval __user *i) |
| 162 | { |
| 163 | return (!access_ok(VERIFY_READ, i, sizeof(*i)) || |
| 164 | (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) | |
| 165 | __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) | |
| 166 | __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) | |
| 167 | __get_user(o->it_value.tv_usec, &i->it_value.tv_usec))); |
| 168 | } |
| 169 | |
| 170 | static inline long put_compat_itimerval(struct compat_itimerval __user *o, |
| 171 | struct itimerval *i) |
| 172 | { |
| 173 | return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) || |
| 174 | (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) | |
| 175 | __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) | |
| 176 | __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) | |
| 177 | __put_user(i->it_value.tv_usec, &o->it_value.tv_usec))); |
| 178 | } |
| 179 | |
| 180 | asmlinkage long compat_sys_getitimer(int which, |
| 181 | struct compat_itimerval __user *it) |
| 182 | { |
| 183 | struct itimerval kit; |
| 184 | int error; |
| 185 | |
| 186 | error = do_getitimer(which, &kit); |
| 187 | if (!error && put_compat_itimerval(it, &kit)) |
| 188 | error = -EFAULT; |
| 189 | return error; |
| 190 | } |
| 191 | |
| 192 | asmlinkage long compat_sys_setitimer(int which, |
| 193 | struct compat_itimerval __user *in, |
| 194 | struct compat_itimerval __user *out) |
| 195 | { |
| 196 | struct itimerval kin, kout; |
| 197 | int error; |
| 198 | |
| 199 | if (in) { |
| 200 | if (get_compat_itimerval(&kin, in)) |
| 201 | return -EFAULT; |
| 202 | } else |
| 203 | memset(&kin, 0, sizeof(kin)); |
| 204 | |
| 205 | error = do_setitimer(which, &kin, out ? &kout : NULL); |
| 206 | if (error || !out) |
| 207 | return error; |
| 208 | if (put_compat_itimerval(out, &kout)) |
| 209 | return -EFAULT; |
| 210 | return 0; |
| 211 | } |
| 212 | |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 213 | static compat_clock_t clock_t_to_compat_clock_t(clock_t x) |
| 214 | { |
| 215 | return compat_jiffies_to_clock_t(clock_t_to_jiffies(x)); |
| 216 | } |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | asmlinkage long compat_sys_times(struct compat_tms __user *tbuf) |
| 219 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | if (tbuf) { |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 221 | struct tms tms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | struct compat_tms tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 224 | do_sys_times(&tms); |
| 225 | /* Convert our struct tms to the compat version. */ |
| 226 | tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime); |
| 227 | tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime); |
| 228 | tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime); |
| 229 | tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | if (copy_to_user(tbuf, &tmp, sizeof(tmp))) |
| 231 | return -EFAULT; |
| 232 | } |
Paul Mackerras | e3d5a27 | 2009-01-06 14:41:02 -0800 | [diff] [blame^] | 233 | force_successful_syscall_return(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | return compat_jiffies_to_clock_t(jiffies); |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Assumption: old_sigset_t and compat_old_sigset_t are both |
| 239 | * types that can be passed to put_user()/get_user(). |
| 240 | */ |
| 241 | |
| 242 | asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set) |
| 243 | { |
| 244 | old_sigset_t s; |
| 245 | long ret; |
| 246 | mm_segment_t old_fs = get_fs(); |
| 247 | |
| 248 | set_fs(KERNEL_DS); |
| 249 | ret = sys_sigpending((old_sigset_t __user *) &s); |
| 250 | set_fs(old_fs); |
| 251 | if (ret == 0) |
| 252 | ret = put_user(s, set); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set, |
| 257 | compat_old_sigset_t __user *oset) |
| 258 | { |
| 259 | old_sigset_t s; |
| 260 | long ret; |
| 261 | mm_segment_t old_fs; |
| 262 | |
| 263 | if (set && get_user(s, set)) |
| 264 | return -EFAULT; |
| 265 | old_fs = get_fs(); |
| 266 | set_fs(KERNEL_DS); |
| 267 | ret = sys_sigprocmask(how, |
| 268 | set ? (old_sigset_t __user *) &s : NULL, |
| 269 | oset ? (old_sigset_t __user *) &s : NULL); |
| 270 | set_fs(old_fs); |
| 271 | if (ret == 0) |
| 272 | if (oset) |
| 273 | ret = put_user(s, oset); |
| 274 | return ret; |
| 275 | } |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | asmlinkage long compat_sys_setrlimit(unsigned int resource, |
| 278 | struct compat_rlimit __user *rlim) |
| 279 | { |
| 280 | struct rlimit r; |
| 281 | int ret; |
| 282 | mm_segment_t old_fs = get_fs (); |
| 283 | |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 284 | if (resource >= RLIM_NLIMITS) |
| 285 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) || |
| 288 | __get_user(r.rlim_cur, &rlim->rlim_cur) || |
| 289 | __get_user(r.rlim_max, &rlim->rlim_max)) |
| 290 | return -EFAULT; |
| 291 | |
| 292 | if (r.rlim_cur == COMPAT_RLIM_INFINITY) |
| 293 | r.rlim_cur = RLIM_INFINITY; |
| 294 | if (r.rlim_max == COMPAT_RLIM_INFINITY) |
| 295 | r.rlim_max = RLIM_INFINITY; |
| 296 | set_fs(KERNEL_DS); |
| 297 | ret = sys_setrlimit(resource, (struct rlimit __user *) &r); |
| 298 | set_fs(old_fs); |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | #ifdef COMPAT_RLIM_OLD_INFINITY |
| 303 | |
| 304 | asmlinkage long compat_sys_old_getrlimit(unsigned int resource, |
| 305 | struct compat_rlimit __user *rlim) |
| 306 | { |
| 307 | struct rlimit r; |
| 308 | int ret; |
| 309 | mm_segment_t old_fs = get_fs(); |
| 310 | |
| 311 | set_fs(KERNEL_DS); |
| 312 | ret = sys_old_getrlimit(resource, &r); |
| 313 | set_fs(old_fs); |
| 314 | |
| 315 | if (!ret) { |
| 316 | if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY) |
| 317 | r.rlim_cur = COMPAT_RLIM_INFINITY; |
| 318 | if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY) |
| 319 | r.rlim_max = COMPAT_RLIM_INFINITY; |
| 320 | |
| 321 | if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) || |
| 322 | __put_user(r.rlim_cur, &rlim->rlim_cur) || |
| 323 | __put_user(r.rlim_max, &rlim->rlim_max)) |
| 324 | return -EFAULT; |
| 325 | } |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | #endif |
| 330 | |
| 331 | asmlinkage long compat_sys_getrlimit (unsigned int resource, |
| 332 | struct compat_rlimit __user *rlim) |
| 333 | { |
| 334 | struct rlimit r; |
| 335 | int ret; |
| 336 | mm_segment_t old_fs = get_fs(); |
| 337 | |
| 338 | set_fs(KERNEL_DS); |
| 339 | ret = sys_getrlimit(resource, (struct rlimit __user *) &r); |
| 340 | set_fs(old_fs); |
| 341 | if (!ret) { |
| 342 | if (r.rlim_cur > COMPAT_RLIM_INFINITY) |
| 343 | r.rlim_cur = COMPAT_RLIM_INFINITY; |
| 344 | if (r.rlim_max > COMPAT_RLIM_INFINITY) |
| 345 | r.rlim_max = COMPAT_RLIM_INFINITY; |
| 346 | |
| 347 | if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) || |
| 348 | __put_user(r.rlim_cur, &rlim->rlim_cur) || |
| 349 | __put_user(r.rlim_max, &rlim->rlim_max)) |
| 350 | return -EFAULT; |
| 351 | } |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru) |
| 356 | { |
| 357 | if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) || |
| 358 | __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) || |
| 359 | __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) || |
| 360 | __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) || |
| 361 | __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) || |
| 362 | __put_user(r->ru_maxrss, &ru->ru_maxrss) || |
| 363 | __put_user(r->ru_ixrss, &ru->ru_ixrss) || |
| 364 | __put_user(r->ru_idrss, &ru->ru_idrss) || |
| 365 | __put_user(r->ru_isrss, &ru->ru_isrss) || |
| 366 | __put_user(r->ru_minflt, &ru->ru_minflt) || |
| 367 | __put_user(r->ru_majflt, &ru->ru_majflt) || |
| 368 | __put_user(r->ru_nswap, &ru->ru_nswap) || |
| 369 | __put_user(r->ru_inblock, &ru->ru_inblock) || |
| 370 | __put_user(r->ru_oublock, &ru->ru_oublock) || |
| 371 | __put_user(r->ru_msgsnd, &ru->ru_msgsnd) || |
| 372 | __put_user(r->ru_msgrcv, &ru->ru_msgrcv) || |
| 373 | __put_user(r->ru_nsignals, &ru->ru_nsignals) || |
| 374 | __put_user(r->ru_nvcsw, &ru->ru_nvcsw) || |
| 375 | __put_user(r->ru_nivcsw, &ru->ru_nivcsw)) |
| 376 | return -EFAULT; |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru) |
| 381 | { |
| 382 | struct rusage r; |
| 383 | int ret; |
| 384 | mm_segment_t old_fs = get_fs(); |
| 385 | |
| 386 | set_fs(KERNEL_DS); |
| 387 | ret = sys_getrusage(who, (struct rusage __user *) &r); |
| 388 | set_fs(old_fs); |
| 389 | |
| 390 | if (ret) |
| 391 | return ret; |
| 392 | |
| 393 | if (put_compat_rusage(&r, ru)) |
| 394 | return -EFAULT; |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | asmlinkage long |
| 400 | compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options, |
| 401 | struct compat_rusage __user *ru) |
| 402 | { |
| 403 | if (!ru) { |
| 404 | return sys_wait4(pid, stat_addr, options, NULL); |
| 405 | } else { |
| 406 | struct rusage r; |
| 407 | int ret; |
| 408 | unsigned int status; |
| 409 | mm_segment_t old_fs = get_fs(); |
| 410 | |
| 411 | set_fs (KERNEL_DS); |
| 412 | ret = sys_wait4(pid, |
| 413 | (stat_addr ? |
| 414 | (unsigned int __user *) &status : NULL), |
| 415 | options, (struct rusage __user *) &r); |
| 416 | set_fs (old_fs); |
| 417 | |
| 418 | if (ret > 0) { |
| 419 | if (put_compat_rusage(&r, ru)) |
| 420 | return -EFAULT; |
| 421 | if (stat_addr && put_user(status, stat_addr)) |
| 422 | return -EFAULT; |
| 423 | } |
| 424 | return ret; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | asmlinkage long compat_sys_waitid(int which, compat_pid_t pid, |
| 429 | struct compat_siginfo __user *uinfo, int options, |
| 430 | struct compat_rusage __user *uru) |
| 431 | { |
| 432 | siginfo_t info; |
| 433 | struct rusage ru; |
| 434 | long ret; |
| 435 | mm_segment_t old_fs = get_fs(); |
| 436 | |
| 437 | memset(&info, 0, sizeof(info)); |
| 438 | |
| 439 | set_fs(KERNEL_DS); |
| 440 | ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options, |
| 441 | uru ? (struct rusage __user *)&ru : NULL); |
| 442 | set_fs(old_fs); |
| 443 | |
| 444 | if ((ret < 0) || (info.si_signo == 0)) |
| 445 | return ret; |
| 446 | |
| 447 | if (uru) { |
| 448 | ret = put_compat_rusage(&ru, uru); |
| 449 | if (ret) |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | BUG_ON(info.si_code & __SI_MASK); |
| 454 | info.si_code |= __SI_CHLD; |
| 455 | return copy_siginfo_to_user32(uinfo, &info); |
| 456 | } |
| 457 | |
| 458 | static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr, |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 459 | unsigned len, struct cpumask *new_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
| 461 | unsigned long *k; |
| 462 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 463 | if (len < cpumask_size()) |
| 464 | memset(new_mask, 0, cpumask_size()); |
| 465 | else if (len > cpumask_size()) |
| 466 | len = cpumask_size(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 468 | k = cpumask_bits(new_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | return compat_get_bitmap(k, user_mask_ptr, len * 8); |
| 470 | } |
| 471 | |
| 472 | asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid, |
| 473 | unsigned int len, |
| 474 | compat_ulong_t __user *user_mask_ptr) |
| 475 | { |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 476 | cpumask_var_t new_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | int retval; |
| 478 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 479 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
| 480 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 482 | retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask); |
| 483 | if (retval) |
| 484 | goto out; |
| 485 | |
| 486 | retval = sched_setaffinity(pid, new_mask); |
| 487 | out: |
| 488 | free_cpumask_var(new_mask); |
| 489 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len, |
| 493 | compat_ulong_t __user *user_mask_ptr) |
| 494 | { |
| 495 | int ret; |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 496 | cpumask_var_t mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | unsigned long *k; |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 498 | unsigned int min_length = cpumask_size(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 500 | if (nr_cpu_ids <= BITS_PER_COMPAT_LONG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | min_length = sizeof(compat_ulong_t); |
| 502 | |
| 503 | if (len < min_length) |
| 504 | return -EINVAL; |
| 505 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 506 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
| 507 | return -ENOMEM; |
| 508 | |
| 509 | ret = sched_getaffinity(pid, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | if (ret < 0) |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 511 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 513 | k = cpumask_bits(mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | ret = compat_put_bitmap(user_mask_ptr, k, min_length * 8); |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 515 | if (ret == 0) |
| 516 | ret = min_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 518 | out: |
| 519 | free_cpumask_var(mask); |
| 520 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Davide Libenzi | 83f5d12 | 2007-05-10 22:23:18 -0700 | [diff] [blame] | 523 | int get_compat_itimerspec(struct itimerspec *dst, |
| 524 | const struct compat_itimerspec __user *src) |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 525 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | if (get_compat_timespec(&dst->it_interval, &src->it_interval) || |
| 527 | get_compat_timespec(&dst->it_value, &src->it_value)) |
| 528 | return -EFAULT; |
| 529 | return 0; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Davide Libenzi | 83f5d12 | 2007-05-10 22:23:18 -0700 | [diff] [blame] | 532 | int put_compat_itimerspec(struct compat_itimerspec __user *dst, |
| 533 | const struct itimerspec *src) |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 534 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | if (put_compat_timespec(&src->it_interval, &dst->it_interval) || |
| 536 | put_compat_timespec(&src->it_value, &dst->it_value)) |
| 537 | return -EFAULT; |
| 538 | return 0; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 539 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Christoph Hellwig | 3a0f69d | 2006-01-09 20:52:08 -0800 | [diff] [blame] | 541 | long compat_sys_timer_create(clockid_t which_clock, |
| 542 | struct compat_sigevent __user *timer_event_spec, |
| 543 | timer_t __user *created_timer_id) |
| 544 | { |
| 545 | struct sigevent __user *event = NULL; |
| 546 | |
| 547 | if (timer_event_spec) { |
| 548 | struct sigevent kevent; |
| 549 | |
| 550 | event = compat_alloc_user_space(sizeof(*event)); |
| 551 | if (get_compat_sigevent(&kevent, timer_event_spec) || |
| 552 | copy_to_user(event, &kevent, sizeof(*event))) |
| 553 | return -EFAULT; |
| 554 | } |
| 555 | |
| 556 | return sys_timer_create(which_clock, event, created_timer_id); |
| 557 | } |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | long compat_sys_timer_settime(timer_t timer_id, int flags, |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 560 | struct compat_itimerspec __user *new, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | struct compat_itimerspec __user *old) |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 562 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | long err; |
| 564 | mm_segment_t oldfs; |
| 565 | struct itimerspec newts, oldts; |
| 566 | |
| 567 | if (!new) |
| 568 | return -EINVAL; |
| 569 | if (get_compat_itimerspec(&newts, new)) |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 570 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | oldfs = get_fs(); |
| 572 | set_fs(KERNEL_DS); |
| 573 | err = sys_timer_settime(timer_id, flags, |
| 574 | (struct itimerspec __user *) &newts, |
| 575 | (struct itimerspec __user *) &oldts); |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 576 | set_fs(oldfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | if (!err && old && put_compat_itimerspec(old, &oldts)) |
| 578 | return -EFAULT; |
| 579 | return err; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 580 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | long compat_sys_timer_gettime(timer_t timer_id, |
| 583 | struct compat_itimerspec __user *setting) |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 584 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | long err; |
| 586 | mm_segment_t oldfs; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 587 | struct itimerspec ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | oldfs = get_fs(); |
| 590 | set_fs(KERNEL_DS); |
| 591 | err = sys_timer_gettime(timer_id, |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 592 | (struct itimerspec __user *) &ts); |
| 593 | set_fs(oldfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | if (!err && put_compat_itimerspec(setting, &ts)) |
| 595 | return -EFAULT; |
| 596 | return err; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
| 599 | long compat_sys_clock_settime(clockid_t which_clock, |
| 600 | struct compat_timespec __user *tp) |
| 601 | { |
| 602 | long err; |
| 603 | mm_segment_t oldfs; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 604 | struct timespec ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
| 606 | if (get_compat_timespec(&ts, tp)) |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 607 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | oldfs = get_fs(); |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 609 | set_fs(KERNEL_DS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | err = sys_clock_settime(which_clock, |
| 611 | (struct timespec __user *) &ts); |
| 612 | set_fs(oldfs); |
| 613 | return err; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 614 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
| 616 | long compat_sys_clock_gettime(clockid_t which_clock, |
| 617 | struct compat_timespec __user *tp) |
| 618 | { |
| 619 | long err; |
| 620 | mm_segment_t oldfs; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 621 | struct timespec ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
| 623 | oldfs = get_fs(); |
| 624 | set_fs(KERNEL_DS); |
| 625 | err = sys_clock_gettime(which_clock, |
| 626 | (struct timespec __user *) &ts); |
| 627 | set_fs(oldfs); |
| 628 | if (!err && put_compat_timespec(&ts, tp)) |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 629 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | return err; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
| 633 | long compat_sys_clock_getres(clockid_t which_clock, |
| 634 | struct compat_timespec __user *tp) |
| 635 | { |
| 636 | long err; |
| 637 | mm_segment_t oldfs; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 638 | struct timespec ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
| 640 | oldfs = get_fs(); |
| 641 | set_fs(KERNEL_DS); |
| 642 | err = sys_clock_getres(which_clock, |
| 643 | (struct timespec __user *) &ts); |
| 644 | set_fs(oldfs); |
| 645 | if (!err && tp && put_compat_timespec(&ts, tp)) |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 646 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | return err; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 648 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 650 | static long compat_clock_nanosleep_restart(struct restart_block *restart) |
| 651 | { |
| 652 | long err; |
| 653 | mm_segment_t oldfs; |
| 654 | struct timespec tu; |
Thomas Gleixner | 029a07e | 2008-02-10 09:17:43 +0100 | [diff] [blame] | 655 | struct compat_timespec *rmtp = restart->nanosleep.compat_rmtp; |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 656 | |
Thomas Gleixner | 029a07e | 2008-02-10 09:17:43 +0100 | [diff] [blame] | 657 | restart->nanosleep.rmtp = (struct timespec __user *) &tu; |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 658 | oldfs = get_fs(); |
| 659 | set_fs(KERNEL_DS); |
| 660 | err = clock_nanosleep_restart(restart); |
| 661 | set_fs(oldfs); |
| 662 | |
| 663 | if ((err == -ERESTART_RESTARTBLOCK) && rmtp && |
| 664 | put_compat_timespec(&tu, rmtp)) |
| 665 | return -EFAULT; |
| 666 | |
| 667 | if (err == -ERESTART_RESTARTBLOCK) { |
| 668 | restart->fn = compat_clock_nanosleep_restart; |
Thomas Gleixner | 029a07e | 2008-02-10 09:17:43 +0100 | [diff] [blame] | 669 | restart->nanosleep.compat_rmtp = rmtp; |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 670 | } |
| 671 | return err; |
| 672 | } |
| 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | long compat_sys_clock_nanosleep(clockid_t which_clock, int flags, |
| 675 | struct compat_timespec __user *rqtp, |
| 676 | struct compat_timespec __user *rmtp) |
| 677 | { |
| 678 | long err; |
| 679 | mm_segment_t oldfs; |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 680 | struct timespec in, out; |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 681 | struct restart_block *restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 683 | if (get_compat_timespec(&in, rqtp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | return -EFAULT; |
| 685 | |
| 686 | oldfs = get_fs(); |
| 687 | set_fs(KERNEL_DS); |
| 688 | err = sys_clock_nanosleep(which_clock, flags, |
| 689 | (struct timespec __user *) &in, |
| 690 | (struct timespec __user *) &out); |
| 691 | set_fs(oldfs); |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | if ((err == -ERESTART_RESTARTBLOCK) && rmtp && |
| 694 | put_compat_timespec(&out, rmtp)) |
| 695 | return -EFAULT; |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 696 | |
| 697 | if (err == -ERESTART_RESTARTBLOCK) { |
| 698 | restart = ¤t_thread_info()->restart_block; |
| 699 | restart->fn = compat_clock_nanosleep_restart; |
Thomas Gleixner | 029a07e | 2008-02-10 09:17:43 +0100 | [diff] [blame] | 700 | restart->nanosleep.compat_rmtp = rmtp; |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 701 | } |
Daniel Walker | bd3a849 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 702 | return err; |
| 703 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
| 705 | /* |
| 706 | * We currently only need the following fields from the sigevent |
| 707 | * structure: sigev_value, sigev_signo, sig_notify and (sometimes |
| 708 | * sigev_notify_thread_id). The others are handled in user mode. |
| 709 | * We also assume that copying sigev_value.sival_int is sufficient |
| 710 | * to keep all the bits of sigev_value.sival_ptr intact. |
| 711 | */ |
| 712 | int get_compat_sigevent(struct sigevent *event, |
| 713 | const struct compat_sigevent __user *u_event) |
| 714 | { |
David S. Miller | 51410d3 | 2005-04-16 15:24:01 -0700 | [diff] [blame] | 715 | memset(event, 0, sizeof(*event)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) || |
| 717 | __get_user(event->sigev_value.sival_int, |
| 718 | &u_event->sigev_value.sival_int) || |
| 719 | __get_user(event->sigev_signo, &u_event->sigev_signo) || |
| 720 | __get_user(event->sigev_notify, &u_event->sigev_notify) || |
| 721 | __get_user(event->sigev_notify_thread_id, |
| 722 | &u_event->sigev_notify_thread_id)) |
| 723 | ? -EFAULT : 0; |
| 724 | } |
| 725 | |
Stephen Rothwell | 5fa3839 | 2006-10-28 10:38:46 -0700 | [diff] [blame] | 726 | long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | unsigned long bitmap_size) |
| 728 | { |
| 729 | int i, j; |
| 730 | unsigned long m; |
| 731 | compat_ulong_t um; |
| 732 | unsigned long nr_compat_longs; |
| 733 | |
| 734 | /* align bitmap up to nearest compat_long_t boundary */ |
| 735 | bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG); |
| 736 | |
| 737 | if (!access_ok(VERIFY_READ, umask, bitmap_size / 8)) |
| 738 | return -EFAULT; |
| 739 | |
| 740 | nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size); |
| 741 | |
| 742 | for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) { |
| 743 | m = 0; |
| 744 | |
| 745 | for (j = 0; j < sizeof(m)/sizeof(um); j++) { |
| 746 | /* |
| 747 | * We dont want to read past the end of the userspace |
| 748 | * bitmap. We must however ensure the end of the |
| 749 | * kernel bitmap is zeroed. |
| 750 | */ |
| 751 | if (nr_compat_longs-- > 0) { |
| 752 | if (__get_user(um, umask)) |
| 753 | return -EFAULT; |
| 754 | } else { |
| 755 | um = 0; |
| 756 | } |
| 757 | |
| 758 | umask++; |
| 759 | m |= (long)um << (j * BITS_PER_COMPAT_LONG); |
| 760 | } |
| 761 | *mask++ = m; |
| 762 | } |
| 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask, |
| 768 | unsigned long bitmap_size) |
| 769 | { |
| 770 | int i, j; |
| 771 | unsigned long m; |
| 772 | compat_ulong_t um; |
| 773 | unsigned long nr_compat_longs; |
| 774 | |
| 775 | /* align bitmap up to nearest compat_long_t boundary */ |
| 776 | bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG); |
| 777 | |
| 778 | if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8)) |
| 779 | return -EFAULT; |
| 780 | |
| 781 | nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size); |
| 782 | |
| 783 | for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) { |
| 784 | m = *mask++; |
| 785 | |
| 786 | for (j = 0; j < sizeof(m)/sizeof(um); j++) { |
| 787 | um = m; |
| 788 | |
| 789 | /* |
| 790 | * We dont want to write past the end of the userspace |
| 791 | * bitmap. |
| 792 | */ |
| 793 | if (nr_compat_longs-- > 0) { |
| 794 | if (__put_user(um, umask)) |
| 795 | return -EFAULT; |
| 796 | } |
| 797 | |
| 798 | umask++; |
| 799 | m >>= 4*sizeof(um); |
| 800 | m >>= 4*sizeof(um); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | void |
| 808 | sigset_from_compat (sigset_t *set, compat_sigset_t *compat) |
| 809 | { |
| 810 | switch (_NSIG_WORDS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 ); |
| 812 | case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 ); |
| 813 | case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 ); |
| 814 | case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | asmlinkage long |
| 819 | compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese, |
| 820 | struct compat_siginfo __user *uinfo, |
| 821 | struct compat_timespec __user *uts, compat_size_t sigsetsize) |
| 822 | { |
| 823 | compat_sigset_t s32; |
| 824 | sigset_t s; |
| 825 | int sig; |
| 826 | struct timespec t; |
| 827 | siginfo_t info; |
| 828 | long ret, timeout = 0; |
| 829 | |
| 830 | if (sigsetsize != sizeof(sigset_t)) |
| 831 | return -EINVAL; |
| 832 | |
| 833 | if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t))) |
| 834 | return -EFAULT; |
| 835 | sigset_from_compat(&s, &s32); |
| 836 | sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 837 | signotset(&s); |
| 838 | |
| 839 | if (uts) { |
| 840 | if (get_compat_timespec (&t, uts)) |
| 841 | return -EFAULT; |
| 842 | if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0 |
| 843 | || t.tv_sec < 0) |
| 844 | return -EINVAL; |
| 845 | } |
| 846 | |
| 847 | spin_lock_irq(¤t->sighand->siglock); |
| 848 | sig = dequeue_signal(current, &s, &info); |
| 849 | if (!sig) { |
| 850 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 851 | if (uts) |
| 852 | timeout = timespec_to_jiffies(&t) |
| 853 | +(t.tv_sec || t.tv_nsec); |
| 854 | if (timeout) { |
| 855 | current->real_blocked = current->blocked; |
| 856 | sigandsets(¤t->blocked, ¤t->blocked, &s); |
| 857 | |
| 858 | recalc_sigpending(); |
| 859 | spin_unlock_irq(¤t->sighand->siglock); |
| 860 | |
Nishanth Aravamudan | 75bcc8c | 2005-09-10 00:27:24 -0700 | [diff] [blame] | 861 | timeout = schedule_timeout_interruptible(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
| 863 | spin_lock_irq(¤t->sighand->siglock); |
| 864 | sig = dequeue_signal(current, &s, &info); |
| 865 | current->blocked = current->real_blocked; |
| 866 | siginitset(¤t->real_blocked, 0); |
| 867 | recalc_sigpending(); |
| 868 | } |
| 869 | } |
| 870 | spin_unlock_irq(¤t->sighand->siglock); |
| 871 | |
| 872 | if (sig) { |
| 873 | ret = sig; |
| 874 | if (uinfo) { |
| 875 | if (copy_siginfo_to_user32(uinfo, &info)) |
| 876 | ret = -EFAULT; |
| 877 | } |
| 878 | }else { |
| 879 | ret = timeout?-EINTR:-EAGAIN; |
| 880 | } |
| 881 | return ret; |
| 882 | |
| 883 | } |
| 884 | |
| 885 | #ifdef __ARCH_WANT_COMPAT_SYS_TIME |
| 886 | |
| 887 | /* compat_time_t is a 32 bit "long" and needs to get converted. */ |
| 888 | |
| 889 | asmlinkage long compat_sys_time(compat_time_t __user * tloc) |
| 890 | { |
| 891 | compat_time_t i; |
| 892 | struct timeval tv; |
| 893 | |
| 894 | do_gettimeofday(&tv); |
| 895 | i = tv.tv_sec; |
| 896 | |
| 897 | if (tloc) { |
| 898 | if (put_user(i,tloc)) |
Paul Mackerras | e3d5a27 | 2009-01-06 14:41:02 -0800 | [diff] [blame^] | 899 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } |
Paul Mackerras | e3d5a27 | 2009-01-06 14:41:02 -0800 | [diff] [blame^] | 901 | force_successful_syscall_return(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | return i; |
| 903 | } |
| 904 | |
| 905 | asmlinkage long compat_sys_stime(compat_time_t __user *tptr) |
| 906 | { |
| 907 | struct timespec tv; |
| 908 | int err; |
| 909 | |
| 910 | if (get_user(tv.tv_sec, tptr)) |
| 911 | return -EFAULT; |
| 912 | |
| 913 | tv.tv_nsec = 0; |
| 914 | |
| 915 | err = security_settime(&tv, NULL); |
| 916 | if (err) |
| 917 | return err; |
| 918 | |
| 919 | do_settimeofday(&tv); |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | #endif /* __ARCH_WANT_COMPAT_SYS_TIME */ |
David Woodhouse | 150256d | 2006-01-18 17:43:57 -0800 | [diff] [blame] | 924 | |
| 925 | #ifdef __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND |
| 926 | asmlinkage long compat_sys_rt_sigsuspend(compat_sigset_t __user *unewset, compat_size_t sigsetsize) |
| 927 | { |
| 928 | sigset_t newset; |
| 929 | compat_sigset_t newset32; |
| 930 | |
| 931 | /* XXX: Don't preclude handling different sized sigset_t's. */ |
| 932 | if (sigsetsize != sizeof(sigset_t)) |
| 933 | return -EINVAL; |
| 934 | |
| 935 | if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t))) |
| 936 | return -EFAULT; |
| 937 | sigset_from_compat(&newset, &newset32); |
| 938 | sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 939 | |
| 940 | spin_lock_irq(¤t->sighand->siglock); |
| 941 | current->saved_sigmask = current->blocked; |
| 942 | current->blocked = newset; |
| 943 | recalc_sigpending(); |
| 944 | spin_unlock_irq(¤t->sighand->siglock); |
| 945 | |
| 946 | current->state = TASK_INTERRUPTIBLE; |
| 947 | schedule(); |
Roland McGrath | 4e4c22c | 2008-04-30 00:53:06 -0700 | [diff] [blame] | 948 | set_restore_sigmask(); |
David Woodhouse | 150256d | 2006-01-18 17:43:57 -0800 | [diff] [blame] | 949 | return -ERESTARTNOHAND; |
| 950 | } |
| 951 | #endif /* __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND */ |
Stephen Rothwell | 3158e94 | 2006-03-26 01:37:29 -0800 | [diff] [blame] | 952 | |
| 953 | asmlinkage long compat_sys_adjtimex(struct compat_timex __user *utp) |
| 954 | { |
| 955 | struct timex txc; |
| 956 | int ret; |
| 957 | |
| 958 | memset(&txc, 0, sizeof(struct timex)); |
| 959 | |
| 960 | if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) || |
| 961 | __get_user(txc.modes, &utp->modes) || |
| 962 | __get_user(txc.offset, &utp->offset) || |
| 963 | __get_user(txc.freq, &utp->freq) || |
| 964 | __get_user(txc.maxerror, &utp->maxerror) || |
| 965 | __get_user(txc.esterror, &utp->esterror) || |
| 966 | __get_user(txc.status, &utp->status) || |
| 967 | __get_user(txc.constant, &utp->constant) || |
| 968 | __get_user(txc.precision, &utp->precision) || |
| 969 | __get_user(txc.tolerance, &utp->tolerance) || |
| 970 | __get_user(txc.time.tv_sec, &utp->time.tv_sec) || |
| 971 | __get_user(txc.time.tv_usec, &utp->time.tv_usec) || |
| 972 | __get_user(txc.tick, &utp->tick) || |
| 973 | __get_user(txc.ppsfreq, &utp->ppsfreq) || |
| 974 | __get_user(txc.jitter, &utp->jitter) || |
| 975 | __get_user(txc.shift, &utp->shift) || |
| 976 | __get_user(txc.stabil, &utp->stabil) || |
| 977 | __get_user(txc.jitcnt, &utp->jitcnt) || |
| 978 | __get_user(txc.calcnt, &utp->calcnt) || |
| 979 | __get_user(txc.errcnt, &utp->errcnt) || |
| 980 | __get_user(txc.stbcnt, &utp->stbcnt)) |
| 981 | return -EFAULT; |
| 982 | |
| 983 | ret = do_adjtimex(&txc); |
| 984 | |
| 985 | if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) || |
| 986 | __put_user(txc.modes, &utp->modes) || |
| 987 | __put_user(txc.offset, &utp->offset) || |
| 988 | __put_user(txc.freq, &utp->freq) || |
| 989 | __put_user(txc.maxerror, &utp->maxerror) || |
| 990 | __put_user(txc.esterror, &utp->esterror) || |
| 991 | __put_user(txc.status, &utp->status) || |
| 992 | __put_user(txc.constant, &utp->constant) || |
| 993 | __put_user(txc.precision, &utp->precision) || |
| 994 | __put_user(txc.tolerance, &utp->tolerance) || |
| 995 | __put_user(txc.time.tv_sec, &utp->time.tv_sec) || |
| 996 | __put_user(txc.time.tv_usec, &utp->time.tv_usec) || |
| 997 | __put_user(txc.tick, &utp->tick) || |
| 998 | __put_user(txc.ppsfreq, &utp->ppsfreq) || |
| 999 | __put_user(txc.jitter, &utp->jitter) || |
| 1000 | __put_user(txc.shift, &utp->shift) || |
| 1001 | __put_user(txc.stabil, &utp->stabil) || |
| 1002 | __put_user(txc.jitcnt, &utp->jitcnt) || |
| 1003 | __put_user(txc.calcnt, &utp->calcnt) || |
| 1004 | __put_user(txc.errcnt, &utp->errcnt) || |
Roman Zippel | 153b5d0 | 2008-05-01 04:34:37 -0700 | [diff] [blame] | 1005 | __put_user(txc.stbcnt, &utp->stbcnt) || |
| 1006 | __put_user(txc.tai, &utp->tai)) |
Stephen Rothwell | 3158e94 | 2006-03-26 01:37:29 -0800 | [diff] [blame] | 1007 | ret = -EFAULT; |
| 1008 | |
| 1009 | return ret; |
| 1010 | } |
Christoph Lameter | 1b2db9f | 2006-06-23 02:03:56 -0700 | [diff] [blame] | 1011 | |
| 1012 | #ifdef CONFIG_NUMA |
| 1013 | asmlinkage long compat_sys_move_pages(pid_t pid, unsigned long nr_pages, |
Christoph Lameter | 9216dfa | 2006-06-23 02:03:57 -0700 | [diff] [blame] | 1014 | compat_uptr_t __user *pages32, |
Christoph Lameter | 1b2db9f | 2006-06-23 02:03:56 -0700 | [diff] [blame] | 1015 | const int __user *nodes, |
| 1016 | int __user *status, |
| 1017 | int flags) |
| 1018 | { |
| 1019 | const void __user * __user *pages; |
| 1020 | int i; |
| 1021 | |
| 1022 | pages = compat_alloc_user_space(nr_pages * sizeof(void *)); |
| 1023 | for (i = 0; i < nr_pages; i++) { |
| 1024 | compat_uptr_t p; |
| 1025 | |
Christoph Lameter | 9216dfa | 2006-06-23 02:03:57 -0700 | [diff] [blame] | 1026 | if (get_user(p, pages32 + i) || |
Christoph Lameter | 1b2db9f | 2006-06-23 02:03:56 -0700 | [diff] [blame] | 1027 | put_user(compat_ptr(p), pages + i)) |
| 1028 | return -EFAULT; |
| 1029 | } |
| 1030 | return sys_move_pages(pid, nr_pages, pages, nodes, status, flags); |
| 1031 | } |
Stephen Rothwell | 3fd5939 | 2006-11-02 22:07:24 -0800 | [diff] [blame] | 1032 | |
| 1033 | asmlinkage long compat_sys_migrate_pages(compat_pid_t pid, |
| 1034 | compat_ulong_t maxnode, |
| 1035 | const compat_ulong_t __user *old_nodes, |
| 1036 | const compat_ulong_t __user *new_nodes) |
| 1037 | { |
| 1038 | unsigned long __user *old = NULL; |
| 1039 | unsigned long __user *new = NULL; |
| 1040 | nodemask_t tmp_mask; |
| 1041 | unsigned long nr_bits; |
| 1042 | unsigned long size; |
| 1043 | |
| 1044 | nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES); |
| 1045 | size = ALIGN(nr_bits, BITS_PER_LONG) / 8; |
| 1046 | if (old_nodes) { |
| 1047 | if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits)) |
| 1048 | return -EFAULT; |
| 1049 | old = compat_alloc_user_space(new_nodes ? size * 2 : size); |
| 1050 | if (new_nodes) |
| 1051 | new = old + size / sizeof(unsigned long); |
| 1052 | if (copy_to_user(old, nodes_addr(tmp_mask), size)) |
| 1053 | return -EFAULT; |
| 1054 | } |
| 1055 | if (new_nodes) { |
| 1056 | if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits)) |
| 1057 | return -EFAULT; |
| 1058 | if (new == NULL) |
| 1059 | new = compat_alloc_user_space(size); |
| 1060 | if (copy_to_user(new, nodes_addr(tmp_mask), size)) |
| 1061 | return -EFAULT; |
| 1062 | } |
| 1063 | return sys_migrate_pages(pid, nr_bits + 1, old, new); |
| 1064 | } |
Christoph Lameter | 1b2db9f | 2006-06-23 02:03:56 -0700 | [diff] [blame] | 1065 | #endif |
Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1066 | |
| 1067 | struct compat_sysinfo { |
| 1068 | s32 uptime; |
| 1069 | u32 loads[3]; |
| 1070 | u32 totalram; |
| 1071 | u32 freeram; |
| 1072 | u32 sharedram; |
| 1073 | u32 bufferram; |
| 1074 | u32 totalswap; |
| 1075 | u32 freeswap; |
| 1076 | u16 procs; |
| 1077 | u16 pad; |
| 1078 | u32 totalhigh; |
| 1079 | u32 freehigh; |
| 1080 | u32 mem_unit; |
| 1081 | char _f[20-2*sizeof(u32)-sizeof(int)]; |
| 1082 | }; |
| 1083 | |
| 1084 | asmlinkage long |
| 1085 | compat_sys_sysinfo(struct compat_sysinfo __user *info) |
| 1086 | { |
| 1087 | struct sysinfo s; |
| 1088 | |
| 1089 | do_sysinfo(&s); |
| 1090 | |
| 1091 | /* Check to see if any memory value is too large for 32-bit and scale |
| 1092 | * down if needed |
| 1093 | */ |
| 1094 | if ((s.totalram >> 32) || (s.totalswap >> 32)) { |
| 1095 | int bitcount = 0; |
| 1096 | |
| 1097 | while (s.mem_unit < PAGE_SIZE) { |
| 1098 | s.mem_unit <<= 1; |
| 1099 | bitcount++; |
| 1100 | } |
| 1101 | |
| 1102 | s.totalram >>= bitcount; |
| 1103 | s.freeram >>= bitcount; |
| 1104 | s.sharedram >>= bitcount; |
| 1105 | s.bufferram >>= bitcount; |
| 1106 | s.totalswap >>= bitcount; |
| 1107 | s.freeswap >>= bitcount; |
| 1108 | s.totalhigh >>= bitcount; |
| 1109 | s.freehigh >>= bitcount; |
| 1110 | } |
| 1111 | |
| 1112 | if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) || |
| 1113 | __put_user (s.uptime, &info->uptime) || |
| 1114 | __put_user (s.loads[0], &info->loads[0]) || |
| 1115 | __put_user (s.loads[1], &info->loads[1]) || |
| 1116 | __put_user (s.loads[2], &info->loads[2]) || |
| 1117 | __put_user (s.totalram, &info->totalram) || |
| 1118 | __put_user (s.freeram, &info->freeram) || |
| 1119 | __put_user (s.sharedram, &info->sharedram) || |
| 1120 | __put_user (s.bufferram, &info->bufferram) || |
| 1121 | __put_user (s.totalswap, &info->totalswap) || |
| 1122 | __put_user (s.freeswap, &info->freeswap) || |
| 1123 | __put_user (s.procs, &info->procs) || |
| 1124 | __put_user (s.totalhigh, &info->totalhigh) || |
| 1125 | __put_user (s.freehigh, &info->freehigh) || |
| 1126 | __put_user (s.mem_unit, &info->mem_unit)) |
| 1127 | return -EFAULT; |
| 1128 | |
| 1129 | return 0; |
| 1130 | } |