1146aad74SMarcel Moolenaar /*- 20ba1b365SEd Maste * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 383ef78beSPedro F. Giffuni * 4146aad74SMarcel Moolenaar * Copyright (c) 2000 Marcel Moolenaar 5146aad74SMarcel Moolenaar * All rights reserved. 6146aad74SMarcel Moolenaar * 7146aad74SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 8146aad74SMarcel Moolenaar * modification, are permitted provided that the following conditions 9146aad74SMarcel Moolenaar * are met: 10146aad74SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 110ba1b365SEd Maste * notice, this list of conditions and the following disclaimer. 12146aad74SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 13146aad74SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 14146aad74SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 15146aad74SMarcel Moolenaar * 160ba1b365SEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 170ba1b365SEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 180ba1b365SEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 190ba1b365SEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 200ba1b365SEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 210ba1b365SEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 220ba1b365SEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 230ba1b365SEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 240ba1b365SEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 250ba1b365SEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 260ba1b365SEd Maste * SUCH DAMAGE. 27146aad74SMarcel Moolenaar */ 28146aad74SMarcel Moolenaar 2927e0099cSDavid E. O'Brien #include <sys/cdefs.h> 3027e0099cSDavid E. O'Brien __FBSDID("$FreeBSD$"); 3127e0099cSDavid E. O'Brien 32146aad74SMarcel Moolenaar #include <sys/param.h> 334a144410SRobert Watson #include <sys/capsicum.h> 34a312f6a3SAlexander Leidinger #include <sys/fcntl.h> 35c0aa0e2cSEd Maste #include <sys/file.h> 36610ecfe0SMaxim Sobolev #include <sys/imgact.h> 377106ca0dSJohn Baldwin #include <sys/lock.h> 38610ecfe0SMaxim Sobolev #include <sys/malloc.h> 39fb919e4dSMark Murray #include <sys/mman.h> 40fb919e4dSMark Murray #include <sys/mutex.h> 41acd3428bSRobert Watson #include <sys/priv.h> 42fb919e4dSMark Murray #include <sys/proc.h> 439b44bfc5SAlexander Leidinger #include <sys/queue.h> 44242fae60SAndrew Gallatin #include <sys/resource.h> 45242fae60SAndrew Gallatin #include <sys/resourcevar.h> 46c0aa0e2cSEd Maste #include <sys/sched.h> 471bc85c0dSDoug Rabson #include <sys/signalvar.h> 48206a5d3aSIan Dowse #include <sys/syscallsubr.h> 49fb919e4dSMark Murray #include <sys/sysproto.h> 50c0aa0e2cSEd Maste #include <sys/systm.h> 51c0aa0e2cSEd Maste #include <sys/sx.h> 52fb919e4dSMark Murray #include <sys/unistd.h> 539b44bfc5SAlexander Leidinger #include <sys/wait.h> 54146aad74SMarcel Moolenaar 55146aad74SMarcel Moolenaar #include <machine/frame.h> 56146aad74SMarcel Moolenaar #include <machine/psl.h> 57146aad74SMarcel Moolenaar #include <machine/segments.h> 58146aad74SMarcel Moolenaar #include <machine/sysarch.h> 59146aad74SMarcel Moolenaar 60242fae60SAndrew Gallatin #include <vm/pmap.h> 61c0aa0e2cSEd Maste #include <vm/vm.h> 62242fae60SAndrew Gallatin #include <vm/vm_map.h> 63242fae60SAndrew Gallatin 64d42a83b1SKyle Evans #include <security/audit/audit.h> 65d42a83b1SKyle Evans 66146aad74SMarcel Moolenaar #include <i386/linux/linux.h> 67ebea8660SMarcel Moolenaar #include <i386/linux/linux_proto.h> 68c0aa0e2cSEd Maste #include <compat/linux/linux_emul.h> 690a4b664aSDmitry Chagin #include <compat/linux/linux_fork.h> 70146aad74SMarcel Moolenaar #include <compat/linux/linux_ipc.h> 71adc7ece0SDmitry Chagin #include <compat/linux/linux_misc.h> 7297d06da6SDmitry Chagin #include <compat/linux/linux_mmap.h> 73146aad74SMarcel Moolenaar #include <compat/linux/linux_signal.h> 74146aad74SMarcel Moolenaar #include <compat/linux/linux_util.h> 759b44bfc5SAlexander Leidinger 769b44bfc5SAlexander Leidinger #include <i386/include/pcb.h> /* needed for pcb definition in linux_set_thread_area */ 779b44bfc5SAlexander Leidinger 789b44bfc5SAlexander Leidinger #include "opt_posix.h" 799b44bfc5SAlexander Leidinger 805002a60fSMarcel Moolenaar struct l_descriptor { 815002a60fSMarcel Moolenaar l_uint entry_number; 825002a60fSMarcel Moolenaar l_ulong base_addr; 835002a60fSMarcel Moolenaar l_uint limit; 845002a60fSMarcel Moolenaar l_uint seg_32bit:1; 855002a60fSMarcel Moolenaar l_uint contents:2; 865002a60fSMarcel Moolenaar l_uint read_exec_only:1; 875002a60fSMarcel Moolenaar l_uint limit_in_pages:1; 885002a60fSMarcel Moolenaar l_uint seg_not_present:1; 895002a60fSMarcel Moolenaar l_uint useable:1; 90146aad74SMarcel Moolenaar }; 91146aad74SMarcel Moolenaar 925002a60fSMarcel Moolenaar struct l_old_select_argv { 935002a60fSMarcel Moolenaar l_int nfds; 945002a60fSMarcel Moolenaar l_fd_set *readfds; 955002a60fSMarcel Moolenaar l_fd_set *writefds; 965002a60fSMarcel Moolenaar l_fd_set *exceptfds; 975002a60fSMarcel Moolenaar struct l_timeval *timeout; 98146aad74SMarcel Moolenaar }; 99146aad74SMarcel Moolenaar 100931a7258SAndrew Gallatin int 101b40ce416SJulian Elischer linux_execve(struct thread *td, struct linux_execve_args *args) 102146aad74SMarcel Moolenaar { 103610ecfe0SMaxim Sobolev struct image_args eargs; 1047b445033SKonstantin Belousov char *newpath; 1057b445033SKonstantin Belousov int error; 106146aad74SMarcel Moolenaar 107a125ed50SMateusz Guzik if (!LUSECONVPATH(td)) { 108a125ed50SMateusz Guzik error = exec_copyin_args(&eargs, args->path, UIO_USERSPACE, 109a125ed50SMateusz Guzik args->argp, args->envp); 110a125ed50SMateusz Guzik } else { 111af4051d2SMateusz Guzik LCONVPATHEXIST(args->path, &newpath); 112610ecfe0SMaxim Sobolev error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE, 113610ecfe0SMaxim Sobolev args->argp, args->envp); 114d5e3895eSMateusz Guzik LFREEPATH(newpath); 115a125ed50SMateusz Guzik } 116610ecfe0SMaxim Sobolev if (error == 0) 11781338031SDmitry Chagin error = linux_common_execve(td, &eargs); 118d42a83b1SKyle Evans AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td); 119610ecfe0SMaxim Sobolev return (error); 120146aad74SMarcel Moolenaar } 121146aad74SMarcel Moolenaar 1225002a60fSMarcel Moolenaar struct l_ipc_kludge { 1235002a60fSMarcel Moolenaar struct l_msgbuf *msgp; 1245002a60fSMarcel Moolenaar l_long msgtyp; 1255002a60fSMarcel Moolenaar }; 1265002a60fSMarcel Moolenaar 127146aad74SMarcel Moolenaar int 128b40ce416SJulian Elischer linux_ipc(struct thread *td, struct linux_ipc_args *args) 129146aad74SMarcel Moolenaar { 1305002a60fSMarcel Moolenaar 1315002a60fSMarcel Moolenaar switch (args->what & 0xFFFF) { 1325002a60fSMarcel Moolenaar case LINUX_SEMOP: { 1335002a60fSMarcel Moolenaar 134f48a6887SDmitry Chagin return (kern_semop(td, args->arg1, PTRIN(args->ptr), 135f48a6887SDmitry Chagin args->arg2, NULL)); 1365002a60fSMarcel Moolenaar } 1375002a60fSMarcel Moolenaar case LINUX_SEMGET: { 1385002a60fSMarcel Moolenaar struct linux_semget_args a; 1395002a60fSMarcel Moolenaar 1405002a60fSMarcel Moolenaar a.key = args->arg1; 1415002a60fSMarcel Moolenaar a.nsems = args->arg2; 1425002a60fSMarcel Moolenaar a.semflg = args->arg3; 143b40ce416SJulian Elischer return (linux_semget(td, &a)); 1445002a60fSMarcel Moolenaar } 1455002a60fSMarcel Moolenaar case LINUX_SEMCTL: { 1465002a60fSMarcel Moolenaar struct linux_semctl_args a; 1475002a60fSMarcel Moolenaar int error; 1485002a60fSMarcel Moolenaar 1495002a60fSMarcel Moolenaar a.semid = args->arg1; 1505002a60fSMarcel Moolenaar a.semnum = args->arg2; 1515002a60fSMarcel Moolenaar a.cmd = args->arg3; 1527dabf89bSDmitry Chagin error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg)); 1535002a60fSMarcel Moolenaar if (error) 1545002a60fSMarcel Moolenaar return (error); 155b40ce416SJulian Elischer return (linux_semctl(td, &a)); 1565002a60fSMarcel Moolenaar } 157*3245a2ecSDmitry Chagin case LINUX_SEMTIMEDOP: { 158*3245a2ecSDmitry Chagin struct linux_semtimedop_args a; 159*3245a2ecSDmitry Chagin 160*3245a2ecSDmitry Chagin a.semid = args->arg1; 161*3245a2ecSDmitry Chagin a.tsops = PTRIN(args->ptr); 162*3245a2ecSDmitry Chagin a.nsops = args->arg2; 163*3245a2ecSDmitry Chagin a.timeout = PTRIN(args->arg5); 164*3245a2ecSDmitry Chagin return (linux_semtimedop(td, &a)); 165*3245a2ecSDmitry Chagin } 1665002a60fSMarcel Moolenaar case LINUX_MSGSND: { 1675002a60fSMarcel Moolenaar struct linux_msgsnd_args a; 1685002a60fSMarcel Moolenaar 1695002a60fSMarcel Moolenaar a.msqid = args->arg1; 1707dabf89bSDmitry Chagin a.msgp = PTRIN(args->ptr); 1715002a60fSMarcel Moolenaar a.msgsz = args->arg2; 1725002a60fSMarcel Moolenaar a.msgflg = args->arg3; 173b40ce416SJulian Elischer return (linux_msgsnd(td, &a)); 1745002a60fSMarcel Moolenaar } 1755002a60fSMarcel Moolenaar case LINUX_MSGRCV: { 1765002a60fSMarcel Moolenaar struct linux_msgrcv_args a; 1775002a60fSMarcel Moolenaar 1785002a60fSMarcel Moolenaar a.msqid = args->arg1; 1795002a60fSMarcel Moolenaar a.msgsz = args->arg2; 1805002a60fSMarcel Moolenaar a.msgflg = args->arg3; 1815002a60fSMarcel Moolenaar if ((args->what >> 16) == 0) { 1825002a60fSMarcel Moolenaar struct l_ipc_kludge tmp; 1835002a60fSMarcel Moolenaar int error; 1845002a60fSMarcel Moolenaar 1857dabf89bSDmitry Chagin if (args->ptr == 0) 1865002a60fSMarcel Moolenaar return (EINVAL); 1877dabf89bSDmitry Chagin error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp)); 1885002a60fSMarcel Moolenaar if (error) 1895002a60fSMarcel Moolenaar return (error); 1907dabf89bSDmitry Chagin a.msgp = PTRIN(tmp.msgp); 1915002a60fSMarcel Moolenaar a.msgtyp = tmp.msgtyp; 1925002a60fSMarcel Moolenaar } else { 1937dabf89bSDmitry Chagin a.msgp = PTRIN(args->ptr); 1945002a60fSMarcel Moolenaar a.msgtyp = args->arg5; 1955002a60fSMarcel Moolenaar } 196b40ce416SJulian Elischer return (linux_msgrcv(td, &a)); 1975002a60fSMarcel Moolenaar } 1985002a60fSMarcel Moolenaar case LINUX_MSGGET: { 1995002a60fSMarcel Moolenaar struct linux_msgget_args a; 2005002a60fSMarcel Moolenaar 2015002a60fSMarcel Moolenaar a.key = args->arg1; 2025002a60fSMarcel Moolenaar a.msgflg = args->arg2; 203b40ce416SJulian Elischer return (linux_msgget(td, &a)); 2045002a60fSMarcel Moolenaar } 2055002a60fSMarcel Moolenaar case LINUX_MSGCTL: { 2065002a60fSMarcel Moolenaar struct linux_msgctl_args a; 2075002a60fSMarcel Moolenaar 2085002a60fSMarcel Moolenaar a.msqid = args->arg1; 2095002a60fSMarcel Moolenaar a.cmd = args->arg2; 2107dabf89bSDmitry Chagin a.buf = PTRIN(args->ptr); 211b40ce416SJulian Elischer return (linux_msgctl(td, &a)); 2125002a60fSMarcel Moolenaar } 2135002a60fSMarcel Moolenaar case LINUX_SHMAT: { 2145002a60fSMarcel Moolenaar struct linux_shmat_args a; 2157dabf89bSDmitry Chagin l_uintptr_t addr; 2167dabf89bSDmitry Chagin int error; 2175002a60fSMarcel Moolenaar 2185002a60fSMarcel Moolenaar a.shmid = args->arg1; 2197dabf89bSDmitry Chagin a.shmaddr = PTRIN(args->ptr); 2205002a60fSMarcel Moolenaar a.shmflg = args->arg2; 2217dabf89bSDmitry Chagin error = linux_shmat(td, &a); 2227dabf89bSDmitry Chagin if (error != 0) 2237dabf89bSDmitry Chagin return (error); 2247dabf89bSDmitry Chagin addr = td->td_retval[0]; 2257dabf89bSDmitry Chagin error = copyout(&addr, PTRIN(args->arg3), sizeof(addr)); 2267dabf89bSDmitry Chagin td->td_retval[0] = 0; 2277dabf89bSDmitry Chagin return (error); 2285002a60fSMarcel Moolenaar } 2295002a60fSMarcel Moolenaar case LINUX_SHMDT: { 2305002a60fSMarcel Moolenaar struct linux_shmdt_args a; 2315002a60fSMarcel Moolenaar 2327dabf89bSDmitry Chagin a.shmaddr = PTRIN(args->ptr); 233b40ce416SJulian Elischer return (linux_shmdt(td, &a)); 2345002a60fSMarcel Moolenaar } 2355002a60fSMarcel Moolenaar case LINUX_SHMGET: { 2365002a60fSMarcel Moolenaar struct linux_shmget_args a; 2375002a60fSMarcel Moolenaar 2385002a60fSMarcel Moolenaar a.key = args->arg1; 2395002a60fSMarcel Moolenaar a.size = args->arg2; 2405002a60fSMarcel Moolenaar a.shmflg = args->arg3; 241b40ce416SJulian Elischer return (linux_shmget(td, &a)); 2425002a60fSMarcel Moolenaar } 2435002a60fSMarcel Moolenaar case LINUX_SHMCTL: { 2445002a60fSMarcel Moolenaar struct linux_shmctl_args a; 2455002a60fSMarcel Moolenaar 2465002a60fSMarcel Moolenaar a.shmid = args->arg1; 2475002a60fSMarcel Moolenaar a.cmd = args->arg2; 2487dabf89bSDmitry Chagin a.buf = PTRIN(args->ptr); 249b40ce416SJulian Elischer return (linux_shmctl(td, &a)); 2505002a60fSMarcel Moolenaar } 2515002a60fSMarcel Moolenaar default: 2525002a60fSMarcel Moolenaar break; 253146aad74SMarcel Moolenaar } 254146aad74SMarcel Moolenaar 2555002a60fSMarcel Moolenaar return (EINVAL); 256146aad74SMarcel Moolenaar } 257146aad74SMarcel Moolenaar 258146aad74SMarcel Moolenaar int 259b40ce416SJulian Elischer linux_old_select(struct thread *td, struct linux_old_select_args *args) 260146aad74SMarcel Moolenaar { 2615002a60fSMarcel Moolenaar struct l_old_select_argv linux_args; 2625002a60fSMarcel Moolenaar struct linux_select_args newsel; 263146aad74SMarcel Moolenaar int error; 264146aad74SMarcel Moolenaar 2654b7ef73dSDag-Erling Smørgrav error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 266146aad74SMarcel Moolenaar if (error) 267146aad74SMarcel Moolenaar return (error); 268146aad74SMarcel Moolenaar 269146aad74SMarcel Moolenaar newsel.nfds = linux_args.nfds; 270146aad74SMarcel Moolenaar newsel.readfds = linux_args.readfds; 271146aad74SMarcel Moolenaar newsel.writefds = linux_args.writefds; 272146aad74SMarcel Moolenaar newsel.exceptfds = linux_args.exceptfds; 273146aad74SMarcel Moolenaar newsel.timeout = linux_args.timeout; 274b40ce416SJulian Elischer return (linux_select(td, &newsel)); 275146aad74SMarcel Moolenaar } 276146aad74SMarcel Moolenaar 277146aad74SMarcel Moolenaar int 2782c7660baSDmitry Chagin linux_set_cloned_tls(struct thread *td, void *desc) 2792c7660baSDmitry Chagin { 2802c7660baSDmitry Chagin struct segment_descriptor sd; 2812c7660baSDmitry Chagin struct l_user_desc info; 2822c7660baSDmitry Chagin int idx, error; 2832c7660baSDmitry Chagin int a[2]; 2842c7660baSDmitry Chagin 2852c7660baSDmitry Chagin error = copyin(desc, &info, sizeof(struct l_user_desc)); 2862c7660baSDmitry Chagin if (error) { 287c5156c77SDmitry Chagin linux_msg(td, "set_cloned_tls copyin failed!"); 2882c7660baSDmitry Chagin } else { 2892c7660baSDmitry Chagin idx = info.entry_number; 2902c7660baSDmitry Chagin 2912c7660baSDmitry Chagin /* 2922c7660baSDmitry Chagin * looks like we're getting the idx we returned 2932c7660baSDmitry Chagin * in the set_thread_area() syscall 2942c7660baSDmitry Chagin */ 2952c7660baSDmitry Chagin if (idx != 6 && idx != 3) { 296c5156c77SDmitry Chagin linux_msg(td, "set_cloned_tls resetting idx!"); 2972c7660baSDmitry Chagin idx = 3; 2982c7660baSDmitry Chagin } 2992c7660baSDmitry Chagin 3002c7660baSDmitry Chagin /* this doesnt happen in practice */ 3012c7660baSDmitry Chagin if (idx == 6) { 3022c7660baSDmitry Chagin /* we might copy out the entry_number as 3 */ 3032c7660baSDmitry Chagin info.entry_number = 3; 3042c7660baSDmitry Chagin error = copyout(&info, desc, sizeof(struct l_user_desc)); 3052c7660baSDmitry Chagin if (error) 306c5156c77SDmitry Chagin linux_msg(td, "set_cloned_tls copyout failed!"); 3072c7660baSDmitry Chagin } 3082c7660baSDmitry Chagin 3092c7660baSDmitry Chagin a[0] = LINUX_LDT_entry_a(&info); 3102c7660baSDmitry Chagin a[1] = LINUX_LDT_entry_b(&info); 3112c7660baSDmitry Chagin 3122c7660baSDmitry Chagin memcpy(&sd, &a, sizeof(a)); 3132c7660baSDmitry Chagin /* set %gs */ 3142c7660baSDmitry Chagin td->td_pcb->pcb_gsd = sd; 3152c7660baSDmitry Chagin td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL); 3162c7660baSDmitry Chagin } 3172c7660baSDmitry Chagin 3182c7660baSDmitry Chagin return (error); 3192c7660baSDmitry Chagin } 3202c7660baSDmitry Chagin 321146aad74SMarcel Moolenaar int 3223b57ddb0SJohn Baldwin linux_set_upcall(struct thread *td, register_t stack) 323c8d6845eSDmitry Chagin { 324c8d6845eSDmitry Chagin 32581338031SDmitry Chagin if (stack) 326c8d6845eSDmitry Chagin td->td_frame->tf_esp = stack; 327c8d6845eSDmitry Chagin 32881338031SDmitry Chagin /* 32981338031SDmitry Chagin * The newly created Linux thread returns 33081338031SDmitry Chagin * to the user space by the same path that a parent do. 33181338031SDmitry Chagin */ 33281338031SDmitry Chagin td->td_frame->tf_eax = 0; 333c8d6845eSDmitry Chagin return (0); 334c8d6845eSDmitry Chagin } 335c8d6845eSDmitry Chagin 3363ad9c842SMaxim Sobolev int 3373ad9c842SMaxim Sobolev linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 3383ad9c842SMaxim Sobolev { 3393ad9c842SMaxim Sobolev 340f12c0348SJohn Baldwin return (linux_mmap_common(td, args->addr, args->len, args->prot, 341f12c0348SJohn Baldwin args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff * 342f12c0348SJohn Baldwin PAGE_SIZE)); 3433ad9c842SMaxim Sobolev } 3443ad9c842SMaxim Sobolev 345146aad74SMarcel Moolenaar int 346b40ce416SJulian Elischer linux_mmap(struct thread *td, struct linux_mmap_args *args) 347146aad74SMarcel Moolenaar { 3483ad9c842SMaxim Sobolev int error; 3493ad9c842SMaxim Sobolev struct l_mmap_argv linux_args; 3503ad9c842SMaxim Sobolev 3514b7ef73dSDag-Erling Smørgrav error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 3523ad9c842SMaxim Sobolev if (error) 3533ad9c842SMaxim Sobolev return (error); 3543ad9c842SMaxim Sobolev 355f12c0348SJohn Baldwin return (linux_mmap_common(td, linux_args.addr, linux_args.len, 356f12c0348SJohn Baldwin linux_args.prot, linux_args.flags, linux_args.fd, 357f12c0348SJohn Baldwin (uint32_t)linux_args.pgoff)); 3583ad9c842SMaxim Sobolev } 3593ad9c842SMaxim Sobolev 360146aad74SMarcel Moolenaar int 36110931a46SJung-uk Kim linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 36210931a46SJung-uk Kim { 36310931a46SJung-uk Kim 36497d06da6SDmitry Chagin return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot)); 36510931a46SJung-uk Kim } 36610931a46SJung-uk Kim 36710931a46SJung-uk Kim int 36852c81be1SEdward Tomasz Napierala linux_madvise(struct thread *td, struct linux_madvise_args *uap) 36952c81be1SEdward Tomasz Napierala { 37052c81be1SEdward Tomasz Napierala 37152c81be1SEdward Tomasz Napierala return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav)); 37252c81be1SEdward Tomasz Napierala } 37352c81be1SEdward Tomasz Napierala 37452c81be1SEdward Tomasz Napierala int 375b40ce416SJulian Elischer linux_ioperm(struct thread *td, struct linux_ioperm_args *args) 376146aad74SMarcel Moolenaar { 37784569dffSMaxim Sobolev int error; 37884569dffSMaxim Sobolev struct i386_ioperm_args iia; 379146aad74SMarcel Moolenaar 38084569dffSMaxim Sobolev iia.start = args->start; 38184569dffSMaxim Sobolev iia.length = args->length; 38284569dffSMaxim Sobolev iia.enable = args->enable; 38384569dffSMaxim Sobolev error = i386_set_ioperm(td, &iia); 38484569dffSMaxim Sobolev return (error); 385146aad74SMarcel Moolenaar } 386146aad74SMarcel Moolenaar 387146aad74SMarcel Moolenaar int 388b40ce416SJulian Elischer linux_iopl(struct thread *td, struct linux_iopl_args *args) 389146aad74SMarcel Moolenaar { 390146aad74SMarcel Moolenaar int error; 391146aad74SMarcel Moolenaar 392146aad74SMarcel Moolenaar if (args->level < 0 || args->level > 3) 393146aad74SMarcel Moolenaar return (EINVAL); 394acd3428bSRobert Watson if ((error = priv_check(td, PRIV_IO)) != 0) 395146aad74SMarcel Moolenaar return (error); 396a854ed98SJohn Baldwin if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 39741c42188SRobert Watson return (error); 398b40ce416SJulian Elischer td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) | 399146aad74SMarcel Moolenaar (args->level * (PSL_IOPL / 3)); 400146aad74SMarcel Moolenaar return (0); 401146aad74SMarcel Moolenaar } 402146aad74SMarcel Moolenaar 403146aad74SMarcel Moolenaar int 404b07cd97eSMark Murray linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap) 405146aad74SMarcel Moolenaar { 406146aad74SMarcel Moolenaar int error; 40784569dffSMaxim Sobolev struct i386_ldt_args ldt; 4085002a60fSMarcel Moolenaar struct l_descriptor ld; 40984569dffSMaxim Sobolev union descriptor desc; 4106259969dSKonstantin Belousov int size, written; 411146aad74SMarcel Moolenaar 412146aad74SMarcel Moolenaar switch (uap->func) { 413146aad74SMarcel Moolenaar case 0x00: /* read_ldt */ 41484569dffSMaxim Sobolev ldt.start = 0; 41584569dffSMaxim Sobolev ldt.descs = uap->ptr; 41684569dffSMaxim Sobolev ldt.num = uap->bytecount / sizeof(union descriptor); 41784569dffSMaxim Sobolev error = i386_get_ldt(td, &ldt); 418b40ce416SJulian Elischer td->td_retval[0] *= sizeof(union descriptor); 419146aad74SMarcel Moolenaar break; 4206259969dSKonstantin Belousov case 0x02: /* read_default_ldt = 0 */ 4216259969dSKonstantin Belousov size = 5*sizeof(struct l_desc_struct); 4226259969dSKonstantin Belousov if (size > uap->bytecount) 4236259969dSKonstantin Belousov size = uap->bytecount; 4246259969dSKonstantin Belousov for (written = error = 0; written < size && error == 0; written++) 4256259969dSKonstantin Belousov error = subyte((char *)uap->ptr + written, 0); 4266259969dSKonstantin Belousov td->td_retval[0] = written; 4276259969dSKonstantin Belousov break; 428146aad74SMarcel Moolenaar case 0x01: /* write_ldt */ 429146aad74SMarcel Moolenaar case 0x11: /* write_ldt */ 430146aad74SMarcel Moolenaar if (uap->bytecount != sizeof(ld)) 431146aad74SMarcel Moolenaar return (EINVAL); 432146aad74SMarcel Moolenaar 433146aad74SMarcel Moolenaar error = copyin(uap->ptr, &ld, sizeof(ld)); 434146aad74SMarcel Moolenaar if (error) 435146aad74SMarcel Moolenaar return (error); 436146aad74SMarcel Moolenaar 43784569dffSMaxim Sobolev ldt.start = ld.entry_number; 43884569dffSMaxim Sobolev ldt.descs = &desc; 43984569dffSMaxim Sobolev ldt.num = 1; 44084569dffSMaxim Sobolev desc.sd.sd_lolimit = (ld.limit & 0x0000ffff); 44184569dffSMaxim Sobolev desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16; 44284569dffSMaxim Sobolev desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff); 44384569dffSMaxim Sobolev desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24; 44484569dffSMaxim Sobolev desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) | 445146aad74SMarcel Moolenaar (ld.contents << 2); 44684569dffSMaxim Sobolev desc.sd.sd_dpl = 3; 44784569dffSMaxim Sobolev desc.sd.sd_p = (ld.seg_not_present ^ 1); 44884569dffSMaxim Sobolev desc.sd.sd_xx = 0; 44984569dffSMaxim Sobolev desc.sd.sd_def32 = ld.seg_32bit; 45084569dffSMaxim Sobolev desc.sd.sd_gran = ld.limit_in_pages; 45184569dffSMaxim Sobolev error = i386_set_ldt(td, &ldt, &desc); 452146aad74SMarcel Moolenaar break; 453146aad74SMarcel Moolenaar default: 4547c020cbbSJohn Baldwin error = ENOSYS; 455146aad74SMarcel Moolenaar break; 456146aad74SMarcel Moolenaar } 457146aad74SMarcel Moolenaar 458146aad74SMarcel Moolenaar if (error == EOPNOTSUPP) { 459c5156c77SDmitry Chagin linux_msg(td, "modify_ldt needs kernel option USER_LDT"); 460146aad74SMarcel Moolenaar error = ENOSYS; 461146aad74SMarcel Moolenaar } 462146aad74SMarcel Moolenaar 463146aad74SMarcel Moolenaar return (error); 464146aad74SMarcel Moolenaar } 465146aad74SMarcel Moolenaar 466146aad74SMarcel Moolenaar int 467b40ce416SJulian Elischer linux_sigaction(struct thread *td, struct linux_sigaction_args *args) 468146aad74SMarcel Moolenaar { 4695002a60fSMarcel Moolenaar l_osigaction_t osa; 4705002a60fSMarcel Moolenaar l_sigaction_t act, oact; 471146aad74SMarcel Moolenaar int error; 472146aad74SMarcel Moolenaar 473146aad74SMarcel Moolenaar if (args->nsa != NULL) { 4744b7ef73dSDag-Erling Smørgrav error = copyin(args->nsa, &osa, sizeof(l_osigaction_t)); 475146aad74SMarcel Moolenaar if (error) 476146aad74SMarcel Moolenaar return (error); 477146aad74SMarcel Moolenaar act.lsa_handler = osa.lsa_handler; 478146aad74SMarcel Moolenaar act.lsa_flags = osa.lsa_flags; 479146aad74SMarcel Moolenaar act.lsa_restorer = osa.lsa_restorer; 480146aad74SMarcel Moolenaar LINUX_SIGEMPTYSET(act.lsa_mask); 4814ab7403bSDmitry Chagin act.lsa_mask.__mask = osa.lsa_mask; 482146aad74SMarcel Moolenaar } 483146aad74SMarcel Moolenaar 484b40ce416SJulian Elischer error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL, 485146aad74SMarcel Moolenaar args->osa ? &oact : NULL); 486146aad74SMarcel Moolenaar 487146aad74SMarcel Moolenaar if (args->osa != NULL && !error) { 488146aad74SMarcel Moolenaar osa.lsa_handler = oact.lsa_handler; 489146aad74SMarcel Moolenaar osa.lsa_flags = oact.lsa_flags; 490146aad74SMarcel Moolenaar osa.lsa_restorer = oact.lsa_restorer; 4914ab7403bSDmitry Chagin osa.lsa_mask = oact.lsa_mask.__mask; 4924b7ef73dSDag-Erling Smørgrav error = copyout(&osa, args->osa, sizeof(l_osigaction_t)); 493146aad74SMarcel Moolenaar } 494146aad74SMarcel Moolenaar 495146aad74SMarcel Moolenaar return (error); 496146aad74SMarcel Moolenaar } 497146aad74SMarcel Moolenaar 498146aad74SMarcel Moolenaar /* 499146aad74SMarcel Moolenaar * Linux has two extra args, restart and oldmask. We dont use these, 500146aad74SMarcel Moolenaar * but it seems that "restart" is actually a context pointer that 501146aad74SMarcel Moolenaar * enables the signal to happen with a different register set. 502146aad74SMarcel Moolenaar */ 503146aad74SMarcel Moolenaar int 504b40ce416SJulian Elischer linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args) 505146aad74SMarcel Moolenaar { 506206a5d3aSIan Dowse sigset_t sigmask; 5075002a60fSMarcel Moolenaar l_sigset_t mask; 508146aad74SMarcel Moolenaar 509146aad74SMarcel Moolenaar LINUX_SIGEMPTYSET(mask); 5104ab7403bSDmitry Chagin mask.__mask = args->mask; 511206a5d3aSIan Dowse linux_to_bsd_sigset(&mask, &sigmask); 512206a5d3aSIan Dowse return (kern_sigsuspend(td, sigmask)); 513146aad74SMarcel Moolenaar } 514146aad74SMarcel Moolenaar 515146aad74SMarcel Moolenaar int 516b40ce416SJulian Elischer linux_pause(struct thread *td, struct linux_pause_args *args) 517146aad74SMarcel Moolenaar { 518b40ce416SJulian Elischer struct proc *p = td->td_proc; 519206a5d3aSIan Dowse sigset_t sigmask; 520146aad74SMarcel Moolenaar 521fdfdfb78SJohn Baldwin PROC_LOCK(p); 5224093529dSJeff Roberson sigmask = td->td_sigmask; 523fdfdfb78SJohn Baldwin PROC_UNLOCK(p); 524206a5d3aSIan Dowse return (kern_sigsuspend(td, sigmask)); 525146aad74SMarcel Moolenaar } 526146aad74SMarcel Moolenaar 527146aad74SMarcel Moolenaar int 5281bc85c0dSDoug Rabson linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args) 5291bc85c0dSDoug Rabson { 5309b44bfc5SAlexander Leidinger struct l_user_desc info; 5319b44bfc5SAlexander Leidinger int error; 5329b44bfc5SAlexander Leidinger int idx; 5339b44bfc5SAlexander Leidinger int a[2]; 5349b44bfc5SAlexander Leidinger struct segment_descriptor sd; 5359b44bfc5SAlexander Leidinger 5369b44bfc5SAlexander Leidinger error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 5379b44bfc5SAlexander Leidinger if (error) 5389b44bfc5SAlexander Leidinger return (error); 5399b44bfc5SAlexander Leidinger 5409b44bfc5SAlexander Leidinger idx = info.entry_number; 5410eef2f8aSAlexander Leidinger /* 542eae594f7SEd Maste * Semantics of Linux version: every thread in the system has array of 543802e08a3SAlexander Leidinger * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This 544802e08a3SAlexander Leidinger * syscall loads one of the selected tls decriptors with a value and 545802e08a3SAlexander Leidinger * also loads GDT descriptors 6, 7 and 8 with the content of the 546802e08a3SAlexander Leidinger * per-thread descriptors. 5479b44bfc5SAlexander Leidinger * 548eae594f7SEd Maste * Semantics of FreeBSD version: I think we can ignore that Linux has 3 549802e08a3SAlexander Leidinger * per-thread descriptors and use just the 1st one. The tls_array[] 550802e08a3SAlexander Leidinger * is used only in set/get-thread_area() syscalls and for loading the 551eae594f7SEd Maste * GDT descriptors. In FreeBSD we use just one GDT descriptor for TLS 552eae594f7SEd Maste * so we will load just one. 553802e08a3SAlexander Leidinger * 554802e08a3SAlexander Leidinger * XXX: this doesn't work when a user space process tries to use more 555eae594f7SEd Maste * than 1 TLS segment. Comment in the Linux sources says wine might do 556802e08a3SAlexander Leidinger * this. 5571bc85c0dSDoug Rabson */ 5589b44bfc5SAlexander Leidinger 5590eef2f8aSAlexander Leidinger /* 5600eef2f8aSAlexander Leidinger * we support just GLIBC TLS now 5619b44bfc5SAlexander Leidinger * we should let 3 proceed as well because we use this segment so 5629b44bfc5SAlexander Leidinger * if code does two subsequent calls it should succeed 5639b44bfc5SAlexander Leidinger */ 5649b44bfc5SAlexander Leidinger if (idx != 6 && idx != -1 && idx != 3) 5659b44bfc5SAlexander Leidinger return (EINVAL); 5669b44bfc5SAlexander Leidinger 5670eef2f8aSAlexander Leidinger /* 5680eef2f8aSAlexander Leidinger * we have to copy out the GDT entry we use 5699b44bfc5SAlexander Leidinger * FreeBSD uses GDT entry #3 for storing %gs so load that 570802e08a3SAlexander Leidinger * 571802e08a3SAlexander Leidinger * XXX: what if a user space program doesn't check this value and tries 5729b44bfc5SAlexander Leidinger * to use 6, 7 or 8? 5739b44bfc5SAlexander Leidinger */ 5749b44bfc5SAlexander Leidinger idx = info.entry_number = 3; 5759b44bfc5SAlexander Leidinger error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 5769b44bfc5SAlexander Leidinger if (error) 5779b44bfc5SAlexander Leidinger return (error); 5789b44bfc5SAlexander Leidinger 579a4e3bad7SJung-uk Kim if (LINUX_LDT_empty(&info)) { 5809b44bfc5SAlexander Leidinger a[0] = 0; 5819b44bfc5SAlexander Leidinger a[1] = 0; 5829b44bfc5SAlexander Leidinger } else { 583a4e3bad7SJung-uk Kim a[0] = LINUX_LDT_entry_a(&info); 584a4e3bad7SJung-uk Kim a[1] = LINUX_LDT_entry_b(&info); 5851bc85c0dSDoug Rabson } 5861bc85c0dSDoug Rabson 5879b44bfc5SAlexander Leidinger memcpy(&sd, &a, sizeof(a)); 5889b44bfc5SAlexander Leidinger /* this is taken from i386 version of cpu_set_user_tls() */ 5899b44bfc5SAlexander Leidinger critical_enter(); 5909b44bfc5SAlexander Leidinger /* set %gs */ 5919b44bfc5SAlexander Leidinger td->td_pcb->pcb_gsd = sd; 5929b44bfc5SAlexander Leidinger PCPU_GET(fsgs_gdt)[1] = sd; 5939b44bfc5SAlexander Leidinger load_gs(GSEL(GUGS_SEL, SEL_UPL)); 5949b44bfc5SAlexander Leidinger critical_exit(); 5959b44bfc5SAlexander Leidinger 5961bc85c0dSDoug Rabson return (0); 5971bc85c0dSDoug Rabson } 5981bc85c0dSDoug Rabson 5991bc85c0dSDoug Rabson int 6009b44bfc5SAlexander Leidinger linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args) 6011bc85c0dSDoug Rabson { 6021bc85c0dSDoug Rabson 6039b44bfc5SAlexander Leidinger struct l_user_desc info; 6049b44bfc5SAlexander Leidinger int error; 6059b44bfc5SAlexander Leidinger int idx; 6069b44bfc5SAlexander Leidinger struct l_desc_struct desc; 6079b44bfc5SAlexander Leidinger struct segment_descriptor sd; 6089b44bfc5SAlexander Leidinger 6099b44bfc5SAlexander Leidinger error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 6109b44bfc5SAlexander Leidinger if (error) 6119b44bfc5SAlexander Leidinger return (error); 6129b44bfc5SAlexander Leidinger 6139b44bfc5SAlexander Leidinger idx = info.entry_number; 6149b44bfc5SAlexander Leidinger /* XXX: I am not sure if we want 3 to be allowed too. */ 6159b44bfc5SAlexander Leidinger if (idx != 6 && idx != 3) 6169b44bfc5SAlexander Leidinger return (EINVAL); 6179b44bfc5SAlexander Leidinger 6189b44bfc5SAlexander Leidinger idx = 3; 6199b44bfc5SAlexander Leidinger 6209b44bfc5SAlexander Leidinger memset(&info, 0, sizeof(info)); 6219b44bfc5SAlexander Leidinger 6229b44bfc5SAlexander Leidinger sd = PCPU_GET(fsgs_gdt)[1]; 6239b44bfc5SAlexander Leidinger 6249b44bfc5SAlexander Leidinger memcpy(&desc, &sd, sizeof(desc)); 6259b44bfc5SAlexander Leidinger 6269b44bfc5SAlexander Leidinger info.entry_number = idx; 627a4e3bad7SJung-uk Kim info.base_addr = LINUX_GET_BASE(&desc); 628a4e3bad7SJung-uk Kim info.limit = LINUX_GET_LIMIT(&desc); 629a4e3bad7SJung-uk Kim info.seg_32bit = LINUX_GET_32BIT(&desc); 630a4e3bad7SJung-uk Kim info.contents = LINUX_GET_CONTENTS(&desc); 631a4e3bad7SJung-uk Kim info.read_exec_only = !LINUX_GET_WRITABLE(&desc); 632a4e3bad7SJung-uk Kim info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc); 633a4e3bad7SJung-uk Kim info.seg_not_present = !LINUX_GET_PRESENT(&desc); 634a4e3bad7SJung-uk Kim info.useable = LINUX_GET_USEABLE(&desc); 6359b44bfc5SAlexander Leidinger 6369b44bfc5SAlexander Leidinger error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 6379b44bfc5SAlexander Leidinger if (error) 6389b44bfc5SAlexander Leidinger return (EFAULT); 6399b44bfc5SAlexander Leidinger 6409b44bfc5SAlexander Leidinger return (0); 6419b44bfc5SAlexander Leidinger } 6429b44bfc5SAlexander Leidinger 6439b44bfc5SAlexander Leidinger /* XXX: this wont work with module - convert it */ 6449b44bfc5SAlexander Leidinger int 6459b44bfc5SAlexander Leidinger linux_mq_open(struct thread *td, struct linux_mq_open_args *args) 6469b44bfc5SAlexander Leidinger { 6479b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 648340f4a8dSEd Maste return (sys_kmq_open(td, (struct kmq_open_args *)args)); 6499b44bfc5SAlexander Leidinger #else 6509b44bfc5SAlexander Leidinger return (ENOSYS); 6519b44bfc5SAlexander Leidinger #endif 6529b44bfc5SAlexander Leidinger } 6539b44bfc5SAlexander Leidinger 6549b44bfc5SAlexander Leidinger int 6559b44bfc5SAlexander Leidinger linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args) 6569b44bfc5SAlexander Leidinger { 6579b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 658340f4a8dSEd Maste return (sys_kmq_unlink(td, (struct kmq_unlink_args *)args)); 6599b44bfc5SAlexander Leidinger #else 6609b44bfc5SAlexander Leidinger return (ENOSYS); 6619b44bfc5SAlexander Leidinger #endif 6629b44bfc5SAlexander Leidinger } 6639b44bfc5SAlexander Leidinger 6649b44bfc5SAlexander Leidinger int 6659b44bfc5SAlexander Leidinger linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args) 6669b44bfc5SAlexander Leidinger { 6679b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 668340f4a8dSEd Maste return (sys_kmq_timedsend(td, (struct kmq_timedsend_args *)args)); 6699b44bfc5SAlexander Leidinger #else 6709b44bfc5SAlexander Leidinger return (ENOSYS); 6719b44bfc5SAlexander Leidinger #endif 6729b44bfc5SAlexander Leidinger } 6739b44bfc5SAlexander Leidinger 6749b44bfc5SAlexander Leidinger int 6759b44bfc5SAlexander Leidinger linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args) 6769b44bfc5SAlexander Leidinger { 6779b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 678340f4a8dSEd Maste return (sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *)args)); 6799b44bfc5SAlexander Leidinger #else 6809b44bfc5SAlexander Leidinger return (ENOSYS); 6819b44bfc5SAlexander Leidinger #endif 6829b44bfc5SAlexander Leidinger } 6839b44bfc5SAlexander Leidinger 6849b44bfc5SAlexander Leidinger int 6859b44bfc5SAlexander Leidinger linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args) 6869b44bfc5SAlexander Leidinger { 6879b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 688340f4a8dSEd Maste return (sys_kmq_notify(td, (struct kmq_notify_args *)args)); 6899b44bfc5SAlexander Leidinger #else 6909b44bfc5SAlexander Leidinger return (ENOSYS); 6919b44bfc5SAlexander Leidinger #endif 6929b44bfc5SAlexander Leidinger } 6939b44bfc5SAlexander Leidinger 6949b44bfc5SAlexander Leidinger int 6959b44bfc5SAlexander Leidinger linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args) 6969b44bfc5SAlexander Leidinger { 6979b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 698340f4a8dSEd Maste return (sys_kmq_setattr(td, (struct kmq_setattr_args *)args)); 6999b44bfc5SAlexander Leidinger #else 7009b44bfc5SAlexander Leidinger return (ENOSYS); 7019b44bfc5SAlexander Leidinger #endif 7021bc85c0dSDoug Rabson } 703