xref: /freebsd/sys/compat/linux/linux_misc.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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  * $FreeBSD$
29  */
30 
31 #include "opt_mac.h"
32 
33 #include <sys/param.h>
34 #include <sys/blist.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact_aout.h>
37 #include <sys/jail.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/mac.h>
41 #include <sys/malloc.h>
42 #include <sys/mman.h>
43 #include <sys/mount.h>
44 #include <sys/mutex.h>
45 #include <sys/namei.h>
46 #include <sys/proc.h>
47 #include <sys/reboot.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
50 #include <sys/stat.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysctl.h>
53 #include <sys/sysproto.h>
54 #include <sys/systm.h>
55 #include <sys/time.h>
56 #include <sys/vmmeter.h>
57 #include <sys/vnode.h>
58 #include <sys/wait.h>
59 
60 #include <vm/vm.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_extern.h>
65 #include <vm/vm_object.h>
66 #include <vm/swap_pager.h>
67 
68 #include <machine/limits.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;
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 	if (swapblist == NULL) {
148 		sysinfo.totalswap= 0;
149 		sysinfo.freeswap = 0;
150 	} else {
151 		sysinfo.totalswap = swapblist->bl_blocks * 1024;
152 		sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
153 	}
154 
155 	sysinfo.procs = 20; /* Hack */
156 
157 	return copyout(&sysinfo, args->info, sizeof(sysinfo));
158 }
159 #endif /*!__alpha__*/
160 
161 #ifndef __alpha__
162 int
163 linux_alarm(struct thread *td, struct linux_alarm_args *args)
164 {
165 	struct itimerval it, old_it;
166 	struct timeval tv;
167 
168 #ifdef DEBUG
169 	if (ldebug(alarm))
170 		printf(ARGS(alarm, "%u"), args->secs);
171 #endif
172 
173 	if (args->secs > 100000000)
174 		return EINVAL;
175 
176 	it.it_value.tv_sec = (long)args->secs;
177 	it.it_value.tv_usec = 0;
178 	it.it_interval.tv_sec = 0;
179 	it.it_interval.tv_usec = 0;
180 	PROC_LOCK(td->td_proc);
181 	old_it = td->td_proc->p_realtimer;
182 	getmicrouptime(&tv);
183 	if (timevalisset(&old_it.it_value))
184 		callout_stop(&td->td_proc->p_itcallout);
185 	if (it.it_value.tv_sec != 0) {
186 		callout_reset(&td->td_proc->p_itcallout, tvtohz(&it.it_value),
187 		    realitexpire, td->td_proc);
188 		timevaladd(&it.it_value, &tv);
189 	}
190 	td->td_proc->p_realtimer = it;
191 	PROC_UNLOCK(td->td_proc);
192 	if (timevalcmp(&old_it.it_value, &tv, >)) {
193 		timevalsub(&old_it.it_value, &tv);
194 		if (old_it.it_value.tv_usec != 0)
195 			old_it.it_value.tv_sec++;
196 		td->td_retval[0] = old_it.it_value.tv_sec;
197 	}
198 	return 0;
199 }
200 #endif /*!__alpha__*/
201 
202 int
203 linux_brk(struct thread *td, struct linux_brk_args *args)
204 {
205 	struct vmspace *vm = td->td_proc->p_vmspace;
206 	vm_offset_t new, old;
207 	struct obreak_args /* {
208 		char * nsize;
209 	} */ tmp;
210 
211 #ifdef DEBUG
212 	if (ldebug(brk))
213 		printf(ARGS(brk, "%p"), (void *)args->dsend);
214 #endif
215 	old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
216 	new = (vm_offset_t)args->dsend;
217 	tmp.nsize = (char *) new;
218 	if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
219 		td->td_retval[0] = (long)new;
220 	else
221 		td->td_retval[0] = (long)old;
222 
223 	return 0;
224 }
225 
226 int
227 linux_uselib(struct thread *td, struct linux_uselib_args *args)
228 {
229 	struct nameidata ni;
230 	struct vnode *vp;
231 	struct exec *a_out;
232 	struct vattr attr;
233 	vm_offset_t vmaddr;
234 	unsigned long file_offset;
235 	vm_offset_t buffer;
236 	unsigned long bss_size;
237 	char *library;
238 	int error;
239 	int locked;
240 
241 	LCONVPATHEXIST(td, args->library, &library);
242 
243 #ifdef DEBUG
244 	if (ldebug(uselib))
245 		printf(ARGS(uselib, "%s"), library);
246 #endif
247 
248 	a_out = NULL;
249 	locked = 0;
250 	vp = NULL;
251 
252 	/*
253 	 * XXX: This code should make use of vn_open(), rather than doing
254 	 * all this stuff itself.
255 	 */
256 	NDINIT(&ni, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, library, td);
257 	error = namei(&ni);
258 	LFREEPATH(library);
259 	if (error)
260 		goto cleanup;
261 
262 	vp = ni.ni_vp;
263 	/*
264 	 * XXX - This looks like a bogus check. A LOCKLEAF namei should not
265 	 * succeed without returning a vnode.
266 	 */
267 	if (vp == NULL) {
268 		error = ENOEXEC;	/* ?? */
269 		goto cleanup;
270 	}
271 	NDFREE(&ni, NDF_ONLY_PNBUF);
272 
273 	/*
274 	 * From here on down, we have a locked vnode that must be unlocked.
275 	 */
276 	locked++;
277 
278 	/* Writable? */
279 	if (vp->v_writecount) {
280 		error = ETXTBSY;
281 		goto cleanup;
282 	}
283 
284 	/* Executable? */
285 	error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
286 	if (error)
287 		goto cleanup;
288 
289 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
290 	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
291 		error = ENOEXEC;
292 		goto cleanup;
293 	}
294 
295 	/* Sensible size? */
296 	if (attr.va_size == 0) {
297 		error = ENOEXEC;
298 		goto cleanup;
299 	}
300 
301 	/* Can we access it? */
302 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
303 	if (error)
304 		goto cleanup;
305 
306 	/*
307 	 * XXX: This should use vn_open() so that it is properly authorized,
308 	 * and to reduce code redundancy all over the place here.
309 	 */
310 #ifdef MAC
311 	error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
312 	if (error)
313 		goto cleanup;
314 #endif
315 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td);
316 	if (error)
317 		goto cleanup;
318 
319 	/* Pull in executable header into kernel_map */
320 	error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
321 	    VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
322 	/*
323 	 * Lock no longer needed
324 	 */
325 	locked = 0;
326 	VOP_UNLOCK(vp, 0, td);
327 
328 	if (error)
329 		goto cleanup;
330 
331 	/* Is it a Linux binary ? */
332 	if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
333 		error = ENOEXEC;
334 		goto cleanup;
335 	}
336 
337 	/*
338 	 * While we are here, we should REALLY do some more checks
339 	 */
340 
341 	/* Set file/virtual offset based on a.out variant. */
342 	switch ((int)(a_out->a_magic & 0xffff)) {
343 	case 0413:	/* ZMAGIC */
344 		file_offset = 1024;
345 		break;
346 	case 0314:	/* QMAGIC */
347 		file_offset = 0;
348 		break;
349 	default:
350 		error = ENOEXEC;
351 		goto cleanup;
352 	}
353 
354 	bss_size = round_page(a_out->a_bss);
355 
356 	/* Check various fields in header for validity/bounds. */
357 	if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
358 		error = ENOEXEC;
359 		goto cleanup;
360 	}
361 
362 	/* text + data can't exceed file size */
363 	if (a_out->a_data + a_out->a_text > attr.va_size) {
364 		error = EFAULT;
365 		goto cleanup;
366 	}
367 
368 	/* To protect td->td_proc->p_rlimit in the if condition. */
369 	mtx_assert(&Giant, MA_OWNED);
370 
371 	/*
372 	 * text/data/bss must not exceed limits
373 	 * XXX - this is not complete. it should check current usage PLUS
374 	 * the resources needed by this library.
375 	 */
376 	if (a_out->a_text > maxtsiz ||
377 	    a_out->a_data + bss_size >
378 	    td->td_proc->p_rlimit[RLIMIT_DATA].rlim_cur) {
379 		error = ENOMEM;
380 		goto cleanup;
381 	}
382 
383 	mp_fixme("Unlocked vflags access.");
384 	/* prevent more writers */
385 	vp->v_vflag |= VV_TEXT;
386 
387 	/*
388 	 * Check if file_offset page aligned. Currently we cannot handle
389 	 * misalinged file offsets, and so we read in the entire image
390 	 * (what a waste).
391 	 */
392 	if (file_offset & PAGE_MASK) {
393 #ifdef DEBUG
394 		printf("uselib: Non page aligned binary %lu\n", file_offset);
395 #endif
396 		/* Map text+data read/write/execute */
397 
398 		/* a_entry is the load address and is page aligned */
399 		vmaddr = trunc_page(a_out->a_entry);
400 
401 		/* get anon user mapping, read+write+execute */
402 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
403 		    &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
404 		    VM_PROT_ALL, 0);
405 		if (error)
406 			goto cleanup;
407 
408 		/* map file into kernel_map */
409 		error = vm_mmap(kernel_map, &buffer,
410 		    round_page(a_out->a_text + a_out->a_data + file_offset),
411 		    VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
412 		    trunc_page(file_offset));
413 		if (error)
414 			goto cleanup;
415 
416 		/* copy from kernel VM space to user space */
417 		error = copyout((void *)(buffer + file_offset),
418 		    (void *)vmaddr, a_out->a_text + a_out->a_data);
419 
420 		/* release temporary kernel space */
421 		vm_map_remove(kernel_map, buffer, buffer +
422 		    round_page(a_out->a_text + a_out->a_data + file_offset));
423 
424 		if (error)
425 			goto cleanup;
426 	} else {
427 #ifdef DEBUG
428 		printf("uselib: Page aligned binary %lu\n", file_offset);
429 #endif
430 		/*
431 		 * for QMAGIC, a_entry is 20 bytes beyond the load address
432 		 * to skip the executable header
433 		 */
434 		vmaddr = trunc_page(a_out->a_entry);
435 
436 		/*
437 		 * Map it all into the process's space as a single
438 		 * copy-on-write "data" segment.
439 		 */
440 		error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
441 		    a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
442 		    MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
443 		if (error)
444 			goto cleanup;
445 	}
446 #ifdef DEBUG
447 	printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
448 	    ((long*)vmaddr)[1]);
449 #endif
450 	if (bss_size != 0) {
451 		/* Calculate BSS start address */
452 		vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
453 		    a_out->a_data;
454 
455 		/* allocate some 'anon' space */
456 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
457 		    &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
458 		if (error)
459 			goto cleanup;
460 	}
461 
462 cleanup:
463 	/* Unlock vnode if needed */
464 	if (locked)
465 		VOP_UNLOCK(vp, 0, td);
466 
467 	/* Release the kernel mapping. */
468 	if (a_out)
469 		vm_map_remove(kernel_map, (vm_offset_t)a_out,
470 		    (vm_offset_t)a_out + PAGE_SIZE);
471 
472 	return error;
473 }
474 
475 int
476 linux_select(struct thread *td, struct linux_select_args *args)
477 {
478 	struct timeval tv0, tv1, utv, *tvp;
479 	int error;
480 
481 #ifdef DEBUG
482 	if (ldebug(select))
483 		printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
484 		    (void *)args->readfds, (void *)args->writefds,
485 		    (void *)args->exceptfds, (void *)args->timeout);
486 #endif
487 
488 	/*
489 	 * Store current time for computation of the amount of
490 	 * time left.
491 	 */
492 	if (args->timeout) {
493 		if ((error = copyin(args->timeout, &utv, sizeof(utv))))
494 			goto select_out;
495 #ifdef DEBUG
496 		if (ldebug(select))
497 			printf(LMSG("incoming timeout (%ld/%ld)"),
498 			    utv.tv_sec, utv.tv_usec);
499 #endif
500 
501 		if (itimerfix(&utv)) {
502 			/*
503 			 * The timeval was invalid.  Convert it to something
504 			 * valid that will act as it does under Linux.
505 			 */
506 			utv.tv_sec += utv.tv_usec / 1000000;
507 			utv.tv_usec %= 1000000;
508 			if (utv.tv_usec < 0) {
509 				utv.tv_sec -= 1;
510 				utv.tv_usec += 1000000;
511 			}
512 			if (utv.tv_sec < 0)
513 				timevalclear(&utv);
514 		}
515 		microtime(&tv0);
516 		tvp = &utv;
517 	} else
518 		tvp = NULL;
519 
520 	error = kern_select(td, args->nfds, args->readfds, args->writefds,
521 	    args->exceptfds, tvp);
522 
523 #ifdef DEBUG
524 	if (ldebug(select))
525 		printf(LMSG("real select returns %d"), error);
526 #endif
527 	if (error) {
528 		/*
529 		 * See fs/select.c in the Linux kernel.  Without this,
530 		 * Maelstrom doesn't work.
531 		 */
532 		if (error == ERESTART)
533 			error = EINTR;
534 		goto select_out;
535 	}
536 
537 	if (args->timeout) {
538 		if (td->td_retval[0]) {
539 			/*
540 			 * Compute how much time was left of the timeout,
541 			 * by subtracting the current time and the time
542 			 * before we started the call, and subtracting
543 			 * that result from the user-supplied value.
544 			 */
545 			microtime(&tv1);
546 			timevalsub(&tv1, &tv0);
547 			timevalsub(&utv, &tv1);
548 			if (utv.tv_sec < 0)
549 				timevalclear(&utv);
550 		} else
551 			timevalclear(&utv);
552 #ifdef DEBUG
553 		if (ldebug(select))
554 			printf(LMSG("outgoing timeout (%ld/%ld)"),
555 			    utv.tv_sec, utv.tv_usec);
556 #endif
557 		if ((error = copyout(&utv, args->timeout, sizeof(utv))))
558 			goto select_out;
559 	}
560 
561 select_out:
562 #ifdef DEBUG
563 	if (ldebug(select))
564 		printf(LMSG("select_out -> %d"), error);
565 #endif
566 	return error;
567 }
568 
569 int
570 linux_mremap(struct thread *td, struct linux_mremap_args *args)
571 {
572 	struct munmap_args /* {
573 		void *addr;
574 		size_t len;
575 	} */ bsd_args;
576 	int error = 0;
577 
578 #ifdef DEBUG
579 	if (ldebug(mremap))
580 		printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
581 		    (void *)args->addr,
582 		    (unsigned long)args->old_len,
583 		    (unsigned long)args->new_len,
584 		    (unsigned long)args->flags);
585 #endif
586 	args->new_len = round_page(args->new_len);
587 	args->old_len = round_page(args->old_len);
588 
589 	if (args->new_len > args->old_len) {
590 		td->td_retval[0] = 0;
591 		return ENOMEM;
592 	}
593 
594 	if (args->new_len < args->old_len) {
595 		bsd_args.addr = (caddr_t)(args->addr + args->new_len);
596 		bsd_args.len = args->old_len - args->new_len;
597 		error = munmap(td, &bsd_args);
598 	}
599 
600 	td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
601 	return error;
602 }
603 
604 #define LINUX_MS_ASYNC       0x0001
605 #define LINUX_MS_INVALIDATE  0x0002
606 #define LINUX_MS_SYNC        0x0004
607 
608 int
609 linux_msync(struct thread *td, struct linux_msync_args *args)
610 {
611 	struct msync_args bsd_args;
612 
613 	bsd_args.addr = (caddr_t)args->addr;
614 	bsd_args.len = args->len;
615 	bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
616 
617 	return msync(td, &bsd_args);
618 }
619 
620 #ifndef __alpha__
621 int
622 linux_time(struct thread *td, struct linux_time_args *args)
623 {
624 	struct timeval tv;
625 	l_time_t tm;
626 	int error;
627 
628 #ifdef DEBUG
629 	if (ldebug(time))
630 		printf(ARGS(time, "*"));
631 #endif
632 
633 	microtime(&tv);
634 	tm = tv.tv_sec;
635 	if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
636 		return error;
637 	td->td_retval[0] = tm;
638 	return 0;
639 }
640 #endif	/*!__alpha__*/
641 
642 struct l_times_argv {
643 	l_long		tms_utime;
644 	l_long		tms_stime;
645 	l_long		tms_cutime;
646 	l_long		tms_cstime;
647 };
648 
649 #ifdef __alpha__
650 #define CLK_TCK 1024	/* Linux uses 1024 on alpha */
651 #else
652 #define CLK_TCK 100	/* Linux uses 100 */
653 #endif
654 
655 #define CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
656 
657 int
658 linux_times(struct thread *td, struct linux_times_args *args)
659 {
660 	struct timeval tv;
661 	struct l_times_argv tms;
662 	struct rusage ru;
663 	int error;
664 
665 #ifdef DEBUG
666 	if (ldebug(times))
667 		printf(ARGS(times, "*"));
668 #endif
669 
670 	mtx_lock_spin(&sched_lock);
671 	calcru(td->td_proc, &ru.ru_utime, &ru.ru_stime, NULL);
672 	mtx_unlock_spin(&sched_lock);
673 
674 	tms.tms_utime = CONVTCK(ru.ru_utime);
675 	tms.tms_stime = CONVTCK(ru.ru_stime);
676 
677 	tms.tms_cutime = CONVTCK(td->td_proc->p_stats->p_cru.ru_utime);
678 	tms.tms_cstime = CONVTCK(td->td_proc->p_stats->p_cru.ru_stime);
679 
680 	if ((error = copyout(&tms, args->buf, sizeof(tms))))
681 		return error;
682 
683 	microuptime(&tv);
684 	td->td_retval[0] = (int)CONVTCK(tv);
685 	return 0;
686 }
687 
688 int
689 linux_newuname(struct thread *td, struct linux_newuname_args *args)
690 {
691 	struct l_new_utsname utsname;
692 	char osname[LINUX_MAX_UTSNAME];
693 	char osrelease[LINUX_MAX_UTSNAME];
694 
695 #ifdef DEBUG
696 	if (ldebug(newuname))
697 		printf(ARGS(newuname, "*"));
698 #endif
699 
700 	linux_get_osname(td, osname);
701 	linux_get_osrelease(td, osrelease);
702 
703 	bzero(&utsname, sizeof(utsname));
704 	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
705 	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
706 	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
707 	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
708 	strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
709 	strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
710 
711 	return (copyout(&utsname, args->buf, sizeof(utsname)));
712 }
713 
714 #if defined(__i386__)
715 struct l_utimbuf {
716 	l_time_t l_actime;
717 	l_time_t l_modtime;
718 };
719 
720 int
721 linux_utime(struct thread *td, struct linux_utime_args *args)
722 {
723 	struct timeval tv[2], *tvp;
724 	struct l_utimbuf lut;
725 	char *fname;
726 	int error;
727 
728 	LCONVPATHEXIST(td, args->fname, &fname);
729 
730 #ifdef DEBUG
731 	if (ldebug(utime))
732 		printf(ARGS(utime, "%s, *"), fname);
733 #endif
734 
735 	if (args->times) {
736 		if ((error = copyin(args->times, &lut, sizeof lut))) {
737 			LFREEPATH(fname);
738 			return error;
739 		}
740 		tv[0].tv_sec = lut.l_actime;
741 		tv[0].tv_usec = 0;
742 		tv[1].tv_sec = lut.l_modtime;
743 		tv[1].tv_usec = 0;
744 		tvp = tv;
745 	} else
746 		tvp = NULL;
747 
748 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
749 	LFREEPATH(fname);
750 	return (error);
751 }
752 #endif /* __i386__ */
753 
754 #define __WCLONE 0x80000000
755 
756 #ifndef __alpha__
757 int
758 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
759 {
760 	struct wait_args /* {
761 		int pid;
762 		int *status;
763 		int options;
764 		struct	rusage *rusage;
765 	} */ tmp;
766 	int error, tmpstat;
767 
768 #ifdef DEBUG
769 	if (ldebug(waitpid))
770 		printf(ARGS(waitpid, "%d, %p, %d"),
771 		    args->pid, (void *)args->status, args->options);
772 #endif
773 
774 	tmp.pid = args->pid;
775 	tmp.status = args->status;
776 	tmp.options = (args->options & (WNOHANG | WUNTRACED));
777 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
778 	if (args->options & __WCLONE)
779 		tmp.options |= WLINUXCLONE;
780 	tmp.rusage = NULL;
781 
782 	if ((error = wait4(td, &tmp)) != 0)
783 		return error;
784 
785 	if (args->status) {
786 		if ((error = copyin(args->status, &tmpstat, sizeof(int))) != 0)
787 			return error;
788 		tmpstat &= 0xffff;
789 		if (WIFSIGNALED(tmpstat))
790 			tmpstat = (tmpstat & 0xffffff80) |
791 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
792 		else if (WIFSTOPPED(tmpstat))
793 			tmpstat = (tmpstat & 0xffff00ff) |
794 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
795 		return copyout(&tmpstat, args->status, sizeof(int));
796 	}
797 
798 	return 0;
799 }
800 #endif	/*!__alpha__*/
801 
802 int
803 linux_wait4(struct thread *td, struct linux_wait4_args *args)
804 {
805 	struct wait_args /* {
806 		int pid;
807 		int *status;
808 		int options;
809 		struct	rusage *rusage;
810 	} */ tmp;
811 	int error, tmpstat;
812 
813 #ifdef DEBUG
814 	if (ldebug(wait4))
815 		printf(ARGS(wait4, "%d, %p, %d, %p"),
816 		    args->pid, (void *)args->status, args->options,
817 		    (void *)args->rusage);
818 #endif
819 
820 	tmp.pid = args->pid;
821 	tmp.status = args->status;
822 	tmp.options = (args->options & (WNOHANG | WUNTRACED));
823 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
824 	if (args->options & __WCLONE)
825 		tmp.options |= WLINUXCLONE;
826 	tmp.rusage = (struct rusage *)args->rusage;
827 
828 	if ((error = wait4(td, &tmp)) != 0)
829 		return error;
830 
831 	PROC_LOCK(td->td_proc);
832 	SIGDELSET(td->td_proc->p_siglist, SIGCHLD);
833 	PROC_UNLOCK(td->td_proc);
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, "%d"), 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