xref: /freebsd/sys/i386/linux/linux_machdep.c (revision c0aa0e2c27d69f73059ae454e4efa760d6599698)
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>
35*c0aa0e2cSEd 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>
46*c0aa0e2cSEd Maste #include <sys/sched.h>
471bc85c0dSDoug Rabson #include <sys/signalvar.h>
48206a5d3aSIan Dowse #include <sys/syscallsubr.h>
49fb919e4dSMark Murray #include <sys/sysproto.h>
50*c0aa0e2cSEd Maste #include <sys/systm.h>
51*c0aa0e2cSEd 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>
61*c0aa0e2cSEd 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>
66*c0aa0e2cSEd 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;
1365002a60fSMarcel Moolenaar 		a.tsops = 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;
1554b7ef73dSDag-Erling Smørgrav 		error = copyin(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;
1645002a60fSMarcel Moolenaar 		a.msgp = 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 
1795002a60fSMarcel Moolenaar 			if (args->ptr == NULL)
1805002a60fSMarcel Moolenaar 				return (EINVAL);
1814b7ef73dSDag-Erling Smørgrav 			error = copyin(args->ptr, &tmp, sizeof(tmp));
1825002a60fSMarcel Moolenaar 			if (error)
1835002a60fSMarcel Moolenaar 				return (error);
1845002a60fSMarcel Moolenaar 			a.msgp = tmp.msgp;
1855002a60fSMarcel Moolenaar 			a.msgtyp = tmp.msgtyp;
1865002a60fSMarcel Moolenaar 		} else {
1875002a60fSMarcel Moolenaar 			a.msgp = 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;
2045002a60fSMarcel Moolenaar 		a.buf = args->ptr;
205b40ce416SJulian Elischer 		return (linux_msgctl(td, &a));
2065002a60fSMarcel Moolenaar 	}
2075002a60fSMarcel Moolenaar 	case LINUX_SHMAT: {
2085002a60fSMarcel Moolenaar 		struct linux_shmat_args a;
2095002a60fSMarcel Moolenaar 
2105002a60fSMarcel Moolenaar 		a.shmid = args->arg1;
2115002a60fSMarcel Moolenaar 		a.shmaddr = args->ptr;
2125002a60fSMarcel Moolenaar 		a.shmflg = args->arg2;
2135002a60fSMarcel Moolenaar 		a.raddr = (l_ulong *)args->arg3;
214b40ce416SJulian Elischer 		return (linux_shmat(td, &a));
2155002a60fSMarcel Moolenaar 	}
2165002a60fSMarcel Moolenaar 	case LINUX_SHMDT: {
2175002a60fSMarcel Moolenaar 		struct linux_shmdt_args a;
2185002a60fSMarcel Moolenaar 
2195002a60fSMarcel Moolenaar 		a.shmaddr = args->ptr;
220b40ce416SJulian Elischer 		return (linux_shmdt(td, &a));
2215002a60fSMarcel Moolenaar 	}
2225002a60fSMarcel Moolenaar 	case LINUX_SHMGET: {
2235002a60fSMarcel Moolenaar 		struct linux_shmget_args a;
2245002a60fSMarcel Moolenaar 
2255002a60fSMarcel Moolenaar 		a.key = args->arg1;
2265002a60fSMarcel Moolenaar 		a.size = args->arg2;
2275002a60fSMarcel Moolenaar 		a.shmflg = args->arg3;
228b40ce416SJulian Elischer 		return (linux_shmget(td, &a));
2295002a60fSMarcel Moolenaar 	}
2305002a60fSMarcel Moolenaar 	case LINUX_SHMCTL: {
2315002a60fSMarcel Moolenaar 		struct linux_shmctl_args a;
2325002a60fSMarcel Moolenaar 
2335002a60fSMarcel Moolenaar 		a.shmid = args->arg1;
2345002a60fSMarcel Moolenaar 		a.cmd = args->arg2;
2355002a60fSMarcel Moolenaar 		a.buf = args->ptr;
236b40ce416SJulian Elischer 		return (linux_shmctl(td, &a));
2375002a60fSMarcel Moolenaar 	}
2385002a60fSMarcel Moolenaar 	default:
2395002a60fSMarcel Moolenaar 		break;
240146aad74SMarcel Moolenaar 	}
241146aad74SMarcel Moolenaar 
2425002a60fSMarcel Moolenaar 	return (EINVAL);
243146aad74SMarcel Moolenaar }
244146aad74SMarcel Moolenaar 
245146aad74SMarcel Moolenaar int
246b40ce416SJulian Elischer linux_old_select(struct thread *td, struct linux_old_select_args *args)
247146aad74SMarcel Moolenaar {
2485002a60fSMarcel Moolenaar 	struct l_old_select_argv linux_args;
2495002a60fSMarcel Moolenaar 	struct linux_select_args newsel;
250146aad74SMarcel Moolenaar 	int error;
251146aad74SMarcel Moolenaar 
2525002a60fSMarcel Moolenaar #ifdef DEBUG
2535002a60fSMarcel Moolenaar 	if (ldebug(old_select))
2546aea6777SPeter Wemm 		printf(ARGS(old_select, "%p"), args->ptr);
255146aad74SMarcel Moolenaar #endif
256146aad74SMarcel Moolenaar 
2574b7ef73dSDag-Erling Smørgrav 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
258146aad74SMarcel Moolenaar 	if (error)
259146aad74SMarcel Moolenaar 		return (error);
260146aad74SMarcel Moolenaar 
261146aad74SMarcel Moolenaar 	newsel.nfds = linux_args.nfds;
262146aad74SMarcel Moolenaar 	newsel.readfds = linux_args.readfds;
263146aad74SMarcel Moolenaar 	newsel.writefds = linux_args.writefds;
264146aad74SMarcel Moolenaar 	newsel.exceptfds = linux_args.exceptfds;
265146aad74SMarcel Moolenaar 	newsel.timeout = linux_args.timeout;
266b40ce416SJulian Elischer 	return (linux_select(td, &newsel));
267146aad74SMarcel Moolenaar }
268146aad74SMarcel Moolenaar 
269146aad74SMarcel Moolenaar int
2702c7660baSDmitry Chagin linux_set_cloned_tls(struct thread *td, void *desc)
2712c7660baSDmitry Chagin {
2722c7660baSDmitry Chagin 	struct segment_descriptor sd;
2732c7660baSDmitry Chagin 	struct l_user_desc info;
2742c7660baSDmitry Chagin 	int idx, error;
2752c7660baSDmitry Chagin 	int a[2];
2762c7660baSDmitry Chagin 
2772c7660baSDmitry Chagin 	error = copyin(desc, &info, sizeof(struct l_user_desc));
2782c7660baSDmitry Chagin 	if (error) {
2792c7660baSDmitry Chagin 		printf(LMSG("copyin failed!"));
2802c7660baSDmitry Chagin 	} else {
2812c7660baSDmitry Chagin 		idx = info.entry_number;
2822c7660baSDmitry Chagin 
2832c7660baSDmitry Chagin 		/*
2842c7660baSDmitry Chagin 		 * looks like we're getting the idx we returned
2852c7660baSDmitry Chagin 		 * in the set_thread_area() syscall
2862c7660baSDmitry Chagin 		 */
2872c7660baSDmitry Chagin 		if (idx != 6 && idx != 3) {
2882c7660baSDmitry Chagin 			printf(LMSG("resetting idx!"));
2892c7660baSDmitry Chagin 			idx = 3;
2902c7660baSDmitry Chagin 		}
2912c7660baSDmitry Chagin 
2922c7660baSDmitry Chagin 		/* this doesnt happen in practice */
2932c7660baSDmitry Chagin 		if (idx == 6) {
2942c7660baSDmitry Chagin 			/* we might copy out the entry_number as 3 */
2952c7660baSDmitry Chagin 			info.entry_number = 3;
2962c7660baSDmitry Chagin 			error = copyout(&info, desc, sizeof(struct l_user_desc));
2972c7660baSDmitry Chagin 			if (error)
2982c7660baSDmitry Chagin 				printf(LMSG("copyout failed!"));
2992c7660baSDmitry Chagin 		}
3002c7660baSDmitry Chagin 
3012c7660baSDmitry Chagin 		a[0] = LINUX_LDT_entry_a(&info);
3022c7660baSDmitry Chagin 		a[1] = LINUX_LDT_entry_b(&info);
3032c7660baSDmitry Chagin 
3042c7660baSDmitry Chagin 		memcpy(&sd, &a, sizeof(a));
3052c7660baSDmitry Chagin #ifdef DEBUG
3062c7660baSDmitry Chagin 		if (ldebug(clone))
3072c7660baSDmitry Chagin 			printf("Segment created in clone with "
3082c7660baSDmitry Chagin 			"CLONE_SETTLS: lobase: %x, hibase: %x, "
3092c7660baSDmitry Chagin 			"lolimit: %x, hilimit: %x, type: %i, "
3102c7660baSDmitry Chagin 			"dpl: %i, p: %i, xx: %i, def32: %i, "
3112c7660baSDmitry Chagin 			"gran: %i\n", sd.sd_lobase, sd.sd_hibase,
3122c7660baSDmitry Chagin 			sd.sd_lolimit, sd.sd_hilimit, sd.sd_type,
3132c7660baSDmitry Chagin 			sd.sd_dpl, sd.sd_p, sd.sd_xx,
3142c7660baSDmitry Chagin 			sd.sd_def32, sd.sd_gran);
3152c7660baSDmitry Chagin #endif
3162c7660baSDmitry Chagin 
3172c7660baSDmitry Chagin 		/* set %gs */
3182c7660baSDmitry Chagin 		td->td_pcb->pcb_gsd = sd;
3192c7660baSDmitry Chagin 		td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
3202c7660baSDmitry Chagin 	}
3212c7660baSDmitry Chagin 
3222c7660baSDmitry Chagin 	return (error);
3232c7660baSDmitry Chagin }
3242c7660baSDmitry Chagin 
325146aad74SMarcel Moolenaar int
326c8d6845eSDmitry Chagin linux_set_upcall_kse(struct thread *td, register_t stack)
327c8d6845eSDmitry Chagin {
328c8d6845eSDmitry Chagin 
32981338031SDmitry Chagin 	if (stack)
330c8d6845eSDmitry Chagin 		td->td_frame->tf_esp = stack;
331c8d6845eSDmitry Chagin 
33281338031SDmitry Chagin 	/*
33381338031SDmitry Chagin 	 * The newly created Linux thread returns
33481338031SDmitry Chagin 	 * to the user space by the same path that a parent do.
33581338031SDmitry Chagin 	 */
33681338031SDmitry Chagin 	td->td_frame->tf_eax = 0;
337c8d6845eSDmitry Chagin 	return (0);
338c8d6845eSDmitry Chagin }
339c8d6845eSDmitry Chagin 
3403ad9c842SMaxim Sobolev int
3413ad9c842SMaxim Sobolev linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
3423ad9c842SMaxim Sobolev {
3433ad9c842SMaxim Sobolev 
3443ad9c842SMaxim Sobolev #ifdef DEBUG
3453ad9c842SMaxim Sobolev 	if (ldebug(mmap2))
3463ad9c842SMaxim Sobolev 		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
3473ad9c842SMaxim Sobolev 		    (void *)args->addr, args->len, args->prot,
3483ad9c842SMaxim Sobolev 		    args->flags, args->fd, args->pgoff);
3493ad9c842SMaxim Sobolev #endif
3503ad9c842SMaxim Sobolev 
351f12c0348SJohn Baldwin 	return (linux_mmap_common(td, args->addr, args->len, args->prot,
352f12c0348SJohn Baldwin 		args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
353f12c0348SJohn Baldwin 		PAGE_SIZE));
3543ad9c842SMaxim Sobolev }
3553ad9c842SMaxim Sobolev 
356146aad74SMarcel Moolenaar int
357b40ce416SJulian Elischer linux_mmap(struct thread *td, struct linux_mmap_args *args)
358146aad74SMarcel Moolenaar {
3593ad9c842SMaxim Sobolev 	int error;
3603ad9c842SMaxim Sobolev 	struct l_mmap_argv linux_args;
3613ad9c842SMaxim Sobolev 
3624b7ef73dSDag-Erling Smørgrav 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
3633ad9c842SMaxim Sobolev 	if (error)
3643ad9c842SMaxim Sobolev 		return (error);
3653ad9c842SMaxim Sobolev 
3663ad9c842SMaxim Sobolev #ifdef DEBUG
3673ad9c842SMaxim Sobolev 	if (ldebug(mmap))
3683ad9c842SMaxim Sobolev 		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
369b45bbfc3SBruce Evans 		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
37010931a46SJung-uk Kim 		    linux_args.flags, linux_args.fd, linux_args.pgoff);
3713ad9c842SMaxim Sobolev #endif
3723ad9c842SMaxim Sobolev 
373f12c0348SJohn Baldwin 	return (linux_mmap_common(td, linux_args.addr, linux_args.len,
374f12c0348SJohn Baldwin 	    linux_args.prot, linux_args.flags, linux_args.fd,
375f12c0348SJohn Baldwin 	    (uint32_t)linux_args.pgoff));
3763ad9c842SMaxim Sobolev }
3773ad9c842SMaxim Sobolev 
378146aad74SMarcel Moolenaar int
37910931a46SJung-uk Kim linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
38010931a46SJung-uk Kim {
38110931a46SJung-uk Kim 
38297d06da6SDmitry Chagin 	return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
38310931a46SJung-uk Kim }
38410931a46SJung-uk Kim 
38510931a46SJung-uk Kim int
386b40ce416SJulian Elischer linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
387146aad74SMarcel Moolenaar {
38884569dffSMaxim Sobolev 	int error;
38984569dffSMaxim Sobolev 	struct i386_ioperm_args iia;
390146aad74SMarcel Moolenaar 
39184569dffSMaxim Sobolev 	iia.start = args->start;
39284569dffSMaxim Sobolev 	iia.length = args->length;
39384569dffSMaxim Sobolev 	iia.enable = args->enable;
39484569dffSMaxim Sobolev 	error = i386_set_ioperm(td, &iia);
39584569dffSMaxim Sobolev 	return (error);
396146aad74SMarcel Moolenaar }
397146aad74SMarcel Moolenaar 
398146aad74SMarcel Moolenaar int
399b40ce416SJulian Elischer linux_iopl(struct thread *td, struct linux_iopl_args *args)
400146aad74SMarcel Moolenaar {
401146aad74SMarcel Moolenaar 	int error;
402146aad74SMarcel Moolenaar 
403146aad74SMarcel Moolenaar 	if (args->level < 0 || args->level > 3)
404146aad74SMarcel Moolenaar 		return (EINVAL);
405acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_IO)) != 0)
406146aad74SMarcel Moolenaar 		return (error);
407a854ed98SJohn Baldwin 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
40841c42188SRobert Watson 		return (error);
409b40ce416SJulian Elischer 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
410146aad74SMarcel Moolenaar 	    (args->level * (PSL_IOPL / 3));
411146aad74SMarcel Moolenaar 	return (0);
412146aad74SMarcel Moolenaar }
413146aad74SMarcel Moolenaar 
414146aad74SMarcel Moolenaar int
415b07cd97eSMark Murray linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
416146aad74SMarcel Moolenaar {
417146aad74SMarcel Moolenaar 	int error;
41884569dffSMaxim Sobolev 	struct i386_ldt_args ldt;
4195002a60fSMarcel Moolenaar 	struct l_descriptor ld;
42084569dffSMaxim Sobolev 	union descriptor desc;
4216259969dSKonstantin Belousov 	int size, written;
422146aad74SMarcel Moolenaar 
423146aad74SMarcel Moolenaar 	switch (uap->func) {
424146aad74SMarcel Moolenaar 	case 0x00: /* read_ldt */
42584569dffSMaxim Sobolev 		ldt.start = 0;
42684569dffSMaxim Sobolev 		ldt.descs = uap->ptr;
42784569dffSMaxim Sobolev 		ldt.num = uap->bytecount / sizeof(union descriptor);
42884569dffSMaxim Sobolev 		error = i386_get_ldt(td, &ldt);
429b40ce416SJulian Elischer 		td->td_retval[0] *= sizeof(union descriptor);
430146aad74SMarcel Moolenaar 		break;
4316259969dSKonstantin Belousov 	case 0x02: /* read_default_ldt = 0 */
4326259969dSKonstantin Belousov 		size = 5*sizeof(struct l_desc_struct);
4336259969dSKonstantin Belousov 		if (size > uap->bytecount)
4346259969dSKonstantin Belousov 			size = uap->bytecount;
4356259969dSKonstantin Belousov 		for (written = error = 0; written < size && error == 0; written++)
4366259969dSKonstantin Belousov 			error = subyte((char *)uap->ptr + written, 0);
4376259969dSKonstantin Belousov 		td->td_retval[0] = written;
4386259969dSKonstantin Belousov 		break;
439146aad74SMarcel Moolenaar 	case 0x01: /* write_ldt */
440146aad74SMarcel Moolenaar 	case 0x11: /* write_ldt */
441146aad74SMarcel Moolenaar 		if (uap->bytecount != sizeof(ld))
442146aad74SMarcel Moolenaar 			return (EINVAL);
443146aad74SMarcel Moolenaar 
444146aad74SMarcel Moolenaar 		error = copyin(uap->ptr, &ld, sizeof(ld));
445146aad74SMarcel Moolenaar 		if (error)
446146aad74SMarcel Moolenaar 			return (error);
447146aad74SMarcel Moolenaar 
44884569dffSMaxim Sobolev 		ldt.start = ld.entry_number;
44984569dffSMaxim Sobolev 		ldt.descs = &desc;
45084569dffSMaxim Sobolev 		ldt.num = 1;
45184569dffSMaxim Sobolev 		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
45284569dffSMaxim Sobolev 		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
45384569dffSMaxim Sobolev 		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
45484569dffSMaxim Sobolev 		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
45584569dffSMaxim Sobolev 		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
456146aad74SMarcel Moolenaar 			(ld.contents << 2);
45784569dffSMaxim Sobolev 		desc.sd.sd_dpl = 3;
45884569dffSMaxim Sobolev 		desc.sd.sd_p = (ld.seg_not_present ^ 1);
45984569dffSMaxim Sobolev 		desc.sd.sd_xx = 0;
46084569dffSMaxim Sobolev 		desc.sd.sd_def32 = ld.seg_32bit;
46184569dffSMaxim Sobolev 		desc.sd.sd_gran = ld.limit_in_pages;
46284569dffSMaxim Sobolev 		error = i386_set_ldt(td, &ldt, &desc);
463146aad74SMarcel Moolenaar 		break;
464146aad74SMarcel Moolenaar 	default:
4657c020cbbSJohn Baldwin 		error = ENOSYS;
466146aad74SMarcel Moolenaar 		break;
467146aad74SMarcel Moolenaar 	}
468146aad74SMarcel Moolenaar 
469146aad74SMarcel Moolenaar 	if (error == EOPNOTSUPP) {
470146aad74SMarcel Moolenaar 		printf("linux: modify_ldt needs kernel option USER_LDT\n");
471146aad74SMarcel Moolenaar 		error = ENOSYS;
472146aad74SMarcel Moolenaar 	}
473146aad74SMarcel Moolenaar 
474146aad74SMarcel Moolenaar 	return (error);
475146aad74SMarcel Moolenaar }
476146aad74SMarcel Moolenaar 
477146aad74SMarcel Moolenaar int
478b40ce416SJulian Elischer linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
479146aad74SMarcel Moolenaar {
4805002a60fSMarcel Moolenaar 	l_osigaction_t osa;
4815002a60fSMarcel Moolenaar 	l_sigaction_t act, oact;
482146aad74SMarcel Moolenaar 	int error;
483146aad74SMarcel Moolenaar 
484146aad74SMarcel Moolenaar #ifdef DEBUG
48524593369SJonathan Lemon 	if (ldebug(sigaction))
48624593369SJonathan Lemon 		printf(ARGS(sigaction, "%d, %p, %p"),
487146aad74SMarcel Moolenaar 		    args->sig, (void *)args->nsa, (void *)args->osa);
488146aad74SMarcel Moolenaar #endif
489146aad74SMarcel Moolenaar 
490146aad74SMarcel Moolenaar 	if (args->nsa != NULL) {
4914b7ef73dSDag-Erling Smørgrav 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
492146aad74SMarcel Moolenaar 		if (error)
493146aad74SMarcel Moolenaar 			return (error);
494146aad74SMarcel Moolenaar 		act.lsa_handler = osa.lsa_handler;
495146aad74SMarcel Moolenaar 		act.lsa_flags = osa.lsa_flags;
496146aad74SMarcel Moolenaar 		act.lsa_restorer = osa.lsa_restorer;
497146aad74SMarcel Moolenaar 		LINUX_SIGEMPTYSET(act.lsa_mask);
4984ab7403bSDmitry Chagin 		act.lsa_mask.__mask = osa.lsa_mask;
499146aad74SMarcel Moolenaar 	}
500146aad74SMarcel Moolenaar 
501b40ce416SJulian Elischer 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
502146aad74SMarcel Moolenaar 	    args->osa ? &oact : NULL);
503146aad74SMarcel Moolenaar 
504146aad74SMarcel Moolenaar 	if (args->osa != NULL && !error) {
505146aad74SMarcel Moolenaar 		osa.lsa_handler = oact.lsa_handler;
506146aad74SMarcel Moolenaar 		osa.lsa_flags = oact.lsa_flags;
507146aad74SMarcel Moolenaar 		osa.lsa_restorer = oact.lsa_restorer;
5084ab7403bSDmitry Chagin 		osa.lsa_mask = oact.lsa_mask.__mask;
5094b7ef73dSDag-Erling Smørgrav 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
510146aad74SMarcel Moolenaar 	}
511146aad74SMarcel Moolenaar 
512146aad74SMarcel Moolenaar 	return (error);
513146aad74SMarcel Moolenaar }
514146aad74SMarcel Moolenaar 
515146aad74SMarcel Moolenaar /*
516146aad74SMarcel Moolenaar  * Linux has two extra args, restart and oldmask.  We dont use these,
517146aad74SMarcel Moolenaar  * but it seems that "restart" is actually a context pointer that
518146aad74SMarcel Moolenaar  * enables the signal to happen with a different register set.
519146aad74SMarcel Moolenaar  */
520146aad74SMarcel Moolenaar int
521b40ce416SJulian Elischer linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
522146aad74SMarcel Moolenaar {
523206a5d3aSIan Dowse 	sigset_t sigmask;
5245002a60fSMarcel Moolenaar 	l_sigset_t mask;
525146aad74SMarcel Moolenaar 
526146aad74SMarcel Moolenaar #ifdef DEBUG
52724593369SJonathan Lemon 	if (ldebug(sigsuspend))
52824593369SJonathan Lemon 		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
529146aad74SMarcel Moolenaar #endif
530146aad74SMarcel Moolenaar 
531146aad74SMarcel Moolenaar 	LINUX_SIGEMPTYSET(mask);
5324ab7403bSDmitry Chagin 	mask.__mask = args->mask;
533206a5d3aSIan Dowse 	linux_to_bsd_sigset(&mask, &sigmask);
534206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
535146aad74SMarcel Moolenaar }
536146aad74SMarcel Moolenaar 
537146aad74SMarcel Moolenaar int
538b07cd97eSMark Murray linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
539146aad74SMarcel Moolenaar {
5405002a60fSMarcel Moolenaar 	l_sigset_t lmask;
541206a5d3aSIan Dowse 	sigset_t sigmask;
542146aad74SMarcel Moolenaar 	int error;
543146aad74SMarcel Moolenaar 
544146aad74SMarcel Moolenaar #ifdef DEBUG
54524593369SJonathan Lemon 	if (ldebug(rt_sigsuspend))
54624593369SJonathan Lemon 		printf(ARGS(rt_sigsuspend, "%p, %d"),
547146aad74SMarcel Moolenaar 		    (void *)uap->newset, uap->sigsetsize);
548146aad74SMarcel Moolenaar #endif
549146aad74SMarcel Moolenaar 
5505002a60fSMarcel Moolenaar 	if (uap->sigsetsize != sizeof(l_sigset_t))
551146aad74SMarcel Moolenaar 		return (EINVAL);
552146aad74SMarcel Moolenaar 
5535002a60fSMarcel Moolenaar 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
554146aad74SMarcel Moolenaar 	if (error)
555146aad74SMarcel Moolenaar 		return (error);
556146aad74SMarcel Moolenaar 
557206a5d3aSIan Dowse 	linux_to_bsd_sigset(&lmask, &sigmask);
558206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
559146aad74SMarcel Moolenaar }
560146aad74SMarcel Moolenaar 
561146aad74SMarcel Moolenaar int
562b40ce416SJulian Elischer linux_pause(struct thread *td, struct linux_pause_args *args)
563146aad74SMarcel Moolenaar {
564b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
565206a5d3aSIan Dowse 	sigset_t sigmask;
566146aad74SMarcel Moolenaar 
567146aad74SMarcel Moolenaar #ifdef DEBUG
56824593369SJonathan Lemon 	if (ldebug(pause))
56924593369SJonathan Lemon 		printf(ARGS(pause, ""));
570146aad74SMarcel Moolenaar #endif
571146aad74SMarcel Moolenaar 
572fdfdfb78SJohn Baldwin 	PROC_LOCK(p);
5734093529dSJeff Roberson 	sigmask = td->td_sigmask;
574fdfdfb78SJohn Baldwin 	PROC_UNLOCK(p);
575206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
576146aad74SMarcel Moolenaar }
577146aad74SMarcel Moolenaar 
578146aad74SMarcel Moolenaar int
579b40ce416SJulian Elischer linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
580146aad74SMarcel Moolenaar {
581206a5d3aSIan Dowse 	stack_t ss, oss;
5825002a60fSMarcel Moolenaar 	l_stack_t lss;
583146aad74SMarcel Moolenaar 	int error;
584146aad74SMarcel Moolenaar 
585146aad74SMarcel Moolenaar #ifdef DEBUG
58624593369SJonathan Lemon 	if (ldebug(sigaltstack))
58724593369SJonathan Lemon 		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
588146aad74SMarcel Moolenaar #endif
589146aad74SMarcel Moolenaar 
590206a5d3aSIan Dowse 	if (uap->uss != NULL) {
5915002a60fSMarcel Moolenaar 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
592146aad74SMarcel Moolenaar 		if (error)
593146aad74SMarcel Moolenaar 			return (error);
594146aad74SMarcel Moolenaar 
595206a5d3aSIan Dowse 		ss.ss_sp = lss.ss_sp;
596206a5d3aSIan Dowse 		ss.ss_size = lss.ss_size;
597206a5d3aSIan Dowse 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
598931a7258SAndrew Gallatin 	}
599ef36ad69SJohn Baldwin 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
600ef36ad69SJohn Baldwin 	    (uap->uoss != NULL) ? &oss : NULL);
601206a5d3aSIan Dowse 	if (!error && uap->uoss != NULL) {
602206a5d3aSIan Dowse 		lss.ss_sp = oss.ss_sp;
603206a5d3aSIan Dowse 		lss.ss_size = oss.ss_size;
604206a5d3aSIan Dowse 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
6055002a60fSMarcel Moolenaar 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
606146aad74SMarcel Moolenaar 	}
607146aad74SMarcel Moolenaar 
608146aad74SMarcel Moolenaar 	return (error);
609146aad74SMarcel Moolenaar }
6103ad9c842SMaxim Sobolev 
6113ad9c842SMaxim Sobolev int
6123ad9c842SMaxim Sobolev linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
6133ad9c842SMaxim Sobolev {
6143ad9c842SMaxim Sobolev 
6153ad9c842SMaxim Sobolev #ifdef DEBUG
6163ad9c842SMaxim Sobolev 	if (ldebug(ftruncate64))
617b45bbfc3SBruce Evans 		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
618b45bbfc3SBruce Evans 		    (intmax_t)args->length);
6193ad9c842SMaxim Sobolev #endif
6203ad9c842SMaxim Sobolev 
621ae6b6ef6SEdward Tomasz Napierala 	return (kern_ftruncate(td, args->fd, args->length));
6223ad9c842SMaxim Sobolev }
6231bc85c0dSDoug Rabson 
6241bc85c0dSDoug Rabson int
6251bc85c0dSDoug Rabson linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
6261bc85c0dSDoug Rabson {
6279b44bfc5SAlexander Leidinger 	struct l_user_desc info;
6289b44bfc5SAlexander Leidinger 	int error;
6299b44bfc5SAlexander Leidinger 	int idx;
6309b44bfc5SAlexander Leidinger 	int a[2];
6319b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
6329b44bfc5SAlexander Leidinger 
6339b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
6349b44bfc5SAlexander Leidinger 	if (error)
6359b44bfc5SAlexander Leidinger 		return (error);
6369b44bfc5SAlexander Leidinger 
6379b44bfc5SAlexander Leidinger #ifdef DEBUG
6389b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
6399b44bfc5SAlexander Leidinger 		printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
6409b44bfc5SAlexander Leidinger 		      info.entry_number,
6419b44bfc5SAlexander Leidinger 		      info.base_addr,
6429b44bfc5SAlexander Leidinger 		      info.limit,
6439b44bfc5SAlexander Leidinger 		      info.seg_32bit,
6449b44bfc5SAlexander Leidinger 		      info.contents,
6459b44bfc5SAlexander Leidinger 		      info.read_exec_only,
6469b44bfc5SAlexander Leidinger 		      info.limit_in_pages,
6479b44bfc5SAlexander Leidinger 		      info.seg_not_present,
6489b44bfc5SAlexander Leidinger 		      info.useable);
6499b44bfc5SAlexander Leidinger #endif
6509b44bfc5SAlexander Leidinger 
6519b44bfc5SAlexander Leidinger 	idx = info.entry_number;
6520eef2f8aSAlexander Leidinger 	/*
653eae594f7SEd Maste 	 * Semantics of Linux version: every thread in the system has array of
654802e08a3SAlexander Leidinger 	 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
655802e08a3SAlexander Leidinger 	 * syscall loads one of the selected tls decriptors with a value and
656802e08a3SAlexander Leidinger 	 * also loads GDT descriptors 6, 7 and 8 with the content of the
657802e08a3SAlexander Leidinger 	 * per-thread descriptors.
6589b44bfc5SAlexander Leidinger 	 *
659eae594f7SEd Maste 	 * Semantics of FreeBSD version: I think we can ignore that Linux has 3
660802e08a3SAlexander Leidinger 	 * per-thread descriptors and use just the 1st one. The tls_array[]
661802e08a3SAlexander Leidinger 	 * is used only in set/get-thread_area() syscalls and for loading the
662eae594f7SEd Maste 	 * GDT descriptors. In FreeBSD we use just one GDT descriptor for TLS
663eae594f7SEd Maste 	 * so we will load just one.
664802e08a3SAlexander Leidinger 	 *
665802e08a3SAlexander Leidinger 	 * XXX: this doesn't work when a user space process tries to use more
666eae594f7SEd Maste 	 * than 1 TLS segment. Comment in the Linux sources says wine might do
667802e08a3SAlexander Leidinger 	 * this.
6681bc85c0dSDoug Rabson 	 */
6699b44bfc5SAlexander Leidinger 
6700eef2f8aSAlexander Leidinger 	/*
6710eef2f8aSAlexander Leidinger 	 * we support just GLIBC TLS now
6729b44bfc5SAlexander Leidinger 	 * we should let 3 proceed as well because we use this segment so
6739b44bfc5SAlexander Leidinger 	 * if code does two subsequent calls it should succeed
6749b44bfc5SAlexander Leidinger 	 */
6759b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != -1 && idx != 3)
6769b44bfc5SAlexander Leidinger 		return (EINVAL);
6779b44bfc5SAlexander Leidinger 
6780eef2f8aSAlexander Leidinger 	/*
6790eef2f8aSAlexander Leidinger 	 * we have to copy out the GDT entry we use
6809b44bfc5SAlexander Leidinger 	 * FreeBSD uses GDT entry #3 for storing %gs so load that
681802e08a3SAlexander Leidinger 	 *
682802e08a3SAlexander Leidinger 	 * XXX: what if a user space program doesn't check this value and tries
6839b44bfc5SAlexander Leidinger 	 * to use 6, 7 or 8?
6849b44bfc5SAlexander Leidinger 	 */
6859b44bfc5SAlexander Leidinger 	idx = info.entry_number = 3;
6869b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
6879b44bfc5SAlexander Leidinger 	if (error)
6889b44bfc5SAlexander Leidinger 		return (error);
6899b44bfc5SAlexander Leidinger 
690a4e3bad7SJung-uk Kim 	if (LINUX_LDT_empty(&info)) {
6919b44bfc5SAlexander Leidinger 		a[0] = 0;
6929b44bfc5SAlexander Leidinger 		a[1] = 0;
6939b44bfc5SAlexander Leidinger 	} else {
694a4e3bad7SJung-uk Kim 		a[0] = LINUX_LDT_entry_a(&info);
695a4e3bad7SJung-uk Kim 		a[1] = LINUX_LDT_entry_b(&info);
6961bc85c0dSDoug Rabson 	}
6971bc85c0dSDoug Rabson 
6989b44bfc5SAlexander Leidinger 	memcpy(&sd, &a, sizeof(a));
6999b44bfc5SAlexander Leidinger #ifdef DEBUG
7009b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
7019b44bfc5SAlexander 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,
7029b44bfc5SAlexander Leidinger 			sd.sd_hibase,
7039b44bfc5SAlexander Leidinger 			sd.sd_lolimit,
7049b44bfc5SAlexander Leidinger 			sd.sd_hilimit,
7059b44bfc5SAlexander Leidinger 			sd.sd_type,
7069b44bfc5SAlexander Leidinger 			sd.sd_dpl,
7079b44bfc5SAlexander Leidinger 			sd.sd_p,
7089b44bfc5SAlexander Leidinger 			sd.sd_xx,
7099b44bfc5SAlexander Leidinger 			sd.sd_def32,
7109b44bfc5SAlexander Leidinger 			sd.sd_gran);
7119b44bfc5SAlexander Leidinger #endif
7121bc85c0dSDoug Rabson 
7139b44bfc5SAlexander Leidinger 	/* this is taken from i386 version of cpu_set_user_tls() */
7149b44bfc5SAlexander Leidinger 	critical_enter();
7159b44bfc5SAlexander Leidinger 	/* set %gs */
7169b44bfc5SAlexander Leidinger 	td->td_pcb->pcb_gsd = sd;
7179b44bfc5SAlexander Leidinger 	PCPU_GET(fsgs_gdt)[1] = sd;
7189b44bfc5SAlexander Leidinger 	load_gs(GSEL(GUGS_SEL, SEL_UPL));
7199b44bfc5SAlexander Leidinger 	critical_exit();
7209b44bfc5SAlexander Leidinger 
7211bc85c0dSDoug Rabson 	return (0);
7221bc85c0dSDoug Rabson }
7231bc85c0dSDoug Rabson 
7241bc85c0dSDoug Rabson int
7259b44bfc5SAlexander Leidinger linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
7261bc85c0dSDoug Rabson {
7271bc85c0dSDoug Rabson 
7289b44bfc5SAlexander Leidinger 	struct l_user_desc info;
7299b44bfc5SAlexander Leidinger 	int error;
7309b44bfc5SAlexander Leidinger 	int idx;
7319b44bfc5SAlexander Leidinger 	struct l_desc_struct desc;
7329b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
7339b44bfc5SAlexander Leidinger 
7349b44bfc5SAlexander Leidinger #ifdef DEBUG
7359b44bfc5SAlexander Leidinger 	if (ldebug(get_thread_area))
7369b44bfc5SAlexander Leidinger 		printf(ARGS(get_thread_area, "%p"), args->desc);
7379b44bfc5SAlexander Leidinger #endif
7389b44bfc5SAlexander Leidinger 
7399b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
7409b44bfc5SAlexander Leidinger 	if (error)
7419b44bfc5SAlexander Leidinger 		return (error);
7429b44bfc5SAlexander Leidinger 
7439b44bfc5SAlexander Leidinger 	idx = info.entry_number;
7449b44bfc5SAlexander Leidinger 	/* XXX: I am not sure if we want 3 to be allowed too. */
7459b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != 3)
7469b44bfc5SAlexander Leidinger 		return (EINVAL);
7479b44bfc5SAlexander Leidinger 
7489b44bfc5SAlexander Leidinger 	idx = 3;
7499b44bfc5SAlexander Leidinger 
7509b44bfc5SAlexander Leidinger 	memset(&info, 0, sizeof(info));
7519b44bfc5SAlexander Leidinger 
7529b44bfc5SAlexander Leidinger 	sd = PCPU_GET(fsgs_gdt)[1];
7539b44bfc5SAlexander Leidinger 
7549b44bfc5SAlexander Leidinger 	memcpy(&desc, &sd, sizeof(desc));
7559b44bfc5SAlexander Leidinger 
7569b44bfc5SAlexander Leidinger 	info.entry_number = idx;
757a4e3bad7SJung-uk Kim 	info.base_addr = LINUX_GET_BASE(&desc);
758a4e3bad7SJung-uk Kim 	info.limit = LINUX_GET_LIMIT(&desc);
759a4e3bad7SJung-uk Kim 	info.seg_32bit = LINUX_GET_32BIT(&desc);
760a4e3bad7SJung-uk Kim 	info.contents = LINUX_GET_CONTENTS(&desc);
761a4e3bad7SJung-uk Kim 	info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
762a4e3bad7SJung-uk Kim 	info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
763a4e3bad7SJung-uk Kim 	info.seg_not_present = !LINUX_GET_PRESENT(&desc);
764a4e3bad7SJung-uk Kim 	info.useable = LINUX_GET_USEABLE(&desc);
7659b44bfc5SAlexander Leidinger 
7669b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
7679b44bfc5SAlexander Leidinger 	if (error)
7689b44bfc5SAlexander Leidinger 		return (EFAULT);
7699b44bfc5SAlexander Leidinger 
7709b44bfc5SAlexander Leidinger 	return (0);
7719b44bfc5SAlexander Leidinger }
7729b44bfc5SAlexander Leidinger 
7739b44bfc5SAlexander Leidinger /* XXX: this wont work with module - convert it */
7749b44bfc5SAlexander Leidinger int
7759b44bfc5SAlexander Leidinger linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
7769b44bfc5SAlexander Leidinger {
7779b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
778340f4a8dSEd Maste 	return (sys_kmq_open(td, (struct kmq_open_args *)args));
7799b44bfc5SAlexander Leidinger #else
7809b44bfc5SAlexander Leidinger 	return (ENOSYS);
7819b44bfc5SAlexander Leidinger #endif
7829b44bfc5SAlexander Leidinger }
7839b44bfc5SAlexander Leidinger 
7849b44bfc5SAlexander Leidinger int
7859b44bfc5SAlexander Leidinger linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
7869b44bfc5SAlexander Leidinger {
7879b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
788340f4a8dSEd Maste 	return (sys_kmq_unlink(td, (struct kmq_unlink_args *)args));
7899b44bfc5SAlexander Leidinger #else
7909b44bfc5SAlexander Leidinger 	return (ENOSYS);
7919b44bfc5SAlexander Leidinger #endif
7929b44bfc5SAlexander Leidinger }
7939b44bfc5SAlexander Leidinger 
7949b44bfc5SAlexander Leidinger int
7959b44bfc5SAlexander Leidinger linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
7969b44bfc5SAlexander Leidinger {
7979b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
798340f4a8dSEd Maste 	return (sys_kmq_timedsend(td, (struct kmq_timedsend_args *)args));
7999b44bfc5SAlexander Leidinger #else
8009b44bfc5SAlexander Leidinger 	return (ENOSYS);
8019b44bfc5SAlexander Leidinger #endif
8029b44bfc5SAlexander Leidinger }
8039b44bfc5SAlexander Leidinger 
8049b44bfc5SAlexander Leidinger int
8059b44bfc5SAlexander Leidinger linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
8069b44bfc5SAlexander Leidinger {
8079b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
808340f4a8dSEd Maste 	return (sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *)args));
8099b44bfc5SAlexander Leidinger #else
8109b44bfc5SAlexander Leidinger 	return (ENOSYS);
8119b44bfc5SAlexander Leidinger #endif
8129b44bfc5SAlexander Leidinger }
8139b44bfc5SAlexander Leidinger 
8149b44bfc5SAlexander Leidinger int
8159b44bfc5SAlexander Leidinger linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
8169b44bfc5SAlexander Leidinger {
8179b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
818340f4a8dSEd Maste 	return (sys_kmq_notify(td, (struct kmq_notify_args *)args));
8199b44bfc5SAlexander Leidinger #else
8209b44bfc5SAlexander Leidinger 	return (ENOSYS);
8219b44bfc5SAlexander Leidinger #endif
8229b44bfc5SAlexander Leidinger }
8239b44bfc5SAlexander Leidinger 
8249b44bfc5SAlexander Leidinger int
8259b44bfc5SAlexander Leidinger linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
8269b44bfc5SAlexander Leidinger {
8279b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
828340f4a8dSEd Maste 	return (sys_kmq_setattr(td, (struct kmq_setattr_args *)args));
8299b44bfc5SAlexander Leidinger #else
8309b44bfc5SAlexander Leidinger 	return (ENOSYS);
8319b44bfc5SAlexander Leidinger #endif
8321bc85c0dSDoug Rabson }
833