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