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