xref: /freebsd/sys/i386/linux/linux_machdep.c (revision 5a0bba9007c527b18db7f9b64f06b486cda4fe9d)
1 /*-
2  * Copyright (c) 2000 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/file.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mman.h>
40 #include <sys/mutex.h>
41 #include <sys/sx.h>
42 #include <sys/priv.h>
43 #include <sys/proc.h>
44 #include <sys/queue.h>
45 #include <sys/resource.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysproto.h>
50 #include <sys/unistd.h>
51 #include <sys/wait.h>
52 #include <sys/sched.h>
53 
54 #include <machine/frame.h>
55 #include <machine/psl.h>
56 #include <machine/segments.h>
57 #include <machine/sysarch.h>
58 
59 #include <vm/vm.h>
60 #include <vm/pmap.h>
61 #include <vm/vm_map.h>
62 
63 #include <i386/linux/linux.h>
64 #include <i386/linux/linux_proto.h>
65 #include <compat/linux/linux_ipc.h>
66 #include <compat/linux/linux_misc.h>
67 #include <compat/linux/linux_signal.h>
68 #include <compat/linux/linux_util.h>
69 #include <compat/linux/linux_emul.h>
70 
71 #include <i386/include/pcb.h>			/* needed for pcb definition in linux_set_thread_area */
72 
73 #include "opt_posix.h"
74 
75 extern struct sysentvec elf32_freebsd_sysvec;	/* defined in i386/i386/elf_machdep.c */
76 
77 struct l_descriptor {
78 	l_uint		entry_number;
79 	l_ulong		base_addr;
80 	l_uint		limit;
81 	l_uint		seg_32bit:1;
82 	l_uint		contents:2;
83 	l_uint		read_exec_only:1;
84 	l_uint		limit_in_pages:1;
85 	l_uint		seg_not_present:1;
86 	l_uint		useable:1;
87 };
88 
89 struct l_old_select_argv {
90 	l_int		nfds;
91 	l_fd_set	*readfds;
92 	l_fd_set	*writefds;
93 	l_fd_set	*exceptfds;
94 	struct l_timeval	*timeout;
95 };
96 
97 static int	linux_mmap_common(struct thread *td, l_uintptr_t addr,
98 		    l_size_t len, l_int prot, l_int flags, l_int fd,
99 		    l_loff_t pos);
100 
101 int
102 linux_to_bsd_sigaltstack(int lsa)
103 {
104 	int bsa = 0;
105 
106 	if (lsa & LINUX_SS_DISABLE)
107 		bsa |= SS_DISABLE;
108 	if (lsa & LINUX_SS_ONSTACK)
109 		bsa |= SS_ONSTACK;
110 	return (bsa);
111 }
112 
113 int
114 bsd_to_linux_sigaltstack(int bsa)
115 {
116 	int lsa = 0;
117 
118 	if (bsa & SS_DISABLE)
119 		lsa |= LINUX_SS_DISABLE;
120 	if (bsa & SS_ONSTACK)
121 		lsa |= LINUX_SS_ONSTACK;
122 	return (lsa);
123 }
124 
125 int
126 linux_execve(struct thread *td, struct linux_execve_args *args)
127 {
128 	int error;
129 	char *newpath;
130 	struct image_args eargs;
131 
132 	LCONVPATHEXIST(td, args->path, &newpath);
133 
134 #ifdef DEBUG
135 	if (ldebug(execve))
136 		printf(ARGS(execve, "%s"), newpath);
137 #endif
138 
139 	error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
140 	    args->argp, args->envp);
141 	free(newpath, M_TEMP);
142 	if (error == 0)
143 		error = kern_execve(td, &eargs, NULL);
144 	if (error == 0)
145 	   	/* linux process can exec fbsd one, dont attempt
146 		 * to create emuldata for such process using
147 		 * linux_proc_init, this leads to a panic on KASSERT
148 		 * because such process has p->p_emuldata == NULL
149 		 */
150 		if (SV_PROC_ABI(td->td_proc) == SV_ABI_LINUX)
151    			error = linux_proc_init(td, 0, 0);
152 	return (error);
153 }
154 
155 struct l_ipc_kludge {
156 	struct l_msgbuf *msgp;
157 	l_long msgtyp;
158 };
159 
160 int
161 linux_ipc(struct thread *td, struct linux_ipc_args *args)
162 {
163 
164 	switch (args->what & 0xFFFF) {
165 	case LINUX_SEMOP: {
166 		struct linux_semop_args a;
167 
168 		a.semid = args->arg1;
169 		a.tsops = args->ptr;
170 		a.nsops = args->arg2;
171 		return (linux_semop(td, &a));
172 	}
173 	case LINUX_SEMGET: {
174 		struct linux_semget_args a;
175 
176 		a.key = args->arg1;
177 		a.nsems = args->arg2;
178 		a.semflg = args->arg3;
179 		return (linux_semget(td, &a));
180 	}
181 	case LINUX_SEMCTL: {
182 		struct linux_semctl_args a;
183 		int error;
184 
185 		a.semid = args->arg1;
186 		a.semnum = args->arg2;
187 		a.cmd = args->arg3;
188 		error = copyin(args->ptr, &a.arg, sizeof(a.arg));
189 		if (error)
190 			return (error);
191 		return (linux_semctl(td, &a));
192 	}
193 	case LINUX_MSGSND: {
194 		struct linux_msgsnd_args a;
195 
196 		a.msqid = args->arg1;
197 		a.msgp = args->ptr;
198 		a.msgsz = args->arg2;
199 		a.msgflg = args->arg3;
200 		return (linux_msgsnd(td, &a));
201 	}
202 	case LINUX_MSGRCV: {
203 		struct linux_msgrcv_args a;
204 
205 		a.msqid = args->arg1;
206 		a.msgsz = args->arg2;
207 		a.msgflg = args->arg3;
208 		if ((args->what >> 16) == 0) {
209 			struct l_ipc_kludge tmp;
210 			int error;
211 
212 			if (args->ptr == NULL)
213 				return (EINVAL);
214 			error = copyin(args->ptr, &tmp, sizeof(tmp));
215 			if (error)
216 				return (error);
217 			a.msgp = tmp.msgp;
218 			a.msgtyp = tmp.msgtyp;
219 		} else {
220 			a.msgp = args->ptr;
221 			a.msgtyp = args->arg5;
222 		}
223 		return (linux_msgrcv(td, &a));
224 	}
225 	case LINUX_MSGGET: {
226 		struct linux_msgget_args a;
227 
228 		a.key = args->arg1;
229 		a.msgflg = args->arg2;
230 		return (linux_msgget(td, &a));
231 	}
232 	case LINUX_MSGCTL: {
233 		struct linux_msgctl_args a;
234 
235 		a.msqid = args->arg1;
236 		a.cmd = args->arg2;
237 		a.buf = args->ptr;
238 		return (linux_msgctl(td, &a));
239 	}
240 	case LINUX_SHMAT: {
241 		struct linux_shmat_args a;
242 
243 		a.shmid = args->arg1;
244 		a.shmaddr = args->ptr;
245 		a.shmflg = args->arg2;
246 		a.raddr = (l_ulong *)args->arg3;
247 		return (linux_shmat(td, &a));
248 	}
249 	case LINUX_SHMDT: {
250 		struct linux_shmdt_args a;
251 
252 		a.shmaddr = args->ptr;
253 		return (linux_shmdt(td, &a));
254 	}
255 	case LINUX_SHMGET: {
256 		struct linux_shmget_args a;
257 
258 		a.key = args->arg1;
259 		a.size = args->arg2;
260 		a.shmflg = args->arg3;
261 		return (linux_shmget(td, &a));
262 	}
263 	case LINUX_SHMCTL: {
264 		struct linux_shmctl_args a;
265 
266 		a.shmid = args->arg1;
267 		a.cmd = args->arg2;
268 		a.buf = args->ptr;
269 		return (linux_shmctl(td, &a));
270 	}
271 	default:
272 		break;
273 	}
274 
275 	return (EINVAL);
276 }
277 
278 int
279 linux_old_select(struct thread *td, struct linux_old_select_args *args)
280 {
281 	struct l_old_select_argv linux_args;
282 	struct linux_select_args newsel;
283 	int error;
284 
285 #ifdef DEBUG
286 	if (ldebug(old_select))
287 		printf(ARGS(old_select, "%p"), args->ptr);
288 #endif
289 
290 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
291 	if (error)
292 		return (error);
293 
294 	newsel.nfds = linux_args.nfds;
295 	newsel.readfds = linux_args.readfds;
296 	newsel.writefds = linux_args.writefds;
297 	newsel.exceptfds = linux_args.exceptfds;
298 	newsel.timeout = linux_args.timeout;
299 	return (linux_select(td, &newsel));
300 }
301 
302 int
303 linux_fork(struct thread *td, struct linux_fork_args *args)
304 {
305 	int error;
306 	struct proc *p2;
307 	struct thread *td2;
308 
309 #ifdef DEBUG
310 	if (ldebug(fork))
311 		printf(ARGS(fork, ""));
312 #endif
313 
314 	if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0)
315 		return (error);
316 
317 	if (error == 0) {
318 		td->td_retval[0] = p2->p_pid;
319 		td->td_retval[1] = 0;
320 	}
321 
322 	if (td->td_retval[1] == 1)
323 		td->td_retval[0] = 0;
324 	error = linux_proc_init(td, td->td_retval[0], 0);
325 	if (error)
326 		return (error);
327 
328 	td2 = FIRST_THREAD_IN_PROC(p2);
329 
330 	/*
331 	 * Make this runnable after we are finished with it.
332 	 */
333 	thread_lock(td2);
334 	TD_SET_CAN_RUN(td2);
335 	sched_add(td2, SRQ_BORING);
336 	thread_unlock(td2);
337 
338 	return (0);
339 }
340 
341 int
342 linux_vfork(struct thread *td, struct linux_vfork_args *args)
343 {
344 	int error;
345 	struct proc *p2;
346 	struct thread *td2;
347 
348 #ifdef DEBUG
349 	if (ldebug(vfork))
350 		printf(ARGS(vfork, ""));
351 #endif
352 
353 	/* exclude RFPPWAIT */
354 	if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0)
355 		return (error);
356 	if (error == 0) {
357 		td->td_retval[0] = p2->p_pid;
358 		td->td_retval[1] = 0;
359 	}
360 	/* Are we the child? */
361 	if (td->td_retval[1] == 1)
362 		td->td_retval[0] = 0;
363 	error = linux_proc_init(td, td->td_retval[0], 0);
364 	if (error)
365 		return (error);
366 
367 	PROC_LOCK(p2);
368 	p2->p_flag |= P_PPWAIT;
369 	PROC_UNLOCK(p2);
370 
371 	td2 = FIRST_THREAD_IN_PROC(p2);
372 
373 	/*
374 	 * Make this runnable after we are finished with it.
375 	 */
376 	thread_lock(td2);
377 	TD_SET_CAN_RUN(td2);
378 	sched_add(td2, SRQ_BORING);
379 	thread_unlock(td2);
380 
381 	/* wait for the children to exit, ie. emulate vfork */
382 	PROC_LOCK(p2);
383 	while (p2->p_flag & P_PPWAIT)
384 		cv_wait(&p2->p_pwait, &p2->p_mtx);
385 	PROC_UNLOCK(p2);
386 
387 	return (0);
388 }
389 
390 int
391 linux_clone(struct thread *td, struct linux_clone_args *args)
392 {
393 	int error, ff = RFPROC | RFSTOPPED;
394 	struct proc *p2;
395 	struct thread *td2;
396 	int exit_signal;
397 	struct linux_emuldata *em;
398 
399 #ifdef DEBUG
400 	if (ldebug(clone)) {
401    	   	printf(ARGS(clone, "flags %x, stack %x, parent tid: %x, child tid: %x"),
402 		    (unsigned int)args->flags, (unsigned int)args->stack,
403 		    (unsigned int)args->parent_tidptr, (unsigned int)args->child_tidptr);
404 	}
405 #endif
406 
407 	exit_signal = args->flags & 0x000000ff;
408 	if (LINUX_SIG_VALID(exit_signal)) {
409 		if (exit_signal <= LINUX_SIGTBLSZ)
410 			exit_signal =
411 			    linux_to_bsd_signal[_SIG_IDX(exit_signal)];
412 	} else if (exit_signal != 0)
413 		return (EINVAL);
414 
415 	if (args->flags & LINUX_CLONE_VM)
416 		ff |= RFMEM;
417 	if (args->flags & LINUX_CLONE_SIGHAND)
418 		ff |= RFSIGSHARE;
419 	/*
420 	 * XXX: in linux sharing of fs info (chroot/cwd/umask)
421 	 * and open files is independant. in fbsd its in one
422 	 * structure but in reality it doesn't cause any problems
423 	 * because both of these flags are usually set together.
424 	 */
425 	if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
426 		ff |= RFFDG;
427 
428 	/*
429 	 * Attempt to detect when linux_clone(2) is used for creating
430 	 * kernel threads. Unfortunately despite the existence of the
431 	 * CLONE_THREAD flag, version of linuxthreads package used in
432 	 * most popular distros as of beginning of 2005 doesn't make
433 	 * any use of it. Therefore, this detection relies on
434 	 * empirical observation that linuxthreads sets certain
435 	 * combination of flags, so that we can make more or less
436 	 * precise detection and notify the FreeBSD kernel that several
437 	 * processes are in fact part of the same threading group, so
438 	 * that special treatment is necessary for signal delivery
439 	 * between those processes and fd locking.
440 	 */
441 	if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
442 		ff |= RFTHREAD;
443 
444 	if (args->flags & LINUX_CLONE_PARENT_SETTID)
445 		if (args->parent_tidptr == NULL)
446 			return (EINVAL);
447 
448 	error = fork1(td, ff, 0, &p2);
449 	if (error)
450 		return (error);
451 
452 	if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
453 	   	sx_xlock(&proctree_lock);
454 		PROC_LOCK(p2);
455 		proc_reparent(p2, td->td_proc->p_pptr);
456 		PROC_UNLOCK(p2);
457 		sx_xunlock(&proctree_lock);
458 	}
459 
460 	/* create the emuldata */
461 	error = linux_proc_init(td, p2->p_pid, args->flags);
462 	/* reference it - no need to check this */
463 	em = em_find(p2, EMUL_DOLOCK);
464 	KASSERT(em != NULL, ("clone: emuldata not found.\n"));
465 	/* and adjust it */
466 
467 	if (args->flags & LINUX_CLONE_THREAD) {
468 	   	/* XXX: linux mangles pgrp and pptr somehow
469 		 * I think it might be this but I am not sure.
470 		 */
471 #ifdef notyet
472 	   	PROC_LOCK(p2);
473 	   	p2->p_pgrp = td->td_proc->p_pgrp;
474 	   	PROC_UNLOCK(p2);
475 #endif
476 	 	exit_signal = 0;
477 	}
478 
479 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
480 		em->child_set_tid = args->child_tidptr;
481 	else
482 	   	em->child_set_tid = NULL;
483 
484 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
485 		em->child_clear_tid = args->child_tidptr;
486 	else
487 	   	em->child_clear_tid = NULL;
488 
489 	EMUL_UNLOCK(&emul_lock);
490 
491 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
492 		error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid));
493 		if (error)
494 			printf(LMSG("copyout failed!"));
495 	}
496 
497 	PROC_LOCK(p2);
498 	p2->p_sigparent = exit_signal;
499 	PROC_UNLOCK(p2);
500 	td2 = FIRST_THREAD_IN_PROC(p2);
501 	/*
502 	 * in a case of stack = NULL we are supposed to COW calling process stack
503 	 * this is what normal fork() does so we just keep the tf_esp arg intact
504 	 */
505 	if (args->stack)
506    	   	td2->td_frame->tf_esp = (unsigned int)args->stack;
507 
508 	if (args->flags & LINUX_CLONE_SETTLS) {
509    	   	struct l_user_desc info;
510    	   	int idx;
511 	   	int a[2];
512 		struct segment_descriptor sd;
513 
514 	   	error = copyin((void *)td->td_frame->tf_esi, &info, sizeof(struct l_user_desc));
515 		if (error) {
516 			printf(LMSG("copyin failed!"));
517 		} else {
518 
519 			idx = info.entry_number;
520 
521 			/*
522 			 * looks like we're getting the idx we returned
523 			 * in the set_thread_area() syscall
524 			 */
525 			if (idx != 6 && idx != 3) {
526 				printf(LMSG("resetting idx!"));
527 				idx = 3;
528 			}
529 
530 			/* this doesnt happen in practice */
531 			if (idx == 6) {
532 		   		/* we might copy out the entry_number as 3 */
533 			   	info.entry_number = 3;
534 				error = copyout(&info, (void *) td->td_frame->tf_esi, sizeof(struct l_user_desc));
535 				if (error)
536 					printf(LMSG("copyout failed!"));
537 			}
538 
539 			a[0] = LINUX_LDT_entry_a(&info);
540 			a[1] = LINUX_LDT_entry_b(&info);
541 
542 			memcpy(&sd, &a, sizeof(a));
543 #ifdef DEBUG
544 		if (ldebug(clone))
545 		   	printf("Segment created in clone with CLONE_SETTLS: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
546 			sd.sd_hibase,
547 			sd.sd_lolimit,
548 			sd.sd_hilimit,
549 			sd.sd_type,
550 			sd.sd_dpl,
551 			sd.sd_p,
552 			sd.sd_xx,
553 			sd.sd_def32,
554 			sd.sd_gran);
555 #endif
556 
557 			/* set %gs */
558 			td2->td_pcb->pcb_gsd = sd;
559 			td2->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
560 		}
561 	}
562 
563 #ifdef DEBUG
564 	if (ldebug(clone))
565 		printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
566 		    (long)p2->p_pid, args->stack, exit_signal);
567 #endif
568 	if (args->flags & LINUX_CLONE_VFORK) {
569 	   	PROC_LOCK(p2);
570 		p2->p_flag |= P_PPWAIT;
571 	   	PROC_UNLOCK(p2);
572 	}
573 
574 	/*
575 	 * Make this runnable after we are finished with it.
576 	 */
577 	thread_lock(td2);
578 	TD_SET_CAN_RUN(td2);
579 	sched_add(td2, SRQ_BORING);
580 	thread_unlock(td2);
581 
582 	td->td_retval[0] = p2->p_pid;
583 	td->td_retval[1] = 0;
584 
585 	if (args->flags & LINUX_CLONE_VFORK) {
586    	   	/* wait for the children to exit, ie. emulate vfork */
587    	   	PROC_LOCK(p2);
588 		while (p2->p_flag & P_PPWAIT)
589 			cv_wait(&p2->p_pwait, &p2->p_mtx);
590 		PROC_UNLOCK(p2);
591 	}
592 
593 	return (0);
594 }
595 
596 #define STACK_SIZE  (2 * 1024 * 1024)
597 #define GUARD_SIZE  (4 * PAGE_SIZE)
598 
599 int
600 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
601 {
602 
603 #ifdef DEBUG
604 	if (ldebug(mmap2))
605 		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
606 		    (void *)args->addr, args->len, args->prot,
607 		    args->flags, args->fd, args->pgoff);
608 #endif
609 
610 	return (linux_mmap_common(td, args->addr, args->len, args->prot,
611 		args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
612 		PAGE_SIZE));
613 }
614 
615 int
616 linux_mmap(struct thread *td, struct linux_mmap_args *args)
617 {
618 	int error;
619 	struct l_mmap_argv linux_args;
620 
621 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
622 	if (error)
623 		return (error);
624 
625 #ifdef DEBUG
626 	if (ldebug(mmap))
627 		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
628 		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
629 		    linux_args.flags, linux_args.fd, linux_args.pgoff);
630 #endif
631 
632 	return (linux_mmap_common(td, linux_args.addr, linux_args.len,
633 	    linux_args.prot, linux_args.flags, linux_args.fd,
634 	    (uint32_t)linux_args.pgoff));
635 }
636 
637 static int
638 linux_mmap_common(struct thread *td, l_uintptr_t addr, l_size_t len, l_int prot,
639     l_int flags, l_int fd, l_loff_t pos)
640 {
641 	struct proc *p = td->td_proc;
642 	struct mmap_args /* {
643 		caddr_t addr;
644 		size_t len;
645 		int prot;
646 		int flags;
647 		int fd;
648 		long pad;
649 		off_t pos;
650 	} */ bsd_args;
651 	int error;
652 	struct file *fp;
653 
654 	error = 0;
655 	bsd_args.flags = 0;
656 	fp = NULL;
657 
658 	/*
659 	 * Linux mmap(2):
660 	 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
661 	 */
662 	if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
663 		return (EINVAL);
664 
665 	if (flags & LINUX_MAP_SHARED)
666 		bsd_args.flags |= MAP_SHARED;
667 	if (flags & LINUX_MAP_PRIVATE)
668 		bsd_args.flags |= MAP_PRIVATE;
669 	if (flags & LINUX_MAP_FIXED)
670 		bsd_args.flags |= MAP_FIXED;
671 	if (flags & LINUX_MAP_ANON) {
672 		/* Enforce pos to be on page boundary, then ignore. */
673 		if ((pos & PAGE_MASK) != 0)
674 			return (EINVAL);
675 		pos = 0;
676 		bsd_args.flags |= MAP_ANON;
677 	} else
678 		bsd_args.flags |= MAP_NOSYNC;
679 	if (flags & LINUX_MAP_GROWSDOWN)
680 		bsd_args.flags |= MAP_STACK;
681 
682 	/*
683 	 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
684 	 * on Linux/i386. We do this to ensure maximum compatibility.
685 	 * Linux/ia64 does the same in i386 emulation mode.
686 	 */
687 	bsd_args.prot = prot;
688 	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
689 		bsd_args.prot |= PROT_READ | PROT_EXEC;
690 
691 	/* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
692 	bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : fd;
693 	if (bsd_args.fd != -1) {
694 		/*
695 		 * Linux follows Solaris mmap(2) description:
696 		 * The file descriptor fildes is opened with
697 		 * read permission, regardless of the
698 		 * protection options specified.
699 		 */
700 
701 		if ((error = fget(td, bsd_args.fd, &fp)) != 0)
702 			return (error);
703 		if (fp->f_type != DTYPE_VNODE) {
704 			fdrop(fp, td);
705 			return (EINVAL);
706 		}
707 
708 		/* Linux mmap() just fails for O_WRONLY files */
709 		if (!(fp->f_flag & FREAD)) {
710 			fdrop(fp, td);
711 			return (EACCES);
712 		}
713 
714 		fdrop(fp, td);
715 	}
716 
717 	if (flags & LINUX_MAP_GROWSDOWN) {
718 		/*
719 		 * The Linux MAP_GROWSDOWN option does not limit auto
720 		 * growth of the region.  Linux mmap with this option
721 		 * takes as addr the inital BOS, and as len, the initial
722 		 * region size.  It can then grow down from addr without
723 		 * limit.  However, linux threads has an implicit internal
724 		 * limit to stack size of STACK_SIZE.  Its just not
725 		 * enforced explicitly in linux.  But, here we impose
726 		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
727 		 * region, since we can do this with our mmap.
728 		 *
729 		 * Our mmap with MAP_STACK takes addr as the maximum
730 		 * downsize limit on BOS, and as len the max size of
731 		 * the region.  It them maps the top SGROWSIZ bytes,
732 		 * and auto grows the region down, up to the limit
733 		 * in addr.
734 		 *
735 		 * If we don't use the MAP_STACK option, the effect
736 		 * of this code is to allocate a stack region of a
737 		 * fixed size of (STACK_SIZE - GUARD_SIZE).
738 		 */
739 
740 		if ((caddr_t)PTRIN(addr) + len > p->p_vmspace->vm_maxsaddr) {
741 			/*
742 			 * Some linux apps will attempt to mmap
743 			 * thread stacks near the top of their
744 			 * address space.  If their TOS is greater
745 			 * than vm_maxsaddr, vm_map_growstack()
746 			 * will confuse the thread stack with the
747 			 * process stack and deliver a SEGV if they
748 			 * attempt to grow the thread stack past their
749 			 * current stacksize rlimit.  To avoid this,
750 			 * adjust vm_maxsaddr upwards to reflect
751 			 * the current stacksize rlimit rather
752 			 * than the maximum possible stacksize.
753 			 * It would be better to adjust the
754 			 * mmap'ed region, but some apps do not check
755 			 * mmap's return value.
756 			 */
757 			PROC_LOCK(p);
758 			p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
759 			    lim_cur(p, RLIMIT_STACK);
760 			PROC_UNLOCK(p);
761 		}
762 
763 		/*
764 		 * This gives us our maximum stack size and a new BOS.
765 		 * If we're using VM_STACK, then mmap will just map
766 		 * the top SGROWSIZ bytes, and let the stack grow down
767 		 * to the limit at BOS.  If we're not using VM_STACK
768 		 * we map the full stack, since we don't have a way
769 		 * to autogrow it.
770 		 */
771 		if (len > STACK_SIZE - GUARD_SIZE) {
772 			bsd_args.addr = (caddr_t)PTRIN(addr);
773 			bsd_args.len = len;
774 		} else {
775 			bsd_args.addr = (caddr_t)PTRIN(addr) -
776 			    (STACK_SIZE - GUARD_SIZE - len);
777 			bsd_args.len = STACK_SIZE - GUARD_SIZE;
778 		}
779 	} else {
780 		bsd_args.addr = (caddr_t)PTRIN(addr);
781 		bsd_args.len  = len;
782 	}
783 	bsd_args.pos = pos;
784 
785 #ifdef DEBUG
786 	if (ldebug(mmap))
787 		printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
788 		    __func__,
789 		    (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
790 		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
791 #endif
792 	error = mmap(td, &bsd_args);
793 #ifdef DEBUG
794 	if (ldebug(mmap))
795 		printf("-> %s() return: 0x%x (0x%08x)\n",
796 			__func__, error, (u_int)td->td_retval[0]);
797 #endif
798 	return (error);
799 }
800 
801 int
802 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
803 {
804 	struct mprotect_args bsd_args;
805 
806 	bsd_args.addr = uap->addr;
807 	bsd_args.len = uap->len;
808 	bsd_args.prot = uap->prot;
809 	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
810 		bsd_args.prot |= PROT_READ | PROT_EXEC;
811 	return (mprotect(td, &bsd_args));
812 }
813 
814 int
815 linux_pipe(struct thread *td, struct linux_pipe_args *args)
816 {
817 	int error;
818 	int fildes[2];
819 
820 #ifdef DEBUG
821 	if (ldebug(pipe))
822 		printf(ARGS(pipe, "*"));
823 #endif
824 
825 	error = kern_pipe(td, fildes);
826 	if (error)
827 		return (error);
828 
829 	/* XXX: Close descriptors on error. */
830 	return (copyout(fildes, args->pipefds, sizeof fildes));
831 }
832 
833 int
834 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
835 {
836 	int error;
837 	struct i386_ioperm_args iia;
838 
839 	iia.start = args->start;
840 	iia.length = args->length;
841 	iia.enable = args->enable;
842 	error = i386_set_ioperm(td, &iia);
843 	return (error);
844 }
845 
846 int
847 linux_iopl(struct thread *td, struct linux_iopl_args *args)
848 {
849 	int error;
850 
851 	if (args->level < 0 || args->level > 3)
852 		return (EINVAL);
853 	if ((error = priv_check(td, PRIV_IO)) != 0)
854 		return (error);
855 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
856 		return (error);
857 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
858 	    (args->level * (PSL_IOPL / 3));
859 	return (0);
860 }
861 
862 int
863 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
864 {
865 	int error;
866 	struct i386_ldt_args ldt;
867 	struct l_descriptor ld;
868 	union descriptor desc;
869 	int size, written;
870 
871 	switch (uap->func) {
872 	case 0x00: /* read_ldt */
873 		ldt.start = 0;
874 		ldt.descs = uap->ptr;
875 		ldt.num = uap->bytecount / sizeof(union descriptor);
876 		error = i386_get_ldt(td, &ldt);
877 		td->td_retval[0] *= sizeof(union descriptor);
878 		break;
879 	case 0x02: /* read_default_ldt = 0 */
880 		size = 5*sizeof(struct l_desc_struct);
881 		if (size > uap->bytecount)
882 			size = uap->bytecount;
883 		for (written = error = 0; written < size && error == 0; written++)
884 			error = subyte((char *)uap->ptr + written, 0);
885 		td->td_retval[0] = written;
886 		break;
887 	case 0x01: /* write_ldt */
888 	case 0x11: /* write_ldt */
889 		if (uap->bytecount != sizeof(ld))
890 			return (EINVAL);
891 
892 		error = copyin(uap->ptr, &ld, sizeof(ld));
893 		if (error)
894 			return (error);
895 
896 		ldt.start = ld.entry_number;
897 		ldt.descs = &desc;
898 		ldt.num = 1;
899 		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
900 		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
901 		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
902 		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
903 		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
904 			(ld.contents << 2);
905 		desc.sd.sd_dpl = 3;
906 		desc.sd.sd_p = (ld.seg_not_present ^ 1);
907 		desc.sd.sd_xx = 0;
908 		desc.sd.sd_def32 = ld.seg_32bit;
909 		desc.sd.sd_gran = ld.limit_in_pages;
910 		error = i386_set_ldt(td, &ldt, &desc);
911 		break;
912 	default:
913 		error = ENOSYS;
914 		break;
915 	}
916 
917 	if (error == EOPNOTSUPP) {
918 		printf("linux: modify_ldt needs kernel option USER_LDT\n");
919 		error = ENOSYS;
920 	}
921 
922 	return (error);
923 }
924 
925 int
926 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
927 {
928 	l_osigaction_t osa;
929 	l_sigaction_t act, oact;
930 	int error;
931 
932 #ifdef DEBUG
933 	if (ldebug(sigaction))
934 		printf(ARGS(sigaction, "%d, %p, %p"),
935 		    args->sig, (void *)args->nsa, (void *)args->osa);
936 #endif
937 
938 	if (args->nsa != NULL) {
939 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
940 		if (error)
941 			return (error);
942 		act.lsa_handler = osa.lsa_handler;
943 		act.lsa_flags = osa.lsa_flags;
944 		act.lsa_restorer = osa.lsa_restorer;
945 		LINUX_SIGEMPTYSET(act.lsa_mask);
946 		act.lsa_mask.__bits[0] = osa.lsa_mask;
947 	}
948 
949 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
950 	    args->osa ? &oact : NULL);
951 
952 	if (args->osa != NULL && !error) {
953 		osa.lsa_handler = oact.lsa_handler;
954 		osa.lsa_flags = oact.lsa_flags;
955 		osa.lsa_restorer = oact.lsa_restorer;
956 		osa.lsa_mask = oact.lsa_mask.__bits[0];
957 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
958 	}
959 
960 	return (error);
961 }
962 
963 /*
964  * Linux has two extra args, restart and oldmask.  We dont use these,
965  * but it seems that "restart" is actually a context pointer that
966  * enables the signal to happen with a different register set.
967  */
968 int
969 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
970 {
971 	sigset_t sigmask;
972 	l_sigset_t mask;
973 
974 #ifdef DEBUG
975 	if (ldebug(sigsuspend))
976 		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
977 #endif
978 
979 	LINUX_SIGEMPTYSET(mask);
980 	mask.__bits[0] = args->mask;
981 	linux_to_bsd_sigset(&mask, &sigmask);
982 	return (kern_sigsuspend(td, sigmask));
983 }
984 
985 int
986 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
987 {
988 	l_sigset_t lmask;
989 	sigset_t sigmask;
990 	int error;
991 
992 #ifdef DEBUG
993 	if (ldebug(rt_sigsuspend))
994 		printf(ARGS(rt_sigsuspend, "%p, %d"),
995 		    (void *)uap->newset, uap->sigsetsize);
996 #endif
997 
998 	if (uap->sigsetsize != sizeof(l_sigset_t))
999 		return (EINVAL);
1000 
1001 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
1002 	if (error)
1003 		return (error);
1004 
1005 	linux_to_bsd_sigset(&lmask, &sigmask);
1006 	return (kern_sigsuspend(td, sigmask));
1007 }
1008 
1009 int
1010 linux_pause(struct thread *td, struct linux_pause_args *args)
1011 {
1012 	struct proc *p = td->td_proc;
1013 	sigset_t sigmask;
1014 
1015 #ifdef DEBUG
1016 	if (ldebug(pause))
1017 		printf(ARGS(pause, ""));
1018 #endif
1019 
1020 	PROC_LOCK(p);
1021 	sigmask = td->td_sigmask;
1022 	PROC_UNLOCK(p);
1023 	return (kern_sigsuspend(td, sigmask));
1024 }
1025 
1026 int
1027 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
1028 {
1029 	stack_t ss, oss;
1030 	l_stack_t lss;
1031 	int error;
1032 
1033 #ifdef DEBUG
1034 	if (ldebug(sigaltstack))
1035 		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1036 #endif
1037 
1038 	if (uap->uss != NULL) {
1039 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
1040 		if (error)
1041 			return (error);
1042 
1043 		ss.ss_sp = lss.ss_sp;
1044 		ss.ss_size = lss.ss_size;
1045 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
1046 	}
1047 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
1048 	    (uap->uoss != NULL) ? &oss : NULL);
1049 	if (!error && uap->uoss != NULL) {
1050 		lss.ss_sp = oss.ss_sp;
1051 		lss.ss_size = oss.ss_size;
1052 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1053 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
1054 	}
1055 
1056 	return (error);
1057 }
1058 
1059 int
1060 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
1061 {
1062 	struct ftruncate_args sa;
1063 
1064 #ifdef DEBUG
1065 	if (ldebug(ftruncate64))
1066 		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
1067 		    (intmax_t)args->length);
1068 #endif
1069 
1070 	sa.fd = args->fd;
1071 	sa.length = args->length;
1072 	return ftruncate(td, &sa);
1073 }
1074 
1075 int
1076 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
1077 {
1078 	struct l_user_desc info;
1079 	int error;
1080 	int idx;
1081 	int a[2];
1082 	struct segment_descriptor sd;
1083 
1084 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1085 	if (error)
1086 		return (error);
1087 
1088 #ifdef DEBUG
1089 	if (ldebug(set_thread_area))
1090 	   	printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
1091 		      info.entry_number,
1092       		      info.base_addr,
1093       		      info.limit,
1094       		      info.seg_32bit,
1095 		      info.contents,
1096       		      info.read_exec_only,
1097       		      info.limit_in_pages,
1098       		      info.seg_not_present,
1099       		      info.useable);
1100 #endif
1101 
1102 	idx = info.entry_number;
1103 	/*
1104 	 * Semantics of linux version: every thread in the system has array of
1105 	 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
1106 	 * syscall loads one of the selected tls decriptors with a value and
1107 	 * also loads GDT descriptors 6, 7 and 8 with the content of the
1108 	 * per-thread descriptors.
1109 	 *
1110 	 * Semantics of fbsd version: I think we can ignore that linux has 3
1111 	 * per-thread descriptors and use just the 1st one. The tls_array[]
1112 	 * is used only in set/get-thread_area() syscalls and for loading the
1113 	 * GDT descriptors. In fbsd we use just one GDT descriptor for TLS so
1114 	 * we will load just one.
1115 	 *
1116 	 * XXX: this doesn't work when a user space process tries to use more
1117 	 * than 1 TLS segment. Comment in the linux sources says wine might do
1118 	 * this.
1119 	 */
1120 
1121 	/*
1122 	 * we support just GLIBC TLS now
1123 	 * we should let 3 proceed as well because we use this segment so
1124 	 * if code does two subsequent calls it should succeed
1125 	 */
1126 	if (idx != 6 && idx != -1 && idx != 3)
1127 		return (EINVAL);
1128 
1129 	/*
1130 	 * we have to copy out the GDT entry we use
1131 	 * FreeBSD uses GDT entry #3 for storing %gs so load that
1132 	 *
1133 	 * XXX: what if a user space program doesn't check this value and tries
1134 	 * to use 6, 7 or 8?
1135 	 */
1136 	idx = info.entry_number = 3;
1137 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1138 	if (error)
1139 		return (error);
1140 
1141 	if (LINUX_LDT_empty(&info)) {
1142 		a[0] = 0;
1143 		a[1] = 0;
1144 	} else {
1145 		a[0] = LINUX_LDT_entry_a(&info);
1146 		a[1] = LINUX_LDT_entry_b(&info);
1147 	}
1148 
1149 	memcpy(&sd, &a, sizeof(a));
1150 #ifdef DEBUG
1151 	if (ldebug(set_thread_area))
1152 	   	printf("Segment created in set_thread_area: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
1153 			sd.sd_hibase,
1154 			sd.sd_lolimit,
1155 			sd.sd_hilimit,
1156 			sd.sd_type,
1157 			sd.sd_dpl,
1158 			sd.sd_p,
1159 			sd.sd_xx,
1160 			sd.sd_def32,
1161 			sd.sd_gran);
1162 #endif
1163 
1164 	/* this is taken from i386 version of cpu_set_user_tls() */
1165 	critical_enter();
1166 	/* set %gs */
1167 	td->td_pcb->pcb_gsd = sd;
1168 	PCPU_GET(fsgs_gdt)[1] = sd;
1169 	load_gs(GSEL(GUGS_SEL, SEL_UPL));
1170 	critical_exit();
1171 
1172 	return (0);
1173 }
1174 
1175 int
1176 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
1177 {
1178 
1179 	struct l_user_desc info;
1180 	int error;
1181 	int idx;
1182 	struct l_desc_struct desc;
1183 	struct segment_descriptor sd;
1184 
1185 #ifdef DEBUG
1186 	if (ldebug(get_thread_area))
1187 		printf(ARGS(get_thread_area, "%p"), args->desc);
1188 #endif
1189 
1190 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1191 	if (error)
1192 		return (error);
1193 
1194 	idx = info.entry_number;
1195 	/* XXX: I am not sure if we want 3 to be allowed too. */
1196 	if (idx != 6 && idx != 3)
1197 		return (EINVAL);
1198 
1199 	idx = 3;
1200 
1201 	memset(&info, 0, sizeof(info));
1202 
1203 	sd = PCPU_GET(fsgs_gdt)[1];
1204 
1205 	memcpy(&desc, &sd, sizeof(desc));
1206 
1207 	info.entry_number = idx;
1208 	info.base_addr = LINUX_GET_BASE(&desc);
1209 	info.limit = LINUX_GET_LIMIT(&desc);
1210 	info.seg_32bit = LINUX_GET_32BIT(&desc);
1211 	info.contents = LINUX_GET_CONTENTS(&desc);
1212 	info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
1213 	info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
1214 	info.seg_not_present = !LINUX_GET_PRESENT(&desc);
1215 	info.useable = LINUX_GET_USEABLE(&desc);
1216 
1217 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1218 	if (error)
1219 	   	return (EFAULT);
1220 
1221 	return (0);
1222 }
1223 
1224 /* copied from kern/kern_time.c */
1225 int
1226 linux_timer_create(struct thread *td, struct linux_timer_create_args *args)
1227 {
1228    	return ktimer_create(td, (struct ktimer_create_args *) args);
1229 }
1230 
1231 int
1232 linux_timer_settime(struct thread *td, struct linux_timer_settime_args *args)
1233 {
1234    	return ktimer_settime(td, (struct ktimer_settime_args *) args);
1235 }
1236 
1237 int
1238 linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *args)
1239 {
1240    	return ktimer_gettime(td, (struct ktimer_gettime_args *) args);
1241 }
1242 
1243 int
1244 linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *args)
1245 {
1246    	return ktimer_getoverrun(td, (struct ktimer_getoverrun_args *) args);
1247 }
1248 
1249 int
1250 linux_timer_delete(struct thread *td, struct linux_timer_delete_args *args)
1251 {
1252    	return ktimer_delete(td, (struct ktimer_delete_args *) args);
1253 }
1254 
1255 /* XXX: this wont work with module - convert it */
1256 int
1257 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
1258 {
1259 #ifdef P1003_1B_MQUEUE
1260    	return kmq_open(td, (struct kmq_open_args *) args);
1261 #else
1262 	return (ENOSYS);
1263 #endif
1264 }
1265 
1266 int
1267 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
1268 {
1269 #ifdef P1003_1B_MQUEUE
1270    	return kmq_unlink(td, (struct kmq_unlink_args *) args);
1271 #else
1272 	return (ENOSYS);
1273 #endif
1274 }
1275 
1276 int
1277 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
1278 {
1279 #ifdef P1003_1B_MQUEUE
1280    	return kmq_timedsend(td, (struct kmq_timedsend_args *) args);
1281 #else
1282 	return (ENOSYS);
1283 #endif
1284 }
1285 
1286 int
1287 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
1288 {
1289 #ifdef P1003_1B_MQUEUE
1290    	return kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
1291 #else
1292 	return (ENOSYS);
1293 #endif
1294 }
1295 
1296 int
1297 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
1298 {
1299 #ifdef P1003_1B_MQUEUE
1300 	return kmq_notify(td, (struct kmq_notify_args *) args);
1301 #else
1302 	return (ENOSYS);
1303 #endif
1304 }
1305 
1306 int
1307 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
1308 {
1309 #ifdef P1003_1B_MQUEUE
1310    	return kmq_setattr(td, (struct kmq_setattr_args *) args);
1311 #else
1312 	return (ENOSYS);
1313 #endif
1314 }
1315 
1316 int
1317 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1318 {
1319 	int error, options;
1320 	struct rusage ru, *rup;
1321 
1322 #ifdef DEBUG
1323 	if (ldebug(wait4))
1324 		printf(ARGS(wait4, "%d, %p, %d, %p"),
1325 		    args->pid, (void *)args->status, args->options,
1326 		    (void *)args->rusage);
1327 #endif
1328 
1329 	options = (args->options & (WNOHANG | WUNTRACED));
1330 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1331 	if (args->options & __WCLONE)
1332 		options |= WLINUXCLONE;
1333 
1334 	if (args->rusage != NULL)
1335 		rup = &ru;
1336 	else
1337 		rup = NULL;
1338 	error = linux_common_wait(td, args->pid, args->status, options, rup);
1339 	if (error)
1340 		return (error);
1341 	if (args->rusage != NULL)
1342 		error = copyout(&ru, args->rusage, sizeof(ru));
1343 
1344 	return (error);
1345 }
1346