xref: /freebsd/sys/i386/linux/linux_machdep.c (revision acd3428b7d3e94cef0e1881c868cb4b131d4ff41)
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>
52146aad74SMarcel Moolenaar 
53146aad74SMarcel Moolenaar #include <machine/frame.h>
54146aad74SMarcel Moolenaar #include <machine/psl.h>
55146aad74SMarcel Moolenaar #include <machine/segments.h>
56146aad74SMarcel Moolenaar #include <machine/sysarch.h>
57146aad74SMarcel Moolenaar 
58242fae60SAndrew Gallatin #include <vm/vm.h>
59242fae60SAndrew Gallatin #include <vm/pmap.h>
60242fae60SAndrew Gallatin #include <vm/vm_map.h>
61242fae60SAndrew Gallatin 
62146aad74SMarcel Moolenaar #include <i386/linux/linux.h>
63ebea8660SMarcel Moolenaar #include <i386/linux/linux_proto.h>
64146aad74SMarcel Moolenaar #include <compat/linux/linux_ipc.h>
65146aad74SMarcel Moolenaar #include <compat/linux/linux_signal.h>
66146aad74SMarcel Moolenaar #include <compat/linux/linux_util.h>
679b44bfc5SAlexander Leidinger #include <compat/linux/linux_emul.h>
689b44bfc5SAlexander Leidinger 
699b44bfc5SAlexander Leidinger #include <i386/include/pcb.h>			/* needed for pcb definition in linux_set_thread_area */
709b44bfc5SAlexander Leidinger 
719b44bfc5SAlexander Leidinger #include "opt_posix.h"
729b44bfc5SAlexander Leidinger 
739b44bfc5SAlexander Leidinger extern struct sysentvec elf32_freebsd_sysvec;	/* defined in i386/i386/elf_machdep.c */
74146aad74SMarcel Moolenaar 
755002a60fSMarcel Moolenaar struct l_descriptor {
765002a60fSMarcel Moolenaar 	l_uint		entry_number;
775002a60fSMarcel Moolenaar 	l_ulong		base_addr;
785002a60fSMarcel Moolenaar 	l_uint		limit;
795002a60fSMarcel Moolenaar 	l_uint		seg_32bit:1;
805002a60fSMarcel Moolenaar 	l_uint		contents:2;
815002a60fSMarcel Moolenaar 	l_uint		read_exec_only:1;
825002a60fSMarcel Moolenaar 	l_uint		limit_in_pages:1;
835002a60fSMarcel Moolenaar 	l_uint		seg_not_present:1;
845002a60fSMarcel Moolenaar 	l_uint		useable:1;
85146aad74SMarcel Moolenaar };
86146aad74SMarcel Moolenaar 
875002a60fSMarcel Moolenaar struct l_old_select_argv {
885002a60fSMarcel Moolenaar 	l_int		nfds;
895002a60fSMarcel Moolenaar 	l_fd_set	*readfds;
905002a60fSMarcel Moolenaar 	l_fd_set	*writefds;
915002a60fSMarcel Moolenaar 	l_fd_set	*exceptfds;
925002a60fSMarcel Moolenaar 	struct l_timeval	*timeout;
93146aad74SMarcel Moolenaar };
94146aad74SMarcel Moolenaar 
95146aad74SMarcel Moolenaar int
96931a7258SAndrew Gallatin linux_to_bsd_sigaltstack(int lsa)
97931a7258SAndrew Gallatin {
98931a7258SAndrew Gallatin 	int bsa = 0;
99931a7258SAndrew Gallatin 
100931a7258SAndrew Gallatin 	if (lsa & LINUX_SS_DISABLE)
101931a7258SAndrew Gallatin 		bsa |= SS_DISABLE;
102931a7258SAndrew Gallatin 	if (lsa & LINUX_SS_ONSTACK)
103931a7258SAndrew Gallatin 		bsa |= SS_ONSTACK;
104931a7258SAndrew Gallatin 	return (bsa);
105931a7258SAndrew Gallatin }
106931a7258SAndrew Gallatin 
107931a7258SAndrew Gallatin int
108931a7258SAndrew Gallatin bsd_to_linux_sigaltstack(int bsa)
109931a7258SAndrew Gallatin {
110931a7258SAndrew Gallatin 	int lsa = 0;
111931a7258SAndrew Gallatin 
112931a7258SAndrew Gallatin 	if (bsa & SS_DISABLE)
113931a7258SAndrew Gallatin 		lsa |= LINUX_SS_DISABLE;
114931a7258SAndrew Gallatin 	if (bsa & SS_ONSTACK)
115931a7258SAndrew Gallatin 		lsa |= LINUX_SS_ONSTACK;
116931a7258SAndrew Gallatin 	return (lsa);
117931a7258SAndrew Gallatin }
118931a7258SAndrew Gallatin 
119931a7258SAndrew Gallatin int
120b40ce416SJulian Elischer linux_execve(struct thread *td, struct linux_execve_args *args)
121146aad74SMarcel Moolenaar {
122610ecfe0SMaxim Sobolev 	int error;
123610ecfe0SMaxim Sobolev 	char *newpath;
124610ecfe0SMaxim Sobolev 	struct image_args eargs;
125146aad74SMarcel Moolenaar 
126d9e97471SJohn Baldwin 	LCONVPATHEXIST(td, args->path, &newpath);
127146aad74SMarcel Moolenaar 
128146aad74SMarcel Moolenaar #ifdef DEBUG
12924593369SJonathan Lemon 	if (ldebug(execve))
130610ecfe0SMaxim Sobolev 		printf(ARGS(execve, "%s"), newpath);
131146aad74SMarcel Moolenaar #endif
132146aad74SMarcel Moolenaar 
133610ecfe0SMaxim Sobolev 	error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
134610ecfe0SMaxim Sobolev 	    args->argp, args->envp);
135610ecfe0SMaxim Sobolev 	free(newpath, M_TEMP);
136610ecfe0SMaxim Sobolev 	if (error == 0)
137c035ac04SMaxim Sobolev 		error = kern_execve(td, &eargs, NULL);
1389b44bfc5SAlexander Leidinger 	if (error == 0)
1399b44bfc5SAlexander Leidinger 	   	/* linux process can exec fbsd one, dont attempt
1409b44bfc5SAlexander Leidinger 		 * to create emuldata for such process using
1419b44bfc5SAlexander Leidinger 		 * linux_proc_init, this leads to a panic on KASSERT
1429b44bfc5SAlexander Leidinger 		 * because such process has p->p_emuldata == NULL
1439b44bfc5SAlexander Leidinger 		 */
1449b44bfc5SAlexander Leidinger 	   	if (td->td_proc->p_sysent == &elf_linux_sysvec)
1459b44bfc5SAlexander Leidinger    		   	error = linux_proc_init(td, 0, 0);
146610ecfe0SMaxim Sobolev 	return (error);
147146aad74SMarcel Moolenaar }
148146aad74SMarcel Moolenaar 
1495002a60fSMarcel Moolenaar struct l_ipc_kludge {
1505002a60fSMarcel Moolenaar 	struct l_msgbuf *msgp;
1515002a60fSMarcel Moolenaar 	l_long msgtyp;
1525002a60fSMarcel Moolenaar };
1535002a60fSMarcel Moolenaar 
154146aad74SMarcel Moolenaar int
155b40ce416SJulian Elischer linux_ipc(struct thread *td, struct linux_ipc_args *args)
156146aad74SMarcel Moolenaar {
1575002a60fSMarcel Moolenaar 
1585002a60fSMarcel Moolenaar 	switch (args->what & 0xFFFF) {
1595002a60fSMarcel Moolenaar 	case LINUX_SEMOP: {
1605002a60fSMarcel Moolenaar 		struct linux_semop_args a;
1615002a60fSMarcel Moolenaar 
1625002a60fSMarcel Moolenaar 		a.semid = args->arg1;
1635002a60fSMarcel Moolenaar 		a.tsops = args->ptr;
1645002a60fSMarcel Moolenaar 		a.nsops = args->arg2;
165b40ce416SJulian Elischer 		return (linux_semop(td, &a));
1665002a60fSMarcel Moolenaar 	}
1675002a60fSMarcel Moolenaar 	case LINUX_SEMGET: {
1685002a60fSMarcel Moolenaar 		struct linux_semget_args a;
1695002a60fSMarcel Moolenaar 
1705002a60fSMarcel Moolenaar 		a.key = args->arg1;
1715002a60fSMarcel Moolenaar 		a.nsems = args->arg2;
1725002a60fSMarcel Moolenaar 		a.semflg = args->arg3;
173b40ce416SJulian Elischer 		return (linux_semget(td, &a));
1745002a60fSMarcel Moolenaar 	}
1755002a60fSMarcel Moolenaar 	case LINUX_SEMCTL: {
1765002a60fSMarcel Moolenaar 		struct linux_semctl_args a;
1775002a60fSMarcel Moolenaar 		int error;
1785002a60fSMarcel Moolenaar 
1795002a60fSMarcel Moolenaar 		a.semid = args->arg1;
1805002a60fSMarcel Moolenaar 		a.semnum = args->arg2;
1815002a60fSMarcel Moolenaar 		a.cmd = args->arg3;
1824b7ef73dSDag-Erling Smørgrav 		error = copyin(args->ptr, &a.arg, sizeof(a.arg));
1835002a60fSMarcel Moolenaar 		if (error)
1845002a60fSMarcel Moolenaar 			return (error);
185b40ce416SJulian Elischer 		return (linux_semctl(td, &a));
1865002a60fSMarcel Moolenaar 	}
1875002a60fSMarcel Moolenaar 	case LINUX_MSGSND: {
1885002a60fSMarcel Moolenaar 		struct linux_msgsnd_args a;
1895002a60fSMarcel Moolenaar 
1905002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
1915002a60fSMarcel Moolenaar 		a.msgp = args->ptr;
1925002a60fSMarcel Moolenaar 		a.msgsz = args->arg2;
1935002a60fSMarcel Moolenaar 		a.msgflg = args->arg3;
194b40ce416SJulian Elischer 		return (linux_msgsnd(td, &a));
1955002a60fSMarcel Moolenaar 	}
1965002a60fSMarcel Moolenaar 	case LINUX_MSGRCV: {
1975002a60fSMarcel Moolenaar 		struct linux_msgrcv_args a;
1985002a60fSMarcel Moolenaar 
1995002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
2005002a60fSMarcel Moolenaar 		a.msgsz = args->arg2;
2015002a60fSMarcel Moolenaar 		a.msgflg = args->arg3;
2025002a60fSMarcel Moolenaar 		if ((args->what >> 16) == 0) {
2035002a60fSMarcel Moolenaar 			struct l_ipc_kludge tmp;
2045002a60fSMarcel Moolenaar 			int error;
2055002a60fSMarcel Moolenaar 
2065002a60fSMarcel Moolenaar 			if (args->ptr == NULL)
2075002a60fSMarcel Moolenaar 				return (EINVAL);
2084b7ef73dSDag-Erling Smørgrav 			error = copyin(args->ptr, &tmp, sizeof(tmp));
2095002a60fSMarcel Moolenaar 			if (error)
2105002a60fSMarcel Moolenaar 				return (error);
2115002a60fSMarcel Moolenaar 			a.msgp = tmp.msgp;
2125002a60fSMarcel Moolenaar 			a.msgtyp = tmp.msgtyp;
2135002a60fSMarcel Moolenaar 		} else {
2145002a60fSMarcel Moolenaar 			a.msgp = args->ptr;
2155002a60fSMarcel Moolenaar 			a.msgtyp = args->arg5;
2165002a60fSMarcel Moolenaar 		}
217b40ce416SJulian Elischer 		return (linux_msgrcv(td, &a));
2185002a60fSMarcel Moolenaar 	}
2195002a60fSMarcel Moolenaar 	case LINUX_MSGGET: {
2205002a60fSMarcel Moolenaar 		struct linux_msgget_args a;
2215002a60fSMarcel Moolenaar 
2225002a60fSMarcel Moolenaar 		a.key = args->arg1;
2235002a60fSMarcel Moolenaar 		a.msgflg = args->arg2;
224b40ce416SJulian Elischer 		return (linux_msgget(td, &a));
2255002a60fSMarcel Moolenaar 	}
2265002a60fSMarcel Moolenaar 	case LINUX_MSGCTL: {
2275002a60fSMarcel Moolenaar 		struct linux_msgctl_args a;
2285002a60fSMarcel Moolenaar 
2295002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
2305002a60fSMarcel Moolenaar 		a.cmd = args->arg2;
2315002a60fSMarcel Moolenaar 		a.buf = args->ptr;
232b40ce416SJulian Elischer 		return (linux_msgctl(td, &a));
2335002a60fSMarcel Moolenaar 	}
2345002a60fSMarcel Moolenaar 	case LINUX_SHMAT: {
2355002a60fSMarcel Moolenaar 		struct linux_shmat_args a;
2365002a60fSMarcel Moolenaar 
2375002a60fSMarcel Moolenaar 		a.shmid = args->arg1;
2385002a60fSMarcel Moolenaar 		a.shmaddr = args->ptr;
2395002a60fSMarcel Moolenaar 		a.shmflg = args->arg2;
2405002a60fSMarcel Moolenaar 		a.raddr = (l_ulong *)args->arg3;
241b40ce416SJulian Elischer 		return (linux_shmat(td, &a));
2425002a60fSMarcel Moolenaar 	}
2435002a60fSMarcel Moolenaar 	case LINUX_SHMDT: {
2445002a60fSMarcel Moolenaar 		struct linux_shmdt_args a;
2455002a60fSMarcel Moolenaar 
2465002a60fSMarcel Moolenaar 		a.shmaddr = args->ptr;
247b40ce416SJulian Elischer 		return (linux_shmdt(td, &a));
2485002a60fSMarcel Moolenaar 	}
2495002a60fSMarcel Moolenaar 	case LINUX_SHMGET: {
2505002a60fSMarcel Moolenaar 		struct linux_shmget_args a;
2515002a60fSMarcel Moolenaar 
2525002a60fSMarcel Moolenaar 		a.key = args->arg1;
2535002a60fSMarcel Moolenaar 		a.size = args->arg2;
2545002a60fSMarcel Moolenaar 		a.shmflg = args->arg3;
255b40ce416SJulian Elischer 		return (linux_shmget(td, &a));
2565002a60fSMarcel Moolenaar 	}
2575002a60fSMarcel Moolenaar 	case LINUX_SHMCTL: {
2585002a60fSMarcel Moolenaar 		struct linux_shmctl_args a;
2595002a60fSMarcel Moolenaar 
2605002a60fSMarcel Moolenaar 		a.shmid = args->arg1;
2615002a60fSMarcel Moolenaar 		a.cmd = args->arg2;
2625002a60fSMarcel Moolenaar 		a.buf = args->ptr;
263b40ce416SJulian Elischer 		return (linux_shmctl(td, &a));
2645002a60fSMarcel Moolenaar 	}
2655002a60fSMarcel Moolenaar 	default:
2665002a60fSMarcel Moolenaar 		break;
267146aad74SMarcel Moolenaar 	}
268146aad74SMarcel Moolenaar 
2695002a60fSMarcel Moolenaar 	return (EINVAL);
270146aad74SMarcel Moolenaar }
271146aad74SMarcel Moolenaar 
272146aad74SMarcel Moolenaar int
273b40ce416SJulian Elischer linux_old_select(struct thread *td, struct linux_old_select_args *args)
274146aad74SMarcel Moolenaar {
2755002a60fSMarcel Moolenaar 	struct l_old_select_argv linux_args;
2765002a60fSMarcel Moolenaar 	struct linux_select_args newsel;
277146aad74SMarcel Moolenaar 	int error;
278146aad74SMarcel Moolenaar 
2795002a60fSMarcel Moolenaar #ifdef DEBUG
2805002a60fSMarcel Moolenaar 	if (ldebug(old_select))
2816aea6777SPeter Wemm 		printf(ARGS(old_select, "%p"), args->ptr);
282146aad74SMarcel Moolenaar #endif
283146aad74SMarcel Moolenaar 
2844b7ef73dSDag-Erling Smørgrav 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
285146aad74SMarcel Moolenaar 	if (error)
286146aad74SMarcel Moolenaar 		return (error);
287146aad74SMarcel Moolenaar 
288146aad74SMarcel Moolenaar 	newsel.nfds = linux_args.nfds;
289146aad74SMarcel Moolenaar 	newsel.readfds = linux_args.readfds;
290146aad74SMarcel Moolenaar 	newsel.writefds = linux_args.writefds;
291146aad74SMarcel Moolenaar 	newsel.exceptfds = linux_args.exceptfds;
292146aad74SMarcel Moolenaar 	newsel.timeout = linux_args.timeout;
293b40ce416SJulian Elischer 	return (linux_select(td, &newsel));
294146aad74SMarcel Moolenaar }
295146aad74SMarcel Moolenaar 
296146aad74SMarcel Moolenaar int
297b40ce416SJulian Elischer linux_fork(struct thread *td, struct linux_fork_args *args)
298146aad74SMarcel Moolenaar {
299146aad74SMarcel Moolenaar 	int error;
300146aad74SMarcel Moolenaar 
301146aad74SMarcel Moolenaar #ifdef DEBUG
30224593369SJonathan Lemon 	if (ldebug(fork))
30324593369SJonathan Lemon 		printf(ARGS(fork, ""));
304146aad74SMarcel Moolenaar #endif
305146aad74SMarcel Moolenaar 
306b40ce416SJulian Elischer 	if ((error = fork(td, (struct fork_args *)args)) != 0)
307146aad74SMarcel Moolenaar 		return (error);
308146aad74SMarcel Moolenaar 
309b40ce416SJulian Elischer 	if (td->td_retval[1] == 1)
310b40ce416SJulian Elischer 		td->td_retval[0] = 0;
3119b44bfc5SAlexander Leidinger 	error = linux_proc_init(td, td->td_retval[0], 0);
3129b44bfc5SAlexander Leidinger 	if (error)
3139b44bfc5SAlexander Leidinger 		return (error);
3149b44bfc5SAlexander Leidinger 
315146aad74SMarcel Moolenaar 	return (0);
316146aad74SMarcel Moolenaar }
317146aad74SMarcel Moolenaar 
318146aad74SMarcel Moolenaar int
319b40ce416SJulian Elischer linux_vfork(struct thread *td, struct linux_vfork_args *args)
320146aad74SMarcel Moolenaar {
321146aad74SMarcel Moolenaar 	int error;
32240f734ddSAlexander Leidinger 	struct proc *p2;
323146aad74SMarcel Moolenaar 
324146aad74SMarcel Moolenaar #ifdef DEBUG
32524593369SJonathan Lemon 	if (ldebug(vfork))
32624593369SJonathan Lemon 		printf(ARGS(vfork, ""));
327146aad74SMarcel Moolenaar #endif
328146aad74SMarcel Moolenaar 
32940f734ddSAlexander Leidinger 	/* exclude RFPPWAIT */
33040f734ddSAlexander Leidinger 	if ((error = fork1(td, RFFDG | RFPROC | RFMEM, 0, &p2)) != 0)
331146aad74SMarcel Moolenaar 		return (error);
33240f734ddSAlexander Leidinger 	if (error == 0) {
33340f734ddSAlexander Leidinger 	   	td->td_retval[0] = p2->p_pid;
33440f734ddSAlexander Leidinger 		td->td_retval[1] = 0;
33540f734ddSAlexander Leidinger 	}
336146aad74SMarcel Moolenaar 	/* Are we the child? */
337b40ce416SJulian Elischer 	if (td->td_retval[1] == 1)
338b40ce416SJulian Elischer 		td->td_retval[0] = 0;
3399b44bfc5SAlexander Leidinger 	error = linux_proc_init(td, td->td_retval[0], 0);
3409b44bfc5SAlexander Leidinger 	if (error)
3419b44bfc5SAlexander Leidinger 		return (error);
34240f734ddSAlexander Leidinger 	/* wait for the children to exit, ie. emulate vfork */
34340f734ddSAlexander Leidinger 	PROC_LOCK(p2);
34440f734ddSAlexander Leidinger 	while (p2->p_flag & P_PPWAIT)
34540f734ddSAlexander Leidinger 	   	msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0);
34640f734ddSAlexander Leidinger 	PROC_UNLOCK(p2);
34740f734ddSAlexander Leidinger 
348146aad74SMarcel Moolenaar 	return (0);
349146aad74SMarcel Moolenaar }
350146aad74SMarcel Moolenaar 
351146aad74SMarcel Moolenaar int
352b40ce416SJulian Elischer linux_clone(struct thread *td, struct linux_clone_args *args)
353146aad74SMarcel Moolenaar {
3546ad0e7c5SJohn Baldwin 	int error, ff = RFPROC | RFSTOPPED;
355146aad74SMarcel Moolenaar 	struct proc *p2;
3569eb78fcfSJohn Baldwin 	struct thread *td2;
357146aad74SMarcel Moolenaar 	int exit_signal;
3589b44bfc5SAlexander Leidinger 	struct linux_emuldata *em;
359146aad74SMarcel Moolenaar 
360146aad74SMarcel Moolenaar #ifdef DEBUG
36124593369SJonathan Lemon 	if (ldebug(clone)) {
3629b44bfc5SAlexander Leidinger    	   	printf(ARGS(clone, "flags %x, stack %x, parent tid: %x, child tid: %x"),
3639b44bfc5SAlexander Leidinger 		    (unsigned int)args->flags, (unsigned int)args->stack,
3649b44bfc5SAlexander Leidinger 		    (unsigned int)args->parent_tidptr, (unsigned int)args->child_tidptr);
36524593369SJonathan Lemon 	}
366146aad74SMarcel Moolenaar #endif
367146aad74SMarcel Moolenaar 
368146aad74SMarcel Moolenaar 	exit_signal = args->flags & 0x000000ff;
3690f054958SAlexander Leidinger 	if (!LINUX_SIG_VALID(exit_signal) && exit_signal != 0)
370146aad74SMarcel Moolenaar 		return (EINVAL);
371146aad74SMarcel Moolenaar 
372146aad74SMarcel Moolenaar 	if (exit_signal <= LINUX_SIGTBLSZ)
373146aad74SMarcel Moolenaar 		exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
374146aad74SMarcel Moolenaar 
375146aad74SMarcel Moolenaar 	if (args->flags & CLONE_VM)
376146aad74SMarcel Moolenaar 		ff |= RFMEM;
377146aad74SMarcel Moolenaar 	if (args->flags & CLONE_SIGHAND)
378146aad74SMarcel Moolenaar 		ff |= RFSIGSHARE;
3794b3583a3SAlexander Leidinger 	/*
3804b3583a3SAlexander Leidinger 	 * XXX: in linux sharing of fs info (chroot/cwd/umask)
3814b3583a3SAlexander Leidinger 	 * and open files is independant. in fbsd its in one
3824b3583a3SAlexander Leidinger 	 * structure but in reality it doesnt make any problems
3834b3583a3SAlexander Leidinger 	 * because both this flags are set at once usually.
3844b3583a3SAlexander Leidinger 	 */
3854b3583a3SAlexander Leidinger 	if (!(args->flags & (CLONE_FILES | CLONE_FS)))
386146aad74SMarcel Moolenaar 		ff |= RFFDG;
387146aad74SMarcel Moolenaar 
3884b178336SMaxim Sobolev 	/*
3894b178336SMaxim Sobolev 	 * Attempt to detect when linux_clone(2) is used for creating
3904b178336SMaxim Sobolev 	 * kernel threads. Unfortunately despite the existence of the
3914b178336SMaxim Sobolev 	 * CLONE_THREAD flag, version of linuxthreads package used in
3924b178336SMaxim Sobolev 	 * most popular distros as of beginning of 2005 doesn't make
3934b178336SMaxim Sobolev 	 * any use of it. Therefore, this detection relay fully on
3944b178336SMaxim Sobolev 	 * empirical observation that linuxthreads sets certain
3954b178336SMaxim Sobolev 	 * combination of flags, so that we can make more or less
3964b178336SMaxim Sobolev 	 * precise detection and notify the FreeBSD kernel that several
3974b178336SMaxim Sobolev 	 * processes are in fact part of the same threading group, so
3984b178336SMaxim Sobolev 	 * that special treatment is necessary for signal delivery
3994b178336SMaxim Sobolev 	 * between those processes and fd locking.
4004b178336SMaxim Sobolev 	 */
4014b178336SMaxim Sobolev 	if ((args->flags & 0xffffff00) == THREADING_FLAGS)
4024b178336SMaxim Sobolev 		ff |= RFTHREAD;
4034b178336SMaxim Sobolev 
404316ec49aSScott Long 	error = fork1(td, ff, 0, &p2);
4059eb78fcfSJohn Baldwin 	if (error)
4069eb78fcfSJohn Baldwin 		return (error);
4079eb78fcfSJohn Baldwin 
4089b44bfc5SAlexander Leidinger 	/* create the emuldata */
4099b44bfc5SAlexander Leidinger 	error = linux_proc_init(td, p2->p_pid, args->flags);
4109b44bfc5SAlexander Leidinger 	/* reference it - no need to check this */
4119b44bfc5SAlexander Leidinger 	em = em_find(p2, EMUL_UNLOCKED);
4129b44bfc5SAlexander Leidinger 	KASSERT(em != NULL, ("clone: emuldata not found.\n"));
4139b44bfc5SAlexander Leidinger 	/* and adjust it */
4149b44bfc5SAlexander Leidinger 	if (args->flags & CLONE_PARENT_SETTID) {
4159b44bfc5SAlexander Leidinger 	   	if (args->parent_tidptr == NULL) {
4169b44bfc5SAlexander Leidinger 		   	EMUL_UNLOCK(&emul_lock);
4179b44bfc5SAlexander Leidinger 			return (EINVAL);
4189b44bfc5SAlexander Leidinger 		}
4199b44bfc5SAlexander Leidinger 		error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid));
4209b44bfc5SAlexander Leidinger 		if (error) {
4219b44bfc5SAlexander Leidinger 		   	EMUL_UNLOCK(&emul_lock);
4229b44bfc5SAlexander Leidinger 			return (error);
4239b44bfc5SAlexander Leidinger 		}
4249b44bfc5SAlexander Leidinger 	}
4259b44bfc5SAlexander Leidinger 
426a6c5f813SAlexander Leidinger 	if (args->flags & (CLONE_PARENT|CLONE_THREAD)) {
427a6c5f813SAlexander Leidinger 	   	sx_xlock(&proctree_lock);
428a6c5f813SAlexander Leidinger 		PROC_LOCK(p2);
429a6c5f813SAlexander Leidinger 		proc_reparent(p2, td->td_proc->p_pptr);
430a6c5f813SAlexander Leidinger 		PROC_UNLOCK(p2);
431a6c5f813SAlexander Leidinger 		sx_xunlock(&proctree_lock);
4329b44bfc5SAlexander Leidinger 	}
4339b44bfc5SAlexander Leidinger 
4349b44bfc5SAlexander Leidinger 	if (args->flags & CLONE_THREAD) {
4359b44bfc5SAlexander Leidinger 	   	/* XXX: linux mangles pgrp and pptr somehow
4369b44bfc5SAlexander Leidinger 		 * I think it might be this but I am not sure.
4379b44bfc5SAlexander Leidinger 		 */
4389b44bfc5SAlexander Leidinger #ifdef notyet
439a6c5f813SAlexander Leidinger 	   	PROC_LOCK(p2);
4409b44bfc5SAlexander Leidinger 	   	p2->p_pgrp = td->td_proc->p_pgrp;
441a6c5f813SAlexander Leidinger 	   	PROC_UNLOCK(p2);
4429b44bfc5SAlexander Leidinger #endif
4439b44bfc5SAlexander Leidinger 	 	exit_signal = 0;
4449b44bfc5SAlexander Leidinger 	}
4459b44bfc5SAlexander Leidinger 
4469b44bfc5SAlexander Leidinger 	if (args->flags & CLONE_CHILD_SETTID)
4479b44bfc5SAlexander Leidinger 		em->child_set_tid = args->child_tidptr;
4489b44bfc5SAlexander Leidinger 	else
4499b44bfc5SAlexander Leidinger 	   	em->child_set_tid = NULL;
4509b44bfc5SAlexander Leidinger 
4519b44bfc5SAlexander Leidinger 	if (args->flags & CLONE_CHILD_CLEARTID)
4529b44bfc5SAlexander Leidinger 		em->child_clear_tid = args->child_tidptr;
4539b44bfc5SAlexander Leidinger 	else
4549b44bfc5SAlexander Leidinger 	   	em->child_clear_tid = NULL;
455a6c5f813SAlexander Leidinger 
4569b44bfc5SAlexander Leidinger 	EMUL_UNLOCK(&emul_lock);
457146aad74SMarcel Moolenaar 
458fdfdfb78SJohn Baldwin 	PROC_LOCK(p2);
459146aad74SMarcel Moolenaar 	p2->p_sigparent = exit_signal;
4609eb78fcfSJohn Baldwin 	PROC_UNLOCK(p2);
4619eb78fcfSJohn Baldwin 	td2 = FIRST_THREAD_IN_PROC(p2);
4620eef2f8aSAlexander Leidinger 	/*
4630eef2f8aSAlexander Leidinger 	 * in a case of stack = NULL we are supposed to COW calling process stack
4649b44bfc5SAlexander Leidinger 	 * this is what normal fork() does so we just keep the tf_esp arg intact
4659b44bfc5SAlexander Leidinger 	 */
4669b44bfc5SAlexander Leidinger 	if (args->stack)
4679eb78fcfSJohn Baldwin    	   	td2->td_frame->tf_esp = (unsigned int)args->stack;
468146aad74SMarcel Moolenaar 
4699b44bfc5SAlexander Leidinger 	if (args->flags & CLONE_SETTLS) {
4709b44bfc5SAlexander Leidinger    	   	struct l_user_desc info;
4719b44bfc5SAlexander Leidinger    	   	int idx;
4729b44bfc5SAlexander Leidinger 	   	int a[2];
4739b44bfc5SAlexander Leidinger 		struct segment_descriptor sd;
4749b44bfc5SAlexander Leidinger 
4759b44bfc5SAlexander Leidinger 	   	error = copyin((void *)td->td_frame->tf_esi, &info, sizeof(struct l_user_desc));
4769b44bfc5SAlexander Leidinger 		if (error)
4779b44bfc5SAlexander Leidinger    		   	return (error);
4789b44bfc5SAlexander Leidinger 
4799b44bfc5SAlexander Leidinger 		idx = info.entry_number;
4809b44bfc5SAlexander Leidinger 
4810eef2f8aSAlexander Leidinger 		/*
4820eef2f8aSAlexander Leidinger 		 * looks like we're getting the idx we returned
4839b44bfc5SAlexander Leidinger 		 * in the set_thread_area() syscall
4849b44bfc5SAlexander Leidinger 		 */
4859b44bfc5SAlexander Leidinger 		if (idx != 6 && idx != 3)
4869b44bfc5SAlexander Leidinger 			return (EINVAL);
4879b44bfc5SAlexander Leidinger 
4889b44bfc5SAlexander Leidinger 		/* this doesnt happen in practice */
4899b44bfc5SAlexander Leidinger 		if (idx == 6) {
4909b44bfc5SAlexander Leidinger 		   	/* we might copy out the entry_number as 3 */
4919b44bfc5SAlexander Leidinger 		   	info.entry_number = 3;
4929b44bfc5SAlexander Leidinger 			error = copyout(&info, (void *) td->td_frame->tf_esi, sizeof(struct l_user_desc));
4939b44bfc5SAlexander Leidinger 			if (error)
4949b44bfc5SAlexander Leidinger 	   		   	return (error);
4959b44bfc5SAlexander Leidinger 		}
4969b44bfc5SAlexander Leidinger 
4979b44bfc5SAlexander Leidinger 		a[0] = LDT_entry_a(&info);
4989b44bfc5SAlexander Leidinger 		a[1] = LDT_entry_b(&info);
4999b44bfc5SAlexander Leidinger 
5009b44bfc5SAlexander Leidinger 		memcpy(&sd, &a, sizeof(a));
5019b44bfc5SAlexander Leidinger #ifdef DEBUG
5029b44bfc5SAlexander Leidinger 	if (ldebug(clone))
5039b44bfc5SAlexander Leidinger 	   	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,
5049b44bfc5SAlexander Leidinger 			sd.sd_hibase,
5059b44bfc5SAlexander Leidinger 			sd.sd_lolimit,
5069b44bfc5SAlexander Leidinger 			sd.sd_hilimit,
5079b44bfc5SAlexander Leidinger 			sd.sd_type,
5089b44bfc5SAlexander Leidinger 			sd.sd_dpl,
5099b44bfc5SAlexander Leidinger 			sd.sd_p,
5109b44bfc5SAlexander Leidinger 			sd.sd_xx,
5119b44bfc5SAlexander Leidinger 			sd.sd_def32,
5129b44bfc5SAlexander Leidinger 			sd.sd_gran);
5139b44bfc5SAlexander Leidinger #endif
5149b44bfc5SAlexander Leidinger 
5159b44bfc5SAlexander Leidinger 		/* set %gs */
5169b44bfc5SAlexander Leidinger 		td2->td_pcb->pcb_gsd = sd;
517a6c5f813SAlexander Leidinger 		td2->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
5189b44bfc5SAlexander Leidinger 	}
5199b44bfc5SAlexander Leidinger 
520146aad74SMarcel Moolenaar #ifdef DEBUG
52124593369SJonathan Lemon 	if (ldebug(clone))
5229eb78fcfSJohn Baldwin 		printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
5239eb78fcfSJohn Baldwin 		    (long)p2->p_pid, args->stack, exit_signal);
524146aad74SMarcel Moolenaar #endif
525146aad74SMarcel Moolenaar 
5266ad0e7c5SJohn Baldwin 	/*
5276ad0e7c5SJohn Baldwin 	 * Make this runnable after we are finished with it.
5286ad0e7c5SJohn Baldwin 	 */
5296ad0e7c5SJohn Baldwin 	mtx_lock_spin(&sched_lock);
5309eb78fcfSJohn Baldwin 	TD_SET_CAN_RUN(td2);
5312630e4c9SJulian Elischer 	setrunqueue(td2, SRQ_BORING);
5326ad0e7c5SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
5336ad0e7c5SJohn Baldwin 
5349eb78fcfSJohn Baldwin 	td->td_retval[0] = p2->p_pid;
5359eb78fcfSJohn Baldwin 	td->td_retval[1] = 0;
5360a62e035SAlexander Leidinger 
5370a62e035SAlexander Leidinger 	if (args->flags & CLONE_VFORK) {
5380a62e035SAlexander Leidinger    	   	/* wait for the children to exit, ie. emulate vfork */
5390a62e035SAlexander Leidinger    	   	PROC_LOCK(p2);
5400a62e035SAlexander Leidinger 		p2->p_flag |= P_PPWAIT;
5410a62e035SAlexander Leidinger 		while (p2->p_flag & P_PPWAIT)
5420a62e035SAlexander Leidinger    		   	msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0);
5430a62e035SAlexander Leidinger 		PROC_UNLOCK(p2);
5440a62e035SAlexander Leidinger 	}
5450a62e035SAlexander Leidinger 
5469eb78fcfSJohn Baldwin 	return (0);
547146aad74SMarcel Moolenaar }
548146aad74SMarcel Moolenaar 
549146aad74SMarcel Moolenaar /* XXX move */
5505002a60fSMarcel Moolenaar struct l_mmap_argv {
5515002a60fSMarcel Moolenaar 	l_caddr_t	addr;
5525002a60fSMarcel Moolenaar 	l_int		len;
5535002a60fSMarcel Moolenaar 	l_int		prot;
5545002a60fSMarcel Moolenaar 	l_int		flags;
5555002a60fSMarcel Moolenaar 	l_int		fd;
5565002a60fSMarcel Moolenaar 	l_int		pos;
557146aad74SMarcel Moolenaar };
558146aad74SMarcel Moolenaar 
559146aad74SMarcel Moolenaar #define STACK_SIZE  (2 * 1024 * 1024)
560146aad74SMarcel Moolenaar #define GUARD_SIZE  (4 * PAGE_SIZE)
561146aad74SMarcel Moolenaar 
5623ad9c842SMaxim Sobolev static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
5633ad9c842SMaxim Sobolev 
5643ad9c842SMaxim Sobolev int
5653ad9c842SMaxim Sobolev linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
5663ad9c842SMaxim Sobolev {
5673ad9c842SMaxim Sobolev 	struct l_mmap_argv linux_args;
5683ad9c842SMaxim Sobolev 
5693ad9c842SMaxim Sobolev #ifdef DEBUG
5703ad9c842SMaxim Sobolev 	if (ldebug(mmap2))
5713ad9c842SMaxim Sobolev 		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
5723ad9c842SMaxim Sobolev 		    (void *)args->addr, args->len, args->prot,
5733ad9c842SMaxim Sobolev 		    args->flags, args->fd, args->pgoff);
5743ad9c842SMaxim Sobolev #endif
5753ad9c842SMaxim Sobolev 
5763ad9c842SMaxim Sobolev 	linux_args.addr = (l_caddr_t)args->addr;
5773ad9c842SMaxim Sobolev 	linux_args.len = args->len;
5783ad9c842SMaxim Sobolev 	linux_args.prot = args->prot;
5793ad9c842SMaxim Sobolev 	linux_args.flags = args->flags;
5803ad9c842SMaxim Sobolev 	linux_args.fd = args->fd;
5813ad9c842SMaxim Sobolev 	linux_args.pos = args->pgoff * PAGE_SIZE;
5823ad9c842SMaxim Sobolev 
5833ad9c842SMaxim Sobolev 	return (linux_mmap_common(td, &linux_args));
5843ad9c842SMaxim Sobolev }
5853ad9c842SMaxim Sobolev 
586146aad74SMarcel Moolenaar int
587b40ce416SJulian Elischer linux_mmap(struct thread *td, struct linux_mmap_args *args)
588146aad74SMarcel Moolenaar {
5893ad9c842SMaxim Sobolev 	int error;
5903ad9c842SMaxim Sobolev 	struct l_mmap_argv linux_args;
5913ad9c842SMaxim Sobolev 
5924b7ef73dSDag-Erling Smørgrav 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
5933ad9c842SMaxim Sobolev 	if (error)
5943ad9c842SMaxim Sobolev 		return (error);
5953ad9c842SMaxim Sobolev 
5963ad9c842SMaxim Sobolev #ifdef DEBUG
5973ad9c842SMaxim Sobolev 	if (ldebug(mmap))
5983ad9c842SMaxim Sobolev 		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
599b45bbfc3SBruce Evans 		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
600b45bbfc3SBruce Evans 		    linux_args.flags, linux_args.fd, linux_args.pos);
6013ad9c842SMaxim Sobolev #endif
6023ad9c842SMaxim Sobolev 
6033ad9c842SMaxim Sobolev 	return (linux_mmap_common(td, &linux_args));
6043ad9c842SMaxim Sobolev }
6053ad9c842SMaxim Sobolev 
6063ad9c842SMaxim Sobolev static int
6073ad9c842SMaxim Sobolev linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
6083ad9c842SMaxim Sobolev {
609b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
610146aad74SMarcel Moolenaar 	struct mmap_args /* {
611146aad74SMarcel Moolenaar 		caddr_t addr;
612146aad74SMarcel Moolenaar 		size_t len;
613146aad74SMarcel Moolenaar 		int prot;
614146aad74SMarcel Moolenaar 		int flags;
615146aad74SMarcel Moolenaar 		int fd;
616146aad74SMarcel Moolenaar 		long pad;
617146aad74SMarcel Moolenaar 		off_t pos;
618146aad74SMarcel Moolenaar 	} */ bsd_args;
61991d631e5SMatthew N. Dodd 	int error;
620a312f6a3SAlexander Leidinger 	struct file *fp;
621146aad74SMarcel Moolenaar 
62291d631e5SMatthew N. Dodd 	error = 0;
623146aad74SMarcel Moolenaar 	bsd_args.flags = 0;
624a312f6a3SAlexander Leidinger 	fp = NULL;
625a312f6a3SAlexander Leidinger 
626a312f6a3SAlexander Leidinger 	/*
627a312f6a3SAlexander Leidinger 	 * Linux mmap(2):
628a312f6a3SAlexander Leidinger 	 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
629a312f6a3SAlexander Leidinger 	 */
630a312f6a3SAlexander Leidinger 	if (! ((linux_args->flags & LINUX_MAP_SHARED) ^
631a312f6a3SAlexander Leidinger 	    (linux_args->flags & LINUX_MAP_PRIVATE)))
6326dc4e810SAlexander Leidinger 		return (EINVAL);
633a312f6a3SAlexander Leidinger 
6343ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_SHARED)
635146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_SHARED;
6363ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_PRIVATE)
637146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_PRIVATE;
6383ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_FIXED)
639146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_FIXED;
6403ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_ANON)
641146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_ANON;
6420cc3ac8bSMatthew Dillon 	else
6430cc3ac8bSMatthew Dillon 		bsd_args.flags |= MAP_NOSYNC;
6443ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
645146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_STACK;
646146aad74SMarcel Moolenaar 
6470eef2f8aSAlexander Leidinger 		/*
6480eef2f8aSAlexander Leidinger 		 * The linux MAP_GROWSDOWN option does not limit auto
649146aad74SMarcel Moolenaar 		 * growth of the region.  Linux mmap with this option
650146aad74SMarcel Moolenaar 		 * takes as addr the inital BOS, and as len, the initial
651146aad74SMarcel Moolenaar 		 * region size.  It can then grow down from addr without
652146aad74SMarcel Moolenaar 		 * limit.  However, linux threads has an implicit internal
653146aad74SMarcel Moolenaar 		 * limit to stack size of STACK_SIZE.  Its just not
654146aad74SMarcel Moolenaar 		 * enforced explicitly in linux.  But, here we impose
655146aad74SMarcel Moolenaar 		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
656146aad74SMarcel Moolenaar 		 * region, since we can do this with our mmap.
657146aad74SMarcel Moolenaar 		 *
658146aad74SMarcel Moolenaar 		 * Our mmap with MAP_STACK takes addr as the maximum
659146aad74SMarcel Moolenaar 		 * downsize limit on BOS, and as len the max size of
660146aad74SMarcel Moolenaar 		 * the region.  It them maps the top SGROWSIZ bytes,
661146aad74SMarcel Moolenaar 		 * and autgrows the region down, up to the limit
662146aad74SMarcel Moolenaar 		 * in addr.
663146aad74SMarcel Moolenaar 		 *
664146aad74SMarcel Moolenaar 		 * If we don't use the MAP_STACK option, the effect
665146aad74SMarcel Moolenaar 		 * of this code is to allocate a stack region of a
666146aad74SMarcel Moolenaar 		 * fixed size of (STACK_SIZE - GUARD_SIZE).
667146aad74SMarcel Moolenaar 		 */
668146aad74SMarcel Moolenaar 
669146aad74SMarcel Moolenaar 		/* This gives us TOS */
6703ad9c842SMaxim Sobolev 		bsd_args.addr = linux_args->addr + linux_args->len;
671146aad74SMarcel Moolenaar 
672242fae60SAndrew Gallatin 		if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
6730eef2f8aSAlexander Leidinger 			/*
6740eef2f8aSAlexander Leidinger 			 * Some linux apps will attempt to mmap
675242fae60SAndrew Gallatin 			 * thread stacks near the top of their
676242fae60SAndrew Gallatin 			 * address space.  If their TOS is greater
677242fae60SAndrew Gallatin 			 * than vm_maxsaddr, vm_map_growstack()
678242fae60SAndrew Gallatin 			 * will confuse the thread stack with the
679242fae60SAndrew Gallatin 			 * process stack and deliver a SEGV if they
680242fae60SAndrew Gallatin 			 * attempt to grow the thread stack past their
681242fae60SAndrew Gallatin 			 * current stacksize rlimit.  To avoid this,
682242fae60SAndrew Gallatin 			 * adjust vm_maxsaddr upwards to reflect
683242fae60SAndrew Gallatin 			 * the current stacksize rlimit rather
684242fae60SAndrew Gallatin 			 * than the maximum possible stacksize.
685242fae60SAndrew Gallatin 			 * It would be better to adjust the
686242fae60SAndrew Gallatin 			 * mmap'ed region, but some apps do not check
687242fae60SAndrew Gallatin 			 * mmap's return value.
688242fae60SAndrew Gallatin 			 */
68991d5354aSJohn Baldwin 			PROC_LOCK(p);
690242fae60SAndrew Gallatin 			p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
69191d5354aSJohn Baldwin 			    lim_cur(p, RLIMIT_STACK);
69291d5354aSJohn Baldwin 			PROC_UNLOCK(p);
693242fae60SAndrew Gallatin 		}
694242fae60SAndrew Gallatin 
695146aad74SMarcel Moolenaar 		/* This gives us our maximum stack size */
6963ad9c842SMaxim Sobolev 		if (linux_args->len > STACK_SIZE - GUARD_SIZE)
6973ad9c842SMaxim Sobolev 			bsd_args.len = linux_args->len;
698146aad74SMarcel Moolenaar 		else
699146aad74SMarcel Moolenaar 			bsd_args.len  = STACK_SIZE - GUARD_SIZE;
700146aad74SMarcel Moolenaar 
7010eef2f8aSAlexander Leidinger 		/*
7020eef2f8aSAlexander Leidinger 		 * This gives us a new BOS.  If we're using VM_STACK, then
703146aad74SMarcel Moolenaar 		 * mmap will just map the top SGROWSIZ bytes, and let
704146aad74SMarcel Moolenaar 		 * the stack grow down to the limit at BOS.  If we're
705146aad74SMarcel Moolenaar 		 * not using VM_STACK we map the full stack, since we
706146aad74SMarcel Moolenaar 		 * don't have a way to autogrow it.
707146aad74SMarcel Moolenaar 		 */
708146aad74SMarcel Moolenaar 		bsd_args.addr -= bsd_args.len;
709146aad74SMarcel Moolenaar 	} else {
7103ad9c842SMaxim Sobolev 		bsd_args.addr = linux_args->addr;
7113ad9c842SMaxim Sobolev 		bsd_args.len  = linux_args->len;
712146aad74SMarcel Moolenaar 	}
713146aad74SMarcel Moolenaar 
714a312f6a3SAlexander Leidinger 	bsd_args.prot = linux_args->prot;
7153ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_ANON)
716146aad74SMarcel Moolenaar 		bsd_args.fd = -1;
717a312f6a3SAlexander Leidinger 	else {
718a312f6a3SAlexander Leidinger 		/*
719a312f6a3SAlexander Leidinger 		 * Linux follows Solaris mmap(2) description:
720a312f6a3SAlexander Leidinger 		 * The file descriptor fildes is opened with
721a312f6a3SAlexander Leidinger 		 * read permission, regardless of the
722a312f6a3SAlexander Leidinger 		 * protection options specified.
723a312f6a3SAlexander Leidinger 		 * If PROT_WRITE is specified, the application
724a312f6a3SAlexander Leidinger 		 * must have opened the file descriptor
725a312f6a3SAlexander Leidinger 		 * fildes with write permission unless
726a312f6a3SAlexander Leidinger 		 * MAP_PRIVATE is specified in the flag
727a312f6a3SAlexander Leidinger 		 * argument as described below.
728a312f6a3SAlexander Leidinger 		 */
729a312f6a3SAlexander Leidinger 
730a312f6a3SAlexander Leidinger 		if ((error = fget(td, linux_args->fd, &fp)) != 0)
7316dc4e810SAlexander Leidinger 			return (error);
732a312f6a3SAlexander Leidinger 		if (fp->f_type != DTYPE_VNODE) {
733a312f6a3SAlexander Leidinger 			fdrop(fp, td);
7346dc4e810SAlexander Leidinger 			return (EINVAL);
735a312f6a3SAlexander Leidinger 		}
736a312f6a3SAlexander Leidinger 
737a312f6a3SAlexander Leidinger 		/* Linux mmap() just fails for O_WRONLY files */
738a312f6a3SAlexander Leidinger 		if (! (fp->f_flag & FREAD)) {
739a312f6a3SAlexander Leidinger 			fdrop(fp, td);
7406dc4e810SAlexander Leidinger 			return (EACCES);
741a312f6a3SAlexander Leidinger 		}
742a312f6a3SAlexander Leidinger 
7433ad9c842SMaxim Sobolev 		bsd_args.fd = linux_args->fd;
744a312f6a3SAlexander Leidinger 		fdrop(fp, td);
745a312f6a3SAlexander Leidinger 	}
7463ad9c842SMaxim Sobolev 	bsd_args.pos = linux_args->pos;
747146aad74SMarcel Moolenaar 	bsd_args.pad = 0;
748146aad74SMarcel Moolenaar 
749146aad74SMarcel Moolenaar #ifdef DEBUG
75024593369SJonathan Lemon 	if (ldebug(mmap))
75191d631e5SMatthew N. Dodd 		printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
75291d631e5SMatthew N. Dodd 		    __func__,
75324593369SJonathan Lemon 		    (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
75424593369SJonathan Lemon 		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
755146aad74SMarcel Moolenaar #endif
75691d631e5SMatthew N. Dodd 	error = mmap(td, &bsd_args);
75791d631e5SMatthew N. Dodd #ifdef DEBUG
75891d631e5SMatthew N. Dodd 	if (ldebug(mmap))
75991d631e5SMatthew N. Dodd 		printf("-> %s() return: 0x%x (0x%08x)\n",
76091d631e5SMatthew N. Dodd 			__func__, error, (u_int)td->td_retval[0]);
76191d631e5SMatthew N. Dodd #endif
76291d631e5SMatthew N. Dodd 	return (error);
763146aad74SMarcel Moolenaar }
764146aad74SMarcel Moolenaar 
765146aad74SMarcel Moolenaar int
766b40ce416SJulian Elischer linux_pipe(struct thread *td, struct linux_pipe_args *args)
767146aad74SMarcel Moolenaar {
768146aad74SMarcel Moolenaar 	int error;
769146aad74SMarcel Moolenaar 	int reg_edx;
770146aad74SMarcel Moolenaar 
771146aad74SMarcel Moolenaar #ifdef DEBUG
77224593369SJonathan Lemon 	if (ldebug(pipe))
77324593369SJonathan Lemon 		printf(ARGS(pipe, "*"));
774146aad74SMarcel Moolenaar #endif
775146aad74SMarcel Moolenaar 
776b40ce416SJulian Elischer 	reg_edx = td->td_retval[1];
777b40ce416SJulian Elischer 	error = pipe(td, 0);
778146aad74SMarcel Moolenaar 	if (error) {
779b40ce416SJulian Elischer 		td->td_retval[1] = reg_edx;
780146aad74SMarcel Moolenaar 		return (error);
781146aad74SMarcel Moolenaar 	}
782146aad74SMarcel Moolenaar 
783b40ce416SJulian Elischer 	error = copyout(td->td_retval, args->pipefds, 2*sizeof(int));
784146aad74SMarcel Moolenaar 	if (error) {
785b40ce416SJulian Elischer 		td->td_retval[1] = reg_edx;
786146aad74SMarcel Moolenaar 		return (error);
787146aad74SMarcel Moolenaar 	}
788146aad74SMarcel Moolenaar 
789b40ce416SJulian Elischer 	td->td_retval[1] = reg_edx;
790b40ce416SJulian Elischer 	td->td_retval[0] = 0;
791146aad74SMarcel Moolenaar 	return (0);
792146aad74SMarcel Moolenaar }
793146aad74SMarcel Moolenaar 
794146aad74SMarcel Moolenaar int
795b40ce416SJulian Elischer linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
796146aad74SMarcel Moolenaar {
79784569dffSMaxim Sobolev 	int error;
79884569dffSMaxim Sobolev 	struct i386_ioperm_args iia;
799146aad74SMarcel Moolenaar 
80084569dffSMaxim Sobolev 	iia.start = args->start;
80184569dffSMaxim Sobolev 	iia.length = args->length;
80284569dffSMaxim Sobolev 	iia.enable = args->enable;
80384569dffSMaxim Sobolev 	mtx_lock(&Giant);
80484569dffSMaxim Sobolev 	error = i386_set_ioperm(td, &iia);
80584569dffSMaxim Sobolev 	mtx_unlock(&Giant);
80684569dffSMaxim Sobolev 	return (error);
807146aad74SMarcel Moolenaar }
808146aad74SMarcel Moolenaar 
809146aad74SMarcel Moolenaar int
810b40ce416SJulian Elischer linux_iopl(struct thread *td, struct linux_iopl_args *args)
811146aad74SMarcel Moolenaar {
812146aad74SMarcel Moolenaar 	int error;
813146aad74SMarcel Moolenaar 
814146aad74SMarcel Moolenaar 	if (args->level < 0 || args->level > 3)
815146aad74SMarcel Moolenaar 		return (EINVAL);
816acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_IO)) != 0)
817146aad74SMarcel Moolenaar 		return (error);
818a854ed98SJohn Baldwin 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
81941c42188SRobert Watson 		return (error);
820b40ce416SJulian Elischer 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
821146aad74SMarcel Moolenaar 	    (args->level * (PSL_IOPL / 3));
822146aad74SMarcel Moolenaar 	return (0);
823146aad74SMarcel Moolenaar }
824146aad74SMarcel Moolenaar 
825146aad74SMarcel Moolenaar int
826b07cd97eSMark Murray linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
827146aad74SMarcel Moolenaar {
828146aad74SMarcel Moolenaar 	int error;
82984569dffSMaxim Sobolev 	struct i386_ldt_args ldt;
8305002a60fSMarcel Moolenaar 	struct l_descriptor ld;
83184569dffSMaxim Sobolev 	union descriptor desc;
832146aad74SMarcel Moolenaar 
833146aad74SMarcel Moolenaar 	if (uap->ptr == NULL)
834146aad74SMarcel Moolenaar 		return (EINVAL);
835146aad74SMarcel Moolenaar 
836146aad74SMarcel Moolenaar 	switch (uap->func) {
837146aad74SMarcel Moolenaar 	case 0x00: /* read_ldt */
83884569dffSMaxim Sobolev 		ldt.start = 0;
83984569dffSMaxim Sobolev 		ldt.descs = uap->ptr;
84084569dffSMaxim Sobolev 		ldt.num = uap->bytecount / sizeof(union descriptor);
84184569dffSMaxim Sobolev 		mtx_lock(&Giant);
84284569dffSMaxim Sobolev 		error = i386_get_ldt(td, &ldt);
843b40ce416SJulian Elischer 		td->td_retval[0] *= sizeof(union descriptor);
84484569dffSMaxim Sobolev 		mtx_unlock(&Giant);
845146aad74SMarcel Moolenaar 		break;
846146aad74SMarcel Moolenaar 	case 0x01: /* write_ldt */
847146aad74SMarcel Moolenaar 	case 0x11: /* write_ldt */
848146aad74SMarcel Moolenaar 		if (uap->bytecount != sizeof(ld))
849146aad74SMarcel Moolenaar 			return (EINVAL);
850146aad74SMarcel Moolenaar 
851146aad74SMarcel Moolenaar 		error = copyin(uap->ptr, &ld, sizeof(ld));
852146aad74SMarcel Moolenaar 		if (error)
853146aad74SMarcel Moolenaar 			return (error);
854146aad74SMarcel Moolenaar 
85584569dffSMaxim Sobolev 		ldt.start = ld.entry_number;
85684569dffSMaxim Sobolev 		ldt.descs = &desc;
85784569dffSMaxim Sobolev 		ldt.num = 1;
85884569dffSMaxim Sobolev 		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
85984569dffSMaxim Sobolev 		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
86084569dffSMaxim Sobolev 		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
86184569dffSMaxim Sobolev 		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
86284569dffSMaxim Sobolev 		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
863146aad74SMarcel Moolenaar 			(ld.contents << 2);
86484569dffSMaxim Sobolev 		desc.sd.sd_dpl = 3;
86584569dffSMaxim Sobolev 		desc.sd.sd_p = (ld.seg_not_present ^ 1);
86684569dffSMaxim Sobolev 		desc.sd.sd_xx = 0;
86784569dffSMaxim Sobolev 		desc.sd.sd_def32 = ld.seg_32bit;
86884569dffSMaxim Sobolev 		desc.sd.sd_gran = ld.limit_in_pages;
86984569dffSMaxim Sobolev 		mtx_lock(&Giant);
87084569dffSMaxim Sobolev 		error = i386_set_ldt(td, &ldt, &desc);
87184569dffSMaxim Sobolev 		mtx_unlock(&Giant);
872146aad74SMarcel Moolenaar 		break;
873146aad74SMarcel Moolenaar 	default:
874146aad74SMarcel Moolenaar 		error = EINVAL;
875146aad74SMarcel Moolenaar 		break;
876146aad74SMarcel Moolenaar 	}
877146aad74SMarcel Moolenaar 
878146aad74SMarcel Moolenaar 	if (error == EOPNOTSUPP) {
879146aad74SMarcel Moolenaar 		printf("linux: modify_ldt needs kernel option USER_LDT\n");
880146aad74SMarcel Moolenaar 		error = ENOSYS;
881146aad74SMarcel Moolenaar 	}
882146aad74SMarcel Moolenaar 
883146aad74SMarcel Moolenaar 	return (error);
884146aad74SMarcel Moolenaar }
885146aad74SMarcel Moolenaar 
886146aad74SMarcel Moolenaar int
887b40ce416SJulian Elischer linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
888146aad74SMarcel Moolenaar {
8895002a60fSMarcel Moolenaar 	l_osigaction_t osa;
8905002a60fSMarcel Moolenaar 	l_sigaction_t act, oact;
891146aad74SMarcel Moolenaar 	int error;
892146aad74SMarcel Moolenaar 
893146aad74SMarcel Moolenaar #ifdef DEBUG
89424593369SJonathan Lemon 	if (ldebug(sigaction))
89524593369SJonathan Lemon 		printf(ARGS(sigaction, "%d, %p, %p"),
896146aad74SMarcel Moolenaar 		    args->sig, (void *)args->nsa, (void *)args->osa);
897146aad74SMarcel Moolenaar #endif
898146aad74SMarcel Moolenaar 
899146aad74SMarcel Moolenaar 	if (args->nsa != NULL) {
9004b7ef73dSDag-Erling Smørgrav 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
901146aad74SMarcel Moolenaar 		if (error)
902146aad74SMarcel Moolenaar 			return (error);
903146aad74SMarcel Moolenaar 		act.lsa_handler = osa.lsa_handler;
904146aad74SMarcel Moolenaar 		act.lsa_flags = osa.lsa_flags;
905146aad74SMarcel Moolenaar 		act.lsa_restorer = osa.lsa_restorer;
906146aad74SMarcel Moolenaar 		LINUX_SIGEMPTYSET(act.lsa_mask);
907146aad74SMarcel Moolenaar 		act.lsa_mask.__bits[0] = osa.lsa_mask;
908146aad74SMarcel Moolenaar 	}
909146aad74SMarcel Moolenaar 
910b40ce416SJulian Elischer 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
911146aad74SMarcel Moolenaar 	    args->osa ? &oact : NULL);
912146aad74SMarcel Moolenaar 
913146aad74SMarcel Moolenaar 	if (args->osa != NULL && !error) {
914146aad74SMarcel Moolenaar 		osa.lsa_handler = oact.lsa_handler;
915146aad74SMarcel Moolenaar 		osa.lsa_flags = oact.lsa_flags;
916146aad74SMarcel Moolenaar 		osa.lsa_restorer = oact.lsa_restorer;
917146aad74SMarcel Moolenaar 		osa.lsa_mask = oact.lsa_mask.__bits[0];
9184b7ef73dSDag-Erling Smørgrav 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
919146aad74SMarcel Moolenaar 	}
920146aad74SMarcel Moolenaar 
921146aad74SMarcel Moolenaar 	return (error);
922146aad74SMarcel Moolenaar }
923146aad74SMarcel Moolenaar 
924146aad74SMarcel Moolenaar /*
925146aad74SMarcel Moolenaar  * Linux has two extra args, restart and oldmask.  We dont use these,
926146aad74SMarcel Moolenaar  * but it seems that "restart" is actually a context pointer that
927146aad74SMarcel Moolenaar  * enables the signal to happen with a different register set.
928146aad74SMarcel Moolenaar  */
929146aad74SMarcel Moolenaar int
930b40ce416SJulian Elischer linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
931146aad74SMarcel Moolenaar {
932206a5d3aSIan Dowse 	sigset_t sigmask;
9335002a60fSMarcel Moolenaar 	l_sigset_t mask;
934146aad74SMarcel Moolenaar 
935146aad74SMarcel Moolenaar #ifdef DEBUG
93624593369SJonathan Lemon 	if (ldebug(sigsuspend))
93724593369SJonathan Lemon 		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
938146aad74SMarcel Moolenaar #endif
939146aad74SMarcel Moolenaar 
940146aad74SMarcel Moolenaar 	LINUX_SIGEMPTYSET(mask);
941146aad74SMarcel Moolenaar 	mask.__bits[0] = args->mask;
942206a5d3aSIan Dowse 	linux_to_bsd_sigset(&mask, &sigmask);
943206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
944146aad74SMarcel Moolenaar }
945146aad74SMarcel Moolenaar 
946146aad74SMarcel Moolenaar int
947b07cd97eSMark Murray linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
948146aad74SMarcel Moolenaar {
9495002a60fSMarcel Moolenaar 	l_sigset_t lmask;
950206a5d3aSIan Dowse 	sigset_t sigmask;
951146aad74SMarcel Moolenaar 	int error;
952146aad74SMarcel Moolenaar 
953146aad74SMarcel Moolenaar #ifdef DEBUG
95424593369SJonathan Lemon 	if (ldebug(rt_sigsuspend))
95524593369SJonathan Lemon 		printf(ARGS(rt_sigsuspend, "%p, %d"),
956146aad74SMarcel Moolenaar 		    (void *)uap->newset, uap->sigsetsize);
957146aad74SMarcel Moolenaar #endif
958146aad74SMarcel Moolenaar 
9595002a60fSMarcel Moolenaar 	if (uap->sigsetsize != sizeof(l_sigset_t))
960146aad74SMarcel Moolenaar 		return (EINVAL);
961146aad74SMarcel Moolenaar 
9625002a60fSMarcel Moolenaar 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
963146aad74SMarcel Moolenaar 	if (error)
964146aad74SMarcel Moolenaar 		return (error);
965146aad74SMarcel Moolenaar 
966206a5d3aSIan Dowse 	linux_to_bsd_sigset(&lmask, &sigmask);
967206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
968146aad74SMarcel Moolenaar }
969146aad74SMarcel Moolenaar 
970146aad74SMarcel Moolenaar int
971b40ce416SJulian Elischer linux_pause(struct thread *td, struct linux_pause_args *args)
972146aad74SMarcel Moolenaar {
973b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
974206a5d3aSIan Dowse 	sigset_t sigmask;
975146aad74SMarcel Moolenaar 
976146aad74SMarcel Moolenaar #ifdef DEBUG
97724593369SJonathan Lemon 	if (ldebug(pause))
97824593369SJonathan Lemon 		printf(ARGS(pause, ""));
979146aad74SMarcel Moolenaar #endif
980146aad74SMarcel Moolenaar 
981fdfdfb78SJohn Baldwin 	PROC_LOCK(p);
9824093529dSJeff Roberson 	sigmask = td->td_sigmask;
983fdfdfb78SJohn Baldwin 	PROC_UNLOCK(p);
984206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
985146aad74SMarcel Moolenaar }
986146aad74SMarcel Moolenaar 
987146aad74SMarcel Moolenaar int
988b40ce416SJulian Elischer linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
989146aad74SMarcel Moolenaar {
990206a5d3aSIan Dowse 	stack_t ss, oss;
9915002a60fSMarcel Moolenaar 	l_stack_t lss;
992146aad74SMarcel Moolenaar 	int error;
993146aad74SMarcel Moolenaar 
994146aad74SMarcel Moolenaar #ifdef DEBUG
99524593369SJonathan Lemon 	if (ldebug(sigaltstack))
99624593369SJonathan Lemon 		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
997146aad74SMarcel Moolenaar #endif
998146aad74SMarcel Moolenaar 
999206a5d3aSIan Dowse 	if (uap->uss != NULL) {
10005002a60fSMarcel Moolenaar 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
1001146aad74SMarcel Moolenaar 		if (error)
1002146aad74SMarcel Moolenaar 			return (error);
1003146aad74SMarcel Moolenaar 
1004206a5d3aSIan Dowse 		ss.ss_sp = lss.ss_sp;
1005206a5d3aSIan Dowse 		ss.ss_size = lss.ss_size;
1006206a5d3aSIan Dowse 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
1007931a7258SAndrew Gallatin 	}
1008ef36ad69SJohn Baldwin 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
1009ef36ad69SJohn Baldwin 	    (uap->uoss != NULL) ? &oss : NULL);
1010206a5d3aSIan Dowse 	if (!error && uap->uoss != NULL) {
1011206a5d3aSIan Dowse 		lss.ss_sp = oss.ss_sp;
1012206a5d3aSIan Dowse 		lss.ss_size = oss.ss_size;
1013206a5d3aSIan Dowse 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
10145002a60fSMarcel Moolenaar 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
1015146aad74SMarcel Moolenaar 	}
1016146aad74SMarcel Moolenaar 
1017146aad74SMarcel Moolenaar 	return (error);
1018146aad74SMarcel Moolenaar }
10193ad9c842SMaxim Sobolev 
10203ad9c842SMaxim Sobolev int
10213ad9c842SMaxim Sobolev linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
10223ad9c842SMaxim Sobolev {
10233ad9c842SMaxim Sobolev 	struct ftruncate_args sa;
10243ad9c842SMaxim Sobolev 
10253ad9c842SMaxim Sobolev #ifdef DEBUG
10263ad9c842SMaxim Sobolev 	if (ldebug(ftruncate64))
1027b45bbfc3SBruce Evans 		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
1028b45bbfc3SBruce Evans 		    (intmax_t)args->length);
10293ad9c842SMaxim Sobolev #endif
10303ad9c842SMaxim Sobolev 
10313ad9c842SMaxim Sobolev 	sa.fd = args->fd;
10323ad9c842SMaxim Sobolev 	sa.pad = 0;
10333ad9c842SMaxim Sobolev 	sa.length = args->length;
10343ad9c842SMaxim Sobolev 	return ftruncate(td, &sa);
10353ad9c842SMaxim Sobolev }
10361bc85c0dSDoug Rabson 
10371bc85c0dSDoug Rabson int
10381bc85c0dSDoug Rabson linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
10391bc85c0dSDoug Rabson {
10409b44bfc5SAlexander Leidinger 	struct l_user_desc info;
10419b44bfc5SAlexander Leidinger 	int error;
10429b44bfc5SAlexander Leidinger 	int idx;
10439b44bfc5SAlexander Leidinger 	int a[2];
10449b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
10459b44bfc5SAlexander Leidinger 
10469b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
10479b44bfc5SAlexander Leidinger 	if (error)
10489b44bfc5SAlexander Leidinger 		return (error);
10499b44bfc5SAlexander Leidinger 
10509b44bfc5SAlexander Leidinger #ifdef DEBUG
10519b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
10529b44bfc5SAlexander Leidinger 	   	printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
10539b44bfc5SAlexander Leidinger 		      info.entry_number,
10549b44bfc5SAlexander Leidinger       		      info.base_addr,
10559b44bfc5SAlexander Leidinger       		      info.limit,
10569b44bfc5SAlexander Leidinger       		      info.seg_32bit,
10579b44bfc5SAlexander Leidinger 		      info.contents,
10589b44bfc5SAlexander Leidinger       		      info.read_exec_only,
10599b44bfc5SAlexander Leidinger       		      info.limit_in_pages,
10609b44bfc5SAlexander Leidinger       		      info.seg_not_present,
10619b44bfc5SAlexander Leidinger       		      info.useable);
10629b44bfc5SAlexander Leidinger #endif
10639b44bfc5SAlexander Leidinger 
10649b44bfc5SAlexander Leidinger 	idx = info.entry_number;
10650eef2f8aSAlexander Leidinger 	/*
10660eef2f8aSAlexander Leidinger 	 * Semantics of linux version: every thread in the system has array
10679b44bfc5SAlexander Leidinger 	 * of 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
10689b44bfc5SAlexander Leidinger 	 * syscall loads one of the selected tls decriptors with a value
10699b44bfc5SAlexander Leidinger 	 * and also loads GDT descriptors 6, 7 and 8 with the content of the per-thread
10709b44bfc5SAlexander Leidinger 	 * descriptors.
10719b44bfc5SAlexander Leidinger 	 *
10729b44bfc5SAlexander Leidinger 	 * Semantics of fbsd version: I think we can ignore that linux has 3 per-thread
10739b44bfc5SAlexander Leidinger 	 * descriptors and use just the 1st one. The tls_array[] is used only in
10749b44bfc5SAlexander Leidinger 	 * set/get-thread_area() syscalls and for loading the GDT descriptors. In fbsd
10759b44bfc5SAlexander Leidinger 	 * we use just one GDT descriptor for TLS so we will load just one.
10769b44bfc5SAlexander Leidinger 	 * XXX: this doesnt work when user-space process tries to use more then 1 TLS segment
10779b44bfc5SAlexander Leidinger 	 * comment in the linux sources says wine might do that.
10781bc85c0dSDoug Rabson 	 */
10799b44bfc5SAlexander Leidinger 
10800eef2f8aSAlexander Leidinger 	/*
10810eef2f8aSAlexander Leidinger 	 * we support just GLIBC TLS now
10829b44bfc5SAlexander Leidinger 	 * we should let 3 proceed as well because we use this segment so
10839b44bfc5SAlexander Leidinger 	 * if code does two subsequent calls it should succeed
10849b44bfc5SAlexander Leidinger 	 */
10859b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != -1 && idx != 3)
10869b44bfc5SAlexander Leidinger 		return (EINVAL);
10879b44bfc5SAlexander Leidinger 
10880eef2f8aSAlexander Leidinger 	/*
10890eef2f8aSAlexander Leidinger 	 * we have to copy out the GDT entry we use
10909b44bfc5SAlexander Leidinger 	 * FreeBSD uses GDT entry #3 for storing %gs so load that
10919b44bfc5SAlexander Leidinger 	 * XXX: what if userspace program doesnt check this value and tries
10929b44bfc5SAlexander Leidinger 	 * to use 6, 7 or 8?
10939b44bfc5SAlexander Leidinger 	 */
10949b44bfc5SAlexander Leidinger 	idx = info.entry_number = 3;
10959b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
10969b44bfc5SAlexander Leidinger 	if (error)
10979b44bfc5SAlexander Leidinger 		return (error);
10989b44bfc5SAlexander Leidinger 
10999b44bfc5SAlexander Leidinger 	if (LDT_empty(&info)) {
11009b44bfc5SAlexander Leidinger 		a[0] = 0;
11019b44bfc5SAlexander Leidinger 		a[1] = 0;
11029b44bfc5SAlexander Leidinger 	} else {
11039b44bfc5SAlexander Leidinger 		a[0] = LDT_entry_a(&info);
11049b44bfc5SAlexander Leidinger 		a[1] = LDT_entry_b(&info);
11051bc85c0dSDoug Rabson 	}
11061bc85c0dSDoug Rabson 
11079b44bfc5SAlexander Leidinger 	memcpy(&sd, &a, sizeof(a));
11089b44bfc5SAlexander Leidinger #ifdef DEBUG
11099b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
11109b44bfc5SAlexander 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,
11119b44bfc5SAlexander Leidinger 			sd.sd_hibase,
11129b44bfc5SAlexander Leidinger 			sd.sd_lolimit,
11139b44bfc5SAlexander Leidinger 			sd.sd_hilimit,
11149b44bfc5SAlexander Leidinger 			sd.sd_type,
11159b44bfc5SAlexander Leidinger 			sd.sd_dpl,
11169b44bfc5SAlexander Leidinger 			sd.sd_p,
11179b44bfc5SAlexander Leidinger 			sd.sd_xx,
11189b44bfc5SAlexander Leidinger 			sd.sd_def32,
11199b44bfc5SAlexander Leidinger 			sd.sd_gran);
11209b44bfc5SAlexander Leidinger #endif
11211bc85c0dSDoug Rabson 
11229b44bfc5SAlexander Leidinger 	/* this is taken from i386 version of cpu_set_user_tls() */
11239b44bfc5SAlexander Leidinger 	critical_enter();
11249b44bfc5SAlexander Leidinger 	/* set %gs */
11259b44bfc5SAlexander Leidinger 	td->td_pcb->pcb_gsd = sd;
11269b44bfc5SAlexander Leidinger 	PCPU_GET(fsgs_gdt)[1] = sd;
11279b44bfc5SAlexander Leidinger 	load_gs(GSEL(GUGS_SEL, SEL_UPL));
11289b44bfc5SAlexander Leidinger 	critical_exit();
11299b44bfc5SAlexander Leidinger 
11301bc85c0dSDoug Rabson 	return (0);
11311bc85c0dSDoug Rabson }
11321bc85c0dSDoug Rabson 
11331bc85c0dSDoug Rabson int
11349b44bfc5SAlexander Leidinger linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
11351bc85c0dSDoug Rabson {
11361bc85c0dSDoug Rabson 
11379b44bfc5SAlexander Leidinger 	struct l_user_desc info;
11389b44bfc5SAlexander Leidinger 	int error;
11399b44bfc5SAlexander Leidinger 	int idx;
11409b44bfc5SAlexander Leidinger 	struct l_desc_struct desc;
11419b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
11429b44bfc5SAlexander Leidinger 
11439b44bfc5SAlexander Leidinger #ifdef DEBUG
11449b44bfc5SAlexander Leidinger 	if (ldebug(get_thread_area))
11459b44bfc5SAlexander Leidinger 		printf(ARGS(get_thread_area, "%p"), args->desc);
11469b44bfc5SAlexander Leidinger #endif
11479b44bfc5SAlexander Leidinger 
11489b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
11499b44bfc5SAlexander Leidinger 	if (error)
11509b44bfc5SAlexander Leidinger 		return (error);
11519b44bfc5SAlexander Leidinger 
11529b44bfc5SAlexander Leidinger 	idx = info.entry_number;
11539b44bfc5SAlexander Leidinger 	/* XXX: I am not sure if we want 3 to be allowed too. */
11549b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != 3)
11559b44bfc5SAlexander Leidinger 		return (EINVAL);
11569b44bfc5SAlexander Leidinger 
11579b44bfc5SAlexander Leidinger 	idx = 3;
11589b44bfc5SAlexander Leidinger 
11599b44bfc5SAlexander Leidinger 	memset(&info, 0, sizeof(info));
11609b44bfc5SAlexander Leidinger 
11619b44bfc5SAlexander Leidinger 	sd = PCPU_GET(fsgs_gdt)[1];
11629b44bfc5SAlexander Leidinger 
11639b44bfc5SAlexander Leidinger 	memcpy(&desc, &sd, sizeof(desc));
11649b44bfc5SAlexander Leidinger 
11659b44bfc5SAlexander Leidinger 	info.entry_number = idx;
11669b44bfc5SAlexander Leidinger 	info.base_addr = GET_BASE(&desc);
11679b44bfc5SAlexander Leidinger 	info.limit = GET_LIMIT(&desc);
11689b44bfc5SAlexander Leidinger 	info.seg_32bit = GET_32BIT(&desc);
11699b44bfc5SAlexander Leidinger 	info.contents = GET_CONTENTS(&desc);
11709b44bfc5SAlexander Leidinger 	info.read_exec_only = !GET_WRITABLE(&desc);
11719b44bfc5SAlexander Leidinger 	info.limit_in_pages = GET_LIMIT_PAGES(&desc);
11729b44bfc5SAlexander Leidinger 	info.seg_not_present = !GET_PRESENT(&desc);
11739b44bfc5SAlexander Leidinger 	info.useable = GET_USEABLE(&desc);
11749b44bfc5SAlexander Leidinger 
11759b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
11769b44bfc5SAlexander Leidinger 	if (error)
11779b44bfc5SAlexander Leidinger 	   	return (EFAULT);
11789b44bfc5SAlexander Leidinger 
11799b44bfc5SAlexander Leidinger 	return (0);
11809b44bfc5SAlexander Leidinger }
11819b44bfc5SAlexander Leidinger 
11829b44bfc5SAlexander Leidinger /* copied from kern/kern_time.c */
11839b44bfc5SAlexander Leidinger int
11849b44bfc5SAlexander Leidinger linux_timer_create(struct thread *td, struct linux_timer_create_args *args)
11859b44bfc5SAlexander Leidinger {
11869b44bfc5SAlexander Leidinger    	return ktimer_create(td, (struct ktimer_create_args *) args);
11879b44bfc5SAlexander Leidinger }
11889b44bfc5SAlexander Leidinger 
11899b44bfc5SAlexander Leidinger int
11909b44bfc5SAlexander Leidinger linux_timer_settime(struct thread *td, struct linux_timer_settime_args *args)
11919b44bfc5SAlexander Leidinger {
11929b44bfc5SAlexander Leidinger    	return ktimer_settime(td, (struct ktimer_settime_args *) args);
11939b44bfc5SAlexander Leidinger }
11949b44bfc5SAlexander Leidinger 
11959b44bfc5SAlexander Leidinger int
11969b44bfc5SAlexander Leidinger linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *args)
11979b44bfc5SAlexander Leidinger {
11989b44bfc5SAlexander Leidinger    	return ktimer_gettime(td, (struct ktimer_gettime_args *) args);
11999b44bfc5SAlexander Leidinger }
12009b44bfc5SAlexander Leidinger 
12019b44bfc5SAlexander Leidinger int
12029b44bfc5SAlexander Leidinger linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *args)
12039b44bfc5SAlexander Leidinger {
12049b44bfc5SAlexander Leidinger    	return ktimer_getoverrun(td, (struct ktimer_getoverrun_args *) args);
12059b44bfc5SAlexander Leidinger }
12069b44bfc5SAlexander Leidinger 
12079b44bfc5SAlexander Leidinger int
12089b44bfc5SAlexander Leidinger linux_timer_delete(struct thread *td, struct linux_timer_delete_args *args)
12099b44bfc5SAlexander Leidinger {
12109b44bfc5SAlexander Leidinger    	return ktimer_delete(td, (struct ktimer_delete_args *) args);
12119b44bfc5SAlexander Leidinger }
12129b44bfc5SAlexander Leidinger 
12139b44bfc5SAlexander Leidinger /* XXX: this wont work with module - convert it */
12149b44bfc5SAlexander Leidinger int
12159b44bfc5SAlexander Leidinger linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
12169b44bfc5SAlexander Leidinger {
12179b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
12189b44bfc5SAlexander Leidinger    	return kmq_open(td, (struct kmq_open_args *) args);
12199b44bfc5SAlexander Leidinger #else
12209b44bfc5SAlexander Leidinger 	return (ENOSYS);
12219b44bfc5SAlexander Leidinger #endif
12229b44bfc5SAlexander Leidinger }
12239b44bfc5SAlexander Leidinger 
12249b44bfc5SAlexander Leidinger int
12259b44bfc5SAlexander Leidinger linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
12269b44bfc5SAlexander Leidinger {
12279b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
12289b44bfc5SAlexander Leidinger    	return kmq_unlink(td, (struct kmq_unlink_args *) args);
12299b44bfc5SAlexander Leidinger #else
12309b44bfc5SAlexander Leidinger 	return (ENOSYS);
12319b44bfc5SAlexander Leidinger #endif
12329b44bfc5SAlexander Leidinger }
12339b44bfc5SAlexander Leidinger 
12349b44bfc5SAlexander Leidinger int
12359b44bfc5SAlexander Leidinger linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
12369b44bfc5SAlexander Leidinger {
12379b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
12389b44bfc5SAlexander Leidinger    	return kmq_timedsend(td, (struct kmq_timedsend_args *) args);
12399b44bfc5SAlexander Leidinger #else
12409b44bfc5SAlexander Leidinger 	return (ENOSYS);
12419b44bfc5SAlexander Leidinger #endif
12429b44bfc5SAlexander Leidinger }
12439b44bfc5SAlexander Leidinger 
12449b44bfc5SAlexander Leidinger int
12459b44bfc5SAlexander Leidinger linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
12469b44bfc5SAlexander Leidinger {
12479b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
12489b44bfc5SAlexander Leidinger    	return kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
12499b44bfc5SAlexander Leidinger #else
12509b44bfc5SAlexander Leidinger 	return (ENOSYS);
12519b44bfc5SAlexander Leidinger #endif
12529b44bfc5SAlexander Leidinger }
12539b44bfc5SAlexander Leidinger 
12549b44bfc5SAlexander Leidinger int
12559b44bfc5SAlexander Leidinger linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
12569b44bfc5SAlexander Leidinger {
12579b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
12589b44bfc5SAlexander Leidinger 	return kmq_notify(td, (struct kmq_notify_args *) args);
12599b44bfc5SAlexander Leidinger #else
12609b44bfc5SAlexander Leidinger 	return (ENOSYS);
12619b44bfc5SAlexander Leidinger #endif
12629b44bfc5SAlexander Leidinger }
12639b44bfc5SAlexander Leidinger 
12649b44bfc5SAlexander Leidinger int
12659b44bfc5SAlexander Leidinger linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
12669b44bfc5SAlexander Leidinger {
12679b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
12689b44bfc5SAlexander Leidinger    	return kmq_setattr(td, (struct kmq_setattr_args *) args);
12699b44bfc5SAlexander Leidinger #else
12709b44bfc5SAlexander Leidinger 	return (ENOSYS);
12719b44bfc5SAlexander Leidinger #endif
12721bc85c0dSDoug Rabson }
12731bc85c0dSDoug Rabson 
1274