4aa734767af0c882ce8ccdf01b2a551d5477d33e
[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.18 2004/01/07 10:54:57 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/namei.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                 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
195         if (it.it_value.tv_sec != 0) {
196                 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
197                     tvtohz_high(&it.it_value));
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 nameidata ni;
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         NDINIT(&ni, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF,
273                 UIO_SYSSPACE, path, td);
274         error = namei(&ni);
275         if (error)
276                 goto cleanup;
277
278         vp = ni.ni_vp;
279         /*
280          * XXX - This looks like a bogus check. A LOCKLEAF namei should not
281          * succeed without returning a vnode.
282          */
283         if (vp == NULL) {
284                 error = ENOEXEC;        /* ?? */
285                 goto cleanup;
286         }
287         NDFREE(&ni, NDF_ONLY_PNBUF);
288
289         /*
290          * From here on down, we have a locked vnode that must be unlocked.
291          */
292         locked++;
293
294         /* Writable? */
295         if (vp->v_writecount) {
296                 error = ETXTBSY;
297                 goto cleanup;
298         }
299
300         /* Executable? */
301         error = VOP_GETATTR(vp, &attr, td);
302         if (error)
303                 goto cleanup;
304
305         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
306             ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
307                 error = ENOEXEC;
308                 goto cleanup;
309         }
310
311         /* Sensible size? */
312         if (attr.va_size == 0) {
313                 error = ENOEXEC;
314                 goto cleanup;
315         }
316
317         /* Can we access it? */
318         error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
319         if (error)
320                 goto cleanup;
321
322         error = VOP_OPEN(vp, FREAD, p->p_ucred, td);
323         if (error)
324                 goto cleanup;
325
326         /*
327          * Lock no longer needed
328          */
329         VOP_UNLOCK(vp, 0, td);
330         locked = 0;
331
332         /* Pull in executable header into kernel_map */
333         error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
334             VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
335         if (error)
336                 goto cleanup;
337
338         /* Is it a Linux binary ? */
339         if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
340                 error = ENOEXEC;
341                 goto cleanup;
342         }
343
344         /*
345          * While we are here, we should REALLY do some more checks
346          */
347
348         /* Set file/virtual offset based on a.out variant. */
349         switch ((int)(a_out->a_magic & 0xffff)) {
350         case 0413:      /* ZMAGIC */
351                 file_offset = 1024;
352                 break;
353         case 0314:      /* QMAGIC */
354                 file_offset = 0;
355                 break;
356         default:
357                 error = ENOEXEC;
358                 goto cleanup;
359         }
360
361         bss_size = round_page(a_out->a_bss);
362
363         /* Check various fields in header for validity/bounds. */
364         if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
365                 error = ENOEXEC;
366                 goto cleanup;
367         }
368
369         /* text + data can't exceed file size */
370         if (a_out->a_data + a_out->a_text > attr.va_size) {
371                 error = EFAULT;
372                 goto cleanup;
373         }
374
375         /*
376          * text/data/bss must not exceed limits
377          * XXX - this is not complete. it should check current usage PLUS
378          * the resources needed by this library.
379          */
380         if (a_out->a_text > maxtsiz ||
381             a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
382                 error = ENOMEM;
383                 goto cleanup;
384         }
385
386         /* prevent more writers */
387         vp->v_flag |= VTEXT;
388
389         /*
390          * Check if file_offset page aligned. Currently we cannot handle
391          * misalinged file offsets, and so we read in the entire image
392          * (what a waste).
393          */
394         if (file_offset & PAGE_MASK) {
395 #ifdef DEBUG
396                 printf("uselib: Non page aligned binary %lu\n", file_offset);
397 #endif
398                 /* Map text+data read/write/execute */
399
400                 /* a_entry is the load address and is page aligned */
401                 vmaddr = trunc_page(a_out->a_entry);
402
403                 /* get anon user mapping, read+write+execute */
404                 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
405                     &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
406                     VM_PROT_ALL, 0);
407                 if (error)
408                         goto cleanup;
409
410                 /* map file into kernel_map */
411                 error = vm_mmap(kernel_map, &buffer,
412                     round_page(a_out->a_text + a_out->a_data + file_offset),
413                     VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
414                     trunc_page(file_offset));
415                 if (error)
416                         goto cleanup;
417
418                 /* copy from kernel VM space to user space */
419                 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
420                     (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
421
422                 /* release temporary kernel space */
423                 vm_map_remove(kernel_map, buffer, buffer +
424                     round_page(a_out->a_text + a_out->a_data + file_offset));
425
426                 if (error)
427                         goto cleanup;
428         } else {
429 #ifdef DEBUG
430                 printf("uselib: Page aligned binary %lu\n", file_offset);
431 #endif
432                 /*
433                  * for QMAGIC, a_entry is 20 bytes beyond the load address
434                  * to skip the executable header
435                  */
436                 vmaddr = trunc_page(a_out->a_entry);
437
438                 /*
439                  * Map it all into the process's space as a single
440                  * copy-on-write "data" segment.
441                  */
442                 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
443                     a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
444                     MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
445                 if (error)
446                         goto cleanup;
447         }
448 #ifdef DEBUG
449         printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
450             ((long*)vmaddr)[1]);
451 #endif
452         if (bss_size != 0) {
453                 /* Calculate BSS start address */
454                 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
455                     a_out->a_data;
456
457                 /* allocate some 'anon' space */
458                 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
459                     &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
460                 if (error)
461                         goto cleanup;
462         }
463
464 cleanup:
465         /* Unlock vnode if needed */
466         if (locked)
467                 VOP_UNLOCK(vp, 0, td);
468
469         /* Release the kernel mapping. */
470         if (a_out)
471                 vm_map_remove(kernel_map, (vm_offset_t)a_out,
472                     (vm_offset_t)a_out + PAGE_SIZE);
473
474         linux_free_path(&path);
475         return error;
476 }
477
478 int
479 linux_select(struct linux_select_args *args)
480 {
481         struct select_args bsa;
482         struct timeval tv0, tv1, utv, *tvp;
483         caddr_t sg;
484         int error;
485
486 #ifdef DEBUG
487         if (ldebug(select))
488                 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
489                     (void *)args->readfds, (void *)args->writefds,
490                     (void *)args->exceptfds, (void *)args->timeout);
491 #endif
492
493         error = 0;
494         bsa.sysmsg_result = 0;
495         bsa.nd = args->nfds;
496         bsa.in = args->readfds;
497         bsa.ou = args->writefds;
498         bsa.ex = args->exceptfds;
499         bsa.tv = (struct timeval *)args->timeout;
500
501         /*
502          * Store current time for computation of the amount of
503          * time left.
504          */
505         if (args->timeout) {
506                 if ((error = copyin((caddr_t)args->timeout, &utv,
507                     sizeof(utv))))
508                         goto select_out;
509 #ifdef DEBUG
510                 if (ldebug(select))
511                         printf(LMSG("incoming timeout (%ld/%ld)"),
512                             utv.tv_sec, utv.tv_usec);
513 #endif
514
515                 if (itimerfix(&utv)) {
516                         /*
517                          * The timeval was invalid.  Convert it to something
518                          * valid that will act as it does under Linux.
519                          */
520                         sg = stackgap_init();
521                         tvp = stackgap_alloc(&sg, sizeof(utv));
522                         utv.tv_sec += utv.tv_usec / 1000000;
523                         utv.tv_usec %= 1000000;
524                         if (utv.tv_usec < 0) {
525                                 utv.tv_sec -= 1;
526                                 utv.tv_usec += 1000000;
527                         }
528                         if (utv.tv_sec < 0)
529                                 timevalclear(&utv);
530                         if ((error = copyout(&utv, tvp, sizeof(utv))))
531                                 goto select_out;
532                         bsa.tv = tvp;
533                 }
534                 microtime(&tv0);
535         }
536
537         error = select(&bsa);
538         args->sysmsg_result = bsa.sysmsg_result;
539 #ifdef DEBUG
540         if (ldebug(select))
541                 printf(LMSG("real select returns %d"), error);
542 #endif
543         if (error) {
544                 /*
545                  * See fs/select.c in the Linux kernel.  Without this,
546                  * Maelstrom doesn't work.
547                  */
548                 if (error == ERESTART)
549                         error = EINTR;
550                 goto select_out;
551         }
552
553         if (args->timeout) {
554                 if (args->sysmsg_result) {
555                         /*
556                          * Compute how much time was left of the timeout,
557                          * by subtracting the current time and the time
558                          * before we started the call, and subtracting
559                          * that result from the user-supplied value.
560                          */
561                         microtime(&tv1);
562                         timevalsub(&tv1, &tv0);
563                         timevalsub(&utv, &tv1);
564                         if (utv.tv_sec < 0)
565                                 timevalclear(&utv);
566                 } else
567                         timevalclear(&utv);
568 #ifdef DEBUG
569                 if (ldebug(select))
570                         printf(LMSG("outgoing timeout (%ld/%ld)"),
571                             utv.tv_sec, utv.tv_usec);
572 #endif
573                 if ((error = copyout(&utv, (caddr_t)args->timeout,
574                     sizeof(utv))))
575                         goto select_out;
576         }
577
578 select_out:
579 #ifdef DEBUG
580         if (ldebug(select))
581                 printf(LMSG("select_out -> %d"), error);
582 #endif
583         return error;
584 }
585
586 int     
587 linux_mremap(struct linux_mremap_args *args)
588 {
589         struct munmap_args bsd_args; 
590         int error = 0;
591
592 #ifdef DEBUG
593         if (ldebug(mremap))
594                 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
595                     (void *)args->addr, 
596                     (unsigned long)args->old_len, 
597                     (unsigned long)args->new_len,
598                     (unsigned long)args->flags);
599 #endif
600         args->new_len = round_page(args->new_len);
601         args->old_len = round_page(args->old_len);
602
603         if (args->new_len > args->old_len) {
604                 args->sysmsg_result = 0;
605                 return ENOMEM;
606         }
607
608         if (args->new_len < args->old_len) {
609                 bsd_args.sysmsg_result = 0;
610                 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
611                 bsd_args.len = args->old_len - args->new_len;
612                 error = munmap(&bsd_args);
613         }
614
615         args->sysmsg_resultp = error ? NULL : (void *)args->addr;
616         return error;
617 }
618
619 #define LINUX_MS_ASYNC          0x0001
620 #define LINUX_MS_INVALIDATE     0x0002
621 #define LINUX_MS_SYNC           0x0004
622
623 int
624 linux_msync(struct linux_msync_args *args)
625 {
626         struct msync_args bsd_args;
627         int error;
628
629         bsd_args.addr = (caddr_t)args->addr;
630         bsd_args.len = args->len;
631         bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
632         bsd_args.sysmsg_result = 0;
633
634         error = msync(&bsd_args);
635         args->sysmsg_result = bsd_args.sysmsg_result;
636         return(error);
637 }
638
639 #ifndef __alpha__
640 int
641 linux_time(struct linux_time_args *args)
642 {
643         struct timeval tv;
644         l_time_t tm;
645         int error;
646
647 #ifdef DEBUG
648         if (ldebug(time))
649                 printf(ARGS(time, "*"));
650 #endif
651
652         microtime(&tv);
653         tm = tv.tv_sec;
654         if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
655                 return error;
656         args->sysmsg_lresult = tm;
657         return 0;
658 }
659 #endif  /*!__alpha__*/
660
661 struct l_times_argv {
662         l_long          tms_utime;
663         l_long          tms_stime;
664         l_long          tms_cutime;
665         l_long          tms_cstime;
666 };
667
668 #ifdef __alpha__
669 #define CLK_TCK 1024    /* Linux uses 1024 on alpha */
670 #else
671 #define CLK_TCK 100     /* Linux uses 100 */
672 #endif
673
674 #define CONVTCK(r)      (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
675
676 int
677 linux_times(struct linux_times_args *args)
678 {
679         struct thread *td = curthread;
680         struct proc *p = td->td_proc;
681         struct timeval tv;
682         struct l_times_argv tms;
683         struct rusage ru;
684         int error;
685
686         KKASSERT(p);
687 #ifdef DEBUG
688         if (ldebug(times))
689                 printf(ARGS(times, "*"));
690 #endif
691
692         calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
693
694         tms.tms_utime = CONVTCK(ru.ru_utime);
695         tms.tms_stime = CONVTCK(ru.ru_stime);
696
697         tms.tms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
698         tms.tms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
699
700         if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
701                 return error;
702
703         microuptime(&tv);
704         args->sysmsg_result = (int)CONVTCK(tv);
705         return 0;
706 }
707
708 int
709 linux_newuname(struct linux_newuname_args *args)
710 {
711         struct thread *td = curthread;
712         struct l_new_utsname utsname;
713         char *osrelease, *osname;
714
715 #ifdef DEBUG
716         if (ldebug(newuname))
717                 printf(ARGS(newuname, "*"));
718 #endif
719
720         osname = linux_get_osname(td);
721         osrelease = linux_get_osrelease(td);
722
723         bzero(&utsname, sizeof(utsname));
724         strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
725         strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
726         strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
727         strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
728         strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
729         strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
730
731         return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
732 }
733
734 #if defined(__i386__)
735 struct l_utimbuf {
736         l_time_t l_actime;
737         l_time_t l_modtime;
738 };
739
740 int
741 linux_utime(struct linux_utime_args *args)
742 {
743         struct thread *td = curthread;
744         struct timeval tv[2];
745         struct l_utimbuf lut;
746         struct nameidata nd;
747         char *path;
748         int error;
749
750         error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
751         if (error)
752                 return (error);
753 #ifdef DEBUG
754         if (ldebug(utime))
755                 printf(ARGS(utime, "%s, *"), path);
756 #endif
757
758         if (args->times) {
759                 error = copyin(args->times, &lut, sizeof(lut));
760                 if (error)
761                         goto cleanup;
762                 tv[0].tv_sec = lut.l_actime;
763                 tv[0].tv_usec = 0;
764                 tv[1].tv_sec = lut.l_modtime;
765                 tv[1].tv_usec = 0;
766         }
767         NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_SYSSPACE, path, td);
768
769         error = kern_utimes(&nd, args->times ? tv : NULL);
770
771 cleanup:
772         linux_free_path(&path);
773         return (error);
774 }
775 #endif /* __i386__ */
776
777 #define __WCLONE 0x80000000
778
779 #ifndef __alpha__
780 int
781 linux_waitpid(struct linux_waitpid_args *args)
782 {
783         int error, options, status;
784
785 #ifdef DEBUG
786         if (ldebug(waitpid))
787                 printf(ARGS(waitpid, "%d, %p, %d"),
788                     args->pid, (void *)args->status, args->options);
789 #endif
790         options = args->options & (WNOHANG | WUNTRACED);
791         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
792         if (args->options & __WCLONE)
793                 options |= WLINUXCLONE;
794
795         error = kern_wait(args->pid, args->status ? &status : NULL, options,
796             NULL, &args->sysmsg_result);
797
798         if (error == 0 && args->status) {
799                 status &= 0xffff;
800                 if (WIFSIGNALED(status))
801                         status = (status & 0xffffff80) |
802                             BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
803                 else if (WIFSTOPPED(status))
804                         status = (status & 0xffff00ff) |
805                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
806                 error = copyout(&status, args->status, sizeof(status));
807         }
808
809         return (error);
810 }
811 #endif  /*!__alpha__*/
812
813 int
814 linux_wait4(struct linux_wait4_args *args)
815 {
816         struct thread *td = curthread;
817         struct proc *p = td->td_proc;
818         struct rusage rusage;
819         int error, options, status;
820
821         KKASSERT(p);
822
823 #ifdef DEBUG
824         if (ldebug(wait4))
825                 printf(ARGS(wait4, "%d, %p, %d, %p"),
826                     args->pid, (void *)args->status, args->options,
827                     (void *)args->rusage);
828 #endif
829         options = args->options & (WNOHANG | WUNTRACED);
830         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
831         if (args->options & __WCLONE)
832                 options |= WLINUXCLONE;
833
834         error = kern_wait(args->pid, args->status ? &status : NULL, options,
835             args->rusage ? &rusage : NULL, &args->sysmsg_result);
836
837         if (error == 0)
838                 SIGDELSET(p->p_siglist, SIGCHLD);
839
840         if (error == 0 && args->status) {
841                 status &= 0xffff;
842                 if (WIFSIGNALED(status))
843                         status = (status & 0xffffff80) |
844                             BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
845                 else if (WIFSTOPPED(status))
846                         status = (status & 0xffff00ff) |
847                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
848                 error = copyout(&status, args->status, sizeof(status));
849         }
850         if (error == 0 && args->rusage)
851                 error = copyout(&rusage, args->rusage, sizeof(rusage));
852
853         return (error);
854 }
855
856 int
857 linux_mknod(struct linux_mknod_args *args)
858 {
859         struct thread *td = curthread;
860         struct nameidata nd;
861         char *path;
862         int error;
863
864         error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
865         if (error)
866                 return (error);
867 #ifdef DEBUG
868         if (ldebug(mknod))
869                 printf(ARGS(mknod, "%s, %d, %d"),
870                     path, args->mode, args->dev);
871 #endif
872         NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_SYSSPACE, path, td);
873
874         if (args->mode & S_IFIFO) {
875                 error = kern_mkfifo(&nd, args->mode);
876         } else {
877                 error = kern_mknod(&nd, args->mode, args->dev);
878         }
879
880         linux_free_path(&path);
881         return(error);
882 }
883
884 /*
885  * UGH! This is just about the dumbest idea I've ever heard!!
886  */
887 int
888 linux_personality(struct linux_personality_args *args)
889 {
890 #ifdef DEBUG
891         if (ldebug(personality))
892                 printf(ARGS(personality, "%d"), args->per);
893 #endif
894 #ifndef __alpha__
895         if (args->per != 0)
896                 return EINVAL;
897 #endif
898
899         /* Yes Jim, it's still a Linux... */
900         args->sysmsg_result = 0;
901         return 0;
902 }
903
904 /*
905  * Wrappers for get/setitimer for debugging..
906  */
907 int
908 linux_setitimer(struct linux_setitimer_args *args)
909 {
910         struct setitimer_args bsa;
911         struct itimerval foo;
912         int error;
913
914 #ifdef DEBUG
915         if (ldebug(setitimer))
916                 printf(ARGS(setitimer, "%p, %p"),
917                     (void *)args->itv, (void *)args->oitv);
918 #endif
919         bsa.which = args->which;
920         bsa.itv = (struct itimerval *)args->itv;
921         bsa.oitv = (struct itimerval *)args->oitv;
922         bsa.sysmsg_result = 0;
923         if (args->itv) {
924             if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
925                 return error;
926 #ifdef DEBUG
927             if (ldebug(setitimer)) {
928                 printf("setitimer: value: sec: %ld, usec: %ld\n",
929                     foo.it_value.tv_sec, foo.it_value.tv_usec);
930                 printf("setitimer: interval: sec: %ld, usec: %ld\n",
931                     foo.it_interval.tv_sec, foo.it_interval.tv_usec);
932             }
933 #endif
934         }
935         error = setitimer(&bsa);
936         args->sysmsg_result = bsa.sysmsg_result;
937         return(error);
938 }
939
940 int
941 linux_getitimer(struct linux_getitimer_args *args)
942 {
943         struct getitimer_args bsa;
944         int error;
945 #ifdef DEBUG
946         if (ldebug(getitimer))
947                 printf(ARGS(getitimer, "%p"), (void *)args->itv);
948 #endif
949         bsa.which = args->which;
950         bsa.itv = (struct itimerval *)args->itv;
951         bsa.sysmsg_result = 0;
952         error = getitimer(&bsa);
953         args->sysmsg_result = bsa.sysmsg_result;
954         return(error);
955 }
956
957 #ifndef __alpha__
958 int
959 linux_nice(struct linux_nice_args *args)
960 {
961         struct setpriority_args bsd_args;
962         int error;
963
964         bsd_args.which = PRIO_PROCESS;
965         bsd_args.who = 0;       /* current process */
966         bsd_args.prio = args->inc;
967         bsd_args.sysmsg_result = 0;
968         error = setpriority(&bsd_args);
969         args->sysmsg_result = bsd_args.sysmsg_result;
970         return(error);
971 }
972 #endif  /*!__alpha__*/
973
974 int
975 linux_setgroups(struct linux_setgroups_args *args)
976 {
977         struct thread *td = curthread;
978         struct proc *p = td->td_proc;
979         struct ucred *newcred, *oldcred;
980         l_gid_t linux_gidset[NGROUPS];
981         gid_t *bsd_gidset;
982         int ngrp, error;
983
984         KKASSERT(p);
985
986         ngrp = args->gidsetsize;
987         oldcred = p->p_ucred;
988
989         /*
990          * cr_groups[0] holds egid. Setting the whole set from
991          * the supplied set will cause egid to be changed too.
992          * Keep cr_groups[0] unchanged to prevent that.
993          */
994
995         if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
996                 return (error);
997
998         if (ngrp >= NGROUPS)
999                 return (EINVAL);
1000
1001         newcred = crdup(oldcred);
1002         if (ngrp > 0) {
1003                 error = copyin((caddr_t)args->grouplist, linux_gidset,
1004                                ngrp * sizeof(l_gid_t));
1005                 if (error)
1006                         return (error);
1007
1008                 newcred->cr_ngroups = ngrp + 1;
1009
1010                 bsd_gidset = newcred->cr_groups;
1011                 ngrp--;
1012                 while (ngrp >= 0) {
1013                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1014                         ngrp--;
1015                 }
1016         } else {
1017                 newcred->cr_ngroups = 1;
1018         }
1019
1020         setsugid();
1021         p->p_ucred = newcred;
1022         crfree(oldcred);
1023         return (0);
1024 }
1025
1026 int
1027 linux_getgroups(struct linux_getgroups_args *args)
1028 {
1029         struct thread *td = curthread;
1030         struct proc *p = td->td_proc;
1031         struct ucred *cred;
1032         l_gid_t linux_gidset[NGROUPS];
1033         gid_t *bsd_gidset;
1034         int bsd_gidsetsz, ngrp, error;
1035
1036         KKASSERT(p);
1037
1038         cred = p->p_ucred;
1039         bsd_gidset = cred->cr_groups;
1040         bsd_gidsetsz = cred->cr_ngroups - 1;
1041
1042         /*
1043          * cr_groups[0] holds egid. Returning the whole set
1044          * here will cause a duplicate. Exclude cr_groups[0]
1045          * to prevent that.
1046          */
1047
1048         if ((ngrp = args->gidsetsize) == 0) {
1049                 args->sysmsg_result = bsd_gidsetsz;
1050                 return (0);
1051         }
1052
1053         if (ngrp < bsd_gidsetsz)
1054                 return (EINVAL);
1055
1056         ngrp = 0;
1057         while (ngrp < bsd_gidsetsz) {
1058                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1059                 ngrp++;
1060         }
1061
1062         if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1063             ngrp * sizeof(l_gid_t))))
1064                 return (error);
1065
1066         args->sysmsg_result = ngrp;
1067         return (0);
1068 }
1069
1070 #ifndef __alpha__
1071 int
1072 linux_setrlimit(struct linux_setrlimit_args *args)
1073 {
1074         struct l_rlimit linux_rlim;
1075         struct rlimit rlim;
1076         u_int which;
1077         int error;
1078
1079 #ifdef DEBUG
1080         if (ldebug(setrlimit))
1081                 printf(ARGS(setrlimit, "%d, %p"),
1082                     args->resource, (void *)args->rlim);
1083 #endif
1084         if (args->resource >= LINUX_RLIM_NLIMITS)
1085                 return (EINVAL);
1086         which = linux_to_bsd_resource[args->resource];
1087         if (which == -1)
1088                 return (EINVAL);
1089
1090         error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1091         if (error)
1092                 return (error);
1093         rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1094         rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1095
1096         error = kern_setrlimit(which, &rlim);
1097
1098         return(error);
1099 }
1100
1101 int
1102 linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1103 {
1104         struct l_rlimit linux_rlim;
1105         struct rlimit rlim;
1106         u_int which;
1107         int error;
1108
1109 #ifdef DEBUG
1110         if (ldebug(old_getrlimit))
1111                 printf(ARGS(old_getrlimit, "%d, %p"),
1112                     args->resource, (void *)args->rlim);
1113 #endif
1114         if (args->resource >= LINUX_RLIM_NLIMITS)
1115                 return (EINVAL);
1116         which = linux_to_bsd_resource[args->resource];
1117         if (which == -1)
1118                 return (EINVAL);
1119
1120         error = kern_getrlimit(which, &rlim);
1121
1122         if (error == 0) {
1123                 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1124                 if (linux_rlim.rlim_cur == ULONG_MAX)
1125                         linux_rlim.rlim_cur = LONG_MAX;
1126                 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1127                 if (linux_rlim.rlim_max == ULONG_MAX)
1128                         linux_rlim.rlim_max = LONG_MAX;
1129                 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1130         }
1131         return (error);
1132 }
1133
1134 int
1135 linux_getrlimit(struct linux_getrlimit_args *args)
1136 {
1137         struct l_rlimit linux_rlim;
1138         struct rlimit rlim;
1139         u_int which;
1140         int error;
1141
1142 #ifdef DEBUG
1143         if (ldebug(getrlimit))
1144                 printf(ARGS(getrlimit, "%d, %p"),
1145                     args->resource, (void *)args->rlim);
1146 #endif
1147         if (args->resource >= LINUX_RLIM_NLIMITS)
1148                 return (EINVAL);
1149         which = linux_to_bsd_resource[args->resource];
1150         if (which == -1)
1151                 return (EINVAL);
1152
1153         error = kern_getrlimit(which, &rlim);
1154
1155         if (error == 0) {
1156                 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1157                 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1158                 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1159         }
1160         return (error);
1161 }
1162 #endif /*!__alpha__*/
1163
1164 int
1165 linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1166 {
1167         struct sched_setscheduler_args bsd;
1168         int error;
1169
1170 #ifdef DEBUG
1171         if (ldebug(sched_setscheduler))
1172                 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1173                     args->pid, args->policy, (const void *)args->param);
1174 #endif
1175
1176         switch (args->policy) {
1177         case LINUX_SCHED_OTHER:
1178                 bsd.policy = SCHED_OTHER;
1179                 break;
1180         case LINUX_SCHED_FIFO:
1181                 bsd.policy = SCHED_FIFO;
1182                 break;
1183         case LINUX_SCHED_RR:
1184                 bsd.policy = SCHED_RR;
1185                 break;
1186         default:
1187                 return EINVAL;
1188         }
1189
1190         bsd.pid = args->pid;
1191         bsd.param = (struct sched_param *)args->param;
1192         bsd.sysmsg_result = 0;
1193
1194         error = sched_setscheduler(&bsd);
1195         args->sysmsg_result = bsd.sysmsg_result;
1196         return(error);
1197 }
1198
1199 int
1200 linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1201 {
1202         struct sched_getscheduler_args bsd;
1203         int error;
1204
1205 #ifdef DEBUG
1206         if (ldebug(sched_getscheduler))
1207                 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1208 #endif
1209
1210         bsd.sysmsg_result = 0;
1211         bsd.pid = args->pid;
1212         error = sched_getscheduler(&bsd);
1213         args->sysmsg_result = bsd.sysmsg_result;
1214
1215         switch (args->sysmsg_result) {
1216         case SCHED_OTHER:
1217                 args->sysmsg_result = LINUX_SCHED_OTHER;
1218                 break;
1219         case SCHED_FIFO:
1220                 args->sysmsg_result = LINUX_SCHED_FIFO;
1221                 break;
1222         case SCHED_RR:
1223                 args->sysmsg_result = LINUX_SCHED_RR;
1224                 break;
1225         }
1226         return error;
1227 }
1228
1229 int
1230 linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1231 {
1232         struct sched_get_priority_max_args bsd;
1233         int error;
1234
1235 #ifdef DEBUG
1236         if (ldebug(sched_get_priority_max))
1237                 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1238 #endif
1239
1240         switch (args->policy) {
1241         case LINUX_SCHED_OTHER:
1242                 bsd.policy = SCHED_OTHER;
1243                 break;
1244         case LINUX_SCHED_FIFO:
1245                 bsd.policy = SCHED_FIFO;
1246                 break;
1247         case LINUX_SCHED_RR:
1248                 bsd.policy = SCHED_RR;
1249                 break;
1250         default:
1251                 return EINVAL;
1252         }
1253         bsd.sysmsg_result = 0;
1254
1255         error = sched_get_priority_max(&bsd);
1256         args->sysmsg_result = bsd.sysmsg_result;
1257         return(error);
1258 }
1259
1260 int
1261 linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1262 {
1263         struct sched_get_priority_min_args bsd;
1264         int error;
1265
1266 #ifdef DEBUG
1267         if (ldebug(sched_get_priority_min))
1268                 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1269 #endif
1270
1271         switch (args->policy) {
1272         case LINUX_SCHED_OTHER:
1273                 bsd.policy = SCHED_OTHER;
1274                 break;
1275         case LINUX_SCHED_FIFO:
1276                 bsd.policy = SCHED_FIFO;
1277                 break;
1278         case LINUX_SCHED_RR:
1279                 bsd.policy = SCHED_RR;
1280                 break;
1281         default:
1282                 return EINVAL;
1283         }
1284         bsd.sysmsg_result = 0;
1285
1286         error = sched_get_priority_min(&bsd);
1287         args->sysmsg_result = bsd.sysmsg_result;
1288         return(error);
1289 }
1290
1291 #define REBOOT_CAD_ON   0x89abcdef
1292 #define REBOOT_CAD_OFF  0
1293 #define REBOOT_HALT     0xcdef0123
1294
1295 int
1296 linux_reboot(struct linux_reboot_args *args)
1297 {
1298         struct reboot_args bsd_args;
1299         int error;
1300
1301 #ifdef DEBUG
1302         if (ldebug(reboot))
1303                 printf(ARGS(reboot, "0x%x"), args->cmd);
1304 #endif
1305         if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1306                 return (0);
1307         bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1308         bsd_args.sysmsg_result = 0;
1309
1310         error = reboot(&bsd_args);
1311         args->sysmsg_result = bsd_args.sysmsg_result;
1312         return(error);
1313 }
1314
1315 #ifndef __alpha__
1316
1317 /*
1318  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1319  * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1320  * globbers registers that are assumed to be preserved. The following
1321  * lightweight syscalls fixes this. See also linux_getgid16() and
1322  * linux_getuid16() in linux_uid16.c.
1323  *
1324  * linux_getpid() - MP SAFE
1325  * linux_getgid() - MP SAFE
1326  * linux_getuid() - MP SAFE
1327  */
1328
1329 int
1330 linux_getpid(struct linux_getpid_args *args)
1331 {
1332         struct thread *td = curthread;
1333         struct proc *p = td->td_proc;
1334
1335         KKASSERT(p);
1336
1337         args->sysmsg_result = p->p_pid;
1338         return (0);
1339 }
1340
1341 int
1342 linux_getgid(struct linux_getgid_args *args)
1343 {
1344         struct thread *td = curthread;
1345         struct proc *p = td->td_proc;
1346
1347         KKASSERT(p);
1348
1349         args->sysmsg_result = p->p_ucred->cr_rgid;
1350         return (0);
1351 }
1352
1353 int
1354 linux_getuid(struct linux_getuid_args *args)
1355 {
1356         struct thread *td = curthread;
1357         struct proc *p = td->td_proc;
1358
1359         KKASSERT(p);
1360
1361         args->sysmsg_result = p->p_ucred->cr_ruid;
1362         return (0);
1363 }
1364
1365 #endif /*!__alpha__*/
1366
1367 int
1368 linux_getsid(struct linux_getsid_args *args)
1369 {
1370         struct getsid_args bsd;
1371         int error;
1372
1373         bsd.sysmsg_result = 0;
1374         bsd.pid = args->pid;
1375         error = getsid(&bsd);
1376         args->sysmsg_result = bsd.sysmsg_result;
1377         return(error);
1378 }
1379