xref: /freebsd/sys/compat/linux/linux_misc.c (revision b2338d532a3cbe82279fd83bfb3dbd38b3a8d1dc)
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 withough 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_compat.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
36 #include <sys/kernel.h>
37 #include <sys/mman.h>
38 #include <sys/proc.h>
39 #include <sys/fcntl.h>
40 #include <sys/imgact_aout.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
43 #include <sys/resourcevar.h>
44 #include <sys/stat.h>
45 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/vnode.h>
48 #include <sys/wait.h>
49 #include <sys/time.h>
50 #include <sys/signalvar.h>
51 
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_extern.h>
57 
58 #include <machine/frame.h>
59 #include <machine/limits.h>
60 #include <machine/psl.h>
61 #include <machine/sysarch.h>
62 #include <machine/segments.h>
63 
64 #include <posix4/sched.h>
65 
66 #include <machine/../linux/linux.h>
67 #include <machine/../linux/linux_proto.h>
68 #include <compat/linux/linux_mib.h>
69 #include <compat/linux/linux_util.h>
70 
71 #define BSD_TO_LINUX_SIGNAL(sig)	\
72 	(((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
73 
74 struct linux_rlimit {
75 	unsigned long rlim_cur;
76 	unsigned long rlim_max;
77 };
78 
79 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] =
80 { RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
81   RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
82   RLIMIT_MEMLOCK, -1
83 };
84 
85 int
86 linux_alarm(struct proc *p, struct linux_alarm_args *args)
87 {
88     struct itimerval it, old_it;
89     struct timeval tv;
90     int s;
91 
92 #ifdef DEBUG
93     printf("Linux-emul(%ld): alarm(%u)\n", (long)p->p_pid, args->secs);
94 #endif
95     if (args->secs > 100000000)
96 	return EINVAL;
97     it.it_value.tv_sec = (long)args->secs;
98     it.it_value.tv_usec = 0;
99     it.it_interval.tv_sec = 0;
100     it.it_interval.tv_usec = 0;
101     s = splsoftclock();
102     old_it = p->p_realtimer;
103     getmicrouptime(&tv);
104     if (timevalisset(&old_it.it_value))
105 	untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
106     if (it.it_value.tv_sec != 0) {
107 	p->p_ithandle = timeout(realitexpire, (caddr_t)p, tvtohz(&it.it_value));
108 	timevaladd(&it.it_value, &tv);
109     }
110     p->p_realtimer = it;
111     splx(s);
112     if (timevalcmp(&old_it.it_value, &tv, >)) {
113 	timevalsub(&old_it.it_value, &tv);
114 	if (old_it.it_value.tv_usec != 0)
115 	    old_it.it_value.tv_sec++;
116 	p->p_retval[0] = old_it.it_value.tv_sec;
117     }
118     return 0;
119 }
120 
121 int
122 linux_brk(struct proc *p, struct linux_brk_args *args)
123 {
124 #if 0
125     struct vmspace *vm = p->p_vmspace;
126     vm_offset_t new, old;
127     int error;
128 
129     if ((vm_offset_t)args->dsend < (vm_offset_t)vm->vm_daddr)
130 	return EINVAL;
131     if (((caddr_t)args->dsend - (caddr_t)vm->vm_daddr)
132 	> p->p_rlimit[RLIMIT_DATA].rlim_cur)
133 	return ENOMEM;
134 
135     old = round_page((vm_offset_t)vm->vm_daddr) + ctob(vm->vm_dsize);
136     new = round_page((vm_offset_t)args->dsend);
137     p->p_retval[0] = old;
138     if ((new-old) > 0) {
139 	if (swap_pager_full)
140 	    return ENOMEM;
141 	error = vm_map_find(&vm->vm_map, NULL, 0, &old, (new-old), FALSE,
142 			VM_PROT_ALL, VM_PROT_ALL, 0);
143 	if (error)
144 	    return error;
145 	vm->vm_dsize += btoc((new-old));
146 	p->p_retval[0] = (int)(vm->vm_daddr + ctob(vm->vm_dsize));
147     }
148     return 0;
149 #else
150     struct vmspace *vm = p->p_vmspace;
151     vm_offset_t new, old;
152     struct obreak_args /* {
153 	char * nsize;
154     } */ tmp;
155 
156 #ifdef DEBUG
157     printf("Linux-emul(%ld): brk(%p)\n", (long)p->p_pid, (void *)args->dsend);
158 #endif
159     old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
160     new = (vm_offset_t)args->dsend;
161     tmp.nsize = (char *) new;
162     if (((caddr_t)new > vm->vm_daddr) && !obreak(p, &tmp))
163 	p->p_retval[0] = (int)new;
164     else
165 	p->p_retval[0] = (int)old;
166 
167     return 0;
168 #endif
169 }
170 
171 int
172 linux_uselib(struct proc *p, struct linux_uselib_args *args)
173 {
174     struct nameidata ni;
175     struct vnode *vp;
176     struct exec *a_out;
177     struct vattr attr;
178     vm_offset_t vmaddr;
179     unsigned long file_offset;
180     vm_offset_t buffer;
181     unsigned long bss_size;
182     int error;
183     caddr_t sg;
184     int locked;
185 
186     sg = stackgap_init();
187     CHECKALTEXIST(p, &sg, args->library);
188 
189 #ifdef DEBUG
190     printf("Linux-emul(%ld): uselib(%s)\n", (long)p->p_pid, args->library);
191 #endif
192 
193     a_out = NULL;
194     locked = 0;
195     vp = NULL;
196 
197     NDINIT(&ni, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, args->library, p);
198     error = namei(&ni);
199     if (error)
200 	goto cleanup;
201 
202     vp = ni.ni_vp;
203     /*
204      * XXX This looks like a bogus check - a LOCKLEAF namei should not succeed
205      * without returning a vnode.
206      */
207     if (vp == NULL) {
208 	error = ENOEXEC;	/* ?? */
209 	goto cleanup;
210     }
211     NDFREE(&ni, NDF_ONLY_PNBUF);
212 
213     /*
214      * From here on down, we have a locked vnode that must be unlocked.
215      */
216     locked++;
217 
218     /*
219      * Writable?
220      */
221     if (vp->v_writecount) {
222 	error = ETXTBSY;
223 	goto cleanup;
224     }
225 
226     /*
227      * Executable?
228      */
229     error = VOP_GETATTR(vp, &attr, p->p_ucred, p);
230     if (error)
231 	goto cleanup;
232 
233     if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
234 	((attr.va_mode & 0111) == 0) ||
235 	(attr.va_type != VREG)) {
236 	    error = ENOEXEC;
237 	    goto cleanup;
238     }
239 
240     /*
241      * Sensible size?
242      */
243     if (attr.va_size == 0) {
244 	error = ENOEXEC;
245 	goto cleanup;
246     }
247 
248     /*
249      * Can we access it?
250      */
251     error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
252     if (error)
253 	goto cleanup;
254 
255     error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
256     if (error)
257 	goto cleanup;
258 
259     /*
260      * Lock no longer needed
261      */
262     VOP_UNLOCK(vp, 0, p);
263     locked = 0;
264 
265     /*
266      * Pull in executable header into kernel_map
267      */
268     error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
269 	    	    VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
270     if (error)
271 	goto cleanup;
272 
273     /*
274      * Is it a Linux binary ?
275      */
276     if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
277 	error = ENOEXEC;
278 	goto cleanup;
279     }
280 
281     /* While we are here, we should REALLY do some more checks */
282 
283     /*
284      * Set file/virtual offset based on a.out variant.
285      */
286     switch ((int)(a_out->a_magic & 0xffff)) {
287     case 0413:	/* ZMAGIC */
288 	file_offset = 1024;
289 	break;
290     case 0314:	/* QMAGIC */
291 	file_offset = 0;
292 	break;
293     default:
294 	error = ENOEXEC;
295 	goto cleanup;
296     }
297 
298     bss_size = round_page(a_out->a_bss);
299 
300     /*
301      * Check various fields in header for validity/bounds.
302      */
303     if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
304 	error = ENOEXEC;
305 	goto cleanup;
306     }
307 
308     /* text + data can't exceed file size */
309     if (a_out->a_data + a_out->a_text > attr.va_size) {
310 	error = EFAULT;
311 	goto cleanup;
312     }
313 
314     /*
315      * text/data/bss must not exceed limits
316      * XXX: this is not complete. it should check current usage PLUS
317      * the resources needed by this library.
318      */
319     if (a_out->a_text > MAXTSIZ ||
320 	a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
321 	error = ENOMEM;
322 	goto cleanup;
323     }
324 
325     /*
326      * prevent more writers
327      */
328     vp->v_flag |= VTEXT;
329 
330     /*
331      * Check if file_offset page aligned,.
332      * Currently we cannot handle misalinged file offsets,
333      * and so we read in the entire image (what a waste).
334      */
335     if (file_offset & PAGE_MASK) {
336 #ifdef DEBUG
337 printf("uselib: Non page aligned binary %lu\n", file_offset);
338 #endif
339 	/*
340 	 * Map text+data read/write/execute
341 	 */
342 
343 	/* a_entry is the load address and is page aligned */
344 	vmaddr = trunc_page(a_out->a_entry);
345 
346 	/* get anon user mapping, read+write+execute */
347 	error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &vmaddr,
348 		    	    a_out->a_text + a_out->a_data, FALSE,
349 			    VM_PROT_ALL, VM_PROT_ALL, 0);
350 	if (error)
351 	    goto cleanup;
352 
353 	/* map file into kernel_map */
354 	error = vm_mmap(kernel_map, &buffer,
355 			round_page(a_out->a_text + a_out->a_data + file_offset),
356 		   	VM_PROT_READ, VM_PROT_READ, 0,
357 			(caddr_t)vp, trunc_page(file_offset));
358 	if (error)
359 	    goto cleanup;
360 
361 	/* copy from kernel VM space to user space */
362 	error = copyout((caddr_t)(void *)(uintptr_t)(buffer + file_offset),
363 			(caddr_t)vmaddr, a_out->a_text + a_out->a_data);
364 
365 	/* release temporary kernel space */
366 	vm_map_remove(kernel_map, buffer,
367 		      buffer + round_page(a_out->a_text + a_out->a_data + file_offset));
368 
369 	if (error)
370 	    goto cleanup;
371     }
372     else {
373 #ifdef DEBUG
374 printf("uselib: Page aligned binary %lu\n", file_offset);
375 #endif
376 	/*
377 	 * for QMAGIC, a_entry is 20 bytes beyond the load address
378 	 * to skip the executable header
379 	 */
380 	vmaddr = trunc_page(a_out->a_entry);
381 
382 	/*
383 	 * Map it all into the process's space as a single copy-on-write
384 	 * "data" segment.
385 	 */
386 	error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
387 		   	a_out->a_text + a_out->a_data,
388 			VM_PROT_ALL, VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED,
389 			(caddr_t)vp, file_offset);
390 	if (error)
391 	    goto cleanup;
392     }
393 #ifdef DEBUG
394 printf("mem=%08x = %08x %08x\n", vmaddr, ((int*)vmaddr)[0], ((int*)vmaddr)[1]);
395 #endif
396     if (bss_size != 0) {
397         /*
398 	 * Calculate BSS start address
399 	 */
400 	vmaddr = trunc_page(a_out->a_entry) + a_out->a_text + a_out->a_data;
401 
402 	/*
403 	 * allocate some 'anon' space
404 	 */
405 	error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &vmaddr,
406 			    bss_size, FALSE,
407 			    VM_PROT_ALL, VM_PROT_ALL, 0);
408 	if (error)
409 	    goto cleanup;
410     }
411 
412 cleanup:
413     /*
414      * Unlock vnode if needed
415      */
416     if (locked)
417 	VOP_UNLOCK(vp, 0, p);
418 
419     /*
420      * Release the kernel mapping.
421      */
422     if (a_out)
423 	vm_map_remove(kernel_map, (vm_offset_t)a_out, (vm_offset_t)a_out + PAGE_SIZE);
424 
425     return error;
426 }
427 
428 int
429 linux_newselect(struct proc *p, struct linux_newselect_args *args)
430 {
431     struct select_args bsa;
432     struct timeval tv0, tv1, utv, *tvp;
433     caddr_t sg;
434     int error;
435 
436 #ifdef DEBUG
437     printf("Linux-emul(%ld): newselect(%d, %p, %p, %p, %p)\n",
438   	(long)p->p_pid, args->nfds, (void *)args->readfds,
439 	(void *)args->writefds, (void *)args->exceptfds,
440 	(void *)args->timeout);
441 #endif
442     error = 0;
443     bsa.nd = args->nfds;
444     bsa.in = args->readfds;
445     bsa.ou = args->writefds;
446     bsa.ex = args->exceptfds;
447     bsa.tv = args->timeout;
448 
449     /*
450      * Store current time for computation of the amount of
451      * time left.
452      */
453     if (args->timeout) {
454 	if ((error = copyin(args->timeout, &utv, sizeof(utv))))
455 	    goto select_out;
456 #ifdef DEBUG
457 	printf("Linux-emul(%ld): incoming timeout (%ld/%ld)\n",
458 	    (long)p->p_pid, utv.tv_sec, utv.tv_usec);
459 #endif
460 	if (itimerfix(&utv)) {
461 	    /*
462 	     * The timeval was invalid.  Convert it to something
463 	     * valid that will act as it does under Linux.
464 	     */
465 	    sg = stackgap_init();
466 	    tvp = stackgap_alloc(&sg, sizeof(utv));
467 	    utv.tv_sec += utv.tv_usec / 1000000;
468 	    utv.tv_usec %= 1000000;
469 	    if (utv.tv_usec < 0) {
470 		utv.tv_sec -= 1;
471 		utv.tv_usec += 1000000;
472 	    }
473 	    if (utv.tv_sec < 0)
474 		timevalclear(&utv);
475 	    if ((error = copyout(&utv, tvp, sizeof(utv))))
476 		goto select_out;
477 	    bsa.tv = tvp;
478 	}
479 	microtime(&tv0);
480     }
481 
482     error = select(p, &bsa);
483 #ifdef DEBUG
484     printf("Linux-emul(%ld): real select returns %d\n", (long)p->p_pid, error);
485 #endif
486 
487     if (error) {
488 	/*
489 	 * See fs/select.c in the Linux kernel.  Without this,
490 	 * Maelstrom doesn't work.
491 	 */
492 	if (error == ERESTART)
493 	    error = EINTR;
494 	goto select_out;
495     }
496 
497     if (args->timeout) {
498 	if (p->p_retval[0]) {
499 	    /*
500 	     * Compute how much time was left of the timeout,
501 	     * by subtracting the current time and the time
502 	     * before we started the call, and subtracting
503 	     * that result from the user-supplied value.
504 	     */
505 	    microtime(&tv1);
506 	    timevalsub(&tv1, &tv0);
507 	    timevalsub(&utv, &tv1);
508 	    if (utv.tv_sec < 0)
509 		timevalclear(&utv);
510 	} else
511 	    timevalclear(&utv);
512 #ifdef DEBUG
513 	printf("Linux-emul(%ld): outgoing timeout (%ld/%ld)\n",
514 	    (long)p->p_pid, utv.tv_sec, utv.tv_usec);
515 #endif
516 	if ((error = copyout(&utv, args->timeout, sizeof(utv))))
517 	    goto select_out;
518     }
519 
520 select_out:
521 #ifdef DEBUG
522     printf("Linux-emul(%ld): newselect_out -> %d\n", (long)p->p_pid, error);
523 #endif
524     return error;
525 }
526 
527 int
528 linux_getpgid(struct proc *p, struct linux_getpgid_args *args)
529 {
530     struct proc *curp;
531 
532 #ifdef DEBUG
533     printf("Linux-emul(%ld): getpgid(%d)\n", (long)p->p_pid, args->pid);
534 #endif
535     if (args->pid != p->p_pid) {
536 	if (!(curp = pfind(args->pid)))
537 	    return ESRCH;
538     }
539     else
540 	curp = p;
541     p->p_retval[0] = curp->p_pgid;
542     return 0;
543 }
544 
545 int
546 linux_mremap(struct proc *p, struct linux_mremap_args *args)
547 {
548 	struct munmap_args /* {
549 		void *addr;
550 		size_t len;
551 	} */ bsd_args;
552 	int error = 0;
553 
554 #ifdef DEBUG
555 	printf("Linux-emul(%ld): mremap(%p, %08x, %08x, %08x)\n",
556 	    (long)p->p_pid, (void *)args->addr, args->old_len, args->new_len,
557 	    args->flags);
558 #endif
559 	args->new_len = round_page(args->new_len);
560 	args->old_len = round_page(args->old_len);
561 
562 	if (args->new_len > args->old_len) {
563 		p->p_retval[0] = 0;
564 		return ENOMEM;
565 	}
566 
567 	if (args->new_len < args->old_len) {
568 		bsd_args.addr = args->addr + args->new_len;
569 		bsd_args.len = args->old_len - args->new_len;
570 		error = munmap(p, &bsd_args);
571 	}
572 
573 	p->p_retval[0] = error ? 0 : (int)args->addr;
574 	return error;
575 }
576 
577 int
578 linux_msync(struct proc *p, struct linux_msync_args *args)
579 {
580 	struct msync_args bsd_args;
581 
582 	bsd_args.addr = args->addr;
583 	bsd_args.len = args->len;
584 	bsd_args.flags = 0;	/* XXX ignore */
585 
586 	return msync(p, &bsd_args);
587 }
588 
589 int
590 linux_time(struct proc *p, struct linux_time_args *args)
591 {
592     struct timeval tv;
593     linux_time_t tm;
594     int error;
595 
596 #ifdef DEBUG
597     printf("Linux-emul(%ld): time(*)\n", (long)p->p_pid);
598 #endif
599     microtime(&tv);
600     tm = tv.tv_sec;
601     if (args->tm && (error = copyout(&tm, args->tm, sizeof(linux_time_t))))
602 	return error;
603     p->p_retval[0] = tm;
604     return 0;
605 }
606 
607 struct linux_times_argv {
608     long    tms_utime;
609     long    tms_stime;
610     long    tms_cutime;
611     long    tms_cstime;
612 };
613 
614 #define CLK_TCK 100	/* Linux uses 100 */
615 #define CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
616 
617 int
618 linux_times(struct proc *p, struct linux_times_args *args)
619 {
620     struct timeval tv;
621     struct linux_times_argv tms;
622     struct rusage ru;
623     int error;
624 
625 #ifdef DEBUG
626     printf("Linux-emul(%ld): times(*)\n", (long)p->p_pid);
627 #endif
628     calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
629 
630     tms.tms_utime = CONVTCK(ru.ru_utime);
631     tms.tms_stime = CONVTCK(ru.ru_stime);
632 
633     tms.tms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
634     tms.tms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
635 
636     if ((error = copyout((caddr_t)&tms, (caddr_t)args->buf,
637 	    	    sizeof(struct linux_times_argv))))
638 	return error;
639 
640     microuptime(&tv);
641     p->p_retval[0] = (int)CONVTCK(tv);
642     return 0;
643 }
644 
645 int
646 linux_newuname(struct proc *p, struct linux_newuname_args *args)
647 {
648 	struct linux_new_utsname utsname;
649 	char *osrelease, *osname;
650 
651 #ifdef DEBUG
652 	printf("Linux-emul(%ld): newuname(*)\n", (long)p->p_pid);
653 #endif
654 
655 	osname = linux_get_osname(p);
656 	osrelease = linux_get_osrelease(p);
657 
658 	bzero(&utsname, sizeof(struct linux_new_utsname));
659 	strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
660 	strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
661 	strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
662 	strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
663 	strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
664 	strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
665 
666 	return (copyout((caddr_t)&utsname, (caddr_t)args->buf,
667 			sizeof(struct linux_new_utsname)));
668 }
669 
670 struct linux_utimbuf {
671 	linux_time_t l_actime;
672 	linux_time_t l_modtime;
673 };
674 
675 int
676 linux_utime(struct proc *p, struct linux_utime_args *args)
677 {
678     struct utimes_args /* {
679 	char	*path;
680 	struct	timeval *tptr;
681     } */ bsdutimes;
682     struct timeval tv[2], *tvp;
683     struct linux_utimbuf lut;
684     int error;
685     caddr_t sg;
686 
687     sg = stackgap_init();
688     CHECKALTEXIST(p, &sg, args->fname);
689 
690 #ifdef DEBUG
691     printf("Linux-emul(%ld): utime(%s, *)\n", (long)p->p_pid, args->fname);
692 #endif
693     if (args->times) {
694 	if ((error = copyin(args->times, &lut, sizeof lut)))
695 	    return error;
696 	tv[0].tv_sec = lut.l_actime;
697 	tv[0].tv_usec = 0;
698 	tv[1].tv_sec = lut.l_modtime;
699 	tv[1].tv_usec = 0;
700 	/* so that utimes can copyin */
701 	tvp = (struct timeval *)stackgap_alloc(&sg, sizeof(tv));
702 	if (tvp == NULL)
703 		return (ENAMETOOLONG);
704 	if ((error = copyout(tv, tvp, sizeof(tv))))
705 	    return error;
706 	bsdutimes.tptr = tvp;
707     } else
708 	bsdutimes.tptr = NULL;
709 
710     bsdutimes.path = args->fname;
711     return utimes(p, &bsdutimes);
712 }
713 
714 #define __WCLONE 0x80000000
715 
716 int
717 linux_waitpid(struct proc *p, struct linux_waitpid_args *args)
718 {
719     struct wait_args /* {
720 	int pid;
721 	int *status;
722 	int options;
723 	struct	rusage *rusage;
724     } */ tmp;
725     int error, tmpstat;
726 
727 #ifdef DEBUG
728     printf("Linux-emul(%ld): waitpid(%d, %p, %d)\n",
729 	(long)p->p_pid, args->pid, (void *)args->status, args->options);
730 #endif
731     tmp.pid = args->pid;
732     tmp.status = args->status;
733     tmp.options = (args->options & (WNOHANG | WUNTRACED));
734     /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
735     if (args->options & __WCLONE)
736 	tmp.options |= WLINUXCLONE;
737     tmp.rusage = NULL;
738 
739     if ((error = wait4(p, &tmp)) != 0)
740 	return error;
741 
742     if (args->status) {
743 	if ((error = copyin(args->status, &tmpstat, sizeof(int))) != 0)
744 	    return error;
745 	tmpstat &= 0xffff;
746 	if (WIFSIGNALED(tmpstat))
747 	    tmpstat = (tmpstat & 0xffffff80) |
748 		      BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
749 	else if (WIFSTOPPED(tmpstat))
750 	    tmpstat = (tmpstat & 0xffff00ff) |
751 		      (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
752 	return copyout(&tmpstat, args->status, sizeof(int));
753     } else
754 	return 0;
755 }
756 
757 int
758 linux_wait4(struct proc *p, struct linux_wait4_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     printf("Linux-emul(%ld): wait4(%d, %p, %d, %p)\n",
770 	(long)p->p_pid, args->pid, (void *)args->status, args->options,
771 	(void *)args->rusage);
772 #endif
773     tmp.pid = args->pid;
774     tmp.status = args->status;
775     tmp.options = (args->options & (WNOHANG | WUNTRACED));
776     /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
777     if (args->options & __WCLONE)
778 	tmp.options |= WLINUXCLONE;
779     tmp.rusage = args->rusage;
780 
781     if ((error = wait4(p, &tmp)) != 0)
782 	return error;
783 
784     SIGDELSET(p->p_siglist, SIGCHLD);
785 
786     if (args->status) {
787 	if ((error = copyin(args->status, &tmpstat, sizeof(int))) != 0)
788 	    return error;
789 	tmpstat &= 0xffff;
790 	if (WIFSIGNALED(tmpstat))
791 	    tmpstat = (tmpstat & 0xffffff80) |
792 		  BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
793 	else if (WIFSTOPPED(tmpstat))
794 	    tmpstat = (tmpstat & 0xffff00ff) |
795 		  (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
796 	return copyout(&tmpstat, args->status, sizeof(int));
797     } else
798 	return 0;
799 }
800 
801 int
802 linux_mknod(struct proc *p, struct linux_mknod_args *args)
803 {
804 	caddr_t sg;
805 	struct mknod_args bsd_mknod;
806 	struct mkfifo_args bsd_mkfifo;
807 
808 	sg = stackgap_init();
809 
810 	CHECKALTCREAT(p, &sg, args->path);
811 
812 #ifdef DEBUG
813 	printf("Linux-emul(%ld): mknod(%s, %d, %d)\n",
814 	   (long)p->p_pid, args->path, args->mode, args->dev);
815 #endif
816 
817 	if (args->mode & S_IFIFO) {
818 		bsd_mkfifo.path = args->path;
819 		bsd_mkfifo.mode = args->mode;
820 		return mkfifo(p, &bsd_mkfifo);
821 	} else {
822 		bsd_mknod.path = args->path;
823 		bsd_mknod.mode = args->mode;
824 		bsd_mknod.dev = args->dev;
825 		return mknod(p, &bsd_mknod);
826 	}
827 }
828 
829 /*
830  * UGH! This is just about the dumbest idea I've ever heard!!
831  */
832 int
833 linux_personality(struct proc *p, struct linux_personality_args *args)
834 {
835 #ifdef DEBUG
836 	printf("Linux-emul(%ld): personality(%d)\n",
837 	   (long)p->p_pid, args->per);
838 #endif
839 	if (args->per != 0)
840 		return EINVAL;
841 
842 	/* Yes Jim, it's still a Linux... */
843 	p->p_retval[0] = 0;
844 	return 0;
845 }
846 
847 /*
848  * Wrappers for get/setitimer for debugging..
849  */
850 int
851 linux_setitimer(struct proc *p, struct linux_setitimer_args *args)
852 {
853 	struct setitimer_args bsa;
854 	struct itimerval foo;
855 	int error;
856 
857 #ifdef DEBUG
858 	printf("Linux-emul(%ld): setitimer(%p, %p)\n",
859 	    (long)p->p_pid, (void *)args->itv, (void *)args->oitv);
860 #endif
861 	bsa.which = args->which;
862 	bsa.itv = args->itv;
863 	bsa.oitv = args->oitv;
864 	if (args->itv) {
865 	    if ((error = copyin((caddr_t)args->itv, (caddr_t)&foo,
866 			sizeof(foo))))
867 		return error;
868 #ifdef DEBUG
869 	    printf("setitimer: value: sec: %ld, usec: %ld\n",
870 		foo.it_value.tv_sec, foo.it_value.tv_usec);
871 	    printf("setitimer: interval: sec: %ld, usec: %ld\n",
872 		foo.it_interval.tv_sec, foo.it_interval.tv_usec);
873 #endif
874 	}
875 	return setitimer(p, &bsa);
876 }
877 
878 int
879 linux_getitimer(struct proc *p, struct linux_getitimer_args *args)
880 {
881 	struct getitimer_args bsa;
882 #ifdef DEBUG
883 	printf("Linux-emul(%ld): getitimer(%p)\n",
884 	    (long)p->p_pid, (void *)args->itv);
885 #endif
886 	bsa.which = args->which;
887 	bsa.itv = args->itv;
888 	return getitimer(p, &bsa);
889 }
890 
891 int
892 linux_nice(struct proc *p, struct linux_nice_args *args)
893 {
894 	struct setpriority_args	bsd_args;
895 
896 	bsd_args.which = PRIO_PROCESS;
897 	bsd_args.who = 0;	/* current process */
898 	bsd_args.prio = args->inc;
899 	return setpriority(p, &bsd_args);
900 }
901 
902 int
903 linux_setgroups(p, uap)
904 	struct proc *p;
905 	struct linux_setgroups_args *uap;
906 {
907 	struct pcred *pc;
908 	linux_gid_t linux_gidset[NGROUPS];
909 	gid_t *bsd_gidset;
910 	int ngrp, error;
911 
912 	pc = p->p_cred;
913 	ngrp = uap->gidsetsize;
914 
915 	/*
916 	 * cr_groups[0] holds egid. Setting the whole set from
917 	 * the supplied set will cause egid to be changed too.
918 	 * Keep cr_groups[0] unchanged to prevent that.
919 	 */
920 
921 	if ((error = suser(p)) != 0)
922 		return (error);
923 
924 	if (ngrp >= NGROUPS)
925 		return (EINVAL);
926 
927 	pc->pc_ucred = crcopy(pc->pc_ucred);
928 	if (ngrp > 0) {
929 		error = copyin((caddr_t)uap->gidset, (caddr_t)linux_gidset,
930 			       ngrp * sizeof(linux_gid_t));
931 		if (error)
932 			return (error);
933 
934 		pc->pc_ucred->cr_ngroups = ngrp + 1;
935 
936 		bsd_gidset = pc->pc_ucred->cr_groups;
937 		ngrp--;
938 		while (ngrp >= 0) {
939 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
940 			ngrp--;
941 		}
942 	}
943 	else
944 		pc->pc_ucred->cr_ngroups = 1;
945 
946 	setsugid(p);
947 	return (0);
948 }
949 
950 int
951 linux_getgroups(p, uap)
952 	struct proc *p;
953 	struct linux_getgroups_args *uap;
954 {
955 	struct pcred *pc;
956 	linux_gid_t linux_gidset[NGROUPS];
957 	gid_t *bsd_gidset;
958 	int bsd_gidsetsz, ngrp, error;
959 
960 	pc = p->p_cred;
961 	bsd_gidset = pc->pc_ucred->cr_groups;
962 	bsd_gidsetsz = pc->pc_ucred->cr_ngroups - 1;
963 
964 	/*
965 	 * cr_groups[0] holds egid. Returning the whole set
966 	 * here will cause a duplicate. Exclude cr_groups[0]
967 	 * to prevent that.
968 	 */
969 
970 	if ((ngrp = uap->gidsetsize) == 0) {
971 		p->p_retval[0] = bsd_gidsetsz;
972 		return (0);
973 	}
974 
975 	if (ngrp < bsd_gidsetsz)
976 		return (EINVAL);
977 
978 	ngrp = 0;
979 	while (ngrp < bsd_gidsetsz) {
980 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
981 		ngrp++;
982 	}
983 
984 	if ((error = copyout((caddr_t)linux_gidset, (caddr_t)uap->gidset,
985 	    ngrp * sizeof(linux_gid_t))))
986 		return (error);
987 
988 	p->p_retval[0] = ngrp;
989 	return (0);
990 }
991 
992 int
993 linux_setrlimit(p, uap)
994 	struct proc *p;
995 	struct linux_setrlimit_args *uap;
996 {
997 	struct __setrlimit_args bsd;
998 	struct linux_rlimit rlim;
999 	int error;
1000 	caddr_t sg = stackgap_init();
1001 
1002 #ifdef DEBUG
1003 	printf("Linux-emul(%ld): setrlimit(%d, %p)\n", (long)p->p_pid,
1004 	    uap->resource, (void *)uap->rlim);
1005 #endif
1006 
1007 	if (uap->resource >= LINUX_RLIM_NLIMITS)
1008 		return (EINVAL);
1009 
1010 	bsd.which = linux_to_bsd_resource[uap->resource];
1011 	if (bsd.which == -1)
1012 		return (EINVAL);
1013 
1014 	error = copyin(uap->rlim, &rlim, sizeof(rlim));
1015 	if (error)
1016 		return (error);
1017 
1018 	bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1019 	bsd.rlp->rlim_cur = (rlim_t)rlim.rlim_cur;
1020 	bsd.rlp->rlim_max = (rlim_t)rlim.rlim_max;
1021 	return (setrlimit(p, &bsd));
1022 }
1023 
1024 int
1025 linux_getrlimit(p, uap)
1026 	struct proc *p;
1027 	struct linux_getrlimit_args *uap;
1028 {
1029 	struct __getrlimit_args bsd;
1030 	struct linux_rlimit rlim;
1031 	int error;
1032 	caddr_t sg = stackgap_init();
1033 
1034 #ifdef DEBUG
1035 	printf("Linux-emul(%ld): getrlimit(%d, %p)\n", (long)p->p_pid,
1036 	    uap->resource, (void *)uap->rlim);
1037 #endif
1038 
1039 	if (uap->resource >= LINUX_RLIM_NLIMITS)
1040 		return (EINVAL);
1041 
1042 	bsd.which = linux_to_bsd_resource[uap->resource];
1043 	if (bsd.which == -1)
1044 		return (EINVAL);
1045 
1046 	bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1047 	error = getrlimit(p, &bsd);
1048 	if (error)
1049 		return (error);
1050 
1051 	rlim.rlim_cur = (unsigned long)bsd.rlp->rlim_cur;
1052 	if (rlim.rlim_cur == ULONG_MAX)
1053 		rlim.rlim_cur = LONG_MAX;
1054 	rlim.rlim_max = (unsigned long)bsd.rlp->rlim_max;
1055 	if (rlim.rlim_max == ULONG_MAX)
1056 		rlim.rlim_max = LONG_MAX;
1057 	return (copyout(&rlim, uap->rlim, sizeof(rlim)));
1058 }
1059 
1060 int
1061 linux_sched_setscheduler(p, uap)
1062 	struct proc *p;
1063 	struct linux_sched_setscheduler_args *uap;
1064 {
1065 	struct sched_setscheduler_args bsd;
1066 
1067 #ifdef DEBUG
1068 	printf("Linux-emul(%ld): sched_setscheduler(%d, %d, %p)\n",
1069 	    (long)p->p_pid, uap->pid, uap->policy, (const void *)uap->param);
1070 #endif
1071 
1072 	switch (uap->policy) {
1073 	case LINUX_SCHED_OTHER:
1074 		bsd.policy = SCHED_OTHER;
1075 		break;
1076 	case LINUX_SCHED_FIFO:
1077 		bsd.policy = SCHED_FIFO;
1078 		break;
1079 	case LINUX_SCHED_RR:
1080 		bsd.policy = SCHED_RR;
1081 		break;
1082 	default:
1083 		return EINVAL;
1084 	}
1085 
1086 	bsd.pid = uap->pid;
1087 	bsd.param = uap->param;
1088 	return sched_setscheduler(p, &bsd);
1089 }
1090 
1091 int
1092 linux_sched_getscheduler(p, uap)
1093 	struct proc *p;
1094 	struct linux_sched_getscheduler_args *uap;
1095 {
1096 	struct sched_getscheduler_args bsd;
1097 	int error;
1098 
1099 #ifdef DEBUG
1100 	printf("Linux-emul(%ld): sched_getscheduler(%d)\n",
1101 	       (long)p->p_pid, uap->pid);
1102 #endif
1103 
1104 	bsd.pid = uap->pid;
1105 	error = sched_getscheduler(p, &bsd);
1106 
1107 	switch (p->p_retval[0]) {
1108 	case SCHED_OTHER:
1109 		p->p_retval[0] = LINUX_SCHED_OTHER;
1110 		break;
1111 	case SCHED_FIFO:
1112 		p->p_retval[0] = LINUX_SCHED_FIFO;
1113 		break;
1114 	case SCHED_RR:
1115 		p->p_retval[0] = LINUX_SCHED_RR;
1116 		break;
1117 	}
1118 
1119 	return error;
1120 }
1121