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