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;
103 linux_execve(struct thread *td, struct linux_execve_args *args)
105 struct image_args eargs;
109 LCONVPATHEXIST(td, args->path, &newpath);
111 error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
112 args->argp, args->envp);
113 free(newpath, M_TEMP);
115 error = linux_common_execve(td, &eargs);
116 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
120 struct l_ipc_kludge {
121 struct l_msgbuf *msgp;
126 linux_ipc(struct thread *td, struct linux_ipc_args *args)
129 switch (args->what & 0xFFFF) {
131 struct linux_semop_args a;
133 a.semid = args->arg1;
134 a.tsops = PTRIN(args->ptr);
135 a.nsops = args->arg2;
136 return (linux_semop(td, &a));
139 struct linux_semget_args a;
142 a.nsems = args->arg2;
143 a.semflg = args->arg3;
144 return (linux_semget(td, &a));
147 struct linux_semctl_args a;
150 a.semid = args->arg1;
151 a.semnum = args->arg2;
153 error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
156 return (linux_semctl(td, &a));
159 struct linux_msgsnd_args a;
161 a.msqid = args->arg1;
162 a.msgp = PTRIN(args->ptr);
163 a.msgsz = args->arg2;
164 a.msgflg = args->arg3;
165 return (linux_msgsnd(td, &a));
168 struct linux_msgrcv_args a;
170 a.msqid = args->arg1;
171 a.msgsz = args->arg2;
172 a.msgflg = args->arg3;
173 if ((args->what >> 16) == 0) {
174 struct l_ipc_kludge tmp;
179 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
182 a.msgp = PTRIN(tmp.msgp);
183 a.msgtyp = tmp.msgtyp;
185 a.msgp = PTRIN(args->ptr);
186 a.msgtyp = args->arg5;
188 return (linux_msgrcv(td, &a));
191 struct linux_msgget_args a;
194 a.msgflg = args->arg2;
195 return (linux_msgget(td, &a));
198 struct linux_msgctl_args a;
200 a.msqid = args->arg1;
202 a.buf = PTRIN(args->ptr);
203 return (linux_msgctl(td, &a));
206 struct linux_shmat_args a;
210 a.shmid = args->arg1;
211 a.shmaddr = PTRIN(args->ptr);
212 a.shmflg = args->arg2;
213 error = linux_shmat(td, &a);
216 addr = td->td_retval[0];
217 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
218 td->td_retval[0] = 0;
222 struct linux_shmdt_args a;
224 a.shmaddr = PTRIN(args->ptr);
225 return (linux_shmdt(td, &a));
228 struct linux_shmget_args a;
232 a.shmflg = args->arg3;
233 return (linux_shmget(td, &a));
236 struct linux_shmctl_args a;
238 a.shmid = args->arg1;
240 a.buf = PTRIN(args->ptr);
241 return (linux_shmctl(td, &a));
251 linux_old_select(struct thread *td, struct linux_old_select_args *args)
253 struct l_old_select_argv linux_args;
254 struct linux_select_args newsel;
257 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
261 newsel.nfds = linux_args.nfds;
262 newsel.readfds = linux_args.readfds;
263 newsel.writefds = linux_args.writefds;
264 newsel.exceptfds = linux_args.exceptfds;
265 newsel.timeout = linux_args.timeout;
266 return (linux_select(td, &newsel));
270 linux_set_cloned_tls(struct thread *td, void *desc)
272 struct segment_descriptor sd;
273 struct l_user_desc info;
277 error = copyin(desc, &info, sizeof(struct l_user_desc));
279 linux_msg(td, "set_cloned_tls copyin failed!");
281 idx = info.entry_number;
284 * looks like we're getting the idx we returned
285 * in the set_thread_area() syscall
287 if (idx != 6 && idx != 3) {
288 linux_msg(td, "set_cloned_tls resetting idx!");
292 /* this doesnt happen in practice */
294 /* we might copy out the entry_number as 3 */
295 info.entry_number = 3;
296 error = copyout(&info, desc, sizeof(struct l_user_desc));
298 linux_msg(td, "set_cloned_tls copyout failed!");
301 a[0] = LINUX_LDT_entry_a(&info);
302 a[1] = LINUX_LDT_entry_b(&info);
304 memcpy(&sd, &a, sizeof(a));
306 td->td_pcb->pcb_gsd = sd;
307 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
314 linux_set_upcall_kse(struct thread *td, register_t stack)
318 td->td_frame->tf_esp = stack;
321 * The newly created Linux thread returns
322 * to the user space by the same path that a parent do.
324 td->td_frame->tf_eax = 0;
329 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
332 return (linux_mmap_common(td, args->addr, args->len, args->prot,
333 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
338 linux_mmap(struct thread *td, struct linux_mmap_args *args)
341 struct l_mmap_argv linux_args;
343 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
347 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
348 linux_args.prot, linux_args.flags, linux_args.fd,
349 (uint32_t)linux_args.pgoff));
353 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
356 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
360 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
363 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
367 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
370 struct i386_ioperm_args iia;
372 iia.start = args->start;
373 iia.length = args->length;
374 iia.enable = args->enable;
375 error = i386_set_ioperm(td, &iia);
380 linux_iopl(struct thread *td, struct linux_iopl_args *args)
384 if (args->level < 0 || args->level > 3)
386 if ((error = priv_check(td, PRIV_IO)) != 0)
388 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
390 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
391 (args->level * (PSL_IOPL / 3));
396 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
399 struct i386_ldt_args ldt;
400 struct l_descriptor ld;
401 union descriptor desc;
405 case 0x00: /* read_ldt */
407 ldt.descs = uap->ptr;
408 ldt.num = uap->bytecount / sizeof(union descriptor);
409 error = i386_get_ldt(td, &ldt);
410 td->td_retval[0] *= sizeof(union descriptor);
412 case 0x02: /* read_default_ldt = 0 */
413 size = 5*sizeof(struct l_desc_struct);
414 if (size > uap->bytecount)
415 size = uap->bytecount;
416 for (written = error = 0; written < size && error == 0; written++)
417 error = subyte((char *)uap->ptr + written, 0);
418 td->td_retval[0] = written;
420 case 0x01: /* write_ldt */
421 case 0x11: /* write_ldt */
422 if (uap->bytecount != sizeof(ld))
425 error = copyin(uap->ptr, &ld, sizeof(ld));
429 ldt.start = ld.entry_number;
432 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
433 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
434 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
435 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
436 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
439 desc.sd.sd_p = (ld.seg_not_present ^ 1);
441 desc.sd.sd_def32 = ld.seg_32bit;
442 desc.sd.sd_gran = ld.limit_in_pages;
443 error = i386_set_ldt(td, &ldt, &desc);
450 if (error == EOPNOTSUPP) {
451 linux_msg(td, "modify_ldt needs kernel option USER_LDT");
459 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
462 l_sigaction_t act, oact;
465 if (args->nsa != NULL) {
466 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
469 act.lsa_handler = osa.lsa_handler;
470 act.lsa_flags = osa.lsa_flags;
471 act.lsa_restorer = osa.lsa_restorer;
472 LINUX_SIGEMPTYSET(act.lsa_mask);
473 act.lsa_mask.__mask = osa.lsa_mask;
476 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
477 args->osa ? &oact : NULL);
479 if (args->osa != NULL && !error) {
480 osa.lsa_handler = oact.lsa_handler;
481 osa.lsa_flags = oact.lsa_flags;
482 osa.lsa_restorer = oact.lsa_restorer;
483 osa.lsa_mask = oact.lsa_mask.__mask;
484 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
491 * Linux has two extra args, restart and oldmask. We dont use these,
492 * but it seems that "restart" is actually a context pointer that
493 * enables the signal to happen with a different register set.
496 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
501 LINUX_SIGEMPTYSET(mask);
502 mask.__mask = args->mask;
503 linux_to_bsd_sigset(&mask, &sigmask);
504 return (kern_sigsuspend(td, sigmask));
508 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
514 if (uap->sigsetsize != sizeof(l_sigset_t))
517 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
521 linux_to_bsd_sigset(&lmask, &sigmask);
522 return (kern_sigsuspend(td, sigmask));
526 linux_pause(struct thread *td, struct linux_pause_args *args)
528 struct proc *p = td->td_proc;
532 sigmask = td->td_sigmask;
534 return (kern_sigsuspend(td, sigmask));
538 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
544 if (uap->uss != NULL) {
545 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
549 ss.ss_sp = lss.ss_sp;
550 ss.ss_size = lss.ss_size;
551 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
553 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
554 (uap->uoss != NULL) ? &oss : NULL);
555 if (!error && uap->uoss != NULL) {
556 lss.ss_sp = oss.ss_sp;
557 lss.ss_size = oss.ss_size;
558 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
559 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
566 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
568 struct l_user_desc info;
572 struct segment_descriptor sd;
574 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
578 idx = info.entry_number;
580 * Semantics of Linux version: every thread in the system has array of
581 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
582 * syscall loads one of the selected tls decriptors with a value and
583 * also loads GDT descriptors 6, 7 and 8 with the content of the
584 * per-thread descriptors.
586 * Semantics of FreeBSD version: I think we can ignore that Linux has 3
587 * per-thread descriptors and use just the 1st one. The tls_array[]
588 * is used only in set/get-thread_area() syscalls and for loading the
589 * GDT descriptors. In FreeBSD we use just one GDT descriptor for TLS
590 * so we will load just one.
592 * XXX: this doesn't work when a user space process tries to use more
593 * than 1 TLS segment. Comment in the Linux sources says wine might do
598 * we support just GLIBC TLS now
599 * we should let 3 proceed as well because we use this segment so
600 * if code does two subsequent calls it should succeed
602 if (idx != 6 && idx != -1 && idx != 3)
606 * we have to copy out the GDT entry we use
607 * FreeBSD uses GDT entry #3 for storing %gs so load that
609 * XXX: what if a user space program doesn't check this value and tries
612 idx = info.entry_number = 3;
613 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
617 if (LINUX_LDT_empty(&info)) {
621 a[0] = LINUX_LDT_entry_a(&info);
622 a[1] = LINUX_LDT_entry_b(&info);
625 memcpy(&sd, &a, sizeof(a));
626 /* this is taken from i386 version of cpu_set_user_tls() */
629 td->td_pcb->pcb_gsd = sd;
630 PCPU_GET(fsgs_gdt)[1] = sd;
631 load_gs(GSEL(GUGS_SEL, SEL_UPL));
638 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
641 struct l_user_desc info;
644 struct l_desc_struct desc;
645 struct segment_descriptor sd;
647 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
651 idx = info.entry_number;
652 /* XXX: I am not sure if we want 3 to be allowed too. */
653 if (idx != 6 && idx != 3)
658 memset(&info, 0, sizeof(info));
660 sd = PCPU_GET(fsgs_gdt)[1];
662 memcpy(&desc, &sd, sizeof(desc));
664 info.entry_number = idx;
665 info.base_addr = LINUX_GET_BASE(&desc);
666 info.limit = LINUX_GET_LIMIT(&desc);
667 info.seg_32bit = LINUX_GET_32BIT(&desc);
668 info.contents = LINUX_GET_CONTENTS(&desc);
669 info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
670 info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
671 info.seg_not_present = !LINUX_GET_PRESENT(&desc);
672 info.useable = LINUX_GET_USEABLE(&desc);
674 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
681 /* XXX: this wont work with module - convert it */
683 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
685 #ifdef P1003_1B_MQUEUE
686 return (sys_kmq_open(td, (struct kmq_open_args *)args));
693 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
695 #ifdef P1003_1B_MQUEUE
696 return (sys_kmq_unlink(td, (struct kmq_unlink_args *)args));
703 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
705 #ifdef P1003_1B_MQUEUE
706 return (sys_kmq_timedsend(td, (struct kmq_timedsend_args *)args));
713 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
715 #ifdef P1003_1B_MQUEUE
716 return (sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *)args));
723 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
725 #ifdef P1003_1B_MQUEUE
726 return (sys_kmq_notify(td, (struct kmq_notify_args *)args));
733 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
735 #ifdef P1003_1B_MQUEUE
736 return (sys_kmq_setattr(td, (struct kmq_setattr_args *)args));