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