linux32_machdep.c revision 168035
1133819Stjr/*-
2133819Stjr * Copyright (c) 2004 Tim J. Robbins
3133819Stjr * Copyright (c) 2002 Doug Rabson
4133819Stjr * Copyright (c) 2000 Marcel Moolenaar
5133819Stjr * All rights reserved.
6133819Stjr *
7133819Stjr * Redistribution and use in source and binary forms, with or without
8133819Stjr * modification, are permitted provided that the following conditions
9133819Stjr * are met:
10133819Stjr * 1. Redistributions of source code must retain the above copyright
11133819Stjr *    notice, this list of conditions and the following disclaimer
12133819Stjr *    in this position and unchanged.
13133819Stjr * 2. Redistributions in binary form must reproduce the above copyright
14133819Stjr *    notice, this list of conditions and the following disclaimer in the
15133819Stjr *    documentation and/or other materials provided with the distribution.
16133819Stjr * 3. The name of the author may not be used to endorse or promote products
17133819Stjr *    derived from this software without specific prior written permission.
18133819Stjr *
19133819Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20133819Stjr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21133819Stjr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22133819Stjr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23133819Stjr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24133819Stjr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25133819Stjr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26133819Stjr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27133819Stjr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28133819Stjr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29133819Stjr */
30133819Stjr
31133819Stjr#include <sys/cdefs.h>
32133819Stjr__FBSDID("$FreeBSD: head/sys/amd64/linux32/linux32_machdep.c 168035 2007-03-30 00:06:21Z jkim $");
33133819Stjr
34133819Stjr#include <sys/param.h>
35133819Stjr#include <sys/kernel.h>
36133819Stjr#include <sys/systm.h>
37165832Snetchild#include <sys/file.h>
38165832Snetchild#include <sys/fcntl.h>
39162954Sphk#include <sys/clock.h>
40142057Sjhb#include <sys/imgact.h>
41161310Snetchild#include <sys/limits.h>
42133819Stjr#include <sys/lock.h>
43133819Stjr#include <sys/malloc.h>
44133819Stjr#include <sys/mman.h>
45133819Stjr#include <sys/mutex.h>
46166729Sjkim#include <sys/priv.h>
47133819Stjr#include <sys/proc.h>
48133819Stjr#include <sys/resource.h>
49133819Stjr#include <sys/resourcevar.h>
50166188Sjeff#include <sys/sched.h>
51133819Stjr#include <sys/syscallsubr.h>
52133819Stjr#include <sys/sysproto.h>
53133819Stjr#include <sys/unistd.h>
54133819Stjr
55133819Stjr#include <machine/frame.h>
56168035Sjkim#include <machine/pcb.h>
57166729Sjkim#include <machine/psl.h>
58168035Sjkim#include <machine/segments.h>
59168035Sjkim#include <machine/specialreg.h>
60133819Stjr
61133819Stjr#include <vm/vm.h>
62133819Stjr#include <vm/pmap.h>
63142057Sjhb#include <vm/vm_extern.h>
64142057Sjhb#include <vm/vm_kern.h>
65133819Stjr#include <vm/vm_map.h>
66133819Stjr
67133819Stjr#include <amd64/linux32/linux.h>
68133819Stjr#include <amd64/linux32/linux32_proto.h>
69133819Stjr#include <compat/linux/linux_ipc.h>
70133819Stjr#include <compat/linux/linux_signal.h>
71133819Stjr#include <compat/linux/linux_util.h>
72161474Snetchild#include <compat/linux/linux_emul.h>
73133819Stjr
74133819Stjrstruct l_old_select_argv {
75133819Stjr	l_int		nfds;
76133819Stjr	l_uintptr_t	readfds;
77133819Stjr	l_uintptr_t	writefds;
78133819Stjr	l_uintptr_t	exceptfds;
79133819Stjr	l_uintptr_t	timeout;
80133819Stjr} __packed;
81133819Stjr
82133819Stjrint
83133819Stjrlinux_to_bsd_sigaltstack(int lsa)
84133819Stjr{
85133819Stjr	int bsa = 0;
86133819Stjr
87133819Stjr	if (lsa & LINUX_SS_DISABLE)
88133819Stjr		bsa |= SS_DISABLE;
89133819Stjr	if (lsa & LINUX_SS_ONSTACK)
90133819Stjr		bsa |= SS_ONSTACK;
91133819Stjr	return (bsa);
92133819Stjr}
93133819Stjr
94133819Stjrint
95133819Stjrbsd_to_linux_sigaltstack(int bsa)
96133819Stjr{
97133819Stjr	int lsa = 0;
98133819Stjr
99133819Stjr	if (bsa & SS_DISABLE)
100133819Stjr		lsa |= LINUX_SS_DISABLE;
101133819Stjr	if (bsa & SS_ONSTACK)
102133819Stjr		lsa |= LINUX_SS_ONSTACK;
103133819Stjr	return (lsa);
104133819Stjr}
105133819Stjr
106142057Sjhb/*
107142057Sjhb * Custom version of exec_copyin_args() so that we can translate
108142057Sjhb * the pointers.
109142057Sjhb */
110142057Sjhbstatic int
111142057Sjhblinux_exec_copyin_args(struct image_args *args, char *fname,
112142057Sjhb    enum uio_seg segflg, char **argv, char **envv)
113133819Stjr{
114142057Sjhb	char *argp, *envp;
115142057Sjhb	u_int32_t *p32, arg;
116142057Sjhb	size_t length;
117133819Stjr	int error;
118133819Stjr
119142057Sjhb	bzero(args, sizeof(*args));
120142057Sjhb	if (argv == NULL)
121142057Sjhb		return (EFAULT);
122133819Stjr
123142057Sjhb	/*
124142057Sjhb	 * Allocate temporary demand zeroed space for argument and
125142057Sjhb	 *	environment strings
126142057Sjhb	 */
127147588Sjhb	args->buf = (char *) kmem_alloc_wait(exec_map,
128147588Sjhb	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
129142057Sjhb	if (args->buf == NULL)
130142057Sjhb		return (ENOMEM);
131142057Sjhb	args->begin_argv = args->buf;
132142057Sjhb	args->endp = args->begin_argv;
133142057Sjhb	args->stringspace = ARG_MAX;
134133819Stjr
135142057Sjhb	args->fname = args->buf + ARG_MAX;
136133819Stjr
137142057Sjhb	/*
138142057Sjhb	 * Copy the file name.
139142057Sjhb	 */
140142057Sjhb	error = (segflg == UIO_SYSSPACE) ?
141142057Sjhb	    copystr(fname, args->fname, PATH_MAX, &length) :
142142057Sjhb	    copyinstr(fname, args->fname, PATH_MAX, &length);
143142057Sjhb	if (error != 0)
144156440Sups		goto err_exit;
145142057Sjhb
146142057Sjhb	/*
147142057Sjhb	 * extract arguments first
148142057Sjhb	 */
149142057Sjhb	p32 = (u_int32_t *)argv;
150142057Sjhb	for (;;) {
151142057Sjhb		error = copyin(p32++, &arg, sizeof(arg));
152142057Sjhb		if (error)
153156440Sups			goto err_exit;
154142057Sjhb		if (arg == 0)
155142057Sjhb			break;
156142057Sjhb		argp = PTRIN(arg);
157142057Sjhb		error = copyinstr(argp, args->endp, args->stringspace, &length);
158142057Sjhb		if (error) {
159142057Sjhb			if (error == ENAMETOOLONG)
160156440Sups				error = E2BIG;
161156440Sups
162156440Sups			goto err_exit;
163142057Sjhb		}
164142057Sjhb		args->stringspace -= length;
165142057Sjhb		args->endp += length;
166142057Sjhb		args->argc++;
167133819Stjr	}
168142057Sjhb
169142057Sjhb	args->begin_envv = args->endp;
170142057Sjhb
171142057Sjhb	/*
172142057Sjhb	 * extract environment strings
173142057Sjhb	 */
174142057Sjhb	if (envv) {
175142057Sjhb		p32 = (u_int32_t *)envv;
176142057Sjhb		for (;;) {
177133819Stjr			error = copyin(p32++, &arg, sizeof(arg));
178133819Stjr			if (error)
179156440Sups				goto err_exit;
180142057Sjhb			if (arg == 0)
181142057Sjhb				break;
182142057Sjhb			envp = PTRIN(arg);
183142057Sjhb			error = copyinstr(envp, args->endp, args->stringspace,
184142057Sjhb			    &length);
185142057Sjhb			if (error) {
186142057Sjhb				if (error == ENAMETOOLONG)
187156440Sups					error = E2BIG;
188156440Sups				goto err_exit;
189142057Sjhb			}
190142057Sjhb			args->stringspace -= length;
191142057Sjhb			args->endp += length;
192142057Sjhb			args->envc++;
193142057Sjhb		}
194133819Stjr	}
195133819Stjr
196142057Sjhb	return (0);
197156440Sups
198156440Supserr_exit:
199156440Sups	kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
200156440Sups	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
201156440Sups	args->buf = NULL;
202156440Sups	return (error);
203133819Stjr}
204133819Stjr
205142057Sjhbint
206142057Sjhblinux_execve(struct thread *td, struct linux_execve_args *args)
207142057Sjhb{
208142057Sjhb	struct image_args eargs;
209142057Sjhb	char *path;
210142057Sjhb	int error;
211142057Sjhb
212142057Sjhb	LCONVPATHEXIST(td, args->path, &path);
213142057Sjhb
214142057Sjhb#ifdef DEBUG
215142057Sjhb	if (ldebug(execve))
216142057Sjhb		printf(ARGS(execve, "%s"), path);
217142057Sjhb#endif
218142057Sjhb
219142057Sjhb	error = linux_exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
220142057Sjhb	    args->envp);
221142057Sjhb	free(path, M_TEMP);
222142057Sjhb	if (error == 0)
223142057Sjhb		error = kern_execve(td, &eargs, NULL);
224161474Snetchild	if (error == 0)
225161474Snetchild	   	/* linux process can exec fbsd one, dont attempt
226161474Snetchild		 * to create emuldata for such process using
227161474Snetchild		 * linux_proc_init, this leads to a panic on KASSERT
228161474Snetchild		 * because such process has p->p_emuldata == NULL
229161474Snetchild		 */
230161474Snetchild	   	if (td->td_proc->p_sysent == &elf_linux_sysvec)
231161474Snetchild   		   	error = linux_proc_init(td, 0, 0);
232142057Sjhb	return (error);
233142057Sjhb}
234142057Sjhb
235133819Stjrstruct iovec32 {
236133819Stjr	u_int32_t iov_base;
237133819Stjr	int	iov_len;
238133819Stjr};
239133819Stjr
240133819StjrCTASSERT(sizeof(struct iovec32) == 8);
241133819Stjr
242144449Sjhbstatic int
243144449Sjhblinux32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
244133819Stjr{
245144449Sjhb	struct iovec32 iov32;
246144449Sjhb	struct iovec *iov;
247144449Sjhb	struct uio *uio;
248144449Sjhb	u_int iovlen;
249144449Sjhb	int error, i;
250133819Stjr
251144449Sjhb	*uiop = NULL;
252144449Sjhb	if (iovcnt > UIO_MAXIOV)
253133819Stjr		return (EINVAL);
254144449Sjhb	iovlen = iovcnt * sizeof(struct iovec);
255144449Sjhb	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
256144449Sjhb	iov = (struct iovec *)(uio + 1);
257144449Sjhb	for (i = 0; i < iovcnt; i++) {
258144449Sjhb		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
259144449Sjhb		if (error) {
260144449Sjhb			free(uio, M_IOV);
261144449Sjhb			return (error);
262144449Sjhb		}
263144449Sjhb		iov[i].iov_base = PTRIN(iov32.iov_base);
264144449Sjhb		iov[i].iov_len = iov32.iov_len;
265133819Stjr	}
266144449Sjhb	uio->uio_iov = iov;
267144449Sjhb	uio->uio_iovcnt = iovcnt;
268144449Sjhb	uio->uio_segflg = UIO_USERSPACE;
269144449Sjhb	uio->uio_offset = -1;
270144449Sjhb	uio->uio_resid = 0;
271144449Sjhb	for (i = 0; i < iovcnt; i++) {
272144449Sjhb		if (iov->iov_len > INT_MAX - uio->uio_resid) {
273144449Sjhb			free(uio, M_IOV);
274144449Sjhb			return (EINVAL);
275144449Sjhb		}
276144449Sjhb		uio->uio_resid += iov->iov_len;
277144449Sjhb		iov++;
278144449Sjhb	}
279144449Sjhb	*uiop = uio;
280144449Sjhb	return (0);
281144449Sjhb}
282133819Stjr
283144449Sjhbint
284144449Sjhblinux_readv(struct thread *td, struct linux_readv_args *uap)
285144449Sjhb{
286144449Sjhb	struct uio *auio;
287144449Sjhb	int error;
288133819Stjr
289144449Sjhb	error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
290144449Sjhb	if (error)
291144449Sjhb		return (error);
292144449Sjhb	error = kern_readv(td, uap->fd, auio);
293144449Sjhb	free(auio, M_IOV);
294133819Stjr	return (error);
295133819Stjr}
296133819Stjr
297133819Stjrint
298133819Stjrlinux_writev(struct thread *td, struct linux_writev_args *uap)
299133819Stjr{
300144449Sjhb	struct uio *auio;
301144449Sjhb	int error;
302133819Stjr
303144449Sjhb	error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
304144449Sjhb	if (error)
305144449Sjhb		return (error);
306144449Sjhb	error = kern_writev(td, uap->fd, auio);
307144449Sjhb	free(auio, M_IOV);
308133819Stjr	return (error);
309133819Stjr}
310133819Stjr
311133819Stjrstruct l_ipc_kludge {
312133819Stjr	l_uintptr_t msgp;
313133819Stjr	l_long msgtyp;
314133819Stjr} __packed;
315133819Stjr
316133819Stjrint
317133819Stjrlinux_ipc(struct thread *td, struct linux_ipc_args *args)
318133819Stjr{
319133819Stjr
320133819Stjr	switch (args->what & 0xFFFF) {
321133819Stjr	case LINUX_SEMOP: {
322133819Stjr		struct linux_semop_args a;
323133819Stjr
324133819Stjr		a.semid = args->arg1;
325133819Stjr		a.tsops = args->ptr;
326133819Stjr		a.nsops = args->arg2;
327133819Stjr		return (linux_semop(td, &a));
328133819Stjr	}
329133819Stjr	case LINUX_SEMGET: {
330133819Stjr		struct linux_semget_args a;
331133819Stjr
332133819Stjr		a.key = args->arg1;
333133819Stjr		a.nsems = args->arg2;
334133819Stjr		a.semflg = args->arg3;
335133819Stjr		return (linux_semget(td, &a));
336133819Stjr	}
337133819Stjr	case LINUX_SEMCTL: {
338133819Stjr		struct linux_semctl_args a;
339133819Stjr		int error;
340133819Stjr
341133819Stjr		a.semid = args->arg1;
342133819Stjr		a.semnum = args->arg2;
343133819Stjr		a.cmd = args->arg3;
344133819Stjr		error = copyin(args->ptr, &a.arg, sizeof(a.arg));
345133819Stjr		if (error)
346133819Stjr			return (error);
347133819Stjr		return (linux_semctl(td, &a));
348133819Stjr	}
349133819Stjr	case LINUX_MSGSND: {
350133819Stjr		struct linux_msgsnd_args a;
351133819Stjr
352133819Stjr		a.msqid = args->arg1;
353133819Stjr		a.msgp = args->ptr;
354133819Stjr		a.msgsz = args->arg2;
355133819Stjr		a.msgflg = args->arg3;
356133819Stjr		return (linux_msgsnd(td, &a));
357133819Stjr	}
358133819Stjr	case LINUX_MSGRCV: {
359133819Stjr		struct linux_msgrcv_args a;
360133819Stjr
361133819Stjr		a.msqid = args->arg1;
362133819Stjr		a.msgsz = args->arg2;
363133819Stjr		a.msgflg = args->arg3;
364133819Stjr		if ((args->what >> 16) == 0) {
365133819Stjr			struct l_ipc_kludge tmp;
366133819Stjr			int error;
367133819Stjr
368133819Stjr			if (args->ptr == 0)
369133819Stjr				return (EINVAL);
370133819Stjr			error = copyin(args->ptr, &tmp, sizeof(tmp));
371133819Stjr			if (error)
372133819Stjr				return (error);
373133819Stjr			a.msgp = PTRIN(tmp.msgp);
374133819Stjr			a.msgtyp = tmp.msgtyp;
375133819Stjr		} else {
376133819Stjr			a.msgp = args->ptr;
377133819Stjr			a.msgtyp = args->arg5;
378133819Stjr		}
379133819Stjr		return (linux_msgrcv(td, &a));
380133819Stjr	}
381133819Stjr	case LINUX_MSGGET: {
382133819Stjr		struct linux_msgget_args a;
383133819Stjr
384133819Stjr		a.key = args->arg1;
385133819Stjr		a.msgflg = args->arg2;
386133819Stjr		return (linux_msgget(td, &a));
387133819Stjr	}
388133819Stjr	case LINUX_MSGCTL: {
389133819Stjr		struct linux_msgctl_args a;
390133819Stjr
391133819Stjr		a.msqid = args->arg1;
392133819Stjr		a.cmd = args->arg2;
393133819Stjr		a.buf = args->ptr;
394133819Stjr		return (linux_msgctl(td, &a));
395133819Stjr	}
396133819Stjr	case LINUX_SHMAT: {
397133819Stjr		struct linux_shmat_args a;
398133819Stjr
399133819Stjr		a.shmid = args->arg1;
400133819Stjr		a.shmaddr = args->ptr;
401133819Stjr		a.shmflg = args->arg2;
402144441Sjhb		a.raddr = PTRIN((l_uint)args->arg3);
403133819Stjr		return (linux_shmat(td, &a));
404133819Stjr	}
405133819Stjr	case LINUX_SHMDT: {
406133819Stjr		struct linux_shmdt_args a;
407133819Stjr
408133819Stjr		a.shmaddr = args->ptr;
409133819Stjr		return (linux_shmdt(td, &a));
410133819Stjr	}
411133819Stjr	case LINUX_SHMGET: {
412133819Stjr		struct linux_shmget_args a;
413133819Stjr
414133819Stjr		a.key = args->arg1;
415133819Stjr		a.size = args->arg2;
416133819Stjr		a.shmflg = args->arg3;
417133819Stjr		return (linux_shmget(td, &a));
418133819Stjr	}
419133819Stjr	case LINUX_SHMCTL: {
420133819Stjr		struct linux_shmctl_args a;
421133819Stjr
422133819Stjr		a.shmid = args->arg1;
423133819Stjr		a.cmd = args->arg2;
424133819Stjr		a.buf = args->ptr;
425133819Stjr		return (linux_shmctl(td, &a));
426133819Stjr	}
427133819Stjr	default:
428133819Stjr		break;
429133819Stjr	}
430133819Stjr
431133819Stjr	return (EINVAL);
432133819Stjr}
433133819Stjr
434133819Stjrint
435133819Stjrlinux_old_select(struct thread *td, struct linux_old_select_args *args)
436133819Stjr{
437133819Stjr	struct l_old_select_argv linux_args;
438133819Stjr	struct linux_select_args newsel;
439133819Stjr	int error;
440133819Stjr
441133819Stjr#ifdef DEBUG
442133819Stjr	if (ldebug(old_select))
443133819Stjr		printf(ARGS(old_select, "%p"), args->ptr);
444133819Stjr#endif
445133819Stjr
446133819Stjr	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
447133819Stjr	if (error)
448133819Stjr		return (error);
449133819Stjr
450133819Stjr	newsel.nfds = linux_args.nfds;
451133819Stjr	newsel.readfds = PTRIN(linux_args.readfds);
452133819Stjr	newsel.writefds = PTRIN(linux_args.writefds);
453133819Stjr	newsel.exceptfds = PTRIN(linux_args.exceptfds);
454133819Stjr	newsel.timeout = PTRIN(linux_args.timeout);
455133819Stjr	return (linux_select(td, &newsel));
456133819Stjr}
457133819Stjr
458133819Stjrint
459133819Stjrlinux_fork(struct thread *td, struct linux_fork_args *args)
460133819Stjr{
461133819Stjr	int error;
462166150Snetchild	struct proc *p2;
463166150Snetchild	struct thread *td2;
464133819Stjr
465133819Stjr#ifdef DEBUG
466133819Stjr	if (ldebug(fork))
467133819Stjr		printf(ARGS(fork, ""));
468133819Stjr#endif
469133819Stjr
470166150Snetchild	if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0)
471133819Stjr		return (error);
472166150Snetchild
473166150Snetchild	if (error == 0) {
474166150Snetchild		td->td_retval[0] = p2->p_pid;
475166150Snetchild		td->td_retval[1] = 0;
476166150Snetchild	}
477133819Stjr
478133819Stjr	if (td->td_retval[1] == 1)
479133819Stjr		td->td_retval[0] = 0;
480161474Snetchild	error = linux_proc_init(td, td->td_retval[0], 0);
481161474Snetchild	if (error)
482161474Snetchild		return (error);
483161474Snetchild
484166150Snetchild	td2 = FIRST_THREAD_IN_PROC(p2);
485166150Snetchild
486166150Snetchild	/* make it run */
487166150Snetchild	mtx_lock_spin(&sched_lock);
488166150Snetchild	TD_SET_CAN_RUN(td2);
489166188Sjeff	sched_add(td2, SRQ_BORING);
490166150Snetchild	mtx_unlock_spin(&sched_lock);
491166150Snetchild
492133819Stjr	return (0);
493133819Stjr}
494133819Stjr
495133819Stjrint
496133819Stjrlinux_vfork(struct thread *td, struct linux_vfork_args *args)
497133819Stjr{
498133819Stjr	int error;
499161611Snetchild	struct proc *p2;
500166150Snetchild	struct thread *td2;
501133819Stjr
502133819Stjr#ifdef DEBUG
503133819Stjr	if (ldebug(vfork))
504133819Stjr		printf(ARGS(vfork, ""));
505133819Stjr#endif
506133819Stjr
507161611Snetchild	/* exclude RFPPWAIT */
508166150Snetchild	if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0)
509133819Stjr		return (error);
510161611Snetchild	if (error == 0) {
511161611Snetchild	   	td->td_retval[0] = p2->p_pid;
512161611Snetchild		td->td_retval[1] = 0;
513161611Snetchild	}
514133819Stjr	/* Are we the child? */
515133819Stjr	if (td->td_retval[1] == 1)
516133819Stjr		td->td_retval[0] = 0;
517161474Snetchild	error = linux_proc_init(td, td->td_retval[0], 0);
518161474Snetchild	if (error)
519161474Snetchild		return (error);
520166150Snetchild
521166150Snetchild	PROC_LOCK(p2);
522166150Snetchild	p2->p_flag |= P_PPWAIT;
523166150Snetchild	PROC_UNLOCK(p2);
524166150Snetchild
525166150Snetchild	td2 = FIRST_THREAD_IN_PROC(p2);
526166150Snetchild
527166150Snetchild	/* make it run */
528166150Snetchild	mtx_lock_spin(&sched_lock);
529166150Snetchild	TD_SET_CAN_RUN(td2);
530166188Sjeff	sched_add(td2, SRQ_BORING);
531166150Snetchild	mtx_unlock_spin(&sched_lock);
532166150Snetchild
533161611Snetchild	/* wait for the children to exit, ie. emulate vfork */
534161611Snetchild	PROC_LOCK(p2);
535161611Snetchild	while (p2->p_flag & P_PPWAIT)
536161611Snetchild	   	msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0);
537161611Snetchild	PROC_UNLOCK(p2);
538166150Snetchild
539133819Stjr	return (0);
540133819Stjr}
541133819Stjr
542133819Stjrint
543133819Stjrlinux_clone(struct thread *td, struct linux_clone_args *args)
544133819Stjr{
545133819Stjr	int error, ff = RFPROC | RFSTOPPED;
546133819Stjr	struct proc *p2;
547133819Stjr	struct thread *td2;
548133819Stjr	int exit_signal;
549161474Snetchild	struct linux_emuldata *em;
550133819Stjr
551133819Stjr#ifdef DEBUG
552133819Stjr	if (ldebug(clone)) {
553161474Snetchild   	   	printf(ARGS(clone, "flags %x, stack %x, parent tid: %x, child tid: %x"),
554161474Snetchild		    (unsigned int)args->flags, (unsigned int)(uintptr_t)args->stack,
555161474Snetchild		    (unsigned int)(uintptr_t)args->parent_tidptr,
556161474Snetchild		    (unsigned int)(uintptr_t)args->child_tidptr);
557133819Stjr	}
558133819Stjr#endif
559133819Stjr
560133819Stjr	exit_signal = args->flags & 0x000000ff;
561166394Skib	if (!LINUX_SIG_VALID(exit_signal) && exit_signal != 0)
562133819Stjr		return (EINVAL);
563133819Stjr
564133819Stjr	if (exit_signal <= LINUX_SIGTBLSZ)
565133819Stjr		exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
566133819Stjr
567167157Sjkim	if (args->flags & LINUX_CLONE_VM)
568133819Stjr		ff |= RFMEM;
569167157Sjkim	if (args->flags & LINUX_CLONE_SIGHAND)
570133819Stjr		ff |= RFSIGSHARE;
571163371Snetchild	/*
572163371Snetchild	 * XXX: in linux sharing of fs info (chroot/cwd/umask)
573163371Snetchild	 * and open files is independant. in fbsd its in one
574166944Snetchild	 * structure but in reality it doesn't cause any problems
575166944Snetchild	 * because both of these flags are usually set together.
576163371Snetchild	 */
577167157Sjkim	if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
578133819Stjr		ff |= RFFDG;
579133819Stjr
580161474Snetchild	/*
581161474Snetchild	 * Attempt to detect when linux_clone(2) is used for creating
582161474Snetchild	 * kernel threads. Unfortunately despite the existence of the
583161474Snetchild	 * CLONE_THREAD flag, version of linuxthreads package used in
584161474Snetchild	 * most popular distros as of beginning of 2005 doesn't make
585166944Snetchild	 * any use of it. Therefore, this detection relies on
586161474Snetchild	 * empirical observation that linuxthreads sets certain
587161474Snetchild	 * combination of flags, so that we can make more or less
588161474Snetchild	 * precise detection and notify the FreeBSD kernel that several
589161474Snetchild	 * processes are in fact part of the same threading group, so
590161474Snetchild	 * that special treatment is necessary for signal delivery
591161474Snetchild	 * between those processes and fd locking.
592161474Snetchild	 */
593167157Sjkim	if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
594161474Snetchild		ff |= RFTHREAD;
595161474Snetchild
596133819Stjr	error = fork1(td, ff, 0, &p2);
597133819Stjr	if (error)
598133819Stjr		return (error);
599166395Skib
600167157Sjkim	if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
601166395Skib	   	sx_xlock(&proctree_lock);
602166395Skib		PROC_LOCK(p2);
603166395Skib		proc_reparent(p2, td->td_proc->p_pptr);
604166395Skib		PROC_UNLOCK(p2);
605166395Skib		sx_xunlock(&proctree_lock);
606166395Skib	}
607133819Stjr
608161474Snetchild	/* create the emuldata */
609161474Snetchild	error = linux_proc_init(td, p2->p_pid, args->flags);
610161474Snetchild	/* reference it - no need to check this */
611165867Snetchild	em = em_find(p2, EMUL_DOLOCK);
612161474Snetchild	KASSERT(em != NULL, ("clone: emuldata not found.\n"));
613161474Snetchild	/* and adjust it */
614167157Sjkim	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
615161474Snetchild	   	if (args->parent_tidptr == NULL) {
616161474Snetchild		   	EMUL_UNLOCK(&emul_lock);
617161474Snetchild			return (EINVAL);
618161474Snetchild		}
619161474Snetchild		error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid));
620161474Snetchild		if (error) {
621161474Snetchild		   	EMUL_UNLOCK(&emul_lock);
622161474Snetchild			return (error);
623161474Snetchild		}
624161474Snetchild	}
625133819Stjr
626167157Sjkim	if (args->flags & LINUX_CLONE_THREAD) {
627161474Snetchild	   	/* XXX: linux mangles pgrp and pptr somehow
628161474Snetchild		 * I think it might be this but I am not sure.
629161474Snetchild		 */
630161474Snetchild#ifdef notyet
631161696Snetchild	   	PROC_LOCK(p2);
632161474Snetchild	   	p2->p_pgrp = td->td_proc->p_pgrp;
633161696Snetchild	   	PROC_UNLOCK(p2);
634161474Snetchild#endif
635161474Snetchild	 	exit_signal = 0;
636161474Snetchild	}
637161474Snetchild
638167157Sjkim	if (args->flags & LINUX_CLONE_CHILD_SETTID)
639161474Snetchild		em->child_set_tid = args->child_tidptr;
640161474Snetchild	else
641161474Snetchild	   	em->child_set_tid = NULL;
642161474Snetchild
643167157Sjkim	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
644161474Snetchild		em->child_clear_tid = args->child_tidptr;
645161474Snetchild	else
646161474Snetchild	   	em->child_clear_tid = NULL;
647161696Snetchild
648161474Snetchild	EMUL_UNLOCK(&emul_lock);
649161474Snetchild
650133819Stjr	PROC_LOCK(p2);
651133819Stjr	p2->p_sigparent = exit_signal;
652133819Stjr	PROC_UNLOCK(p2);
653133819Stjr	td2 = FIRST_THREAD_IN_PROC(p2);
654161365Snetchild	/*
655161365Snetchild	 * in a case of stack = NULL we are supposed to COW calling process stack
656161310Snetchild	 * this is what normal fork() does so we just keep the tf_rsp arg intact
657161310Snetchild	 */
658161310Snetchild	if (args->stack)
659161310Snetchild   	   	td2->td_frame->tf_rsp = PTROUT(args->stack);
660133819Stjr
661167157Sjkim	if (args->flags & LINUX_CLONE_SETTLS) {
662168035Sjkim		struct user_segment_descriptor sd;
663168035Sjkim		struct l_user_desc info;
664168035Sjkim	   	int a[2];
665168035Sjkim
666168035Sjkim	   	error = copyin((void *)td->td_frame->tf_rsi, &info,
667168035Sjkim		    sizeof(struct l_user_desc));
668168035Sjkim		if (error) {
669168035Sjkim			printf(LMSG("copyin failed!"));
670168035Sjkim		} else {
671168035Sjkim			/* We might copy out the entry_number as GUGS32_SEL. */
672168035Sjkim		   	info.entry_number = GUGS32_SEL;
673168035Sjkim			error = copyout(&info, (void *)td->td_frame->tf_rsi,
674168035Sjkim			    sizeof(struct l_user_desc));
675168035Sjkim			if (error)
676168035Sjkim				printf(LMSG("copyout failed!"));
677168035Sjkim
678168035Sjkim			a[0] = LINUX_LDT_entry_a(&info);
679168035Sjkim			a[1] = LINUX_LDT_entry_b(&info);
680168035Sjkim
681168035Sjkim			memcpy(&sd, &a, sizeof(a));
682168035Sjkim#ifdef DEBUG
683168035Sjkim			if (ldebug(clone))
684168035Sjkim				printf("Segment created in clone with "
685168035Sjkim				    "CLONE_SETTLS: lobase: %x, hibase: %x, "
686168035Sjkim				    "lolimit: %x, hilimit: %x, type: %i, "
687168035Sjkim				    "dpl: %i, p: %i, xx: %i, long: %i, "
688168035Sjkim				    "def32: %i, gran: %i\n", sd.sd_lobase,
689168035Sjkim				    sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit,
690168035Sjkim				    sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
691168035Sjkim				    sd.sd_long, sd.sd_def32, sd.sd_gran);
692168035Sjkim#endif
693168035Sjkim			td2->td_pcb->pcb_gsbase = (register_t)info.base_addr;
694168035Sjkim			td2->td_pcb->pcb_gs32sd = sd;
695168035Sjkim			td2->td_pcb->pcb_gs32p = &gdt[GUGS32_SEL];
696168035Sjkim			td2->td_pcb->pcb_gs = GSEL(GUGS32_SEL, SEL_UPL);
697168035Sjkim			td2->td_pcb->pcb_flags |= PCB_32BIT;
698168035Sjkim		}
699161474Snetchild	}
700161474Snetchild
701133819Stjr#ifdef DEBUG
702133819Stjr	if (ldebug(clone))
703133819Stjr		printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
704133819Stjr		    (long)p2->p_pid, args->stack, exit_signal);
705133819Stjr#endif
706167157Sjkim	if (args->flags & LINUX_CLONE_VFORK) {
707166150Snetchild	   	PROC_LOCK(p2);
708166150Snetchild	   	p2->p_flag |= P_PPWAIT;
709166150Snetchild	   	PROC_UNLOCK(p2);
710166150Snetchild	}
711133819Stjr
712133819Stjr	/*
713133819Stjr	 * Make this runnable after we are finished with it.
714133819Stjr	 */
715133819Stjr	mtx_lock_spin(&sched_lock);
716133819Stjr	TD_SET_CAN_RUN(td2);
717166188Sjeff	sched_add(td2, SRQ_BORING);
718133819Stjr	mtx_unlock_spin(&sched_lock);
719133819Stjr
720133819Stjr	td->td_retval[0] = p2->p_pid;
721133819Stjr	td->td_retval[1] = 0;
722163374Snetchild
723167157Sjkim	if (args->flags & LINUX_CLONE_VFORK) {
724163374Snetchild   	   	/* wait for the children to exit, ie. emulate vfork */
725163374Snetchild   	   	PROC_LOCK(p2);
726163374Snetchild		while (p2->p_flag & P_PPWAIT)
727163374Snetchild   		   	msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0);
728163374Snetchild		PROC_UNLOCK(p2);
729163374Snetchild	}
730163374Snetchild
731133819Stjr	return (0);
732133819Stjr}
733133819Stjr
734133819Stjr#define STACK_SIZE  (2 * 1024 * 1024)
735133819Stjr#define GUARD_SIZE  (4 * PAGE_SIZE)
736133819Stjr
737133819Stjrstatic int linux_mmap_common(struct thread *, struct l_mmap_argv *);
738133819Stjr
739133819Stjrint
740133819Stjrlinux_mmap2(struct thread *td, struct linux_mmap2_args *args)
741133819Stjr{
742133819Stjr	struct l_mmap_argv linux_args;
743133819Stjr
744133819Stjr#ifdef DEBUG
745133819Stjr	if (ldebug(mmap2))
746133819Stjr		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
747133843Sobrien		    (void *)(intptr_t)args->addr, args->len, args->prot,
748133819Stjr		    args->flags, args->fd, args->pgoff);
749133819Stjr#endif
750133819Stjr
751133819Stjr	linux_args.addr = PTROUT(args->addr);
752133819Stjr	linux_args.len = args->len;
753133819Stjr	linux_args.prot = args->prot;
754133819Stjr	linux_args.flags = args->flags;
755133819Stjr	linux_args.fd = args->fd;
756144441Sjhb	linux_args.pgoff = args->pgoff;
757133819Stjr
758133819Stjr	return (linux_mmap_common(td, &linux_args));
759133819Stjr}
760133819Stjr
761133819Stjrint
762133819Stjrlinux_mmap(struct thread *td, struct linux_mmap_args *args)
763133819Stjr{
764133819Stjr	int error;
765133819Stjr	struct l_mmap_argv linux_args;
766133819Stjr
767133819Stjr	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
768133819Stjr	if (error)
769133819Stjr		return (error);
770133819Stjr
771133819Stjr#ifdef DEBUG
772133819Stjr	if (ldebug(mmap))
773133819Stjr		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
774133843Sobrien		    (void *)(intptr_t)linux_args.addr, linux_args.len,
775133843Sobrien		    linux_args.prot, linux_args.flags, linux_args.fd,
776144670Sjhb		    linux_args.pgoff);
777133819Stjr#endif
778144441Sjhb	if ((linux_args.pgoff % PAGE_SIZE) != 0)
779144441Sjhb		return (EINVAL);
780144441Sjhb	linux_args.pgoff /= PAGE_SIZE;
781133819Stjr
782133819Stjr	return (linux_mmap_common(td, &linux_args));
783133819Stjr}
784133819Stjr
785133819Stjrstatic int
786133819Stjrlinux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
787133819Stjr{
788133819Stjr	struct proc *p = td->td_proc;
789133819Stjr	struct mmap_args /* {
790133819Stjr		caddr_t addr;
791133819Stjr		size_t len;
792133819Stjr		int prot;
793133819Stjr		int flags;
794133819Stjr		int fd;
795133819Stjr		long pad;
796133819Stjr		off_t pos;
797133819Stjr	} */ bsd_args;
798133819Stjr	int error;
799165832Snetchild	struct file *fp;
800133819Stjr
801133819Stjr	error = 0;
802133819Stjr	bsd_args.flags = 0;
803165832Snetchild	fp = NULL;
804165832Snetchild
805165832Snetchild	/*
806165832Snetchild	 * Linux mmap(2):
807165832Snetchild	 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
808165832Snetchild	 */
809165832Snetchild	if (! ((linux_args->flags & LINUX_MAP_SHARED) ^
810165832Snetchild	    (linux_args->flags & LINUX_MAP_PRIVATE)))
811165832Snetchild		return (EINVAL);
812165832Snetchild
813133819Stjr	if (linux_args->flags & LINUX_MAP_SHARED)
814133819Stjr		bsd_args.flags |= MAP_SHARED;
815133819Stjr	if (linux_args->flags & LINUX_MAP_PRIVATE)
816133819Stjr		bsd_args.flags |= MAP_PRIVATE;
817133819Stjr	if (linux_args->flags & LINUX_MAP_FIXED)
818133819Stjr		bsd_args.flags |= MAP_FIXED;
819133819Stjr	if (linux_args->flags & LINUX_MAP_ANON)
820133819Stjr		bsd_args.flags |= MAP_ANON;
821133819Stjr	else
822133819Stjr		bsd_args.flags |= MAP_NOSYNC;
823166727Sjkim	if (linux_args->flags & LINUX_MAP_GROWSDOWN)
824133819Stjr		bsd_args.flags |= MAP_STACK;
825133819Stjr
826166727Sjkim	/*
827166727Sjkim	 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
828166727Sjkim	 * on Linux/i386. We do this to ensure maximum compatibility.
829166727Sjkim	 * Linux/ia64 does the same in i386 emulation mode.
830166727Sjkim	 */
831166727Sjkim	bsd_args.prot = linux_args->prot;
832166727Sjkim	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
833166727Sjkim		bsd_args.prot |= PROT_READ | PROT_EXEC;
834166727Sjkim
835167048Sjkim	/* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
836167048Sjkim	bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : linux_args->fd;
837167048Sjkim	if (bsd_args.fd != -1) {
838166727Sjkim		/*
839166727Sjkim		 * Linux follows Solaris mmap(2) description:
840166727Sjkim		 * The file descriptor fildes is opened with
841166727Sjkim		 * read permission, regardless of the
842166727Sjkim		 * protection options specified.
843166727Sjkim		 */
844166727Sjkim
845167048Sjkim		if ((error = fget(td, bsd_args.fd, &fp)) != 0)
846166727Sjkim			return (error);
847166727Sjkim		if (fp->f_type != DTYPE_VNODE) {
848166727Sjkim			fdrop(fp, td);
849166727Sjkim			return (EINVAL);
850166727Sjkim		}
851166727Sjkim
852166727Sjkim		/* Linux mmap() just fails for O_WRONLY files */
853166727Sjkim		if (!(fp->f_flag & FREAD)) {
854166727Sjkim			fdrop(fp, td);
855166727Sjkim			return (EACCES);
856166727Sjkim		}
857166727Sjkim
858166727Sjkim		fdrop(fp, td);
859166727Sjkim	}
860166727Sjkim
861166727Sjkim	if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
862161365Snetchild		/*
863161365Snetchild		 * The linux MAP_GROWSDOWN option does not limit auto
864133819Stjr		 * growth of the region.  Linux mmap with this option
865133819Stjr		 * takes as addr the inital BOS, and as len, the initial
866133819Stjr		 * region size.  It can then grow down from addr without
867133819Stjr		 * limit.  However, linux threads has an implicit internal
868133819Stjr		 * limit to stack size of STACK_SIZE.  Its just not
869133819Stjr		 * enforced explicitly in linux.  But, here we impose
870133819Stjr		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
871133819Stjr		 * region, since we can do this with our mmap.
872133819Stjr		 *
873133819Stjr		 * Our mmap with MAP_STACK takes addr as the maximum
874133819Stjr		 * downsize limit on BOS, and as len the max size of
875133819Stjr		 * the region.  It them maps the top SGROWSIZ bytes,
876166944Snetchild		 * and auto grows the region down, up to the limit
877133819Stjr		 * in addr.
878133819Stjr		 *
879133819Stjr		 * If we don't use the MAP_STACK option, the effect
880133819Stjr		 * of this code is to allocate a stack region of a
881133819Stjr		 * fixed size of (STACK_SIZE - GUARD_SIZE).
882133819Stjr		 */
883133819Stjr
884166727Sjkim		if ((caddr_t)PTRIN(linux_args->addr) + linux_args->len >
885133819Stjr		    p->p_vmspace->vm_maxsaddr) {
886161365Snetchild			/*
887161365Snetchild			 * Some linux apps will attempt to mmap
888133819Stjr			 * thread stacks near the top of their
889133819Stjr			 * address space.  If their TOS is greater
890133819Stjr			 * than vm_maxsaddr, vm_map_growstack()
891133819Stjr			 * will confuse the thread stack with the
892133819Stjr			 * process stack and deliver a SEGV if they
893133819Stjr			 * attempt to grow the thread stack past their
894133819Stjr			 * current stacksize rlimit.  To avoid this,
895133819Stjr			 * adjust vm_maxsaddr upwards to reflect
896133819Stjr			 * the current stacksize rlimit rather
897133819Stjr			 * than the maximum possible stacksize.
898133819Stjr			 * It would be better to adjust the
899133819Stjr			 * mmap'ed region, but some apps do not check
900133819Stjr			 * mmap's return value.
901133819Stjr			 */
902133819Stjr			PROC_LOCK(p);
903166727Sjkim			p->p_vmspace->vm_maxsaddr = (char *)LINUX32_USRSTACK -
904133819Stjr			    lim_cur(p, RLIMIT_STACK);
905133819Stjr			PROC_UNLOCK(p);
906133819Stjr		}
907133819Stjr
908133819Stjr		/* This gives us our maximum stack size */
909133819Stjr		if (linux_args->len > STACK_SIZE - GUARD_SIZE)
910133819Stjr			bsd_args.len = linux_args->len;
911133819Stjr		else
912133819Stjr			bsd_args.len  = STACK_SIZE - GUARD_SIZE;
913133819Stjr
914161365Snetchild		/*
915161365Snetchild		 * This gives us a new BOS.  If we're using VM_STACK, then
916133819Stjr		 * mmap will just map the top SGROWSIZ bytes, and let
917133819Stjr		 * the stack grow down to the limit at BOS.  If we're
918133819Stjr		 * not using VM_STACK we map the full stack, since we
919133819Stjr		 * don't have a way to autogrow it.
920133819Stjr		 */
921166727Sjkim		bsd_args.addr = (caddr_t)PTRIN(linux_args->addr) -
922166727Sjkim		    bsd_args.len;
923133819Stjr	} else {
924133819Stjr		bsd_args.addr = (caddr_t)PTRIN(linux_args->addr);
925133819Stjr		bsd_args.len  = linux_args->len;
926133819Stjr	}
927144441Sjhb	bsd_args.pos = (off_t)linux_args->pgoff * PAGE_SIZE;
928133819Stjr	bsd_args.pad = 0;
929133819Stjr
930133819Stjr#ifdef DEBUG
931133819Stjr	if (ldebug(mmap))
932133819Stjr		printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
933133819Stjr		    __func__,
934133843Sobrien		    (void *)bsd_args.addr, (int)bsd_args.len, bsd_args.prot,
935133819Stjr		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
936133819Stjr#endif
937133819Stjr	error = mmap(td, &bsd_args);
938133819Stjr#ifdef DEBUG
939133819Stjr	if (ldebug(mmap))
940133819Stjr		printf("-> %s() return: 0x%x (0x%08x)\n",
941133819Stjr			__func__, error, (u_int)td->td_retval[0]);
942133819Stjr#endif
943133819Stjr	return (error);
944133819Stjr}
945133819Stjr
946133819Stjrint
947168035Sjkimlinux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
948168035Sjkim{
949168035Sjkim	struct mprotect_args bsd_args;
950168035Sjkim
951168035Sjkim	bsd_args.addr = uap->addr;
952168035Sjkim	bsd_args.len = uap->len;
953168035Sjkim	bsd_args.prot = uap->prot;
954168035Sjkim	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
955168035Sjkim		bsd_args.prot |= PROT_READ | PROT_EXEC;
956168035Sjkim	return (mprotect(td, &bsd_args));
957168035Sjkim}
958168035Sjkim
959168035Sjkimint
960166729Sjkimlinux_iopl(struct thread *td, struct linux_iopl_args *args)
961166729Sjkim{
962166729Sjkim	int error;
963166729Sjkim
964166729Sjkim	if (args->level < 0 || args->level > 3)
965166729Sjkim		return (EINVAL);
966166729Sjkim	if ((error = priv_check(td, PRIV_IO)) != 0)
967166729Sjkim		return (error);
968166729Sjkim	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
969166729Sjkim		return (error);
970166729Sjkim	td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
971166729Sjkim	    (args->level * (PSL_IOPL / 3));
972166729Sjkim
973166729Sjkim	return (0);
974166729Sjkim}
975166729Sjkim
976166729Sjkimint
977133819Stjrlinux_pipe(struct thread *td, struct linux_pipe_args *args)
978133819Stjr{
979133819Stjr	int pip[2];
980133819Stjr	int error;
981133819Stjr	register_t reg_rdx;
982133819Stjr
983133819Stjr#ifdef DEBUG
984133819Stjr	if (ldebug(pipe))
985133819Stjr		printf(ARGS(pipe, "*"));
986133819Stjr#endif
987133819Stjr
988133819Stjr	reg_rdx = td->td_retval[1];
989133819Stjr	error = pipe(td, 0);
990133819Stjr	if (error) {
991133819Stjr		td->td_retval[1] = reg_rdx;
992133819Stjr		return (error);
993133819Stjr	}
994133819Stjr
995133819Stjr	pip[0] = td->td_retval[0];
996133819Stjr	pip[1] = td->td_retval[1];
997133819Stjr	error = copyout(pip, args->pipefds, 2 * sizeof(int));
998133819Stjr	if (error) {
999133819Stjr		td->td_retval[1] = reg_rdx;
1000133819Stjr		return (error);
1001133819Stjr	}
1002133819Stjr
1003133819Stjr	td->td_retval[1] = reg_rdx;
1004133819Stjr	td->td_retval[0] = 0;
1005133819Stjr	return (0);
1006133819Stjr}
1007166731Sjkim
1008133819Stjrint
1009133819Stjrlinux_sigaction(struct thread *td, struct linux_sigaction_args *args)
1010133819Stjr{
1011133819Stjr	l_osigaction_t osa;
1012133819Stjr	l_sigaction_t act, oact;
1013133819Stjr	int error;
1014133819Stjr
1015133819Stjr#ifdef DEBUG
1016133819Stjr	if (ldebug(sigaction))
1017133819Stjr		printf(ARGS(sigaction, "%d, %p, %p"),
1018133819Stjr		    args->sig, (void *)args->nsa, (void *)args->osa);
1019133819Stjr#endif
1020133819Stjr
1021133819Stjr	if (args->nsa != NULL) {
1022133819Stjr		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
1023133819Stjr		if (error)
1024133819Stjr			return (error);
1025133819Stjr		act.lsa_handler = osa.lsa_handler;
1026133819Stjr		act.lsa_flags = osa.lsa_flags;
1027133819Stjr		act.lsa_restorer = osa.lsa_restorer;
1028133819Stjr		LINUX_SIGEMPTYSET(act.lsa_mask);
1029133819Stjr		act.lsa_mask.__bits[0] = osa.lsa_mask;
1030133819Stjr	}
1031133819Stjr
1032133819Stjr	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
1033133819Stjr	    args->osa ? &oact : NULL);
1034133819Stjr
1035133819Stjr	if (args->osa != NULL && !error) {
1036133819Stjr		osa.lsa_handler = oact.lsa_handler;
1037133819Stjr		osa.lsa_flags = oact.lsa_flags;
1038133819Stjr		osa.lsa_restorer = oact.lsa_restorer;
1039133819Stjr		osa.lsa_mask = oact.lsa_mask.__bits[0];
1040133819Stjr		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
1041133819Stjr	}
1042133819Stjr
1043133819Stjr	return (error);
1044133819Stjr}
1045133819Stjr
1046133819Stjr/*
1047133819Stjr * Linux has two extra args, restart and oldmask.  We dont use these,
1048133819Stjr * but it seems that "restart" is actually a context pointer that
1049133819Stjr * enables the signal to happen with a different register set.
1050133819Stjr */
1051133819Stjrint
1052133819Stjrlinux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
1053133819Stjr{
1054133819Stjr	sigset_t sigmask;
1055133819Stjr	l_sigset_t mask;
1056133819Stjr
1057133819Stjr#ifdef DEBUG
1058133819Stjr	if (ldebug(sigsuspend))
1059133819Stjr		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
1060133819Stjr#endif
1061133819Stjr
1062133819Stjr	LINUX_SIGEMPTYSET(mask);
1063133819Stjr	mask.__bits[0] = args->mask;
1064133819Stjr	linux_to_bsd_sigset(&mask, &sigmask);
1065133819Stjr	return (kern_sigsuspend(td, sigmask));
1066133819Stjr}
1067133819Stjr
1068133819Stjrint
1069133819Stjrlinux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
1070133819Stjr{
1071133819Stjr	l_sigset_t lmask;
1072133819Stjr	sigset_t sigmask;
1073133819Stjr	int error;
1074133819Stjr
1075133819Stjr#ifdef DEBUG
1076133819Stjr	if (ldebug(rt_sigsuspend))
1077133819Stjr		printf(ARGS(rt_sigsuspend, "%p, %d"),
1078133819Stjr		    (void *)uap->newset, uap->sigsetsize);
1079133819Stjr#endif
1080133819Stjr
1081133819Stjr	if (uap->sigsetsize != sizeof(l_sigset_t))
1082133819Stjr		return (EINVAL);
1083133819Stjr
1084133819Stjr	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
1085133819Stjr	if (error)
1086133819Stjr		return (error);
1087133819Stjr
1088133819Stjr	linux_to_bsd_sigset(&lmask, &sigmask);
1089133819Stjr	return (kern_sigsuspend(td, sigmask));
1090133819Stjr}
1091133819Stjr
1092133819Stjrint
1093133819Stjrlinux_pause(struct thread *td, struct linux_pause_args *args)
1094133819Stjr{
1095133819Stjr	struct proc *p = td->td_proc;
1096133819Stjr	sigset_t sigmask;
1097133819Stjr
1098133819Stjr#ifdef DEBUG
1099133819Stjr	if (ldebug(pause))
1100133819Stjr		printf(ARGS(pause, ""));
1101133819Stjr#endif
1102133819Stjr
1103133819Stjr	PROC_LOCK(p);
1104133819Stjr	sigmask = td->td_sigmask;
1105133819Stjr	PROC_UNLOCK(p);
1106133819Stjr	return (kern_sigsuspend(td, sigmask));
1107133819Stjr}
1108133819Stjr
1109133819Stjrint
1110133819Stjrlinux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
1111133819Stjr{
1112133819Stjr	stack_t ss, oss;
1113133819Stjr	l_stack_t lss;
1114133819Stjr	int error;
1115133819Stjr
1116133819Stjr#ifdef DEBUG
1117133819Stjr	if (ldebug(sigaltstack))
1118133819Stjr		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1119133819Stjr#endif
1120133819Stjr
1121133819Stjr	if (uap->uss != NULL) {
1122133819Stjr		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
1123133819Stjr		if (error)
1124133819Stjr			return (error);
1125133819Stjr
1126133819Stjr		ss.ss_sp = PTRIN(lss.ss_sp);
1127133819Stjr		ss.ss_size = lss.ss_size;
1128133819Stjr		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
1129133819Stjr	}
1130134269Sjhb	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
1131134269Sjhb	    (uap->uoss != NULL) ? &oss : NULL);
1132133819Stjr	if (!error && uap->uoss != NULL) {
1133133819Stjr		lss.ss_sp = PTROUT(oss.ss_sp);
1134133819Stjr		lss.ss_size = oss.ss_size;
1135133819Stjr		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1136133819Stjr		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
1137133819Stjr	}
1138133819Stjr
1139133819Stjr	return (error);
1140133819Stjr}
1141133819Stjr
1142133819Stjrint
1143133819Stjrlinux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
1144133819Stjr{
1145133819Stjr	struct ftruncate_args sa;
1146133819Stjr
1147133819Stjr#ifdef DEBUG
1148133819Stjr	if (ldebug(ftruncate64))
1149133819Stjr		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
1150133819Stjr		    (intmax_t)args->length);
1151133819Stjr#endif
1152133819Stjr
1153133819Stjr	sa.fd = args->fd;
1154133819Stjr	sa.pad = 0;
1155133819Stjr	sa.length = args->length;
1156133819Stjr	return ftruncate(td, &sa);
1157133819Stjr}
1158133819Stjr
1159133819Stjrint
1160133819Stjrlinux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
1161133819Stjr{
1162133819Stjr	struct timeval atv;
1163133819Stjr	l_timeval atv32;
1164133819Stjr	struct timezone rtz;
1165133819Stjr	int error = 0;
1166133819Stjr
1167133819Stjr	if (uap->tp) {
1168133819Stjr		microtime(&atv);
1169133819Stjr		atv32.tv_sec = atv.tv_sec;
1170133819Stjr		atv32.tv_usec = atv.tv_usec;
1171133819Stjr		error = copyout(&atv32, uap->tp, sizeof (atv32));
1172133819Stjr	}
1173133819Stjr	if (error == 0 && uap->tzp != NULL) {
1174133819Stjr		rtz.tz_minuteswest = tz_minuteswest;
1175133819Stjr		rtz.tz_dsttime = tz_dsttime;
1176133819Stjr		error = copyout(&rtz, uap->tzp, sizeof (rtz));
1177133819Stjr	}
1178133819Stjr	return (error);
1179133819Stjr}
1180133819Stjr
1181133819Stjrint
1182133819Stjrlinux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
1183133819Stjr{
1184136152Sjhb	struct l_rusage s32;
1185136152Sjhb	struct rusage s;
1186133819Stjr	int error;
1187133819Stjr
1188136152Sjhb	error = kern_getrusage(td, uap->who, &s);
1189133819Stjr	if (error != 0)
1190133819Stjr		return (error);
1191136152Sjhb	if (uap->rusage != NULL) {
1192133819Stjr		s32.ru_utime.tv_sec = s.ru_utime.tv_sec;
1193133819Stjr		s32.ru_utime.tv_usec = s.ru_utime.tv_usec;
1194133819Stjr		s32.ru_stime.tv_sec = s.ru_stime.tv_sec;
1195133819Stjr		s32.ru_stime.tv_usec = s.ru_stime.tv_usec;
1196133819Stjr		s32.ru_maxrss = s.ru_maxrss;
1197133819Stjr		s32.ru_ixrss = s.ru_ixrss;
1198133819Stjr		s32.ru_idrss = s.ru_idrss;
1199133819Stjr		s32.ru_isrss = s.ru_isrss;
1200133819Stjr		s32.ru_minflt = s.ru_minflt;
1201133819Stjr		s32.ru_majflt = s.ru_majflt;
1202133819Stjr		s32.ru_nswap = s.ru_nswap;
1203133819Stjr		s32.ru_inblock = s.ru_inblock;
1204133819Stjr		s32.ru_oublock = s.ru_oublock;
1205133819Stjr		s32.ru_msgsnd = s.ru_msgsnd;
1206133819Stjr		s32.ru_msgrcv = s.ru_msgrcv;
1207133819Stjr		s32.ru_nsignals = s.ru_nsignals;
1208133819Stjr		s32.ru_nvcsw = s.ru_nvcsw;
1209133819Stjr		s32.ru_nivcsw = s.ru_nivcsw;
1210136152Sjhb		error = copyout(&s32, uap->rusage, sizeof(s32));
1211133819Stjr	}
1212133819Stjr	return (error);
1213133819Stjr}
1214133819Stjr
1215133819Stjrint
1216133819Stjrlinux_sched_rr_get_interval(struct thread *td,
1217133819Stjr    struct linux_sched_rr_get_interval_args *uap)
1218133819Stjr{
1219133819Stjr	struct timespec ts;
1220133819Stjr	struct l_timespec ts32;
1221133819Stjr	int error;
1222133819Stjr
1223144449Sjhb	error = kern_sched_rr_get_interval(td, uap->pid, &ts);
1224133819Stjr	if (error != 0)
1225133819Stjr		return (error);
1226133819Stjr	ts32.tv_sec = ts.tv_sec;
1227133819Stjr	ts32.tv_nsec = ts.tv_nsec;
1228133819Stjr	return (copyout(&ts32, uap->interval, sizeof(ts32)));
1229133819Stjr}
1230133819Stjr
1231133819Stjrint
1232168035Sjkimlinux_set_thread_area(struct thread *td,
1233168035Sjkim    struct linux_set_thread_area_args *args)
1234133819Stjr{
1235168035Sjkim	struct l_user_desc info;
1236168035Sjkim	struct user_segment_descriptor sd;
1237168035Sjkim	int a[2];
1238168035Sjkim	int error;
1239133819Stjr
1240168035Sjkim	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1241168035Sjkim	if (error)
1242168035Sjkim		return (error);
1243168035Sjkim
1244168035Sjkim#ifdef DEBUG
1245168035Sjkim	if (ldebug(set_thread_area))
1246168035Sjkim	   	printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, "
1247168035Sjkim		    "%i, %i, %i"), info.entry_number, info.base_addr,
1248168035Sjkim		    info.limit, info.seg_32bit, info.contents,
1249168035Sjkim		    info.read_exec_only, info.limit_in_pages,
1250168035Sjkim		    info.seg_not_present, info.useable);
1251168035Sjkim#endif
1252168035Sjkim
1253168035Sjkim	/*
1254168035Sjkim	 * Semantics of Linux version: every thread in the system has array
1255168035Sjkim	 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
1256168035Sjkim	 * This syscall loads one of the selected TLS decriptors with a value
1257168035Sjkim	 * and also loads GDT descriptors 6, 7 and 8 with the content of
1258168035Sjkim	 * the per-thread descriptors.
1259168035Sjkim	 *
1260168035Sjkim	 * Semantics of FreeBSD version: I think we can ignore that Linux has
1261168035Sjkim	 * three per-thread descriptors and use just the first one.
1262168035Sjkim	 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
1263168035Sjkim	 * for loading the GDT descriptors. We use just one GDT descriptor
1264168035Sjkim	 * for TLS, so we will load just one.
1265168035Sjkim	 * XXX: This doesnt work when user-space process tries to use more
1266168035Sjkim	 * than one TLS segment. Comment in the Linux source says wine might
1267168035Sjkim	 * do that.
1268168035Sjkim	 */
1269168035Sjkim
1270168035Sjkim	/*
1271168035Sjkim	 * GLIBC reads current %gs and call set_thread_area() with it.
1272168035Sjkim	 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
1273168035Sjkim	 * we use these segments.
1274168035Sjkim	 */
1275168035Sjkim	switch (info.entry_number) {
1276168035Sjkim	case GUGS32_SEL:
1277168035Sjkim	case GUDATA_SEL:
1278168035Sjkim	case 6:
1279168035Sjkim	case -1:
1280168035Sjkim		info.entry_number = GUGS32_SEL;
1281168035Sjkim		break;
1282168035Sjkim	default:
1283168035Sjkim		return (EINVAL);
1284168035Sjkim	}
1285168035Sjkim
1286168035Sjkim	/*
1287168035Sjkim	 * We have to copy out the GDT entry we use.
1288168035Sjkim	 * XXX: What if userspace program does not check return value and
1289168035Sjkim	 * tries to use 6, 7 or 8?
1290168035Sjkim	 */
1291168035Sjkim	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1292168035Sjkim	if (error)
1293168035Sjkim		return (error);
1294168035Sjkim
1295168035Sjkim	if (LINUX_LDT_empty(&info)) {
1296168035Sjkim		a[0] = 0;
1297168035Sjkim		a[1] = 0;
1298168035Sjkim	} else {
1299168035Sjkim		a[0] = LINUX_LDT_entry_a(&info);
1300168035Sjkim		a[1] = LINUX_LDT_entry_b(&info);
1301168035Sjkim	}
1302168035Sjkim
1303168035Sjkim	memcpy(&sd, &a, sizeof(a));
1304168035Sjkim#ifdef DEBUG
1305168035Sjkim	if (ldebug(set_thread_area))
1306168035Sjkim		printf("Segment created in set_thread_area: "
1307168035Sjkim		    "lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, "
1308168035Sjkim		    "type: %i, dpl: %i, p: %i, xx: %i, long: %i, "
1309168035Sjkim		    "def32: %i, gran: %i\n",
1310168035Sjkim		    sd.sd_lobase,
1311168035Sjkim		    sd.sd_hibase,
1312168035Sjkim		    sd.sd_lolimit,
1313168035Sjkim		    sd.sd_hilimit,
1314168035Sjkim		    sd.sd_type,
1315168035Sjkim		    sd.sd_dpl,
1316168035Sjkim		    sd.sd_p,
1317168035Sjkim		    sd.sd_xx,
1318168035Sjkim		    sd.sd_long,
1319168035Sjkim		    sd.sd_def32,
1320168035Sjkim		    sd.sd_gran);
1321168035Sjkim#endif
1322168035Sjkim
1323168035Sjkim	critical_enter();
1324168035Sjkim	td->td_pcb->pcb_gsbase = (register_t)info.base_addr;
1325168035Sjkim	td->td_pcb->pcb_gs32sd = gdt[GUGS32_SEL] = sd;
1326168035Sjkim	td->td_pcb->pcb_gs32p = &gdt[GUGS32_SEL];
1327168035Sjkim	td->td_pcb->pcb_flags |= PCB_32BIT;
1328168035Sjkim	wrmsr(MSR_KGSBASE, td->td_pcb->pcb_gsbase);
1329168035Sjkim	critical_exit();
1330168035Sjkim
1331168035Sjkim	return (0);
1332133819Stjr}
1333