compat.c 25 KB

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