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