Home | History | Annotate | Line # | Download | only in kern
kern_syscall.c revision 1.4.6.2
      1 /*	$NetBSD: kern_syscall.c,v 1.4.6.2 2010/08/11 22:54:41 yamt 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: kern_syscall.c,v 1.4.6.2 2010/08/11 22:54:41 yamt Exp $");
     34 
     35 #include "opt_modular.h"
     36 
     37 /* XXX To get syscall prototypes. */
     38 #define SYSVSHM
     39 #define SYSVSEM
     40 #define SYSVMSG
     41 
     42 #include <sys/param.h>
     43 #include <sys/module.h>
     44 #include <sys/sched.h>
     45 #include <sys/syscall.h>
     46 #include <sys/syscallargs.h>
     47 #include <sys/syscallvar.h>
     48 #include <sys/xcall.h>
     49 
     50 int
     51 sys_nomodule(struct lwp *l, const void *v, register_t *retval)
     52 {
     53 #ifdef MODULAR
     54 	static struct {
     55 		u_int		al_code;
     56 		const char	*al_module;
     57 	} const autoload[] = {
     58 	    { SYS_aio_cancel, "aio" },
     59 	    { SYS_aio_error, "aio" },
     60 	    { SYS_aio_fsync, "aio" },
     61 	    { SYS_aio_read, "aio" },
     62 	    { SYS_aio_return, "aio" },
     63 	    { SYS___aio_suspend50, "aio" },
     64 	    { SYS_aio_write, "aio" },
     65 	    { SYS_lio_listio, "aio" },
     66 	    { SYS_mq_open, "mqueue" },
     67 	    { SYS_mq_close, "mqueue" },
     68 	    { SYS_mq_unlink, "mqueue" },
     69 	    { SYS_mq_getattr, "mqueue" },
     70 	    { SYS_mq_setattr, "mqueue" },
     71 	    { SYS_mq_notify, "mqueue" },
     72 	    { SYS_mq_send, "mqueue" },
     73 	    { SYS_mq_receive, "mqueue" },
     74 	    { SYS___mq_timedsend50, "mqueue" },
     75 	    { SYS___mq_timedreceive50, "mqueue" },
     76 	    { SYS_compat_43_fstat43, "compat" },
     77 	    { SYS_compat_43_lstat43, "compat" },
     78 	    { SYS_compat_43_oaccept, "compat" },
     79 	    { SYS_compat_43_ocreat, "compat" },
     80 	    { SYS_compat_43_oftruncate, "compat" },
     81 	    { SYS_compat_43_ogetdirentries, "compat" },
     82 	    { SYS_compat_43_ogetdtablesize, "compat" },
     83 	    { SYS_compat_43_ogethostid, "compat" },
     84 	    { SYS_compat_43_ogethostname, "compat" },
     85 	    { SYS_compat_43_ogetkerninfo, "compat" },
     86 	    { SYS_compat_43_ogetpagesize, "compat" },
     87 	    { SYS_compat_43_ogetpeername, "compat" },
     88 	    { SYS_compat_43_ogetrlimit, "compat" },
     89 	    { SYS_compat_43_ogetsockname, "compat" },
     90 	    { SYS_compat_43_okillpg, "compat" },
     91 	    { SYS_compat_43_olseek, "compat" },
     92 	    { SYS_compat_43_ommap, "compat" },
     93 	    { SYS_compat_43_oquota, "compat" },
     94 	    { SYS_compat_43_orecv, "compat" },
     95 	    { SYS_compat_43_orecvfrom, "compat" },
     96 	    { SYS_compat_43_orecvmsg, "compat" },
     97 	    { SYS_compat_43_osend, "compat" },
     98 	    { SYS_compat_43_osendmsg, "compat" },
     99 	    { SYS_compat_43_osethostid, "compat" },
    100 	    { SYS_compat_43_osethostname, "compat" },
    101 	    { SYS_compat_43_osetrlimit, "compat" },
    102 	    { SYS_compat_43_osigblock, "compat" },
    103 	    { SYS_compat_43_osigsetmask, "compat" },
    104 	    { SYS_compat_43_osigstack, "compat" },
    105 	    { SYS_compat_43_osigvec, "compat" },
    106 	    { SYS_compat_43_otruncate, "compat" },
    107 	    { SYS_compat_43_owait, "compat" },
    108 	    { SYS_compat_43_stat43, "compat" },
    109 	    { SYS_compat_09_ogetdomainname, "compat" },
    110 	    { SYS_compat_09_osetdomainname, "compat" },
    111 	    { SYS_compat_09_ouname, "compat" },
    112 #ifndef _LP64
    113 	    { SYS_compat_10_omsgsys, "compat" },
    114 	    { SYS_compat_10_osemsys, "compat" },
    115 	    { SYS_compat_10_oshmsys, "compat" },
    116 #endif
    117 	    { SYS_compat_12_fstat12, "compat" },
    118 	    { SYS_compat_12_getdirentries, "compat" },
    119 	    { SYS_compat_12_lstat12, "compat" },
    120 	    { SYS_compat_12_msync, "compat" },
    121 	    { SYS_compat_12_oreboot, "compat" },
    122 	    { SYS_compat_12_oswapon, "compat" },
    123 	    { SYS_compat_12_stat12, "compat" },
    124 	    { SYS_compat_13_sigaction13, "compat" },
    125 	    { SYS_compat_13_sigaltstack13, "compat" },
    126 	    { SYS_compat_13_sigpending13, "compat" },
    127 	    { SYS_compat_13_sigprocmask13, "compat" },
    128 	    { SYS_compat_13_sigreturn13, "compat" },
    129 	    { SYS_compat_13_sigsuspend13, "compat" },
    130 	    { SYS_compat_14___semctl, "compat" },
    131 	    { SYS_compat_14_msgctl, "compat" },
    132 	    { SYS_compat_14_shmctl, "compat" },
    133 	    { SYS_compat_16___sigaction14, "compat" },
    134 	    { SYS_compat_16___sigreturn14, "compat" },
    135 	    { SYS_compat_20_fhstatfs, "compat" },
    136 	    { SYS_compat_20_fstatfs, "compat" },
    137 	    { SYS_compat_20_getfsstat, "compat" },
    138 	    { SYS_compat_20_statfs, "compat" },
    139 	    { SYS_compat_30___fhstat30, "compat" },
    140 	    { SYS_compat_30___fstat13, "compat" },
    141 	    { SYS_compat_30___lstat13, "compat" },
    142 	    { SYS_compat_30___stat13, "compat" },
    143 	    { SYS_compat_30_fhopen, "compat" },
    144 	    { SYS_compat_30_fhstat, "compat" },
    145 	    { SYS_compat_30_fhstatvfs1, "compat" },
    146 	    { SYS_compat_30_getdents, "compat" },
    147 	    { SYS_compat_30_getfh, "compat" },
    148 	    { SYS_compat_30_socket, "compat" },
    149 	    { SYS_compat_40_mount, "compat" },
    150 	    { SYS_compat_50_wait4, "compat" },
    151 	    { SYS_compat_50_mknod, "compat" },
    152 	    { SYS_compat_50_setitimer, "compat" },
    153 	    { SYS_compat_50_getitimer, "compat" },
    154 	    { SYS_compat_50_select, "compat" },
    155 	    { SYS_compat_50_gettimeofday, "compat" },
    156 	    { SYS_compat_50_getrusage, "compat" },
    157 	    { SYS_compat_50_settimeofday, "compat" },
    158 	    { SYS_compat_50_utimes, "compat" },
    159 	    { SYS_compat_50_adjtime, "compat" },
    160 	    { SYS_compat_50_lfs_segwait, "compat" },
    161 	    { SYS_compat_50_futimes, "compat" },
    162 	    { SYS_compat_50_clock_gettime, "compat" },
    163 	    { SYS_compat_50_clock_settime, "compat" },
    164 	    { SYS_compat_50_clock_getres, "compat" },
    165 	    { SYS_compat_50_timer_settime, "compat" },
    166 	    { SYS_compat_50_timer_gettime, "compat" },
    167 	    { SYS_compat_50_nanosleep, "compat" },
    168 	    { SYS_compat_50___sigtimedwait, "compat" },
    169 	    { SYS_compat_50_mq_timedsend, "compat" },
    170 	    { SYS_compat_50_mq_timedreceive, "compat" },
    171 	    { SYS_compat_50_lutimes, "compat" },
    172 	    { SYS_compat_50_____semctl13, "compat" },
    173 	    { SYS_compat_50___msgctl13, "compat" },
    174 	    { SYS_compat_50___shmctl13, "compat" },
    175 	    { SYS_compat_50__lwp_park, "compat" },
    176 	    { SYS_compat_50_kevent, "compat" },
    177 	    { SYS_compat_50_pselect, "compat" },
    178 	    { SYS_compat_50_pollts, "compat" },
    179 	    { SYS_compat_50___stat30, "compat" },
    180 	    { SYS_compat_50___fstat30, "compat" },
    181 	    { SYS_compat_50___lstat30, "compat" },
    182 	    { SYS_compat_50___ntp_gettime30, "compat" },
    183 	    { SYS_compat_50___fhstat40, "compat" },
    184 	    { SYS_compat_50_aio_suspend, "compat" },
    185 	    { SYS__ksem_init, "ksem" },
    186 	    { SYS__ksem_open, "ksem" },
    187 	    { SYS__ksem_unlink, "ksem" },
    188 	    { SYS__ksem_close, "ksem" },
    189 	    { SYS__ksem_post, "ksem" },
    190 	    { SYS__ksem_wait, "ksem" },
    191 	    { SYS__ksem_trywait, "ksem" },
    192 	    { SYS__ksem_getvalue, "ksem" },
    193 	    { SYS__ksem_destroy, "ksem" },
    194 	    { SYS_nfssvc, "nfsserver" },
    195 	};
    196 	const struct sysent *sy;
    197 	const struct emul *em;
    198 	int code, i;
    199 
    200 	/*
    201 	 * Restart the syscall if we interrupted a module unload that
    202 	 * failed.  Acquiring module_lock delays us until any unload
    203 	 * has been completed or rolled back.
    204 	 */
    205 	mutex_enter(&module_lock);
    206 	sy = l->l_sysent;
    207 	if (sy->sy_call != sys_nomodule) {
    208 		mutex_exit(&module_lock);
    209 		return ERESTART;
    210 	}
    211 	/*
    212 	 * Try to autoload a module to satisfy the request.  If it
    213 	 * works, retry the request.
    214 	 */
    215 	em = l->l_proc->p_emul;
    216 	if (em == &emul_netbsd) {
    217 		code = sy - em->e_sysent;
    218 		for (i = 0; i < __arraycount(autoload); i++) {
    219 			if (autoload[i].al_code != code) {
    220 				continue;
    221 			}
    222 			if (module_autoload(autoload[i].al_module,
    223 			    MODULE_CLASS_ANY) != 0 ||
    224 			    sy->sy_call == sys_nomodule) {
    225 			    	break;
    226 			}
    227 			mutex_exit(&module_lock);
    228 			return ERESTART;
    229 		}
    230 	}
    231 	mutex_exit(&module_lock);
    232 #endif	/* MODULAR */
    233 
    234 	return sys_nosys(l, v, retval);
    235 }
    236 
    237 int
    238 syscall_establish(const struct emul *em, const struct syscall_package *sp)
    239 {
    240 	struct sysent *sy;
    241 	int i;
    242 
    243 	KASSERT(mutex_owned(&module_lock));
    244 
    245 	if (em == NULL) {
    246 		em = &emul_netbsd;
    247 	}
    248 	sy = em->e_sysent;
    249 
    250 	/*
    251 	 * Ensure that all preconditions are valid, since this is
    252 	 * an all or nothing deal.  Once a system call is entered,
    253 	 * it can become busy and we could be unable to remove it
    254 	 * on error.
    255 	 */
    256 	for (i = 0; sp[i].sp_call != NULL; i++) {
    257 		if (sy[sp[i].sp_code].sy_call != sys_nomodule) {
    258 #ifdef DIAGNOSTIC
    259 			printf("syscall %d is busy\n", sp[i].sp_code);
    260 #endif
    261 			return EBUSY;
    262 		}
    263 	}
    264 	/* Everything looks good, patch them in. */
    265 	for (i = 0; sp[i].sp_call != NULL; i++) {
    266 		sy[sp[i].sp_code].sy_call = sp[i].sp_call;
    267 	}
    268 
    269 	return 0;
    270 }
    271 
    272 int
    273 syscall_disestablish(const struct emul *em, const struct syscall_package *sp)
    274 {
    275 	struct sysent *sy;
    276 	uint64_t where;
    277 	lwp_t *l;
    278 	int i;
    279 
    280 	KASSERT(mutex_owned(&module_lock));
    281 
    282 	if (em == NULL) {
    283 		em = &emul_netbsd;
    284 	}
    285 	sy = em->e_sysent;
    286 
    287 	/*
    288 	 * First, patch the system calls to sys_nomodule to gate further
    289 	 * activity.
    290 	 */
    291 	for (i = 0; sp[i].sp_call != NULL; i++) {
    292 		KASSERT(sy[sp[i].sp_code].sy_call == sp[i].sp_call);
    293 		sy[sp[i].sp_code].sy_call = sys_nomodule;
    294 	}
    295 
    296 	/*
    297 	 * Run a cross call to cycle through all CPUs.  This does two
    298 	 * things: lock activity provides a barrier and makes our update
    299 	 * of sy_call visible to all CPUs, and upon return we can be sure
    300 	 * that we see pertinent values of l_sysent posted by remote CPUs.
    301 	 */
    302 	where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    303 	xc_wait(where);
    304 
    305 	/*
    306 	 * Now it's safe to check l_sysent.  Run through all LWPs and see
    307 	 * if anyone is still using the system call.
    308 	 */
    309 	for (i = 0; sp[i].sp_call != NULL; i++) {
    310 		mutex_enter(proc_lock);
    311 		LIST_FOREACH(l, &alllwp, l_list) {
    312 			if (l->l_sysent == &sy[sp[i].sp_code]) {
    313 				break;
    314 			}
    315 		}
    316 		mutex_exit(proc_lock);
    317 		if (l == NULL) {
    318 			continue;
    319 		}
    320 		/*
    321 		 * We lose: one or more calls are still in use.  Put back
    322 		 * the old entrypoints and act like nothing happened.
    323 		 * When we drop module_lock, any system calls held in
    324 		 * sys_nomodule() will be restarted.
    325 		 */
    326 		for (i = 0; sp[i].sp_call != NULL; i++) {
    327 			sy[sp[i].sp_code].sy_call = sp[i].sp_call;
    328 		}
    329 		return EBUSY;
    330 	}
    331 
    332 	return 0;
    333 }
    334