linux_machdep.c revision 166395
1/*-
2 * Copyright (c) 2000 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/i386/linux/linux_machdep.c 166395 2007-02-01 13:27:52Z kib $");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/file.h>
35#include <sys/fcntl.h>
36#include <sys/imgact.h>
37#include <sys/lock.h>
38#include <sys/malloc.h>
39#include <sys/mman.h>
40#include <sys/mutex.h>
41#include <sys/sx.h>
42#include <sys/priv.h>
43#include <sys/proc.h>
44#include <sys/queue.h>
45#include <sys/resource.h>
46#include <sys/resourcevar.h>
47#include <sys/signalvar.h>
48#include <sys/syscallsubr.h>
49#include <sys/sysproto.h>
50#include <sys/unistd.h>
51#include <sys/wait.h>
52#include <sys/sched.h>
53
54#include <machine/frame.h>
55#include <machine/psl.h>
56#include <machine/segments.h>
57#include <machine/sysarch.h>
58
59#include <vm/vm.h>
60#include <vm/pmap.h>
61#include <vm/vm_map.h>
62
63#include <i386/linux/linux.h>
64#include <i386/linux/linux_proto.h>
65#include <compat/linux/linux_ipc.h>
66#include <compat/linux/linux_signal.h>
67#include <compat/linux/linux_util.h>
68#include <compat/linux/linux_emul.h>
69
70#include <i386/include/pcb.h>			/* needed for pcb definition in linux_set_thread_area */
71
72#include "opt_posix.h"
73
74extern struct sysentvec elf32_freebsd_sysvec;	/* defined in i386/i386/elf_machdep.c */
75
76struct l_descriptor {
77	l_uint		entry_number;
78	l_ulong		base_addr;
79	l_uint		limit;
80	l_uint		seg_32bit:1;
81	l_uint		contents:2;
82	l_uint		read_exec_only:1;
83	l_uint		limit_in_pages:1;
84	l_uint		seg_not_present:1;
85	l_uint		useable:1;
86};
87
88struct l_old_select_argv {
89	l_int		nfds;
90	l_fd_set	*readfds;
91	l_fd_set	*writefds;
92	l_fd_set	*exceptfds;
93	struct l_timeval	*timeout;
94};
95
96int
97linux_to_bsd_sigaltstack(int lsa)
98{
99	int bsa = 0;
100
101	if (lsa & LINUX_SS_DISABLE)
102		bsa |= SS_DISABLE;
103	if (lsa & LINUX_SS_ONSTACK)
104		bsa |= SS_ONSTACK;
105	return (bsa);
106}
107
108int
109bsd_to_linux_sigaltstack(int bsa)
110{
111	int lsa = 0;
112
113	if (bsa & SS_DISABLE)
114		lsa |= LINUX_SS_DISABLE;
115	if (bsa & SS_ONSTACK)
116		lsa |= LINUX_SS_ONSTACK;
117	return (lsa);
118}
119
120int
121linux_execve(struct thread *td, struct linux_execve_args *args)
122{
123	int error;
124	char *newpath;
125	struct image_args eargs;
126
127	LCONVPATHEXIST(td, args->path, &newpath);
128
129#ifdef DEBUG
130	if (ldebug(execve))
131		printf(ARGS(execve, "%s"), newpath);
132#endif
133
134	error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
135	    args->argp, args->envp);
136	free(newpath, M_TEMP);
137	if (error == 0)
138		error = kern_execve(td, &eargs, NULL);
139	if (error == 0)
140	   	/* linux process can exec fbsd one, dont attempt
141		 * to create emuldata for such process using
142		 * linux_proc_init, this leads to a panic on KASSERT
143		 * because such process has p->p_emuldata == NULL
144		 */
145	   	if (td->td_proc->p_sysent == &elf_linux_sysvec)
146   		   	error = linux_proc_init(td, 0, 0);
147	return (error);
148}
149
150struct l_ipc_kludge {
151	struct l_msgbuf *msgp;
152	l_long msgtyp;
153};
154
155int
156linux_ipc(struct thread *td, struct linux_ipc_args *args)
157{
158
159	switch (args->what & 0xFFFF) {
160	case LINUX_SEMOP: {
161		struct linux_semop_args a;
162
163		a.semid = args->arg1;
164		a.tsops = args->ptr;
165		a.nsops = args->arg2;
166		return (linux_semop(td, &a));
167	}
168	case LINUX_SEMGET: {
169		struct linux_semget_args a;
170
171		a.key = args->arg1;
172		a.nsems = args->arg2;
173		a.semflg = args->arg3;
174		return (linux_semget(td, &a));
175	}
176	case LINUX_SEMCTL: {
177		struct linux_semctl_args a;
178		int error;
179
180		a.semid = args->arg1;
181		a.semnum = args->arg2;
182		a.cmd = args->arg3;
183		error = copyin(args->ptr, &a.arg, sizeof(a.arg));
184		if (error)
185			return (error);
186		return (linux_semctl(td, &a));
187	}
188	case LINUX_MSGSND: {
189		struct linux_msgsnd_args a;
190
191		a.msqid = args->arg1;
192		a.msgp = args->ptr;
193		a.msgsz = args->arg2;
194		a.msgflg = args->arg3;
195		return (linux_msgsnd(td, &a));
196	}
197	case LINUX_MSGRCV: {
198		struct linux_msgrcv_args a;
199
200		a.msqid = args->arg1;
201		a.msgsz = args->arg2;
202		a.msgflg = args->arg3;
203		if ((args->what >> 16) == 0) {
204			struct l_ipc_kludge tmp;
205			int error;
206
207			if (args->ptr == NULL)
208				return (EINVAL);
209			error = copyin(args->ptr, &tmp, sizeof(tmp));
210			if (error)
211				return (error);
212			a.msgp = tmp.msgp;
213			a.msgtyp = tmp.msgtyp;
214		} else {
215			a.msgp = args->ptr;
216			a.msgtyp = args->arg5;
217		}
218		return (linux_msgrcv(td, &a));
219	}
220	case LINUX_MSGGET: {
221		struct linux_msgget_args a;
222
223		a.key = args->arg1;
224		a.msgflg = args->arg2;
225		return (linux_msgget(td, &a));
226	}
227	case LINUX_MSGCTL: {
228		struct linux_msgctl_args a;
229
230		a.msqid = args->arg1;
231		a.cmd = args->arg2;
232		a.buf = args->ptr;
233		return (linux_msgctl(td, &a));
234	}
235	case LINUX_SHMAT: {
236		struct linux_shmat_args a;
237
238		a.shmid = args->arg1;
239		a.shmaddr = args->ptr;
240		a.shmflg = args->arg2;
241		a.raddr = (l_ulong *)args->arg3;
242		return (linux_shmat(td, &a));
243	}
244	case LINUX_SHMDT: {
245		struct linux_shmdt_args a;
246
247		a.shmaddr = args->ptr;
248		return (linux_shmdt(td, &a));
249	}
250	case LINUX_SHMGET: {
251		struct linux_shmget_args a;
252
253		a.key = args->arg1;
254		a.size = args->arg2;
255		a.shmflg = args->arg3;
256		return (linux_shmget(td, &a));
257	}
258	case LINUX_SHMCTL: {
259		struct linux_shmctl_args a;
260
261		a.shmid = args->arg1;
262		a.cmd = args->arg2;
263		a.buf = args->ptr;
264		return (linux_shmctl(td, &a));
265	}
266	default:
267		break;
268	}
269
270	return (EINVAL);
271}
272
273int
274linux_old_select(struct thread *td, struct linux_old_select_args *args)
275{
276	struct l_old_select_argv linux_args;
277	struct linux_select_args newsel;
278	int error;
279
280#ifdef DEBUG
281	if (ldebug(old_select))
282		printf(ARGS(old_select, "%p"), args->ptr);
283#endif
284
285	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
286	if (error)
287		return (error);
288
289	newsel.nfds = linux_args.nfds;
290	newsel.readfds = linux_args.readfds;
291	newsel.writefds = linux_args.writefds;
292	newsel.exceptfds = linux_args.exceptfds;
293	newsel.timeout = linux_args.timeout;
294	return (linux_select(td, &newsel));
295}
296
297int
298linux_fork(struct thread *td, struct linux_fork_args *args)
299{
300	int error;
301	struct proc *p2;
302	struct thread *td2;
303
304#ifdef DEBUG
305	if (ldebug(fork))
306		printf(ARGS(fork, ""));
307#endif
308
309	if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0)
310		return (error);
311
312	if (error == 0) {
313		td->td_retval[0] = p2->p_pid;
314		td->td_retval[1] = 0;
315	}
316
317	if (td->td_retval[1] == 1)
318		td->td_retval[0] = 0;
319	error = linux_proc_init(td, td->td_retval[0], 0);
320	if (error)
321		return (error);
322
323	td2 = FIRST_THREAD_IN_PROC(p2);
324
325	/*
326	 * Make this runnable after we are finished with it.
327	 */
328	mtx_lock_spin(&sched_lock);
329	TD_SET_CAN_RUN(td2);
330	sched_add(td2, SRQ_BORING);
331	mtx_unlock_spin(&sched_lock);
332
333	return (0);
334}
335
336int
337linux_vfork(struct thread *td, struct linux_vfork_args *args)
338{
339	int error;
340	struct proc *p2;
341	struct thread *td2;
342
343#ifdef DEBUG
344	if (ldebug(vfork))
345		printf(ARGS(vfork, ""));
346#endif
347
348	/* exclude RFPPWAIT */
349	if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0)
350		return (error);
351	if (error == 0) {
352		td->td_retval[0] = p2->p_pid;
353		td->td_retval[1] = 0;
354	}
355	/* Are we the child? */
356	if (td->td_retval[1] == 1)
357		td->td_retval[0] = 0;
358	error = linux_proc_init(td, td->td_retval[0], 0);
359	if (error)
360		return (error);
361
362	PROC_LOCK(p2);
363	p2->p_flag |= P_PPWAIT;
364	PROC_UNLOCK(p2);
365
366	td2 = FIRST_THREAD_IN_PROC(p2);
367
368	/*
369	 * Make this runnable after we are finished with it.
370	 */
371	mtx_lock_spin(&sched_lock);
372	TD_SET_CAN_RUN(td2);
373	sched_add(td2, SRQ_BORING);
374	mtx_unlock_spin(&sched_lock);
375
376	/* wait for the children to exit, ie. emulate vfork */
377	PROC_LOCK(p2);
378	while (p2->p_flag & P_PPWAIT)
379	   	msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0);
380	PROC_UNLOCK(p2);
381
382	return (0);
383}
384
385int
386linux_clone(struct thread *td, struct linux_clone_args *args)
387{
388	int error, ff = RFPROC | RFSTOPPED;
389	struct proc *p2;
390	struct thread *td2;
391	int exit_signal;
392	struct linux_emuldata *em;
393
394#ifdef DEBUG
395	if (ldebug(clone)) {
396   	   	printf(ARGS(clone, "flags %x, stack %x, parent tid: %x, child tid: %x"),
397		    (unsigned int)args->flags, (unsigned int)args->stack,
398		    (unsigned int)args->parent_tidptr, (unsigned int)args->child_tidptr);
399	}
400#endif
401
402	exit_signal = args->flags & 0x000000ff;
403	if (!LINUX_SIG_VALID(exit_signal) && exit_signal != 0)
404		return (EINVAL);
405
406	if (exit_signal <= LINUX_SIGTBLSZ)
407		exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
408
409	if (args->flags & CLONE_VM)
410		ff |= RFMEM;
411	if (args->flags & CLONE_SIGHAND)
412		ff |= RFSIGSHARE;
413	/*
414	 * XXX: in linux sharing of fs info (chroot/cwd/umask)
415	 * and open files is independant. in fbsd its in one
416	 * structure but in reality it doesnt make any problems
417	 * because both this flags are set at once usually.
418	 */
419	if (!(args->flags & (CLONE_FILES | CLONE_FS)))
420		ff |= RFFDG;
421
422	/*
423	 * Attempt to detect when linux_clone(2) is used for creating
424	 * kernel threads. Unfortunately despite the existence of the
425	 * CLONE_THREAD flag, version of linuxthreads package used in
426	 * most popular distros as of beginning of 2005 doesn't make
427	 * any use of it. Therefore, this detection relay fully on
428	 * empirical observation that linuxthreads sets certain
429	 * combination of flags, so that we can make more or less
430	 * precise detection and notify the FreeBSD kernel that several
431	 * processes are in fact part of the same threading group, so
432	 * that special treatment is necessary for signal delivery
433	 * between those processes and fd locking.
434	 */
435	if ((args->flags & 0xffffff00) == THREADING_FLAGS)
436		ff |= RFTHREAD;
437
438	error = fork1(td, ff, 0, &p2);
439	if (error)
440		return (error);
441
442	if (args->flags & (CLONE_PARENT|CLONE_THREAD)) {
443	   	sx_xlock(&proctree_lock);
444		PROC_LOCK(p2);
445		proc_reparent(p2, td->td_proc->p_pptr);
446		PROC_UNLOCK(p2);
447		sx_xunlock(&proctree_lock);
448	}
449
450	/* create the emuldata */
451	error = linux_proc_init(td, p2->p_pid, args->flags);
452	/* reference it - no need to check this */
453	em = em_find(p2, EMUL_DOLOCK);
454	KASSERT(em != NULL, ("clone: emuldata not found.\n"));
455	/* and adjust it */
456	if (args->flags & CLONE_PARENT_SETTID) {
457	   	if (args->parent_tidptr == NULL) {
458		   	EMUL_UNLOCK(&emul_lock);
459			return (EINVAL);
460		}
461		error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid));
462		if (error) {
463		   	EMUL_UNLOCK(&emul_lock);
464			return (error);
465		}
466	}
467
468	if (args->flags & CLONE_THREAD) {
469	   	/* XXX: linux mangles pgrp and pptr somehow
470		 * I think it might be this but I am not sure.
471		 */
472#ifdef notyet
473	   	PROC_LOCK(p2);
474	   	p2->p_pgrp = td->td_proc->p_pgrp;
475	   	PROC_UNLOCK(p2);
476#endif
477	 	exit_signal = 0;
478	}
479
480	if (args->flags & CLONE_CHILD_SETTID)
481		em->child_set_tid = args->child_tidptr;
482	else
483	   	em->child_set_tid = NULL;
484
485	if (args->flags & CLONE_CHILD_CLEARTID)
486		em->child_clear_tid = args->child_tidptr;
487	else
488	   	em->child_clear_tid = NULL;
489
490	EMUL_UNLOCK(&emul_lock);
491
492	PROC_LOCK(p2);
493	p2->p_sigparent = exit_signal;
494	PROC_UNLOCK(p2);
495	td2 = FIRST_THREAD_IN_PROC(p2);
496	/*
497	 * in a case of stack = NULL we are supposed to COW calling process stack
498	 * this is what normal fork() does so we just keep the tf_esp arg intact
499	 */
500	if (args->stack)
501   	   	td2->td_frame->tf_esp = (unsigned int)args->stack;
502
503	if (args->flags & CLONE_SETTLS) {
504   	   	struct l_user_desc info;
505   	   	int idx;
506	   	int a[2];
507		struct segment_descriptor sd;
508
509	   	error = copyin((void *)td->td_frame->tf_esi, &info, sizeof(struct l_user_desc));
510		if (error)
511   		   	return (error);
512
513		idx = info.entry_number;
514
515		/*
516		 * looks like we're getting the idx we returned
517		 * in the set_thread_area() syscall
518		 */
519		if (idx != 6 && idx != 3)
520			return (EINVAL);
521
522		/* this doesnt happen in practice */
523		if (idx == 6) {
524		   	/* we might copy out the entry_number as 3 */
525		   	info.entry_number = 3;
526			error = copyout(&info, (void *) td->td_frame->tf_esi, sizeof(struct l_user_desc));
527			if (error)
528	   		   	return (error);
529		}
530
531		a[0] = LDT_entry_a(&info);
532		a[1] = LDT_entry_b(&info);
533
534		memcpy(&sd, &a, sizeof(a));
535#ifdef DEBUG
536	if (ldebug(clone))
537	   	printf("Segment created in clone with CLONE_SETTLS: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
538			sd.sd_hibase,
539			sd.sd_lolimit,
540			sd.sd_hilimit,
541			sd.sd_type,
542			sd.sd_dpl,
543			sd.sd_p,
544			sd.sd_xx,
545			sd.sd_def32,
546			sd.sd_gran);
547#endif
548
549		/* set %gs */
550		td2->td_pcb->pcb_gsd = sd;
551		td2->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
552	}
553
554#ifdef DEBUG
555	if (ldebug(clone))
556		printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
557		    (long)p2->p_pid, args->stack, exit_signal);
558#endif
559	if (args->flags & CLONE_VFORK) {
560	   	PROC_LOCK(p2);
561		p2->p_flag |= P_PPWAIT;
562	   	PROC_UNLOCK(p2);
563	}
564
565	/*
566	 * Make this runnable after we are finished with it.
567	 */
568	mtx_lock_spin(&sched_lock);
569	TD_SET_CAN_RUN(td2);
570	sched_add(td2, SRQ_BORING);
571	mtx_unlock_spin(&sched_lock);
572
573	td->td_retval[0] = p2->p_pid;
574	td->td_retval[1] = 0;
575
576	if (args->flags & CLONE_VFORK) {
577   	   	/* wait for the children to exit, ie. emulate vfork */
578   	   	PROC_LOCK(p2);
579		while (p2->p_flag & P_PPWAIT)
580   		   	msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0);
581		PROC_UNLOCK(p2);
582	}
583
584	return (0);
585}
586
587/* XXX move */
588struct l_mmap_argv {
589	l_caddr_t	addr;
590	l_int		len;
591	l_int		prot;
592	l_int		flags;
593	l_int		fd;
594	l_int		pos;
595};
596
597#define STACK_SIZE  (2 * 1024 * 1024)
598#define GUARD_SIZE  (4 * PAGE_SIZE)
599
600static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
601
602int
603linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
604{
605	struct l_mmap_argv linux_args;
606
607#ifdef DEBUG
608	if (ldebug(mmap2))
609		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
610		    (void *)args->addr, args->len, args->prot,
611		    args->flags, args->fd, args->pgoff);
612#endif
613
614	linux_args.addr = (l_caddr_t)args->addr;
615	linux_args.len = args->len;
616	linux_args.prot = args->prot;
617	linux_args.flags = args->flags;
618	linux_args.fd = args->fd;
619	linux_args.pos = args->pgoff * PAGE_SIZE;
620
621	return (linux_mmap_common(td, &linux_args));
622}
623
624int
625linux_mmap(struct thread *td, struct linux_mmap_args *args)
626{
627	int error;
628	struct l_mmap_argv linux_args;
629
630	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
631	if (error)
632		return (error);
633
634#ifdef DEBUG
635	if (ldebug(mmap))
636		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
637		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
638		    linux_args.flags, linux_args.fd, linux_args.pos);
639#endif
640
641	return (linux_mmap_common(td, &linux_args));
642}
643
644static int
645linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
646{
647	struct proc *p = td->td_proc;
648	struct mmap_args /* {
649		caddr_t addr;
650		size_t len;
651		int prot;
652		int flags;
653		int fd;
654		long pad;
655		off_t pos;
656	} */ bsd_args;
657	int error;
658	struct file *fp;
659
660	error = 0;
661	bsd_args.flags = 0;
662	fp = NULL;
663
664	/*
665	 * Linux mmap(2):
666	 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
667	 */
668	if (! ((linux_args->flags & LINUX_MAP_SHARED) ^
669	    (linux_args->flags & LINUX_MAP_PRIVATE)))
670		return (EINVAL);
671
672	if (linux_args->flags & LINUX_MAP_SHARED)
673		bsd_args.flags |= MAP_SHARED;
674	if (linux_args->flags & LINUX_MAP_PRIVATE)
675		bsd_args.flags |= MAP_PRIVATE;
676	if (linux_args->flags & LINUX_MAP_FIXED)
677		bsd_args.flags |= MAP_FIXED;
678	if (linux_args->flags & LINUX_MAP_ANON)
679		bsd_args.flags |= MAP_ANON;
680	else
681		bsd_args.flags |= MAP_NOSYNC;
682	if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
683		bsd_args.flags |= MAP_STACK;
684
685		/*
686		 * The linux MAP_GROWSDOWN option does not limit auto
687		 * growth of the region.  Linux mmap with this option
688		 * takes as addr the inital BOS, and as len, the initial
689		 * region size.  It can then grow down from addr without
690		 * limit.  However, linux threads has an implicit internal
691		 * limit to stack size of STACK_SIZE.  Its just not
692		 * enforced explicitly in linux.  But, here we impose
693		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
694		 * region, since we can do this with our mmap.
695		 *
696		 * Our mmap with MAP_STACK takes addr as the maximum
697		 * downsize limit on BOS, and as len the max size of
698		 * the region.  It them maps the top SGROWSIZ bytes,
699		 * and autgrows the region down, up to the limit
700		 * in addr.
701		 *
702		 * If we don't use the MAP_STACK option, the effect
703		 * of this code is to allocate a stack region of a
704		 * fixed size of (STACK_SIZE - GUARD_SIZE).
705		 */
706
707		/* This gives us TOS */
708		bsd_args.addr = linux_args->addr + linux_args->len;
709
710		if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
711			/*
712			 * Some linux apps will attempt to mmap
713			 * thread stacks near the top of their
714			 * address space.  If their TOS is greater
715			 * than vm_maxsaddr, vm_map_growstack()
716			 * will confuse the thread stack with the
717			 * process stack and deliver a SEGV if they
718			 * attempt to grow the thread stack past their
719			 * current stacksize rlimit.  To avoid this,
720			 * adjust vm_maxsaddr upwards to reflect
721			 * the current stacksize rlimit rather
722			 * than the maximum possible stacksize.
723			 * It would be better to adjust the
724			 * mmap'ed region, but some apps do not check
725			 * mmap's return value.
726			 */
727			PROC_LOCK(p);
728			p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
729			    lim_cur(p, RLIMIT_STACK);
730			PROC_UNLOCK(p);
731		}
732
733		/* This gives us our maximum stack size */
734		if (linux_args->len > STACK_SIZE - GUARD_SIZE)
735			bsd_args.len = linux_args->len;
736		else
737			bsd_args.len  = STACK_SIZE - GUARD_SIZE;
738
739		/*
740		 * This gives us a new BOS.  If we're using VM_STACK, then
741		 * mmap will just map the top SGROWSIZ bytes, and let
742		 * the stack grow down to the limit at BOS.  If we're
743		 * not using VM_STACK we map the full stack, since we
744		 * don't have a way to autogrow it.
745		 */
746		bsd_args.addr -= bsd_args.len;
747	} else {
748		bsd_args.addr = linux_args->addr;
749		bsd_args.len  = linux_args->len;
750	}
751
752	bsd_args.prot = linux_args->prot;
753	if (linux_args->flags & LINUX_MAP_ANON)
754		bsd_args.fd = -1;
755	else {
756		/*
757		 * Linux follows Solaris mmap(2) description:
758		 * The file descriptor fildes is opened with
759		 * read permission, regardless of the
760		 * protection options specified.
761		 * If PROT_WRITE is specified, the application
762		 * must have opened the file descriptor
763		 * fildes with write permission unless
764		 * MAP_PRIVATE is specified in the flag
765		 * argument as described below.
766		 */
767
768		if ((error = fget(td, linux_args->fd, &fp)) != 0)
769			return (error);
770		if (fp->f_type != DTYPE_VNODE) {
771			fdrop(fp, td);
772			return (EINVAL);
773		}
774
775		/* Linux mmap() just fails for O_WRONLY files */
776		if (! (fp->f_flag & FREAD)) {
777			fdrop(fp, td);
778			return (EACCES);
779		}
780
781		bsd_args.fd = linux_args->fd;
782		fdrop(fp, td);
783	}
784	bsd_args.pos = linux_args->pos;
785	bsd_args.pad = 0;
786
787#ifdef DEBUG
788	if (ldebug(mmap))
789		printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
790		    __func__,
791		    (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
792		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
793#endif
794	error = mmap(td, &bsd_args);
795#ifdef DEBUG
796	if (ldebug(mmap))
797		printf("-> %s() return: 0x%x (0x%08x)\n",
798			__func__, error, (u_int)td->td_retval[0]);
799#endif
800	return (error);
801}
802
803int
804linux_pipe(struct thread *td, struct linux_pipe_args *args)
805{
806	int error;
807	int reg_edx;
808
809#ifdef DEBUG
810	if (ldebug(pipe))
811		printf(ARGS(pipe, "*"));
812#endif
813
814	reg_edx = td->td_retval[1];
815	error = pipe(td, 0);
816	if (error) {
817		td->td_retval[1] = reg_edx;
818		return (error);
819	}
820
821	error = copyout(td->td_retval, args->pipefds, 2*sizeof(int));
822	if (error) {
823		td->td_retval[1] = reg_edx;
824		return (error);
825	}
826
827	td->td_retval[1] = reg_edx;
828	td->td_retval[0] = 0;
829	return (0);
830}
831
832int
833linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
834{
835	int error;
836	struct i386_ioperm_args iia;
837
838	iia.start = args->start;
839	iia.length = args->length;
840	iia.enable = args->enable;
841	mtx_lock(&Giant);
842	error = i386_set_ioperm(td, &iia);
843	mtx_unlock(&Giant);
844	return (error);
845}
846
847int
848linux_iopl(struct thread *td, struct linux_iopl_args *args)
849{
850	int error;
851
852	if (args->level < 0 || args->level > 3)
853		return (EINVAL);
854	if ((error = priv_check(td, PRIV_IO)) != 0)
855		return (error);
856	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
857		return (error);
858	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
859	    (args->level * (PSL_IOPL / 3));
860	return (0);
861}
862
863int
864linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
865{
866	int error;
867	struct i386_ldt_args ldt;
868	struct l_descriptor ld;
869	union descriptor desc;
870
871	if (uap->ptr == NULL)
872		return (EINVAL);
873
874	switch (uap->func) {
875	case 0x00: /* read_ldt */
876		ldt.start = 0;
877		ldt.descs = uap->ptr;
878		ldt.num = uap->bytecount / sizeof(union descriptor);
879		mtx_lock(&Giant);
880		error = i386_get_ldt(td, &ldt);
881		td->td_retval[0] *= sizeof(union descriptor);
882		mtx_unlock(&Giant);
883		break;
884	case 0x01: /* write_ldt */
885	case 0x11: /* write_ldt */
886		if (uap->bytecount != sizeof(ld))
887			return (EINVAL);
888
889		error = copyin(uap->ptr, &ld, sizeof(ld));
890		if (error)
891			return (error);
892
893		ldt.start = ld.entry_number;
894		ldt.descs = &desc;
895		ldt.num = 1;
896		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
897		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
898		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
899		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
900		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
901			(ld.contents << 2);
902		desc.sd.sd_dpl = 3;
903		desc.sd.sd_p = (ld.seg_not_present ^ 1);
904		desc.sd.sd_xx = 0;
905		desc.sd.sd_def32 = ld.seg_32bit;
906		desc.sd.sd_gran = ld.limit_in_pages;
907		mtx_lock(&Giant);
908		error = i386_set_ldt(td, &ldt, &desc);
909		mtx_unlock(&Giant);
910		break;
911	default:
912		error = EINVAL;
913		break;
914	}
915
916	if (error == EOPNOTSUPP) {
917		printf("linux: modify_ldt needs kernel option USER_LDT\n");
918		error = ENOSYS;
919	}
920
921	return (error);
922}
923
924int
925linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
926{
927	l_osigaction_t osa;
928	l_sigaction_t act, oact;
929	int error;
930
931#ifdef DEBUG
932	if (ldebug(sigaction))
933		printf(ARGS(sigaction, "%d, %p, %p"),
934		    args->sig, (void *)args->nsa, (void *)args->osa);
935#endif
936
937	if (args->nsa != NULL) {
938		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
939		if (error)
940			return (error);
941		act.lsa_handler = osa.lsa_handler;
942		act.lsa_flags = osa.lsa_flags;
943		act.lsa_restorer = osa.lsa_restorer;
944		LINUX_SIGEMPTYSET(act.lsa_mask);
945		act.lsa_mask.__bits[0] = osa.lsa_mask;
946	}
947
948	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
949	    args->osa ? &oact : NULL);
950
951	if (args->osa != NULL && !error) {
952		osa.lsa_handler = oact.lsa_handler;
953		osa.lsa_flags = oact.lsa_flags;
954		osa.lsa_restorer = oact.lsa_restorer;
955		osa.lsa_mask = oact.lsa_mask.__bits[0];
956		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
957	}
958
959	return (error);
960}
961
962/*
963 * Linux has two extra args, restart and oldmask.  We dont use these,
964 * but it seems that "restart" is actually a context pointer that
965 * enables the signal to happen with a different register set.
966 */
967int
968linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
969{
970	sigset_t sigmask;
971	l_sigset_t mask;
972
973#ifdef DEBUG
974	if (ldebug(sigsuspend))
975		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
976#endif
977
978	LINUX_SIGEMPTYSET(mask);
979	mask.__bits[0] = args->mask;
980	linux_to_bsd_sigset(&mask, &sigmask);
981	return (kern_sigsuspend(td, sigmask));
982}
983
984int
985linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
986{
987	l_sigset_t lmask;
988	sigset_t sigmask;
989	int error;
990
991#ifdef DEBUG
992	if (ldebug(rt_sigsuspend))
993		printf(ARGS(rt_sigsuspend, "%p, %d"),
994		    (void *)uap->newset, uap->sigsetsize);
995#endif
996
997	if (uap->sigsetsize != sizeof(l_sigset_t))
998		return (EINVAL);
999
1000	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
1001	if (error)
1002		return (error);
1003
1004	linux_to_bsd_sigset(&lmask, &sigmask);
1005	return (kern_sigsuspend(td, sigmask));
1006}
1007
1008int
1009linux_pause(struct thread *td, struct linux_pause_args *args)
1010{
1011	struct proc *p = td->td_proc;
1012	sigset_t sigmask;
1013
1014#ifdef DEBUG
1015	if (ldebug(pause))
1016		printf(ARGS(pause, ""));
1017#endif
1018
1019	PROC_LOCK(p);
1020	sigmask = td->td_sigmask;
1021	PROC_UNLOCK(p);
1022	return (kern_sigsuspend(td, sigmask));
1023}
1024
1025int
1026linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
1027{
1028	stack_t ss, oss;
1029	l_stack_t lss;
1030	int error;
1031
1032#ifdef DEBUG
1033	if (ldebug(sigaltstack))
1034		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1035#endif
1036
1037	if (uap->uss != NULL) {
1038		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
1039		if (error)
1040			return (error);
1041
1042		ss.ss_sp = lss.ss_sp;
1043		ss.ss_size = lss.ss_size;
1044		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
1045	}
1046	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
1047	    (uap->uoss != NULL) ? &oss : NULL);
1048	if (!error && uap->uoss != NULL) {
1049		lss.ss_sp = oss.ss_sp;
1050		lss.ss_size = oss.ss_size;
1051		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1052		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
1053	}
1054
1055	return (error);
1056}
1057
1058int
1059linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
1060{
1061	struct ftruncate_args sa;
1062
1063#ifdef DEBUG
1064	if (ldebug(ftruncate64))
1065		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
1066		    (intmax_t)args->length);
1067#endif
1068
1069	sa.fd = args->fd;
1070	sa.pad = 0;
1071	sa.length = args->length;
1072	return ftruncate(td, &sa);
1073}
1074
1075int
1076linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
1077{
1078	struct l_user_desc info;
1079	int error;
1080	int idx;
1081	int a[2];
1082	struct segment_descriptor sd;
1083
1084	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1085	if (error)
1086		return (error);
1087
1088#ifdef DEBUG
1089	if (ldebug(set_thread_area))
1090	   	printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
1091		      info.entry_number,
1092      		      info.base_addr,
1093      		      info.limit,
1094      		      info.seg_32bit,
1095		      info.contents,
1096      		      info.read_exec_only,
1097      		      info.limit_in_pages,
1098      		      info.seg_not_present,
1099      		      info.useable);
1100#endif
1101
1102	idx = info.entry_number;
1103	/*
1104	 * Semantics of linux version: every thread in the system has array
1105	 * of 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
1106	 * syscall loads one of the selected tls decriptors with a value
1107	 * and also loads GDT descriptors 6, 7 and 8 with the content of the per-thread
1108	 * descriptors.
1109	 *
1110	 * Semantics of fbsd version: I think we can ignore that linux has 3 per-thread
1111	 * descriptors and use just the 1st one. The tls_array[] is used only in
1112	 * set/get-thread_area() syscalls and for loading the GDT descriptors. In fbsd
1113	 * we use just one GDT descriptor for TLS so we will load just one.
1114	 * XXX: this doesnt work when user-space process tries to use more then 1 TLS segment
1115	 * comment in the linux sources says wine might do that.
1116	 */
1117
1118	/*
1119	 * we support just GLIBC TLS now
1120	 * we should let 3 proceed as well because we use this segment so
1121	 * if code does two subsequent calls it should succeed
1122	 */
1123	if (idx != 6 && idx != -1 && idx != 3)
1124		return (EINVAL);
1125
1126	/*
1127	 * we have to copy out the GDT entry we use
1128	 * FreeBSD uses GDT entry #3 for storing %gs so load that
1129	 * XXX: what if userspace program doesnt check this value and tries
1130	 * to use 6, 7 or 8?
1131	 */
1132	idx = info.entry_number = 3;
1133	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1134	if (error)
1135		return (error);
1136
1137	if (LDT_empty(&info)) {
1138		a[0] = 0;
1139		a[1] = 0;
1140	} else {
1141		a[0] = LDT_entry_a(&info);
1142		a[1] = LDT_entry_b(&info);
1143	}
1144
1145	memcpy(&sd, &a, sizeof(a));
1146#ifdef DEBUG
1147	if (ldebug(set_thread_area))
1148	   	printf("Segment created in set_thread_area: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
1149			sd.sd_hibase,
1150			sd.sd_lolimit,
1151			sd.sd_hilimit,
1152			sd.sd_type,
1153			sd.sd_dpl,
1154			sd.sd_p,
1155			sd.sd_xx,
1156			sd.sd_def32,
1157			sd.sd_gran);
1158#endif
1159
1160	/* this is taken from i386 version of cpu_set_user_tls() */
1161	critical_enter();
1162	/* set %gs */
1163	td->td_pcb->pcb_gsd = sd;
1164	PCPU_GET(fsgs_gdt)[1] = sd;
1165	load_gs(GSEL(GUGS_SEL, SEL_UPL));
1166	critical_exit();
1167
1168	return (0);
1169}
1170
1171int
1172linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
1173{
1174
1175	struct l_user_desc info;
1176	int error;
1177	int idx;
1178	struct l_desc_struct desc;
1179	struct segment_descriptor sd;
1180
1181#ifdef DEBUG
1182	if (ldebug(get_thread_area))
1183		printf(ARGS(get_thread_area, "%p"), args->desc);
1184#endif
1185
1186	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1187	if (error)
1188		return (error);
1189
1190	idx = info.entry_number;
1191	/* XXX: I am not sure if we want 3 to be allowed too. */
1192	if (idx != 6 && idx != 3)
1193		return (EINVAL);
1194
1195	idx = 3;
1196
1197	memset(&info, 0, sizeof(info));
1198
1199	sd = PCPU_GET(fsgs_gdt)[1];
1200
1201	memcpy(&desc, &sd, sizeof(desc));
1202
1203	info.entry_number = idx;
1204	info.base_addr = GET_BASE(&desc);
1205	info.limit = GET_LIMIT(&desc);
1206	info.seg_32bit = GET_32BIT(&desc);
1207	info.contents = GET_CONTENTS(&desc);
1208	info.read_exec_only = !GET_WRITABLE(&desc);
1209	info.limit_in_pages = GET_LIMIT_PAGES(&desc);
1210	info.seg_not_present = !GET_PRESENT(&desc);
1211	info.useable = GET_USEABLE(&desc);
1212
1213	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1214	if (error)
1215	   	return (EFAULT);
1216
1217	return (0);
1218}
1219
1220/* copied from kern/kern_time.c */
1221int
1222linux_timer_create(struct thread *td, struct linux_timer_create_args *args)
1223{
1224   	return ktimer_create(td, (struct ktimer_create_args *) args);
1225}
1226
1227int
1228linux_timer_settime(struct thread *td, struct linux_timer_settime_args *args)
1229{
1230   	return ktimer_settime(td, (struct ktimer_settime_args *) args);
1231}
1232
1233int
1234linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *args)
1235{
1236   	return ktimer_gettime(td, (struct ktimer_gettime_args *) args);
1237}
1238
1239int
1240linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *args)
1241{
1242   	return ktimer_getoverrun(td, (struct ktimer_getoverrun_args *) args);
1243}
1244
1245int
1246linux_timer_delete(struct thread *td, struct linux_timer_delete_args *args)
1247{
1248   	return ktimer_delete(td, (struct ktimer_delete_args *) args);
1249}
1250
1251/* XXX: this wont work with module - convert it */
1252int
1253linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
1254{
1255#ifdef P1003_1B_MQUEUE
1256   	return kmq_open(td, (struct kmq_open_args *) args);
1257#else
1258	return (ENOSYS);
1259#endif
1260}
1261
1262int
1263linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
1264{
1265#ifdef P1003_1B_MQUEUE
1266   	return kmq_unlink(td, (struct kmq_unlink_args *) args);
1267#else
1268	return (ENOSYS);
1269#endif
1270}
1271
1272int
1273linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
1274{
1275#ifdef P1003_1B_MQUEUE
1276   	return kmq_timedsend(td, (struct kmq_timedsend_args *) args);
1277#else
1278	return (ENOSYS);
1279#endif
1280}
1281
1282int
1283linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
1284{
1285#ifdef P1003_1B_MQUEUE
1286   	return kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
1287#else
1288	return (ENOSYS);
1289#endif
1290}
1291
1292int
1293linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
1294{
1295#ifdef P1003_1B_MQUEUE
1296	return kmq_notify(td, (struct kmq_notify_args *) args);
1297#else
1298	return (ENOSYS);
1299#endif
1300}
1301
1302int
1303linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
1304{
1305#ifdef P1003_1B_MQUEUE
1306   	return kmq_setattr(td, (struct kmq_setattr_args *) args);
1307#else
1308	return (ENOSYS);
1309#endif
1310}
1311
1312