compat.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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_settime(timer_t timer_id, int flags,
  442. struct compat_itimerspec __user *new,
  443. struct compat_itimerspec __user *old)
  444. {
  445. long err;
  446. mm_segment_t oldfs;
  447. struct itimerspec newts, oldts;
  448. if (!new)
  449. return -EINVAL;
  450. if (get_compat_itimerspec(&newts, new))
  451. return -EFAULT;
  452. oldfs = get_fs();
  453. set_fs(KERNEL_DS);
  454. err = sys_timer_settime(timer_id, flags,
  455. (struct itimerspec __user *) &newts,
  456. (struct itimerspec __user *) &oldts);
  457. set_fs(oldfs);
  458. if (!err && old && put_compat_itimerspec(old, &oldts))
  459. return -EFAULT;
  460. return err;
  461. }
  462. long compat_sys_timer_gettime(timer_t timer_id,
  463. struct compat_itimerspec __user *setting)
  464. {
  465. long err;
  466. mm_segment_t oldfs;
  467. struct itimerspec ts;
  468. oldfs = get_fs();
  469. set_fs(KERNEL_DS);
  470. err = sys_timer_gettime(timer_id,
  471. (struct itimerspec __user *) &ts);
  472. set_fs(oldfs);
  473. if (!err && put_compat_itimerspec(setting, &ts))
  474. return -EFAULT;
  475. return err;
  476. }
  477. long compat_sys_clock_settime(clockid_t which_clock,
  478. struct compat_timespec __user *tp)
  479. {
  480. long err;
  481. mm_segment_t oldfs;
  482. struct timespec ts;
  483. if (get_compat_timespec(&ts, tp))
  484. return -EFAULT;
  485. oldfs = get_fs();
  486. set_fs(KERNEL_DS);
  487. err = sys_clock_settime(which_clock,
  488. (struct timespec __user *) &ts);
  489. set_fs(oldfs);
  490. return err;
  491. }
  492. long compat_sys_clock_gettime(clockid_t which_clock,
  493. struct compat_timespec __user *tp)
  494. {
  495. long err;
  496. mm_segment_t oldfs;
  497. struct timespec ts;
  498. oldfs = get_fs();
  499. set_fs(KERNEL_DS);
  500. err = sys_clock_gettime(which_clock,
  501. (struct timespec __user *) &ts);
  502. set_fs(oldfs);
  503. if (!err && put_compat_timespec(&ts, tp))
  504. return -EFAULT;
  505. return err;
  506. }
  507. long compat_sys_clock_getres(clockid_t which_clock,
  508. struct compat_timespec __user *tp)
  509. {
  510. long err;
  511. mm_segment_t oldfs;
  512. struct timespec ts;
  513. oldfs = get_fs();
  514. set_fs(KERNEL_DS);
  515. err = sys_clock_getres(which_clock,
  516. (struct timespec __user *) &ts);
  517. set_fs(oldfs);
  518. if (!err && tp && put_compat_timespec(&ts, tp))
  519. return -EFAULT;
  520. return err;
  521. }
  522. long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
  523. struct compat_timespec __user *rqtp,
  524. struct compat_timespec __user *rmtp)
  525. {
  526. long err;
  527. mm_segment_t oldfs;
  528. struct timespec in, out;
  529. if (get_compat_timespec(&in, rqtp))
  530. return -EFAULT;
  531. oldfs = get_fs();
  532. set_fs(KERNEL_DS);
  533. err = sys_clock_nanosleep(which_clock, flags,
  534. (struct timespec __user *) &in,
  535. (struct timespec __user *) &out);
  536. set_fs(oldfs);
  537. if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
  538. put_compat_timespec(&out, rmtp))
  539. return -EFAULT;
  540. return err;
  541. }
  542. /*
  543. * We currently only need the following fields from the sigevent
  544. * structure: sigev_value, sigev_signo, sig_notify and (sometimes
  545. * sigev_notify_thread_id). The others are handled in user mode.
  546. * We also assume that copying sigev_value.sival_int is sufficient
  547. * to keep all the bits of sigev_value.sival_ptr intact.
  548. */
  549. int get_compat_sigevent(struct sigevent *event,
  550. const struct compat_sigevent __user *u_event)
  551. {
  552. memset(event, 0, sizeof(*event));
  553. return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
  554. __get_user(event->sigev_value.sival_int,
  555. &u_event->sigev_value.sival_int) ||
  556. __get_user(event->sigev_signo, &u_event->sigev_signo) ||
  557. __get_user(event->sigev_notify, &u_event->sigev_notify) ||
  558. __get_user(event->sigev_notify_thread_id,
  559. &u_event->sigev_notify_thread_id))
  560. ? -EFAULT : 0;
  561. }
  562. /* timer_create is architecture specific because it needs sigevent conversion */
  563. long compat_get_bitmap(unsigned long *mask, compat_ulong_t __user *umask,
  564. unsigned long bitmap_size)
  565. {
  566. int i, j;
  567. unsigned long m;
  568. compat_ulong_t um;
  569. unsigned long nr_compat_longs;
  570. /* align bitmap up to nearest compat_long_t boundary */
  571. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  572. if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
  573. return -EFAULT;
  574. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  575. for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
  576. m = 0;
  577. for (j = 0; j < sizeof(m)/sizeof(um); j++) {
  578. /*
  579. * We dont want to read past the end of the userspace
  580. * bitmap. We must however ensure the end of the
  581. * kernel bitmap is zeroed.
  582. */
  583. if (nr_compat_longs-- > 0) {
  584. if (__get_user(um, umask))
  585. return -EFAULT;
  586. } else {
  587. um = 0;
  588. }
  589. umask++;
  590. m |= (long)um << (j * BITS_PER_COMPAT_LONG);
  591. }
  592. *mask++ = m;
  593. }
  594. return 0;
  595. }
  596. long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
  597. unsigned long bitmap_size)
  598. {
  599. int i, j;
  600. unsigned long m;
  601. compat_ulong_t um;
  602. unsigned long nr_compat_longs;
  603. /* align bitmap up to nearest compat_long_t boundary */
  604. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  605. if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
  606. return -EFAULT;
  607. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  608. for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
  609. m = *mask++;
  610. for (j = 0; j < sizeof(m)/sizeof(um); j++) {
  611. um = m;
  612. /*
  613. * We dont want to write past the end of the userspace
  614. * bitmap.
  615. */
  616. if (nr_compat_longs-- > 0) {
  617. if (__put_user(um, umask))
  618. return -EFAULT;
  619. }
  620. umask++;
  621. m >>= 4*sizeof(um);
  622. m >>= 4*sizeof(um);
  623. }
  624. }
  625. return 0;
  626. }
  627. void
  628. sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
  629. {
  630. switch (_NSIG_WORDS) {
  631. #if defined (__COMPAT_ENDIAN_SWAP__)
  632. case 4: set->sig[3] = compat->sig[7] | (((long)compat->sig[6]) << 32 );
  633. case 3: set->sig[2] = compat->sig[5] | (((long)compat->sig[4]) << 32 );
  634. case 2: set->sig[1] = compat->sig[3] | (((long)compat->sig[2]) << 32 );
  635. case 1: set->sig[0] = compat->sig[1] | (((long)compat->sig[0]) << 32 );
  636. #else
  637. case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
  638. case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
  639. case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
  640. case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
  641. #endif
  642. }
  643. }
  644. asmlinkage long
  645. compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
  646. struct compat_siginfo __user *uinfo,
  647. struct compat_timespec __user *uts, compat_size_t sigsetsize)
  648. {
  649. compat_sigset_t s32;
  650. sigset_t s;
  651. int sig;
  652. struct timespec t;
  653. siginfo_t info;
  654. long ret, timeout = 0;
  655. if (sigsetsize != sizeof(sigset_t))
  656. return -EINVAL;
  657. if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
  658. return -EFAULT;
  659. sigset_from_compat(&s, &s32);
  660. sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
  661. signotset(&s);
  662. if (uts) {
  663. if (get_compat_timespec (&t, uts))
  664. return -EFAULT;
  665. if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0
  666. || t.tv_sec < 0)
  667. return -EINVAL;
  668. }
  669. spin_lock_irq(&current->sighand->siglock);
  670. sig = dequeue_signal(current, &s, &info);
  671. if (!sig) {
  672. timeout = MAX_SCHEDULE_TIMEOUT;
  673. if (uts)
  674. timeout = timespec_to_jiffies(&t)
  675. +(t.tv_sec || t.tv_nsec);
  676. if (timeout) {
  677. current->real_blocked = current->blocked;
  678. sigandsets(&current->blocked, &current->blocked, &s);
  679. recalc_sigpending();
  680. spin_unlock_irq(&current->sighand->siglock);
  681. timeout = schedule_timeout_interruptible(timeout);
  682. spin_lock_irq(&current->sighand->siglock);
  683. sig = dequeue_signal(current, &s, &info);
  684. current->blocked = current->real_blocked;
  685. siginitset(&current->real_blocked, 0);
  686. recalc_sigpending();
  687. }
  688. }
  689. spin_unlock_irq(&current->sighand->siglock);
  690. if (sig) {
  691. ret = sig;
  692. if (uinfo) {
  693. if (copy_siginfo_to_user32(uinfo, &info))
  694. ret = -EFAULT;
  695. }
  696. }else {
  697. ret = timeout?-EINTR:-EAGAIN;
  698. }
  699. return ret;
  700. }
  701. #ifdef __ARCH_WANT_COMPAT_SYS_TIME
  702. /* compat_time_t is a 32 bit "long" and needs to get converted. */
  703. asmlinkage long compat_sys_time(compat_time_t __user * tloc)
  704. {
  705. compat_time_t i;
  706. struct timeval tv;
  707. do_gettimeofday(&tv);
  708. i = tv.tv_sec;
  709. if (tloc) {
  710. if (put_user(i,tloc))
  711. i = -EFAULT;
  712. }
  713. return i;
  714. }
  715. asmlinkage long compat_sys_stime(compat_time_t __user *tptr)
  716. {
  717. struct timespec tv;
  718. int err;
  719. if (get_user(tv.tv_sec, tptr))
  720. return -EFAULT;
  721. tv.tv_nsec = 0;
  722. err = security_settime(&tv, NULL);
  723. if (err)
  724. return err;
  725. do_settimeofday(&tv);
  726. return 0;
  727. }
  728. #endif /* __ARCH_WANT_COMPAT_SYS_TIME */