2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/capsicum.h>
34 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
38 #include <sys/malloc.h>
40 #include <sys/mutex.h>
43 #include <sys/queue.h>
44 #include <sys/resource.h>
45 #include <sys/resourcevar.h>
46 #include <sys/sched.h>
47 #include <sys/signalvar.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysproto.h>
50 #include <sys/systm.h>
52 #include <sys/unistd.h>
55 #include <machine/frame.h>
56 #include <machine/psl.h>
57 #include <machine/segments.h>
58 #include <machine/sysarch.h>
62 #include <vm/vm_map.h>
64 #include <security/audit/audit.h>
66 #include <i386/linux/linux.h>
67 #include <i386/linux/linux_proto.h>
68 #include <compat/linux/linux_emul.h>
69 #include <compat/linux/linux_ipc.h>
70 #include <compat/linux/linux_misc.h>
71 #include <compat/linux/linux_mmap.h>
72 #include <compat/linux/linux_signal.h>
73 #include <compat/linux/linux_util.h>
75 #include <i386/include/pcb.h> /* needed for pcb definition in linux_set_thread_area */
77 #include "opt_posix.h"
79 extern struct sysentvec elf32_freebsd_sysvec; /* defined in i386/i386/elf_machdep.c */
87 l_uint read_exec_only:1;
88 l_uint limit_in_pages:1;
89 l_uint seg_not_present:1;
93 struct l_old_select_argv {
98 struct l_timeval *timeout;
102 linux_execve(struct thread *td, struct linux_execve_args *args)
104 struct image_args eargs;
108 if (!LUSECONVPATH(td)) {
109 error = exec_copyin_args(&eargs, args->path, UIO_USERSPACE,
110 args->argp, args->envp);
112 LCONVPATHEXIST(td, args->path, &newpath);
113 error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
114 args->argp, args->envp);
118 error = linux_common_execve(td, &eargs);
119 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
123 struct l_ipc_kludge {
124 struct l_msgbuf *msgp;
129 linux_ipc(struct thread *td, struct linux_ipc_args *args)
132 switch (args->what & 0xFFFF) {
134 struct linux_semop_args a;
136 a.semid = args->arg1;
137 a.tsops = PTRIN(args->ptr);
138 a.nsops = args->arg2;
139 return (linux_semop(td, &a));
142 struct linux_semget_args a;
145 a.nsems = args->arg2;
146 a.semflg = args->arg3;
147 return (linux_semget(td, &a));
150 struct linux_semctl_args a;
153 a.semid = args->arg1;
154 a.semnum = args->arg2;
156 error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
159 return (linux_semctl(td, &a));
162 struct linux_msgsnd_args a;
164 a.msqid = args->arg1;
165 a.msgp = PTRIN(args->ptr);
166 a.msgsz = args->arg2;
167 a.msgflg = args->arg3;
168 return (linux_msgsnd(td, &a));
171 struct linux_msgrcv_args a;
173 a.msqid = args->arg1;
174 a.msgsz = args->arg2;
175 a.msgflg = args->arg3;
176 if ((args->what >> 16) == 0) {
177 struct l_ipc_kludge tmp;
182 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
185 a.msgp = PTRIN(tmp.msgp);
186 a.msgtyp = tmp.msgtyp;
188 a.msgp = PTRIN(args->ptr);
189 a.msgtyp = args->arg5;
191 return (linux_msgrcv(td, &a));
194 struct linux_msgget_args a;
197 a.msgflg = args->arg2;
198 return (linux_msgget(td, &a));
201 struct linux_msgctl_args a;
203 a.msqid = args->arg1;
205 a.buf = PTRIN(args->ptr);
206 return (linux_msgctl(td, &a));
209 struct linux_shmat_args a;
213 a.shmid = args->arg1;
214 a.shmaddr = PTRIN(args->ptr);
215 a.shmflg = args->arg2;
216 error = linux_shmat(td, &a);
219 addr = td->td_retval[0];
220 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
221 td->td_retval[0] = 0;
225 struct linux_shmdt_args a;
227 a.shmaddr = PTRIN(args->ptr);
228 return (linux_shmdt(td, &a));
231 struct linux_shmget_args a;
235 a.shmflg = args->arg3;
236 return (linux_shmget(td, &a));
239 struct linux_shmctl_args a;
241 a.shmid = args->arg1;
243 a.buf = PTRIN(args->ptr);
244 return (linux_shmctl(td, &a));
254 linux_old_select(struct thread *td, struct linux_old_select_args *args)
256 struct l_old_select_argv linux_args;
257 struct linux_select_args newsel;
260 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
264 newsel.nfds = linux_args.nfds;
265 newsel.readfds = linux_args.readfds;
266 newsel.writefds = linux_args.writefds;
267 newsel.exceptfds = linux_args.exceptfds;
268 newsel.timeout = linux_args.timeout;
269 return (linux_select(td, &newsel));
273 linux_set_cloned_tls(struct thread *td, void *desc)
275 struct segment_descriptor sd;
276 struct l_user_desc info;
280 error = copyin(desc, &info, sizeof(struct l_user_desc));
282 linux_msg(td, "set_cloned_tls copyin failed!");
284 idx = info.entry_number;
287 * looks like we're getting the idx we returned
288 * in the set_thread_area() syscall
290 if (idx != 6 && idx != 3) {
291 linux_msg(td, "set_cloned_tls resetting idx!");
295 /* this doesnt happen in practice */
297 /* we might copy out the entry_number as 3 */
298 info.entry_number = 3;
299 error = copyout(&info, desc, sizeof(struct l_user_desc));
301 linux_msg(td, "set_cloned_tls copyout failed!");
304 a[0] = LINUX_LDT_entry_a(&info);
305 a[1] = LINUX_LDT_entry_b(&info);
307 memcpy(&sd, &a, sizeof(a));
309 td->td_pcb->pcb_gsd = sd;
310 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
317 linux_set_upcall_kse(struct thread *td, register_t stack)
321 td->td_frame->tf_esp = stack;
324 * The newly created Linux thread returns
325 * to the user space by the same path that a parent do.
327 td->td_frame->tf_eax = 0;
332 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
335 return (linux_mmap_common(td, args->addr, args->len, args->prot,
336 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
341 linux_mmap(struct thread *td, struct linux_mmap_args *args)
344 struct l_mmap_argv linux_args;
346 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
350 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
351 linux_args.prot, linux_args.flags, linux_args.fd,
352 (uint32_t)linux_args.pgoff));
356 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
359 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
363 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
366 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
370 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
373 struct i386_ioperm_args iia;
375 iia.start = args->start;
376 iia.length = args->length;
377 iia.enable = args->enable;
378 error = i386_set_ioperm(td, &iia);
383 linux_iopl(struct thread *td, struct linux_iopl_args *args)
387 if (args->level < 0 || args->level > 3)
389 if ((error = priv_check(td, PRIV_IO)) != 0)
391 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
393 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
394 (args->level * (PSL_IOPL / 3));
399 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
402 struct i386_ldt_args ldt;
403 struct l_descriptor ld;
404 union descriptor desc;
408 case 0x00: /* read_ldt */
410 ldt.descs = uap->ptr;
411 ldt.num = uap->bytecount / sizeof(union descriptor);
412 error = i386_get_ldt(td, &ldt);
413 td->td_retval[0] *= sizeof(union descriptor);
415 case 0x02: /* read_default_ldt = 0 */
416 size = 5*sizeof(struct l_desc_struct);
417 if (size > uap->bytecount)
418 size = uap->bytecount;
419 for (written = error = 0; written < size && error == 0; written++)
420 error = subyte((char *)uap->ptr + written, 0);
421 td->td_retval[0] = written;
423 case 0x01: /* write_ldt */
424 case 0x11: /* write_ldt */
425 if (uap->bytecount != sizeof(ld))
428 error = copyin(uap->ptr, &ld, sizeof(ld));
432 ldt.start = ld.entry_number;
435 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
436 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
437 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
438 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
439 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
442 desc.sd.sd_p = (ld.seg_not_present ^ 1);
444 desc.sd.sd_def32 = ld.seg_32bit;
445 desc.sd.sd_gran = ld.limit_in_pages;
446 error = i386_set_ldt(td, &ldt, &desc);
453 if (error == EOPNOTSUPP) {
454 linux_msg(td, "modify_ldt needs kernel option USER_LDT");
462 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
465 l_sigaction_t act, oact;
468 if (args->nsa != NULL) {
469 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
472 act.lsa_handler = osa.lsa_handler;
473 act.lsa_flags = osa.lsa_flags;
474 act.lsa_restorer = osa.lsa_restorer;
475 LINUX_SIGEMPTYSET(act.lsa_mask);
476 act.lsa_mask.__mask = osa.lsa_mask;
479 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
480 args->osa ? &oact : NULL);
482 if (args->osa != NULL && !error) {
483 osa.lsa_handler = oact.lsa_handler;
484 osa.lsa_flags = oact.lsa_flags;
485 osa.lsa_restorer = oact.lsa_restorer;
486 osa.lsa_mask = oact.lsa_mask.__mask;
487 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
494 * Linux has two extra args, restart and oldmask. We dont use these,
495 * but it seems that "restart" is actually a context pointer that
496 * enables the signal to happen with a different register set.
499 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
504 LINUX_SIGEMPTYSET(mask);
505 mask.__mask = args->mask;
506 linux_to_bsd_sigset(&mask, &sigmask);
507 return (kern_sigsuspend(td, sigmask));
511 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
517 if (uap->sigsetsize != sizeof(l_sigset_t))
520 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
524 linux_to_bsd_sigset(&lmask, &sigmask);
525 return (kern_sigsuspend(td, sigmask));
529 linux_pause(struct thread *td, struct linux_pause_args *args)
531 struct proc *p = td->td_proc;
535 sigmask = td->td_sigmask;
537 return (kern_sigsuspend(td, sigmask));
541 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
547 if (uap->uss != NULL) {
548 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
552 ss.ss_sp = lss.ss_sp;
553 ss.ss_size = lss.ss_size;
554 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
556 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
557 (uap->uoss != NULL) ? &oss : NULL);
558 if (!error && uap->uoss != NULL) {
559 lss.ss_sp = oss.ss_sp;
560 lss.ss_size = oss.ss_size;
561 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
562 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
569 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
571 struct l_user_desc info;
575 struct segment_descriptor sd;
577 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
581 idx = info.entry_number;
583 * Semantics of Linux version: every thread in the system has array of
584 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
585 * syscall loads one of the selected tls decriptors with a value and
586 * also loads GDT descriptors 6, 7 and 8 with the content of the
587 * per-thread descriptors.
589 * Semantics of FreeBSD version: I think we can ignore that Linux has 3
590 * per-thread descriptors and use just the 1st one. The tls_array[]
591 * is used only in set/get-thread_area() syscalls and for loading the
592 * GDT descriptors. In FreeBSD we use just one GDT descriptor for TLS
593 * so we will load just one.
595 * XXX: this doesn't work when a user space process tries to use more
596 * than 1 TLS segment. Comment in the Linux sources says wine might do
601 * we support just GLIBC TLS now
602 * we should let 3 proceed as well because we use this segment so
603 * if code does two subsequent calls it should succeed
605 if (idx != 6 && idx != -1 && idx != 3)
609 * we have to copy out the GDT entry we use
610 * FreeBSD uses GDT entry #3 for storing %gs so load that
612 * XXX: what if a user space program doesn't check this value and tries
615 idx = info.entry_number = 3;
616 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
620 if (LINUX_LDT_empty(&info)) {
624 a[0] = LINUX_LDT_entry_a(&info);
625 a[1] = LINUX_LDT_entry_b(&info);
628 memcpy(&sd, &a, sizeof(a));
629 /* this is taken from i386 version of cpu_set_user_tls() */
632 td->td_pcb->pcb_gsd = sd;
633 PCPU_GET(fsgs_gdt)[1] = sd;
634 load_gs(GSEL(GUGS_SEL, SEL_UPL));
641 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
644 struct l_user_desc info;
647 struct l_desc_struct desc;
648 struct segment_descriptor sd;
650 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
654 idx = info.entry_number;
655 /* XXX: I am not sure if we want 3 to be allowed too. */
656 if (idx != 6 && idx != 3)
661 memset(&info, 0, sizeof(info));
663 sd = PCPU_GET(fsgs_gdt)[1];
665 memcpy(&desc, &sd, sizeof(desc));
667 info.entry_number = idx;
668 info.base_addr = LINUX_GET_BASE(&desc);
669 info.limit = LINUX_GET_LIMIT(&desc);
670 info.seg_32bit = LINUX_GET_32BIT(&desc);
671 info.contents = LINUX_GET_CONTENTS(&desc);
672 info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
673 info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
674 info.seg_not_present = !LINUX_GET_PRESENT(&desc);
675 info.useable = LINUX_GET_USEABLE(&desc);
677 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
684 /* XXX: this wont work with module - convert it */
686 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
688 #ifdef P1003_1B_MQUEUE
689 return (sys_kmq_open(td, (struct kmq_open_args *)args));
696 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
698 #ifdef P1003_1B_MQUEUE
699 return (sys_kmq_unlink(td, (struct kmq_unlink_args *)args));
706 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
708 #ifdef P1003_1B_MQUEUE
709 return (sys_kmq_timedsend(td, (struct kmq_timedsend_args *)args));
716 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
718 #ifdef P1003_1B_MQUEUE
719 return (sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *)args));
726 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
728 #ifdef P1003_1B_MQUEUE
729 return (sys_kmq_notify(td, (struct kmq_notify_args *)args));
736 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
738 #ifdef P1003_1B_MQUEUE
739 return (sys_kmq_setattr(td, (struct kmq_setattr_args *)args));