1 /* $NetBSD: linux_exec_powerpc.c,v 1.17 2005/12/11 12:20:16 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). 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.17 2005/12/11 12:20:16 christos 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 #include <sys/resourcevar.h> 69 70 #include <uvm/uvm_extern.h> 71 72 #include <compat/linux/common/linux_exec.h> 73 74 /* 75 * Alpha and PowerPC specific linux copyargs function. 76 */ 77 int 78 ELFNAME2(linux,copyargs)(l, pack, arginfo, stackp, argp) 79 struct lwp *l; 80 struct exec_package *pack; 81 struct ps_strings *arginfo; 82 char **stackp; 83 void *argp; 84 { 85 size_t len; 86 AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a; 87 struct elf_args *ap; 88 struct proc *p; 89 int error; 90 91 p = l->l_proc; 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 *stackp = (char *)(((unsigned long)*stackp - 1) & ~LINUX_SHIFT); 99 #endif 100 101 if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0) 102 return error; 103 104 #ifdef LINUX_SHIFT 105 /* 106 * From Linux's arch/ppc/kernel/process.c:shove_aux_table(). GNU ld.so 107 * expects the ELF auxiliary table to start on a 16 bytes boundary on 108 * the PowerPC. 109 */ 110 *stackp = (char *)(((unsigned long)(*stackp) + LINUX_SHIFT) 111 & ~LINUX_SHIFT); 112 #endif 113 114 memset(ai, 0, sizeof(AuxInfo) * 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 #if 1 124 /* 125 * The exec_package doesn't have a proc pointer and it's not 126 * exactly trivial to add one since the credentials are 127 * changing. XXX Linux uses curlwp's credentials. 128 * Why can't we use them too? 129 */ 130 a->a_type = LINUX_AT_EGID; 131 a->a_v = p->p_ucred->cr_gid; 132 a++; 133 134 a->a_type = LINUX_AT_GID; 135 a->a_v = p->p_cred->p_rgid; 136 a++; 137 138 a->a_type = LINUX_AT_EUID; 139 a->a_v = p->p_ucred->cr_uid; 140 a++; 141 142 a->a_type = LINUX_AT_UID; 143 a->a_v = p->p_cred->p_ruid; 144 a++; 145 #endif 146 147 a->a_type = AT_ENTRY; 148 a->a_v = ap->arg_entry; 149 a++; 150 151 a->a_type = AT_FLAGS; 152 a->a_v = 0; 153 a++; 154 155 a->a_type = AT_BASE; 156 a->a_v = ap->arg_interp; 157 a++; 158 159 a->a_type = AT_PHNUM; 160 a->a_v = ap->arg_phnum; 161 a++; 162 163 a->a_type = AT_PHENT; 164 a->a_v = ap->arg_phentsize; 165 a++; 166 167 a->a_type = AT_PHDR; 168 a->a_v = ap->arg_phaddr; 169 a++; 170 171 a->a_type = LINUX_AT_CLKTCK; 172 a->a_v = LINUX_CLOCKS_PER_SEC; 173 a++; 174 175 a->a_type = AT_PAGESZ; 176 a->a_v = PAGE_SIZE; 177 a++; 178 179 a->a_type = LINUX_AT_HWCAP; 180 a->a_v = LINUX_ELF_HWCAP; 181 a++; 182 183 free((char *)ap, M_TEMP); 184 pack->ep_emul_arg = NULL; 185 } 186 187 a->a_type = AT_NULL; 188 a->a_v = 0; 189 a++; 190 191 len = (a - ai) * sizeof(AuxInfo); 192 193 if ((error = copyout(ai, *stackp, len)) != 0) 194 return error; 195 *stackp += len; 196 return 0; 197 } 198