xref: /netbsd-src/sys/compat/linux/arch/powerpc/linux_exec_powerpc.c (revision 17dd36da8292193180754d5047c0926dbb56818c)
1 /* $NetBSD: linux_exec_powerpc.c,v 1.2 2001/02/04 22:59:26 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
68 extern linux_sp_wrap_start;
69 extern linux_sp_wrap_end;
70 extern linux_sp_wrap_entry;
71 #endif
72 /*
73  * Alpha and PowerPC specific linux copyargs function.
74  */
75 void *
76 ELFNAME2(linux,copyargs)(pack, arginfo, stack, argp)
77 	struct exec_package *pack;
78 	struct ps_strings *arginfo;
79 	void *stack;
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 
92 #ifdef LINUX_SHIFT
93 	/*
94 	 * Seems that PowerPC Linux binaries expect argc to start on a 16 bytes
95 	 * aligned address. And we need one more 16 byte shift if it was already
96 	 * 16 bytes aligned,
97 	 */
98 	(unsigned long)stack = ((unsigned long)stack - 1) & ~LINUX_SHIFT;
99 #endif
100 
101 	stack = copyargs(pack, arginfo, stack, argp);
102 	if (!stack)
103 		return(NULL);
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 	stack = (void *)(((unsigned long) stack + LINUX_SHIFT) & ~LINUX_SHIFT);
112 #endif
113 
114 	memset(ai, 0, sizeof(LinuxAuxInfo) * LINUX_ELF_AUX_ENTRIES);
115 
116 	a = ai;
117 
118 	/*
119 	 * Push extra arguments on the stack needed by dynamically
120 	 * linked binaries.
121 	 */
122 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
123 #ifdef LINUX_SP_WRAP
124 		memset(linux_sp_wrap_code, 0, LINUX_SP_WRAP);
125 		bcopy(&linux_sp_wrap_start, linux_sp_wrap_code,
126 		    (unsigned long)(&linux_sp_wrap_end)
127 		    - (unsigned long)(&linux_sp_wrap_start));
128 		(unsigned long)cga = ((unsigned long)linux_sp_wrap_code)
129 		    + ((unsigned long)(&linux_sp_wrap_entry))
130 		    - ((unsigned long)(&linux_sp_wrap_start));
131 		(*cga) = (unsigned long)(ap->arg_entry);
132 #endif
133 #if 1
134 		/*
135 		 * The exec_package doesn't have a proc pointer and it's not
136 		 * exactly trivial to add one since the credentials are
137 		 * changing. XXX Linux uses curproc's credentials.
138 		 * Why can't we use them too?
139 		 */
140 		a->a_type = LINUX_AT_EGID;
141 		a->a_v = p->p_ucred->cr_gid;
142 		a++;
143 
144 		a->a_type = LINUX_AT_GID;
145 		a->a_v = p->p_cred->p_rgid;
146 		a++;
147 
148 		a->a_type = LINUX_AT_EUID;
149 		a->a_v = p->p_ucred->cr_uid;
150 		a++;
151 
152 		a->a_type = LINUX_AT_UID;
153 		a->a_v = p->p_cred->p_ruid;
154 		a++;
155 #endif
156 
157 		a->a_type = AT_ENTRY;
158 		a->a_v = ap->arg_entry;
159 #ifdef LINUX_SP_WRAP
160 		prog_entry = a;
161 #endif
162 		a++;
163 
164 		a->a_type = AT_FLAGS;
165 		a->a_v = 0;
166 		a++;
167 
168 		a->a_type = AT_BASE;
169 		a->a_v = ap->arg_interp;
170 		a++;
171 
172 		a->a_type = AT_PHNUM;
173 		a->a_v = ap->arg_phnum;
174 		a++;
175 
176 		a->a_type = AT_PHENT;
177 		a->a_v = ap->arg_phentsize;
178 		a++;
179 
180 		a->a_type = AT_PHDR;
181 		a->a_v = ap->arg_phaddr;
182 		a++;
183 
184 		a->a_type = LINUX_AT_CLKTCK;
185 		a->a_v = LINUX_CLOCKS_PER_SEC;
186 		a++;
187 
188 		a->a_type = AT_PAGESZ;
189 		a->a_v = NBPG;
190 		a++;
191 
192 		a->a_type = LINUX_AT_HWCAP;
193 		a->a_v = LINUX_ELF_HWCAP;
194 		a++;
195 
196 		free((char *)ap, M_TEMP);
197 		pack->ep_emul_arg = NULL;
198 	}
199 
200 	a->a_type = AT_NULL;
201 	a->a_v = 0;
202 	a++;
203 
204 	len = (a - ai) * sizeof(LinuxAuxInfo);
205 
206 #ifdef LINUX_SP_WRAP
207 	if (prog_entry != NULL)
208 		prog_entry->a_v = (unsigned long)stack + len;
209 #endif
210 
211 	if (copyout(ai, stack, len))
212 		return NULL;
213 	stack = (caddr_t)stack + len;
214 
215 #ifdef LINUX_SP_WRAP
216 	if (prog_entry != NULL) {
217 		if (copyout(linux_sp_wrap_code, stack, LINUX_SP_WRAP))
218 			return NULL;
219 		stack = (caddr_t)stack + LINUX_SP_WRAP;
220 	}
221 #endif
222 
223 	return(stack);
224 }
225