linux_misc.c revision 293503
1/*-
2 * Copyright (c) 2002 Doug Rabson
3 * Copyright (c) 1994-1995 S��ren Schmidt
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer
11 *    in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: stable/10/sys/compat/linux/linux_misc.c 293503 2016-01-09 15:28:05Z dchagin $");
32
33#include "opt_compat.h"
34#include "opt_kdtrace.h"
35
36#include <sys/param.h>
37#include <sys/blist.h>
38#include <sys/fcntl.h>
39#if defined(__i386__)
40#include <sys/imgact_aout.h>
41#endif
42#include <sys/jail.h>
43#include <sys/kernel.h>
44#include <sys/limits.h>
45#include <sys/lock.h>
46#include <sys/malloc.h>
47#include <sys/mman.h>
48#include <sys/mount.h>
49#include <sys/mutex.h>
50#include <sys/namei.h>
51#include <sys/priv.h>
52#include <sys/proc.h>
53#include <sys/reboot.h>
54#include <sys/racct.h>
55#include <sys/resourcevar.h>
56#include <sys/sched.h>
57#include <sys/signalvar.h>
58#include <sys/stat.h>
59#include <sys/syscallsubr.h>
60#include <sys/sysctl.h>
61#include <sys/sysproto.h>
62#include <sys/systm.h>
63#include <sys/time.h>
64#include <sys/vmmeter.h>
65#include <sys/vnode.h>
66#include <sys/wait.h>
67#include <sys/cpuset.h>
68
69#include <security/mac/mac_framework.h>
70
71#include <vm/vm.h>
72#include <vm/pmap.h>
73#include <vm/vm_kern.h>
74#include <vm/vm_map.h>
75#include <vm/vm_extern.h>
76#include <vm/vm_object.h>
77#include <vm/swap_pager.h>
78
79#ifdef COMPAT_LINUX32
80#include <machine/../linux32/linux.h>
81#include <machine/../linux32/linux32_proto.h>
82#else
83#include <machine/../linux/linux.h>
84#include <machine/../linux/linux_proto.h>
85#endif
86
87#include <compat/linux/linux_file.h>
88#include <compat/linux/linux_mib.h>
89#include <compat/linux/linux_signal.h>
90#include <compat/linux/linux_util.h>
91#include <compat/linux/linux_sysproto.h>
92#include <compat/linux/linux_emul.h>
93#include <compat/linux/linux_misc.h>
94
95int stclohz;				/* Statistics clock frequency */
96
97static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
98	RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
99	RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
100	RLIMIT_MEMLOCK, RLIMIT_AS
101};
102
103struct l_sysinfo {
104	l_long		uptime;		/* Seconds since boot */
105	l_ulong		loads[3];	/* 1, 5, and 15 minute load averages */
106#define LINUX_SYSINFO_LOADS_SCALE 65536
107	l_ulong		totalram;	/* Total usable main memory size */
108	l_ulong		freeram;	/* Available memory size */
109	l_ulong		sharedram;	/* Amount of shared memory */
110	l_ulong		bufferram;	/* Memory used by buffers */
111	l_ulong		totalswap;	/* Total swap space size */
112	l_ulong		freeswap;	/* swap space still available */
113	l_ushort	procs;		/* Number of current processes */
114	l_ushort	pads;
115	l_ulong		totalbig;
116	l_ulong		freebig;
117	l_uint		mem_unit;
118	char		_f[20-2*sizeof(l_long)-sizeof(l_int)];	/* padding */
119};
120int
121linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
122{
123	struct l_sysinfo sysinfo;
124	vm_object_t object;
125	int i, j;
126	struct timespec ts;
127
128	getnanouptime(&ts);
129	if (ts.tv_nsec != 0)
130		ts.tv_sec++;
131	sysinfo.uptime = ts.tv_sec;
132
133	/* Use the information from the mib to get our load averages */
134	for (i = 0; i < 3; i++)
135		sysinfo.loads[i] = averunnable.ldavg[i] *
136		    LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
137
138	sysinfo.totalram = physmem * PAGE_SIZE;
139	sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
140
141	sysinfo.sharedram = 0;
142	mtx_lock(&vm_object_list_mtx);
143	TAILQ_FOREACH(object, &vm_object_list, object_list)
144		if (object->shadow_count > 1)
145			sysinfo.sharedram += object->resident_page_count;
146	mtx_unlock(&vm_object_list_mtx);
147
148	sysinfo.sharedram *= PAGE_SIZE;
149	sysinfo.bufferram = 0;
150
151	swap_pager_status(&i, &j);
152	sysinfo.totalswap = i * PAGE_SIZE;
153	sysinfo.freeswap = (i - j) * PAGE_SIZE;
154
155	sysinfo.procs = nprocs;
156
157	/* The following are only present in newer Linux kernels. */
158	sysinfo.totalbig = 0;
159	sysinfo.freebig = 0;
160	sysinfo.mem_unit = 1;
161
162	return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
163}
164
165int
166linux_alarm(struct thread *td, struct linux_alarm_args *args)
167{
168	struct itimerval it, old_it;
169	u_int secs;
170	int error;
171
172#ifdef DEBUG
173	if (ldebug(alarm))
174		printf(ARGS(alarm, "%u"), args->secs);
175#endif
176
177	secs = args->secs;
178
179	if (secs > INT_MAX)
180		secs = INT_MAX;
181
182	it.it_value.tv_sec = (long) secs;
183	it.it_value.tv_usec = 0;
184	it.it_interval.tv_sec = 0;
185	it.it_interval.tv_usec = 0;
186	error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
187	if (error)
188		return (error);
189	if (timevalisset(&old_it.it_value)) {
190		if (old_it.it_value.tv_usec != 0)
191			old_it.it_value.tv_sec++;
192		td->td_retval[0] = old_it.it_value.tv_sec;
193	}
194	return (0);
195}
196
197int
198linux_brk(struct thread *td, struct linux_brk_args *args)
199{
200	struct vmspace *vm = td->td_proc->p_vmspace;
201	vm_offset_t new, old;
202	struct obreak_args /* {
203		char * nsize;
204	} */ tmp;
205
206#ifdef DEBUG
207	if (ldebug(brk))
208		printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
209#endif
210	old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
211	new = (vm_offset_t)args->dsend;
212	tmp.nsize = (char *)new;
213	if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
214		td->td_retval[0] = (long)new;
215	else
216		td->td_retval[0] = (long)old;
217
218	return (0);
219}
220
221#if defined(__i386__)
222/* XXX: what about amd64/linux32? */
223
224int
225linux_uselib(struct thread *td, struct linux_uselib_args *args)
226{
227	struct nameidata ni;
228	struct vnode *vp;
229	struct exec *a_out;
230	struct vattr attr;
231	vm_offset_t vmaddr;
232	unsigned long file_offset;
233	unsigned long bss_size;
234	char *library;
235	ssize_t aresid;
236	int error, locked, writecount;
237
238	LCONVPATHEXIST(td, args->library, &library);
239
240#ifdef DEBUG
241	if (ldebug(uselib))
242		printf(ARGS(uselib, "%s"), library);
243#endif
244
245	a_out = NULL;
246	locked = 0;
247	vp = NULL;
248
249	NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
250	    UIO_SYSSPACE, library, td);
251	error = namei(&ni);
252	LFREEPATH(library);
253	if (error)
254		goto cleanup;
255
256	vp = ni.ni_vp;
257	NDFREE(&ni, NDF_ONLY_PNBUF);
258
259	/*
260	 * From here on down, we have a locked vnode that must be unlocked.
261	 * XXX: The code below largely duplicates exec_check_permissions().
262	 */
263	locked = 1;
264
265	/* Writable? */
266	error = VOP_GET_WRITECOUNT(vp, &writecount);
267	if (error != 0)
268		goto cleanup;
269	if (writecount != 0) {
270		error = ETXTBSY;
271		goto cleanup;
272	}
273
274	/* Executable? */
275	error = VOP_GETATTR(vp, &attr, td->td_ucred);
276	if (error)
277		goto cleanup;
278
279	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
280	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
281		/* EACCESS is what exec(2) returns. */
282		error = ENOEXEC;
283		goto cleanup;
284	}
285
286	/* Sensible size? */
287	if (attr.va_size == 0) {
288		error = ENOEXEC;
289		goto cleanup;
290	}
291
292	/* Can we access it? */
293	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
294	if (error)
295		goto cleanup;
296
297	/*
298	 * XXX: This should use vn_open() so that it is properly authorized,
299	 * and to reduce code redundancy all over the place here.
300	 * XXX: Not really, it duplicates far more of exec_check_permissions()
301	 * than vn_open().
302	 */
303#ifdef MAC
304	error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
305	if (error)
306		goto cleanup;
307#endif
308	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
309	if (error)
310		goto cleanup;
311
312	/* Pull in executable header into exec_map */
313	error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
314	    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
315	if (error)
316		goto cleanup;
317
318	/* Is it a Linux binary ? */
319	if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
320		error = ENOEXEC;
321		goto cleanup;
322	}
323
324	/*
325	 * While we are here, we should REALLY do some more checks
326	 */
327
328	/* Set file/virtual offset based on a.out variant. */
329	switch ((int)(a_out->a_magic & 0xffff)) {
330	case 0413:			/* ZMAGIC */
331		file_offset = 1024;
332		break;
333	case 0314:			/* QMAGIC */
334		file_offset = 0;
335		break;
336	default:
337		error = ENOEXEC;
338		goto cleanup;
339	}
340
341	bss_size = round_page(a_out->a_bss);
342
343	/* Check various fields in header for validity/bounds. */
344	if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
345		error = ENOEXEC;
346		goto cleanup;
347	}
348
349	/* text + data can't exceed file size */
350	if (a_out->a_data + a_out->a_text > attr.va_size) {
351		error = EFAULT;
352		goto cleanup;
353	}
354
355	/*
356	 * text/data/bss must not exceed limits
357	 * XXX - this is not complete. it should check current usage PLUS
358	 * the resources needed by this library.
359	 */
360	PROC_LOCK(td->td_proc);
361	if (a_out->a_text > maxtsiz ||
362	    a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA) ||
363	    racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
364	    bss_size) != 0) {
365		PROC_UNLOCK(td->td_proc);
366		error = ENOMEM;
367		goto cleanup;
368	}
369	PROC_UNLOCK(td->td_proc);
370
371	/*
372	 * Prevent more writers.
373	 * XXX: Note that if any of the VM operations fail below we don't
374	 * clear this flag.
375	 */
376	VOP_SET_TEXT(vp);
377
378	/*
379	 * Lock no longer needed
380	 */
381	locked = 0;
382	VOP_UNLOCK(vp, 0);
383
384	/*
385	 * Check if file_offset page aligned. Currently we cannot handle
386	 * misalinged file offsets, and so we read in the entire image
387	 * (what a waste).
388	 */
389	if (file_offset & PAGE_MASK) {
390#ifdef DEBUG
391		printf("uselib: Non page aligned binary %lu\n", file_offset);
392#endif
393		/* Map text+data read/write/execute */
394
395		/* a_entry is the load address and is page aligned */
396		vmaddr = trunc_page(a_out->a_entry);
397
398		/* get anon user mapping, read+write+execute */
399		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
400		    &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
401		    VM_PROT_ALL, VM_PROT_ALL, 0);
402		if (error)
403			goto cleanup;
404
405		error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
406		    a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
407		    td->td_ucred, NOCRED, &aresid, td);
408		if (error != 0)
409			goto cleanup;
410		if (aresid != 0) {
411			error = ENOEXEC;
412			goto cleanup;
413		}
414	} else {
415#ifdef DEBUG
416		printf("uselib: Page aligned binary %lu\n", file_offset);
417#endif
418		/*
419		 * for QMAGIC, a_entry is 20 bytes beyond the load address
420		 * to skip the executable header
421		 */
422		vmaddr = trunc_page(a_out->a_entry);
423
424		/*
425		 * Map it all into the process's space as a single
426		 * copy-on-write "data" segment.
427		 */
428		error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
429		    a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
430		    MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
431		if (error)
432			goto cleanup;
433	}
434#ifdef DEBUG
435	printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
436	    ((long *)vmaddr)[1]);
437#endif
438	if (bss_size != 0) {
439		/* Calculate BSS start address */
440		vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
441		    a_out->a_data;
442
443		/* allocate some 'anon' space */
444		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
445		    &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
446		    VM_PROT_ALL, 0);
447		if (error)
448			goto cleanup;
449	}
450
451cleanup:
452	/* Unlock vnode if needed */
453	if (locked)
454		VOP_UNLOCK(vp, 0);
455
456	/* Release the temporary mapping. */
457	if (a_out)
458		kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
459
460	return (error);
461}
462
463#endif	/* __i386__ */
464
465int
466linux_select(struct thread *td, struct linux_select_args *args)
467{
468	l_timeval ltv;
469	struct timeval tv0, tv1, utv, *tvp;
470	int error;
471
472#ifdef DEBUG
473	if (ldebug(select))
474		printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
475		    (void *)args->readfds, (void *)args->writefds,
476		    (void *)args->exceptfds, (void *)args->timeout);
477#endif
478
479	/*
480	 * Store current time for computation of the amount of
481	 * time left.
482	 */
483	if (args->timeout) {
484		if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
485			goto select_out;
486		utv.tv_sec = ltv.tv_sec;
487		utv.tv_usec = ltv.tv_usec;
488#ifdef DEBUG
489		if (ldebug(select))
490			printf(LMSG("incoming timeout (%jd/%ld)"),
491			    (intmax_t)utv.tv_sec, utv.tv_usec);
492#endif
493
494		if (itimerfix(&utv)) {
495			/*
496			 * The timeval was invalid.  Convert it to something
497			 * valid that will act as it does under Linux.
498			 */
499			utv.tv_sec += utv.tv_usec / 1000000;
500			utv.tv_usec %= 1000000;
501			if (utv.tv_usec < 0) {
502				utv.tv_sec -= 1;
503				utv.tv_usec += 1000000;
504			}
505			if (utv.tv_sec < 0)
506				timevalclear(&utv);
507		}
508		microtime(&tv0);
509		tvp = &utv;
510	} else
511		tvp = NULL;
512
513	error = kern_select(td, args->nfds, args->readfds, args->writefds,
514	    args->exceptfds, tvp, sizeof(l_int) * 8);
515
516#ifdef DEBUG
517	if (ldebug(select))
518		printf(LMSG("real select returns %d"), error);
519#endif
520	if (error)
521		goto select_out;
522
523	if (args->timeout) {
524		if (td->td_retval[0]) {
525			/*
526			 * Compute how much time was left of the timeout,
527			 * by subtracting the current time and the time
528			 * before we started the call, and subtracting
529			 * that result from the user-supplied value.
530			 */
531			microtime(&tv1);
532			timevalsub(&tv1, &tv0);
533			timevalsub(&utv, &tv1);
534			if (utv.tv_sec < 0)
535				timevalclear(&utv);
536		} else
537			timevalclear(&utv);
538#ifdef DEBUG
539		if (ldebug(select))
540			printf(LMSG("outgoing timeout (%jd/%ld)"),
541			    (intmax_t)utv.tv_sec, utv.tv_usec);
542#endif
543		ltv.tv_sec = utv.tv_sec;
544		ltv.tv_usec = utv.tv_usec;
545		if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
546			goto select_out;
547	}
548
549select_out:
550#ifdef DEBUG
551	if (ldebug(select))
552		printf(LMSG("select_out -> %d"), error);
553#endif
554	return (error);
555}
556
557int
558linux_mremap(struct thread *td, struct linux_mremap_args *args)
559{
560	struct munmap_args /* {
561		void *addr;
562		size_t len;
563	} */ bsd_args;
564	int error = 0;
565
566#ifdef DEBUG
567	if (ldebug(mremap))
568		printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
569		    (void *)(uintptr_t)args->addr,
570		    (unsigned long)args->old_len,
571		    (unsigned long)args->new_len,
572		    (unsigned long)args->flags);
573#endif
574
575	if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
576		td->td_retval[0] = 0;
577		return (EINVAL);
578	}
579
580	/*
581	 * Check for the page alignment.
582	 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
583	 */
584	if (args->addr & PAGE_MASK) {
585		td->td_retval[0] = 0;
586		return (EINVAL);
587	}
588
589	args->new_len = round_page(args->new_len);
590	args->old_len = round_page(args->old_len);
591
592	if (args->new_len > args->old_len) {
593		td->td_retval[0] = 0;
594		return (ENOMEM);
595	}
596
597	if (args->new_len < args->old_len) {
598		bsd_args.addr =
599		    (caddr_t)((uintptr_t)args->addr + args->new_len);
600		bsd_args.len = args->old_len - args->new_len;
601		error = sys_munmap(td, &bsd_args);
602	}
603
604	td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
605	return (error);
606}
607
608#define LINUX_MS_ASYNC       0x0001
609#define LINUX_MS_INVALIDATE  0x0002
610#define LINUX_MS_SYNC        0x0004
611
612int
613linux_msync(struct thread *td, struct linux_msync_args *args)
614{
615	struct msync_args bsd_args;
616
617	bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
618	bsd_args.len = (uintptr_t)args->len;
619	bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
620
621	return (sys_msync(td, &bsd_args));
622}
623
624int
625linux_time(struct thread *td, struct linux_time_args *args)
626{
627	struct timeval tv;
628	l_time_t tm;
629	int error;
630
631#ifdef DEBUG
632	if (ldebug(time))
633		printf(ARGS(time, "*"));
634#endif
635
636	microtime(&tv);
637	tm = tv.tv_sec;
638	if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
639		return (error);
640	td->td_retval[0] = tm;
641	return (0);
642}
643
644struct l_times_argv {
645	l_clock_t	tms_utime;
646	l_clock_t	tms_stime;
647	l_clock_t	tms_cutime;
648	l_clock_t	tms_cstime;
649};
650
651
652/*
653 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
654 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
655 * auxiliary vector entry.
656 */
657#define	CLK_TCK		100
658
659#define	CONVOTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
660#define	CONVNTCK(r)	(r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
661
662#define	CONVTCK(r)	(linux_kernver(td) >= LINUX_KERNVER_2004000 ?		\
663			    CONVNTCK(r) : CONVOTCK(r))
664
665int
666linux_times(struct thread *td, struct linux_times_args *args)
667{
668	struct timeval tv, utime, stime, cutime, cstime;
669	struct l_times_argv tms;
670	struct proc *p;
671	int error;
672
673#ifdef DEBUG
674	if (ldebug(times))
675		printf(ARGS(times, "*"));
676#endif
677
678	if (args->buf != NULL) {
679		p = td->td_proc;
680		PROC_LOCK(p);
681		PROC_STATLOCK(p);
682		calcru(p, &utime, &stime);
683		PROC_STATUNLOCK(p);
684		calccru(p, &cutime, &cstime);
685		PROC_UNLOCK(p);
686
687		tms.tms_utime = CONVTCK(utime);
688		tms.tms_stime = CONVTCK(stime);
689
690		tms.tms_cutime = CONVTCK(cutime);
691		tms.tms_cstime = CONVTCK(cstime);
692
693		if ((error = copyout(&tms, args->buf, sizeof(tms))))
694			return (error);
695	}
696
697	microuptime(&tv);
698	td->td_retval[0] = (int)CONVTCK(tv);
699	return (0);
700}
701
702int
703linux_newuname(struct thread *td, struct linux_newuname_args *args)
704{
705	struct l_new_utsname utsname;
706	char osname[LINUX_MAX_UTSNAME];
707	char osrelease[LINUX_MAX_UTSNAME];
708	char *p;
709
710#ifdef DEBUG
711	if (ldebug(newuname))
712		printf(ARGS(newuname, "*"));
713#endif
714
715	linux_get_osname(td, osname);
716	linux_get_osrelease(td, osrelease);
717
718	bzero(&utsname, sizeof(utsname));
719	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
720	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
721	getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
722	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
723	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
724	for (p = utsname.version; *p != '\0'; ++p)
725		if (*p == '\n') {
726			*p = '\0';
727			break;
728		}
729	strlcpy(utsname.machine, linux_platform, LINUX_MAX_UTSNAME);
730
731	return (copyout(&utsname, args->buf, sizeof(utsname)));
732}
733
734#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
735struct l_utimbuf {
736	l_time_t l_actime;
737	l_time_t l_modtime;
738};
739
740int
741linux_utime(struct thread *td, struct linux_utime_args *args)
742{
743	struct timeval tv[2], *tvp;
744	struct l_utimbuf lut;
745	char *fname;
746	int error;
747
748	LCONVPATHEXIST(td, args->fname, &fname);
749
750#ifdef DEBUG
751	if (ldebug(utime))
752		printf(ARGS(utime, "%s, *"), fname);
753#endif
754
755	if (args->times) {
756		if ((error = copyin(args->times, &lut, sizeof lut))) {
757			LFREEPATH(fname);
758			return (error);
759		}
760		tv[0].tv_sec = lut.l_actime;
761		tv[0].tv_usec = 0;
762		tv[1].tv_sec = lut.l_modtime;
763		tv[1].tv_usec = 0;
764		tvp = tv;
765	} else
766		tvp = NULL;
767
768	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
769	LFREEPATH(fname);
770	return (error);
771}
772
773int
774linux_utimes(struct thread *td, struct linux_utimes_args *args)
775{
776	l_timeval ltv[2];
777	struct timeval tv[2], *tvp = NULL;
778	char *fname;
779	int error;
780
781	LCONVPATHEXIST(td, args->fname, &fname);
782
783#ifdef DEBUG
784	if (ldebug(utimes))
785		printf(ARGS(utimes, "%s, *"), fname);
786#endif
787
788	if (args->tptr != NULL) {
789		if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
790			LFREEPATH(fname);
791			return (error);
792		}
793		tv[0].tv_sec = ltv[0].tv_sec;
794		tv[0].tv_usec = ltv[0].tv_usec;
795		tv[1].tv_sec = ltv[1].tv_sec;
796		tv[1].tv_usec = ltv[1].tv_usec;
797		tvp = tv;
798	}
799
800	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
801	LFREEPATH(fname);
802	return (error);
803}
804
805int
806linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
807{
808	l_timeval ltv[2];
809	struct timeval tv[2], *tvp = NULL;
810	char *fname;
811	int error, dfd;
812
813	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
814	LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
815
816#ifdef DEBUG
817	if (ldebug(futimesat))
818		printf(ARGS(futimesat, "%s, *"), fname);
819#endif
820
821	if (args->utimes != NULL) {
822		if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
823			LFREEPATH(fname);
824			return (error);
825		}
826		tv[0].tv_sec = ltv[0].tv_sec;
827		tv[0].tv_usec = ltv[0].tv_usec;
828		tv[1].tv_sec = ltv[1].tv_sec;
829		tv[1].tv_usec = ltv[1].tv_usec;
830		tvp = tv;
831	}
832
833	error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
834	LFREEPATH(fname);
835	return (error);
836}
837#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
838
839int
840linux_common_wait(struct thread *td, int pid, int *status,
841    int options, struct rusage *ru)
842{
843	int error, tmpstat;
844
845	error = kern_wait(td, pid, &tmpstat, options, ru);
846	if (error)
847		return (error);
848
849	if (status) {
850		tmpstat &= 0xffff;
851		if (WIFSIGNALED(tmpstat))
852			tmpstat = (tmpstat & 0xffffff80) |
853			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
854		else if (WIFSTOPPED(tmpstat))
855			tmpstat = (tmpstat & 0xffff00ff) |
856			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
857		error = copyout(&tmpstat, status, sizeof(int));
858	}
859
860	return (error);
861}
862
863int
864linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
865{
866	int options;
867
868#ifdef DEBUG
869	if (ldebug(waitpid))
870		printf(ARGS(waitpid, "%d, %p, %d"),
871		    args->pid, (void *)args->status, args->options);
872#endif
873	/*
874	 * this is necessary because the test in kern_wait doesn't work
875	 * because we mess with the options here
876	 */
877	if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
878		return (EINVAL);
879
880	options = (args->options & (WNOHANG | WUNTRACED));
881	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
882	if (args->options & __WCLONE)
883		options |= WLINUXCLONE;
884
885	return (linux_common_wait(td, args->pid, args->status, options, NULL));
886}
887
888int
889linux_wait4(struct thread *td, struct linux_wait4_args *args)
890{
891	int error, options;
892	struct rusage ru, *rup;
893
894#ifdef DEBUG
895	if (ldebug(wait4))
896		printf(ARGS(wait4, "%d, %p, %d, %p"),
897		    args->pid, (void *)args->status, args->options,
898		    (void *)args->rusage);
899#endif
900
901	options = (args->options & (WNOHANG | WUNTRACED));
902	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
903	if (args->options & __WCLONE)
904		options |= WLINUXCLONE;
905
906	if (args->rusage != NULL)
907		rup = &ru;
908	else
909		rup = NULL;
910	error = linux_common_wait(td, args->pid, args->status, options, rup);
911	if (error != 0)
912		return (error);
913	if (args->rusage != NULL)
914		error = linux_copyout_rusage(&ru, args->rusage);
915	return (error);
916}
917
918int
919linux_waitid(struct thread *td, struct linux_waitid_args *args)
920{
921	int status, options, sig;
922	struct __wrusage wru;
923	siginfo_t siginfo;
924	l_siginfo_t lsi;
925	idtype_t idtype;
926	struct proc *p;
927	int error;
928
929	options = 0;
930	linux_to_bsd_waitopts(args->options, &options);
931
932	if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
933		return (EINVAL);
934	if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
935		return (EINVAL);
936
937	switch (args->idtype) {
938	case LINUX_P_ALL:
939		idtype = P_ALL;
940		break;
941	case LINUX_P_PID:
942		if (args->id <= 0)
943			return (EINVAL);
944		idtype = P_PID;
945		break;
946	case LINUX_P_PGID:
947		if (args->id <= 0)
948			return (EINVAL);
949		idtype = P_PGID;
950		break;
951	default:
952		return (EINVAL);
953	}
954
955	error = kern_wait6(td, idtype, args->id, &status, options,
956	    &wru, &siginfo);
957	if (error != 0)
958		return (error);
959	if (args->rusage != NULL) {
960		error = linux_copyout_rusage(&wru.wru_children,
961		    args->rusage);
962		if (error != 0)
963			return (error);
964	}
965	if (args->info != NULL) {
966		p = td->td_proc;
967		if (td->td_retval[0] == 0)
968			bzero(&lsi, sizeof(lsi));
969		else {
970			sig = BSD_TO_LINUX_SIGNAL(siginfo.si_signo);
971			siginfo_to_lsiginfo(&siginfo, &lsi, sig);
972		}
973		error = copyout(&lsi, args->info, sizeof(lsi));
974	}
975	td->td_retval[0] = 0;
976
977	return (error);
978}
979
980int
981linux_mknod(struct thread *td, struct linux_mknod_args *args)
982{
983	char *path;
984	int error;
985
986	LCONVPATHCREAT(td, args->path, &path);
987
988#ifdef DEBUG
989	if (ldebug(mknod))
990		printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
991#endif
992
993	switch (args->mode & S_IFMT) {
994	case S_IFIFO:
995	case S_IFSOCK:
996		error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
997		break;
998
999	case S_IFCHR:
1000	case S_IFBLK:
1001		error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
1002		    args->dev);
1003		break;
1004
1005	case S_IFDIR:
1006		error = EPERM;
1007		break;
1008
1009	case 0:
1010		args->mode |= S_IFREG;
1011		/* FALLTHROUGH */
1012	case S_IFREG:
1013		error = kern_open(td, path, UIO_SYSSPACE,
1014		    O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1015		if (error == 0)
1016			kern_close(td, td->td_retval[0]);
1017		break;
1018
1019	default:
1020		error = EINVAL;
1021		break;
1022	}
1023	LFREEPATH(path);
1024	return (error);
1025}
1026
1027int
1028linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1029{
1030	char *path;
1031	int error, dfd;
1032
1033	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1034	LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1035
1036#ifdef DEBUG
1037	if (ldebug(mknodat))
1038		printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1039#endif
1040
1041	switch (args->mode & S_IFMT) {
1042	case S_IFIFO:
1043	case S_IFSOCK:
1044		error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1045		break;
1046
1047	case S_IFCHR:
1048	case S_IFBLK:
1049		error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1050		    args->dev);
1051		break;
1052
1053	case S_IFDIR:
1054		error = EPERM;
1055		break;
1056
1057	case 0:
1058		args->mode |= S_IFREG;
1059		/* FALLTHROUGH */
1060	case S_IFREG:
1061		error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1062		    O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1063		if (error == 0)
1064			kern_close(td, td->td_retval[0]);
1065		break;
1066
1067	default:
1068		error = EINVAL;
1069		break;
1070	}
1071	LFREEPATH(path);
1072	return (error);
1073}
1074
1075/*
1076 * UGH! This is just about the dumbest idea I've ever heard!!
1077 */
1078int
1079linux_personality(struct thread *td, struct linux_personality_args *args)
1080{
1081#ifdef DEBUG
1082	if (ldebug(personality))
1083		printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1084#endif
1085	if (args->per != 0)
1086		return (EINVAL);
1087
1088	/* Yes Jim, it's still a Linux... */
1089	td->td_retval[0] = 0;
1090	return (0);
1091}
1092
1093struct l_itimerval {
1094	l_timeval it_interval;
1095	l_timeval it_value;
1096};
1097
1098#define	B2L_ITIMERVAL(bip, lip) 					\
1099	(bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;		\
1100	(bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;	\
1101	(bip)->it_value.tv_sec = (lip)->it_value.tv_sec;		\
1102	(bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1103
1104int
1105linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1106{
1107	int error;
1108	struct l_itimerval ls;
1109	struct itimerval aitv, oitv;
1110
1111#ifdef DEBUG
1112	if (ldebug(setitimer))
1113		printf(ARGS(setitimer, "%p, %p"),
1114		    (void *)uap->itv, (void *)uap->oitv);
1115#endif
1116
1117	if (uap->itv == NULL) {
1118		uap->itv = uap->oitv;
1119		return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1120	}
1121
1122	error = copyin(uap->itv, &ls, sizeof(ls));
1123	if (error != 0)
1124		return (error);
1125	B2L_ITIMERVAL(&aitv, &ls);
1126#ifdef DEBUG
1127	if (ldebug(setitimer)) {
1128		printf("setitimer: value: sec: %jd, usec: %ld\n",
1129		    (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1130		printf("setitimer: interval: sec: %jd, usec: %ld\n",
1131		    (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1132	}
1133#endif
1134	error = kern_setitimer(td, uap->which, &aitv, &oitv);
1135	if (error != 0 || uap->oitv == NULL)
1136		return (error);
1137	B2L_ITIMERVAL(&ls, &oitv);
1138
1139	return (copyout(&ls, uap->oitv, sizeof(ls)));
1140}
1141
1142int
1143linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1144{
1145	int error;
1146	struct l_itimerval ls;
1147	struct itimerval aitv;
1148
1149#ifdef DEBUG
1150	if (ldebug(getitimer))
1151		printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1152#endif
1153	error = kern_getitimer(td, uap->which, &aitv);
1154	if (error != 0)
1155		return (error);
1156	B2L_ITIMERVAL(&ls, &aitv);
1157	return (copyout(&ls, uap->itv, sizeof(ls)));
1158}
1159
1160int
1161linux_nice(struct thread *td, struct linux_nice_args *args)
1162{
1163	struct setpriority_args bsd_args;
1164
1165	bsd_args.which = PRIO_PROCESS;
1166	bsd_args.who = 0;		/* current process */
1167	bsd_args.prio = args->inc;
1168	return (sys_setpriority(td, &bsd_args));
1169}
1170
1171int
1172linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1173{
1174	struct ucred *newcred, *oldcred;
1175	l_gid_t *linux_gidset;
1176	gid_t *bsd_gidset;
1177	int ngrp, error;
1178	struct proc *p;
1179
1180	ngrp = args->gidsetsize;
1181	if (ngrp < 0 || ngrp >= ngroups_max + 1)
1182		return (EINVAL);
1183	linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
1184	error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1185	if (error)
1186		goto out;
1187	newcred = crget();
1188	p = td->td_proc;
1189	PROC_LOCK(p);
1190	oldcred = crcopysafe(p, newcred);
1191
1192	/*
1193	 * cr_groups[0] holds egid. Setting the whole set from
1194	 * the supplied set will cause egid to be changed too.
1195	 * Keep cr_groups[0] unchanged to prevent that.
1196	 */
1197
1198	if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1199		PROC_UNLOCK(p);
1200		crfree(newcred);
1201		goto out;
1202	}
1203
1204	if (ngrp > 0) {
1205		newcred->cr_ngroups = ngrp + 1;
1206
1207		bsd_gidset = newcred->cr_groups;
1208		ngrp--;
1209		while (ngrp >= 0) {
1210			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1211			ngrp--;
1212		}
1213	} else
1214		newcred->cr_ngroups = 1;
1215
1216	setsugid(p);
1217	p->p_ucred = newcred;
1218	PROC_UNLOCK(p);
1219	crfree(oldcred);
1220	error = 0;
1221out:
1222	free(linux_gidset, M_TEMP);
1223	return (error);
1224}
1225
1226int
1227linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1228{
1229	struct ucred *cred;
1230	l_gid_t *linux_gidset;
1231	gid_t *bsd_gidset;
1232	int bsd_gidsetsz, ngrp, error;
1233
1234	cred = td->td_ucred;
1235	bsd_gidset = cred->cr_groups;
1236	bsd_gidsetsz = cred->cr_ngroups - 1;
1237
1238	/*
1239	 * cr_groups[0] holds egid. Returning the whole set
1240	 * here will cause a duplicate. Exclude cr_groups[0]
1241	 * to prevent that.
1242	 */
1243
1244	if ((ngrp = args->gidsetsize) == 0) {
1245		td->td_retval[0] = bsd_gidsetsz;
1246		return (0);
1247	}
1248
1249	if (ngrp < bsd_gidsetsz)
1250		return (EINVAL);
1251
1252	ngrp = 0;
1253	linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1254	    M_TEMP, M_WAITOK);
1255	while (ngrp < bsd_gidsetsz) {
1256		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1257		ngrp++;
1258	}
1259
1260	error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1261	free(linux_gidset, M_TEMP);
1262	if (error)
1263		return (error);
1264
1265	td->td_retval[0] = ngrp;
1266	return (0);
1267}
1268
1269int
1270linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1271{
1272	struct rlimit bsd_rlim;
1273	struct l_rlimit rlim;
1274	u_int which;
1275	int error;
1276
1277#ifdef DEBUG
1278	if (ldebug(setrlimit))
1279		printf(ARGS(setrlimit, "%d, %p"),
1280		    args->resource, (void *)args->rlim);
1281#endif
1282
1283	if (args->resource >= LINUX_RLIM_NLIMITS)
1284		return (EINVAL);
1285
1286	which = linux_to_bsd_resource[args->resource];
1287	if (which == -1)
1288		return (EINVAL);
1289
1290	error = copyin(args->rlim, &rlim, sizeof(rlim));
1291	if (error)
1292		return (error);
1293
1294	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1295	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1296	return (kern_setrlimit(td, which, &bsd_rlim));
1297}
1298
1299int
1300linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1301{
1302	struct l_rlimit rlim;
1303	struct proc *p = td->td_proc;
1304	struct rlimit bsd_rlim;
1305	u_int which;
1306
1307#ifdef DEBUG
1308	if (ldebug(old_getrlimit))
1309		printf(ARGS(old_getrlimit, "%d, %p"),
1310		    args->resource, (void *)args->rlim);
1311#endif
1312
1313	if (args->resource >= LINUX_RLIM_NLIMITS)
1314		return (EINVAL);
1315
1316	which = linux_to_bsd_resource[args->resource];
1317	if (which == -1)
1318		return (EINVAL);
1319
1320	PROC_LOCK(p);
1321	lim_rlimit(p, which, &bsd_rlim);
1322	PROC_UNLOCK(p);
1323
1324#ifdef COMPAT_LINUX32
1325	rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1326	if (rlim.rlim_cur == UINT_MAX)
1327		rlim.rlim_cur = INT_MAX;
1328	rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1329	if (rlim.rlim_max == UINT_MAX)
1330		rlim.rlim_max = INT_MAX;
1331#else
1332	rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1333	if (rlim.rlim_cur == ULONG_MAX)
1334		rlim.rlim_cur = LONG_MAX;
1335	rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1336	if (rlim.rlim_max == ULONG_MAX)
1337		rlim.rlim_max = LONG_MAX;
1338#endif
1339	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1340}
1341
1342int
1343linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1344{
1345	struct l_rlimit rlim;
1346	struct proc *p = td->td_proc;
1347	struct rlimit bsd_rlim;
1348	u_int which;
1349
1350#ifdef DEBUG
1351	if (ldebug(getrlimit))
1352		printf(ARGS(getrlimit, "%d, %p"),
1353		    args->resource, (void *)args->rlim);
1354#endif
1355
1356	if (args->resource >= LINUX_RLIM_NLIMITS)
1357		return (EINVAL);
1358
1359	which = linux_to_bsd_resource[args->resource];
1360	if (which == -1)
1361		return (EINVAL);
1362
1363	PROC_LOCK(p);
1364	lim_rlimit(p, which, &bsd_rlim);
1365	PROC_UNLOCK(p);
1366
1367	rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1368	rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1369	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1370}
1371
1372int
1373linux_sched_setscheduler(struct thread *td,
1374    struct linux_sched_setscheduler_args *args)
1375{
1376	struct sched_param sched_param;
1377	struct thread *tdt;
1378	int error, policy;
1379
1380#ifdef DEBUG
1381	if (ldebug(sched_setscheduler))
1382		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1383		    args->pid, args->policy, (const void *)args->param);
1384#endif
1385
1386	switch (args->policy) {
1387	case LINUX_SCHED_OTHER:
1388		policy = SCHED_OTHER;
1389		break;
1390	case LINUX_SCHED_FIFO:
1391		policy = SCHED_FIFO;
1392		break;
1393	case LINUX_SCHED_RR:
1394		policy = SCHED_RR;
1395		break;
1396	default:
1397		return (EINVAL);
1398	}
1399
1400	error = copyin(args->param, &sched_param, sizeof(sched_param));
1401	if (error)
1402		return (error);
1403
1404	tdt = linux_tdfind(td, args->pid, -1);
1405	if (tdt == NULL)
1406		return (ESRCH);
1407
1408	error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1409	PROC_UNLOCK(tdt->td_proc);
1410	return (error);
1411}
1412
1413int
1414linux_sched_getscheduler(struct thread *td,
1415    struct linux_sched_getscheduler_args *args)
1416{
1417	struct thread *tdt;
1418	int error, policy;
1419
1420#ifdef DEBUG
1421	if (ldebug(sched_getscheduler))
1422		printf(ARGS(sched_getscheduler, "%d"), args->pid);
1423#endif
1424
1425	tdt = linux_tdfind(td, args->pid, -1);
1426	if (tdt == NULL)
1427		return (ESRCH);
1428
1429	error = kern_sched_getscheduler(td, tdt, &policy);
1430	PROC_UNLOCK(tdt->td_proc);
1431
1432	switch (policy) {
1433	case SCHED_OTHER:
1434		td->td_retval[0] = LINUX_SCHED_OTHER;
1435		break;
1436	case SCHED_FIFO:
1437		td->td_retval[0] = LINUX_SCHED_FIFO;
1438		break;
1439	case SCHED_RR:
1440		td->td_retval[0] = LINUX_SCHED_RR;
1441		break;
1442	}
1443	return (error);
1444}
1445
1446int
1447linux_sched_get_priority_max(struct thread *td,
1448    struct linux_sched_get_priority_max_args *args)
1449{
1450	struct sched_get_priority_max_args bsd;
1451
1452#ifdef DEBUG
1453	if (ldebug(sched_get_priority_max))
1454		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1455#endif
1456
1457	switch (args->policy) {
1458	case LINUX_SCHED_OTHER:
1459		bsd.policy = SCHED_OTHER;
1460		break;
1461	case LINUX_SCHED_FIFO:
1462		bsd.policy = SCHED_FIFO;
1463		break;
1464	case LINUX_SCHED_RR:
1465		bsd.policy = SCHED_RR;
1466		break;
1467	default:
1468		return (EINVAL);
1469	}
1470	return (sys_sched_get_priority_max(td, &bsd));
1471}
1472
1473int
1474linux_sched_get_priority_min(struct thread *td,
1475    struct linux_sched_get_priority_min_args *args)
1476{
1477	struct sched_get_priority_min_args bsd;
1478
1479#ifdef DEBUG
1480	if (ldebug(sched_get_priority_min))
1481		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1482#endif
1483
1484	switch (args->policy) {
1485	case LINUX_SCHED_OTHER:
1486		bsd.policy = SCHED_OTHER;
1487		break;
1488	case LINUX_SCHED_FIFO:
1489		bsd.policy = SCHED_FIFO;
1490		break;
1491	case LINUX_SCHED_RR:
1492		bsd.policy = SCHED_RR;
1493		break;
1494	default:
1495		return (EINVAL);
1496	}
1497	return (sys_sched_get_priority_min(td, &bsd));
1498}
1499
1500#define REBOOT_CAD_ON	0x89abcdef
1501#define REBOOT_CAD_OFF	0
1502#define REBOOT_HALT	0xcdef0123
1503#define REBOOT_RESTART	0x01234567
1504#define REBOOT_RESTART2	0xA1B2C3D4
1505#define REBOOT_POWEROFF	0x4321FEDC
1506#define REBOOT_MAGIC1	0xfee1dead
1507#define REBOOT_MAGIC2	0x28121969
1508#define REBOOT_MAGIC2A	0x05121996
1509#define REBOOT_MAGIC2B	0x16041998
1510
1511int
1512linux_reboot(struct thread *td, struct linux_reboot_args *args)
1513{
1514	struct reboot_args bsd_args;
1515
1516#ifdef DEBUG
1517	if (ldebug(reboot))
1518		printf(ARGS(reboot, "0x%x"), args->cmd);
1519#endif
1520
1521	if (args->magic1 != REBOOT_MAGIC1)
1522		return (EINVAL);
1523
1524	switch (args->magic2) {
1525	case REBOOT_MAGIC2:
1526	case REBOOT_MAGIC2A:
1527	case REBOOT_MAGIC2B:
1528		break;
1529	default:
1530		return (EINVAL);
1531	}
1532
1533	switch (args->cmd) {
1534	case REBOOT_CAD_ON:
1535	case REBOOT_CAD_OFF:
1536		return (priv_check(td, PRIV_REBOOT));
1537	case REBOOT_HALT:
1538		bsd_args.opt = RB_HALT;
1539		break;
1540	case REBOOT_RESTART:
1541	case REBOOT_RESTART2:
1542		bsd_args.opt = 0;
1543		break;
1544	case REBOOT_POWEROFF:
1545		bsd_args.opt = RB_POWEROFF;
1546		break;
1547	default:
1548		return (EINVAL);
1549	}
1550	return (sys_reboot(td, &bsd_args));
1551}
1552
1553
1554/*
1555 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1556 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1557 * are assumed to be preserved. The following lightweight syscalls fixes
1558 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1559 *
1560 * linux_getpid() - MP SAFE
1561 * linux_getgid() - MP SAFE
1562 * linux_getuid() - MP SAFE
1563 */
1564
1565int
1566linux_getpid(struct thread *td, struct linux_getpid_args *args)
1567{
1568
1569#ifdef DEBUG
1570	if (ldebug(getpid))
1571		printf(ARGS(getpid, ""));
1572#endif
1573	td->td_retval[0] = td->td_proc->p_pid;
1574
1575	return (0);
1576}
1577
1578int
1579linux_gettid(struct thread *td, struct linux_gettid_args *args)
1580{
1581	struct linux_emuldata *em;
1582
1583#ifdef DEBUG
1584	if (ldebug(gettid))
1585		printf(ARGS(gettid, ""));
1586#endif
1587
1588	em = em_find(td);
1589	KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1590
1591	td->td_retval[0] = em->em_tid;
1592
1593	return (0);
1594}
1595
1596
1597int
1598linux_getppid(struct thread *td, struct linux_getppid_args *args)
1599{
1600
1601#ifdef DEBUG
1602	if (ldebug(getppid))
1603		printf(ARGS(getppid, ""));
1604#endif
1605
1606	PROC_LOCK(td->td_proc);
1607	td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1608	PROC_UNLOCK(td->td_proc);
1609	return (0);
1610}
1611
1612int
1613linux_getgid(struct thread *td, struct linux_getgid_args *args)
1614{
1615
1616#ifdef DEBUG
1617	if (ldebug(getgid))
1618		printf(ARGS(getgid, ""));
1619#endif
1620
1621	td->td_retval[0] = td->td_ucred->cr_rgid;
1622	return (0);
1623}
1624
1625int
1626linux_getuid(struct thread *td, struct linux_getuid_args *args)
1627{
1628
1629#ifdef DEBUG
1630	if (ldebug(getuid))
1631		printf(ARGS(getuid, ""));
1632#endif
1633
1634	td->td_retval[0] = td->td_ucred->cr_ruid;
1635	return (0);
1636}
1637
1638
1639int
1640linux_getsid(struct thread *td, struct linux_getsid_args *args)
1641{
1642	struct getsid_args bsd;
1643
1644#ifdef DEBUG
1645	if (ldebug(getsid))
1646		printf(ARGS(getsid, "%i"), args->pid);
1647#endif
1648
1649	bsd.pid = args->pid;
1650	return (sys_getsid(td, &bsd));
1651}
1652
1653int
1654linux_nosys(struct thread *td, struct nosys_args *ignore)
1655{
1656
1657	return (ENOSYS);
1658}
1659
1660int
1661linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1662{
1663	struct getpriority_args bsd_args;
1664	int error;
1665
1666#ifdef DEBUG
1667	if (ldebug(getpriority))
1668		printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1669#endif
1670
1671	bsd_args.which = args->which;
1672	bsd_args.who = args->who;
1673	error = sys_getpriority(td, &bsd_args);
1674	td->td_retval[0] = 20 - td->td_retval[0];
1675	return (error);
1676}
1677
1678int
1679linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1680{
1681	int name[2];
1682
1683#ifdef DEBUG
1684	if (ldebug(sethostname))
1685		printf(ARGS(sethostname, "*, %i"), args->len);
1686#endif
1687
1688	name[0] = CTL_KERN;
1689	name[1] = KERN_HOSTNAME;
1690	return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1691	    args->len, 0, 0));
1692}
1693
1694int
1695linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1696{
1697	int name[2];
1698
1699#ifdef DEBUG
1700	if (ldebug(setdomainname))
1701		printf(ARGS(setdomainname, "*, %i"), args->len);
1702#endif
1703
1704	name[0] = CTL_KERN;
1705	name[1] = KERN_NISDOMAINNAME;
1706	return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1707	    args->len, 0, 0));
1708}
1709
1710int
1711linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1712{
1713
1714#ifdef DEBUG
1715	if (ldebug(exit_group))
1716		printf(ARGS(exit_group, "%i"), args->error_code);
1717#endif
1718
1719	LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1720	    args->error_code);
1721
1722	/*
1723	 * XXX: we should send a signal to the parent if
1724	 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1725	 * as it doesnt occur often.
1726	 */
1727	exit1(td, W_EXITCODE(args->error_code, 0));
1728		/* NOTREACHED */
1729}
1730
1731#define _LINUX_CAPABILITY_VERSION  0x19980330
1732
1733struct l_user_cap_header {
1734	l_int	version;
1735	l_int	pid;
1736};
1737
1738struct l_user_cap_data {
1739	l_int	effective;
1740	l_int	permitted;
1741	l_int	inheritable;
1742};
1743
1744int
1745linux_capget(struct thread *td, struct linux_capget_args *args)
1746{
1747	struct l_user_cap_header luch;
1748	struct l_user_cap_data lucd;
1749	int error;
1750
1751	if (args->hdrp == NULL)
1752		return (EFAULT);
1753
1754	error = copyin(args->hdrp, &luch, sizeof(luch));
1755	if (error != 0)
1756		return (error);
1757
1758	if (luch.version != _LINUX_CAPABILITY_VERSION) {
1759		luch.version = _LINUX_CAPABILITY_VERSION;
1760		error = copyout(&luch, args->hdrp, sizeof(luch));
1761		if (error)
1762			return (error);
1763		return (EINVAL);
1764	}
1765
1766	if (luch.pid)
1767		return (EPERM);
1768
1769	if (args->datap) {
1770		/*
1771		 * The current implementation doesn't support setting
1772		 * a capability (it's essentially a stub) so indicate
1773		 * that no capabilities are currently set or available
1774		 * to request.
1775		 */
1776		bzero (&lucd, sizeof(lucd));
1777		error = copyout(&lucd, args->datap, sizeof(lucd));
1778	}
1779
1780	return (error);
1781}
1782
1783int
1784linux_capset(struct thread *td, struct linux_capset_args *args)
1785{
1786	struct l_user_cap_header luch;
1787	struct l_user_cap_data lucd;
1788	int error;
1789
1790	if (args->hdrp == NULL || args->datap == NULL)
1791		return (EFAULT);
1792
1793	error = copyin(args->hdrp, &luch, sizeof(luch));
1794	if (error != 0)
1795		return (error);
1796
1797	if (luch.version != _LINUX_CAPABILITY_VERSION) {
1798		luch.version = _LINUX_CAPABILITY_VERSION;
1799		error = copyout(&luch, args->hdrp, sizeof(luch));
1800		if (error)
1801			return (error);
1802		return (EINVAL);
1803	}
1804
1805	if (luch.pid)
1806		return (EPERM);
1807
1808	error = copyin(args->datap, &lucd, sizeof(lucd));
1809	if (error != 0)
1810		return (error);
1811
1812	/* We currently don't support setting any capabilities. */
1813	if (lucd.effective || lucd.permitted || lucd.inheritable) {
1814		linux_msg(td,
1815			  "capset effective=0x%x, permitted=0x%x, "
1816			  "inheritable=0x%x is not implemented",
1817			  (int)lucd.effective, (int)lucd.permitted,
1818			  (int)lucd.inheritable);
1819		return (EPERM);
1820	}
1821
1822	return (0);
1823}
1824
1825int
1826linux_prctl(struct thread *td, struct linux_prctl_args *args)
1827{
1828	int error = 0, max_size;
1829	struct proc *p = td->td_proc;
1830	char comm[LINUX_MAX_COMM_LEN];
1831	struct linux_emuldata *em;
1832	int pdeath_signal;
1833
1834#ifdef DEBUG
1835	if (ldebug(prctl))
1836		printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1837		    args->arg2, args->arg3, args->arg4, args->arg5);
1838#endif
1839
1840	switch (args->option) {
1841	case LINUX_PR_SET_PDEATHSIG:
1842		if (!LINUX_SIG_VALID(args->arg2))
1843			return (EINVAL);
1844		em = em_find(td);
1845		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1846		em->pdeath_signal = args->arg2;
1847		break;
1848	case LINUX_PR_GET_PDEATHSIG:
1849		em = em_find(td);
1850		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1851		pdeath_signal = em->pdeath_signal;
1852		error = copyout(&pdeath_signal,
1853		    (void *)(register_t)args->arg2,
1854		    sizeof(pdeath_signal));
1855		break;
1856	case LINUX_PR_GET_KEEPCAPS:
1857		/*
1858		 * Indicate that we always clear the effective and
1859		 * permitted capability sets when the user id becomes
1860		 * non-zero (actually the capability sets are simply
1861		 * always zero in the current implementation).
1862		 */
1863		td->td_retval[0] = 0;
1864		break;
1865	case LINUX_PR_SET_KEEPCAPS:
1866		/*
1867		 * Ignore requests to keep the effective and permitted
1868		 * capability sets when the user id becomes non-zero.
1869		 */
1870		break;
1871	case LINUX_PR_SET_NAME:
1872		/*
1873		 * To be on the safe side we need to make sure to not
1874		 * overflow the size a linux program expects. We already
1875		 * do this here in the copyin, so that we don't need to
1876		 * check on copyout.
1877		 */
1878		max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1879		error = copyinstr((void *)(register_t)args->arg2, comm,
1880		    max_size, NULL);
1881
1882		/* Linux silently truncates the name if it is too long. */
1883		if (error == ENAMETOOLONG) {
1884			/*
1885			 * XXX: copyinstr() isn't documented to populate the
1886			 * array completely, so do a copyin() to be on the
1887			 * safe side. This should be changed in case
1888			 * copyinstr() is changed to guarantee this.
1889			 */
1890			error = copyin((void *)(register_t)args->arg2, comm,
1891			    max_size - 1);
1892			comm[max_size - 1] = '\0';
1893		}
1894		if (error)
1895			return (error);
1896
1897		PROC_LOCK(p);
1898		strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1899		PROC_UNLOCK(p);
1900		break;
1901	case LINUX_PR_GET_NAME:
1902		PROC_LOCK(p);
1903		strlcpy(comm, p->p_comm, sizeof(comm));
1904		PROC_UNLOCK(p);
1905		error = copyout(comm, (void *)(register_t)args->arg2,
1906		    strlen(comm) + 1);
1907		break;
1908	default:
1909		error = EINVAL;
1910		break;
1911	}
1912
1913	return (error);
1914}
1915
1916int
1917linux_sched_setparam(struct thread *td,
1918    struct linux_sched_setparam_args *uap)
1919{
1920	struct sched_param sched_param;
1921	struct thread *tdt;
1922	int error;
1923
1924#ifdef DEBUG
1925	if (ldebug(sched_setparam))
1926		printf(ARGS(sched_setparam, "%d, *"), uap->pid);
1927#endif
1928
1929	error = copyin(uap->param, &sched_param, sizeof(sched_param));
1930	if (error)
1931		return (error);
1932
1933	tdt = linux_tdfind(td, uap->pid, -1);
1934	if (tdt == NULL)
1935		return (ESRCH);
1936
1937	error = kern_sched_setparam(td, tdt, &sched_param);
1938	PROC_UNLOCK(tdt->td_proc);
1939	return (error);
1940}
1941
1942int
1943linux_sched_getparam(struct thread *td,
1944    struct linux_sched_getparam_args *uap)
1945{
1946	struct sched_param sched_param;
1947	struct thread *tdt;
1948	int error;
1949
1950#ifdef DEBUG
1951	if (ldebug(sched_getparam))
1952		printf(ARGS(sched_getparam, "%d, *"), uap->pid);
1953#endif
1954
1955	tdt = linux_tdfind(td, uap->pid, -1);
1956	if (tdt == NULL)
1957		return (ESRCH);
1958
1959	error = kern_sched_getparam(td, tdt, &sched_param);
1960	PROC_UNLOCK(tdt->td_proc);
1961	if (error == 0)
1962		error = copyout(&sched_param, uap->param,
1963		    sizeof(sched_param));
1964	return (error);
1965}
1966
1967/*
1968 * Get affinity of a process.
1969 */
1970int
1971linux_sched_getaffinity(struct thread *td,
1972    struct linux_sched_getaffinity_args *args)
1973{
1974	int error;
1975	struct thread *tdt;
1976	struct cpuset_getaffinity_args cga;
1977
1978#ifdef DEBUG
1979	if (ldebug(sched_getaffinity))
1980		printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1981		    args->len);
1982#endif
1983	if (args->len < sizeof(cpuset_t))
1984		return (EINVAL);
1985
1986	tdt = linux_tdfind(td, args->pid, -1);
1987	if (tdt == NULL)
1988		return (ESRCH);
1989
1990	PROC_UNLOCK(tdt->td_proc);
1991	cga.level = CPU_LEVEL_WHICH;
1992	cga.which = CPU_WHICH_TID;
1993	cga.id = tdt->td_tid;
1994	cga.cpusetsize = sizeof(cpuset_t);
1995	cga.mask = (cpuset_t *) args->user_mask_ptr;
1996
1997	if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
1998		td->td_retval[0] = sizeof(cpuset_t);
1999
2000	return (error);
2001}
2002
2003/*
2004 *  Set affinity of a process.
2005 */
2006int
2007linux_sched_setaffinity(struct thread *td,
2008    struct linux_sched_setaffinity_args *args)
2009{
2010	struct cpuset_setaffinity_args csa;
2011	struct thread *tdt;
2012
2013#ifdef DEBUG
2014	if (ldebug(sched_setaffinity))
2015		printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2016		    args->len);
2017#endif
2018	if (args->len < sizeof(cpuset_t))
2019		return (EINVAL);
2020
2021	tdt = linux_tdfind(td, args->pid, -1);
2022	if (tdt == NULL)
2023		return (ESRCH);
2024
2025	PROC_UNLOCK(tdt->td_proc);
2026	csa.level = CPU_LEVEL_WHICH;
2027	csa.which = CPU_WHICH_TID;
2028	csa.id = tdt->td_tid;
2029	csa.cpusetsize = sizeof(cpuset_t);
2030	csa.mask = (cpuset_t *) args->user_mask_ptr;
2031
2032	return (sys_cpuset_setaffinity(td, &csa));
2033}
2034
2035int
2036linux_sched_rr_get_interval(struct thread *td,
2037    struct linux_sched_rr_get_interval_args *uap)
2038{
2039	struct timespec ts;
2040	struct l_timespec lts;
2041	struct thread *tdt;
2042	int error;
2043
2044	tdt = linux_tdfind(td, uap->pid, -1);
2045	if (tdt == NULL)
2046		return (ESRCH);
2047
2048	error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2049	PROC_UNLOCK(tdt->td_proc);
2050	if (error != 0)
2051		return (error);
2052	lts.tv_sec = ts.tv_sec;
2053	lts.tv_nsec = ts.tv_nsec;
2054	return (copyout(&lts, uap->interval, sizeof(lts)));
2055}
2056
2057/*
2058 * In case when the Linux thread is the initial thread in
2059 * the thread group thread id is equal to the process id.
2060 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2061 */
2062struct thread *
2063linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2064{
2065	struct linux_emuldata *em;
2066	struct thread *tdt;
2067	struct proc *p;
2068
2069	tdt = NULL;
2070	if (tid == 0 || tid == td->td_tid) {
2071		tdt = td;
2072		PROC_LOCK(tdt->td_proc);
2073	} else if (tid > PID_MAX)
2074		tdt = tdfind(tid, pid);
2075	else {
2076		/*
2077		 * Initial thread where the tid equal to the pid.
2078		 */
2079		p = pfind(tid);
2080		if (p != NULL) {
2081			if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2082				/*
2083				 * p is not a Linuxulator process.
2084				 */
2085				PROC_UNLOCK(p);
2086				return (NULL);
2087			}
2088			FOREACH_THREAD_IN_PROC(p, tdt) {
2089				em = em_find(tdt);
2090				if (tid == em->em_tid)
2091					return (tdt);
2092			}
2093			PROC_UNLOCK(p);
2094		}
2095		return (NULL);
2096	}
2097
2098	return (tdt);
2099}
2100
2101void
2102linux_to_bsd_waitopts(int options, int *bsdopts)
2103{
2104
2105	if (options & LINUX_WNOHANG)
2106		*bsdopts |= WNOHANG;
2107	if (options & LINUX_WUNTRACED)
2108		*bsdopts |= WUNTRACED;
2109	if (options & LINUX_WEXITED)
2110		*bsdopts |= WEXITED;
2111	if (options & LINUX_WCONTINUED)
2112		*bsdopts |= WCONTINUED;
2113	if (options & LINUX_WNOWAIT)
2114		*bsdopts |= WNOWAIT;
2115
2116	if (options & __WCLONE)
2117		*bsdopts |= WLINUXCLONE;
2118}
2119