linux_exec_powerpc.c revision 1.4
11.4Schristos/* $NetBSD: linux_exec_powerpc.c,v 1.4 2001/07/29 21:28:45 christos Exp $ */
21.1Smanu
31.1Smanu/*-
41.1Smanu * Copyright (c) 2001 The NetBSD Foundation, Inc.
51.1Smanu * All rights reserved.
61.1Smanu *
71.1Smanu * This code is derived from software contributed to The NetBSD Foundation
81.1Smanu * by Emmanuel Dreyfus.
91.1Smanu *
101.1Smanu * Redistribution and use in source and binary forms, with or without
111.1Smanu * modification, are permitted provided that the following conditions
121.1Smanu * are met:
131.1Smanu * 1. Redistributions of source code must retain the above copyright
141.1Smanu *    notice, this list of conditions and the following disclaimer.
151.1Smanu * 2. Redistributions in binary form must reproduce the above copyright
161.1Smanu *    notice, this list of conditions and the following disclaimer in the
171.1Smanu *    documentation and/or other materials provided with the distribution.
181.1Smanu * 3. All advertising materials mentioning features or use of this software
191.1Smanu *    must display the following acknowledgement:
201.1Smanu *      This product includes software developed by the NetBSD
211.1Smanu *      Foundation, Inc. and its contributors.
221.1Smanu * 4. Neither the name of The NetBSD Foundation nor the names of its
231.1Smanu *    contributors may be used to endorse or promote products derived
241.1Smanu *    from this software without specific prior written permission.
251.1Smanu *
261.1Smanu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.1Smanu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.1Smanu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.1Smanu * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.1Smanu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.1Smanu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.1Smanu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.1Smanu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.1Smanu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.1Smanu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.1Smanu * POSSIBILITY OF SUCH DAMAGE.
371.1Smanu */
381.1Smanu
391.1Smanu/*
401.1Smanu * From NetBSD's sys/compat/arch/alpha/linux_exec_alpha.c, with some
411.1Smanu * powerpc add-ons (ifdef LINUX_SHIFT and LINUX_SP_WRAP).
421.1Smanu *
431.1Smanu * This code is to be common to alpha and powerpc. If it works on alpha, it
441.1Smanu * should be moved to common/linux_exec_elf32.c. Beware that it needs
451.1Smanu * LINUX_ELF_AUX_ENTRIES in arch/<arch>/linux_exec.h to also be moved to common
461.1Smanu *
471.1Smanu * Emmanuel Dreyfus <p99dreyf@criens.u-psud.fr>
481.1Smanu */
491.1Smanu#if defined (__alpha__)
501.1Smanu#define ELFSIZE 64
511.1Smanu#elif defined (__powerpc__)
521.1Smanu#define ELFSIZE 32
531.1Smanu#else
541.1Smanu#error Unified linux_elf_{32|64}copyargs not tested for this platform
551.1Smanu#endif
561.1Smanu
571.1Smanu#include <sys/param.h>
581.1Smanu#include <sys/systm.h>
591.1Smanu#include <sys/kernel.h>
601.1Smanu#include <sys/malloc.h>
611.1Smanu#include <sys/proc.h>
621.1Smanu#include <sys/exec.h>
631.1Smanu#include <sys/exec_elf.h>
641.1Smanu
651.1Smanu#include <compat/linux/common/linux_exec.h>
661.1Smanu
671.1Smanu#ifdef LINUX_SP_WRAP
681.3Swizextern int linux_sp_wrap_start;
691.3Swizextern int linux_sp_wrap_end;
701.3Swizextern int linux_sp_wrap_entry;
711.1Smanu#endif
721.1Smanu/*
731.1Smanu * Alpha and PowerPC specific linux copyargs function.
741.1Smanu */
751.4Schristosint
761.4SchristosELFNAME2(linux,copyargs)(pack, arginfo, stackp, argp)
771.1Smanu	struct exec_package *pack;
781.1Smanu	struct ps_strings *arginfo;
791.4Schristos	char **stackp;
801.1Smanu	void *argp;
811.1Smanu{
821.1Smanu	size_t len;
831.1Smanu	LinuxAuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
841.1Smanu	struct elf_args *ap;
851.1Smanu	struct proc *p = curproc;
861.1Smanu#ifdef LINUX_SP_WRAP
871.1Smanu	LinuxAuxInfo *prog_entry = NULL;
881.1Smanu	char	linux_sp_wrap_code[LINUX_SP_WRAP];
891.1Smanu	unsigned long*	cga;
901.1Smanu#endif
911.4Schristos	int error;
921.1Smanu
931.1Smanu#ifdef LINUX_SHIFT
941.1Smanu	/*
951.1Smanu	 * Seems that PowerPC Linux binaries expect argc to start on a 16 bytes
961.1Smanu	 * aligned address. And we need one more 16 byte shift if it was already
971.1Smanu	 * 16 bytes aligned,
981.1Smanu	 */
991.4Schristos	*stackp = (char *)((unsigned long)*stackp - 1) & ~LINUX_SHIFT;
1001.1Smanu#endif
1011.1Smanu
1021.4Schristos	if ((error = copyargs(pack, arginfo, stackp, argp)) != 0)
1031.4Schristos		return error;
1041.1Smanu
1051.1Smanu#ifdef LINUX_SHIFT
1061.1Smanu	/*
1071.1Smanu	 * From Linux's arch/ppc/kernel/process.c:shove_aux_table(). GNU ld.so
1081.2Schristos	 * expects the ELF auxiliary table to start on a 16 bytes boundary on
1091.2Schristos	 * the PowerPC.
1101.1Smanu	 */
1111.4Schristos	*stackp = (char *)(((unsigned long)(*stackp) + LINUX_SHIFT)
1121.4Schristos	    & ~LINUX_SHIFT);
1131.1Smanu#endif
1141.1Smanu
1151.1Smanu	memset(ai, 0, sizeof(LinuxAuxInfo) * LINUX_ELF_AUX_ENTRIES);
1161.1Smanu
1171.1Smanu	a = ai;
1181.1Smanu
1191.1Smanu	/*
1201.1Smanu	 * Push extra arguments on the stack needed by dynamically
1211.1Smanu	 * linked binaries.
1221.1Smanu	 */
1231.1Smanu	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1241.1Smanu#ifdef LINUX_SP_WRAP
1251.1Smanu		memset(linux_sp_wrap_code, 0, LINUX_SP_WRAP);
1261.1Smanu		bcopy(&linux_sp_wrap_start, linux_sp_wrap_code,
1271.1Smanu		    (unsigned long)(&linux_sp_wrap_end)
1281.1Smanu		    - (unsigned long)(&linux_sp_wrap_start));
1291.1Smanu		(unsigned long)cga = ((unsigned long)linux_sp_wrap_code)
1301.1Smanu		    + ((unsigned long)(&linux_sp_wrap_entry))
1311.1Smanu		    - ((unsigned long)(&linux_sp_wrap_start));
1321.1Smanu		(*cga) = (unsigned long)(ap->arg_entry);
1331.1Smanu#endif
1341.1Smanu#if 1
1351.1Smanu		/*
1361.1Smanu		 * The exec_package doesn't have a proc pointer and it's not
1371.2Schristos		 * exactly trivial to add one since the credentials are
1381.2Schristos		 * changing. XXX Linux uses curproc's credentials.
1391.2Schristos		 * Why can't we use them too?
1401.1Smanu		 */
1411.1Smanu		a->a_type = LINUX_AT_EGID;
1421.1Smanu		a->a_v = p->p_ucred->cr_gid;
1431.1Smanu		a++;
1441.1Smanu
1451.1Smanu		a->a_type = LINUX_AT_GID;
1461.1Smanu		a->a_v = p->p_cred->p_rgid;
1471.1Smanu		a++;
1481.1Smanu
1491.1Smanu		a->a_type = LINUX_AT_EUID;
1501.1Smanu		a->a_v = p->p_ucred->cr_uid;
1511.1Smanu		a++;
1521.1Smanu
1531.1Smanu		a->a_type = LINUX_AT_UID;
1541.1Smanu		a->a_v = p->p_cred->p_ruid;
1551.1Smanu		a++;
1561.1Smanu#endif
1571.1Smanu
1581.1Smanu		a->a_type = AT_ENTRY;
1591.1Smanu		a->a_v = ap->arg_entry;
1601.1Smanu#ifdef LINUX_SP_WRAP
1611.1Smanu		prog_entry = a;
1621.1Smanu#endif
1631.1Smanu		a++;
1641.1Smanu
1651.1Smanu		a->a_type = AT_FLAGS;
1661.1Smanu		a->a_v = 0;
1671.1Smanu		a++;
1681.1Smanu
1691.1Smanu		a->a_type = AT_BASE;
1701.1Smanu		a->a_v = ap->arg_interp;
1711.1Smanu		a++;
1721.1Smanu
1731.1Smanu		a->a_type = AT_PHNUM;
1741.1Smanu		a->a_v = ap->arg_phnum;
1751.1Smanu		a++;
1761.1Smanu
1771.1Smanu		a->a_type = AT_PHENT;
1781.1Smanu		a->a_v = ap->arg_phentsize;
1791.1Smanu		a++;
1801.1Smanu
1811.1Smanu		a->a_type = AT_PHDR;
1821.1Smanu		a->a_v = ap->arg_phaddr;
1831.1Smanu		a++;
1841.1Smanu
1851.1Smanu		a->a_type = LINUX_AT_CLKTCK;
1861.1Smanu		a->a_v = LINUX_CLOCKS_PER_SEC;
1871.1Smanu		a++;
1881.1Smanu
1891.1Smanu		a->a_type = AT_PAGESZ;
1901.1Smanu		a->a_v = NBPG;
1911.1Smanu		a++;
1921.1Smanu
1931.1Smanu		a->a_type = LINUX_AT_HWCAP;
1941.1Smanu		a->a_v = LINUX_ELF_HWCAP;
1951.1Smanu		a++;
1961.1Smanu
1971.1Smanu		free((char *)ap, M_TEMP);
1981.1Smanu		pack->ep_emul_arg = NULL;
1991.1Smanu	}
2001.1Smanu
2011.1Smanu	a->a_type = AT_NULL;
2021.1Smanu	a->a_v = 0;
2031.1Smanu	a++;
2041.1Smanu
2051.1Smanu	len = (a - ai) * sizeof(LinuxAuxInfo);
2061.1Smanu
2071.1Smanu#ifdef LINUX_SP_WRAP
2081.1Smanu	if (prog_entry != NULL)
2091.4Schristos		prog_entry->a_v = (unsigned long)(*stackp) + len;
2101.1Smanu#endif
2111.1Smanu
2121.4Schristos	if ((error = copyout(ai, *stackp, len)) != 0)
2131.4Schristos		return error;
2141.4Schristos	*stackp += len;
2151.1Smanu
2161.1Smanu#ifdef LINUX_SP_WRAP
2171.1Smanu	if (prog_entry != NULL) {
2181.4Schristos		if ((error = copyout(linux_sp_wrap_code, *stackp,
2191.4Schristos		    LINUX_SP_WRAP)) != 0)
2201.4Schristos			return error;
2211.4Schristos		*stackp += LINUX_SP_WRAP;
2221.1Smanu	}
2231.1Smanu#endif
2241.1Smanu
2251.4Schristos	return 0;
2261.1Smanu}
227