]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/compat/linux/linux_misc.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / 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_mac.h"
34
35 #include <sys/param.h>
36 #include <sys/blist.h>
37 #include <sys/fcntl.h>
38 #if defined(__i386__) || defined(__alpha__)
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/mac.h>
46 #include <sys/malloc.h>
47 #include <sys/mman.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/proc.h>
52 #include <sys/reboot.h>
53 #include <sys/resourcevar.h>
54 #include <sys/signalvar.h>
55 #include <sys/stat.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysproto.h>
59 #include <sys/systm.h>
60 #include <sys/time.h>
61 #include <sys/vmmeter.h>
62 #include <sys/vnode.h>
63 #include <sys/wait.h>
64
65 #include <vm/vm.h>
66 #include <vm/pmap.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_object.h>
71 #include <vm/swap_pager.h>
72
73 #include <posix4/sched.h>
74
75 #include "opt_compat.h"
76
77 #include <compat/linux/linux_sysproto.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_mib.h>
88 #include <compat/linux/linux_util.h>
89
90 #ifdef __i386__
91 #include <machine/cputypes.h>
92 #endif
93
94 #ifdef __alpha__
95 #define BSD_TO_LINUX_SIGNAL(sig)       (sig)
96 #else
97 #define BSD_TO_LINUX_SIGNAL(sig)        \
98         (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
99 #endif
100
101 #ifndef __alpha__
102 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
103         RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
104         RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
105         RLIMIT_MEMLOCK, RLIMIT_AS
106 };
107 #endif /*!__alpha__*/
108
109 struct l_sysinfo {
110         l_long          uptime;         /* Seconds since boot */
111         l_ulong         loads[3];       /* 1, 5, and 15 minute load averages */
112 #define LINUX_SYSINFO_LOADS_SCALE 65536
113         l_ulong         totalram;       /* Total usable main memory size */
114         l_ulong         freeram;        /* Available memory size */
115         l_ulong         sharedram;      /* Amount of shared memory */
116         l_ulong         bufferram;      /* Memory used by buffers */
117         l_ulong         totalswap;      /* Total swap space size */
118         l_ulong         freeswap;       /* swap space still available */
119         l_ushort        procs;          /* Number of current processes */
120         l_ulong         totalbig;
121         l_ulong         freebig;
122         l_uint          mem_unit;
123         char            _f[6];          /* Pads structure to 64 bytes */
124 };
125 #ifndef __alpha__
126 int
127 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
128 {
129         struct l_sysinfo sysinfo;
130         vm_object_t object;
131         int i, j;
132         struct timespec ts;
133
134         getnanouptime(&ts);
135         if (ts.tv_nsec != 0)
136                 ts.tv_sec++;
137         sysinfo.uptime = ts.tv_sec;
138
139         /* Use the information from the mib to get our load averages */
140         for (i = 0; i < 3; i++)
141                 sysinfo.loads[i] = averunnable.ldavg[i] *
142                     LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
143
144         sysinfo.totalram = physmem * PAGE_SIZE;
145         sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
146
147         sysinfo.sharedram = 0;
148         mtx_lock(&vm_object_list_mtx);
149         TAILQ_FOREACH(object, &vm_object_list, object_list)
150                 if (object->shadow_count > 1)
151                         sysinfo.sharedram += object->resident_page_count;
152         mtx_unlock(&vm_object_list_mtx);
153
154         sysinfo.sharedram *= PAGE_SIZE;
155         sysinfo.bufferram = 0;
156
157         swap_pager_status(&i, &j);
158         sysinfo.totalswap = i * PAGE_SIZE;
159         sysinfo.freeswap = (i - j) * PAGE_SIZE;
160
161         sysinfo.procs = nprocs;
162
163         /* The following are only present in newer Linux kernels. */
164         sysinfo.totalbig = 0;
165         sysinfo.freebig = 0;
166         sysinfo.mem_unit = 1;
167
168         return copyout(&sysinfo, args->info, sizeof(sysinfo));
169 }
170 #endif /*!__alpha__*/
171
172 #ifndef __alpha__
173 int
174 linux_alarm(struct thread *td, struct linux_alarm_args *args)
175 {
176         struct itimerval it, old_it;
177         int error;
178
179 #ifdef DEBUG
180         if (ldebug(alarm))
181                 printf(ARGS(alarm, "%u"), args->secs);
182 #endif
183
184         if (args->secs > 100000000)
185                 return (EINVAL);
186
187         it.it_value.tv_sec = (long)args->secs;
188         it.it_value.tv_usec = 0;
189         it.it_interval.tv_sec = 0;
190         it.it_interval.tv_usec = 0;
191         error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
192         if (error)
193                 return (error);
194         if (timevalisset(&old_it.it_value)) {           
195                 if (old_it.it_value.tv_usec != 0)
196                         old_it.it_value.tv_sec++;
197                 td->td_retval[0] = old_it.it_value.tv_sec;
198         }
199         return (0);
200 }
201 #endif /*!__alpha__*/
202
203 int
204 linux_brk(struct thread *td, struct linux_brk_args *args)
205 {
206         struct vmspace *vm = td->td_proc->p_vmspace;
207         vm_offset_t new, old;
208         struct obreak_args /* {
209                 char * nsize;
210         } */ tmp;
211
212 #ifdef DEBUG
213         if (ldebug(brk))
214                 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
215 #endif
216         old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
217         new = (vm_offset_t)args->dsend;
218         tmp.nsize = (char *)new;
219         if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
220                 td->td_retval[0] = (long)new;
221         else
222                 td->td_retval[0] = (long)old;
223
224         return 0;
225 }
226
227 #if defined(__i386__) || defined(__alpha__)
228
229 int
230 linux_uselib(struct thread *td, struct linux_uselib_args *args)
231 {
232         struct nameidata ni;
233         struct vnode *vp;
234         struct exec *a_out;
235         struct vattr attr;
236         vm_offset_t vmaddr;
237         unsigned long file_offset;
238         vm_offset_t buffer;
239         unsigned long bss_size;
240         char *library;
241         int error;
242         int locked;
243
244         LCONVPATHEXIST(td, args->library, &library);
245
246 #ifdef DEBUG
247         if (ldebug(uselib))
248                 printf(ARGS(uselib, "%s"), library);
249 #endif
250
251         a_out = NULL;
252         locked = 0;
253         vp = NULL;
254
255         /*
256          * XXX: This code should make use of vn_open(), rather than doing
257          * all this stuff itself.
258          */
259         NDINIT(&ni, LOOKUP, ISOPEN|FOLLOW|LOCKLEAF, UIO_SYSSPACE, library, td);
260         error = namei(&ni);
261         LFREEPATH(library);
262         if (error)
263                 goto cleanup;
264
265         vp = ni.ni_vp;
266         /*
267          * XXX - This looks like a bogus check. A LOCKLEAF namei should not
268          * succeed without returning a vnode.
269          */
270         if (vp == NULL) {
271                 error = ENOEXEC;        /* ?? */
272                 goto cleanup;
273         }
274         NDFREE(&ni, NDF_ONLY_PNBUF);
275
276         /*
277          * From here on down, we have a locked vnode that must be unlocked.
278          */
279         locked++;
280
281         /* Writable? */
282         if (vp->v_writecount) {
283                 error = ETXTBSY;
284                 goto cleanup;
285         }
286
287         /* Executable? */
288         error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
289         if (error)
290                 goto cleanup;
291
292         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
293             ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
294                 /* EACCESS is what exec(2) returns. */
295                 error = ENOEXEC;
296                 goto cleanup;
297         }
298
299         /* Sensible size? */
300         if (attr.va_size == 0) {
301                 error = ENOEXEC;
302                 goto cleanup;
303         }
304
305         /* Can we access it? */
306         error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
307         if (error)
308                 goto cleanup;
309
310         /*
311          * XXX: This should use vn_open() so that it is properly authorized,
312          * and to reduce code redundancy all over the place here.
313          */
314 #ifdef MAC
315         error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
316         if (error)
317                 goto cleanup;
318 #endif
319         error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
320         if (error)
321                 goto cleanup;
322
323         /* Pull in executable header into kernel_map */
324         error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
325             VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
326         /*
327          * Lock no longer needed
328          */
329         locked = 0;
330         VOP_UNLOCK(vp, 0, td);
331
332         if (error)
333                 goto cleanup;
334
335         /* Is it a Linux binary ? */
336         if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
337                 error = ENOEXEC;
338                 goto cleanup;
339         }
340
341         /*
342          * While we are here, we should REALLY do some more checks
343          */
344
345         /* Set file/virtual offset based on a.out variant. */
346         switch ((int)(a_out->a_magic & 0xffff)) {
347         case 0413:                      /* ZMAGIC */
348                 file_offset = 1024;
349                 break;
350         case 0314:                      /* QMAGIC */
351                 file_offset = 0;
352                 break;
353         default:
354                 error = ENOEXEC;
355                 goto cleanup;
356         }
357
358         bss_size = round_page(a_out->a_bss);
359
360         /* Check various fields in header for validity/bounds. */
361         if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
362                 error = ENOEXEC;
363                 goto cleanup;
364         }
365
366         /* text + data can't exceed file size */
367         if (a_out->a_data + a_out->a_text > attr.va_size) {
368                 error = EFAULT;
369                 goto cleanup;
370         }
371
372         /*
373          * text/data/bss must not exceed limits
374          * XXX - this is not complete. it should check current usage PLUS
375          * the resources needed by this library.
376          */
377         PROC_LOCK(td->td_proc);
378         if (a_out->a_text > maxtsiz ||
379             a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
380                 PROC_UNLOCK(td->td_proc);
381                 error = ENOMEM;
382                 goto cleanup;
383         }
384         PROC_UNLOCK(td->td_proc);
385
386         mp_fixme("Unlocked vflags access.");
387         /* prevent more writers */
388         vp->v_vflag |= VV_TEXT;
389
390         /*
391          * Check if file_offset page aligned. Currently we cannot handle
392          * misalinged file offsets, and so we read in the entire image
393          * (what a waste).
394          */
395         if (file_offset & PAGE_MASK) {
396 #ifdef DEBUG
397                 printf("uselib: Non page aligned binary %lu\n", file_offset);
398 #endif
399                 /* Map text+data read/write/execute */
400
401                 /* a_entry is the load address and is page aligned */
402                 vmaddr = trunc_page(a_out->a_entry);
403
404                 /* get anon user mapping, read+write+execute */
405                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
406                     &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
407                     VM_PROT_ALL, 0);
408                 if (error)
409                         goto cleanup;
410
411                 /* map file into kernel_map */
412                 error = vm_mmap(kernel_map, &buffer,
413                     round_page(a_out->a_text + a_out->a_data + file_offset),
414                     VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp,
415                     trunc_page(file_offset));
416                 if (error)
417                         goto cleanup;
418
419                 /* copy from kernel VM space to user space */
420                 error = copyout(PTRIN(buffer + file_offset),
421                     (void *)vmaddr, a_out->a_text + a_out->a_data);
422
423                 /* release temporary kernel space */
424                 vm_map_remove(kernel_map, buffer, buffer +
425                     round_page(a_out->a_text + a_out->a_data + file_offset));
426
427                 if (error)
428                         goto cleanup;
429         } else {
430 #ifdef DEBUG
431                 printf("uselib: Page aligned binary %lu\n", file_offset);
432 #endif
433                 /*
434                  * for QMAGIC, a_entry is 20 bytes beyond the load address
435                  * to skip the executable header
436                  */
437                 vmaddr = trunc_page(a_out->a_entry);
438
439                 /*
440                  * Map it all into the process's space as a single
441                  * copy-on-write "data" segment.
442                  */
443                 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
444                     a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
445                     MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
446                 if (error)
447                         goto cleanup;
448         }
449 #ifdef DEBUG
450         printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
451             ((long *)vmaddr)[1]);
452 #endif
453         if (bss_size != 0) {
454                 /* Calculate BSS start address */
455                 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
456                     a_out->a_data;
457
458                 /* allocate some 'anon' space */
459                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
460                     &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
461                 if (error)
462                         goto cleanup;
463         }
464
465 cleanup:
466         /* Unlock vnode if needed */
467         if (locked)
468                 VOP_UNLOCK(vp, 0, td);
469
470         /* Release the kernel mapping. */
471         if (a_out)
472                 vm_map_remove(kernel_map, (vm_offset_t)a_out,
473                     (vm_offset_t)a_out + PAGE_SIZE);
474
475         return error;
476 }
477
478 #endif  /* __i386__ || __alpha__ */
479
480 int
481 linux_select(struct thread *td, struct linux_select_args *args)
482 {
483         l_timeval ltv;
484         struct timeval tv0, tv1, utv, *tvp;
485         int error;
486
487 #ifdef DEBUG
488         if (ldebug(select))
489                 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
490                     (void *)args->readfds, (void *)args->writefds,
491                     (void *)args->exceptfds, (void *)args->timeout);
492 #endif
493
494         /*
495          * Store current time for computation of the amount of
496          * time left.
497          */
498         if (args->timeout) {
499                 if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
500                         goto select_out;
501                 utv.tv_sec = ltv.tv_sec;
502                 utv.tv_usec = ltv.tv_usec;
503 #ifdef DEBUG
504                 if (ldebug(select))
505                         printf(LMSG("incoming timeout (%jd/%ld)"),
506                             (intmax_t)utv.tv_sec, utv.tv_usec);
507 #endif
508
509                 if (itimerfix(&utv)) {
510                         /*
511                          * The timeval was invalid.  Convert it to something
512                          * valid that will act as it does under Linux.
513                          */
514                         utv.tv_sec += utv.tv_usec / 1000000;
515                         utv.tv_usec %= 1000000;
516                         if (utv.tv_usec < 0) {
517                                 utv.tv_sec -= 1;
518                                 utv.tv_usec += 1000000;
519                         }
520                         if (utv.tv_sec < 0)
521                                 timevalclear(&utv);
522                 }
523                 microtime(&tv0);
524                 tvp = &utv;
525         } else
526                 tvp = NULL;
527
528         error = kern_select(td, args->nfds, args->readfds, args->writefds,
529             args->exceptfds, tvp);
530
531 #ifdef DEBUG
532         if (ldebug(select))
533                 printf(LMSG("real select returns %d"), error);
534 #endif
535         if (error) {
536                 /*
537                  * See fs/select.c in the Linux kernel.  Without this,
538                  * Maelstrom doesn't work.
539                  */
540                 if (error == ERESTART)
541                         error = EINTR;
542                 goto select_out;
543         }
544
545         if (args->timeout) {
546                 if (td->td_retval[0]) {
547                         /*
548                          * Compute how much time was left of the timeout,
549                          * by subtracting the current time and the time
550                          * before we started the call, and subtracting
551                          * that result from the user-supplied value.
552                          */
553                         microtime(&tv1);
554                         timevalsub(&tv1, &tv0);
555                         timevalsub(&utv, &tv1);
556                         if (utv.tv_sec < 0)
557                                 timevalclear(&utv);
558                 } else
559                         timevalclear(&utv);
560 #ifdef DEBUG
561                 if (ldebug(select))
562                         printf(LMSG("outgoing timeout (%jd/%ld)"),
563                             (intmax_t)utv.tv_sec, utv.tv_usec);
564 #endif
565                 ltv.tv_sec = utv.tv_sec;
566                 ltv.tv_usec = utv.tv_usec;
567                 if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
568                         goto select_out;
569         }
570
571 select_out:
572 #ifdef DEBUG
573         if (ldebug(select))
574                 printf(LMSG("select_out -> %d"), error);
575 #endif
576         return error;
577 }
578
579 int
580 linux_mremap(struct thread *td, struct linux_mremap_args *args)
581 {
582         struct munmap_args /* {
583                 void *addr;
584                 size_t len;
585         } */ bsd_args;
586         int error = 0;
587
588 #ifdef DEBUG
589         if (ldebug(mremap))
590                 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
591                     (void *)(uintptr_t)args->addr,
592                     (unsigned long)args->old_len,
593                     (unsigned long)args->new_len,
594                     (unsigned long)args->flags);
595 #endif
596         args->new_len = round_page(args->new_len);
597         args->old_len = round_page(args->old_len);
598
599         if (args->new_len > args->old_len) {
600                 td->td_retval[0] = 0;
601                 return ENOMEM;
602         }
603
604         if (args->new_len < args->old_len) {
605                 bsd_args.addr =
606                     (caddr_t)((uintptr_t)args->addr + args->new_len);
607                 bsd_args.len = args->old_len - args->new_len;
608                 error = munmap(td, &bsd_args);
609         }
610
611         td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
612         return error;
613 }
614
615 #define LINUX_MS_ASYNC       0x0001
616 #define LINUX_MS_INVALIDATE  0x0002
617 #define LINUX_MS_SYNC        0x0004
618
619 int
620 linux_msync(struct thread *td, struct linux_msync_args *args)
621 {
622         struct msync_args bsd_args;
623
624         bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
625         bsd_args.len = (uintptr_t)args->len;
626         bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
627
628         return msync(td, &bsd_args);
629 }
630
631 #ifndef __alpha__
632 int
633 linux_time(struct thread *td, struct linux_time_args *args)
634 {
635         struct timeval tv;
636         l_time_t tm;
637         int error;
638
639 #ifdef DEBUG
640         if (ldebug(time))
641                 printf(ARGS(time, "*"));
642 #endif
643
644         microtime(&tv);
645         tm = tv.tv_sec;
646         if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
647                 return error;
648         td->td_retval[0] = tm;
649         return 0;
650 }
651 #endif  /*!__alpha__*/
652
653 struct l_times_argv {
654         l_long          tms_utime;
655         l_long          tms_stime;
656         l_long          tms_cutime;
657         l_long          tms_cstime;
658 };
659
660 #ifdef __alpha__
661 #define CLK_TCK 1024    /* Linux uses 1024 on alpha */
662 #else
663 #define CLK_TCK 100     /* Linux uses 100 */
664 #endif
665
666 #define CONVTCK(r)      (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
667
668 int
669 linux_times(struct thread *td, struct linux_times_args *args)
670 {
671         struct timeval tv, utime, stime, cutime, cstime;
672         struct l_times_argv tms;
673         struct proc *p;
674         int error;
675
676 #ifdef DEBUG
677         if (ldebug(times))
678                 printf(ARGS(times, "*"));
679 #endif
680
681         if (args->buf != NULL) {
682                 p = td->td_proc;
683                 PROC_LOCK(p);
684                 calcru(p, &utime, &stime);
685                 calccru(p, &cutime, &cstime);
686                 PROC_UNLOCK(p);
687
688                 tms.tms_utime = CONVTCK(utime);
689                 tms.tms_stime = CONVTCK(stime);
690
691                 tms.tms_cutime = CONVTCK(cutime);
692                 tms.tms_cstime = CONVTCK(cstime);
693
694                 if ((error = copyout(&tms, args->buf, sizeof(tms))))
695                         return error;
696         }
697
698         microuptime(&tv);
699         td->td_retval[0] = (int)CONVTCK(tv);
700         return 0;
701 }
702
703 int
704 linux_newuname(struct thread *td, struct linux_newuname_args *args)
705 {
706         struct l_new_utsname utsname;
707         char osname[LINUX_MAX_UTSNAME];
708         char osrelease[LINUX_MAX_UTSNAME];
709         char *p;
710
711 #ifdef DEBUG
712         if (ldebug(newuname))
713                 printf(ARGS(newuname, "*"));
714 #endif
715
716         linux_get_osname(td, osname);
717         linux_get_osrelease(td, osrelease);
718
719         bzero(&utsname, sizeof(utsname));
720         strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
721         getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
722         strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
723         strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
724         for (p = utsname.version; *p != '\0'; ++p)
725                 if (*p == '\n') {
726                         *p = '\0';
727                         break;
728                 }
729 #ifdef __i386__
730         {
731                 const char *class;
732
733                 switch (cpu_class) {
734                 case CPUCLASS_686:
735                         class = "i686";
736                         break;
737                 case CPUCLASS_586:
738                         class = "i586";
739                         break;
740                 case CPUCLASS_486:
741                         class = "i486";
742                         break;
743                 default:
744                         class = "i386";
745                 }
746                 strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME);
747         }
748 #elif defined(__amd64__)        /* XXX: Linux can change 'personality'. */
749 #ifdef COMPAT_LINUX32
750         strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
751 #else
752         strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
753 #endif /* COMPAT_LINUX32 */
754 #else /* something other than i386 or amd64 - assume we and Linux agree */
755         strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
756 #endif /* __i386__ */
757         strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
758
759         return (copyout(&utsname, args->buf, sizeof(utsname)));
760 }
761
762 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
763 struct l_utimbuf {
764         l_time_t l_actime;
765         l_time_t l_modtime;
766 };
767
768 int
769 linux_utime(struct thread *td, struct linux_utime_args *args)
770 {
771         struct timeval tv[2], *tvp;
772         struct l_utimbuf lut;
773         char *fname;
774         int error;
775
776         LCONVPATHEXIST(td, args->fname, &fname);
777
778 #ifdef DEBUG
779         if (ldebug(utime))
780                 printf(ARGS(utime, "%s, *"), fname);
781 #endif
782
783         if (args->times) {
784                 if ((error = copyin(args->times, &lut, sizeof lut))) {
785                         LFREEPATH(fname);
786                         return error;
787                 }
788                 tv[0].tv_sec = lut.l_actime;
789                 tv[0].tv_usec = 0;
790                 tv[1].tv_sec = lut.l_modtime;
791                 tv[1].tv_usec = 0;
792                 tvp = tv;
793         } else
794                 tvp = NULL;
795
796         error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
797         LFREEPATH(fname);
798         return (error);
799 }
800
801 int
802 linux_utimes(struct thread *td, struct linux_utimes_args *args)
803 {
804         l_timeval ltv[2];
805         struct timeval tv[2], *tvp = NULL;
806         char *fname;
807         int error;
808
809         LCONVPATHEXIST(td, args->fname, &fname);
810
811 #ifdef DEBUG
812         if (ldebug(utimes))
813                 printf(ARGS(utimes, "%s, *"), fname);
814 #endif
815
816         if (args->tptr != NULL) {
817                 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
818                         LFREEPATH(fname);
819                         return (error);
820                 }
821                 tv[0].tv_sec = ltv[0].tv_sec;
822                 tv[0].tv_usec = ltv[0].tv_usec;
823                 tv[1].tv_sec = ltv[1].tv_sec;
824                 tv[1].tv_usec = ltv[1].tv_usec;
825                 tvp = tv;
826         }
827
828         error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
829         LFREEPATH(fname);
830         return (error);
831 }
832 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
833
834 #define __WCLONE 0x80000000
835
836 #ifndef __alpha__
837 int
838 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
839 {
840         int error, options, tmpstat;
841
842 #ifdef DEBUG
843         if (ldebug(waitpid))
844                 printf(ARGS(waitpid, "%d, %p, %d"),
845                     args->pid, (void *)args->status, args->options);
846 #endif
847         /*
848          * this is necessary because the test in kern_wait doesn't work
849          * because we mess with the options here
850          */
851         if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
852                 return (EINVAL);
853
854         options = (args->options & (WNOHANG | WUNTRACED));
855         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
856         if (args->options & __WCLONE)
857                 options |= WLINUXCLONE;
858
859         error = kern_wait(td, args->pid, &tmpstat, options, NULL);
860         if (error)
861                 return error;
862
863         if (args->status) {
864                 tmpstat &= 0xffff;
865                 if (WIFSIGNALED(tmpstat))
866                         tmpstat = (tmpstat & 0xffffff80) |
867                             BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
868                 else if (WIFSTOPPED(tmpstat))
869                         tmpstat = (tmpstat & 0xffff00ff) |
870                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
871                 return copyout(&tmpstat, args->status, sizeof(int));
872         }
873
874         return 0;
875 }
876 #endif  /*!__alpha__*/
877
878 int
879 linux_wait4(struct thread *td, struct linux_wait4_args *args)
880 {
881         int error, options, tmpstat;
882         struct rusage ru, *rup;
883         struct proc *p;
884
885 #ifdef DEBUG
886         if (ldebug(wait4))
887                 printf(ARGS(wait4, "%d, %p, %d, %p"),
888                     args->pid, (void *)args->status, args->options,
889                     (void *)args->rusage);
890 #endif
891
892         options = (args->options & (WNOHANG | WUNTRACED));
893         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
894         if (args->options & __WCLONE)
895                 options |= WLINUXCLONE;
896
897         if (args->rusage != NULL)
898                 rup = &ru;
899         else
900                 rup = NULL;
901         error = kern_wait(td, args->pid, &tmpstat, options, rup);
902         if (error)
903                 return error;
904
905         p = td->td_proc;
906         PROC_LOCK(p);
907         SIGDELSET(p->p_siglist, SIGCHLD);
908         PROC_UNLOCK(p);
909
910         if (args->status) {
911                 tmpstat &= 0xffff;
912                 if (WIFSIGNALED(tmpstat))
913                         tmpstat = (tmpstat & 0xffffff80) |
914                             BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
915                 else if (WIFSTOPPED(tmpstat))
916                         tmpstat = (tmpstat & 0xffff00ff) |
917                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
918                 error = copyout(&tmpstat, args->status, sizeof(int));
919         }
920         if (args->rusage != NULL && error == 0)
921                 error = copyout(&ru, args->rusage, sizeof(ru));
922
923         return (error);
924 }
925
926 int
927 linux_mknod(struct thread *td, struct linux_mknod_args *args)
928 {
929         char *path;
930         int error;
931
932         LCONVPATHCREAT(td, args->path, &path);
933
934 #ifdef DEBUG
935         if (ldebug(mknod))
936                 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
937 #endif
938
939         switch (args->mode & S_IFMT) {
940         case S_IFIFO:
941         case S_IFSOCK:
942                 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
943                 break;
944
945         case S_IFCHR:
946         case S_IFBLK:
947                 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
948                     args->dev);
949                 break;
950
951         case S_IFDIR:
952                 error = EPERM;
953                 break;
954
955         case 0:
956                 args->mode |= S_IFREG;
957                 /* FALLTHROUGH */
958         case S_IFREG:
959                 error = kern_open(td, path, UIO_SYSSPACE,
960                     O_WRONLY | O_CREAT | O_TRUNC, args->mode);
961                 break;
962
963         default:
964                 error = EINVAL;
965                 break;
966         }
967         LFREEPATH(path);
968         return (error);
969 }
970
971 /*
972  * UGH! This is just about the dumbest idea I've ever heard!!
973  */
974 int
975 linux_personality(struct thread *td, struct linux_personality_args *args)
976 {
977 #ifdef DEBUG
978         if (ldebug(personality))
979                 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
980 #endif
981 #ifndef __alpha__
982         if (args->per != 0)
983                 return EINVAL;
984 #endif
985
986         /* Yes Jim, it's still a Linux... */
987         td->td_retval[0] = 0;
988         return 0;
989 }
990
991 struct l_itimerval {
992         l_timeval it_interval;
993         l_timeval it_value;
994 };
995
996 #define B2L_ITIMERVAL(bip, lip)                                         \
997         (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;          \
998         (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;        \
999         (bip)->it_value.tv_sec = (lip)->it_value.tv_sec;                \
1000         (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1001
1002 int
1003 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1004 {
1005         int error;
1006         struct l_itimerval ls;
1007         struct itimerval aitv, oitv;
1008
1009 #ifdef DEBUG
1010         if (ldebug(setitimer))
1011                 printf(ARGS(setitimer, "%p, %p"),
1012                     (void *)uap->itv, (void *)uap->oitv);
1013 #endif
1014
1015         if (uap->itv == NULL) {
1016                 uap->itv = uap->oitv;
1017                 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1018         }
1019
1020         error = copyin(uap->itv, &ls, sizeof(ls));
1021         if (error != 0)
1022                 return (error);
1023         B2L_ITIMERVAL(&aitv, &ls);
1024 #ifdef DEBUG
1025         if (ldebug(setitimer)) {
1026                 printf("setitimer: value: sec: %jd, usec: %ld\n",
1027                     (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1028                 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1029                     (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1030         }
1031 #endif
1032         error = kern_setitimer(td, uap->which, &aitv, &oitv);
1033         if (error != 0 || uap->oitv == NULL)
1034                 return (error);
1035         B2L_ITIMERVAL(&ls, &oitv);
1036
1037         return (copyout(&ls, uap->oitv, sizeof(ls)));
1038 }
1039
1040 int
1041 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1042 {
1043         int error;
1044         struct l_itimerval ls;
1045         struct itimerval aitv;
1046
1047 #ifdef DEBUG
1048         if (ldebug(getitimer))
1049                 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1050 #endif
1051         error = kern_getitimer(td, uap->which, &aitv);
1052         if (error != 0)
1053                 return (error);
1054         B2L_ITIMERVAL(&ls, &aitv);
1055         return (copyout(&ls, uap->itv, sizeof(ls)));
1056 }
1057
1058 #ifndef __alpha__
1059 int
1060 linux_nice(struct thread *td, struct linux_nice_args *args)
1061 {
1062         struct setpriority_args bsd_args;
1063
1064         bsd_args.which = PRIO_PROCESS;
1065         bsd_args.who = 0;       /* current process */
1066         bsd_args.prio = args->inc;
1067         return setpriority(td, &bsd_args);
1068 }
1069 #endif  /*!__alpha__*/
1070
1071 int
1072 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1073 {
1074         struct ucred *newcred, *oldcred;
1075         l_gid_t linux_gidset[NGROUPS];
1076         gid_t *bsd_gidset;
1077         int ngrp, error;
1078         struct proc *p;
1079
1080         ngrp = args->gidsetsize;
1081         if (ngrp < 0 || ngrp >= NGROUPS)
1082                 return (EINVAL);
1083         error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1084         if (error)
1085                 return (error);
1086         newcred = crget();
1087         p = td->td_proc;
1088         PROC_LOCK(p);
1089         oldcred = p->p_ucred;
1090
1091         /*
1092          * cr_groups[0] holds egid. Setting the whole set from
1093          * the supplied set will cause egid to be changed too.
1094          * Keep cr_groups[0] unchanged to prevent that.
1095          */
1096
1097         if ((error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
1098                 PROC_UNLOCK(p);
1099                 crfree(newcred);
1100                 return (error);
1101         }
1102
1103         crcopy(newcred, oldcred);
1104         if (ngrp > 0) {
1105                 newcred->cr_ngroups = ngrp + 1;
1106
1107                 bsd_gidset = newcred->cr_groups;
1108                 ngrp--;
1109                 while (ngrp >= 0) {
1110                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1111                         ngrp--;
1112                 }
1113         } else
1114                 newcred->cr_ngroups = 1;
1115
1116         setsugid(p);
1117         p->p_ucred = newcred;
1118         PROC_UNLOCK(p);
1119         crfree(oldcred);
1120         return (0);
1121 }
1122
1123 int
1124 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1125 {
1126         struct ucred *cred;
1127         l_gid_t linux_gidset[NGROUPS];
1128         gid_t *bsd_gidset;
1129         int bsd_gidsetsz, ngrp, error;
1130
1131         cred = td->td_ucred;
1132         bsd_gidset = cred->cr_groups;
1133         bsd_gidsetsz = cred->cr_ngroups - 1;
1134
1135         /*
1136          * cr_groups[0] holds egid. Returning the whole set
1137          * here will cause a duplicate. Exclude cr_groups[0]
1138          * to prevent that.
1139          */
1140
1141         if ((ngrp = args->gidsetsize) == 0) {
1142                 td->td_retval[0] = bsd_gidsetsz;
1143                 return (0);
1144         }
1145
1146         if (ngrp < bsd_gidsetsz)
1147                 return (EINVAL);
1148
1149         ngrp = 0;
1150         while (ngrp < bsd_gidsetsz) {
1151                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1152                 ngrp++;
1153         }
1154
1155         if ((error = copyout(linux_gidset, args->grouplist,
1156             ngrp * sizeof(l_gid_t))))
1157                 return (error);
1158
1159         td->td_retval[0] = ngrp;
1160         return (0);
1161 }
1162
1163 #ifndef __alpha__
1164 int
1165 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1166 {
1167         struct rlimit bsd_rlim;
1168         struct l_rlimit rlim;
1169         u_int which;
1170         int error;
1171
1172 #ifdef DEBUG
1173         if (ldebug(setrlimit))
1174                 printf(ARGS(setrlimit, "%d, %p"),
1175                     args->resource, (void *)args->rlim);
1176 #endif
1177
1178         if (args->resource >= LINUX_RLIM_NLIMITS)
1179                 return (EINVAL);
1180
1181         which = linux_to_bsd_resource[args->resource];
1182         if (which == -1)
1183                 return (EINVAL);
1184
1185         error = copyin(args->rlim, &rlim, sizeof(rlim));
1186         if (error)
1187                 return (error);
1188
1189         bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1190         bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1191         return (kern_setrlimit(td, which, &bsd_rlim));
1192 }
1193
1194 int
1195 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1196 {
1197         struct l_rlimit rlim;
1198         struct proc *p = td->td_proc;
1199         struct rlimit bsd_rlim;
1200         u_int which;
1201
1202 #ifdef DEBUG
1203         if (ldebug(old_getrlimit))
1204                 printf(ARGS(old_getrlimit, "%d, %p"),
1205                     args->resource, (void *)args->rlim);
1206 #endif
1207
1208         if (args->resource >= LINUX_RLIM_NLIMITS)
1209                 return (EINVAL);
1210
1211         which = linux_to_bsd_resource[args->resource];
1212         if (which == -1)
1213                 return (EINVAL);
1214
1215         PROC_LOCK(p);
1216         lim_rlimit(p, which, &bsd_rlim);
1217         PROC_UNLOCK(p);
1218
1219 #ifdef COMPAT_LINUX32
1220         rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1221         if (rlim.rlim_cur == UINT_MAX)
1222                 rlim.rlim_cur = INT_MAX;
1223         rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1224         if (rlim.rlim_max == UINT_MAX)
1225                 rlim.rlim_max = INT_MAX;
1226 #else
1227         rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1228         if (rlim.rlim_cur == ULONG_MAX)
1229                 rlim.rlim_cur = LONG_MAX;
1230         rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1231         if (rlim.rlim_max == ULONG_MAX)
1232                 rlim.rlim_max = LONG_MAX;
1233 #endif
1234         return (copyout(&rlim, args->rlim, sizeof(rlim)));
1235 }
1236
1237 int
1238 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1239 {
1240         struct l_rlimit rlim;
1241         struct proc *p = td->td_proc;
1242         struct rlimit bsd_rlim;
1243         u_int which;
1244
1245 #ifdef DEBUG
1246         if (ldebug(getrlimit))
1247                 printf(ARGS(getrlimit, "%d, %p"),
1248                     args->resource, (void *)args->rlim);
1249 #endif
1250
1251         if (args->resource >= LINUX_RLIM_NLIMITS)
1252                 return (EINVAL);
1253
1254         which = linux_to_bsd_resource[args->resource];
1255         if (which == -1)
1256                 return (EINVAL);
1257
1258         PROC_LOCK(p);
1259         lim_rlimit(p, which, &bsd_rlim);
1260         PROC_UNLOCK(p);
1261
1262         rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1263         rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1264         return (copyout(&rlim, args->rlim, sizeof(rlim)));
1265 }
1266 #endif /*!__alpha__*/
1267
1268 int
1269 linux_sched_setscheduler(struct thread *td,
1270     struct linux_sched_setscheduler_args *args)
1271 {
1272         struct sched_setscheduler_args bsd;
1273
1274 #ifdef DEBUG
1275         if (ldebug(sched_setscheduler))
1276                 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1277                     args->pid, args->policy, (const void *)args->param);
1278 #endif
1279
1280         switch (args->policy) {
1281         case LINUX_SCHED_OTHER:
1282                 bsd.policy = SCHED_OTHER;
1283                 break;
1284         case LINUX_SCHED_FIFO:
1285                 bsd.policy = SCHED_FIFO;
1286                 break;
1287         case LINUX_SCHED_RR:
1288                 bsd.policy = SCHED_RR;
1289                 break;
1290         default:
1291                 return EINVAL;
1292         }
1293
1294         bsd.pid = args->pid;
1295         bsd.param = (struct sched_param *)args->param;
1296         return sched_setscheduler(td, &bsd);
1297 }
1298
1299 int
1300 linux_sched_getscheduler(struct thread *td,
1301     struct linux_sched_getscheduler_args *args)
1302 {
1303         struct sched_getscheduler_args bsd;
1304         int error;
1305
1306 #ifdef DEBUG
1307         if (ldebug(sched_getscheduler))
1308                 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1309 #endif
1310
1311         bsd.pid = args->pid;
1312         error = sched_getscheduler(td, &bsd);
1313
1314         switch (td->td_retval[0]) {
1315         case SCHED_OTHER:
1316                 td->td_retval[0] = LINUX_SCHED_OTHER;
1317                 break;
1318         case SCHED_FIFO:
1319                 td->td_retval[0] = LINUX_SCHED_FIFO;
1320                 break;
1321         case SCHED_RR:
1322                 td->td_retval[0] = LINUX_SCHED_RR;
1323                 break;
1324         }
1325
1326         return error;
1327 }
1328
1329 int
1330 linux_sched_get_priority_max(struct thread *td,
1331     struct linux_sched_get_priority_max_args *args)
1332 {
1333         struct sched_get_priority_max_args bsd;
1334
1335 #ifdef DEBUG
1336         if (ldebug(sched_get_priority_max))
1337                 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1338 #endif
1339
1340         switch (args->policy) {
1341         case LINUX_SCHED_OTHER:
1342                 bsd.policy = SCHED_OTHER;
1343                 break;
1344         case LINUX_SCHED_FIFO:
1345                 bsd.policy = SCHED_FIFO;
1346                 break;
1347         case LINUX_SCHED_RR:
1348                 bsd.policy = SCHED_RR;
1349                 break;
1350         default:
1351                 return EINVAL;
1352         }
1353         return sched_get_priority_max(td, &bsd);
1354 }
1355
1356 int
1357 linux_sched_get_priority_min(struct thread *td,
1358     struct linux_sched_get_priority_min_args *args)
1359 {
1360         struct sched_get_priority_min_args bsd;
1361
1362 #ifdef DEBUG
1363         if (ldebug(sched_get_priority_min))
1364                 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1365 #endif
1366
1367         switch (args->policy) {
1368         case LINUX_SCHED_OTHER:
1369                 bsd.policy = SCHED_OTHER;
1370                 break;
1371         case LINUX_SCHED_FIFO:
1372                 bsd.policy = SCHED_FIFO;
1373                 break;
1374         case LINUX_SCHED_RR:
1375                 bsd.policy = SCHED_RR;
1376                 break;
1377         default:
1378                 return EINVAL;
1379         }
1380         return sched_get_priority_min(td, &bsd);
1381 }
1382
1383 #define REBOOT_CAD_ON   0x89abcdef
1384 #define REBOOT_CAD_OFF  0
1385 #define REBOOT_HALT     0xcdef0123
1386 #define REBOOT_RESTART  0x01234567
1387 #define REBOOT_RESTART2 0xA1B2C3D4
1388 #define REBOOT_POWEROFF 0x4321FEDC
1389 #define REBOOT_MAGIC1   0xfee1dead
1390 #define REBOOT_MAGIC2   0x28121969
1391 #define REBOOT_MAGIC2A  0x05121996
1392 #define REBOOT_MAGIC2B  0x16041998
1393
1394 int
1395 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1396 {
1397         struct reboot_args bsd_args;
1398
1399 #ifdef DEBUG
1400         if (ldebug(reboot))
1401                 printf(ARGS(reboot, "0x%x"), args->cmd);
1402 #endif
1403
1404         if (args->magic1 != REBOOT_MAGIC1)
1405                 return EINVAL;
1406
1407         switch (args->magic2) {
1408         case REBOOT_MAGIC2:
1409         case REBOOT_MAGIC2A:
1410         case REBOOT_MAGIC2B:
1411                 break;
1412         default:
1413                 return EINVAL;
1414         }
1415
1416         switch (args->cmd) {
1417         case REBOOT_CAD_ON:
1418         case REBOOT_CAD_OFF:
1419                 return suser(td);
1420         case REBOOT_HALT:
1421                 bsd_args.opt = RB_HALT;
1422                 break;
1423         case REBOOT_RESTART:
1424         case REBOOT_RESTART2:
1425                 bsd_args.opt = 0;
1426                 break;
1427         case REBOOT_POWEROFF:
1428                 bsd_args.opt = RB_POWEROFF;
1429                 break;
1430         default:
1431                 return EINVAL;
1432         }
1433         return reboot(td, &bsd_args);
1434 }
1435
1436 #ifndef __alpha__
1437
1438 /*
1439  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1440  * td->td_retval[1] when COMPAT_43 is defined. This
1441  * globbers registers that are assumed to be preserved. The following
1442  * lightweight syscalls fixes this. See also linux_getgid16() and
1443  * linux_getuid16() in linux_uid16.c.
1444  *
1445  * linux_getpid() - MP SAFE
1446  * linux_getgid() - MP SAFE
1447  * linux_getuid() - MP SAFE
1448  */
1449
1450 int
1451 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1452 {
1453
1454         td->td_retval[0] = td->td_proc->p_pid;
1455         return (0);
1456 }
1457
1458 int
1459 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1460 {
1461
1462         td->td_retval[0] = td->td_ucred->cr_rgid;
1463         return (0);
1464 }
1465
1466 int
1467 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1468 {
1469
1470         td->td_retval[0] = td->td_ucred->cr_ruid;
1471         return (0);
1472 }
1473
1474 #endif /*!__alpha__*/
1475
1476 int
1477 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1478 {
1479         struct getsid_args bsd;
1480         bsd.pid = args->pid;
1481         return getsid(td, &bsd);
1482 }
1483
1484 int
1485 linux_nosys(struct thread *td, struct nosys_args *ignore)
1486 {
1487
1488         return (ENOSYS);
1489 }
1490
1491 int
1492 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1493 {
1494         struct getpriority_args bsd_args;
1495         int error;
1496
1497         bsd_args.which = args->which;
1498         bsd_args.who = args->who;
1499         error = getpriority(td, &bsd_args);
1500         td->td_retval[0] = 20 - td->td_retval[0];
1501         return error;
1502 }
1503
1504 int
1505 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1506 {
1507         int name[2];
1508
1509 #ifdef DEBUG
1510         if (ldebug(sethostname))
1511                 printf(ARGS(sethostname, "*, %i"), args->len);
1512 #endif
1513
1514         name[0] = CTL_KERN;
1515         name[1] = KERN_HOSTNAME;
1516         return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1517             args->len, 0, 0));
1518 }
1519