1 /* $NetBSD: linux_mod.c,v 1.11 2019/01/27 02:08:40 pgoyette 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 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: linux_mod.c,v 1.11 2019/01/27 02:08:40 pgoyette Exp $"); 34 35 #ifdef _KERNEL_OPT 36 #include "opt_execfmt.h" 37 #endif 38 39 #ifndef ELFSIZE 40 #define ELFSIZE ARCH_ELFSIZE 41 #endif 42 43 #include <sys/param.h> 44 #include <sys/module.h> 45 #include <sys/exec.h> 46 #include <sys/signalvar.h> 47 #include <sys/sysctl.h> 48 49 #include <compat/linux/common/linux_sysctl.h> 50 #include <compat/linux/common/linux_futex.h> 51 #include <compat/linux/common/linux_exec.h> 52 53 #if defined(EXEC_ELF32) && ELFSIZE == 32 54 # define MD1 ",exec_elf32" 55 #else 56 # define MD1 "" 57 #endif 58 #if defined(EXEC_ELF64) 59 # define MD2 ",exec_elf64" 60 #else 61 # define MD2 "" 62 #endif 63 #if defined(EXEC_AOUT) 64 # define MD3 ",exec_aout" 65 #else 66 # define MD3 "" 67 #endif 68 69 #define REQ1 "compat_ossaudio,sysv_ipc,compat_util" 70 #define REQ2 ",compat_50,compat_43" 71 72 MODULE(MODULE_CLASS_EXEC, compat_linux, REQ1 REQ2 MD1 MD2 MD3); 73 74 static struct execsw linux_execsw[] = { 75 #if defined(EXEC_ELF32) && ELFSIZE == 32 76 { 77 .es_hdrsz = sizeof (Elf32_Ehdr), 78 .es_makecmds = exec_elf32_makecmds, 79 .u = { 80 .elf_probe_func = linux_elf32_probe, 81 }, 82 .es_emul = &emul_linux, 83 .es_prio = EXECSW_PRIO_ANY, 84 .es_arglen = LINUX_ELF_AUX_ARGSIZ, 85 .es_copyargs = linux_elf32_copyargs, 86 .es_setregs = NULL, 87 .es_coredump = coredump_elf32, 88 .es_setup_stack = linux_exec_setup_stack, 89 }, 90 #elif defined(EXEC_ELF64) 91 { 92 .es_hdrsz = sizeof (Elf64_Ehdr), 93 .es_makecmds = exec_elf64_makecmds, 94 .u = { 95 .elf_probe_func = linux_elf64_probe, 96 }, 97 .es_emul = &emul_linux, 98 .es_prio = EXECSW_PRIO_ANY, 99 .es_arglen = LINUX_ELF_AUX_ARGSIZ, 100 .es_copyargs = linux_elf64_copyargs, 101 .es_setregs = NULL, 102 .es_coredump = coredump_elf64, 103 .es_setup_stack = linux_exec_setup_stack, 104 }, 105 #endif 106 #ifdef EXEC_AOUT 107 { 108 .es_hdrsz = LINUX_AOUT_HDR_SIZE, 109 .es_makecmds = exec_linux_aout_makecmds, 110 .u = { 111 .elf_probe_func = NULL, 112 }, 113 .es_emul = &emul_linux, 114 .es_prio = EXECSW_PRIO_LAST, 115 .es_arglen = LINUX_AOUT_AUX_ARGSIZ, 116 .es_copyargs = linux_aout_copyargs, 117 .es_setregs = NULL, 118 .es_coredump = coredump_netbsd, 119 .es_setup_stack = linux_exec_setup_stack, 120 }, 121 #endif 122 }; 123 124 int linux_enabled = 1; 125 126 int 127 linux_sysctl_enable(SYSCTLFN_ARGS) 128 { 129 struct sysctlnode node; 130 int error, val; 131 132 val = *(int *)rnode->sysctl_data; 133 134 node = *rnode; 135 node.sysctl_data = &val; 136 137 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 138 if (error != 0 || newp == NULL) 139 return error; 140 141 if (val == *(int *)rnode->sysctl_data) 142 return 0; 143 144 if (val == 1) 145 error = exec_add(linux_execsw, __arraycount(linux_execsw)); 146 else if (val == 0) 147 error = exec_remove(linux_execsw, __arraycount(linux_execsw)); 148 else 149 error = EINVAL; 150 151 if (error) 152 return error; 153 154 *(int *)rnode->sysctl_data = val; 155 156 return 0; 157 } 158 159 static int 160 compat_linux_modcmd(modcmd_t cmd, void *arg) 161 { 162 int error; 163 164 switch (cmd) { 165 case MODULE_CMD_INIT: 166 linux_futex_init(); 167 linux_sysctl_init(); 168 error = exec_add(linux_execsw, __arraycount(linux_execsw)); 169 if (error != 0) 170 linux_sysctl_fini(); 171 return error; 172 173 case MODULE_CMD_FINI: 174 error = exec_remove(linux_execsw, __arraycount(linux_execsw)); 175 if (error) 176 return error; 177 linux_sysctl_fini(); 178 linux_futex_fini(); 179 return 0; 180 181 default: 182 return ENOTTY; 183 } 184 } 185