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