compat.c 27 KB

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