xref: /netbsd-src/sys/compat/linux/arch/i386/linux_exec_machdep.c (revision 2de962bd804263c16657f586aa00f1704045df8e)
1 /*	$NetBSD: linux_exec_machdep.c,v 1.5 2008/04/28 20:23:42 martin 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.5 2008/04/28 20:23:42 martin Exp $");
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_vm86.h"
37 #include "opt_user_ldt.h"
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/resource.h>
43 #include <sys/proc.h>
44 #include <sys/conf.h>
45 #include <sys/exec.h>
46 #include <sys/exec_elf.h>
47 #include <sys/vnode.h>
48 #include <sys/lwp.h>
49 
50 #include <sys/cpu.h>
51 #include <machine/vmparam.h>
52 
53 #include <uvm/uvm.h>
54 
55 #include <compat/linux/common/linux_types.h>
56 #include <compat/linux/common/linux_signal.h>
57 #include <compat/linux/common/linux_util.h>
58 #include <compat/linux/common/linux_ioctl.h>
59 #include <compat/linux/common/linux_hdio.h>
60 #include <compat/linux/common/linux_exec.h>
61 #include <compat/linux/common/linux_machdep.h>
62 #include <compat/linux/common/linux_errno.h>
63 
64 int
65 linux_exec_setup_stack(struct lwp *l, struct exec_package *epp)
66 {
67 	u_long max_stack_size;
68 	u_long access_linear_min, access_size;
69 	u_long noaccess_linear_min, noaccess_size;
70 
71 #ifndef	USRSTACK32
72 #define USRSTACK32	(0x00000000ffffffffL&~PGOFSET)
73 #endif
74 
75 	if (epp->ep_flags & EXEC_32) {
76 		epp->ep_minsaddr = USRSTACK32;
77 		max_stack_size = MAXSSIZ;
78 	} else {
79 		epp->ep_minsaddr = USRSTACK;
80 		max_stack_size = MAXSSIZ;
81 	}
82 
83 	if (epp->ep_minsaddr > LINUX_USRSTACK)
84 		epp->ep_minsaddr = LINUX_USRSTACK;
85 #ifdef DEBUG_LINUX
86 	else {
87 		/*
88 		 * Someone needs to make KERNBASE and TEXTADDR
89 		 * java versions < 1.4.2 need the stack to be
90 		 * at 0xC0000000
91 		 */
92 		uprintf("Cannot setup stack to 0xC0000000, "
93 		    "java will not work properly\n");
94 	}
95 #endif
96 	epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
97 		max_stack_size);
98 	epp->ep_ssize = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
99 
100 	/*
101 	 * set up commands for stack.  note that this takes *two*, one to
102 	 * map the part of the stack which we can access, and one to map
103 	 * the part which we can't.
104 	 *
105 	 * arguably, it could be made into one, but that would require the
106 	 * addition of another mapping proc, which is unnecessary
107 	 */
108 	access_size = epp->ep_ssize;
109 	access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
110 	noaccess_size = max_stack_size - access_size;
111 	noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
112 	    access_size), noaccess_size);
113 	if (noaccess_size > 0) {
114 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
115 		    noaccess_linear_min, NULLVP, 0, VM_PROT_NONE);
116 	}
117 	KASSERT(access_size > 0);
118 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
119 	    access_linear_min, NULLVP, 0, VM_PROT_READ | VM_PROT_WRITE);
120 
121 	return 0;
122 }
123