compat.c 23 KB

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