1 /*- 2 * Copyright (c) 1997 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/kernel.h> 31 #include <sys/systm.h> 32 #include <sys/eventhandler.h> 33 #include <sys/malloc.h> 34 #include <sys/sysproto.h> 35 #include <sys/sysent.h> 36 #include <sys/module.h> 37 #include <sys/linker.h> 38 #include <sys/proc.h> 39 40 #define M_MODULE M_TEMP /* XXX */ 41 42 typedef TAILQ_HEAD(, module) modulelist_t; 43 struct module { 44 TAILQ_ENTRY(module) link; /* chain together all modules */ 45 TAILQ_ENTRY(module) flink; /* all modules in a file */ 46 struct linker_file* file; /* file which contains this module */ 47 int refs; /* reference count */ 48 int id; /* unique id number */ 49 char *name; /* module name */ 50 modeventhand_t handler; /* event handler */ 51 void *arg; /* argument for handler */ 52 modspecific_t data; /* module specific data */ 53 }; 54 55 #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg) 56 57 static modulelist_t modules; 58 static int nextid = 1; 59 60 static void module_shutdown(void*, int); 61 62 static void 63 module_init(void* arg) 64 { 65 TAILQ_INIT(&modules); 66 EVENTHANDLER_REGISTER(shutdown_post_sync, module_shutdown, NULL, 67 SHUTDOWN_PRI_DEFAULT); 68 } 69 70 SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0); 71 72 static void 73 module_shutdown(void* arg1, int arg2) 74 { 75 module_t mod; 76 77 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) 78 MOD_EVENT(mod, MOD_SHUTDOWN); 79 } 80 81 void 82 module_register_init(const void *arg) 83 { 84 const moduledata_t* data = (const moduledata_t*) arg; 85 int error; 86 module_t mod; 87 88 mod = module_lookupbyname(data->name); 89 if (mod == NULL) { 90 #if 0 91 panic("module_register_init: module named %s not found\n", data->name); 92 #else 93 /* temporary kludge until kernel `file' attachment registers modules */ 94 error = module_register(data, linker_kernel_file); 95 if (error) 96 panic("module_register_init: register of module failed! %d", error); 97 mod = module_lookupbyname(data->name); 98 if (mod == NULL) 99 panic("module_register_init: module STILL not found!"); 100 #endif 101 } 102 error = MOD_EVENT(mod, MOD_LOAD); 103 if (error) { 104 MOD_EVENT(mod, MOD_UNLOAD); 105 module_release(mod); 106 printf("module_register_init: MOD_LOAD (%s, %lx, %p) error %d\n", 107 data->name, (u_long)(uintfptr_t)data->evhand, data->priv, error); 108 } 109 } 110 111 int 112 module_register(const moduledata_t *data, linker_file_t container) 113 { 114 size_t namelen; 115 module_t newmod; 116 117 newmod = module_lookupbyname(data->name); 118 if (newmod != NULL) { 119 printf("module_register: module %s already exists!\n", data->name); 120 return EEXIST; 121 } 122 namelen = strlen(data->name) + 1; 123 newmod = (module_t) malloc(sizeof(struct module) + namelen, 124 M_MODULE, M_WAITOK); 125 if (newmod == 0) 126 return ENOMEM; 127 128 newmod->refs = 1; 129 newmod->id = nextid++; 130 newmod->name = (char *) (newmod + 1); 131 strcpy(newmod->name, data->name); 132 newmod->handler = data->evhand; 133 newmod->arg = data->priv; 134 bzero(&newmod->data, sizeof(newmod->data)); 135 TAILQ_INSERT_TAIL(&modules, newmod, link); 136 137 if (container == NULL) 138 container = linker_current_file; 139 if (container) 140 TAILQ_INSERT_TAIL(&container->modules, newmod, flink); 141 newmod->file = container; 142 143 return 0; 144 } 145 146 void 147 module_reference(module_t mod) 148 { 149 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs)); 150 151 mod->refs++; 152 } 153 154 void 155 module_release(module_t mod) 156 { 157 if (mod->refs <= 0) 158 panic("module_release: bad reference count"); 159 160 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs)); 161 162 mod->refs--; 163 if (mod->refs == 0) { 164 TAILQ_REMOVE(&modules, mod, link); 165 if (mod->file) { 166 TAILQ_REMOVE(&mod->file->modules, mod, flink); 167 } 168 free(mod, M_MODULE); 169 } 170 } 171 172 module_t 173 module_lookupbyname(const char* name) 174 { 175 module_t mod; 176 177 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) { 178 if (!strcmp(mod->name, name)) 179 return mod; 180 } 181 182 return 0; 183 } 184 185 module_t 186 module_lookupbyid(int modid) 187 { 188 module_t mod; 189 190 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) { 191 if (mod->id == modid) 192 return mod; 193 } 194 195 return 0; 196 } 197 198 int 199 module_unload(module_t mod) 200 { 201 return MOD_EVENT(mod, MOD_UNLOAD); 202 } 203 204 int 205 module_getid(module_t mod) 206 { 207 return mod->id; 208 } 209 210 module_t 211 module_getfnext(module_t mod) 212 { 213 return TAILQ_NEXT(mod, flink); 214 } 215 216 void 217 module_setspecific(module_t mod, modspecific_t *datap) 218 { 219 mod->data = *datap; 220 } 221 222 /* 223 * Syscalls. 224 */ 225 int 226 modnext(struct proc* p, struct modnext_args* uap) 227 { 228 module_t mod; 229 230 p->p_retval[0] = -1; 231 if (SCARG(uap, modid) == 0) { 232 mod = TAILQ_FIRST(&modules); 233 if (mod) { 234 p->p_retval[0] = mod->id; 235 return 0; 236 } else 237 return ENOENT; 238 } 239 240 mod = module_lookupbyid(SCARG(uap, modid)); 241 if (!mod) 242 return ENOENT; 243 244 if (TAILQ_NEXT(mod, link)) 245 p->p_retval[0] = TAILQ_NEXT(mod, link)->id; 246 else 247 p->p_retval[0] = 0; 248 return 0; 249 } 250 251 int 252 modfnext(struct proc* p, struct modfnext_args* uap) 253 { 254 module_t mod; 255 256 p->p_retval[0] = -1; 257 258 mod = module_lookupbyid(SCARG(uap, modid)); 259 if (!mod) 260 return ENOENT; 261 262 if (TAILQ_NEXT(mod, flink)) 263 p->p_retval[0] = TAILQ_NEXT(mod, flink)->id; 264 else 265 p->p_retval[0] = 0; 266 return 0; 267 } 268 269 struct module_stat_v1 { 270 int version; /* set to sizeof(struct module_stat) */ 271 char name[MAXMODNAME]; 272 int refs; 273 int id; 274 }; 275 276 int 277 modstat(struct proc* p, struct modstat_args* uap) 278 { 279 module_t mod; 280 int error = 0; 281 int namelen; 282 int version; 283 struct module_stat* stat; 284 285 mod = module_lookupbyid(SCARG(uap, modid)); 286 if (!mod) 287 return ENOENT; 288 289 stat = SCARG(uap, stat); 290 291 /* 292 * Check the version of the user's structure. 293 */ 294 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0) 295 goto out; 296 if (version != sizeof(struct module_stat_v1) 297 && version != sizeof(struct module_stat)) { 298 error = EINVAL; 299 goto out; 300 } 301 302 namelen = strlen(mod->name) + 1; 303 if (namelen > MAXMODNAME) 304 namelen = MAXMODNAME; 305 if ((error = copyout(mod->name, &stat->name[0], namelen)) != 0) 306 goto out; 307 308 if ((error = copyout(&mod->refs, &stat->refs, sizeof(int))) != 0) 309 goto out; 310 if ((error = copyout(&mod->id, &stat->id, sizeof(int))) != 0) 311 goto out; 312 313 /* 314 * >v1 stat includes module data. 315 */ 316 if (version == sizeof(struct module_stat)) { 317 if ((error = copyout(&mod->data, &stat->data, sizeof(mod->data))) != 0) 318 goto out; 319 } 320 321 p->p_retval[0] = 0; 322 323 out: 324 return error; 325 } 326 327 int 328 modfind(struct proc* p, struct modfind_args* uap) 329 { 330 int error = 0; 331 char name[MAXMODNAME]; 332 module_t mod; 333 334 if ((error = copyinstr(SCARG(uap, name), name, sizeof name, 0)) != 0) 335 goto out; 336 337 mod = module_lookupbyname(name); 338 if (!mod) 339 error = ENOENT; 340 else 341 p->p_retval[0] = mod->id; 342 343 out: 344 return error; 345 } 346