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