xref: /netbsd-src/sys/kern/sys_module.c (revision 62a8debe1dc62962e18a1c918def78666141273b)
1 /*	$NetBSD: sys_module.c,v 1.11 2010/03/05 18:35:01 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * System calls relating to loadable modules.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: sys_module.c,v 1.11 2010/03/05 18:35:01 pooka Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/namei.h>
40 #include <sys/kmem.h>
41 #include <sys/kobj.h>
42 #include <sys/module.h>
43 #include <sys/syscall.h>
44 #include <sys/syscallargs.h>
45 
46 static int
47 handle_modctl_load(modctl_load_t *ml)
48 {
49 	char *path;
50 	char *props;
51 	int error;
52 	prop_dictionary_t dict;
53 	size_t propslen;
54 
55 	if ((ml->ml_props != NULL && ml->ml_propslen == 0) ||
56 	    (ml->ml_props == NULL && ml->ml_propslen > 0)) {
57 		error = EINVAL;
58 		goto out1;
59 	}
60 
61 	path = PNBUF_GET();
62 	error = copyinstr(ml->ml_filename, path, MAXPATHLEN, NULL);
63 	if (error != 0)
64 		goto out2;
65 
66 	if (ml->ml_props != NULL) {
67 		propslen = ml->ml_propslen + 1;
68 		props = (char *)kmem_alloc(propslen, KM_SLEEP);
69 		if (props == NULL) {
70 			error = ENOMEM;
71 			goto out2;
72 		}
73 
74 		error = copyinstr(ml->ml_props, props, propslen, NULL);
75 		if (error != 0)
76 			goto out3;
77 
78 		dict = prop_dictionary_internalize(props);
79 		if (dict == NULL) {
80 			error = EINVAL;
81 			goto out3;
82 		}
83 	} else {
84 		dict = NULL;
85 		props = NULL;
86 	}
87 
88 	error = module_load(path, ml->ml_flags, dict, MODULE_CLASS_ANY);
89 
90 	if (dict != NULL) {
91 		prop_object_release(dict);
92 	}
93 
94 out3:
95 	if (props != NULL) {
96 		kmem_free(props, propslen);
97 	}
98 out2:
99 	PNBUF_PUT(path);
100 out1:
101 
102 	return error;
103 }
104 
105 int
106 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap,
107 	   register_t *retval)
108 {
109 	/* {
110 		syscallarg(int)		cmd;
111 		syscallarg(void *)	arg;
112 	} */
113 	char buf[MAXMODNAME];
114 	size_t mslen;
115 	module_t *mod;
116 	modinfo_t *mi;
117 	modstat_t *ms, *mso;
118 	vaddr_t addr;
119 	size_t size;
120 	struct iovec iov;
121 	modctl_load_t ml;
122 	int error;
123 	void *arg;
124 
125 	arg = SCARG(uap, arg);
126 
127 	switch (SCARG(uap, cmd)) {
128 	case MODCTL_LOAD:
129 		error = copyin(arg, &ml, sizeof(ml));
130 		if (error != 0)
131 			break;
132 		error = handle_modctl_load(&ml);
133 		break;
134 
135 	case MODCTL_UNLOAD:
136 		error = copyinstr(arg, buf, sizeof(buf), NULL);
137 		if (error == 0) {
138 			error = module_unload(buf);
139 		}
140 		break;
141 
142 	case MODCTL_STAT:
143 		error = copyin(arg, &iov, sizeof(iov));
144 		if (error != 0) {
145 			break;
146 		}
147 		mutex_enter(&module_lock);
148 		mslen = (module_count+module_builtinlist+1) * sizeof(modstat_t);
149 		mso = kmem_zalloc(mslen, KM_SLEEP);
150 		if (mso == NULL) {
151 			mutex_exit(&module_lock);
152 			return ENOMEM;
153 		}
154 		ms = mso;
155 		TAILQ_FOREACH(mod, &module_list, mod_chain) {
156 			mi = mod->mod_info;
157 			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
158 			if (mi->mi_required != NULL) {
159 				strlcpy(ms->ms_required, mi->mi_required,
160 				    sizeof(ms->ms_required));
161 			}
162 			if (mod->mod_kobj != NULL) {
163 				kobj_stat(mod->mod_kobj, &addr, &size);
164 				ms->ms_addr = addr;
165 				ms->ms_size = size;
166 			}
167 			ms->ms_class = mi->mi_class;
168 			ms->ms_refcnt = mod->mod_refcnt;
169 			ms->ms_source = mod->mod_source;
170 			ms++;
171 		}
172 		TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
173 			mi = mod->mod_info;
174 			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
175 			if (mi->mi_required != NULL) {
176 				strlcpy(ms->ms_required, mi->mi_required,
177 				    sizeof(ms->ms_required));
178 			}
179 			if (mod->mod_kobj != NULL) {
180 				kobj_stat(mod->mod_kobj, &addr, &size);
181 				ms->ms_addr = addr;
182 				ms->ms_size = size;
183 			}
184 			ms->ms_class = mi->mi_class;
185 			ms->ms_refcnt = -1;
186 			KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
187 			ms->ms_source = mod->mod_source;
188 			ms++;
189 		}
190 		mutex_exit(&module_lock);
191 		error = copyout(mso, iov.iov_base,
192 		    min(mslen - sizeof(modstat_t), iov.iov_len));
193 		kmem_free(mso, mslen);
194 		if (error == 0) {
195 			iov.iov_len = mslen - sizeof(modstat_t);
196 			error = copyout(&iov, arg, sizeof(iov));
197 		}
198 		break;
199 
200 	default:
201 		error = EINVAL;
202 		break;
203 	}
204 
205 	return error;
206 }
207