Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_module.c revision 1.2.2.2
      1 /*	$NetBSD: netbsd32_module.c,v 1.2.2.2 2015/09/22 12:05:55 skrll 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  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_module.c,v 1.2.2.2 2015/09/22 12:05:55 skrll Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/dirent.h>
     36 #include <sys/kauth.h>
     37 #include <sys/module.h>
     38 #include <sys/kobj.h>
     39 
     40 #include <compat/netbsd32/netbsd32.h>
     41 #include <compat/netbsd32/netbsd32_syscall.h>
     42 #include <compat/netbsd32/netbsd32_syscallargs.h>
     43 #include <compat/netbsd32/netbsd32_conv.h>
     44 
     45 int
     46 netbsd32_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
     47 	register_t *result)
     48 {
     49 	/* {
     50 		syscallarg(int) cmd;
     51 		syscallarg(netbsd32_voidp) arg;
     52 	} */
     53 	char buf[MAXMODNAME];
     54 	size_t mslen;
     55 	module_t *mod;
     56 	modinfo_t *mi;
     57 	modstat_t *ms, *mso;
     58 	vaddr_t addr;
     59 	size_t size;
     60 	struct netbsd32_iovec iov;
     61 	struct netbsd32_modctl_load ml;
     62 	int error;
     63 	void *arg;
     64 #ifdef MODULAR
     65 	uintptr_t loadtype;
     66 #endif
     67 
     68 	arg = SCARG_P32(uap, arg);
     69 
     70 	switch (SCARG(uap, cmd)) {
     71 	case MODCTL_LOAD:
     72 		error = copyin(arg, &ml, sizeof(ml));
     73 		if (error != 0)
     74 			break;
     75 		error = handle_modctl_load(NETBSD32PTR64(ml.ml_filename),
     76 		     ml.ml_flags, NETBSD32PTR64(ml.ml_props), ml.ml_propslen);
     77 		break;
     78 
     79 	case MODCTL_UNLOAD:
     80 		error = copyinstr(arg, buf, sizeof(buf), NULL);
     81 		if (error == 0) {
     82 			error = module_unload(buf);
     83 		}
     84 		break;
     85 
     86 	case MODCTL_STAT:
     87 		error = copyin(arg, &iov, sizeof(iov));
     88 		if (error != 0) {
     89 			break;
     90 		}
     91 		kernconfig_lock();
     92 		mslen = (module_count+module_builtinlist+1) * sizeof(modstat_t);
     93 		mso = kmem_zalloc(mslen, KM_SLEEP);
     94 		if (mso == NULL) {
     95 			kernconfig_unlock();
     96 			return ENOMEM;
     97 		}
     98 		ms = mso;
     99 		TAILQ_FOREACH(mod, &module_list, mod_chain) {
    100 			mi = mod->mod_info;
    101 			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
    102 			if (mi->mi_required != NULL) {
    103 				strlcpy(ms->ms_required, mi->mi_required,
    104 				    sizeof(ms->ms_required));
    105 			}
    106 			if (mod->mod_kobj != NULL) {
    107 				kobj_stat(mod->mod_kobj, &addr, &size);
    108 				ms->ms_addr = addr;
    109 				ms->ms_size = size;
    110 			}
    111 			ms->ms_class = mi->mi_class;
    112 			ms->ms_refcnt = mod->mod_refcnt;
    113 			ms->ms_source = mod->mod_source;
    114 			ms++;
    115 		}
    116 		TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    117 			mi = mod->mod_info;
    118 			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
    119 			if (mi->mi_required != NULL) {
    120 				strlcpy(ms->ms_required, mi->mi_required,
    121 				    sizeof(ms->ms_required));
    122 			}
    123 			if (mod->mod_kobj != NULL) {
    124 				kobj_stat(mod->mod_kobj, &addr, &size);
    125 				ms->ms_addr = addr;
    126 				ms->ms_size = size;
    127 			}
    128 			ms->ms_class = mi->mi_class;
    129 			ms->ms_refcnt = -1;
    130 			KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
    131 			ms->ms_source = mod->mod_source;
    132 			ms++;
    133 		}
    134 		kernconfig_unlock();
    135 		error = copyout(mso, NETBSD32PTR64(iov.iov_base),
    136 		    min(mslen - sizeof(modstat_t), iov.iov_len));
    137 		kmem_free(mso, mslen);
    138 		if (error == 0) {
    139 			iov.iov_len = mslen - sizeof(modstat_t);
    140 			error = copyout(&iov, arg, sizeof(iov));
    141 		}
    142 		break;
    143 
    144 	case MODCTL_EXISTS:
    145 #ifndef MODULAR
    146 		error = ENOSYS;
    147 #else
    148 		loadtype = (uintptr_t)arg;
    149 		switch (loadtype) {	/* 0 = modload, 1 = autoload */
    150 		case 0:			/* FALLTHROUGH */
    151 		case 1:
    152 			error = kauth_authorize_system(kauth_cred_get(),
    153 			     KAUTH_SYSTEM_MODULE, 0,
    154 			     (void *)(uintptr_t)MODCTL_LOAD,
    155 			     (void *)loadtype, NULL);
    156 			break;
    157 
    158 		default:
    159 			error = EINVAL;
    160 			break;
    161 		}
    162 #endif
    163 		break;
    164 
    165 	default:
    166 		error = EINVAL;
    167 		break;
    168 	}
    169 
    170 	return error;
    171 }
    172 
    173 
    174