11.25Sriastrad/* $NetBSD: linux_exec_powerpc.c,v 1.25 2021/09/07 11:43:04 riastradh 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 *
191.1Smanu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Smanu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Smanu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Smanu * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Smanu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Smanu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Smanu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Smanu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Smanu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Smanu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Smanu * POSSIBILITY OF SUCH DAMAGE.
301.1Smanu */
311.1Smanu
321.16Sperry/*
331.16Sperry * From NetBSD's sys/compat/arch/alpha/linux_exec_alpha.c, with some
341.15Schs * powerpc add-ons (ifdef LINUX_SHIFT).
351.16Sperry *
361.16Sperry * This code is to be common to alpha and powerpc. If it works on alpha, it
371.16Sperry * should be moved to common/linux_exec_elf32.c. Beware that it needs
381.1Smanu * LINUX_ELF_AUX_ENTRIES in arch/<arch>/linux_exec.h to also be moved to common
391.16Sperry *
401.1Smanu * Emmanuel Dreyfus <p99dreyf@criens.u-psud.fr>
411.1Smanu */
421.6Slukem
431.6Slukem#include <sys/cdefs.h>
441.25Sriastrad__KERNEL_RCSID(0, "$NetBSD: linux_exec_powerpc.c,v 1.25 2021/09/07 11:43:04 riastradh Exp $");
451.6Slukem
461.1Smanu#define ELFSIZE 32
471.1Smanu
481.1Smanu#include <sys/param.h>
491.1Smanu#include <sys/systm.h>
501.1Smanu#include <sys/kernel.h>
511.1Smanu#include <sys/proc.h>
521.1Smanu#include <sys/exec.h>
531.1Smanu#include <sys/exec_elf.h>
541.14Smanu#include <sys/resourcevar.h>
551.19Syamt#include <sys/kauth.h>
561.1Smanu
571.10Sthorpej#include <uvm/uvm_extern.h>
581.10Sthorpej
591.1Smanu#include <compat/linux/common/linux_exec.h>
601.1Smanu
611.1Smanu/*
621.1Smanu * Alpha and PowerPC specific linux copyargs function.
631.1Smanu */
641.4Schristosint
651.24SrjsELFNAME2(linux,copyargs)(struct lwp *l, struct exec_package *pack,
661.24Srjs			 struct ps_strings *arginfo, char **stackp,
671.24Srjs			 void *argp)
681.1Smanu{
691.1Smanu	size_t len;
701.8Sjdolecek	AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
711.1Smanu	struct elf_args *ap;
721.4Schristos	int error;
731.1Smanu
741.1Smanu#ifdef LINUX_SHIFT
751.16Sperry	/*
761.1Smanu	 * Seems that PowerPC Linux binaries expect argc to start on a 16 bytes
771.1Smanu	 * aligned address. And we need one more 16 byte shift if it was already
781.16Sperry	 * 16 bytes aligned,
791.1Smanu	 */
801.5Schristos	*stackp = (char *)(((unsigned long)*stackp - 1) & ~LINUX_SHIFT);
811.1Smanu#endif
821.1Smanu
831.17Schristos	if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
841.4Schristos		return error;
851.1Smanu
861.1Smanu#ifdef LINUX_SHIFT
871.16Sperry	/*
881.16Sperry	 * From Linux's arch/ppc/kernel/process.c:shove_aux_table(). GNU ld.so
891.2Schristos	 * expects the ELF auxiliary table to start on a 16 bytes boundary on
901.2Schristos	 * the PowerPC.
911.1Smanu	 */
921.4Schristos	*stackp = (char *)(((unsigned long)(*stackp) + LINUX_SHIFT)
931.4Schristos	    & ~LINUX_SHIFT);
941.16Sperry#endif
951.1Smanu
961.25Sriastrad	memset(ai, 0, sizeof(ai));
971.1Smanu
981.1Smanu	a = ai;
991.1Smanu
1001.1Smanu	/*
1011.1Smanu	 * Push extra arguments on the stack needed by dynamically
1021.1Smanu	 * linked binaries.
1031.1Smanu	 */
1041.1Smanu	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1051.1Smanu#if 1
1061.1Smanu		/*
1071.1Smanu		 * The exec_package doesn't have a proc pointer and it's not
1081.2Schristos		 * exactly trivial to add one since the credentials are
1091.9Sthorpej		 * changing. XXX Linux uses curlwp's credentials.
1101.20Sad		 * Why can't we use them too? XXXad we do, what's different?
1111.1Smanu		 */
1121.1Smanu		a->a_type = LINUX_AT_EGID;
1131.20Sad		a->a_v = kauth_cred_getegid(l->l_cred);
1141.1Smanu		a++;
1151.1Smanu
1161.1Smanu		a->a_type = LINUX_AT_GID;
1171.20Sad		a->a_v = kauth_cred_getgid(l->l_cred);
1181.1Smanu		a++;
1191.1Smanu
1201.1Smanu		a->a_type = LINUX_AT_EUID;
1211.20Sad		a->a_v = kauth_cred_geteuid(l->l_cred);
1221.1Smanu		a++;
1231.1Smanu
1241.1Smanu		a->a_type = LINUX_AT_UID;
1251.20Sad		a->a_v = kauth_cred_getuid(l->l_cred);
1261.1Smanu		a++;
1271.1Smanu#endif
1281.1Smanu
1291.1Smanu		a->a_type = AT_ENTRY;
1301.1Smanu		a->a_v = ap->arg_entry;
1311.1Smanu		a++;
1321.1Smanu
1331.1Smanu		a->a_type = AT_FLAGS;
1341.1Smanu		a->a_v = 0;
1351.1Smanu		a++;
1361.1Smanu
1371.1Smanu		a->a_type = AT_BASE;
1381.1Smanu		a->a_v = ap->arg_interp;
1391.1Smanu		a++;
1401.1Smanu
1411.1Smanu		a->a_type = AT_PHNUM;
1421.1Smanu		a->a_v = ap->arg_phnum;
1431.1Smanu		a++;
1441.1Smanu
1451.1Smanu		a->a_type = AT_PHENT;
1461.1Smanu		a->a_v = ap->arg_phentsize;
1471.1Smanu		a++;
1481.1Smanu
1491.1Smanu		a->a_type = AT_PHDR;
1501.1Smanu		a->a_v = ap->arg_phaddr;
1511.1Smanu		a++;
1521.1Smanu
1531.1Smanu		a->a_type = LINUX_AT_CLKTCK;
1541.1Smanu		a->a_v = LINUX_CLOCKS_PER_SEC;
1551.1Smanu		a++;
1561.1Smanu
1571.1Smanu		a->a_type = AT_PAGESZ;
1581.10Sthorpej		a->a_v = PAGE_SIZE;
1591.1Smanu		a++;
1601.1Smanu
1611.1Smanu		a->a_type = LINUX_AT_HWCAP;
1621.1Smanu		a->a_v = LINUX_ELF_HWCAP;
1631.1Smanu		a++;
1641.1Smanu
1651.23Smatt		exec_free_emul_arg(pack);
1661.1Smanu	}
1671.1Smanu
1681.1Smanu	a->a_type = AT_NULL;
1691.1Smanu	a->a_v = 0;
1701.1Smanu	a++;
1711.1Smanu
1721.8Sjdolecek	len = (a - ai) * sizeof(AuxInfo);
1731.1Smanu
1741.4Schristos	if ((error = copyout(ai, *stackp, len)) != 0)
1751.4Schristos		return error;
1761.16Sperry	*stackp += len;
1771.13Smanu	return 0;
1781.13Smanu}
179