compat.c 27 KB

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