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 <i386/linux/linux.h>
65 #include <i386/linux/linux_proto.h>
66 #include <compat/linux/linux_emul.h>
67 #include <compat/linux/linux_ipc.h>
68 #include <compat/linux/linux_misc.h>
69 #include <compat/linux/linux_mmap.h>
70 #include <compat/linux/linux_signal.h>
71 #include <compat/linux/linux_util.h>
73 #include <i386/include/pcb.h> /* needed for pcb definition in linux_set_thread_area */
75 #include "opt_posix.h"
77 extern struct sysentvec elf32_freebsd_sysvec; /* defined in i386/i386/elf_machdep.c */
85 l_uint read_exec_only:1;
86 l_uint limit_in_pages:1;
87 l_uint seg_not_present:1;
91 struct l_old_select_argv {
96 struct l_timeval *timeout;
101 linux_execve(struct thread *td, struct linux_execve_args *args)
103 struct image_args eargs;
107 LCONVPATHEXIST(td, args->path, &newpath);
109 error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
110 args->argp, args->envp);
111 free(newpath, M_TEMP);
113 error = linux_common_execve(td, &eargs);
117 struct l_ipc_kludge {
118 struct l_msgbuf *msgp;
123 linux_ipc(struct thread *td, struct linux_ipc_args *args)
126 switch (args->what & 0xFFFF) {
128 struct linux_semop_args a;
130 a.semid = args->arg1;
131 a.tsops = PTRIN(args->ptr);
132 a.nsops = args->arg2;
133 return (linux_semop(td, &a));
136 struct linux_semget_args a;
139 a.nsems = args->arg2;
140 a.semflg = args->arg3;
141 return (linux_semget(td, &a));
144 struct linux_semctl_args a;
147 a.semid = args->arg1;
148 a.semnum = args->arg2;
150 error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
153 return (linux_semctl(td, &a));
156 struct linux_msgsnd_args a;
158 a.msqid = args->arg1;
159 a.msgp = PTRIN(args->ptr);
160 a.msgsz = args->arg2;
161 a.msgflg = args->arg3;
162 return (linux_msgsnd(td, &a));
165 struct linux_msgrcv_args a;
167 a.msqid = args->arg1;
168 a.msgsz = args->arg2;
169 a.msgflg = args->arg3;
170 if ((args->what >> 16) == 0) {
171 struct l_ipc_kludge tmp;
176 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
179 a.msgp = PTRIN(tmp.msgp);
180 a.msgtyp = tmp.msgtyp;
182 a.msgp = PTRIN(args->ptr);
183 a.msgtyp = args->arg5;
185 return (linux_msgrcv(td, &a));
188 struct linux_msgget_args a;
191 a.msgflg = args->arg2;
192 return (linux_msgget(td, &a));
195 struct linux_msgctl_args a;
197 a.msqid = args->arg1;
199 a.buf = PTRIN(args->ptr);
200 return (linux_msgctl(td, &a));
203 struct linux_shmat_args a;
207 a.shmid = args->arg1;
208 a.shmaddr = PTRIN(args->ptr);
209 a.shmflg = args->arg2;
210 error = linux_shmat(td, &a);
213 addr = td->td_retval[0];
214 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
215 td->td_retval[0] = 0;
219 struct linux_shmdt_args a;
221 a.shmaddr = PTRIN(args->ptr);
222 return (linux_shmdt(td, &a));
225 struct linux_shmget_args a;
229 a.shmflg = args->arg3;
230 return (linux_shmget(td, &a));
233 struct linux_shmctl_args a;
235 a.shmid = args->arg1;
237 a.buf = PTRIN(args->ptr);
238 return (linux_shmctl(td, &a));
248 linux_old_select(struct thread *td, struct linux_old_select_args *args)
250 struct l_old_select_argv linux_args;
251 struct linux_select_args newsel;
254 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
258 newsel.nfds = linux_args.nfds;
259 newsel.readfds = linux_args.readfds;
260 newsel.writefds = linux_args.writefds;
261 newsel.exceptfds = linux_args.exceptfds;
262 newsel.timeout = linux_args.timeout;
263 return (linux_select(td, &newsel));
267 linux_set_cloned_tls(struct thread *td, void *desc)
269 struct segment_descriptor sd;
270 struct l_user_desc info;
274 error = copyin(desc, &info, sizeof(struct l_user_desc));
276 linux_msg(td, "set_cloned_tls copyin failed!");
278 idx = info.entry_number;
281 * looks like we're getting the idx we returned
282 * in the set_thread_area() syscall
284 if (idx != 6 && idx != 3) {
285 linux_msg(td, "set_cloned_tls resetting idx!");
289 /* this doesnt happen in practice */
291 /* we might copy out the entry_number as 3 */
292 info.entry_number = 3;
293 error = copyout(&info, desc, sizeof(struct l_user_desc));
295 linux_msg(td, "set_cloned_tls copyout failed!");
298 a[0] = LINUX_LDT_entry_a(&info);
299 a[1] = LINUX_LDT_entry_b(&info);
301 memcpy(&sd, &a, sizeof(a));
303 td->td_pcb->pcb_gsd = sd;
304 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
311 linux_set_upcall_kse(struct thread *td, register_t stack)
315 td->td_frame->tf_esp = stack;
318 * The newly created Linux thread returns
319 * to the user space by the same path that a parent do.
321 td->td_frame->tf_eax = 0;
326 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
329 return (linux_mmap_common(td, args->addr, args->len, args->prot,
330 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
335 linux_mmap(struct thread *td, struct linux_mmap_args *args)
338 struct l_mmap_argv linux_args;
340 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
344 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
345 linux_args.prot, linux_args.flags, linux_args.fd,
346 (uint32_t)linux_args.pgoff));
350 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
353 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
357 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
360 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
364 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
367 struct i386_ioperm_args iia;
369 iia.start = args->start;
370 iia.length = args->length;
371 iia.enable = args->enable;
372 error = i386_set_ioperm(td, &iia);
377 linux_iopl(struct thread *td, struct linux_iopl_args *args)
381 if (args->level < 0 || args->level > 3)
383 if ((error = priv_check(td, PRIV_IO)) != 0)
385 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
387 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
388 (args->level * (PSL_IOPL / 3));
393 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
396 struct i386_ldt_args ldt;
397 struct l_descriptor ld;
398 union descriptor desc;
402 case 0x00: /* read_ldt */
404 ldt.descs = uap->ptr;
405 ldt.num = uap->bytecount / sizeof(union descriptor);
406 error = i386_get_ldt(td, &ldt);
407 td->td_retval[0] *= sizeof(union descriptor);
409 case 0x02: /* read_default_ldt = 0 */
410 size = 5*sizeof(struct l_desc_struct);
411 if (size > uap->bytecount)
412 size = uap->bytecount;
413 for (written = error = 0; written < size && error == 0; written++)
414 error = subyte((char *)uap->ptr + written, 0);
415 td->td_retval[0] = written;
417 case 0x01: /* write_ldt */
418 case 0x11: /* write_ldt */
419 if (uap->bytecount != sizeof(ld))
422 error = copyin(uap->ptr, &ld, sizeof(ld));
426 ldt.start = ld.entry_number;
429 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
430 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
431 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
432 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
433 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
436 desc.sd.sd_p = (ld.seg_not_present ^ 1);
438 desc.sd.sd_def32 = ld.seg_32bit;
439 desc.sd.sd_gran = ld.limit_in_pages;
440 error = i386_set_ldt(td, &ldt, &desc);
447 if (error == EOPNOTSUPP) {
448 linux_msg(td, "modify_ldt needs kernel option USER_LDT");
456 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
459 l_sigaction_t act, oact;
462 if (args->nsa != NULL) {
463 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
466 act.lsa_handler = osa.lsa_handler;
467 act.lsa_flags = osa.lsa_flags;
468 act.lsa_restorer = osa.lsa_restorer;
469 LINUX_SIGEMPTYSET(act.lsa_mask);
470 act.lsa_mask.__mask = osa.lsa_mask;
473 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
474 args->osa ? &oact : NULL);
476 if (args->osa != NULL && !error) {
477 osa.lsa_handler = oact.lsa_handler;
478 osa.lsa_flags = oact.lsa_flags;
479 osa.lsa_restorer = oact.lsa_restorer;
480 osa.lsa_mask = oact.lsa_mask.__mask;
481 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
488 * Linux has two extra args, restart and oldmask. We dont use these,
489 * but it seems that "restart" is actually a context pointer that
490 * enables the signal to happen with a different register set.
493 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
498 LINUX_SIGEMPTYSET(mask);
499 mask.__mask = args->mask;
500 linux_to_bsd_sigset(&mask, &sigmask);
501 return (kern_sigsuspend(td, sigmask));
505 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
511 if (uap->sigsetsize != sizeof(l_sigset_t))
514 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
518 linux_to_bsd_sigset(&lmask, &sigmask);
519 return (kern_sigsuspend(td, sigmask));
523 linux_pause(struct thread *td, struct linux_pause_args *args)
525 struct proc *p = td->td_proc;
529 sigmask = td->td_sigmask;
531 return (kern_sigsuspend(td, sigmask));
535 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
541 if (uap->uss != NULL) {
542 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
546 ss.ss_sp = lss.ss_sp;
547 ss.ss_size = lss.ss_size;
548 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
550 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
551 (uap->uoss != NULL) ? &oss : NULL);
552 if (!error && uap->uoss != NULL) {
553 lss.ss_sp = oss.ss_sp;
554 lss.ss_size = oss.ss_size;
555 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
556 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
563 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
565 struct l_user_desc info;
569 struct segment_descriptor sd;
571 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
575 idx = info.entry_number;
577 * Semantics of Linux version: every thread in the system has array of
578 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
579 * syscall loads one of the selected tls decriptors with a value and
580 * also loads GDT descriptors 6, 7 and 8 with the content of the
581 * per-thread descriptors.
583 * Semantics of FreeBSD version: I think we can ignore that Linux has 3
584 * per-thread descriptors and use just the 1st one. The tls_array[]
585 * is used only in set/get-thread_area() syscalls and for loading the
586 * GDT descriptors. In FreeBSD we use just one GDT descriptor for TLS
587 * so we will load just one.
589 * XXX: this doesn't work when a user space process tries to use more
590 * than 1 TLS segment. Comment in the Linux sources says wine might do
595 * we support just GLIBC TLS now
596 * we should let 3 proceed as well because we use this segment so
597 * if code does two subsequent calls it should succeed
599 if (idx != 6 && idx != -1 && idx != 3)
603 * we have to copy out the GDT entry we use
604 * FreeBSD uses GDT entry #3 for storing %gs so load that
606 * XXX: what if a user space program doesn't check this value and tries
609 idx = info.entry_number = 3;
610 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
614 if (LINUX_LDT_empty(&info)) {
618 a[0] = LINUX_LDT_entry_a(&info);
619 a[1] = LINUX_LDT_entry_b(&info);
622 memcpy(&sd, &a, sizeof(a));
623 /* this is taken from i386 version of cpu_set_user_tls() */
626 td->td_pcb->pcb_gsd = sd;
627 PCPU_GET(fsgs_gdt)[1] = sd;
628 load_gs(GSEL(GUGS_SEL, SEL_UPL));
635 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
638 struct l_user_desc info;
641 struct l_desc_struct desc;
642 struct segment_descriptor sd;
644 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
648 idx = info.entry_number;
649 /* XXX: I am not sure if we want 3 to be allowed too. */
650 if (idx != 6 && idx != 3)
655 memset(&info, 0, sizeof(info));
657 sd = PCPU_GET(fsgs_gdt)[1];
659 memcpy(&desc, &sd, sizeof(desc));
661 info.entry_number = idx;
662 info.base_addr = LINUX_GET_BASE(&desc);
663 info.limit = LINUX_GET_LIMIT(&desc);
664 info.seg_32bit = LINUX_GET_32BIT(&desc);
665 info.contents = LINUX_GET_CONTENTS(&desc);
666 info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
667 info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
668 info.seg_not_present = !LINUX_GET_PRESENT(&desc);
669 info.useable = LINUX_GET_USEABLE(&desc);
671 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
678 /* XXX: this wont work with module - convert it */
680 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
682 #ifdef P1003_1B_MQUEUE
683 return (sys_kmq_open(td, (struct kmq_open_args *)args));
690 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
692 #ifdef P1003_1B_MQUEUE
693 return (sys_kmq_unlink(td, (struct kmq_unlink_args *)args));
700 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
702 #ifdef P1003_1B_MQUEUE
703 return (sys_kmq_timedsend(td, (struct kmq_timedsend_args *)args));
710 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
712 #ifdef P1003_1B_MQUEUE
713 return (sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *)args));
720 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
722 #ifdef P1003_1B_MQUEUE
723 return (sys_kmq_notify(td, (struct kmq_notify_args *)args));
730 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
732 #ifdef P1003_1B_MQUEUE
733 return (sys_kmq_setattr(td, (struct kmq_setattr_args *)args));