1 /*-
2 * Copyright (c) 1994-1995 Søren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: src/sys/compat/linux/linux_misc.c,v 1.85.2.9 2002/09/24 08:11:41 mdodd Exp $
29 */
30
31 #include "opt_compat.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact_aout.h>
37 #include <sys/kernel.h>
38 #include <sys/kern_syscall.h>
39 #include <sys/lock.h>
40 #include <sys/mman.h>
41 #include <sys/mount.h>
42 #include <sys/poll.h>
43 #include <sys/proc.h>
44 #include <sys/priv.h>
45 #include <sys/nlookup.h>
46 #include <sys/blist.h>
47 #include <sys/reboot.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
50 #include <sys/stat.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysproto.h>
53 #include <sys/time.h>
54 #include <sys/unistd.h>
55 #include <sys/vmmeter.h>
56 #include <sys/vnode.h>
57 #include <sys/wait.h>
58
59 #include <sys/signal2.h>
60 #include <sys/thread2.h>
61 #include <sys/mplock2.h>
62 #include <sys/spinlock2.h>
63
64 #include <vm/vm.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_zone.h>
71 #include <vm/swap_pager.h>
72
73 #include <machine/frame.h>
74 #include <machine/limits.h>
75 #include <machine/psl.h>
76 #include <machine/sysarch.h>
77 #ifdef __i386__
78 #include <machine/segments.h>
79 #endif
80
81 #include <sys/sched.h>
82
83 #include <emulation/linux/linux_sysproto.h>
84 #include <arch_linux/linux.h>
85 #include <arch_linux/linux_proto.h>
86 #include "linux_mib.h"
87 #include "linux_util.h"
88 #include "linux_emuldata.h"
89 #include "i386/linux.h"
90
91 #define BSD_TO_LINUX_SIGNAL(sig) \
92 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
93
94 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
95 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
96 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
97 RLIMIT_MEMLOCK, -1
98 };
99
100 struct l_sysinfo {
101 l_long uptime; /* Seconds since boot */
102 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
103 l_ulong totalram; /* Total usable main memory size */
104 l_ulong freeram; /* Available memory size */
105 l_ulong sharedram; /* Amount of shared memory */
106 l_ulong bufferram; /* Memory used by buffers */
107 l_ulong totalswap; /* Total swap space size */
108 l_ulong freeswap; /* swap space still available */
109 l_ushort procs; /* Number of current processes */
110 l_ushort pad; /* explicit padding */
111 l_ulong totalhigh; /* Total high memory size */
112 l_ulong freehigh; /* Available high memory size */
113 l_uint mem_unit; /* Memory unit size in bytes */
114 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* Padding for libc5 */
115 };
116
117 int
118 sys_linux_madvise(struct linux_madvise_args *args)
119 {
120 return 0;
121 }
122
123 /*
124 * MPALMOSTSAFE
125 */
126 int
127 sys_linux_sysinfo(struct linux_sysinfo_args *args)
128 {
129 struct l_sysinfo sysinfo;
130 struct timespec ts;
131 int error;
132 int i;
133 int n;
134
135 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
136 getnanouptime(&ts);
137 i = 0;
138 if (ts.tv_sec >= 86400) {
139 ts.tv_sec %= 86400;
140 i = 1;
141 }
142 if (i || ts.tv_sec >= 3600) {
143 ts.tv_sec %= 3600;
144 i = 1;
145 }
146 if (i || ts.tv_sec >= 60) {
147 ts.tv_sec %= 60;
148 i = 1;
149 }
150
151 bzero(&sysinfo, sizeof(sysinfo));
152 sysinfo.uptime=ts.tv_sec;
153
154 /* Use the information from the mib to get our load averages */
155 for (i = 0; i < 3; i++)
156 sysinfo.loads[i] = averunnable.ldavg[i];
157
158 sysinfo.totalram = Maxmem * PAGE_SIZE;
159 sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
160 sysinfo.sharedram = 0;
161
162 for (n = 0; n < ncpus; ++n) {
163 globaldata_t gd = globaldata_find(n);
164
165 sysinfo.sharedram += gd->gd_vmtotal.t_avmshr;
166 }
167 sysinfo.sharedram *= PAGE_SIZE;
168 sysinfo.bufferram = 0;
169
170 if (swapblist == NULL) {
171 sysinfo.totalswap= 0;
172 sysinfo.freeswap = 0;
173 } else {
174 sysinfo.totalswap = swapblist->bl_blocks * 1024;
175 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
176 }
177
178 sysinfo.procs = nprocs;
179 sysinfo.totalhigh = 0;
180 sysinfo.freehigh = 0;
181 sysinfo.mem_unit = 1; /* Set the basic mem unit to 1 */
182
183 error = copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
184 return (error);
185 }
186
187 /*
188 * MPALMOSTSAFE
189 */
190 int
191 sys_linux_alarm(struct linux_alarm_args *args)
192 {
193 struct thread *td = curthread;
194 struct proc *p = td->td_proc;
195 struct itimerval it, old_it;
196 struct timeval tv;
197
198 #ifdef DEBUG
199 if (ldebug(alarm))
200 kprintf(ARGS(alarm, "%u"), args->secs);
201 #endif
202
203 if (args->secs > 100000000)
204 return EINVAL;
205
206 it.it_value.tv_sec = (long)args->secs;
207 it.it_value.tv_usec = 0;
208 it.it_interval.tv_sec = 0;
209 it.it_interval.tv_usec = 0;
210 get_mplock();
211 crit_enter();
212 old_it = p->p_realtimer;
213 getmicrouptime(&tv);
214 if (timevalisset(&old_it.it_value))
215 callout_stop(&p->p_ithandle);
216 if (it.it_value.tv_sec != 0) {
217 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
218 realitexpire, p);
219 timevaladd(&it.it_value, &tv);
220 }
221 p->p_realtimer = it;
222 crit_exit();
223 rel_mplock();
224 if (timevalcmp(&old_it.it_value, &tv, >)) {
225 timevalsub(&old_it.it_value, &tv);
226 if (old_it.it_value.tv_usec != 0)
227 old_it.it_value.tv_sec++;
228 args->sysmsg_result = old_it.it_value.tv_sec;
229 }
230 return 0;
231 }
232
233 /*
234 * MPALMOSTSAFE
235 */
236 int
237 sys_linux_brk(struct linux_brk_args *args)
238 {
239 struct thread *td = curthread;
240 struct proc *p = td->td_proc;
241 struct vmspace *vm;
242 vm_offset_t new, old;
243 struct obreak_args bsd_args;
244
245 get_mplock();
246 vm = p->p_vmspace;
247 #ifdef DEBUG
248 if (ldebug(brk))
249 kprintf(ARGS(brk, "%p"), (void *)args->dsend);
250 #endif
251 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
252 new = (vm_offset_t)args->dsend;
253 bsd_args.sysmsg_result = 0;
254 bsd_args.nsize = (char *) new;
255 bsd_args.sysmsg_result = 0;
256 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(&bsd_args))
257 args->sysmsg_result = (long)new;
258 else
259 args->sysmsg_result = (long)old;
260 rel_mplock();
261
262 return 0;
263 }
264
265 /*
266 * MPALMOSTSAFE
267 */
268 int
269 sys_linux_uselib(struct linux_uselib_args *args)
270 {
271 struct thread *td = curthread;
272 struct proc *p;
273 struct nlookupdata nd;
274 struct vnode *vp;
275 struct exec *a_out;
276 struct vattr attr;
277 vm_offset_t vmaddr;
278 unsigned long file_offset;
279 vm_offset_t buffer;
280 unsigned long bss_size;
281 int error;
282 int locked;
283 char *path;
284
285 p = td->td_proc;
286
287 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
288 if (error)
289 return (error);
290 #ifdef DEBUG
291 if (ldebug(uselib))
292 kprintf(ARGS(uselib, "%s"), path);
293 #endif
294
295 a_out = NULL;
296 locked = 0;
297 vp = NULL;
298
299 get_mplock();
300 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
301 nd.nl_flags |= NLC_EXEC;
302 if (error == 0)
303 error = nlookup(&nd);
304 if (error == 0)
305 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
306 if (error)
307 goto cleanup;
308 /*
309 * From here on down, we have a locked vnode that must be unlocked.
310 */
311 locked = 1;
312
313 /* Writable? */
314 if (vp->v_writecount) {
315 error = ETXTBSY;
316 goto cleanup;
317 }
318
319 /* Executable? */
320 error = VOP_GETATTR(vp, &attr);
321 if (error)
322 goto cleanup;
323
324 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
325 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
326 error = ENOEXEC;
327 goto cleanup;
328 }
329
330 /* Sensible size? */
331 if (attr.va_size == 0) {
332 error = ENOEXEC;
333 goto cleanup;
334 }
335
336 error = VOP_OPEN(vp, FREAD, td->td_ucred, NULL);
337 if (error)
338 goto cleanup;
339
340 /*
341 * Lock no longer needed
342 */
343 vn_unlock(vp);
344 locked = 0;
345
346 /* Pull in executable header into kernel_map */
347 error = vm_mmap(&kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
348 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
349 if (error)
350 goto cleanup;
351
352 /* Is it a Linux binary ? */
353 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
354 error = ENOEXEC;
355 goto cleanup;
356 }
357
358 /*
359 * While we are here, we should REALLY do some more checks
360 */
361
362 /* Set file/virtual offset based on a.out variant. */
363 switch ((int)(a_out->a_magic & 0xffff)) {
364 case 0413: /* ZMAGIC */
365 file_offset = 1024;
366 break;
367 case 0314: /* QMAGIC */
368 file_offset = 0;
369 break;
370 default:
371 error = ENOEXEC;
372 goto cleanup;
373 }
374
375 bss_size = round_page(a_out->a_bss);
376
377 /* Check various fields in header for validity/bounds. */
378 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
379 error = ENOEXEC;
380 goto cleanup;
381 }
382
383 /* text + data can't exceed file size */
384 if (a_out->a_data + a_out->a_text > attr.va_size) {
385 error = EFAULT;
386 goto cleanup;
387 }
388
389 /*
390 * text/data/bss must not exceed limits
391 * XXX - this is not complete. it should check current usage PLUS
392 * the resources needed by this library.
393 */
394 if (a_out->a_text > maxtsiz ||
395 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
396 error = ENOMEM;
397 goto cleanup;
398 }
399
400 /* prevent more writers */
401 vsetflags(vp, VTEXT);
402
403 /*
404 * Check if file_offset page aligned. Currently we cannot handle
405 * misalinged file offsets, and so we read in the entire image
406 * (what a waste).
407 */
408 if (file_offset & PAGE_MASK) {
409 #ifdef DEBUG
410 kprintf("uselib: Non page aligned binary %lu\n", file_offset);
411 #endif
412 /* Map text+data read/write/execute */
413
414 /* a_entry is the load address and is page aligned */
415 vmaddr = trunc_page(a_out->a_entry);
416
417 /* get anon user mapping, read+write+execute */
418 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
419 &vmaddr, a_out->a_text + a_out->a_data,
420 PAGE_SIZE,
421 FALSE, VM_MAPTYPE_NORMAL,
422 VM_PROT_ALL, VM_PROT_ALL,
423 0);
424 if (error)
425 goto cleanup;
426
427 /* map file into kernel_map */
428 error = vm_mmap(&kernel_map, &buffer,
429 round_page(a_out->a_text + a_out->a_data + file_offset),
430 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
431 trunc_page(file_offset));
432 if (error)
433 goto cleanup;
434
435 /* copy from kernel VM space to user space */
436 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
437 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
438
439 /* release temporary kernel space */
440 vm_map_remove(&kernel_map, buffer, buffer +
441 round_page(a_out->a_text + a_out->a_data + file_offset));
442
443 if (error)
444 goto cleanup;
445 } else {
446 #ifdef DEBUG
447 kprintf("uselib: Page aligned binary %lu\n", file_offset);
448 #endif
449 /*
450 * for QMAGIC, a_entry is 20 bytes beyond the load address
451 * to skip the executable header
452 */
453 vmaddr = trunc_page(a_out->a_entry);
454
455 /*
456 * Map it all into the process's space as a single
457 * copy-on-write "data" segment.
458 */
459 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
460 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
461 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
462 if (error)
463 goto cleanup;
464 }
465 #ifdef DEBUG
466 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
467 ((long*)vmaddr)[1]);
468 #endif
469 if (bss_size != 0) {
470 /* Calculate BSS start address */
471 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
472 a_out->a_data;
473
474 /* allocate some 'anon' space */
475 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
476 &vmaddr, bss_size,
477 PAGE_SIZE,
478 FALSE, VM_MAPTYPE_NORMAL,
479 VM_PROT_ALL, VM_PROT_ALL,
480 0);
481 if (error)
482 goto cleanup;
483 }
484
485 cleanup:
486 /* Unlock/release vnode */
487 if (vp) {
488 if (locked)
489 vn_unlock(vp);
490 vrele(vp);
491 }
492 /* Release the kernel mapping. */
493 if (a_out) {
494 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
495 (vm_offset_t)a_out + PAGE_SIZE);
496 }
497 nlookup_done(&nd);
498 rel_mplock();
499 linux_free_path(&path);
500 return (error);
501 }
502
503 /*
504 * MPSAFE
505 */
506 int
507 sys_linux_select(struct linux_select_args *args)
508 {
509 struct select_args bsa;
510 struct timeval tv0, tv1, utv, *tvp;
511 caddr_t sg;
512 int error;
513
514 #ifdef DEBUG
515 if (ldebug(select))
516 kprintf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
517 (void *)args->readfds, (void *)args->writefds,
518 (void *)args->exceptfds, (void *)args->timeout);
519 #endif
520
521 error = 0;
522 bsa.sysmsg_result = 0;
523 bsa.nd = args->nfds;
524 bsa.in = args->readfds;
525 bsa.ou = args->writefds;
526 bsa.ex = args->exceptfds;
527 bsa.tv = (struct timeval *)args->timeout;
528
529 /*
530 * Store current time for computation of the amount of
531 * time left.
532 */
533 if (args->timeout) {
534 if ((error = copyin((caddr_t)args->timeout, &utv,
535 sizeof(utv))))
536 goto select_out;
537 #ifdef DEBUG
538 if (ldebug(select))
539 kprintf(LMSG("incoming timeout (%ld/%ld)"),
540 utv.tv_sec, utv.tv_usec);
541 #endif
542
543 if (itimerfix(&utv)) {
544 /*
545 * The timeval was invalid. Convert it to something
546 * valid that will act as it does under Linux.
547 */
548 sg = stackgap_init();
549 tvp = stackgap_alloc(&sg, sizeof(utv));
550 utv.tv_sec += utv.tv_usec / 1000000;
551 utv.tv_usec %= 1000000;
552 if (utv.tv_usec < 0) {
553 utv.tv_sec -= 1;
554 utv.tv_usec += 1000000;
555 }
556 if (utv.tv_sec < 0)
557 timevalclear(&utv);
558 if ((error = copyout(&utv, tvp, sizeof(utv))))
559 goto select_out;
560 bsa.tv = tvp;
561 }
562 microtime(&tv0);
563 }
564
565 error = sys_select(&bsa);
566 args->sysmsg_result = bsa.sysmsg_result;
567 #ifdef DEBUG
568 if (ldebug(select))
569 kprintf(LMSG("real select returns %d"), error);
570 #endif
571 if (error) {
572 /*
573 * See fs/select.c in the Linux kernel. Without this,
574 * Maelstrom doesn't work.
575 */
576 if (error == ERESTART)
577 error = EINTR;
578 goto select_out;
579 }
580
581 if (args->timeout) {
582 if (args->sysmsg_result) {
583 /*
584 * Compute how much time was left of the timeout,
585 * by subtracting the current time and the time
586 * before we started the call, and subtracting
587 * that result from the user-supplied value.
588 */
589 microtime(&tv1);
590 timevalsub(&tv1, &tv0);
591 timevalsub(&utv, &tv1);
592 if (utv.tv_sec < 0)
593 timevalclear(&utv);
594 } else
595 timevalclear(&utv);
596 #ifdef DEBUG
597 if (ldebug(select))
598 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
599 utv.tv_sec, utv.tv_usec);
600 #endif
601 if ((error = copyout(&utv, (caddr_t)args->timeout,
602 sizeof(utv))))
603 goto select_out;
604 }
605
606 select_out:
607 #ifdef DEBUG
608 if (ldebug(select))
609 kprintf(LMSG("select_out -> %d"), error);
610 #endif
611 return error;
612 }
613
614 /*
615 * MPSAFE
616 */
617 int
618 sys_linux_mremap(struct linux_mremap_args *args)
619 {
620 struct munmap_args bsd_args;
621 int error = 0;
622
623 #ifdef DEBUG
624 if (ldebug(mremap))
625 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
626 (void *)args->addr,
627 (unsigned long)args->old_len,
628 (unsigned long)args->new_len,
629 (unsigned long)args->flags);
630 #endif
631 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
632 args->sysmsg_resultp = NULL;
633 return (EINVAL);
634 }
635
636 /*
637 * Check for the page alignment.
638 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
639 */
640 if (args->addr & PAGE_MASK) {
641 args->sysmsg_resultp = NULL;
642 return (EINVAL);
643 }
644
645 args->new_len = round_page(args->new_len);
646 args->old_len = round_page(args->old_len);
647
648 if (args->new_len > args->old_len) {
649 args->sysmsg_result = 0;
650 return ENOMEM;
651 }
652
653 if (args->new_len < args->old_len) {
654 bsd_args.sysmsg_result = 0;
655 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
656 bsd_args.len = args->old_len - args->new_len;
657 error = sys_munmap(&bsd_args);
658 }
659
660 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
661 return error;
662 }
663
664 #define LINUX_MS_ASYNC 0x0001
665 #define LINUX_MS_INVALIDATE 0x0002
666 #define LINUX_MS_SYNC 0x0004
667
668 /*
669 * MPSAFE
670 */
671 int
672 sys_linux_msync(struct linux_msync_args *args)
673 {
674 struct msync_args bsd_args;
675 int error;
676
677 bsd_args.addr = (caddr_t)args->addr;
678 bsd_args.len = args->len;
679 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
680 bsd_args.sysmsg_result = 0;
681
682 error = sys_msync(&bsd_args);
683 args->sysmsg_result = bsd_args.sysmsg_result;
684 return(error);
685 }
686
687 /*
688 * MPSAFE
689 */
690 int
691 sys_linux_time(struct linux_time_args *args)
692 {
693 struct timeval tv;
694 l_time_t tm;
695 int error;
696
697 #ifdef DEBUG
698 if (ldebug(time))
699 kprintf(ARGS(time, "*"));
700 #endif
701
702 microtime(&tv);
703 tm = tv.tv_sec;
704 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
705 return error;
706 args->sysmsg_lresult = tm;
707 return 0;
708 }
709
710 struct l_times_argv {
711 l_long tms_utime;
712 l_long tms_stime;
713 l_long tms_cutime;
714 l_long tms_cstime;
715 };
716
717 #define CLK_TCK 100 /* Linux uses 100 */
718
719 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
720
721 /*
722 * MPALMOSTSAFE
723 */
724 int
725 sys_linux_times(struct linux_times_args *args)
726 {
727 struct thread *td = curthread;
728 struct proc *p = td->td_proc;
729 struct timeval tv;
730 struct l_times_argv tms;
731 struct rusage ru;
732 int error;
733
734 #ifdef DEBUG
735 if (ldebug(times))
736 kprintf(ARGS(times, "*"));
737 #endif
738
739 get_mplock();
740 calcru_proc(p, &ru);
741 rel_mplock();
742
743 tms.tms_utime = CONVTCK(ru.ru_utime);
744 tms.tms_stime = CONVTCK(ru.ru_stime);
745
746 tms.tms_cutime = CONVTCK(p->p_cru.ru_utime);
747 tms.tms_cstime = CONVTCK(p->p_cru.ru_stime);
748
749 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
750 return error;
751
752 microuptime(&tv);
753 args->sysmsg_result = (int)CONVTCK(tv);
754 return 0;
755 }
756
757 /*
758 * MPALMOSTSAFE
759 */
760 int
761 sys_linux_newuname(struct linux_newuname_args *args)
762 {
763 struct thread *td = curthread;
764 struct l_new_utsname utsname;
765 char *osrelease, *osname;
766
767 #ifdef DEBUG
768 if (ldebug(newuname))
769 kprintf(ARGS(newuname, "*"));
770 #endif
771
772 get_mplock();
773 osname = linux_get_osname(td);
774 osrelease = linux_get_osrelease(td);
775
776 bzero(&utsname, sizeof(utsname));
777 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
778 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
779 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
780 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
781 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
782 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
783 rel_mplock();
784
785 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
786 }
787
788 /* XXX: why would this be i386-only? most of these are wrong! */
789 #if defined(__i386__)
790 struct l_utimbuf {
791 l_time_t l_actime;
792 l_time_t l_modtime;
793 };
794
795 /*
796 * MPALMOSTSAFE
797 */
798 int
799 sys_linux_utime(struct linux_utime_args *args)
800 {
801 struct timeval tv[2];
802 struct l_utimbuf lut;
803 struct nlookupdata nd;
804 char *path;
805 int error;
806
807 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
808 if (error)
809 return (error);
810 #ifdef DEBUG
811 if (ldebug(utime))
812 kprintf(ARGS(utime, "%s, *"), path);
813 #endif
814
815 if (args->times) {
816 error = copyin(args->times, &lut, sizeof(lut));
817 if (error)
818 goto cleanup;
819 tv[0].tv_sec = lut.l_actime;
820 tv[0].tv_usec = 0;
821 tv[1].tv_sec = lut.l_modtime;
822 tv[1].tv_usec = 0;
823 }
824 get_mplock();
825 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
826 if (error == 0)
827 error = kern_utimes(&nd, args->times ? tv : NULL);
828 nlookup_done(&nd);
829 rel_mplock();
830 cleanup:
831 linux_free_path(&path);
832 return (error);
833 }
834
835 int
836 sys_linux_utimes(struct linux_utimes_args *args)
837 {
838 l_timeval ltv[2];
839 struct timeval tv[2], *tvp = NULL;
840 struct nlookupdata nd;
841 char *path;
842 int error;
843
844 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
845 if (error)
846 return (error);
847 #ifdef DEBUG
848 if (ldebug(utimes))
849 kprintf(ARGS(utimes, "%s, *"), path);
850 #endif
851
852 if (args->tptr) {
853 error = copyin(args->tptr, ltv, sizeof(ltv));
854 if (error)
855 goto cleanup;
856 tv[0].tv_sec = ltv[0].tv_sec;
857 tv[0].tv_usec = ltv[0].tv_usec;
858 tv[1].tv_sec = ltv[1].tv_sec;
859 tv[1].tv_usec = ltv[1].tv_usec;
860 tvp = tv;
861 }
862 get_mplock();
863 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
864 if (error == 0)
865 error = kern_utimes(&nd, tvp);
866 nlookup_done(&nd);
867 rel_mplock();
868 cleanup:
869 linux_free_path(&path);
870 return (error);
871 }
872
873 int
874 sys_linux_futimesat(struct linux_futimesat_args *args)
875 {
876 l_timeval ltv[2];
877 struct timeval tv[2], *tvp = NULL;
878 struct file *fp;
879 struct nlookupdata nd;
880 char *path;
881 int dfd,error;
882
883 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
884 if (error)
885 return (error);
886 #ifdef DEBUG
887 if (ldebug(futimesat))
888 kprintf(ARGS(futimesat, "%s, *"), path);
889 #endif
890 if (args->tptr) {
891 error = copyin(args->tptr, ltv, sizeof(ltv));
892 if (error)
893 goto cleanup;
894 tv[0].tv_sec = ltv[0].tv_sec;
895 tv[0].tv_usec = ltv[0].tv_usec;
896 tv[1].tv_sec = ltv[1].tv_sec;
897 tv[1].tv_usec = ltv[1].tv_usec;
898 tvp = tv;
899 }
900 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
901 get_mplock();
902 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, NLC_FOLLOW);
903 if (error == 0)
904 error = kern_utimes(&nd, tvp);
905 nlookup_done_at(&nd, fp);
906 rel_mplock();
907 cleanup:
908 linux_free_path(&path);
909 return (error);
910 }
911
912
913 int
914 sys_linux_utimensat(struct linux_utimensat_args *args)
915 {
916 struct l_timespec ltv[2];
917 struct timeval tv[2], *tvp = NULL;
918 struct file *fp;
919 struct nlookupdata nd;
920 char *path;
921 int dfd, flags, error = 0;
922
923 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
924 return (EINVAL);
925
926 if (args->dfd == LINUX_AT_FDCWD && args->fname == NULL)
927 return (EINVAL);
928
929 if (args->fname) {
930 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
931 if (error)
932 return (error);
933 }
934 #ifdef DEBUG
935 if (ldebug(utimensat))
936 kprintf(ARGS(utimensat, "%s, *"), path);
937 #endif
938 if (args->tptr) {
939 error = copyin(args->tptr, ltv, sizeof(ltv));
940 if (error)
941 goto cleanup;
942
943 if (ltv[0].tv_sec == LINUX_UTIME_NOW) {
944 microtime(&tv[0]);
945 } else if (ltv[0].tv_sec == LINUX_UTIME_OMIT) {
946 /* XXX: this is not right, but will do for now */
947 microtime(&tv[0]);
948 } else {
949 tv[0].tv_sec = ltv[0].tv_sec;
950 /* XXX: we lose precision here, as we don't have ns */
951 tv[0].tv_usec = ltv[0].tv_nsec/1000;
952 }
953 if (ltv[1].tv_sec == LINUX_UTIME_NOW) {
954 microtime(&tv[1]);
955 } else if (ltv[1].tv_sec == LINUX_UTIME_OMIT) {
956 /* XXX: this is not right, but will do for now */
957 microtime(&tv[1]);
958 } else {
959 tv[1].tv_sec = ltv[1].tv_sec;
960 /* XXX: we lose precision here, as we don't have ns */
961 tv[1].tv_usec = ltv[1].tv_nsec/1000;
962 }
963 tvp = tv;
964 }
965
966 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
967 flags = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
968
969 get_mplock();
970 if (args->fname) {
971 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, flags);
972 if (error == 0)
973 error = kern_utimes(&nd, tvp);
974 nlookup_done_at(&nd, fp);
975 } else {
976 /* Thank you, Linux, for another non-standard "feature" */
977 KKASSERT(dfd != AT_FDCWD);
978 error = kern_futimes(dfd, tvp);
979 }
980 rel_mplock();
981 cleanup:
982 if (args->fname)
983 linux_free_path(&path);
984
985 return (error);
986 }
987 #endif /* __i386__ */
988
989 #define __WCLONE 0x80000000
990
991 /*
992 * MPALMOSTSAFE
993 */
994 int
995 sys_linux_waitpid(struct linux_waitpid_args *args)
996 {
997 int error, options, status;
998
999 #ifdef DEBUG
1000 if (ldebug(waitpid))
1001 kprintf(ARGS(waitpid, "%d, %p, %d"),
1002 args->pid, (void *)args->status, args->options);
1003 #endif
1004 options = args->options & (WNOHANG | WUNTRACED);
1005 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1006 if (args->options & __WCLONE)
1007 options |= WLINUXCLONE;
1008
1009 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1010 NULL, &args->sysmsg_result);
1011
1012 if (error == 0 && args->status) {
1013 status &= 0xffff;
1014 if (WIFSIGNALED(status))
1015 status = (status & 0xffffff80) |
1016 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1017 else if (WIFSTOPPED(status))
1018 status = (status & 0xffff00ff) |
1019 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1020 error = copyout(&status, args->status, sizeof(status));
1021 }
1022
1023 return (error);
1024 }
1025
1026 /*
1027 * MPALMOSTSAFE
1028 */
1029 int
1030 sys_linux_wait4(struct linux_wait4_args *args)
1031 {
1032 struct thread *td = curthread;
1033 struct lwp *lp = td->td_lwp;
1034 struct rusage rusage;
1035 int error, options, status;
1036
1037 #ifdef DEBUG
1038 if (ldebug(wait4))
1039 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
1040 args->pid, (void *)args->status, args->options,
1041 (void *)args->rusage);
1042 #endif
1043 options = args->options & (WNOHANG | WUNTRACED);
1044 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1045 if (args->options & __WCLONE)
1046 options |= WLINUXCLONE;
1047
1048 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1049 args->rusage ? &rusage : NULL, &args->sysmsg_result);
1050
1051 if (error == 0) {
1052 spin_lock(&lp->lwp_spin);
1053 lwp_delsig(lp, SIGCHLD);
1054 spin_unlock(&lp->lwp_spin);
1055 }
1056
1057 if (error == 0 && args->status) {
1058 status &= 0xffff;
1059 if (WIFSIGNALED(status))
1060 status = (status & 0xffffff80) |
1061 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1062 else if (WIFSTOPPED(status))
1063 status = (status & 0xffff00ff) |
1064 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1065 error = copyout(&status, args->status, sizeof(status));
1066 }
1067 if (error == 0 && args->rusage)
1068 error = copyout(&rusage, args->rusage, sizeof(rusage));
1069
1070 return (error);
1071 }
1072
1073 /*
1074 * MPALMOSTSAFE
1075 */
1076 int
1077 sys_linux_mknod(struct linux_mknod_args *args)
1078 {
1079 struct nlookupdata nd;
1080 char *path;
1081 int error;
1082
1083 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1084 if (error)
1085 return (error);
1086 #ifdef DEBUG
1087 if (ldebug(mknod))
1088 kprintf(ARGS(mknod, "%s, %d, %d"),
1089 path, args->mode, args->dev);
1090 #endif
1091 get_mplock();
1092 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
1093 if (error == 0) {
1094 if (args->mode & S_IFIFO) {
1095 error = kern_mkfifo(&nd, args->mode);
1096 } else {
1097 error = kern_mknod(&nd, args->mode,
1098 umajor(args->dev),
1099 uminor(args->dev));
1100 }
1101 }
1102 nlookup_done(&nd);
1103 rel_mplock();
1104
1105 linux_free_path(&path);
1106 return(error);
1107 }
1108
1109 int
1110 sys_linux_mknodat(struct linux_mknodat_args *args)
1111 {
1112 struct nlookupdata nd;
1113 struct file *fp;
1114 char *path;
1115 int dfd, error;
1116
1117 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1118 if (error)
1119 return (error);
1120 #ifdef DEBUG
1121 if (ldebug(mknod))
1122 kprintf(ARGS(mknod, "%s, %d, %d"),
1123 path, args->mode, args->dev);
1124 #endif
1125 get_mplock();
1126 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1127 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, 0);
1128 if (error == 0) {
1129 if (args->mode & S_IFIFO) {
1130 error = kern_mkfifo(&nd, args->mode);
1131 } else {
1132 error = kern_mknod(&nd, args->mode,
1133 umajor(args->dev),
1134 uminor(args->dev));
1135 }
1136 }
1137 nlookup_done_at(&nd, fp);
1138 rel_mplock();
1139
1140 linux_free_path(&path);
1141 return(error);
1142 }
1143
1144 /*
1145 * UGH! This is just about the dumbest idea I've ever heard!!
1146 *
1147 * MPSAFE
1148 */
1149 int
1150 sys_linux_personality(struct linux_personality_args *args)
1151 {
1152 #ifdef DEBUG
1153 if (ldebug(personality))
1154 kprintf(ARGS(personality, "%d"), args->per);
1155 #endif
1156 if (args->per != 0)
1157 return EINVAL;
1158
1159 /* Yes Jim, it's still a Linux... */
1160 args->sysmsg_result = 0;
1161 return 0;
1162 }
1163
1164 /*
1165 * Wrappers for get/setitimer for debugging..
1166 *
1167 * MPSAFE
1168 */
1169 int
1170 sys_linux_setitimer(struct linux_setitimer_args *args)
1171 {
1172 struct setitimer_args bsa;
1173 struct itimerval foo;
1174 int error;
1175
1176 #ifdef DEBUG
1177 if (ldebug(setitimer))
1178 kprintf(ARGS(setitimer, "%p, %p"),
1179 (void *)args->itv, (void *)args->oitv);
1180 #endif
1181 bsa.which = args->which;
1182 bsa.itv = (struct itimerval *)args->itv;
1183 bsa.oitv = (struct itimerval *)args->oitv;
1184 bsa.sysmsg_result = 0;
1185 if (args->itv) {
1186 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
1187 return error;
1188 #ifdef DEBUG
1189 if (ldebug(setitimer)) {
1190 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
1191 foo.it_value.tv_sec, foo.it_value.tv_usec);
1192 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
1193 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
1194 }
1195 #endif
1196 }
1197 error = sys_setitimer(&bsa);
1198 args->sysmsg_result = bsa.sysmsg_result;
1199 return(error);
1200 }
1201
1202 /*
1203 * MPSAFE
1204 */
1205 int
1206 sys_linux_getitimer(struct linux_getitimer_args *args)
1207 {
1208 struct getitimer_args bsa;
1209 int error;
1210 #ifdef DEBUG
1211 if (ldebug(getitimer))
1212 kprintf(ARGS(getitimer, "%p"), (void *)args->itv);
1213 #endif
1214 bsa.which = args->which;
1215 bsa.itv = (struct itimerval *)args->itv;
1216 bsa.sysmsg_result = 0;
1217 error = sys_getitimer(&bsa);
1218 args->sysmsg_result = bsa.sysmsg_result;
1219 return(error);
1220 }
1221
1222 /*
1223 * MPSAFE
1224 */
1225 int
1226 sys_linux_nice(struct linux_nice_args *args)
1227 {
1228 struct setpriority_args bsd_args;
1229 int error;
1230
1231 bsd_args.which = PRIO_PROCESS;
1232 bsd_args.who = 0; /* current process */
1233 bsd_args.prio = args->inc;
1234 bsd_args.sysmsg_result = 0;
1235 error = sys_setpriority(&bsd_args);
1236 args->sysmsg_result = bsd_args.sysmsg_result;
1237 return(error);
1238 }
1239
1240 /*
1241 * MPALMOSTSAFE
1242 */
1243 int
1244 sys_linux_setgroups(struct linux_setgroups_args *args)
1245 {
1246 struct thread *td = curthread;
1247 struct proc *p = td->td_proc;
1248 struct ucred *newcred, *oldcred;
1249 l_gid_t linux_gidset[NGROUPS];
1250 gid_t *bsd_gidset;
1251 int ngrp, error;
1252
1253 ngrp = args->gidsetsize;
1254 oldcred = td->td_ucred;
1255
1256 /*
1257 * cr_groups[0] holds egid. Setting the whole set from
1258 * the supplied set will cause egid to be changed too.
1259 * Keep cr_groups[0] unchanged to prevent that.
1260 */
1261
1262 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0)
1263 return (error);
1264
1265 if ((u_int)ngrp >= NGROUPS)
1266 return (EINVAL);
1267
1268 get_mplock();
1269 newcred = crdup(oldcred);
1270 if (ngrp > 0) {
1271 error = copyin((caddr_t)args->grouplist, linux_gidset,
1272 ngrp * sizeof(l_gid_t));
1273 if (error) {
1274 crfree(newcred);
1275 rel_mplock();
1276 return (error);
1277 }
1278
1279 newcred->cr_ngroups = ngrp + 1;
1280
1281 bsd_gidset = newcred->cr_groups;
1282 ngrp--;
1283 while (ngrp >= 0) {
1284 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1285 ngrp--;
1286 }
1287 } else {
1288 newcred->cr_ngroups = 1;
1289 }
1290
1291 setsugid();
1292 oldcred = p->p_ucred; /* reload, deal with threads race */
1293 p->p_ucred = newcred;
1294 crfree(oldcred);
1295 rel_mplock();
1296 return (0);
1297 }
1298
1299 /*
1300 * MPSAFE
1301 */
1302 int
1303 sys_linux_getgroups(struct linux_getgroups_args *args)
1304 {
1305 struct thread *td = curthread;
1306 struct ucred *cred;
1307 l_gid_t linux_gidset[NGROUPS];
1308 gid_t *bsd_gidset;
1309 int bsd_gidsetsz, ngrp, error;
1310
1311 cred = td->td_ucred;
1312 bsd_gidset = cred->cr_groups;
1313 bsd_gidsetsz = cred->cr_ngroups - 1;
1314
1315 /*
1316 * cr_groups[0] holds egid. Returning the whole set
1317 * here will cause a duplicate. Exclude cr_groups[0]
1318 * to prevent that.
1319 */
1320
1321 if ((ngrp = args->gidsetsize) == 0) {
1322 args->sysmsg_result = bsd_gidsetsz;
1323 return (0);
1324 }
1325
1326 if ((u_int)ngrp < bsd_gidsetsz)
1327 return (EINVAL);
1328
1329 ngrp = 0;
1330 while (ngrp < bsd_gidsetsz) {
1331 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1332 ngrp++;
1333 }
1334
1335 if ((error = copyout(linux_gidset, args->grouplist,
1336 ngrp * sizeof(l_gid_t)))) {
1337 return (error);
1338 }
1339
1340 args->sysmsg_result = ngrp;
1341 return (0);
1342 }
1343
1344 /*
1345 * MPSAFE
1346 */
1347 int
1348 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1349 {
1350 struct l_rlimit linux_rlim;
1351 struct rlimit rlim;
1352 u_int which;
1353 int error;
1354
1355 #ifdef DEBUG
1356 if (ldebug(setrlimit))
1357 kprintf(ARGS(setrlimit, "%d, %p"),
1358 args->resource, (void *)args->rlim);
1359 #endif
1360 if (args->resource >= LINUX_RLIM_NLIMITS)
1361 return (EINVAL);
1362 which = linux_to_bsd_resource[args->resource];
1363 if (which == -1)
1364 return (EINVAL);
1365
1366 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1367 if (error)
1368 return (error);
1369 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1370 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1371
1372 error = kern_setrlimit(which, &rlim);
1373
1374 return(error);
1375 }
1376
1377 /*
1378 * MPSAFE
1379 */
1380 int
1381 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1382 {
1383 struct l_rlimit linux_rlim;
1384 struct rlimit rlim;
1385 u_int which;
1386 int error;
1387
1388 #ifdef DEBUG
1389 if (ldebug(old_getrlimit))
1390 kprintf(ARGS(old_getrlimit, "%d, %p"),
1391 args->resource, (void *)args->rlim);
1392 #endif
1393 if (args->resource >= LINUX_RLIM_NLIMITS)
1394 return (EINVAL);
1395 which = linux_to_bsd_resource[args->resource];
1396 if (which == -1)
1397 return (EINVAL);
1398
1399 error = kern_getrlimit(which, &rlim);
1400
1401 if (error == 0) {
1402 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1403 if (linux_rlim.rlim_cur == ULONG_MAX)
1404 linux_rlim.rlim_cur = LONG_MAX;
1405 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1406 if (linux_rlim.rlim_max == ULONG_MAX)
1407 linux_rlim.rlim_max = LONG_MAX;
1408 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1409 }
1410 return (error);
1411 }
1412
1413 /*
1414 * MPSAFE
1415 */
1416 int
1417 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1418 {
1419 struct l_rlimit linux_rlim;
1420 struct rlimit rlim;
1421 u_int which;
1422 int error;
1423
1424 #ifdef DEBUG
1425 if (ldebug(getrlimit))
1426 kprintf(ARGS(getrlimit, "%d, %p"),
1427 args->resource, (void *)args->rlim);
1428 #endif
1429 if (args->resource >= LINUX_RLIM_NLIMITS)
1430 return (EINVAL);
1431 which = linux_to_bsd_resource[args->resource];
1432 if (which == -1)
1433 return (EINVAL);
1434
1435 error = kern_getrlimit(which, &rlim);
1436
1437 if (error == 0) {
1438 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1439 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1440 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1441 }
1442 return (error);
1443 }
1444
1445 /*
1446 * MPSAFE
1447 */
1448 int
1449 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1450 {
1451 struct sched_setscheduler_args bsd;
1452 int error;
1453
1454 #ifdef DEBUG
1455 if (ldebug(sched_setscheduler))
1456 kprintf(ARGS(sched_setscheduler, "%d, %d, %p"),
1457 args->pid, args->policy, (const void *)args->param);
1458 #endif
1459
1460 switch (args->policy) {
1461 case LINUX_SCHED_OTHER:
1462 bsd.policy = SCHED_OTHER;
1463 break;
1464 case LINUX_SCHED_FIFO:
1465 bsd.policy = SCHED_FIFO;
1466 break;
1467 case LINUX_SCHED_RR:
1468 bsd.policy = SCHED_RR;
1469 break;
1470 default:
1471 return EINVAL;
1472 }
1473
1474 bsd.pid = args->pid;
1475 bsd.param = (struct sched_param *)args->param;
1476 bsd.sysmsg_result = 0;
1477
1478 error = sys_sched_setscheduler(&bsd);
1479 args->sysmsg_result = bsd.sysmsg_result;
1480 return(error);
1481 }
1482
1483 /*
1484 * MPSAFE
1485 */
1486 int
1487 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1488 {
1489 struct sched_getscheduler_args bsd;
1490 int error;
1491
1492 #ifdef DEBUG
1493 if (ldebug(sched_getscheduler))
1494 kprintf(ARGS(sched_getscheduler, "%d"), args->pid);
1495 #endif
1496
1497 bsd.sysmsg_result = 0;
1498 bsd.pid = args->pid;
1499 error = sys_sched_getscheduler(&bsd);
1500 args->sysmsg_result = bsd.sysmsg_result;
1501
1502 switch (args->sysmsg_result) {
1503 case SCHED_OTHER:
1504 args->sysmsg_result = LINUX_SCHED_OTHER;
1505 break;
1506 case SCHED_FIFO:
1507 args->sysmsg_result = LINUX_SCHED_FIFO;
1508 break;
1509 case SCHED_RR:
1510 args->sysmsg_result = LINUX_SCHED_RR;
1511 break;
1512 }
1513 return error;
1514 }
1515
1516 /*
1517 * MPSAFE
1518 */
1519 int
1520 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1521 {
1522 struct sched_get_priority_max_args bsd;
1523 int error;
1524
1525 #ifdef DEBUG
1526 if (ldebug(sched_get_priority_max))
1527 kprintf(ARGS(sched_get_priority_max, "%d"), args->policy);
1528 #endif
1529
1530 switch (args->policy) {
1531 case LINUX_SCHED_OTHER:
1532 bsd.policy = SCHED_OTHER;
1533 break;
1534 case LINUX_SCHED_FIFO:
1535 bsd.policy = SCHED_FIFO;
1536 break;
1537 case LINUX_SCHED_RR:
1538 bsd.policy = SCHED_RR;
1539 break;
1540 default:
1541 return EINVAL;
1542 }
1543 bsd.sysmsg_result = 0;
1544
1545 error = sys_sched_get_priority_max(&bsd);
1546 args->sysmsg_result = bsd.sysmsg_result;
1547 return(error);
1548 }
1549
1550 /*
1551 * MPSAFE
1552 */
1553 int
1554 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1555 {
1556 struct sched_get_priority_min_args bsd;
1557 int error;
1558
1559 #ifdef DEBUG
1560 if (ldebug(sched_get_priority_min))
1561 kprintf(ARGS(sched_get_priority_min, "%d"), args->policy);
1562 #endif
1563
1564 switch (args->policy) {
1565 case LINUX_SCHED_OTHER:
1566 bsd.policy = SCHED_OTHER;
1567 break;
1568 case LINUX_SCHED_FIFO:
1569 bsd.policy = SCHED_FIFO;
1570 break;
1571 case LINUX_SCHED_RR:
1572 bsd.policy = SCHED_RR;
1573 break;
1574 default:
1575 return EINVAL;
1576 }
1577 bsd.sysmsg_result = 0;
1578
1579 error = sys_sched_get_priority_min(&bsd);
1580 args->sysmsg_result = bsd.sysmsg_result;
1581 return(error);
1582 }
1583
1584 #define REBOOT_CAD_ON 0x89abcdef
1585 #define REBOOT_CAD_OFF 0
1586 #define REBOOT_HALT 0xcdef0123
1587 #define REBOOT_RESTART 0x01234567
1588 #define REBOOT_RESTART2 0xA1B2C3D4
1589 #define REBOOT_POWEROFF 0x4321FEDC
1590 #define REBOOT_MAGIC1 0xfee1dead
1591 #define REBOOT_MAGIC2 0x28121969
1592 #define REBOOT_MAGIC2A 0x05121996
1593 #define REBOOT_MAGIC2B 0x16041998
1594
1595 /*
1596 * MPSAFE
1597 */
1598 int
1599 sys_linux_reboot(struct linux_reboot_args *args)
1600 {
1601 struct reboot_args bsd_args;
1602 int error;
1603
1604 #ifdef DEBUG
1605 if (ldebug(reboot))
1606 kprintf(ARGS(reboot, "0x%x"), args->cmd);
1607 #endif
1608
1609 if ((args->magic1 != REBOOT_MAGIC1) ||
1610 ((args->magic2 != REBOOT_MAGIC2) &&
1611 (args->magic2 != REBOOT_MAGIC2A) &&
1612 (args->magic2 != REBOOT_MAGIC2B)))
1613 return EINVAL;
1614
1615 switch (args->cmd) {
1616 case REBOOT_CAD_ON:
1617 case REBOOT_CAD_OFF:
1618 return (priv_check(curthread, PRIV_REBOOT));
1619 /* NOTREACHED */
1620 case REBOOT_HALT:
1621 bsd_args.opt = RB_HALT;
1622 break;
1623 case REBOOT_RESTART:
1624 case REBOOT_RESTART2:
1625 bsd_args.opt = 0;
1626 break;
1627 case REBOOT_POWEROFF:
1628 bsd_args.opt = RB_POWEROFF;
1629 break;
1630 default:
1631 return EINVAL;
1632 /* NOTREACHED */
1633 }
1634
1635 bsd_args.sysmsg_result = 0;
1636
1637 error = sys_reboot(&bsd_args);
1638 args->sysmsg_result = bsd_args.sysmsg_result;
1639 return(error);
1640 }
1641
1642 /*
1643 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1644 * p->p_retval[1] when COMPAT_43 is defined. This
1645 * globbers registers that are assumed to be preserved. The following
1646 * lightweight syscalls fixes this. See also linux_getgid16() and
1647 * linux_getuid16() in linux_uid16.c.
1648 *
1649 * linux_getpid() - MP SAFE
1650 * linux_getgid() - MP SAFE
1651 * linux_getuid() - MP SAFE
1652 */
1653
1654 /*
1655 * MPALMOSTSAFE
1656 */
1657 int
1658 sys_linux_getpid(struct linux_getpid_args *args)
1659 {
1660 struct linux_emuldata *em;
1661 struct proc *p = curproc;
1662
1663
1664 EMUL_LOCK();
1665 em = emuldata_get(p);
1666 if (em == NULL) /* this should never happen */
1667 args->sysmsg_result = p->p_pid;
1668 else
1669 args->sysmsg_result = em->s->group_pid;
1670 EMUL_UNLOCK();
1671
1672 return (0);
1673 }
1674
1675 /*
1676 * MPALMOSTSAFE
1677 */
1678 int
1679 sys_linux_getppid(struct linux_getppid_args *args)
1680 {
1681 struct linux_emuldata *em;
1682 struct proc *parent;
1683 struct proc *p;
1684 pid_t group_pid;
1685
1686 EMUL_LOCK();
1687 em = emuldata_get(curproc);
1688 KKASSERT(em != NULL);
1689 group_pid = em->s->group_pid;
1690 EMUL_UNLOCK();
1691
1692 p = pfind(group_pid);
1693 /* We are not allowed to fail */
1694 if (p == NULL)
1695 goto out;
1696
1697 parent = p->p_pptr;
1698 if (parent->p_sysent == &elf_linux_sysvec) {
1699 EMUL_LOCK();
1700 em = emuldata_get(parent);
1701 args->sysmsg_result = em->s->group_pid;
1702 EMUL_UNLOCK();
1703 } else {
1704 args->sysmsg_result = parent->p_pid;
1705 }
1706 PRELE(p);
1707
1708 out:
1709 return (0);
1710 }
1711
1712 /*
1713 * MPSAFE
1714 */
1715 int
1716 sys_linux_getgid(struct linux_getgid_args *args)
1717 {
1718 struct thread *td = curthread;
1719
1720 args->sysmsg_result = td->td_ucred->cr_rgid;
1721 return (0);
1722 }
1723
1724 /*
1725 * MPSAFE
1726 */
1727 int
1728 sys_linux_getuid(struct linux_getuid_args *args)
1729 {
1730 struct thread *td = curthread;
1731
1732 args->sysmsg_result = td->td_ucred->cr_ruid;
1733 return (0);
1734 }
1735
1736 /*
1737 * MPSAFE
1738 */
1739 int
1740 sys_linux_getsid(struct linux_getsid_args *args)
1741 {
1742 struct getsid_args bsd;
1743 int error;
1744
1745 bsd.sysmsg_result = 0;
1746 bsd.pid = args->pid;
1747 error = sys_getsid(&bsd);
1748 args->sysmsg_result = bsd.sysmsg_result;
1749 return(error);
1750 }
1751
1752 /*
1753 * MPSAFE
1754 */
1755 int
1756 linux_nosys(struct nosys_args *args)
1757 {
1758 /* XXX */
1759 return (ENOSYS);
1760 }
1761
1762 int
1763 sys_linux_mq_open(struct linux_mq_open_args *args)
1764 {
1765 struct mq_open_args moa;
1766 int error, oflag;
1767
1768 oflag = 0;
1769 if (args->oflag & LINUX_O_RDONLY)
1770 oflag |= O_RDONLY;
1771 if (args->oflag & LINUX_O_WRONLY)
1772 oflag |= O_WRONLY;
1773 if (args->oflag & LINUX_O_RDWR)
1774 oflag |= O_RDWR;
1775
1776 if (args->oflag & LINUX_O_NONBLOCK)
1777 oflag |= O_NONBLOCK;
1778 if (args->oflag & LINUX_O_CREAT)
1779 oflag |= O_CREAT;
1780 if (args->oflag & LINUX_O_EXCL)
1781 oflag |= O_EXCL;
1782
1783 moa.name = args->name;
1784 moa.oflag = oflag;
1785 moa.mode = args->mode;
1786 moa.attr = args->attr;
1787
1788 error = sys_mq_open(&moa);
1789
1790 return (error);
1791 }
1792
1793 int
1794 sys_linux_mq_getsetattr(struct linux_mq_getsetattr_args *args)
1795 {
1796 struct mq_getattr_args gaa;
1797 struct mq_setattr_args saa;
1798 int error;
1799
1800 gaa.mqdes = args->mqd;
1801 gaa.mqstat = args->oattr;
1802
1803 saa.mqdes = args->mqd;
1804 saa.mqstat = args->attr;
1805 saa.mqstat = args->oattr;
1806
1807 if (args->attr != NULL) {
1808 error = sys_mq_setattr(&saa);
1809 } else {
1810 error = sys_mq_getattr(&gaa);
1811 }
1812
1813 return error;
1814 }
1815
1816 /*
1817 * Get affinity of a process.
1818 */
1819 int
1820 sys_linux_sched_getaffinity(struct linux_sched_getaffinity_args *args)
1821 {
1822 cpumask_t mask;
1823 struct proc *p;
1824 struct lwp *lp;
1825 int error = 0;
1826
1827 #ifdef DEBUG
1828 if (ldebug(sched_getaffinity))
1829 kprintf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1830 args->len);
1831 #endif
1832 if (args->len < sizeof(cpumask_t))
1833 return (EINVAL);
1834 #if 0
1835 if ((error = priv_check(curthread, PRIV_SCHED_CPUSET)) != 0)
1836 return (EPERM);
1837 #endif
1838 /* Get the mplock to ensure that the proc is not running */
1839 get_mplock();
1840 if (args->pid == 0) {
1841 p = curproc;
1842 PHOLD(p);
1843 } else {
1844 p = pfind(args->pid);
1845 if (p == NULL) {
1846 error = ESRCH;
1847 goto done;
1848 }
1849 }
1850
1851 lp = FIRST_LWP_IN_PROC(p);
1852 /*
1853 * XXX: if lwp_cpumask is ever changed to support more than
1854 * 32 processors, this needs to be changed to a bcopy.
1855 */
1856 mask = lp->lwp_cpumask;
1857 if ((error = copyout(&mask, args->user_mask_ptr, sizeof(cpumask_t))))
1858 error = EFAULT;
1859 done:
1860 rel_mplock();
1861 #if 0
1862 if (error == 0)
1863 args->sysmsg_iresult = sizeof(cpumask_t);
1864 #endif
1865 if (p)
1866 PRELE(p);
1867 return (error);
1868 }
1869
1870 /*
1871 * Set affinity of a process.
1872 */
1873 int
1874 sys_linux_sched_setaffinity(struct linux_sched_setaffinity_args *args)
1875 {
1876 #ifdef DEBUG
1877 if (ldebug(sched_setaffinity))
1878 kprintf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1879 args->len);
1880 #endif
1881 /*
1882 * From Linux man page:
1883 * sched_setaffinity() sets the CPU affinity mask of the process
1884 * whose ID is pid to the value specified by mask. If pid is zero,
1885 * then the calling process is used. The argument cpusetsize is
1886 * the length (in bytes) of the data pointed to by mask. Normally
1887 * this argument would be specified as sizeof(cpu_set_t).
1888 *
1889 * If the process specified by pid is not currently running on one
1890 * of the CPUs specified in mask, then that process is migrated to
1891 * one of the CPUs specified in mask.
1892 */
1893 /*
1894 * About our implementation: I don't think that it is too important
1895 * to have a working implementation, but if it was ever needed,
1896 * the best approach would be to implement the whole mechanism
1897 * properly in kern_usched.
1898 * The idea has to be to change the affinity mask AND migrate the
1899 * lwp to one of the new valid CPUs for the lwp, in case the current
1900 * CPU isn't anymore in the affinity mask passed in.
1901 * For now, we'll just signal success even if we didn't do anything.
1902 */
1903 return 0;
1904 }
1905
1906 int
1907 sys_linux_gettid(struct linux_gettid_args *args)
1908 {
1909 args->sysmsg_iresult = curproc->p_pid;
1910 return 0;
1911 }
1912
1913 int
1914 sys_linux_getcpu(struct linux_getcpu_args *args)
1915 {
1916 struct globaldata *gd;
1917 l_uint node = 0;
1918 int error;
1919
1920 gd = mycpu;
1921 error = copyout(&gd->gd_cpuid, args->pcpu, sizeof(gd->gd_cpuid));
1922 if (error)
1923 return (error);
1924 /*
1925 * XXX: this should be the NUMA node, but since we don't implement it,
1926 * just return 0 for it.
1927 */
1928 error = copyout(&node, args->pnode, sizeof(node));
1929 return (error);
1930 }
1931
1932 int
1933 sys_linux_sethostname(struct linux_sethostname_args *uap)
1934 {
1935 struct thread *td = curthread;
1936 size_t len;
1937 char *hostname;
1938 int name[2];
1939 int error;
1940
1941 name[0] = CTL_KERN;
1942 name[1] = KERN_HOSTNAME;
1943 error = priv_check_cred(td->td_ucred, PRIV_SETHOSTNAME, 0);
1944 if (error)
1945 return (error);
1946 len = MIN(uap->len, MAXHOSTNAMELEN);
1947 hostname = kmalloc(MAXHOSTNAMELEN, M_TEMP, M_WAITOK);
1948
1949 error = copyin(uap->hostname, hostname, len);
1950 if (error) {
1951 kfree(hostname, M_TEMP);
1952 return (error);
1953 }
1954
1955 get_mplock();
1956 error = kernel_sysctl(name, 2, NULL, 0, hostname, len, NULL);
1957 rel_mplock();
1958
1959 kfree(hostname, M_TEMP);
1960 return (error);
1961 }
Cache object: bf4da00590ac4440e49ed81b14dc16b5
|