xref: /freebsd/sys/i386/linux/linux_machdep.c (revision 7dabf89bcf0667b4392cbc40528caeba82b21ddd)
1146aad74SMarcel Moolenaar /*-
20ba1b365SEd Maste  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
383ef78beSPedro F. Giffuni  *
4146aad74SMarcel Moolenaar  * Copyright (c) 2000 Marcel Moolenaar
5146aad74SMarcel Moolenaar  * All rights reserved.
6146aad74SMarcel Moolenaar  *
7146aad74SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
8146aad74SMarcel Moolenaar  * modification, are permitted provided that the following conditions
9146aad74SMarcel Moolenaar  * are met:
10146aad74SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
110ba1b365SEd Maste  *    notice, this list of conditions and the following disclaimer.
12146aad74SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
13146aad74SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
14146aad74SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
15146aad74SMarcel Moolenaar  *
160ba1b365SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
170ba1b365SEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180ba1b365SEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190ba1b365SEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
200ba1b365SEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210ba1b365SEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220ba1b365SEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230ba1b365SEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240ba1b365SEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250ba1b365SEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260ba1b365SEd Maste  * 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>
334a144410SRobert Watson #include <sys/capsicum.h>
34a312f6a3SAlexander Leidinger #include <sys/fcntl.h>
35c0aa0e2cSEd Maste #include <sys/file.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>
41acd3428bSRobert Watson #include <sys/priv.h>
42fb919e4dSMark Murray #include <sys/proc.h>
439b44bfc5SAlexander Leidinger #include <sys/queue.h>
44242fae60SAndrew Gallatin #include <sys/resource.h>
45242fae60SAndrew Gallatin #include <sys/resourcevar.h>
46c0aa0e2cSEd Maste #include <sys/sched.h>
471bc85c0dSDoug Rabson #include <sys/signalvar.h>
48206a5d3aSIan Dowse #include <sys/syscallsubr.h>
49fb919e4dSMark Murray #include <sys/sysproto.h>
50c0aa0e2cSEd Maste #include <sys/systm.h>
51c0aa0e2cSEd Maste #include <sys/sx.h>
52fb919e4dSMark Murray #include <sys/unistd.h>
539b44bfc5SAlexander Leidinger #include <sys/wait.h>
54146aad74SMarcel Moolenaar 
55146aad74SMarcel Moolenaar #include <machine/frame.h>
56146aad74SMarcel Moolenaar #include <machine/psl.h>
57146aad74SMarcel Moolenaar #include <machine/segments.h>
58146aad74SMarcel Moolenaar #include <machine/sysarch.h>
59146aad74SMarcel Moolenaar 
60242fae60SAndrew Gallatin #include <vm/pmap.h>
61c0aa0e2cSEd Maste #include <vm/vm.h>
62242fae60SAndrew Gallatin #include <vm/vm_map.h>
63242fae60SAndrew Gallatin 
64146aad74SMarcel Moolenaar #include <i386/linux/linux.h>
65ebea8660SMarcel Moolenaar #include <i386/linux/linux_proto.h>
66c0aa0e2cSEd Maste #include <compat/linux/linux_emul.h>
67146aad74SMarcel Moolenaar #include <compat/linux/linux_ipc.h>
68adc7ece0SDmitry Chagin #include <compat/linux/linux_misc.h>
6997d06da6SDmitry Chagin #include <compat/linux/linux_mmap.h>
70146aad74SMarcel Moolenaar #include <compat/linux/linux_signal.h>
71146aad74SMarcel Moolenaar #include <compat/linux/linux_util.h>
729b44bfc5SAlexander Leidinger 
739b44bfc5SAlexander Leidinger #include <i386/include/pcb.h>			/* needed for pcb definition in linux_set_thread_area */
749b44bfc5SAlexander Leidinger 
759b44bfc5SAlexander Leidinger #include "opt_posix.h"
769b44bfc5SAlexander Leidinger 
779b44bfc5SAlexander Leidinger extern struct sysentvec elf32_freebsd_sysvec;	/* defined in i386/i386/elf_machdep.c */
78146aad74SMarcel Moolenaar 
795002a60fSMarcel Moolenaar struct l_descriptor {
805002a60fSMarcel Moolenaar 	l_uint		entry_number;
815002a60fSMarcel Moolenaar 	l_ulong		base_addr;
825002a60fSMarcel Moolenaar 	l_uint		limit;
835002a60fSMarcel Moolenaar 	l_uint		seg_32bit:1;
845002a60fSMarcel Moolenaar 	l_uint		contents:2;
855002a60fSMarcel Moolenaar 	l_uint		read_exec_only:1;
865002a60fSMarcel Moolenaar 	l_uint		limit_in_pages:1;
875002a60fSMarcel Moolenaar 	l_uint		seg_not_present:1;
885002a60fSMarcel Moolenaar 	l_uint		useable:1;
89146aad74SMarcel Moolenaar };
90146aad74SMarcel Moolenaar 
915002a60fSMarcel Moolenaar struct l_old_select_argv {
925002a60fSMarcel Moolenaar 	l_int		nfds;
935002a60fSMarcel Moolenaar 	l_fd_set	*readfds;
945002a60fSMarcel Moolenaar 	l_fd_set	*writefds;
955002a60fSMarcel Moolenaar 	l_fd_set	*exceptfds;
965002a60fSMarcel Moolenaar 	struct l_timeval	*timeout;
97146aad74SMarcel Moolenaar };
98146aad74SMarcel Moolenaar 
99931a7258SAndrew Gallatin 
100931a7258SAndrew Gallatin int
101b40ce416SJulian Elischer linux_execve(struct thread *td, struct linux_execve_args *args)
102146aad74SMarcel Moolenaar {
103610ecfe0SMaxim Sobolev 	struct image_args eargs;
1047b445033SKonstantin Belousov 	char *newpath;
1057b445033SKonstantin Belousov 	int error;
106146aad74SMarcel Moolenaar 
107d9e97471SJohn Baldwin 	LCONVPATHEXIST(td, args->path, &newpath);
108146aad74SMarcel Moolenaar 
109146aad74SMarcel Moolenaar #ifdef DEBUG
11024593369SJonathan Lemon 	if (ldebug(execve))
111610ecfe0SMaxim Sobolev 		printf(ARGS(execve, "%s"), newpath);
112146aad74SMarcel Moolenaar #endif
113146aad74SMarcel Moolenaar 
114610ecfe0SMaxim Sobolev 	error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
115610ecfe0SMaxim Sobolev 	    args->argp, args->envp);
116610ecfe0SMaxim Sobolev 	free(newpath, M_TEMP);
117610ecfe0SMaxim Sobolev 	if (error == 0)
11881338031SDmitry Chagin 		error = linux_common_execve(td, &eargs);
119610ecfe0SMaxim Sobolev 	return (error);
120146aad74SMarcel Moolenaar }
121146aad74SMarcel Moolenaar 
1225002a60fSMarcel Moolenaar struct l_ipc_kludge {
1235002a60fSMarcel Moolenaar 	struct l_msgbuf *msgp;
1245002a60fSMarcel Moolenaar 	l_long msgtyp;
1255002a60fSMarcel Moolenaar };
1265002a60fSMarcel Moolenaar 
127146aad74SMarcel Moolenaar int
128b40ce416SJulian Elischer linux_ipc(struct thread *td, struct linux_ipc_args *args)
129146aad74SMarcel Moolenaar {
1305002a60fSMarcel Moolenaar 
1315002a60fSMarcel Moolenaar 	switch (args->what & 0xFFFF) {
1325002a60fSMarcel Moolenaar 	case LINUX_SEMOP: {
1335002a60fSMarcel Moolenaar 		struct linux_semop_args a;
1345002a60fSMarcel Moolenaar 
1355002a60fSMarcel Moolenaar 		a.semid = args->arg1;
136*7dabf89bSDmitry Chagin 		a.tsops = PTRIN(args->ptr);
1375002a60fSMarcel Moolenaar 		a.nsops = args->arg2;
138b40ce416SJulian Elischer 		return (linux_semop(td, &a));
1395002a60fSMarcel Moolenaar 	}
1405002a60fSMarcel Moolenaar 	case LINUX_SEMGET: {
1415002a60fSMarcel Moolenaar 		struct linux_semget_args a;
1425002a60fSMarcel Moolenaar 
1435002a60fSMarcel Moolenaar 		a.key = args->arg1;
1445002a60fSMarcel Moolenaar 		a.nsems = args->arg2;
1455002a60fSMarcel Moolenaar 		a.semflg = args->arg3;
146b40ce416SJulian Elischer 		return (linux_semget(td, &a));
1475002a60fSMarcel Moolenaar 	}
1485002a60fSMarcel Moolenaar 	case LINUX_SEMCTL: {
1495002a60fSMarcel Moolenaar 		struct linux_semctl_args a;
1505002a60fSMarcel Moolenaar 		int error;
1515002a60fSMarcel Moolenaar 
1525002a60fSMarcel Moolenaar 		a.semid = args->arg1;
1535002a60fSMarcel Moolenaar 		a.semnum = args->arg2;
1545002a60fSMarcel Moolenaar 		a.cmd = args->arg3;
155*7dabf89bSDmitry Chagin 		error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
1565002a60fSMarcel Moolenaar 		if (error)
1575002a60fSMarcel Moolenaar 			return (error);
158b40ce416SJulian Elischer 		return (linux_semctl(td, &a));
1595002a60fSMarcel Moolenaar 	}
1605002a60fSMarcel Moolenaar 	case LINUX_MSGSND: {
1615002a60fSMarcel Moolenaar 		struct linux_msgsnd_args a;
1625002a60fSMarcel Moolenaar 
1635002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
164*7dabf89bSDmitry Chagin 		a.msgp = PTRIN(args->ptr);
1655002a60fSMarcel Moolenaar 		a.msgsz = args->arg2;
1665002a60fSMarcel Moolenaar 		a.msgflg = args->arg3;
167b40ce416SJulian Elischer 		return (linux_msgsnd(td, &a));
1685002a60fSMarcel Moolenaar 	}
1695002a60fSMarcel Moolenaar 	case LINUX_MSGRCV: {
1705002a60fSMarcel Moolenaar 		struct linux_msgrcv_args a;
1715002a60fSMarcel Moolenaar 
1725002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
1735002a60fSMarcel Moolenaar 		a.msgsz = args->arg2;
1745002a60fSMarcel Moolenaar 		a.msgflg = args->arg3;
1755002a60fSMarcel Moolenaar 		if ((args->what >> 16) == 0) {
1765002a60fSMarcel Moolenaar 			struct l_ipc_kludge tmp;
1775002a60fSMarcel Moolenaar 			int error;
1785002a60fSMarcel Moolenaar 
179*7dabf89bSDmitry Chagin 			if (args->ptr == 0)
1805002a60fSMarcel Moolenaar 				return (EINVAL);
181*7dabf89bSDmitry Chagin 			error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
1825002a60fSMarcel Moolenaar 			if (error)
1835002a60fSMarcel Moolenaar 				return (error);
184*7dabf89bSDmitry Chagin 			a.msgp = PTRIN(tmp.msgp);
1855002a60fSMarcel Moolenaar 			a.msgtyp = tmp.msgtyp;
1865002a60fSMarcel Moolenaar 		} else {
187*7dabf89bSDmitry Chagin 			a.msgp = PTRIN(args->ptr);
1885002a60fSMarcel Moolenaar 			a.msgtyp = args->arg5;
1895002a60fSMarcel Moolenaar 		}
190b40ce416SJulian Elischer 		return (linux_msgrcv(td, &a));
1915002a60fSMarcel Moolenaar 	}
1925002a60fSMarcel Moolenaar 	case LINUX_MSGGET: {
1935002a60fSMarcel Moolenaar 		struct linux_msgget_args a;
1945002a60fSMarcel Moolenaar 
1955002a60fSMarcel Moolenaar 		a.key = args->arg1;
1965002a60fSMarcel Moolenaar 		a.msgflg = args->arg2;
197b40ce416SJulian Elischer 		return (linux_msgget(td, &a));
1985002a60fSMarcel Moolenaar 	}
1995002a60fSMarcel Moolenaar 	case LINUX_MSGCTL: {
2005002a60fSMarcel Moolenaar 		struct linux_msgctl_args a;
2015002a60fSMarcel Moolenaar 
2025002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
2035002a60fSMarcel Moolenaar 		a.cmd = args->arg2;
204*7dabf89bSDmitry Chagin 		a.buf = PTRIN(args->ptr);
205b40ce416SJulian Elischer 		return (linux_msgctl(td, &a));
2065002a60fSMarcel Moolenaar 	}
2075002a60fSMarcel Moolenaar 	case LINUX_SHMAT: {
2085002a60fSMarcel Moolenaar 		struct linux_shmat_args a;
209*7dabf89bSDmitry Chagin 		l_uintptr_t addr;
210*7dabf89bSDmitry Chagin 		int error;
2115002a60fSMarcel Moolenaar 
2125002a60fSMarcel Moolenaar 		a.shmid = args->arg1;
213*7dabf89bSDmitry Chagin 		a.shmaddr = PTRIN(args->ptr);
2145002a60fSMarcel Moolenaar 		a.shmflg = args->arg2;
215*7dabf89bSDmitry Chagin 		error = linux_shmat(td, &a);
216*7dabf89bSDmitry Chagin 		if (error != 0)
217*7dabf89bSDmitry Chagin 			return (error);
218*7dabf89bSDmitry Chagin 		addr = td->td_retval[0];
219*7dabf89bSDmitry Chagin 		error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
220*7dabf89bSDmitry Chagin 		td->td_retval[0] = 0;
221*7dabf89bSDmitry Chagin 		return (error);
2225002a60fSMarcel Moolenaar 	}
2235002a60fSMarcel Moolenaar 	case LINUX_SHMDT: {
2245002a60fSMarcel Moolenaar 		struct linux_shmdt_args a;
2255002a60fSMarcel Moolenaar 
226*7dabf89bSDmitry Chagin 		a.shmaddr = PTRIN(args->ptr);
227b40ce416SJulian Elischer 		return (linux_shmdt(td, &a));
2285002a60fSMarcel Moolenaar 	}
2295002a60fSMarcel Moolenaar 	case LINUX_SHMGET: {
2305002a60fSMarcel Moolenaar 		struct linux_shmget_args a;
2315002a60fSMarcel Moolenaar 
2325002a60fSMarcel Moolenaar 		a.key = args->arg1;
2335002a60fSMarcel Moolenaar 		a.size = args->arg2;
2345002a60fSMarcel Moolenaar 		a.shmflg = args->arg3;
235b40ce416SJulian Elischer 		return (linux_shmget(td, &a));
2365002a60fSMarcel Moolenaar 	}
2375002a60fSMarcel Moolenaar 	case LINUX_SHMCTL: {
2385002a60fSMarcel Moolenaar 		struct linux_shmctl_args a;
2395002a60fSMarcel Moolenaar 
2405002a60fSMarcel Moolenaar 		a.shmid = args->arg1;
2415002a60fSMarcel Moolenaar 		a.cmd = args->arg2;
242*7dabf89bSDmitry Chagin 		a.buf = PTRIN(args->ptr);
243b40ce416SJulian Elischer 		return (linux_shmctl(td, &a));
2445002a60fSMarcel Moolenaar 	}
2455002a60fSMarcel Moolenaar 	default:
2465002a60fSMarcel Moolenaar 		break;
247146aad74SMarcel Moolenaar 	}
248146aad74SMarcel Moolenaar 
2495002a60fSMarcel Moolenaar 	return (EINVAL);
250146aad74SMarcel Moolenaar }
251146aad74SMarcel Moolenaar 
252146aad74SMarcel Moolenaar int
253b40ce416SJulian Elischer linux_old_select(struct thread *td, struct linux_old_select_args *args)
254146aad74SMarcel Moolenaar {
2555002a60fSMarcel Moolenaar 	struct l_old_select_argv linux_args;
2565002a60fSMarcel Moolenaar 	struct linux_select_args newsel;
257146aad74SMarcel Moolenaar 	int error;
258146aad74SMarcel Moolenaar 
2595002a60fSMarcel Moolenaar #ifdef DEBUG
2605002a60fSMarcel Moolenaar 	if (ldebug(old_select))
2616aea6777SPeter Wemm 		printf(ARGS(old_select, "%p"), args->ptr);
262146aad74SMarcel Moolenaar #endif
263146aad74SMarcel Moolenaar 
2644b7ef73dSDag-Erling Smørgrav 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
265146aad74SMarcel Moolenaar 	if (error)
266146aad74SMarcel Moolenaar 		return (error);
267146aad74SMarcel Moolenaar 
268146aad74SMarcel Moolenaar 	newsel.nfds = linux_args.nfds;
269146aad74SMarcel Moolenaar 	newsel.readfds = linux_args.readfds;
270146aad74SMarcel Moolenaar 	newsel.writefds = linux_args.writefds;
271146aad74SMarcel Moolenaar 	newsel.exceptfds = linux_args.exceptfds;
272146aad74SMarcel Moolenaar 	newsel.timeout = linux_args.timeout;
273b40ce416SJulian Elischer 	return (linux_select(td, &newsel));
274146aad74SMarcel Moolenaar }
275146aad74SMarcel Moolenaar 
276146aad74SMarcel Moolenaar int
2772c7660baSDmitry Chagin linux_set_cloned_tls(struct thread *td, void *desc)
2782c7660baSDmitry Chagin {
2792c7660baSDmitry Chagin 	struct segment_descriptor sd;
2802c7660baSDmitry Chagin 	struct l_user_desc info;
2812c7660baSDmitry Chagin 	int idx, error;
2822c7660baSDmitry Chagin 	int a[2];
2832c7660baSDmitry Chagin 
2842c7660baSDmitry Chagin 	error = copyin(desc, &info, sizeof(struct l_user_desc));
2852c7660baSDmitry Chagin 	if (error) {
2862c7660baSDmitry Chagin 		printf(LMSG("copyin failed!"));
2872c7660baSDmitry Chagin 	} else {
2882c7660baSDmitry Chagin 		idx = info.entry_number;
2892c7660baSDmitry Chagin 
2902c7660baSDmitry Chagin 		/*
2912c7660baSDmitry Chagin 		 * looks like we're getting the idx we returned
2922c7660baSDmitry Chagin 		 * in the set_thread_area() syscall
2932c7660baSDmitry Chagin 		 */
2942c7660baSDmitry Chagin 		if (idx != 6 && idx != 3) {
2952c7660baSDmitry Chagin 			printf(LMSG("resetting idx!"));
2962c7660baSDmitry Chagin 			idx = 3;
2972c7660baSDmitry Chagin 		}
2982c7660baSDmitry Chagin 
2992c7660baSDmitry Chagin 		/* this doesnt happen in practice */
3002c7660baSDmitry Chagin 		if (idx == 6) {
3012c7660baSDmitry Chagin 			/* we might copy out the entry_number as 3 */
3022c7660baSDmitry Chagin 			info.entry_number = 3;
3032c7660baSDmitry Chagin 			error = copyout(&info, desc, sizeof(struct l_user_desc));
3042c7660baSDmitry Chagin 			if (error)
3052c7660baSDmitry Chagin 				printf(LMSG("copyout failed!"));
3062c7660baSDmitry Chagin 		}
3072c7660baSDmitry Chagin 
3082c7660baSDmitry Chagin 		a[0] = LINUX_LDT_entry_a(&info);
3092c7660baSDmitry Chagin 		a[1] = LINUX_LDT_entry_b(&info);
3102c7660baSDmitry Chagin 
3112c7660baSDmitry Chagin 		memcpy(&sd, &a, sizeof(a));
3122c7660baSDmitry Chagin #ifdef DEBUG
3132c7660baSDmitry Chagin 		if (ldebug(clone))
3142c7660baSDmitry Chagin 			printf("Segment created in clone with "
3152c7660baSDmitry Chagin 			"CLONE_SETTLS: lobase: %x, hibase: %x, "
3162c7660baSDmitry Chagin 			"lolimit: %x, hilimit: %x, type: %i, "
3172c7660baSDmitry Chagin 			"dpl: %i, p: %i, xx: %i, def32: %i, "
3182c7660baSDmitry Chagin 			"gran: %i\n", sd.sd_lobase, sd.sd_hibase,
3192c7660baSDmitry Chagin 			sd.sd_lolimit, sd.sd_hilimit, sd.sd_type,
3202c7660baSDmitry Chagin 			sd.sd_dpl, sd.sd_p, sd.sd_xx,
3212c7660baSDmitry Chagin 			sd.sd_def32, sd.sd_gran);
3222c7660baSDmitry Chagin #endif
3232c7660baSDmitry Chagin 
3242c7660baSDmitry Chagin 		/* set %gs */
3252c7660baSDmitry Chagin 		td->td_pcb->pcb_gsd = sd;
3262c7660baSDmitry Chagin 		td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
3272c7660baSDmitry Chagin 	}
3282c7660baSDmitry Chagin 
3292c7660baSDmitry Chagin 	return (error);
3302c7660baSDmitry Chagin }
3312c7660baSDmitry Chagin 
332146aad74SMarcel Moolenaar int
333c8d6845eSDmitry Chagin linux_set_upcall_kse(struct thread *td, register_t stack)
334c8d6845eSDmitry Chagin {
335c8d6845eSDmitry Chagin 
33681338031SDmitry Chagin 	if (stack)
337c8d6845eSDmitry Chagin 		td->td_frame->tf_esp = stack;
338c8d6845eSDmitry Chagin 
33981338031SDmitry Chagin 	/*
34081338031SDmitry Chagin 	 * The newly created Linux thread returns
34181338031SDmitry Chagin 	 * to the user space by the same path that a parent do.
34281338031SDmitry Chagin 	 */
34381338031SDmitry Chagin 	td->td_frame->tf_eax = 0;
344c8d6845eSDmitry Chagin 	return (0);
345c8d6845eSDmitry Chagin }
346c8d6845eSDmitry Chagin 
3473ad9c842SMaxim Sobolev int
3483ad9c842SMaxim Sobolev linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
3493ad9c842SMaxim Sobolev {
3503ad9c842SMaxim Sobolev 
3513ad9c842SMaxim Sobolev #ifdef DEBUG
3523ad9c842SMaxim Sobolev 	if (ldebug(mmap2))
3533ad9c842SMaxim Sobolev 		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
3543ad9c842SMaxim Sobolev 		    (void *)args->addr, args->len, args->prot,
3553ad9c842SMaxim Sobolev 		    args->flags, args->fd, args->pgoff);
3563ad9c842SMaxim Sobolev #endif
3573ad9c842SMaxim Sobolev 
358f12c0348SJohn Baldwin 	return (linux_mmap_common(td, args->addr, args->len, args->prot,
359f12c0348SJohn Baldwin 		args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
360f12c0348SJohn Baldwin 		PAGE_SIZE));
3613ad9c842SMaxim Sobolev }
3623ad9c842SMaxim Sobolev 
363146aad74SMarcel Moolenaar int
364b40ce416SJulian Elischer linux_mmap(struct thread *td, struct linux_mmap_args *args)
365146aad74SMarcel Moolenaar {
3663ad9c842SMaxim Sobolev 	int error;
3673ad9c842SMaxim Sobolev 	struct l_mmap_argv linux_args;
3683ad9c842SMaxim Sobolev 
3694b7ef73dSDag-Erling Smørgrav 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
3703ad9c842SMaxim Sobolev 	if (error)
3713ad9c842SMaxim Sobolev 		return (error);
3723ad9c842SMaxim Sobolev 
3733ad9c842SMaxim Sobolev #ifdef DEBUG
3743ad9c842SMaxim Sobolev 	if (ldebug(mmap))
3753ad9c842SMaxim Sobolev 		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
376b45bbfc3SBruce Evans 		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
37710931a46SJung-uk Kim 		    linux_args.flags, linux_args.fd, linux_args.pgoff);
3783ad9c842SMaxim Sobolev #endif
3793ad9c842SMaxim Sobolev 
380f12c0348SJohn Baldwin 	return (linux_mmap_common(td, linux_args.addr, linux_args.len,
381f12c0348SJohn Baldwin 	    linux_args.prot, linux_args.flags, linux_args.fd,
382f12c0348SJohn Baldwin 	    (uint32_t)linux_args.pgoff));
3833ad9c842SMaxim Sobolev }
3843ad9c842SMaxim Sobolev 
385146aad74SMarcel Moolenaar int
38610931a46SJung-uk Kim linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
38710931a46SJung-uk Kim {
38810931a46SJung-uk Kim 
38997d06da6SDmitry Chagin 	return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
39010931a46SJung-uk Kim }
39110931a46SJung-uk Kim 
39210931a46SJung-uk Kim int
393b40ce416SJulian Elischer linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
394146aad74SMarcel Moolenaar {
39584569dffSMaxim Sobolev 	int error;
39684569dffSMaxim Sobolev 	struct i386_ioperm_args iia;
397146aad74SMarcel Moolenaar 
39884569dffSMaxim Sobolev 	iia.start = args->start;
39984569dffSMaxim Sobolev 	iia.length = args->length;
40084569dffSMaxim Sobolev 	iia.enable = args->enable;
40184569dffSMaxim Sobolev 	error = i386_set_ioperm(td, &iia);
40284569dffSMaxim Sobolev 	return (error);
403146aad74SMarcel Moolenaar }
404146aad74SMarcel Moolenaar 
405146aad74SMarcel Moolenaar int
406b40ce416SJulian Elischer linux_iopl(struct thread *td, struct linux_iopl_args *args)
407146aad74SMarcel Moolenaar {
408146aad74SMarcel Moolenaar 	int error;
409146aad74SMarcel Moolenaar 
410146aad74SMarcel Moolenaar 	if (args->level < 0 || args->level > 3)
411146aad74SMarcel Moolenaar 		return (EINVAL);
412acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_IO)) != 0)
413146aad74SMarcel Moolenaar 		return (error);
414a854ed98SJohn Baldwin 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
41541c42188SRobert Watson 		return (error);
416b40ce416SJulian Elischer 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
417146aad74SMarcel Moolenaar 	    (args->level * (PSL_IOPL / 3));
418146aad74SMarcel Moolenaar 	return (0);
419146aad74SMarcel Moolenaar }
420146aad74SMarcel Moolenaar 
421146aad74SMarcel Moolenaar int
422b07cd97eSMark Murray linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
423146aad74SMarcel Moolenaar {
424146aad74SMarcel Moolenaar 	int error;
42584569dffSMaxim Sobolev 	struct i386_ldt_args ldt;
4265002a60fSMarcel Moolenaar 	struct l_descriptor ld;
42784569dffSMaxim Sobolev 	union descriptor desc;
4286259969dSKonstantin Belousov 	int size, written;
429146aad74SMarcel Moolenaar 
430146aad74SMarcel Moolenaar 	switch (uap->func) {
431146aad74SMarcel Moolenaar 	case 0x00: /* read_ldt */
43284569dffSMaxim Sobolev 		ldt.start = 0;
43384569dffSMaxim Sobolev 		ldt.descs = uap->ptr;
43484569dffSMaxim Sobolev 		ldt.num = uap->bytecount / sizeof(union descriptor);
43584569dffSMaxim Sobolev 		error = i386_get_ldt(td, &ldt);
436b40ce416SJulian Elischer 		td->td_retval[0] *= sizeof(union descriptor);
437146aad74SMarcel Moolenaar 		break;
4386259969dSKonstantin Belousov 	case 0x02: /* read_default_ldt = 0 */
4396259969dSKonstantin Belousov 		size = 5*sizeof(struct l_desc_struct);
4406259969dSKonstantin Belousov 		if (size > uap->bytecount)
4416259969dSKonstantin Belousov 			size = uap->bytecount;
4426259969dSKonstantin Belousov 		for (written = error = 0; written < size && error == 0; written++)
4436259969dSKonstantin Belousov 			error = subyte((char *)uap->ptr + written, 0);
4446259969dSKonstantin Belousov 		td->td_retval[0] = written;
4456259969dSKonstantin Belousov 		break;
446146aad74SMarcel Moolenaar 	case 0x01: /* write_ldt */
447146aad74SMarcel Moolenaar 	case 0x11: /* write_ldt */
448146aad74SMarcel Moolenaar 		if (uap->bytecount != sizeof(ld))
449146aad74SMarcel Moolenaar 			return (EINVAL);
450146aad74SMarcel Moolenaar 
451146aad74SMarcel Moolenaar 		error = copyin(uap->ptr, &ld, sizeof(ld));
452146aad74SMarcel Moolenaar 		if (error)
453146aad74SMarcel Moolenaar 			return (error);
454146aad74SMarcel Moolenaar 
45584569dffSMaxim Sobolev 		ldt.start = ld.entry_number;
45684569dffSMaxim Sobolev 		ldt.descs = &desc;
45784569dffSMaxim Sobolev 		ldt.num = 1;
45884569dffSMaxim Sobolev 		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
45984569dffSMaxim Sobolev 		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
46084569dffSMaxim Sobolev 		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
46184569dffSMaxim Sobolev 		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
46284569dffSMaxim Sobolev 		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
463146aad74SMarcel Moolenaar 			(ld.contents << 2);
46484569dffSMaxim Sobolev 		desc.sd.sd_dpl = 3;
46584569dffSMaxim Sobolev 		desc.sd.sd_p = (ld.seg_not_present ^ 1);
46684569dffSMaxim Sobolev 		desc.sd.sd_xx = 0;
46784569dffSMaxim Sobolev 		desc.sd.sd_def32 = ld.seg_32bit;
46884569dffSMaxim Sobolev 		desc.sd.sd_gran = ld.limit_in_pages;
46984569dffSMaxim Sobolev 		error = i386_set_ldt(td, &ldt, &desc);
470146aad74SMarcel Moolenaar 		break;
471146aad74SMarcel Moolenaar 	default:
4727c020cbbSJohn Baldwin 		error = ENOSYS;
473146aad74SMarcel Moolenaar 		break;
474146aad74SMarcel Moolenaar 	}
475146aad74SMarcel Moolenaar 
476146aad74SMarcel Moolenaar 	if (error == EOPNOTSUPP) {
477146aad74SMarcel Moolenaar 		printf("linux: modify_ldt needs kernel option USER_LDT\n");
478146aad74SMarcel Moolenaar 		error = ENOSYS;
479146aad74SMarcel Moolenaar 	}
480146aad74SMarcel Moolenaar 
481146aad74SMarcel Moolenaar 	return (error);
482146aad74SMarcel Moolenaar }
483146aad74SMarcel Moolenaar 
484146aad74SMarcel Moolenaar int
485b40ce416SJulian Elischer linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
486146aad74SMarcel Moolenaar {
4875002a60fSMarcel Moolenaar 	l_osigaction_t osa;
4885002a60fSMarcel Moolenaar 	l_sigaction_t act, oact;
489146aad74SMarcel Moolenaar 	int error;
490146aad74SMarcel Moolenaar 
491146aad74SMarcel Moolenaar #ifdef DEBUG
49224593369SJonathan Lemon 	if (ldebug(sigaction))
49324593369SJonathan Lemon 		printf(ARGS(sigaction, "%d, %p, %p"),
494146aad74SMarcel Moolenaar 		    args->sig, (void *)args->nsa, (void *)args->osa);
495146aad74SMarcel Moolenaar #endif
496146aad74SMarcel Moolenaar 
497146aad74SMarcel Moolenaar 	if (args->nsa != NULL) {
4984b7ef73dSDag-Erling Smørgrav 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
499146aad74SMarcel Moolenaar 		if (error)
500146aad74SMarcel Moolenaar 			return (error);
501146aad74SMarcel Moolenaar 		act.lsa_handler = osa.lsa_handler;
502146aad74SMarcel Moolenaar 		act.lsa_flags = osa.lsa_flags;
503146aad74SMarcel Moolenaar 		act.lsa_restorer = osa.lsa_restorer;
504146aad74SMarcel Moolenaar 		LINUX_SIGEMPTYSET(act.lsa_mask);
5054ab7403bSDmitry Chagin 		act.lsa_mask.__mask = osa.lsa_mask;
506146aad74SMarcel Moolenaar 	}
507146aad74SMarcel Moolenaar 
508b40ce416SJulian Elischer 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
509146aad74SMarcel Moolenaar 	    args->osa ? &oact : NULL);
510146aad74SMarcel Moolenaar 
511146aad74SMarcel Moolenaar 	if (args->osa != NULL && !error) {
512146aad74SMarcel Moolenaar 		osa.lsa_handler = oact.lsa_handler;
513146aad74SMarcel Moolenaar 		osa.lsa_flags = oact.lsa_flags;
514146aad74SMarcel Moolenaar 		osa.lsa_restorer = oact.lsa_restorer;
5154ab7403bSDmitry Chagin 		osa.lsa_mask = oact.lsa_mask.__mask;
5164b7ef73dSDag-Erling Smørgrav 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
517146aad74SMarcel Moolenaar 	}
518146aad74SMarcel Moolenaar 
519146aad74SMarcel Moolenaar 	return (error);
520146aad74SMarcel Moolenaar }
521146aad74SMarcel Moolenaar 
522146aad74SMarcel Moolenaar /*
523146aad74SMarcel Moolenaar  * Linux has two extra args, restart and oldmask.  We dont use these,
524146aad74SMarcel Moolenaar  * but it seems that "restart" is actually a context pointer that
525146aad74SMarcel Moolenaar  * enables the signal to happen with a different register set.
526146aad74SMarcel Moolenaar  */
527146aad74SMarcel Moolenaar int
528b40ce416SJulian Elischer linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
529146aad74SMarcel Moolenaar {
530206a5d3aSIan Dowse 	sigset_t sigmask;
5315002a60fSMarcel Moolenaar 	l_sigset_t mask;
532146aad74SMarcel Moolenaar 
533146aad74SMarcel Moolenaar #ifdef DEBUG
53424593369SJonathan Lemon 	if (ldebug(sigsuspend))
53524593369SJonathan Lemon 		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
536146aad74SMarcel Moolenaar #endif
537146aad74SMarcel Moolenaar 
538146aad74SMarcel Moolenaar 	LINUX_SIGEMPTYSET(mask);
5394ab7403bSDmitry Chagin 	mask.__mask = args->mask;
540206a5d3aSIan Dowse 	linux_to_bsd_sigset(&mask, &sigmask);
541206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
542146aad74SMarcel Moolenaar }
543146aad74SMarcel Moolenaar 
544146aad74SMarcel Moolenaar int
545b07cd97eSMark Murray linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
546146aad74SMarcel Moolenaar {
5475002a60fSMarcel Moolenaar 	l_sigset_t lmask;
548206a5d3aSIan Dowse 	sigset_t sigmask;
549146aad74SMarcel Moolenaar 	int error;
550146aad74SMarcel Moolenaar 
551146aad74SMarcel Moolenaar #ifdef DEBUG
55224593369SJonathan Lemon 	if (ldebug(rt_sigsuspend))
55324593369SJonathan Lemon 		printf(ARGS(rt_sigsuspend, "%p, %d"),
554146aad74SMarcel Moolenaar 		    (void *)uap->newset, uap->sigsetsize);
555146aad74SMarcel Moolenaar #endif
556146aad74SMarcel Moolenaar 
5575002a60fSMarcel Moolenaar 	if (uap->sigsetsize != sizeof(l_sigset_t))
558146aad74SMarcel Moolenaar 		return (EINVAL);
559146aad74SMarcel Moolenaar 
5605002a60fSMarcel Moolenaar 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
561146aad74SMarcel Moolenaar 	if (error)
562146aad74SMarcel Moolenaar 		return (error);
563146aad74SMarcel Moolenaar 
564206a5d3aSIan Dowse 	linux_to_bsd_sigset(&lmask, &sigmask);
565206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
566146aad74SMarcel Moolenaar }
567146aad74SMarcel Moolenaar 
568146aad74SMarcel Moolenaar int
569b40ce416SJulian Elischer linux_pause(struct thread *td, struct linux_pause_args *args)
570146aad74SMarcel Moolenaar {
571b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
572206a5d3aSIan Dowse 	sigset_t sigmask;
573146aad74SMarcel Moolenaar 
574146aad74SMarcel Moolenaar #ifdef DEBUG
57524593369SJonathan Lemon 	if (ldebug(pause))
57624593369SJonathan Lemon 		printf(ARGS(pause, ""));
577146aad74SMarcel Moolenaar #endif
578146aad74SMarcel Moolenaar 
579fdfdfb78SJohn Baldwin 	PROC_LOCK(p);
5804093529dSJeff Roberson 	sigmask = td->td_sigmask;
581fdfdfb78SJohn Baldwin 	PROC_UNLOCK(p);
582206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
583146aad74SMarcel Moolenaar }
584146aad74SMarcel Moolenaar 
585146aad74SMarcel Moolenaar int
586b40ce416SJulian Elischer linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
587146aad74SMarcel Moolenaar {
588206a5d3aSIan Dowse 	stack_t ss, oss;
5895002a60fSMarcel Moolenaar 	l_stack_t lss;
590146aad74SMarcel Moolenaar 	int error;
591146aad74SMarcel Moolenaar 
592146aad74SMarcel Moolenaar #ifdef DEBUG
59324593369SJonathan Lemon 	if (ldebug(sigaltstack))
59424593369SJonathan Lemon 		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
595146aad74SMarcel Moolenaar #endif
596146aad74SMarcel Moolenaar 
597206a5d3aSIan Dowse 	if (uap->uss != NULL) {
5985002a60fSMarcel Moolenaar 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
599146aad74SMarcel Moolenaar 		if (error)
600146aad74SMarcel Moolenaar 			return (error);
601146aad74SMarcel Moolenaar 
602206a5d3aSIan Dowse 		ss.ss_sp = lss.ss_sp;
603206a5d3aSIan Dowse 		ss.ss_size = lss.ss_size;
604206a5d3aSIan Dowse 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
605931a7258SAndrew Gallatin 	}
606ef36ad69SJohn Baldwin 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
607ef36ad69SJohn Baldwin 	    (uap->uoss != NULL) ? &oss : NULL);
608206a5d3aSIan Dowse 	if (!error && uap->uoss != NULL) {
609206a5d3aSIan Dowse 		lss.ss_sp = oss.ss_sp;
610206a5d3aSIan Dowse 		lss.ss_size = oss.ss_size;
611206a5d3aSIan Dowse 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
6125002a60fSMarcel Moolenaar 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
613146aad74SMarcel Moolenaar 	}
614146aad74SMarcel Moolenaar 
615146aad74SMarcel Moolenaar 	return (error);
616146aad74SMarcel Moolenaar }
6173ad9c842SMaxim Sobolev 
6183ad9c842SMaxim Sobolev int
6193ad9c842SMaxim Sobolev linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
6203ad9c842SMaxim Sobolev {
6213ad9c842SMaxim Sobolev 
6223ad9c842SMaxim Sobolev #ifdef DEBUG
6233ad9c842SMaxim Sobolev 	if (ldebug(ftruncate64))
624b45bbfc3SBruce Evans 		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
625b45bbfc3SBruce Evans 		    (intmax_t)args->length);
6263ad9c842SMaxim Sobolev #endif
6273ad9c842SMaxim Sobolev 
628ae6b6ef6SEdward Tomasz Napierala 	return (kern_ftruncate(td, args->fd, args->length));
6293ad9c842SMaxim Sobolev }
6301bc85c0dSDoug Rabson 
6311bc85c0dSDoug Rabson int
6321bc85c0dSDoug Rabson linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
6331bc85c0dSDoug Rabson {
6349b44bfc5SAlexander Leidinger 	struct l_user_desc info;
6359b44bfc5SAlexander Leidinger 	int error;
6369b44bfc5SAlexander Leidinger 	int idx;
6379b44bfc5SAlexander Leidinger 	int a[2];
6389b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
6399b44bfc5SAlexander Leidinger 
6409b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
6419b44bfc5SAlexander Leidinger 	if (error)
6429b44bfc5SAlexander Leidinger 		return (error);
6439b44bfc5SAlexander Leidinger 
6449b44bfc5SAlexander Leidinger #ifdef DEBUG
6459b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
6469b44bfc5SAlexander Leidinger 		printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
6479b44bfc5SAlexander Leidinger 		      info.entry_number,
6489b44bfc5SAlexander Leidinger 		      info.base_addr,
6499b44bfc5SAlexander Leidinger 		      info.limit,
6509b44bfc5SAlexander Leidinger 		      info.seg_32bit,
6519b44bfc5SAlexander Leidinger 		      info.contents,
6529b44bfc5SAlexander Leidinger 		      info.read_exec_only,
6539b44bfc5SAlexander Leidinger 		      info.limit_in_pages,
6549b44bfc5SAlexander Leidinger 		      info.seg_not_present,
6559b44bfc5SAlexander Leidinger 		      info.useable);
6569b44bfc5SAlexander Leidinger #endif
6579b44bfc5SAlexander Leidinger 
6589b44bfc5SAlexander Leidinger 	idx = info.entry_number;
6590eef2f8aSAlexander Leidinger 	/*
660eae594f7SEd Maste 	 * Semantics of Linux version: every thread in the system has array of
661802e08a3SAlexander Leidinger 	 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
662802e08a3SAlexander Leidinger 	 * syscall loads one of the selected tls decriptors with a value and
663802e08a3SAlexander Leidinger 	 * also loads GDT descriptors 6, 7 and 8 with the content of the
664802e08a3SAlexander Leidinger 	 * per-thread descriptors.
6659b44bfc5SAlexander Leidinger 	 *
666eae594f7SEd Maste 	 * Semantics of FreeBSD version: I think we can ignore that Linux has 3
667802e08a3SAlexander Leidinger 	 * per-thread descriptors and use just the 1st one. The tls_array[]
668802e08a3SAlexander Leidinger 	 * is used only in set/get-thread_area() syscalls and for loading the
669eae594f7SEd Maste 	 * GDT descriptors. In FreeBSD we use just one GDT descriptor for TLS
670eae594f7SEd Maste 	 * so we will load just one.
671802e08a3SAlexander Leidinger 	 *
672802e08a3SAlexander Leidinger 	 * XXX: this doesn't work when a user space process tries to use more
673eae594f7SEd Maste 	 * than 1 TLS segment. Comment in the Linux sources says wine might do
674802e08a3SAlexander Leidinger 	 * this.
6751bc85c0dSDoug Rabson 	 */
6769b44bfc5SAlexander Leidinger 
6770eef2f8aSAlexander Leidinger 	/*
6780eef2f8aSAlexander Leidinger 	 * we support just GLIBC TLS now
6799b44bfc5SAlexander Leidinger 	 * we should let 3 proceed as well because we use this segment so
6809b44bfc5SAlexander Leidinger 	 * if code does two subsequent calls it should succeed
6819b44bfc5SAlexander Leidinger 	 */
6829b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != -1 && idx != 3)
6839b44bfc5SAlexander Leidinger 		return (EINVAL);
6849b44bfc5SAlexander Leidinger 
6850eef2f8aSAlexander Leidinger 	/*
6860eef2f8aSAlexander Leidinger 	 * we have to copy out the GDT entry we use
6879b44bfc5SAlexander Leidinger 	 * FreeBSD uses GDT entry #3 for storing %gs so load that
688802e08a3SAlexander Leidinger 	 *
689802e08a3SAlexander Leidinger 	 * XXX: what if a user space program doesn't check this value and tries
6909b44bfc5SAlexander Leidinger 	 * to use 6, 7 or 8?
6919b44bfc5SAlexander Leidinger 	 */
6929b44bfc5SAlexander Leidinger 	idx = info.entry_number = 3;
6939b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
6949b44bfc5SAlexander Leidinger 	if (error)
6959b44bfc5SAlexander Leidinger 		return (error);
6969b44bfc5SAlexander Leidinger 
697a4e3bad7SJung-uk Kim 	if (LINUX_LDT_empty(&info)) {
6989b44bfc5SAlexander Leidinger 		a[0] = 0;
6999b44bfc5SAlexander Leidinger 		a[1] = 0;
7009b44bfc5SAlexander Leidinger 	} else {
701a4e3bad7SJung-uk Kim 		a[0] = LINUX_LDT_entry_a(&info);
702a4e3bad7SJung-uk Kim 		a[1] = LINUX_LDT_entry_b(&info);
7031bc85c0dSDoug Rabson 	}
7041bc85c0dSDoug Rabson 
7059b44bfc5SAlexander Leidinger 	memcpy(&sd, &a, sizeof(a));
7069b44bfc5SAlexander Leidinger #ifdef DEBUG
7079b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
7089b44bfc5SAlexander 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,
7099b44bfc5SAlexander Leidinger 			sd.sd_hibase,
7109b44bfc5SAlexander Leidinger 			sd.sd_lolimit,
7119b44bfc5SAlexander Leidinger 			sd.sd_hilimit,
7129b44bfc5SAlexander Leidinger 			sd.sd_type,
7139b44bfc5SAlexander Leidinger 			sd.sd_dpl,
7149b44bfc5SAlexander Leidinger 			sd.sd_p,
7159b44bfc5SAlexander Leidinger 			sd.sd_xx,
7169b44bfc5SAlexander Leidinger 			sd.sd_def32,
7179b44bfc5SAlexander Leidinger 			sd.sd_gran);
7189b44bfc5SAlexander Leidinger #endif
7191bc85c0dSDoug Rabson 
7209b44bfc5SAlexander Leidinger 	/* this is taken from i386 version of cpu_set_user_tls() */
7219b44bfc5SAlexander Leidinger 	critical_enter();
7229b44bfc5SAlexander Leidinger 	/* set %gs */
7239b44bfc5SAlexander Leidinger 	td->td_pcb->pcb_gsd = sd;
7249b44bfc5SAlexander Leidinger 	PCPU_GET(fsgs_gdt)[1] = sd;
7259b44bfc5SAlexander Leidinger 	load_gs(GSEL(GUGS_SEL, SEL_UPL));
7269b44bfc5SAlexander Leidinger 	critical_exit();
7279b44bfc5SAlexander Leidinger 
7281bc85c0dSDoug Rabson 	return (0);
7291bc85c0dSDoug Rabson }
7301bc85c0dSDoug Rabson 
7311bc85c0dSDoug Rabson int
7329b44bfc5SAlexander Leidinger linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
7331bc85c0dSDoug Rabson {
7341bc85c0dSDoug Rabson 
7359b44bfc5SAlexander Leidinger 	struct l_user_desc info;
7369b44bfc5SAlexander Leidinger 	int error;
7379b44bfc5SAlexander Leidinger 	int idx;
7389b44bfc5SAlexander Leidinger 	struct l_desc_struct desc;
7399b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
7409b44bfc5SAlexander Leidinger 
7419b44bfc5SAlexander Leidinger #ifdef DEBUG
7429b44bfc5SAlexander Leidinger 	if (ldebug(get_thread_area))
7439b44bfc5SAlexander Leidinger 		printf(ARGS(get_thread_area, "%p"), args->desc);
7449b44bfc5SAlexander Leidinger #endif
7459b44bfc5SAlexander Leidinger 
7469b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
7479b44bfc5SAlexander Leidinger 	if (error)
7489b44bfc5SAlexander Leidinger 		return (error);
7499b44bfc5SAlexander Leidinger 
7509b44bfc5SAlexander Leidinger 	idx = info.entry_number;
7519b44bfc5SAlexander Leidinger 	/* XXX: I am not sure if we want 3 to be allowed too. */
7529b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != 3)
7539b44bfc5SAlexander Leidinger 		return (EINVAL);
7549b44bfc5SAlexander Leidinger 
7559b44bfc5SAlexander Leidinger 	idx = 3;
7569b44bfc5SAlexander Leidinger 
7579b44bfc5SAlexander Leidinger 	memset(&info, 0, sizeof(info));
7589b44bfc5SAlexander Leidinger 
7599b44bfc5SAlexander Leidinger 	sd = PCPU_GET(fsgs_gdt)[1];
7609b44bfc5SAlexander Leidinger 
7619b44bfc5SAlexander Leidinger 	memcpy(&desc, &sd, sizeof(desc));
7629b44bfc5SAlexander Leidinger 
7639b44bfc5SAlexander Leidinger 	info.entry_number = idx;
764a4e3bad7SJung-uk Kim 	info.base_addr = LINUX_GET_BASE(&desc);
765a4e3bad7SJung-uk Kim 	info.limit = LINUX_GET_LIMIT(&desc);
766a4e3bad7SJung-uk Kim 	info.seg_32bit = LINUX_GET_32BIT(&desc);
767a4e3bad7SJung-uk Kim 	info.contents = LINUX_GET_CONTENTS(&desc);
768a4e3bad7SJung-uk Kim 	info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
769a4e3bad7SJung-uk Kim 	info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
770a4e3bad7SJung-uk Kim 	info.seg_not_present = !LINUX_GET_PRESENT(&desc);
771a4e3bad7SJung-uk Kim 	info.useable = LINUX_GET_USEABLE(&desc);
7729b44bfc5SAlexander Leidinger 
7739b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
7749b44bfc5SAlexander Leidinger 	if (error)
7759b44bfc5SAlexander Leidinger 		return (EFAULT);
7769b44bfc5SAlexander Leidinger 
7779b44bfc5SAlexander Leidinger 	return (0);
7789b44bfc5SAlexander Leidinger }
7799b44bfc5SAlexander Leidinger 
7809b44bfc5SAlexander Leidinger /* XXX: this wont work with module - convert it */
7819b44bfc5SAlexander Leidinger int
7829b44bfc5SAlexander Leidinger linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
7839b44bfc5SAlexander Leidinger {
7849b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
785340f4a8dSEd Maste 	return (sys_kmq_open(td, (struct kmq_open_args *)args));
7869b44bfc5SAlexander Leidinger #else
7879b44bfc5SAlexander Leidinger 	return (ENOSYS);
7889b44bfc5SAlexander Leidinger #endif
7899b44bfc5SAlexander Leidinger }
7909b44bfc5SAlexander Leidinger 
7919b44bfc5SAlexander Leidinger int
7929b44bfc5SAlexander Leidinger linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
7939b44bfc5SAlexander Leidinger {
7949b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
795340f4a8dSEd Maste 	return (sys_kmq_unlink(td, (struct kmq_unlink_args *)args));
7969b44bfc5SAlexander Leidinger #else
7979b44bfc5SAlexander Leidinger 	return (ENOSYS);
7989b44bfc5SAlexander Leidinger #endif
7999b44bfc5SAlexander Leidinger }
8009b44bfc5SAlexander Leidinger 
8019b44bfc5SAlexander Leidinger int
8029b44bfc5SAlexander Leidinger linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
8039b44bfc5SAlexander Leidinger {
8049b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
805340f4a8dSEd Maste 	return (sys_kmq_timedsend(td, (struct kmq_timedsend_args *)args));
8069b44bfc5SAlexander Leidinger #else
8079b44bfc5SAlexander Leidinger 	return (ENOSYS);
8089b44bfc5SAlexander Leidinger #endif
8099b44bfc5SAlexander Leidinger }
8109b44bfc5SAlexander Leidinger 
8119b44bfc5SAlexander Leidinger int
8129b44bfc5SAlexander Leidinger linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
8139b44bfc5SAlexander Leidinger {
8149b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
815340f4a8dSEd Maste 	return (sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *)args));
8169b44bfc5SAlexander Leidinger #else
8179b44bfc5SAlexander Leidinger 	return (ENOSYS);
8189b44bfc5SAlexander Leidinger #endif
8199b44bfc5SAlexander Leidinger }
8209b44bfc5SAlexander Leidinger 
8219b44bfc5SAlexander Leidinger int
8229b44bfc5SAlexander Leidinger linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
8239b44bfc5SAlexander Leidinger {
8249b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
825340f4a8dSEd Maste 	return (sys_kmq_notify(td, (struct kmq_notify_args *)args));
8269b44bfc5SAlexander Leidinger #else
8279b44bfc5SAlexander Leidinger 	return (ENOSYS);
8289b44bfc5SAlexander Leidinger #endif
8299b44bfc5SAlexander Leidinger }
8309b44bfc5SAlexander Leidinger 
8319b44bfc5SAlexander Leidinger int
8329b44bfc5SAlexander Leidinger linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
8339b44bfc5SAlexander Leidinger {
8349b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
835340f4a8dSEd Maste 	return (sys_kmq_setattr(td, (struct kmq_setattr_args *)args));
8369b44bfc5SAlexander Leidinger #else
8379b44bfc5SAlexander Leidinger 	return (ENOSYS);
8389b44bfc5SAlexander Leidinger #endif
8391bc85c0dSDoug Rabson }
840