xref: /freebsd/sys/kern/kern_syscalls.c (revision 4ea6a9a28fd7ae3cc6e8697bc93f9ce5ea6b6262)
1 /*-
2  * Copyright (c) 1999 Assar Westerlund
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/lock.h>
33 #include <sys/module.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.h>
36 #include <sys/sx.h>
37 #include <sys/syscall.h>
38 #include <sys/sysent.h>
39 #include <sys/sysproto.h>
40 #include <sys/systm.h>
41 #include <machine/atomic.h>
42 
43 /*
44  * Acts like "nosys" but can be identified in sysent for dynamic call
45  * number assignment for a limited number of calls.
46  *
47  * Place holder for system call slots reserved for loadable modules.
48  */
49 int
50 lkmnosys(struct thread *td, struct nosys_args *args)
51 {
52 
53 	return (nosys(td, args));
54 }
55 
56 int
57 lkmressys(struct thread *td, struct nosys_args *args)
58 {
59 
60 	return (nosys(td, args));
61 }
62 
63 static void
64 syscall_thread_drain(struct sysent *se)
65 {
66 	u_int32_t cnt, oldcnt;
67 
68 	do {
69 		oldcnt = se->sy_thrcnt;
70 		KASSERT((oldcnt & SY_THR_STATIC) == 0,
71 		    ("drain on static syscall"));
72 		cnt = oldcnt | SY_THR_DRAINING;
73 	} while (atomic_cmpset_acq_32(&se->sy_thrcnt, oldcnt, cnt) == 0);
74 	while (atomic_cmpset_32(&se->sy_thrcnt, SY_THR_DRAINING,
75 	    SY_THR_ABSENT) == 0)
76 		pause("scdrn", hz/2);
77 }
78 
79 int
80 syscall_thread_enter(struct thread *td, struct sysent *se)
81 {
82 	u_int32_t cnt, oldcnt;
83 
84 	do {
85 		oldcnt = se->sy_thrcnt;
86 		if ((oldcnt & SY_THR_STATIC) != 0)
87 			return (0);
88 		if ((oldcnt & (SY_THR_DRAINING | SY_THR_ABSENT)) != 0)
89 			return (ENOSYS);
90 		cnt = oldcnt + SY_THR_INCR;
91 	} while (atomic_cmpset_acq_32(&se->sy_thrcnt, oldcnt, cnt) == 0);
92 	return (0);
93 }
94 
95 void
96 syscall_thread_exit(struct thread *td, struct sysent *se)
97 {
98 	u_int32_t cnt, oldcnt;
99 
100 	do {
101 		oldcnt = se->sy_thrcnt;
102 		if ((oldcnt & SY_THR_STATIC) != 0)
103 			return;
104 		cnt = oldcnt - SY_THR_INCR;
105 	} while (atomic_cmpset_rel_32(&se->sy_thrcnt, oldcnt, cnt) == 0);
106 }
107 
108 int
109 syscall_register(int *offset, struct sysent *new_sysent,
110     struct sysent *old_sysent, int flags)
111 {
112 	int i;
113 
114 	if ((flags & ~SY_THR_STATIC) != 0)
115 		return (EINVAL);
116 
117 	if (*offset == NO_SYSCALL) {
118 		for (i = 1; i < SYS_MAXSYSCALL; ++i)
119 			if (sysent[i].sy_call == (sy_call_t *)lkmnosys)
120 				break;
121 		if (i == SYS_MAXSYSCALL)
122 			return (ENFILE);
123 		*offset = i;
124 	} else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
125 		return (EINVAL);
126 	else if (sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
127 	    sysent[*offset].sy_call != (sy_call_t *)lkmressys)
128 		return (EEXIST);
129 
130 	KASSERT(sysent[*offset].sy_thrcnt == SY_THR_ABSENT,
131 	    ("dynamic syscall is not protected"));
132 	*old_sysent = sysent[*offset];
133 	new_sysent->sy_thrcnt = SY_THR_ABSENT;
134 	sysent[*offset] = *new_sysent;
135 	atomic_store_rel_32(&sysent[*offset].sy_thrcnt, flags);
136 	return (0);
137 }
138 
139 int
140 syscall_deregister(int *offset, struct sysent *old_sysent)
141 {
142 	struct sysent *se;
143 
144 	if (*offset == 0)
145 		return (0); /* XXX? */
146 
147 	se = &sysent[*offset];
148 	if ((se->sy_thrcnt & SY_THR_STATIC) != 0)
149 		return (EINVAL);
150 	syscall_thread_drain(se);
151 	sysent[*offset] = *old_sysent;
152 	return (0);
153 }
154 
155 int
156 syscall_module_handler(struct module *mod, int what, void *arg)
157 {
158 	struct syscall_module_data *data = arg;
159 	modspecific_t ms;
160 	int error;
161 
162 	switch (what) {
163 	case MOD_LOAD:
164 		error = syscall_register(data->offset, data->new_sysent,
165 		    &data->old_sysent, data->flags);
166 		if (error) {
167 			/* Leave a mark so we know to safely unload below. */
168 			data->offset = NULL;
169 			return (error);
170 		}
171 		ms.intval = *data->offset;
172 		MOD_XLOCK;
173 		module_setspecific(mod, &ms);
174 		MOD_XUNLOCK;
175 		if (data->chainevh)
176 			error = data->chainevh(mod, what, data->chainarg);
177 		return (error);
178 	case MOD_UNLOAD:
179 		/*
180 		 * MOD_LOAD failed, so just return without calling the
181 		 * chained handler since we didn't pass along the MOD_LOAD
182 		 * event.
183 		 */
184 		if (data->offset == NULL)
185 			return (0);
186 		if (data->chainevh) {
187 			error = data->chainevh(mod, what, data->chainarg);
188 			if (error)
189 				return error;
190 		}
191 		error = syscall_deregister(data->offset, &data->old_sysent);
192 		return (error);
193 	default:
194 		if (data->chainevh)
195 			return (data->chainevh(mod, what, data->chainarg));
196 		return (EOPNOTSUPP);
197 	}
198 
199 	/* NOTREACHED */
200 }
201 
202 int
203 syscall_helper_register(struct syscall_helper_data *sd, int flags)
204 {
205 	struct syscall_helper_data *sd1;
206 	int error;
207 
208 	for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
209 		error = syscall_register(&sd1->syscall_no, &sd1->new_sysent,
210 		    &sd1->old_sysent, flags);
211 		if (error != 0) {
212 			syscall_helper_unregister(sd);
213 			return (error);
214 		}
215 		sd1->registered = 1;
216 	}
217 	return (0);
218 }
219 
220 int
221 syscall_helper_unregister(struct syscall_helper_data *sd)
222 {
223 	struct syscall_helper_data *sd1;
224 
225 	for (sd1 = sd; sd1->registered != 0; sd1++) {
226 		syscall_deregister(&sd1->syscall_no, &sd1->old_sysent);
227 		sd1->registered = 0;
228 	}
229 	return (0);
230 }
231