1146aad74SMarcel Moolenaar /*- 2146aad74SMarcel Moolenaar * Copyright (c) 2000 Marcel Moolenaar 3146aad74SMarcel Moolenaar * All rights reserved. 4146aad74SMarcel Moolenaar * 5146aad74SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 6146aad74SMarcel Moolenaar * modification, are permitted provided that the following conditions 7146aad74SMarcel Moolenaar * are met: 8146aad74SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 9146aad74SMarcel Moolenaar * notice, this list of conditions and the following disclaimer 10146aad74SMarcel Moolenaar * in this position and unchanged. 11146aad74SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 12146aad74SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 13146aad74SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 14146aad74SMarcel Moolenaar * 3. The name of the author may not be used to endorse or promote products 15bc34729cSMarcel Moolenaar * derived from this software without specific prior written permission. 16146aad74SMarcel Moolenaar * 17146aad74SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18146aad74SMarcel Moolenaar * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19146aad74SMarcel Moolenaar * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20146aad74SMarcel Moolenaar * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21146aad74SMarcel Moolenaar * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22146aad74SMarcel Moolenaar * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23146aad74SMarcel Moolenaar * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24146aad74SMarcel Moolenaar * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25146aad74SMarcel Moolenaar * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26146aad74SMarcel Moolenaar * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 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> 33146aad74SMarcel Moolenaar #include <sys/systm.h> 34a312f6a3SAlexander Leidinger #include <sys/file.h> 35a312f6a3SAlexander Leidinger #include <sys/fcntl.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> 419b44bfc5SAlexander Leidinger #include <sys/sx.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> 461bc85c0dSDoug Rabson #include <sys/signalvar.h> 47206a5d3aSIan Dowse #include <sys/syscallsubr.h> 48fb919e4dSMark Murray #include <sys/sysproto.h> 49fb919e4dSMark Murray #include <sys/unistd.h> 509b44bfc5SAlexander Leidinger #include <sys/wait.h> 51146aad74SMarcel Moolenaar 52146aad74SMarcel Moolenaar #include <machine/frame.h> 53146aad74SMarcel Moolenaar #include <machine/psl.h> 54146aad74SMarcel Moolenaar #include <machine/segments.h> 55146aad74SMarcel Moolenaar #include <machine/sysarch.h> 56146aad74SMarcel Moolenaar 57242fae60SAndrew Gallatin #include <vm/vm.h> 58242fae60SAndrew Gallatin #include <vm/pmap.h> 59242fae60SAndrew Gallatin #include <vm/vm_map.h> 60242fae60SAndrew Gallatin 61146aad74SMarcel Moolenaar #include <i386/linux/linux.h> 62ebea8660SMarcel Moolenaar #include <i386/linux/linux_proto.h> 63146aad74SMarcel Moolenaar #include <compat/linux/linux_ipc.h> 64146aad74SMarcel Moolenaar #include <compat/linux/linux_signal.h> 65146aad74SMarcel Moolenaar #include <compat/linux/linux_util.h> 669b44bfc5SAlexander Leidinger #include <compat/linux/linux_emul.h> 679b44bfc5SAlexander Leidinger 689b44bfc5SAlexander Leidinger #include <i386/include/pcb.h> /* needed for pcb definition in linux_set_thread_area */ 699b44bfc5SAlexander Leidinger 709b44bfc5SAlexander Leidinger #include "opt_posix.h" 719b44bfc5SAlexander Leidinger 729b44bfc5SAlexander Leidinger extern struct sysentvec elf32_freebsd_sysvec; /* defined in i386/i386/elf_machdep.c */ 73146aad74SMarcel Moolenaar 745002a60fSMarcel Moolenaar struct l_descriptor { 755002a60fSMarcel Moolenaar l_uint entry_number; 765002a60fSMarcel Moolenaar l_ulong base_addr; 775002a60fSMarcel Moolenaar l_uint limit; 785002a60fSMarcel Moolenaar l_uint seg_32bit:1; 795002a60fSMarcel Moolenaar l_uint contents:2; 805002a60fSMarcel Moolenaar l_uint read_exec_only:1; 815002a60fSMarcel Moolenaar l_uint limit_in_pages:1; 825002a60fSMarcel Moolenaar l_uint seg_not_present:1; 835002a60fSMarcel Moolenaar l_uint useable:1; 84146aad74SMarcel Moolenaar }; 85146aad74SMarcel Moolenaar 865002a60fSMarcel Moolenaar struct l_old_select_argv { 875002a60fSMarcel Moolenaar l_int nfds; 885002a60fSMarcel Moolenaar l_fd_set *readfds; 895002a60fSMarcel Moolenaar l_fd_set *writefds; 905002a60fSMarcel Moolenaar l_fd_set *exceptfds; 915002a60fSMarcel Moolenaar struct l_timeval *timeout; 92146aad74SMarcel Moolenaar }; 93146aad74SMarcel Moolenaar 94146aad74SMarcel Moolenaar int 95931a7258SAndrew Gallatin linux_to_bsd_sigaltstack(int lsa) 96931a7258SAndrew Gallatin { 97931a7258SAndrew Gallatin int bsa = 0; 98931a7258SAndrew Gallatin 99931a7258SAndrew Gallatin if (lsa & LINUX_SS_DISABLE) 100931a7258SAndrew Gallatin bsa |= SS_DISABLE; 101931a7258SAndrew Gallatin if (lsa & LINUX_SS_ONSTACK) 102931a7258SAndrew Gallatin bsa |= SS_ONSTACK; 103931a7258SAndrew Gallatin return (bsa); 104931a7258SAndrew Gallatin } 105931a7258SAndrew Gallatin 106931a7258SAndrew Gallatin int 107931a7258SAndrew Gallatin bsd_to_linux_sigaltstack(int bsa) 108931a7258SAndrew Gallatin { 109931a7258SAndrew Gallatin int lsa = 0; 110931a7258SAndrew Gallatin 111931a7258SAndrew Gallatin if (bsa & SS_DISABLE) 112931a7258SAndrew Gallatin lsa |= LINUX_SS_DISABLE; 113931a7258SAndrew Gallatin if (bsa & SS_ONSTACK) 114931a7258SAndrew Gallatin lsa |= LINUX_SS_ONSTACK; 115931a7258SAndrew Gallatin return (lsa); 116931a7258SAndrew Gallatin } 117931a7258SAndrew Gallatin 118931a7258SAndrew Gallatin int 119b40ce416SJulian Elischer linux_execve(struct thread *td, struct linux_execve_args *args) 120146aad74SMarcel Moolenaar { 121610ecfe0SMaxim Sobolev int error; 122610ecfe0SMaxim Sobolev char *newpath; 123610ecfe0SMaxim Sobolev struct image_args eargs; 124146aad74SMarcel Moolenaar 125d9e97471SJohn Baldwin LCONVPATHEXIST(td, args->path, &newpath); 126146aad74SMarcel Moolenaar 127146aad74SMarcel Moolenaar #ifdef DEBUG 12824593369SJonathan Lemon if (ldebug(execve)) 129610ecfe0SMaxim Sobolev printf(ARGS(execve, "%s"), newpath); 130146aad74SMarcel Moolenaar #endif 131146aad74SMarcel Moolenaar 132610ecfe0SMaxim Sobolev error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE, 133610ecfe0SMaxim Sobolev args->argp, args->envp); 134610ecfe0SMaxim Sobolev free(newpath, M_TEMP); 135610ecfe0SMaxim Sobolev if (error == 0) 136c035ac04SMaxim Sobolev error = kern_execve(td, &eargs, NULL); 1379b44bfc5SAlexander Leidinger if (error == 0) 1389b44bfc5SAlexander Leidinger /* linux process can exec fbsd one, dont attempt 1399b44bfc5SAlexander Leidinger * to create emuldata for such process using 1409b44bfc5SAlexander Leidinger * linux_proc_init, this leads to a panic on KASSERT 1419b44bfc5SAlexander Leidinger * because such process has p->p_emuldata == NULL 1429b44bfc5SAlexander Leidinger */ 1439b44bfc5SAlexander Leidinger if (td->td_proc->p_sysent == &elf_linux_sysvec) 1449b44bfc5SAlexander Leidinger error = linux_proc_init(td, 0, 0); 145610ecfe0SMaxim Sobolev return (error); 146146aad74SMarcel Moolenaar } 147146aad74SMarcel Moolenaar 1485002a60fSMarcel Moolenaar struct l_ipc_kludge { 1495002a60fSMarcel Moolenaar struct l_msgbuf *msgp; 1505002a60fSMarcel Moolenaar l_long msgtyp; 1515002a60fSMarcel Moolenaar }; 1525002a60fSMarcel Moolenaar 153146aad74SMarcel Moolenaar int 154b40ce416SJulian Elischer linux_ipc(struct thread *td, struct linux_ipc_args *args) 155146aad74SMarcel Moolenaar { 1565002a60fSMarcel Moolenaar 1575002a60fSMarcel Moolenaar switch (args->what & 0xFFFF) { 1585002a60fSMarcel Moolenaar case LINUX_SEMOP: { 1595002a60fSMarcel Moolenaar struct linux_semop_args a; 1605002a60fSMarcel Moolenaar 1615002a60fSMarcel Moolenaar a.semid = args->arg1; 1625002a60fSMarcel Moolenaar a.tsops = args->ptr; 1635002a60fSMarcel Moolenaar a.nsops = args->arg2; 164b40ce416SJulian Elischer return (linux_semop(td, &a)); 1655002a60fSMarcel Moolenaar } 1665002a60fSMarcel Moolenaar case LINUX_SEMGET: { 1675002a60fSMarcel Moolenaar struct linux_semget_args a; 1685002a60fSMarcel Moolenaar 1695002a60fSMarcel Moolenaar a.key = args->arg1; 1705002a60fSMarcel Moolenaar a.nsems = args->arg2; 1715002a60fSMarcel Moolenaar a.semflg = args->arg3; 172b40ce416SJulian Elischer return (linux_semget(td, &a)); 1735002a60fSMarcel Moolenaar } 1745002a60fSMarcel Moolenaar case LINUX_SEMCTL: { 1755002a60fSMarcel Moolenaar struct linux_semctl_args a; 1765002a60fSMarcel Moolenaar int error; 1775002a60fSMarcel Moolenaar 1785002a60fSMarcel Moolenaar a.semid = args->arg1; 1795002a60fSMarcel Moolenaar a.semnum = args->arg2; 1805002a60fSMarcel Moolenaar a.cmd = args->arg3; 1814b7ef73dSDag-Erling Smørgrav error = copyin(args->ptr, &a.arg, sizeof(a.arg)); 1825002a60fSMarcel Moolenaar if (error) 1835002a60fSMarcel Moolenaar return (error); 184b40ce416SJulian Elischer return (linux_semctl(td, &a)); 1855002a60fSMarcel Moolenaar } 1865002a60fSMarcel Moolenaar case LINUX_MSGSND: { 1875002a60fSMarcel Moolenaar struct linux_msgsnd_args a; 1885002a60fSMarcel Moolenaar 1895002a60fSMarcel Moolenaar a.msqid = args->arg1; 1905002a60fSMarcel Moolenaar a.msgp = args->ptr; 1915002a60fSMarcel Moolenaar a.msgsz = args->arg2; 1925002a60fSMarcel Moolenaar a.msgflg = args->arg3; 193b40ce416SJulian Elischer return (linux_msgsnd(td, &a)); 1945002a60fSMarcel Moolenaar } 1955002a60fSMarcel Moolenaar case LINUX_MSGRCV: { 1965002a60fSMarcel Moolenaar struct linux_msgrcv_args a; 1975002a60fSMarcel Moolenaar 1985002a60fSMarcel Moolenaar a.msqid = args->arg1; 1995002a60fSMarcel Moolenaar a.msgsz = args->arg2; 2005002a60fSMarcel Moolenaar a.msgflg = args->arg3; 2015002a60fSMarcel Moolenaar if ((args->what >> 16) == 0) { 2025002a60fSMarcel Moolenaar struct l_ipc_kludge tmp; 2035002a60fSMarcel Moolenaar int error; 2045002a60fSMarcel Moolenaar 2055002a60fSMarcel Moolenaar if (args->ptr == NULL) 2065002a60fSMarcel Moolenaar return (EINVAL); 2074b7ef73dSDag-Erling Smørgrav error = copyin(args->ptr, &tmp, sizeof(tmp)); 2085002a60fSMarcel Moolenaar if (error) 2095002a60fSMarcel Moolenaar return (error); 2105002a60fSMarcel Moolenaar a.msgp = tmp.msgp; 2115002a60fSMarcel Moolenaar a.msgtyp = tmp.msgtyp; 2125002a60fSMarcel Moolenaar } else { 2135002a60fSMarcel Moolenaar a.msgp = args->ptr; 2145002a60fSMarcel Moolenaar a.msgtyp = args->arg5; 2155002a60fSMarcel Moolenaar } 216b40ce416SJulian Elischer return (linux_msgrcv(td, &a)); 2175002a60fSMarcel Moolenaar } 2185002a60fSMarcel Moolenaar case LINUX_MSGGET: { 2195002a60fSMarcel Moolenaar struct linux_msgget_args a; 2205002a60fSMarcel Moolenaar 2215002a60fSMarcel Moolenaar a.key = args->arg1; 2225002a60fSMarcel Moolenaar a.msgflg = args->arg2; 223b40ce416SJulian Elischer return (linux_msgget(td, &a)); 2245002a60fSMarcel Moolenaar } 2255002a60fSMarcel Moolenaar case LINUX_MSGCTL: { 2265002a60fSMarcel Moolenaar struct linux_msgctl_args a; 2275002a60fSMarcel Moolenaar 2285002a60fSMarcel Moolenaar a.msqid = args->arg1; 2295002a60fSMarcel Moolenaar a.cmd = args->arg2; 2305002a60fSMarcel Moolenaar a.buf = args->ptr; 231b40ce416SJulian Elischer return (linux_msgctl(td, &a)); 2325002a60fSMarcel Moolenaar } 2335002a60fSMarcel Moolenaar case LINUX_SHMAT: { 2345002a60fSMarcel Moolenaar struct linux_shmat_args a; 2355002a60fSMarcel Moolenaar 2365002a60fSMarcel Moolenaar a.shmid = args->arg1; 2375002a60fSMarcel Moolenaar a.shmaddr = args->ptr; 2385002a60fSMarcel Moolenaar a.shmflg = args->arg2; 2395002a60fSMarcel Moolenaar a.raddr = (l_ulong *)args->arg3; 240b40ce416SJulian Elischer return (linux_shmat(td, &a)); 2415002a60fSMarcel Moolenaar } 2425002a60fSMarcel Moolenaar case LINUX_SHMDT: { 2435002a60fSMarcel Moolenaar struct linux_shmdt_args a; 2445002a60fSMarcel Moolenaar 2455002a60fSMarcel Moolenaar a.shmaddr = args->ptr; 246b40ce416SJulian Elischer return (linux_shmdt(td, &a)); 2475002a60fSMarcel Moolenaar } 2485002a60fSMarcel Moolenaar case LINUX_SHMGET: { 2495002a60fSMarcel Moolenaar struct linux_shmget_args a; 2505002a60fSMarcel Moolenaar 2515002a60fSMarcel Moolenaar a.key = args->arg1; 2525002a60fSMarcel Moolenaar a.size = args->arg2; 2535002a60fSMarcel Moolenaar a.shmflg = args->arg3; 254b40ce416SJulian Elischer return (linux_shmget(td, &a)); 2555002a60fSMarcel Moolenaar } 2565002a60fSMarcel Moolenaar case LINUX_SHMCTL: { 2575002a60fSMarcel Moolenaar struct linux_shmctl_args a; 2585002a60fSMarcel Moolenaar 2595002a60fSMarcel Moolenaar a.shmid = args->arg1; 2605002a60fSMarcel Moolenaar a.cmd = args->arg2; 2615002a60fSMarcel Moolenaar a.buf = args->ptr; 262b40ce416SJulian Elischer return (linux_shmctl(td, &a)); 2635002a60fSMarcel Moolenaar } 2645002a60fSMarcel Moolenaar default: 2655002a60fSMarcel Moolenaar break; 266146aad74SMarcel Moolenaar } 267146aad74SMarcel Moolenaar 2685002a60fSMarcel Moolenaar return (EINVAL); 269146aad74SMarcel Moolenaar } 270146aad74SMarcel Moolenaar 271146aad74SMarcel Moolenaar int 272b40ce416SJulian Elischer linux_old_select(struct thread *td, struct linux_old_select_args *args) 273146aad74SMarcel Moolenaar { 2745002a60fSMarcel Moolenaar struct l_old_select_argv linux_args; 2755002a60fSMarcel Moolenaar struct linux_select_args newsel; 276146aad74SMarcel Moolenaar int error; 277146aad74SMarcel Moolenaar 2785002a60fSMarcel Moolenaar #ifdef DEBUG 2795002a60fSMarcel Moolenaar if (ldebug(old_select)) 2806aea6777SPeter Wemm printf(ARGS(old_select, "%p"), args->ptr); 281146aad74SMarcel Moolenaar #endif 282146aad74SMarcel Moolenaar 2834b7ef73dSDag-Erling Smørgrav error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 284146aad74SMarcel Moolenaar if (error) 285146aad74SMarcel Moolenaar return (error); 286146aad74SMarcel Moolenaar 287146aad74SMarcel Moolenaar newsel.nfds = linux_args.nfds; 288146aad74SMarcel Moolenaar newsel.readfds = linux_args.readfds; 289146aad74SMarcel Moolenaar newsel.writefds = linux_args.writefds; 290146aad74SMarcel Moolenaar newsel.exceptfds = linux_args.exceptfds; 291146aad74SMarcel Moolenaar newsel.timeout = linux_args.timeout; 292b40ce416SJulian Elischer return (linux_select(td, &newsel)); 293146aad74SMarcel Moolenaar } 294146aad74SMarcel Moolenaar 295146aad74SMarcel Moolenaar int 296b40ce416SJulian Elischer linux_fork(struct thread *td, struct linux_fork_args *args) 297146aad74SMarcel Moolenaar { 298146aad74SMarcel Moolenaar int error; 299146aad74SMarcel Moolenaar 300146aad74SMarcel Moolenaar #ifdef DEBUG 30124593369SJonathan Lemon if (ldebug(fork)) 30224593369SJonathan Lemon printf(ARGS(fork, "")); 303146aad74SMarcel Moolenaar #endif 304146aad74SMarcel Moolenaar 305b40ce416SJulian Elischer if ((error = fork(td, (struct fork_args *)args)) != 0) 306146aad74SMarcel Moolenaar return (error); 307146aad74SMarcel Moolenaar 308b40ce416SJulian Elischer if (td->td_retval[1] == 1) 309b40ce416SJulian Elischer td->td_retval[0] = 0; 3109b44bfc5SAlexander Leidinger error = linux_proc_init(td, td->td_retval[0], 0); 3119b44bfc5SAlexander Leidinger if (error) 3129b44bfc5SAlexander Leidinger return (error); 3139b44bfc5SAlexander Leidinger 314146aad74SMarcel Moolenaar return (0); 315146aad74SMarcel Moolenaar } 316146aad74SMarcel Moolenaar 317146aad74SMarcel Moolenaar int 318b40ce416SJulian Elischer linux_vfork(struct thread *td, struct linux_vfork_args *args) 319146aad74SMarcel Moolenaar { 320146aad74SMarcel Moolenaar int error; 32140f734ddSAlexander Leidinger struct proc *p2; 322146aad74SMarcel Moolenaar 323146aad74SMarcel Moolenaar #ifdef DEBUG 32424593369SJonathan Lemon if (ldebug(vfork)) 32524593369SJonathan Lemon printf(ARGS(vfork, "")); 326146aad74SMarcel Moolenaar #endif 327146aad74SMarcel Moolenaar 32840f734ddSAlexander Leidinger /* exclude RFPPWAIT */ 32940f734ddSAlexander Leidinger if ((error = fork1(td, RFFDG | RFPROC | RFMEM, 0, &p2)) != 0) 330146aad74SMarcel Moolenaar return (error); 33140f734ddSAlexander Leidinger if (error == 0) { 33240f734ddSAlexander Leidinger td->td_retval[0] = p2->p_pid; 33340f734ddSAlexander Leidinger td->td_retval[1] = 0; 33440f734ddSAlexander Leidinger } 335146aad74SMarcel Moolenaar /* Are we the child? */ 336b40ce416SJulian Elischer if (td->td_retval[1] == 1) 337b40ce416SJulian Elischer td->td_retval[0] = 0; 3389b44bfc5SAlexander Leidinger error = linux_proc_init(td, td->td_retval[0], 0); 3399b44bfc5SAlexander Leidinger if (error) 3409b44bfc5SAlexander Leidinger return (error); 34140f734ddSAlexander Leidinger /* wait for the children to exit, ie. emulate vfork */ 34240f734ddSAlexander Leidinger PROC_LOCK(p2); 34340f734ddSAlexander Leidinger while (p2->p_flag & P_PPWAIT) 34440f734ddSAlexander Leidinger msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0); 34540f734ddSAlexander Leidinger PROC_UNLOCK(p2); 34640f734ddSAlexander Leidinger 347146aad74SMarcel Moolenaar return (0); 348146aad74SMarcel Moolenaar } 349146aad74SMarcel Moolenaar 350146aad74SMarcel Moolenaar int 351b40ce416SJulian Elischer linux_clone(struct thread *td, struct linux_clone_args *args) 352146aad74SMarcel Moolenaar { 3536ad0e7c5SJohn Baldwin int error, ff = RFPROC | RFSTOPPED; 354146aad74SMarcel Moolenaar struct proc *p2; 3559eb78fcfSJohn Baldwin struct thread *td2; 356146aad74SMarcel Moolenaar int exit_signal; 3579b44bfc5SAlexander Leidinger struct linux_emuldata *em; 358146aad74SMarcel Moolenaar 359146aad74SMarcel Moolenaar #ifdef DEBUG 36024593369SJonathan Lemon if (ldebug(clone)) { 3619b44bfc5SAlexander Leidinger printf(ARGS(clone, "flags %x, stack %x, parent tid: %x, child tid: %x"), 3629b44bfc5SAlexander Leidinger (unsigned int)args->flags, (unsigned int)args->stack, 3639b44bfc5SAlexander Leidinger (unsigned int)args->parent_tidptr, (unsigned int)args->child_tidptr); 36424593369SJonathan Lemon } 365146aad74SMarcel Moolenaar #endif 366146aad74SMarcel Moolenaar 367146aad74SMarcel Moolenaar exit_signal = args->flags & 0x000000ff; 368146aad74SMarcel Moolenaar if (exit_signal >= LINUX_NSIG) 369146aad74SMarcel Moolenaar return (EINVAL); 370146aad74SMarcel Moolenaar 371146aad74SMarcel Moolenaar if (exit_signal <= LINUX_SIGTBLSZ) 372146aad74SMarcel Moolenaar exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)]; 373146aad74SMarcel Moolenaar 374146aad74SMarcel Moolenaar if (args->flags & CLONE_VM) 375146aad74SMarcel Moolenaar ff |= RFMEM; 376146aad74SMarcel Moolenaar if (args->flags & CLONE_SIGHAND) 377146aad74SMarcel Moolenaar ff |= RFSIGSHARE; 378146aad74SMarcel Moolenaar if (!(args->flags & CLONE_FILES)) 379146aad74SMarcel Moolenaar ff |= RFFDG; 380146aad74SMarcel Moolenaar 3814b178336SMaxim Sobolev /* 3824b178336SMaxim Sobolev * Attempt to detect when linux_clone(2) is used for creating 3834b178336SMaxim Sobolev * kernel threads. Unfortunately despite the existence of the 3844b178336SMaxim Sobolev * CLONE_THREAD flag, version of linuxthreads package used in 3854b178336SMaxim Sobolev * most popular distros as of beginning of 2005 doesn't make 3864b178336SMaxim Sobolev * any use of it. Therefore, this detection relay fully on 3874b178336SMaxim Sobolev * empirical observation that linuxthreads sets certain 3884b178336SMaxim Sobolev * combination of flags, so that we can make more or less 3894b178336SMaxim Sobolev * precise detection and notify the FreeBSD kernel that several 3904b178336SMaxim Sobolev * processes are in fact part of the same threading group, so 3914b178336SMaxim Sobolev * that special treatment is necessary for signal delivery 3924b178336SMaxim Sobolev * between those processes and fd locking. 3934b178336SMaxim Sobolev */ 3944b178336SMaxim Sobolev if ((args->flags & 0xffffff00) == THREADING_FLAGS) 3954b178336SMaxim Sobolev ff |= RFTHREAD; 3964b178336SMaxim Sobolev 397316ec49aSScott Long error = fork1(td, ff, 0, &p2); 3989eb78fcfSJohn Baldwin if (error) 3999eb78fcfSJohn Baldwin return (error); 4009eb78fcfSJohn Baldwin 4019b44bfc5SAlexander Leidinger /* create the emuldata */ 4029b44bfc5SAlexander Leidinger error = linux_proc_init(td, p2->p_pid, args->flags); 4039b44bfc5SAlexander Leidinger /* reference it - no need to check this */ 4049b44bfc5SAlexander Leidinger em = em_find(p2, EMUL_UNLOCKED); 4059b44bfc5SAlexander Leidinger KASSERT(em != NULL, ("clone: emuldata not found.\n")); 4069b44bfc5SAlexander Leidinger /* and adjust it */ 4079b44bfc5SAlexander Leidinger if (args->flags & CLONE_PARENT_SETTID) { 4089b44bfc5SAlexander Leidinger if (args->parent_tidptr == NULL) { 4099b44bfc5SAlexander Leidinger EMUL_UNLOCK(&emul_lock); 4109b44bfc5SAlexander Leidinger return (EINVAL); 4119b44bfc5SAlexander Leidinger } 4129b44bfc5SAlexander Leidinger error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid)); 4139b44bfc5SAlexander Leidinger if (error) { 4149b44bfc5SAlexander Leidinger EMUL_UNLOCK(&emul_lock); 4159b44bfc5SAlexander Leidinger return (error); 4169b44bfc5SAlexander Leidinger } 4179b44bfc5SAlexander Leidinger } 4189b44bfc5SAlexander Leidinger 419a6c5f813SAlexander Leidinger if (args->flags & (CLONE_PARENT|CLONE_THREAD)) { 420a6c5f813SAlexander Leidinger sx_xlock(&proctree_lock); 421a6c5f813SAlexander Leidinger PROC_LOCK(p2); 422a6c5f813SAlexander Leidinger proc_reparent(p2, td->td_proc->p_pptr); 423a6c5f813SAlexander Leidinger PROC_UNLOCK(p2); 424a6c5f813SAlexander Leidinger sx_xunlock(&proctree_lock); 4259b44bfc5SAlexander Leidinger } 4269b44bfc5SAlexander Leidinger 4279b44bfc5SAlexander Leidinger if (args->flags & CLONE_THREAD) { 4289b44bfc5SAlexander Leidinger /* XXX: linux mangles pgrp and pptr somehow 4299b44bfc5SAlexander Leidinger * I think it might be this but I am not sure. 4309b44bfc5SAlexander Leidinger */ 4319b44bfc5SAlexander Leidinger #ifdef notyet 432a6c5f813SAlexander Leidinger PROC_LOCK(p2); 4339b44bfc5SAlexander Leidinger p2->p_pgrp = td->td_proc->p_pgrp; 434a6c5f813SAlexander Leidinger PROC_UNLOCK(p2); 4359b44bfc5SAlexander Leidinger #endif 4369b44bfc5SAlexander Leidinger exit_signal = 0; 4379b44bfc5SAlexander Leidinger } 4389b44bfc5SAlexander Leidinger 4399b44bfc5SAlexander Leidinger if (args->flags & CLONE_CHILD_SETTID) 4409b44bfc5SAlexander Leidinger em->child_set_tid = args->child_tidptr; 4419b44bfc5SAlexander Leidinger else 4429b44bfc5SAlexander Leidinger em->child_set_tid = NULL; 4439b44bfc5SAlexander Leidinger 4449b44bfc5SAlexander Leidinger if (args->flags & CLONE_CHILD_CLEARTID) 4459b44bfc5SAlexander Leidinger em->child_clear_tid = args->child_tidptr; 4469b44bfc5SAlexander Leidinger else 4479b44bfc5SAlexander Leidinger em->child_clear_tid = NULL; 448a6c5f813SAlexander Leidinger 4499b44bfc5SAlexander Leidinger EMUL_UNLOCK(&emul_lock); 450146aad74SMarcel Moolenaar 451fdfdfb78SJohn Baldwin PROC_LOCK(p2); 452146aad74SMarcel Moolenaar p2->p_sigparent = exit_signal; 4539eb78fcfSJohn Baldwin PROC_UNLOCK(p2); 4549eb78fcfSJohn Baldwin td2 = FIRST_THREAD_IN_PROC(p2); 4550eef2f8aSAlexander Leidinger /* 4560eef2f8aSAlexander Leidinger * in a case of stack = NULL we are supposed to COW calling process stack 4579b44bfc5SAlexander Leidinger * this is what normal fork() does so we just keep the tf_esp arg intact 4589b44bfc5SAlexander Leidinger */ 4599b44bfc5SAlexander Leidinger if (args->stack) 4609eb78fcfSJohn Baldwin td2->td_frame->tf_esp = (unsigned int)args->stack; 461146aad74SMarcel Moolenaar 4629b44bfc5SAlexander Leidinger if (args->flags & CLONE_SETTLS) { 4639b44bfc5SAlexander Leidinger struct l_user_desc info; 4649b44bfc5SAlexander Leidinger int idx; 4659b44bfc5SAlexander Leidinger int a[2]; 4669b44bfc5SAlexander Leidinger struct segment_descriptor sd; 4679b44bfc5SAlexander Leidinger 4689b44bfc5SAlexander Leidinger error = copyin((void *)td->td_frame->tf_esi, &info, sizeof(struct l_user_desc)); 4699b44bfc5SAlexander Leidinger if (error) 4709b44bfc5SAlexander Leidinger return (error); 4719b44bfc5SAlexander Leidinger 4729b44bfc5SAlexander Leidinger idx = info.entry_number; 4739b44bfc5SAlexander Leidinger 4740eef2f8aSAlexander Leidinger /* 4750eef2f8aSAlexander Leidinger * looks like we're getting the idx we returned 4769b44bfc5SAlexander Leidinger * in the set_thread_area() syscall 4779b44bfc5SAlexander Leidinger */ 4789b44bfc5SAlexander Leidinger if (idx != 6 && idx != 3) 4799b44bfc5SAlexander Leidinger return (EINVAL); 4809b44bfc5SAlexander Leidinger 4819b44bfc5SAlexander Leidinger /* this doesnt happen in practice */ 4829b44bfc5SAlexander Leidinger if (idx == 6) { 4839b44bfc5SAlexander Leidinger /* we might copy out the entry_number as 3 */ 4849b44bfc5SAlexander Leidinger info.entry_number = 3; 4859b44bfc5SAlexander Leidinger error = copyout(&info, (void *) td->td_frame->tf_esi, sizeof(struct l_user_desc)); 4869b44bfc5SAlexander Leidinger if (error) 4879b44bfc5SAlexander Leidinger return (error); 4889b44bfc5SAlexander Leidinger } 4899b44bfc5SAlexander Leidinger 4909b44bfc5SAlexander Leidinger a[0] = LDT_entry_a(&info); 4919b44bfc5SAlexander Leidinger a[1] = LDT_entry_b(&info); 4929b44bfc5SAlexander Leidinger 4939b44bfc5SAlexander Leidinger memcpy(&sd, &a, sizeof(a)); 4949b44bfc5SAlexander Leidinger #ifdef DEBUG 4959b44bfc5SAlexander Leidinger if (ldebug(clone)) 4969b44bfc5SAlexander Leidinger printf("Segment created in clone with CLONE_SETTLS: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase, 4979b44bfc5SAlexander Leidinger sd.sd_hibase, 4989b44bfc5SAlexander Leidinger sd.sd_lolimit, 4999b44bfc5SAlexander Leidinger sd.sd_hilimit, 5009b44bfc5SAlexander Leidinger sd.sd_type, 5019b44bfc5SAlexander Leidinger sd.sd_dpl, 5029b44bfc5SAlexander Leidinger sd.sd_p, 5039b44bfc5SAlexander Leidinger sd.sd_xx, 5049b44bfc5SAlexander Leidinger sd.sd_def32, 5059b44bfc5SAlexander Leidinger sd.sd_gran); 5069b44bfc5SAlexander Leidinger #endif 5079b44bfc5SAlexander Leidinger 5089b44bfc5SAlexander Leidinger /* set %gs */ 5099b44bfc5SAlexander Leidinger td2->td_pcb->pcb_gsd = sd; 510a6c5f813SAlexander Leidinger td2->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL); 5119b44bfc5SAlexander Leidinger } 5129b44bfc5SAlexander Leidinger 513146aad74SMarcel Moolenaar #ifdef DEBUG 51424593369SJonathan Lemon if (ldebug(clone)) 5159eb78fcfSJohn Baldwin printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"), 5169eb78fcfSJohn Baldwin (long)p2->p_pid, args->stack, exit_signal); 517146aad74SMarcel Moolenaar #endif 518146aad74SMarcel Moolenaar 5196ad0e7c5SJohn Baldwin /* 5206ad0e7c5SJohn Baldwin * Make this runnable after we are finished with it. 5216ad0e7c5SJohn Baldwin */ 5226ad0e7c5SJohn Baldwin mtx_lock_spin(&sched_lock); 5239eb78fcfSJohn Baldwin TD_SET_CAN_RUN(td2); 5242630e4c9SJulian Elischer setrunqueue(td2, SRQ_BORING); 5256ad0e7c5SJohn Baldwin mtx_unlock_spin(&sched_lock); 5266ad0e7c5SJohn Baldwin 5279eb78fcfSJohn Baldwin td->td_retval[0] = p2->p_pid; 5289eb78fcfSJohn Baldwin td->td_retval[1] = 0; 5299eb78fcfSJohn Baldwin return (0); 530146aad74SMarcel Moolenaar } 531146aad74SMarcel Moolenaar 532146aad74SMarcel Moolenaar /* XXX move */ 5335002a60fSMarcel Moolenaar struct l_mmap_argv { 5345002a60fSMarcel Moolenaar l_caddr_t addr; 5355002a60fSMarcel Moolenaar l_int len; 5365002a60fSMarcel Moolenaar l_int prot; 5375002a60fSMarcel Moolenaar l_int flags; 5385002a60fSMarcel Moolenaar l_int fd; 5395002a60fSMarcel Moolenaar l_int pos; 540146aad74SMarcel Moolenaar }; 541146aad74SMarcel Moolenaar 542146aad74SMarcel Moolenaar #define STACK_SIZE (2 * 1024 * 1024) 543146aad74SMarcel Moolenaar #define GUARD_SIZE (4 * PAGE_SIZE) 544146aad74SMarcel Moolenaar 5453ad9c842SMaxim Sobolev static int linux_mmap_common(struct thread *, struct l_mmap_argv *); 5463ad9c842SMaxim Sobolev 5473ad9c842SMaxim Sobolev int 5483ad9c842SMaxim Sobolev linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 5493ad9c842SMaxim Sobolev { 5503ad9c842SMaxim Sobolev struct l_mmap_argv linux_args; 5513ad9c842SMaxim Sobolev 5523ad9c842SMaxim Sobolev #ifdef DEBUG 5533ad9c842SMaxim Sobolev if (ldebug(mmap2)) 5543ad9c842SMaxim Sobolev printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"), 5553ad9c842SMaxim Sobolev (void *)args->addr, args->len, args->prot, 5563ad9c842SMaxim Sobolev args->flags, args->fd, args->pgoff); 5573ad9c842SMaxim Sobolev #endif 5583ad9c842SMaxim Sobolev 5593ad9c842SMaxim Sobolev linux_args.addr = (l_caddr_t)args->addr; 5603ad9c842SMaxim Sobolev linux_args.len = args->len; 5613ad9c842SMaxim Sobolev linux_args.prot = args->prot; 5623ad9c842SMaxim Sobolev linux_args.flags = args->flags; 5633ad9c842SMaxim Sobolev linux_args.fd = args->fd; 5643ad9c842SMaxim Sobolev linux_args.pos = args->pgoff * PAGE_SIZE; 5653ad9c842SMaxim Sobolev 5663ad9c842SMaxim Sobolev return (linux_mmap_common(td, &linux_args)); 5673ad9c842SMaxim Sobolev } 5683ad9c842SMaxim Sobolev 569146aad74SMarcel Moolenaar int 570b40ce416SJulian Elischer linux_mmap(struct thread *td, struct linux_mmap_args *args) 571146aad74SMarcel Moolenaar { 5723ad9c842SMaxim Sobolev int error; 5733ad9c842SMaxim Sobolev struct l_mmap_argv linux_args; 5743ad9c842SMaxim Sobolev 5754b7ef73dSDag-Erling Smørgrav error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 5763ad9c842SMaxim Sobolev if (error) 5773ad9c842SMaxim Sobolev return (error); 5783ad9c842SMaxim Sobolev 5793ad9c842SMaxim Sobolev #ifdef DEBUG 5803ad9c842SMaxim Sobolev if (ldebug(mmap)) 5813ad9c842SMaxim Sobolev printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"), 582b45bbfc3SBruce Evans (void *)linux_args.addr, linux_args.len, linux_args.prot, 583b45bbfc3SBruce Evans linux_args.flags, linux_args.fd, linux_args.pos); 5843ad9c842SMaxim Sobolev #endif 5853ad9c842SMaxim Sobolev 5863ad9c842SMaxim Sobolev return (linux_mmap_common(td, &linux_args)); 5873ad9c842SMaxim Sobolev } 5883ad9c842SMaxim Sobolev 5893ad9c842SMaxim Sobolev static int 5903ad9c842SMaxim Sobolev linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args) 5913ad9c842SMaxim Sobolev { 592b40ce416SJulian Elischer struct proc *p = td->td_proc; 593146aad74SMarcel Moolenaar struct mmap_args /* { 594146aad74SMarcel Moolenaar caddr_t addr; 595146aad74SMarcel Moolenaar size_t len; 596146aad74SMarcel Moolenaar int prot; 597146aad74SMarcel Moolenaar int flags; 598146aad74SMarcel Moolenaar int fd; 599146aad74SMarcel Moolenaar long pad; 600146aad74SMarcel Moolenaar off_t pos; 601146aad74SMarcel Moolenaar } */ bsd_args; 60291d631e5SMatthew N. Dodd int error; 603a312f6a3SAlexander Leidinger struct file *fp; 604146aad74SMarcel Moolenaar 60591d631e5SMatthew N. Dodd error = 0; 606146aad74SMarcel Moolenaar bsd_args.flags = 0; 607a312f6a3SAlexander Leidinger fp = NULL; 608a312f6a3SAlexander Leidinger 609a312f6a3SAlexander Leidinger /* 610a312f6a3SAlexander Leidinger * Linux mmap(2): 611a312f6a3SAlexander Leidinger * You must specify exactly one of MAP_SHARED and MAP_PRIVATE 612a312f6a3SAlexander Leidinger */ 613a312f6a3SAlexander Leidinger if (! ((linux_args->flags & LINUX_MAP_SHARED) ^ 614a312f6a3SAlexander Leidinger (linux_args->flags & LINUX_MAP_PRIVATE))) 615a312f6a3SAlexander Leidinger return EINVAL; 616a312f6a3SAlexander Leidinger 6173ad9c842SMaxim Sobolev if (linux_args->flags & LINUX_MAP_SHARED) 618146aad74SMarcel Moolenaar bsd_args.flags |= MAP_SHARED; 6193ad9c842SMaxim Sobolev if (linux_args->flags & LINUX_MAP_PRIVATE) 620146aad74SMarcel Moolenaar bsd_args.flags |= MAP_PRIVATE; 6213ad9c842SMaxim Sobolev if (linux_args->flags & LINUX_MAP_FIXED) 622146aad74SMarcel Moolenaar bsd_args.flags |= MAP_FIXED; 6233ad9c842SMaxim Sobolev if (linux_args->flags & LINUX_MAP_ANON) 624146aad74SMarcel Moolenaar bsd_args.flags |= MAP_ANON; 6250cc3ac8bSMatthew Dillon else 6260cc3ac8bSMatthew Dillon bsd_args.flags |= MAP_NOSYNC; 6273ad9c842SMaxim Sobolev if (linux_args->flags & LINUX_MAP_GROWSDOWN) { 628146aad74SMarcel Moolenaar bsd_args.flags |= MAP_STACK; 629146aad74SMarcel Moolenaar 6300eef2f8aSAlexander Leidinger /* 6310eef2f8aSAlexander Leidinger * The linux MAP_GROWSDOWN option does not limit auto 632146aad74SMarcel Moolenaar * growth of the region. Linux mmap with this option 633146aad74SMarcel Moolenaar * takes as addr the inital BOS, and as len, the initial 634146aad74SMarcel Moolenaar * region size. It can then grow down from addr without 635146aad74SMarcel Moolenaar * limit. However, linux threads has an implicit internal 636146aad74SMarcel Moolenaar * limit to stack size of STACK_SIZE. Its just not 637146aad74SMarcel Moolenaar * enforced explicitly in linux. But, here we impose 638146aad74SMarcel Moolenaar * a limit of (STACK_SIZE - GUARD_SIZE) on the stack 639146aad74SMarcel Moolenaar * region, since we can do this with our mmap. 640146aad74SMarcel Moolenaar * 641146aad74SMarcel Moolenaar * Our mmap with MAP_STACK takes addr as the maximum 642146aad74SMarcel Moolenaar * downsize limit on BOS, and as len the max size of 643146aad74SMarcel Moolenaar * the region. It them maps the top SGROWSIZ bytes, 644146aad74SMarcel Moolenaar * and autgrows the region down, up to the limit 645146aad74SMarcel Moolenaar * in addr. 646146aad74SMarcel Moolenaar * 647146aad74SMarcel Moolenaar * If we don't use the MAP_STACK option, the effect 648146aad74SMarcel Moolenaar * of this code is to allocate a stack region of a 649146aad74SMarcel Moolenaar * fixed size of (STACK_SIZE - GUARD_SIZE). 650146aad74SMarcel Moolenaar */ 651146aad74SMarcel Moolenaar 652146aad74SMarcel Moolenaar /* This gives us TOS */ 6533ad9c842SMaxim Sobolev bsd_args.addr = linux_args->addr + linux_args->len; 654146aad74SMarcel Moolenaar 655242fae60SAndrew Gallatin if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) { 6560eef2f8aSAlexander Leidinger /* 6570eef2f8aSAlexander Leidinger * Some linux apps will attempt to mmap 658242fae60SAndrew Gallatin * thread stacks near the top of their 659242fae60SAndrew Gallatin * address space. If their TOS is greater 660242fae60SAndrew Gallatin * than vm_maxsaddr, vm_map_growstack() 661242fae60SAndrew Gallatin * will confuse the thread stack with the 662242fae60SAndrew Gallatin * process stack and deliver a SEGV if they 663242fae60SAndrew Gallatin * attempt to grow the thread stack past their 664242fae60SAndrew Gallatin * current stacksize rlimit. To avoid this, 665242fae60SAndrew Gallatin * adjust vm_maxsaddr upwards to reflect 666242fae60SAndrew Gallatin * the current stacksize rlimit rather 667242fae60SAndrew Gallatin * than the maximum possible stacksize. 668242fae60SAndrew Gallatin * It would be better to adjust the 669242fae60SAndrew Gallatin * mmap'ed region, but some apps do not check 670242fae60SAndrew Gallatin * mmap's return value. 671242fae60SAndrew Gallatin */ 67291d5354aSJohn Baldwin PROC_LOCK(p); 673242fae60SAndrew Gallatin p->p_vmspace->vm_maxsaddr = (char *)USRSTACK - 67491d5354aSJohn Baldwin lim_cur(p, RLIMIT_STACK); 67591d5354aSJohn Baldwin PROC_UNLOCK(p); 676242fae60SAndrew Gallatin } 677242fae60SAndrew Gallatin 678146aad74SMarcel Moolenaar /* This gives us our maximum stack size */ 6793ad9c842SMaxim Sobolev if (linux_args->len > STACK_SIZE - GUARD_SIZE) 6803ad9c842SMaxim Sobolev bsd_args.len = linux_args->len; 681146aad74SMarcel Moolenaar else 682146aad74SMarcel Moolenaar bsd_args.len = STACK_SIZE - GUARD_SIZE; 683146aad74SMarcel Moolenaar 6840eef2f8aSAlexander Leidinger /* 6850eef2f8aSAlexander Leidinger * This gives us a new BOS. If we're using VM_STACK, then 686146aad74SMarcel Moolenaar * mmap will just map the top SGROWSIZ bytes, and let 687146aad74SMarcel Moolenaar * the stack grow down to the limit at BOS. If we're 688146aad74SMarcel Moolenaar * not using VM_STACK we map the full stack, since we 689146aad74SMarcel Moolenaar * don't have a way to autogrow it. 690146aad74SMarcel Moolenaar */ 691146aad74SMarcel Moolenaar bsd_args.addr -= bsd_args.len; 692146aad74SMarcel Moolenaar } else { 6933ad9c842SMaxim Sobolev bsd_args.addr = linux_args->addr; 6943ad9c842SMaxim Sobolev bsd_args.len = linux_args->len; 695146aad74SMarcel Moolenaar } 696146aad74SMarcel Moolenaar 697a312f6a3SAlexander Leidinger bsd_args.prot = linux_args->prot; 6983ad9c842SMaxim Sobolev if (linux_args->flags & LINUX_MAP_ANON) 699146aad74SMarcel Moolenaar bsd_args.fd = -1; 700a312f6a3SAlexander Leidinger else { 701a312f6a3SAlexander Leidinger /* 702a312f6a3SAlexander Leidinger * Linux follows Solaris mmap(2) description: 703a312f6a3SAlexander Leidinger * The file descriptor fildes is opened with 704a312f6a3SAlexander Leidinger * read permission, regardless of the 705a312f6a3SAlexander Leidinger * protection options specified. 706a312f6a3SAlexander Leidinger * If PROT_WRITE is specified, the application 707a312f6a3SAlexander Leidinger * must have opened the file descriptor 708a312f6a3SAlexander Leidinger * fildes with write permission unless 709a312f6a3SAlexander Leidinger * MAP_PRIVATE is specified in the flag 710a312f6a3SAlexander Leidinger * argument as described below. 711a312f6a3SAlexander Leidinger */ 712a312f6a3SAlexander Leidinger 713a312f6a3SAlexander Leidinger if ((error = fget(td, linux_args->fd, &fp)) != 0) 714a312f6a3SAlexander Leidinger return error; 715a312f6a3SAlexander Leidinger if (fp->f_type != DTYPE_VNODE) { 716a312f6a3SAlexander Leidinger fdrop(fp, td); 717a312f6a3SAlexander Leidinger return EINVAL; 718a312f6a3SAlexander Leidinger } 719a312f6a3SAlexander Leidinger 720a312f6a3SAlexander Leidinger /* Linux mmap() just fails for O_WRONLY files */ 721a312f6a3SAlexander Leidinger if (! (fp->f_flag & FREAD)) { 722a312f6a3SAlexander Leidinger fdrop(fp, td); 723a312f6a3SAlexander Leidinger return EACCES; 724a312f6a3SAlexander Leidinger } 725a312f6a3SAlexander Leidinger 7263ad9c842SMaxim Sobolev bsd_args.fd = linux_args->fd; 727a312f6a3SAlexander Leidinger fdrop(fp, td); 728a312f6a3SAlexander Leidinger } 7293ad9c842SMaxim Sobolev bsd_args.pos = linux_args->pos; 730146aad74SMarcel Moolenaar bsd_args.pad = 0; 731146aad74SMarcel Moolenaar 732146aad74SMarcel Moolenaar #ifdef DEBUG 73324593369SJonathan Lemon if (ldebug(mmap)) 73491d631e5SMatthew N. Dodd printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n", 73591d631e5SMatthew N. Dodd __func__, 73624593369SJonathan Lemon (void *)bsd_args.addr, bsd_args.len, bsd_args.prot, 73724593369SJonathan Lemon bsd_args.flags, bsd_args.fd, (int)bsd_args.pos); 738146aad74SMarcel Moolenaar #endif 73991d631e5SMatthew N. Dodd error = mmap(td, &bsd_args); 74091d631e5SMatthew N. Dodd #ifdef DEBUG 74191d631e5SMatthew N. Dodd if (ldebug(mmap)) 74291d631e5SMatthew N. Dodd printf("-> %s() return: 0x%x (0x%08x)\n", 74391d631e5SMatthew N. Dodd __func__, error, (u_int)td->td_retval[0]); 74491d631e5SMatthew N. Dodd #endif 74591d631e5SMatthew N. Dodd return (error); 746146aad74SMarcel Moolenaar } 747146aad74SMarcel Moolenaar 748146aad74SMarcel Moolenaar int 749b40ce416SJulian Elischer linux_pipe(struct thread *td, struct linux_pipe_args *args) 750146aad74SMarcel Moolenaar { 751146aad74SMarcel Moolenaar int error; 752146aad74SMarcel Moolenaar int reg_edx; 753146aad74SMarcel Moolenaar 754146aad74SMarcel Moolenaar #ifdef DEBUG 75524593369SJonathan Lemon if (ldebug(pipe)) 75624593369SJonathan Lemon printf(ARGS(pipe, "*")); 757146aad74SMarcel Moolenaar #endif 758146aad74SMarcel Moolenaar 759b40ce416SJulian Elischer reg_edx = td->td_retval[1]; 760b40ce416SJulian Elischer error = pipe(td, 0); 761146aad74SMarcel Moolenaar if (error) { 762b40ce416SJulian Elischer td->td_retval[1] = reg_edx; 763146aad74SMarcel Moolenaar return (error); 764146aad74SMarcel Moolenaar } 765146aad74SMarcel Moolenaar 766b40ce416SJulian Elischer error = copyout(td->td_retval, args->pipefds, 2*sizeof(int)); 767146aad74SMarcel Moolenaar if (error) { 768b40ce416SJulian Elischer td->td_retval[1] = reg_edx; 769146aad74SMarcel Moolenaar return (error); 770146aad74SMarcel Moolenaar } 771146aad74SMarcel Moolenaar 772b40ce416SJulian Elischer td->td_retval[1] = reg_edx; 773b40ce416SJulian Elischer td->td_retval[0] = 0; 774146aad74SMarcel Moolenaar return (0); 775146aad74SMarcel Moolenaar } 776146aad74SMarcel Moolenaar 777146aad74SMarcel Moolenaar int 778b40ce416SJulian Elischer linux_ioperm(struct thread *td, struct linux_ioperm_args *args) 779146aad74SMarcel Moolenaar { 78084569dffSMaxim Sobolev int error; 78184569dffSMaxim Sobolev struct i386_ioperm_args iia; 782146aad74SMarcel Moolenaar 78384569dffSMaxim Sobolev iia.start = args->start; 78484569dffSMaxim Sobolev iia.length = args->length; 78584569dffSMaxim Sobolev iia.enable = args->enable; 78684569dffSMaxim Sobolev mtx_lock(&Giant); 78784569dffSMaxim Sobolev error = i386_set_ioperm(td, &iia); 78884569dffSMaxim Sobolev mtx_unlock(&Giant); 78984569dffSMaxim Sobolev return (error); 790146aad74SMarcel Moolenaar } 791146aad74SMarcel Moolenaar 792146aad74SMarcel Moolenaar int 793b40ce416SJulian Elischer linux_iopl(struct thread *td, struct linux_iopl_args *args) 794146aad74SMarcel Moolenaar { 795146aad74SMarcel Moolenaar int error; 796146aad74SMarcel Moolenaar 797146aad74SMarcel Moolenaar if (args->level < 0 || args->level > 3) 798146aad74SMarcel Moolenaar return (EINVAL); 79944731cabSJohn Baldwin if ((error = suser(td)) != 0) 800146aad74SMarcel Moolenaar return (error); 801a854ed98SJohn Baldwin if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 80241c42188SRobert Watson return (error); 803b40ce416SJulian Elischer td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) | 804146aad74SMarcel Moolenaar (args->level * (PSL_IOPL / 3)); 805146aad74SMarcel Moolenaar return (0); 806146aad74SMarcel Moolenaar } 807146aad74SMarcel Moolenaar 808146aad74SMarcel Moolenaar int 809b07cd97eSMark Murray linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap) 810146aad74SMarcel Moolenaar { 811146aad74SMarcel Moolenaar int error; 81284569dffSMaxim Sobolev struct i386_ldt_args ldt; 8135002a60fSMarcel Moolenaar struct l_descriptor ld; 81484569dffSMaxim Sobolev union descriptor desc; 815146aad74SMarcel Moolenaar 816146aad74SMarcel Moolenaar if (uap->ptr == NULL) 817146aad74SMarcel Moolenaar return (EINVAL); 818146aad74SMarcel Moolenaar 819146aad74SMarcel Moolenaar switch (uap->func) { 820146aad74SMarcel Moolenaar case 0x00: /* read_ldt */ 82184569dffSMaxim Sobolev ldt.start = 0; 82284569dffSMaxim Sobolev ldt.descs = uap->ptr; 82384569dffSMaxim Sobolev ldt.num = uap->bytecount / sizeof(union descriptor); 82484569dffSMaxim Sobolev mtx_lock(&Giant); 82584569dffSMaxim Sobolev error = i386_get_ldt(td, &ldt); 826b40ce416SJulian Elischer td->td_retval[0] *= sizeof(union descriptor); 82784569dffSMaxim Sobolev mtx_unlock(&Giant); 828146aad74SMarcel Moolenaar break; 829146aad74SMarcel Moolenaar case 0x01: /* write_ldt */ 830146aad74SMarcel Moolenaar case 0x11: /* write_ldt */ 831146aad74SMarcel Moolenaar if (uap->bytecount != sizeof(ld)) 832146aad74SMarcel Moolenaar return (EINVAL); 833146aad74SMarcel Moolenaar 834146aad74SMarcel Moolenaar error = copyin(uap->ptr, &ld, sizeof(ld)); 835146aad74SMarcel Moolenaar if (error) 836146aad74SMarcel Moolenaar return (error); 837146aad74SMarcel Moolenaar 83884569dffSMaxim Sobolev ldt.start = ld.entry_number; 83984569dffSMaxim Sobolev ldt.descs = &desc; 84084569dffSMaxim Sobolev ldt.num = 1; 84184569dffSMaxim Sobolev desc.sd.sd_lolimit = (ld.limit & 0x0000ffff); 84284569dffSMaxim Sobolev desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16; 84384569dffSMaxim Sobolev desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff); 84484569dffSMaxim Sobolev desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24; 84584569dffSMaxim Sobolev desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) | 846146aad74SMarcel Moolenaar (ld.contents << 2); 84784569dffSMaxim Sobolev desc.sd.sd_dpl = 3; 84884569dffSMaxim Sobolev desc.sd.sd_p = (ld.seg_not_present ^ 1); 84984569dffSMaxim Sobolev desc.sd.sd_xx = 0; 85084569dffSMaxim Sobolev desc.sd.sd_def32 = ld.seg_32bit; 85184569dffSMaxim Sobolev desc.sd.sd_gran = ld.limit_in_pages; 85284569dffSMaxim Sobolev mtx_lock(&Giant); 85384569dffSMaxim Sobolev error = i386_set_ldt(td, &ldt, &desc); 85484569dffSMaxim Sobolev mtx_unlock(&Giant); 855146aad74SMarcel Moolenaar break; 856146aad74SMarcel Moolenaar default: 857146aad74SMarcel Moolenaar error = EINVAL; 858146aad74SMarcel Moolenaar break; 859146aad74SMarcel Moolenaar } 860146aad74SMarcel Moolenaar 861146aad74SMarcel Moolenaar if (error == EOPNOTSUPP) { 862146aad74SMarcel Moolenaar printf("linux: modify_ldt needs kernel option USER_LDT\n"); 863146aad74SMarcel Moolenaar error = ENOSYS; 864146aad74SMarcel Moolenaar } 865146aad74SMarcel Moolenaar 866146aad74SMarcel Moolenaar return (error); 867146aad74SMarcel Moolenaar } 868146aad74SMarcel Moolenaar 869146aad74SMarcel Moolenaar int 870b40ce416SJulian Elischer linux_sigaction(struct thread *td, struct linux_sigaction_args *args) 871146aad74SMarcel Moolenaar { 8725002a60fSMarcel Moolenaar l_osigaction_t osa; 8735002a60fSMarcel Moolenaar l_sigaction_t act, oact; 874146aad74SMarcel Moolenaar int error; 875146aad74SMarcel Moolenaar 876146aad74SMarcel Moolenaar #ifdef DEBUG 87724593369SJonathan Lemon if (ldebug(sigaction)) 87824593369SJonathan Lemon printf(ARGS(sigaction, "%d, %p, %p"), 879146aad74SMarcel Moolenaar args->sig, (void *)args->nsa, (void *)args->osa); 880146aad74SMarcel Moolenaar #endif 881146aad74SMarcel Moolenaar 882146aad74SMarcel Moolenaar if (args->nsa != NULL) { 8834b7ef73dSDag-Erling Smørgrav error = copyin(args->nsa, &osa, sizeof(l_osigaction_t)); 884146aad74SMarcel Moolenaar if (error) 885146aad74SMarcel Moolenaar return (error); 886146aad74SMarcel Moolenaar act.lsa_handler = osa.lsa_handler; 887146aad74SMarcel Moolenaar act.lsa_flags = osa.lsa_flags; 888146aad74SMarcel Moolenaar act.lsa_restorer = osa.lsa_restorer; 889146aad74SMarcel Moolenaar LINUX_SIGEMPTYSET(act.lsa_mask); 890146aad74SMarcel Moolenaar act.lsa_mask.__bits[0] = osa.lsa_mask; 891146aad74SMarcel Moolenaar } 892146aad74SMarcel Moolenaar 893b40ce416SJulian Elischer error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL, 894146aad74SMarcel Moolenaar args->osa ? &oact : NULL); 895146aad74SMarcel Moolenaar 896146aad74SMarcel Moolenaar if (args->osa != NULL && !error) { 897146aad74SMarcel Moolenaar osa.lsa_handler = oact.lsa_handler; 898146aad74SMarcel Moolenaar osa.lsa_flags = oact.lsa_flags; 899146aad74SMarcel Moolenaar osa.lsa_restorer = oact.lsa_restorer; 900146aad74SMarcel Moolenaar osa.lsa_mask = oact.lsa_mask.__bits[0]; 9014b7ef73dSDag-Erling Smørgrav error = copyout(&osa, args->osa, sizeof(l_osigaction_t)); 902146aad74SMarcel Moolenaar } 903146aad74SMarcel Moolenaar 904146aad74SMarcel Moolenaar return (error); 905146aad74SMarcel Moolenaar } 906146aad74SMarcel Moolenaar 907146aad74SMarcel Moolenaar /* 908146aad74SMarcel Moolenaar * Linux has two extra args, restart and oldmask. We dont use these, 909146aad74SMarcel Moolenaar * but it seems that "restart" is actually a context pointer that 910146aad74SMarcel Moolenaar * enables the signal to happen with a different register set. 911146aad74SMarcel Moolenaar */ 912146aad74SMarcel Moolenaar int 913b40ce416SJulian Elischer linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args) 914146aad74SMarcel Moolenaar { 915206a5d3aSIan Dowse sigset_t sigmask; 9165002a60fSMarcel Moolenaar l_sigset_t mask; 917146aad74SMarcel Moolenaar 918146aad74SMarcel Moolenaar #ifdef DEBUG 91924593369SJonathan Lemon if (ldebug(sigsuspend)) 92024593369SJonathan Lemon printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask); 921146aad74SMarcel Moolenaar #endif 922146aad74SMarcel Moolenaar 923146aad74SMarcel Moolenaar LINUX_SIGEMPTYSET(mask); 924146aad74SMarcel Moolenaar mask.__bits[0] = args->mask; 925206a5d3aSIan Dowse linux_to_bsd_sigset(&mask, &sigmask); 926206a5d3aSIan Dowse return (kern_sigsuspend(td, sigmask)); 927146aad74SMarcel Moolenaar } 928146aad74SMarcel Moolenaar 929146aad74SMarcel Moolenaar int 930b07cd97eSMark Murray linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap) 931146aad74SMarcel Moolenaar { 9325002a60fSMarcel Moolenaar l_sigset_t lmask; 933206a5d3aSIan Dowse sigset_t sigmask; 934146aad74SMarcel Moolenaar int error; 935146aad74SMarcel Moolenaar 936146aad74SMarcel Moolenaar #ifdef DEBUG 93724593369SJonathan Lemon if (ldebug(rt_sigsuspend)) 93824593369SJonathan Lemon printf(ARGS(rt_sigsuspend, "%p, %d"), 939146aad74SMarcel Moolenaar (void *)uap->newset, uap->sigsetsize); 940146aad74SMarcel Moolenaar #endif 941146aad74SMarcel Moolenaar 9425002a60fSMarcel Moolenaar if (uap->sigsetsize != sizeof(l_sigset_t)) 943146aad74SMarcel Moolenaar return (EINVAL); 944146aad74SMarcel Moolenaar 9455002a60fSMarcel Moolenaar error = copyin(uap->newset, &lmask, sizeof(l_sigset_t)); 946146aad74SMarcel Moolenaar if (error) 947146aad74SMarcel Moolenaar return (error); 948146aad74SMarcel Moolenaar 949206a5d3aSIan Dowse linux_to_bsd_sigset(&lmask, &sigmask); 950206a5d3aSIan Dowse return (kern_sigsuspend(td, sigmask)); 951146aad74SMarcel Moolenaar } 952146aad74SMarcel Moolenaar 953146aad74SMarcel Moolenaar int 954b40ce416SJulian Elischer linux_pause(struct thread *td, struct linux_pause_args *args) 955146aad74SMarcel Moolenaar { 956b40ce416SJulian Elischer struct proc *p = td->td_proc; 957206a5d3aSIan Dowse sigset_t sigmask; 958146aad74SMarcel Moolenaar 959146aad74SMarcel Moolenaar #ifdef DEBUG 96024593369SJonathan Lemon if (ldebug(pause)) 96124593369SJonathan Lemon printf(ARGS(pause, "")); 962146aad74SMarcel Moolenaar #endif 963146aad74SMarcel Moolenaar 964fdfdfb78SJohn Baldwin PROC_LOCK(p); 9654093529dSJeff Roberson sigmask = td->td_sigmask; 966fdfdfb78SJohn Baldwin PROC_UNLOCK(p); 967206a5d3aSIan Dowse return (kern_sigsuspend(td, sigmask)); 968146aad74SMarcel Moolenaar } 969146aad74SMarcel Moolenaar 970146aad74SMarcel Moolenaar int 971b40ce416SJulian Elischer linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap) 972146aad74SMarcel Moolenaar { 973206a5d3aSIan Dowse stack_t ss, oss; 9745002a60fSMarcel Moolenaar l_stack_t lss; 975146aad74SMarcel Moolenaar int error; 976146aad74SMarcel Moolenaar 977146aad74SMarcel Moolenaar #ifdef DEBUG 97824593369SJonathan Lemon if (ldebug(sigaltstack)) 97924593369SJonathan Lemon printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss); 980146aad74SMarcel Moolenaar #endif 981146aad74SMarcel Moolenaar 982206a5d3aSIan Dowse if (uap->uss != NULL) { 9835002a60fSMarcel Moolenaar error = copyin(uap->uss, &lss, sizeof(l_stack_t)); 984146aad74SMarcel Moolenaar if (error) 985146aad74SMarcel Moolenaar return (error); 986146aad74SMarcel Moolenaar 987206a5d3aSIan Dowse ss.ss_sp = lss.ss_sp; 988206a5d3aSIan Dowse ss.ss_size = lss.ss_size; 989206a5d3aSIan Dowse ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags); 990931a7258SAndrew Gallatin } 991ef36ad69SJohn Baldwin error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL, 992ef36ad69SJohn Baldwin (uap->uoss != NULL) ? &oss : NULL); 993206a5d3aSIan Dowse if (!error && uap->uoss != NULL) { 994206a5d3aSIan Dowse lss.ss_sp = oss.ss_sp; 995206a5d3aSIan Dowse lss.ss_size = oss.ss_size; 996206a5d3aSIan Dowse lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags); 9975002a60fSMarcel Moolenaar error = copyout(&lss, uap->uoss, sizeof(l_stack_t)); 998146aad74SMarcel Moolenaar } 999146aad74SMarcel Moolenaar 1000146aad74SMarcel Moolenaar return (error); 1001146aad74SMarcel Moolenaar } 10023ad9c842SMaxim Sobolev 10033ad9c842SMaxim Sobolev int 10043ad9c842SMaxim Sobolev linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args) 10053ad9c842SMaxim Sobolev { 10063ad9c842SMaxim Sobolev struct ftruncate_args sa; 10073ad9c842SMaxim Sobolev 10083ad9c842SMaxim Sobolev #ifdef DEBUG 10093ad9c842SMaxim Sobolev if (ldebug(ftruncate64)) 1010b45bbfc3SBruce Evans printf(ARGS(ftruncate64, "%u, %jd"), args->fd, 1011b45bbfc3SBruce Evans (intmax_t)args->length); 10123ad9c842SMaxim Sobolev #endif 10133ad9c842SMaxim Sobolev 10143ad9c842SMaxim Sobolev sa.fd = args->fd; 10153ad9c842SMaxim Sobolev sa.pad = 0; 10163ad9c842SMaxim Sobolev sa.length = args->length; 10173ad9c842SMaxim Sobolev return ftruncate(td, &sa); 10183ad9c842SMaxim Sobolev } 10191bc85c0dSDoug Rabson 10201bc85c0dSDoug Rabson int 10211bc85c0dSDoug Rabson linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args) 10221bc85c0dSDoug Rabson { 10239b44bfc5SAlexander Leidinger struct l_user_desc info; 10249b44bfc5SAlexander Leidinger int error; 10259b44bfc5SAlexander Leidinger int idx; 10269b44bfc5SAlexander Leidinger int a[2]; 10279b44bfc5SAlexander Leidinger struct segment_descriptor sd; 10289b44bfc5SAlexander Leidinger 10299b44bfc5SAlexander Leidinger error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 10309b44bfc5SAlexander Leidinger if (error) 10319b44bfc5SAlexander Leidinger return (error); 10329b44bfc5SAlexander Leidinger 10339b44bfc5SAlexander Leidinger #ifdef DEBUG 10349b44bfc5SAlexander Leidinger if (ldebug(set_thread_area)) 10359b44bfc5SAlexander Leidinger printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"), 10369b44bfc5SAlexander Leidinger info.entry_number, 10379b44bfc5SAlexander Leidinger info.base_addr, 10389b44bfc5SAlexander Leidinger info.limit, 10399b44bfc5SAlexander Leidinger info.seg_32bit, 10409b44bfc5SAlexander Leidinger info.contents, 10419b44bfc5SAlexander Leidinger info.read_exec_only, 10429b44bfc5SAlexander Leidinger info.limit_in_pages, 10439b44bfc5SAlexander Leidinger info.seg_not_present, 10449b44bfc5SAlexander Leidinger info.useable); 10459b44bfc5SAlexander Leidinger #endif 10469b44bfc5SAlexander Leidinger 10479b44bfc5SAlexander Leidinger idx = info.entry_number; 10480eef2f8aSAlexander Leidinger /* 10490eef2f8aSAlexander Leidinger * Semantics of linux version: every thread in the system has array 10509b44bfc5SAlexander Leidinger * of 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This 10519b44bfc5SAlexander Leidinger * syscall loads one of the selected tls decriptors with a value 10529b44bfc5SAlexander Leidinger * and also loads GDT descriptors 6, 7 and 8 with the content of the per-thread 10539b44bfc5SAlexander Leidinger * descriptors. 10549b44bfc5SAlexander Leidinger * 10559b44bfc5SAlexander Leidinger * Semantics of fbsd version: I think we can ignore that linux has 3 per-thread 10569b44bfc5SAlexander Leidinger * descriptors and use just the 1st one. The tls_array[] is used only in 10579b44bfc5SAlexander Leidinger * set/get-thread_area() syscalls and for loading the GDT descriptors. In fbsd 10589b44bfc5SAlexander Leidinger * we use just one GDT descriptor for TLS so we will load just one. 10599b44bfc5SAlexander Leidinger * XXX: this doesnt work when user-space process tries to use more then 1 TLS segment 10609b44bfc5SAlexander Leidinger * comment in the linux sources says wine might do that. 10611bc85c0dSDoug Rabson */ 10629b44bfc5SAlexander Leidinger 10630eef2f8aSAlexander Leidinger /* 10640eef2f8aSAlexander Leidinger * we support just GLIBC TLS now 10659b44bfc5SAlexander Leidinger * we should let 3 proceed as well because we use this segment so 10669b44bfc5SAlexander Leidinger * if code does two subsequent calls it should succeed 10679b44bfc5SAlexander Leidinger */ 10689b44bfc5SAlexander Leidinger if (idx != 6 && idx != -1 && idx != 3) 10699b44bfc5SAlexander Leidinger return (EINVAL); 10709b44bfc5SAlexander Leidinger 10710eef2f8aSAlexander Leidinger /* 10720eef2f8aSAlexander Leidinger * we have to copy out the GDT entry we use 10739b44bfc5SAlexander Leidinger * FreeBSD uses GDT entry #3 for storing %gs so load that 10749b44bfc5SAlexander Leidinger * XXX: what if userspace program doesnt check this value and tries 10759b44bfc5SAlexander Leidinger * to use 6, 7 or 8? 10769b44bfc5SAlexander Leidinger */ 10779b44bfc5SAlexander Leidinger idx = info.entry_number = 3; 10789b44bfc5SAlexander Leidinger error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 10799b44bfc5SAlexander Leidinger if (error) 10809b44bfc5SAlexander Leidinger return (error); 10819b44bfc5SAlexander Leidinger 10829b44bfc5SAlexander Leidinger if (LDT_empty(&info)) { 10839b44bfc5SAlexander Leidinger a[0] = 0; 10849b44bfc5SAlexander Leidinger a[1] = 0; 10859b44bfc5SAlexander Leidinger } else { 10869b44bfc5SAlexander Leidinger a[0] = LDT_entry_a(&info); 10879b44bfc5SAlexander Leidinger a[1] = LDT_entry_b(&info); 10881bc85c0dSDoug Rabson } 10891bc85c0dSDoug Rabson 10909b44bfc5SAlexander Leidinger memcpy(&sd, &a, sizeof(a)); 10919b44bfc5SAlexander Leidinger #ifdef DEBUG 10929b44bfc5SAlexander Leidinger if (ldebug(set_thread_area)) 10939b44bfc5SAlexander Leidinger 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, 10949b44bfc5SAlexander Leidinger sd.sd_hibase, 10959b44bfc5SAlexander Leidinger sd.sd_lolimit, 10969b44bfc5SAlexander Leidinger sd.sd_hilimit, 10979b44bfc5SAlexander Leidinger sd.sd_type, 10989b44bfc5SAlexander Leidinger sd.sd_dpl, 10999b44bfc5SAlexander Leidinger sd.sd_p, 11009b44bfc5SAlexander Leidinger sd.sd_xx, 11019b44bfc5SAlexander Leidinger sd.sd_def32, 11029b44bfc5SAlexander Leidinger sd.sd_gran); 11039b44bfc5SAlexander Leidinger #endif 11041bc85c0dSDoug Rabson 11059b44bfc5SAlexander Leidinger /* this is taken from i386 version of cpu_set_user_tls() */ 11069b44bfc5SAlexander Leidinger critical_enter(); 11079b44bfc5SAlexander Leidinger /* set %gs */ 11089b44bfc5SAlexander Leidinger td->td_pcb->pcb_gsd = sd; 11099b44bfc5SAlexander Leidinger PCPU_GET(fsgs_gdt)[1] = sd; 11109b44bfc5SAlexander Leidinger load_gs(GSEL(GUGS_SEL, SEL_UPL)); 11119b44bfc5SAlexander Leidinger critical_exit(); 11129b44bfc5SAlexander Leidinger 11131bc85c0dSDoug Rabson return (0); 11141bc85c0dSDoug Rabson } 11151bc85c0dSDoug Rabson 11161bc85c0dSDoug Rabson int 11179b44bfc5SAlexander Leidinger linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args) 11181bc85c0dSDoug Rabson { 11191bc85c0dSDoug Rabson 11209b44bfc5SAlexander Leidinger struct l_user_desc info; 11219b44bfc5SAlexander Leidinger int error; 11229b44bfc5SAlexander Leidinger int idx; 11239b44bfc5SAlexander Leidinger struct l_desc_struct desc; 11249b44bfc5SAlexander Leidinger struct segment_descriptor sd; 11259b44bfc5SAlexander Leidinger 11269b44bfc5SAlexander Leidinger #ifdef DEBUG 11279b44bfc5SAlexander Leidinger if (ldebug(get_thread_area)) 11289b44bfc5SAlexander Leidinger printf(ARGS(get_thread_area, "%p"), args->desc); 11299b44bfc5SAlexander Leidinger #endif 11309b44bfc5SAlexander Leidinger 11319b44bfc5SAlexander Leidinger error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 11329b44bfc5SAlexander Leidinger if (error) 11339b44bfc5SAlexander Leidinger return (error); 11349b44bfc5SAlexander Leidinger 11359b44bfc5SAlexander Leidinger idx = info.entry_number; 11369b44bfc5SAlexander Leidinger /* XXX: I am not sure if we want 3 to be allowed too. */ 11379b44bfc5SAlexander Leidinger if (idx != 6 && idx != 3) 11389b44bfc5SAlexander Leidinger return (EINVAL); 11399b44bfc5SAlexander Leidinger 11409b44bfc5SAlexander Leidinger idx = 3; 11419b44bfc5SAlexander Leidinger 11429b44bfc5SAlexander Leidinger memset(&info, 0, sizeof(info)); 11439b44bfc5SAlexander Leidinger 11449b44bfc5SAlexander Leidinger sd = PCPU_GET(fsgs_gdt)[1]; 11459b44bfc5SAlexander Leidinger 11469b44bfc5SAlexander Leidinger memcpy(&desc, &sd, sizeof(desc)); 11479b44bfc5SAlexander Leidinger 11489b44bfc5SAlexander Leidinger info.entry_number = idx; 11499b44bfc5SAlexander Leidinger info.base_addr = GET_BASE(&desc); 11509b44bfc5SAlexander Leidinger info.limit = GET_LIMIT(&desc); 11519b44bfc5SAlexander Leidinger info.seg_32bit = GET_32BIT(&desc); 11529b44bfc5SAlexander Leidinger info.contents = GET_CONTENTS(&desc); 11539b44bfc5SAlexander Leidinger info.read_exec_only = !GET_WRITABLE(&desc); 11549b44bfc5SAlexander Leidinger info.limit_in_pages = GET_LIMIT_PAGES(&desc); 11559b44bfc5SAlexander Leidinger info.seg_not_present = !GET_PRESENT(&desc); 11569b44bfc5SAlexander Leidinger info.useable = GET_USEABLE(&desc); 11579b44bfc5SAlexander Leidinger 11589b44bfc5SAlexander Leidinger error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 11599b44bfc5SAlexander Leidinger if (error) 11609b44bfc5SAlexander Leidinger return (EFAULT); 11619b44bfc5SAlexander Leidinger 11629b44bfc5SAlexander Leidinger return (0); 11639b44bfc5SAlexander Leidinger } 11649b44bfc5SAlexander Leidinger 11659b44bfc5SAlexander Leidinger /* copied from kern/kern_time.c */ 11669b44bfc5SAlexander Leidinger int 11679b44bfc5SAlexander Leidinger linux_timer_create(struct thread *td, struct linux_timer_create_args *args) 11689b44bfc5SAlexander Leidinger { 11699b44bfc5SAlexander Leidinger return ktimer_create(td, (struct ktimer_create_args *) args); 11709b44bfc5SAlexander Leidinger } 11719b44bfc5SAlexander Leidinger 11729b44bfc5SAlexander Leidinger int 11739b44bfc5SAlexander Leidinger linux_timer_settime(struct thread *td, struct linux_timer_settime_args *args) 11749b44bfc5SAlexander Leidinger { 11759b44bfc5SAlexander Leidinger return ktimer_settime(td, (struct ktimer_settime_args *) args); 11769b44bfc5SAlexander Leidinger } 11779b44bfc5SAlexander Leidinger 11789b44bfc5SAlexander Leidinger int 11799b44bfc5SAlexander Leidinger linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *args) 11809b44bfc5SAlexander Leidinger { 11819b44bfc5SAlexander Leidinger return ktimer_gettime(td, (struct ktimer_gettime_args *) args); 11829b44bfc5SAlexander Leidinger } 11839b44bfc5SAlexander Leidinger 11849b44bfc5SAlexander Leidinger int 11859b44bfc5SAlexander Leidinger linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *args) 11869b44bfc5SAlexander Leidinger { 11879b44bfc5SAlexander Leidinger return ktimer_getoverrun(td, (struct ktimer_getoverrun_args *) args); 11889b44bfc5SAlexander Leidinger } 11899b44bfc5SAlexander Leidinger 11909b44bfc5SAlexander Leidinger int 11919b44bfc5SAlexander Leidinger linux_timer_delete(struct thread *td, struct linux_timer_delete_args *args) 11929b44bfc5SAlexander Leidinger { 11939b44bfc5SAlexander Leidinger return ktimer_delete(td, (struct ktimer_delete_args *) args); 11949b44bfc5SAlexander Leidinger } 11959b44bfc5SAlexander Leidinger 11969b44bfc5SAlexander Leidinger /* XXX: this wont work with module - convert it */ 11979b44bfc5SAlexander Leidinger int 11989b44bfc5SAlexander Leidinger linux_mq_open(struct thread *td, struct linux_mq_open_args *args) 11999b44bfc5SAlexander Leidinger { 12009b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 12019b44bfc5SAlexander Leidinger return kmq_open(td, (struct kmq_open_args *) args); 12029b44bfc5SAlexander Leidinger #else 12039b44bfc5SAlexander Leidinger return (ENOSYS); 12049b44bfc5SAlexander Leidinger #endif 12059b44bfc5SAlexander Leidinger } 12069b44bfc5SAlexander Leidinger 12079b44bfc5SAlexander Leidinger int 12089b44bfc5SAlexander Leidinger linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args) 12099b44bfc5SAlexander Leidinger { 12109b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 12119b44bfc5SAlexander Leidinger return kmq_unlink(td, (struct kmq_unlink_args *) args); 12129b44bfc5SAlexander Leidinger #else 12139b44bfc5SAlexander Leidinger return (ENOSYS); 12149b44bfc5SAlexander Leidinger #endif 12159b44bfc5SAlexander Leidinger } 12169b44bfc5SAlexander Leidinger 12179b44bfc5SAlexander Leidinger int 12189b44bfc5SAlexander Leidinger linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args) 12199b44bfc5SAlexander Leidinger { 12209b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 12219b44bfc5SAlexander Leidinger return kmq_timedsend(td, (struct kmq_timedsend_args *) args); 12229b44bfc5SAlexander Leidinger #else 12239b44bfc5SAlexander Leidinger return (ENOSYS); 12249b44bfc5SAlexander Leidinger #endif 12259b44bfc5SAlexander Leidinger } 12269b44bfc5SAlexander Leidinger 12279b44bfc5SAlexander Leidinger int 12289b44bfc5SAlexander Leidinger linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args) 12299b44bfc5SAlexander Leidinger { 12309b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 12319b44bfc5SAlexander Leidinger return kmq_timedreceive(td, (struct kmq_timedreceive_args *) args); 12329b44bfc5SAlexander Leidinger #else 12339b44bfc5SAlexander Leidinger return (ENOSYS); 12349b44bfc5SAlexander Leidinger #endif 12359b44bfc5SAlexander Leidinger } 12369b44bfc5SAlexander Leidinger 12379b44bfc5SAlexander Leidinger int 12389b44bfc5SAlexander Leidinger linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args) 12399b44bfc5SAlexander Leidinger { 12409b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 12419b44bfc5SAlexander Leidinger return kmq_notify(td, (struct kmq_notify_args *) args); 12429b44bfc5SAlexander Leidinger #else 12439b44bfc5SAlexander Leidinger return (ENOSYS); 12449b44bfc5SAlexander Leidinger #endif 12459b44bfc5SAlexander Leidinger } 12469b44bfc5SAlexander Leidinger 12479b44bfc5SAlexander Leidinger int 12489b44bfc5SAlexander Leidinger linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args) 12499b44bfc5SAlexander Leidinger { 12509b44bfc5SAlexander Leidinger #ifdef P1003_1B_MQUEUE 12519b44bfc5SAlexander Leidinger return kmq_setattr(td, (struct kmq_setattr_args *) args); 12529b44bfc5SAlexander Leidinger #else 12539b44bfc5SAlexander Leidinger return (ENOSYS); 12549b44bfc5SAlexander Leidinger #endif 12551bc85c0dSDoug Rabson } 12561bc85c0dSDoug Rabson 1257