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