Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_module.c revision 1.6.2.2
      1  1.6.2.2  pgoyette /*	$NetBSD: netbsd32_module.c,v 1.6.2.2 2018/09/06 06:55:46 pgoyette 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.6.2.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: netbsd32_module.c,v 1.6.2.2 2018/09/06 06:55:46 pgoyette 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.6.2.1  pgoyette #ifdef COMPAT_80
     46  1.6.2.1  pgoyette static void
     47  1.6.2.1  pgoyette copy_oalias(omodstat_t *oms, const char * const *aliasp, modinfo_t *mi,
     48  1.6.2.1  pgoyette     module_t *mod)
     49  1.6.2.1  pgoyette {
     50  1.6.2.1  pgoyette 
     51  1.6.2.1  pgoyette 	strlcpy(oms->oms_name, *aliasp, sizeof(oms->oms_name));
     52  1.6.2.1  pgoyette 	strlcpy(oms->oms_required, mi->mi_name, sizeof(oms->oms_required));
     53  1.6.2.1  pgoyette 	oms->oms_class = mi->mi_class;
     54  1.6.2.1  pgoyette 	oms->oms_source = mod->mod_source;
     55  1.6.2.1  pgoyette 	oms->oms_flags = mod->mod_flags | MODFLG_IS_ALIAS;
     56  1.6.2.1  pgoyette }
     57  1.6.2.1  pgoyette 
     58      1.3      maxv static int
     59  1.6.2.1  pgoyette modctl32_handle_ostat(int cmd, struct netbsd32_iovec *iov, void *arg)
     60      1.3      maxv {
     61  1.6.2.1  pgoyette 	omodstat_t *oms, *omso;
     62      1.3      maxv 	modinfo_t *mi;
     63      1.3      maxv 	module_t *mod;
     64      1.3      maxv 	vaddr_t addr;
     65      1.3      maxv 	size_t size;
     66  1.6.2.1  pgoyette 	size_t omslen;
     67  1.6.2.1  pgoyette 	size_t used;
     68      1.3      maxv 	int error;
     69  1.6.2.1  pgoyette 	int omscnt;
     70      1.6      maxv 	bool stataddr;
     71  1.6.2.1  pgoyette 	const char * const *aliasp;
     72  1.6.2.1  pgoyette 	const char *suffix = "...";
     73  1.6.2.1  pgoyette 
     74  1.6.2.1  pgoyette 	if (cmd != MODCTL_OSTAT)
     75  1.6.2.1  pgoyette 		return EINVAL;
     76      1.6      maxv 
     77      1.6      maxv 	/* If not privileged, don't expose kernel addresses. */
     78      1.6      maxv 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
     79      1.6      maxv 	    0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
     80      1.6      maxv 	stataddr = (error == 0);
     81      1.3      maxv 
     82      1.3      maxv 	kernconfig_lock();
     83  1.6.2.1  pgoyette 	omscnt = 0;
     84      1.3      maxv 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
     85  1.6.2.1  pgoyette 		omscnt++;
     86      1.3      maxv 		mi = mod->mod_info;
     87  1.6.2.1  pgoyette 		if ((aliasp = *mi->mi_aliases) != NULL) {
     88  1.6.2.1  pgoyette 			while (*aliasp++ != NULL)
     89  1.6.2.1  pgoyette 			omscnt++;
     90  1.6.2.1  pgoyette 		}
     91  1.6.2.1  pgoyette 	}
     92  1.6.2.1  pgoyette 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
     93  1.6.2.1  pgoyette 		omscnt++;
     94  1.6.2.1  pgoyette 		mi = mod->mod_info;
     95  1.6.2.1  pgoyette 		if ((aliasp = *mi->mi_aliases) != NULL) {
     96  1.6.2.1  pgoyette 			while (*aliasp++ != NULL)
     97  1.6.2.1  pgoyette 			omscnt++;
     98  1.6.2.1  pgoyette 		}
     99  1.6.2.1  pgoyette 	}
    100  1.6.2.1  pgoyette 	omslen = omscnt * sizeof(omodstat_t);
    101  1.6.2.1  pgoyette 	omso = kmem_zalloc(omslen, KM_SLEEP);
    102  1.6.2.1  pgoyette 	oms = omso;
    103  1.6.2.1  pgoyette 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
    104  1.6.2.1  pgoyette 		mi = mod->mod_info;
    105  1.6.2.1  pgoyette 		strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
    106      1.3      maxv 		if (mi->mi_required != NULL) {
    107  1.6.2.1  pgoyette 			used = strlcpy(oms->oms_required, mi->mi_required,
    108  1.6.2.1  pgoyette 			    sizeof(oms->oms_required));
    109  1.6.2.1  pgoyette 			if (used >= sizeof(oms->oms_required)) {
    110  1.6.2.1  pgoyette 				oms->oms_required[sizeof(oms->oms_required) -
    111  1.6.2.1  pgoyette 				    strlen(suffix) - 1] = '\0';
    112  1.6.2.1  pgoyette 				strlcat(oms->oms_required, suffix,
    113  1.6.2.1  pgoyette 				    sizeof(oms->oms_required));
    114  1.6.2.1  pgoyette                         }
    115      1.3      maxv 		}
    116      1.6      maxv 		if (mod->mod_kobj != NULL && stataddr) {
    117      1.3      maxv 			kobj_stat(mod->mod_kobj, &addr, &size);
    118  1.6.2.1  pgoyette 			oms->oms_addr = addr;
    119  1.6.2.1  pgoyette 			oms->oms_size = size;
    120      1.3      maxv 		}
    121  1.6.2.1  pgoyette 		oms->oms_class = mi->mi_class;
    122  1.6.2.1  pgoyette 		oms->oms_refcnt = mod->mod_refcnt;
    123  1.6.2.1  pgoyette 		oms->oms_source = mod->mod_source;
    124  1.6.2.1  pgoyette 		oms->oms_flags = mod->mod_flags;
    125  1.6.2.1  pgoyette 		oms++;
    126  1.6.2.1  pgoyette 		aliasp = *mi->mi_aliases;
    127  1.6.2.1  pgoyette 		if (aliasp == NULL)
    128  1.6.2.1  pgoyette 			continue;
    129  1.6.2.1  pgoyette 		while (*aliasp) {
    130  1.6.2.1  pgoyette 			copy_oalias(oms, aliasp, mi, mod);
    131  1.6.2.1  pgoyette 			aliasp++;
    132  1.6.2.1  pgoyette 			oms++;
    133  1.6.2.1  pgoyette 		}
    134  1.6.2.1  pgoyette 	}
    135  1.6.2.1  pgoyette 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    136  1.6.2.1  pgoyette 		mi = mod->mod_info;
    137  1.6.2.1  pgoyette 		strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
    138  1.6.2.1  pgoyette 		if (mi->mi_required != NULL) {
    139  1.6.2.1  pgoyette 			used = strlcpy(oms->oms_required, mi->mi_required,
    140  1.6.2.1  pgoyette 			    sizeof(oms->oms_required));
    141  1.6.2.1  pgoyette 			if (used >= sizeof(oms->oms_required)) {
    142  1.6.2.1  pgoyette 				oms->oms_required[sizeof(oms->oms_required) -
    143  1.6.2.1  pgoyette 				    strlen(suffix) - 1] = '\0';
    144  1.6.2.1  pgoyette 				strlcat(oms->oms_required, suffix,
    145  1.6.2.1  pgoyette 				    sizeof(oms->oms_required));
    146  1.6.2.1  pgoyette                         }
    147  1.6.2.1  pgoyette 		}
    148  1.6.2.1  pgoyette 		if (mod->mod_kobj != NULL && stataddr) {
    149  1.6.2.1  pgoyette 			kobj_stat(mod->mod_kobj, &addr, &size);
    150  1.6.2.1  pgoyette 			oms->oms_addr = addr;
    151  1.6.2.1  pgoyette 			oms->oms_size = size;
    152  1.6.2.1  pgoyette 		}
    153  1.6.2.1  pgoyette 		oms->oms_class = mi->mi_class;
    154  1.6.2.1  pgoyette 		oms->oms_refcnt = -1;
    155  1.6.2.1  pgoyette 		KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
    156  1.6.2.1  pgoyette 		oms->oms_source = mod->mod_source;
    157  1.6.2.1  pgoyette 		oms++;
    158  1.6.2.1  pgoyette 		aliasp = *mi->mi_aliases;
    159  1.6.2.1  pgoyette 		if (aliasp == NULL)
    160  1.6.2.1  pgoyette 			continue;
    161  1.6.2.1  pgoyette 		while (*aliasp) {
    162  1.6.2.1  pgoyette 			copy_oalias(oms, aliasp, mi, mod);
    163  1.6.2.1  pgoyette 			aliasp++;
    164  1.6.2.1  pgoyette 			oms++;
    165  1.6.2.1  pgoyette 		}
    166  1.6.2.1  pgoyette 	}
    167  1.6.2.1  pgoyette 	kernconfig_unlock();
    168  1.6.2.1  pgoyette 	error = copyout(omso, NETBSD32PTR64(iov->iov_base),
    169  1.6.2.2  pgoyette 	    uimin(omslen - sizeof(modstat_t), iov->iov_len));
    170  1.6.2.1  pgoyette 	kmem_free(omso, omslen);
    171  1.6.2.1  pgoyette 	if (error == 0) {
    172  1.6.2.1  pgoyette 		iov->iov_len = omslen - sizeof(modstat_t);
    173  1.6.2.1  pgoyette 		error = copyout(iov, arg, sizeof(*iov));
    174  1.6.2.1  pgoyette 	}
    175  1.6.2.1  pgoyette 
    176  1.6.2.1  pgoyette 	return error;
    177  1.6.2.1  pgoyette }
    178  1.6.2.1  pgoyette #endif	/* COMPAT_80 */
    179  1.6.2.1  pgoyette 
    180  1.6.2.1  pgoyette static void
    181  1.6.2.1  pgoyette copy_alias(modstat_t *ms, const char * const *aliasp, modinfo_t *mi,
    182  1.6.2.1  pgoyette     module_t *mod)
    183  1.6.2.1  pgoyette {
    184  1.6.2.1  pgoyette 
    185  1.6.2.1  pgoyette 	strlcpy(ms->ms_name, *aliasp, sizeof(ms->ms_name));
    186  1.6.2.1  pgoyette 	ms->ms_class = mi->mi_class;
    187  1.6.2.1  pgoyette 	ms->ms_source = mod->mod_source;
    188  1.6.2.1  pgoyette 	ms->ms_flags = mod->mod_flags | MODFLG_IS_ALIAS;
    189  1.6.2.1  pgoyette 	ms->ms_reqoffset = 0;
    190  1.6.2.1  pgoyette }
    191  1.6.2.1  pgoyette 
    192  1.6.2.1  pgoyette static int
    193  1.6.2.1  pgoyette modctl32_handle_stat(struct netbsd32_iovec *iov, void *arg)
    194  1.6.2.1  pgoyette {
    195  1.6.2.1  pgoyette 	int ms_cnt;
    196  1.6.2.1  pgoyette 	modstat_t *ms, *mso;
    197  1.6.2.1  pgoyette 	size_t ms_len;
    198  1.6.2.1  pgoyette 	int req_cnt;
    199  1.6.2.1  pgoyette 	char *req, *reqo;
    200  1.6.2.1  pgoyette 	size_t req_len;
    201  1.6.2.1  pgoyette 	char *out_p;
    202  1.6.2.1  pgoyette 	size_t out_s;
    203  1.6.2.1  pgoyette 
    204  1.6.2.1  pgoyette 	modinfo_t *mi;
    205  1.6.2.1  pgoyette 	module_t *mod;
    206  1.6.2.1  pgoyette 	vaddr_t addr;
    207  1.6.2.1  pgoyette 	size_t size;
    208  1.6.2.1  pgoyette 	size_t used;
    209  1.6.2.1  pgoyette 	int off;
    210  1.6.2.1  pgoyette 	int error;
    211  1.6.2.1  pgoyette 	bool stataddr;
    212  1.6.2.1  pgoyette 	const char * const *aliasp;
    213  1.6.2.1  pgoyette 
    214  1.6.2.1  pgoyette 	/* If not privileged, don't expose kernel addresses. */
    215  1.6.2.1  pgoyette 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    216  1.6.2.1  pgoyette 	    0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
    217  1.6.2.1  pgoyette 	stataddr = (error == 0);
    218  1.6.2.1  pgoyette 
    219  1.6.2.1  pgoyette 	kernconfig_lock();
    220  1.6.2.1  pgoyette 	ms_cnt = 0;
    221  1.6.2.1  pgoyette 	req_len = 1;
    222  1.6.2.1  pgoyette 
    223  1.6.2.1  pgoyette 	/*
    224  1.6.2.1  pgoyette 	 * Count up the number of modstat_t needed, and total size of
    225  1.6.2.1  pgoyette 	 * require_module lists on both active and built-in lists
    226  1.6.2.1  pgoyette 	 */
    227  1.6.2.1  pgoyette 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
    228  1.6.2.1  pgoyette 		ms_cnt++;
    229  1.6.2.1  pgoyette 		mi = mod->mod_info;
    230  1.6.2.1  pgoyette 		if ((aliasp = *mi->mi_aliases) != NULL) {
    231  1.6.2.1  pgoyette 			while (*aliasp++ != NULL)
    232  1.6.2.1  pgoyette 				ms_cnt++;
    233  1.6.2.1  pgoyette 		}
    234  1.6.2.1  pgoyette 		if (mi->mi_required != NULL) {
    235  1.6.2.1  pgoyette 			req_cnt++;
    236  1.6.2.1  pgoyette 			req_len += strlen(mi->mi_required) + 1;
    237  1.6.2.1  pgoyette 		}
    238  1.6.2.1  pgoyette 	}
    239  1.6.2.1  pgoyette 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    240  1.6.2.1  pgoyette 	ms_cnt++;
    241  1.6.2.1  pgoyette 	mi = mod->mod_info;
    242  1.6.2.1  pgoyette 	if ((aliasp = *mi->mi_aliases) != NULL) {
    243  1.6.2.1  pgoyette 		while (*aliasp++ != NULL)
    244  1.6.2.1  pgoyette 			ms_cnt++;
    245  1.6.2.1  pgoyette 		}
    246  1.6.2.1  pgoyette 		if (mi->mi_required != NULL) {
    247  1.6.2.1  pgoyette 			req_cnt++;
    248  1.6.2.1  pgoyette 			req_len += strlen(mi->mi_required) + 1;
    249  1.6.2.1  pgoyette 		}
    250  1.6.2.1  pgoyette 	}
    251  1.6.2.1  pgoyette 
    252  1.6.2.1  pgoyette 	/* Allocate internal buffers to hold all the output data */
    253  1.6.2.1  pgoyette 	ms_len = ms_cnt * sizeof(modstat_t);
    254  1.6.2.1  pgoyette 	ms = kmem_zalloc(ms_len, KM_SLEEP);
    255  1.6.2.1  pgoyette 	req = kmem_zalloc(req_len, KM_SLEEP);
    256  1.6.2.1  pgoyette 
    257  1.6.2.1  pgoyette 	mso = ms;
    258  1.6.2.1  pgoyette 	reqo = req++;
    259  1.6.2.1  pgoyette 	off = 1;
    260  1.6.2.1  pgoyette 
    261  1.6.2.1  pgoyette 	/*
    262  1.6.2.1  pgoyette 	 * Load data into our internal buffers for both active and
    263  1.6.2.1  pgoyette 	 * build-in module lists
    264  1.6.2.1  pgoyette 	 */
    265  1.6.2.1  pgoyette 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
    266  1.6.2.1  pgoyette 	mi = mod->mod_info;
    267  1.6.2.1  pgoyette 	strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
    268  1.6.2.1  pgoyette 	if (mi->mi_required != NULL) {
    269  1.6.2.1  pgoyette 		ms->ms_reqoffset = off;
    270  1.6.2.1  pgoyette 		used = strlcpy(req,  mi->mi_required, req_len - off);
    271  1.6.2.1  pgoyette 		KASSERTMSG(used < req_len - off, "reqlist grew!");
    272  1.6.2.1  pgoyette 		off = used + 1;
    273  1.6.2.1  pgoyette 		req += used + 1;
    274  1.6.2.1  pgoyette 	} else
    275  1.6.2.1  pgoyette 		ms->ms_reqoffset = 0;
    276  1.6.2.1  pgoyette 	if (mod->mod_kobj != NULL && stataddr) {
    277  1.6.2.1  pgoyette 		kobj_stat(mod->mod_kobj, &addr, &size);
    278  1.6.2.1  pgoyette 		ms->ms_addr = addr;
    279  1.6.2.1  pgoyette 		ms->ms_size = size;
    280  1.6.2.1  pgoyette 	}
    281  1.6.2.1  pgoyette 	ms->ms_class = mi->mi_class;
    282  1.6.2.1  pgoyette 	ms->ms_refcnt = mod->mod_refcnt;
    283  1.6.2.1  pgoyette 	ms->ms_source = mod->mod_source;
    284  1.6.2.1  pgoyette 	ms->ms_flags = mod->mod_flags;
    285  1.6.2.1  pgoyette 	ms++;
    286  1.6.2.1  pgoyette 	aliasp = *mi->mi_aliases;
    287  1.6.2.1  pgoyette 	if (aliasp == NULL)
    288  1.6.2.1  pgoyette 		continue;
    289  1.6.2.1  pgoyette 	while (*aliasp) {
    290  1.6.2.1  pgoyette 		copy_alias(ms, aliasp, mi, mod);
    291  1.6.2.1  pgoyette 		aliasp++;
    292      1.3      maxv 		ms++;
    293  1.6.2.1  pgoyette 		}
    294      1.3      maxv 	}
    295      1.3      maxv 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    296      1.3      maxv 		mi = mod->mod_info;
    297      1.3      maxv 		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
    298      1.3      maxv 		if (mi->mi_required != NULL) {
    299  1.6.2.1  pgoyette 			ms->ms_reqoffset = off;
    300  1.6.2.1  pgoyette 			used = strlcpy(req,  mi->mi_required, req_len - off);
    301  1.6.2.1  pgoyette 			KASSERTMSG(used < req_len - off, "reqlist grew!");
    302  1.6.2.1  pgoyette 			off += used + 1;
    303  1.6.2.1  pgoyette 			req += used + 1;
    304  1.6.2.1  pgoyette 		} else
    305  1.6.2.1  pgoyette 			ms->ms_reqoffset = 0;
    306      1.6      maxv 		if (mod->mod_kobj != NULL && stataddr) {
    307      1.3      maxv 			kobj_stat(mod->mod_kobj, &addr, &size);
    308      1.3      maxv 			ms->ms_addr = addr;
    309      1.3      maxv 			ms->ms_size = size;
    310      1.3      maxv 		}
    311      1.3      maxv 		ms->ms_class = mi->mi_class;
    312      1.3      maxv 		ms->ms_refcnt = -1;
    313      1.3      maxv 		KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
    314      1.3      maxv 		ms->ms_source = mod->mod_source;
    315      1.3      maxv 		ms++;
    316  1.6.2.1  pgoyette 		aliasp = *mi->mi_aliases;
    317  1.6.2.1  pgoyette 		if (aliasp == NULL)
    318  1.6.2.1  pgoyette 			continue;
    319  1.6.2.1  pgoyette 		while (*aliasp) {
    320  1.6.2.1  pgoyette 			copy_alias(ms, aliasp, mi, mod);
    321  1.6.2.1  pgoyette 			aliasp++;
    322  1.6.2.1  pgoyette 			ms++;
    323  1.6.2.1  pgoyette 		}
    324      1.3      maxv 	}
    325      1.3      maxv 	kernconfig_unlock();
    326  1.6.2.1  pgoyette 
    327  1.6.2.1  pgoyette 	/*
    328  1.6.2.1  pgoyette 	 * Now copyout our internal buffers back to userland
    329  1.6.2.1  pgoyette 	 */
    330  1.6.2.1  pgoyette 	out_p = NETBSD32PTR64(iov->iov_base);
    331  1.6.2.1  pgoyette 	out_s = iov->iov_len;
    332  1.6.2.1  pgoyette 	size = sizeof(ms_cnt);
    333  1.6.2.1  pgoyette 
    334  1.6.2.1  pgoyette 	/* Copy out the count of modstat_t */
    335  1.6.2.1  pgoyette 	if (out_s) {
    336  1.6.2.2  pgoyette 		size = uimin(sizeof(ms_cnt), out_s);
    337  1.6.2.1  pgoyette 		error = copyout(&ms_cnt, out_p, size);
    338  1.6.2.1  pgoyette 		out_p += size;
    339  1.6.2.1  pgoyette 		out_s -= size;
    340  1.6.2.1  pgoyette 	}
    341  1.6.2.1  pgoyette 	/* Copy out the modstat_t array */
    342  1.6.2.1  pgoyette 	if (out_s && error == 0) {
    343  1.6.2.2  pgoyette 		size = uimin(ms_len, out_s);
    344  1.6.2.1  pgoyette 		error = copyout(mso, out_p, size);
    345  1.6.2.1  pgoyette 		out_p += size;
    346  1.6.2.1  pgoyette 		out_s -= size;
    347  1.6.2.1  pgoyette 	}
    348  1.6.2.1  pgoyette 	/* Copy out the "required" strings */
    349  1.6.2.1  pgoyette 	if (out_s && error == 0) {
    350  1.6.2.2  pgoyette 		size = uimin(req_len, out_s);
    351  1.6.2.1  pgoyette 		error = copyout(reqo, out_p, size);
    352  1.6.2.1  pgoyette 		out_p += size;
    353  1.6.2.1  pgoyette 		out_s -= size;
    354  1.6.2.1  pgoyette 	}
    355  1.6.2.1  pgoyette 	kmem_free(mso, ms_len);
    356  1.6.2.1  pgoyette 	kmem_free(reqo, req_len);
    357  1.6.2.1  pgoyette 
    358  1.6.2.1  pgoyette 	/* Finally, update the userland copy of the iovec's length */
    359      1.3      maxv 	if (error == 0) {
    360  1.6.2.1  pgoyette 		iov->iov_len = ms_len + req_len + sizeof(ms_cnt);
    361      1.3      maxv 		error = copyout(iov, arg, sizeof(*iov));
    362      1.3      maxv 	}
    363      1.3      maxv 
    364      1.3      maxv 	return error;
    365      1.3      maxv }
    366      1.3      maxv 
    367  1.6.2.1  pgoyette 
    368      1.1    martin int
    369      1.1    martin netbsd32_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
    370      1.1    martin 	register_t *result)
    371      1.1    martin {
    372      1.1    martin 	/* {
    373      1.1    martin 		syscallarg(int) cmd;
    374      1.1    martin 		syscallarg(netbsd32_voidp) arg;
    375      1.1    martin 	} */
    376      1.1    martin 	char buf[MAXMODNAME];
    377      1.1    martin 	struct netbsd32_iovec iov;
    378      1.1    martin 	struct netbsd32_modctl_load ml;
    379      1.1    martin 	int error;
    380      1.1    martin 	void *arg;
    381      1.1    martin #ifdef MODULAR
    382      1.1    martin 	uintptr_t loadtype;
    383      1.1    martin #endif
    384      1.1    martin 
    385      1.1    martin 	arg = SCARG_P32(uap, arg);
    386      1.1    martin 
    387      1.1    martin 	switch (SCARG(uap, cmd)) {
    388      1.1    martin 	case MODCTL_LOAD:
    389      1.1    martin 		error = copyin(arg, &ml, sizeof(ml));
    390      1.1    martin 		if (error != 0)
    391      1.1    martin 			break;
    392      1.1    martin 		error = handle_modctl_load(NETBSD32PTR64(ml.ml_filename),
    393      1.1    martin 		     ml.ml_flags, NETBSD32PTR64(ml.ml_props), ml.ml_propslen);
    394      1.1    martin 		break;
    395      1.1    martin 
    396      1.1    martin 	case MODCTL_UNLOAD:
    397      1.1    martin 		error = copyinstr(arg, buf, sizeof(buf), NULL);
    398      1.1    martin 		if (error == 0) {
    399      1.1    martin 			error = module_unload(buf);
    400      1.1    martin 		}
    401      1.1    martin 		break;
    402      1.1    martin 
    403  1.6.2.1  pgoyette #ifdef COMPAT_80
    404  1.6.2.1  pgoyette 	case MODCTL_OSTAT:
    405  1.6.2.1  pgoyette 		error = copyin(arg, &iov, sizeof(iov));
    406  1.6.2.1  pgoyette 		if (error != 0) {
    407  1.6.2.1  pgoyette 			break;
    408  1.6.2.1  pgoyette 		}
    409  1.6.2.1  pgoyette 		error = modctl32_handle_ostat(SCARG(uap, cmd), &iov, arg);
    410  1.6.2.1  pgoyette 		break;
    411  1.6.2.1  pgoyette #endif
    412  1.6.2.1  pgoyette 
    413      1.1    martin 	case MODCTL_STAT:
    414      1.1    martin 		error = copyin(arg, &iov, sizeof(iov));
    415      1.1    martin 		if (error != 0) {
    416      1.1    martin 			break;
    417      1.1    martin 		}
    418      1.3      maxv 		error = modctl32_handle_stat(&iov, arg);
    419      1.1    martin 		break;
    420      1.1    martin 
    421      1.1    martin 	case MODCTL_EXISTS:
    422      1.1    martin #ifndef MODULAR
    423      1.1    martin 		error = ENOSYS;
    424      1.1    martin #else
    425      1.1    martin 		loadtype = (uintptr_t)arg;
    426      1.1    martin 		switch (loadtype) {	/* 0 = modload, 1 = autoload */
    427      1.1    martin 		case 0:			/* FALLTHROUGH */
    428      1.1    martin 		case 1:
    429      1.1    martin 			error = kauth_authorize_system(kauth_cred_get(),
    430      1.1    martin 			     KAUTH_SYSTEM_MODULE, 0,
    431      1.1    martin 			     (void *)(uintptr_t)MODCTL_LOAD,
    432      1.1    martin 			     (void *)loadtype, NULL);
    433      1.1    martin 			break;
    434      1.1    martin 
    435      1.1    martin 		default:
    436      1.1    martin 			error = EINVAL;
    437      1.1    martin 			break;
    438      1.1    martin 		}
    439      1.1    martin #endif
    440      1.1    martin 		break;
    441      1.1    martin 
    442      1.1    martin 	default:
    443      1.1    martin 		error = EINVAL;
    444      1.1    martin 		break;
    445      1.1    martin 	}
    446      1.1    martin 
    447      1.1    martin 	return error;
    448      1.1    martin }
    449