linux32_machdep.c revision 176193
11195Srgrimes/*-
22619Srgrimes * Copyright (c) 2004 Tim J. Robbins
337Srgrimes * Copyright (c) 2002 Doug Rabson
437Srgrimes * Copyright (c) 2000 Marcel Moolenaar
537Srgrimes * All rights reserved.
6147Srgrimes *
7147Srgrimes * Redistribution and use in source and binary forms, with or without
8147Srgrimes * modification, are permitted provided that the following conditions
9207Snate * are met:
101734Sjkh * 1. Redistributions of source code must retain the above copyright
111518Sguido *    notice, this list of conditions and the following disclaimer
121518Sguido *    in this position and unchanged.
131734Sjkh * 2. Redistributions in binary form must reproduce the above copyright
141734Sjkh *    notice, this list of conditions and the following disclaimer in the
151734Sjkh *    documentation and/or other materials provided with the distribution.
1637Srgrimes * 3. The name of the author may not be used to endorse or promote products
1737Srgrimes *    derived from this software without specific prior written permission.
1837Srgrimes *
1937Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
201773Sjkh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21147Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22147Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
232570Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
242570Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
252570Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261767Sjkh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
272570Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
282570Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
292570Srgrimes */
302570Srgrimes
312570Srgrimes#include <sys/cdefs.h>
322570Srgrimes__FBSDID("$FreeBSD: head/sys/amd64/linux32/linux32_machdep.c 176193 2008-02-11 19:35:03Z jkim $");
332570Srgrimes
342570Srgrimes#include <sys/param.h>
351782Sjkh#include <sys/kernel.h>
361782Sjkh#include <sys/systm.h>
37491Srgrimes#include <sys/file.h>
3837Srgrimes#include <sys/fcntl.h>
3937Srgrimes#include <sys/clock.h>
4037Srgrimes#include <sys/imgact.h>
4137Srgrimes#include <sys/limits.h>
4237Srgrimes#include <sys/lock.h>
43263Srgrimes#include <sys/malloc.h>
441130Srgrimes#include <sys/mman.h>
451767Sjkh#include <sys/mutex.h>
461126Srgrimes#include <sys/priv.h>
47993Srgrimes#include <sys/proc.h>
48277Srgrimes#include <sys/resource.h>
49277Srgrimes#include <sys/resourcevar.h>
50284Srgrimes#include <sys/sched.h>
51463Srgrimes#include <sys/syscallsubr.h>
521205Srgrimes#include <sys/sysproto.h>
53284Srgrimes#include <sys/unistd.h>
54284Srgrimes
55284Srgrimes#include <machine/frame.h>
56284Srgrimes#include <machine/pcb.h>
57284Srgrimes#include <machine/psl.h>
581285Srgrimes#include <machine/segments.h>
59284Srgrimes#include <machine/specialreg.h>
601767Sjkh
611285Srgrimes#include <vm/vm.h>
622499Sgpalmer#include <vm/pmap.h>
631285Srgrimes#include <vm/vm_extern.h>
64284Srgrimes#include <vm/vm_kern.h>
651371Srgrimes#include <vm/vm_map.h>
661371Srgrimes
671194Srgrimes#include <amd64/linux32/linux.h>
68358Srgrimes#include <amd64/linux32/linux32_proto.h>
69358Srgrimes#include <compat/linux/linux_ipc.h>
70412Salm#include <compat/linux/linux_signal.h>
71358Srgrimes#include <compat/linux/linux_util.h>
72452Srgrimes#include <compat/linux/linux_emul.h>
73358Srgrimes
741194Srgrimesstruct l_old_select_argv {
751767Sjkh	l_int		nfds;
761194Srgrimes	l_uintptr_t	readfds;
771194Srgrimes	l_uintptr_t	writefds;
781194Srgrimes	l_uintptr_t	exceptfds;
791194Srgrimes	l_uintptr_t	timeout;
801194Srgrimes} __packed;
811194Srgrimes
821194Srgrimesint
831194Srgrimeslinux_to_bsd_sigaltstack(int lsa)
841194Srgrimes{
851194Srgrimes	int bsa = 0;
861194Srgrimes
871194Srgrimes	if (lsa & LINUX_SS_DISABLE)
881194Srgrimes		bsa |= SS_DISABLE;
891194Srgrimes	if (lsa & LINUX_SS_ONSTACK)
901194Srgrimes		bsa |= SS_ONSTACK;
911194Srgrimes	return (bsa);
921194Srgrimes}
931194Srgrimes
941243Srgrimesint
95263Srgrimesbsd_to_linux_sigaltstack(int bsa)
96358Srgrimes{
971194Srgrimes	int lsa = 0;
981194Srgrimes
991194Srgrimes	if (bsa & SS_DISABLE)
1001773Sjkh		lsa |= LINUX_SS_DISABLE;
1012499Sgpalmer	if (bsa & SS_ONSTACK)
1021194Srgrimes		lsa |= LINUX_SS_ONSTACK;
103452Srgrimes	return (lsa);
1041194Srgrimes}
1051194Srgrimes
106358Srgrimes/*
1071194Srgrimes * Custom version of exec_copyin_args() so that we can translate
1081695Scsgr * the pointers.
1091695Scsgr */
1101695Scsgrstatic int
1111695Scsgrlinux_exec_copyin_args(struct image_args *args, char *fname,
1121695Scsgr    enum uio_seg segflg, char **argv, char **envv)
1131695Scsgr{
1141695Scsgr	char *argp, *envp;
1151243Srgrimes	u_int32_t *p32, arg;
1161194Srgrimes	size_t length;
1171194Srgrimes	int error;
1181243Srgrimes
1191243Srgrimes	bzero(args, sizeof(*args));
120284Srgrimes	if (argv == NULL)
1212570Srgrimes		return (EFAULT);
1222570Srgrimes
123372Srgrimes	/*
124372Srgrimes	 * Allocate temporary demand zeroed space for argument and
125372Srgrimes	 *	environment strings
1262570Srgrimes	 */
1272570Srgrimes	args->buf = (char *)kmem_alloc_wait(exec_map,
128372Srgrimes	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1291126Srgrimes	if (args->buf == NULL)
130347Srgrimes		return (ENOMEM);
1311767Sjkh	args->begin_argv = args->buf;
13237Srgrimes	args->endp = args->begin_argv;
1332538Spst	args->stringspace = ARG_MAX;
134347Srgrimes
1352538Spst	args->fname = args->buf + ARG_MAX;
136355Srgrimes
137372Srgrimes	/*
138347Srgrimes	 * Copy the file name.
139355Srgrimes	 */
140347Srgrimes	error = (segflg == UIO_SYSSPACE) ?
1412538Spst	    copystr(fname, args->fname, PATH_MAX, &length) :
1422538Spst	    copyinstr(fname, args->fname, PATH_MAX, &length);
1432538Spst	if (error != 0)
1442538Spst		goto err_exit;
1452538Spst
146372Srgrimes	/*
147347Srgrimes	 * extract arguments first
148355Srgrimes	 */
149347Srgrimes	p32 = (u_int32_t *)argv;
150347Srgrimes	for (;;) {
1512538Spst		error = copyin(p32++, &arg, sizeof(arg));
152147Srgrimes		if (error)
1531759Sjkh			goto err_exit;
1541759Sjkh		if (arg == 0)
1551759Sjkh			break;
1561759Sjkh		argp = PTRIN(arg);
1571759Sjkh		error = copyinstr(argp, args->endp, args->stringspace, &length);
1581731Sjkh		if (error) {
1591759Sjkh			if (error == ENAMETOOLONG)
1601731Sjkh				error = E2BIG;
1611759Sjkh
1621759Sjkh			goto err_exit;
16337Srgrimes		}
1641759Sjkh		args->stringspace -= length;
165347Srgrimes		args->endp += length;
1661759Sjkh		args->argc++;
167347Srgrimes	}
1681731Sjkh
1691731Sjkh	args->begin_envv = args->endp;
17037Srgrimes
1711731Sjkh	/*
17237Srgrimes	 * extract environment strings
1731731Sjkh	 */
17437Srgrimes	if (envv) {
1751731Sjkh		p32 = (u_int32_t *)envv;
17637Srgrimes		for (;;) {
17737Srgrimes			error = copyin(p32++, &arg, sizeof(arg));
17837Srgrimes			if (error)
17937Srgrimes				goto err_exit;
1801731Sjkh			if (arg == 0)
1811731Sjkh				break;
1821731Sjkh			envp = PTRIN(arg);
1831731Sjkh			error = copyinstr(envp, args->endp, args->stringspace,
18437Srgrimes			    &length);
18537Srgrimes			if (error) {
186147Srgrimes				if (error == ENAMETOOLONG)
187147Srgrimes					error = E2BIG;
18837Srgrimes				goto err_exit;
189147Srgrimes			}
19037Srgrimes			args->stringspace -= length;
19137Srgrimes			args->endp += length;
19237Srgrimes			args->envc++;
193288Srgrimes		}
194288Srgrimes	}
195147Srgrimes
19637Srgrimes	return (0);
197147Srgrimes
198147Srgrimeserr_exit:
19937Srgrimes	kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
2001759Sjkh	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
2011759Sjkh	args->buf = NULL;
2021759Sjkh	return (error);
2031759Sjkh}
204347Srgrimes
2052538Spstint
2062538Spstlinux_execve(struct thread *td, struct linux_execve_args *args)
207347Srgrimes{
2082538Spst	struct image_args eargs;
2091775Sjkh	char *path;
210347Srgrimes	int error;
2111759Sjkh
212355Srgrimes	LCONVPATHEXIST(td, args->path, &path);
213277Srgrimes
2141126Srgrimes#ifdef DEBUG
2151126Srgrimes	if (ldebug(execve))
2161731Sjkh		printf(ARGS(execve, "%s"), path);
217238Sroot#endif
2181759Sjkh
2191731Sjkh	error = linux_exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
2201759Sjkh	    args->envp);
221333Srgrimes	free(path, M_TEMP);
2221759Sjkh	if (error == 0)
2231759Sjkh		error = kern_execve(td, &eargs, NULL);
224168Srgrimes	if (error == 0)
225333Srgrimes		/* Linux process can execute FreeBSD one, do not attempt
2261759Sjkh		 * to create emuldata for such process using
2271759Sjkh		 * linux_proc_init, this leads to a panic on KASSERT
228333Srgrimes		 * because such process has p->p_emuldata == NULL.
22937Srgrimes		 */
2301731Sjkh	   	if (td->td_proc->p_sysent == &elf_linux_sysvec)
2312619Srgrimes			error = linux_proc_init(td, 0, 0);
2321782Sjkh	return (error);
2332619Srgrimes}
2341782Sjkh
2351731Sjkhstruct iovec32 {
2361731Sjkh	u_int32_t iov_base;
2371731Sjkh	int	iov_len;
2382570Srgrimes};
2392570Srgrimes
2401731SjkhCTASSERT(sizeof(struct iovec32) == 8);
2412570Srgrimes
2422570Srgrimesstatic int
2431731Sjkhlinux32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
2442570Srgrimes{
2452570Srgrimes	struct iovec32 iov32;
2461731Sjkh	struct iovec *iov;
24737Srgrimes	struct uio *uio;
2481759Sjkh	u_int iovlen;
2491759Sjkh	int error, i;
25037Srgrimes
2511759Sjkh	*uiop = NULL;
2521759Sjkh	if (iovcnt > UIO_MAXIOV)
2531759Sjkh		return (EINVAL);
2541759Sjkh	iovlen = iovcnt * sizeof(struct iovec);
2551731Sjkh	uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
256320Srgrimes	iov = (struct iovec *)(uio + 1);
2571759Sjkh	for (i = 0; i < iovcnt; i++) {
2581731Sjkh		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
259320Srgrimes		if (error) {
260320Srgrimes			free(uio, M_IOV);
261358Srgrimes			return (error);
262568Srgrimes		}
2631672Sjkh		iov[i].iov_base = PTRIN(iov32.iov_base);
2641762Sjkh		iov[i].iov_len = iov32.iov_len;
2651027Sache	}
2661027Sache	uio->uio_iov = iov;
2671731Sjkh	uio->uio_iovcnt = iovcnt;
268333Srgrimes	uio->uio_segflg = UIO_USERSPACE;
269284Srgrimes	uio->uio_offset = -1;
270320Srgrimes	uio->uio_resid = 0;
2712570Srgrimes	for (i = 0; i < iovcnt; i++) {
272284Srgrimes		if (iov->iov_len > INT_MAX - uio->uio_resid) {
273320Srgrimes			free(uio, M_IOV);
2741731Sjkh			return (EINVAL);
2751731Sjkh		}
2761731Sjkh		uio->uio_resid += iov->iov_len;
2771762Sjkh		iov++;
2781194Srgrimes	}
2791194Srgrimes	*uiop = uio;
2801194Srgrimes	return (0);
2811194Srgrimes}
282320Srgrimes
2831205Srgrimesint
2842570Srgrimeslinux_readv(struct thread *td, struct linux_readv_args *uap)
2851759Sjkh{
2861731Sjkh	struct uio *auio;
287277Srgrimes	int error;
2881027Sache
2891027Sache	error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2901205Srgrimes	if (error)
291358Srgrimes		return (error);
2921205Srgrimes	error = kern_readv(td, uap->fd, auio);
2931782Sjkh	free(auio, M_IOV);
294277Srgrimes	return (error);
2951205Srgrimes}
2962570Srgrimes
2971759Sjkhint
2981731Sjkhlinux_writev(struct thread *td, struct linux_writev_args *uap)
299320Srgrimes{
3001027Sache	struct uio *auio;
3011027Sache	int error;
3021205Srgrimes
303358Srgrimes	error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
3041205Srgrimes	if (error)
3051782Sjkh		return (error);
306320Srgrimes	error = kern_writev(td, uap->fd, auio);
3071285Srgrimes	free(auio, M_IOV);
3082570Srgrimes	return (error);
3091371Srgrimes}
3101371Srgrimes
3111371Srgrimesstruct l_ipc_kludge {
3121371Srgrimes	l_uintptr_t msgp;
3131371Srgrimes	l_long msgtyp;
3141285Srgrimes} __packed;
3151731Sjkh
3161285Srgrimesint
3171731Sjkhlinux_ipc(struct thread *td, struct linux_ipc_args *args)
3181731Sjkh{
3191731Sjkh
3201731Sjkh	switch (args->what & 0xFFFF) {
3211285Srgrimes	case LINUX_SEMOP: {
3221285Srgrimes		struct linux_semop_args a;
3232570Srgrimes
3241759Sjkh		a.semid = args->arg1;
3251731Sjkh		a.tsops = args->ptr;
3261285Srgrimes		a.nsops = args->arg2;
3271285Srgrimes		return (linux_semop(td, &a));
3281285Srgrimes	}
3291285Srgrimes	case LINUX_SEMGET: {
3301285Srgrimes		struct linux_semget_args a;
3311285Srgrimes
3321782Sjkh		a.key = args->arg1;
3331285Srgrimes		a.nsems = args->arg2;
3341285Srgrimes		a.semflg = args->arg3;
3352570Srgrimes		return (linux_semget(td, &a));
3361759Sjkh	}
3371731Sjkh	case LINUX_SEMCTL: {
3381285Srgrimes		struct linux_semctl_args a;
3391285Srgrimes		int error;
3401285Srgrimes
3411285Srgrimes		a.semid = args->arg1;
3421285Srgrimes		a.semnum = args->arg2;
3431285Srgrimes		a.cmd = args->arg3;
3441782Sjkh		error = copyin(args->ptr, &a.arg, sizeof(a.arg));
3451285Srgrimes		if (error)
3461205Srgrimes			return (error);
347568Srgrimes		return (linux_semctl(td, &a));
3481672Sjkh	}
349568Srgrimes	case LINUX_MSGSND: {
3501027Sache		struct linux_msgsnd_args a;
3511027Sache
3521731Sjkh		a.msqid = args->arg1;
353333Srgrimes		a.msgp = args->ptr;
354284Srgrimes		a.msgsz = args->arg2;
355358Srgrimes		a.msgflg = args->arg3;
3562570Srgrimes		return (linux_msgsnd(td, &a));
357284Srgrimes	}
3581194Srgrimes	case LINUX_MSGRCV: {
3591243Srgrimes		struct linux_msgrcv_args a;
3601194Srgrimes
3611194Srgrimes		a.msqid = args->arg1;
3621731Sjkh		a.msgsz = args->arg2;
3631731Sjkh		a.msgflg = args->arg3;
3641731Sjkh		if ((args->what >> 16) == 0) {
3651731Sjkh			struct l_ipc_kludge tmp;
3661731Sjkh			int error;
3671762Sjkh
368284Srgrimes			if (args->ptr == 0)
3691194Srgrimes				return (EINVAL);
3701194Srgrimes			error = copyin(args->ptr, &tmp, sizeof(tmp));
3711194Srgrimes			if (error)
3721194Srgrimes				return (error);
373358Srgrimes			a.msgp = PTRIN(tmp.msgp);
3741194Srgrimes			a.msgtyp = tmp.msgtyp;
375358Srgrimes		} else {
376358Srgrimes			a.msgp = args->ptr;
3771243Srgrimes			a.msgtyp = args->arg5;
378333Srgrimes		}
379284Srgrimes		return (linux_msgrcv(td, &a));
3801027Sache	}
3811027Sache	case LINUX_MSGGET: {
3821205Srgrimes		struct linux_msgget_args a;
383358Srgrimes
3841205Srgrimes		a.key = args->arg1;
3851782Sjkh		a.msgflg = args->arg2;
386284Srgrimes		return (linux_msgget(td, &a));
3871205Srgrimes	}
388568Srgrimes	case LINUX_MSGCTL: {
3891672Sjkh		struct linux_msgctl_args a;
390568Srgrimes
3911027Sache		a.msqid = args->arg1;
3921027Sache		a.cmd = args->arg2;
3931731Sjkh		a.buf = args->ptr;
394333Srgrimes		return (linux_msgctl(td, &a));
395284Srgrimes	}
396358Srgrimes	case LINUX_SHMAT: {
3971769Sjkh		struct linux_shmat_args a;
3981769Sjkh
3991769Sjkh		a.shmid = args->arg1;
4001773Sjkh		a.shmaddr = args->ptr;
4011773Sjkh		a.shmflg = args->arg2;
4021773Sjkh		a.raddr = PTRIN((l_uint)args->arg3);
4031769Sjkh		return (linux_shmat(td, &a));
4041769Sjkh	}
4051769Sjkh	case LINUX_SHMDT: {
4061773Sjkh		struct linux_shmdt_args a;
4071773Sjkh
4081773Sjkh		a.shmaddr = args->ptr;
409284Srgrimes		return (linux_shmdt(td, &a));
410444Srgrimes	}
4111194Srgrimes	case LINUX_SHMGET: {
4121194Srgrimes		struct linux_shmget_args a;
4131773Sjkh
4141769Sjkh		a.key = args->arg1;
4151773Sjkh		a.size = args->arg2;
4161773Sjkh		a.shmflg = args->arg3;
4171731Sjkh		return (linux_shmget(td, &a));
4181731Sjkh	}
4191731Sjkh	case LINUX_SHMCTL: {
4201731Sjkh		struct linux_shmctl_args a;
4211731Sjkh
4221731Sjkh		a.shmid = args->arg1;
423284Srgrimes		a.cmd = args->arg2;
4241027Sache		a.buf = args->ptr;
4251027Sache		return (linux_shmctl(td, &a));
4261205Srgrimes	}
427358Srgrimes	default:
4281205Srgrimes		break;
4291782Sjkh	}
430284Srgrimes
431372Srgrimes	return (EINVAL);
432372Srgrimes}
433538Srgrimes
4341782Sjkhint
435372Srgrimeslinux_old_select(struct thread *td, struct linux_old_select_args *args)
4361782Sjkh{
4372570Srgrimes	struct l_old_select_argv linux_args;
4381782Sjkh	struct linux_select_args newsel;
4392570Srgrimes	int error;
4401782Sjkh
4411782Sjkh#ifdef DEBUG
4421782Sjkh	if (ldebug(old_select))
443538Srgrimes		printf(ARGS(old_select, "%p"), args->ptr);
444376Srgrimes#endif
4451782Sjkh
4461782Sjkh	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
447376Srgrimes	if (error)
448538Srgrimes		return (error);
449376Srgrimes
4501782Sjkh	newsel.nfds = linux_args.nfds;
4512570Srgrimes	newsel.readfds = PTRIN(linux_args.readfds);
452376Srgrimes	newsel.writefds = PTRIN(linux_args.writefds);
453538Srgrimes	newsel.exceptfds = PTRIN(linux_args.exceptfds);
454538Srgrimes	newsel.timeout = PTRIN(linux_args.timeout);
4551782Sjkh	return (linux_select(td, &newsel));
4561782Sjkh}
457538Srgrimes
458538Srgrimesint
459538Srgrimeslinux_fork(struct thread *td, struct linux_fork_args *args)
4601782Sjkh{
4611782Sjkh	int error;
462538Srgrimes	struct proc *p2;
463538Srgrimes	struct thread *td2;
464538Srgrimes
4651782Sjkh#ifdef DEBUG
4661782Sjkh	if (ldebug(fork))
467538Srgrimes		printf(ARGS(fork, ""));
468538Srgrimes#endif
469538Srgrimes
4701782Sjkh	if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0)
4711782Sjkh		return (error);
472538Srgrimes
473538Srgrimes	if (error == 0) {
474538Srgrimes		td->td_retval[0] = p2->p_pid;
4751782Sjkh		td->td_retval[1] = 0;
4761782Sjkh	}
4771782Sjkh
478538Srgrimes	if (td->td_retval[1] == 1)
479538Srgrimes		td->td_retval[0] = 0;
480538Srgrimes	error = linux_proc_init(td, td->td_retval[0], 0);
4811782Sjkh	if (error)
4821782Sjkh		return (error);
483538Srgrimes
484538Srgrimes	td2 = FIRST_THREAD_IN_PROC(p2);
485538Srgrimes
4861782Sjkh	/*
4871782Sjkh	 * Make this runnable after we are finished with it.
488538Srgrimes	 */
489538Srgrimes	thread_lock(td2);
490538Srgrimes	TD_SET_CAN_RUN(td2);
4911782Sjkh	sched_add(td2, SRQ_BORING);
4921782Sjkh	thread_unlock(td2);
493538Srgrimes
494538Srgrimes	return (0);
495538Srgrimes}
4961782Sjkh
4971782Sjkhint
498538Srgrimeslinux_vfork(struct thread *td, struct linux_vfork_args *args)
499538Srgrimes{
500538Srgrimes	int error;
5011782Sjkh	struct proc *p2;
5021782Sjkh	struct thread *td2;
503538Srgrimes
504538Srgrimes#ifdef DEBUG
505538Srgrimes	if (ldebug(vfork))
5061782Sjkh		printf(ARGS(vfork, ""));
5071782Sjkh#endif
508538Srgrimes
509538Srgrimes	/* Exclude RFPPWAIT */
510538Srgrimes	if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0)
5111782Sjkh		return (error);
5121782Sjkh	if (error == 0) {
513538Srgrimes	   	td->td_retval[0] = p2->p_pid;
5142619Srgrimes		td->td_retval[1] = 0;
5151782Sjkh	}
5161782Sjkh	/* Are we the child? */
5171782Sjkh	if (td->td_retval[1] == 1)
518538Srgrimes		td->td_retval[0] = 0;
519372Srgrimes	error = linux_proc_init(td, td->td_retval[0], 0);
520372Srgrimes	if (error)
521372Srgrimes		return (error);
522372Srgrimes
523372Srgrimes	PROC_LOCK(p2);
524372Srgrimes	p2->p_flag |= P_PPWAIT;
525372Srgrimes	PROC_UNLOCK(p2);
526372Srgrimes
527372Srgrimes	td2 = FIRST_THREAD_IN_PROC(p2);
528372Srgrimes
529372Srgrimes	/*
530372Srgrimes	 * Make this runnable after we are finished with it.
531372Srgrimes	 */
532372Srgrimes	thread_lock(td2);
533372Srgrimes	TD_SET_CAN_RUN(td2);
534372Srgrimes	sched_add(td2, SRQ_BORING);
535372Srgrimes	thread_unlock(td2);
536372Srgrimes
537372Srgrimes	/* wait for the children to exit, ie. emulate vfork */
538372Srgrimes	PROC_LOCK(p2);
539372Srgrimes	while (p2->p_flag & P_PPWAIT)
540372Srgrimes	   	msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0);
541372Srgrimes	PROC_UNLOCK(p2);
542372Srgrimes
543372Srgrimes	return (0);
544372Srgrimes}
545372Srgrimes
546538Srgrimesint
5471782Sjkhlinux_clone(struct thread *td, struct linux_clone_args *args)
548372Srgrimes{
549372Srgrimes	int error, ff = RFPROC | RFSTOPPED;
550147Srgrimes	struct proc *p2;
5511731Sjkh	struct thread *td2;
5521731Sjkh	int exit_signal;
5531731Sjkh	struct linux_emuldata *em;
554372Srgrimes
5551731Sjkh#ifdef DEBUG
556372Srgrimes	if (ldebug(clone)) {
557410Srgrimes		printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
558147Srgrimes		    "child tid: %p"), (unsigned)args->flags,
5591285Srgrimes		    args->stack, args->parent_tidptr, args->child_tidptr);
5601285Srgrimes	}
561372Srgrimes#endif
5621731Sjkh
5631769Sjkh	exit_signal = args->flags & 0x000000ff;
5641731Sjkh	if (LINUX_SIG_VALID(exit_signal)) {
565372Srgrimes		if (exit_signal <= LINUX_SIGTBLSZ)
5662611Srgrimes			exit_signal =
5672611Srgrimes			    linux_to_bsd_signal[_SIG_IDX(exit_signal)];
5682611Srgrimes	} else if (exit_signal != 0)
569372Srgrimes		return (EINVAL);
57037Srgrimes
571	if (args->flags & LINUX_CLONE_VM)
572		ff |= RFMEM;
573	if (args->flags & LINUX_CLONE_SIGHAND)
574		ff |= RFSIGSHARE;
575	/*
576	 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
577	 * and open files is independant.  In FreeBSD, its in one
578	 * structure but in reality it does not cause any problems
579	 * because both of these flags are usually set together.
580	 */
581	if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
582		ff |= RFFDG;
583
584	/*
585	 * Attempt to detect when linux_clone(2) is used for creating
586	 * kernel threads. Unfortunately despite the existence of the
587	 * CLONE_THREAD flag, version of linuxthreads package used in
588	 * most popular distros as of beginning of 2005 doesn't make
589	 * any use of it. Therefore, this detection relies on
590	 * empirical observation that linuxthreads sets certain
591	 * combination of flags, so that we can make more or less
592	 * precise detection and notify the FreeBSD kernel that several
593	 * processes are in fact part of the same threading group, so
594	 * that special treatment is necessary for signal delivery
595	 * between those processes and fd locking.
596	 */
597	if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
598		ff |= RFTHREAD;
599
600	if (args->flags & LINUX_CLONE_PARENT_SETTID)
601		if (args->parent_tidptr == NULL)
602			return (EINVAL);
603
604	error = fork1(td, ff, 0, &p2);
605	if (error)
606		return (error);
607
608	if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
609	   	sx_xlock(&proctree_lock);
610		PROC_LOCK(p2);
611		proc_reparent(p2, td->td_proc->p_pptr);
612		PROC_UNLOCK(p2);
613		sx_xunlock(&proctree_lock);
614	}
615
616	/* create the emuldata */
617	error = linux_proc_init(td, p2->p_pid, args->flags);
618	/* reference it - no need to check this */
619	em = em_find(p2, EMUL_DOLOCK);
620	KASSERT(em != NULL, ("clone: emuldata not found.\n"));
621	/* and adjust it */
622
623	if (args->flags & LINUX_CLONE_THREAD) {
624#ifdef notyet
625	   	PROC_LOCK(p2);
626	   	p2->p_pgrp = td->td_proc->p_pgrp;
627	   	PROC_UNLOCK(p2);
628#endif
629		exit_signal = 0;
630	}
631
632	if (args->flags & LINUX_CLONE_CHILD_SETTID)
633		em->child_set_tid = args->child_tidptr;
634	else
635	   	em->child_set_tid = NULL;
636
637	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
638		em->child_clear_tid = args->child_tidptr;
639	else
640	   	em->child_clear_tid = NULL;
641
642	EMUL_UNLOCK(&emul_lock);
643
644	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
645		error = copyout(&p2->p_pid, args->parent_tidptr,
646		    sizeof(p2->p_pid));
647		if (error)
648			printf(LMSG("copyout failed!"));
649	}
650
651	PROC_LOCK(p2);
652	p2->p_sigparent = exit_signal;
653	PROC_UNLOCK(p2);
654	td2 = FIRST_THREAD_IN_PROC(p2);
655	/*
656	 * In a case of stack = NULL, we are supposed to COW calling process
657	 * stack. This is what normal fork() does, so we just keep tf_rsp arg
658	 * intact.
659	 */
660	if (args->stack)
661		td2->td_frame->tf_rsp = PTROUT(args->stack);
662
663	if (args->flags & LINUX_CLONE_SETTLS) {
664		struct user_segment_descriptor sd;
665		struct l_user_desc info;
666		int a[2];
667
668		error = copyin((void *)td->td_frame->tf_rsi, &info,
669		    sizeof(struct l_user_desc));
670		if (error) {
671			printf(LMSG("copyin failed!"));
672		} else {
673			/* We might copy out the entry_number as GUGS32_SEL. */
674			info.entry_number = GUGS32_SEL;
675			error = copyout(&info, (void *)td->td_frame->tf_rsi,
676			    sizeof(struct l_user_desc));
677			if (error)
678				printf(LMSG("copyout failed!"));
679
680			a[0] = LINUX_LDT_entry_a(&info);
681			a[1] = LINUX_LDT_entry_b(&info);
682
683			memcpy(&sd, &a, sizeof(a));
684#ifdef DEBUG
685			if (ldebug(clone))
686				printf("Segment created in clone with "
687				    "CLONE_SETTLS: lobase: %x, hibase: %x, "
688				    "lolimit: %x, hilimit: %x, type: %i, "
689				    "dpl: %i, p: %i, xx: %i, long: %i, "
690				    "def32: %i, gran: %i\n", sd.sd_lobase,
691				    sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit,
692				    sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
693				    sd.sd_long, sd.sd_def32, sd.sd_gran);
694#endif
695			td2->td_pcb->pcb_gsbase = (register_t)info.base_addr;
696			td2->td_pcb->pcb_gs32sd = sd;
697			td2->td_pcb->pcb_gs32p = &gdt[GUGS32_SEL];
698			td2->td_pcb->pcb_gs = GSEL(GUGS32_SEL, SEL_UPL);
699			td2->td_pcb->pcb_flags |= PCB_32BIT;
700		}
701	}
702
703#ifdef DEBUG
704	if (ldebug(clone))
705		printf(LMSG("clone: successful rfork to %d, "
706		    "stack %p sig = %d"), (int)p2->p_pid, args->stack,
707		    exit_signal);
708#endif
709	if (args->flags & LINUX_CLONE_VFORK) {
710	   	PROC_LOCK(p2);
711	   	p2->p_flag |= P_PPWAIT;
712	   	PROC_UNLOCK(p2);
713	}
714
715	/*
716	 * Make this runnable after we are finished with it.
717	 */
718	thread_lock(td2);
719	TD_SET_CAN_RUN(td2);
720	sched_add(td2, SRQ_BORING);
721	thread_unlock(td2);
722
723	td->td_retval[0] = p2->p_pid;
724	td->td_retval[1] = 0;
725
726	if (args->flags & LINUX_CLONE_VFORK) {
727		/* wait for the children to exit, ie. emulate vfork */
728		PROC_LOCK(p2);
729		while (p2->p_flag & P_PPWAIT)
730			msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0);
731		PROC_UNLOCK(p2);
732	}
733
734	return (0);
735}
736
737#define STACK_SIZE  (2 * 1024 * 1024)
738#define GUARD_SIZE  (4 * PAGE_SIZE)
739
740static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
741
742int
743linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
744{
745	struct l_mmap_argv linux_args;
746
747#ifdef DEBUG
748	if (ldebug(mmap2))
749		printf(ARGS(mmap2, "0x%08x, %d, %d, 0x%08x, %d, %d"),
750		    args->addr, args->len, args->prot,
751		    args->flags, args->fd, args->pgoff);
752#endif
753
754	linux_args.addr = PTROUT(args->addr);
755	linux_args.len = args->len;
756	linux_args.prot = args->prot;
757	linux_args.flags = args->flags;
758	linux_args.fd = args->fd;
759	linux_args.pgoff = args->pgoff;
760
761	return (linux_mmap_common(td, &linux_args));
762}
763
764int
765linux_mmap(struct thread *td, struct linux_mmap_args *args)
766{
767	int error;
768	struct l_mmap_argv linux_args;
769
770	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
771	if (error)
772		return (error);
773
774#ifdef DEBUG
775	if (ldebug(mmap))
776		printf(ARGS(mmap, "0x%08x, %d, %d, 0x%08x, %d, %d"),
777		    linux_args.addr, linux_args.len, linux_args.prot,
778		    linux_args.flags, linux_args.fd, linux_args.pgoff);
779#endif
780	if ((linux_args.pgoff % PAGE_SIZE) != 0)
781		return (EINVAL);
782	linux_args.pgoff /= PAGE_SIZE;
783
784	return (linux_mmap_common(td, &linux_args));
785}
786
787static int
788linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
789{
790	struct proc *p = td->td_proc;
791	struct mmap_args /* {
792		caddr_t addr;
793		size_t len;
794		int prot;
795		int flags;
796		int fd;
797		long pad;
798		off_t pos;
799	} */ bsd_args;
800	int error;
801	struct file *fp;
802
803	error = 0;
804	bsd_args.flags = 0;
805	fp = NULL;
806
807	/*
808	 * Linux mmap(2):
809	 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
810	 */
811	if (! ((linux_args->flags & LINUX_MAP_SHARED) ^
812	    (linux_args->flags & LINUX_MAP_PRIVATE)))
813		return (EINVAL);
814
815	if (linux_args->flags & LINUX_MAP_SHARED)
816		bsd_args.flags |= MAP_SHARED;
817	if (linux_args->flags & LINUX_MAP_PRIVATE)
818		bsd_args.flags |= MAP_PRIVATE;
819	if (linux_args->flags & LINUX_MAP_FIXED)
820		bsd_args.flags |= MAP_FIXED;
821	if (linux_args->flags & LINUX_MAP_ANON)
822		bsd_args.flags |= MAP_ANON;
823	else
824		bsd_args.flags |= MAP_NOSYNC;
825	if (linux_args->flags & LINUX_MAP_GROWSDOWN)
826		bsd_args.flags |= MAP_STACK;
827
828	/*
829	 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
830	 * on Linux/i386. We do this to ensure maximum compatibility.
831	 * Linux/ia64 does the same in i386 emulation mode.
832	 */
833	bsd_args.prot = linux_args->prot;
834	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
835		bsd_args.prot |= PROT_READ | PROT_EXEC;
836
837	/* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
838	bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : linux_args->fd;
839	if (bsd_args.fd != -1) {
840		/*
841		 * Linux follows Solaris mmap(2) description:
842		 * The file descriptor fildes is opened with
843		 * read permission, regardless of the
844		 * protection options specified.
845		 */
846
847		if ((error = fget(td, bsd_args.fd, &fp)) != 0)
848			return (error);
849		if (fp->f_type != DTYPE_VNODE) {
850			fdrop(fp, td);
851			return (EINVAL);
852		}
853
854		/* Linux mmap() just fails for O_WRONLY files */
855		if (!(fp->f_flag & FREAD)) {
856			fdrop(fp, td);
857			return (EACCES);
858		}
859
860		fdrop(fp, td);
861	}
862
863	if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
864		/*
865		 * The Linux MAP_GROWSDOWN option does not limit auto
866		 * growth of the region.  Linux mmap with this option
867		 * takes as addr the inital BOS, and as len, the initial
868		 * region size.  It can then grow down from addr without
869		 * limit.  However, Linux threads has an implicit internal
870		 * limit to stack size of STACK_SIZE.  Its just not
871		 * enforced explicitly in Linux.  But, here we impose
872		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
873		 * region, since we can do this with our mmap.
874		 *
875		 * Our mmap with MAP_STACK takes addr as the maximum
876		 * downsize limit on BOS, and as len the max size of
877		 * the region.  It then maps the top SGROWSIZ bytes,
878		 * and auto grows the region down, up to the limit
879		 * in addr.
880		 *
881		 * If we don't use the MAP_STACK option, the effect
882		 * of this code is to allocate a stack region of a
883		 * fixed size of (STACK_SIZE - GUARD_SIZE).
884		 */
885
886		if ((caddr_t)PTRIN(linux_args->addr) + linux_args->len >
887		    p->p_vmspace->vm_maxsaddr) {
888			/*
889			 * Some Linux apps will attempt to mmap
890			 * thread stacks near the top of their
891			 * address space.  If their TOS is greater
892			 * than vm_maxsaddr, vm_map_growstack()
893			 * will confuse the thread stack with the
894			 * process stack and deliver a SEGV if they
895			 * attempt to grow the thread stack past their
896			 * current stacksize rlimit.  To avoid this,
897			 * adjust vm_maxsaddr upwards to reflect
898			 * the current stacksize rlimit rather
899			 * than the maximum possible stacksize.
900			 * It would be better to adjust the
901			 * mmap'ed region, but some apps do not check
902			 * mmap's return value.
903			 */
904			PROC_LOCK(p);
905			p->p_vmspace->vm_maxsaddr = (char *)LINUX32_USRSTACK -
906			    lim_cur(p, RLIMIT_STACK);
907			PROC_UNLOCK(p);
908		}
909
910		/*
911		 * This gives us our maximum stack size and a new BOS.
912		 * If we're using VM_STACK, then mmap will just map
913		 * the top SGROWSIZ bytes, and let the stack grow down
914		 * to the limit at BOS.  If we're not using VM_STACK
915		 * we map the full stack, since we don't have a way
916		 * to autogrow it.
917		 */
918		if (linux_args->len > STACK_SIZE - GUARD_SIZE) {
919			bsd_args.addr = (caddr_t)PTRIN(linux_args->addr);
920			bsd_args.len = linux_args->len;
921		} else {
922			bsd_args.addr = (caddr_t)PTRIN(linux_args->addr) -
923			    (STACK_SIZE - GUARD_SIZE - linux_args->len);
924			bsd_args.len = STACK_SIZE - GUARD_SIZE;
925		}
926	} else {
927		bsd_args.addr = (caddr_t)PTRIN(linux_args->addr);
928		bsd_args.len  = linux_args->len;
929	}
930	bsd_args.pos = (off_t)linux_args->pgoff * PAGE_SIZE;
931
932#ifdef DEBUG
933	if (ldebug(mmap))
934		printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
935		    __func__,
936		    (void *)bsd_args.addr, (int)bsd_args.len, bsd_args.prot,
937		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
938#endif
939	error = mmap(td, &bsd_args);
940#ifdef DEBUG
941	if (ldebug(mmap))
942		printf("-> %s() return: 0x%x (0x%08x)\n",
943			__func__, error, (u_int)td->td_retval[0]);
944#endif
945	return (error);
946}
947
948int
949linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
950{
951	struct mprotect_args bsd_args;
952
953	bsd_args.addr = uap->addr;
954	bsd_args.len = uap->len;
955	bsd_args.prot = uap->prot;
956	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
957		bsd_args.prot |= PROT_READ | PROT_EXEC;
958	return (mprotect(td, &bsd_args));
959}
960
961int
962linux_iopl(struct thread *td, struct linux_iopl_args *args)
963{
964	int error;
965
966	if (args->level < 0 || args->level > 3)
967		return (EINVAL);
968	if ((error = priv_check(td, PRIV_IO)) != 0)
969		return (error);
970	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
971		return (error);
972	td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
973	    (args->level * (PSL_IOPL / 3));
974
975	return (0);
976}
977
978int
979linux_pipe(struct thread *td, struct linux_pipe_args *args)
980{
981	int pip[2];
982	int error;
983	register_t reg_rdx;
984
985#ifdef DEBUG
986	if (ldebug(pipe))
987		printf(ARGS(pipe, "*"));
988#endif
989
990	reg_rdx = td->td_retval[1];
991	error = pipe(td, 0);
992	if (error) {
993		td->td_retval[1] = reg_rdx;
994		return (error);
995	}
996
997	pip[0] = td->td_retval[0];
998	pip[1] = td->td_retval[1];
999	error = copyout(pip, args->pipefds, 2 * sizeof(int));
1000	if (error) {
1001		td->td_retval[1] = reg_rdx;
1002		return (error);
1003	}
1004
1005	td->td_retval[1] = reg_rdx;
1006	td->td_retval[0] = 0;
1007	return (0);
1008}
1009
1010int
1011linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
1012{
1013	l_osigaction_t osa;
1014	l_sigaction_t act, oact;
1015	int error;
1016
1017#ifdef DEBUG
1018	if (ldebug(sigaction))
1019		printf(ARGS(sigaction, "%d, %p, %p"),
1020		    args->sig, (void *)args->nsa, (void *)args->osa);
1021#endif
1022
1023	if (args->nsa != NULL) {
1024		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
1025		if (error)
1026			return (error);
1027		act.lsa_handler = osa.lsa_handler;
1028		act.lsa_flags = osa.lsa_flags;
1029		act.lsa_restorer = osa.lsa_restorer;
1030		LINUX_SIGEMPTYSET(act.lsa_mask);
1031		act.lsa_mask.__bits[0] = osa.lsa_mask;
1032	}
1033
1034	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
1035	    args->osa ? &oact : NULL);
1036
1037	if (args->osa != NULL && !error) {
1038		osa.lsa_handler = oact.lsa_handler;
1039		osa.lsa_flags = oact.lsa_flags;
1040		osa.lsa_restorer = oact.lsa_restorer;
1041		osa.lsa_mask = oact.lsa_mask.__bits[0];
1042		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
1043	}
1044
1045	return (error);
1046}
1047
1048/*
1049 * Linux has two extra args, restart and oldmask.  We don't use these,
1050 * but it seems that "restart" is actually a context pointer that
1051 * enables the signal to happen with a different register set.
1052 */
1053int
1054linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
1055{
1056	sigset_t sigmask;
1057	l_sigset_t mask;
1058
1059#ifdef DEBUG
1060	if (ldebug(sigsuspend))
1061		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
1062#endif
1063
1064	LINUX_SIGEMPTYSET(mask);
1065	mask.__bits[0] = args->mask;
1066	linux_to_bsd_sigset(&mask, &sigmask);
1067	return (kern_sigsuspend(td, sigmask));
1068}
1069
1070int
1071linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
1072{
1073	l_sigset_t lmask;
1074	sigset_t sigmask;
1075	int error;
1076
1077#ifdef DEBUG
1078	if (ldebug(rt_sigsuspend))
1079		printf(ARGS(rt_sigsuspend, "%p, %d"),
1080		    (void *)uap->newset, uap->sigsetsize);
1081#endif
1082
1083	if (uap->sigsetsize != sizeof(l_sigset_t))
1084		return (EINVAL);
1085
1086	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
1087	if (error)
1088		return (error);
1089
1090	linux_to_bsd_sigset(&lmask, &sigmask);
1091	return (kern_sigsuspend(td, sigmask));
1092}
1093
1094int
1095linux_pause(struct thread *td, struct linux_pause_args *args)
1096{
1097	struct proc *p = td->td_proc;
1098	sigset_t sigmask;
1099
1100#ifdef DEBUG
1101	if (ldebug(pause))
1102		printf(ARGS(pause, ""));
1103#endif
1104
1105	PROC_LOCK(p);
1106	sigmask = td->td_sigmask;
1107	PROC_UNLOCK(p);
1108	return (kern_sigsuspend(td, sigmask));
1109}
1110
1111int
1112linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
1113{
1114	stack_t ss, oss;
1115	l_stack_t lss;
1116	int error;
1117
1118#ifdef DEBUG
1119	if (ldebug(sigaltstack))
1120		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1121#endif
1122
1123	if (uap->uss != NULL) {
1124		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
1125		if (error)
1126			return (error);
1127
1128		ss.ss_sp = PTRIN(lss.ss_sp);
1129		ss.ss_size = lss.ss_size;
1130		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
1131	}
1132	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
1133	    (uap->uoss != NULL) ? &oss : NULL);
1134	if (!error && uap->uoss != NULL) {
1135		lss.ss_sp = PTROUT(oss.ss_sp);
1136		lss.ss_size = oss.ss_size;
1137		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1138		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
1139	}
1140
1141	return (error);
1142}
1143
1144int
1145linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
1146{
1147	struct ftruncate_args sa;
1148
1149#ifdef DEBUG
1150	if (ldebug(ftruncate64))
1151		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
1152		    (intmax_t)args->length);
1153#endif
1154
1155	sa.fd = args->fd;
1156	sa.length = args->length;
1157	return ftruncate(td, &sa);
1158}
1159
1160int
1161linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
1162{
1163	struct timeval atv;
1164	l_timeval atv32;
1165	struct timezone rtz;
1166	int error = 0;
1167
1168	if (uap->tp) {
1169		microtime(&atv);
1170		atv32.tv_sec = atv.tv_sec;
1171		atv32.tv_usec = atv.tv_usec;
1172		error = copyout(&atv32, uap->tp, sizeof(atv32));
1173	}
1174	if (error == 0 && uap->tzp != NULL) {
1175		rtz.tz_minuteswest = tz_minuteswest;
1176		rtz.tz_dsttime = tz_dsttime;
1177		error = copyout(&rtz, uap->tzp, sizeof(rtz));
1178	}
1179	return (error);
1180}
1181
1182int
1183linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
1184{
1185	l_timeval atv32;
1186	struct timeval atv, *tvp;
1187	struct timezone atz, *tzp;
1188	int error;
1189
1190	if (uap->tp) {
1191		error = copyin(uap->tp, &atv32, sizeof(atv32));
1192		if (error)
1193			return (error);
1194		atv.tv_sec = atv32.tv_sec;
1195		atv.tv_usec = atv32.tv_usec;
1196		tvp = &atv;
1197	} else
1198		tvp = NULL;
1199	if (uap->tzp) {
1200		error = copyin(uap->tzp, &atz, sizeof(atz));
1201		if (error)
1202			return (error);
1203		tzp = &atz;
1204	} else
1205		tzp = NULL;
1206	return (kern_settimeofday(td, tvp, tzp));
1207}
1208
1209int
1210linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
1211{
1212	struct l_rusage s32;
1213	struct rusage s;
1214	int error;
1215
1216	error = kern_getrusage(td, uap->who, &s);
1217	if (error != 0)
1218		return (error);
1219	if (uap->rusage != NULL) {
1220		s32.ru_utime.tv_sec = s.ru_utime.tv_sec;
1221		s32.ru_utime.tv_usec = s.ru_utime.tv_usec;
1222		s32.ru_stime.tv_sec = s.ru_stime.tv_sec;
1223		s32.ru_stime.tv_usec = s.ru_stime.tv_usec;
1224		s32.ru_maxrss = s.ru_maxrss;
1225		s32.ru_ixrss = s.ru_ixrss;
1226		s32.ru_idrss = s.ru_idrss;
1227		s32.ru_isrss = s.ru_isrss;
1228		s32.ru_minflt = s.ru_minflt;
1229		s32.ru_majflt = s.ru_majflt;
1230		s32.ru_nswap = s.ru_nswap;
1231		s32.ru_inblock = s.ru_inblock;
1232		s32.ru_oublock = s.ru_oublock;
1233		s32.ru_msgsnd = s.ru_msgsnd;
1234		s32.ru_msgrcv = s.ru_msgrcv;
1235		s32.ru_nsignals = s.ru_nsignals;
1236		s32.ru_nvcsw = s.ru_nvcsw;
1237		s32.ru_nivcsw = s.ru_nivcsw;
1238		error = copyout(&s32, uap->rusage, sizeof(s32));
1239	}
1240	return (error);
1241}
1242
1243int
1244linux_sched_rr_get_interval(struct thread *td,
1245    struct linux_sched_rr_get_interval_args *uap)
1246{
1247	struct timespec ts;
1248	struct l_timespec ts32;
1249	int error;
1250
1251	error = kern_sched_rr_get_interval(td, uap->pid, &ts);
1252	if (error != 0)
1253		return (error);
1254	ts32.tv_sec = ts.tv_sec;
1255	ts32.tv_nsec = ts.tv_nsec;
1256	return (copyout(&ts32, uap->interval, sizeof(ts32)));
1257}
1258
1259int
1260linux_set_thread_area(struct thread *td,
1261    struct linux_set_thread_area_args *args)
1262{
1263	struct l_user_desc info;
1264	struct user_segment_descriptor sd;
1265	int a[2];
1266	int error;
1267
1268	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1269	if (error)
1270		return (error);
1271
1272#ifdef DEBUG
1273	if (ldebug(set_thread_area))
1274		printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, "
1275		    "%i, %i, %i"), info.entry_number, info.base_addr,
1276		    info.limit, info.seg_32bit, info.contents,
1277		    info.read_exec_only, info.limit_in_pages,
1278		    info.seg_not_present, info.useable);
1279#endif
1280
1281	/*
1282	 * Semantics of Linux version: every thread in the system has array
1283	 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
1284	 * This syscall loads one of the selected TLS decriptors with a value
1285	 * and also loads GDT descriptors 6, 7 and 8 with the content of
1286	 * the per-thread descriptors.
1287	 *
1288	 * Semantics of FreeBSD version: I think we can ignore that Linux has
1289	 * three per-thread descriptors and use just the first one.
1290	 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
1291	 * for loading the GDT descriptors. We use just one GDT descriptor
1292	 * for TLS, so we will load just one.
1293	 *
1294	 * XXX: This doesn't work when a user space process tries to use more
1295	 * than one TLS segment. Comment in the Linux source says wine might
1296	 * do this.
1297	 */
1298
1299	/*
1300	 * GLIBC reads current %gs and call set_thread_area() with it.
1301	 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
1302	 * we use these segments.
1303	 */
1304	switch (info.entry_number) {
1305	case GUGS32_SEL:
1306	case GUDATA_SEL:
1307	case 6:
1308	case -1:
1309		info.entry_number = GUGS32_SEL;
1310		break;
1311	default:
1312		return (EINVAL);
1313	}
1314
1315	/*
1316	 * We have to copy out the GDT entry we use.
1317	 *
1318	 * XXX: What if a user space program does not check the return value
1319	 * and tries to use 6, 7 or 8?
1320	 */
1321	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1322	if (error)
1323		return (error);
1324
1325	if (LINUX_LDT_empty(&info)) {
1326		a[0] = 0;
1327		a[1] = 0;
1328	} else {
1329		a[0] = LINUX_LDT_entry_a(&info);
1330		a[1] = LINUX_LDT_entry_b(&info);
1331	}
1332
1333	memcpy(&sd, &a, sizeof(a));
1334#ifdef DEBUG
1335	if (ldebug(set_thread_area))
1336		printf("Segment created in set_thread_area: "
1337		    "lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, "
1338		    "type: %i, dpl: %i, p: %i, xx: %i, long: %i, "
1339		    "def32: %i, gran: %i\n",
1340		    sd.sd_lobase,
1341		    sd.sd_hibase,
1342		    sd.sd_lolimit,
1343		    sd.sd_hilimit,
1344		    sd.sd_type,
1345		    sd.sd_dpl,
1346		    sd.sd_p,
1347		    sd.sd_xx,
1348		    sd.sd_long,
1349		    sd.sd_def32,
1350		    sd.sd_gran);
1351#endif
1352
1353	critical_enter();
1354	td->td_pcb->pcb_gsbase = (register_t)info.base_addr;
1355	td->td_pcb->pcb_gs32sd = gdt[GUGS32_SEL] = sd;
1356	td->td_pcb->pcb_gs32p = &gdt[GUGS32_SEL];
1357	td->td_pcb->pcb_flags |= PCB_32BIT;
1358	wrmsr(MSR_KGSBASE, td->td_pcb->pcb_gsbase);
1359	critical_exit();
1360
1361	return (0);
1362}
1363