1 1.1 jmcneill /* $NetBSD: syscallemu.c,v 1.1 2012/01/05 13:26:51 jmcneill Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2012 Jared D. McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include <sys/cdefs.h> 30 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: syscallemu.c,v 1.1 2012/01/05 13:26:51 jmcneill Exp $"); 31 1.1 jmcneill 32 1.1 jmcneill #include <sys/param.h> 33 1.1 jmcneill #include <sys/proc.h> 34 1.1 jmcneill #include <sys/systm.h> 35 1.1 jmcneill #include <sys/module.h> 36 1.1 jmcneill #include <sys/atomic.h> 37 1.1 jmcneill #include <sys/syscallvar.h> 38 1.1 jmcneill 39 1.1 jmcneill #include "syscallemu.h" 40 1.1 jmcneill 41 1.1 jmcneill #if !defined(__HAVE_SYSCALL_INTERN) 42 1.1 jmcneill #error syscallemu requires __HAVE_SYSCALL_INTERN 43 1.1 jmcneill #endif 44 1.1 jmcneill 45 1.1 jmcneill static specificdata_key_t syscallemu_data_key; 46 1.1 jmcneill static unsigned int syscallemu_refcnt; 47 1.1 jmcneill 48 1.1 jmcneill static const struct syscall_package syscallemu_syscalls[] = { 49 1.1 jmcneill { SYS_syscallemu, 0, (sy_call_t *)sys_syscallemu }, 50 1.1 jmcneill { 0, 0, NULL }, 51 1.1 jmcneill }; 52 1.1 jmcneill 53 1.1 jmcneill struct syscallemu_data * 54 1.1 jmcneill syscallemu_getsce(struct proc *p) 55 1.1 jmcneill { 56 1.1 jmcneill return proc_getspecific(p, syscallemu_data_key); 57 1.1 jmcneill } 58 1.1 jmcneill 59 1.1 jmcneill void 60 1.1 jmcneill syscallemu_setsce(struct proc *p, struct syscallemu_data *sce) 61 1.1 jmcneill { 62 1.1 jmcneill proc_setspecific(p, syscallemu_data_key, sce); 63 1.1 jmcneill } 64 1.1 jmcneill 65 1.1 jmcneill /* 66 1.1 jmcneill * specificdata destructor 67 1.1 jmcneill */ 68 1.1 jmcneill static void 69 1.1 jmcneill syscallemu_dtor(void *priv) 70 1.1 jmcneill { 71 1.1 jmcneill struct syscallemu_data *sce = priv; 72 1.1 jmcneill 73 1.1 jmcneill kmem_free(sce, sizeof(*sce)); 74 1.1 jmcneill atomic_dec_uint(&syscallemu_refcnt); 75 1.1 jmcneill } 76 1.1 jmcneill 77 1.1 jmcneill /* 78 1.1 jmcneill * Allocate private storage for the syscallemu parameters and stash it 79 1.1 jmcneill * in process specificdata. This can only be called once per process. 80 1.1 jmcneill * 81 1.1 jmcneill * Returns EINVAL if the specified start address falls after the end. 82 1.1 jmcneill * Returns EACCESS if syscallemu has already been configured for this process. 83 1.1 jmcneill */ 84 1.1 jmcneill int 85 1.1 jmcneill sys_syscallemu(lwp_t *l, const struct sys_syscallemu_args *uap, 86 1.1 jmcneill register_t *retval) 87 1.1 jmcneill { 88 1.1 jmcneill /* { 89 1.1 jmcneill syscallarg(uintptr_t) user_start; 90 1.1 jmcneill syscallarg(uintptr_t) user_end; 91 1.1 jmcneill } */ 92 1.1 jmcneill vaddr_t user_start = (vaddr_t)SCARG(uap, user_start); 93 1.1 jmcneill vaddr_t user_end = (vaddr_t)SCARG(uap, user_end); 94 1.1 jmcneill struct syscallemu_data *sce; 95 1.1 jmcneill struct proc *p = l->l_proc; 96 1.1 jmcneill 97 1.1 jmcneill if (syscallemu_getsce(p) != NULL) 98 1.1 jmcneill return EACCES; 99 1.1 jmcneill if (user_start >= user_end) 100 1.1 jmcneill return EINVAL; 101 1.1 jmcneill 102 1.1 jmcneill sce = kmem_alloc(sizeof(*sce), KM_SLEEP); 103 1.1 jmcneill sce->sce_user_start = user_start; 104 1.1 jmcneill sce->sce_user_end = user_end; 105 1.1 jmcneill sce->sce_md_syscall = md_syscallemu(p); 106 1.1 jmcneill KASSERT(sce->sce_md_syscall != NULL); 107 1.1 jmcneill 108 1.1 jmcneill atomic_inc_uint(&syscallemu_refcnt); 109 1.1 jmcneill syscallemu_setsce(p, sce); 110 1.1 jmcneill 111 1.1 jmcneill #ifdef DEBUG 112 1.1 jmcneill printf("syscallemu: enabled for pid %d\n", p->p_pid); 113 1.1 jmcneill #endif 114 1.1 jmcneill 115 1.1 jmcneill return 0; 116 1.1 jmcneill } 117 1.1 jmcneill 118 1.1 jmcneill /* 119 1.1 jmcneill * Initialize the syscallemu module 120 1.1 jmcneill */ 121 1.1 jmcneill static int 122 1.1 jmcneill syscallemu_init(void) 123 1.1 jmcneill { 124 1.1 jmcneill int error; 125 1.1 jmcneill 126 1.1 jmcneill syscallemu_refcnt = 0; 127 1.1 jmcneill 128 1.1 jmcneill /* XXX workaround for kern/45781 */ 129 1.1 jmcneill if (emul_netbsd.e_sysent[SYS_syscallemu].sy_call == sys_nosys) { 130 1.1 jmcneill printf("syscallemu: applying workaround for kern/45781\n"); 131 1.1 jmcneill emul_netbsd.e_sysent[SYS_syscallemu].sy_call = sys_nomodule; 132 1.1 jmcneill } 133 1.1 jmcneill emul_netbsd.e_sysent[SYS_syscallemu].sy_narg = 134 1.1 jmcneill sizeof(struct sys_syscallemu_args) / sizeof(register_t); 135 1.1 jmcneill emul_netbsd.e_sysent[SYS_syscallemu].sy_argsize = 136 1.1 jmcneill sizeof(struct sys_syscallemu_args); 137 1.1 jmcneill 138 1.1 jmcneill error = proc_specific_key_create(&syscallemu_data_key, syscallemu_dtor); 139 1.1 jmcneill if (error) { 140 1.1 jmcneill printf("syscallemu: couldn't create proc specific key (%d)\n", 141 1.1 jmcneill error); 142 1.1 jmcneill return error; 143 1.1 jmcneill } 144 1.1 jmcneill 145 1.1 jmcneill error = syscall_establish(NULL, syscallemu_syscalls); 146 1.1 jmcneill if (error) { 147 1.1 jmcneill printf("syscallemu: couldn't establish syscalls\n"); 148 1.1 jmcneill proc_specific_key_delete(syscallemu_data_key); 149 1.1 jmcneill return ENXIO; 150 1.1 jmcneill } 151 1.1 jmcneill 152 1.1 jmcneill return 0; 153 1.1 jmcneill } 154 1.1 jmcneill 155 1.1 jmcneill /* 156 1.1 jmcneill * Finalize the syscallemu module 157 1.1 jmcneill */ 158 1.1 jmcneill static int 159 1.1 jmcneill syscallemu_fini(void) 160 1.1 jmcneill { 161 1.1 jmcneill if (syscallemu_refcnt > 0) 162 1.1 jmcneill return EBUSY; 163 1.1 jmcneill 164 1.1 jmcneill syscall_disestablish(NULL, syscallemu_syscalls); 165 1.1 jmcneill proc_specific_key_delete(syscallemu_data_key); 166 1.1 jmcneill return 0; 167 1.1 jmcneill } 168 1.1 jmcneill 169 1.1 jmcneill /* 170 1.1 jmcneill * Module glue 171 1.1 jmcneill */ 172 1.1 jmcneill MODULE(MODULE_CLASS_MISC, syscallemu, NULL); 173 1.1 jmcneill 174 1.1 jmcneill static int 175 1.1 jmcneill syscallemu_modcmd(modcmd_t cmd, void *arg) 176 1.1 jmcneill { 177 1.1 jmcneill switch (cmd) { 178 1.1 jmcneill case MODULE_CMD_INIT: 179 1.1 jmcneill return syscallemu_init(); 180 1.1 jmcneill case MODULE_CMD_FINI: 181 1.1 jmcneill return syscallemu_fini(); 182 1.1 jmcneill case MODULE_CMD_AUTOUNLOAD: 183 1.1 jmcneill return EBUSY; 184 1.1 jmcneill default: 185 1.1 jmcneill return ENOTTY; 186 1.1 jmcneill } 187 1.1 jmcneill } 188