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