xref: /freebsd/sys/i386/linux/linux_machdep.c (revision 83ef78be9591275896b8a0bda1d2155becca3f3c)
1146aad74SMarcel Moolenaar /*-
2*83ef78beSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*83ef78beSPedro 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
11146aad74SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer
12146aad74SMarcel Moolenaar  *    in this position and unchanged.
13146aad74SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
14146aad74SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
15146aad74SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
16146aad74SMarcel Moolenaar  * 3. The name of the author may not be used to endorse or promote products
17bc34729cSMarcel Moolenaar  *    derived from this software without specific prior written permission.
18146aad74SMarcel Moolenaar  *
19146aad74SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20146aad74SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21146aad74SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22146aad74SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23146aad74SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24146aad74SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25146aad74SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26146aad74SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27146aad74SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28146aad74SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29146aad74SMarcel Moolenaar  */
30146aad74SMarcel Moolenaar 
3127e0099cSDavid E. O'Brien #include <sys/cdefs.h>
3227e0099cSDavid E. O'Brien __FBSDID("$FreeBSD$");
3327e0099cSDavid E. O'Brien 
34146aad74SMarcel Moolenaar #include <sys/param.h>
35146aad74SMarcel Moolenaar #include <sys/systm.h>
364a144410SRobert Watson #include <sys/capsicum.h>
37a312f6a3SAlexander Leidinger #include <sys/file.h>
38a312f6a3SAlexander Leidinger #include <sys/fcntl.h>
39610ecfe0SMaxim Sobolev #include <sys/imgact.h>
407106ca0dSJohn Baldwin #include <sys/lock.h>
41610ecfe0SMaxim Sobolev #include <sys/malloc.h>
42fb919e4dSMark Murray #include <sys/mman.h>
43fb919e4dSMark Murray #include <sys/mutex.h>
449b44bfc5SAlexander Leidinger #include <sys/sx.h>
45acd3428bSRobert Watson #include <sys/priv.h>
46fb919e4dSMark Murray #include <sys/proc.h>
479b44bfc5SAlexander Leidinger #include <sys/queue.h>
48242fae60SAndrew Gallatin #include <sys/resource.h>
49242fae60SAndrew Gallatin #include <sys/resourcevar.h>
501bc85c0dSDoug Rabson #include <sys/signalvar.h>
51206a5d3aSIan Dowse #include <sys/syscallsubr.h>
52fb919e4dSMark Murray #include <sys/sysproto.h>
53fb919e4dSMark Murray #include <sys/unistd.h>
549b44bfc5SAlexander Leidinger #include <sys/wait.h>
55f0393f06SJeff Roberson #include <sys/sched.h>
56146aad74SMarcel Moolenaar 
57146aad74SMarcel Moolenaar #include <machine/frame.h>
58146aad74SMarcel Moolenaar #include <machine/psl.h>
59146aad74SMarcel Moolenaar #include <machine/segments.h>
60146aad74SMarcel Moolenaar #include <machine/sysarch.h>
61146aad74SMarcel Moolenaar 
62242fae60SAndrew Gallatin #include <vm/vm.h>
63242fae60SAndrew Gallatin #include <vm/pmap.h>
64242fae60SAndrew Gallatin #include <vm/vm_map.h>
65242fae60SAndrew Gallatin 
66146aad74SMarcel Moolenaar #include <i386/linux/linux.h>
67ebea8660SMarcel Moolenaar #include <i386/linux/linux_proto.h>
68146aad74SMarcel Moolenaar #include <compat/linux/linux_ipc.h>
69adc7ece0SDmitry Chagin #include <compat/linux/linux_misc.h>
7097d06da6SDmitry Chagin #include <compat/linux/linux_mmap.h>
71146aad74SMarcel Moolenaar #include <compat/linux/linux_signal.h>
72146aad74SMarcel Moolenaar #include <compat/linux/linux_util.h>
739b44bfc5SAlexander Leidinger #include <compat/linux/linux_emul.h>
749b44bfc5SAlexander Leidinger 
759b44bfc5SAlexander Leidinger #include <i386/include/pcb.h>			/* needed for pcb definition in linux_set_thread_area */
769b44bfc5SAlexander Leidinger 
779b44bfc5SAlexander Leidinger #include "opt_posix.h"
789b44bfc5SAlexander Leidinger 
799b44bfc5SAlexander Leidinger extern struct sysentvec elf32_freebsd_sysvec;	/* defined in i386/i386/elf_machdep.c */
80146aad74SMarcel Moolenaar 
815002a60fSMarcel Moolenaar struct l_descriptor {
825002a60fSMarcel Moolenaar 	l_uint		entry_number;
835002a60fSMarcel Moolenaar 	l_ulong		base_addr;
845002a60fSMarcel Moolenaar 	l_uint		limit;
855002a60fSMarcel Moolenaar 	l_uint		seg_32bit:1;
865002a60fSMarcel Moolenaar 	l_uint		contents:2;
875002a60fSMarcel Moolenaar 	l_uint		read_exec_only:1;
885002a60fSMarcel Moolenaar 	l_uint		limit_in_pages:1;
895002a60fSMarcel Moolenaar 	l_uint		seg_not_present:1;
905002a60fSMarcel Moolenaar 	l_uint		useable:1;
91146aad74SMarcel Moolenaar };
92146aad74SMarcel Moolenaar 
935002a60fSMarcel Moolenaar struct l_old_select_argv {
945002a60fSMarcel Moolenaar 	l_int		nfds;
955002a60fSMarcel Moolenaar 	l_fd_set	*readfds;
965002a60fSMarcel Moolenaar 	l_fd_set	*writefds;
975002a60fSMarcel Moolenaar 	l_fd_set	*exceptfds;
985002a60fSMarcel Moolenaar 	struct l_timeval	*timeout;
99146aad74SMarcel Moolenaar };
100146aad74SMarcel Moolenaar 
101931a7258SAndrew Gallatin 
102931a7258SAndrew Gallatin int
103b40ce416SJulian Elischer linux_execve(struct thread *td, struct linux_execve_args *args)
104146aad74SMarcel Moolenaar {
105610ecfe0SMaxim Sobolev 	struct image_args eargs;
1067b445033SKonstantin Belousov 	char *newpath;
1077b445033SKonstantin Belousov 	int error;
108146aad74SMarcel Moolenaar 
109d9e97471SJohn Baldwin 	LCONVPATHEXIST(td, args->path, &newpath);
110146aad74SMarcel Moolenaar 
111146aad74SMarcel Moolenaar #ifdef DEBUG
11224593369SJonathan Lemon 	if (ldebug(execve))
113610ecfe0SMaxim Sobolev 		printf(ARGS(execve, "%s"), newpath);
114146aad74SMarcel Moolenaar #endif
115146aad74SMarcel Moolenaar 
116610ecfe0SMaxim Sobolev 	error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
117610ecfe0SMaxim Sobolev 	    args->argp, args->envp);
118610ecfe0SMaxim Sobolev 	free(newpath, M_TEMP);
119610ecfe0SMaxim Sobolev 	if (error == 0)
12081338031SDmitry Chagin 		error = linux_common_execve(td, &eargs);
121610ecfe0SMaxim Sobolev 	return (error);
122146aad74SMarcel Moolenaar }
123146aad74SMarcel Moolenaar 
1245002a60fSMarcel Moolenaar struct l_ipc_kludge {
1255002a60fSMarcel Moolenaar 	struct l_msgbuf *msgp;
1265002a60fSMarcel Moolenaar 	l_long msgtyp;
1275002a60fSMarcel Moolenaar };
1285002a60fSMarcel Moolenaar 
129146aad74SMarcel Moolenaar int
130b40ce416SJulian Elischer linux_ipc(struct thread *td, struct linux_ipc_args *args)
131146aad74SMarcel Moolenaar {
1325002a60fSMarcel Moolenaar 
1335002a60fSMarcel Moolenaar 	switch (args->what & 0xFFFF) {
1345002a60fSMarcel Moolenaar 	case LINUX_SEMOP: {
1355002a60fSMarcel Moolenaar 		struct linux_semop_args a;
1365002a60fSMarcel Moolenaar 
1375002a60fSMarcel Moolenaar 		a.semid = args->arg1;
1385002a60fSMarcel Moolenaar 		a.tsops = args->ptr;
1395002a60fSMarcel Moolenaar 		a.nsops = args->arg2;
140b40ce416SJulian Elischer 		return (linux_semop(td, &a));
1415002a60fSMarcel Moolenaar 	}
1425002a60fSMarcel Moolenaar 	case LINUX_SEMGET: {
1435002a60fSMarcel Moolenaar 		struct linux_semget_args a;
1445002a60fSMarcel Moolenaar 
1455002a60fSMarcel Moolenaar 		a.key = args->arg1;
1465002a60fSMarcel Moolenaar 		a.nsems = args->arg2;
1475002a60fSMarcel Moolenaar 		a.semflg = args->arg3;
148b40ce416SJulian Elischer 		return (linux_semget(td, &a));
1495002a60fSMarcel Moolenaar 	}
1505002a60fSMarcel Moolenaar 	case LINUX_SEMCTL: {
1515002a60fSMarcel Moolenaar 		struct linux_semctl_args a;
1525002a60fSMarcel Moolenaar 		int error;
1535002a60fSMarcel Moolenaar 
1545002a60fSMarcel Moolenaar 		a.semid = args->arg1;
1555002a60fSMarcel Moolenaar 		a.semnum = args->arg2;
1565002a60fSMarcel Moolenaar 		a.cmd = args->arg3;
1574b7ef73dSDag-Erling Smørgrav 		error = copyin(args->ptr, &a.arg, sizeof(a.arg));
1585002a60fSMarcel Moolenaar 		if (error)
1595002a60fSMarcel Moolenaar 			return (error);
160b40ce416SJulian Elischer 		return (linux_semctl(td, &a));
1615002a60fSMarcel Moolenaar 	}
1625002a60fSMarcel Moolenaar 	case LINUX_MSGSND: {
1635002a60fSMarcel Moolenaar 		struct linux_msgsnd_args a;
1645002a60fSMarcel Moolenaar 
1655002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
1665002a60fSMarcel Moolenaar 		a.msgp = args->ptr;
1675002a60fSMarcel Moolenaar 		a.msgsz = args->arg2;
1685002a60fSMarcel Moolenaar 		a.msgflg = args->arg3;
169b40ce416SJulian Elischer 		return (linux_msgsnd(td, &a));
1705002a60fSMarcel Moolenaar 	}
1715002a60fSMarcel Moolenaar 	case LINUX_MSGRCV: {
1725002a60fSMarcel Moolenaar 		struct linux_msgrcv_args a;
1735002a60fSMarcel Moolenaar 
1745002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
1755002a60fSMarcel Moolenaar 		a.msgsz = args->arg2;
1765002a60fSMarcel Moolenaar 		a.msgflg = args->arg3;
1775002a60fSMarcel Moolenaar 		if ((args->what >> 16) == 0) {
1785002a60fSMarcel Moolenaar 			struct l_ipc_kludge tmp;
1795002a60fSMarcel Moolenaar 			int error;
1805002a60fSMarcel Moolenaar 
1815002a60fSMarcel Moolenaar 			if (args->ptr == NULL)
1825002a60fSMarcel Moolenaar 				return (EINVAL);
1834b7ef73dSDag-Erling Smørgrav 			error = copyin(args->ptr, &tmp, sizeof(tmp));
1845002a60fSMarcel Moolenaar 			if (error)
1855002a60fSMarcel Moolenaar 				return (error);
1865002a60fSMarcel Moolenaar 			a.msgp = tmp.msgp;
1875002a60fSMarcel Moolenaar 			a.msgtyp = tmp.msgtyp;
1885002a60fSMarcel Moolenaar 		} else {
1895002a60fSMarcel Moolenaar 			a.msgp = args->ptr;
1905002a60fSMarcel Moolenaar 			a.msgtyp = args->arg5;
1915002a60fSMarcel Moolenaar 		}
192b40ce416SJulian Elischer 		return (linux_msgrcv(td, &a));
1935002a60fSMarcel Moolenaar 	}
1945002a60fSMarcel Moolenaar 	case LINUX_MSGGET: {
1955002a60fSMarcel Moolenaar 		struct linux_msgget_args a;
1965002a60fSMarcel Moolenaar 
1975002a60fSMarcel Moolenaar 		a.key = args->arg1;
1985002a60fSMarcel Moolenaar 		a.msgflg = args->arg2;
199b40ce416SJulian Elischer 		return (linux_msgget(td, &a));
2005002a60fSMarcel Moolenaar 	}
2015002a60fSMarcel Moolenaar 	case LINUX_MSGCTL: {
2025002a60fSMarcel Moolenaar 		struct linux_msgctl_args a;
2035002a60fSMarcel Moolenaar 
2045002a60fSMarcel Moolenaar 		a.msqid = args->arg1;
2055002a60fSMarcel Moolenaar 		a.cmd = args->arg2;
2065002a60fSMarcel Moolenaar 		a.buf = args->ptr;
207b40ce416SJulian Elischer 		return (linux_msgctl(td, &a));
2085002a60fSMarcel Moolenaar 	}
2095002a60fSMarcel Moolenaar 	case LINUX_SHMAT: {
2105002a60fSMarcel Moolenaar 		struct linux_shmat_args a;
2115002a60fSMarcel Moolenaar 
2125002a60fSMarcel Moolenaar 		a.shmid = args->arg1;
2135002a60fSMarcel Moolenaar 		a.shmaddr = args->ptr;
2145002a60fSMarcel Moolenaar 		a.shmflg = args->arg2;
2155002a60fSMarcel Moolenaar 		a.raddr = (l_ulong *)args->arg3;
216b40ce416SJulian Elischer 		return (linux_shmat(td, &a));
2175002a60fSMarcel Moolenaar 	}
2185002a60fSMarcel Moolenaar 	case LINUX_SHMDT: {
2195002a60fSMarcel Moolenaar 		struct linux_shmdt_args a;
2205002a60fSMarcel Moolenaar 
2215002a60fSMarcel Moolenaar 		a.shmaddr = args->ptr;
222b40ce416SJulian Elischer 		return (linux_shmdt(td, &a));
2235002a60fSMarcel Moolenaar 	}
2245002a60fSMarcel Moolenaar 	case LINUX_SHMGET: {
2255002a60fSMarcel Moolenaar 		struct linux_shmget_args a;
2265002a60fSMarcel Moolenaar 
2275002a60fSMarcel Moolenaar 		a.key = args->arg1;
2285002a60fSMarcel Moolenaar 		a.size = args->arg2;
2295002a60fSMarcel Moolenaar 		a.shmflg = args->arg3;
230b40ce416SJulian Elischer 		return (linux_shmget(td, &a));
2315002a60fSMarcel Moolenaar 	}
2325002a60fSMarcel Moolenaar 	case LINUX_SHMCTL: {
2335002a60fSMarcel Moolenaar 		struct linux_shmctl_args a;
2345002a60fSMarcel Moolenaar 
2355002a60fSMarcel Moolenaar 		a.shmid = args->arg1;
2365002a60fSMarcel Moolenaar 		a.cmd = args->arg2;
2375002a60fSMarcel Moolenaar 		a.buf = args->ptr;
238b40ce416SJulian Elischer 		return (linux_shmctl(td, &a));
2395002a60fSMarcel Moolenaar 	}
2405002a60fSMarcel Moolenaar 	default:
2415002a60fSMarcel Moolenaar 		break;
242146aad74SMarcel Moolenaar 	}
243146aad74SMarcel Moolenaar 
2445002a60fSMarcel Moolenaar 	return (EINVAL);
245146aad74SMarcel Moolenaar }
246146aad74SMarcel Moolenaar 
247146aad74SMarcel Moolenaar int
248b40ce416SJulian Elischer linux_old_select(struct thread *td, struct linux_old_select_args *args)
249146aad74SMarcel Moolenaar {
2505002a60fSMarcel Moolenaar 	struct l_old_select_argv linux_args;
2515002a60fSMarcel Moolenaar 	struct linux_select_args newsel;
252146aad74SMarcel Moolenaar 	int error;
253146aad74SMarcel Moolenaar 
2545002a60fSMarcel Moolenaar #ifdef DEBUG
2555002a60fSMarcel Moolenaar 	if (ldebug(old_select))
2566aea6777SPeter Wemm 		printf(ARGS(old_select, "%p"), args->ptr);
257146aad74SMarcel Moolenaar #endif
258146aad74SMarcel Moolenaar 
2594b7ef73dSDag-Erling Smørgrav 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
260146aad74SMarcel Moolenaar 	if (error)
261146aad74SMarcel Moolenaar 		return (error);
262146aad74SMarcel Moolenaar 
263146aad74SMarcel Moolenaar 	newsel.nfds = linux_args.nfds;
264146aad74SMarcel Moolenaar 	newsel.readfds = linux_args.readfds;
265146aad74SMarcel Moolenaar 	newsel.writefds = linux_args.writefds;
266146aad74SMarcel Moolenaar 	newsel.exceptfds = linux_args.exceptfds;
267146aad74SMarcel Moolenaar 	newsel.timeout = linux_args.timeout;
268b40ce416SJulian Elischer 	return (linux_select(td, &newsel));
269146aad74SMarcel Moolenaar }
270146aad74SMarcel Moolenaar 
271146aad74SMarcel Moolenaar int
2722c7660baSDmitry Chagin linux_set_cloned_tls(struct thread *td, void *desc)
2732c7660baSDmitry Chagin {
2742c7660baSDmitry Chagin 	struct segment_descriptor sd;
2752c7660baSDmitry Chagin 	struct l_user_desc info;
2762c7660baSDmitry Chagin 	int idx, error;
2772c7660baSDmitry Chagin 	int a[2];
2782c7660baSDmitry Chagin 
2792c7660baSDmitry Chagin 	error = copyin(desc, &info, sizeof(struct l_user_desc));
2802c7660baSDmitry Chagin 	if (error) {
2812c7660baSDmitry Chagin 		printf(LMSG("copyin failed!"));
2822c7660baSDmitry Chagin 	} else {
2832c7660baSDmitry Chagin 		idx = info.entry_number;
2842c7660baSDmitry Chagin 
2852c7660baSDmitry Chagin 		/*
2862c7660baSDmitry Chagin 		 * looks like we're getting the idx we returned
2872c7660baSDmitry Chagin 		 * in the set_thread_area() syscall
2882c7660baSDmitry Chagin 		 */
2892c7660baSDmitry Chagin 		if (idx != 6 && idx != 3) {
2902c7660baSDmitry Chagin 			printf(LMSG("resetting idx!"));
2912c7660baSDmitry Chagin 			idx = 3;
2922c7660baSDmitry Chagin 		}
2932c7660baSDmitry Chagin 
2942c7660baSDmitry Chagin 		/* this doesnt happen in practice */
2952c7660baSDmitry Chagin 		if (idx == 6) {
2962c7660baSDmitry Chagin 	   		/* we might copy out the entry_number as 3 */
2972c7660baSDmitry Chagin 		   	info.entry_number = 3;
2982c7660baSDmitry Chagin 			error = copyout(&info, desc, sizeof(struct l_user_desc));
2992c7660baSDmitry Chagin 			if (error)
3002c7660baSDmitry Chagin 				printf(LMSG("copyout failed!"));
3012c7660baSDmitry Chagin 		}
3022c7660baSDmitry Chagin 
3032c7660baSDmitry Chagin 		a[0] = LINUX_LDT_entry_a(&info);
3042c7660baSDmitry Chagin 		a[1] = LINUX_LDT_entry_b(&info);
3052c7660baSDmitry Chagin 
3062c7660baSDmitry Chagin 		memcpy(&sd, &a, sizeof(a));
3072c7660baSDmitry Chagin #ifdef DEBUG
3082c7660baSDmitry Chagin 		if (ldebug(clone))
3092c7660baSDmitry Chagin 			printf("Segment created in clone with "
3102c7660baSDmitry Chagin 			"CLONE_SETTLS: lobase: %x, hibase: %x, "
3112c7660baSDmitry Chagin 			"lolimit: %x, hilimit: %x, type: %i, "
3122c7660baSDmitry Chagin 			"dpl: %i, p: %i, xx: %i, def32: %i, "
3132c7660baSDmitry Chagin 			"gran: %i\n", sd.sd_lobase, sd.sd_hibase,
3142c7660baSDmitry Chagin 			sd.sd_lolimit, sd.sd_hilimit, sd.sd_type,
3152c7660baSDmitry Chagin 			sd.sd_dpl, sd.sd_p, sd.sd_xx,
3162c7660baSDmitry Chagin 			sd.sd_def32, sd.sd_gran);
3172c7660baSDmitry Chagin #endif
3182c7660baSDmitry Chagin 
3192c7660baSDmitry Chagin 		/* set %gs */
3202c7660baSDmitry Chagin 		td->td_pcb->pcb_gsd = sd;
3212c7660baSDmitry Chagin 		td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
3222c7660baSDmitry Chagin 	}
3232c7660baSDmitry Chagin 
3242c7660baSDmitry Chagin 	return (error);
3252c7660baSDmitry Chagin }
3262c7660baSDmitry Chagin 
327146aad74SMarcel Moolenaar int
328c8d6845eSDmitry Chagin linux_set_upcall_kse(struct thread *td, register_t stack)
329c8d6845eSDmitry Chagin {
330c8d6845eSDmitry Chagin 
33181338031SDmitry Chagin 	if (stack)
332c8d6845eSDmitry Chagin 		td->td_frame->tf_esp = stack;
333c8d6845eSDmitry Chagin 
33481338031SDmitry Chagin 	/*
33581338031SDmitry Chagin 	 * The newly created Linux thread returns
33681338031SDmitry Chagin 	 * to the user space by the same path that a parent do.
33781338031SDmitry Chagin 	 */
33881338031SDmitry Chagin 	td->td_frame->tf_eax = 0;
339c8d6845eSDmitry Chagin 	return (0);
340c8d6845eSDmitry Chagin }
341c8d6845eSDmitry Chagin 
3423ad9c842SMaxim Sobolev int
3433ad9c842SMaxim Sobolev linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
3443ad9c842SMaxim Sobolev {
3453ad9c842SMaxim Sobolev 
3463ad9c842SMaxim Sobolev #ifdef DEBUG
3473ad9c842SMaxim Sobolev 	if (ldebug(mmap2))
3483ad9c842SMaxim Sobolev 		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
3493ad9c842SMaxim Sobolev 		    (void *)args->addr, args->len, args->prot,
3503ad9c842SMaxim Sobolev 		    args->flags, args->fd, args->pgoff);
3513ad9c842SMaxim Sobolev #endif
3523ad9c842SMaxim Sobolev 
353f12c0348SJohn Baldwin 	return (linux_mmap_common(td, args->addr, args->len, args->prot,
354f12c0348SJohn Baldwin 		args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
355f12c0348SJohn Baldwin 		PAGE_SIZE));
3563ad9c842SMaxim Sobolev }
3573ad9c842SMaxim Sobolev 
358146aad74SMarcel Moolenaar int
359b40ce416SJulian Elischer linux_mmap(struct thread *td, struct linux_mmap_args *args)
360146aad74SMarcel Moolenaar {
3613ad9c842SMaxim Sobolev 	int error;
3623ad9c842SMaxim Sobolev 	struct l_mmap_argv linux_args;
3633ad9c842SMaxim Sobolev 
3644b7ef73dSDag-Erling Smørgrav 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
3653ad9c842SMaxim Sobolev 	if (error)
3663ad9c842SMaxim Sobolev 		return (error);
3673ad9c842SMaxim Sobolev 
3683ad9c842SMaxim Sobolev #ifdef DEBUG
3693ad9c842SMaxim Sobolev 	if (ldebug(mmap))
3703ad9c842SMaxim Sobolev 		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
371b45bbfc3SBruce Evans 		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
37210931a46SJung-uk Kim 		    linux_args.flags, linux_args.fd, linux_args.pgoff);
3733ad9c842SMaxim Sobolev #endif
3743ad9c842SMaxim Sobolev 
375f12c0348SJohn Baldwin 	return (linux_mmap_common(td, linux_args.addr, linux_args.len,
376f12c0348SJohn Baldwin 	    linux_args.prot, linux_args.flags, linux_args.fd,
377f12c0348SJohn Baldwin 	    (uint32_t)linux_args.pgoff));
3783ad9c842SMaxim Sobolev }
3793ad9c842SMaxim Sobolev 
380146aad74SMarcel Moolenaar int
38110931a46SJung-uk Kim linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
38210931a46SJung-uk Kim {
38310931a46SJung-uk Kim 
38497d06da6SDmitry Chagin 	return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
38510931a46SJung-uk Kim }
38610931a46SJung-uk Kim 
38710931a46SJung-uk Kim int
388b40ce416SJulian Elischer linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
389146aad74SMarcel Moolenaar {
39084569dffSMaxim Sobolev 	int error;
39184569dffSMaxim Sobolev 	struct i386_ioperm_args iia;
392146aad74SMarcel Moolenaar 
39384569dffSMaxim Sobolev 	iia.start = args->start;
39484569dffSMaxim Sobolev 	iia.length = args->length;
39584569dffSMaxim Sobolev 	iia.enable = args->enable;
39684569dffSMaxim Sobolev 	error = i386_set_ioperm(td, &iia);
39784569dffSMaxim Sobolev 	return (error);
398146aad74SMarcel Moolenaar }
399146aad74SMarcel Moolenaar 
400146aad74SMarcel Moolenaar int
401b40ce416SJulian Elischer linux_iopl(struct thread *td, struct linux_iopl_args *args)
402146aad74SMarcel Moolenaar {
403146aad74SMarcel Moolenaar 	int error;
404146aad74SMarcel Moolenaar 
405146aad74SMarcel Moolenaar 	if (args->level < 0 || args->level > 3)
406146aad74SMarcel Moolenaar 		return (EINVAL);
407acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_IO)) != 0)
408146aad74SMarcel Moolenaar 		return (error);
409a854ed98SJohn Baldwin 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
41041c42188SRobert Watson 		return (error);
411b40ce416SJulian Elischer 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
412146aad74SMarcel Moolenaar 	    (args->level * (PSL_IOPL / 3));
413146aad74SMarcel Moolenaar 	return (0);
414146aad74SMarcel Moolenaar }
415146aad74SMarcel Moolenaar 
416146aad74SMarcel Moolenaar int
417b07cd97eSMark Murray linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
418146aad74SMarcel Moolenaar {
419146aad74SMarcel Moolenaar 	int error;
42084569dffSMaxim Sobolev 	struct i386_ldt_args ldt;
4215002a60fSMarcel Moolenaar 	struct l_descriptor ld;
42284569dffSMaxim Sobolev 	union descriptor desc;
4236259969dSKonstantin Belousov 	int size, written;
424146aad74SMarcel Moolenaar 
425146aad74SMarcel Moolenaar 	switch (uap->func) {
426146aad74SMarcel Moolenaar 	case 0x00: /* read_ldt */
42784569dffSMaxim Sobolev 		ldt.start = 0;
42884569dffSMaxim Sobolev 		ldt.descs = uap->ptr;
42984569dffSMaxim Sobolev 		ldt.num = uap->bytecount / sizeof(union descriptor);
43084569dffSMaxim Sobolev 		error = i386_get_ldt(td, &ldt);
431b40ce416SJulian Elischer 		td->td_retval[0] *= sizeof(union descriptor);
432146aad74SMarcel Moolenaar 		break;
4336259969dSKonstantin Belousov 	case 0x02: /* read_default_ldt = 0 */
4346259969dSKonstantin Belousov 		size = 5*sizeof(struct l_desc_struct);
4356259969dSKonstantin Belousov 		if (size > uap->bytecount)
4366259969dSKonstantin Belousov 			size = uap->bytecount;
4376259969dSKonstantin Belousov 		for (written = error = 0; written < size && error == 0; written++)
4386259969dSKonstantin Belousov 			error = subyte((char *)uap->ptr + written, 0);
4396259969dSKonstantin Belousov 		td->td_retval[0] = written;
4406259969dSKonstantin Belousov 		break;
441146aad74SMarcel Moolenaar 	case 0x01: /* write_ldt */
442146aad74SMarcel Moolenaar 	case 0x11: /* write_ldt */
443146aad74SMarcel Moolenaar 		if (uap->bytecount != sizeof(ld))
444146aad74SMarcel Moolenaar 			return (EINVAL);
445146aad74SMarcel Moolenaar 
446146aad74SMarcel Moolenaar 		error = copyin(uap->ptr, &ld, sizeof(ld));
447146aad74SMarcel Moolenaar 		if (error)
448146aad74SMarcel Moolenaar 			return (error);
449146aad74SMarcel Moolenaar 
45084569dffSMaxim Sobolev 		ldt.start = ld.entry_number;
45184569dffSMaxim Sobolev 		ldt.descs = &desc;
45284569dffSMaxim Sobolev 		ldt.num = 1;
45384569dffSMaxim Sobolev 		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
45484569dffSMaxim Sobolev 		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
45584569dffSMaxim Sobolev 		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
45684569dffSMaxim Sobolev 		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
45784569dffSMaxim Sobolev 		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
458146aad74SMarcel Moolenaar 			(ld.contents << 2);
45984569dffSMaxim Sobolev 		desc.sd.sd_dpl = 3;
46084569dffSMaxim Sobolev 		desc.sd.sd_p = (ld.seg_not_present ^ 1);
46184569dffSMaxim Sobolev 		desc.sd.sd_xx = 0;
46284569dffSMaxim Sobolev 		desc.sd.sd_def32 = ld.seg_32bit;
46384569dffSMaxim Sobolev 		desc.sd.sd_gran = ld.limit_in_pages;
46484569dffSMaxim Sobolev 		error = i386_set_ldt(td, &ldt, &desc);
465146aad74SMarcel Moolenaar 		break;
466146aad74SMarcel Moolenaar 	default:
4677c020cbbSJohn Baldwin 		error = ENOSYS;
468146aad74SMarcel Moolenaar 		break;
469146aad74SMarcel Moolenaar 	}
470146aad74SMarcel Moolenaar 
471146aad74SMarcel Moolenaar 	if (error == EOPNOTSUPP) {
472146aad74SMarcel Moolenaar 		printf("linux: modify_ldt needs kernel option USER_LDT\n");
473146aad74SMarcel Moolenaar 		error = ENOSYS;
474146aad74SMarcel Moolenaar 	}
475146aad74SMarcel Moolenaar 
476146aad74SMarcel Moolenaar 	return (error);
477146aad74SMarcel Moolenaar }
478146aad74SMarcel Moolenaar 
479146aad74SMarcel Moolenaar int
480b40ce416SJulian Elischer linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
481146aad74SMarcel Moolenaar {
4825002a60fSMarcel Moolenaar 	l_osigaction_t osa;
4835002a60fSMarcel Moolenaar 	l_sigaction_t act, oact;
484146aad74SMarcel Moolenaar 	int error;
485146aad74SMarcel Moolenaar 
486146aad74SMarcel Moolenaar #ifdef DEBUG
48724593369SJonathan Lemon 	if (ldebug(sigaction))
48824593369SJonathan Lemon 		printf(ARGS(sigaction, "%d, %p, %p"),
489146aad74SMarcel Moolenaar 		    args->sig, (void *)args->nsa, (void *)args->osa);
490146aad74SMarcel Moolenaar #endif
491146aad74SMarcel Moolenaar 
492146aad74SMarcel Moolenaar 	if (args->nsa != NULL) {
4934b7ef73dSDag-Erling Smørgrav 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
494146aad74SMarcel Moolenaar 		if (error)
495146aad74SMarcel Moolenaar 			return (error);
496146aad74SMarcel Moolenaar 		act.lsa_handler = osa.lsa_handler;
497146aad74SMarcel Moolenaar 		act.lsa_flags = osa.lsa_flags;
498146aad74SMarcel Moolenaar 		act.lsa_restorer = osa.lsa_restorer;
499146aad74SMarcel Moolenaar 		LINUX_SIGEMPTYSET(act.lsa_mask);
5004ab7403bSDmitry Chagin 		act.lsa_mask.__mask = osa.lsa_mask;
501146aad74SMarcel Moolenaar 	}
502146aad74SMarcel Moolenaar 
503b40ce416SJulian Elischer 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
504146aad74SMarcel Moolenaar 	    args->osa ? &oact : NULL);
505146aad74SMarcel Moolenaar 
506146aad74SMarcel Moolenaar 	if (args->osa != NULL && !error) {
507146aad74SMarcel Moolenaar 		osa.lsa_handler = oact.lsa_handler;
508146aad74SMarcel Moolenaar 		osa.lsa_flags = oact.lsa_flags;
509146aad74SMarcel Moolenaar 		osa.lsa_restorer = oact.lsa_restorer;
5104ab7403bSDmitry Chagin 		osa.lsa_mask = oact.lsa_mask.__mask;
5114b7ef73dSDag-Erling Smørgrav 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
512146aad74SMarcel Moolenaar 	}
513146aad74SMarcel Moolenaar 
514146aad74SMarcel Moolenaar 	return (error);
515146aad74SMarcel Moolenaar }
516146aad74SMarcel Moolenaar 
517146aad74SMarcel Moolenaar /*
518146aad74SMarcel Moolenaar  * Linux has two extra args, restart and oldmask.  We dont use these,
519146aad74SMarcel Moolenaar  * but it seems that "restart" is actually a context pointer that
520146aad74SMarcel Moolenaar  * enables the signal to happen with a different register set.
521146aad74SMarcel Moolenaar  */
522146aad74SMarcel Moolenaar int
523b40ce416SJulian Elischer linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
524146aad74SMarcel Moolenaar {
525206a5d3aSIan Dowse 	sigset_t sigmask;
5265002a60fSMarcel Moolenaar 	l_sigset_t mask;
527146aad74SMarcel Moolenaar 
528146aad74SMarcel Moolenaar #ifdef DEBUG
52924593369SJonathan Lemon 	if (ldebug(sigsuspend))
53024593369SJonathan Lemon 		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
531146aad74SMarcel Moolenaar #endif
532146aad74SMarcel Moolenaar 
533146aad74SMarcel Moolenaar 	LINUX_SIGEMPTYSET(mask);
5344ab7403bSDmitry Chagin 	mask.__mask = args->mask;
535206a5d3aSIan Dowse 	linux_to_bsd_sigset(&mask, &sigmask);
536206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
537146aad74SMarcel Moolenaar }
538146aad74SMarcel Moolenaar 
539146aad74SMarcel Moolenaar int
540b07cd97eSMark Murray linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
541146aad74SMarcel Moolenaar {
5425002a60fSMarcel Moolenaar 	l_sigset_t lmask;
543206a5d3aSIan Dowse 	sigset_t sigmask;
544146aad74SMarcel Moolenaar 	int error;
545146aad74SMarcel Moolenaar 
546146aad74SMarcel Moolenaar #ifdef DEBUG
54724593369SJonathan Lemon 	if (ldebug(rt_sigsuspend))
54824593369SJonathan Lemon 		printf(ARGS(rt_sigsuspend, "%p, %d"),
549146aad74SMarcel Moolenaar 		    (void *)uap->newset, uap->sigsetsize);
550146aad74SMarcel Moolenaar #endif
551146aad74SMarcel Moolenaar 
5525002a60fSMarcel Moolenaar 	if (uap->sigsetsize != sizeof(l_sigset_t))
553146aad74SMarcel Moolenaar 		return (EINVAL);
554146aad74SMarcel Moolenaar 
5555002a60fSMarcel Moolenaar 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
556146aad74SMarcel Moolenaar 	if (error)
557146aad74SMarcel Moolenaar 		return (error);
558146aad74SMarcel Moolenaar 
559206a5d3aSIan Dowse 	linux_to_bsd_sigset(&lmask, &sigmask);
560206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
561146aad74SMarcel Moolenaar }
562146aad74SMarcel Moolenaar 
563146aad74SMarcel Moolenaar int
564b40ce416SJulian Elischer linux_pause(struct thread *td, struct linux_pause_args *args)
565146aad74SMarcel Moolenaar {
566b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
567206a5d3aSIan Dowse 	sigset_t sigmask;
568146aad74SMarcel Moolenaar 
569146aad74SMarcel Moolenaar #ifdef DEBUG
57024593369SJonathan Lemon 	if (ldebug(pause))
57124593369SJonathan Lemon 		printf(ARGS(pause, ""));
572146aad74SMarcel Moolenaar #endif
573146aad74SMarcel Moolenaar 
574fdfdfb78SJohn Baldwin 	PROC_LOCK(p);
5754093529dSJeff Roberson 	sigmask = td->td_sigmask;
576fdfdfb78SJohn Baldwin 	PROC_UNLOCK(p);
577206a5d3aSIan Dowse 	return (kern_sigsuspend(td, sigmask));
578146aad74SMarcel Moolenaar }
579146aad74SMarcel Moolenaar 
580146aad74SMarcel Moolenaar int
581b40ce416SJulian Elischer linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
582146aad74SMarcel Moolenaar {
583206a5d3aSIan Dowse 	stack_t ss, oss;
5845002a60fSMarcel Moolenaar 	l_stack_t lss;
585146aad74SMarcel Moolenaar 	int error;
586146aad74SMarcel Moolenaar 
587146aad74SMarcel Moolenaar #ifdef DEBUG
58824593369SJonathan Lemon 	if (ldebug(sigaltstack))
58924593369SJonathan Lemon 		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
590146aad74SMarcel Moolenaar #endif
591146aad74SMarcel Moolenaar 
592206a5d3aSIan Dowse 	if (uap->uss != NULL) {
5935002a60fSMarcel Moolenaar 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
594146aad74SMarcel Moolenaar 		if (error)
595146aad74SMarcel Moolenaar 			return (error);
596146aad74SMarcel Moolenaar 
597206a5d3aSIan Dowse 		ss.ss_sp = lss.ss_sp;
598206a5d3aSIan Dowse 		ss.ss_size = lss.ss_size;
599206a5d3aSIan Dowse 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
600931a7258SAndrew Gallatin 	}
601ef36ad69SJohn Baldwin 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
602ef36ad69SJohn Baldwin 	    (uap->uoss != NULL) ? &oss : NULL);
603206a5d3aSIan Dowse 	if (!error && uap->uoss != NULL) {
604206a5d3aSIan Dowse 		lss.ss_sp = oss.ss_sp;
605206a5d3aSIan Dowse 		lss.ss_size = oss.ss_size;
606206a5d3aSIan Dowse 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
6075002a60fSMarcel Moolenaar 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
608146aad74SMarcel Moolenaar 	}
609146aad74SMarcel Moolenaar 
610146aad74SMarcel Moolenaar 	return (error);
611146aad74SMarcel Moolenaar }
6123ad9c842SMaxim Sobolev 
6133ad9c842SMaxim Sobolev int
6143ad9c842SMaxim Sobolev linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
6153ad9c842SMaxim Sobolev {
6163ad9c842SMaxim Sobolev 
6173ad9c842SMaxim Sobolev #ifdef DEBUG
6183ad9c842SMaxim Sobolev 	if (ldebug(ftruncate64))
619b45bbfc3SBruce Evans 		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
620b45bbfc3SBruce Evans 		    (intmax_t)args->length);
6213ad9c842SMaxim Sobolev #endif
6223ad9c842SMaxim Sobolev 
623ae6b6ef6SEdward Tomasz Napierala 	return (kern_ftruncate(td, args->fd, args->length));
6243ad9c842SMaxim Sobolev }
6251bc85c0dSDoug Rabson 
6261bc85c0dSDoug Rabson int
6271bc85c0dSDoug Rabson linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
6281bc85c0dSDoug Rabson {
6299b44bfc5SAlexander Leidinger 	struct l_user_desc info;
6309b44bfc5SAlexander Leidinger 	int error;
6319b44bfc5SAlexander Leidinger 	int idx;
6329b44bfc5SAlexander Leidinger 	int a[2];
6339b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
6349b44bfc5SAlexander Leidinger 
6359b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
6369b44bfc5SAlexander Leidinger 	if (error)
6379b44bfc5SAlexander Leidinger 		return (error);
6389b44bfc5SAlexander Leidinger 
6399b44bfc5SAlexander Leidinger #ifdef DEBUG
6409b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
6419b44bfc5SAlexander Leidinger 	   	printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
6429b44bfc5SAlexander Leidinger 		      info.entry_number,
6439b44bfc5SAlexander Leidinger       		      info.base_addr,
6449b44bfc5SAlexander Leidinger       		      info.limit,
6459b44bfc5SAlexander Leidinger       		      info.seg_32bit,
6469b44bfc5SAlexander Leidinger 		      info.contents,
6479b44bfc5SAlexander Leidinger       		      info.read_exec_only,
6489b44bfc5SAlexander Leidinger       		      info.limit_in_pages,
6499b44bfc5SAlexander Leidinger       		      info.seg_not_present,
6509b44bfc5SAlexander Leidinger       		      info.useable);
6519b44bfc5SAlexander Leidinger #endif
6529b44bfc5SAlexander Leidinger 
6539b44bfc5SAlexander Leidinger 	idx = info.entry_number;
6540eef2f8aSAlexander Leidinger 	/*
655802e08a3SAlexander Leidinger 	 * Semantics of linux version: every thread in the system has array of
656802e08a3SAlexander Leidinger 	 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
657802e08a3SAlexander Leidinger 	 * syscall loads one of the selected tls decriptors with a value and
658802e08a3SAlexander Leidinger 	 * also loads GDT descriptors 6, 7 and 8 with the content of the
659802e08a3SAlexander Leidinger 	 * per-thread descriptors.
6609b44bfc5SAlexander Leidinger 	 *
661802e08a3SAlexander Leidinger 	 * Semantics of fbsd version: I think we can ignore that linux has 3
662802e08a3SAlexander Leidinger 	 * per-thread descriptors and use just the 1st one. The tls_array[]
663802e08a3SAlexander Leidinger 	 * is used only in set/get-thread_area() syscalls and for loading the
664802e08a3SAlexander Leidinger 	 * GDT descriptors. In fbsd we use just one GDT descriptor for TLS so
665802e08a3SAlexander Leidinger 	 * we will load just one.
666802e08a3SAlexander Leidinger 	 *
667802e08a3SAlexander Leidinger 	 * XXX: this doesn't work when a user space process tries to use more
668802e08a3SAlexander Leidinger 	 * than 1 TLS segment. Comment in the linux sources says wine might do
669802e08a3SAlexander Leidinger 	 * this.
6701bc85c0dSDoug Rabson 	 */
6719b44bfc5SAlexander Leidinger 
6720eef2f8aSAlexander Leidinger 	/*
6730eef2f8aSAlexander Leidinger 	 * we support just GLIBC TLS now
6749b44bfc5SAlexander Leidinger 	 * we should let 3 proceed as well because we use this segment so
6759b44bfc5SAlexander Leidinger 	 * if code does two subsequent calls it should succeed
6769b44bfc5SAlexander Leidinger 	 */
6779b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != -1 && idx != 3)
6789b44bfc5SAlexander Leidinger 		return (EINVAL);
6799b44bfc5SAlexander Leidinger 
6800eef2f8aSAlexander Leidinger 	/*
6810eef2f8aSAlexander Leidinger 	 * we have to copy out the GDT entry we use
6829b44bfc5SAlexander Leidinger 	 * FreeBSD uses GDT entry #3 for storing %gs so load that
683802e08a3SAlexander Leidinger 	 *
684802e08a3SAlexander Leidinger 	 * XXX: what if a user space program doesn't check this value and tries
6859b44bfc5SAlexander Leidinger 	 * to use 6, 7 or 8?
6869b44bfc5SAlexander Leidinger 	 */
6879b44bfc5SAlexander Leidinger 	idx = info.entry_number = 3;
6889b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
6899b44bfc5SAlexander Leidinger 	if (error)
6909b44bfc5SAlexander Leidinger 		return (error);
6919b44bfc5SAlexander Leidinger 
692a4e3bad7SJung-uk Kim 	if (LINUX_LDT_empty(&info)) {
6939b44bfc5SAlexander Leidinger 		a[0] = 0;
6949b44bfc5SAlexander Leidinger 		a[1] = 0;
6959b44bfc5SAlexander Leidinger 	} else {
696a4e3bad7SJung-uk Kim 		a[0] = LINUX_LDT_entry_a(&info);
697a4e3bad7SJung-uk Kim 		a[1] = LINUX_LDT_entry_b(&info);
6981bc85c0dSDoug Rabson 	}
6991bc85c0dSDoug Rabson 
7009b44bfc5SAlexander Leidinger 	memcpy(&sd, &a, sizeof(a));
7019b44bfc5SAlexander Leidinger #ifdef DEBUG
7029b44bfc5SAlexander Leidinger 	if (ldebug(set_thread_area))
7039b44bfc5SAlexander 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,
7049b44bfc5SAlexander Leidinger 			sd.sd_hibase,
7059b44bfc5SAlexander Leidinger 			sd.sd_lolimit,
7069b44bfc5SAlexander Leidinger 			sd.sd_hilimit,
7079b44bfc5SAlexander Leidinger 			sd.sd_type,
7089b44bfc5SAlexander Leidinger 			sd.sd_dpl,
7099b44bfc5SAlexander Leidinger 			sd.sd_p,
7109b44bfc5SAlexander Leidinger 			sd.sd_xx,
7119b44bfc5SAlexander Leidinger 			sd.sd_def32,
7129b44bfc5SAlexander Leidinger 			sd.sd_gran);
7139b44bfc5SAlexander Leidinger #endif
7141bc85c0dSDoug Rabson 
7159b44bfc5SAlexander Leidinger 	/* this is taken from i386 version of cpu_set_user_tls() */
7169b44bfc5SAlexander Leidinger 	critical_enter();
7179b44bfc5SAlexander Leidinger 	/* set %gs */
7189b44bfc5SAlexander Leidinger 	td->td_pcb->pcb_gsd = sd;
7199b44bfc5SAlexander Leidinger 	PCPU_GET(fsgs_gdt)[1] = sd;
7209b44bfc5SAlexander Leidinger 	load_gs(GSEL(GUGS_SEL, SEL_UPL));
7219b44bfc5SAlexander Leidinger 	critical_exit();
7229b44bfc5SAlexander Leidinger 
7231bc85c0dSDoug Rabson 	return (0);
7241bc85c0dSDoug Rabson }
7251bc85c0dSDoug Rabson 
7261bc85c0dSDoug Rabson int
7279b44bfc5SAlexander Leidinger linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
7281bc85c0dSDoug Rabson {
7291bc85c0dSDoug Rabson 
7309b44bfc5SAlexander Leidinger 	struct l_user_desc info;
7319b44bfc5SAlexander Leidinger 	int error;
7329b44bfc5SAlexander Leidinger 	int idx;
7339b44bfc5SAlexander Leidinger 	struct l_desc_struct desc;
7349b44bfc5SAlexander Leidinger 	struct segment_descriptor sd;
7359b44bfc5SAlexander Leidinger 
7369b44bfc5SAlexander Leidinger #ifdef DEBUG
7379b44bfc5SAlexander Leidinger 	if (ldebug(get_thread_area))
7389b44bfc5SAlexander Leidinger 		printf(ARGS(get_thread_area, "%p"), args->desc);
7399b44bfc5SAlexander Leidinger #endif
7409b44bfc5SAlexander Leidinger 
7419b44bfc5SAlexander Leidinger 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
7429b44bfc5SAlexander Leidinger 	if (error)
7439b44bfc5SAlexander Leidinger 		return (error);
7449b44bfc5SAlexander Leidinger 
7459b44bfc5SAlexander Leidinger 	idx = info.entry_number;
7469b44bfc5SAlexander Leidinger 	/* XXX: I am not sure if we want 3 to be allowed too. */
7479b44bfc5SAlexander Leidinger 	if (idx != 6 && idx != 3)
7489b44bfc5SAlexander Leidinger 		return (EINVAL);
7499b44bfc5SAlexander Leidinger 
7509b44bfc5SAlexander Leidinger 	idx = 3;
7519b44bfc5SAlexander Leidinger 
7529b44bfc5SAlexander Leidinger 	memset(&info, 0, sizeof(info));
7539b44bfc5SAlexander Leidinger 
7549b44bfc5SAlexander Leidinger 	sd = PCPU_GET(fsgs_gdt)[1];
7559b44bfc5SAlexander Leidinger 
7569b44bfc5SAlexander Leidinger 	memcpy(&desc, &sd, sizeof(desc));
7579b44bfc5SAlexander Leidinger 
7589b44bfc5SAlexander Leidinger 	info.entry_number = idx;
759a4e3bad7SJung-uk Kim 	info.base_addr = LINUX_GET_BASE(&desc);
760a4e3bad7SJung-uk Kim 	info.limit = LINUX_GET_LIMIT(&desc);
761a4e3bad7SJung-uk Kim 	info.seg_32bit = LINUX_GET_32BIT(&desc);
762a4e3bad7SJung-uk Kim 	info.contents = LINUX_GET_CONTENTS(&desc);
763a4e3bad7SJung-uk Kim 	info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
764a4e3bad7SJung-uk Kim 	info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
765a4e3bad7SJung-uk Kim 	info.seg_not_present = !LINUX_GET_PRESENT(&desc);
766a4e3bad7SJung-uk Kim 	info.useable = LINUX_GET_USEABLE(&desc);
7679b44bfc5SAlexander Leidinger 
7689b44bfc5SAlexander Leidinger 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
7699b44bfc5SAlexander Leidinger 	if (error)
7709b44bfc5SAlexander Leidinger 	   	return (EFAULT);
7719b44bfc5SAlexander Leidinger 
7729b44bfc5SAlexander Leidinger 	return (0);
7739b44bfc5SAlexander Leidinger }
7749b44bfc5SAlexander Leidinger 
7759b44bfc5SAlexander Leidinger /* XXX: this wont work with module - convert it */
7769b44bfc5SAlexander Leidinger int
7779b44bfc5SAlexander Leidinger linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
7789b44bfc5SAlexander Leidinger {
7799b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
7808451d0ddSKip Macy    	return sys_kmq_open(td, (struct kmq_open_args *) args);
7819b44bfc5SAlexander Leidinger #else
7829b44bfc5SAlexander Leidinger 	return (ENOSYS);
7839b44bfc5SAlexander Leidinger #endif
7849b44bfc5SAlexander Leidinger }
7859b44bfc5SAlexander Leidinger 
7869b44bfc5SAlexander Leidinger int
7879b44bfc5SAlexander Leidinger linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
7889b44bfc5SAlexander Leidinger {
7899b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
7908451d0ddSKip Macy    	return sys_kmq_unlink(td, (struct kmq_unlink_args *) args);
7919b44bfc5SAlexander Leidinger #else
7929b44bfc5SAlexander Leidinger 	return (ENOSYS);
7939b44bfc5SAlexander Leidinger #endif
7949b44bfc5SAlexander Leidinger }
7959b44bfc5SAlexander Leidinger 
7969b44bfc5SAlexander Leidinger int
7979b44bfc5SAlexander Leidinger linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
7989b44bfc5SAlexander Leidinger {
7999b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
8008451d0ddSKip Macy    	return sys_kmq_timedsend(td, (struct kmq_timedsend_args *) args);
8019b44bfc5SAlexander Leidinger #else
8029b44bfc5SAlexander Leidinger 	return (ENOSYS);
8039b44bfc5SAlexander Leidinger #endif
8049b44bfc5SAlexander Leidinger }
8059b44bfc5SAlexander Leidinger 
8069b44bfc5SAlexander Leidinger int
8079b44bfc5SAlexander Leidinger linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
8089b44bfc5SAlexander Leidinger {
8099b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
8108451d0ddSKip Macy    	return sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
8119b44bfc5SAlexander Leidinger #else
8129b44bfc5SAlexander Leidinger 	return (ENOSYS);
8139b44bfc5SAlexander Leidinger #endif
8149b44bfc5SAlexander Leidinger }
8159b44bfc5SAlexander Leidinger 
8169b44bfc5SAlexander Leidinger int
8179b44bfc5SAlexander Leidinger linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
8189b44bfc5SAlexander Leidinger {
8199b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
8208451d0ddSKip Macy 	return sys_kmq_notify(td, (struct kmq_notify_args *) args);
8219b44bfc5SAlexander Leidinger #else
8229b44bfc5SAlexander Leidinger 	return (ENOSYS);
8239b44bfc5SAlexander Leidinger #endif
8249b44bfc5SAlexander Leidinger }
8259b44bfc5SAlexander Leidinger 
8269b44bfc5SAlexander Leidinger int
8279b44bfc5SAlexander Leidinger linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
8289b44bfc5SAlexander Leidinger {
8299b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE
8308451d0ddSKip Macy    	return sys_kmq_setattr(td, (struct kmq_setattr_args *) args);
8319b44bfc5SAlexander Leidinger #else
8329b44bfc5SAlexander Leidinger 	return (ENOSYS);
8339b44bfc5SAlexander Leidinger #endif
8341bc85c0dSDoug Rabson }
835