linux_sched.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* $OpenBSD: linux_sched.c,v 1.18 2014/07/09 14:42:53 guenther Exp $ */
  2. /* $NetBSD: linux_sched.c,v 1.6 2000/05/28 05:49:05 thorpej Exp $ */
  3. /*-
  4. * Copyright (c) 1999 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
  9. * NASA Ames Research Center; by Matthias Scheler.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. * Linux compatibility module. Try to deal with scheduler related syscalls.
  34. */
  35. #include <sys/types.h>
  36. #include <sys/param.h>
  37. #include <sys/mount.h>
  38. #include <sys/proc.h>
  39. #include <sys/systm.h>
  40. #include <sys/syscallargs.h>
  41. #include <sys/signalvar.h>
  42. #include <machine/cpu.h>
  43. #include <machine/pcb.h>
  44. #include <machine/linux_machdep.h>
  45. #include <compat/linux/linux_emuldata.h>
  46. #include <compat/linux/linux_types.h>
  47. #include <compat/linux/linux_sched.h>
  48. #include <compat/linux/linux_signal.h>
  49. #include <compat/linux/linux_syscallargs.h>
  50. void linux_child_return(void *);
  51. int
  52. linux_sys_clone(struct proc *p, void *v, register_t *retval)
  53. {
  54. struct linux_sys_clone_args *uap = v;
  55. struct linux_emuldata *emul = p->p_emuldata;
  56. int cflags = SCARG(uap, flags);
  57. int flags = FORK_TFORK;
  58. int error = 0;
  59. /*
  60. * We only support certain bits. The Linux crew keep adding more,
  61. * so let's test for anything outside of what we support and complain
  62. * about them. Not everything in this list is completely supported,
  63. * they just aren't _always_ an error.
  64. */
  65. if (cflags & ~(LINUX_CLONE_CSIGNAL | LINUX_CLONE_VM | LINUX_CLONE_FS |
  66. LINUX_CLONE_FILES | LINUX_CLONE_SIGHAND | LINUX_CLONE_VFORK |
  67. LINUX_CLONE_PARENT | LINUX_CLONE_THREAD | LINUX_CLONE_SYSVSEM |
  68. LINUX_CLONE_DETACHED | LINUX_CLONE_UNTRACED | LINUX_CLONE_SETTLS |
  69. LINUX_CLONE_PARENT_SETTID | LINUX_CLONE_CHILD_CLEARTID |
  70. LINUX_CLONE_CHILD_SETTID))
  71. return (EINVAL);
  72. if (cflags & LINUX_CLONE_VM)
  73. flags |= FORK_SHAREVM;
  74. if (cflags & LINUX_CLONE_FILES)
  75. flags |= FORK_SHAREFILES;
  76. if (cflags & LINUX_CLONE_SIGHAND) {
  77. /* According to Linux, CLONE_SIGHAND requires CLONE_VM */
  78. if ((cflags & LINUX_CLONE_VM) == 0)
  79. return (EINVAL);
  80. flags |= FORK_SIGHAND;
  81. }
  82. if (cflags & LINUX_CLONE_VFORK)
  83. flags |= FORK_PPWAIT;
  84. if (cflags & LINUX_CLONE_THREAD) {
  85. /*
  86. * Linux agrees with us: CLONE_THREAD requires
  87. * CLONE_SIGHAND. Unlike Linux, we also also require
  88. * CLONE_FS and CLONE_SYSVSEM. Also, we decree it
  89. * to be incompatible with CLONE_VFORK, as I don't
  90. * want to work out whether that's 100% safe.
  91. * Requires CLONE_FILES so that the rest of the kernel
  92. * can assume that threads share an fd table.
  93. */
  94. #define REQUIRED \
  95. ( LINUX_CLONE_SIGHAND \
  96. | LINUX_CLONE_FS \
  97. | LINUX_CLONE_SYSVSEM \
  98. | LINUX_CLONE_FILES \
  99. )
  100. #define BANNED \
  101. LINUX_CLONE_VFORK
  102. if ((cflags & (REQUIRED | BANNED)) != REQUIRED)
  103. return (EINVAL);
  104. flags |= FORK_THREAD;
  105. } else {
  106. /*
  107. * These are only supported with CLONE_THREAD. Arguably,
  108. * CLONE_FS should be in this list, because we don't
  109. * support sharing of working directory and root directory
  110. * (chdir + chroot) except via threads. On the other
  111. * hand, we tie the sharing of umask to the sharing of
  112. * files, so a process that doesn't request CLONE_FS but
  113. * does ask for CLONE_FILES is going to get some of the
  114. * former's effect. Some programs (e.g., Opera) at least
  115. * _seem_ to work if we let it through, so we'll just
  116. * cross our fingers for now and silently ignore it if
  117. * CLONE_FILES was also requested.
  118. */
  119. if (cflags & (LINUX_CLONE_PARENT | LINUX_CLONE_SYSVSEM))
  120. return (EINVAL);
  121. if ((cflags & (LINUX_CLONE_FS | LINUX_CLONE_FILES)) ==
  122. LINUX_CLONE_FS)
  123. return (EINVAL);
  124. /* We don't support alternate exit signals. */
  125. if ((cflags & LINUX_CLONE_CSIGNAL) != LINUX_SIGCHLD)
  126. return (EINVAL);
  127. }
  128. /*
  129. * Since we don't support CLONE_PTRACE, the CLONE_UNTRACED
  130. * flag can be silently ignored. CLONE_DETACHED is always
  131. * ignored by Linux.
  132. */
  133. if (cflags & LINUX_CLONE_CHILD_SETTID)
  134. emul->child_set_tid = SCARG(uap, child_tidptr);
  135. else
  136. emul->child_set_tid = NULL;
  137. if (cflags & LINUX_CLONE_CHILD_CLEARTID)
  138. emul->child_clear_tid = SCARG(uap, child_tidptr);
  139. else
  140. emul->child_clear_tid = NULL;
  141. if (cflags & LINUX_CLONE_PARENT_SETTID)
  142. if (SCARG(uap, parent_tidptr) == NULL)
  143. return (EINVAL);
  144. if (cflags & LINUX_CLONE_SETTLS) {
  145. struct l_segment_descriptor ldesc;
  146. error = copyin(SCARG(uap, tls), &ldesc, sizeof(ldesc));
  147. if (error)
  148. return (error);
  149. if (ldesc.entry_number != GUGS_SEL)
  150. return (EINVAL);
  151. emul->child_tls_base = ldesc.base_addr;
  152. emul->set_tls_base = 1;
  153. }
  154. else
  155. emul->set_tls_base = 0;
  156. error = fork1(p, flags, SCARG(uap, stack), 0, linux_child_return,
  157. NULL, retval, NULL);
  158. if (error)
  159. return error;
  160. if (cflags & LINUX_CLONE_PARENT_SETTID) {
  161. pid_t pid = retval[0];
  162. error = copyout(&pid, SCARG(uap, parent_tidptr), sizeof(pid));
  163. }
  164. return (error);
  165. }
  166. int
  167. linux_sys_sched_setparam(struct proc *cp, void *v, register_t *retval)
  168. {
  169. struct linux_sys_sched_setparam_args /* {
  170. syscallarg(linux_pid_t) pid;
  171. syscallarg(const struct linux_sched_param *) sp;
  172. } */ *uap = v;
  173. int error;
  174. struct linux_sched_param lp;
  175. struct proc *p;
  176. /*
  177. * We only check for valid parameters and return afterwards.
  178. */
  179. if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
  180. return (EINVAL);
  181. error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
  182. if (error)
  183. return (error);
  184. if (SCARG(uap, pid) != 0) {
  185. struct ucred *uc = cp->p_ucred;
  186. if ((p = pfind(SCARG(uap, pid))) == NULL)
  187. return (ESRCH);
  188. if (!(cp == p ||
  189. uc->cr_uid == 0 ||
  190. uc->cr_ruid == p->p_ucred->cr_ruid ||
  191. uc->cr_uid == p->p_ucred->cr_ruid ||
  192. uc->cr_ruid == p->p_ucred->cr_uid ||
  193. uc->cr_uid == p->p_ucred->cr_uid))
  194. return (EPERM);
  195. }
  196. return (0);
  197. }
  198. int
  199. linux_sys_sched_getparam(struct proc *cp, void *v, register_t *retval)
  200. {
  201. struct linux_sys_sched_getparam_args /* {
  202. syscallarg(linux_pid_t) pid;
  203. syscallarg(struct linux_sched_param *) sp;
  204. } */ *uap = v;
  205. struct proc *p;
  206. struct linux_sched_param lp;
  207. /*
  208. * We only check for valid parameters and return a dummy priority
  209. * afterwards.
  210. */
  211. if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
  212. return (EINVAL);
  213. if (SCARG(uap, pid) != 0) {
  214. struct ucred *uc = cp->p_ucred;
  215. if ((p = pfind(SCARG(uap, pid))) == NULL)
  216. return (ESRCH);
  217. if (!(cp == p ||
  218. uc->cr_uid == 0 ||
  219. uc->cr_ruid == p->p_ucred->cr_ruid ||
  220. uc->cr_uid == p->p_ucred->cr_ruid ||
  221. uc->cr_ruid == p->p_ucred->cr_uid ||
  222. uc->cr_uid == p->p_ucred->cr_uid))
  223. return (EPERM);
  224. }
  225. lp.sched_priority = 0;
  226. return (copyout(&lp, SCARG(uap, sp), sizeof lp));
  227. }
  228. int
  229. linux_sys_sched_setscheduler(struct proc *cp, void *v, register_t *retval)
  230. {
  231. struct linux_sys_sched_setscheduler_args /* {
  232. syscallarg(linux_pid_t) pid;
  233. syscallarg(int) policy;
  234. syscallarg(cont struct linux_sched_scheduler *) sp;
  235. } */ *uap = v;
  236. int error;
  237. struct linux_sched_param lp;
  238. struct proc *p;
  239. /*
  240. * We only check for valid parameters and return afterwards.
  241. */
  242. if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
  243. return (EINVAL);
  244. error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
  245. if (error)
  246. return (error);
  247. if (SCARG(uap, pid) != 0) {
  248. struct ucred *uc = cp->p_ucred;
  249. if ((p = pfind(SCARG(uap, pid))) == NULL)
  250. return (ESRCH);
  251. if (!(cp == p ||
  252. uc->cr_uid == 0 ||
  253. uc->cr_ruid == p->p_ucred->cr_ruid ||
  254. uc->cr_uid == p->p_ucred->cr_ruid ||
  255. uc->cr_ruid == p->p_ucred->cr_uid ||
  256. uc->cr_uid == p->p_ucred->cr_uid))
  257. return (EPERM);
  258. }
  259. /*
  260. * We can't emulate anything but the default scheduling policy.
  261. */
  262. if (SCARG(uap, policy) != LINUX_SCHED_OTHER || lp.sched_priority != 0)
  263. return (EINVAL);
  264. return (0);
  265. }
  266. int
  267. linux_sys_sched_getscheduler(struct proc *cp, void *v, register_t *retval)
  268. {
  269. struct linux_sys_sched_getscheduler_args /* {
  270. syscallarg(linux_pid_t) pid;
  271. } */ *uap = v;
  272. struct proc *p;
  273. *retval = -1;
  274. /*
  275. * We only check for valid parameters and return afterwards.
  276. */
  277. if (SCARG(uap, pid) != 0) {
  278. struct ucred *uc = cp->p_ucred;
  279. if ((p = pfind(SCARG(uap, pid))) == NULL)
  280. return (ESRCH);
  281. if (!(cp == p ||
  282. uc->cr_uid == 0 ||
  283. uc->cr_ruid == p->p_ucred->cr_ruid ||
  284. uc->cr_uid == p->p_ucred->cr_ruid ||
  285. uc->cr_ruid == p->p_ucred->cr_uid ||
  286. uc->cr_uid == p->p_ucred->cr_uid))
  287. return (EPERM);
  288. }
  289. /*
  290. * We can't emulate anything but the default scheduling policy.
  291. */
  292. *retval = LINUX_SCHED_OTHER;
  293. return (0);
  294. }
  295. int
  296. linux_sys_sched_yield(struct proc *cp, void *v, register_t *retval)
  297. {
  298. need_resched(curcpu());
  299. return (0);
  300. }
  301. int
  302. linux_sys_sched_get_priority_max(struct proc *cp, void *v, register_t *retval)
  303. {
  304. struct linux_sys_sched_get_priority_max_args /* {
  305. syscallarg(int) policy;
  306. } */ *uap = v;
  307. /*
  308. * We can't emulate anything but the default scheduling policy.
  309. */
  310. if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
  311. *retval = -1;
  312. return (EINVAL);
  313. }
  314. *retval = 0;
  315. return (0);
  316. }
  317. int
  318. linux_sys_sched_get_priority_min(struct proc *cp, void *v, register_t *retval)
  319. {
  320. struct linux_sys_sched_get_priority_min_args /* {
  321. syscallarg(int) policy;
  322. } */ *uap = v;
  323. /*
  324. * We can't emulate anything but the default scheduling policy.
  325. */
  326. if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
  327. *retval = -1;
  328. return (EINVAL);
  329. }
  330. *retval = 0;
  331. return (0);
  332. }
  333. int
  334. linux_sys_set_tid_address(struct proc *p, void *v, register_t *retval)
  335. {
  336. struct linux_sys_set_tid_address_args *uap = v;
  337. struct linux_emuldata *emul = p->p_emuldata;
  338. emul->my_clear_tid = SCARG(uap, tidptr);
  339. *retval = p->p_p->ps_pid;
  340. return 0;
  341. }
  342. void
  343. linux_child_return(void *arg)
  344. {
  345. struct proc *p = (struct proc *)arg;
  346. struct linux_emuldata *emul = p->p_emuldata;
  347. if (emul->set_tls_base)
  348. i386_set_threadbase(p, emul->my_tls_base, TSEG_GS);
  349. if (emul->my_set_tid) {
  350. pid_t pid = p->p_pid + THREAD_PID_OFFSET;
  351. if (copyout(&pid, emul->my_set_tid, sizeof(pid)))
  352. psignal(p, SIGSEGV);
  353. }
  354. child_return(p);
  355. }