1 /* $NetBSD: netbsd32_mod.c,v 1.23 2020/08/08 19:08:48 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software developed for The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Charles M. Hannum, and by Maxime Villard. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 49 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 51 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 58 * POSSIBILITY OF SUCH DAMAGE. 59 */ 60 61 #include <sys/cdefs.h> 62 __KERNEL_RCSID(0, "$NetBSD: netbsd32_mod.c,v 1.23 2020/08/08 19:08:48 christos Exp $"); 63 64 #ifdef _KERNEL_OPT 65 #include "opt_execfmt.h" 66 #endif 67 68 #ifndef ELFSIZE 69 #define ELFSIZE ARCH_ELFSIZE 70 #endif 71 72 #include <sys/param.h> 73 #include <sys/module.h> 74 #include <sys/exec.h> 75 #include <sys/exec_elf.h> 76 #include <sys/module_hook.h> 77 #include <sys/compat_stub.h> 78 79 #include <compat/netbsd32/netbsd32_sysctl.h> 80 #include <compat/netbsd32/netbsd32_kern_proc.h> 81 #include <compat/netbsd32/netbsd32_exec.h> 82 83 #define ELF32_AUXSIZE (howmany(ELF_AUX_ENTRIES * sizeof(Aux32Info), \ 84 sizeof(Elf32_Addr)) + MAXPATHLEN + ALIGN(1)) 85 86 struct compat32_80_modctl_hook_t compat32_80_modctl_hook; 87 88 # define DEPS1 "ksem,compat_util" 89 90 #if defined(EXEC_ELF32) 91 # define DEPS2 ",exec_elf32" 92 #else 93 # define DEPS2 "" 94 #endif 95 96 MODULE(MODULE_CLASS_EXEC, compat_netbsd32, DEPS1 DEPS2); 97 98 static struct execsw netbsd32_execsw[] = { 99 #ifdef EXEC_AOUT 100 { 101 .es_hdrsz = sizeof(struct netbsd32_exec), 102 .es_makecmds = exec_netbsd32_makecmds, 103 .u = { 104 .elf_probe_func = NULL, 105 }, 106 .es_emul = &emul_netbsd32, 107 .es_prio = EXECSW_PRIO_FIRST, 108 .es_arglen = 0, 109 .es_copyargs = netbsd32_copyargs, 110 .es_setregs = NULL, 111 .es_coredump = coredump_netbsd32, 112 .es_setup_stack = exec_setup_stack, 113 }, 114 #endif 115 #ifdef EXEC_ELF32 116 { 117 .es_hdrsz = sizeof (Elf32_Ehdr), 118 .es_makecmds = exec_elf32_makecmds, 119 .u = { 120 .elf_probe_func = netbsd32_elf32_probe, 121 }, 122 .es_emul = &emul_netbsd32, 123 .es_prio = EXECSW_PRIO_ANY, 124 .es_arglen = ELF32_AUXSIZE, 125 .es_copyargs = netbsd32_elf32_copyargs, 126 .es_setregs = NULL, 127 .es_coredump = coredump_elf32, 128 .es_setup_stack = exec_setup_stack, 129 }, 130 #endif 131 }; 132 133 #if defined(__amd64__) 134 #include <x86/cpu.h> 135 136 /* This code was moved here, from $SRC/arch/amd64/amd64/trap.c */ 137 138 static int 139 amd64_oosyscall_handle(struct proc *p, struct trapframe *frame) 140 { 141 int error = EPASSTHROUGH; 142 #define LCALLSZ 7 143 144 /* Check for the oosyscall lcall instruction. */ 145 if (p->p_emul == &emul_netbsd32 && 146 frame->tf_rip < VM_MAXUSER_ADDRESS32 - LCALLSZ && 147 (error = x86_cpu_is_lcall((void *)frame->tf_rip)) == 0) 148 { 149 /* Advance past the lcall and save instruction size. */ 150 frame->tf_rip += LCALLSZ; 151 frame->tf_err = LCALLSZ; 152 return 0; 153 } 154 155 return error; 156 } 157 #endif /* defined(__amd64__) */ 158 159 static int 160 compat_netbsd32_modcmd(modcmd_t cmd, void *arg) 161 { 162 int error; 163 164 switch (cmd) { 165 case MODULE_CMD_INIT: 166 error = exec_add(netbsd32_execsw, 167 __arraycount(netbsd32_execsw)); 168 if (error == 0) { 169 netbsd32_machdep_md_init(); 170 netbsd32_kern_proc_32_init(); 171 #if defined(__amd64__) 172 MODULE_HOOK_SET(amd64_oosyscall_hook, 173 amd64_oosyscall_handle); 174 #endif /* defined(__amd64__) */ 175 } 176 return error; 177 178 case MODULE_CMD_FINI: 179 #if defined(__amd64__) 180 MODULE_HOOK_UNSET(amd64_oosyscall_hook); 181 #endif /* defined(__amd64__) */ 182 netbsd32_machdep_md_fini(); 183 netbsd32_sysctl_fini(); 184 netbsd32_kern_proc_32_fini(); 185 186 error = exec_remove(netbsd32_execsw, 187 __arraycount(netbsd32_execsw)); 188 if (error) { 189 netbsd32_kern_proc_32_init(); 190 netbsd32_machdep_md_init(); 191 #if defined(__amd64__) 192 MODULE_HOOK_SET(amd64_oosyscall_hook, 193 amd64_oosyscall_handle); 194 #endif /* defined(__amd64__) */ 195 } 196 return error; 197 198 default: 199 return ENOTTY; 200 } 201 } 202