xref: /netbsd-src/sys/compat/linux/arch/powerpc/linux_exec_powerpc.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /* $NetBSD: linux_exec_powerpc.c,v 1.21 2008/04/28 20:23:43 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * From NetBSD's sys/compat/arch/alpha/linux_exec_alpha.c, with some
34  * powerpc add-ons (ifdef LINUX_SHIFT).
35  *
36  * This code is to be common to alpha and powerpc. If it works on alpha, it
37  * should be moved to common/linux_exec_elf32.c. Beware that it needs
38  * LINUX_ELF_AUX_ENTRIES in arch/<arch>/linux_exec.h to also be moved to common
39  *
40  * Emmanuel Dreyfus <p99dreyf@criens.u-psud.fr>
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: linux_exec_powerpc.c,v 1.21 2008/04/28 20:23:43 martin Exp $");
45 
46 #if defined (__alpha__)
47 #define ELFSIZE 64
48 #elif defined (__powerpc__)
49 #define ELFSIZE 32
50 #else
51 #error Unified linux_elf_{32|64}copyargs not tested for this platform
52 #endif
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/proc.h>
59 #include <sys/exec.h>
60 #include <sys/exec_elf.h>
61 #include <sys/resourcevar.h>
62 #include <sys/kauth.h>
63 
64 #include <uvm/uvm_extern.h>
65 
66 #include <compat/linux/common/linux_exec.h>
67 
68 /*
69  * Alpha and PowerPC specific linux copyargs function.
70  */
71 int
72 ELFNAME2(linux,copyargs)(l, pack, arginfo, stackp, argp)
73 	struct lwp *l;
74 	struct exec_package *pack;
75 	struct ps_strings *arginfo;
76 	char **stackp;
77 	void *argp;
78 {
79 	size_t len;
80 	AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
81 	struct elf_args *ap;
82 	int error;
83 
84 #ifdef LINUX_SHIFT
85 	/*
86 	 * Seems that PowerPC Linux binaries expect argc to start on a 16 bytes
87 	 * aligned address. And we need one more 16 byte shift if it was already
88 	 * 16 bytes aligned,
89 	 */
90 	*stackp = (char *)(((unsigned long)*stackp - 1) & ~LINUX_SHIFT);
91 #endif
92 
93 	if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
94 		return error;
95 
96 #ifdef LINUX_SHIFT
97 	/*
98 	 * From Linux's arch/ppc/kernel/process.c:shove_aux_table(). GNU ld.so
99 	 * expects the ELF auxiliary table to start on a 16 bytes boundary on
100 	 * the PowerPC.
101 	 */
102 	*stackp = (char *)(((unsigned long)(*stackp) + LINUX_SHIFT)
103 	    & ~LINUX_SHIFT);
104 #endif
105 
106 	memset(ai, 0, sizeof(AuxInfo) * LINUX_ELF_AUX_ENTRIES);
107 
108 	a = ai;
109 
110 	/*
111 	 * Push extra arguments on the stack needed by dynamically
112 	 * linked binaries.
113 	 */
114 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
115 #if 1
116 		/*
117 		 * The exec_package doesn't have a proc pointer and it's not
118 		 * exactly trivial to add one since the credentials are
119 		 * changing. XXX Linux uses curlwp's credentials.
120 		 * Why can't we use them too? XXXad we do, what's different?
121 		 */
122 		a->a_type = LINUX_AT_EGID;
123 		a->a_v = kauth_cred_getegid(l->l_cred);
124 		a++;
125 
126 		a->a_type = LINUX_AT_GID;
127 		a->a_v = kauth_cred_getgid(l->l_cred);
128 		a++;
129 
130 		a->a_type = LINUX_AT_EUID;
131 		a->a_v = kauth_cred_geteuid(l->l_cred);
132 		a++;
133 
134 		a->a_type = LINUX_AT_UID;
135 		a->a_v = kauth_cred_getuid(l->l_cred);
136 		a++;
137 #endif
138 
139 		a->a_type = AT_ENTRY;
140 		a->a_v = ap->arg_entry;
141 		a++;
142 
143 		a->a_type = AT_FLAGS;
144 		a->a_v = 0;
145 		a++;
146 
147 		a->a_type = AT_BASE;
148 		a->a_v = ap->arg_interp;
149 		a++;
150 
151 		a->a_type = AT_PHNUM;
152 		a->a_v = ap->arg_phnum;
153 		a++;
154 
155 		a->a_type = AT_PHENT;
156 		a->a_v = ap->arg_phentsize;
157 		a++;
158 
159 		a->a_type = AT_PHDR;
160 		a->a_v = ap->arg_phaddr;
161 		a++;
162 
163 		a->a_type = LINUX_AT_CLKTCK;
164 		a->a_v = LINUX_CLOCKS_PER_SEC;
165 		a++;
166 
167 		a->a_type = AT_PAGESZ;
168 		a->a_v = PAGE_SIZE;
169 		a++;
170 
171 		a->a_type = LINUX_AT_HWCAP;
172 		a->a_v = LINUX_ELF_HWCAP;
173 		a++;
174 
175 		free((char *)ap, M_TEMP);
176 		pack->ep_emul_arg = NULL;
177 	}
178 
179 	a->a_type = AT_NULL;
180 	a->a_v = 0;
181 	a++;
182 
183 	len = (a - ai) * sizeof(AuxInfo);
184 
185 	if ((error = copyout(ai, *stackp, len)) != 0)
186 		return error;
187 	*stackp += len;
188 	return 0;
189 }
190