linux_misc.c revision 161317
19313Ssos/*-
2133749Stjr * Copyright (c) 2002 Doug Rabson
39313Ssos * Copyright (c) 1994-1995 S�ren Schmidt
49313Ssos * All rights reserved.
59313Ssos *
69313Ssos * Redistribution and use in source and binary forms, with or without
79313Ssos * modification, are permitted provided that the following conditions
89313Ssos * are met:
99313Ssos * 1. Redistributions of source code must retain the above copyright
1014331Speter *    notice, this list of conditions and the following disclaimer
119313Ssos *    in this position and unchanged.
129313Ssos * 2. Redistributions in binary form must reproduce the above copyright
139313Ssos *    notice, this list of conditions and the following disclaimer in the
149313Ssos *    documentation and/or other materials provided with the distribution.
159313Ssos * 3. The name of the author may not be used to endorse or promote products
1697748Sschweikh *    derived from this software without specific prior written permission
179313Ssos *
189313Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199313Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
209313Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
219313Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
229313Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
239313Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
249313Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
259313Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
269313Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
279313Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289313Ssos */
299313Ssos
30116173Sobrien#include <sys/cdefs.h>
31116173Sobrien__FBSDID("$FreeBSD: head/sys/compat/linux/linux_misc.c 161317 2006-08-15 15:15:17Z netchild $");
32116173Sobrien
33156874Sru#include "opt_compat.h"
34101189Srwatson#include "opt_mac.h"
3549842Smarcel
369313Ssos#include <sys/param.h>
37102954Sbde#include <sys/blist.h>
3876166Smarkm#include <sys/fcntl.h>
39158415Snetchild#if defined(__i386__)
4076166Smarkm#include <sys/imgact_aout.h>
41133816Stjr#endif
4291385Srobert#include <sys/jail.h>
4312458Sbde#include <sys/kernel.h>
44114216Skan#include <sys/limits.h>
4576166Smarkm#include <sys/lock.h>
46101189Srwatson#include <sys/mac.h>
47102954Sbde#include <sys/malloc.h>
489313Ssos#include <sys/mman.h>
499313Ssos#include <sys/mount.h>
5076166Smarkm#include <sys/mutex.h>
519313Ssos#include <sys/namei.h>
5276166Smarkm#include <sys/proc.h>
5372538Sjlemon#include <sys/reboot.h>
549313Ssos#include <sys/resourcevar.h>
5576166Smarkm#include <sys/signalvar.h>
569313Ssos#include <sys/stat.h>
57102814Siedowse#include <sys/syscallsubr.h>
5812458Sbde#include <sys/sysctl.h>
5976166Smarkm#include <sys/sysproto.h>
60102954Sbde#include <sys/systm.h>
6176166Smarkm#include <sys/time.h>
6280180Spirzyk#include <sys/vmmeter.h>
639313Ssos#include <sys/vnode.h>
649313Ssos#include <sys/wait.h>
659313Ssos
6612652Sbde#include <vm/vm.h>
6712689Speter#include <vm/pmap.h>
6812458Sbde#include <vm/vm_kern.h>
6912689Speter#include <vm/vm_map.h>
7012842Sbde#include <vm/vm_extern.h>
7180180Spirzyk#include <vm/vm_object.h>
7280180Spirzyk#include <vm/swap_pager.h>
7312458Sbde
7449849Smarcel#include <posix4/sched.h>
7549849Smarcel
76143197Ssobomax#include <compat/linux/linux_sysproto.h>
77161310Snetchild#include <compat/linux/linux_emul.h>
78143197Ssobomax
79140214Sobrien#ifdef COMPAT_LINUX32
80140214Sobrien#include <machine/../linux32/linux.h>
81140214Sobrien#include <machine/../linux32/linux32_proto.h>
82140214Sobrien#else
8364909Smarcel#include <machine/../linux/linux.h>
8468583Smarcel#include <machine/../linux/linux_proto.h>
85133816Stjr#endif
86102954Sbde
8764909Smarcel#include <compat/linux/linux_mib.h>
8864909Smarcel#include <compat/linux/linux_util.h>
8964909Smarcel
90118149Sdes#ifdef __i386__
91118149Sdes#include <machine/cputypes.h>
92118149Sdes#endif
93118149Sdes
9451793Smarcel#define BSD_TO_LINUX_SIGNAL(sig)	\
9551793Smarcel	(((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
9651793Smarcel
97161310Snetchildextern struct sx emul_shared_lock;
98161310Snetchildextern struct sx emul_lock;
99161310Snetchild
10083221Smarcelstatic unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
10183221Smarcel	RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
10283221Smarcel	RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
10383221Smarcel	RLIMIT_MEMLOCK, -1
10449626Smarcel};
10549626Smarcel
10683221Smarcelstruct l_sysinfo {
10783221Smarcel	l_long		uptime;		/* Seconds since boot */
10883221Smarcel	l_ulong		loads[3];	/* 1, 5, and 15 minute load averages */
109122802Ssobomax#define LINUX_SYSINFO_LOADS_SCALE 65536
11083221Smarcel	l_ulong		totalram;	/* Total usable main memory size */
11183221Smarcel	l_ulong		freeram;	/* Available memory size */
11283221Smarcel	l_ulong		sharedram;	/* Amount of shared memory */
11383221Smarcel	l_ulong		bufferram;	/* Memory used by buffers */
11483221Smarcel	l_ulong		totalswap;	/* Total swap space size */
11583221Smarcel	l_ulong		freeswap;	/* swap space still available */
11683221Smarcel	l_ushort	procs;		/* Number of current processes */
117122802Ssobomax	l_ulong		totalbig;
118122802Ssobomax	l_ulong		freebig;
119122802Ssobomax	l_uint		mem_unit;
120122802Ssobomax	char		_f[6];		/* Pads structure to 64 bytes */
12180180Spirzyk};
1229313Ssosint
12383366Sjulianlinux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
12480180Spirzyk{
12583221Smarcel	struct l_sysinfo sysinfo;
12683221Smarcel	vm_object_t object;
127117723Sphk	int i, j;
12883221Smarcel	struct timespec ts;
12980180Spirzyk
13083221Smarcel	getnanouptime(&ts);
131147816Sjhb	if (ts.tv_nsec != 0)
132147816Sjhb		ts.tv_sec++;
133147816Sjhb	sysinfo.uptime = ts.tv_sec;
13480180Spirzyk
13583221Smarcel	/* Use the information from the mib to get our load averages */
13683221Smarcel	for (i = 0; i < 3; i++)
137122802Ssobomax		sysinfo.loads[i] = averunnable.ldavg[i] *
138122802Ssobomax		    LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
13980180Spirzyk
14083221Smarcel	sysinfo.totalram = physmem * PAGE_SIZE;
14183221Smarcel	sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
14280180Spirzyk
14383221Smarcel	sysinfo.sharedram = 0;
144124082Salc	mtx_lock(&vm_object_list_mtx);
145124082Salc	TAILQ_FOREACH(object, &vm_object_list, object_list)
14683221Smarcel		if (object->shadow_count > 1)
14783221Smarcel			sysinfo.sharedram += object->resident_page_count;
148124082Salc	mtx_unlock(&vm_object_list_mtx);
14980180Spirzyk
15083221Smarcel	sysinfo.sharedram *= PAGE_SIZE;
15183221Smarcel	sysinfo.bufferram = 0;
15280180Spirzyk
153117723Sphk	swap_pager_status(&i, &j);
154117723Sphk	sysinfo.totalswap= i * PAGE_SIZE;
155117723Sphk	sysinfo.freeswap = (i - j) * PAGE_SIZE;
15680180Spirzyk
157122802Ssobomax	sysinfo.procs = nprocs;
15880180Spirzyk
159122802Ssobomax	/* The following are only present in newer Linux kernels. */
160122802Ssobomax	sysinfo.totalbig = 0;
161122802Ssobomax	sysinfo.freebig = 0;
162122802Ssobomax	sysinfo.mem_unit = 1;
163122802Ssobomax
164111797Sdes	return copyout(&sysinfo, args->info, sizeof(sysinfo));
16580180Spirzyk}
16680180Spirzyk
16780180Spirzykint
16883366Sjulianlinux_alarm(struct thread *td, struct linux_alarm_args *args)
1699313Ssos{
17083221Smarcel	struct itimerval it, old_it;
171141467Sjhb	int error;
1729313Ssos
1739313Ssos#ifdef DEBUG
17472543Sjlemon	if (ldebug(alarm))
17572543Sjlemon		printf(ARGS(alarm, "%u"), args->secs);
1769313Ssos#endif
17783221Smarcel
17883221Smarcel	if (args->secs > 100000000)
179141467Sjhb		return (EINVAL);
18083221Smarcel
18183221Smarcel	it.it_value.tv_sec = (long)args->secs;
18283221Smarcel	it.it_value.tv_usec = 0;
18383221Smarcel	it.it_interval.tv_sec = 0;
18483221Smarcel	it.it_interval.tv_usec = 0;
185141467Sjhb	error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
186141467Sjhb	if (error)
187141467Sjhb		return (error);
188141467Sjhb	if (timevalisset(&old_it.it_value)) {
18983221Smarcel		if (old_it.it_value.tv_usec != 0)
19083221Smarcel			old_it.it_value.tv_sec++;
19183366Sjulian		td->td_retval[0] = old_it.it_value.tv_sec;
19283221Smarcel	}
193141467Sjhb	return (0);
1949313Ssos}
1959313Ssos
1969313Ssosint
19783366Sjulianlinux_brk(struct thread *td, struct linux_brk_args *args)
1989313Ssos{
19983366Sjulian	struct vmspace *vm = td->td_proc->p_vmspace;
20083221Smarcel	vm_offset_t new, old;
20183221Smarcel	struct obreak_args /* {
20283221Smarcel		char * nsize;
20383221Smarcel	} */ tmp;
2049313Ssos
2059313Ssos#ifdef DEBUG
20672543Sjlemon	if (ldebug(brk))
207133845Sobrien		printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
2089313Ssos#endif
20983221Smarcel	old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
21083221Smarcel	new = (vm_offset_t)args->dsend;
21183221Smarcel	tmp.nsize = (char *) new;
21283366Sjulian	if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
21383366Sjulian		td->td_retval[0] = (long)new;
21483221Smarcel	else
21583366Sjulian		td->td_retval[0] = (long)old;
2169313Ssos
21783221Smarcel	return 0;
2189313Ssos}
2199313Ssos
220158415Snetchild#if defined(__i386__)
221158415Snetchild/* XXX: what about amd64/linux32? */
222133816Stjr
2239313Ssosint
22483366Sjulianlinux_uselib(struct thread *td, struct linux_uselib_args *args)
2259313Ssos{
22683221Smarcel	struct nameidata ni;
22783221Smarcel	struct vnode *vp;
22883221Smarcel	struct exec *a_out;
22983221Smarcel	struct vattr attr;
23083221Smarcel	vm_offset_t vmaddr;
23183221Smarcel	unsigned long file_offset;
23283221Smarcel	vm_offset_t buffer;
23383221Smarcel	unsigned long bss_size;
234102814Siedowse	char *library;
23583221Smarcel	int error;
236160555Sjhb	int locked, vfslocked;
2379313Ssos
238102814Siedowse	LCONVPATHEXIST(td, args->library, &library);
23914331Speter
2409313Ssos#ifdef DEBUG
24172543Sjlemon	if (ldebug(uselib))
242102814Siedowse		printf(ARGS(uselib, "%s"), library);
2439313Ssos#endif
2449313Ssos
24583221Smarcel	a_out = NULL;
246160555Sjhb	vfslocked = 0;
24783221Smarcel	locked = 0;
24883221Smarcel	vp = NULL;
24914114Speter
250160555Sjhb	NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
251160555Sjhb	    UIO_SYSSPACE, library, td);
25283221Smarcel	error = namei(&ni);
253102814Siedowse	LFREEPATH(library);
25483221Smarcel	if (error)
25583221Smarcel		goto cleanup;
2569313Ssos
25783221Smarcel	vp = ni.ni_vp;
258160555Sjhb	vfslocked = NDHASGIANT(&ni);
25983221Smarcel	NDFREE(&ni, NDF_ONLY_PNBUF);
2609313Ssos
26183221Smarcel	/*
26283221Smarcel	 * From here on down, we have a locked vnode that must be unlocked.
263160555Sjhb	 * XXX: The code below largely duplicates exec_check_permissions().
26483221Smarcel	 */
265160555Sjhb	locked = 1;
26614114Speter
26783221Smarcel	/* Writable? */
26883221Smarcel	if (vp->v_writecount) {
26983221Smarcel		error = ETXTBSY;
27083221Smarcel		goto cleanup;
27183221Smarcel	}
2729313Ssos
27383221Smarcel	/* Executable? */
27491406Sjhb	error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
27583221Smarcel	if (error)
27683221Smarcel		goto cleanup;
2779313Ssos
27883221Smarcel	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
27983221Smarcel	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
280160555Sjhb		/* EACCESS is what exec(2) returns. */
28183221Smarcel		error = ENOEXEC;
28283221Smarcel		goto cleanup;
28383221Smarcel	}
2849313Ssos
28583221Smarcel	/* Sensible size? */
28683221Smarcel	if (attr.va_size == 0) {
28783221Smarcel		error = ENOEXEC;
28883221Smarcel		goto cleanup;
28983221Smarcel	}
2909313Ssos
29183221Smarcel	/* Can we access it? */
29291406Sjhb	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
29383221Smarcel	if (error)
29483221Smarcel		goto cleanup;
2959313Ssos
29698209Srwatson	/*
29798209Srwatson	 * XXX: This should use vn_open() so that it is properly authorized,
29898209Srwatson	 * and to reduce code redundancy all over the place here.
299160555Sjhb	 * XXX: Not really, it duplicates far more of exec_check_permissions()
300160555Sjhb	 * than vn_open().
30198209Srwatson	 */
302101189Srwatson#ifdef MAC
303101189Srwatson	error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
304101189Srwatson	if (error)
305101189Srwatson		goto cleanup;
306101189Srwatson#endif
307118047Sphk	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
30883221Smarcel	if (error)
30983221Smarcel		goto cleanup;
3109313Ssos
311103941Sjeff	/* Pull in executable header into kernel_map */
312103941Sjeff	error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
313144501Sjhb	    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
31483221Smarcel	if (error)
31583221Smarcel		goto cleanup;
3169313Ssos
31783221Smarcel	/* Is it a Linux binary ? */
31883221Smarcel	if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
31983221Smarcel		error = ENOEXEC;
32083221Smarcel		goto cleanup;
32183221Smarcel	}
3229313Ssos
32383221Smarcel	/*
32483221Smarcel	 * While we are here, we should REALLY do some more checks
32583221Smarcel	 */
32614114Speter
32783221Smarcel	/* Set file/virtual offset based on a.out variant. */
32883221Smarcel	switch ((int)(a_out->a_magic & 0xffff)) {
32983221Smarcel	case 0413:	/* ZMAGIC */
33083221Smarcel		file_offset = 1024;
33183221Smarcel		break;
33283221Smarcel	case 0314:	/* QMAGIC */
33383221Smarcel		file_offset = 0;
33483221Smarcel		break;
33583221Smarcel	default:
33683221Smarcel		error = ENOEXEC;
33783221Smarcel		goto cleanup;
33883221Smarcel	}
3399313Ssos
34083221Smarcel	bss_size = round_page(a_out->a_bss);
34114114Speter
34283221Smarcel	/* Check various fields in header for validity/bounds. */
34383221Smarcel	if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
34483221Smarcel		error = ENOEXEC;
34583221Smarcel		goto cleanup;
34683221Smarcel	}
34714114Speter
34883221Smarcel	/* text + data can't exceed file size */
34983221Smarcel	if (a_out->a_data + a_out->a_text > attr.va_size) {
35083221Smarcel		error = EFAULT;
35183221Smarcel		goto cleanup;
35283221Smarcel	}
35314114Speter
35483221Smarcel	/*
35583221Smarcel	 * text/data/bss must not exceed limits
35683221Smarcel	 * XXX - this is not complete. it should check current usage PLUS
35783221Smarcel	 * the resources needed by this library.
35883221Smarcel	 */
359125454Sjhb	PROC_LOCK(td->td_proc);
36084783Sps	if (a_out->a_text > maxtsiz ||
361125454Sjhb	    a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
362125454Sjhb		PROC_UNLOCK(td->td_proc);
36383221Smarcel		error = ENOMEM;
36483221Smarcel		goto cleanup;
36583221Smarcel	}
366125454Sjhb	PROC_UNLOCK(td->td_proc);
36714114Speter
368160555Sjhb	/*
369160555Sjhb	 * Prevent more writers.
370160555Sjhb	 * XXX: Note that if any of the VM operations fail below we don't
371160555Sjhb	 * clear this flag.
372160555Sjhb	 */
373101308Sjeff	vp->v_vflag |= VV_TEXT;
37414114Speter
37583221Smarcel	/*
376160555Sjhb	 * Lock no longer needed
377160555Sjhb	 */
378160555Sjhb	locked = 0;
379160555Sjhb	VOP_UNLOCK(vp, 0, td);
380160555Sjhb	VFS_UNLOCK_GIANT(vfslocked);
381160555Sjhb
382160555Sjhb	/*
38383221Smarcel	 * Check if file_offset page aligned. Currently we cannot handle
38483221Smarcel	 * misalinged file offsets, and so we read in the entire image
38583221Smarcel	 * (what a waste).
38683221Smarcel	 */
38783221Smarcel	if (file_offset & PAGE_MASK) {
3889313Ssos#ifdef DEBUG
38983221Smarcel		printf("uselib: Non page aligned binary %lu\n", file_offset);
3909313Ssos#endif
39183221Smarcel		/* Map text+data read/write/execute */
39214114Speter
39383221Smarcel		/* a_entry is the load address and is page aligned */
39483221Smarcel		vmaddr = trunc_page(a_out->a_entry);
39514114Speter
39683221Smarcel		/* get anon user mapping, read+write+execute */
39783366Sjulian		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
39883366Sjulian		    &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
39983221Smarcel		    VM_PROT_ALL, 0);
40083221Smarcel		if (error)
40183221Smarcel			goto cleanup;
4029313Ssos
40383221Smarcel		/* map file into kernel_map */
40483221Smarcel		error = vm_mmap(kernel_map, &buffer,
40583221Smarcel		    round_page(a_out->a_text + a_out->a_data + file_offset),
406144501Sjhb		    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp,
40783221Smarcel		    trunc_page(file_offset));
40883221Smarcel		if (error)
40983221Smarcel			goto cleanup;
4109313Ssos
41183221Smarcel		/* copy from kernel VM space to user space */
412133816Stjr		error = copyout(PTRIN(buffer + file_offset),
413111797Sdes		    (void *)vmaddr, a_out->a_text + a_out->a_data);
4149313Ssos
41583221Smarcel		/* release temporary kernel space */
41683221Smarcel		vm_map_remove(kernel_map, buffer, buffer +
41783221Smarcel		    round_page(a_out->a_text + a_out->a_data + file_offset));
4189313Ssos
41983221Smarcel		if (error)
42083221Smarcel			goto cleanup;
42183221Smarcel	} else {
4229313Ssos#ifdef DEBUG
42383221Smarcel		printf("uselib: Page aligned binary %lu\n", file_offset);
4249313Ssos#endif
42583221Smarcel		/*
42683221Smarcel		 * for QMAGIC, a_entry is 20 bytes beyond the load address
42783221Smarcel		 * to skip the executable header
42883221Smarcel		 */
42983221Smarcel		vmaddr = trunc_page(a_out->a_entry);
43014114Speter
43183221Smarcel		/*
43283221Smarcel		 * Map it all into the process's space as a single
43383221Smarcel		 * copy-on-write "data" segment.
43483221Smarcel		 */
43583366Sjulian		error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
43683221Smarcel		    a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
437144501Sjhb		    MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
43883221Smarcel		if (error)
43983221Smarcel			goto cleanup;
44083221Smarcel	}
4419313Ssos#ifdef DEBUG
44283221Smarcel	printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
44383221Smarcel	    ((long*)vmaddr)[1]);
4449313Ssos#endif
44583221Smarcel	if (bss_size != 0) {
44683221Smarcel		/* Calculate BSS start address */
44783221Smarcel		vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
44883221Smarcel		    a_out->a_data;
44914114Speter
45083221Smarcel		/* allocate some 'anon' space */
45183366Sjulian		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
45283366Sjulian		    &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
45383221Smarcel		if (error)
45483221Smarcel			goto cleanup;
45583221Smarcel	}
45614114Speter
45714114Spetercleanup:
45883221Smarcel	/* Unlock vnode if needed */
459160555Sjhb	if (locked) {
46083366Sjulian		VOP_UNLOCK(vp, 0, td);
461160555Sjhb		VFS_UNLOCK_GIANT(vfslocked);
462160555Sjhb	}
46314114Speter
46483221Smarcel	/* Release the kernel mapping. */
46583221Smarcel	if (a_out)
46683221Smarcel		vm_map_remove(kernel_map, (vm_offset_t)a_out,
46783221Smarcel		    (vm_offset_t)a_out + PAGE_SIZE);
46814114Speter
46983221Smarcel	return error;
4709313Ssos}
4719313Ssos
472158415Snetchild#endif	/* __i386__ */
473133816Stjr
4749313Ssosint
47583366Sjulianlinux_select(struct thread *td, struct linux_select_args *args)
47614331Speter{
477133747Stjr	l_timeval ltv;
47883221Smarcel	struct timeval tv0, tv1, utv, *tvp;
47983221Smarcel	int error;
48014331Speter
4819313Ssos#ifdef DEBUG
48283221Smarcel	if (ldebug(select))
48383221Smarcel		printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
48483221Smarcel		    (void *)args->readfds, (void *)args->writefds,
48583221Smarcel		    (void *)args->exceptfds, (void *)args->timeout);
4869313Ssos#endif
48714331Speter
48883221Smarcel	/*
48983221Smarcel	 * Store current time for computation of the amount of
49083221Smarcel	 * time left.
49183221Smarcel	 */
49283221Smarcel	if (args->timeout) {
493133747Stjr		if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
49483221Smarcel			goto select_out;
495133747Stjr		utv.tv_sec = ltv.tv_sec;
496133747Stjr		utv.tv_usec = ltv.tv_usec;
49714331Speter#ifdef DEBUG
49883221Smarcel		if (ldebug(select))
499153775Strhodes			printf(LMSG("incoming timeout (%jd/%ld)"),
500153775Strhodes			    (intmax_t)utv.tv_sec, utv.tv_usec);
50114331Speter#endif
50283221Smarcel
50383221Smarcel		if (itimerfix(&utv)) {
50483221Smarcel			/*
50583221Smarcel			 * The timeval was invalid.  Convert it to something
50683221Smarcel			 * valid that will act as it does under Linux.
50783221Smarcel			 */
50883221Smarcel			utv.tv_sec += utv.tv_usec / 1000000;
50983221Smarcel			utv.tv_usec %= 1000000;
51083221Smarcel			if (utv.tv_usec < 0) {
51183221Smarcel				utv.tv_sec -= 1;
51283221Smarcel				utv.tv_usec += 1000000;
51383221Smarcel			}
51483221Smarcel			if (utv.tv_sec < 0)
51583221Smarcel				timevalclear(&utv);
51683221Smarcel		}
51783221Smarcel		microtime(&tv0);
518102814Siedowse		tvp = &utv;
519102814Siedowse	} else
520102814Siedowse		tvp = NULL;
52114331Speter
522102814Siedowse	error = kern_select(td, args->nfds, args->readfds, args->writefds,
523102814Siedowse	    args->exceptfds, tvp);
524102814Siedowse
52514331Speter#ifdef DEBUG
52683221Smarcel	if (ldebug(select))
52772543Sjlemon		printf(LMSG("real select returns %d"), error);
52814331Speter#endif
52983221Smarcel	if (error) {
53083221Smarcel		/*
53183221Smarcel		 * See fs/select.c in the Linux kernel.  Without this,
53283221Smarcel		 * Maelstrom doesn't work.
53383221Smarcel		 */
53483221Smarcel		if (error == ERESTART)
53583221Smarcel			error = EINTR;
53683221Smarcel		goto select_out;
53783221Smarcel	}
53814331Speter
53983221Smarcel	if (args->timeout) {
54083366Sjulian		if (td->td_retval[0]) {
54183221Smarcel			/*
54283221Smarcel			 * Compute how much time was left of the timeout,
54383221Smarcel			 * by subtracting the current time and the time
54483221Smarcel			 * before we started the call, and subtracting
54583221Smarcel			 * that result from the user-supplied value.
54683221Smarcel			 */
54783221Smarcel			microtime(&tv1);
54883221Smarcel			timevalsub(&tv1, &tv0);
54983221Smarcel			timevalsub(&utv, &tv1);
55083221Smarcel			if (utv.tv_sec < 0)
55183221Smarcel				timevalclear(&utv);
55283221Smarcel		} else
55383221Smarcel			timevalclear(&utv);
55414331Speter#ifdef DEBUG
55583221Smarcel		if (ldebug(select))
556153775Strhodes			printf(LMSG("outgoing timeout (%jd/%ld)"),
557153775Strhodes			    (intmax_t)utv.tv_sec, utv.tv_usec);
55814331Speter#endif
559133747Stjr		ltv.tv_sec = utv.tv_sec;
560133747Stjr		ltv.tv_usec = utv.tv_usec;
561133747Stjr		if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
56283221Smarcel			goto select_out;
56383221Smarcel	}
56414331Speter
56514331Speterselect_out:
56614331Speter#ifdef DEBUG
56783221Smarcel	if (ldebug(select))
56883221Smarcel		printf(LMSG("select_out -> %d"), error);
56914331Speter#endif
57083221Smarcel	return error;
5719313Ssos}
5729313Ssos
573111798Sdesint
57483366Sjulianlinux_mremap(struct thread *td, struct linux_mremap_args *args)
57537548Sjkh{
57637548Sjkh	struct munmap_args /* {
57737548Sjkh		void *addr;
57837548Sjkh		size_t len;
579111798Sdes	} */ bsd_args;
58037548Sjkh	int error = 0;
581111798Sdes
58237548Sjkh#ifdef DEBUG
58372543Sjlemon	if (ldebug(mremap))
58472543Sjlemon		printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
585133845Sobrien		    (void *)(uintptr_t)args->addr,
586111798Sdes		    (unsigned long)args->old_len,
58772543Sjlemon		    (unsigned long)args->new_len,
58872543Sjlemon		    (unsigned long)args->flags);
58937548Sjkh#endif
59037548Sjkh	args->new_len = round_page(args->new_len);
59137548Sjkh	args->old_len = round_page(args->old_len);
59237548Sjkh
59337548Sjkh	if (args->new_len > args->old_len) {
59483366Sjulian		td->td_retval[0] = 0;
59537548Sjkh		return ENOMEM;
59637548Sjkh	}
59737548Sjkh
59837548Sjkh	if (args->new_len < args->old_len) {
599133816Stjr		bsd_args.addr =
600133816Stjr		    (caddr_t)((uintptr_t)args->addr + args->new_len);
60137548Sjkh		bsd_args.len = args->old_len - args->new_len;
60283366Sjulian		error = munmap(td, &bsd_args);
60337548Sjkh	}
60437548Sjkh
605102963Sbde	td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
60637548Sjkh	return error;
60737548Sjkh}
60837548Sjkh
609103652Smdodd#define LINUX_MS_ASYNC       0x0001
610103652Smdodd#define LINUX_MS_INVALIDATE  0x0002
611103652Smdodd#define LINUX_MS_SYNC        0x0004
612103652Smdodd
61314331Speterint
61483366Sjulianlinux_msync(struct thread *td, struct linux_msync_args *args)
61514331Speter{
61614331Speter	struct msync_args bsd_args;
6179313Ssos
618133816Stjr	bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
619133816Stjr	bsd_args.len = (uintptr_t)args->len;
620103652Smdodd	bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
62114331Speter
62283366Sjulian	return msync(td, &bsd_args);
62314331Speter}
62414331Speter
6259313Ssosint
62683366Sjulianlinux_time(struct thread *td, struct linux_time_args *args)
6279313Ssos{
62883221Smarcel	struct timeval tv;
62983221Smarcel	l_time_t tm;
63083221Smarcel	int error;
6319313Ssos
6329313Ssos#ifdef DEBUG
63372543Sjlemon	if (ldebug(time))
63472543Sjlemon		printf(ARGS(time, "*"));
6359313Ssos#endif
63683221Smarcel
63783221Smarcel	microtime(&tv);
63883221Smarcel	tm = tv.tv_sec;
639111797Sdes	if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
64083221Smarcel		return error;
64183366Sjulian	td->td_retval[0] = tm;
64283221Smarcel	return 0;
6439313Ssos}
6449313Ssos
64583221Smarcelstruct l_times_argv {
64683221Smarcel	l_long		tms_utime;
64783221Smarcel	l_long		tms_stime;
64883221Smarcel	l_long		tms_cutime;
64983221Smarcel	l_long		tms_cstime;
6509313Ssos};
6519313Ssos
65214381Speter#define CLK_TCK 100	/* Linux uses 100 */
65374701Sgallatin
65414381Speter#define CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
65514381Speter
6569313Ssosint
65783366Sjulianlinux_times(struct thread *td, struct linux_times_args *args)
6589313Ssos{
659136152Sjhb	struct timeval tv, utime, stime, cutime, cstime;
66083221Smarcel	struct l_times_argv tms;
661136152Sjhb	struct proc *p;
66283221Smarcel	int error;
6639313Ssos
6649313Ssos#ifdef DEBUG
66572543Sjlemon	if (ldebug(times))
66672543Sjlemon		printf(ARGS(times, "*"));
6679313Ssos#endif
66814381Speter
669159896Snetchild	if (args->buf != NULL) {
670159896Snetchild		p = td->td_proc;
671159896Snetchild		PROC_LOCK(p);
672159896Snetchild		calcru(p, &utime, &stime);
673159896Snetchild		calccru(p, &cutime, &cstime);
674159896Snetchild		PROC_UNLOCK(p);
67514381Speter
676159896Snetchild		tms.tms_utime = CONVTCK(utime);
677159896Snetchild		tms.tms_stime = CONVTCK(stime);
67814381Speter
679159896Snetchild		tms.tms_cutime = CONVTCK(cutime);
680159896Snetchild		tms.tms_cstime = CONVTCK(cstime);
68114381Speter
682159896Snetchild		if ((error = copyout(&tms, args->buf, sizeof(tms))))
683159896Snetchild			return error;
684159896Snetchild	}
68583221Smarcel
68683221Smarcel	microuptime(&tv);
68783366Sjulian	td->td_retval[0] = (int)CONVTCK(tv);
68883221Smarcel	return 0;
6899313Ssos}
6909313Ssos
6919313Ssosint
69283366Sjulianlinux_newuname(struct thread *td, struct linux_newuname_args *args)
6939313Ssos{
69483221Smarcel	struct l_new_utsname utsname;
69587275Srwatson	char osname[LINUX_MAX_UTSNAME];
69687275Srwatson	char osrelease[LINUX_MAX_UTSNAME];
697118149Sdes	char *p;
6989313Ssos
6999313Ssos#ifdef DEBUG
70072543Sjlemon	if (ldebug(newuname))
70172543Sjlemon		printf(ARGS(newuname, "*"));
7029313Ssos#endif
70350345Smarcel
704112206Sjhb	linux_get_osname(td, osname);
705112206Sjhb	linux_get_osrelease(td, osrelease);
70650465Smarcel
70783221Smarcel	bzero(&utsname, sizeof(utsname));
708105359Srobert	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
709105359Srobert	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
710105359Srobert	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
711105359Srobert	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
712118149Sdes	for (p = utsname.version; *p != '\0'; ++p)
713118149Sdes		if (*p == '\n') {
714118149Sdes			*p = '\0';
715118149Sdes			break;
716118149Sdes		}
717118149Sdes#ifdef __i386__
718118149Sdes	{
719118149Sdes		const char *class;
720118149Sdes		switch (cpu_class) {
721118149Sdes		case CPUCLASS_686:
722118149Sdes			class = "i686";
723118149Sdes			break;
724118149Sdes		case CPUCLASS_586:
725118149Sdes			class = "i586";
726118149Sdes			break;
727118149Sdes		case CPUCLASS_486:
728118149Sdes			class = "i486";
729118149Sdes			break;
730118149Sdes		default:
731118149Sdes			class = "i386";
732118149Sdes		}
733118149Sdes		strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME);
734118149Sdes	}
735140213Sobrien#elif defined(__amd64__)	/* XXX: Linux can change 'personality'. */
736140213Sobrien#ifdef COMPAT_LINUX32
737140213Sobrien	strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
738118149Sdes#else
739140213Sobrien	strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
740140213Sobrien#endif /* COMPAT_LINUX32 */
741140213Sobrien#else /* something other than i386 or amd64 - assume we and Linux agree */
742105359Srobert	strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
743140213Sobrien#endif /* __i386__ */
744105359Srobert	strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
74550345Smarcel
746111797Sdes	return (copyout(&utsname, args->buf, sizeof(utsname)));
7479313Ssos}
7489313Ssos
749140214Sobrien#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
75083221Smarcelstruct l_utimbuf {
75183221Smarcel	l_time_t l_actime;
75283221Smarcel	l_time_t l_modtime;
75314381Speter};
7549313Ssos
7559313Ssosint
75683366Sjulianlinux_utime(struct thread *td, struct linux_utime_args *args)
7579313Ssos{
75883221Smarcel	struct timeval tv[2], *tvp;
75983221Smarcel	struct l_utimbuf lut;
760102814Siedowse	char *fname;
76183221Smarcel	int error;
7629313Ssos
763102814Siedowse	LCONVPATHEXIST(td, args->fname, &fname);
76414331Speter
7659313Ssos#ifdef DEBUG
76672543Sjlemon	if (ldebug(utime))
767102814Siedowse		printf(ARGS(utime, "%s, *"), fname);
7689313Ssos#endif
76914381Speter
77083221Smarcel	if (args->times) {
771111797Sdes		if ((error = copyin(args->times, &lut, sizeof lut))) {
772102814Siedowse			LFREEPATH(fname);
77383221Smarcel			return error;
774102814Siedowse		}
77583221Smarcel		tv[0].tv_sec = lut.l_actime;
77683221Smarcel		tv[0].tv_usec = 0;
77783221Smarcel		tv[1].tv_sec = lut.l_modtime;
77883221Smarcel		tv[1].tv_usec = 0;
779102814Siedowse		tvp = tv;
78083221Smarcel	} else
781102814Siedowse		tvp = NULL;
78283221Smarcel
783102814Siedowse	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
784102814Siedowse	LFREEPATH(fname);
785102814Siedowse	return (error);
7869313Ssos}
787133816Stjr#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
7889313Ssos
78944384Sjulian#define __WCLONE 0x80000000
79044384Sjulian
7919313Ssosint
79283366Sjulianlinux_waitpid(struct thread *td, struct linux_waitpid_args *args)
7939313Ssos{
794127140Sjhb	int error, options, tmpstat;
7959313Ssos
7969313Ssos#ifdef DEBUG
79772543Sjlemon	if (ldebug(waitpid))
79872543Sjlemon		printf(ARGS(waitpid, "%d, %p, %d"),
79972543Sjlemon		    args->pid, (void *)args->status, args->options);
8009313Ssos#endif
8019313Ssos
802127140Sjhb	options = (args->options & (WNOHANG | WUNTRACED));
80383221Smarcel	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
80483221Smarcel	if (args->options & __WCLONE)
805127140Sjhb		options |= WLINUXCLONE;
80643208Sjulian
807127140Sjhb	error = kern_wait(td, args->pid, &tmpstat, options, NULL);
808127140Sjhb	if (error)
80983221Smarcel		return error;
81083221Smarcel
81183221Smarcel	if (args->status) {
81283221Smarcel		tmpstat &= 0xffff;
81383221Smarcel		if (WIFSIGNALED(tmpstat))
81483221Smarcel			tmpstat = (tmpstat & 0xffffff80) |
81583221Smarcel			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
81683221Smarcel		else if (WIFSTOPPED(tmpstat))
81783221Smarcel			tmpstat = (tmpstat & 0xffff00ff) |
81883221Smarcel			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
819111797Sdes		return copyout(&tmpstat, args->status, sizeof(int));
82083221Smarcel	}
82183221Smarcel
82214331Speter	return 0;
8239313Ssos}
8249313Ssos
82514331Speterint
82683366Sjulianlinux_wait4(struct thread *td, struct linux_wait4_args *args)
8279313Ssos{
828127140Sjhb	int error, options, tmpstat;
829136152Sjhb	struct rusage ru, *rup;
830113613Sjhb	struct proc *p;
8319313Ssos
8329313Ssos#ifdef DEBUG
83372543Sjlemon	if (ldebug(wait4))
83472543Sjlemon		printf(ARGS(wait4, "%d, %p, %d, %p"),
83572543Sjlemon		    args->pid, (void *)args->status, args->options,
83672543Sjlemon		    (void *)args->rusage);
8379313Ssos#endif
8389313Ssos
839127140Sjhb	options = (args->options & (WNOHANG | WUNTRACED));
84083221Smarcel	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
84183221Smarcel	if (args->options & __WCLONE)
842127140Sjhb		options |= WLINUXCLONE;
84314331Speter
844136152Sjhb	if (args->rusage != NULL)
845136152Sjhb		rup = &ru;
846136152Sjhb	else
847136152Sjhb		rup = NULL;
848136152Sjhb	error = kern_wait(td, args->pid, &tmpstat, options, rup);
849127140Sjhb	if (error)
85083221Smarcel		return error;
85114331Speter
852113613Sjhb	p = td->td_proc;
853113613Sjhb	PROC_LOCK(p);
854151316Sdavidxu	sigqueue_delete(&p->p_sigqueue, SIGCHLD);
855113613Sjhb	PROC_UNLOCK(p);
85683221Smarcel
85783221Smarcel	if (args->status) {
85883221Smarcel		tmpstat &= 0xffff;
85983221Smarcel		if (WIFSIGNALED(tmpstat))
86083221Smarcel			tmpstat = (tmpstat & 0xffffff80) |
86183221Smarcel			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
86283221Smarcel		else if (WIFSTOPPED(tmpstat))
86383221Smarcel			tmpstat = (tmpstat & 0xffff00ff) |
86483221Smarcel			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
865127140Sjhb		error = copyout(&tmpstat, args->status, sizeof(int));
86683221Smarcel	}
867127140Sjhb	if (args->rusage != NULL && error == 0)
868127140Sjhb		error = copyout(&ru, args->rusage, sizeof(ru));
86983221Smarcel
870127140Sjhb	return (error);
8719313Ssos}
87213420Ssos
87314331Speterint
87483366Sjulianlinux_mknod(struct thread *td, struct linux_mknod_args *args)
87513420Ssos{
876102814Siedowse	char *path;
877102814Siedowse	int error;
87814331Speter
879102814Siedowse	LCONVPATHCREAT(td, args->path, &path);
88014331Speter
88114331Speter#ifdef DEBUG
88272543Sjlemon	if (ldebug(mknod))
883102814Siedowse		printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
88414331Speter#endif
88514331Speter
886102814Siedowse	if (args->mode & S_IFIFO)
887102814Siedowse		error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
888102814Siedowse	else
889102814Siedowse		error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
890102814Siedowse		    args->dev);
891102814Siedowse	LFREEPATH(path);
892102814Siedowse	return (error);
89313420Ssos}
89414331Speter
89514331Speter/*
89614331Speter * UGH! This is just about the dumbest idea I've ever heard!!
89714331Speter */
89814331Speterint
89983366Sjulianlinux_personality(struct thread *td, struct linux_personality_args *args)
90014331Speter{
90114331Speter#ifdef DEBUG
90272543Sjlemon	if (ldebug(personality))
903113579Sjhb		printf(ARGS(personality, "%lu"), (unsigned long)args->per);
90414331Speter#endif
90514331Speter	if (args->per != 0)
90614331Speter		return EINVAL;
90714331Speter
90814331Speter	/* Yes Jim, it's still a Linux... */
90983366Sjulian	td->td_retval[0] = 0;
91014331Speter	return 0;
91114331Speter}
91214331Speter
913133749Stjrstruct l_itimerval {
914133749Stjr	l_timeval it_interval;
915133749Stjr	l_timeval it_value;
916133749Stjr};
917133749Stjr
918140832Ssobomax#define	B2L_ITIMERVAL(bip, lip) 					\
919140832Ssobomax	(bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;		\
920140832Ssobomax	(bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;	\
921140832Ssobomax	(bip)->it_value.tv_sec = (lip)->it_value.tv_sec;		\
922140832Ssobomax	(bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
923140832Ssobomax
92414331Speterint
925133749Stjrlinux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
92614331Speter{
92714331Speter	int error;
928140832Ssobomax	struct l_itimerval ls;
929140832Ssobomax	struct itimerval aitv, oitv;
93014331Speter
93114331Speter#ifdef DEBUG
93272543Sjlemon	if (ldebug(setitimer))
93372543Sjlemon		printf(ARGS(setitimer, "%p, %p"),
934133840Sobrien		    (void *)uap->itv, (void *)uap->oitv);
93514331Speter#endif
936140832Ssobomax
937140832Ssobomax	if (uap->itv == NULL) {
938140832Ssobomax		uap->itv = uap->oitv;
939140832Ssobomax		return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
94014331Speter	}
941140832Ssobomax
942140832Ssobomax	error = copyin(uap->itv, &ls, sizeof(ls));
943133749Stjr	if (error != 0)
944133749Stjr		return (error);
945140832Ssobomax	B2L_ITIMERVAL(&aitv, &ls);
946140832Ssobomax#ifdef DEBUG
947140832Ssobomax	if (ldebug(setitimer)) {
948153775Strhodes		printf("setitimer: value: sec: %jd, usec: %ld\n",
949153775Strhodes		    (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
950153775Strhodes		printf("setitimer: interval: sec: %jd, usec: %ld\n",
951153775Strhodes		    (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
952133749Stjr	}
953140832Ssobomax#endif
954140832Ssobomax	error = kern_setitimer(td, uap->which, &aitv, &oitv);
955140832Ssobomax	if (error != 0 || uap->oitv == NULL)
956140832Ssobomax		return (error);
957140832Ssobomax	B2L_ITIMERVAL(&ls, &oitv);
958140832Ssobomax
959140832Ssobomax	return (copyout(&ls, uap->oitv, sizeof(ls)));
96014331Speter}
96114331Speter
96214331Speterint
963133749Stjrlinux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
96414331Speter{
965133749Stjr	int error;
966140832Ssobomax	struct l_itimerval ls;
967140832Ssobomax	struct itimerval aitv;
968133749Stjr
96914331Speter#ifdef DEBUG
97072543Sjlemon	if (ldebug(getitimer))
971133840Sobrien		printf(ARGS(getitimer, "%p"), (void *)uap->itv);
97214331Speter#endif
973140832Ssobomax	error = kern_getitimer(td, uap->which, &aitv);
974133749Stjr	if (error != 0)
975133749Stjr		return (error);
976140832Ssobomax	B2L_ITIMERVAL(&ls, &aitv);
977140832Ssobomax	return (copyout(&ls, uap->itv, sizeof(ls)));
97814331Speter}
97930837Skato
98030837Skatoint
98183366Sjulianlinux_nice(struct thread *td, struct linux_nice_args *args)
98230837Skato{
98330837Skato	struct setpriority_args	bsd_args;
98430837Skato
98530837Skato	bsd_args.which = PRIO_PROCESS;
98630837Skato	bsd_args.who = 0;	/* current process */
98730837Skato	bsd_args.prio = args->inc;
98883366Sjulian	return setpriority(td, &bsd_args);
98930837Skato}
99030837Skato
99142185Ssosint
99283366Sjulianlinux_setgroups(struct thread *td, struct linux_setgroups_args *args)
99342185Ssos{
99477183Srwatson	struct ucred *newcred, *oldcred;
99583221Smarcel	l_gid_t linux_gidset[NGROUPS];
99650350Smarcel	gid_t *bsd_gidset;
99750350Smarcel	int ngrp, error;
99894621Sjhb	struct proc *p;
99942185Ssos
100083221Smarcel	ngrp = args->gidsetsize;
1001121302Stjr	if (ngrp < 0 || ngrp >= NGROUPS)
100294621Sjhb		return (EINVAL);
1003111797Sdes	error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
100494621Sjhb	if (error)
100594621Sjhb		return (error);
100694621Sjhb	newcred = crget();
100794621Sjhb	p = td->td_proc;
100894621Sjhb	PROC_LOCK(p);
100994621Sjhb	oldcred = p->p_ucred;
101042185Ssos
101150350Smarcel	/*
101250350Smarcel	 * cr_groups[0] holds egid. Setting the whole set from
101350350Smarcel	 * the supplied set will cause egid to be changed too.
101450350Smarcel	 * Keep cr_groups[0] unchanged to prevent that.
101550350Smarcel	 */
101642185Ssos
1017132653Scperciva	if ((error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
101894621Sjhb		PROC_UNLOCK(p);
101994621Sjhb		crfree(newcred);
102050350Smarcel		return (error);
102194621Sjhb	}
102242185Ssos
102394621Sjhb	crcopy(newcred, oldcred);
102450350Smarcel	if (ngrp > 0) {
102577183Srwatson		newcred->cr_ngroups = ngrp + 1;
102650350Smarcel
102777183Srwatson		bsd_gidset = newcred->cr_groups;
102850350Smarcel		ngrp--;
102950350Smarcel		while (ngrp >= 0) {
103050350Smarcel			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
103150350Smarcel			ngrp--;
103250350Smarcel		}
103350350Smarcel	}
103450350Smarcel	else
103577183Srwatson		newcred->cr_ngroups = 1;
103650350Smarcel
103794621Sjhb	setsugid(p);
103894621Sjhb	p->p_ucred = newcred;
103994621Sjhb	PROC_UNLOCK(p);
104077183Srwatson	crfree(oldcred);
104150350Smarcel	return (0);
104242185Ssos}
104342185Ssos
104442185Ssosint
104583366Sjulianlinux_getgroups(struct thread *td, struct linux_getgroups_args *args)
104642185Ssos{
104777183Srwatson	struct ucred *cred;
104883221Smarcel	l_gid_t linux_gidset[NGROUPS];
104950350Smarcel	gid_t *bsd_gidset;
105050350Smarcel	int bsd_gidsetsz, ngrp, error;
105142185Ssos
105294454Sjhb	cred = td->td_ucred;
105377183Srwatson	bsd_gidset = cred->cr_groups;
105477183Srwatson	bsd_gidsetsz = cred->cr_ngroups - 1;
105542185Ssos
105650350Smarcel	/*
105750350Smarcel	 * cr_groups[0] holds egid. Returning the whole set
105850350Smarcel	 * here will cause a duplicate. Exclude cr_groups[0]
105950350Smarcel	 * to prevent that.
106050350Smarcel	 */
106142185Ssos
106283221Smarcel	if ((ngrp = args->gidsetsize) == 0) {
106383366Sjulian		td->td_retval[0] = bsd_gidsetsz;
106450350Smarcel		return (0);
106550350Smarcel	}
106642185Ssos
106750546Smarcel	if (ngrp < bsd_gidsetsz)
106850350Smarcel		return (EINVAL);
106942185Ssos
107050546Smarcel	ngrp = 0;
107150350Smarcel	while (ngrp < bsd_gidsetsz) {
107250546Smarcel		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
107350350Smarcel		ngrp++;
107450350Smarcel	}
107550350Smarcel
1076111797Sdes	if ((error = copyout(linux_gidset, args->grouplist,
107783221Smarcel	    ngrp * sizeof(l_gid_t))))
107850350Smarcel		return (error);
107950350Smarcel
108083366Sjulian	td->td_retval[0] = ngrp;
108150350Smarcel	return (0);
108242185Ssos}
108349626Smarcel
108449626Smarcelint
108583366Sjulianlinux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
108649626Smarcel{
1087102814Siedowse	struct rlimit bsd_rlim;
108883221Smarcel	struct l_rlimit rlim;
1089102814Siedowse	u_int which;
109065099Smarcel	int error;
109149842Smarcel
109249626Smarcel#ifdef DEBUG
109372543Sjlemon	if (ldebug(setrlimit))
109472543Sjlemon		printf(ARGS(setrlimit, "%d, %p"),
109583221Smarcel		    args->resource, (void *)args->rlim);
109649626Smarcel#endif
109749626Smarcel
109883221Smarcel	if (args->resource >= LINUX_RLIM_NLIMITS)
109965099Smarcel		return (EINVAL);
110049626Smarcel
1101102814Siedowse	which = linux_to_bsd_resource[args->resource];
1102102814Siedowse	if (which == -1)
110365099Smarcel		return (EINVAL);
110449626Smarcel
1105111797Sdes	error = copyin(args->rlim, &rlim, sizeof(rlim));
110665099Smarcel	if (error)
110765099Smarcel		return (error);
110849626Smarcel
1109102814Siedowse	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1110102814Siedowse	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1111125454Sjhb	return (kern_setrlimit(td, which, &bsd_rlim));
111249626Smarcel}
111349626Smarcel
111449626Smarcelint
111583366Sjulianlinux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
111649626Smarcel{
111783221Smarcel	struct l_rlimit rlim;
1118102814Siedowse	struct proc *p = td->td_proc;
1119125454Sjhb	struct rlimit bsd_rlim;
1120102814Siedowse	u_int which;
112149842Smarcel
112249626Smarcel#ifdef DEBUG
112383221Smarcel	if (ldebug(old_getrlimit))
112483221Smarcel		printf(ARGS(old_getrlimit, "%d, %p"),
112583221Smarcel		    args->resource, (void *)args->rlim);
112649626Smarcel#endif
112749626Smarcel
112883221Smarcel	if (args->resource >= LINUX_RLIM_NLIMITS)
112965099Smarcel		return (EINVAL);
113049626Smarcel
1131102814Siedowse	which = linux_to_bsd_resource[args->resource];
1132102814Siedowse	if (which == -1)
113365099Smarcel		return (EINVAL);
113449626Smarcel
1135125454Sjhb	PROC_LOCK(p);
1136125454Sjhb	lim_rlimit(p, which, &bsd_rlim);
1137125454Sjhb	PROC_UNLOCK(p);
1138125454Sjhb
1139140214Sobrien#ifdef COMPAT_LINUX32
1140140214Sobrien	rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1141140214Sobrien	if (rlim.rlim_cur == UINT_MAX)
1142140214Sobrien		rlim.rlim_cur = INT_MAX;
1143140214Sobrien	rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1144140214Sobrien	if (rlim.rlim_max == UINT_MAX)
1145140214Sobrien		rlim.rlim_max = INT_MAX;
1146140214Sobrien#else
1147125454Sjhb	rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
114865106Smarcel	if (rlim.rlim_cur == ULONG_MAX)
114965106Smarcel		rlim.rlim_cur = LONG_MAX;
1150125454Sjhb	rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
115165106Smarcel	if (rlim.rlim_max == ULONG_MAX)
115265106Smarcel		rlim.rlim_max = LONG_MAX;
1153133816Stjr#endif
1154111797Sdes	return (copyout(&rlim, args->rlim, sizeof(rlim)));
115549626Smarcel}
115683221Smarcel
115783221Smarcelint
115883366Sjulianlinux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
115983221Smarcel{
116083221Smarcel	struct l_rlimit rlim;
1161102814Siedowse	struct proc *p = td->td_proc;
1162125454Sjhb	struct rlimit bsd_rlim;
1163102814Siedowse	u_int which;
116483221Smarcel
116583221Smarcel#ifdef DEBUG
116683221Smarcel	if (ldebug(getrlimit))
116783221Smarcel		printf(ARGS(getrlimit, "%d, %p"),
116883221Smarcel		    args->resource, (void *)args->rlim);
116983221Smarcel#endif
117083221Smarcel
117183221Smarcel	if (args->resource >= LINUX_RLIM_NLIMITS)
117283221Smarcel		return (EINVAL);
117383221Smarcel
1174102814Siedowse	which = linux_to_bsd_resource[args->resource];
1175102814Siedowse	if (which == -1)
117683221Smarcel		return (EINVAL);
117783221Smarcel
1178125454Sjhb	PROC_LOCK(p);
1179125454Sjhb	lim_rlimit(p, which, &bsd_rlim);
1180125454Sjhb	PROC_UNLOCK(p);
1181125454Sjhb
1182125454Sjhb	rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1183125454Sjhb	rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1184111797Sdes	return (copyout(&rlim, args->rlim, sizeof(rlim)));
118583221Smarcel}
118649849Smarcel
118749849Smarcelint
118883366Sjulianlinux_sched_setscheduler(struct thread *td,
118983221Smarcel    struct linux_sched_setscheduler_args *args)
119049849Smarcel{
119149849Smarcel	struct sched_setscheduler_args bsd;
119249849Smarcel
119349849Smarcel#ifdef DEBUG
119472543Sjlemon	if (ldebug(sched_setscheduler))
119572543Sjlemon		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
119683221Smarcel		    args->pid, args->policy, (const void *)args->param);
119749849Smarcel#endif
119849849Smarcel
119983221Smarcel	switch (args->policy) {
120049849Smarcel	case LINUX_SCHED_OTHER:
120149849Smarcel		bsd.policy = SCHED_OTHER;
120249849Smarcel		break;
120349849Smarcel	case LINUX_SCHED_FIFO:
120449849Smarcel		bsd.policy = SCHED_FIFO;
120549849Smarcel		break;
120649849Smarcel	case LINUX_SCHED_RR:
120749849Smarcel		bsd.policy = SCHED_RR;
120849849Smarcel		break;
120949849Smarcel	default:
121049849Smarcel		return EINVAL;
121149849Smarcel	}
121249849Smarcel
121383221Smarcel	bsd.pid = args->pid;
121483221Smarcel	bsd.param = (struct sched_param *)args->param;
121583366Sjulian	return sched_setscheduler(td, &bsd);
121649849Smarcel}
121749849Smarcel
121849849Smarcelint
121983366Sjulianlinux_sched_getscheduler(struct thread *td,
122083221Smarcel    struct linux_sched_getscheduler_args *args)
122149849Smarcel{
122249849Smarcel	struct sched_getscheduler_args bsd;
122349849Smarcel	int error;
122449849Smarcel
122549849Smarcel#ifdef DEBUG
122672543Sjlemon	if (ldebug(sched_getscheduler))
122783221Smarcel		printf(ARGS(sched_getscheduler, "%d"), args->pid);
122849849Smarcel#endif
122949849Smarcel
123083221Smarcel	bsd.pid = args->pid;
123183366Sjulian	error = sched_getscheduler(td, &bsd);
123249849Smarcel
123383366Sjulian	switch (td->td_retval[0]) {
123449849Smarcel	case SCHED_OTHER:
123583366Sjulian		td->td_retval[0] = LINUX_SCHED_OTHER;
123649849Smarcel		break;
123749849Smarcel	case SCHED_FIFO:
123883366Sjulian		td->td_retval[0] = LINUX_SCHED_FIFO;
123949849Smarcel		break;
124049849Smarcel	case SCHED_RR:
124183366Sjulian		td->td_retval[0] = LINUX_SCHED_RR;
124249849Smarcel		break;
124349849Smarcel	}
124449849Smarcel
124549849Smarcel	return error;
124649849Smarcel}
124772538Sjlemon
124875053Salcint
124983366Sjulianlinux_sched_get_priority_max(struct thread *td,
125083221Smarcel    struct linux_sched_get_priority_max_args *args)
125175053Salc{
125275053Salc	struct sched_get_priority_max_args bsd;
125375053Salc
125475053Salc#ifdef DEBUG
125575053Salc	if (ldebug(sched_get_priority_max))
125683221Smarcel		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
125775053Salc#endif
125875053Salc
125983221Smarcel	switch (args->policy) {
126075053Salc	case LINUX_SCHED_OTHER:
126175053Salc		bsd.policy = SCHED_OTHER;
126275053Salc		break;
126375053Salc	case LINUX_SCHED_FIFO:
126475053Salc		bsd.policy = SCHED_FIFO;
126575053Salc		break;
126675053Salc	case LINUX_SCHED_RR:
126775053Salc		bsd.policy = SCHED_RR;
126875053Salc		break;
126975053Salc	default:
127075053Salc		return EINVAL;
127175053Salc	}
127283366Sjulian	return sched_get_priority_max(td, &bsd);
127375053Salc}
127475053Salc
127575053Salcint
127683366Sjulianlinux_sched_get_priority_min(struct thread *td,
127783221Smarcel    struct linux_sched_get_priority_min_args *args)
127875053Salc{
127975053Salc	struct sched_get_priority_min_args bsd;
128075053Salc
128175053Salc#ifdef DEBUG
128275053Salc	if (ldebug(sched_get_priority_min))
128383221Smarcel		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
128475053Salc#endif
128575053Salc
128683221Smarcel	switch (args->policy) {
128775053Salc	case LINUX_SCHED_OTHER:
128875053Salc		bsd.policy = SCHED_OTHER;
128975053Salc		break;
129075053Salc	case LINUX_SCHED_FIFO:
129175053Salc		bsd.policy = SCHED_FIFO;
129275053Salc		break;
129375053Salc	case LINUX_SCHED_RR:
129475053Salc		bsd.policy = SCHED_RR;
129575053Salc		break;
129675053Salc	default:
129775053Salc		return EINVAL;
129875053Salc	}
129983366Sjulian	return sched_get_priority_min(td, &bsd);
130075053Salc}
130175053Salc
130272538Sjlemon#define REBOOT_CAD_ON	0x89abcdef
130372538Sjlemon#define REBOOT_CAD_OFF	0
130472538Sjlemon#define REBOOT_HALT	0xcdef0123
130572538Sjlemon
130672538Sjlemonint
130783366Sjulianlinux_reboot(struct thread *td, struct linux_reboot_args *args)
130872538Sjlemon{
130972538Sjlemon	struct reboot_args bsd_args;
131072538Sjlemon
131172538Sjlemon#ifdef DEBUG
131272538Sjlemon	if (ldebug(reboot))
131383221Smarcel		printf(ARGS(reboot, "0x%x"), args->cmd);
131472538Sjlemon#endif
131583221Smarcel	if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
131672538Sjlemon		return (0);
131783221Smarcel	bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
131883366Sjulian	return (reboot(td, &bsd_args));
131972538Sjlemon}
132083221Smarcel
132189717Sgallatin
132283221Smarcel/*
132383221Smarcel * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1324130344Sphk * td->td_retval[1] when COMPAT_43 is defined. This
132583221Smarcel * globbers registers that are assumed to be preserved. The following
132683221Smarcel * lightweight syscalls fixes this. See also linux_getgid16() and
132783221Smarcel * linux_getuid16() in linux_uid16.c.
132883221Smarcel *
132983221Smarcel * linux_getpid() - MP SAFE
133083221Smarcel * linux_getgid() - MP SAFE
133183221Smarcel * linux_getuid() - MP SAFE
133283221Smarcel */
133383221Smarcel
133483221Smarcelint
133583366Sjulianlinux_getpid(struct thread *td, struct linux_getpid_args *args)
133683221Smarcel{
1337161317Snetchild#ifdef __i386__
1338161310Snetchild   	struct linux_emuldata *em;
133983366Sjulian
1340161310Snetchild	em = em_find(td->td_proc, EMUL_UNLOCKED);
1341161310Snetchild
1342161310Snetchild	KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1343161310Snetchild
1344161310Snetchild	td->td_retval[0] = em->shared->group_pid;
1345161310Snetchild	EMUL_UNLOCK(&emul_lock);
1346161317Snetchild#else
1347161317Snetchild	td->td_retval[0] = td->td_proc->p_pid;
1348161317Snetchild#endif
1349161310Snetchild	return (0);
1350161310Snetchild}
1351161310Snetchild
1352161310Snetchildint
1353161310Snetchildlinux_gettid(struct thread *td, struct linux_gettid_args *args)
1354161310Snetchild{
1355161310Snetchild#ifdef DEBUG
1356161310Snetchild	if (ldebug(gettid))
1357161310Snetchild		printf(ARGS(gettid, ""));
1358161310Snetchild#endif
1359161310Snetchild
136083366Sjulian	td->td_retval[0] = td->td_proc->p_pid;
136183221Smarcel	return (0);
136283221Smarcel}
136383221Smarcel
1364161310Snetchild
136583221Smarcelint
1366161310Snetchildlinux_getppid(struct thread *td, struct linux_getppid_args *args)
1367161310Snetchild{
1368161317Snetchild#ifdef	__i386__
1369161310Snetchild   	struct linux_emuldata *em;
1370161310Snetchild	struct proc *p, *pp;
1371161310Snetchild
1372161310Snetchild	em = em_find(td->td_proc, EMUL_UNLOCKED);
1373161310Snetchild
1374161310Snetchild	KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1375161310Snetchild
1376161310Snetchild	/* find the group leader */
1377161310Snetchild	p = pfind(em->shared->group_pid);
1378161310Snetchild
1379161310Snetchild	if (p == NULL) {
1380161310Snetchild#ifdef DEBUG
1381161310Snetchild	   	printf(LMSG("parent process not found.\n"));
1382161310Snetchild#endif
1383161310Snetchild		return (0);
1384161310Snetchild	}
1385161310Snetchild
1386161310Snetchild	pp = p->p_pptr;		/* switch to parent */
1387161310Snetchild	PROC_LOCK(pp);
1388161310Snetchild	PROC_UNLOCK(p);
1389161310Snetchild
1390161310Snetchild	/* if its also linux process */
1391161310Snetchild	if (pp->p_sysent == &elf_linux_sysvec) {
1392161310Snetchild   	   	em = em_find(pp, EMUL_LOCKED);
1393161310Snetchild		KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1394161310Snetchild
1395161310Snetchild	   	td->td_retval[0] = em->shared->group_pid;
1396161310Snetchild	} else
1397161310Snetchild	   	td->td_retval[0] = pp->p_pid;
1398161310Snetchild
1399161310Snetchild	EMUL_UNLOCK(&emul_lock);
1400161310Snetchild	PROC_UNLOCK(pp);
1401161317Snetchild#else
1402161317Snetchild	return getppid(td, (struct getppid_args *) args);
1403161317Snetchild#endif
1404161310Snetchild	return (0);
1405161310Snetchild}
1406161310Snetchild
1407161310Snetchildint
140883366Sjulianlinux_getgid(struct thread *td, struct linux_getgid_args *args)
140983221Smarcel{
141083366Sjulian
141194454Sjhb	td->td_retval[0] = td->td_ucred->cr_rgid;
141283221Smarcel	return (0);
141383221Smarcel}
141483221Smarcel
141583221Smarcelint
141683366Sjulianlinux_getuid(struct thread *td, struct linux_getuid_args *args)
141783221Smarcel{
141883366Sjulian
141994454Sjhb	td->td_retval[0] = td->td_ucred->cr_ruid;
142083221Smarcel	return (0);
142183221Smarcel}
142283503Smr
142389717Sgallatin
142483503Smrint
142583503Smrlinux_getsid(struct thread *td, struct linux_getsid_args *args)
142683503Smr{
142783503Smr	struct getsid_args bsd;
142883503Smr	bsd.pid = args->pid;
142983503Smr	return getsid(td, &bsd);
143083503Smr}
1431143197Ssobomax
1432143197Ssobomaxint
1433143197Ssobomaxlinux_nosys(struct thread *td, struct nosys_args *ignore)
1434143197Ssobomax{
1435143197Ssobomax
1436143197Ssobomax	return (ENOSYS);
1437143197Ssobomax}
1438147141Ssobomax
1439147141Ssobomaxint
1440147141Ssobomaxlinux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1441147141Ssobomax{
1442147141Ssobomax	struct getpriority_args	bsd_args;
1443147141Ssobomax	int error;
1444147141Ssobomax
1445147141Ssobomax	bsd_args.which = args->which;
1446147141Ssobomax	bsd_args.who = args->who;
1447147141Ssobomax	error = getpriority(td, &bsd_args);
1448147141Ssobomax	td->td_retval[0] = 20 - td->td_retval[0];
1449147141Ssobomax	return error;
1450147141Ssobomax}
1451156842Snetchild
1452156842Snetchildint
1453156842Snetchildlinux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1454156842Snetchild{
1455156842Snetchild	int name[2];
1456156842Snetchild	int error;
1457156842Snetchild
1458156842Snetchild	name[0] = CTL_KERN;
1459156842Snetchild	name[1] = KERN_HOSTNAME;
1460157183Sjhb	if ((error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL)))
1461156842Snetchild		return (error);
1462156842Snetchild	return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1463156842Snetchild		 args->len, 0, 0));
1464156842Snetchild}
1465156842Snetchild
1466161310Snetchildint
1467161310Snetchildlinux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1468161310Snetchild{
1469161310Snetchild   	struct linux_emuldata *em, *td_em, *tmp_em;
1470161310Snetchild	struct proc *sp;
1471161310Snetchild
1472161310Snetchild#ifdef DEBUG
1473161310Snetchild	if (ldebug(exit_group))
1474161310Snetchild		printf(ARGS(exit_group, "%i"), args->error_code);
1475161310Snetchild#endif
1476161310Snetchild
1477161310Snetchild	td_em = em_find(td->td_proc, EMUL_UNLOCKED);
1478161310Snetchild
1479161310Snetchild	KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n"));
1480161310Snetchild
1481161310Snetchild	EMUL_SHARED_RLOCK(&emul_shared_lock);
1482161310Snetchild     	LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
1483161310Snetchild	   	if (em->pid == td_em->pid)
1484161310Snetchild		   	continue;
1485161310Snetchild
1486161310Snetchild		sp = pfind(em->pid);
1487161310Snetchild		psignal(sp, SIGKILL);
1488161310Snetchild		PROC_UNLOCK(sp);
1489161310Snetchild#ifdef DEBUG
1490161310Snetchild		printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid);
1491161310Snetchild#endif
1492161310Snetchild	}
1493161310Snetchild
1494161310Snetchild	EMUL_SHARED_RUNLOCK(&emul_shared_lock);
1495161310Snetchild	EMUL_UNLOCK(&emul_lock);
1496161310Snetchild
1497161310Snetchild	exit1(td, W_EXITCODE(args->error_code,0));
1498161310Snetchild
1499161310Snetchild   	return (0);
1500161310Snetchild}
1501161310Snetchild
1502