linux_exec_powerpc.c revision 1.5
1/* $NetBSD: linux_exec_powerpc.c,v 1.5 2001/08/08 13:23:34 christos 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#if defined (__alpha__)
50#define ELFSIZE 64
51#elif defined (__powerpc__)
52#define ELFSIZE 32
53#else
54#error Unified linux_elf_{32|64}copyargs not tested for this platform
55#endif
56
57#include <sys/param.h>
58#include <sys/systm.h>
59#include <sys/kernel.h>
60#include <sys/malloc.h>
61#include <sys/proc.h>
62#include <sys/exec.h>
63#include <sys/exec_elf.h>
64
65#include <compat/linux/common/linux_exec.h>
66
67#ifdef LINUX_SP_WRAP
68extern int linux_sp_wrap_start;
69extern int linux_sp_wrap_end;
70extern int linux_sp_wrap_entry;
71#endif
72/*
73 * Alpha and PowerPC specific linux copyargs function.
74 */
75int
76ELFNAME2(linux,copyargs)(pack, arginfo, stackp, argp)
77	struct exec_package *pack;
78	struct ps_strings *arginfo;
79	char **stackp;
80	void *argp;
81{
82	size_t len;
83	LinuxAuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
84	struct elf_args *ap;
85	struct proc *p = curproc;
86#ifdef LINUX_SP_WRAP
87	LinuxAuxInfo *prog_entry = NULL;
88	char	linux_sp_wrap_code[LINUX_SP_WRAP];
89	unsigned long*	cga;
90#endif
91	int error;
92
93#ifdef LINUX_SHIFT
94	/*
95	 * Seems that PowerPC Linux binaries expect argc to start on a 16 bytes
96	 * aligned address. And we need one more 16 byte shift if it was already
97	 * 16 bytes aligned,
98	 */
99	*stackp = (char *)(((unsigned long)*stackp - 1) & ~LINUX_SHIFT);
100#endif
101
102	if ((error = copyargs(pack, arginfo, stackp, argp)) != 0)
103		return error;
104
105#ifdef LINUX_SHIFT
106	/*
107	 * From Linux's arch/ppc/kernel/process.c:shove_aux_table(). GNU ld.so
108	 * expects the ELF auxiliary table to start on a 16 bytes boundary on
109	 * the PowerPC.
110	 */
111	*stackp = (char *)(((unsigned long)(*stackp) + LINUX_SHIFT)
112	    & ~LINUX_SHIFT);
113#endif
114
115	memset(ai, 0, sizeof(LinuxAuxInfo) * LINUX_ELF_AUX_ENTRIES);
116
117	a = ai;
118
119	/*
120	 * Push extra arguments on the stack needed by dynamically
121	 * linked binaries.
122	 */
123	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
124#ifdef LINUX_SP_WRAP
125		memset(linux_sp_wrap_code, 0, LINUX_SP_WRAP);
126		bcopy(&linux_sp_wrap_start, linux_sp_wrap_code,
127		    (unsigned long)(&linux_sp_wrap_end)
128		    - (unsigned long)(&linux_sp_wrap_start));
129		(unsigned long)cga = ((unsigned long)linux_sp_wrap_code)
130		    + ((unsigned long)(&linux_sp_wrap_entry))
131		    - ((unsigned long)(&linux_sp_wrap_start));
132		(*cga) = (unsigned long)(ap->arg_entry);
133#endif
134#if 1
135		/*
136		 * The exec_package doesn't have a proc pointer and it's not
137		 * exactly trivial to add one since the credentials are
138		 * changing. XXX Linux uses curproc's credentials.
139		 * Why can't we use them too?
140		 */
141		a->a_type = LINUX_AT_EGID;
142		a->a_v = p->p_ucred->cr_gid;
143		a++;
144
145		a->a_type = LINUX_AT_GID;
146		a->a_v = p->p_cred->p_rgid;
147		a++;
148
149		a->a_type = LINUX_AT_EUID;
150		a->a_v = p->p_ucred->cr_uid;
151		a++;
152
153		a->a_type = LINUX_AT_UID;
154		a->a_v = p->p_cred->p_ruid;
155		a++;
156#endif
157
158		a->a_type = AT_ENTRY;
159		a->a_v = ap->arg_entry;
160#ifdef LINUX_SP_WRAP
161		prog_entry = a;
162#endif
163		a++;
164
165		a->a_type = AT_FLAGS;
166		a->a_v = 0;
167		a++;
168
169		a->a_type = AT_BASE;
170		a->a_v = ap->arg_interp;
171		a++;
172
173		a->a_type = AT_PHNUM;
174		a->a_v = ap->arg_phnum;
175		a++;
176
177		a->a_type = AT_PHENT;
178		a->a_v = ap->arg_phentsize;
179		a++;
180
181		a->a_type = AT_PHDR;
182		a->a_v = ap->arg_phaddr;
183		a++;
184
185		a->a_type = LINUX_AT_CLKTCK;
186		a->a_v = LINUX_CLOCKS_PER_SEC;
187		a++;
188
189		a->a_type = AT_PAGESZ;
190		a->a_v = NBPG;
191		a++;
192
193		a->a_type = LINUX_AT_HWCAP;
194		a->a_v = LINUX_ELF_HWCAP;
195		a++;
196
197		free((char *)ap, M_TEMP);
198		pack->ep_emul_arg = NULL;
199	}
200
201	a->a_type = AT_NULL;
202	a->a_v = 0;
203	a++;
204
205	len = (a - ai) * sizeof(LinuxAuxInfo);
206
207#ifdef LINUX_SP_WRAP
208	if (prog_entry != NULL)
209		prog_entry->a_v = (unsigned long)(*stackp) + len;
210#endif
211
212	if ((error = copyout(ai, *stackp, len)) != 0)
213		return error;
214	*stackp += len;
215
216#ifdef LINUX_SP_WRAP
217	if (prog_entry != NULL) {
218		if ((error = copyout(linux_sp_wrap_code, *stackp,
219		    LINUX_SP_WRAP)) != 0)
220			return error;
221		*stackp += LINUX_SP_WRAP;
222	}
223#endif
224
225	return 0;
226}
227