summaryrefslogtreecommitdiff
path: root/sys/compat/linux/arch/powerpc/linux_exec_powerpc.c
blob: d91503f2f0250ee3fb7a8604b8398dc0d98265a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* $NetBSD: linux_exec_powerpc.c,v 1.12 2003/06/29 22:29:25 fvdl Exp $ */

/*-
 * Copyright (c) 2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Emmanuel Dreyfus.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the NetBSD
 *      Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/* 
 * From NetBSD's sys/compat/arch/alpha/linux_exec_alpha.c, with some 
 * powerpc add-ons (ifdef LINUX_SHIFT and LINUX_SP_WRAP).
 * 
 * This code is to be common to alpha and powerpc. If it works on alpha, it 
 * should be moved to common/linux_exec_elf32.c. Beware that it needs 
 * LINUX_ELF_AUX_ENTRIES in arch/<arch>/linux_exec.h to also be moved to common
 * 
 * Emmanuel Dreyfus <p99dreyf@criens.u-psud.fr>
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux_exec_powerpc.c,v 1.12 2003/06/29 22:29:25 fvdl Exp $");

#if defined (__alpha__)
#define ELFSIZE 64
#elif defined (__powerpc__)
#define ELFSIZE 32
#else
#error Unified linux_elf_{32|64}copyargs not tested for this platform 
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/exec.h>
#include <sys/exec_elf.h>

#include <uvm/uvm_extern.h>

#include <compat/linux/common/linux_exec.h>

#ifdef LINUX_SP_WRAP
extern int linux_sp_wrap_start;
extern int linux_sp_wrap_end;
extern int linux_sp_wrap_entry;
#endif
/*
 * Alpha and PowerPC specific linux copyargs function.
 */
int
ELFNAME2(linux,copyargs)(p, pack, arginfo, stackp, argp)
	struct proc *p;
	struct exec_package *pack;
	struct ps_strings *arginfo;
	char **stackp;
	void *argp;
{
	size_t len;
	AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
	struct elf_args *ap;
#ifdef LINUX_SP_WRAP
	AuxInfo *prog_entry = NULL;
	char	linux_sp_wrap_code[LINUX_SP_WRAP];
	unsigned long*	cga;
#endif
	int error;

#ifdef LINUX_SHIFT
	/* 
	 * Seems that PowerPC Linux binaries expect argc to start on a 16 bytes
	 * aligned address. And we need one more 16 byte shift if it was already
	 * 16 bytes aligned,	
	 */
	*stackp = (char *)(((unsigned long)*stackp - 1) & ~LINUX_SHIFT);
#endif

	if ((error = copyargs(p, pack, arginfo, stackp, argp)) != 0)
		return error;

#ifdef LINUX_SHIFT
	/* 
	 * From Linux's arch/ppc/kernel/process.c:shove_aux_table(). GNU ld.so 
	 * expects the ELF auxiliary table to start on a 16 bytes boundary on
	 * the PowerPC.
	 */
	*stackp = (char *)(((unsigned long)(*stackp) + LINUX_SHIFT)
	    & ~LINUX_SHIFT);
#endif 

	memset(ai, 0, sizeof(AuxInfo) * LINUX_ELF_AUX_ENTRIES);

	a = ai;

	/*
	 * Push extra arguments on the stack needed by dynamically
	 * linked binaries.
	 */
	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
#ifdef LINUX_SP_WRAP
		memset(linux_sp_wrap_code, 0, LINUX_SP_WRAP);
		bcopy(&linux_sp_wrap_start, linux_sp_wrap_code, 
		    (unsigned long)(&linux_sp_wrap_end) 
		    - (unsigned long)(&linux_sp_wrap_start));
		(unsigned long)cga = ((unsigned long)linux_sp_wrap_code) 
		    + ((unsigned long)(&linux_sp_wrap_entry))
		    - ((unsigned long)(&linux_sp_wrap_start));	
		(*cga) = (unsigned long)(ap->arg_entry); 
#endif
#if 1
		/*
		 * The exec_package doesn't have a proc pointer and it's not
		 * exactly trivial to add one since the credentials are
		 * changing. XXX Linux uses curlwp's credentials.
		 * Why can't we use them too?
		 */
		a->a_type = LINUX_AT_EGID;
		a->a_v = p->p_ucred->cr_gid;
		a++;

		a->a_type = LINUX_AT_GID;
		a->a_v = p->p_cred->p_rgid;
		a++;

		a->a_type = LINUX_AT_EUID;
		a->a_v = p->p_ucred->cr_uid;
		a++;

		a->a_type = LINUX_AT_UID;
		a->a_v = p->p_cred->p_ruid;
		a++;
#endif

		a->a_type = AT_ENTRY;
		a->a_v = ap->arg_entry;
#ifdef LINUX_SP_WRAP
		prog_entry = a;
#endif
		a++;

		a->a_type = AT_FLAGS;
		a->a_v = 0;
		a++;

		a->a_type = AT_BASE;
		a->a_v = ap->arg_interp;
		a++;

		a->a_type = AT_PHNUM;
		a->a_v = ap->arg_phnum;
		a++;

		a->a_type = AT_PHENT;
		a->a_v = ap->arg_phentsize;
		a++;

		a->a_type = AT_PHDR;
		a->a_v = ap->arg_phaddr;
		a++;

		a->a_type = LINUX_AT_CLKTCK;
		a->a_v = LINUX_CLOCKS_PER_SEC;
		a++;

		a->a_type = AT_PAGESZ;
		a->a_v = PAGE_SIZE;
		a++;

		a->a_type = LINUX_AT_HWCAP;
		a->a_v = LINUX_ELF_HWCAP;
		a++;

		free((char *)ap, M_TEMP);
		pack->ep_emul_arg = NULL;
	}

	a->a_type = AT_NULL;
	a->a_v = 0;
	a++;

	len = (a - ai) * sizeof(AuxInfo);

#ifdef LINUX_SP_WRAP
	if (prog_entry != NULL) 
		prog_entry->a_v = (unsigned long)(*stackp) + len;
#endif

	if ((error = copyout(ai, *stackp, len)) != 0)
		return error;
	*stackp += len; 

#ifdef LINUX_SP_WRAP
	if (prog_entry != NULL) {
		if ((error = copyout(linux_sp_wrap_code, *stackp,
		    LINUX_SP_WRAP)) != 0)
			return error;
		*stackp += LINUX_SP_WRAP;
	}
#endif

	return 0;
}