xref: /netbsd-src/sys/compat/linux/arch/amd64/linux_exec_machdep.c (revision d9ac053afa92f3d7a493f791e10558f738234217)
1 /*	$NetBSD: linux_exec_machdep.c,v 1.7 2006/05/15 13:12:13 yamt Exp $ */
2 
3 /*-
4  * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Emmanuel Dreyfus
17  * 4. The name of the author may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: linux_exec_machdep.c,v 1.7 2006/05/15 13:12:13 yamt Exp $");
36 
37 #ifdef __amd64__
38 #define ELFSIZE 64
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/resource.h>
45 #include <sys/proc.h>
46 #include <sys/conf.h>
47 #include <sys/malloc.h>
48 #include <sys/exec_elf.h>
49 #include <sys/vnode.h>
50 #include <sys/lwp.h>
51 #include <sys/exec.h>
52 #include <sys/stat.h>
53 #include <sys/exec_elf.h>
54 #include <sys/kauth.h>
55 
56 #include <machine/cpu.h>
57 #include <machine/vmparam.h>
58 
59 #include <uvm/uvm.h>
60 
61 #include <compat/linux/common/linux_types.h>
62 #include <compat/linux/common/linux_signal.h>
63 #include <compat/linux/common/linux_util.h>
64 #include <compat/linux/common/linux_ioctl.h>
65 #include <compat/linux/common/linux_hdio.h>
66 #include <compat/linux/common/linux_exec.h>
67 #include <compat/linux/common/linux_machdep.h>
68 #include <compat/linux/common/linux_errno.h>
69 
70 int
71 linux_exec_setup_stack(struct lwp *l, struct exec_package *epp)
72 {
73 	u_long max_stack_size;
74 	u_long access_linear_min, access_size;
75 	u_long noaccess_linear_min, noaccess_size;
76 
77 #ifndef USRSTACK32
78 #define USRSTACK32      (0x00000000ffffffffL & ~PGOFSET)
79 #endif
80 
81 	if (epp->ep_flags & EXEC_32) {
82 		epp->ep_minsaddr = USRSTACK32;
83 		max_stack_size = MAXSSIZ;
84 		if (epp->ep_minsaddr > LINUX_USRSTACK32)
85 			epp->ep_minsaddr = LINUX_USRSTACK32;
86 	} else {
87 		epp->ep_minsaddr = USRSTACK;
88 		max_stack_size = MAXSSIZ;
89 		if (epp->ep_minsaddr > LINUX_USRSTACK)
90 			epp->ep_minsaddr = LINUX_USRSTACK;
91 
92 	}
93 
94 	epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
95 		max_stack_size);
96 	epp->ep_ssize = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
97 
98 	/*
99 	 * set up commands for stack.  note that this takes *two*, one to
100 	 * map the part of the stack which we can access, and one to map
101 	 * the part which we can't.
102 	 *
103 	 * arguably, it could be made into one, but that would require the
104 	 * addition of another mapping proc, which is unnecessary
105 	 */
106 	access_size = epp->ep_ssize;
107 	access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
108 	noaccess_size = max_stack_size - access_size;
109 	noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
110 	    access_size), noaccess_size);
111 	if (noaccess_size > 0) {
112 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
113 		    noaccess_linear_min, NULLVP, 0, VM_PROT_NONE);
114 	}
115 	KASSERT(access_size > 0);
116 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
117 	    access_linear_min, NULLVP, 0, VM_PROT_READ | VM_PROT_WRITE);
118 
119 	return 0;
120 }
121 
122 int
123 ELFNAME2(linux,copyargs)(l, pack, arginfo, stackp, argp)
124 	struct lwp *l;
125 	struct exec_package *pack;
126 	struct ps_strings *arginfo;
127 	char **stackp;
128 	void *argp;
129 {
130 	struct linux_extra_stack_data64 *esdp, esd;
131 	struct elf_args *ap;
132 	struct vattr *vap;
133 	struct proc *p = l->l_proc;
134 	Elf_Ehdr *eh;
135 	Elf_Phdr *ph;
136 	u_long phsize;
137 	Elf_Addr phdr = 0;
138 	int error;
139 	int i;
140 
141 	if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
142 		return error;
143 
144 	/*
145 	 * Push extra arguments on the stack needed by dynamically
146 	 * linked binaries and static binaries as well.
147 	 */
148 	memset(&esd, 0, sizeof(esd));
149 	esdp = (struct linux_extra_stack_data64 *)(*stackp);
150 	ap = (struct elf_args *)pack->ep_emul_arg;
151 	vap = pack->ep_vap;
152 	eh = (Elf_Ehdr *)pack->ep_hdr;
153 
154 	/*
155 	 * We forgot this, so we ned to reload it now. XXX keep track of it?
156 	 */
157 	if (ap == NULL) {
158 		phsize = eh->e_phnum * sizeof(Elf_Phdr);
159 		ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
160 		error = exec_read_from(l, pack->ep_vp, eh->e_phoff, ph, phsize);
161 		if (error != 0) {
162 			for (i = 0; i < eh->e_phnum; i++) {
163 				if (ph[i].p_type == PT_PHDR) {
164 					phdr = ph[i].p_vaddr;
165 					break;
166 				}
167 			}
168 		}
169 		free(ph, M_TEMP);
170 	}
171 
172 
173 	/*
174 	 * The exec_package doesn't have a proc pointer and it's not
175 	 * exactly trivial to add one since the credentials are
176 	 * changing. XXX Linux uses curlwp's credentials.
177 	 * Why can't we use them too?
178 	 */
179 
180 	i = 0;
181 	esd.ai[i].a_type = LINUX_AT_HWCAP;
182 	esd.ai[i++].a_v = rcr4();
183 
184 	esd.ai[i].a_type = AT_PAGESZ;
185 	esd.ai[i++].a_v = PAGE_SIZE;
186 
187 	esd.ai[i].a_type = LINUX_AT_CLKTCK;
188 	esd.ai[i++].a_v = hz;
189 
190 	esd.ai[i].a_type = AT_PHDR;
191 	esd.ai[i++].a_v = (ap ? ap->arg_phaddr: phdr);
192 
193 	esd.ai[i].a_type = AT_PHENT;
194 	esd.ai[i++].a_v = (ap ? ap->arg_phentsize : eh->e_phentsize);
195 
196 	esd.ai[i].a_type = AT_PHNUM;
197 	esd.ai[i++].a_v = (ap ? ap->arg_phnum : eh->e_phnum);
198 
199 	esd.ai[i].a_type = AT_BASE;
200 	esd.ai[i++].a_v = (ap ? ap->arg_interp : 0);
201 
202 	esd.ai[i].a_type = AT_FLAGS;
203 	esd.ai[i++].a_v = 0;
204 
205 	esd.ai[i].a_type = AT_ENTRY;
206 	esd.ai[i++].a_v = (ap ? ap->arg_entry : eh->e_entry);
207 
208 	esd.ai[i].a_type = LINUX_AT_EGID;
209 	esd.ai[i++].a_v =
210 	    ((vap->va_mode & S_ISGID) ? vap->va_gid : kauth_cred_getegid(p->p_cred));
211 
212 	esd.ai[i].a_type = LINUX_AT_GID;
213 	esd.ai[i++].a_v = kauth_cred_getgid(p->p_cred);
214 
215 	esd.ai[i].a_type = LINUX_AT_EUID;
216 	esd.ai[i++].a_v =
217 	    ((vap->va_mode & S_ISUID) ? vap->va_uid : kauth_cred_geteuid(p->p_cred));
218 
219 	esd.ai[i].a_type = LINUX_AT_UID;
220 	esd.ai[i++].a_v = kauth_cred_getuid(p->p_cred);
221 
222 	esd.ai[i].a_type = LINUX_AT_SECURE;
223 	esd.ai[i++].a_v = 0;
224 
225 	esd.ai[i].a_type = LINUX_AT_PLATFORM;
226 	esd.ai[i++].a_v = (Elf_Addr)&esdp->hw_platform[0];
227 
228 	esd.ai[i].a_type = AT_NULL;
229 	esd.ai[i++].a_v = 0;
230 
231 #ifdef DEBUG_LINUX
232 	if (i != LINUX_ELF_AUX_ENTRIES) {
233 		printf("linux_elf64_copyargs: %d Aux entries\n", i);
234 		return EINVAL;
235 	}
236 #endif
237 
238 	strcpy(esd.hw_platform, LINUX_PLATFORM);
239 
240 	if (ap) {
241 		free((char *)ap, M_TEMP);
242 		pack->ep_emul_arg = NULL;
243 	}
244 
245 	/*
246 	 * Copy out the ELF auxiliary table and hw platform name
247 	 */
248 	if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
249 		return error;
250 	*stackp += sizeof(esd);
251 
252 	return 0;
253 }
254