compat.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. #include <linux/linkage.h>
  14. #include <linux/compat.h>
  15. #include <linux/errno.h>
  16. #include <linux/time.h>
  17. #include <linux/signal.h>
  18. #include <linux/sched.h> /* for MAX_SCHEDULE_TIMEOUT */
  19. #include <linux/futex.h> /* for FUTEX_WAIT */
  20. #include <linux/syscalls.h>
  21. #include <linux/unistd.h>
  22. #include <linux/security.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/bug.h>
  25. int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
  26. {
  27. return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
  28. __get_user(ts->tv_sec, &cts->tv_sec) ||
  29. __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
  30. }
  31. int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
  32. {
  33. return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
  34. __put_user(ts->tv_sec, &cts->tv_sec) ||
  35. __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
  36. }
  37. static long compat_nanosleep_restart(struct restart_block *restart)
  38. {
  39. unsigned long expire = restart->arg0, now = jiffies;
  40. struct compat_timespec __user *rmtp;
  41. /* Did it expire while we handled signals? */
  42. if (!time_after(expire, now))
  43. return 0;
  44. expire = schedule_timeout_interruptible(expire - now);
  45. if (expire == 0)
  46. return 0;
  47. rmtp = (struct compat_timespec __user *)restart->arg1;
  48. if (rmtp) {
  49. struct compat_timespec ct;
  50. struct timespec t;
  51. jiffies_to_timespec(expire, &t);
  52. ct.tv_sec = t.tv_sec;
  53. ct.tv_nsec = t.tv_nsec;
  54. if (copy_to_user(rmtp, &ct, sizeof(ct)))
  55. return -EFAULT;
  56. }
  57. /* The 'restart' block is already filled in */
  58. return -ERESTART_RESTARTBLOCK;
  59. }
  60. asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
  61. struct compat_timespec __user *rmtp)
  62. {
  63. struct timespec t;
  64. struct restart_block *restart;
  65. unsigned long expire;
  66. if (get_compat_timespec(&t, rqtp))
  67. return -EFAULT;
  68. if ((t.tv_nsec >= 1000000000L) || (t.tv_nsec < 0) || (t.tv_sec < 0))
  69. return -EINVAL;
  70. expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
  71. expire = schedule_timeout_interruptible(expire);
  72. if (expire == 0)
  73. return 0;
  74. if (rmtp) {
  75. jiffies_to_timespec(expire, &t);
  76. if (put_compat_timespec(&t, rmtp))
  77. return -EFAULT;
  78. }
  79. restart = &current_thread_info()->restart_block;
  80. restart->fn = compat_nanosleep_restart;
  81. restart->arg0 = jiffies + expire;
  82. restart->arg1 = (unsigned long) rmtp;
  83. return -ERESTART_RESTARTBLOCK;
  84. }
  85. static inline long get_compat_itimerval(struct itimerval *o,
  86. struct compat_itimerval __user *i)
  87. {
  88. return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
  89. (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
  90. __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
  91. __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
  92. __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
  93. }
  94. static inline long put_compat_itimerval(struct compat_itimerval __user *o,
  95. struct itimerval *i)
  96. {
  97. return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
  98. (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
  99. __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
  100. __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
  101. __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
  102. }
  103. asmlinkage long compat_sys_getitimer(int which,
  104. struct compat_itimerval __user *it)
  105. {
  106. struct itimerval kit;
  107. int error;
  108. error = do_getitimer(which, &kit);
  109. if (!error && put_compat_itimerval(it, &kit))
  110. error = -EFAULT;
  111. return error;
  112. }
  113. asmlinkage long compat_sys_setitimer(int which,
  114. struct compat_itimerval __user *in,
  115. struct compat_itimerval __user *out)
  116. {
  117. struct itimerval kin, kout;
  118. int error;
  119. if (in) {
  120. if (get_compat_itimerval(&kin, in))
  121. return -EFAULT;
  122. } else
  123. memset(&kin, 0, sizeof(kin));
  124. error = do_setitimer(which, &kin, out ? &kout : NULL);
  125. if (error || !out)
  126. return error;
  127. if (put_compat_itimerval(out, &kout))
  128. return -EFAULT;
  129. return 0;
  130. }
  131. asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
  132. {
  133. /*
  134. * In the SMP world we might just be unlucky and have one of
  135. * the times increment as we use it. Since the value is an
  136. * atomically safe type this is just fine. Conceptually its
  137. * as if the syscall took an instant longer to occur.
  138. */
  139. if (tbuf) {
  140. struct compat_tms tmp;
  141. struct task_struct *tsk = current;
  142. struct task_struct *t;
  143. cputime_t utime, stime, cutime, cstime;
  144. read_lock(&tasklist_lock);
  145. utime = tsk->signal->utime;
  146. stime = tsk->signal->stime;
  147. t = tsk;
  148. do {
  149. utime = cputime_add(utime, t->utime);
  150. stime = cputime_add(stime, t->stime);
  151. t = next_thread(t);
  152. } while (t != tsk);
  153. /*
  154. * While we have tasklist_lock read-locked, no dying thread
  155. * can be updating current->signal->[us]time. Instead,
  156. * we got their counts included in the live thread loop.
  157. * However, another thread can come in right now and
  158. * do a wait call that updates current->signal->c[us]time.
  159. * To make sure we always see that pair updated atomically,
  160. * we take the siglock around fetching them.
  161. */
  162. spin_lock_irq(&tsk->sighand->siglock);
  163. cutime = tsk->signal->cutime;
  164. cstime = tsk->signal->cstime;
  165. spin_unlock_irq(&tsk->sighand->siglock);
  166. read_unlock(&tasklist_lock);
  167. tmp.tms_utime = compat_jiffies_to_clock_t(cputime_to_jiffies(utime));
  168. tmp.tms_stime = compat_jiffies_to_clock_t(cputime_to_jiffies(stime));
  169. tmp.tms_cutime = compat_jiffies_to_clock_t(cputime_to_jiffies(cutime));
  170. tmp.tms_cstime = compat_jiffies_to_clock_t(cputime_to_jiffies(cstime));
  171. if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
  172. return -EFAULT;
  173. }
  174. return compat_jiffies_to_clock_t(jiffies);
  175. }
  176. /*
  177. * Assumption: old_sigset_t and compat_old_sigset_t are both
  178. * types that can be passed to put_user()/get_user().
  179. */
  180. asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
  181. {
  182. old_sigset_t s;
  183. long ret;
  184. mm_segment_t old_fs = get_fs();
  185. set_fs(KERNEL_DS);
  186. ret = sys_sigpending((old_sigset_t __user *) &s);
  187. set_fs(old_fs);
  188. if (ret == 0)
  189. ret = put_user(s, set);
  190. return ret;
  191. }
  192. asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set,
  193. compat_old_sigset_t __user *oset)
  194. {
  195. old_sigset_t s;
  196. long ret;
  197. mm_segment_t old_fs;
  198. if (set && get_user(s, set))
  199. return -EFAULT;
  200. old_fs = get_fs();
  201. set_fs(KERNEL_DS);
  202. ret = sys_sigprocmask(how,
  203. set ? (old_sigset_t __user *) &s : NULL,
  204. oset ? (old_sigset_t __user *) &s : NULL);
  205. set_fs(old_fs);
  206. if (ret == 0)
  207. if (oset)
  208. ret = put_user(s, oset);
  209. return ret;
  210. }
  211. #ifdef CONFIG_FUTEX
  212. asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, int val,
  213. struct compat_timespec __user *utime, u32 __user *uaddr2,
  214. int val3)
  215. {
  216. struct timespec t;
  217. unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
  218. int val2 = 0;
  219. if ((op == FUTEX_WAIT) && utime) {
  220. if (get_compat_timespec(&t, utime))
  221. return -EFAULT;
  222. timeout = timespec_to_jiffies(&t) + 1;
  223. }
  224. if (op >= FUTEX_REQUEUE)
  225. val2 = (int) (unsigned long) utime;
  226. return do_futex((unsigned long)uaddr, op, val, timeout,
  227. (unsigned long)uaddr2, val2, val3);
  228. }
  229. #endif
  230. asmlinkage long compat_sys_setrlimit(unsigned int resource,
  231. struct compat_rlimit __user *rlim)
  232. {
  233. struct rlimit r;
  234. int ret;
  235. mm_segment_t old_fs = get_fs ();
  236. if (resource >= RLIM_NLIMITS)
  237. return -EINVAL;
  238. if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
  239. __get_user(r.rlim_cur, &rlim->rlim_cur) ||
  240. __get_user(r.rlim_max, &rlim->rlim_max))
  241. return -EFAULT;
  242. if (r.rlim_cur == COMPAT_RLIM_INFINITY)
  243. r.rlim_cur = RLIM_INFINITY;
  244. if (r.rlim_max == COMPAT_RLIM_INFINITY)
  245. r.rlim_max = RLIM_INFINITY;
  246. set_fs(KERNEL_DS);
  247. ret = sys_setrlimit(resource, (struct rlimit __user *) &r);
  248. set_fs(old_fs);
  249. return ret;
  250. }
  251. #ifdef COMPAT_RLIM_OLD_INFINITY
  252. asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
  253. struct compat_rlimit __user *rlim)
  254. {
  255. struct rlimit r;
  256. int ret;
  257. mm_segment_t old_fs = get_fs();
  258. set_fs(KERNEL_DS);
  259. ret = sys_old_getrlimit(resource, &r);
  260. set_fs(old_fs);
  261. if (!ret) {
  262. if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
  263. r.rlim_cur = COMPAT_RLIM_INFINITY;
  264. if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
  265. r.rlim_max = COMPAT_RLIM_INFINITY;
  266. if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
  267. __put_user(r.rlim_cur, &rlim->rlim_cur) ||
  268. __put_user(r.rlim_max, &rlim->rlim_max))
  269. return -EFAULT;
  270. }
  271. return ret;
  272. }
  273. #endif
  274. asmlinkage long compat_sys_getrlimit (unsigned int resource,
  275. struct compat_rlimit __user *rlim)
  276. {
  277. struct rlimit r;
  278. int ret;
  279. mm_segment_t old_fs = get_fs();
  280. set_fs(KERNEL_DS);
  281. ret = sys_getrlimit(resource, (struct rlimit __user *) &r);
  282. set_fs(old_fs);
  283. if (!ret) {
  284. if (r.rlim_cur > COMPAT_RLIM_INFINITY)
  285. r.rlim_cur = COMPAT_RLIM_INFINITY;
  286. if (r.rlim_max > COMPAT_RLIM_INFINITY)
  287. r.rlim_max = COMPAT_RLIM_INFINITY;
  288. if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
  289. __put_user(r.rlim_cur, &rlim->rlim_cur) ||
  290. __put_user(r.rlim_max, &rlim->rlim_max))
  291. return -EFAULT;
  292. }
  293. return ret;
  294. }
  295. int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
  296. {
  297. if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
  298. __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
  299. __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
  300. __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
  301. __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
  302. __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
  303. __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
  304. __put_user(r->ru_idrss, &ru->ru_idrss) ||
  305. __put_user(r->ru_isrss, &ru->ru_isrss) ||
  306. __put_user(r->ru_minflt, &ru->ru_minflt) ||
  307. __put_user(r->ru_majflt, &ru->ru_majflt) ||
  308. __put_user(r->ru_nswap, &ru->ru_nswap) ||
  309. __put_user(r->ru_inblock, &ru->ru_inblock) ||
  310. __put_user(r->ru_oublock, &ru->ru_oublock) ||
  311. __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
  312. __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
  313. __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
  314. __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
  315. __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
  316. return -EFAULT;
  317. return 0;
  318. }
  319. asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
  320. {
  321. struct rusage r;
  322. int ret;
  323. mm_segment_t old_fs = get_fs();
  324. set_fs(KERNEL_DS);
  325. ret = sys_getrusage(who, (struct rusage __user *) &r);
  326. set_fs(old_fs);
  327. if (ret)
  328. return ret;
  329. if (put_compat_rusage(&r, ru))
  330. return -EFAULT;
  331. return 0;
  332. }
  333. asmlinkage long
  334. compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
  335. struct compat_rusage __user *ru)
  336. {
  337. if (!ru) {
  338. return sys_wait4(pid, stat_addr, options, NULL);
  339. } else {
  340. struct rusage r;
  341. int ret;
  342. unsigned int status;
  343. mm_segment_t old_fs = get_fs();
  344. set_fs (KERNEL_DS);
  345. ret = sys_wait4(pid,
  346. (stat_addr ?
  347. (unsigned int __user *) &status : NULL),
  348. options, (struct rusage __user *) &r);
  349. set_fs (old_fs);
  350. if (ret > 0) {
  351. if (put_compat_rusage(&r, ru))
  352. return -EFAULT;
  353. if (stat_addr && put_user(status, stat_addr))
  354. return -EFAULT;
  355. }
  356. return ret;
  357. }
  358. }
  359. asmlinkage long compat_sys_waitid(int which, compat_pid_t pid,
  360. struct compat_siginfo __user *uinfo, int options,
  361. struct compat_rusage __user *uru)
  362. {
  363. siginfo_t info;
  364. struct rusage ru;
  365. long ret;
  366. mm_segment_t old_fs = get_fs();
  367. memset(&info, 0, sizeof(info));
  368. set_fs(KERNEL_DS);
  369. ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
  370. uru ? (struct rusage __user *)&ru : NULL);
  371. set_fs(old_fs);
  372. if ((ret < 0) || (info.si_signo == 0))
  373. return ret;
  374. if (uru) {
  375. ret = put_compat_rusage(&ru, uru);
  376. if (ret)
  377. return ret;
  378. }
  379. BUG_ON(info.si_code & __SI_MASK);
  380. info.si_code |= __SI_CHLD;
  381. return copy_siginfo_to_user32(uinfo, &info);
  382. }
  383. static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
  384. unsigned len, cpumask_t *new_mask)
  385. {
  386. unsigned long *k;
  387. if (len < sizeof(cpumask_t))
  388. memset(new_mask, 0, sizeof(cpumask_t));
  389. else if (len > sizeof(cpumask_t))
  390. len = sizeof(cpumask_t);
  391. k = cpus_addr(*new_mask);
  392. return compat_get_bitmap(k, user_mask_ptr, len * 8);
  393. }
  394. asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
  395. unsigned int len,
  396. compat_ulong_t __user *user_mask_ptr)
  397. {
  398. cpumask_t new_mask;
  399. int retval;
  400. retval = compat_get_user_cpu_mask(user_mask_ptr, len, &new_mask);
  401. if (retval)
  402. return retval;
  403. return sched_setaffinity(pid, new_mask);
  404. }
  405. asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
  406. compat_ulong_t __user *user_mask_ptr)
  407. {
  408. int ret;
  409. cpumask_t mask;
  410. unsigned long *k;
  411. unsigned int min_length = sizeof(cpumask_t);
  412. if (NR_CPUS <= BITS_PER_COMPAT_LONG)
  413. min_length = sizeof(compat_ulong_t);
  414. if (len < min_length)
  415. return -EINVAL;
  416. ret = sched_getaffinity(pid, &mask);
  417. if (ret < 0)
  418. return ret;
  419. k = cpus_addr(mask);
  420. ret = compat_put_bitmap(user_mask_ptr, k, min_length * 8);
  421. if (ret)
  422. return ret;
  423. return min_length;
  424. }
  425. static int get_compat_itimerspec(struct itimerspec *dst,
  426. struct compat_itimerspec __user *src)
  427. {
  428. if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
  429. get_compat_timespec(&dst->it_value, &src->it_value))
  430. return -EFAULT;
  431. return 0;
  432. }
  433. static int put_compat_itimerspec(struct compat_itimerspec __user *dst,
  434. struct itimerspec *src)
  435. {
  436. if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
  437. put_compat_timespec(&src->it_value, &dst->it_value))
  438. return -EFAULT;
  439. return 0;
  440. }
  441. long compat_sys_timer_create(clockid_t which_clock,
  442. struct compat_sigevent __user *timer_event_spec,
  443. timer_t __user *created_timer_id)
  444. {
  445. struct sigevent __user *event = NULL;
  446. if (timer_event_spec) {
  447. struct sigevent kevent;
  448. event = compat_alloc_user_space(sizeof(*event));
  449. if (get_compat_sigevent(&kevent, timer_event_spec) ||
  450. copy_to_user(event, &kevent, sizeof(*event)))
  451. return -EFAULT;
  452. }
  453. return sys_timer_create(which_clock, event, created_timer_id);
  454. }
  455. long compat_sys_timer_settime(timer_t timer_id, int flags,
  456. struct compat_itimerspec __user *new,
  457. struct compat_itimerspec __user *old)
  458. {
  459. long err;
  460. mm_segment_t oldfs;
  461. struct itimerspec newts, oldts;
  462. if (!new)
  463. return -EINVAL;
  464. if (get_compat_itimerspec(&newts, new))
  465. return -EFAULT;
  466. oldfs = get_fs();
  467. set_fs(KERNEL_DS);
  468. err = sys_timer_settime(timer_id, flags,
  469. (struct itimerspec __user *) &newts,
  470. (struct itimerspec __user *) &oldts);
  471. set_fs(oldfs);
  472. if (!err && old && put_compat_itimerspec(old, &oldts))
  473. return -EFAULT;
  474. return err;
  475. }
  476. long compat_sys_timer_gettime(timer_t timer_id,
  477. struct compat_itimerspec __user *setting)
  478. {
  479. long err;
  480. mm_segment_t oldfs;
  481. struct itimerspec ts;
  482. oldfs = get_fs();
  483. set_fs(KERNEL_DS);
  484. err = sys_timer_gettime(timer_id,
  485. (struct itimerspec __user *) &ts);
  486. set_fs(oldfs);
  487. if (!err && put_compat_itimerspec(setting, &ts))
  488. return -EFAULT;
  489. return err;
  490. }
  491. long compat_sys_clock_settime(clockid_t which_clock,
  492. struct compat_timespec __user *tp)
  493. {
  494. long err;
  495. mm_segment_t oldfs;
  496. struct timespec ts;
  497. if (get_compat_timespec(&ts, tp))
  498. return -EFAULT;
  499. oldfs = get_fs();
  500. set_fs(KERNEL_DS);
  501. err = sys_clock_settime(which_clock,
  502. (struct timespec __user *) &ts);
  503. set_fs(oldfs);
  504. return err;
  505. }
  506. long compat_sys_clock_gettime(clockid_t which_clock,
  507. struct compat_timespec __user *tp)
  508. {
  509. long err;
  510. mm_segment_t oldfs;
  511. struct timespec ts;
  512. oldfs = get_fs();
  513. set_fs(KERNEL_DS);
  514. err = sys_clock_gettime(which_clock,
  515. (struct timespec __user *) &ts);
  516. set_fs(oldfs);
  517. if (!err && put_compat_timespec(&ts, tp))
  518. return -EFAULT;
  519. return err;
  520. }
  521. long compat_sys_clock_getres(clockid_t which_clock,
  522. struct compat_timespec __user *tp)
  523. {
  524. long err;
  525. mm_segment_t oldfs;
  526. struct timespec ts;
  527. oldfs = get_fs();
  528. set_fs(KERNEL_DS);
  529. err = sys_clock_getres(which_clock,
  530. (struct timespec __user *) &ts);
  531. set_fs(oldfs);
  532. if (!err && tp && put_compat_timespec(&ts, tp))
  533. return -EFAULT;
  534. return err;
  535. }
  536. long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
  537. struct compat_timespec __user *rqtp,
  538. struct compat_timespec __user *rmtp)
  539. {
  540. long err;
  541. mm_segment_t oldfs;
  542. struct timespec in, out;
  543. if (get_compat_timespec(&in, rqtp))
  544. return -EFAULT;
  545. oldfs = get_fs();
  546. set_fs(KERNEL_DS);
  547. err = sys_clock_nanosleep(which_clock, flags,
  548. (struct timespec __user *) &in,
  549. (struct timespec __user *) &out);
  550. set_fs(oldfs);
  551. if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
  552. put_compat_timespec(&out, rmtp))
  553. return -EFAULT;
  554. return err;
  555. }
  556. /*
  557. * We currently only need the following fields from the sigevent
  558. * structure: sigev_value, sigev_signo, sig_notify and (sometimes
  559. * sigev_notify_thread_id). The others are handled in user mode.
  560. * We also assume that copying sigev_value.sival_int is sufficient
  561. * to keep all the bits of sigev_value.sival_ptr intact.
  562. */
  563. int get_compat_sigevent(struct sigevent *event,
  564. const struct compat_sigevent __user *u_event)
  565. {
  566. memset(event, 0, sizeof(*event));
  567. return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
  568. __get_user(event->sigev_value.sival_int,
  569. &u_event->sigev_value.sival_int) ||
  570. __get_user(event->sigev_signo, &u_event->sigev_signo) ||
  571. __get_user(event->sigev_notify, &u_event->sigev_notify) ||
  572. __get_user(event->sigev_notify_thread_id,
  573. &u_event->sigev_notify_thread_id))
  574. ? -EFAULT : 0;
  575. }
  576. long compat_get_bitmap(unsigned long *mask, compat_ulong_t __user *umask,
  577. unsigned long bitmap_size)
  578. {
  579. int i, j;
  580. unsigned long m;
  581. compat_ulong_t um;
  582. unsigned long nr_compat_longs;
  583. /* align bitmap up to nearest compat_long_t boundary */
  584. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  585. if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
  586. return -EFAULT;
  587. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  588. for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
  589. m = 0;
  590. for (j = 0; j < sizeof(m)/sizeof(um); j++) {
  591. /*
  592. * We dont want to read past the end of the userspace
  593. * bitmap. We must however ensure the end of the
  594. * kernel bitmap is zeroed.
  595. */
  596. if (nr_compat_longs-- > 0) {
  597. if (__get_user(um, umask))
  598. return -EFAULT;
  599. } else {
  600. um = 0;
  601. }
  602. umask++;
  603. m |= (long)um << (j * BITS_PER_COMPAT_LONG);
  604. }
  605. *mask++ = m;
  606. }
  607. return 0;
  608. }
  609. long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
  610. unsigned long bitmap_size)
  611. {
  612. int i, j;
  613. unsigned long m;
  614. compat_ulong_t um;
  615. unsigned long nr_compat_longs;
  616. /* align bitmap up to nearest compat_long_t boundary */
  617. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  618. if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
  619. return -EFAULT;
  620. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  621. for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
  622. m = *mask++;
  623. for (j = 0; j < sizeof(m)/sizeof(um); j++) {
  624. um = m;
  625. /*
  626. * We dont want to write past the end of the userspace
  627. * bitmap.
  628. */
  629. if (nr_compat_longs-- > 0) {
  630. if (__put_user(um, umask))
  631. return -EFAULT;
  632. }
  633. umask++;
  634. m >>= 4*sizeof(um);
  635. m >>= 4*sizeof(um);
  636. }
  637. }
  638. return 0;
  639. }
  640. void
  641. sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
  642. {
  643. switch (_NSIG_WORDS) {
  644. #if defined (__COMPAT_ENDIAN_SWAP__)
  645. case 4: set->sig[3] = compat->sig[7] | (((long)compat->sig[6]) << 32 );
  646. case 3: set->sig[2] = compat->sig[5] | (((long)compat->sig[4]) << 32 );
  647. case 2: set->sig[1] = compat->sig[3] | (((long)compat->sig[2]) << 32 );
  648. case 1: set->sig[0] = compat->sig[1] | (((long)compat->sig[0]) << 32 );
  649. #else
  650. case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
  651. case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
  652. case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
  653. case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
  654. #endif
  655. }
  656. }
  657. asmlinkage long
  658. compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
  659. struct compat_siginfo __user *uinfo,
  660. struct compat_timespec __user *uts, compat_size_t sigsetsize)
  661. {
  662. compat_sigset_t s32;
  663. sigset_t s;
  664. int sig;
  665. struct timespec t;
  666. siginfo_t info;
  667. long ret, timeout = 0;
  668. if (sigsetsize != sizeof(sigset_t))
  669. return -EINVAL;
  670. if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
  671. return -EFAULT;
  672. sigset_from_compat(&s, &s32);
  673. sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
  674. signotset(&s);
  675. if (uts) {
  676. if (get_compat_timespec (&t, uts))
  677. return -EFAULT;
  678. if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0
  679. || t.tv_sec < 0)
  680. return -EINVAL;
  681. }
  682. spin_lock_irq(&current->sighand->siglock);
  683. sig = dequeue_signal(current, &s, &info);
  684. if (!sig) {
  685. timeout = MAX_SCHEDULE_TIMEOUT;
  686. if (uts)
  687. timeout = timespec_to_jiffies(&t)
  688. +(t.tv_sec || t.tv_nsec);
  689. if (timeout) {
  690. current->real_blocked = current->blocked;
  691. sigandsets(&current->blocked, &current->blocked, &s);
  692. recalc_sigpending();
  693. spin_unlock_irq(&current->sighand->siglock);
  694. timeout = schedule_timeout_interruptible(timeout);
  695. spin_lock_irq(&current->sighand->siglock);
  696. sig = dequeue_signal(current, &s, &info);
  697. current->blocked = current->real_blocked;
  698. siginitset(&current->real_blocked, 0);
  699. recalc_sigpending();
  700. }
  701. }
  702. spin_unlock_irq(&current->sighand->siglock);
  703. if (sig) {
  704. ret = sig;
  705. if (uinfo) {
  706. if (copy_siginfo_to_user32(uinfo, &info))
  707. ret = -EFAULT;
  708. }
  709. }else {
  710. ret = timeout?-EINTR:-EAGAIN;
  711. }
  712. return ret;
  713. }
  714. #ifdef __ARCH_WANT_COMPAT_SYS_TIME
  715. /* compat_time_t is a 32 bit "long" and needs to get converted. */
  716. asmlinkage long compat_sys_time(compat_time_t __user * tloc)
  717. {
  718. compat_time_t i;
  719. struct timeval tv;
  720. do_gettimeofday(&tv);
  721. i = tv.tv_sec;
  722. if (tloc) {
  723. if (put_user(i,tloc))
  724. i = -EFAULT;
  725. }
  726. return i;
  727. }
  728. asmlinkage long compat_sys_stime(compat_time_t __user *tptr)
  729. {
  730. struct timespec tv;
  731. int err;
  732. if (get_user(tv.tv_sec, tptr))
  733. return -EFAULT;
  734. tv.tv_nsec = 0;
  735. err = security_settime(&tv, NULL);
  736. if (err)
  737. return err;
  738. do_settimeofday(&tv);
  739. return 0;
  740. }
  741. #endif /* __ARCH_WANT_COMPAT_SYS_TIME */
  742. #ifdef __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND
  743. asmlinkage long compat_sys_rt_sigsuspend(compat_sigset_t __user *unewset, compat_size_t sigsetsize)
  744. {
  745. sigset_t newset;
  746. compat_sigset_t newset32;
  747. /* XXX: Don't preclude handling different sized sigset_t's. */
  748. if (sigsetsize != sizeof(sigset_t))
  749. return -EINVAL;
  750. if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
  751. return -EFAULT;
  752. sigset_from_compat(&newset, &newset32);
  753. sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  754. spin_lock_irq(&current->sighand->siglock);
  755. current->saved_sigmask = current->blocked;
  756. current->blocked = newset;
  757. recalc_sigpending();
  758. spin_unlock_irq(&current->sighand->siglock);
  759. current->state = TASK_INTERRUPTIBLE;
  760. schedule();
  761. set_thread_flag(TIF_RESTORE_SIGMASK);
  762. return -ERESTARTNOHAND;
  763. }
  764. #endif /* __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND */