xref: /netbsd-src/sys/compat/linux/arch/powerpc/linux_exec_powerpc.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /* $NetBSD: linux_exec_powerpc.c,v 1.14 2004/06/18 17:06:15 manu Exp $ */
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Emmanuel Dreyfus.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the NetBSD
21  *      Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * From NetBSD's sys/compat/arch/alpha/linux_exec_alpha.c, with some
41  * powerpc add-ons (ifdef LINUX_SHIFT and LINUX_SP_WRAP).
42  *
43  * This code is to be common to alpha and powerpc. If it works on alpha, it
44  * should be moved to common/linux_exec_elf32.c. Beware that it needs
45  * LINUX_ELF_AUX_ENTRIES in arch/<arch>/linux_exec.h to also be moved to common
46  *
47  * Emmanuel Dreyfus <p99dreyf@criens.u-psud.fr>
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: linux_exec_powerpc.c,v 1.14 2004/06/18 17:06:15 manu Exp $");
52 
53 #if defined (__alpha__)
54 #define ELFSIZE 64
55 #elif defined (__powerpc__)
56 #define ELFSIZE 32
57 #else
58 #error Unified linux_elf_{32|64}copyargs not tested for this platform
59 #endif
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
65 #include <sys/proc.h>
66 #include <sys/exec.h>
67 #include <sys/exec_elf.h>
68 #include <sys/resourcevar.h>
69 
70 #include <uvm/uvm_extern.h>
71 
72 #include <compat/linux/common/linux_exec.h>
73 
74 #ifdef LINUX_SP_WRAP
75 extern int linux_sp_wrap_start;
76 extern int linux_sp_wrap_end;
77 extern int linux_sp_wrap_entry;
78 #endif
79 /*
80  * Alpha and PowerPC specific linux copyargs function.
81  */
82 int
83 ELFNAME2(linux,copyargs)(p, pack, arginfo, stackp, argp)
84 	struct proc *p;
85 	struct exec_package *pack;
86 	struct ps_strings *arginfo;
87 	char **stackp;
88 	void *argp;
89 {
90 	size_t len;
91 	AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
92 	struct elf_args *ap;
93 #ifdef LINUX_SP_WRAP
94 	AuxInfo *prog_entry = NULL;
95 	char	linux_sp_wrap_code[LINUX_SP_WRAP];
96 	unsigned long*	cga;
97 #endif
98 	int error;
99 
100 #ifdef LINUX_SHIFT
101 	/*
102 	 * Seems that PowerPC Linux binaries expect argc to start on a 16 bytes
103 	 * aligned address. And we need one more 16 byte shift if it was already
104 	 * 16 bytes aligned,
105 	 */
106 	*stackp = (char *)(((unsigned long)*stackp - 1) & ~LINUX_SHIFT);
107 #endif
108 
109 	if ((error = copyargs(p, pack, arginfo, stackp, argp)) != 0)
110 		return error;
111 
112 #ifdef LINUX_SHIFT
113 	/*
114 	 * From Linux's arch/ppc/kernel/process.c:shove_aux_table(). GNU ld.so
115 	 * expects the ELF auxiliary table to start on a 16 bytes boundary on
116 	 * the PowerPC.
117 	 */
118 	*stackp = (char *)(((unsigned long)(*stackp) + LINUX_SHIFT)
119 	    & ~LINUX_SHIFT);
120 #endif
121 
122 	memset(ai, 0, sizeof(AuxInfo) * LINUX_ELF_AUX_ENTRIES);
123 
124 	a = ai;
125 
126 	/*
127 	 * Push extra arguments on the stack needed by dynamically
128 	 * linked binaries.
129 	 */
130 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
131 #ifdef LINUX_SP_WRAP
132 		memset(linux_sp_wrap_code, 0, LINUX_SP_WRAP);
133 		bcopy(&linux_sp_wrap_start, linux_sp_wrap_code,
134 		    (unsigned long)(&linux_sp_wrap_end)
135 		    - (unsigned long)(&linux_sp_wrap_start));
136 		(unsigned long)cga = ((unsigned long)linux_sp_wrap_code)
137 		    + ((unsigned long)(&linux_sp_wrap_entry))
138 		    - ((unsigned long)(&linux_sp_wrap_start));
139 		(*cga) = (unsigned long)(ap->arg_entry);
140 #endif
141 #if 1
142 		/*
143 		 * The exec_package doesn't have a proc pointer and it's not
144 		 * exactly trivial to add one since the credentials are
145 		 * changing. XXX Linux uses curlwp's credentials.
146 		 * Why can't we use them too?
147 		 */
148 		a->a_type = LINUX_AT_EGID;
149 		a->a_v = p->p_ucred->cr_gid;
150 		a++;
151 
152 		a->a_type = LINUX_AT_GID;
153 		a->a_v = p->p_cred->p_rgid;
154 		a++;
155 
156 		a->a_type = LINUX_AT_EUID;
157 		a->a_v = p->p_ucred->cr_uid;
158 		a++;
159 
160 		a->a_type = LINUX_AT_UID;
161 		a->a_v = p->p_cred->p_ruid;
162 		a++;
163 #endif
164 
165 		a->a_type = AT_ENTRY;
166 		a->a_v = ap->arg_entry;
167 #ifdef LINUX_SP_WRAP
168 		prog_entry = a;
169 #endif
170 		a++;
171 
172 		a->a_type = AT_FLAGS;
173 		a->a_v = 0;
174 		a++;
175 
176 		a->a_type = AT_BASE;
177 		a->a_v = ap->arg_interp;
178 		a++;
179 
180 		a->a_type = AT_PHNUM;
181 		a->a_v = ap->arg_phnum;
182 		a++;
183 
184 		a->a_type = AT_PHENT;
185 		a->a_v = ap->arg_phentsize;
186 		a++;
187 
188 		a->a_type = AT_PHDR;
189 		a->a_v = ap->arg_phaddr;
190 		a++;
191 
192 		a->a_type = LINUX_AT_CLKTCK;
193 		a->a_v = LINUX_CLOCKS_PER_SEC;
194 		a++;
195 
196 		a->a_type = AT_PAGESZ;
197 		a->a_v = PAGE_SIZE;
198 		a++;
199 
200 		a->a_type = LINUX_AT_HWCAP;
201 		a->a_v = LINUX_ELF_HWCAP;
202 		a++;
203 
204 		free((char *)ap, M_TEMP);
205 		pack->ep_emul_arg = NULL;
206 	}
207 
208 	a->a_type = AT_NULL;
209 	a->a_v = 0;
210 	a++;
211 
212 	len = (a - ai) * sizeof(AuxInfo);
213 
214 #ifdef LINUX_SP_WRAP
215 	if (prog_entry != NULL)
216 		prog_entry->a_v = (unsigned long)(*stackp) + len;
217 #endif
218 
219 	if ((error = copyout(ai, *stackp, len)) != 0)
220 		return error;
221 	*stackp += len;
222 
223 #ifdef LINUX_SP_WRAP
224 	if (prog_entry != NULL) {
225 		if ((error = copyout(linux_sp_wrap_code, *stackp,
226 		    LINUX_SP_WRAP)) != 0)
227 			return error;
228 		*stackp += LINUX_SP_WRAP;
229 	}
230 #endif
231 
232 	return 0;
233 }
234 
235 /*
236  * This is copied from sys/kern/exec_subr.c:exec_setup_stack()
237  * We need a Linux version only to avoid the non executable
238  * mappings. They will probably break signal delivery on Linux,
239  * and they surely break the stack fixup hack.
240  */
241 int
242 linux_exec_setup_stack(p, epp)
243 	struct proc *p;
244 	struct exec_package *epp;
245 {
246 	u_long max_stack_size;
247 	u_long access_linear_min, access_size;
248 	u_long noaccess_linear_min, noaccess_size;
249 
250 #ifndef	USRSTACK32
251 #define USRSTACK32	(0x00000000ffffffffL&~PGOFSET)
252 #endif
253 
254 	if (epp->ep_flags & EXEC_32) {
255 		epp->ep_minsaddr = USRSTACK32;
256 		max_stack_size = MAXSSIZ;
257 	} else {
258 		epp->ep_minsaddr = USRSTACK;
259 		max_stack_size = MAXSSIZ;
260 	}
261 	epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
262 		max_stack_size);
263 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
264 
265 	/*
266 	 * set up commands for stack.  note that this takes *two*, one to
267 	 * map the part of the stack which we can access, and one to map
268 	 * the part which we can't.
269 	 *
270 	 * arguably, it could be made into one, but that would require the
271 	 * addition of another mapping proc, which is unnecessary
272 	 */
273 	access_size = epp->ep_ssize;
274 	access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
275 	noaccess_size = max_stack_size - access_size;
276 	noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
277 	    access_size), noaccess_size);
278 	if (noaccess_size > 0) {
279 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
280 		    noaccess_linear_min, NULL, 0, VM_PROT_NONE);
281 	}
282 	KASSERT(access_size > 0);
283 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
284 	    access_linear_min, NULL, 0,
285 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
286 
287 	return 0;
288 }
289