1*e092cf89Srin /* $NetBSD: cpu_exec.c,v 1.13 2020/12/01 02:43:13 rin Exp $ */
29fae3660Smatt
39fae3660Smatt /*-
49fae3660Smatt * Copyright (c) 2012 The NetBSD Foundation, Inc.
59fae3660Smatt * All rights reserved.
69fae3660Smatt *
79fae3660Smatt * This code is derived from software contributed to The NetBSD Foundation
89fae3660Smatt * by Matt Thomas of 3am Software Foundry.
99fae3660Smatt *
109fae3660Smatt * Redistribution and use in source and binary forms, with or without
119fae3660Smatt * modification, are permitted provided that the following conditions
129fae3660Smatt * are met:
139fae3660Smatt * 1. Redistributions of source code must retain the above copyright
149fae3660Smatt * notice, this list of conditions and the following disclaimer.
159fae3660Smatt * 2. Redistributions in binary form must reproduce the above copyright
169fae3660Smatt * notice, this list of conditions and the following disclaimer in the
179fae3660Smatt * documentation and/or other materials provided with the distribution.
189fae3660Smatt *
199fae3660Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209fae3660Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219fae3660Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229fae3660Smatt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239fae3660Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249fae3660Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259fae3660Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269fae3660Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279fae3660Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289fae3660Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299fae3660Smatt * POSSIBILITY OF SUCH DAMAGE.
309fae3660Smatt */
319fae3660Smatt
329fae3660Smatt #include <sys/cdefs.h>
33*e092cf89Srin __KERNEL_RCSID(0, "$NetBSD: cpu_exec.c,v 1.13 2020/12/01 02:43:13 rin Exp $");
349fae3660Smatt
359fae3660Smatt #include "opt_compat_netbsd.h"
369fae3660Smatt
379fae3660Smatt #include <sys/param.h>
389fae3660Smatt #include <sys/systm.h>
399fae3660Smatt #include <sys/proc.h>
409fae3660Smatt #include <sys/exec.h>
419fae3660Smatt
429fae3660Smatt #include <uvm/uvm_extern.h>
439fae3660Smatt
449fae3660Smatt #include <compat/common/compat_util.h>
459fae3660Smatt #include <sys/exec_elf.h> /* mandatory */
469fae3660Smatt
47053c552bSmatt #include <arm/locore.h>
48053c552bSmatt
499fae3660Smatt #if EXEC_ELF32
509fae3660Smatt int
arm_netbsd_elf32_probe(struct lwp * l,struct exec_package * epp,void * eh0,char * itp,vaddr_t * start_p)519fae3660Smatt arm_netbsd_elf32_probe(struct lwp *l, struct exec_package *epp, void *eh0,
529fae3660Smatt char *itp, vaddr_t *start_p)
539fae3660Smatt {
549fae3660Smatt const char *itp_suffix = NULL;
553e95365cSmatt const Elf_Ehdr * const eh = eh0;
563e95365cSmatt const bool elf_aapcs_p =
573e95365cSmatt (eh->e_flags & EF_ARM_EABIMASK) >= EF_ARM_EABI_VER4;
58aac12b9dSrin #if defined(COMPAT_NETBSD32) || defined(MODULAR)
591f2a4425Srin const bool netbsd32_p = (epp->ep_esch->es_emul != &emul_netbsd);
60aac12b9dSrin #else
61aac12b9dSrin const bool netbsd32_p = false;
62aac12b9dSrin #endif
639fae3660Smatt #ifdef __ARM_EABI__
649fae3660Smatt const bool aapcs_p = true;
659fae3660Smatt #else
669fae3660Smatt const bool aapcs_p = false;
679fae3660Smatt #endif
688bb79fa0Smatt #ifdef __ARMEB__
698bb79fa0Smatt const bool be8_p = (eh->e_flags & EF_ARM_BE8) != 0;
708bb79fa0Smatt
718bb79fa0Smatt /*
728bb79fa0Smatt * If the BE-8 model is supported, CPSR[7] will be clear.
738bb79fa0Smatt * If the BE-32 model is supported, CPSR[7] will be set.
748bb79fa0Smatt */
75*e092cf89Srin #ifdef _ARM_ARCH_BE8
76*e092cf89Srin if (!be8_p)
77*e092cf89Srin #else
78*e092cf89Srin if (be8_p)
79*e092cf89Srin #endif
808bb79fa0Smatt return ENOEXEC;
818bb79fa0Smatt #endif /* __ARMEB__ */
829fae3660Smatt
839fae3660Smatt /*
840d06f966Sjoerg * This is subtle. If we are netbsd32, then we don't want to match the
859fae3660Smatt * same ABI as the kernel. If we aren't (netbsd32 == false), then we
869fae3660Smatt * don't want to be different from the kernel's ABI.
879fae3660Smatt * true true true ENOEXEC
889fae3660Smatt * true false true 0
899fae3660Smatt * true true false 0
909fae3660Smatt * true false false ENOEXEC
919fae3660Smatt * false true true 0
929fae3660Smatt * false false true ENOEXEC
939fae3660Smatt * false true false ENOEXEC
949fae3660Smatt * false false false 0
959fae3660Smatt */
969fae3660Smatt if (netbsd32_p ^ elf_aapcs_p ^ aapcs_p)
979fae3660Smatt return ENOEXEC;
989fae3660Smatt
999fae3660Smatt if (netbsd32_p)
10033c1aaf8Smatt itp_suffix = (elf_aapcs_p) ? "eabi" : "oabi";
1019fae3660Smatt
1029fae3660Smatt if (itp_suffix != NULL)
1039fae3660Smatt (void)compat_elf_check_interp(epp, itp, itp_suffix);
10450bfeb71Smatt
10550bfeb71Smatt /*
10650bfeb71Smatt * Copy (if any) the machine_arch of the executable to the proc.
10750bfeb71Smatt */
10850bfeb71Smatt if (epp->ep_machine_arch[0] != 0) {
10950bfeb71Smatt strlcpy(l->l_proc->p_md.md_march, epp->ep_machine_arch,
11050bfeb71Smatt sizeof(l->l_proc->p_md.md_march));
11150bfeb71Smatt }
1121ae61159Smatt
113053c552bSmatt /*
114053c552bSmatt * If we are AAPCS (EABI) and armv6/armv7, we want alignment faults
1151ae61159Smatt * to be off.
116053c552bSmatt */
117*e092cf89Srin #if defined(__ARMEL__)
118*e092cf89Srin if (aapcs_p && (CPU_IS_ARMV7_P() || CPU_IS_ARMV6_P()))
119*e092cf89Srin #elif defined(_ARM_ARCH_BE8)
120*e092cf89Srin if (aapcs_p)
121*e092cf89Srin #else
122*e092cf89Srin if (false /* CONSTCOND */)
123*e092cf89Srin #endif
124*e092cf89Srin {
125053c552bSmatt l->l_md.md_flags |= MDLWP_NOALIGNFLT;
1261ae61159Smatt } else {
1271ae61159Smatt l->l_md.md_flags &= ~MDLWP_NOALIGNFLT;
128053c552bSmatt }
1299fae3660Smatt return 0;
1309fae3660Smatt }
1319fae3660Smatt #endif
132