]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/compat/linux/linux_misc.c
MFC r283474:
[FreeBSD/stable/10.git] / sys / compat / linux / linux_misc.c
1 /*-
2  * Copyright (c) 2002 Doug Rabson
3  * Copyright (c) 1994-1995 Søren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_compat.h"
34 #include "opt_kdtrace.h"
35
36 #include <sys/param.h>
37 #include <sys/blist.h>
38 #include <sys/fcntl.h>
39 #if defined(__i386__)
40 #include <sys/imgact_aout.h>
41 #endif
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/limits.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mman.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/reboot.h>
54 #include <sys/racct.h>
55 #include <sys/resourcevar.h>
56 #include <sys/sched.h>
57 #include <sys/sdt.h>
58 #include <sys/signalvar.h>
59 #include <sys/stat.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysproto.h>
63 #include <sys/systm.h>
64 #include <sys/time.h>
65 #include <sys/vmmeter.h>
66 #include <sys/vnode.h>
67 #include <sys/wait.h>
68 #include <sys/cpuset.h>
69
70 #include <security/mac/mac_framework.h>
71
72 #include <vm/vm.h>
73 #include <vm/pmap.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_object.h>
78 #include <vm/swap_pager.h>
79
80 #ifdef COMPAT_LINUX32
81 #include <machine/../linux32/linux.h>
82 #include <machine/../linux32/linux32_proto.h>
83 #else
84 #include <machine/../linux/linux.h>
85 #include <machine/../linux/linux_proto.h>
86 #endif
87
88 #include <compat/linux/linux_dtrace.h>
89 #include <compat/linux/linux_file.h>
90 #include <compat/linux/linux_mib.h>
91 #include <compat/linux/linux_signal.h>
92 #include <compat/linux/linux_timer.h>
93 #include <compat/linux/linux_util.h>
94 #include <compat/linux/linux_sysproto.h>
95 #include <compat/linux/linux_emul.h>
96 #include <compat/linux/linux_misc.h>
97
98 /**
99  * Special DTrace provider for the linuxulator.
100  *
101  * In this file we define the provider for the entire linuxulator. All
102  * modules (= files of the linuxulator) use it.
103  *
104  * We define a different name depending on the emulated bitsize, see
105  * ../../<ARCH>/linux{,32}/linux.h, e.g.:
106  *      native bitsize          = linuxulator
107  *      amd64, 32bit emulation  = linuxulator32
108  */
109 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
110
111 int stclohz;                            /* Statistics clock frequency */
112
113 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
114         RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
115         RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
116         RLIMIT_MEMLOCK, RLIMIT_AS 
117 };
118
119 struct l_sysinfo {
120         l_long          uptime;         /* Seconds since boot */
121         l_ulong         loads[3];       /* 1, 5, and 15 minute load averages */
122 #define LINUX_SYSINFO_LOADS_SCALE 65536
123         l_ulong         totalram;       /* Total usable main memory size */
124         l_ulong         freeram;        /* Available memory size */
125         l_ulong         sharedram;      /* Amount of shared memory */
126         l_ulong         bufferram;      /* Memory used by buffers */
127         l_ulong         totalswap;      /* Total swap space size */
128         l_ulong         freeswap;       /* swap space still available */
129         l_ushort        procs;          /* Number of current processes */
130         l_ushort        pads;
131         l_ulong         totalbig;
132         l_ulong         freebig;
133         l_uint          mem_unit;
134         char            _f[20-2*sizeof(l_long)-sizeof(l_int)];  /* padding */
135 };
136
137 struct l_pselect6arg {
138         l_uintptr_t     ss;
139         l_size_t        ss_len;
140 };
141
142 int
143 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
144 {
145         struct l_sysinfo sysinfo;
146         vm_object_t object;
147         int i, j;
148         struct timespec ts;
149
150         getnanouptime(&ts);
151         if (ts.tv_nsec != 0)
152                 ts.tv_sec++;
153         sysinfo.uptime = ts.tv_sec;
154
155         /* Use the information from the mib to get our load averages */
156         for (i = 0; i < 3; i++)
157                 sysinfo.loads[i] = averunnable.ldavg[i] *
158                     LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
159
160         sysinfo.totalram = physmem * PAGE_SIZE;
161         sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
162
163         sysinfo.sharedram = 0;
164         mtx_lock(&vm_object_list_mtx);
165         TAILQ_FOREACH(object, &vm_object_list, object_list)
166                 if (object->shadow_count > 1)
167                         sysinfo.sharedram += object->resident_page_count;
168         mtx_unlock(&vm_object_list_mtx);
169
170         sysinfo.sharedram *= PAGE_SIZE;
171         sysinfo.bufferram = 0;
172
173         swap_pager_status(&i, &j);
174         sysinfo.totalswap = i * PAGE_SIZE;
175         sysinfo.freeswap = (i - j) * PAGE_SIZE;
176
177         sysinfo.procs = nprocs;
178
179         /* The following are only present in newer Linux kernels. */
180         sysinfo.totalbig = 0;
181         sysinfo.freebig = 0;
182         sysinfo.mem_unit = 1;
183
184         return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
185 }
186
187 int
188 linux_alarm(struct thread *td, struct linux_alarm_args *args)
189 {
190         struct itimerval it, old_it;
191         u_int secs;
192         int error;
193
194 #ifdef DEBUG
195         if (ldebug(alarm))
196                 printf(ARGS(alarm, "%u"), args->secs);
197 #endif
198         
199         secs = args->secs;
200
201         if (secs > INT_MAX)
202                 secs = INT_MAX;
203
204         it.it_value.tv_sec = (long) secs;
205         it.it_value.tv_usec = 0;
206         it.it_interval.tv_sec = 0;
207         it.it_interval.tv_usec = 0;
208         error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
209         if (error)
210                 return (error);
211         if (timevalisset(&old_it.it_value)) {
212                 if (old_it.it_value.tv_usec != 0)
213                         old_it.it_value.tv_sec++;
214                 td->td_retval[0] = old_it.it_value.tv_sec;
215         }
216         return (0);
217 }
218
219 int
220 linux_brk(struct thread *td, struct linux_brk_args *args)
221 {
222         struct vmspace *vm = td->td_proc->p_vmspace;
223         vm_offset_t new, old;
224         struct obreak_args /* {
225                 char * nsize;
226         } */ tmp;
227
228 #ifdef DEBUG
229         if (ldebug(brk))
230                 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
231 #endif
232         old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
233         new = (vm_offset_t)args->dsend;
234         tmp.nsize = (char *)new;
235         if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
236                 td->td_retval[0] = (long)new;
237         else
238                 td->td_retval[0] = (long)old;
239
240         return (0);
241 }
242
243 #if defined(__i386__)
244 /* XXX: what about amd64/linux32? */
245
246 int
247 linux_uselib(struct thread *td, struct linux_uselib_args *args)
248 {
249         struct nameidata ni;
250         struct vnode *vp;
251         struct exec *a_out;
252         struct vattr attr;
253         vm_offset_t vmaddr;
254         unsigned long file_offset;
255         unsigned long bss_size;
256         char *library;
257         ssize_t aresid;
258         int error, locked, writecount;
259
260         LCONVPATHEXIST(td, args->library, &library);
261
262 #ifdef DEBUG
263         if (ldebug(uselib))
264                 printf(ARGS(uselib, "%s"), library);
265 #endif
266
267         a_out = NULL;
268         locked = 0;
269         vp = NULL;
270
271         NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
272             UIO_SYSSPACE, library, td);
273         error = namei(&ni);
274         LFREEPATH(library);
275         if (error)
276                 goto cleanup;
277
278         vp = ni.ni_vp;
279         NDFREE(&ni, NDF_ONLY_PNBUF);
280
281         /*
282          * From here on down, we have a locked vnode that must be unlocked.
283          * XXX: The code below largely duplicates exec_check_permissions().
284          */
285         locked = 1;
286
287         /* Writable? */
288         error = VOP_GET_WRITECOUNT(vp, &writecount);
289         if (error != 0)
290                 goto cleanup;
291         if (writecount != 0) {
292                 error = ETXTBSY;
293                 goto cleanup;
294         }
295
296         /* Executable? */
297         error = VOP_GETATTR(vp, &attr, td->td_ucred);
298         if (error)
299                 goto cleanup;
300
301         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
302             ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
303                 /* EACCESS is what exec(2) returns. */
304                 error = ENOEXEC;
305                 goto cleanup;
306         }
307
308         /* Sensible size? */
309         if (attr.va_size == 0) {
310                 error = ENOEXEC;
311                 goto cleanup;
312         }
313
314         /* Can we access it? */
315         error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
316         if (error)
317                 goto cleanup;
318
319         /*
320          * XXX: This should use vn_open() so that it is properly authorized,
321          * and to reduce code redundancy all over the place here.
322          * XXX: Not really, it duplicates far more of exec_check_permissions()
323          * than vn_open().
324          */
325 #ifdef MAC
326         error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
327         if (error)
328                 goto cleanup;
329 #endif
330         error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
331         if (error)
332                 goto cleanup;
333
334         /* Pull in executable header into exec_map */
335         error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
336             VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
337         if (error)
338                 goto cleanup;
339
340         /* Is it a Linux binary ? */
341         if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
342                 error = ENOEXEC;
343                 goto cleanup;
344         }
345
346         /*
347          * While we are here, we should REALLY do some more checks
348          */
349
350         /* Set file/virtual offset based on a.out variant. */
351         switch ((int)(a_out->a_magic & 0xffff)) {
352         case 0413:                      /* ZMAGIC */
353                 file_offset = 1024;
354                 break;
355         case 0314:                      /* QMAGIC */
356                 file_offset = 0;
357                 break;
358         default:
359                 error = ENOEXEC;
360                 goto cleanup;
361         }
362
363         bss_size = round_page(a_out->a_bss);
364
365         /* Check various fields in header for validity/bounds. */
366         if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
367                 error = ENOEXEC;
368                 goto cleanup;
369         }
370
371         /* text + data can't exceed file size */
372         if (a_out->a_data + a_out->a_text > attr.va_size) {
373                 error = EFAULT;
374                 goto cleanup;
375         }
376
377         /*
378          * text/data/bss must not exceed limits
379          * XXX - this is not complete. it should check current usage PLUS
380          * the resources needed by this library.
381          */
382         PROC_LOCK(td->td_proc);
383         if (a_out->a_text > maxtsiz ||
384             a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA) ||
385             racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
386             bss_size) != 0) {
387                 PROC_UNLOCK(td->td_proc);
388                 error = ENOMEM;
389                 goto cleanup;
390         }
391         PROC_UNLOCK(td->td_proc);
392
393         /*
394          * Prevent more writers.
395          * XXX: Note that if any of the VM operations fail below we don't
396          * clear this flag.
397          */
398         VOP_SET_TEXT(vp);
399
400         /*
401          * Lock no longer needed
402          */
403         locked = 0;
404         VOP_UNLOCK(vp, 0);
405
406         /*
407          * Check if file_offset page aligned. Currently we cannot handle
408          * misalinged file offsets, and so we read in the entire image
409          * (what a waste).
410          */
411         if (file_offset & PAGE_MASK) {
412 #ifdef DEBUG
413                 printf("uselib: Non page aligned binary %lu\n", file_offset);
414 #endif
415                 /* Map text+data read/write/execute */
416
417                 /* a_entry is the load address and is page aligned */
418                 vmaddr = trunc_page(a_out->a_entry);
419
420                 /* get anon user mapping, read+write+execute */
421                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
422                     &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
423                     VM_PROT_ALL, VM_PROT_ALL, 0);
424                 if (error)
425                         goto cleanup;
426
427                 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
428                     a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
429                     td->td_ucred, NOCRED, &aresid, td);
430                 if (error != 0)
431                         goto cleanup;
432                 if (aresid != 0) {
433                         error = ENOEXEC;
434                         goto cleanup;
435                 }
436         } else {
437 #ifdef DEBUG
438                 printf("uselib: Page aligned binary %lu\n", file_offset);
439 #endif
440                 /*
441                  * for QMAGIC, a_entry is 20 bytes beyond the load address
442                  * to skip the executable header
443                  */
444                 vmaddr = trunc_page(a_out->a_entry);
445
446                 /*
447                  * Map it all into the process's space as a single
448                  * copy-on-write "data" segment.
449                  */
450                 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
451                     a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
452                     MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
453                 if (error)
454                         goto cleanup;
455         }
456 #ifdef DEBUG
457         printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
458             ((long *)vmaddr)[1]);
459 #endif
460         if (bss_size != 0) {
461                 /* Calculate BSS start address */
462                 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
463                     a_out->a_data;
464
465                 /* allocate some 'anon' space */
466                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
467                     &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
468                     VM_PROT_ALL, 0);
469                 if (error)
470                         goto cleanup;
471         }
472
473 cleanup:
474         /* Unlock vnode if needed */
475         if (locked)
476                 VOP_UNLOCK(vp, 0);
477
478         /* Release the temporary mapping. */
479         if (a_out)
480                 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
481
482         return (error);
483 }
484
485 #endif  /* __i386__ */
486
487 int
488 linux_select(struct thread *td, struct linux_select_args *args)
489 {
490         l_timeval ltv;
491         struct timeval tv0, tv1, utv, *tvp;
492         int error;
493
494 #ifdef DEBUG
495         if (ldebug(select))
496                 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
497                     (void *)args->readfds, (void *)args->writefds,
498                     (void *)args->exceptfds, (void *)args->timeout);
499 #endif
500
501         /*
502          * Store current time for computation of the amount of
503          * time left.
504          */
505         if (args->timeout) {
506                 if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
507                         goto select_out;
508                 utv.tv_sec = ltv.tv_sec;
509                 utv.tv_usec = ltv.tv_usec;
510 #ifdef DEBUG
511                 if (ldebug(select))
512                         printf(LMSG("incoming timeout (%jd/%ld)"),
513                             (intmax_t)utv.tv_sec, utv.tv_usec);
514 #endif
515
516                 if (itimerfix(&utv)) {
517                         /*
518                          * The timeval was invalid.  Convert it to something
519                          * valid that will act as it does under Linux.
520                          */
521                         utv.tv_sec += utv.tv_usec / 1000000;
522                         utv.tv_usec %= 1000000;
523                         if (utv.tv_usec < 0) {
524                                 utv.tv_sec -= 1;
525                                 utv.tv_usec += 1000000;
526                         }
527                         if (utv.tv_sec < 0)
528                                 timevalclear(&utv);
529                 }
530                 microtime(&tv0);
531                 tvp = &utv;
532         } else
533                 tvp = NULL;
534
535         error = kern_select(td, args->nfds, args->readfds, args->writefds,
536             args->exceptfds, tvp, LINUX_NFDBITS);
537
538 #ifdef DEBUG
539         if (ldebug(select))
540                 printf(LMSG("real select returns %d"), error);
541 #endif
542         if (error)
543                 goto select_out;
544
545         if (args->timeout) {
546                 if (td->td_retval[0]) {
547                         /*
548                          * Compute how much time was left of the timeout,
549                          * by subtracting the current time and the time
550                          * before we started the call, and subtracting
551                          * that result from the user-supplied value.
552                          */
553                         microtime(&tv1);
554                         timevalsub(&tv1, &tv0);
555                         timevalsub(&utv, &tv1);
556                         if (utv.tv_sec < 0)
557                                 timevalclear(&utv);
558                 } else
559                         timevalclear(&utv);
560 #ifdef DEBUG
561                 if (ldebug(select))
562                         printf(LMSG("outgoing timeout (%jd/%ld)"),
563                             (intmax_t)utv.tv_sec, utv.tv_usec);
564 #endif
565                 ltv.tv_sec = utv.tv_sec;
566                 ltv.tv_usec = utv.tv_usec;
567                 if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
568                         goto select_out;
569         }
570
571 select_out:
572 #ifdef DEBUG
573         if (ldebug(select))
574                 printf(LMSG("select_out -> %d"), error);
575 #endif
576         return (error);
577 }
578
579 int
580 linux_mremap(struct thread *td, struct linux_mremap_args *args)
581 {
582         struct munmap_args /* {
583                 void *addr;
584                 size_t len;
585         } */ bsd_args;
586         int error = 0;
587
588 #ifdef DEBUG
589         if (ldebug(mremap))
590                 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
591                     (void *)(uintptr_t)args->addr,
592                     (unsigned long)args->old_len,
593                     (unsigned long)args->new_len,
594                     (unsigned long)args->flags);
595 #endif
596
597         if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
598                 td->td_retval[0] = 0;
599                 return (EINVAL);
600         }
601
602         /*
603          * Check for the page alignment.
604          * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
605          */
606         if (args->addr & PAGE_MASK) {
607                 td->td_retval[0] = 0;
608                 return (EINVAL);
609         }
610
611         args->new_len = round_page(args->new_len);
612         args->old_len = round_page(args->old_len);
613
614         if (args->new_len > args->old_len) {
615                 td->td_retval[0] = 0;
616                 return (ENOMEM);
617         }
618
619         if (args->new_len < args->old_len) {
620                 bsd_args.addr =
621                     (caddr_t)((uintptr_t)args->addr + args->new_len);
622                 bsd_args.len = args->old_len - args->new_len;
623                 error = sys_munmap(td, &bsd_args);
624         }
625
626         td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
627         return (error);
628 }
629
630 #define LINUX_MS_ASYNC       0x0001
631 #define LINUX_MS_INVALIDATE  0x0002
632 #define LINUX_MS_SYNC        0x0004
633
634 int
635 linux_msync(struct thread *td, struct linux_msync_args *args)
636 {
637         struct msync_args bsd_args;
638
639         bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
640         bsd_args.len = (uintptr_t)args->len;
641         bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
642
643         return (sys_msync(td, &bsd_args));
644 }
645
646 int
647 linux_time(struct thread *td, struct linux_time_args *args)
648 {
649         struct timeval tv;
650         l_time_t tm;
651         int error;
652
653 #ifdef DEBUG
654         if (ldebug(time))
655                 printf(ARGS(time, "*"));
656 #endif
657
658         microtime(&tv);
659         tm = tv.tv_sec;
660         if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
661                 return (error);
662         td->td_retval[0] = tm;
663         return (0);
664 }
665
666 struct l_times_argv {
667         l_clock_t       tms_utime;
668         l_clock_t       tms_stime;
669         l_clock_t       tms_cutime;
670         l_clock_t       tms_cstime;
671 };
672
673
674 /*
675  * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
676  * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
677  * auxiliary vector entry.
678  */
679 #define CLK_TCK         100
680
681 #define CONVOTCK(r)     (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
682 #define CONVNTCK(r)     (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
683
684 #define CONVTCK(r)      (linux_kernver(td) >= LINUX_KERNVER_2004000 ?           \
685                             CONVNTCK(r) : CONVOTCK(r))
686
687 int
688 linux_times(struct thread *td, struct linux_times_args *args)
689 {
690         struct timeval tv, utime, stime, cutime, cstime;
691         struct l_times_argv tms;
692         struct proc *p;
693         int error;
694
695 #ifdef DEBUG
696         if (ldebug(times))
697                 printf(ARGS(times, "*"));
698 #endif
699
700         if (args->buf != NULL) {
701                 p = td->td_proc;
702                 PROC_LOCK(p);
703                 PROC_STATLOCK(p);
704                 calcru(p, &utime, &stime);
705                 PROC_STATUNLOCK(p);
706                 calccru(p, &cutime, &cstime);
707                 PROC_UNLOCK(p);
708
709                 tms.tms_utime = CONVTCK(utime);
710                 tms.tms_stime = CONVTCK(stime);
711
712                 tms.tms_cutime = CONVTCK(cutime);
713                 tms.tms_cstime = CONVTCK(cstime);
714
715                 if ((error = copyout(&tms, args->buf, sizeof(tms))))
716                         return (error);
717         }
718
719         microuptime(&tv);
720         td->td_retval[0] = (int)CONVTCK(tv);
721         return (0);
722 }
723
724 int
725 linux_newuname(struct thread *td, struct linux_newuname_args *args)
726 {
727         struct l_new_utsname utsname;
728         char osname[LINUX_MAX_UTSNAME];
729         char osrelease[LINUX_MAX_UTSNAME];
730         char *p;
731
732 #ifdef DEBUG
733         if (ldebug(newuname))
734                 printf(ARGS(newuname, "*"));
735 #endif
736
737         linux_get_osname(td, osname);
738         linux_get_osrelease(td, osrelease);
739
740         bzero(&utsname, sizeof(utsname));
741         strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
742         getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
743         getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
744         strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
745         strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
746         for (p = utsname.version; *p != '\0'; ++p)
747                 if (*p == '\n') {
748                         *p = '\0';
749                         break;
750                 }
751         strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
752
753         return (copyout(&utsname, args->buf, sizeof(utsname)));
754 }
755
756 struct l_utimbuf {
757         l_time_t l_actime;
758         l_time_t l_modtime;
759 };
760
761 int
762 linux_utime(struct thread *td, struct linux_utime_args *args)
763 {
764         struct timeval tv[2], *tvp;
765         struct l_utimbuf lut;
766         char *fname;
767         int error;
768
769         LCONVPATHEXIST(td, args->fname, &fname);
770
771 #ifdef DEBUG
772         if (ldebug(utime))
773                 printf(ARGS(utime, "%s, *"), fname);
774 #endif
775
776         if (args->times) {
777                 if ((error = copyin(args->times, &lut, sizeof lut))) {
778                         LFREEPATH(fname);
779                         return (error);
780                 }
781                 tv[0].tv_sec = lut.l_actime;
782                 tv[0].tv_usec = 0;
783                 tv[1].tv_sec = lut.l_modtime;
784                 tv[1].tv_usec = 0;
785                 tvp = tv;
786         } else
787                 tvp = NULL;
788
789         error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
790         LFREEPATH(fname);
791         return (error);
792 }
793
794 int
795 linux_utimes(struct thread *td, struct linux_utimes_args *args)
796 {
797         l_timeval ltv[2];
798         struct timeval tv[2], *tvp = NULL;
799         char *fname;
800         int error;
801
802         LCONVPATHEXIST(td, args->fname, &fname);
803
804 #ifdef DEBUG
805         if (ldebug(utimes))
806                 printf(ARGS(utimes, "%s, *"), fname);
807 #endif
808
809         if (args->tptr != NULL) {
810                 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
811                         LFREEPATH(fname);
812                         return (error);
813                 }
814                 tv[0].tv_sec = ltv[0].tv_sec;
815                 tv[0].tv_usec = ltv[0].tv_usec;
816                 tv[1].tv_sec = ltv[1].tv_sec;
817                 tv[1].tv_usec = ltv[1].tv_usec;
818                 tvp = tv;
819         }
820
821         error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
822         LFREEPATH(fname);
823         return (error);
824 }
825
826 int
827 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
828 {
829         l_timeval ltv[2];
830         struct timeval tv[2], *tvp = NULL;
831         char *fname;
832         int error, dfd;
833
834         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
835         LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
836
837 #ifdef DEBUG
838         if (ldebug(futimesat))
839                 printf(ARGS(futimesat, "%s, *"), fname);
840 #endif
841
842         if (args->utimes != NULL) {
843                 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
844                         LFREEPATH(fname);
845                         return (error);
846                 }
847                 tv[0].tv_sec = ltv[0].tv_sec;
848                 tv[0].tv_usec = ltv[0].tv_usec;
849                 tv[1].tv_sec = ltv[1].tv_sec;
850                 tv[1].tv_usec = ltv[1].tv_usec;
851                 tvp = tv;
852         }
853
854         error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
855         LFREEPATH(fname);
856         return (error);
857 }
858
859 int
860 linux_common_wait(struct thread *td, int pid, int *status,
861     int options, struct rusage *ru)
862 {
863         int error, tmpstat;
864
865         error = kern_wait(td, pid, &tmpstat, options, ru);
866         if (error)
867                 return (error);
868
869         if (status) {
870                 tmpstat &= 0xffff;
871                 if (WIFSIGNALED(tmpstat))
872                         tmpstat = (tmpstat & 0xffffff80) |
873                             bsd_to_linux_signal(WTERMSIG(tmpstat));
874                 else if (WIFSTOPPED(tmpstat))
875                         tmpstat = (tmpstat & 0xffff00ff) |
876                             (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
877                 else if (WIFCONTINUED(tmpstat))
878                         tmpstat = 0xffff;
879                 error = copyout(&tmpstat, status, sizeof(int));
880         }
881
882         return (error);
883 }
884
885 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
886 int
887 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
888 {
889         struct linux_wait4_args wait4_args;
890
891 #ifdef DEBUG
892         if (ldebug(waitpid))
893                 printf(ARGS(waitpid, "%d, %p, %d"),
894                     args->pid, (void *)args->status, args->options);
895 #endif
896
897         wait4_args.pid = args->pid;
898         wait4_args.status = args->status;
899         wait4_args.options = args->options;
900         wait4_args.rusage = NULL;
901
902         return (linux_wait4(td, &wait4_args));
903 }
904 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
905
906 int
907 linux_wait4(struct thread *td, struct linux_wait4_args *args)
908 {
909         int error, options;
910         struct rusage ru, *rup;
911
912 #ifdef DEBUG
913         if (ldebug(wait4))
914                 printf(ARGS(wait4, "%d, %p, %d, %p"),
915                     args->pid, (void *)args->status, args->options,
916                     (void *)args->rusage);
917 #endif
918         if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
919             LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
920                 return (EINVAL);
921
922         options = WEXITED;
923         linux_to_bsd_waitopts(args->options, &options);
924
925         if (args->rusage != NULL)
926                 rup = &ru;
927         else
928                 rup = NULL;
929         error = linux_common_wait(td, args->pid, args->status, options, rup);
930         if (error != 0)
931                 return (error);
932         if (args->rusage != NULL)
933                 error = linux_copyout_rusage(&ru, args->rusage);
934         return (error);
935 }
936
937 int
938 linux_waitid(struct thread *td, struct linux_waitid_args *args)
939 {
940         int status, options, sig;
941         struct __wrusage wru;
942         siginfo_t siginfo;
943         l_siginfo_t lsi;
944         idtype_t idtype;
945         struct proc *p;
946         int error;
947
948         options = 0;
949         linux_to_bsd_waitopts(args->options, &options);
950
951         if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
952                 return (EINVAL);
953         if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
954                 return (EINVAL);
955
956         switch (args->idtype) {
957         case LINUX_P_ALL:
958                 idtype = P_ALL;
959                 break;
960         case LINUX_P_PID:
961                 if (args->id <= 0)
962                         return (EINVAL);
963                 idtype = P_PID;
964                 break;
965         case LINUX_P_PGID:
966                 if (args->id <= 0)
967                         return (EINVAL);
968                 idtype = P_PGID;
969                 break;
970         default:
971                 return (EINVAL);
972         }
973
974         error = kern_wait6(td, idtype, args->id, &status, options,
975             &wru, &siginfo);
976         if (error != 0)
977                 return (error);
978         if (args->rusage != NULL) {
979                 error = linux_copyout_rusage(&wru.wru_children,
980                     args->rusage);
981                 if (error != 0)
982                         return (error);
983         }
984         if (args->info != NULL) {
985                 p = td->td_proc;
986                 if (td->td_retval[0] == 0)
987                         bzero(&lsi, sizeof(lsi));
988                 else {
989                         sig = bsd_to_linux_signal(siginfo.si_signo);
990                         siginfo_to_lsiginfo(&siginfo, &lsi, sig);
991                 }
992                 error = copyout(&lsi, args->info, sizeof(lsi));
993         }
994         td->td_retval[0] = 0;
995
996         return (error);
997 }
998
999 int
1000 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1001 {
1002         char *path;
1003         int error;
1004
1005         LCONVPATHCREAT(td, args->path, &path);
1006
1007 #ifdef DEBUG
1008         if (ldebug(mknod))
1009                 printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode,
1010                     (uintmax_t)args->dev);
1011 #endif
1012
1013         switch (args->mode & S_IFMT) {
1014         case S_IFIFO:
1015         case S_IFSOCK:
1016                 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
1017                 break;
1018
1019         case S_IFCHR:
1020         case S_IFBLK:
1021                 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
1022                     args->dev);
1023                 break;
1024
1025         case S_IFDIR:
1026                 error = EPERM;
1027                 break;
1028
1029         case 0:
1030                 args->mode |= S_IFREG;
1031                 /* FALLTHROUGH */
1032         case S_IFREG:
1033                 error = kern_open(td, path, UIO_SYSSPACE,
1034                     O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1035                 if (error == 0)
1036                         kern_close(td, td->td_retval[0]);
1037                 break;
1038
1039         default:
1040                 error = EINVAL;
1041                 break;
1042         }
1043         LFREEPATH(path);
1044         return (error);
1045 }
1046
1047 int
1048 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1049 {
1050         char *path;
1051         int error, dfd;
1052
1053         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1054         LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1055
1056 #ifdef DEBUG
1057         if (ldebug(mknodat))
1058                 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1059 #endif
1060
1061         switch (args->mode & S_IFMT) {
1062         case S_IFIFO:
1063         case S_IFSOCK:
1064                 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1065                 break;
1066
1067         case S_IFCHR:
1068         case S_IFBLK:
1069                 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1070                     args->dev);
1071                 break;
1072
1073         case S_IFDIR:
1074                 error = EPERM;
1075                 break;
1076
1077         case 0:
1078                 args->mode |= S_IFREG;
1079                 /* FALLTHROUGH */
1080         case S_IFREG:
1081                 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1082                     O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1083                 if (error == 0)
1084                         kern_close(td, td->td_retval[0]);
1085                 break;
1086
1087         default:
1088                 error = EINVAL;
1089                 break;
1090         }
1091         LFREEPATH(path);
1092         return (error);
1093 }
1094
1095 /*
1096  * UGH! This is just about the dumbest idea I've ever heard!!
1097  */
1098 int
1099 linux_personality(struct thread *td, struct linux_personality_args *args)
1100 {
1101 #ifdef DEBUG
1102         if (ldebug(personality))
1103                 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1104 #endif
1105         if (args->per != 0)
1106                 return (EINVAL);
1107
1108         /* Yes Jim, it's still a Linux... */
1109         td->td_retval[0] = 0;
1110         return (0);
1111 }
1112
1113 struct l_itimerval {
1114         l_timeval it_interval;
1115         l_timeval it_value;
1116 };
1117
1118 #define B2L_ITIMERVAL(bip, lip)                                         \
1119         (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;          \
1120         (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;        \
1121         (bip)->it_value.tv_sec = (lip)->it_value.tv_sec;                \
1122         (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1123
1124 int
1125 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1126 {
1127         int error;
1128         struct l_itimerval ls;
1129         struct itimerval aitv, oitv;
1130
1131 #ifdef DEBUG
1132         if (ldebug(setitimer))
1133                 printf(ARGS(setitimer, "%p, %p"),
1134                     (void *)uap->itv, (void *)uap->oitv);
1135 #endif
1136
1137         if (uap->itv == NULL) {
1138                 uap->itv = uap->oitv;
1139                 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1140         }
1141
1142         error = copyin(uap->itv, &ls, sizeof(ls));
1143         if (error != 0)
1144                 return (error);
1145         B2L_ITIMERVAL(&aitv, &ls);
1146 #ifdef DEBUG
1147         if (ldebug(setitimer)) {
1148                 printf("setitimer: value: sec: %jd, usec: %ld\n",
1149                     (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1150                 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1151                     (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1152         }
1153 #endif
1154         error = kern_setitimer(td, uap->which, &aitv, &oitv);
1155         if (error != 0 || uap->oitv == NULL)
1156                 return (error);
1157         B2L_ITIMERVAL(&ls, &oitv);
1158
1159         return (copyout(&ls, uap->oitv, sizeof(ls)));
1160 }
1161
1162 int
1163 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1164 {
1165         int error;
1166         struct l_itimerval ls;
1167         struct itimerval aitv;
1168
1169 #ifdef DEBUG
1170         if (ldebug(getitimer))
1171                 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1172 #endif
1173         error = kern_getitimer(td, uap->which, &aitv);
1174         if (error != 0)
1175                 return (error);
1176         B2L_ITIMERVAL(&ls, &aitv);
1177         return (copyout(&ls, uap->itv, sizeof(ls)));
1178 }
1179
1180 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1181 int
1182 linux_nice(struct thread *td, struct linux_nice_args *args)
1183 {
1184         struct setpriority_args bsd_args;
1185
1186         bsd_args.which = PRIO_PROCESS;
1187         bsd_args.who = 0;               /* current process */
1188         bsd_args.prio = args->inc;
1189         return (sys_setpriority(td, &bsd_args));
1190 }
1191 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1192
1193 int
1194 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1195 {
1196         struct ucred *newcred, *oldcred;
1197         l_gid_t *linux_gidset;
1198         gid_t *bsd_gidset;
1199         int ngrp, error;
1200         struct proc *p;
1201
1202         ngrp = args->gidsetsize;
1203         if (ngrp < 0 || ngrp >= ngroups_max + 1)
1204                 return (EINVAL);
1205         linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1206         error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1207         if (error)
1208                 goto out;
1209         newcred = crget();
1210         p = td->td_proc;
1211         PROC_LOCK(p);
1212         oldcred = crcopysafe(p, newcred);
1213
1214         /*
1215          * cr_groups[0] holds egid. Setting the whole set from
1216          * the supplied set will cause egid to be changed too.
1217          * Keep cr_groups[0] unchanged to prevent that.
1218          */
1219
1220         if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1221                 PROC_UNLOCK(p);
1222                 crfree(newcred);
1223                 goto out;
1224         }
1225
1226         if (ngrp > 0) {
1227                 newcred->cr_ngroups = ngrp + 1;
1228
1229                 bsd_gidset = newcred->cr_groups;
1230                 ngrp--;
1231                 while (ngrp >= 0) {
1232                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1233                         ngrp--;
1234                 }
1235         } else
1236                 newcred->cr_ngroups = 1;
1237
1238         setsugid(p);
1239         p->p_ucred = newcred;
1240         PROC_UNLOCK(p);
1241         crfree(oldcred);
1242         error = 0;
1243 out:
1244         free(linux_gidset, M_LINUX);
1245         return (error);
1246 }
1247
1248 int
1249 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1250 {
1251         struct ucred *cred;
1252         l_gid_t *linux_gidset;
1253         gid_t *bsd_gidset;
1254         int bsd_gidsetsz, ngrp, error;
1255
1256         cred = td->td_ucred;
1257         bsd_gidset = cred->cr_groups;
1258         bsd_gidsetsz = cred->cr_ngroups - 1;
1259
1260         /*
1261          * cr_groups[0] holds egid. Returning the whole set
1262          * here will cause a duplicate. Exclude cr_groups[0]
1263          * to prevent that.
1264          */
1265
1266         if ((ngrp = args->gidsetsize) == 0) {
1267                 td->td_retval[0] = bsd_gidsetsz;
1268                 return (0);
1269         }
1270
1271         if (ngrp < bsd_gidsetsz)
1272                 return (EINVAL);
1273
1274         ngrp = 0;
1275         linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1276             M_LINUX, M_WAITOK);
1277         while (ngrp < bsd_gidsetsz) {
1278                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1279                 ngrp++;
1280         }
1281
1282         error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1283         free(linux_gidset, M_LINUX);
1284         if (error)
1285                 return (error);
1286
1287         td->td_retval[0] = ngrp;
1288         return (0);
1289 }
1290
1291 int
1292 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1293 {
1294         struct rlimit bsd_rlim;
1295         struct l_rlimit rlim;
1296         u_int which;
1297         int error;
1298
1299 #ifdef DEBUG
1300         if (ldebug(setrlimit))
1301                 printf(ARGS(setrlimit, "%d, %p"),
1302                     args->resource, (void *)args->rlim);
1303 #endif
1304
1305         if (args->resource >= LINUX_RLIM_NLIMITS)
1306                 return (EINVAL);
1307
1308         which = linux_to_bsd_resource[args->resource];
1309         if (which == -1)
1310                 return (EINVAL);
1311
1312         error = copyin(args->rlim, &rlim, sizeof(rlim));
1313         if (error)
1314                 return (error);
1315
1316         bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1317         bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1318         return (kern_setrlimit(td, which, &bsd_rlim));
1319 }
1320
1321 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1322 int
1323 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1324 {
1325         struct l_rlimit rlim;
1326         struct proc *p = td->td_proc;
1327         struct rlimit bsd_rlim;
1328         u_int which;
1329
1330 #ifdef DEBUG
1331         if (ldebug(old_getrlimit))
1332                 printf(ARGS(old_getrlimit, "%d, %p"),
1333                     args->resource, (void *)args->rlim);
1334 #endif
1335
1336         if (args->resource >= LINUX_RLIM_NLIMITS)
1337                 return (EINVAL);
1338
1339         which = linux_to_bsd_resource[args->resource];
1340         if (which == -1)
1341                 return (EINVAL);
1342
1343         PROC_LOCK(p);
1344         lim_rlimit(p, which, &bsd_rlim);
1345         PROC_UNLOCK(p);
1346
1347 #ifdef COMPAT_LINUX32
1348         rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1349         if (rlim.rlim_cur == UINT_MAX)
1350                 rlim.rlim_cur = INT_MAX;
1351         rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1352         if (rlim.rlim_max == UINT_MAX)
1353                 rlim.rlim_max = INT_MAX;
1354 #else
1355         rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1356         if (rlim.rlim_cur == ULONG_MAX)
1357                 rlim.rlim_cur = LONG_MAX;
1358         rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1359         if (rlim.rlim_max == ULONG_MAX)
1360                 rlim.rlim_max = LONG_MAX;
1361 #endif
1362         return (copyout(&rlim, args->rlim, sizeof(rlim)));
1363 }
1364 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1365
1366 int
1367 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1368 {
1369         struct l_rlimit rlim;
1370         struct proc *p = td->td_proc;
1371         struct rlimit bsd_rlim;
1372         u_int which;
1373
1374 #ifdef DEBUG
1375         if (ldebug(getrlimit))
1376                 printf(ARGS(getrlimit, "%d, %p"),
1377                     args->resource, (void *)args->rlim);
1378 #endif
1379
1380         if (args->resource >= LINUX_RLIM_NLIMITS)
1381                 return (EINVAL);
1382
1383         which = linux_to_bsd_resource[args->resource];
1384         if (which == -1)
1385                 return (EINVAL);
1386
1387         PROC_LOCK(p);
1388         lim_rlimit(p, which, &bsd_rlim);
1389         PROC_UNLOCK(p);
1390
1391         rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1392         rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1393         return (copyout(&rlim, args->rlim, sizeof(rlim)));
1394 }
1395
1396 int
1397 linux_sched_setscheduler(struct thread *td,
1398     struct linux_sched_setscheduler_args *args)
1399 {
1400         struct sched_param sched_param;
1401         struct thread *tdt;
1402         int error, policy;
1403
1404 #ifdef DEBUG
1405         if (ldebug(sched_setscheduler))
1406                 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1407                     args->pid, args->policy, (const void *)args->param);
1408 #endif
1409
1410         switch (args->policy) {
1411         case LINUX_SCHED_OTHER:
1412                 policy = SCHED_OTHER;
1413                 break;
1414         case LINUX_SCHED_FIFO:
1415                 policy = SCHED_FIFO;
1416                 break;
1417         case LINUX_SCHED_RR:
1418                 policy = SCHED_RR;
1419                 break;
1420         default:
1421                 return (EINVAL);
1422         }
1423
1424         error = copyin(args->param, &sched_param, sizeof(sched_param));
1425         if (error)
1426                 return (error);
1427
1428         tdt = linux_tdfind(td, args->pid, -1);
1429         if (tdt == NULL)
1430                 return (ESRCH);
1431
1432         error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1433         PROC_UNLOCK(tdt->td_proc);
1434         return (error);
1435 }
1436
1437 int
1438 linux_sched_getscheduler(struct thread *td,
1439     struct linux_sched_getscheduler_args *args)
1440 {
1441         struct thread *tdt;
1442         int error, policy;
1443
1444 #ifdef DEBUG
1445         if (ldebug(sched_getscheduler))
1446                 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1447 #endif
1448
1449         tdt = linux_tdfind(td, args->pid, -1);
1450         if (tdt == NULL)
1451                 return (ESRCH);
1452
1453         error = kern_sched_getscheduler(td, tdt, &policy);
1454         PROC_UNLOCK(tdt->td_proc);
1455
1456         switch (policy) {
1457         case SCHED_OTHER:
1458                 td->td_retval[0] = LINUX_SCHED_OTHER;
1459                 break;
1460         case SCHED_FIFO:
1461                 td->td_retval[0] = LINUX_SCHED_FIFO;
1462                 break;
1463         case SCHED_RR:
1464                 td->td_retval[0] = LINUX_SCHED_RR;
1465                 break;
1466         }
1467         return (error);
1468 }
1469
1470 int
1471 linux_sched_get_priority_max(struct thread *td,
1472     struct linux_sched_get_priority_max_args *args)
1473 {
1474         struct sched_get_priority_max_args bsd;
1475
1476 #ifdef DEBUG
1477         if (ldebug(sched_get_priority_max))
1478                 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1479 #endif
1480
1481         switch (args->policy) {
1482         case LINUX_SCHED_OTHER:
1483                 bsd.policy = SCHED_OTHER;
1484                 break;
1485         case LINUX_SCHED_FIFO:
1486                 bsd.policy = SCHED_FIFO;
1487                 break;
1488         case LINUX_SCHED_RR:
1489                 bsd.policy = SCHED_RR;
1490                 break;
1491         default:
1492                 return (EINVAL);
1493         }
1494         return (sys_sched_get_priority_max(td, &bsd));
1495 }
1496
1497 int
1498 linux_sched_get_priority_min(struct thread *td,
1499     struct linux_sched_get_priority_min_args *args)
1500 {
1501         struct sched_get_priority_min_args bsd;
1502
1503 #ifdef DEBUG
1504         if (ldebug(sched_get_priority_min))
1505                 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1506 #endif
1507
1508         switch (args->policy) {
1509         case LINUX_SCHED_OTHER:
1510                 bsd.policy = SCHED_OTHER;
1511                 break;
1512         case LINUX_SCHED_FIFO:
1513                 bsd.policy = SCHED_FIFO;
1514                 break;
1515         case LINUX_SCHED_RR:
1516                 bsd.policy = SCHED_RR;
1517                 break;
1518         default:
1519                 return (EINVAL);
1520         }
1521         return (sys_sched_get_priority_min(td, &bsd));
1522 }
1523
1524 #define REBOOT_CAD_ON   0x89abcdef
1525 #define REBOOT_CAD_OFF  0
1526 #define REBOOT_HALT     0xcdef0123
1527 #define REBOOT_RESTART  0x01234567
1528 #define REBOOT_RESTART2 0xA1B2C3D4
1529 #define REBOOT_POWEROFF 0x4321FEDC
1530 #define REBOOT_MAGIC1   0xfee1dead
1531 #define REBOOT_MAGIC2   0x28121969
1532 #define REBOOT_MAGIC2A  0x05121996
1533 #define REBOOT_MAGIC2B  0x16041998
1534
1535 int
1536 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1537 {
1538         struct reboot_args bsd_args;
1539
1540 #ifdef DEBUG
1541         if (ldebug(reboot))
1542                 printf(ARGS(reboot, "0x%x"), args->cmd);
1543 #endif
1544
1545         if (args->magic1 != REBOOT_MAGIC1)
1546                 return (EINVAL);
1547
1548         switch (args->magic2) {
1549         case REBOOT_MAGIC2:
1550         case REBOOT_MAGIC2A:
1551         case REBOOT_MAGIC2B:
1552                 break;
1553         default:
1554                 return (EINVAL);
1555         }
1556
1557         switch (args->cmd) {
1558         case REBOOT_CAD_ON:
1559         case REBOOT_CAD_OFF:
1560                 return (priv_check(td, PRIV_REBOOT));
1561         case REBOOT_HALT:
1562                 bsd_args.opt = RB_HALT;
1563                 break;
1564         case REBOOT_RESTART:
1565         case REBOOT_RESTART2:
1566                 bsd_args.opt = 0;
1567                 break;
1568         case REBOOT_POWEROFF:
1569                 bsd_args.opt = RB_POWEROFF;
1570                 break;
1571         default:
1572                 return (EINVAL);
1573         }
1574         return (sys_reboot(td, &bsd_args));
1575 }
1576
1577
1578 /*
1579  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1580  * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1581  * are assumed to be preserved. The following lightweight syscalls fixes
1582  * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1583  *
1584  * linux_getpid() - MP SAFE
1585  * linux_getgid() - MP SAFE
1586  * linux_getuid() - MP SAFE
1587  */
1588
1589 int
1590 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1591 {
1592
1593 #ifdef DEBUG
1594         if (ldebug(getpid))
1595                 printf(ARGS(getpid, ""));
1596 #endif
1597         td->td_retval[0] = td->td_proc->p_pid;
1598
1599         return (0);
1600 }
1601
1602 int
1603 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1604 {
1605         struct linux_emuldata *em;
1606
1607 #ifdef DEBUG
1608         if (ldebug(gettid))
1609                 printf(ARGS(gettid, ""));
1610 #endif
1611
1612         em = em_find(td);
1613         KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1614
1615         td->td_retval[0] = em->em_tid;
1616
1617         return (0);
1618 }
1619
1620
1621 int
1622 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1623 {
1624
1625 #ifdef DEBUG
1626         if (ldebug(getppid))
1627                 printf(ARGS(getppid, ""));
1628 #endif
1629
1630         PROC_LOCK(td->td_proc);
1631         td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1632         PROC_UNLOCK(td->td_proc);
1633         return (0);
1634 }
1635
1636 int
1637 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1638 {
1639
1640 #ifdef DEBUG
1641         if (ldebug(getgid))
1642                 printf(ARGS(getgid, ""));
1643 #endif
1644
1645         td->td_retval[0] = td->td_ucred->cr_rgid;
1646         return (0);
1647 }
1648
1649 int
1650 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1651 {
1652
1653 #ifdef DEBUG
1654         if (ldebug(getuid))
1655                 printf(ARGS(getuid, ""));
1656 #endif
1657
1658         td->td_retval[0] = td->td_ucred->cr_ruid;
1659         return (0);
1660 }
1661
1662
1663 int
1664 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1665 {
1666         struct getsid_args bsd;
1667
1668 #ifdef DEBUG
1669         if (ldebug(getsid))
1670                 printf(ARGS(getsid, "%i"), args->pid);
1671 #endif
1672
1673         bsd.pid = args->pid;
1674         return (sys_getsid(td, &bsd));
1675 }
1676
1677 int
1678 linux_nosys(struct thread *td, struct nosys_args *ignore)
1679 {
1680
1681         return (ENOSYS);
1682 }
1683
1684 int
1685 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1686 {
1687         struct getpriority_args bsd_args;
1688         int error;
1689
1690 #ifdef DEBUG
1691         if (ldebug(getpriority))
1692                 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1693 #endif
1694
1695         bsd_args.which = args->which;
1696         bsd_args.who = args->who;
1697         error = sys_getpriority(td, &bsd_args);
1698         td->td_retval[0] = 20 - td->td_retval[0];
1699         return (error);
1700 }
1701
1702 int
1703 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1704 {
1705         int name[2];
1706
1707 #ifdef DEBUG
1708         if (ldebug(sethostname))
1709                 printf(ARGS(sethostname, "*, %i"), args->len);
1710 #endif
1711
1712         name[0] = CTL_KERN;
1713         name[1] = KERN_HOSTNAME;
1714         return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1715             args->len, 0, 0));
1716 }
1717
1718 int
1719 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1720 {
1721         int name[2];
1722
1723 #ifdef DEBUG
1724         if (ldebug(setdomainname))
1725                 printf(ARGS(setdomainname, "*, %i"), args->len);
1726 #endif
1727
1728         name[0] = CTL_KERN;
1729         name[1] = KERN_NISDOMAINNAME;
1730         return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1731             args->len, 0, 0));
1732 }
1733
1734 int
1735 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1736 {
1737
1738 #ifdef DEBUG
1739         if (ldebug(exit_group))
1740                 printf(ARGS(exit_group, "%i"), args->error_code);
1741 #endif
1742
1743         LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1744             args->error_code);
1745
1746         /*
1747          * XXX: we should send a signal to the parent if
1748          * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1749          * as it doesnt occur often.
1750          */
1751         exit1(td, W_EXITCODE(args->error_code, 0));
1752                 /* NOTREACHED */
1753 }
1754
1755 #define _LINUX_CAPABILITY_VERSION  0x19980330
1756
1757 struct l_user_cap_header {
1758         l_int   version;
1759         l_int   pid;
1760 };
1761
1762 struct l_user_cap_data {
1763         l_int   effective;
1764         l_int   permitted;
1765         l_int   inheritable;
1766 };
1767
1768 int
1769 linux_capget(struct thread *td, struct linux_capget_args *args)
1770 {
1771         struct l_user_cap_header luch;
1772         struct l_user_cap_data lucd;
1773         int error;
1774
1775         if (args->hdrp == NULL)
1776                 return (EFAULT);
1777
1778         error = copyin(args->hdrp, &luch, sizeof(luch));
1779         if (error != 0)
1780                 return (error);
1781
1782         if (luch.version != _LINUX_CAPABILITY_VERSION) {
1783                 luch.version = _LINUX_CAPABILITY_VERSION;
1784                 error = copyout(&luch, args->hdrp, sizeof(luch));
1785                 if (error)
1786                         return (error);
1787                 return (EINVAL);
1788         }
1789
1790         if (luch.pid)
1791                 return (EPERM);
1792
1793         if (args->datap) {
1794                 /*
1795                  * The current implementation doesn't support setting
1796                  * a capability (it's essentially a stub) so indicate
1797                  * that no capabilities are currently set or available
1798                  * to request.
1799                  */
1800                 bzero (&lucd, sizeof(lucd));
1801                 error = copyout(&lucd, args->datap, sizeof(lucd));
1802         }
1803
1804         return (error);
1805 }
1806
1807 int
1808 linux_capset(struct thread *td, struct linux_capset_args *args)
1809 {
1810         struct l_user_cap_header luch;
1811         struct l_user_cap_data lucd;
1812         int error;
1813
1814         if (args->hdrp == NULL || args->datap == NULL)
1815                 return (EFAULT);
1816
1817         error = copyin(args->hdrp, &luch, sizeof(luch));
1818         if (error != 0)
1819                 return (error);
1820
1821         if (luch.version != _LINUX_CAPABILITY_VERSION) {
1822                 luch.version = _LINUX_CAPABILITY_VERSION;
1823                 error = copyout(&luch, args->hdrp, sizeof(luch));
1824                 if (error)
1825                         return (error);
1826                 return (EINVAL);
1827         }
1828
1829         if (luch.pid)
1830                 return (EPERM);
1831
1832         error = copyin(args->datap, &lucd, sizeof(lucd));
1833         if (error != 0)
1834                 return (error);
1835
1836         /* We currently don't support setting any capabilities. */
1837         if (lucd.effective || lucd.permitted || lucd.inheritable) {
1838                 linux_msg(td,
1839                           "capset effective=0x%x, permitted=0x%x, "
1840                           "inheritable=0x%x is not implemented",
1841                           (int)lucd.effective, (int)lucd.permitted,
1842                           (int)lucd.inheritable);
1843                 return (EPERM);
1844         }
1845
1846         return (0);
1847 }
1848
1849 int
1850 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1851 {
1852         int error = 0, max_size;
1853         struct proc *p = td->td_proc;
1854         char comm[LINUX_MAX_COMM_LEN];
1855         struct linux_emuldata *em;
1856         int pdeath_signal;
1857
1858 #ifdef DEBUG
1859         if (ldebug(prctl))
1860                 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option,
1861                     (uintmax_t)args->arg2, (uintmax_t)args->arg3,
1862                     (uintmax_t)args->arg4, (uintmax_t)args->arg5);
1863 #endif
1864
1865         switch (args->option) {
1866         case LINUX_PR_SET_PDEATHSIG:
1867                 if (!LINUX_SIG_VALID(args->arg2))
1868                         return (EINVAL);
1869                 em = em_find(td);
1870                 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1871                 em->pdeath_signal = args->arg2;
1872                 break;
1873         case LINUX_PR_GET_PDEATHSIG:
1874                 em = em_find(td);
1875                 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1876                 pdeath_signal = em->pdeath_signal;
1877                 error = copyout(&pdeath_signal,
1878                     (void *)(register_t)args->arg2,
1879                     sizeof(pdeath_signal));
1880                 break;
1881         case LINUX_PR_GET_KEEPCAPS:
1882                 /*
1883                  * Indicate that we always clear the effective and
1884                  * permitted capability sets when the user id becomes
1885                  * non-zero (actually the capability sets are simply
1886                  * always zero in the current implementation).
1887                  */
1888                 td->td_retval[0] = 0;
1889                 break;
1890         case LINUX_PR_SET_KEEPCAPS:
1891                 /*
1892                  * Ignore requests to keep the effective and permitted
1893                  * capability sets when the user id becomes non-zero.
1894                  */
1895                 break;
1896         case LINUX_PR_SET_NAME:
1897                 /*
1898                  * To be on the safe side we need to make sure to not
1899                  * overflow the size a linux program expects. We already
1900                  * do this here in the copyin, so that we don't need to
1901                  * check on copyout.
1902                  */
1903                 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1904                 error = copyinstr((void *)(register_t)args->arg2, comm,
1905                     max_size, NULL);
1906
1907                 /* Linux silently truncates the name if it is too long. */
1908                 if (error == ENAMETOOLONG) {
1909                         /*
1910                          * XXX: copyinstr() isn't documented to populate the
1911                          * array completely, so do a copyin() to be on the
1912                          * safe side. This should be changed in case
1913                          * copyinstr() is changed to guarantee this.
1914                          */
1915                         error = copyin((void *)(register_t)args->arg2, comm,
1916                             max_size - 1);
1917                         comm[max_size - 1] = '\0';
1918                 }
1919                 if (error)
1920                         return (error);
1921
1922                 PROC_LOCK(p);
1923                 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1924                 PROC_UNLOCK(p);
1925                 break;
1926         case LINUX_PR_GET_NAME:
1927                 PROC_LOCK(p);
1928                 strlcpy(comm, p->p_comm, sizeof(comm));
1929                 PROC_UNLOCK(p);
1930                 error = copyout(comm, (void *)(register_t)args->arg2,
1931                     strlen(comm) + 1);
1932                 break;
1933         default:
1934                 error = EINVAL;
1935                 break;
1936         }
1937
1938         return (error);
1939 }
1940
1941 int
1942 linux_sched_setparam(struct thread *td,
1943     struct linux_sched_setparam_args *uap)
1944 {
1945         struct sched_param sched_param;
1946         struct thread *tdt;
1947         int error;
1948
1949 #ifdef DEBUG
1950         if (ldebug(sched_setparam))
1951                 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
1952 #endif
1953
1954         error = copyin(uap->param, &sched_param, sizeof(sched_param));
1955         if (error)
1956                 return (error);
1957
1958         tdt = linux_tdfind(td, uap->pid, -1);
1959         if (tdt == NULL)
1960                 return (ESRCH);
1961
1962         error = kern_sched_setparam(td, tdt, &sched_param);
1963         PROC_UNLOCK(tdt->td_proc);
1964         return (error);
1965 }
1966
1967 int
1968 linux_sched_getparam(struct thread *td,
1969     struct linux_sched_getparam_args *uap)
1970 {
1971         struct sched_param sched_param;
1972         struct thread *tdt;
1973         int error;
1974
1975 #ifdef DEBUG
1976         if (ldebug(sched_getparam))
1977                 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
1978 #endif
1979
1980         tdt = linux_tdfind(td, uap->pid, -1);
1981         if (tdt == NULL)
1982                 return (ESRCH);
1983
1984         error = kern_sched_getparam(td, tdt, &sched_param);
1985         PROC_UNLOCK(tdt->td_proc);
1986         if (error == 0)
1987                 error = copyout(&sched_param, uap->param,
1988                     sizeof(sched_param));
1989         return (error);
1990 }
1991
1992 /*
1993  * Get affinity of a process.
1994  */
1995 int
1996 linux_sched_getaffinity(struct thread *td,
1997     struct linux_sched_getaffinity_args *args)
1998 {
1999         int error;
2000         struct thread *tdt;
2001         struct cpuset_getaffinity_args cga;
2002
2003 #ifdef DEBUG
2004         if (ldebug(sched_getaffinity))
2005                 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
2006                     args->len);
2007 #endif
2008         if (args->len < sizeof(cpuset_t))
2009                 return (EINVAL);
2010
2011         tdt = linux_tdfind(td, args->pid, -1);
2012         if (tdt == NULL)
2013                 return (ESRCH);
2014
2015         PROC_UNLOCK(tdt->td_proc);
2016         cga.level = CPU_LEVEL_WHICH;
2017         cga.which = CPU_WHICH_TID;
2018         cga.id = tdt->td_tid;
2019         cga.cpusetsize = sizeof(cpuset_t);
2020         cga.mask = (cpuset_t *) args->user_mask_ptr;
2021
2022         if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
2023                 td->td_retval[0] = sizeof(cpuset_t);
2024
2025         return (error);
2026 }
2027
2028 /*
2029  *  Set affinity of a process.
2030  */
2031 int
2032 linux_sched_setaffinity(struct thread *td,
2033     struct linux_sched_setaffinity_args *args)
2034 {
2035         struct cpuset_setaffinity_args csa;
2036         struct thread *tdt;
2037
2038 #ifdef DEBUG
2039         if (ldebug(sched_setaffinity))
2040                 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2041                     args->len);
2042 #endif
2043         if (args->len < sizeof(cpuset_t))
2044                 return (EINVAL);
2045
2046         tdt = linux_tdfind(td, args->pid, -1);
2047         if (tdt == NULL)
2048                 return (ESRCH);
2049
2050         PROC_UNLOCK(tdt->td_proc);
2051         csa.level = CPU_LEVEL_WHICH;
2052         csa.which = CPU_WHICH_TID;
2053         csa.id = tdt->td_tid;
2054         csa.cpusetsize = sizeof(cpuset_t);
2055         csa.mask = (cpuset_t *) args->user_mask_ptr;
2056
2057         return (sys_cpuset_setaffinity(td, &csa));
2058 }
2059
2060 struct linux_rlimit64 {
2061         uint64_t        rlim_cur;
2062         uint64_t        rlim_max;
2063 };
2064
2065 int
2066 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2067 {
2068         struct rlimit rlim, nrlim;
2069         struct linux_rlimit64 lrlim;
2070         struct proc *p;
2071         u_int which;
2072         int flags;
2073         int error;
2074
2075 #ifdef DEBUG
2076         if (ldebug(prlimit64))
2077                 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2078                     args->resource, (void *)args->new, (void *)args->old);
2079 #endif
2080
2081         if (args->resource >= LINUX_RLIM_NLIMITS)
2082                 return (EINVAL);
2083
2084         which = linux_to_bsd_resource[args->resource];
2085         if (which == -1)
2086                 return (EINVAL);
2087
2088         if (args->new != NULL) {
2089                 /*
2090                  * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2091                  * rlim is unsigned 64-bit. FreeBSD treats negative limits
2092                  * as INFINITY so we do not need a conversion even.
2093                  */
2094                 error = copyin(args->new, &nrlim, sizeof(nrlim));
2095                 if (error != 0)
2096                         return (error);
2097         }
2098
2099         flags = PGET_HOLD | PGET_NOTWEXIT;
2100         if (args->new != NULL)
2101                 flags |= PGET_CANDEBUG;
2102         else
2103                 flags |= PGET_CANSEE;
2104         error = pget(args->pid, flags, &p);
2105         if (error != 0)
2106                 return (error);
2107
2108         if (args->old != NULL) {
2109                 PROC_LOCK(p);
2110                 lim_rlimit(p, which, &rlim);
2111                 PROC_UNLOCK(p);
2112                 if (rlim.rlim_cur == RLIM_INFINITY)
2113                         lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2114                 else
2115                         lrlim.rlim_cur = rlim.rlim_cur;
2116                 if (rlim.rlim_max == RLIM_INFINITY)
2117                         lrlim.rlim_max = LINUX_RLIM_INFINITY;
2118                 else
2119                         lrlim.rlim_max = rlim.rlim_max;
2120                 error = copyout(&lrlim, args->old, sizeof(lrlim));
2121                 if (error != 0)
2122                         goto out;
2123         }
2124
2125         if (args->new != NULL)
2126                 error = kern_proc_setrlimit(td, p, which, &nrlim);
2127
2128  out:
2129         PRELE(p);
2130         return (error);
2131 }
2132
2133 int
2134 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2135 {
2136         struct timeval utv, tv0, tv1, *tvp;
2137         struct l_pselect6arg lpse6;
2138         struct l_timespec lts;
2139         struct timespec uts;
2140         l_sigset_t l_ss;
2141         sigset_t *ssp;
2142         sigset_t ss;
2143         int error;
2144
2145         ssp = NULL;
2146         if (args->sig != NULL) {
2147                 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2148                 if (error != 0)
2149                         return (error);
2150                 if (lpse6.ss_len != sizeof(l_ss))
2151                         return (EINVAL);
2152                 if (lpse6.ss != 0) {
2153                         error = copyin(PTRIN(lpse6.ss), &l_ss,
2154                             sizeof(l_ss));
2155                         if (error != 0)
2156                                 return (error);
2157                         linux_to_bsd_sigset(&l_ss, &ss);
2158                         ssp = &ss;
2159                 }
2160         }
2161
2162         /*
2163          * Currently glibc changes nanosecond number to microsecond.
2164          * This mean losing precision but for now it is hardly seen.
2165          */
2166         if (args->tsp != NULL) {
2167                 error = copyin(args->tsp, &lts, sizeof(lts));
2168                 if (error != 0)
2169                         return (error);
2170                 error = linux_to_native_timespec(&uts, &lts);
2171                 if (error != 0)
2172                         return (error);
2173
2174                 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2175                 if (itimerfix(&utv))
2176                         return (EINVAL);
2177
2178                 microtime(&tv0);
2179                 tvp = &utv;
2180         } else
2181                 tvp = NULL;
2182
2183         error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2184             args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2185
2186         if (error == 0 && args->tsp != NULL) {
2187                 if (td->td_retval[0] != 0) {
2188                         /*
2189                          * Compute how much time was left of the timeout,
2190                          * by subtracting the current time and the time
2191                          * before we started the call, and subtracting
2192                          * that result from the user-supplied value.
2193                          */
2194
2195                         microtime(&tv1);
2196                         timevalsub(&tv1, &tv0);
2197                         timevalsub(&utv, &tv1);
2198                         if (utv.tv_sec < 0)
2199                                 timevalclear(&utv);
2200                 } else
2201                         timevalclear(&utv);
2202
2203                 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2204
2205                 native_to_linux_timespec(&lts, &uts);
2206                 error = copyout(&lts, args->tsp, sizeof(lts));
2207         }
2208
2209         return (error);
2210 }
2211
2212 int
2213 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2214 {
2215         struct timespec ts0, ts1;
2216         struct l_timespec lts;
2217         struct timespec uts, *tsp;
2218         l_sigset_t l_ss;
2219         sigset_t *ssp;
2220         sigset_t ss;
2221         int error;
2222
2223         if (args->sset != NULL) {
2224                 if (args->ssize != sizeof(l_ss))
2225                         return (EINVAL);
2226                 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2227                 if (error)
2228                         return (error);
2229                 linux_to_bsd_sigset(&l_ss, &ss);
2230                 ssp = &ss;
2231         } else
2232                 ssp = NULL;
2233         if (args->tsp != NULL) {
2234                 error = copyin(args->tsp, &lts, sizeof(lts));
2235                 if (error)
2236                         return (error);
2237                 error = linux_to_native_timespec(&uts, &lts);
2238                 if (error != 0)
2239                         return (error);
2240
2241                 nanotime(&ts0);
2242                 tsp = &uts;
2243         } else
2244                 tsp = NULL;
2245
2246         error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2247
2248         if (error == 0 && args->tsp != NULL) {
2249                 if (td->td_retval[0]) {
2250                         nanotime(&ts1);
2251                         timespecsub(&ts1, &ts0);
2252                         timespecsub(&uts, &ts1);
2253                         if (uts.tv_sec < 0)
2254                                 timespecclear(&uts);
2255                 } else
2256                         timespecclear(&uts);
2257
2258                 native_to_linux_timespec(&lts, &uts);
2259                 error = copyout(&lts, args->tsp, sizeof(lts));
2260         }
2261
2262         return (error);
2263 }
2264
2265 #if defined(DEBUG) || defined(KTR)
2266 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2267
2268 u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))];
2269
2270 static int
2271 linux_debug(int syscall, int toggle, int global)
2272 {
2273
2274         if (global) {
2275                 char c = toggle ? 0 : 0xff;
2276
2277                 memset(linux_debug_map, c, sizeof(linux_debug_map));
2278                 return (0);
2279         }
2280         if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL)
2281                 return (EINVAL);
2282         if (toggle)
2283                 clrbit(linux_debug_map, syscall);
2284         else
2285                 setbit(linux_debug_map, syscall);
2286         return (0);
2287 }
2288
2289 /*
2290  * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2291  *
2292  *    E.g.: sysctl linux.debug=21.0
2293  *
2294  * As a special case, syscall "all" will apply to all syscalls globally.
2295  */
2296 #define LINUX_MAX_DEBUGSTR      16
2297 int
2298 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
2299 {
2300         char value[LINUX_MAX_DEBUGSTR], *p;
2301         int error, sysc, toggle;
2302         int global = 0;
2303
2304         value[0] = '\0';
2305         error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2306         if (error || req->newptr == NULL)
2307                 return (error);
2308         for (p = value; *p != '\0' && *p != '.'; p++);
2309         if (*p == '\0')
2310                 return (EINVAL);
2311         *p++ = '\0';
2312         sysc = strtol(value, NULL, 0);
2313         toggle = strtol(p, NULL, 0);
2314         if (strcmp(value, "all") == 0)
2315                 global = 1;
2316         error = linux_debug(sysc, toggle, global);
2317         return (error);
2318 }
2319
2320 #endif /* DEBUG || KTR */
2321
2322 int
2323 linux_sched_rr_get_interval(struct thread *td,
2324     struct linux_sched_rr_get_interval_args *uap)
2325 {
2326         struct timespec ts;
2327         struct l_timespec lts;
2328         struct thread *tdt;
2329         int error;
2330
2331         /*
2332          * According to man in case the invalid pid specified
2333          * EINVAL should be returned.
2334          */
2335         if (uap->pid < 0)
2336                 return (EINVAL);
2337
2338         tdt = linux_tdfind(td, uap->pid, -1);
2339         if (tdt == NULL)
2340                 return (ESRCH);
2341
2342         error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2343         PROC_UNLOCK(tdt->td_proc);
2344         if (error != 0)
2345                 return (error);
2346         native_to_linux_timespec(&lts, &ts);
2347         return (copyout(&lts, uap->interval, sizeof(lts)));
2348 }
2349
2350 /*
2351  * In case when the Linux thread is the initial thread in
2352  * the thread group thread id is equal to the process id.
2353  * Glibc depends on this magic (assert in pthread_getattr_np.c).
2354  */
2355 struct thread *
2356 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2357 {
2358         struct linux_emuldata *em;
2359         struct thread *tdt;
2360         struct proc *p;
2361
2362         tdt = NULL;
2363         if (tid == 0 || tid == td->td_tid) {
2364                 tdt = td;
2365                 PROC_LOCK(tdt->td_proc);
2366         } else if (tid > PID_MAX)
2367                 tdt = tdfind(tid, pid);
2368         else {
2369                 /*
2370                  * Initial thread where the tid equal to the pid.
2371                  */
2372                 p = pfind(tid);
2373                 if (p != NULL) {
2374                         if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2375                                 /*
2376                                  * p is not a Linuxulator process.
2377                                  */
2378                                 PROC_UNLOCK(p);
2379                                 return (NULL);
2380                         }
2381                         FOREACH_THREAD_IN_PROC(p, tdt) {
2382                                 em = em_find(tdt);
2383                                 if (tid == em->em_tid)
2384                                         return (tdt);
2385                         }
2386                         PROC_UNLOCK(p);
2387                 }
2388                 return (NULL);
2389         }
2390
2391         return (tdt);
2392 }
2393
2394 void
2395 linux_to_bsd_waitopts(int options, int *bsdopts)
2396 {
2397
2398         if (options & LINUX_WNOHANG)
2399                 *bsdopts |= WNOHANG;
2400         if (options & LINUX_WUNTRACED)
2401                 *bsdopts |= WUNTRACED;
2402         if (options & LINUX_WEXITED)
2403                 *bsdopts |= WEXITED;
2404         if (options & LINUX_WCONTINUED)
2405                 *bsdopts |= WCONTINUED;
2406         if (options & LINUX_WNOWAIT)
2407                 *bsdopts |= WNOWAIT;
2408
2409         if (options & __WCLONE)
2410                 *bsdopts |= WLINUXCLONE;
2411 }