Home | History | Annotate | Line # | Download | only in i386
linux_exec_machdep.c revision 1.17.32.1
      1 /*	$NetBSD: linux_exec_machdep.c,v 1.17.32.1 2017/08/28 17:51:58 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: linux_exec_machdep.c,v 1.17.32.1 2017/08/28 17:51:58 skrll Exp $");
     34 
     35 #if defined(_KERNEL_OPT)
     36 #include "opt_user_ldt.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/resource.h>
     42 #include <sys/proc.h>
     43 #include <sys/conf.h>
     44 #include <sys/exec.h>
     45 #include <sys/exec_elf.h>
     46 #include <sys/vnode.h>
     47 #include <sys/lwp.h>
     48 
     49 #include <sys/cpu.h>
     50 #include <machine/vmparam.h>
     51 
     52 #include <uvm/uvm.h>
     53 
     54 #include <sys/syscallargs.h>
     55 
     56 #ifndef DEBUG_LINUX
     57 #define DPRINTF(a)
     58 #else
     59 #define DPRINTF(a)	uprintf a
     60 #endif
     61 
     62 #include <compat/linux/common/linux_types.h>
     63 #include <compat/linux/common/linux_signal.h>
     64 #include <compat/linux/common/linux_machdep.h>
     65 #include <compat/linux/common/linux_util.h>
     66 #include <compat/linux/common/linux_ioctl.h>
     67 #include <compat/linux/common/linux_hdio.h>
     68 #include <compat/linux/common/linux_exec.h>
     69 #include <compat/linux/common/linux_errno.h>
     70 #include <compat/linux/linux_syscallargs.h>
     71 
     72 int
     73 linux_exec_setup_stack(struct lwp *l, struct exec_package *epp)
     74 {
     75 	u_long max_stack_size;
     76 	u_long access_linear_min, access_size;
     77 	u_long noaccess_linear_min, noaccess_size;
     78 
     79 #ifndef	USRSTACK32
     80 #define USRSTACK32	(0x00000000ffffffffL&~PGOFSET)
     81 #endif
     82 
     83 	if (epp->ep_flags & EXEC_32) {
     84 		epp->ep_minsaddr = USRSTACK32;
     85 		max_stack_size = MAXSSIZ;
     86 	} else {
     87 		epp->ep_minsaddr = USRSTACK;
     88 		max_stack_size = MAXSSIZ;
     89 	}
     90 
     91 	if (epp->ep_minsaddr > LINUX_USRSTACK)
     92 		epp->ep_minsaddr = LINUX_USRSTACK;
     93 #ifdef DEBUG_LINUX
     94 	else {
     95 		/*
     96 		 * Someone needs to make KERNBASE and TEXTADDR
     97 		 * java versions < 1.4.2 need the stack to be
     98 		 * at 0xC0000000
     99 		 */
    100 		uprintf("Cannot setup stack to 0xC0000000, "
    101 		    "java will not work properly\n");
    102 	}
    103 #endif
    104 	epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
    105 		max_stack_size);
    106 	epp->ep_ssize = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
    107 
    108 	/*
    109 	 * set up commands for stack.  note that this takes *two*, one to
    110 	 * map the part of the stack which we can access, and one to map
    111 	 * the part which we can't.
    112 	 *
    113 	 * arguably, it could be made into one, but that would require the
    114 	 * addition of another mapping proc, which is unnecessary
    115 	 */
    116 	access_size = epp->ep_ssize;
    117 	access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
    118 	noaccess_size = max_stack_size - access_size;
    119 	noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
    120 	    access_size), noaccess_size);
    121 	if (noaccess_size > 0) {
    122 		NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
    123 		    noaccess_linear_min, NULLVP, 0, VM_PROT_NONE, VMCMD_STACK);
    124 	}
    125 	KASSERT(access_size > 0);
    126 	NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
    127 	    access_linear_min, NULLVP, 0, VM_PROT_READ | VM_PROT_WRITE,
    128 	    VMCMD_STACK);
    129 
    130 	return 0;
    131 }
    132 
    133 int
    134 linux_sys_set_thread_area(struct lwp *l,
    135     const struct linux_sys_set_thread_area_args *uap, register_t *retval)
    136 {
    137 	/* {
    138 		syscallarg(struct linux_user_desc *) desc;
    139 	} */
    140 
    141 	return linux_lwp_setprivate(l, SCARG(uap, desc));
    142 }
    143 
    144 int
    145 linux_sys_get_thread_area(struct lwp *l,
    146     const struct linux_sys_get_thread_area_args *uap, register_t *retval)
    147 {
    148 	/* {
    149 		syscallarg(struct linux_user_desc *) desc;
    150 	} */
    151 
    152 	/* glibc doesn't actually call this. */
    153 	return ENOSYS;
    154 }
    155