xref: /freebsd/sys/i386/linux/linux_machdep.c (revision 2c7660ba3e67c64161b4c77b403534028f0faf6f)
1146aad74SMarcel Moolenaar /*-
2146aad74SMarcel Moolenaar  * Copyright (c) 2000 Marcel Moolenaar
3146aad74SMarcel Moolenaar  * All rights reserved.
4146aad74SMarcel Moolenaar  *
5146aad74SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
6146aad74SMarcel Moolenaar  * modification, are permitted provided that the following conditions
7146aad74SMarcel Moolenaar  * are met:
8146aad74SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
9146aad74SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer
10146aad74SMarcel Moolenaar  *    in this position and unchanged.
11146aad74SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
12146aad74SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
13146aad74SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
14146aad74SMarcel Moolenaar  * 3. The name of the author may not be used to endorse or promote products
15bc34729cSMarcel Moolenaar  *    derived from this software without specific prior written permission.
16146aad74SMarcel Moolenaar  *
17146aad74SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18146aad74SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19146aad74SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20146aad74SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21146aad74SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22146aad74SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23146aad74SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24146aad74SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25146aad74SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26146aad74SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27146aad74SMarcel Moolenaar  */
28146aad74SMarcel Moolenaar 
2927e0099cSDavid E. O'Brien #include <sys/cdefs.h>
3027e0099cSDavid E. O'Brien __FBSDID("$FreeBSD$");
3127e0099cSDavid E. O'Brien 
32146aad74SMarcel Moolenaar #include <sys/param.h>
33146aad74SMarcel Moolenaar #include <sys/systm.h>
34a312f6a3SAlexander Leidinger #include <sys/file.h>
35a312f6a3SAlexander Leidinger #include <sys/fcntl.h>
36610ecfe0SMaxim Sobolev #include <sys/imgact.h>
377106ca0dSJohn Baldwin #include <sys/lock.h>
38610ecfe0SMaxim Sobolev #include <sys/malloc.h>
39fb919e4dSMark Murray #include <sys/mman.h>
40fb919e4dSMark Murray #include <sys/mutex.h>
419b44bfc5SAlexander Leidinger #include <sys/sx.h>
42acd3428bSRobert Watson #include <sys/priv.h>
43fb919e4dSMark Murray #include <sys/proc.h>
449b44bfc5SAlexander Leidinger #include <sys/queue.h>
45242fae60SAndrew Gallatin #include <sys/resource.h>
46242fae60SAndrew Gallatin #include <sys/resourcevar.h>
471bc85c0dSDoug Rabson #include <sys/signalvar.h>
48206a5d3aSIan Dowse #include <sys/syscallsubr.h>
49fb919e4dSMark Murray #include <sys/sysproto.h>
50fb919e4dSMark Murray #include <sys/unistd.h>
519b44bfc5SAlexander Leidinger #include <sys/wait.h>
52f0393f06SJeff Roberson #include <sys/sched.h>
53146aad74SMarcel Moolenaar 
54146aad74SMarcel Moolenaar #include <machine/frame.h>
55146aad74SMarcel Moolenaar #include <machine/psl.h>
56146aad74SMarcel Moolenaar #include <machine/segments.h>
57146aad74SMarcel Moolenaar #include <machine/sysarch.h>
58146aad74SMarcel Moolenaar 
59242fae60SAndrew Gallatin #include <vm/vm.h>
60242fae60SAndrew Gallatin #include <vm/pmap.h>
61242fae60SAndrew Gallatin #include <vm/vm_map.h>
62242fae60SAndrew Gallatin 
63146aad74SMarcel Moolenaar #include <i386/linux/linux.h>
64ebea8660SMarcel Moolenaar #include <i386/linux/linux_proto.h>
65146aad74SMarcel Moolenaar #include <compat/linux/linux_ipc.h>
66adc7ece0SDmitry Chagin #include <compat/linux/linux_misc.h>
67146aad74SMarcel Moolenaar #include <compat/linux/linux_signal.h>
68146aad74SMarcel Moolenaar #include <compat/linux/linux_util.h>
699b44bfc5SAlexander Leidinger #include <compat/linux/linux_emul.h>
709b44bfc5SAlexander Leidinger 
719b44bfc5SAlexander Leidinger #include <i386/include/pcb.h>			/* needed for pcb definition in linux_set_thread_area */
729b44bfc5SAlexander Leidinger 
739b44bfc5SAlexander Leidinger #include "opt_posix.h"
749b44bfc5SAlexander Leidinger 
759b44bfc5SAlexander Leidinger extern struct sysentvec elf32_freebsd_sysvec;	/* defined in i386/i386/elf_machdep.c */
76146aad74SMarcel Moolenaar 
775002a60fSMarcel Moolenaar struct l_descriptor {
785002a60fSMarcel Moolenaar 	l_uint		entry_number;
795002a60fSMarcel Moolenaar 	l_ulong		base_addr;
805002a60fSMarcel Moolenaar 	l_uint		limit;
815002a60fSMarcel Moolenaar 	l_uint		seg_32bit:1;
825002a60fSMarcel Moolenaar 	l_uint		contents:2;
835002a60fSMarcel Moolenaar 	l_uint		read_exec_only:1;
845002a60fSMarcel Moolenaar 	l_uint		limit_in_pages:1;
855002a60fSMarcel Moolenaar 	l_uint		seg_not_present:1;
865002a60fSMarcel Moolenaar 	l_uint		useable:1;
87146aad74SMarcel Moolenaar };
88146aad74SMarcel Moolenaar 
895002a60fSMarcel Moolenaar struct l_old_select_argv {
905002a60fSMarcel Moolenaar 	l_int		nfds;
915002a60fSMarcel Moolenaar 	l_fd_set	*readfds;
925002a60fSMarcel Moolenaar 	l_fd_set	*writefds;
935002a60fSMarcel Moolenaar 	l_fd_set	*exceptfds;
945002a60fSMarcel Moolenaar 	struct l_timeval	*timeout;
95146aad74SMarcel Moolenaar };
96146aad74SMarcel Moolenaar 
97f12c0348SJohn Baldwin static int	linux_mmap_common(struct thread *td, l_uintptr_t addr,
98f12c0348SJohn Baldwin 		    l_size_t len, l_int prot, l_int flags, l_int fd,
99f12c0348SJohn Baldwin 		    l_loff_t pos);
100f12c0348SJohn Baldwin 
101146aad74SMarcel Moolenaar int
102931a7258SAndrew Gallatin linux_to_bsd_sigaltstack(int lsa)
103931a7258SAndrew Gallatin {
104931a7258SAndrew Gallatin 	int bsa = 0;
105931a7258SAndrew Gallatin 
106931a7258SAndrew Gallatin 	if (lsa & LINUX_SS_DISABLE)
107931a7258SAndrew Gallatin 		bsa |= SS_DISABLE;
108931a7258SAndrew Gallatin 	if (lsa & LINUX_SS_ONSTACK)
109931a7258SAndrew Gallatin 		bsa |= SS_ONSTACK;
110931a7258SAndrew Gallatin 	return (bsa);
111931a7258SAndrew Gallatin }
112931a7258SAndrew Gallatin 
113931a7258SAndrew Gallatin int
114931a7258SAndrew Gallatin bsd_to_linux_sigaltstack(int bsa)
115931a7258SAndrew Gallatin {
116931a7258SAndrew Gallatin 	int lsa = 0;
117931a7258SAndrew Gallatin 
118931a7258SAndrew Gallatin 	if (bsa & SS_DISABLE)
119931a7258SAndrew Gallatin 		lsa |= LINUX_SS_DISABLE;
120931a7258SAndrew Gallatin 	if (bsa & SS_ONSTACK)
121931a7258SAndrew Gallatin 		lsa |= LINUX_SS_ONSTACK;
122931a7258SAndrew Gallatin 	return (lsa);
123931a7258SAndrew Gallatin }
124931a7258SAndrew Gallatin 
125931a7258SAndrew Gallatin int
126b40ce416SJulian Elischer linux_execve(struct thread *td, struct linux_execve_args *args)
127146aad74SMarcel Moolenaar {
128610ecfe0SMaxim Sobolev 	int error;
129610ecfe0SMaxim Sobolev 	char *newpath;
130610ecfe0SMaxim Sobolev 	struct image_args eargs;
131146aad74SMarcel Moolenaar 
132d9e97471SJohn Baldwin 	LCONVPATHEXIST(td, args->path, &newpath);
133146aad74SMarcel Moolenaar 
134146aad74SMarcel Moolenaar #ifdef DEBUG
13524593369SJonathan Lemon 	if (ldebug(execve))
136610ecfe0SMaxim Sobolev 		printf(ARGS(execve, "%s"), newpath);
137146aad74SMarcel Moolenaar #endif
138146aad74SMarcel Moolenaar 
139610ecfe0SMaxim Sobolev 	error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
140610ecfe0SMaxim Sobolev 	    args->argp, args->envp);
141610ecfe0SMaxim Sobolev 	free(newpath, M_TEMP);
142610ecfe0SMaxim Sobolev 	if (error == 0)
143c035ac04SMaxim Sobolev 		error = kern_execve(td, &eargs, NULL);
1449b44bfc5SAlexander Leidinger 	if (error == 0)
1459b44bfc5SAlexander Leidinger 	   	/* linux process can exec fbsd one, dont attempt
1469b44bfc5SAlexander Leidinger 		 * to create emuldata for such process using
1479b44bfc5SAlexander Leidinger 		 * linux_proc_init, this leads to a panic on KASSERT
1489b44bfc5SAlexander Leidinger 		 * because such process has p->p_emuldata == NULL
1499b44bfc5SAlexander Leidinger 		 */
150a5c1afadSDmitry Chagin 		if (SV_PROC_ABI(td->td_proc) == SV_ABI_LINUX)
1519b44bfc5SAlexander Leidinger    			error = linux_proc_init(td, 0, 0);
152610ecfe0SMaxim Sobolev 	return (error);
153146aad74SMarcel Moolenaar }
154146aad74SMarcel Moolenaar 
1555002a60fSMarcel Moolenaar struct l_ipc_kludge {
1565002a60fSMarcel Moolenaar 	struct l_msgbuf *msgp;
1575002a60fSMarcel Moolenaar 	l_long msgtyp;
1585002a60fSMarcel Moolenaar };
1595002a60fSMarcel Moolenaar 
160146aad74SMarcel Moolenaar int
161b40ce416SJulian Elischer linux_ipc(struct thread *td, struct linux_ipc_args *args)
162146aad74SMarcel Moolenaar {
1635002a60fSMarcel Moolenaar 
1645002a60fSMarcel Moolenaar 	switch (args->what & 0xFFFF) {
1655002a60fSMarcel Moolenaar 	case LINUX_SEMOP: {
1665002a60fSMarcel Moolenaar 		struct linux_semop_args a;
1675002a60fSMarcel Moolenaar 
1685002a60fSMarcel Moolenaar 		a.semid = args->arg1;
1695002a60fSMarcel Moolenaar 		a.tsops = args->ptr;
1705002a60fSMarcel Moolenaar 		a.nsops = args->arg2;
171b40ce416SJulian Elischer 		return (linux_semop(td, &a));
1725002a60fSMarcel Moolenaar 	}
1735002a60fSMarcel Moolenaar 	case LINUX_SEMGET: {
1745002a60fSMarcel Moolenaar 		struct linux_semget_args a;
1755002a60fSMarcel Moolenaar 
1765002a60fSMarcel Moolenaar 		a.key = args->arg1;
1775002a60fSMarcel Moolenaar 		a.nsems = args->arg2;
1785002a60fSMarcel Moolenaar 		a.semflg = args->arg3;
179b40ce416SJulian Elischer 		return (linux_semget(td, &a));
1805002a60fSMarcel Moolenaar 	}
1815002a60fSMarcel Moolenaar 	case LINUX_SEMCTL: {
1825002a60fSMarcel Moolenaar 		struct linux_semctl_args a;
1835002a60fSMarcel Moolenaar 		int error;
1845002a60fSMarcel Moolenaar 
1855002a60fSMarcel Moolenaar 		a.semid = args->arg1;
1865002a60fSMarcel Moolenaar 		a.semnum = args->arg2;
1875002a60fSMarcel Moolenaar 		a.cmd = args->arg3;
1884b7ef73dSDag-Erling Smørgrav 		error = copyin(args->ptr, &a.arg, sizeof(a.arg));
1895002a60fSMarcel Moolenaar 		if (error)
1905002a60fSMarcel Moolenaar 			return (error);
191b40ce416SJulian Elischer 		return (linux_semctl(td, &a));
1925002a60fSMarcel Moolenaar 	}
1935002a60fSMarcel Moolenaar 	case LINUX_MSGSND: {
1945002a60fSMarcel Moolenaar 		struct linux_msgsnd_args a;
1955002a60fSMarcel Moolenaar 
1965002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
1975002a60fSMarcel Moolenaar 		a.msgp = args->ptr;
1985002a60fSMarcel Moolenaar 		a.msgsz = args->arg2;
1995002a60fSMarcel Moolenaar 		a.msgflg = args->arg3;
200b40ce416SJulian Elischer 		return (linux_msgsnd(td, &a));
2015002a60fSMarcel Moolenaar 	}
2025002a60fSMarcel Moolenaar 	case LINUX_MSGRCV: {
2035002a60fSMarcel Moolenaar 		struct linux_msgrcv_args a;
2045002a60fSMarcel Moolenaar 
2055002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
2065002a60fSMarcel Moolenaar 		a.msgsz = args->arg2;
2075002a60fSMarcel Moolenaar 		a.msgflg = args->arg3;
2085002a60fSMarcel Moolenaar 		if ((args->what >> 16) == 0) {
2095002a60fSMarcel Moolenaar 			struct l_ipc_kludge tmp;
2105002a60fSMarcel Moolenaar 			int error;
2115002a60fSMarcel Moolenaar 
2125002a60fSMarcel Moolenaar 			if (args->ptr == NULL)
2135002a60fSMarcel Moolenaar 				return (EINVAL);
2144b7ef73dSDag-Erling Smørgrav 			error = copyin(args->ptr, &tmp, sizeof(tmp));
2155002a60fSMarcel Moolenaar 			if (error)
2165002a60fSMarcel Moolenaar 				return (error);
2175002a60fSMarcel Moolenaar 			a.msgp = tmp.msgp;
2185002a60fSMarcel Moolenaar 			a.msgtyp = tmp.msgtyp;
2195002a60fSMarcel Moolenaar 		} else {
2205002a60fSMarcel Moolenaar 			a.msgp = args->ptr;
2215002a60fSMarcel Moolenaar 			a.msgtyp = args->arg5;
2225002a60fSMarcel Moolenaar 		}
223b40ce416SJulian Elischer 		return (linux_msgrcv(td, &a));
2245002a60fSMarcel Moolenaar 	}
2255002a60fSMarcel Moolenaar 	case LINUX_MSGGET: {
2265002a60fSMarcel Moolenaar 		struct linux_msgget_args a;
2275002a60fSMarcel Moolenaar 
2285002a60fSMarcel Moolenaar 		a.key = args->arg1;
2295002a60fSMarcel Moolenaar 		a.msgflg = args->arg2;
230b40ce416SJulian Elischer 		return (linux_msgget(td, &a));
2315002a60fSMarcel Moolenaar 	}
2325002a60fSMarcel Moolenaar 	case LINUX_MSGCTL: {
2335002a60fSMarcel Moolenaar 		struct linux_msgctl_args a;
2345002a60fSMarcel Moolenaar 
2355002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
2365002a60fSMarcel Moolenaar 		a.cmd = args->arg2;
2375002a60fSMarcel Moolenaar 		a.buf = args->ptr;
238b40ce416SJulian Elischer 		return (linux_msgctl(td, &a));
2395002a60fSMarcel Moolenaar 	}
2405002a60fSMarcel Moolenaar 	case LINUX_SHMAT: {
2415002a60fSMarcel Moolenaar 		struct linux_shmat_args a;
2425002a60fSMarcel Moolenaar 
2435002a60fSMarcel Moolenaar 		a.shmid = args->arg1;
2445002a60fSMarcel Moolenaar 		a.shmaddr = args->ptr;
2455002a60fSMarcel Moolenaar 		a.shmflg = args->arg2;
2465002a60fSMarcel Moolenaar 		a.raddr = (l_ulong *)args->arg3;
247b40ce416SJulian Elischer 		return (linux_shmat(td, &a));
2485002a60fSMarcel Moolenaar 	}
2495002a60fSMarcel Moolenaar 	case LINUX_SHMDT: {
2505002a60fSMarcel Moolenaar 		struct linux_shmdt_args a;
2515002a60fSMarcel Moolenaar 
2525002a60fSMarcel Moolenaar 		a.shmaddr = args->ptr;
253b40ce416SJulian Elischer 		return (linux_shmdt(td, &a));
2545002a60fSMarcel Moolenaar 	}
2555002a60fSMarcel Moolenaar 	case LINUX_SHMGET: {
2565002a60fSMarcel Moolenaar 		struct linux_shmget_args a;
2575002a60fSMarcel Moolenaar 
2585002a60fSMarcel Moolenaar 		a.key = args->arg1;
2595002a60fSMarcel Moolenaar 		a.size = args->arg2;
2605002a60fSMarcel Moolenaar 		a.shmflg = args->arg3;
261b40ce416SJulian Elischer 		return (linux_shmget(td, &a));
2625002a60fSMarcel Moolenaar 	}
2635002a60fSMarcel Moolenaar 	case LINUX_SHMCTL: {
2645002a60fSMarcel Moolenaar 		struct linux_shmctl_args a;
2655002a60fSMarcel Moolenaar 
2665002a60fSMarcel Moolenaar 		a.shmid = args->arg1;
2675002a60fSMarcel Moolenaar 		a.cmd = args->arg2;
2685002a60fSMarcel Moolenaar 		a.buf = args->ptr;
269b40ce416SJulian Elischer 		return (linux_shmctl(td, &a));
2705002a60fSMarcel Moolenaar 	}
2715002a60fSMarcel Moolenaar 	default:
2725002a60fSMarcel Moolenaar 		break;
273146aad74SMarcel Moolenaar 	}
274146aad74SMarcel Moolenaar 
2755002a60fSMarcel Moolenaar 	return (EINVAL);
276146aad74SMarcel Moolenaar }
277146aad74SMarcel Moolenaar 
278146aad74SMarcel Moolenaar int
279b40ce416SJulian Elischer linux_old_select(struct thread *td, struct linux_old_select_args *args)
280146aad74SMarcel Moolenaar {
2815002a60fSMarcel Moolenaar 	struct l_old_select_argv linux_args;
2825002a60fSMarcel Moolenaar 	struct linux_select_args newsel;
283146aad74SMarcel Moolenaar 	int error;
284146aad74SMarcel Moolenaar 
2855002a60fSMarcel Moolenaar #ifdef DEBUG
2865002a60fSMarcel Moolenaar 	if (ldebug(old_select))
2876aea6777SPeter Wemm 		printf(ARGS(old_select, "%p"), args->ptr);
288146aad74SMarcel Moolenaar #endif
289146aad74SMarcel Moolenaar 
2904b7ef73dSDag-Erling Smørgrav 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
291146aad74SMarcel Moolenaar 	if (error)
292146aad74SMarcel Moolenaar 		return (error);
293146aad74SMarcel Moolenaar 
294146aad74SMarcel Moolenaar 	newsel.nfds = linux_args.nfds;
295146aad74SMarcel Moolenaar 	newsel.readfds = linux_args.readfds;
296146aad74SMarcel Moolenaar 	newsel.writefds = linux_args.writefds;
297146aad74SMarcel Moolenaar 	newsel.exceptfds = linux_args.exceptfds;
298146aad74SMarcel Moolenaar 	newsel.timeout = linux_args.timeout;
299b40ce416SJulian Elischer 	return (linux_select(td, &newsel));
300146aad74SMarcel Moolenaar }
301146aad74SMarcel Moolenaar 
302146aad74SMarcel Moolenaar int
303b40ce416SJulian Elischer linux_fork(struct thread *td, struct linux_fork_args *args)
304146aad74SMarcel Moolenaar {
305146aad74SMarcel Moolenaar 	int error;
306d071f504SAlexander Leidinger 	struct proc *p2;
307d071f504SAlexander Leidinger 	struct thread *td2;
308146aad74SMarcel Moolenaar 
309146aad74SMarcel Moolenaar #ifdef DEBUG
31024593369SJonathan Lemon 	if (ldebug(fork))
31124593369SJonathan Lemon 		printf(ARGS(fork, ""));
312146aad74SMarcel Moolenaar #endif
313146aad74SMarcel Moolenaar 
314d071f504SAlexander Leidinger 	if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0)
315146aad74SMarcel Moolenaar 		return (error);
316146aad74SMarcel Moolenaar 
317d071f504SAlexander Leidinger 	if (error == 0) {
318d071f504SAlexander Leidinger 		td->td_retval[0] = p2->p_pid;
319d071f504SAlexander Leidinger 		td->td_retval[1] = 0;
320d071f504SAlexander Leidinger 	}
321d071f504SAlexander Leidinger 
322b40ce416SJulian Elischer 	if (td->td_retval[1] == 1)
323b40ce416SJulian Elischer 		td->td_retval[0] = 0;
3249b44bfc5SAlexander Leidinger 	error = linux_proc_init(td, td->td_retval[0], 0);
3259b44bfc5SAlexander Leidinger 	if (error)
3269b44bfc5SAlexander Leidinger 		return (error);
3279b44bfc5SAlexander Leidinger 
328d071f504SAlexander Leidinger 	td2 = FIRST_THREAD_IN_PROC(p2);
329d071f504SAlexander Leidinger 
330d071f504SAlexander Leidinger 	/*
331d071f504SAlexander Leidinger 	 * Make this runnable after we are finished with it.
332d071f504SAlexander Leidinger 	 */
333982d11f8SJeff Roberson 	thread_lock(td2);
334d071f504SAlexander Leidinger 	TD_SET_CAN_RUN(td2);
335f0393f06SJeff Roberson 	sched_add(td2, SRQ_BORING);
336982d11f8SJeff Roberson 	thread_unlock(td2);
337d071f504SAlexander Leidinger 
338146aad74SMarcel Moolenaar 	return (0);
339146aad74SMarcel Moolenaar }
340146aad74SMarcel Moolenaar 
341146aad74SMarcel Moolenaar int
342b40ce416SJulian Elischer linux_vfork(struct thread *td, struct linux_vfork_args *args)
343146aad74SMarcel Moolenaar {
344146aad74SMarcel Moolenaar 	int error;
34540f734ddSAlexander Leidinger 	struct proc *p2;
346d071f504SAlexander Leidinger 	struct thread *td2;
347146aad74SMarcel Moolenaar 
348146aad74SMarcel Moolenaar #ifdef DEBUG
34924593369SJonathan Lemon 	if (ldebug(vfork))
35024593369SJonathan Lemon 		printf(ARGS(vfork, ""));
351146aad74SMarcel Moolenaar #endif
352146aad74SMarcel Moolenaar 
35340f734ddSAlexander Leidinger 	/* exclude RFPPWAIT */
354d071f504SAlexander Leidinger 	if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0)
355146aad74SMarcel Moolenaar 		return (error);
35640f734ddSAlexander Leidinger 	if (error == 0) {
35740f734ddSAlexander Leidinger 		td->td_retval[0] = p2->p_pid;
35840f734ddSAlexander Leidinger 		td->td_retval[1] = 0;
35940f734ddSAlexander Leidinger 	}
360146aad74SMarcel Moolenaar 	/* Are we the child? */
361b40ce416SJulian Elischer 	if (td->td_retval[1] == 1)
362b40ce416SJulian Elischer 		td->td_retval[0] = 0;
3639b44bfc5SAlexander Leidinger 	error = linux_proc_init(td, td->td_retval[0], 0);
3649b44bfc5SAlexander Leidinger 	if (error)
3659b44bfc5SAlexander Leidinger 		return (error);
366d071f504SAlexander Leidinger 
36740f734ddSAlexander Leidinger 	PROC_LOCK(p2);
368973ac082SAlexander Leidinger 	p2->p_flag |= P_PPWAIT;
369d071f504SAlexander Leidinger 	PROC_UNLOCK(p2);
370d071f504SAlexander Leidinger 
371d071f504SAlexander Leidinger 	td2 = FIRST_THREAD_IN_PROC(p2);
372d071f504SAlexander Leidinger 
373d071f504SAlexander Leidinger 	/*
374d071f504SAlexander Leidinger 	 * Make this runnable after we are finished with it.
375d071f504SAlexander Leidinger 	 */
376982d11f8SJeff Roberson 	thread_lock(td2);
377d071f504SAlexander Leidinger 	TD_SET_CAN_RUN(td2);
378f0393f06SJeff Roberson 	sched_add(td2, SRQ_BORING);
379982d11f8SJeff Roberson 	thread_unlock(td2);
380d071f504SAlexander Leidinger 
381d071f504SAlexander Leidinger 	/* wait for the children to exit, ie. emulate vfork */
382d071f504SAlexander Leidinger 	PROC_LOCK(p2);
38340f734ddSAlexander Leidinger 	while (p2->p_flag & P_PPWAIT)
38499b7f1a1SKonstantin Belousov 		cv_wait(&p2->p_pwait, &p2->p_mtx);
38540f734ddSAlexander Leidinger 	PROC_UNLOCK(p2);
38640f734ddSAlexander Leidinger 
387146aad74SMarcel Moolenaar 	return (0);
388146aad74SMarcel Moolenaar }
389146aad74SMarcel Moolenaar 
390*2c7660baSDmitry Chagin static int
391*2c7660baSDmitry Chagin linux_set_cloned_tls(struct thread *td, void *desc)
392*2c7660baSDmitry Chagin {
393*2c7660baSDmitry Chagin 	struct segment_descriptor sd;
394*2c7660baSDmitry Chagin 	struct l_user_desc info;
395*2c7660baSDmitry Chagin 	int idx, error;
396*2c7660baSDmitry Chagin 	int a[2];
397*2c7660baSDmitry Chagin 
398*2c7660baSDmitry Chagin 	error = copyin(desc, &info, sizeof(struct l_user_desc));
399*2c7660baSDmitry Chagin 	if (error) {
400*2c7660baSDmitry Chagin 		printf(LMSG("copyin failed!"));
401*2c7660baSDmitry Chagin 	} else {
402*2c7660baSDmitry Chagin 		idx = info.entry_number;
403*2c7660baSDmitry Chagin 
404*2c7660baSDmitry Chagin 		/*
405*2c7660baSDmitry Chagin 		 * looks like we're getting the idx we returned
406*2c7660baSDmitry Chagin 		 * in the set_thread_area() syscall
407*2c7660baSDmitry Chagin 		 */
408*2c7660baSDmitry Chagin 		if (idx != 6 && idx != 3) {
409*2c7660baSDmitry Chagin 			printf(LMSG("resetting idx!"));
410*2c7660baSDmitry Chagin 			idx = 3;
411*2c7660baSDmitry Chagin 		}
412*2c7660baSDmitry Chagin 
413*2c7660baSDmitry Chagin 		/* this doesnt happen in practice */
414*2c7660baSDmitry Chagin 		if (idx == 6) {
415*2c7660baSDmitry Chagin 	   		/* we might copy out the entry_number as 3 */
416*2c7660baSDmitry Chagin 		   	info.entry_number = 3;
417*2c7660baSDmitry Chagin 			error = copyout(&info, desc, sizeof(struct l_user_desc));
418*2c7660baSDmitry Chagin 			if (error)
419*2c7660baSDmitry Chagin 				printf(LMSG("copyout failed!"));
420*2c7660baSDmitry Chagin 		}
421*2c7660baSDmitry Chagin 
422*2c7660baSDmitry Chagin 		a[0] = LINUX_LDT_entry_a(&info);
423*2c7660baSDmitry Chagin 		a[1] = LINUX_LDT_entry_b(&info);
424*2c7660baSDmitry Chagin 
425*2c7660baSDmitry Chagin 		memcpy(&sd, &a, sizeof(a));
426*2c7660baSDmitry Chagin #ifdef DEBUG
427*2c7660baSDmitry Chagin 		if (ldebug(clone))
428*2c7660baSDmitry Chagin 			printf("Segment created in clone with "
429*2c7660baSDmitry Chagin 			"CLONE_SETTLS: lobase: %x, hibase: %x, "
430*2c7660baSDmitry Chagin 			"lolimit: %x, hilimit: %x, type: %i, "
431*2c7660baSDmitry Chagin 			"dpl: %i, p: %i, xx: %i, def32: %i, "
432*2c7660baSDmitry Chagin 			"gran: %i\n", sd.sd_lobase, sd.sd_hibase,
433*2c7660baSDmitry Chagin 			sd.sd_lolimit, sd.sd_hilimit, sd.sd_type,
434*2c7660baSDmitry Chagin 			sd.sd_dpl, sd.sd_p, sd.sd_xx,
435*2c7660baSDmitry Chagin 			sd.sd_def32, sd.sd_gran);
436*2c7660baSDmitry Chagin #endif
437*2c7660baSDmitry Chagin 
438*2c7660baSDmitry Chagin 		/* set %gs */
439*2c7660baSDmitry Chagin 		td->td_pcb->pcb_gsd = sd;
440*2c7660baSDmitry Chagin 		td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
441*2c7660baSDmitry Chagin 	}
442*2c7660baSDmitry Chagin 
443*2c7660baSDmitry Chagin 	return (error);
444*2c7660baSDmitry Chagin }
445*2c7660baSDmitry Chagin 
446146aad74SMarcel Moolenaar int
447b40ce416SJulian Elischer linux_clone(struct thread *td, struct linux_clone_args *args)
448146aad74SMarcel Moolenaar {
4496ad0e7c5SJohn Baldwin 	int error, ff = RFPROC | RFSTOPPED;
450146aad74SMarcel Moolenaar 	struct proc *p2;
4519eb78fcfSJohn Baldwin 	struct thread *td2;
452146aad74SMarcel Moolenaar 	int exit_signal;
4539b44bfc5SAlexander Leidinger 	struct linux_emuldata *em;
454146aad74SMarcel Moolenaar 
455146aad74SMarcel Moolenaar #ifdef DEBUG
45624593369SJonathan Lemon 	if (ldebug(clone)) {
4579b44bfc5SAlexander Leidinger    	   	printf(ARGS(clone, "flags %x, stack %x, parent tid: %x, child tid: %x"),
4589b44bfc5SAlexander Leidinger 		    (unsigned int)args->flags, (unsigned int)args->stack,
4599b44bfc5SAlexander Leidinger 		    (unsigned int)args->parent_tidptr, (unsigned int)args->child_tidptr);
46024593369SJonathan Lemon 	}
461146aad74SMarcel Moolenaar #endif
462146aad74SMarcel Moolenaar 
463146aad74SMarcel Moolenaar 	exit_signal = args->flags & 0x000000ff;
464ec69a8a6SAlexander Kabaev 	if (LINUX_SIG_VALID(exit_signal)) {
465146aad74SMarcel Moolenaar 		if (exit_signal <= LINUX_SIGTBLSZ)
466ec69a8a6SAlexander Kabaev 			exit_signal =
467ec69a8a6SAlexander Kabaev 			    linux_to_bsd_signal[_SIG_IDX(exit_signal)];
468ec69a8a6SAlexander Kabaev 	} else if (exit_signal != 0)
469ec69a8a6SAlexander Kabaev 		return (EINVAL);
470146aad74SMarcel Moolenaar 
471a4e3bad7SJung-uk Kim 	if (args->flags & LINUX_CLONE_VM)
472146aad74SMarcel Moolenaar 		ff |= RFMEM;
473a4e3bad7SJung-uk Kim 	if (args->flags & LINUX_CLONE_SIGHAND)
474146aad74SMarcel Moolenaar 		ff |= RFSIGSHARE;
4754b3583a3SAlexander Leidinger 	/*
4764b3583a3SAlexander Leidinger 	 * XXX: in linux sharing of fs info (chroot/cwd/umask)
4774b3583a3SAlexander Leidinger 	 * and open files is independant. in fbsd its in one
478802e08a3SAlexander Leidinger 	 * structure but in reality it doesn't cause any problems
479802e08a3SAlexander Leidinger 	 * because both of these flags are usually set together.
4804b3583a3SAlexander Leidinger 	 */
481a4e3bad7SJung-uk Kim 	if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
482146aad74SMarcel Moolenaar 		ff |= RFFDG;
483146aad74SMarcel Moolenaar 
4844b178336SMaxim Sobolev 	/*
4854b178336SMaxim Sobolev 	 * Attempt to detect when linux_clone(2) is used for creating
4864b178336SMaxim Sobolev 	 * kernel threads. Unfortunately despite the existence of the
4874b178336SMaxim Sobolev 	 * CLONE_THREAD flag, version of linuxthreads package used in
4884b178336SMaxim Sobolev 	 * most popular distros as of beginning of 2005 doesn't make
489802e08a3SAlexander Leidinger 	 * any use of it. Therefore, this detection relies on
4904b178336SMaxim Sobolev 	 * empirical observation that linuxthreads sets certain
4914b178336SMaxim Sobolev 	 * combination of flags, so that we can make more or less
4924b178336SMaxim Sobolev 	 * precise detection and notify the FreeBSD kernel that several
4934b178336SMaxim Sobolev 	 * processes are in fact part of the same threading group, so
4944b178336SMaxim Sobolev 	 * that special treatment is necessary for signal delivery
4954b178336SMaxim Sobolev 	 * between those processes and fd locking.
4964b178336SMaxim Sobolev 	 */
497a4e3bad7SJung-uk Kim 	if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
4984b178336SMaxim Sobolev 		ff |= RFTHREAD;
4994b178336SMaxim Sobolev 
500a4e3bad7SJung-uk Kim 	if (args->flags & LINUX_CLONE_PARENT_SETTID)
5011a26db0aSAlexander Leidinger 		if (args->parent_tidptr == NULL)
5021a26db0aSAlexander Leidinger 			return (EINVAL);
5031a26db0aSAlexander Leidinger 
504316ec49aSScott Long 	error = fork1(td, ff, 0, &p2);
5059eb78fcfSJohn Baldwin 	if (error)
5069eb78fcfSJohn Baldwin 		return (error);
5079eb78fcfSJohn Baldwin 
508a4e3bad7SJung-uk Kim 	if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
509a9ccaccfSKonstantin Belousov 	   	sx_xlock(&proctree_lock);
510a9ccaccfSKonstantin Belousov 		PROC_LOCK(p2);
511a9ccaccfSKonstantin Belousov 		proc_reparent(p2, td->td_proc->p_pptr);
512a9ccaccfSKonstantin Belousov 		PROC_UNLOCK(p2);
513a9ccaccfSKonstantin Belousov 		sx_xunlock(&proctree_lock);
514a9ccaccfSKonstantin Belousov 	}
515a9ccaccfSKonstantin Belousov 
5169b44bfc5SAlexander Leidinger 	/* create the emuldata */
5179b44bfc5SAlexander Leidinger 	error = linux_proc_init(td, p2->p_pid, args->flags);
5189b44bfc5SAlexander Leidinger 	/* reference it - no need to check this */
5191c65504cSAlexander Leidinger 	em = em_find(p2, EMUL_DOLOCK);
5209b44bfc5SAlexander Leidinger 	KASSERT(em != NULL, ("clone: emuldata not found.\n"));
5219b44bfc5SAlexander Leidinger 	/* and adjust it */
5229b44bfc5SAlexander Leidinger 
523a4e3bad7SJung-uk Kim 	if (args->flags & LINUX_CLONE_THREAD) {
5249b44bfc5SAlexander Leidinger 	   	/* XXX: linux mangles pgrp and pptr somehow
5259b44bfc5SAlexander Leidinger 		 * I think it might be this but I am not sure.
5269b44bfc5SAlexander Leidinger 		 */
5279b44bfc5SAlexander Leidinger #ifdef notyet
528a6c5f813SAlexander Leidinger 	   	PROC_LOCK(p2);
5299b44bfc5SAlexander Leidinger 	   	p2->p_pgrp = td->td_proc->p_pgrp;
530a6c5f813SAlexander Leidinger 	   	PROC_UNLOCK(p2);
5319b44bfc5SAlexander Leidinger #endif
5329b44bfc5SAlexander Leidinger 	 	exit_signal = 0;
5339b44bfc5SAlexander Leidinger 	}
5349b44bfc5SAlexander Leidinger 
535a4e3bad7SJung-uk Kim 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
5369b44bfc5SAlexander Leidinger 		em->child_set_tid = args->child_tidptr;
5379b44bfc5SAlexander Leidinger 	else
5389b44bfc5SAlexander Leidinger 	   	em->child_set_tid = NULL;
5399b44bfc5SAlexander Leidinger 
540a4e3bad7SJung-uk Kim 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
5419b44bfc5SAlexander Leidinger 		em->child_clear_tid = args->child_tidptr;
5429b44bfc5SAlexander Leidinger 	else
5439b44bfc5SAlexander Leidinger 	   	em->child_clear_tid = NULL;
544a6c5f813SAlexander Leidinger 
5459b44bfc5SAlexander Leidinger 	EMUL_UNLOCK(&emul_lock);
546146aad74SMarcel Moolenaar 
547a4e3bad7SJung-uk Kim 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
5481a26db0aSAlexander Leidinger 		error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid));
5491a26db0aSAlexander Leidinger 		if (error)
5501a26db0aSAlexander Leidinger 			printf(LMSG("copyout failed!"));
5511a26db0aSAlexander Leidinger 	}
5521a26db0aSAlexander Leidinger 
553fdfdfb78SJohn Baldwin 	PROC_LOCK(p2);
554146aad74SMarcel Moolenaar 	p2->p_sigparent = exit_signal;
5559eb78fcfSJohn Baldwin 	PROC_UNLOCK(p2);
5569eb78fcfSJohn Baldwin 	td2 = FIRST_THREAD_IN_PROC(p2);
5570eef2f8aSAlexander Leidinger 	/*
5580eef2f8aSAlexander Leidinger 	 * in a case of stack = NULL we are supposed to COW calling process stack
5599b44bfc5SAlexander Leidinger 	 * this is what normal fork() does so we just keep the tf_esp arg intact
5609b44bfc5SAlexander Leidinger 	 */
5619b44bfc5SAlexander Leidinger 	if (args->stack)
5629eb78fcfSJohn Baldwin    	   	td2->td_frame->tf_esp = (unsigned int)args->stack;
563146aad74SMarcel Moolenaar 
564*2c7660baSDmitry Chagin 	if (args->flags & LINUX_CLONE_SETTLS)
565*2c7660baSDmitry Chagin 		linux_set_cloned_tls(td2, args->tls);
5669b44bfc5SAlexander Leidinger 
567146aad74SMarcel Moolenaar #ifdef DEBUG
56824593369SJonathan Lemon 	if (ldebug(clone))
5699eb78fcfSJohn Baldwin 		printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
5709eb78fcfSJohn Baldwin 		    (long)p2->p_pid, args->stack, exit_signal);
571146aad74SMarcel Moolenaar #endif
572a4e3bad7SJung-uk Kim 	if (args->flags & LINUX_CLONE_VFORK) {
573d071f504SAlexander Leidinger 	   	PROC_LOCK(p2);
574d071f504SAlexander Leidinger 		p2->p_flag |= P_PPWAIT;
575d071f504SAlexander Leidinger 	   	PROC_UNLOCK(p2);
576d071f504SAlexander Leidinger 	}
577146aad74SMarcel Moolenaar 
5786ad0e7c5SJohn Baldwin 	/*
5796ad0e7c5SJohn Baldwin 	 * Make this runnable after we are finished with it.
5806ad0e7c5SJohn Baldwin 	 */
581982d11f8SJeff Roberson 	thread_lock(td2);
5829eb78fcfSJohn Baldwin 	TD_SET_CAN_RUN(td2);
583f0393f06SJeff Roberson 	sched_add(td2, SRQ_BORING);
584982d11f8SJeff Roberson 	thread_unlock(td2);
5856ad0e7c5SJohn Baldwin 
5869eb78fcfSJohn Baldwin 	td->td_retval[0] = p2->p_pid;
5879eb78fcfSJohn Baldwin 	td->td_retval[1] = 0;
5880a62e035SAlexander Leidinger 
589a4e3bad7SJung-uk Kim 	if (args->flags & LINUX_CLONE_VFORK) {
5900a62e035SAlexander Leidinger    	   	/* wait for the children to exit, ie. emulate vfork */
5910a62e035SAlexander Leidinger    	   	PROC_LOCK(p2);
5920a62e035SAlexander Leidinger 		while (p2->p_flag & P_PPWAIT)
59399b7f1a1SKonstantin Belousov 			cv_wait(&p2->p_pwait, &p2->p_mtx);
5940a62e035SAlexander Leidinger 		PROC_UNLOCK(p2);
5950a62e035SAlexander Leidinger 	}
5960a62e035SAlexander Leidinger 
5979eb78fcfSJohn Baldwin 	return (0);
598146aad74SMarcel Moolenaar }
599146aad74SMarcel Moolenaar 
600146aad74SMarcel Moolenaar #define STACK_SIZE  (2 * 1024 * 1024)
601146aad74SMarcel Moolenaar #define GUARD_SIZE  (4 * PAGE_SIZE)
602146aad74SMarcel Moolenaar 
6033ad9c842SMaxim Sobolev int
6043ad9c842SMaxim Sobolev linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
6053ad9c842SMaxim Sobolev {
6063ad9c842SMaxim Sobolev 
6073ad9c842SMaxim Sobolev #ifdef DEBUG
6083ad9c842SMaxim Sobolev 	if (ldebug(mmap2))
6093ad9c842SMaxim Sobolev 		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
6103ad9c842SMaxim Sobolev 		    (void *)args->addr, args->len, args->prot,
6113ad9c842SMaxim Sobolev 		    args->flags, args->fd, args->pgoff);
6123ad9c842SMaxim Sobolev #endif
6133ad9c842SMaxim Sobolev 
614f12c0348SJohn Baldwin 	return (linux_mmap_common(td, args->addr, args->len, args->prot,
615f12c0348SJohn Baldwin 		args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
616f12c0348SJohn Baldwin 		PAGE_SIZE));
6173ad9c842SMaxim Sobolev }
6183ad9c842SMaxim Sobolev 
619146aad74SMarcel Moolenaar int
620b40ce416SJulian Elischer linux_mmap(struct thread *td, struct linux_mmap_args *args)
621146aad74SMarcel Moolenaar {
6223ad9c842SMaxim Sobolev 	int error;
6233ad9c842SMaxim Sobolev 	struct l_mmap_argv linux_args;
6243ad9c842SMaxim Sobolev 
6254b7ef73dSDag-Erling Smørgrav 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
6263ad9c842SMaxim Sobolev 	if (error)
6273ad9c842SMaxim Sobolev 		return (error);
6283ad9c842SMaxim Sobolev 
6293ad9c842SMaxim Sobolev #ifdef DEBUG
6303ad9c842SMaxim Sobolev 	if (ldebug(mmap))
6313ad9c842SMaxim Sobolev 		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
632b45bbfc3SBruce Evans 		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
63310931a46SJung-uk Kim 		    linux_args.flags, linux_args.fd, linux_args.pgoff);
6343ad9c842SMaxim Sobolev #endif
6353ad9c842SMaxim Sobolev 
636f12c0348SJohn Baldwin 	return (linux_mmap_common(td, linux_args.addr, linux_args.len,
637f12c0348SJohn Baldwin 	    linux_args.prot, linux_args.flags, linux_args.fd,
638f12c0348SJohn Baldwin 	    (uint32_t)linux_args.pgoff));
6393ad9c842SMaxim Sobolev }
6403ad9c842SMaxim Sobolev 
6413ad9c842SMaxim Sobolev static int
642f12c0348SJohn Baldwin linux_mmap_common(struct thread *td, l_uintptr_t addr, l_size_t len, l_int prot,
643f12c0348SJohn Baldwin     l_int flags, l_int fd, l_loff_t pos)
6443ad9c842SMaxim Sobolev {
645b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
646146aad74SMarcel Moolenaar 	struct mmap_args /* {
647146aad74SMarcel Moolenaar 		caddr_t addr;
648146aad74SMarcel Moolenaar 		size_t len;
649146aad74SMarcel Moolenaar 		int prot;
650146aad74SMarcel Moolenaar 		int flags;
651146aad74SMarcel Moolenaar 		int fd;
652146aad74SMarcel Moolenaar 		long pad;
653146aad74SMarcel Moolenaar 		off_t pos;
654146aad74SMarcel Moolenaar 	} */ bsd_args;
65591d631e5SMatthew N. Dodd 	int error;
656a312f6a3SAlexander Leidinger 	struct file *fp;
657146aad74SMarcel Moolenaar 
65891d631e5SMatthew N. Dodd 	error = 0;
659146aad74SMarcel Moolenaar 	bsd_args.flags = 0;
660a312f6a3SAlexander Leidinger 	fp = NULL;
661a312f6a3SAlexander Leidinger 
662a312f6a3SAlexander Leidinger 	/*
663a312f6a3SAlexander Leidinger 	 * Linux mmap(2):
664a312f6a3SAlexander Leidinger 	 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
665a312f6a3SAlexander Leidinger 	 */
666f12c0348SJohn Baldwin 	if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
6676dc4e810SAlexander Leidinger 		return (EINVAL);
668a312f6a3SAlexander Leidinger 
669f12c0348SJohn Baldwin 	if (flags & LINUX_MAP_SHARED)
670146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_SHARED;
671f12c0348SJohn Baldwin 	if (flags & LINUX_MAP_PRIVATE)
672146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_PRIVATE;
673f12c0348SJohn Baldwin 	if (flags & LINUX_MAP_FIXED)
674146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_FIXED;
67560743cbdSAlexander Kabaev 	if (flags & LINUX_MAP_ANON) {
67660743cbdSAlexander Kabaev 		/* Enforce pos to be on page boundary, then ignore. */
67760743cbdSAlexander Kabaev 		if ((pos & PAGE_MASK) != 0)
67860743cbdSAlexander Kabaev 			return (EINVAL);
67960743cbdSAlexander Kabaev 		pos = 0;
680146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_ANON;
68160743cbdSAlexander Kabaev 	} else
6820cc3ac8bSMatthew Dillon 		bsd_args.flags |= MAP_NOSYNC;
683f12c0348SJohn Baldwin 	if (flags & LINUX_MAP_GROWSDOWN)
684146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_STACK;
685146aad74SMarcel Moolenaar 
6860eef2f8aSAlexander Leidinger 	/*
68710931a46SJung-uk Kim 	 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
68810931a46SJung-uk Kim 	 * on Linux/i386. We do this to ensure maximum compatibility.
68910931a46SJung-uk Kim 	 * Linux/ia64 does the same in i386 emulation mode.
69010931a46SJung-uk Kim 	 */
691f12c0348SJohn Baldwin 	bsd_args.prot = prot;
69210931a46SJung-uk Kim 	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
69310931a46SJung-uk Kim 		bsd_args.prot |= PROT_READ | PROT_EXEC;
69410931a46SJung-uk Kim 
6956a5964d3SJung-uk Kim 	/* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
696f12c0348SJohn Baldwin 	bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : fd;
6976a5964d3SJung-uk Kim 	if (bsd_args.fd != -1) {
69810931a46SJung-uk Kim 		/*
69910931a46SJung-uk Kim 		 * Linux follows Solaris mmap(2) description:
70010931a46SJung-uk Kim 		 * The file descriptor fildes is opened with
70110931a46SJung-uk Kim 		 * read permission, regardless of the
70210931a46SJung-uk Kim 		 * protection options specified.
70310931a46SJung-uk Kim 		 */
70410931a46SJung-uk Kim 
7056a5964d3SJung-uk Kim 		if ((error = fget(td, bsd_args.fd, &fp)) != 0)
70610931a46SJung-uk Kim 			return (error);
70710931a46SJung-uk Kim 		if (fp->f_type != DTYPE_VNODE) {
70810931a46SJung-uk Kim 			fdrop(fp, td);
70910931a46SJung-uk Kim 			return (EINVAL);
71010931a46SJung-uk Kim 		}
71110931a46SJung-uk Kim 
71210931a46SJung-uk Kim 		/* Linux mmap() just fails for O_WRONLY files */
71310931a46SJung-uk Kim 		if (!(fp->f_flag & FREAD)) {
71410931a46SJung-uk Kim 			fdrop(fp, td);
71510931a46SJung-uk Kim 			return (EACCES);
71610931a46SJung-uk Kim 		}
71710931a46SJung-uk Kim 
71810931a46SJung-uk Kim 		fdrop(fp, td);
71910931a46SJung-uk Kim 	}
72010931a46SJung-uk Kim 
721f12c0348SJohn Baldwin 	if (flags & LINUX_MAP_GROWSDOWN) {
72210931a46SJung-uk Kim 		/*
723f12c0348SJohn Baldwin 		 * The Linux MAP_GROWSDOWN option does not limit auto
724146aad74SMarcel Moolenaar 		 * growth of the region.  Linux mmap with this option
725146aad74SMarcel Moolenaar 		 * takes as addr the inital BOS, and as len, the initial
726146aad74SMarcel Moolenaar 		 * region size.  It can then grow down from addr without
727146aad74SMarcel Moolenaar 		 * limit.  However, linux threads has an implicit internal
728146aad74SMarcel Moolenaar 		 * limit to stack size of STACK_SIZE.  Its just not
729146aad74SMarcel Moolenaar 		 * enforced explicitly in linux.  But, here we impose
730146aad74SMarcel Moolenaar 		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
731146aad74SMarcel Moolenaar 		 * region, since we can do this with our mmap.
732146aad74SMarcel Moolenaar 		 *
733146aad74SMarcel Moolenaar 		 * Our mmap with MAP_STACK takes addr as the maximum
734146aad74SMarcel Moolenaar 		 * downsize limit on BOS, and as len the max size of
735146aad74SMarcel Moolenaar 		 * the region.  It them maps the top SGROWSIZ bytes,
736802e08a3SAlexander Leidinger 		 * and auto grows the region down, up to the limit
737146aad74SMarcel Moolenaar 		 * in addr.
738146aad74SMarcel Moolenaar 		 *
739146aad74SMarcel Moolenaar 		 * If we don't use the MAP_STACK option, the effect
740146aad74SMarcel Moolenaar 		 * of this code is to allocate a stack region of a
741146aad74SMarcel Moolenaar 		 * fixed size of (STACK_SIZE - GUARD_SIZE).
742146aad74SMarcel Moolenaar 		 */
743146aad74SMarcel Moolenaar 
744f12c0348SJohn Baldwin 		if ((caddr_t)PTRIN(addr) + len > p->p_vmspace->vm_maxsaddr) {
7450eef2f8aSAlexander Leidinger 			/*
7460eef2f8aSAlexander Leidinger 			 * Some linux apps will attempt to mmap
747242fae60SAndrew Gallatin 			 * thread stacks near the top of their
748242fae60SAndrew Gallatin 			 * address space.  If their TOS is greater
749242fae60SAndrew Gallatin 			 * than vm_maxsaddr, vm_map_growstack()
750242fae60SAndrew Gallatin 			 * will confuse the thread stack with the
751242fae60SAndrew Gallatin 			 * process stack and deliver a SEGV if they
752242fae60SAndrew Gallatin 			 * attempt to grow the thread stack past their
753242fae60SAndrew Gallatin 			 * current stacksize rlimit.  To avoid this,
754242fae60SAndrew Gallatin 			 * adjust vm_maxsaddr upwards to reflect
755242fae60SAndrew Gallatin 			 * the current stacksize rlimit rather
756242fae60SAndrew Gallatin 			 * than the maximum possible stacksize.
757242fae60SAndrew Gallatin 			 * It would be better to adjust the
758242fae60SAndrew Gallatin 			 * mmap'ed region, but some apps do not check
759242fae60SAndrew Gallatin 			 * mmap's return value.
760242fae60SAndrew Gallatin 			 */
76191d5354aSJohn Baldwin 			PROC_LOCK(p);
762242fae60SAndrew Gallatin 			p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
76391d5354aSJohn Baldwin 			    lim_cur(p, RLIMIT_STACK);
76491d5354aSJohn Baldwin 			PROC_UNLOCK(p);
765242fae60SAndrew Gallatin 		}
766242fae60SAndrew Gallatin 
7670eef2f8aSAlexander Leidinger 		/*
768865df544SJung-uk Kim 		 * This gives us our maximum stack size and a new BOS.
769865df544SJung-uk Kim 		 * If we're using VM_STACK, then mmap will just map
770865df544SJung-uk Kim 		 * the top SGROWSIZ bytes, and let the stack grow down
771865df544SJung-uk Kim 		 * to the limit at BOS.  If we're not using VM_STACK
772865df544SJung-uk Kim 		 * we map the full stack, since we don't have a way
773865df544SJung-uk Kim 		 * to autogrow it.
774146aad74SMarcel Moolenaar 		 */
775f12c0348SJohn Baldwin 		if (len > STACK_SIZE - GUARD_SIZE) {
776f12c0348SJohn Baldwin 			bsd_args.addr = (caddr_t)PTRIN(addr);
777f12c0348SJohn Baldwin 			bsd_args.len = len;
778865df544SJung-uk Kim 		} else {
779f12c0348SJohn Baldwin 			bsd_args.addr = (caddr_t)PTRIN(addr) -
780f12c0348SJohn Baldwin 			    (STACK_SIZE - GUARD_SIZE - len);
781865df544SJung-uk Kim 			bsd_args.len = STACK_SIZE - GUARD_SIZE;
782865df544SJung-uk Kim 		}
783146aad74SMarcel Moolenaar 	} else {
784f12c0348SJohn Baldwin 		bsd_args.addr = (caddr_t)PTRIN(addr);
785f12c0348SJohn Baldwin 		bsd_args.len  = len;
786146aad74SMarcel Moolenaar 	}
787f12c0348SJohn Baldwin 	bsd_args.pos = pos;
788146aad74SMarcel Moolenaar 
789146aad74SMarcel Moolenaar #ifdef DEBUG
79024593369SJonathan Lemon 	if (ldebug(mmap))
79191d631e5SMatthew N. Dodd 		printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
79291d631e5SMatthew N. Dodd 		    __func__,
79324593369SJonathan Lemon 		    (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
79424593369SJonathan Lemon 		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
795146aad74SMarcel Moolenaar #endif
79691d631e5SMatthew N. Dodd 	error = mmap(td, &bsd_args);
79791d631e5SMatthew N. Dodd #ifdef DEBUG
79891d631e5SMatthew N. Dodd 	if (ldebug(mmap))
79991d631e5SMatthew N. Dodd 		printf("-> %s() return: 0x%x (0x%08x)\n",
80091d631e5SMatthew N. Dodd 			__func__, error, (u_int)td->td_retval[0]);
80191d631e5SMatthew N. Dodd #endif
80291d631e5SMatthew N. Dodd 	return (error);
803146aad74SMarcel Moolenaar }
804146aad74SMarcel Moolenaar 
805146aad74SMarcel Moolenaar int
80610931a46SJung-uk Kim linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
80710931a46SJung-uk Kim {
80810931a46SJung-uk Kim 	struct mprotect_args bsd_args;
80910931a46SJung-uk Kim 
81010931a46SJung-uk Kim 	bsd_args.addr = uap->addr;
81110931a46SJung-uk Kim 	bsd_args.len = uap->len;
81210931a46SJung-uk Kim 	bsd_args.prot = uap->prot;
81310931a46SJung-uk Kim 	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
81410931a46SJung-uk Kim 		bsd_args.prot |= PROT_READ | PROT_EXEC;
81510931a46SJung-uk Kim 	return (mprotect(td, &bsd_args));
81610931a46SJung-uk Kim }
81710931a46SJung-uk Kim 
81810931a46SJung-uk Kim int
819b40ce416SJulian Elischer linux_pipe(struct thread *td, struct linux_pipe_args *args)
820146aad74SMarcel Moolenaar {
821146aad74SMarcel Moolenaar 	int error;
822ab0d10f6SEd Schouten 	int fildes[2];
823146aad74SMarcel Moolenaar 
824146aad74SMarcel Moolenaar #ifdef DEBUG
82524593369SJonathan Lemon 	if (ldebug(pipe))
82624593369SJonathan Lemon 		printf(ARGS(pipe, "*"));
827146aad74SMarcel Moolenaar #endif
828146aad74SMarcel Moolenaar 
829ab0d10f6SEd Schouten 	error = kern_pipe(td, fildes);
830ab0d10f6SEd Schouten 	if (error)
831146aad74SMarcel Moolenaar 		return (error);
832146aad74SMarcel Moolenaar 
833ab0d10f6SEd Schouten 	/* XXX: Close descriptors on error. */
834ab0d10f6SEd Schouten 	return (copyout(fildes, args->pipefds, sizeof fildes));
835146aad74SMarcel Moolenaar }
836146aad74SMarcel Moolenaar 
837146aad74SMarcel Moolenaar int
838b40ce416SJulian Elischer linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
839146aad74SMarcel Moolenaar {
84084569dffSMaxim Sobolev 	int error;
84184569dffSMaxim Sobolev 	struct i386_ioperm_args iia;
842146aad74SMarcel Moolenaar 
84384569dffSMaxim Sobolev 	iia.start = args->start;
84484569dffSMaxim Sobolev 	iia.length = args->length;
84584569dffSMaxim Sobolev 	iia.enable = args->enable;
84684569dffSMaxim Sobolev 	error = i386_set_ioperm(td, &iia);
84784569dffSMaxim Sobolev 	return (error);
848146aad74SMarcel Moolenaar }
849146aad74SMarcel Moolenaar 
850146aad74SMarcel Moolenaar int
851b40ce416SJulian Elischer linux_iopl(struct thread *td, struct linux_iopl_args *args)
852146aad74SMarcel Moolenaar {
853146aad74SMarcel Moolenaar 	int error;
854146aad74SMarcel Moolenaar 
855146aad74SMarcel Moolenaar 	if (args->level < 0 || args->level > 3)
856146aad74SMarcel Moolenaar 		return (EINVAL);
857acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_IO)) != 0)
858146aad74SMarcel Moolenaar 		return (error);
859a854ed98SJohn Baldwin 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
86041c42188SRobert Watson 		return (error);
861b40ce416SJulian Elischer 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
862146aad74SMarcel Moolenaar 	    (args->level * (PSL_IOPL / 3));
863146aad74SMarcel Moolenaar 	return (0);
864146aad74SMarcel Moolenaar }
865146aad74SMarcel Moolenaar 
866146aad74SMarcel Moolenaar int
867b07cd97eSMark Murray linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
868146aad74SMarcel Moolenaar {
869146aad74SMarcel Moolenaar 	int error;
87084569dffSMaxim Sobolev 	struct i386_ldt_args ldt;
8715002a60fSMarcel Moolenaar 	struct l_descriptor ld;
87284569dffSMaxim Sobolev 	union descriptor desc;
8736259969dSKonstantin Belousov 	int size, written;
874146aad74SMarcel Moolenaar 
875146aad74SMarcel Moolenaar 	switch (uap->func) {
876146aad74SMarcel Moolenaar 	case 0x00: /* read_ldt */
87784569dffSMaxim Sobolev 		ldt.start = 0;
87884569dffSMaxim Sobolev 		ldt.descs = uap->ptr;
87984569dffSMaxim Sobolev 		ldt.num = uap->bytecount / sizeof(union descriptor);
88084569dffSMaxim Sobolev 		error = i386_get_ldt(td, &ldt);
881b40ce416SJulian Elischer 		td->td_retval[0] *= sizeof(union descriptor);
882146aad74SMarcel Moolenaar 		break;
8836259969dSKonstantin Belousov 	case 0x02: /* read_default_ldt = 0 */
8846259969dSKonstantin Belousov 		size = 5*sizeof(struct l_desc_struct);
8856259969dSKonstantin Belousov 		if (size > uap->bytecount)
8866259969dSKonstantin Belousov 			size = uap->bytecount;
8876259969dSKonstantin Belousov 		for (written = error = 0; written < size && error == 0; written++)
8886259969dSKonstantin Belousov 			error = subyte((char *)uap->ptr + written, 0);
8896259969dSKonstantin Belousov 		td->td_retval[0] = written;
8906259969dSKonstantin Belousov 		break;
891146aad74SMarcel Moolenaar 	case 0x01: /* write_ldt */
892146aad74SMarcel Moolenaar 	case 0x11: /* write_ldt */
893146aad74SMarcel Moolenaar 		if (uap->bytecount != sizeof(ld))
894146aad74SMarcel Moolenaar 			return (EINVAL);
895146aad74SMarcel Moolenaar 
896146aad74SMarcel Moolenaar 		error = copyin(uap->ptr, &ld, sizeof(ld));
897146aad74SMarcel Moolenaar 		if (error)
898146aad74SMarcel Moolenaar 			return (error);
899146aad74SMarcel Moolenaar 
90084569dffSMaxim Sobolev 		ldt.start = ld.entry_number;
90184569dffSMaxim Sobolev 		ldt.descs = &desc;
90284569dffSMaxim Sobolev 		ldt.num = 1;
90384569dffSMaxim Sobolev 		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
90484569dffSMaxim Sobolev 		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
90584569dffSMaxim Sobolev 		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
90684569dffSMaxim Sobolev 		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
90784569dffSMaxim Sobolev 		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
908146aad74SMarcel Moolenaar 			(ld.contents << 2);
90984569dffSMaxim Sobolev 		desc.sd.sd_dpl = 3;
91084569dffSMaxim Sobolev 		desc.sd.sd_p = (ld.seg_not_present ^ 1);
91184569dffSMaxim Sobolev 		desc.sd.sd_xx = 0;
91284569dffSMaxim Sobolev 		desc.sd.sd_def32 = ld.seg_32bit;
91384569dffSMaxim Sobolev 		desc.sd.sd_gran = ld.limit_in_pages;
91484569dffSMaxim Sobolev 		error = i386_set_ldt(td, &ldt, &desc);
915146aad74SMarcel Moolenaar 		break;
916146aad74SMarcel Moolenaar 	default:
9177c020cbbSJohn Baldwin 		error = ENOSYS;
918146aad74SMarcel Moolenaar 		break;
919146aad74SMarcel Moolenaar 	}
920146aad74SMarcel Moolenaar 
921146aad74SMarcel Moolenaar 	if (error == EOPNOTSUPP) {
922146aad74SMarcel Moolenaar 		printf("linux: modify_ldt needs kernel option USER_LDT\n");
923146aad74SMarcel Moolenaar 		error = ENOSYS;
924146aad74SMarcel Moolenaar 	}
925146aad74SMarcel Moolenaar 
926146aad74SMarcel Moolenaar 	return (error);
927146aad74SMarcel Moolenaar }
928146aad74SMarcel Moolenaar 
929146aad74SMarcel Moolenaar int
930b40ce416SJulian Elischer linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
931146aad74SMarcel Moolenaar {
9325002a60fSMarcel Moolenaar 	l_osigaction_t osa;
9335002a60fSMarcel Moolenaar 	l_sigaction_t act, oact;
934146aad74SMarcel Moolenaar 	int error;
935146aad74SMarcel Moolenaar 
936146aad74SMarcel Moolenaar #ifdef DEBUG
93724593369SJonathan Lemon 	if (ldebug(sigaction))
93824593369SJonathan Lemon 		printf(ARGS(sigaction, "%d, %p, %p"),
939146aad74SMarcel Moolenaar 		    args->sig, (void *)args->nsa, (void *)args->osa);
940146aad74SMarcel Moolenaar #endif
941146aad74SMarcel Moolenaar 
942146aad74SMarcel Moolenaar 	if (args->nsa != NULL) {
9434b7ef73dSDag-Erling Smørgrav 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
944146aad74SMarcel Moolenaar 		if (error)
945146aad74SMarcel Moolenaar 			return (error);
946146aad74SMarcel Moolenaar 		act.lsa_handler = osa.lsa_handler;
947146aad74SMarcel Moolenaar 		act.lsa_flags = osa.lsa_flags;
948146aad74SMarcel Moolenaar 		act.lsa_restorer = osa.lsa_restorer;
949146aad74SMarcel Moolenaar 		LINUX_SIGEMPTYSET(act.lsa_mask);
950146aad74SMarcel Moolenaar 		act.lsa_mask.__bits[0] = osa.lsa_mask;
951146aad74SMarcel Moolenaar 	}
952146aad74SMarcel Moolenaar 
953b40ce416SJulian Elischer 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
954146aad74SMarcel Moolenaar 	    args->osa ? &oact : NULL);
955146aad74SMarcel Moolenaar 
956146aad74SMarcel Moolenaar 	if (args->osa != NULL && !error) {
957146aad74SMarcel Moolenaar 		osa.lsa_handler = oact.lsa_handler;
958146aad74SMarcel Moolenaar 		osa.lsa_flags = oact.lsa_flags;
959146aad74SMarcel Moolenaar 		osa.lsa_restorer = oact.lsa_restorer;
960146aad74SMarcel Moolenaar 		osa.lsa_mask = oact.lsa_mask.__bits[0];
9614b7ef73dSDag-Erling Smørgrav 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
962146aad74SMarcel Moolenaar 	}
963146aad74SMarcel Moolenaar 
964146aad74SMarcel Moolenaar 	return (error);
965146aad74SMarcel Moolenaar }
966146aad74SMarcel Moolenaar 
967146aad74SMarcel Moolenaar /*
968146aad74SMarcel Moolenaar  * Linux has two extra args, restart and oldmask.  We dont use these,
969146aad74SMarcel Moolenaar  * but it seems that "restart" is actually a context pointer that
970146aad74SMarcel Moolenaar  * enables the signal to happen with a different register set.
971146aad74SMarcel Moolenaar  */
972146aad74SMarcel Moolenaar int
973b40ce416SJulian Elischer linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
974146aad74SMarcel Moolenaar {
975206a5d3aSIan Dowse 	sigset_t sigmask;
9765002a60fSMarcel Moolenaar 	l_sigset_t mask;
977146aad74SMarcel Moolenaar 
978146aad74SMarcel Moolenaar #ifdef DEBUG
97924593369SJonathan Lemon 	if (ldebug(sigsuspend))
98024593369SJonathan Lemon 		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
981146aad74SMarcel Moolenaar #endif
982146aad74SMarcel Moolenaar 
983146aad74SMarcel Moolenaar 	LINUX_SIGEMPTYSET(mask);
984146aad74SMarcel Moolenaar 	mask.__bits[0] = args->mask;
985206a5d3aSIan Dowse 	linux_to_bsd_sigset(&mask, &sigmask);
986206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
987146aad74SMarcel Moolenaar }
988146aad74SMarcel Moolenaar 
989146aad74SMarcel Moolenaar int
990b07cd97eSMark Murray linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
991146aad74SMarcel Moolenaar {
9925002a60fSMarcel Moolenaar 	l_sigset_t lmask;
993206a5d3aSIan Dowse 	sigset_t sigmask;
994146aad74SMarcel Moolenaar 	int error;
995146aad74SMarcel Moolenaar 
996146aad74SMarcel Moolenaar #ifdef DEBUG
99724593369SJonathan Lemon 	if (ldebug(rt_sigsuspend))
99824593369SJonathan Lemon 		printf(ARGS(rt_sigsuspend, "%p, %d"),
999146aad74SMarcel Moolenaar 		    (void *)uap->newset, uap->sigsetsize);
1000146aad74SMarcel Moolenaar #endif
1001146aad74SMarcel Moolenaar 
10025002a60fSMarcel Moolenaar 	if (uap->sigsetsize != sizeof(l_sigset_t))
1003146aad74SMarcel Moolenaar 		return (EINVAL);
1004146aad74SMarcel Moolenaar 
10055002a60fSMarcel Moolenaar 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
1006146aad74SMarcel Moolenaar 	if (error)
1007146aad74SMarcel Moolenaar 		return (error);
1008146aad74SMarcel Moolenaar 
1009206a5d3aSIan Dowse 	linux_to_bsd_sigset(&lmask, &sigmask);
1010206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
1011146aad74SMarcel Moolenaar }
1012146aad74SMarcel Moolenaar 
1013146aad74SMarcel Moolenaar int
1014b40ce416SJulian Elischer linux_pause(struct thread *td, struct linux_pause_args *args)
1015146aad74SMarcel Moolenaar {
1016b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1017206a5d3aSIan Dowse 	sigset_t sigmask;
1018146aad74SMarcel Moolenaar 
1019146aad74SMarcel Moolenaar #ifdef DEBUG
102024593369SJonathan Lemon 	if (ldebug(pause))
102124593369SJonathan Lemon 		printf(ARGS(pause, ""));
1022146aad74SMarcel Moolenaar #endif
1023146aad74SMarcel Moolenaar 
1024fdfdfb78SJohn Baldwin 	PROC_LOCK(p);
10254093529dSJeff Roberson 	sigmask = td->td_sigmask;
1026fdfdfb78SJohn Baldwin 	PROC_UNLOCK(p);
1027206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
1028146aad74SMarcel Moolenaar }
1029146aad74SMarcel Moolenaar 
1030146aad74SMarcel Moolenaar int
1031b40ce416SJulian Elischer linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
1032146aad74SMarcel Moolenaar {
1033206a5d3aSIan Dowse 	stack_t ss, oss;
10345002a60fSMarcel Moolenaar 	l_stack_t lss;
1035146aad74SMarcel Moolenaar 	int error;
1036146aad74SMarcel Moolenaar 
1037146aad74SMarcel Moolenaar #ifdef DEBUG
103824593369SJonathan Lemon 	if (ldebug(sigaltstack))
103924593369SJonathan Lemon 		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1040146aad74SMarcel Moolenaar #endif
1041146aad74SMarcel Moolenaar 
1042206a5d3aSIan Dowse 	if (uap->uss != NULL) {
10435002a60fSMarcel Moolenaar 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
1044146aad74SMarcel Moolenaar 		if (error)
1045146aad74SMarcel Moolenaar 			return (error);
1046146aad74SMarcel Moolenaar 
1047206a5d3aSIan Dowse 		ss.ss_sp = lss.ss_sp;
1048206a5d3aSIan Dowse 		ss.ss_size = lss.ss_size;
1049206a5d3aSIan Dowse 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
1050931a7258SAndrew Gallatin 	}
1051ef36ad69SJohn Baldwin 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
1052ef36ad69SJohn Baldwin 	    (uap->uoss != NULL) ? &oss : NULL);
1053206a5d3aSIan Dowse 	if (!error && uap->uoss != NULL) {
1054206a5d3aSIan Dowse 		lss.ss_sp = oss.ss_sp;
1055206a5d3aSIan Dowse 		lss.ss_size = oss.ss_size;
1056206a5d3aSIan Dowse 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
10575002a60fSMarcel Moolenaar 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
1058146aad74SMarcel Moolenaar 	}
1059146aad74SMarcel Moolenaar 
1060146aad74SMarcel Moolenaar 	return (error);
1061146aad74SMarcel Moolenaar }
10623ad9c842SMaxim Sobolev 
10633ad9c842SMaxim Sobolev int
10643ad9c842SMaxim Sobolev linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
10653ad9c842SMaxim Sobolev {
10663ad9c842SMaxim Sobolev 	struct ftruncate_args sa;
10673ad9c842SMaxim Sobolev 
10683ad9c842SMaxim Sobolev #ifdef DEBUG
10693ad9c842SMaxim Sobolev 	if (ldebug(ftruncate64))
1070b45bbfc3SBruce Evans 		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
1071b45bbfc3SBruce Evans 		    (intmax_t)args->length);
10723ad9c842SMaxim Sobolev #endif
10733ad9c842SMaxim Sobolev 
10743ad9c842SMaxim Sobolev 	sa.fd = args->fd;
10753ad9c842SMaxim Sobolev 	sa.length = args->length;
10763ad9c842SMaxim Sobolev 	return ftruncate(td, &sa);
10773ad9c842SMaxim Sobolev }
10781bc85c0dSDoug Rabson 
10791bc85c0dSDoug Rabson int
10801bc85c0dSDoug Rabson linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
10811bc85c0dSDoug Rabson {
10829b44bfc5SAlexander Leidinger 	struct l_user_desc info;
10839b44bfc5SAlexander Leidinger 	int error;
10849b44bfc5SAlexander Leidinger 	int idx;
10859b44bfc5SAlexander Leidinger 	int a[2];
10869b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
10879b44bfc5SAlexander Leidinger 
10889b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
10899b44bfc5SAlexander Leidinger 	if (error)
10909b44bfc5SAlexander Leidinger 		return (error);
10919b44bfc5SAlexander Leidinger 
10929b44bfc5SAlexander Leidinger #ifdef DEBUG
10939b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
10949b44bfc5SAlexander Leidinger 	   	printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
10959b44bfc5SAlexander Leidinger 		      info.entry_number,
10969b44bfc5SAlexander Leidinger       		      info.base_addr,
10979b44bfc5SAlexander Leidinger       		      info.limit,
10989b44bfc5SAlexander Leidinger       		      info.seg_32bit,
10999b44bfc5SAlexander Leidinger 		      info.contents,
11009b44bfc5SAlexander Leidinger       		      info.read_exec_only,
11019b44bfc5SAlexander Leidinger       		      info.limit_in_pages,
11029b44bfc5SAlexander Leidinger       		      info.seg_not_present,
11039b44bfc5SAlexander Leidinger       		      info.useable);
11049b44bfc5SAlexander Leidinger #endif
11059b44bfc5SAlexander Leidinger 
11069b44bfc5SAlexander Leidinger 	idx = info.entry_number;
11070eef2f8aSAlexander Leidinger 	/*
1108802e08a3SAlexander Leidinger 	 * Semantics of linux version: every thread in the system has array of
1109802e08a3SAlexander Leidinger 	 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
1110802e08a3SAlexander Leidinger 	 * syscall loads one of the selected tls decriptors with a value and
1111802e08a3SAlexander Leidinger 	 * also loads GDT descriptors 6, 7 and 8 with the content of the
1112802e08a3SAlexander Leidinger 	 * per-thread descriptors.
11139b44bfc5SAlexander Leidinger 	 *
1114802e08a3SAlexander Leidinger 	 * Semantics of fbsd version: I think we can ignore that linux has 3
1115802e08a3SAlexander Leidinger 	 * per-thread descriptors and use just the 1st one. The tls_array[]
1116802e08a3SAlexander Leidinger 	 * is used only in set/get-thread_area() syscalls and for loading the
1117802e08a3SAlexander Leidinger 	 * GDT descriptors. In fbsd we use just one GDT descriptor for TLS so
1118802e08a3SAlexander Leidinger 	 * we will load just one.
1119802e08a3SAlexander Leidinger 	 *
1120802e08a3SAlexander Leidinger 	 * XXX: this doesn't work when a user space process tries to use more
1121802e08a3SAlexander Leidinger 	 * than 1 TLS segment. Comment in the linux sources says wine might do
1122802e08a3SAlexander Leidinger 	 * this.
11231bc85c0dSDoug Rabson 	 */
11249b44bfc5SAlexander Leidinger 
11250eef2f8aSAlexander Leidinger 	/*
11260eef2f8aSAlexander Leidinger 	 * we support just GLIBC TLS now
11279b44bfc5SAlexander Leidinger 	 * we should let 3 proceed as well because we use this segment so
11289b44bfc5SAlexander Leidinger 	 * if code does two subsequent calls it should succeed
11299b44bfc5SAlexander Leidinger 	 */
11309b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != -1 && idx != 3)
11319b44bfc5SAlexander Leidinger 		return (EINVAL);
11329b44bfc5SAlexander Leidinger 
11330eef2f8aSAlexander Leidinger 	/*
11340eef2f8aSAlexander Leidinger 	 * we have to copy out the GDT entry we use
11359b44bfc5SAlexander Leidinger 	 * FreeBSD uses GDT entry #3 for storing %gs so load that
1136802e08a3SAlexander Leidinger 	 *
1137802e08a3SAlexander Leidinger 	 * XXX: what if a user space program doesn't check this value and tries
11389b44bfc5SAlexander Leidinger 	 * to use 6, 7 or 8?
11399b44bfc5SAlexander Leidinger 	 */
11409b44bfc5SAlexander Leidinger 	idx = info.entry_number = 3;
11419b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
11429b44bfc5SAlexander Leidinger 	if (error)
11439b44bfc5SAlexander Leidinger 		return (error);
11449b44bfc5SAlexander Leidinger 
1145a4e3bad7SJung-uk Kim 	if (LINUX_LDT_empty(&info)) {
11469b44bfc5SAlexander Leidinger 		a[0] = 0;
11479b44bfc5SAlexander Leidinger 		a[1] = 0;
11489b44bfc5SAlexander Leidinger 	} else {
1149a4e3bad7SJung-uk Kim 		a[0] = LINUX_LDT_entry_a(&info);
1150a4e3bad7SJung-uk Kim 		a[1] = LINUX_LDT_entry_b(&info);
11511bc85c0dSDoug Rabson 	}
11521bc85c0dSDoug Rabson 
11539b44bfc5SAlexander Leidinger 	memcpy(&sd, &a, sizeof(a));
11549b44bfc5SAlexander Leidinger #ifdef DEBUG
11559b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
11569b44bfc5SAlexander Leidinger 	   	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,
11579b44bfc5SAlexander Leidinger 			sd.sd_hibase,
11589b44bfc5SAlexander Leidinger 			sd.sd_lolimit,
11599b44bfc5SAlexander Leidinger 			sd.sd_hilimit,
11609b44bfc5SAlexander Leidinger 			sd.sd_type,
11619b44bfc5SAlexander Leidinger 			sd.sd_dpl,
11629b44bfc5SAlexander Leidinger 			sd.sd_p,
11639b44bfc5SAlexander Leidinger 			sd.sd_xx,
11649b44bfc5SAlexander Leidinger 			sd.sd_def32,
11659b44bfc5SAlexander Leidinger 			sd.sd_gran);
11669b44bfc5SAlexander Leidinger #endif
11671bc85c0dSDoug Rabson 
11689b44bfc5SAlexander Leidinger 	/* this is taken from i386 version of cpu_set_user_tls() */
11699b44bfc5SAlexander Leidinger 	critical_enter();
11709b44bfc5SAlexander Leidinger 	/* set %gs */
11719b44bfc5SAlexander Leidinger 	td->td_pcb->pcb_gsd = sd;
11729b44bfc5SAlexander Leidinger 	PCPU_GET(fsgs_gdt)[1] = sd;
11739b44bfc5SAlexander Leidinger 	load_gs(GSEL(GUGS_SEL, SEL_UPL));
11749b44bfc5SAlexander Leidinger 	critical_exit();
11759b44bfc5SAlexander Leidinger 
11761bc85c0dSDoug Rabson 	return (0);
11771bc85c0dSDoug Rabson }
11781bc85c0dSDoug Rabson 
11791bc85c0dSDoug Rabson int
11809b44bfc5SAlexander Leidinger linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
11811bc85c0dSDoug Rabson {
11821bc85c0dSDoug Rabson 
11839b44bfc5SAlexander Leidinger 	struct l_user_desc info;
11849b44bfc5SAlexander Leidinger 	int error;
11859b44bfc5SAlexander Leidinger 	int idx;
11869b44bfc5SAlexander Leidinger 	struct l_desc_struct desc;
11879b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
11889b44bfc5SAlexander Leidinger 
11899b44bfc5SAlexander Leidinger #ifdef DEBUG
11909b44bfc5SAlexander Leidinger 	if (ldebug(get_thread_area))
11919b44bfc5SAlexander Leidinger 		printf(ARGS(get_thread_area, "%p"), args->desc);
11929b44bfc5SAlexander Leidinger #endif
11939b44bfc5SAlexander Leidinger 
11949b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
11959b44bfc5SAlexander Leidinger 	if (error)
11969b44bfc5SAlexander Leidinger 		return (error);
11979b44bfc5SAlexander Leidinger 
11989b44bfc5SAlexander Leidinger 	idx = info.entry_number;
11999b44bfc5SAlexander Leidinger 	/* XXX: I am not sure if we want 3 to be allowed too. */
12009b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != 3)
12019b44bfc5SAlexander Leidinger 		return (EINVAL);
12029b44bfc5SAlexander Leidinger 
12039b44bfc5SAlexander Leidinger 	idx = 3;
12049b44bfc5SAlexander Leidinger 
12059b44bfc5SAlexander Leidinger 	memset(&info, 0, sizeof(info));
12069b44bfc5SAlexander Leidinger 
12079b44bfc5SAlexander Leidinger 	sd = PCPU_GET(fsgs_gdt)[1];
12089b44bfc5SAlexander Leidinger 
12099b44bfc5SAlexander Leidinger 	memcpy(&desc, &sd, sizeof(desc));
12109b44bfc5SAlexander Leidinger 
12119b44bfc5SAlexander Leidinger 	info.entry_number = idx;
1212a4e3bad7SJung-uk Kim 	info.base_addr = LINUX_GET_BASE(&desc);
1213a4e3bad7SJung-uk Kim 	info.limit = LINUX_GET_LIMIT(&desc);
1214a4e3bad7SJung-uk Kim 	info.seg_32bit = LINUX_GET_32BIT(&desc);
1215a4e3bad7SJung-uk Kim 	info.contents = LINUX_GET_CONTENTS(&desc);
1216a4e3bad7SJung-uk Kim 	info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
1217a4e3bad7SJung-uk Kim 	info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
1218a4e3bad7SJung-uk Kim 	info.seg_not_present = !LINUX_GET_PRESENT(&desc);
1219a4e3bad7SJung-uk Kim 	info.useable = LINUX_GET_USEABLE(&desc);
12209b44bfc5SAlexander Leidinger 
12219b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
12229b44bfc5SAlexander Leidinger 	if (error)
12239b44bfc5SAlexander Leidinger 	   	return (EFAULT);
12249b44bfc5SAlexander Leidinger 
12259b44bfc5SAlexander Leidinger 	return (0);
12269b44bfc5SAlexander Leidinger }
12279b44bfc5SAlexander Leidinger 
12289b44bfc5SAlexander Leidinger /* copied from kern/kern_time.c */
12299b44bfc5SAlexander Leidinger int
12309b44bfc5SAlexander Leidinger linux_timer_create(struct thread *td, struct linux_timer_create_args *args)
12319b44bfc5SAlexander Leidinger {
12329b44bfc5SAlexander Leidinger    	return ktimer_create(td, (struct ktimer_create_args *) args);
12339b44bfc5SAlexander Leidinger }
12349b44bfc5SAlexander Leidinger 
12359b44bfc5SAlexander Leidinger int
12369b44bfc5SAlexander Leidinger linux_timer_settime(struct thread *td, struct linux_timer_settime_args *args)
12379b44bfc5SAlexander Leidinger {
12389b44bfc5SAlexander Leidinger    	return ktimer_settime(td, (struct ktimer_settime_args *) args);
12399b44bfc5SAlexander Leidinger }
12409b44bfc5SAlexander Leidinger 
12419b44bfc5SAlexander Leidinger int
12429b44bfc5SAlexander Leidinger linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *args)
12439b44bfc5SAlexander Leidinger {
12449b44bfc5SAlexander Leidinger    	return ktimer_gettime(td, (struct ktimer_gettime_args *) args);
12459b44bfc5SAlexander Leidinger }
12469b44bfc5SAlexander Leidinger 
12479b44bfc5SAlexander Leidinger int
12489b44bfc5SAlexander Leidinger linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *args)
12499b44bfc5SAlexander Leidinger {
12509b44bfc5SAlexander Leidinger    	return ktimer_getoverrun(td, (struct ktimer_getoverrun_args *) args);
12519b44bfc5SAlexander Leidinger }
12529b44bfc5SAlexander Leidinger 
12539b44bfc5SAlexander Leidinger int
12549b44bfc5SAlexander Leidinger linux_timer_delete(struct thread *td, struct linux_timer_delete_args *args)
12559b44bfc5SAlexander Leidinger {
12569b44bfc5SAlexander Leidinger    	return ktimer_delete(td, (struct ktimer_delete_args *) args);
12579b44bfc5SAlexander Leidinger }
12589b44bfc5SAlexander Leidinger 
12599b44bfc5SAlexander Leidinger /* XXX: this wont work with module - convert it */
12609b44bfc5SAlexander Leidinger int
12619b44bfc5SAlexander Leidinger linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
12629b44bfc5SAlexander Leidinger {
12639b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
12649b44bfc5SAlexander Leidinger    	return kmq_open(td, (struct kmq_open_args *) args);
12659b44bfc5SAlexander Leidinger #else
12669b44bfc5SAlexander Leidinger 	return (ENOSYS);
12679b44bfc5SAlexander Leidinger #endif
12689b44bfc5SAlexander Leidinger }
12699b44bfc5SAlexander Leidinger 
12709b44bfc5SAlexander Leidinger int
12719b44bfc5SAlexander Leidinger linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
12729b44bfc5SAlexander Leidinger {
12739b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
12749b44bfc5SAlexander Leidinger    	return kmq_unlink(td, (struct kmq_unlink_args *) args);
12759b44bfc5SAlexander Leidinger #else
12769b44bfc5SAlexander Leidinger 	return (ENOSYS);
12779b44bfc5SAlexander Leidinger #endif
12789b44bfc5SAlexander Leidinger }
12799b44bfc5SAlexander Leidinger 
12809b44bfc5SAlexander Leidinger int
12819b44bfc5SAlexander Leidinger linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
12829b44bfc5SAlexander Leidinger {
12839b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
12849b44bfc5SAlexander Leidinger    	return kmq_timedsend(td, (struct kmq_timedsend_args *) args);
12859b44bfc5SAlexander Leidinger #else
12869b44bfc5SAlexander Leidinger 	return (ENOSYS);
12879b44bfc5SAlexander Leidinger #endif
12889b44bfc5SAlexander Leidinger }
12899b44bfc5SAlexander Leidinger 
12909b44bfc5SAlexander Leidinger int
12919b44bfc5SAlexander Leidinger linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
12929b44bfc5SAlexander Leidinger {
12939b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
12949b44bfc5SAlexander Leidinger    	return kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
12959b44bfc5SAlexander Leidinger #else
12969b44bfc5SAlexander Leidinger 	return (ENOSYS);
12979b44bfc5SAlexander Leidinger #endif
12989b44bfc5SAlexander Leidinger }
12999b44bfc5SAlexander Leidinger 
13009b44bfc5SAlexander Leidinger int
13019b44bfc5SAlexander Leidinger linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
13029b44bfc5SAlexander Leidinger {
13039b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
13049b44bfc5SAlexander Leidinger 	return kmq_notify(td, (struct kmq_notify_args *) args);
13059b44bfc5SAlexander Leidinger #else
13069b44bfc5SAlexander Leidinger 	return (ENOSYS);
13079b44bfc5SAlexander Leidinger #endif
13089b44bfc5SAlexander Leidinger }
13099b44bfc5SAlexander Leidinger 
13109b44bfc5SAlexander Leidinger int
13119b44bfc5SAlexander Leidinger linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
13129b44bfc5SAlexander Leidinger {
13139b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
13149b44bfc5SAlexander Leidinger    	return kmq_setattr(td, (struct kmq_setattr_args *) args);
13159b44bfc5SAlexander Leidinger #else
13169b44bfc5SAlexander Leidinger 	return (ENOSYS);
13179b44bfc5SAlexander Leidinger #endif
13181bc85c0dSDoug Rabson }
13191bc85c0dSDoug Rabson 
1320adc7ece0SDmitry Chagin int
1321adc7ece0SDmitry Chagin linux_wait4(struct thread *td, struct linux_wait4_args *args)
1322adc7ece0SDmitry Chagin {
1323adc7ece0SDmitry Chagin 	int error, options;
1324adc7ece0SDmitry Chagin 	struct rusage ru, *rup;
1325adc7ece0SDmitry Chagin 
1326adc7ece0SDmitry Chagin #ifdef DEBUG
1327adc7ece0SDmitry Chagin 	if (ldebug(wait4))
1328adc7ece0SDmitry Chagin 		printf(ARGS(wait4, "%d, %p, %d, %p"),
1329adc7ece0SDmitry Chagin 		    args->pid, (void *)args->status, args->options,
1330adc7ece0SDmitry Chagin 		    (void *)args->rusage);
1331adc7ece0SDmitry Chagin #endif
1332adc7ece0SDmitry Chagin 
1333adc7ece0SDmitry Chagin 	options = (args->options & (WNOHANG | WUNTRACED));
1334adc7ece0SDmitry Chagin 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1335adc7ece0SDmitry Chagin 	if (args->options & __WCLONE)
1336adc7ece0SDmitry Chagin 		options |= WLINUXCLONE;
1337adc7ece0SDmitry Chagin 
1338adc7ece0SDmitry Chagin 	if (args->rusage != NULL)
1339adc7ece0SDmitry Chagin 		rup = &ru;
1340adc7ece0SDmitry Chagin 	else
1341adc7ece0SDmitry Chagin 		rup = NULL;
1342adc7ece0SDmitry Chagin 	error = linux_common_wait(td, args->pid, args->status, options, rup);
1343adc7ece0SDmitry Chagin 	if (error)
1344adc7ece0SDmitry Chagin 		return (error);
1345adc7ece0SDmitry Chagin 	if (args->rusage != NULL)
1346adc7ece0SDmitry Chagin 		error = copyout(&ru, args->rusage, sizeof(ru));
1347adc7ece0SDmitry Chagin 
1348adc7ece0SDmitry Chagin 	return (error);
1349adc7ece0SDmitry Chagin }
1350