xref: /freebsd/sys/compat/linux/linux_misc.c (revision b97916da4491997aa0f45a64c74c1d92a04277f0)
1 /*-
2  * Copyright (c) 1994-1995 S�ren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_mac.h"
33 
34 #include <sys/param.h>
35 #include <sys/blist.h>
36 #include <sys/fcntl.h>
37 #include <sys/imgact_aout.h>
38 #include <sys/jail.h>
39 #include <sys/kernel.h>
40 #include <sys/limits.h>
41 #include <sys/lock.h>
42 #include <sys/mac.h>
43 #include <sys/malloc.h>
44 #include <sys/mman.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/namei.h>
48 #include <sys/proc.h>
49 #include <sys/reboot.h>
50 #include <sys/resourcevar.h>
51 #include <sys/signalvar.h>
52 #include <sys/stat.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysctl.h>
55 #include <sys/sysproto.h>
56 #include <sys/systm.h>
57 #include <sys/time.h>
58 #include <sys/vmmeter.h>
59 #include <sys/vnode.h>
60 #include <sys/wait.h>
61 
62 #include <vm/vm.h>
63 #include <vm/pmap.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_object.h>
68 #include <vm/swap_pager.h>
69 
70 #include <posix4/sched.h>
71 
72 #include <machine/../linux/linux.h>
73 #include <machine/../linux/linux_proto.h>
74 
75 #include <compat/linux/linux_mib.h>
76 #include <compat/linux/linux_util.h>
77 
78 #ifdef __alpha__
79 #define BSD_TO_LINUX_SIGNAL(sig)       (sig)
80 #else
81 #define BSD_TO_LINUX_SIGNAL(sig)	\
82 	(((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
83 #endif
84 
85 #ifndef __alpha__
86 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
87 	RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
88 	RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
89 	RLIMIT_MEMLOCK, -1
90 };
91 #endif /*!__alpha__*/
92 
93 struct l_sysinfo {
94 	l_long		uptime;		/* Seconds since boot */
95 	l_ulong		loads[3];	/* 1, 5, and 15 minute load averages */
96 	l_ulong		totalram;	/* Total usable main memory size */
97 	l_ulong		freeram;	/* Available memory size */
98 	l_ulong		sharedram;	/* Amount of shared memory */
99 	l_ulong		bufferram;	/* Memory used by buffers */
100 	l_ulong		totalswap;	/* Total swap space size */
101 	l_ulong		freeswap;	/* swap space still available */
102 	l_ushort	procs;		/* Number of current processes */
103 	char		_f[22];		/* Pads structure to 64 bytes */
104 };
105 #ifndef __alpha__
106 int
107 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
108 {
109 	struct l_sysinfo sysinfo;
110 	vm_object_t object;
111 	int i, j;
112 	struct timespec ts;
113 
114 	/* Uptime is copied out of print_uptime() in kern_shutdown.c */
115 	getnanouptime(&ts);
116 	i = 0;
117 	if (ts.tv_sec >= 86400) {
118 		ts.tv_sec %= 86400;
119 		i = 1;
120 	}
121 	if (i || ts.tv_sec >= 3600) {
122 		ts.tv_sec %= 3600;
123 		i = 1;
124 	}
125 	if (i || ts.tv_sec >= 60) {
126 		ts.tv_sec %= 60;
127 		i = 1;
128 	}
129 	sysinfo.uptime=ts.tv_sec;
130 
131 	/* Use the information from the mib to get our load averages */
132 	for (i = 0; i < 3; i++)
133 		sysinfo.loads[i] = averunnable.ldavg[i];
134 
135 	sysinfo.totalram = physmem * PAGE_SIZE;
136 	sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
137 
138 	sysinfo.sharedram = 0;
139 	for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
140 	     object = TAILQ_NEXT(object, object_list))
141 		if (object->shadow_count > 1)
142 			sysinfo.sharedram += object->resident_page_count;
143 
144 	sysinfo.sharedram *= PAGE_SIZE;
145 	sysinfo.bufferram = 0;
146 
147 	swap_pager_status(&i, &j);
148 	sysinfo.totalswap= i * PAGE_SIZE;
149 	sysinfo.freeswap = (i - j) * PAGE_SIZE;
150 
151 	sysinfo.procs = 20; /* Hack */
152 
153 	return copyout(&sysinfo, args->info, sizeof(sysinfo));
154 }
155 #endif /*!__alpha__*/
156 
157 #ifndef __alpha__
158 int
159 linux_alarm(struct thread *td, struct linux_alarm_args *args)
160 {
161 	struct itimerval it, old_it;
162 	struct timeval tv;
163 	struct proc *p;
164 
165 #ifdef DEBUG
166 	if (ldebug(alarm))
167 		printf(ARGS(alarm, "%u"), args->secs);
168 #endif
169 
170 	if (args->secs > 100000000)
171 		return EINVAL;
172 
173 	it.it_value.tv_sec = (long)args->secs;
174 	it.it_value.tv_usec = 0;
175 	it.it_interval.tv_sec = 0;
176 	it.it_interval.tv_usec = 0;
177 	p = td->td_proc;
178 	PROC_LOCK(p);
179 	old_it = p->p_realtimer;
180 	getmicrouptime(&tv);
181 	if (timevalisset(&old_it.it_value))
182 		callout_stop(&p->p_itcallout);
183 	if (it.it_value.tv_sec != 0) {
184 		callout_reset(&p->p_itcallout, tvtohz(&it.it_value),
185 		    realitexpire, p);
186 		timevaladd(&it.it_value, &tv);
187 	}
188 	p->p_realtimer = it;
189 	PROC_UNLOCK(p);
190 	if (timevalcmp(&old_it.it_value, &tv, >)) {
191 		timevalsub(&old_it.it_value, &tv);
192 		if (old_it.it_value.tv_usec != 0)
193 			old_it.it_value.tv_sec++;
194 		td->td_retval[0] = old_it.it_value.tv_sec;
195 	}
196 	return 0;
197 }
198 #endif /*!__alpha__*/
199 
200 int
201 linux_brk(struct thread *td, struct linux_brk_args *args)
202 {
203 	struct vmspace *vm = td->td_proc->p_vmspace;
204 	vm_offset_t new, old;
205 	struct obreak_args /* {
206 		char * nsize;
207 	} */ tmp;
208 
209 #ifdef DEBUG
210 	if (ldebug(brk))
211 		printf(ARGS(brk, "%p"), (void *)args->dsend);
212 #endif
213 	old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
214 	new = (vm_offset_t)args->dsend;
215 	tmp.nsize = (char *) new;
216 	if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
217 		td->td_retval[0] = (long)new;
218 	else
219 		td->td_retval[0] = (long)old;
220 
221 	return 0;
222 }
223 
224 int
225 linux_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 	vm_offset_t buffer;
234 	unsigned long bss_size;
235 	char *library;
236 	int error;
237 	int locked;
238 
239 	LCONVPATHEXIST(td, args->library, &library);
240 
241 #ifdef DEBUG
242 	if (ldebug(uselib))
243 		printf(ARGS(uselib, "%s"), library);
244 #endif
245 
246 	a_out = NULL;
247 	locked = 0;
248 	vp = NULL;
249 
250 	/*
251 	 * XXX: This code should make use of vn_open(), rather than doing
252 	 * all this stuff itself.
253 	 */
254 	NDINIT(&ni, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, library, td);
255 	error = namei(&ni);
256 	LFREEPATH(library);
257 	if (error)
258 		goto cleanup;
259 
260 	vp = ni.ni_vp;
261 	/*
262 	 * XXX - This looks like a bogus check. A LOCKLEAF namei should not
263 	 * succeed without returning a vnode.
264 	 */
265 	if (vp == NULL) {
266 		error = ENOEXEC;	/* ?? */
267 		goto cleanup;
268 	}
269 	NDFREE(&ni, NDF_ONLY_PNBUF);
270 
271 	/*
272 	 * From here on down, we have a locked vnode that must be unlocked.
273 	 */
274 	locked++;
275 
276 	/* Writable? */
277 	if (vp->v_writecount) {
278 		error = ETXTBSY;
279 		goto cleanup;
280 	}
281 
282 	/* Executable? */
283 	error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
284 	if (error)
285 		goto cleanup;
286 
287 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
288 	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
289 		error = ENOEXEC;
290 		goto cleanup;
291 	}
292 
293 	/* Sensible size? */
294 	if (attr.va_size == 0) {
295 		error = ENOEXEC;
296 		goto cleanup;
297 	}
298 
299 	/* Can we access it? */
300 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
301 	if (error)
302 		goto cleanup;
303 
304 	/*
305 	 * XXX: This should use vn_open() so that it is properly authorized,
306 	 * and to reduce code redundancy all over the place here.
307 	 */
308 #ifdef MAC
309 	error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
310 	if (error)
311 		goto cleanup;
312 #endif
313 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td);
314 	if (error)
315 		goto cleanup;
316 
317 	/* Pull in executable header into kernel_map */
318 	error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
319 	    VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
320 	/*
321 	 * Lock no longer needed
322 	 */
323 	locked = 0;
324 	VOP_UNLOCK(vp, 0, td);
325 
326 	if (error)
327 		goto cleanup;
328 
329 	/* Is it a Linux binary ? */
330 	if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
331 		error = ENOEXEC;
332 		goto cleanup;
333 	}
334 
335 	/*
336 	 * While we are here, we should REALLY do some more checks
337 	 */
338 
339 	/* Set file/virtual offset based on a.out variant. */
340 	switch ((int)(a_out->a_magic & 0xffff)) {
341 	case 0413:	/* ZMAGIC */
342 		file_offset = 1024;
343 		break;
344 	case 0314:	/* QMAGIC */
345 		file_offset = 0;
346 		break;
347 	default:
348 		error = ENOEXEC;
349 		goto cleanup;
350 	}
351 
352 	bss_size = round_page(a_out->a_bss);
353 
354 	/* Check various fields in header for validity/bounds. */
355 	if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
356 		error = ENOEXEC;
357 		goto cleanup;
358 	}
359 
360 	/* text + data can't exceed file size */
361 	if (a_out->a_data + a_out->a_text > attr.va_size) {
362 		error = EFAULT;
363 		goto cleanup;
364 	}
365 
366 	/* To protect td->td_proc->p_rlimit in the if condition. */
367 	mtx_assert(&Giant, MA_OWNED);
368 
369 	/*
370 	 * text/data/bss must not exceed limits
371 	 * XXX - this is not complete. it should check current usage PLUS
372 	 * the resources needed by this library.
373 	 */
374 	if (a_out->a_text > maxtsiz ||
375 	    a_out->a_data + bss_size >
376 	    td->td_proc->p_rlimit[RLIMIT_DATA].rlim_cur) {
377 		error = ENOMEM;
378 		goto cleanup;
379 	}
380 
381 	mp_fixme("Unlocked vflags access.");
382 	/* prevent more writers */
383 	vp->v_vflag |= VV_TEXT;
384 
385 	/*
386 	 * Check if file_offset page aligned. Currently we cannot handle
387 	 * misalinged file offsets, and so we read in the entire image
388 	 * (what a waste).
389 	 */
390 	if (file_offset & PAGE_MASK) {
391 #ifdef DEBUG
392 		printf("uselib: Non page aligned binary %lu\n", file_offset);
393 #endif
394 		/* Map text+data read/write/execute */
395 
396 		/* a_entry is the load address and is page aligned */
397 		vmaddr = trunc_page(a_out->a_entry);
398 
399 		/* get anon user mapping, read+write+execute */
400 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
401 		    &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
402 		    VM_PROT_ALL, 0);
403 		if (error)
404 			goto cleanup;
405 
406 		/* map file into kernel_map */
407 		error = vm_mmap(kernel_map, &buffer,
408 		    round_page(a_out->a_text + a_out->a_data + file_offset),
409 		    VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
410 		    trunc_page(file_offset));
411 		if (error)
412 			goto cleanup;
413 
414 		/* copy from kernel VM space to user space */
415 		error = copyout((void *)(buffer + file_offset),
416 		    (void *)vmaddr, a_out->a_text + a_out->a_data);
417 
418 		/* release temporary kernel space */
419 		vm_map_remove(kernel_map, buffer, buffer +
420 		    round_page(a_out->a_text + a_out->a_data + file_offset));
421 
422 		if (error)
423 			goto cleanup;
424 	} else {
425 #ifdef DEBUG
426 		printf("uselib: Page aligned binary %lu\n", file_offset);
427 #endif
428 		/*
429 		 * for QMAGIC, a_entry is 20 bytes beyond the load address
430 		 * to skip the executable header
431 		 */
432 		vmaddr = trunc_page(a_out->a_entry);
433 
434 		/*
435 		 * Map it all into the process's space as a single
436 		 * copy-on-write "data" segment.
437 		 */
438 		error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
439 		    a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
440 		    MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
441 		if (error)
442 			goto cleanup;
443 	}
444 #ifdef DEBUG
445 	printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
446 	    ((long*)vmaddr)[1]);
447 #endif
448 	if (bss_size != 0) {
449 		/* Calculate BSS start address */
450 		vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
451 		    a_out->a_data;
452 
453 		/* allocate some 'anon' space */
454 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
455 		    &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
456 		if (error)
457 			goto cleanup;
458 	}
459 
460 cleanup:
461 	/* Unlock vnode if needed */
462 	if (locked)
463 		VOP_UNLOCK(vp, 0, td);
464 
465 	/* Release the kernel mapping. */
466 	if (a_out)
467 		vm_map_remove(kernel_map, (vm_offset_t)a_out,
468 		    (vm_offset_t)a_out + PAGE_SIZE);
469 
470 	return error;
471 }
472 
473 int
474 linux_select(struct thread *td, struct linux_select_args *args)
475 {
476 	struct timeval tv0, tv1, utv, *tvp;
477 	int error;
478 
479 #ifdef DEBUG
480 	if (ldebug(select))
481 		printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
482 		    (void *)args->readfds, (void *)args->writefds,
483 		    (void *)args->exceptfds, (void *)args->timeout);
484 #endif
485 
486 	/*
487 	 * Store current time for computation of the amount of
488 	 * time left.
489 	 */
490 	if (args->timeout) {
491 		if ((error = copyin(args->timeout, &utv, sizeof(utv))))
492 			goto select_out;
493 #ifdef DEBUG
494 		if (ldebug(select))
495 			printf(LMSG("incoming timeout (%ld/%ld)"),
496 			    utv.tv_sec, utv.tv_usec);
497 #endif
498 
499 		if (itimerfix(&utv)) {
500 			/*
501 			 * The timeval was invalid.  Convert it to something
502 			 * valid that will act as it does under Linux.
503 			 */
504 			utv.tv_sec += utv.tv_usec / 1000000;
505 			utv.tv_usec %= 1000000;
506 			if (utv.tv_usec < 0) {
507 				utv.tv_sec -= 1;
508 				utv.tv_usec += 1000000;
509 			}
510 			if (utv.tv_sec < 0)
511 				timevalclear(&utv);
512 		}
513 		microtime(&tv0);
514 		tvp = &utv;
515 	} else
516 		tvp = NULL;
517 
518 	error = kern_select(td, args->nfds, args->readfds, args->writefds,
519 	    args->exceptfds, tvp);
520 
521 #ifdef DEBUG
522 	if (ldebug(select))
523 		printf(LMSG("real select returns %d"), error);
524 #endif
525 	if (error) {
526 		/*
527 		 * See fs/select.c in the Linux kernel.  Without this,
528 		 * Maelstrom doesn't work.
529 		 */
530 		if (error == ERESTART)
531 			error = EINTR;
532 		goto select_out;
533 	}
534 
535 	if (args->timeout) {
536 		if (td->td_retval[0]) {
537 			/*
538 			 * Compute how much time was left of the timeout,
539 			 * by subtracting the current time and the time
540 			 * before we started the call, and subtracting
541 			 * that result from the user-supplied value.
542 			 */
543 			microtime(&tv1);
544 			timevalsub(&tv1, &tv0);
545 			timevalsub(&utv, &tv1);
546 			if (utv.tv_sec < 0)
547 				timevalclear(&utv);
548 		} else
549 			timevalclear(&utv);
550 #ifdef DEBUG
551 		if (ldebug(select))
552 			printf(LMSG("outgoing timeout (%ld/%ld)"),
553 			    utv.tv_sec, utv.tv_usec);
554 #endif
555 		if ((error = copyout(&utv, args->timeout, sizeof(utv))))
556 			goto select_out;
557 	}
558 
559 select_out:
560 #ifdef DEBUG
561 	if (ldebug(select))
562 		printf(LMSG("select_out -> %d"), error);
563 #endif
564 	return error;
565 }
566 
567 int
568 linux_mremap(struct thread *td, struct linux_mremap_args *args)
569 {
570 	struct munmap_args /* {
571 		void *addr;
572 		size_t len;
573 	} */ bsd_args;
574 	int error = 0;
575 
576 #ifdef DEBUG
577 	if (ldebug(mremap))
578 		printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
579 		    (void *)args->addr,
580 		    (unsigned long)args->old_len,
581 		    (unsigned long)args->new_len,
582 		    (unsigned long)args->flags);
583 #endif
584 	args->new_len = round_page(args->new_len);
585 	args->old_len = round_page(args->old_len);
586 
587 	if (args->new_len > args->old_len) {
588 		td->td_retval[0] = 0;
589 		return ENOMEM;
590 	}
591 
592 	if (args->new_len < args->old_len) {
593 		bsd_args.addr = (caddr_t)(args->addr + args->new_len);
594 		bsd_args.len = args->old_len - args->new_len;
595 		error = munmap(td, &bsd_args);
596 	}
597 
598 	td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
599 	return error;
600 }
601 
602 #define LINUX_MS_ASYNC       0x0001
603 #define LINUX_MS_INVALIDATE  0x0002
604 #define LINUX_MS_SYNC        0x0004
605 
606 int
607 linux_msync(struct thread *td, struct linux_msync_args *args)
608 {
609 	struct msync_args bsd_args;
610 
611 	bsd_args.addr = (caddr_t)args->addr;
612 	bsd_args.len = args->len;
613 	bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
614 
615 	return msync(td, &bsd_args);
616 }
617 
618 #ifndef __alpha__
619 int
620 linux_time(struct thread *td, struct linux_time_args *args)
621 {
622 	struct timeval tv;
623 	l_time_t tm;
624 	int error;
625 
626 #ifdef DEBUG
627 	if (ldebug(time))
628 		printf(ARGS(time, "*"));
629 #endif
630 
631 	microtime(&tv);
632 	tm = tv.tv_sec;
633 	if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
634 		return error;
635 	td->td_retval[0] = tm;
636 	return 0;
637 }
638 #endif	/*!__alpha__*/
639 
640 struct l_times_argv {
641 	l_long		tms_utime;
642 	l_long		tms_stime;
643 	l_long		tms_cutime;
644 	l_long		tms_cstime;
645 };
646 
647 #ifdef __alpha__
648 #define CLK_TCK 1024	/* Linux uses 1024 on alpha */
649 #else
650 #define CLK_TCK 100	/* Linux uses 100 */
651 #endif
652 
653 #define CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
654 
655 int
656 linux_times(struct thread *td, struct linux_times_args *args)
657 {
658 	struct timeval tv;
659 	struct l_times_argv tms;
660 	struct rusage ru;
661 	int error;
662 
663 #ifdef DEBUG
664 	if (ldebug(times))
665 		printf(ARGS(times, "*"));
666 #endif
667 
668 	mtx_lock_spin(&sched_lock);
669 	calcru(td->td_proc, &ru.ru_utime, &ru.ru_stime, NULL);
670 	mtx_unlock_spin(&sched_lock);
671 
672 	tms.tms_utime = CONVTCK(ru.ru_utime);
673 	tms.tms_stime = CONVTCK(ru.ru_stime);
674 
675 	tms.tms_cutime = CONVTCK(td->td_proc->p_stats->p_cru.ru_utime);
676 	tms.tms_cstime = CONVTCK(td->td_proc->p_stats->p_cru.ru_stime);
677 
678 	if ((error = copyout(&tms, args->buf, sizeof(tms))))
679 		return error;
680 
681 	microuptime(&tv);
682 	td->td_retval[0] = (int)CONVTCK(tv);
683 	return 0;
684 }
685 
686 int
687 linux_newuname(struct thread *td, struct linux_newuname_args *args)
688 {
689 	struct l_new_utsname utsname;
690 	char osname[LINUX_MAX_UTSNAME];
691 	char osrelease[LINUX_MAX_UTSNAME];
692 
693 #ifdef DEBUG
694 	if (ldebug(newuname))
695 		printf(ARGS(newuname, "*"));
696 #endif
697 
698 	linux_get_osname(td, osname);
699 	linux_get_osrelease(td, osrelease);
700 
701 	bzero(&utsname, sizeof(utsname));
702 	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
703 	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
704 	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
705 	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
706 	strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
707 	strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
708 
709 	return (copyout(&utsname, args->buf, sizeof(utsname)));
710 }
711 
712 #if defined(__i386__)
713 struct l_utimbuf {
714 	l_time_t l_actime;
715 	l_time_t l_modtime;
716 };
717 
718 int
719 linux_utime(struct thread *td, struct linux_utime_args *args)
720 {
721 	struct timeval tv[2], *tvp;
722 	struct l_utimbuf lut;
723 	char *fname;
724 	int error;
725 
726 	LCONVPATHEXIST(td, args->fname, &fname);
727 
728 #ifdef DEBUG
729 	if (ldebug(utime))
730 		printf(ARGS(utime, "%s, *"), fname);
731 #endif
732 
733 	if (args->times) {
734 		if ((error = copyin(args->times, &lut, sizeof lut))) {
735 			LFREEPATH(fname);
736 			return error;
737 		}
738 		tv[0].tv_sec = lut.l_actime;
739 		tv[0].tv_usec = 0;
740 		tv[1].tv_sec = lut.l_modtime;
741 		tv[1].tv_usec = 0;
742 		tvp = tv;
743 	} else
744 		tvp = NULL;
745 
746 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
747 	LFREEPATH(fname);
748 	return (error);
749 }
750 #endif /* __i386__ */
751 
752 #define __WCLONE 0x80000000
753 
754 #ifndef __alpha__
755 int
756 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
757 {
758 	struct wait_args /* {
759 		int pid;
760 		int *status;
761 		int options;
762 		struct	rusage *rusage;
763 	} */ tmp;
764 	int error, tmpstat;
765 
766 #ifdef DEBUG
767 	if (ldebug(waitpid))
768 		printf(ARGS(waitpid, "%d, %p, %d"),
769 		    args->pid, (void *)args->status, args->options);
770 #endif
771 
772 	tmp.pid = args->pid;
773 	tmp.status = args->status;
774 	tmp.options = (args->options & (WNOHANG | WUNTRACED));
775 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
776 	if (args->options & __WCLONE)
777 		tmp.options |= WLINUXCLONE;
778 	tmp.rusage = NULL;
779 
780 	if ((error = wait4(td, &tmp)) != 0)
781 		return error;
782 
783 	if (args->status) {
784 		if ((error = copyin(args->status, &tmpstat, sizeof(int))) != 0)
785 			return error;
786 		tmpstat &= 0xffff;
787 		if (WIFSIGNALED(tmpstat))
788 			tmpstat = (tmpstat & 0xffffff80) |
789 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
790 		else if (WIFSTOPPED(tmpstat))
791 			tmpstat = (tmpstat & 0xffff00ff) |
792 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
793 		return copyout(&tmpstat, args->status, sizeof(int));
794 	}
795 
796 	return 0;
797 }
798 #endif	/*!__alpha__*/
799 
800 int
801 linux_wait4(struct thread *td, struct linux_wait4_args *args)
802 {
803 	struct wait_args /* {
804 		int pid;
805 		int *status;
806 		int options;
807 		struct	rusage *rusage;
808 	} */ tmp;
809 	int error, tmpstat;
810 	struct proc *p;
811 
812 #ifdef DEBUG
813 	if (ldebug(wait4))
814 		printf(ARGS(wait4, "%d, %p, %d, %p"),
815 		    args->pid, (void *)args->status, args->options,
816 		    (void *)args->rusage);
817 #endif
818 
819 	tmp.pid = args->pid;
820 	tmp.status = args->status;
821 	tmp.options = (args->options & (WNOHANG | WUNTRACED));
822 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
823 	if (args->options & __WCLONE)
824 		tmp.options |= WLINUXCLONE;
825 	tmp.rusage = (struct rusage *)args->rusage;
826 
827 	if ((error = wait4(td, &tmp)) != 0)
828 		return error;
829 
830 	p = td->td_proc;
831 	PROC_LOCK(p);
832 	SIGDELSET(p->p_siglist, SIGCHLD);
833 	PROC_UNLOCK(p);
834 
835 	if (args->status) {
836 		if ((error = copyin(args->status, &tmpstat, sizeof(int))) != 0)
837 			return error;
838 		tmpstat &= 0xffff;
839 		if (WIFSIGNALED(tmpstat))
840 			tmpstat = (tmpstat & 0xffffff80) |
841 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
842 		else if (WIFSTOPPED(tmpstat))
843 			tmpstat = (tmpstat & 0xffff00ff) |
844 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
845 		return copyout(&tmpstat, args->status, sizeof(int));
846 	}
847 
848 	return 0;
849 }
850 
851 int
852 linux_mknod(struct thread *td, struct linux_mknod_args *args)
853 {
854 	char *path;
855 	int error;
856 
857 	LCONVPATHCREAT(td, args->path, &path);
858 
859 #ifdef DEBUG
860 	if (ldebug(mknod))
861 		printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
862 #endif
863 
864 	if (args->mode & S_IFIFO)
865 		error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
866 	else
867 		error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
868 		    args->dev);
869 	LFREEPATH(path);
870 	return (error);
871 }
872 
873 /*
874  * UGH! This is just about the dumbest idea I've ever heard!!
875  */
876 int
877 linux_personality(struct thread *td, struct linux_personality_args *args)
878 {
879 #ifdef DEBUG
880 	if (ldebug(personality))
881 		printf(ARGS(personality, "%lu"), (unsigned long)args->per);
882 #endif
883 #ifndef __alpha__
884 	if (args->per != 0)
885 		return EINVAL;
886 #endif
887 
888 	/* Yes Jim, it's still a Linux... */
889 	td->td_retval[0] = 0;
890 	return 0;
891 }
892 
893 /*
894  * Wrappers for get/setitimer for debugging..
895  */
896 int
897 linux_setitimer(struct thread *td, struct linux_setitimer_args *args)
898 {
899 	struct setitimer_args bsa;
900 	struct itimerval foo;
901 	int error;
902 
903 #ifdef DEBUG
904 	if (ldebug(setitimer))
905 		printf(ARGS(setitimer, "%p, %p"),
906 		    (void *)args->itv, (void *)args->oitv);
907 #endif
908 	bsa.which = args->which;
909 	bsa.itv = (struct itimerval *)args->itv;
910 	bsa.oitv = (struct itimerval *)args->oitv;
911 	if (args->itv) {
912 	    if ((error = copyin(args->itv, &foo, sizeof(foo))))
913 		return error;
914 #ifdef DEBUG
915 	    if (ldebug(setitimer)) {
916 		printf("setitimer: value: sec: %ld, usec: %ld\n",
917 		    foo.it_value.tv_sec, foo.it_value.tv_usec);
918 		printf("setitimer: interval: sec: %ld, usec: %ld\n",
919 		    foo.it_interval.tv_sec, foo.it_interval.tv_usec);
920 	    }
921 #endif
922 	}
923 	return setitimer(td, &bsa);
924 }
925 
926 int
927 linux_getitimer(struct thread *td, struct linux_getitimer_args *args)
928 {
929 	struct getitimer_args bsa;
930 #ifdef DEBUG
931 	if (ldebug(getitimer))
932 		printf(ARGS(getitimer, "%p"), (void *)args->itv);
933 #endif
934 	bsa.which = args->which;
935 	bsa.itv = (struct itimerval *)args->itv;
936 	return getitimer(td, &bsa);
937 }
938 
939 #ifndef __alpha__
940 int
941 linux_nice(struct thread *td, struct linux_nice_args *args)
942 {
943 	struct setpriority_args	bsd_args;
944 
945 	bsd_args.which = PRIO_PROCESS;
946 	bsd_args.who = 0;	/* current process */
947 	bsd_args.prio = args->inc;
948 	return setpriority(td, &bsd_args);
949 }
950 #endif	/*!__alpha__*/
951 
952 int
953 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
954 {
955 	struct ucred *newcred, *oldcred;
956 	l_gid_t linux_gidset[NGROUPS];
957 	gid_t *bsd_gidset;
958 	int ngrp, error;
959 	struct proc *p;
960 
961 	ngrp = args->gidsetsize;
962 	if (ngrp >= NGROUPS)
963 		return (EINVAL);
964 	error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
965 	if (error)
966 		return (error);
967 	newcred = crget();
968 	p = td->td_proc;
969 	PROC_LOCK(p);
970 	oldcred = p->p_ucred;
971 
972 	/*
973 	 * cr_groups[0] holds egid. Setting the whole set from
974 	 * the supplied set will cause egid to be changed too.
975 	 * Keep cr_groups[0] unchanged to prevent that.
976 	 */
977 
978 	if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0) {
979 		PROC_UNLOCK(p);
980 		crfree(newcred);
981 		return (error);
982 	}
983 
984 	crcopy(newcred, oldcred);
985 	if (ngrp > 0) {
986 		newcred->cr_ngroups = ngrp + 1;
987 
988 		bsd_gidset = newcred->cr_groups;
989 		ngrp--;
990 		while (ngrp >= 0) {
991 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
992 			ngrp--;
993 		}
994 	}
995 	else
996 		newcred->cr_ngroups = 1;
997 
998 	setsugid(p);
999 	p->p_ucred = newcred;
1000 	PROC_UNLOCK(p);
1001 	crfree(oldcred);
1002 	return (0);
1003 }
1004 
1005 int
1006 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1007 {
1008 	struct ucred *cred;
1009 	l_gid_t linux_gidset[NGROUPS];
1010 	gid_t *bsd_gidset;
1011 	int bsd_gidsetsz, ngrp, error;
1012 
1013 	cred = td->td_ucred;
1014 	bsd_gidset = cred->cr_groups;
1015 	bsd_gidsetsz = cred->cr_ngroups - 1;
1016 
1017 	/*
1018 	 * cr_groups[0] holds egid. Returning the whole set
1019 	 * here will cause a duplicate. Exclude cr_groups[0]
1020 	 * to prevent that.
1021 	 */
1022 
1023 	if ((ngrp = args->gidsetsize) == 0) {
1024 		td->td_retval[0] = bsd_gidsetsz;
1025 		return (0);
1026 	}
1027 
1028 	if (ngrp < bsd_gidsetsz)
1029 		return (EINVAL);
1030 
1031 	ngrp = 0;
1032 	while (ngrp < bsd_gidsetsz) {
1033 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1034 		ngrp++;
1035 	}
1036 
1037 	if ((error = copyout(linux_gidset, args->grouplist,
1038 	    ngrp * sizeof(l_gid_t))))
1039 		return (error);
1040 
1041 	td->td_retval[0] = ngrp;
1042 	return (0);
1043 }
1044 
1045 #ifndef __alpha__
1046 int
1047 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1048 {
1049 	struct rlimit bsd_rlim;
1050 	struct l_rlimit rlim;
1051 	u_int which;
1052 	int error;
1053 
1054 #ifdef DEBUG
1055 	if (ldebug(setrlimit))
1056 		printf(ARGS(setrlimit, "%d, %p"),
1057 		    args->resource, (void *)args->rlim);
1058 #endif
1059 
1060 	if (args->resource >= LINUX_RLIM_NLIMITS)
1061 		return (EINVAL);
1062 
1063 	which = linux_to_bsd_resource[args->resource];
1064 	if (which == -1)
1065 		return (EINVAL);
1066 
1067 	error = copyin(args->rlim, &rlim, sizeof(rlim));
1068 	if (error)
1069 		return (error);
1070 
1071 	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1072 	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1073 	return (dosetrlimit(td, which, &bsd_rlim));
1074 }
1075 
1076 int
1077 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1078 {
1079 	struct l_rlimit rlim;
1080 	struct proc *p = td->td_proc;
1081 	struct rlimit *bsd_rlp;
1082 	u_int which;
1083 
1084 #ifdef DEBUG
1085 	if (ldebug(old_getrlimit))
1086 		printf(ARGS(old_getrlimit, "%d, %p"),
1087 		    args->resource, (void *)args->rlim);
1088 #endif
1089 
1090 	if (args->resource >= LINUX_RLIM_NLIMITS)
1091 		return (EINVAL);
1092 
1093 	which = linux_to_bsd_resource[args->resource];
1094 	if (which == -1)
1095 		return (EINVAL);
1096 	bsd_rlp = &p->p_rlimit[which];
1097 
1098 	rlim.rlim_cur = (unsigned long)bsd_rlp->rlim_cur;
1099 	if (rlim.rlim_cur == ULONG_MAX)
1100 		rlim.rlim_cur = LONG_MAX;
1101 	rlim.rlim_max = (unsigned long)bsd_rlp->rlim_max;
1102 	if (rlim.rlim_max == ULONG_MAX)
1103 		rlim.rlim_max = LONG_MAX;
1104 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1105 }
1106 
1107 int
1108 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1109 {
1110 	struct l_rlimit rlim;
1111 	struct proc *p = td->td_proc;
1112 	struct rlimit *bsd_rlp;
1113 	u_int which;
1114 
1115 #ifdef DEBUG
1116 	if (ldebug(getrlimit))
1117 		printf(ARGS(getrlimit, "%d, %p"),
1118 		    args->resource, (void *)args->rlim);
1119 #endif
1120 
1121 	if (args->resource >= LINUX_RLIM_NLIMITS)
1122 		return (EINVAL);
1123 
1124 	which = linux_to_bsd_resource[args->resource];
1125 	if (which == -1)
1126 		return (EINVAL);
1127 	bsd_rlp = &p->p_rlimit[which];
1128 
1129 	rlim.rlim_cur = (l_ulong)bsd_rlp->rlim_cur;
1130 	rlim.rlim_max = (l_ulong)bsd_rlp->rlim_max;
1131 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1132 }
1133 #endif /*!__alpha__*/
1134 
1135 int
1136 linux_sched_setscheduler(struct thread *td,
1137     struct linux_sched_setscheduler_args *args)
1138 {
1139 	struct sched_setscheduler_args bsd;
1140 
1141 #ifdef DEBUG
1142 	if (ldebug(sched_setscheduler))
1143 		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1144 		    args->pid, args->policy, (const void *)args->param);
1145 #endif
1146 
1147 	switch (args->policy) {
1148 	case LINUX_SCHED_OTHER:
1149 		bsd.policy = SCHED_OTHER;
1150 		break;
1151 	case LINUX_SCHED_FIFO:
1152 		bsd.policy = SCHED_FIFO;
1153 		break;
1154 	case LINUX_SCHED_RR:
1155 		bsd.policy = SCHED_RR;
1156 		break;
1157 	default:
1158 		return EINVAL;
1159 	}
1160 
1161 	bsd.pid = args->pid;
1162 	bsd.param = (struct sched_param *)args->param;
1163 	return sched_setscheduler(td, &bsd);
1164 }
1165 
1166 int
1167 linux_sched_getscheduler(struct thread *td,
1168     struct linux_sched_getscheduler_args *args)
1169 {
1170 	struct sched_getscheduler_args bsd;
1171 	int error;
1172 
1173 #ifdef DEBUG
1174 	if (ldebug(sched_getscheduler))
1175 		printf(ARGS(sched_getscheduler, "%d"), args->pid);
1176 #endif
1177 
1178 	bsd.pid = args->pid;
1179 	error = sched_getscheduler(td, &bsd);
1180 
1181 	switch (td->td_retval[0]) {
1182 	case SCHED_OTHER:
1183 		td->td_retval[0] = LINUX_SCHED_OTHER;
1184 		break;
1185 	case SCHED_FIFO:
1186 		td->td_retval[0] = LINUX_SCHED_FIFO;
1187 		break;
1188 	case SCHED_RR:
1189 		td->td_retval[0] = LINUX_SCHED_RR;
1190 		break;
1191 	}
1192 
1193 	return error;
1194 }
1195 
1196 int
1197 linux_sched_get_priority_max(struct thread *td,
1198     struct linux_sched_get_priority_max_args *args)
1199 {
1200 	struct sched_get_priority_max_args bsd;
1201 
1202 #ifdef DEBUG
1203 	if (ldebug(sched_get_priority_max))
1204 		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1205 #endif
1206 
1207 	switch (args->policy) {
1208 	case LINUX_SCHED_OTHER:
1209 		bsd.policy = SCHED_OTHER;
1210 		break;
1211 	case LINUX_SCHED_FIFO:
1212 		bsd.policy = SCHED_FIFO;
1213 		break;
1214 	case LINUX_SCHED_RR:
1215 		bsd.policy = SCHED_RR;
1216 		break;
1217 	default:
1218 		return EINVAL;
1219 	}
1220 	return sched_get_priority_max(td, &bsd);
1221 }
1222 
1223 int
1224 linux_sched_get_priority_min(struct thread *td,
1225     struct linux_sched_get_priority_min_args *args)
1226 {
1227 	struct sched_get_priority_min_args bsd;
1228 
1229 #ifdef DEBUG
1230 	if (ldebug(sched_get_priority_min))
1231 		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1232 #endif
1233 
1234 	switch (args->policy) {
1235 	case LINUX_SCHED_OTHER:
1236 		bsd.policy = SCHED_OTHER;
1237 		break;
1238 	case LINUX_SCHED_FIFO:
1239 		bsd.policy = SCHED_FIFO;
1240 		break;
1241 	case LINUX_SCHED_RR:
1242 		bsd.policy = SCHED_RR;
1243 		break;
1244 	default:
1245 		return EINVAL;
1246 	}
1247 	return sched_get_priority_min(td, &bsd);
1248 }
1249 
1250 #define REBOOT_CAD_ON	0x89abcdef
1251 #define REBOOT_CAD_OFF	0
1252 #define REBOOT_HALT	0xcdef0123
1253 
1254 int
1255 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1256 {
1257 	struct reboot_args bsd_args;
1258 
1259 #ifdef DEBUG
1260 	if (ldebug(reboot))
1261 		printf(ARGS(reboot, "0x%x"), args->cmd);
1262 #endif
1263 	if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1264 		return (0);
1265 	bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1266 	return (reboot(td, &bsd_args));
1267 }
1268 
1269 #ifndef __alpha__
1270 
1271 /*
1272  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1273  * td->td_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1274  * globbers registers that are assumed to be preserved. The following
1275  * lightweight syscalls fixes this. See also linux_getgid16() and
1276  * linux_getuid16() in linux_uid16.c.
1277  *
1278  * linux_getpid() - MP SAFE
1279  * linux_getgid() - MP SAFE
1280  * linux_getuid() - MP SAFE
1281  */
1282 
1283 int
1284 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1285 {
1286 
1287 	td->td_retval[0] = td->td_proc->p_pid;
1288 	return (0);
1289 }
1290 
1291 int
1292 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1293 {
1294 
1295 	td->td_retval[0] = td->td_ucred->cr_rgid;
1296 	return (0);
1297 }
1298 
1299 int
1300 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1301 {
1302 
1303 	td->td_retval[0] = td->td_ucred->cr_ruid;
1304 	return (0);
1305 }
1306 
1307 #endif /*!__alpha__*/
1308 
1309 int
1310 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1311 {
1312 	struct getsid_args bsd;
1313 	bsd.pid = args->pid;
1314 	return getsid(td, &bsd);
1315 }
1316