kern_syscall.c revision 1.1 1 1.1 pooka /* $NetBSD: kern_syscall.c,v 1.1 2010/04/14 14:46:59 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 pooka * All rights reserved.
6 1.1 pooka *
7 1.1 pooka * This code is derived from software developed for The NetBSD Foundation
8 1.1 pooka * by Andrew Doran.
9 1.1 pooka *
10 1.1 pooka * Redistribution and use in source and binary forms, with or without
11 1.1 pooka * modification, are permitted provided that the following conditions
12 1.1 pooka * are met:
13 1.1 pooka * 1. Redistributions of source code must retain the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer.
15 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pooka * notice, this list of conditions and the following disclaimer in the
17 1.1 pooka * documentation and/or other materials provided with the distribution.
18 1.1 pooka *
19 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 pooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 pooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 pooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 pooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 pooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 pooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 pooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 pooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 pooka * POSSIBILITY OF SUCH DAMAGE.
30 1.1 pooka */
31 1.1 pooka
32 1.1 pooka #include <sys/cdefs.h>
33 1.1 pooka __KERNEL_RCSID(0, "$NetBSD: kern_syscall.c,v 1.1 2010/04/14 14:46:59 pooka Exp $");
34 1.1 pooka
35 1.1 pooka #include <sys/param.h>
36 1.1 pooka #include <sys/module.h>
37 1.1 pooka #include <sys/syscall.h>
38 1.1 pooka #include <sys/syscallargs.h>
39 1.1 pooka #include <sys/syscallvar.h>
40 1.1 pooka #include <sys/xcall.h>
41 1.1 pooka
42 1.1 pooka int
43 1.1 pooka sys_nomodule(struct lwp *l, const void *v, register_t *retval)
44 1.1 pooka {
45 1.1 pooka #ifdef MODULAR
46 1.1 pooka static struct {
47 1.1 pooka u_int al_code;
48 1.1 pooka const char *al_module;
49 1.1 pooka } const autoload[] = {
50 1.1 pooka { SYS_aio_cancel, "aio" },
51 1.1 pooka { SYS_aio_error, "aio" },
52 1.1 pooka { SYS_aio_fsync, "aio" },
53 1.1 pooka { SYS_aio_read, "aio" },
54 1.1 pooka { SYS_aio_return, "aio" },
55 1.1 pooka { SYS___aio_suspend50, "aio" },
56 1.1 pooka { SYS_aio_write, "aio" },
57 1.1 pooka { SYS_lio_listio, "aio" },
58 1.1 pooka { SYS_mq_open, "mqueue" },
59 1.1 pooka { SYS_mq_close, "mqueue" },
60 1.1 pooka { SYS_mq_unlink, "mqueue" },
61 1.1 pooka { SYS_mq_getattr, "mqueue" },
62 1.1 pooka { SYS_mq_setattr, "mqueue" },
63 1.1 pooka { SYS_mq_notify, "mqueue" },
64 1.1 pooka { SYS_mq_send, "mqueue" },
65 1.1 pooka { SYS_mq_receive, "mqueue" },
66 1.1 pooka { SYS___mq_timedsend50, "mqueue" },
67 1.1 pooka { SYS___mq_timedreceive50, "mqueue" },
68 1.1 pooka { SYS_compat_43_fstat43, "compat" },
69 1.1 pooka { SYS_compat_43_lstat43, "compat" },
70 1.1 pooka { SYS_compat_43_oaccept, "compat" },
71 1.1 pooka { SYS_compat_43_ocreat, "compat" },
72 1.1 pooka { SYS_compat_43_oftruncate, "compat" },
73 1.1 pooka { SYS_compat_43_ogetdirentries, "compat" },
74 1.1 pooka { SYS_compat_43_ogetdtablesize, "compat" },
75 1.1 pooka { SYS_compat_43_ogethostid, "compat" },
76 1.1 pooka { SYS_compat_43_ogethostname, "compat" },
77 1.1 pooka { SYS_compat_43_ogetkerninfo, "compat" },
78 1.1 pooka { SYS_compat_43_ogetpagesize, "compat" },
79 1.1 pooka { SYS_compat_43_ogetpeername, "compat" },
80 1.1 pooka { SYS_compat_43_ogetrlimit, "compat" },
81 1.1 pooka { SYS_compat_43_ogetsockname, "compat" },
82 1.1 pooka { SYS_compat_43_okillpg, "compat" },
83 1.1 pooka { SYS_compat_43_olseek, "compat" },
84 1.1 pooka { SYS_compat_43_ommap, "compat" },
85 1.1 pooka { SYS_compat_43_oquota, "compat" },
86 1.1 pooka { SYS_compat_43_orecv, "compat" },
87 1.1 pooka { SYS_compat_43_orecvfrom, "compat" },
88 1.1 pooka { SYS_compat_43_orecvmsg, "compat" },
89 1.1 pooka { SYS_compat_43_osend, "compat" },
90 1.1 pooka { SYS_compat_43_osendmsg, "compat" },
91 1.1 pooka { SYS_compat_43_osethostid, "compat" },
92 1.1 pooka { SYS_compat_43_osethostname, "compat" },
93 1.1 pooka { SYS_compat_43_osetrlimit, "compat" },
94 1.1 pooka { SYS_compat_43_osigblock, "compat" },
95 1.1 pooka { SYS_compat_43_osigsetmask, "compat" },
96 1.1 pooka { SYS_compat_43_osigstack, "compat" },
97 1.1 pooka { SYS_compat_43_osigvec, "compat" },
98 1.1 pooka { SYS_compat_43_otruncate, "compat" },
99 1.1 pooka { SYS_compat_43_owait, "compat" },
100 1.1 pooka { SYS_compat_43_stat43, "compat" },
101 1.1 pooka { SYS_compat_09_ogetdomainname, "compat" },
102 1.1 pooka { SYS_compat_09_osetdomainname, "compat" },
103 1.1 pooka { SYS_compat_09_ouname, "compat" },
104 1.1 pooka #ifndef _LP64
105 1.1 pooka { SYS_compat_10_omsgsys, "compat" },
106 1.1 pooka { SYS_compat_10_osemsys, "compat" },
107 1.1 pooka { SYS_compat_10_oshmsys, "compat" },
108 1.1 pooka #endif
109 1.1 pooka { SYS_compat_12_fstat12, "compat" },
110 1.1 pooka { SYS_compat_12_getdirentries, "compat" },
111 1.1 pooka { SYS_compat_12_lstat12, "compat" },
112 1.1 pooka { SYS_compat_12_msync, "compat" },
113 1.1 pooka { SYS_compat_12_oreboot, "compat" },
114 1.1 pooka { SYS_compat_12_oswapon, "compat" },
115 1.1 pooka { SYS_compat_12_stat12, "compat" },
116 1.1 pooka { SYS_compat_13_sigaction13, "compat" },
117 1.1 pooka { SYS_compat_13_sigaltstack13, "compat" },
118 1.1 pooka { SYS_compat_13_sigpending13, "compat" },
119 1.1 pooka { SYS_compat_13_sigprocmask13, "compat" },
120 1.1 pooka { SYS_compat_13_sigreturn13, "compat" },
121 1.1 pooka { SYS_compat_13_sigsuspend13, "compat" },
122 1.1 pooka { SYS_compat_14___semctl, "compat" },
123 1.1 pooka { SYS_compat_14_msgctl, "compat" },
124 1.1 pooka { SYS_compat_14_shmctl, "compat" },
125 1.1 pooka { SYS_compat_16___sigaction14, "compat" },
126 1.1 pooka { SYS_compat_16___sigreturn14, "compat" },
127 1.1 pooka { SYS_compat_20_fhstatfs, "compat" },
128 1.1 pooka { SYS_compat_20_fstatfs, "compat" },
129 1.1 pooka { SYS_compat_20_getfsstat, "compat" },
130 1.1 pooka { SYS_compat_20_statfs, "compat" },
131 1.1 pooka { SYS_compat_30___fhstat30, "compat" },
132 1.1 pooka { SYS_compat_30___fstat13, "compat" },
133 1.1 pooka { SYS_compat_30___lstat13, "compat" },
134 1.1 pooka { SYS_compat_30___stat13, "compat" },
135 1.1 pooka { SYS_compat_30_fhopen, "compat" },
136 1.1 pooka { SYS_compat_30_fhstat, "compat" },
137 1.1 pooka { SYS_compat_30_fhstatvfs1, "compat" },
138 1.1 pooka { SYS_compat_30_getdents, "compat" },
139 1.1 pooka { SYS_compat_30_getfh, "compat" },
140 1.1 pooka { SYS_compat_30_socket, "compat" },
141 1.1 pooka { SYS_compat_40_mount, "compat" },
142 1.1 pooka { SYS_compat_50_wait4, "compat" },
143 1.1 pooka { SYS_compat_50_mknod, "compat" },
144 1.1 pooka { SYS_compat_50_setitimer, "compat" },
145 1.1 pooka { SYS_compat_50_getitimer, "compat" },
146 1.1 pooka { SYS_compat_50_select, "compat" },
147 1.1 pooka { SYS_compat_50_gettimeofday, "compat" },
148 1.1 pooka { SYS_compat_50_getrusage, "compat" },
149 1.1 pooka { SYS_compat_50_settimeofday, "compat" },
150 1.1 pooka { SYS_compat_50_utimes, "compat" },
151 1.1 pooka { SYS_compat_50_adjtime, "compat" },
152 1.1 pooka { SYS_compat_50_lfs_segwait, "compat" },
153 1.1 pooka { SYS_compat_50_futimes, "compat" },
154 1.1 pooka { SYS_compat_50_clock_gettime, "compat" },
155 1.1 pooka { SYS_compat_50_clock_settime, "compat" },
156 1.1 pooka { SYS_compat_50_clock_getres, "compat" },
157 1.1 pooka { SYS_compat_50_timer_settime, "compat" },
158 1.1 pooka { SYS_compat_50_timer_gettime, "compat" },
159 1.1 pooka { SYS_compat_50_nanosleep, "compat" },
160 1.1 pooka { SYS_compat_50___sigtimedwait, "compat" },
161 1.1 pooka { SYS_compat_50_mq_timedsend, "compat" },
162 1.1 pooka { SYS_compat_50_mq_timedreceive, "compat" },
163 1.1 pooka { SYS_compat_50_lutimes, "compat" },
164 1.1 pooka { SYS_compat_50_____semctl13, "compat" },
165 1.1 pooka { SYS_compat_50___msgctl13, "compat" },
166 1.1 pooka { SYS_compat_50___shmctl13, "compat" },
167 1.1 pooka { SYS_compat_50__lwp_park, "compat" },
168 1.1 pooka { SYS_compat_50_kevent, "compat" },
169 1.1 pooka { SYS_compat_50_pselect, "compat" },
170 1.1 pooka { SYS_compat_50_pollts, "compat" },
171 1.1 pooka { SYS_compat_50___stat30, "compat" },
172 1.1 pooka { SYS_compat_50___fstat30, "compat" },
173 1.1 pooka { SYS_compat_50___lstat30, "compat" },
174 1.1 pooka { SYS_compat_50___ntp_gettime30, "compat" },
175 1.1 pooka { SYS_compat_50___fhstat40, "compat" },
176 1.1 pooka { SYS_compat_50_aio_suspend, "compat" },
177 1.1 pooka { SYS__ksem_init, "ksem" },
178 1.1 pooka { SYS__ksem_open, "ksem" },
179 1.1 pooka { SYS__ksem_unlink, "ksem" },
180 1.1 pooka { SYS__ksem_close, "ksem" },
181 1.1 pooka { SYS__ksem_post, "ksem" },
182 1.1 pooka { SYS__ksem_wait, "ksem" },
183 1.1 pooka { SYS__ksem_trywait, "ksem" },
184 1.1 pooka { SYS__ksem_getvalue, "ksem" },
185 1.1 pooka { SYS__ksem_destroy, "ksem" },
186 1.1 pooka { SYS_nfssvc, "nfsserver" },
187 1.1 pooka };
188 1.1 pooka const struct sysent *sy;
189 1.1 pooka const struct emul *em;
190 1.1 pooka int code, i;
191 1.1 pooka
192 1.1 pooka /*
193 1.1 pooka * Restart the syscall if we interrupted a module unload that
194 1.1 pooka * failed. Acquiring module_lock delays us until any unload
195 1.1 pooka * has been completed or rolled back.
196 1.1 pooka */
197 1.1 pooka mutex_enter(&module_lock);
198 1.1 pooka sy = l->l_sysent;
199 1.1 pooka if (sy->sy_call != sys_nomodule) {
200 1.1 pooka mutex_exit(&module_lock);
201 1.1 pooka return ERESTART;
202 1.1 pooka }
203 1.1 pooka /*
204 1.1 pooka * Try to autoload a module to satisfy the request. If it
205 1.1 pooka * works, retry the request.
206 1.1 pooka */
207 1.1 pooka em = l->l_proc->p_emul;
208 1.1 pooka if (em == &emul_netbsd) {
209 1.1 pooka code = sy - em->e_sysent;
210 1.1 pooka for (i = 0; i < __arraycount(autoload); i++) {
211 1.1 pooka if (autoload[i].al_code != code) {
212 1.1 pooka continue;
213 1.1 pooka }
214 1.1 pooka if (module_autoload(autoload[i].al_module,
215 1.1 pooka MODULE_CLASS_ANY) != 0 ||
216 1.1 pooka sy->sy_call == sys_nomodule) {
217 1.1 pooka break;
218 1.1 pooka }
219 1.1 pooka mutex_exit(&module_lock);
220 1.1 pooka return ERESTART;
221 1.1 pooka }
222 1.1 pooka }
223 1.1 pooka mutex_exit(&module_lock);
224 1.1 pooka #endif /* MODULAR */
225 1.1 pooka
226 1.1 pooka return sys_nosys(l, v, retval);
227 1.1 pooka }
228 1.1 pooka
229 1.1 pooka int
230 1.1 pooka syscall_establish(const struct emul *em, const struct syscall_package *sp)
231 1.1 pooka {
232 1.1 pooka struct sysent *sy;
233 1.1 pooka int i;
234 1.1 pooka
235 1.1 pooka KASSERT(mutex_owned(&module_lock));
236 1.1 pooka
237 1.1 pooka if (em == NULL) {
238 1.1 pooka em = &emul_netbsd;
239 1.1 pooka }
240 1.1 pooka sy = em->e_sysent;
241 1.1 pooka
242 1.1 pooka /*
243 1.1 pooka * Ensure that all preconditions are valid, since this is
244 1.1 pooka * an all or nothing deal. Once a system call is entered,
245 1.1 pooka * it can become busy and we could be unable to remove it
246 1.1 pooka * on error.
247 1.1 pooka */
248 1.1 pooka for (i = 0; sp[i].sp_call != NULL; i++) {
249 1.1 pooka if (sy[sp[i].sp_code].sy_call != sys_nomodule) {
250 1.1 pooka #ifdef DIAGNOSTIC
251 1.1 pooka printf("syscall %d is busy\n", sp[i].sp_code);
252 1.1 pooka #endif
253 1.1 pooka return EBUSY;
254 1.1 pooka }
255 1.1 pooka }
256 1.1 pooka /* Everything looks good, patch them in. */
257 1.1 pooka for (i = 0; sp[i].sp_call != NULL; i++) {
258 1.1 pooka sy[sp[i].sp_code].sy_call = sp[i].sp_call;
259 1.1 pooka }
260 1.1 pooka
261 1.1 pooka return 0;
262 1.1 pooka }
263 1.1 pooka
264 1.1 pooka int
265 1.1 pooka syscall_disestablish(const struct emul *em, const struct syscall_package *sp)
266 1.1 pooka {
267 1.1 pooka struct sysent *sy;
268 1.1 pooka uint64_t where;
269 1.1 pooka lwp_t *l;
270 1.1 pooka int i;
271 1.1 pooka
272 1.1 pooka KASSERT(mutex_owned(&module_lock));
273 1.1 pooka
274 1.1 pooka if (em == NULL) {
275 1.1 pooka em = &emul_netbsd;
276 1.1 pooka }
277 1.1 pooka sy = em->e_sysent;
278 1.1 pooka
279 1.1 pooka /*
280 1.1 pooka * First, patch the system calls to sys_nomodule to gate further
281 1.1 pooka * activity.
282 1.1 pooka */
283 1.1 pooka for (i = 0; sp[i].sp_call != NULL; i++) {
284 1.1 pooka KASSERT(sy[sp[i].sp_code].sy_call == sp[i].sp_call);
285 1.1 pooka sy[sp[i].sp_code].sy_call = sys_nomodule;
286 1.1 pooka }
287 1.1 pooka
288 1.1 pooka /*
289 1.1 pooka * Run a cross call to cycle through all CPUs. This does two
290 1.1 pooka * things: lock activity provides a barrier and makes our update
291 1.1 pooka * of sy_call visible to all CPUs, and upon return we can be sure
292 1.1 pooka * that we see pertinent values of l_sysent posted by remote CPUs.
293 1.1 pooka */
294 1.1 pooka where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
295 1.1 pooka xc_wait(where);
296 1.1 pooka
297 1.1 pooka /*
298 1.1 pooka * Now it's safe to check l_sysent. Run through all LWPs and see
299 1.1 pooka * if anyone is still using the system call.
300 1.1 pooka */
301 1.1 pooka for (i = 0; sp[i].sp_call != NULL; i++) {
302 1.1 pooka mutex_enter(proc_lock);
303 1.1 pooka LIST_FOREACH(l, &alllwp, l_list) {
304 1.1 pooka if (l->l_sysent == &sy[sp[i].sp_code]) {
305 1.1 pooka break;
306 1.1 pooka }
307 1.1 pooka }
308 1.1 pooka mutex_exit(proc_lock);
309 1.1 pooka if (l == NULL) {
310 1.1 pooka continue;
311 1.1 pooka }
312 1.1 pooka /*
313 1.1 pooka * We lose: one or more calls are still in use. Put back
314 1.1 pooka * the old entrypoints and act like nothing happened.
315 1.1 pooka * When we drop module_lock, any system calls held in
316 1.1 pooka * sys_nomodule() will be restarted.
317 1.1 pooka */
318 1.1 pooka for (i = 0; sp[i].sp_call != NULL; i++) {
319 1.1 pooka sy[sp[i].sp_code].sy_call = sp[i].sp_call;
320 1.1 pooka }
321 1.1 pooka return EBUSY;
322 1.1 pooka }
323 1.1 pooka
324 1.1 pooka return 0;
325 1.1 pooka }
326