linux32_machdep.c revision 168844
1259698Sdim/*-
2259698Sdim * Copyright (c) 2004 Tim J. Robbins
3259698Sdim * Copyright (c) 2002 Doug Rabson
4259698Sdim * Copyright (c) 2000 Marcel Moolenaar
5259698Sdim * All rights reserved.
6259698Sdim *
7259698Sdim * Redistribution and use in source and binary forms, with or without
8259698Sdim * modification, are permitted provided that the following conditions
9259698Sdim * are met:
10259698Sdim * 1. Redistributions of source code must retain the above copyright
11259698Sdim *    notice, this list of conditions and the following disclaimer
12259698Sdim *    in this position and unchanged.
13259698Sdim * 2. Redistributions in binary form must reproduce the above copyright
14259698Sdim *    notice, this list of conditions and the following disclaimer in the
15259698Sdim *    documentation and/or other materials provided with the distribution.
16259698Sdim * 3. The name of the author may not be used to endorse or promote products
17259698Sdim *    derived from this software without specific prior written permission.
18259698Sdim *
19259698Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20259698Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21259698Sdim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22259698Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23259698Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24259698Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25259698Sdim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26259698Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27259698Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28259698Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29259698Sdim */
30259698Sdim
31259698Sdim#include <sys/cdefs.h>
32259698Sdim__FBSDID("$FreeBSD: head/sys/amd64/linux32/linux32_machdep.c 168844 2007-04-18 18:11:32Z jkim $");
33259698Sdim
34259698Sdim#include <sys/param.h>
35259698Sdim#include <sys/kernel.h>
36259698Sdim#include <sys/systm.h>
37259698Sdim#include <sys/file.h>
38259698Sdim#include <sys/fcntl.h>
39259698Sdim#include <sys/clock.h>
40259698Sdim#include <sys/imgact.h>
41259698Sdim#include <sys/limits.h>
42259698Sdim#include <sys/lock.h>
43259698Sdim#include <sys/malloc.h>
44259698Sdim#include <sys/mman.h>
45259698Sdim#include <sys/mutex.h>
46259698Sdim#include <sys/priv.h>
47259698Sdim#include <sys/proc.h>
48259698Sdim#include <sys/resource.h>
49259698Sdim#include <sys/resourcevar.h>
50259698Sdim#include <sys/sched.h>
51259698Sdim#include <sys/syscallsubr.h>
52259698Sdim#include <sys/sysproto.h>
53259698Sdim#include <sys/unistd.h>
54259698Sdim
55259698Sdim#include <machine/frame.h>
56259698Sdim#include <machine/pcb.h>
57259698Sdim#include <machine/psl.h>
58259698Sdim#include <machine/segments.h>
59259698Sdim#include <machine/specialreg.h>
60259698Sdim
61259698Sdim#include <vm/vm.h>
62259698Sdim#include <vm/pmap.h>
63259698Sdim#include <vm/vm_extern.h>
64259698Sdim#include <vm/vm_kern.h>
65259698Sdim#include <vm/vm_map.h>
66259698Sdim
67259698Sdim#include <amd64/linux32/linux.h>
68259698Sdim#include <amd64/linux32/linux32_proto.h>
69259698Sdim#include <compat/linux/linux_ipc.h>
70259698Sdim#include <compat/linux/linux_signal.h>
71259698Sdim#include <compat/linux/linux_util.h>
72259698Sdim#include <compat/linux/linux_emul.h>
73259698Sdim
74259698Sdimstruct l_old_select_argv {
75259698Sdim	l_int		nfds;
76259698Sdim	l_uintptr_t	readfds;
77259698Sdim	l_uintptr_t	writefds;
78259698Sdim	l_uintptr_t	exceptfds;
79259698Sdim	l_uintptr_t	timeout;
80259698Sdim} __packed;
81259698Sdim
82259698Sdimint
83259698Sdimlinux_to_bsd_sigaltstack(int lsa)
84259698Sdim{
85259698Sdim	int bsa = 0;
86259698Sdim
87259698Sdim	if (lsa & LINUX_SS_DISABLE)
88259698Sdim		bsa |= SS_DISABLE;
89259698Sdim	if (lsa & LINUX_SS_ONSTACK)
90259698Sdim		bsa |= SS_ONSTACK;
91259698Sdim	return (bsa);
92259698Sdim}
93259698Sdim
94259698Sdimint
95259698Sdimbsd_to_linux_sigaltstack(int bsa)
96259698Sdim{
97259698Sdim	int lsa = 0;
98259698Sdim
99259698Sdim	if (bsa & SS_DISABLE)
100259698Sdim		lsa |= LINUX_SS_DISABLE;
101259698Sdim	if (bsa & SS_ONSTACK)
102259698Sdim		lsa |= LINUX_SS_ONSTACK;
103259698Sdim	return (lsa);
104259698Sdim}
105259698Sdim
106259698Sdim/*
107259698Sdim * Custom version of exec_copyin_args() so that we can translate
108259698Sdim * the pointers.
109259698Sdim */
110259698Sdimstatic int
111259698Sdimlinux_exec_copyin_args(struct image_args *args, char *fname,
112259698Sdim    enum uio_seg segflg, char **argv, char **envv)
113259698Sdim{
114259698Sdim	char *argp, *envp;
115259698Sdim	u_int32_t *p32, arg;
116259698Sdim	size_t length;
117259698Sdim	int error;
118259698Sdim
119259698Sdim	bzero(args, sizeof(*args));
120259698Sdim	if (argv == NULL)
121259698Sdim		return (EFAULT);
122259698Sdim
123259698Sdim	/*
124259698Sdim	 * Allocate temporary demand zeroed space for argument and
125259698Sdim	 *	environment strings
126259698Sdim	 */
127259698Sdim	args->buf = (char *)kmem_alloc_wait(exec_map,
128259698Sdim	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
129259698Sdim	if (args->buf == NULL)
130259698Sdim		return (ENOMEM);
131259698Sdim	args->begin_argv = args->buf;
132259698Sdim	args->endp = args->begin_argv;
133259698Sdim	args->stringspace = ARG_MAX;
134259698Sdim
135259698Sdim	args->fname = args->buf + ARG_MAX;
136259698Sdim
137259698Sdim	/*
138259698Sdim	 * Copy the file name.
139259698Sdim	 */
140259698Sdim	error = (segflg == UIO_SYSSPACE) ?
141259698Sdim	    copystr(fname, args->fname, PATH_MAX, &length) :
142259698Sdim	    copyinstr(fname, args->fname, PATH_MAX, &length);
143259698Sdim	if (error != 0)
144259698Sdim		goto err_exit;
145259698Sdim
146259698Sdim	/*
147259698Sdim	 * extract arguments first
148259698Sdim	 */
149259698Sdim	p32 = (u_int32_t *)argv;
150259698Sdim	for (;;) {
151259698Sdim		error = copyin(p32++, &arg, sizeof(arg));
152259698Sdim		if (error)
153259698Sdim			goto err_exit;
154259698Sdim		if (arg == 0)
155259698Sdim			break;
156259698Sdim		argp = PTRIN(arg);
157259698Sdim		error = copyinstr(argp, args->endp, args->stringspace, &length);
158259698Sdim		if (error) {
159259698Sdim			if (error == ENAMETOOLONG)
160259698Sdim				error = E2BIG;
161259698Sdim
162259698Sdim			goto err_exit;
163259698Sdim		}
164259698Sdim		args->stringspace -= length;
165259698Sdim		args->endp += length;
166259698Sdim		args->argc++;
167259698Sdim	}
168259698Sdim
169259698Sdim	args->begin_envv = args->endp;
170259698Sdim
171259698Sdim	/*
172259698Sdim	 * extract environment strings
173259698Sdim	 */
174259698Sdim	if (envv) {
175259698Sdim		p32 = (u_int32_t *)envv;
176259698Sdim		for (;;) {
177259698Sdim			error = copyin(p32++, &arg, sizeof(arg));
178259698Sdim			if (error)
179259698Sdim				goto err_exit;
180259698Sdim			if (arg == 0)
181259698Sdim				break;
182259698Sdim			envp = PTRIN(arg);
183259698Sdim			error = copyinstr(envp, args->endp, args->stringspace,
184259698Sdim			    &length);
185259698Sdim			if (error) {
186259698Sdim				if (error == ENAMETOOLONG)
187259698Sdim					error = E2BIG;
188259698Sdim				goto err_exit;
189259698Sdim			}
190259698Sdim			args->stringspace -= length;
191259698Sdim			args->endp += length;
192259698Sdim			args->envc++;
193259698Sdim		}
194259698Sdim	}
195259698Sdim
196259698Sdim	return (0);
197259698Sdim
198259698Sdimerr_exit:
199259698Sdim	kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
200259698Sdim	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
201259698Sdim	args->buf = NULL;
202259698Sdim	return (error);
203259698Sdim}
204259698Sdim
205259698Sdimint
206259698Sdimlinux_execve(struct thread *td, struct linux_execve_args *args)
207259698Sdim{
208259698Sdim	struct image_args eargs;
209259698Sdim	char *path;
210259698Sdim	int error;
211259698Sdim
212259698Sdim	LCONVPATHEXIST(td, args->path, &path);
213259698Sdim
214259698Sdim#ifdef DEBUG
215259698Sdim	if (ldebug(execve))
216259698Sdim		printf(ARGS(execve, "%s"), path);
217259698Sdim#endif
218259698Sdim
219259698Sdim	error = linux_exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
220259698Sdim	    args->envp);
221259698Sdim	free(path, M_TEMP);
222259698Sdim	if (error == 0)
223259698Sdim		error = kern_execve(td, &eargs, NULL);
224259698Sdim	if (error == 0)
225259698Sdim		/* Linux process can execute FreeBSD one, do not attempt
226259698Sdim		 * to create emuldata for such process using
227259698Sdim		 * linux_proc_init, this leads to a panic on KASSERT
228259698Sdim		 * because such process has p->p_emuldata == NULL.
229259698Sdim		 */
230259698Sdim	   	if (td->td_proc->p_sysent == &elf_linux_sysvec)
231259698Sdim			error = linux_proc_init(td, 0, 0);
232259698Sdim	return (error);
233259698Sdim}
234259698Sdim
235259698Sdimstruct iovec32 {
236259698Sdim	u_int32_t iov_base;
237259698Sdim	int	iov_len;
238259698Sdim};
239259698Sdim
240259698SdimCTASSERT(sizeof(struct iovec32) == 8);
241259698Sdim
242259698Sdimstatic int
243259698Sdimlinux32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
244259698Sdim{
245259698Sdim	struct iovec32 iov32;
246259698Sdim	struct iovec *iov;
247259698Sdim	struct uio *uio;
248259698Sdim	u_int iovlen;
249259698Sdim	int error, i;
250259698Sdim
251259698Sdim	*uiop = NULL;
252259698Sdim	if (iovcnt > UIO_MAXIOV)
253259698Sdim		return (EINVAL);
254259698Sdim	iovlen = iovcnt * sizeof(struct iovec);
255259698Sdim	uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
256259698Sdim	iov = (struct iovec *)(uio + 1);
257259698Sdim	for (i = 0; i < iovcnt; i++) {
258259698Sdim		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
259259698Sdim		if (error) {
260259698Sdim			free(uio, M_IOV);
261259698Sdim			return (error);
262259698Sdim		}
263259698Sdim		iov[i].iov_base = PTRIN(iov32.iov_base);
264259698Sdim		iov[i].iov_len = iov32.iov_len;
265259698Sdim	}
266259698Sdim	uio->uio_iov = iov;
267259698Sdim	uio->uio_iovcnt = iovcnt;
268259698Sdim	uio->uio_segflg = UIO_USERSPACE;
269259698Sdim	uio->uio_offset = -1;
270259698Sdim	uio->uio_resid = 0;
271259698Sdim	for (i = 0; i < iovcnt; i++) {
272259698Sdim		if (iov->iov_len > INT_MAX - uio->uio_resid) {
273259698Sdim			free(uio, M_IOV);
274259698Sdim			return (EINVAL);
275259698Sdim		}
276259698Sdim		uio->uio_resid += iov->iov_len;
277259698Sdim		iov++;
278259698Sdim	}
279259698Sdim	*uiop = uio;
280259698Sdim	return (0);
281259698Sdim}
282259698Sdim
283259698Sdimint
284259698Sdimlinux_readv(struct thread *td, struct linux_readv_args *uap)
285259698Sdim{
286259698Sdim	struct uio *auio;
287259698Sdim	int error;
288259698Sdim
289259698Sdim	error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
290259698Sdim	if (error)
291259698Sdim		return (error);
292259698Sdim	error = kern_readv(td, uap->fd, auio);
293259698Sdim	free(auio, M_IOV);
294259698Sdim	return (error);
295259698Sdim}
296259698Sdim
297259698Sdimint
298259698Sdimlinux_writev(struct thread *td, struct linux_writev_args *uap)
299259698Sdim{
300259698Sdim	struct uio *auio;
301259698Sdim	int error;
302259698Sdim
303259698Sdim	error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
304	if (error)
305		return (error);
306	error = kern_writev(td, uap->fd, auio);
307	free(auio, M_IOV);
308	return (error);
309}
310
311struct l_ipc_kludge {
312	l_uintptr_t msgp;
313	l_long msgtyp;
314} __packed;
315
316int
317linux_ipc(struct thread *td, struct linux_ipc_args *args)
318{
319
320	switch (args->what & 0xFFFF) {
321	case LINUX_SEMOP: {
322		struct linux_semop_args a;
323
324		a.semid = args->arg1;
325		a.tsops = args->ptr;
326		a.nsops = args->arg2;
327		return (linux_semop(td, &a));
328	}
329	case LINUX_SEMGET: {
330		struct linux_semget_args a;
331
332		a.key = args->arg1;
333		a.nsems = args->arg2;
334		a.semflg = args->arg3;
335		return (linux_semget(td, &a));
336	}
337	case LINUX_SEMCTL: {
338		struct linux_semctl_args a;
339		int error;
340
341		a.semid = args->arg1;
342		a.semnum = args->arg2;
343		a.cmd = args->arg3;
344		error = copyin(args->ptr, &a.arg, sizeof(a.arg));
345		if (error)
346			return (error);
347		return (linux_semctl(td, &a));
348	}
349	case LINUX_MSGSND: {
350		struct linux_msgsnd_args a;
351
352		a.msqid = args->arg1;
353		a.msgp = args->ptr;
354		a.msgsz = args->arg2;
355		a.msgflg = args->arg3;
356		return (linux_msgsnd(td, &a));
357	}
358	case LINUX_MSGRCV: {
359		struct linux_msgrcv_args a;
360
361		a.msqid = args->arg1;
362		a.msgsz = args->arg2;
363		a.msgflg = args->arg3;
364		if ((args->what >> 16) == 0) {
365			struct l_ipc_kludge tmp;
366			int error;
367
368			if (args->ptr == 0)
369				return (EINVAL);
370			error = copyin(args->ptr, &tmp, sizeof(tmp));
371			if (error)
372				return (error);
373			a.msgp = PTRIN(tmp.msgp);
374			a.msgtyp = tmp.msgtyp;
375		} else {
376			a.msgp = args->ptr;
377			a.msgtyp = args->arg5;
378		}
379		return (linux_msgrcv(td, &a));
380	}
381	case LINUX_MSGGET: {
382		struct linux_msgget_args a;
383
384		a.key = args->arg1;
385		a.msgflg = args->arg2;
386		return (linux_msgget(td, &a));
387	}
388	case LINUX_MSGCTL: {
389		struct linux_msgctl_args a;
390
391		a.msqid = args->arg1;
392		a.cmd = args->arg2;
393		a.buf = args->ptr;
394		return (linux_msgctl(td, &a));
395	}
396	case LINUX_SHMAT: {
397		struct linux_shmat_args a;
398
399		a.shmid = args->arg1;
400		a.shmaddr = args->ptr;
401		a.shmflg = args->arg2;
402		a.raddr = PTRIN((l_uint)args->arg3);
403		return (linux_shmat(td, &a));
404	}
405	case LINUX_SHMDT: {
406		struct linux_shmdt_args a;
407
408		a.shmaddr = args->ptr;
409		return (linux_shmdt(td, &a));
410	}
411	case LINUX_SHMGET: {
412		struct linux_shmget_args a;
413
414		a.key = args->arg1;
415		a.size = args->arg2;
416		a.shmflg = args->arg3;
417		return (linux_shmget(td, &a));
418	}
419	case LINUX_SHMCTL: {
420		struct linux_shmctl_args a;
421
422		a.shmid = args->arg1;
423		a.cmd = args->arg2;
424		a.buf = args->ptr;
425		return (linux_shmctl(td, &a));
426	}
427	default:
428		break;
429	}
430
431	return (EINVAL);
432}
433
434int
435linux_old_select(struct thread *td, struct linux_old_select_args *args)
436{
437	struct l_old_select_argv linux_args;
438	struct linux_select_args newsel;
439	int error;
440
441#ifdef DEBUG
442	if (ldebug(old_select))
443		printf(ARGS(old_select, "%p"), args->ptr);
444#endif
445
446	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
447	if (error)
448		return (error);
449
450	newsel.nfds = linux_args.nfds;
451	newsel.readfds = PTRIN(linux_args.readfds);
452	newsel.writefds = PTRIN(linux_args.writefds);
453	newsel.exceptfds = PTRIN(linux_args.exceptfds);
454	newsel.timeout = PTRIN(linux_args.timeout);
455	return (linux_select(td, &newsel));
456}
457
458int
459linux_fork(struct thread *td, struct linux_fork_args *args)
460{
461	int error;
462	struct proc *p2;
463	struct thread *td2;
464
465#ifdef DEBUG
466	if (ldebug(fork))
467		printf(ARGS(fork, ""));
468#endif
469
470	if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0)
471		return (error);
472
473	if (error == 0) {
474		td->td_retval[0] = p2->p_pid;
475		td->td_retval[1] = 0;
476	}
477
478	if (td->td_retval[1] == 1)
479		td->td_retval[0] = 0;
480	error = linux_proc_init(td, td->td_retval[0], 0);
481	if (error)
482		return (error);
483
484	td2 = FIRST_THREAD_IN_PROC(p2);
485
486	/*
487	 * Make this runnable after we are finished with it.
488	 */
489	mtx_lock_spin(&sched_lock);
490	TD_SET_CAN_RUN(td2);
491	sched_add(td2, SRQ_BORING);
492	mtx_unlock_spin(&sched_lock);
493
494	return (0);
495}
496
497int
498linux_vfork(struct thread *td, struct linux_vfork_args *args)
499{
500	int error;
501	struct proc *p2;
502	struct thread *td2;
503
504#ifdef DEBUG
505	if (ldebug(vfork))
506		printf(ARGS(vfork, ""));
507#endif
508
509	/* Exclude RFPPWAIT */
510	if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0)
511		return (error);
512	if (error == 0) {
513	   	td->td_retval[0] = p2->p_pid;
514		td->td_retval[1] = 0;
515	}
516	/* Are we the child? */
517	if (td->td_retval[1] == 1)
518		td->td_retval[0] = 0;
519	error = linux_proc_init(td, td->td_retval[0], 0);
520	if (error)
521		return (error);
522
523	PROC_LOCK(p2);
524	p2->p_flag |= P_PPWAIT;
525	PROC_UNLOCK(p2);
526
527	td2 = FIRST_THREAD_IN_PROC(p2);
528
529	/* make it run */
530	mtx_lock_spin(&sched_lock);
531	TD_SET_CAN_RUN(td2);
532	sched_add(td2, SRQ_BORING);
533	mtx_unlock_spin(&sched_lock);
534
535	/* wait for the children to exit, ie. emulate vfork */
536	PROC_LOCK(p2);
537	while (p2->p_flag & P_PPWAIT)
538	   	msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0);
539	PROC_UNLOCK(p2);
540
541	return (0);
542}
543
544int
545linux_clone(struct thread *td, struct linux_clone_args *args)
546{
547	int error, ff = RFPROC | RFSTOPPED;
548	struct proc *p2;
549	struct thread *td2;
550	int exit_signal;
551	struct linux_emuldata *em;
552
553#ifdef DEBUG
554	if (ldebug(clone)) {
555		printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
556		    "child tid: %p"), (unsigned)args->flags,
557		    args->stack, args->parent_tidptr, args->child_tidptr);
558	}
559#endif
560
561	exit_signal = args->flags & 0x000000ff;
562	if (!LINUX_SIG_VALID(exit_signal) && exit_signal != 0)
563		return (EINVAL);
564
565	if (exit_signal <= LINUX_SIGTBLSZ)
566		exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
567
568	if (args->flags & LINUX_CLONE_VM)
569		ff |= RFMEM;
570	if (args->flags & LINUX_CLONE_SIGHAND)
571		ff |= RFSIGSHARE;
572	/*
573	 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
574	 * and open files is independant.  In FreeBSD, its in one
575	 * structure but in reality it does not make any problems
576	 * because both of these flags are set at once usually.
577	 */
578	if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
579		ff |= RFFDG;
580
581	/*
582	 * Attempt to detect when linux_clone(2) is used for creating
583	 * kernel threads. Unfortunately despite the existence of the
584	 * CLONE_THREAD flag, version of linuxthreads package used in
585	 * most popular distros as of beginning of 2005 doesn't make
586	 * any use of it. Therefore, this detection relies on
587	 * empirical observation that linuxthreads sets certain
588	 * combination of flags, so that we can make more or less
589	 * precise detection and notify the FreeBSD kernel that several
590	 * processes are in fact part of the same threading group, so
591	 * that special treatment is necessary for signal delivery
592	 * between those processes and fd locking.
593	 */
594	if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
595		ff |= RFTHREAD;
596
597	if (args->flags & LINUX_CLONE_PARENT_SETTID)
598		if (args->parent_tidptr == NULL)
599			return (EINVAL);
600
601	error = fork1(td, ff, 0, &p2);
602	if (error)
603		return (error);
604
605	if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
606	   	sx_xlock(&proctree_lock);
607		PROC_LOCK(p2);
608		proc_reparent(p2, td->td_proc->p_pptr);
609		PROC_UNLOCK(p2);
610		sx_xunlock(&proctree_lock);
611	}
612
613	/* create the emuldata */
614	error = linux_proc_init(td, p2->p_pid, args->flags);
615	/* reference it - no need to check this */
616	em = em_find(p2, EMUL_DOLOCK);
617	KASSERT(em != NULL, ("clone: emuldata not found.\n"));
618	/* and adjust it */
619
620	if (args->flags & LINUX_CLONE_THREAD) {
621#ifdef notyet
622	   	PROC_LOCK(p2);
623	   	p2->p_pgrp = td->td_proc->p_pgrp;
624	   	PROC_UNLOCK(p2);
625#endif
626		exit_signal = 0;
627	}
628
629	if (args->flags & LINUX_CLONE_CHILD_SETTID)
630		em->child_set_tid = args->child_tidptr;
631	else
632	   	em->child_set_tid = NULL;
633
634	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
635		em->child_clear_tid = args->child_tidptr;
636	else
637	   	em->child_clear_tid = NULL;
638
639	EMUL_UNLOCK(&emul_lock);
640
641	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
642		error = copyout(&p2->p_pid, args->parent_tidptr,
643		    sizeof(p2->p_pid));
644		if (error)
645			printf(LMSG("copyout failed!"));
646	}
647
648	PROC_LOCK(p2);
649	p2->p_sigparent = exit_signal;
650	PROC_UNLOCK(p2);
651	td2 = FIRST_THREAD_IN_PROC(p2);
652	/*
653	 * In a case of stack = NULL, we are supposed to COW calling process
654	 * stack. This is what normal fork() does, so we just keep tf_rsp arg
655	 * intact.
656	 */
657	if (args->stack)
658		td2->td_frame->tf_rsp = PTROUT(args->stack);
659
660	if (args->flags & LINUX_CLONE_SETTLS) {
661		struct user_segment_descriptor sd;
662		struct l_user_desc info;
663	   	int a[2];
664
665	   	error = copyin((void *)td->td_frame->tf_rsi, &info,
666		    sizeof(struct l_user_desc));
667		if (error) {
668			printf(LMSG("copyin failed!"));
669		} else {
670			/* We might copy out the entry_number as GUGS32_SEL. */
671		   	info.entry_number = GUGS32_SEL;
672			error = copyout(&info, (void *)td->td_frame->tf_rsi,
673			    sizeof(struct l_user_desc));
674			if (error)
675				printf(LMSG("copyout failed!"));
676
677			a[0] = LINUX_LDT_entry_a(&info);
678			a[1] = LINUX_LDT_entry_b(&info);
679
680			memcpy(&sd, &a, sizeof(a));
681#ifdef DEBUG
682			if (ldebug(clone))
683				printf("Segment created in clone with "
684				    "CLONE_SETTLS: lobase: %x, hibase: %x, "
685				    "lolimit: %x, hilimit: %x, type: %i, "
686				    "dpl: %i, p: %i, xx: %i, long: %i, "
687				    "def32: %i, gran: %i\n", sd.sd_lobase,
688				    sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit,
689				    sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
690				    sd.sd_long, sd.sd_def32, sd.sd_gran);
691#endif
692			td2->td_pcb->pcb_gsbase = (register_t)info.base_addr;
693			td2->td_pcb->pcb_gs32sd = sd;
694			td2->td_pcb->pcb_gs32p = &gdt[GUGS32_SEL];
695			td2->td_pcb->pcb_gs = GSEL(GUGS32_SEL, SEL_UPL);
696			td2->td_pcb->pcb_flags |= PCB_32BIT;
697		}
698	}
699
700#ifdef DEBUG
701	if (ldebug(clone))
702		printf(LMSG("clone: successful rfork to %d, "
703		    "stack %p sig = %d"), (int)p2->p_pid, args->stack,
704		    exit_signal);
705#endif
706	if (args->flags & LINUX_CLONE_VFORK) {
707	   	PROC_LOCK(p2);
708	   	p2->p_flag |= P_PPWAIT;
709	   	PROC_UNLOCK(p2);
710	}
711
712	/*
713	 * Make this runnable after we are finished with it.
714	 */
715	mtx_lock_spin(&sched_lock);
716	TD_SET_CAN_RUN(td2);
717	sched_add(td2, SRQ_BORING);
718	mtx_unlock_spin(&sched_lock);
719
720	td->td_retval[0] = p2->p_pid;
721	td->td_retval[1] = 0;
722
723	if (args->flags & LINUX_CLONE_VFORK) {
724		/* wait for the children to exit, ie. emulate vfork */
725		PROC_LOCK(p2);
726		while (p2->p_flag & P_PPWAIT)
727			msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0);
728		PROC_UNLOCK(p2);
729	}
730
731	return (0);
732}
733
734#define STACK_SIZE  (2 * 1024 * 1024)
735#define GUARD_SIZE  (4 * PAGE_SIZE)
736
737static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
738
739int
740linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
741{
742	struct l_mmap_argv linux_args;
743
744#ifdef DEBUG
745	if (ldebug(mmap2))
746		printf(ARGS(mmap2, "0x%08x, %d, %d, 0x%08x, %d, %d"),
747		    args->addr, args->len, args->prot,
748		    args->flags, args->fd, args->pgoff);
749#endif
750
751	linux_args.addr = PTROUT(args->addr);
752	linux_args.len = args->len;
753	linux_args.prot = args->prot;
754	linux_args.flags = args->flags;
755	linux_args.fd = args->fd;
756	linux_args.pgoff = args->pgoff;
757
758	return (linux_mmap_common(td, &linux_args));
759}
760
761int
762linux_mmap(struct thread *td, struct linux_mmap_args *args)
763{
764	int error;
765	struct l_mmap_argv linux_args;
766
767	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
768	if (error)
769		return (error);
770
771#ifdef DEBUG
772	if (ldebug(mmap))
773		printf(ARGS(mmap, "0x%08x, %d, %d, 0x%08x, %d, %d"),
774		    linux_args.addr, linux_args.len, linux_args.prot,
775		    linux_args.flags, linux_args.fd, linux_args.pgoff);
776#endif
777	if ((linux_args.pgoff % PAGE_SIZE) != 0)
778		return (EINVAL);
779	linux_args.pgoff /= PAGE_SIZE;
780
781	return (linux_mmap_common(td, &linux_args));
782}
783
784static int
785linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
786{
787	struct proc *p = td->td_proc;
788	struct mmap_args /* {
789		caddr_t addr;
790		size_t len;
791		int prot;
792		int flags;
793		int fd;
794		long pad;
795		off_t pos;
796	} */ bsd_args;
797	int error;
798	struct file *fp;
799
800	error = 0;
801	bsd_args.flags = 0;
802	fp = NULL;
803
804	/*
805	 * Linux mmap(2):
806	 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
807	 */
808	if (! ((linux_args->flags & LINUX_MAP_SHARED) ^
809	    (linux_args->flags & LINUX_MAP_PRIVATE)))
810		return (EINVAL);
811
812	if (linux_args->flags & LINUX_MAP_SHARED)
813		bsd_args.flags |= MAP_SHARED;
814	if (linux_args->flags & LINUX_MAP_PRIVATE)
815		bsd_args.flags |= MAP_PRIVATE;
816	if (linux_args->flags & LINUX_MAP_FIXED)
817		bsd_args.flags |= MAP_FIXED;
818	if (linux_args->flags & LINUX_MAP_ANON)
819		bsd_args.flags |= MAP_ANON;
820	else
821		bsd_args.flags |= MAP_NOSYNC;
822	if (linux_args->flags & LINUX_MAP_GROWSDOWN)
823		bsd_args.flags |= MAP_STACK;
824
825	/*
826	 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
827	 * on Linux/i386. We do this to ensure maximum compatibility.
828	 * Linux/ia64 does the same in i386 emulation mode.
829	 */
830	bsd_args.prot = linux_args->prot;
831	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
832		bsd_args.prot |= PROT_READ | PROT_EXEC;
833
834	/* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
835	bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : linux_args->fd;
836	if (bsd_args.fd != -1) {
837		/*
838		 * Linux follows Solaris mmap(2) description:
839		 * The file descriptor fildes is opened with
840		 * read permission, regardless of the
841		 * protection options specified.
842		 */
843
844		if ((error = fget(td, bsd_args.fd, &fp)) != 0)
845			return (error);
846		if (fp->f_type != DTYPE_VNODE) {
847			fdrop(fp, td);
848			return (EINVAL);
849		}
850
851		/* Linux mmap() just fails for O_WRONLY files */
852		if (!(fp->f_flag & FREAD)) {
853			fdrop(fp, td);
854			return (EACCES);
855		}
856
857		fdrop(fp, td);
858	}
859
860	if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
861		/*
862		 * The Linux MAP_GROWSDOWN option does not limit auto
863		 * growth of the region.  Linux mmap with this option
864		 * takes as addr the inital BOS, and as len, the initial
865		 * region size.  It can then grow down from addr without
866		 * limit.  However, Linux threads has an implicit internal
867		 * limit to stack size of STACK_SIZE.  Its just not
868		 * enforced explicitly in Linux.  But, here we impose
869		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
870		 * region, since we can do this with our mmap.
871		 *
872		 * Our mmap with MAP_STACK takes addr as the maximum
873		 * downsize limit on BOS, and as len the max size of
874		 * the region.  It them maps the top SGROWSIZ bytes,
875		 * and auto grows the region down, up to the limit
876		 * in addr.
877		 *
878		 * If we don't use the MAP_STACK option, the effect
879		 * of this code is to allocate a stack region of a
880		 * fixed size of (STACK_SIZE - GUARD_SIZE).
881		 */
882
883		if ((caddr_t)PTRIN(linux_args->addr) + linux_args->len >
884		    p->p_vmspace->vm_maxsaddr) {
885			/*
886			 * Some Linux apps will attempt to mmap
887			 * thread stacks near the top of their
888			 * address space.  If their TOS is greater
889			 * than vm_maxsaddr, vm_map_growstack()
890			 * will confuse the thread stack with the
891			 * process stack and deliver a SEGV if they
892			 * attempt to grow the thread stack past their
893			 * current stacksize rlimit.  To avoid this,
894			 * adjust vm_maxsaddr upwards to reflect
895			 * the current stacksize rlimit rather
896			 * than the maximum possible stacksize.
897			 * It would be better to adjust the
898			 * mmap'ed region, but some apps do not check
899			 * mmap's return value.
900			 */
901			PROC_LOCK(p);
902			p->p_vmspace->vm_maxsaddr = (char *)LINUX32_USRSTACK -
903			    lim_cur(p, RLIMIT_STACK);
904			PROC_UNLOCK(p);
905		}
906
907		/* This gives us our maximum stack size */
908		if (linux_args->len > STACK_SIZE - GUARD_SIZE)
909			bsd_args.len = linux_args->len;
910		else
911			bsd_args.len  = STACK_SIZE - GUARD_SIZE;
912
913		/*
914		 * This gives us a new BOS.  If we're using VM_STACK, then
915		 * mmap will just map the top SGROWSIZ bytes, and let
916		 * the stack grow down to the limit at BOS.  If we're
917		 * not using VM_STACK we map the full stack, since we
918		 * don't have a way to autogrow it.
919		 */
920		bsd_args.addr = (caddr_t)PTRIN(linux_args->addr) -
921		    bsd_args.len;
922	} else {
923		bsd_args.addr = (caddr_t)PTRIN(linux_args->addr);
924		bsd_args.len  = linux_args->len;
925	}
926	bsd_args.pos = (off_t)linux_args->pgoff * PAGE_SIZE;
927	bsd_args.pad = 0;
928
929#ifdef DEBUG
930	if (ldebug(mmap))
931		printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
932		    __func__,
933		    (void *)bsd_args.addr, (int)bsd_args.len, bsd_args.prot,
934		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
935#endif
936	error = mmap(td, &bsd_args);
937#ifdef DEBUG
938	if (ldebug(mmap))
939		printf("-> %s() return: 0x%x (0x%08x)\n",
940			__func__, error, (u_int)td->td_retval[0]);
941#endif
942	return (error);
943}
944
945int
946linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
947{
948	struct mprotect_args bsd_args;
949
950	bsd_args.addr = uap->addr;
951	bsd_args.len = uap->len;
952	bsd_args.prot = uap->prot;
953	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
954		bsd_args.prot |= PROT_READ | PROT_EXEC;
955	return (mprotect(td, &bsd_args));
956}
957
958int
959linux_iopl(struct thread *td, struct linux_iopl_args *args)
960{
961	int error;
962
963	if (args->level < 0 || args->level > 3)
964		return (EINVAL);
965	if ((error = priv_check(td, PRIV_IO)) != 0)
966		return (error);
967	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
968		return (error);
969	td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
970	    (args->level * (PSL_IOPL / 3));
971
972	return (0);
973}
974
975int
976linux_pipe(struct thread *td, struct linux_pipe_args *args)
977{
978	int pip[2];
979	int error;
980	register_t reg_rdx;
981
982#ifdef DEBUG
983	if (ldebug(pipe))
984		printf(ARGS(pipe, "*"));
985#endif
986
987	reg_rdx = td->td_retval[1];
988	error = pipe(td, 0);
989	if (error) {
990		td->td_retval[1] = reg_rdx;
991		return (error);
992	}
993
994	pip[0] = td->td_retval[0];
995	pip[1] = td->td_retval[1];
996	error = copyout(pip, args->pipefds, 2 * sizeof(int));
997	if (error) {
998		td->td_retval[1] = reg_rdx;
999		return (error);
1000	}
1001
1002	td->td_retval[1] = reg_rdx;
1003	td->td_retval[0] = 0;
1004	return (0);
1005}
1006
1007int
1008linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
1009{
1010	l_osigaction_t osa;
1011	l_sigaction_t act, oact;
1012	int error;
1013
1014#ifdef DEBUG
1015	if (ldebug(sigaction))
1016		printf(ARGS(sigaction, "%d, %p, %p"),
1017		    args->sig, (void *)args->nsa, (void *)args->osa);
1018#endif
1019
1020	if (args->nsa != NULL) {
1021		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
1022		if (error)
1023			return (error);
1024		act.lsa_handler = osa.lsa_handler;
1025		act.lsa_flags = osa.lsa_flags;
1026		act.lsa_restorer = osa.lsa_restorer;
1027		LINUX_SIGEMPTYSET(act.lsa_mask);
1028		act.lsa_mask.__bits[0] = osa.lsa_mask;
1029	}
1030
1031	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
1032	    args->osa ? &oact : NULL);
1033
1034	if (args->osa != NULL && !error) {
1035		osa.lsa_handler = oact.lsa_handler;
1036		osa.lsa_flags = oact.lsa_flags;
1037		osa.lsa_restorer = oact.lsa_restorer;
1038		osa.lsa_mask = oact.lsa_mask.__bits[0];
1039		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
1040	}
1041
1042	return (error);
1043}
1044
1045/*
1046 * Linux has two extra args, restart and oldmask.  We don't use these,
1047 * but it seems that "restart" is actually a context pointer that
1048 * enables the signal to happen with a different register set.
1049 */
1050int
1051linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
1052{
1053	sigset_t sigmask;
1054	l_sigset_t mask;
1055
1056#ifdef DEBUG
1057	if (ldebug(sigsuspend))
1058		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
1059#endif
1060
1061	LINUX_SIGEMPTYSET(mask);
1062	mask.__bits[0] = args->mask;
1063	linux_to_bsd_sigset(&mask, &sigmask);
1064	return (kern_sigsuspend(td, sigmask));
1065}
1066
1067int
1068linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
1069{
1070	l_sigset_t lmask;
1071	sigset_t sigmask;
1072	int error;
1073
1074#ifdef DEBUG
1075	if (ldebug(rt_sigsuspend))
1076		printf(ARGS(rt_sigsuspend, "%p, %d"),
1077		    (void *)uap->newset, uap->sigsetsize);
1078#endif
1079
1080	if (uap->sigsetsize != sizeof(l_sigset_t))
1081		return (EINVAL);
1082
1083	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
1084	if (error)
1085		return (error);
1086
1087	linux_to_bsd_sigset(&lmask, &sigmask);
1088	return (kern_sigsuspend(td, sigmask));
1089}
1090
1091int
1092linux_pause(struct thread *td, struct linux_pause_args *args)
1093{
1094	struct proc *p = td->td_proc;
1095	sigset_t sigmask;
1096
1097#ifdef DEBUG
1098	if (ldebug(pause))
1099		printf(ARGS(pause, ""));
1100#endif
1101
1102	PROC_LOCK(p);
1103	sigmask = td->td_sigmask;
1104	PROC_UNLOCK(p);
1105	return (kern_sigsuspend(td, sigmask));
1106}
1107
1108int
1109linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
1110{
1111	stack_t ss, oss;
1112	l_stack_t lss;
1113	int error;
1114
1115#ifdef DEBUG
1116	if (ldebug(sigaltstack))
1117		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1118#endif
1119
1120	if (uap->uss != NULL) {
1121		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
1122		if (error)
1123			return (error);
1124
1125		ss.ss_sp = PTRIN(lss.ss_sp);
1126		ss.ss_size = lss.ss_size;
1127		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
1128	}
1129	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
1130	    (uap->uoss != NULL) ? &oss : NULL);
1131	if (!error && uap->uoss != NULL) {
1132		lss.ss_sp = PTROUT(oss.ss_sp);
1133		lss.ss_size = oss.ss_size;
1134		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1135		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
1136	}
1137
1138	return (error);
1139}
1140
1141int
1142linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
1143{
1144	struct ftruncate_args sa;
1145
1146#ifdef DEBUG
1147	if (ldebug(ftruncate64))
1148		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
1149		    (intmax_t)args->length);
1150#endif
1151
1152	sa.fd = args->fd;
1153	sa.pad = 0;
1154	sa.length = args->length;
1155	return ftruncate(td, &sa);
1156}
1157
1158int
1159linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
1160{
1161	struct timeval atv;
1162	l_timeval atv32;
1163	struct timezone rtz;
1164	int error = 0;
1165
1166	if (uap->tp) {
1167		microtime(&atv);
1168		atv32.tv_sec = atv.tv_sec;
1169		atv32.tv_usec = atv.tv_usec;
1170		error = copyout(&atv32, uap->tp, sizeof(atv32));
1171	}
1172	if (error == 0 && uap->tzp != NULL) {
1173		rtz.tz_minuteswest = tz_minuteswest;
1174		rtz.tz_dsttime = tz_dsttime;
1175		error = copyout(&rtz, uap->tzp, sizeof(rtz));
1176	}
1177	return (error);
1178}
1179
1180int
1181linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
1182{
1183	l_timeval atv32;
1184	struct timeval atv, *tvp;
1185	struct timezone atz, *tzp;
1186	int error;
1187
1188	if (uap->tp) {
1189		error = copyin(uap->tp, &atv32, sizeof(atv32));
1190		if (error)
1191			return (error);
1192		atv.tv_sec = atv32.tv_sec;
1193		atv.tv_usec = atv32.tv_usec;
1194		tvp = &atv;
1195	} else
1196		tvp = NULL;
1197	if (uap->tzp) {
1198		error = copyin(uap->tzp, &atz, sizeof(atz));
1199		if (error)
1200			return (error);
1201		tzp = &atz;
1202	} else
1203		tzp = NULL;
1204	return (kern_settimeofday(td, tvp, tzp));
1205}
1206
1207int
1208linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
1209{
1210	struct l_rusage s32;
1211	struct rusage s;
1212	int error;
1213
1214	error = kern_getrusage(td, uap->who, &s);
1215	if (error != 0)
1216		return (error);
1217	if (uap->rusage != NULL) {
1218		s32.ru_utime.tv_sec = s.ru_utime.tv_sec;
1219		s32.ru_utime.tv_usec = s.ru_utime.tv_usec;
1220		s32.ru_stime.tv_sec = s.ru_stime.tv_sec;
1221		s32.ru_stime.tv_usec = s.ru_stime.tv_usec;
1222		s32.ru_maxrss = s.ru_maxrss;
1223		s32.ru_ixrss = s.ru_ixrss;
1224		s32.ru_idrss = s.ru_idrss;
1225		s32.ru_isrss = s.ru_isrss;
1226		s32.ru_minflt = s.ru_minflt;
1227		s32.ru_majflt = s.ru_majflt;
1228		s32.ru_nswap = s.ru_nswap;
1229		s32.ru_inblock = s.ru_inblock;
1230		s32.ru_oublock = s.ru_oublock;
1231		s32.ru_msgsnd = s.ru_msgsnd;
1232		s32.ru_msgrcv = s.ru_msgrcv;
1233		s32.ru_nsignals = s.ru_nsignals;
1234		s32.ru_nvcsw = s.ru_nvcsw;
1235		s32.ru_nivcsw = s.ru_nivcsw;
1236		error = copyout(&s32, uap->rusage, sizeof(s32));
1237	}
1238	return (error);
1239}
1240
1241int
1242linux_sched_rr_get_interval(struct thread *td,
1243    struct linux_sched_rr_get_interval_args *uap)
1244{
1245	struct timespec ts;
1246	struct l_timespec ts32;
1247	int error;
1248
1249	error = kern_sched_rr_get_interval(td, uap->pid, &ts);
1250	if (error != 0)
1251		return (error);
1252	ts32.tv_sec = ts.tv_sec;
1253	ts32.tv_nsec = ts.tv_nsec;
1254	return (copyout(&ts32, uap->interval, sizeof(ts32)));
1255}
1256
1257int
1258linux_set_thread_area(struct thread *td,
1259    struct linux_set_thread_area_args *args)
1260{
1261	struct l_user_desc info;
1262	struct user_segment_descriptor sd;
1263	int a[2];
1264	int error;
1265
1266	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1267	if (error)
1268		return (error);
1269
1270#ifdef DEBUG
1271	if (ldebug(set_thread_area))
1272	   	printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, "
1273		    "%i, %i, %i"), info.entry_number, info.base_addr,
1274		    info.limit, info.seg_32bit, info.contents,
1275		    info.read_exec_only, info.limit_in_pages,
1276		    info.seg_not_present, info.useable);
1277#endif
1278
1279	/*
1280	 * Semantics of Linux version: every thread in the system has array
1281	 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
1282	 * This syscall loads one of the selected TLS decriptors with a value
1283	 * and also loads GDT descriptors 6, 7 and 8 with the content of
1284	 * the per-thread descriptors.
1285	 *
1286	 * Semantics of FreeBSD version: I think we can ignore that Linux has
1287	 * three per-thread descriptors and use just the first one.
1288	 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
1289	 * for loading the GDT descriptors. We use just one GDT descriptor
1290	 * for TLS, so we will load just one.
1291	 * XXX: This doesnt work when user-space process tries to use more
1292	 * than one TLS segment. Comment in the Linux source says wine might
1293	 * do that.
1294	 */
1295
1296	/*
1297	 * GLIBC reads current %gs and call set_thread_area() with it.
1298	 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
1299	 * we use these segments.
1300	 */
1301	switch (info.entry_number) {
1302	case GUGS32_SEL:
1303	case GUDATA_SEL:
1304	case 6:
1305	case -1:
1306		info.entry_number = GUGS32_SEL;
1307		break;
1308	default:
1309		return (EINVAL);
1310	}
1311
1312	/*
1313	 * We have to copy out the GDT entry we use.
1314	 * XXX: What if userspace program does not check return value and
1315	 * tries to use 6, 7 or 8?
1316	 */
1317	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1318	if (error)
1319		return (error);
1320
1321	if (LINUX_LDT_empty(&info)) {
1322		a[0] = 0;
1323		a[1] = 0;
1324	} else {
1325		a[0] = LINUX_LDT_entry_a(&info);
1326		a[1] = LINUX_LDT_entry_b(&info);
1327	}
1328
1329	memcpy(&sd, &a, sizeof(a));
1330#ifdef DEBUG
1331	if (ldebug(set_thread_area))
1332		printf("Segment created in set_thread_area: "
1333		    "lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, "
1334		    "type: %i, dpl: %i, p: %i, xx: %i, long: %i, "
1335		    "def32: %i, gran: %i\n",
1336		    sd.sd_lobase,
1337		    sd.sd_hibase,
1338		    sd.sd_lolimit,
1339		    sd.sd_hilimit,
1340		    sd.sd_type,
1341		    sd.sd_dpl,
1342		    sd.sd_p,
1343		    sd.sd_xx,
1344		    sd.sd_long,
1345		    sd.sd_def32,
1346		    sd.sd_gran);
1347#endif
1348
1349	critical_enter();
1350	td->td_pcb->pcb_gsbase = (register_t)info.base_addr;
1351	td->td_pcb->pcb_gs32sd = gdt[GUGS32_SEL] = sd;
1352	td->td_pcb->pcb_gs32p = &gdt[GUGS32_SEL];
1353	td->td_pcb->pcb_flags |= PCB_32BIT;
1354	wrmsr(MSR_KGSBASE, td->td_pcb->pcb_gsbase);
1355	critical_exit();
1356
1357	return (0);
1358}
1359