xref: /netbsd/sys/arch/arm/arm/cpu_exec.c (revision 4b72a076)
1*4b72a076Srin /*	$NetBSD: cpu_exec.c,v 1.13 2020/12/01 02:43:13 rin Exp $	*/
21cbec577Smatt 
31cbec577Smatt /*-
41cbec577Smatt  * Copyright (c) 2012 The NetBSD Foundation, Inc.
51cbec577Smatt  * All rights reserved.
61cbec577Smatt  *
71cbec577Smatt  * This code is derived from software contributed to The NetBSD Foundation
81cbec577Smatt  * by Matt Thomas of 3am Software Foundry.
91cbec577Smatt  *
101cbec577Smatt  * Redistribution and use in source and binary forms, with or without
111cbec577Smatt  * modification, are permitted provided that the following conditions
121cbec577Smatt  * are met:
131cbec577Smatt  * 1. Redistributions of source code must retain the above copyright
141cbec577Smatt  *    notice, this list of conditions and the following disclaimer.
151cbec577Smatt  * 2. Redistributions in binary form must reproduce the above copyright
161cbec577Smatt  *    notice, this list of conditions and the following disclaimer in the
171cbec577Smatt  *    documentation and/or other materials provided with the distribution.
181cbec577Smatt  *
191cbec577Smatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201cbec577Smatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211cbec577Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221cbec577Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231cbec577Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241cbec577Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251cbec577Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261cbec577Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271cbec577Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281cbec577Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291cbec577Smatt  * POSSIBILITY OF SUCH DAMAGE.
301cbec577Smatt  */
311cbec577Smatt 
321cbec577Smatt #include <sys/cdefs.h>
33*4b72a076Srin __KERNEL_RCSID(0, "$NetBSD: cpu_exec.c,v 1.13 2020/12/01 02:43:13 rin Exp $");
341cbec577Smatt 
351cbec577Smatt #include "opt_compat_netbsd.h"
361cbec577Smatt 
371cbec577Smatt #include <sys/param.h>
381cbec577Smatt #include <sys/systm.h>
391cbec577Smatt #include <sys/proc.h>
401cbec577Smatt #include <sys/exec.h>
411cbec577Smatt 
421cbec577Smatt #include <uvm/uvm_extern.h>
431cbec577Smatt 
441cbec577Smatt #include <compat/common/compat_util.h>
451cbec577Smatt #include <sys/exec_elf.h>			/* mandatory */
461cbec577Smatt 
478e4773eaSmatt #include <arm/locore.h>
488e4773eaSmatt 
491cbec577Smatt #if EXEC_ELF32
501cbec577Smatt int
arm_netbsd_elf32_probe(struct lwp * l,struct exec_package * epp,void * eh0,char * itp,vaddr_t * start_p)511cbec577Smatt arm_netbsd_elf32_probe(struct lwp *l, struct exec_package *epp, void *eh0,
521cbec577Smatt 	char *itp, vaddr_t *start_p)
531cbec577Smatt {
541cbec577Smatt 	const char *itp_suffix = NULL;
5566aec3aeSmatt 	const Elf_Ehdr * const eh = eh0;
5666aec3aeSmatt 	const bool elf_aapcs_p =
5766aec3aeSmatt 	    (eh->e_flags & EF_ARM_EABIMASK) >= EF_ARM_EABI_VER4;
58aac865adSrin #if defined(COMPAT_NETBSD32) || defined(MODULAR)
595e2e3bf1Srin 	const bool netbsd32_p = (epp->ep_esch->es_emul != &emul_netbsd);
60aac865adSrin #else
61aac865adSrin 	const bool netbsd32_p = false;
62aac865adSrin #endif
631cbec577Smatt #ifdef __ARM_EABI__
641cbec577Smatt 	const bool aapcs_p = true;
651cbec577Smatt #else
661cbec577Smatt 	const bool aapcs_p = false;
671cbec577Smatt #endif
68385114bcSmatt #ifdef __ARMEB__
69385114bcSmatt 	const bool be8_p = (eh->e_flags & EF_ARM_BE8) != 0;
70385114bcSmatt 
71385114bcSmatt 	/*
72385114bcSmatt 	 * If the BE-8 model is supported, CPSR[7] will be clear.
73385114bcSmatt 	 * If the BE-32 model is supported, CPSR[7] will be set.
74385114bcSmatt 	 */
75*4b72a076Srin #ifdef _ARM_ARCH_BE8
76*4b72a076Srin 	if (!be8_p)
77*4b72a076Srin #else
78*4b72a076Srin 	if (be8_p)
79*4b72a076Srin #endif
80385114bcSmatt 		return ENOEXEC;
81385114bcSmatt #endif /* __ARMEB__ */
821cbec577Smatt 
831cbec577Smatt 	/*
84ace6976bSjoerg 	 * This is subtle.  If we are netbsd32, then we don't want to match the
851cbec577Smatt 	 * same ABI as the kernel.  If we aren't (netbsd32 == false), then we
861cbec577Smatt 	 * don't want to be different from the kernel's ABI.
871cbec577Smatt 	 *    true   true   true  ENOEXEC
881cbec577Smatt 	 *    true   false  true  0
891cbec577Smatt 	 *    true   true   false 0
901cbec577Smatt 	 *    true   false  false ENOEXEC
911cbec577Smatt 	 *    false  true   true  0
921cbec577Smatt 	 *    false  false  true  ENOEXEC
931cbec577Smatt 	 *    false  true   false ENOEXEC
941cbec577Smatt 	 *    false  false  false 0
951cbec577Smatt 	 */
961cbec577Smatt 	if (netbsd32_p ^ elf_aapcs_p ^ aapcs_p)
971cbec577Smatt 		return ENOEXEC;
981cbec577Smatt 
991cbec577Smatt 	if (netbsd32_p)
1000635eb21Smatt 		itp_suffix = (elf_aapcs_p) ? "eabi" : "oabi";
1011cbec577Smatt 
1021cbec577Smatt 	if (itp_suffix != NULL)
1031cbec577Smatt 		(void)compat_elf_check_interp(epp, itp, itp_suffix);
10495db636eSmatt 
10595db636eSmatt 	/*
10695db636eSmatt 	 * Copy (if any) the machine_arch of the executable to the proc.
10795db636eSmatt 	 */
10895db636eSmatt 	if (epp->ep_machine_arch[0] != 0) {
10995db636eSmatt 		strlcpy(l->l_proc->p_md.md_march, epp->ep_machine_arch,
11095db636eSmatt 		    sizeof(l->l_proc->p_md.md_march));
11195db636eSmatt 	}
11276b334d5Smatt 
1138e4773eaSmatt 	/*
1148e4773eaSmatt 	 * If we are AAPCS (EABI) and armv6/armv7, we want alignment faults
11576b334d5Smatt 	 * to be off.
1168e4773eaSmatt 	 */
117*4b72a076Srin #if defined(__ARMEL__)
118*4b72a076Srin 	if (aapcs_p && (CPU_IS_ARMV7_P() || CPU_IS_ARMV6_P()))
119*4b72a076Srin #elif defined(_ARM_ARCH_BE8)
120*4b72a076Srin 	if (aapcs_p)
121*4b72a076Srin #else
122*4b72a076Srin 	if (false /* CONSTCOND */)
123*4b72a076Srin #endif
124*4b72a076Srin 	{
1258e4773eaSmatt 		l->l_md.md_flags |= MDLWP_NOALIGNFLT;
12676b334d5Smatt 	} else {
12776b334d5Smatt 		l->l_md.md_flags &= ~MDLWP_NOALIGNFLT;
1288e4773eaSmatt 	}
1291cbec577Smatt 	return 0;
1301cbec577Smatt }
1311cbec577Smatt #endif
132