xref: /freebsd/sys/i386/linux/linux_machdep.c (revision 9b44bfc5560bf0cf1da18eaadb3a9da279a76efa)
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>
34610ecfe0SMaxim Sobolev #include <sys/imgact.h>
357106ca0dSJohn Baldwin #include <sys/lock.h>
36610ecfe0SMaxim Sobolev #include <sys/malloc.h>
37fb919e4dSMark Murray #include <sys/mman.h>
38fb919e4dSMark Murray #include <sys/mutex.h>
399b44bfc5SAlexander Leidinger #include <sys/sx.h>
40fb919e4dSMark Murray #include <sys/proc.h>
419b44bfc5SAlexander Leidinger #include <sys/queue.h>
42242fae60SAndrew Gallatin #include <sys/resource.h>
43242fae60SAndrew Gallatin #include <sys/resourcevar.h>
441bc85c0dSDoug Rabson #include <sys/signalvar.h>
45206a5d3aSIan Dowse #include <sys/syscallsubr.h>
46fb919e4dSMark Murray #include <sys/sysproto.h>
47fb919e4dSMark Murray #include <sys/unistd.h>
489b44bfc5SAlexander Leidinger #include <sys/wait.h>
49146aad74SMarcel Moolenaar 
50146aad74SMarcel Moolenaar #include <machine/frame.h>
51146aad74SMarcel Moolenaar #include <machine/psl.h>
52146aad74SMarcel Moolenaar #include <machine/segments.h>
53146aad74SMarcel Moolenaar #include <machine/sysarch.h>
54146aad74SMarcel Moolenaar 
55242fae60SAndrew Gallatin #include <vm/vm.h>
56242fae60SAndrew Gallatin #include <vm/pmap.h>
57242fae60SAndrew Gallatin #include <vm/vm_map.h>
58242fae60SAndrew Gallatin 
59146aad74SMarcel Moolenaar #include <i386/linux/linux.h>
60ebea8660SMarcel Moolenaar #include <i386/linux/linux_proto.h>
61146aad74SMarcel Moolenaar #include <compat/linux/linux_ipc.h>
62146aad74SMarcel Moolenaar #include <compat/linux/linux_signal.h>
63146aad74SMarcel Moolenaar #include <compat/linux/linux_util.h>
649b44bfc5SAlexander Leidinger #include <compat/linux/linux_emul.h>
659b44bfc5SAlexander Leidinger 
669b44bfc5SAlexander Leidinger #include <i386/include/pcb.h>			/* needed for pcb definition in linux_set_thread_area */
679b44bfc5SAlexander Leidinger 
689b44bfc5SAlexander Leidinger #include "opt_posix.h"
699b44bfc5SAlexander Leidinger 
709b44bfc5SAlexander Leidinger extern struct sx emul_shared_lock;
719b44bfc5SAlexander Leidinger extern struct sx emul_lock;
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;
322146aad74SMarcel Moolenaar 
323146aad74SMarcel Moolenaar #ifdef DEBUG
32424593369SJonathan Lemon 	if (ldebug(vfork))
32524593369SJonathan Lemon 		printf(ARGS(vfork, ""));
326146aad74SMarcel Moolenaar #endif
327146aad74SMarcel Moolenaar 
328b40ce416SJulian Elischer 	if ((error = vfork(td, (struct vfork_args *)args)) != 0)
329146aad74SMarcel Moolenaar 		return (error);
330146aad74SMarcel Moolenaar 	/* Are we the child? */
331b40ce416SJulian Elischer 	if (td->td_retval[1] == 1)
332b40ce416SJulian Elischer 		td->td_retval[0] = 0;
3339b44bfc5SAlexander Leidinger 	error = linux_proc_init(td, td->td_retval[0], 0);
3349b44bfc5SAlexander Leidinger 	if (error)
3359b44bfc5SAlexander Leidinger 		return (error);
336146aad74SMarcel Moolenaar 	return (0);
337146aad74SMarcel Moolenaar }
338146aad74SMarcel Moolenaar 
339146aad74SMarcel Moolenaar int
340b40ce416SJulian Elischer linux_clone(struct thread *td, struct linux_clone_args *args)
341146aad74SMarcel Moolenaar {
3426ad0e7c5SJohn Baldwin 	int error, ff = RFPROC | RFSTOPPED;
343146aad74SMarcel Moolenaar 	struct proc *p2;
3449eb78fcfSJohn Baldwin 	struct thread *td2;
345146aad74SMarcel Moolenaar 	int exit_signal;
3469b44bfc5SAlexander Leidinger 	struct linux_emuldata *em;
347146aad74SMarcel Moolenaar 
348146aad74SMarcel Moolenaar #ifdef DEBUG
34924593369SJonathan Lemon 	if (ldebug(clone)) {
3509b44bfc5SAlexander Leidinger    	   	printf(ARGS(clone, "flags %x, stack %x, parent tid: %x, child tid: %x"),
3519b44bfc5SAlexander Leidinger 		    (unsigned int)args->flags, (unsigned int)args->stack,
3529b44bfc5SAlexander Leidinger 		    (unsigned int)args->parent_tidptr, (unsigned int)args->child_tidptr);
35324593369SJonathan Lemon 	}
354146aad74SMarcel Moolenaar #endif
355146aad74SMarcel Moolenaar 
356146aad74SMarcel Moolenaar 	exit_signal = args->flags & 0x000000ff;
357146aad74SMarcel Moolenaar 	if (exit_signal >= LINUX_NSIG)
358146aad74SMarcel Moolenaar 		return (EINVAL);
359146aad74SMarcel Moolenaar 
360146aad74SMarcel Moolenaar 	if (exit_signal <= LINUX_SIGTBLSZ)
361146aad74SMarcel Moolenaar 		exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
362146aad74SMarcel Moolenaar 
363146aad74SMarcel Moolenaar 	if (args->flags & CLONE_VM)
364146aad74SMarcel Moolenaar 		ff |= RFMEM;
365146aad74SMarcel Moolenaar 	if (args->flags & CLONE_SIGHAND)
366146aad74SMarcel Moolenaar 		ff |= RFSIGSHARE;
367146aad74SMarcel Moolenaar 	if (!(args->flags & CLONE_FILES))
368146aad74SMarcel Moolenaar 		ff |= RFFDG;
369146aad74SMarcel Moolenaar 
3704b178336SMaxim Sobolev 	/*
3714b178336SMaxim Sobolev 	 * Attempt to detect when linux_clone(2) is used for creating
3724b178336SMaxim Sobolev 	 * kernel threads. Unfortunately despite the existence of the
3734b178336SMaxim Sobolev 	 * CLONE_THREAD flag, version of linuxthreads package used in
3744b178336SMaxim Sobolev 	 * most popular distros as of beginning of 2005 doesn't make
3754b178336SMaxim Sobolev 	 * any use of it. Therefore, this detection relay fully on
3764b178336SMaxim Sobolev 	 * empirical observation that linuxthreads sets certain
3774b178336SMaxim Sobolev 	 * combination of flags, so that we can make more or less
3784b178336SMaxim Sobolev 	 * precise detection and notify the FreeBSD kernel that several
3794b178336SMaxim Sobolev 	 * processes are in fact part of the same threading group, so
3804b178336SMaxim Sobolev 	 * that special treatment is necessary for signal delivery
3814b178336SMaxim Sobolev 	 * between those processes and fd locking.
3824b178336SMaxim Sobolev 	 */
3834b178336SMaxim Sobolev 	if ((args->flags & 0xffffff00) == THREADING_FLAGS)
3844b178336SMaxim Sobolev 		ff |= RFTHREAD;
3854b178336SMaxim Sobolev 
386316ec49aSScott Long 	error = fork1(td, ff, 0, &p2);
3879eb78fcfSJohn Baldwin 	if (error)
3889eb78fcfSJohn Baldwin 		return (error);
3899eb78fcfSJohn Baldwin 
3909b44bfc5SAlexander Leidinger 	/* create the emuldata */
3919b44bfc5SAlexander Leidinger 	error = linux_proc_init(td, p2->p_pid, args->flags);
3929b44bfc5SAlexander Leidinger 	/* reference it - no need to check this */
3939b44bfc5SAlexander Leidinger 	em = em_find(p2, EMUL_UNLOCKED);
3949b44bfc5SAlexander Leidinger 	KASSERT(em != NULL, ("clone: emuldata not found.\n"));
3959b44bfc5SAlexander Leidinger 	/* and adjust it */
3969b44bfc5SAlexander Leidinger 	if (args->flags & CLONE_PARENT_SETTID) {
3979b44bfc5SAlexander Leidinger 	   	if (args->parent_tidptr == NULL) {
3989b44bfc5SAlexander Leidinger 		   	EMUL_UNLOCK(&emul_lock);
3999b44bfc5SAlexander Leidinger 			return (EINVAL);
4009b44bfc5SAlexander Leidinger 		}
4019b44bfc5SAlexander Leidinger 		error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid));
4029b44bfc5SAlexander Leidinger 		if (error) {
4039b44bfc5SAlexander Leidinger 		   	EMUL_UNLOCK(&emul_lock);
4049b44bfc5SAlexander Leidinger 			return (error);
4059b44bfc5SAlexander Leidinger 		}
4069b44bfc5SAlexander Leidinger 	}
4079b44bfc5SAlexander Leidinger 
4089b44bfc5SAlexander Leidinger 	if (args->flags & CLONE_PARENT) {
4099b44bfc5SAlexander Leidinger #ifdef DEBUG
4109b44bfc5SAlexander Leidinger 	   	printf("linux_clone: CLONE_PARENT\n");
4119b44bfc5SAlexander Leidinger #endif
4129b44bfc5SAlexander Leidinger 	}
4139b44bfc5SAlexander Leidinger 
4149b44bfc5SAlexander Leidinger 	if (args->flags & CLONE_THREAD) {
4159b44bfc5SAlexander Leidinger 	   	/* XXX: linux mangles pgrp and pptr somehow
4169b44bfc5SAlexander Leidinger 		 * I think it might be this but I am not sure.
4179b44bfc5SAlexander Leidinger 		 */
4189b44bfc5SAlexander Leidinger #ifdef notyet
4199b44bfc5SAlexander Leidinger 	   	p2->p_pgrp = td->td_proc->p_pgrp;
4209b44bfc5SAlexander Leidinger 	 	p2->p_pptr = td->td_proc->p_pptr;
4219b44bfc5SAlexander Leidinger #endif
4229b44bfc5SAlexander Leidinger 	 	exit_signal = 0;
4239b44bfc5SAlexander Leidinger #ifdef DEBUG
4249b44bfc5SAlexander Leidinger 	   	printf("linux_clone: CLONE_THREADS\n");
4259b44bfc5SAlexander Leidinger #endif
4269b44bfc5SAlexander Leidinger 	}
4279b44bfc5SAlexander Leidinger 
4289b44bfc5SAlexander Leidinger 	if (args->flags & CLONE_CHILD_SETTID)
4299b44bfc5SAlexander Leidinger 		em->child_set_tid = args->child_tidptr;
4309b44bfc5SAlexander Leidinger 	else
4319b44bfc5SAlexander Leidinger 	   	em->child_set_tid = NULL;
4329b44bfc5SAlexander Leidinger 
4339b44bfc5SAlexander Leidinger 	if (args->flags & CLONE_CHILD_CLEARTID)
4349b44bfc5SAlexander Leidinger 		em->child_clear_tid = args->child_tidptr;
4359b44bfc5SAlexander Leidinger 	else
4369b44bfc5SAlexander Leidinger 	   	em->child_clear_tid = NULL;
4379b44bfc5SAlexander Leidinger 	EMUL_UNLOCK(&emul_lock);
438146aad74SMarcel Moolenaar 
439fdfdfb78SJohn Baldwin 	PROC_LOCK(p2);
440146aad74SMarcel Moolenaar 	p2->p_sigparent = exit_signal;
4419eb78fcfSJohn Baldwin 	PROC_UNLOCK(p2);
4429eb78fcfSJohn Baldwin 	td2 = FIRST_THREAD_IN_PROC(p2);
4439b44bfc5SAlexander Leidinger 	/* in a case of stack = NULL we are supposed to COW calling process stack
4449b44bfc5SAlexander Leidinger 	 * this is what normal fork() does so we just keep the tf_esp arg intact
4459b44bfc5SAlexander Leidinger 	 */
4469b44bfc5SAlexander Leidinger 	if (args->stack)
4479eb78fcfSJohn Baldwin    	   	td2->td_frame->tf_esp = (unsigned int)args->stack;
448146aad74SMarcel Moolenaar 
4499b44bfc5SAlexander Leidinger 	if (args->flags & CLONE_SETTLS) {
4509b44bfc5SAlexander Leidinger    	   	struct l_user_desc info;
4519b44bfc5SAlexander Leidinger    	   	int idx;
4529b44bfc5SAlexander Leidinger 	   	int a[2];
4539b44bfc5SAlexander Leidinger 		struct segment_descriptor sd;
4549b44bfc5SAlexander Leidinger 
4559b44bfc5SAlexander Leidinger 	   	error = copyin((void *)td->td_frame->tf_esi, &info, sizeof(struct l_user_desc));
4569b44bfc5SAlexander Leidinger 		if (error)
4579b44bfc5SAlexander Leidinger    		   	return (error);
4589b44bfc5SAlexander Leidinger 
4599b44bfc5SAlexander Leidinger 		idx = info.entry_number;
4609b44bfc5SAlexander Leidinger 
4619b44bfc5SAlexander Leidinger 		/* looks like we're getting the idx we returned
4629b44bfc5SAlexander Leidinger 		 * in the set_thread_area() syscall
4639b44bfc5SAlexander Leidinger 		 */
4649b44bfc5SAlexander Leidinger 		if (idx != 6 && idx != 3)
4659b44bfc5SAlexander Leidinger 			return (EINVAL);
4669b44bfc5SAlexander Leidinger 
4679b44bfc5SAlexander Leidinger 		/* this doesnt happen in practice */
4689b44bfc5SAlexander Leidinger 		if (idx == 6) {
4699b44bfc5SAlexander Leidinger 		   	/* we might copy out the entry_number as 3 */
4709b44bfc5SAlexander Leidinger 		   	info.entry_number = 3;
4719b44bfc5SAlexander Leidinger 			error = copyout(&info, (void *) td->td_frame->tf_esi, sizeof(struct l_user_desc));
4729b44bfc5SAlexander Leidinger 			if (error)
4739b44bfc5SAlexander Leidinger 	   		   	return (error);
4749b44bfc5SAlexander Leidinger 		}
4759b44bfc5SAlexander Leidinger 
4769b44bfc5SAlexander Leidinger 		a[0] = LDT_entry_a(&info);
4779b44bfc5SAlexander Leidinger 		a[1] = LDT_entry_b(&info);
4789b44bfc5SAlexander Leidinger 
4799b44bfc5SAlexander Leidinger 		memcpy(&sd, &a, sizeof(a));
4809b44bfc5SAlexander Leidinger #ifdef DEBUG
4819b44bfc5SAlexander Leidinger 	if (ldebug(clone))
4829b44bfc5SAlexander 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,
4839b44bfc5SAlexander Leidinger 			sd.sd_hibase,
4849b44bfc5SAlexander Leidinger 			sd.sd_lolimit,
4859b44bfc5SAlexander Leidinger 			sd.sd_hilimit,
4869b44bfc5SAlexander Leidinger 			sd.sd_type,
4879b44bfc5SAlexander Leidinger 			sd.sd_dpl,
4889b44bfc5SAlexander Leidinger 			sd.sd_p,
4899b44bfc5SAlexander Leidinger 			sd.sd_xx,
4909b44bfc5SAlexander Leidinger 			sd.sd_def32,
4919b44bfc5SAlexander Leidinger 			sd.sd_gran);
4929b44bfc5SAlexander Leidinger #endif
4939b44bfc5SAlexander Leidinger 
4949b44bfc5SAlexander Leidinger 		/* this is taken from i386 version of cpu_set_user_tls() */
4959b44bfc5SAlexander Leidinger 		critical_enter();
4969b44bfc5SAlexander Leidinger 		/* set %gs */
4979b44bfc5SAlexander Leidinger 		td2->td_pcb->pcb_gsd = sd;
4989b44bfc5SAlexander Leidinger 		PCPU_GET(fsgs_gdt)[1] = sd;
4999b44bfc5SAlexander Leidinger 		load_gs(GSEL(GUGS_SEL, SEL_UPL));
5009b44bfc5SAlexander Leidinger 		critical_exit();
5019b44bfc5SAlexander Leidinger 	}
5029b44bfc5SAlexander Leidinger 
503146aad74SMarcel Moolenaar #ifdef DEBUG
50424593369SJonathan Lemon 	if (ldebug(clone))
5059eb78fcfSJohn Baldwin 		printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
5069eb78fcfSJohn Baldwin 		    (long)p2->p_pid, args->stack, exit_signal);
507146aad74SMarcel Moolenaar #endif
508146aad74SMarcel Moolenaar 
5096ad0e7c5SJohn Baldwin 	/*
5106ad0e7c5SJohn Baldwin 	 * Make this runnable after we are finished with it.
5116ad0e7c5SJohn Baldwin 	 */
5126ad0e7c5SJohn Baldwin 	mtx_lock_spin(&sched_lock);
5139eb78fcfSJohn Baldwin 	TD_SET_CAN_RUN(td2);
5142630e4c9SJulian Elischer 	setrunqueue(td2, SRQ_BORING);
5156ad0e7c5SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
5166ad0e7c5SJohn Baldwin 
5179eb78fcfSJohn Baldwin 	td->td_retval[0] = p2->p_pid;
5189eb78fcfSJohn Baldwin 	td->td_retval[1] = 0;
5199eb78fcfSJohn Baldwin 	return (0);
520146aad74SMarcel Moolenaar }
521146aad74SMarcel Moolenaar 
522146aad74SMarcel Moolenaar /* XXX move */
5235002a60fSMarcel Moolenaar struct l_mmap_argv {
5245002a60fSMarcel Moolenaar 	l_caddr_t	addr;
5255002a60fSMarcel Moolenaar 	l_int		len;
5265002a60fSMarcel Moolenaar 	l_int		prot;
5275002a60fSMarcel Moolenaar 	l_int		flags;
5285002a60fSMarcel Moolenaar 	l_int		fd;
5295002a60fSMarcel Moolenaar 	l_int		pos;
530146aad74SMarcel Moolenaar };
531146aad74SMarcel Moolenaar 
532146aad74SMarcel Moolenaar #define STACK_SIZE  (2 * 1024 * 1024)
533146aad74SMarcel Moolenaar #define GUARD_SIZE  (4 * PAGE_SIZE)
534146aad74SMarcel Moolenaar 
5353ad9c842SMaxim Sobolev static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
5363ad9c842SMaxim Sobolev 
5373ad9c842SMaxim Sobolev int
5383ad9c842SMaxim Sobolev linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
5393ad9c842SMaxim Sobolev {
5403ad9c842SMaxim Sobolev 	struct l_mmap_argv linux_args;
5413ad9c842SMaxim Sobolev 
5423ad9c842SMaxim Sobolev #ifdef DEBUG
5433ad9c842SMaxim Sobolev 	if (ldebug(mmap2))
5443ad9c842SMaxim Sobolev 		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
5453ad9c842SMaxim Sobolev 		    (void *)args->addr, args->len, args->prot,
5463ad9c842SMaxim Sobolev 		    args->flags, args->fd, args->pgoff);
5473ad9c842SMaxim Sobolev #endif
5483ad9c842SMaxim Sobolev 
5493ad9c842SMaxim Sobolev 	linux_args.addr = (l_caddr_t)args->addr;
5503ad9c842SMaxim Sobolev 	linux_args.len = args->len;
5513ad9c842SMaxim Sobolev 	linux_args.prot = args->prot;
5523ad9c842SMaxim Sobolev 	linux_args.flags = args->flags;
5533ad9c842SMaxim Sobolev 	linux_args.fd = args->fd;
5543ad9c842SMaxim Sobolev 	linux_args.pos = args->pgoff * PAGE_SIZE;
5553ad9c842SMaxim Sobolev 
5563ad9c842SMaxim Sobolev 	return (linux_mmap_common(td, &linux_args));
5573ad9c842SMaxim Sobolev }
5583ad9c842SMaxim Sobolev 
559146aad74SMarcel Moolenaar int
560b40ce416SJulian Elischer linux_mmap(struct thread *td, struct linux_mmap_args *args)
561146aad74SMarcel Moolenaar {
5623ad9c842SMaxim Sobolev 	int error;
5633ad9c842SMaxim Sobolev 	struct l_mmap_argv linux_args;
5643ad9c842SMaxim Sobolev 
5654b7ef73dSDag-Erling Smørgrav 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
5663ad9c842SMaxim Sobolev 	if (error)
5673ad9c842SMaxim Sobolev 		return (error);
5683ad9c842SMaxim Sobolev 
5693ad9c842SMaxim Sobolev #ifdef DEBUG
5703ad9c842SMaxim Sobolev 	if (ldebug(mmap))
5713ad9c842SMaxim Sobolev 		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
572b45bbfc3SBruce Evans 		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
573b45bbfc3SBruce Evans 		    linux_args.flags, linux_args.fd, linux_args.pos);
5743ad9c842SMaxim Sobolev #endif
5753ad9c842SMaxim Sobolev 
5763ad9c842SMaxim Sobolev 	return (linux_mmap_common(td, &linux_args));
5773ad9c842SMaxim Sobolev }
5783ad9c842SMaxim Sobolev 
5793ad9c842SMaxim Sobolev static int
5803ad9c842SMaxim Sobolev linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
5813ad9c842SMaxim Sobolev {
582b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
583146aad74SMarcel Moolenaar 	struct mmap_args /* {
584146aad74SMarcel Moolenaar 		caddr_t addr;
585146aad74SMarcel Moolenaar 		size_t len;
586146aad74SMarcel Moolenaar 		int prot;
587146aad74SMarcel Moolenaar 		int flags;
588146aad74SMarcel Moolenaar 		int fd;
589146aad74SMarcel Moolenaar 		long pad;
590146aad74SMarcel Moolenaar 		off_t pos;
591146aad74SMarcel Moolenaar 	} */ bsd_args;
59291d631e5SMatthew N. Dodd 	int error;
593146aad74SMarcel Moolenaar 
59491d631e5SMatthew N. Dodd 	error = 0;
595146aad74SMarcel Moolenaar 	bsd_args.flags = 0;
5963ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_SHARED)
597146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_SHARED;
5983ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_PRIVATE)
599146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_PRIVATE;
6003ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_FIXED)
601146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_FIXED;
6023ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_ANON)
603146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_ANON;
6040cc3ac8bSMatthew Dillon 	else
6050cc3ac8bSMatthew Dillon 		bsd_args.flags |= MAP_NOSYNC;
6063ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
607146aad74SMarcel Moolenaar 		bsd_args.flags |= MAP_STACK;
608146aad74SMarcel Moolenaar 
609146aad74SMarcel Moolenaar 		/* The linux MAP_GROWSDOWN option does not limit auto
610146aad74SMarcel Moolenaar 		 * growth of the region.  Linux mmap with this option
611146aad74SMarcel Moolenaar 		 * takes as addr the inital BOS, and as len, the initial
612146aad74SMarcel Moolenaar 		 * region size.  It can then grow down from addr without
613146aad74SMarcel Moolenaar 		 * limit.  However, linux threads has an implicit internal
614146aad74SMarcel Moolenaar 		 * limit to stack size of STACK_SIZE.  Its just not
615146aad74SMarcel Moolenaar 		 * enforced explicitly in linux.  But, here we impose
616146aad74SMarcel Moolenaar 		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
617146aad74SMarcel Moolenaar 		 * region, since we can do this with our mmap.
618146aad74SMarcel Moolenaar 		 *
619146aad74SMarcel Moolenaar 		 * Our mmap with MAP_STACK takes addr as the maximum
620146aad74SMarcel Moolenaar 		 * downsize limit on BOS, and as len the max size of
621146aad74SMarcel Moolenaar 		 * the region.  It them maps the top SGROWSIZ bytes,
622146aad74SMarcel Moolenaar 		 * and autgrows the region down, up to the limit
623146aad74SMarcel Moolenaar 		 * in addr.
624146aad74SMarcel Moolenaar 		 *
625146aad74SMarcel Moolenaar 		 * If we don't use the MAP_STACK option, the effect
626146aad74SMarcel Moolenaar 		 * of this code is to allocate a stack region of a
627146aad74SMarcel Moolenaar 		 * fixed size of (STACK_SIZE - GUARD_SIZE).
628146aad74SMarcel Moolenaar 		 */
629146aad74SMarcel Moolenaar 
630146aad74SMarcel Moolenaar 		/* This gives us TOS */
6313ad9c842SMaxim Sobolev 		bsd_args.addr = linux_args->addr + linux_args->len;
632146aad74SMarcel Moolenaar 
633242fae60SAndrew Gallatin 		if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
634242fae60SAndrew Gallatin 			/* Some linux apps will attempt to mmap
635242fae60SAndrew Gallatin 			 * thread stacks near the top of their
636242fae60SAndrew Gallatin 			 * address space.  If their TOS is greater
637242fae60SAndrew Gallatin 			 * than vm_maxsaddr, vm_map_growstack()
638242fae60SAndrew Gallatin 			 * will confuse the thread stack with the
639242fae60SAndrew Gallatin 			 * process stack and deliver a SEGV if they
640242fae60SAndrew Gallatin 			 * attempt to grow the thread stack past their
641242fae60SAndrew Gallatin 			 * current stacksize rlimit.  To avoid this,
642242fae60SAndrew Gallatin 			 * adjust vm_maxsaddr upwards to reflect
643242fae60SAndrew Gallatin 			 * the current stacksize rlimit rather
644242fae60SAndrew Gallatin 			 * than the maximum possible stacksize.
645242fae60SAndrew Gallatin 			 * It would be better to adjust the
646242fae60SAndrew Gallatin 			 * mmap'ed region, but some apps do not check
647242fae60SAndrew Gallatin 			 * mmap's return value.
648242fae60SAndrew Gallatin 			 */
64991d5354aSJohn Baldwin 			PROC_LOCK(p);
650242fae60SAndrew Gallatin 			p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
65191d5354aSJohn Baldwin 			    lim_cur(p, RLIMIT_STACK);
65291d5354aSJohn Baldwin 			PROC_UNLOCK(p);
653242fae60SAndrew Gallatin 		}
654242fae60SAndrew Gallatin 
655146aad74SMarcel Moolenaar 		/* This gives us our maximum stack size */
6563ad9c842SMaxim Sobolev 		if (linux_args->len > STACK_SIZE - GUARD_SIZE)
6573ad9c842SMaxim Sobolev 			bsd_args.len = linux_args->len;
658146aad74SMarcel Moolenaar 		else
659146aad74SMarcel Moolenaar 			bsd_args.len  = STACK_SIZE - GUARD_SIZE;
660146aad74SMarcel Moolenaar 
661146aad74SMarcel Moolenaar 		/* This gives us a new BOS.  If we're using VM_STACK, then
662146aad74SMarcel Moolenaar 		 * mmap will just map the top SGROWSIZ bytes, and let
663146aad74SMarcel Moolenaar 		 * the stack grow down to the limit at BOS.  If we're
664146aad74SMarcel Moolenaar 		 * not using VM_STACK we map the full stack, since we
665146aad74SMarcel Moolenaar 		 * don't have a way to autogrow it.
666146aad74SMarcel Moolenaar 		 */
667146aad74SMarcel Moolenaar 		bsd_args.addr -= bsd_args.len;
668146aad74SMarcel Moolenaar 	} else {
6693ad9c842SMaxim Sobolev 		bsd_args.addr = linux_args->addr;
6703ad9c842SMaxim Sobolev 		bsd_args.len  = linux_args->len;
671146aad74SMarcel Moolenaar 	}
672146aad74SMarcel Moolenaar 
6733ad9c842SMaxim Sobolev 	bsd_args.prot = linux_args->prot | PROT_READ;	/* always required */
6743ad9c842SMaxim Sobolev 	if (linux_args->flags & LINUX_MAP_ANON)
675146aad74SMarcel Moolenaar 		bsd_args.fd = -1;
676146aad74SMarcel Moolenaar 	else
6773ad9c842SMaxim Sobolev 		bsd_args.fd = linux_args->fd;
6783ad9c842SMaxim Sobolev 	bsd_args.pos = linux_args->pos;
679146aad74SMarcel Moolenaar 	bsd_args.pad = 0;
680146aad74SMarcel Moolenaar 
681146aad74SMarcel Moolenaar #ifdef DEBUG
68224593369SJonathan Lemon 	if (ldebug(mmap))
68391d631e5SMatthew N. Dodd 		printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
68491d631e5SMatthew N. Dodd 		    __func__,
68524593369SJonathan Lemon 		    (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
68624593369SJonathan Lemon 		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
687146aad74SMarcel Moolenaar #endif
68891d631e5SMatthew N. Dodd 	error = mmap(td, &bsd_args);
68991d631e5SMatthew N. Dodd #ifdef DEBUG
69091d631e5SMatthew N. Dodd 	if (ldebug(mmap))
69191d631e5SMatthew N. Dodd 		printf("-> %s() return: 0x%x (0x%08x)\n",
69291d631e5SMatthew N. Dodd 			__func__, error, (u_int)td->td_retval[0]);
69391d631e5SMatthew N. Dodd #endif
69491d631e5SMatthew N. Dodd 	return (error);
695146aad74SMarcel Moolenaar }
696146aad74SMarcel Moolenaar 
697146aad74SMarcel Moolenaar int
698b40ce416SJulian Elischer linux_pipe(struct thread *td, struct linux_pipe_args *args)
699146aad74SMarcel Moolenaar {
700146aad74SMarcel Moolenaar 	int error;
701146aad74SMarcel Moolenaar 	int reg_edx;
702146aad74SMarcel Moolenaar 
703146aad74SMarcel Moolenaar #ifdef DEBUG
70424593369SJonathan Lemon 	if (ldebug(pipe))
70524593369SJonathan Lemon 		printf(ARGS(pipe, "*"));
706146aad74SMarcel Moolenaar #endif
707146aad74SMarcel Moolenaar 
708b40ce416SJulian Elischer 	reg_edx = td->td_retval[1];
709b40ce416SJulian Elischer 	error = pipe(td, 0);
710146aad74SMarcel Moolenaar 	if (error) {
711b40ce416SJulian Elischer 		td->td_retval[1] = reg_edx;
712146aad74SMarcel Moolenaar 		return (error);
713146aad74SMarcel Moolenaar 	}
714146aad74SMarcel Moolenaar 
715b40ce416SJulian Elischer 	error = copyout(td->td_retval, args->pipefds, 2*sizeof(int));
716146aad74SMarcel Moolenaar 	if (error) {
717b40ce416SJulian Elischer 		td->td_retval[1] = reg_edx;
718146aad74SMarcel Moolenaar 		return (error);
719146aad74SMarcel Moolenaar 	}
720146aad74SMarcel Moolenaar 
721b40ce416SJulian Elischer 	td->td_retval[1] = reg_edx;
722b40ce416SJulian Elischer 	td->td_retval[0] = 0;
723146aad74SMarcel Moolenaar 	return (0);
724146aad74SMarcel Moolenaar }
725146aad74SMarcel Moolenaar 
726146aad74SMarcel Moolenaar int
727b40ce416SJulian Elischer linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
728146aad74SMarcel Moolenaar {
72984569dffSMaxim Sobolev 	int error;
73084569dffSMaxim Sobolev 	struct i386_ioperm_args iia;
731146aad74SMarcel Moolenaar 
73284569dffSMaxim Sobolev 	iia.start = args->start;
73384569dffSMaxim Sobolev 	iia.length = args->length;
73484569dffSMaxim Sobolev 	iia.enable = args->enable;
73584569dffSMaxim Sobolev 	mtx_lock(&Giant);
73684569dffSMaxim Sobolev 	error = i386_set_ioperm(td, &iia);
73784569dffSMaxim Sobolev 	mtx_unlock(&Giant);
73884569dffSMaxim Sobolev 	return (error);
739146aad74SMarcel Moolenaar }
740146aad74SMarcel Moolenaar 
741146aad74SMarcel Moolenaar int
742b40ce416SJulian Elischer linux_iopl(struct thread *td, struct linux_iopl_args *args)
743146aad74SMarcel Moolenaar {
744146aad74SMarcel Moolenaar 	int error;
745146aad74SMarcel Moolenaar 
746146aad74SMarcel Moolenaar 	if (args->level < 0 || args->level > 3)
747146aad74SMarcel Moolenaar 		return (EINVAL);
74844731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
749146aad74SMarcel Moolenaar 		return (error);
750a854ed98SJohn Baldwin 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
75141c42188SRobert Watson 		return (error);
752b40ce416SJulian Elischer 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
753146aad74SMarcel Moolenaar 	    (args->level * (PSL_IOPL / 3));
754146aad74SMarcel Moolenaar 	return (0);
755146aad74SMarcel Moolenaar }
756146aad74SMarcel Moolenaar 
757146aad74SMarcel Moolenaar int
758b07cd97eSMark Murray linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
759146aad74SMarcel Moolenaar {
760146aad74SMarcel Moolenaar 	int error;
76184569dffSMaxim Sobolev 	struct i386_ldt_args ldt;
7625002a60fSMarcel Moolenaar 	struct l_descriptor ld;
76384569dffSMaxim Sobolev 	union descriptor desc;
764146aad74SMarcel Moolenaar 
765146aad74SMarcel Moolenaar 	if (uap->ptr == NULL)
766146aad74SMarcel Moolenaar 		return (EINVAL);
767146aad74SMarcel Moolenaar 
768146aad74SMarcel Moolenaar 	switch (uap->func) {
769146aad74SMarcel Moolenaar 	case 0x00: /* read_ldt */
77084569dffSMaxim Sobolev 		ldt.start = 0;
77184569dffSMaxim Sobolev 		ldt.descs = uap->ptr;
77284569dffSMaxim Sobolev 		ldt.num = uap->bytecount / sizeof(union descriptor);
77384569dffSMaxim Sobolev 		mtx_lock(&Giant);
77484569dffSMaxim Sobolev 		error = i386_get_ldt(td, &ldt);
775b40ce416SJulian Elischer 		td->td_retval[0] *= sizeof(union descriptor);
77684569dffSMaxim Sobolev 		mtx_unlock(&Giant);
777146aad74SMarcel Moolenaar 		break;
778146aad74SMarcel Moolenaar 	case 0x01: /* write_ldt */
779146aad74SMarcel Moolenaar 	case 0x11: /* write_ldt */
780146aad74SMarcel Moolenaar 		if (uap->bytecount != sizeof(ld))
781146aad74SMarcel Moolenaar 			return (EINVAL);
782146aad74SMarcel Moolenaar 
783146aad74SMarcel Moolenaar 		error = copyin(uap->ptr, &ld, sizeof(ld));
784146aad74SMarcel Moolenaar 		if (error)
785146aad74SMarcel Moolenaar 			return (error);
786146aad74SMarcel Moolenaar 
78784569dffSMaxim Sobolev 		ldt.start = ld.entry_number;
78884569dffSMaxim Sobolev 		ldt.descs = &desc;
78984569dffSMaxim Sobolev 		ldt.num = 1;
79084569dffSMaxim Sobolev 		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
79184569dffSMaxim Sobolev 		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
79284569dffSMaxim Sobolev 		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
79384569dffSMaxim Sobolev 		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
79484569dffSMaxim Sobolev 		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
795146aad74SMarcel Moolenaar 			(ld.contents << 2);
79684569dffSMaxim Sobolev 		desc.sd.sd_dpl = 3;
79784569dffSMaxim Sobolev 		desc.sd.sd_p = (ld.seg_not_present ^ 1);
79884569dffSMaxim Sobolev 		desc.sd.sd_xx = 0;
79984569dffSMaxim Sobolev 		desc.sd.sd_def32 = ld.seg_32bit;
80084569dffSMaxim Sobolev 		desc.sd.sd_gran = ld.limit_in_pages;
80184569dffSMaxim Sobolev 		mtx_lock(&Giant);
80284569dffSMaxim Sobolev 		error = i386_set_ldt(td, &ldt, &desc);
80384569dffSMaxim Sobolev 		mtx_unlock(&Giant);
804146aad74SMarcel Moolenaar 		break;
805146aad74SMarcel Moolenaar 	default:
806146aad74SMarcel Moolenaar 		error = EINVAL;
807146aad74SMarcel Moolenaar 		break;
808146aad74SMarcel Moolenaar 	}
809146aad74SMarcel Moolenaar 
810146aad74SMarcel Moolenaar 	if (error == EOPNOTSUPP) {
811146aad74SMarcel Moolenaar 		printf("linux: modify_ldt needs kernel option USER_LDT\n");
812146aad74SMarcel Moolenaar 		error = ENOSYS;
813146aad74SMarcel Moolenaar 	}
814146aad74SMarcel Moolenaar 
815146aad74SMarcel Moolenaar 	return (error);
816146aad74SMarcel Moolenaar }
817146aad74SMarcel Moolenaar 
818146aad74SMarcel Moolenaar int
819b40ce416SJulian Elischer linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
820146aad74SMarcel Moolenaar {
8215002a60fSMarcel Moolenaar 	l_osigaction_t osa;
8225002a60fSMarcel Moolenaar 	l_sigaction_t act, oact;
823146aad74SMarcel Moolenaar 	int error;
824146aad74SMarcel Moolenaar 
825146aad74SMarcel Moolenaar #ifdef DEBUG
82624593369SJonathan Lemon 	if (ldebug(sigaction))
82724593369SJonathan Lemon 		printf(ARGS(sigaction, "%d, %p, %p"),
828146aad74SMarcel Moolenaar 		    args->sig, (void *)args->nsa, (void *)args->osa);
829146aad74SMarcel Moolenaar #endif
830146aad74SMarcel Moolenaar 
831146aad74SMarcel Moolenaar 	if (args->nsa != NULL) {
8324b7ef73dSDag-Erling Smørgrav 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
833146aad74SMarcel Moolenaar 		if (error)
834146aad74SMarcel Moolenaar 			return (error);
835146aad74SMarcel Moolenaar 		act.lsa_handler = osa.lsa_handler;
836146aad74SMarcel Moolenaar 		act.lsa_flags = osa.lsa_flags;
837146aad74SMarcel Moolenaar 		act.lsa_restorer = osa.lsa_restorer;
838146aad74SMarcel Moolenaar 		LINUX_SIGEMPTYSET(act.lsa_mask);
839146aad74SMarcel Moolenaar 		act.lsa_mask.__bits[0] = osa.lsa_mask;
840146aad74SMarcel Moolenaar 	}
841146aad74SMarcel Moolenaar 
842b40ce416SJulian Elischer 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
843146aad74SMarcel Moolenaar 	    args->osa ? &oact : NULL);
844146aad74SMarcel Moolenaar 
845146aad74SMarcel Moolenaar 	if (args->osa != NULL && !error) {
846146aad74SMarcel Moolenaar 		osa.lsa_handler = oact.lsa_handler;
847146aad74SMarcel Moolenaar 		osa.lsa_flags = oact.lsa_flags;
848146aad74SMarcel Moolenaar 		osa.lsa_restorer = oact.lsa_restorer;
849146aad74SMarcel Moolenaar 		osa.lsa_mask = oact.lsa_mask.__bits[0];
8504b7ef73dSDag-Erling Smørgrav 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
851146aad74SMarcel Moolenaar 	}
852146aad74SMarcel Moolenaar 
853146aad74SMarcel Moolenaar 	return (error);
854146aad74SMarcel Moolenaar }
855146aad74SMarcel Moolenaar 
856146aad74SMarcel Moolenaar /*
857146aad74SMarcel Moolenaar  * Linux has two extra args, restart and oldmask.  We dont use these,
858146aad74SMarcel Moolenaar  * but it seems that "restart" is actually a context pointer that
859146aad74SMarcel Moolenaar  * enables the signal to happen with a different register set.
860146aad74SMarcel Moolenaar  */
861146aad74SMarcel Moolenaar int
862b40ce416SJulian Elischer linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
863146aad74SMarcel Moolenaar {
864206a5d3aSIan Dowse 	sigset_t sigmask;
8655002a60fSMarcel Moolenaar 	l_sigset_t mask;
866146aad74SMarcel Moolenaar 
867146aad74SMarcel Moolenaar #ifdef DEBUG
86824593369SJonathan Lemon 	if (ldebug(sigsuspend))
86924593369SJonathan Lemon 		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
870146aad74SMarcel Moolenaar #endif
871146aad74SMarcel Moolenaar 
872146aad74SMarcel Moolenaar 	LINUX_SIGEMPTYSET(mask);
873146aad74SMarcel Moolenaar 	mask.__bits[0] = args->mask;
874206a5d3aSIan Dowse 	linux_to_bsd_sigset(&mask, &sigmask);
875206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
876146aad74SMarcel Moolenaar }
877146aad74SMarcel Moolenaar 
878146aad74SMarcel Moolenaar int
879b07cd97eSMark Murray linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
880146aad74SMarcel Moolenaar {
8815002a60fSMarcel Moolenaar 	l_sigset_t lmask;
882206a5d3aSIan Dowse 	sigset_t sigmask;
883146aad74SMarcel Moolenaar 	int error;
884146aad74SMarcel Moolenaar 
885146aad74SMarcel Moolenaar #ifdef DEBUG
88624593369SJonathan Lemon 	if (ldebug(rt_sigsuspend))
88724593369SJonathan Lemon 		printf(ARGS(rt_sigsuspend, "%p, %d"),
888146aad74SMarcel Moolenaar 		    (void *)uap->newset, uap->sigsetsize);
889146aad74SMarcel Moolenaar #endif
890146aad74SMarcel Moolenaar 
8915002a60fSMarcel Moolenaar 	if (uap->sigsetsize != sizeof(l_sigset_t))
892146aad74SMarcel Moolenaar 		return (EINVAL);
893146aad74SMarcel Moolenaar 
8945002a60fSMarcel Moolenaar 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
895146aad74SMarcel Moolenaar 	if (error)
896146aad74SMarcel Moolenaar 		return (error);
897146aad74SMarcel Moolenaar 
898206a5d3aSIan Dowse 	linux_to_bsd_sigset(&lmask, &sigmask);
899206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
900146aad74SMarcel Moolenaar }
901146aad74SMarcel Moolenaar 
902146aad74SMarcel Moolenaar int
903b40ce416SJulian Elischer linux_pause(struct thread *td, struct linux_pause_args *args)
904146aad74SMarcel Moolenaar {
905b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
906206a5d3aSIan Dowse 	sigset_t sigmask;
907146aad74SMarcel Moolenaar 
908146aad74SMarcel Moolenaar #ifdef DEBUG
90924593369SJonathan Lemon 	if (ldebug(pause))
91024593369SJonathan Lemon 		printf(ARGS(pause, ""));
911146aad74SMarcel Moolenaar #endif
912146aad74SMarcel Moolenaar 
913fdfdfb78SJohn Baldwin 	PROC_LOCK(p);
9144093529dSJeff Roberson 	sigmask = td->td_sigmask;
915fdfdfb78SJohn Baldwin 	PROC_UNLOCK(p);
916206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
917146aad74SMarcel Moolenaar }
918146aad74SMarcel Moolenaar 
919146aad74SMarcel Moolenaar int
920b40ce416SJulian Elischer linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
921146aad74SMarcel Moolenaar {
922206a5d3aSIan Dowse 	stack_t ss, oss;
9235002a60fSMarcel Moolenaar 	l_stack_t lss;
924146aad74SMarcel Moolenaar 	int error;
925146aad74SMarcel Moolenaar 
926146aad74SMarcel Moolenaar #ifdef DEBUG
92724593369SJonathan Lemon 	if (ldebug(sigaltstack))
92824593369SJonathan Lemon 		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
929146aad74SMarcel Moolenaar #endif
930146aad74SMarcel Moolenaar 
931206a5d3aSIan Dowse 	if (uap->uss != NULL) {
9325002a60fSMarcel Moolenaar 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
933146aad74SMarcel Moolenaar 		if (error)
934146aad74SMarcel Moolenaar 			return (error);
935146aad74SMarcel Moolenaar 
936206a5d3aSIan Dowse 		ss.ss_sp = lss.ss_sp;
937206a5d3aSIan Dowse 		ss.ss_size = lss.ss_size;
938206a5d3aSIan Dowse 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
939931a7258SAndrew Gallatin 	}
940ef36ad69SJohn Baldwin 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
941ef36ad69SJohn Baldwin 	    (uap->uoss != NULL) ? &oss : NULL);
942206a5d3aSIan Dowse 	if (!error && uap->uoss != NULL) {
943206a5d3aSIan Dowse 		lss.ss_sp = oss.ss_sp;
944206a5d3aSIan Dowse 		lss.ss_size = oss.ss_size;
945206a5d3aSIan Dowse 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
9465002a60fSMarcel Moolenaar 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
947146aad74SMarcel Moolenaar 	}
948146aad74SMarcel Moolenaar 
949146aad74SMarcel Moolenaar 	return (error);
950146aad74SMarcel Moolenaar }
9513ad9c842SMaxim Sobolev 
9523ad9c842SMaxim Sobolev int
9533ad9c842SMaxim Sobolev linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
9543ad9c842SMaxim Sobolev {
9553ad9c842SMaxim Sobolev 	struct ftruncate_args sa;
9563ad9c842SMaxim Sobolev 
9573ad9c842SMaxim Sobolev #ifdef DEBUG
9583ad9c842SMaxim Sobolev 	if (ldebug(ftruncate64))
959b45bbfc3SBruce Evans 		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
960b45bbfc3SBruce Evans 		    (intmax_t)args->length);
9613ad9c842SMaxim Sobolev #endif
9623ad9c842SMaxim Sobolev 
9633ad9c842SMaxim Sobolev 	sa.fd = args->fd;
9643ad9c842SMaxim Sobolev 	sa.pad = 0;
9653ad9c842SMaxim Sobolev 	sa.length = args->length;
9663ad9c842SMaxim Sobolev 	return ftruncate(td, &sa);
9673ad9c842SMaxim Sobolev }
9681bc85c0dSDoug Rabson 
9691bc85c0dSDoug Rabson int
9701bc85c0dSDoug Rabson linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
9711bc85c0dSDoug Rabson {
9729b44bfc5SAlexander Leidinger 	struct l_user_desc info;
9739b44bfc5SAlexander Leidinger 	int error;
9749b44bfc5SAlexander Leidinger 	int idx;
9759b44bfc5SAlexander Leidinger 	int a[2];
9769b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
9779b44bfc5SAlexander Leidinger 
9789b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
9799b44bfc5SAlexander Leidinger 	if (error)
9809b44bfc5SAlexander Leidinger 		return (error);
9819b44bfc5SAlexander Leidinger 
9829b44bfc5SAlexander Leidinger #ifdef DEBUG
9839b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
9849b44bfc5SAlexander Leidinger 	   	printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
9859b44bfc5SAlexander Leidinger 		      info.entry_number,
9869b44bfc5SAlexander Leidinger       		      info.base_addr,
9879b44bfc5SAlexander Leidinger       		      info.limit,
9889b44bfc5SAlexander Leidinger       		      info.seg_32bit,
9899b44bfc5SAlexander Leidinger 		      info.contents,
9909b44bfc5SAlexander Leidinger       		      info.read_exec_only,
9919b44bfc5SAlexander Leidinger       		      info.limit_in_pages,
9929b44bfc5SAlexander Leidinger       		      info.seg_not_present,
9939b44bfc5SAlexander Leidinger       		      info.useable);
9949b44bfc5SAlexander Leidinger #endif
9959b44bfc5SAlexander Leidinger 
9969b44bfc5SAlexander Leidinger 	idx = info.entry_number;
9979b44bfc5SAlexander Leidinger 	/* Semantics of linux version: every thread in the system has array
9989b44bfc5SAlexander Leidinger 	 * of 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
9999b44bfc5SAlexander Leidinger 	 * syscall loads one of the selected tls decriptors with a value
10009b44bfc5SAlexander Leidinger 	 * and also loads GDT descriptors 6, 7 and 8 with the content of the per-thread
10019b44bfc5SAlexander Leidinger 	 * descriptors.
10029b44bfc5SAlexander Leidinger 	 *
10039b44bfc5SAlexander Leidinger 	 * Semantics of fbsd version: I think we can ignore that linux has 3 per-thread
10049b44bfc5SAlexander Leidinger 	 * descriptors and use just the 1st one. The tls_array[] is used only in
10059b44bfc5SAlexander Leidinger 	 * set/get-thread_area() syscalls and for loading the GDT descriptors. In fbsd
10069b44bfc5SAlexander Leidinger 	 * we use just one GDT descriptor for TLS so we will load just one.
10079b44bfc5SAlexander Leidinger 	 * XXX: this doesnt work when user-space process tries to use more then 1 TLS segment
10089b44bfc5SAlexander Leidinger 	 * comment in the linux sources says wine might do that.
10091bc85c0dSDoug Rabson 	 */
10109b44bfc5SAlexander Leidinger 
10119b44bfc5SAlexander Leidinger 	/* we support just GLIBC TLS now
10129b44bfc5SAlexander Leidinger 	 * we should let 3 proceed as well because we use this segment so
10139b44bfc5SAlexander Leidinger 	 * if code does two subsequent calls it should succeed
10149b44bfc5SAlexander Leidinger 	 */
10159b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != -1 && idx != 3)
10169b44bfc5SAlexander Leidinger 		return (EINVAL);
10179b44bfc5SAlexander Leidinger 
10189b44bfc5SAlexander Leidinger 	/* we have to copy out the GDT entry we use
10199b44bfc5SAlexander Leidinger 	 * FreeBSD uses GDT entry #3 for storing %gs so load that
10209b44bfc5SAlexander Leidinger 	 * XXX: what if userspace program doesnt check this value and tries
10219b44bfc5SAlexander Leidinger 	 * to use 6, 7 or 8?
10229b44bfc5SAlexander Leidinger 	 */
10239b44bfc5SAlexander Leidinger 	idx = info.entry_number = 3;
10249b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
10259b44bfc5SAlexander Leidinger 	if (error)
10269b44bfc5SAlexander Leidinger 		return (error);
10279b44bfc5SAlexander Leidinger 
10289b44bfc5SAlexander Leidinger 	if (LDT_empty(&info)) {
10299b44bfc5SAlexander Leidinger 		a[0] = 0;
10309b44bfc5SAlexander Leidinger 		a[1] = 0;
10319b44bfc5SAlexander Leidinger 	} else {
10329b44bfc5SAlexander Leidinger 		a[0] = LDT_entry_a(&info);
10339b44bfc5SAlexander Leidinger 		a[1] = LDT_entry_b(&info);
10341bc85c0dSDoug Rabson 	}
10351bc85c0dSDoug Rabson 
10369b44bfc5SAlexander Leidinger 	memcpy(&sd, &a, sizeof(a));
10379b44bfc5SAlexander Leidinger #ifdef DEBUG
10389b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
10399b44bfc5SAlexander 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,
10409b44bfc5SAlexander Leidinger 			sd.sd_hibase,
10419b44bfc5SAlexander Leidinger 			sd.sd_lolimit,
10429b44bfc5SAlexander Leidinger 			sd.sd_hilimit,
10439b44bfc5SAlexander Leidinger 			sd.sd_type,
10449b44bfc5SAlexander Leidinger 			sd.sd_dpl,
10459b44bfc5SAlexander Leidinger 			sd.sd_p,
10469b44bfc5SAlexander Leidinger 			sd.sd_xx,
10479b44bfc5SAlexander Leidinger 			sd.sd_def32,
10489b44bfc5SAlexander Leidinger 			sd.sd_gran);
10499b44bfc5SAlexander Leidinger #endif
10501bc85c0dSDoug Rabson 
10519b44bfc5SAlexander Leidinger 	/* this is taken from i386 version of cpu_set_user_tls() */
10529b44bfc5SAlexander Leidinger 	critical_enter();
10539b44bfc5SAlexander Leidinger 	/* set %gs */
10549b44bfc5SAlexander Leidinger 	td->td_pcb->pcb_gsd = sd;
10559b44bfc5SAlexander Leidinger 	PCPU_GET(fsgs_gdt)[1] = sd;
10569b44bfc5SAlexander Leidinger 	load_gs(GSEL(GUGS_SEL, SEL_UPL));
10579b44bfc5SAlexander Leidinger 	critical_exit();
10589b44bfc5SAlexander Leidinger 
10591bc85c0dSDoug Rabson 	return (0);
10601bc85c0dSDoug Rabson }
10611bc85c0dSDoug Rabson 
10621bc85c0dSDoug Rabson int
10639b44bfc5SAlexander Leidinger linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
10641bc85c0dSDoug Rabson {
10651bc85c0dSDoug Rabson 
10669b44bfc5SAlexander Leidinger 	struct l_user_desc info;
10679b44bfc5SAlexander Leidinger 	int error;
10689b44bfc5SAlexander Leidinger 	int idx;
10699b44bfc5SAlexander Leidinger 	struct l_desc_struct desc;
10709b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
10719b44bfc5SAlexander Leidinger 
10729b44bfc5SAlexander Leidinger #ifdef DEBUG
10739b44bfc5SAlexander Leidinger 	if (ldebug(get_thread_area))
10749b44bfc5SAlexander Leidinger 		printf(ARGS(get_thread_area, "%p"), args->desc);
10759b44bfc5SAlexander Leidinger #endif
10769b44bfc5SAlexander Leidinger 
10779b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
10789b44bfc5SAlexander Leidinger 	if (error)
10799b44bfc5SAlexander Leidinger 		return (error);
10809b44bfc5SAlexander Leidinger 
10819b44bfc5SAlexander Leidinger 	idx = info.entry_number;
10829b44bfc5SAlexander Leidinger 	/* XXX: I am not sure if we want 3 to be allowed too. */
10839b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != 3)
10849b44bfc5SAlexander Leidinger 		return (EINVAL);
10859b44bfc5SAlexander Leidinger 
10869b44bfc5SAlexander Leidinger 	idx = 3;
10879b44bfc5SAlexander Leidinger 
10889b44bfc5SAlexander Leidinger 	memset(&info, 0, sizeof(info));
10899b44bfc5SAlexander Leidinger 
10909b44bfc5SAlexander Leidinger 	sd = PCPU_GET(fsgs_gdt)[1];
10919b44bfc5SAlexander Leidinger 
10929b44bfc5SAlexander Leidinger 	memcpy(&desc, &sd, sizeof(desc));
10939b44bfc5SAlexander Leidinger 
10949b44bfc5SAlexander Leidinger 	info.entry_number = idx;
10959b44bfc5SAlexander Leidinger 	info.base_addr = GET_BASE(&desc);
10969b44bfc5SAlexander Leidinger 	info.limit = GET_LIMIT(&desc);
10979b44bfc5SAlexander Leidinger 	info.seg_32bit = GET_32BIT(&desc);
10989b44bfc5SAlexander Leidinger 	info.contents = GET_CONTENTS(&desc);
10999b44bfc5SAlexander Leidinger 	info.read_exec_only = !GET_WRITABLE(&desc);
11009b44bfc5SAlexander Leidinger 	info.limit_in_pages = GET_LIMIT_PAGES(&desc);
11019b44bfc5SAlexander Leidinger 	info.seg_not_present = !GET_PRESENT(&desc);
11029b44bfc5SAlexander Leidinger 	info.useable = GET_USEABLE(&desc);
11039b44bfc5SAlexander Leidinger 
11049b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
11059b44bfc5SAlexander Leidinger 	if (error)
11069b44bfc5SAlexander Leidinger 	   	return (EFAULT);
11079b44bfc5SAlexander Leidinger 
11089b44bfc5SAlexander Leidinger 	return (0);
11099b44bfc5SAlexander Leidinger }
11109b44bfc5SAlexander Leidinger 
11119b44bfc5SAlexander Leidinger /* copied from kern/kern_time.c */
11129b44bfc5SAlexander Leidinger int
11139b44bfc5SAlexander Leidinger linux_timer_create(struct thread *td, struct linux_timer_create_args *args)
11149b44bfc5SAlexander Leidinger {
11159b44bfc5SAlexander Leidinger    	return ktimer_create(td, (struct ktimer_create_args *) args);
11169b44bfc5SAlexander Leidinger }
11179b44bfc5SAlexander Leidinger 
11189b44bfc5SAlexander Leidinger int
11199b44bfc5SAlexander Leidinger linux_timer_settime(struct thread *td, struct linux_timer_settime_args *args)
11209b44bfc5SAlexander Leidinger {
11219b44bfc5SAlexander Leidinger    	return ktimer_settime(td, (struct ktimer_settime_args *) args);
11229b44bfc5SAlexander Leidinger }
11239b44bfc5SAlexander Leidinger 
11249b44bfc5SAlexander Leidinger int
11259b44bfc5SAlexander Leidinger linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *args)
11269b44bfc5SAlexander Leidinger {
11279b44bfc5SAlexander Leidinger    	return ktimer_gettime(td, (struct ktimer_gettime_args *) args);
11289b44bfc5SAlexander Leidinger }
11299b44bfc5SAlexander Leidinger 
11309b44bfc5SAlexander Leidinger int
11319b44bfc5SAlexander Leidinger linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *args)
11329b44bfc5SAlexander Leidinger {
11339b44bfc5SAlexander Leidinger    	return ktimer_getoverrun(td, (struct ktimer_getoverrun_args *) args);
11349b44bfc5SAlexander Leidinger }
11359b44bfc5SAlexander Leidinger 
11369b44bfc5SAlexander Leidinger int
11379b44bfc5SAlexander Leidinger linux_timer_delete(struct thread *td, struct linux_timer_delete_args *args)
11389b44bfc5SAlexander Leidinger {
11399b44bfc5SAlexander Leidinger    	return ktimer_delete(td, (struct ktimer_delete_args *) args);
11409b44bfc5SAlexander Leidinger }
11419b44bfc5SAlexander Leidinger 
11429b44bfc5SAlexander Leidinger /* XXX: this wont work with module - convert it */
11439b44bfc5SAlexander Leidinger int
11449b44bfc5SAlexander Leidinger linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
11459b44bfc5SAlexander Leidinger {
11469b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
11479b44bfc5SAlexander Leidinger    	return kmq_open(td, (struct kmq_open_args *) args);
11489b44bfc5SAlexander Leidinger #else
11499b44bfc5SAlexander Leidinger 	return (ENOSYS);
11509b44bfc5SAlexander Leidinger #endif
11519b44bfc5SAlexander Leidinger }
11529b44bfc5SAlexander Leidinger 
11539b44bfc5SAlexander Leidinger int
11549b44bfc5SAlexander Leidinger linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
11559b44bfc5SAlexander Leidinger {
11569b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
11579b44bfc5SAlexander Leidinger    	return kmq_unlink(td, (struct kmq_unlink_args *) args);
11589b44bfc5SAlexander Leidinger #else
11599b44bfc5SAlexander Leidinger 	return (ENOSYS);
11609b44bfc5SAlexander Leidinger #endif
11619b44bfc5SAlexander Leidinger }
11629b44bfc5SAlexander Leidinger 
11639b44bfc5SAlexander Leidinger int
11649b44bfc5SAlexander Leidinger linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
11659b44bfc5SAlexander Leidinger {
11669b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
11679b44bfc5SAlexander Leidinger    	return kmq_timedsend(td, (struct kmq_timedsend_args *) args);
11689b44bfc5SAlexander Leidinger #else
11699b44bfc5SAlexander Leidinger 	return (ENOSYS);
11709b44bfc5SAlexander Leidinger #endif
11719b44bfc5SAlexander Leidinger }
11729b44bfc5SAlexander Leidinger 
11739b44bfc5SAlexander Leidinger int
11749b44bfc5SAlexander Leidinger linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
11759b44bfc5SAlexander Leidinger {
11769b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
11779b44bfc5SAlexander Leidinger    	return kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
11789b44bfc5SAlexander Leidinger #else
11799b44bfc5SAlexander Leidinger 	return (ENOSYS);
11809b44bfc5SAlexander Leidinger #endif
11819b44bfc5SAlexander Leidinger }
11829b44bfc5SAlexander Leidinger 
11839b44bfc5SAlexander Leidinger int
11849b44bfc5SAlexander Leidinger linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
11859b44bfc5SAlexander Leidinger {
11869b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
11879b44bfc5SAlexander Leidinger 	return kmq_notify(td, (struct kmq_notify_args *) args);
11889b44bfc5SAlexander Leidinger #else
11899b44bfc5SAlexander Leidinger 	return (ENOSYS);
11909b44bfc5SAlexander Leidinger #endif
11919b44bfc5SAlexander Leidinger }
11929b44bfc5SAlexander Leidinger 
11939b44bfc5SAlexander Leidinger int
11949b44bfc5SAlexander Leidinger linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
11959b44bfc5SAlexander Leidinger {
11969b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
11979b44bfc5SAlexander Leidinger    	return kmq_setattr(td, (struct kmq_setattr_args *) args);
11989b44bfc5SAlexander Leidinger #else
11999b44bfc5SAlexander Leidinger 	return (ENOSYS);
12009b44bfc5SAlexander Leidinger #endif
12011bc85c0dSDoug Rabson }
12021bc85c0dSDoug Rabson 
1203