linux_exec_powerpc.c revision 1.14
11.14Smanu/* $NetBSD: linux_exec_powerpc.c,v 1.14 2004/06/18 17:06:15 manu 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.6Slukem
501.6Slukem#include <sys/cdefs.h>
511.14Smanu__KERNEL_RCSID(0, "$NetBSD: linux_exec_powerpc.c,v 1.14 2004/06/18 17:06:15 manu Exp $");
521.6Slukem
531.1Smanu#if defined (__alpha__)
541.1Smanu#define ELFSIZE 64
551.1Smanu#elif defined (__powerpc__)
561.1Smanu#define ELFSIZE 32
571.1Smanu#else
581.1Smanu#error Unified linux_elf_{32|64}copyargs not tested for this platform
591.1Smanu#endif
601.1Smanu
611.1Smanu#include <sys/param.h>
621.1Smanu#include <sys/systm.h>
631.1Smanu#include <sys/kernel.h>
641.1Smanu#include <sys/malloc.h>
651.1Smanu#include <sys/proc.h>
661.1Smanu#include <sys/exec.h>
671.1Smanu#include <sys/exec_elf.h>
681.14Smanu#include <sys/resourcevar.h>
691.1Smanu
701.10Sthorpej#include <uvm/uvm_extern.h>
711.10Sthorpej
721.1Smanu#include <compat/linux/common/linux_exec.h>
731.1Smanu
741.1Smanu#ifdef LINUX_SP_WRAP
751.3Swizextern int linux_sp_wrap_start;
761.3Swizextern int linux_sp_wrap_end;
771.3Swizextern int linux_sp_wrap_entry;
781.1Smanu#endif
791.1Smanu/*
801.1Smanu * Alpha and PowerPC specific linux copyargs function.
811.1Smanu */
821.4Schristosint
831.12SfvdlELFNAME2(linux,copyargs)(p, pack, arginfo, stackp, argp)
841.12Sfvdl	struct proc *p;
851.1Smanu	struct exec_package *pack;
861.1Smanu	struct ps_strings *arginfo;
871.4Schristos	char **stackp;
881.1Smanu	void *argp;
891.1Smanu{
901.1Smanu	size_t len;
911.8Sjdolecek	AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
921.1Smanu	struct elf_args *ap;
931.1Smanu#ifdef LINUX_SP_WRAP
941.8Sjdolecek	AuxInfo *prog_entry = NULL;
951.1Smanu	char	linux_sp_wrap_code[LINUX_SP_WRAP];
961.1Smanu	unsigned long*	cga;
971.1Smanu#endif
981.4Schristos	int error;
991.1Smanu
1001.1Smanu#ifdef LINUX_SHIFT
1011.1Smanu	/*
1021.1Smanu	 * Seems that PowerPC Linux binaries expect argc to start on a 16 bytes
1031.1Smanu	 * aligned address. And we need one more 16 byte shift if it was already
1041.1Smanu	 * 16 bytes aligned,
1051.1Smanu	 */
1061.5Schristos	*stackp = (char *)(((unsigned long)*stackp - 1) & ~LINUX_SHIFT);
1071.1Smanu#endif
1081.1Smanu
1091.12Sfvdl	if ((error = copyargs(p, pack, arginfo, stackp, argp)) != 0)
1101.4Schristos		return error;
1111.1Smanu
1121.1Smanu#ifdef LINUX_SHIFT
1131.1Smanu	/*
1141.1Smanu	 * From Linux's arch/ppc/kernel/process.c:shove_aux_table(). GNU ld.so
1151.2Schristos	 * expects the ELF auxiliary table to start on a 16 bytes boundary on
1161.2Schristos	 * the PowerPC.
1171.1Smanu	 */
1181.4Schristos	*stackp = (char *)(((unsigned long)(*stackp) + LINUX_SHIFT)
1191.4Schristos	    & ~LINUX_SHIFT);
1201.1Smanu#endif
1211.1Smanu
1221.8Sjdolecek	memset(ai, 0, sizeof(AuxInfo) * LINUX_ELF_AUX_ENTRIES);
1231.1Smanu
1241.1Smanu	a = ai;
1251.1Smanu
1261.1Smanu	/*
1271.1Smanu	 * Push extra arguments on the stack needed by dynamically
1281.1Smanu	 * linked binaries.
1291.1Smanu	 */
1301.1Smanu	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1311.1Smanu#ifdef LINUX_SP_WRAP
1321.1Smanu		memset(linux_sp_wrap_code, 0, LINUX_SP_WRAP);
1331.1Smanu		bcopy(&linux_sp_wrap_start, linux_sp_wrap_code,
1341.1Smanu		    (unsigned long)(&linux_sp_wrap_end)
1351.1Smanu		    - (unsigned long)(&linux_sp_wrap_start));
1361.1Smanu		(unsigned long)cga = ((unsigned long)linux_sp_wrap_code)
1371.1Smanu		    + ((unsigned long)(&linux_sp_wrap_entry))
1381.1Smanu		    - ((unsigned long)(&linux_sp_wrap_start));
1391.1Smanu		(*cga) = (unsigned long)(ap->arg_entry);
1401.1Smanu#endif
1411.1Smanu#if 1
1421.1Smanu		/*
1431.1Smanu		 * The exec_package doesn't have a proc pointer and it's not
1441.2Schristos		 * exactly trivial to add one since the credentials are
1451.9Sthorpej		 * changing. XXX Linux uses curlwp's credentials.
1461.2Schristos		 * Why can't we use them too?
1471.1Smanu		 */
1481.1Smanu		a->a_type = LINUX_AT_EGID;
1491.1Smanu		a->a_v = p->p_ucred->cr_gid;
1501.1Smanu		a++;
1511.1Smanu
1521.1Smanu		a->a_type = LINUX_AT_GID;
1531.1Smanu		a->a_v = p->p_cred->p_rgid;
1541.1Smanu		a++;
1551.1Smanu
1561.1Smanu		a->a_type = LINUX_AT_EUID;
1571.1Smanu		a->a_v = p->p_ucred->cr_uid;
1581.1Smanu		a++;
1591.1Smanu
1601.1Smanu		a->a_type = LINUX_AT_UID;
1611.1Smanu		a->a_v = p->p_cred->p_ruid;
1621.1Smanu		a++;
1631.1Smanu#endif
1641.1Smanu
1651.1Smanu		a->a_type = AT_ENTRY;
1661.1Smanu		a->a_v = ap->arg_entry;
1671.1Smanu#ifdef LINUX_SP_WRAP
1681.1Smanu		prog_entry = a;
1691.1Smanu#endif
1701.1Smanu		a++;
1711.1Smanu
1721.1Smanu		a->a_type = AT_FLAGS;
1731.1Smanu		a->a_v = 0;
1741.1Smanu		a++;
1751.1Smanu
1761.1Smanu		a->a_type = AT_BASE;
1771.1Smanu		a->a_v = ap->arg_interp;
1781.1Smanu		a++;
1791.1Smanu
1801.1Smanu		a->a_type = AT_PHNUM;
1811.1Smanu		a->a_v = ap->arg_phnum;
1821.1Smanu		a++;
1831.1Smanu
1841.1Smanu		a->a_type = AT_PHENT;
1851.1Smanu		a->a_v = ap->arg_phentsize;
1861.1Smanu		a++;
1871.1Smanu
1881.1Smanu		a->a_type = AT_PHDR;
1891.1Smanu		a->a_v = ap->arg_phaddr;
1901.1Smanu		a++;
1911.1Smanu
1921.1Smanu		a->a_type = LINUX_AT_CLKTCK;
1931.1Smanu		a->a_v = LINUX_CLOCKS_PER_SEC;
1941.1Smanu		a++;
1951.1Smanu
1961.1Smanu		a->a_type = AT_PAGESZ;
1971.10Sthorpej		a->a_v = PAGE_SIZE;
1981.1Smanu		a++;
1991.1Smanu
2001.1Smanu		a->a_type = LINUX_AT_HWCAP;
2011.1Smanu		a->a_v = LINUX_ELF_HWCAP;
2021.1Smanu		a++;
2031.1Smanu
2041.1Smanu		free((char *)ap, M_TEMP);
2051.1Smanu		pack->ep_emul_arg = NULL;
2061.1Smanu	}
2071.1Smanu
2081.1Smanu	a->a_type = AT_NULL;
2091.1Smanu	a->a_v = 0;
2101.1Smanu	a++;
2111.1Smanu
2121.8Sjdolecek	len = (a - ai) * sizeof(AuxInfo);
2131.1Smanu
2141.1Smanu#ifdef LINUX_SP_WRAP
2151.1Smanu	if (prog_entry != NULL)
2161.4Schristos		prog_entry->a_v = (unsigned long)(*stackp) + len;
2171.1Smanu#endif
2181.1Smanu
2191.4Schristos	if ((error = copyout(ai, *stackp, len)) != 0)
2201.4Schristos		return error;
2211.4Schristos	*stackp += len;
2221.1Smanu
2231.1Smanu#ifdef LINUX_SP_WRAP
2241.1Smanu	if (prog_entry != NULL) {
2251.4Schristos		if ((error = copyout(linux_sp_wrap_code, *stackp,
2261.4Schristos		    LINUX_SP_WRAP)) != 0)
2271.4Schristos			return error;
2281.4Schristos		*stackp += LINUX_SP_WRAP;
2291.1Smanu	}
2301.1Smanu#endif
2311.1Smanu
2321.4Schristos	return 0;
2331.1Smanu}
2341.13Smanu
2351.13Smanu/*
2361.13Smanu * This is copied from sys/kern/exec_subr.c:exec_setup_stack()
2371.13Smanu * We need a Linux version only to avoid the non executable
2381.13Smanu * mappings. They will probably break signal delivery on Linux,
2391.13Smanu * and they surely break the stack fixup hack.
2401.13Smanu */
2411.13Smanuint
2421.13Smanulinux_exec_setup_stack(p, epp)
2431.13Smanu	struct proc *p;
2441.13Smanu	struct exec_package *epp;
2451.13Smanu{
2461.13Smanu	u_long max_stack_size;
2471.13Smanu	u_long access_linear_min, access_size;
2481.13Smanu	u_long noaccess_linear_min, noaccess_size;
2491.13Smanu
2501.13Smanu#ifndef	USRSTACK32
2511.13Smanu#define USRSTACK32	(0x00000000ffffffffL&~PGOFSET)
2521.13Smanu#endif
2531.13Smanu
2541.13Smanu	if (epp->ep_flags & EXEC_32) {
2551.13Smanu		epp->ep_minsaddr = USRSTACK32;
2561.13Smanu		max_stack_size = MAXSSIZ;
2571.13Smanu	} else {
2581.13Smanu		epp->ep_minsaddr = USRSTACK;
2591.13Smanu		max_stack_size = MAXSSIZ;
2601.13Smanu	}
2611.13Smanu	epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
2621.13Smanu		max_stack_size);
2631.13Smanu	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
2641.13Smanu
2651.13Smanu	/*
2661.13Smanu	 * set up commands for stack.  note that this takes *two*, one to
2671.13Smanu	 * map the part of the stack which we can access, and one to map
2681.13Smanu	 * the part which we can't.
2691.13Smanu	 *
2701.13Smanu	 * arguably, it could be made into one, but that would require the
2711.13Smanu	 * addition of another mapping proc, which is unnecessary
2721.13Smanu	 */
2731.13Smanu	access_size = epp->ep_ssize;
2741.13Smanu	access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
2751.13Smanu	noaccess_size = max_stack_size - access_size;
2761.13Smanu	noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
2771.13Smanu	    access_size), noaccess_size);
2781.13Smanu	if (noaccess_size > 0) {
2791.13Smanu		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
2801.13Smanu		    noaccess_linear_min, NULL, 0, VM_PROT_NONE);
2811.13Smanu	}
2821.13Smanu	KASSERT(access_size > 0);
2831.13Smanu	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
2841.13Smanu	    access_linear_min, NULL, 0,
2851.13Smanu	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
2861.13Smanu
2871.13Smanu	return 0;
2881.13Smanu}
289