2 * SPDX-License-Identifier: BSD-3-Clause
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 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/capsicum.h>
38 #include <sys/fcntl.h>
39 #include <sys/imgact.h>
41 #include <sys/malloc.h>
43 #include <sys/mutex.h>
47 #include <sys/queue.h>
48 #include <sys/resource.h>
49 #include <sys/resourcevar.h>
50 #include <sys/signalvar.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysproto.h>
53 #include <sys/unistd.h>
55 #include <sys/sched.h>
57 #include <machine/frame.h>
58 #include <machine/psl.h>
59 #include <machine/segments.h>
60 #include <machine/sysarch.h>
64 #include <vm/vm_map.h>
66 #include <i386/linux/linux.h>
67 #include <i386/linux/linux_proto.h>
68 #include <compat/linux/linux_ipc.h>
69 #include <compat/linux/linux_misc.h>
70 #include <compat/linux/linux_mmap.h>
71 #include <compat/linux/linux_signal.h>
72 #include <compat/linux/linux_util.h>
73 #include <compat/linux/linux_emul.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);
113 printf(ARGS(execve, "%s"), newpath);
116 error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
117 args->argp, args->envp);
118 free(newpath, M_TEMP);
120 error = linux_common_execve(td, &eargs);
124 struct l_ipc_kludge {
125 struct l_msgbuf *msgp;
130 linux_ipc(struct thread *td, struct linux_ipc_args *args)
133 switch (args->what & 0xFFFF) {
135 struct linux_semop_args a;
137 a.semid = args->arg1;
139 a.nsops = args->arg2;
140 return (linux_semop(td, &a));
143 struct linux_semget_args a;
146 a.nsems = args->arg2;
147 a.semflg = args->arg3;
148 return (linux_semget(td, &a));
151 struct linux_semctl_args a;
154 a.semid = args->arg1;
155 a.semnum = args->arg2;
157 error = copyin(args->ptr, &a.arg, sizeof(a.arg));
160 return (linux_semctl(td, &a));
163 struct linux_msgsnd_args a;
165 a.msqid = args->arg1;
167 a.msgsz = args->arg2;
168 a.msgflg = args->arg3;
169 return (linux_msgsnd(td, &a));
172 struct linux_msgrcv_args a;
174 a.msqid = args->arg1;
175 a.msgsz = args->arg2;
176 a.msgflg = args->arg3;
177 if ((args->what >> 16) == 0) {
178 struct l_ipc_kludge tmp;
181 if (args->ptr == NULL)
183 error = copyin(args->ptr, &tmp, sizeof(tmp));
187 a.msgtyp = tmp.msgtyp;
190 a.msgtyp = args->arg5;
192 return (linux_msgrcv(td, &a));
195 struct linux_msgget_args a;
198 a.msgflg = args->arg2;
199 return (linux_msgget(td, &a));
202 struct linux_msgctl_args a;
204 a.msqid = args->arg1;
207 return (linux_msgctl(td, &a));
210 struct linux_shmat_args a;
212 a.shmid = args->arg1;
213 a.shmaddr = args->ptr;
214 a.shmflg = args->arg2;
215 a.raddr = (l_ulong *)args->arg3;
216 return (linux_shmat(td, &a));
219 struct linux_shmdt_args a;
221 a.shmaddr = 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;
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;
255 if (ldebug(old_select))
256 printf(ARGS(old_select, "%p"), args->ptr);
259 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
263 newsel.nfds = linux_args.nfds;
264 newsel.readfds = linux_args.readfds;
265 newsel.writefds = linux_args.writefds;
266 newsel.exceptfds = linux_args.exceptfds;
267 newsel.timeout = linux_args.timeout;
268 return (linux_select(td, &newsel));
272 linux_set_cloned_tls(struct thread *td, void *desc)
274 struct segment_descriptor sd;
275 struct l_user_desc info;
279 error = copyin(desc, &info, sizeof(struct l_user_desc));
281 printf(LMSG("copyin failed!"));
283 idx = info.entry_number;
286 * looks like we're getting the idx we returned
287 * in the set_thread_area() syscall
289 if (idx != 6 && idx != 3) {
290 printf(LMSG("resetting idx!"));
294 /* this doesnt happen in practice */
296 /* we might copy out the entry_number as 3 */
297 info.entry_number = 3;
298 error = copyout(&info, desc, sizeof(struct l_user_desc));
300 printf(LMSG("copyout failed!"));
303 a[0] = LINUX_LDT_entry_a(&info);
304 a[1] = LINUX_LDT_entry_b(&info);
306 memcpy(&sd, &a, sizeof(a));
309 printf("Segment created in clone with "
310 "CLONE_SETTLS: lobase: %x, hibase: %x, "
311 "lolimit: %x, hilimit: %x, type: %i, "
312 "dpl: %i, p: %i, xx: %i, def32: %i, "
313 "gran: %i\n", sd.sd_lobase, sd.sd_hibase,
314 sd.sd_lolimit, sd.sd_hilimit, sd.sd_type,
315 sd.sd_dpl, sd.sd_p, sd.sd_xx,
316 sd.sd_def32, sd.sd_gran);
320 td->td_pcb->pcb_gsd = sd;
321 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
328 linux_set_upcall_kse(struct thread *td, register_t stack)
332 td->td_frame->tf_esp = stack;
335 * The newly created Linux thread returns
336 * to the user space by the same path that a parent do.
338 td->td_frame->tf_eax = 0;
343 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
348 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
349 (void *)args->addr, args->len, args->prot,
350 args->flags, args->fd, args->pgoff);
353 return (linux_mmap_common(td, args->addr, args->len, args->prot,
354 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
359 linux_mmap(struct thread *td, struct linux_mmap_args *args)
362 struct l_mmap_argv linux_args;
364 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
370 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
371 (void *)linux_args.addr, linux_args.len, linux_args.prot,
372 linux_args.flags, linux_args.fd, linux_args.pgoff);
375 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
376 linux_args.prot, linux_args.flags, linux_args.fd,
377 (uint32_t)linux_args.pgoff));
381 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
384 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
388 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
391 struct i386_ioperm_args iia;
393 iia.start = args->start;
394 iia.length = args->length;
395 iia.enable = args->enable;
396 error = i386_set_ioperm(td, &iia);
401 linux_iopl(struct thread *td, struct linux_iopl_args *args)
405 if (args->level < 0 || args->level > 3)
407 if ((error = priv_check(td, PRIV_IO)) != 0)
409 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
411 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
412 (args->level * (PSL_IOPL / 3));
417 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
420 struct i386_ldt_args ldt;
421 struct l_descriptor ld;
422 union descriptor desc;
426 case 0x00: /* read_ldt */
428 ldt.descs = uap->ptr;
429 ldt.num = uap->bytecount / sizeof(union descriptor);
430 error = i386_get_ldt(td, &ldt);
431 td->td_retval[0] *= sizeof(union descriptor);
433 case 0x02: /* read_default_ldt = 0 */
434 size = 5*sizeof(struct l_desc_struct);
435 if (size > uap->bytecount)
436 size = uap->bytecount;
437 for (written = error = 0; written < size && error == 0; written++)
438 error = subyte((char *)uap->ptr + written, 0);
439 td->td_retval[0] = written;
441 case 0x01: /* write_ldt */
442 case 0x11: /* write_ldt */
443 if (uap->bytecount != sizeof(ld))
446 error = copyin(uap->ptr, &ld, sizeof(ld));
450 ldt.start = ld.entry_number;
453 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
454 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
455 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
456 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
457 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
460 desc.sd.sd_p = (ld.seg_not_present ^ 1);
462 desc.sd.sd_def32 = ld.seg_32bit;
463 desc.sd.sd_gran = ld.limit_in_pages;
464 error = i386_set_ldt(td, &ldt, &desc);
471 if (error == EOPNOTSUPP) {
472 printf("linux: modify_ldt needs kernel option USER_LDT\n");
480 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
483 l_sigaction_t act, oact;
487 if (ldebug(sigaction))
488 printf(ARGS(sigaction, "%d, %p, %p"),
489 args->sig, (void *)args->nsa, (void *)args->osa);
492 if (args->nsa != NULL) {
493 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
496 act.lsa_handler = osa.lsa_handler;
497 act.lsa_flags = osa.lsa_flags;
498 act.lsa_restorer = osa.lsa_restorer;
499 LINUX_SIGEMPTYSET(act.lsa_mask);
500 act.lsa_mask.__mask = osa.lsa_mask;
503 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
504 args->osa ? &oact : NULL);
506 if (args->osa != NULL && !error) {
507 osa.lsa_handler = oact.lsa_handler;
508 osa.lsa_flags = oact.lsa_flags;
509 osa.lsa_restorer = oact.lsa_restorer;
510 osa.lsa_mask = oact.lsa_mask.__mask;
511 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
518 * Linux has two extra args, restart and oldmask. We dont use these,
519 * but it seems that "restart" is actually a context pointer that
520 * enables the signal to happen with a different register set.
523 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
529 if (ldebug(sigsuspend))
530 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
533 LINUX_SIGEMPTYSET(mask);
534 mask.__mask = args->mask;
535 linux_to_bsd_sigset(&mask, &sigmask);
536 return (kern_sigsuspend(td, sigmask));
540 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
547 if (ldebug(rt_sigsuspend))
548 printf(ARGS(rt_sigsuspend, "%p, %d"),
549 (void *)uap->newset, uap->sigsetsize);
552 if (uap->sigsetsize != sizeof(l_sigset_t))
555 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
559 linux_to_bsd_sigset(&lmask, &sigmask);
560 return (kern_sigsuspend(td, sigmask));
564 linux_pause(struct thread *td, struct linux_pause_args *args)
566 struct proc *p = td->td_proc;
571 printf(ARGS(pause, ""));
575 sigmask = td->td_sigmask;
577 return (kern_sigsuspend(td, sigmask));
581 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
588 if (ldebug(sigaltstack))
589 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
592 if (uap->uss != NULL) {
593 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
597 ss.ss_sp = lss.ss_sp;
598 ss.ss_size = lss.ss_size;
599 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
601 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
602 (uap->uoss != NULL) ? &oss : NULL);
603 if (!error && uap->uoss != NULL) {
604 lss.ss_sp = oss.ss_sp;
605 lss.ss_size = oss.ss_size;
606 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
607 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
614 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
618 if (ldebug(ftruncate64))
619 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
620 (intmax_t)args->length);
623 return (kern_ftruncate(td, args->fd, args->length));
627 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
629 struct l_user_desc info;
633 struct segment_descriptor sd;
635 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
640 if (ldebug(set_thread_area))
641 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
649 info.seg_not_present,
653 idx = info.entry_number;
655 * Semantics of linux version: every thread in the system has array of
656 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
657 * syscall loads one of the selected tls decriptors with a value and
658 * also loads GDT descriptors 6, 7 and 8 with the content of the
659 * per-thread descriptors.
661 * Semantics of fbsd version: I think we can ignore that linux has 3
662 * per-thread descriptors and use just the 1st one. The tls_array[]
663 * is used only in set/get-thread_area() syscalls and for loading the
664 * GDT descriptors. In fbsd we use just one GDT descriptor for TLS so
665 * we will load just one.
667 * XXX: this doesn't work when a user space process tries to use more
668 * than 1 TLS segment. Comment in the linux sources says wine might do
673 * we support just GLIBC TLS now
674 * we should let 3 proceed as well because we use this segment so
675 * if code does two subsequent calls it should succeed
677 if (idx != 6 && idx != -1 && idx != 3)
681 * we have to copy out the GDT entry we use
682 * FreeBSD uses GDT entry #3 for storing %gs so load that
684 * XXX: what if a user space program doesn't check this value and tries
687 idx = info.entry_number = 3;
688 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
692 if (LINUX_LDT_empty(&info)) {
696 a[0] = LINUX_LDT_entry_a(&info);
697 a[1] = LINUX_LDT_entry_b(&info);
700 memcpy(&sd, &a, sizeof(a));
702 if (ldebug(set_thread_area))
703 printf("Segment created in set_thread_area: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
715 /* this is taken from i386 version of cpu_set_user_tls() */
718 td->td_pcb->pcb_gsd = sd;
719 PCPU_GET(fsgs_gdt)[1] = sd;
720 load_gs(GSEL(GUGS_SEL, SEL_UPL));
727 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
730 struct l_user_desc info;
733 struct l_desc_struct desc;
734 struct segment_descriptor sd;
737 if (ldebug(get_thread_area))
738 printf(ARGS(get_thread_area, "%p"), args->desc);
741 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
745 idx = info.entry_number;
746 /* XXX: I am not sure if we want 3 to be allowed too. */
747 if (idx != 6 && idx != 3)
752 memset(&info, 0, sizeof(info));
754 sd = PCPU_GET(fsgs_gdt)[1];
756 memcpy(&desc, &sd, sizeof(desc));
758 info.entry_number = idx;
759 info.base_addr = LINUX_GET_BASE(&desc);
760 info.limit = LINUX_GET_LIMIT(&desc);
761 info.seg_32bit = LINUX_GET_32BIT(&desc);
762 info.contents = LINUX_GET_CONTENTS(&desc);
763 info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
764 info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
765 info.seg_not_present = !LINUX_GET_PRESENT(&desc);
766 info.useable = LINUX_GET_USEABLE(&desc);
768 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
775 /* XXX: this wont work with module - convert it */
777 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
779 #ifdef P1003_1B_MQUEUE
780 return sys_kmq_open(td, (struct kmq_open_args *) args);
787 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
789 #ifdef P1003_1B_MQUEUE
790 return sys_kmq_unlink(td, (struct kmq_unlink_args *) args);
797 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
799 #ifdef P1003_1B_MQUEUE
800 return sys_kmq_timedsend(td, (struct kmq_timedsend_args *) args);
807 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
809 #ifdef P1003_1B_MQUEUE
810 return sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
817 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
819 #ifdef P1003_1B_MQUEUE
820 return sys_kmq_notify(td, (struct kmq_notify_args *) args);
827 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
829 #ifdef P1003_1B_MQUEUE
830 return sys_kmq_setattr(td, (struct kmq_setattr_args *) args);