xref: /netbsd-src/sys/compat/linux/arch/powerpc/linux_exec_powerpc.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /* $NetBSD: linux_exec_powerpc.c,v 1.22 2010/07/07 01:30:34 chs 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.22 2010/07/07 01:30:34 chs Exp $");
45 
46 #define ELFSIZE 32
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/proc.h>
53 #include <sys/exec.h>
54 #include <sys/exec_elf.h>
55 #include <sys/resourcevar.h>
56 #include <sys/kauth.h>
57 
58 #include <uvm/uvm_extern.h>
59 
60 #include <compat/linux/common/linux_exec.h>
61 
62 /*
63  * Alpha and PowerPC specific linux copyargs function.
64  */
65 int
66 ELFNAME2(linux,copyargs)(l, pack, arginfo, stackp, argp)
67 	struct lwp *l;
68 	struct exec_package *pack;
69 	struct ps_strings *arginfo;
70 	char **stackp;
71 	void *argp;
72 {
73 	size_t len;
74 	AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
75 	struct elf_args *ap;
76 	int error;
77 
78 #ifdef LINUX_SHIFT
79 	/*
80 	 * Seems that PowerPC Linux binaries expect argc to start on a 16 bytes
81 	 * aligned address. And we need one more 16 byte shift if it was already
82 	 * 16 bytes aligned,
83 	 */
84 	*stackp = (char *)(((unsigned long)*stackp - 1) & ~LINUX_SHIFT);
85 #endif
86 
87 	if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
88 		return error;
89 
90 #ifdef LINUX_SHIFT
91 	/*
92 	 * From Linux's arch/ppc/kernel/process.c:shove_aux_table(). GNU ld.so
93 	 * expects the ELF auxiliary table to start on a 16 bytes boundary on
94 	 * the PowerPC.
95 	 */
96 	*stackp = (char *)(((unsigned long)(*stackp) + LINUX_SHIFT)
97 	    & ~LINUX_SHIFT);
98 #endif
99 
100 	memset(ai, 0, sizeof(AuxInfo) * LINUX_ELF_AUX_ENTRIES);
101 
102 	a = ai;
103 
104 	/*
105 	 * Push extra arguments on the stack needed by dynamically
106 	 * linked binaries.
107 	 */
108 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
109 #if 1
110 		/*
111 		 * The exec_package doesn't have a proc pointer and it's not
112 		 * exactly trivial to add one since the credentials are
113 		 * changing. XXX Linux uses curlwp's credentials.
114 		 * Why can't we use them too? XXXad we do, what's different?
115 		 */
116 		a->a_type = LINUX_AT_EGID;
117 		a->a_v = kauth_cred_getegid(l->l_cred);
118 		a++;
119 
120 		a->a_type = LINUX_AT_GID;
121 		a->a_v = kauth_cred_getgid(l->l_cred);
122 		a++;
123 
124 		a->a_type = LINUX_AT_EUID;
125 		a->a_v = kauth_cred_geteuid(l->l_cred);
126 		a++;
127 
128 		a->a_type = LINUX_AT_UID;
129 		a->a_v = kauth_cred_getuid(l->l_cred);
130 		a++;
131 #endif
132 
133 		a->a_type = AT_ENTRY;
134 		a->a_v = ap->arg_entry;
135 		a++;
136 
137 		a->a_type = AT_FLAGS;
138 		a->a_v = 0;
139 		a++;
140 
141 		a->a_type = AT_BASE;
142 		a->a_v = ap->arg_interp;
143 		a++;
144 
145 		a->a_type = AT_PHNUM;
146 		a->a_v = ap->arg_phnum;
147 		a++;
148 
149 		a->a_type = AT_PHENT;
150 		a->a_v = ap->arg_phentsize;
151 		a++;
152 
153 		a->a_type = AT_PHDR;
154 		a->a_v = ap->arg_phaddr;
155 		a++;
156 
157 		a->a_type = LINUX_AT_CLKTCK;
158 		a->a_v = LINUX_CLOCKS_PER_SEC;
159 		a++;
160 
161 		a->a_type = AT_PAGESZ;
162 		a->a_v = PAGE_SIZE;
163 		a++;
164 
165 		a->a_type = LINUX_AT_HWCAP;
166 		a->a_v = LINUX_ELF_HWCAP;
167 		a++;
168 
169 		free((char *)ap, M_TEMP);
170 		pack->ep_emul_arg = NULL;
171 	}
172 
173 	a->a_type = AT_NULL;
174 	a->a_v = 0;
175 	a++;
176 
177 	len = (a - ai) * sizeof(AuxInfo);
178 
179 	if ((error = copyout(ai, *stackp, len)) != 0)
180 		return error;
181 	*stackp += len;
182 	return 0;
183 }
184