static int linker_file_register_modules(linker_file_t lf) { struct mod_metadata **start, **stop, **mdp; const moduledata_t *moddata; int first_error, error; KLD_DPF(FILE, ("linker_file_register_modules: registering modules" " in %s\n", lf->filename)); start = SET_BEGIN(modmetadata_set); stop = SET_LIMIT(modmetadata_set); first_error = 0; for (mdp = start; mdp < stop; mdp++) { if ((*mdp)->md_type != MDT_MODULE) continue; moddata = (*mdp)->md_data; KLD_DPF(FILE, ("Registering module %s in %s\n", moddata->name, lf->filename)); error = module_register(moddata, lf); if (error) { printf("Module %s failed to register: %d\n", moddata->name, error); if (first_error == 0) first_error = error; } } return (first_error); }
int linker_file_unload(linker_file_t file) { module_t mod, next; struct common_symbol* cp; int error = 0; int i; KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc); if (file->refs == 1) { KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n")); /* * Inform any modules associated with this file. */ for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { next = module_getfnext(mod); /* * Give the module a chance to veto the unload. */ if (error = module_unload(mod)) { KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n", mod)); lockmgr(&lock, LK_RELEASE, 0, curproc); goto out; } module_release(mod); } } file->refs--; if (file->refs > 0) { lockmgr(&lock, LK_RELEASE, 0, curproc); goto out; } linker_file_sysuninit(file); TAILQ_REMOVE(&files, file, link); lockmgr(&lock, LK_RELEASE, 0, curproc); for (i = 0; i < file->ndeps; i++) linker_file_unload(file->deps[i]); free(file->deps, M_LINKER); for (cp = STAILQ_FIRST(&file->common); cp; cp = STAILQ_FIRST(&file->common)) { STAILQ_REMOVE(&file->common, cp, common_symbol, link); free(cp, M_LINKER); } file->ops->unload(file); free(file, M_LINKER); out: return error; }
int linker_load_file(const char* filename, linker_file_t* result) { linker_class_t lc; linker_file_t lf; int foundfile, error = 0; char *koname = NULL; lf = linker_find_file_by_name(filename); if (lf) { KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename)); *result = lf; lf->refs++; goto out; } koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); if (koname == NULL) { error = ENOMEM; goto out; } sprintf(koname, "%s.ko", filename); lf = NULL; foundfile = 0; for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n", filename, lc->desc)); error = lc->ops->load_file(koname, &lf); /* First with .ko */ if (lf == NULL && error == ENOENT) error = lc->ops->load_file(filename, &lf); /* Then try without */ /* * If we got something other than ENOENT, then it exists but we cannot * load it for some other reason. */ if (error != ENOENT) foundfile = 1; if (lf) { linker_file_sysinit(lf); *result = lf; error = 0; goto out; } } /* * Less than ideal, but tells the user whether it failed to load or * the module was not found. */ if (foundfile) error = ENOEXEC; /* Format not recognised (or unloadable) */ else error = ENOENT; /* Nothing found */ out: if (koname) free(koname, M_LINKER); return error; }
static void linker_file_sysuninit(linker_file_t lf) { struct linker_set* sysuninits; struct sysinit** sipp; struct sysinit** xipp; struct sysinit* save; KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", lf->filename)); sysuninits = (struct linker_set*) linker_file_lookup_symbol(lf, "sysuninit_set", 0); KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs %p\n", sysuninits)); if (!sysuninits) return; /* * Perform a reverse bubble sort of the system initialization objects * by their subsystem (primary key) and order (secondary key). * * Since some things care about execution order, this is the * operation which ensures continued function. */ for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) { for (xipp = sipp + 1; *xipp; xipp++) { if ((*sipp)->subsystem >= (*xipp)->subsystem || ((*sipp)->subsystem == (*xipp)->subsystem && (*sipp)->order >= (*xipp)->order)) continue; /* skip*/ save = *sipp; *sipp = *xipp; *xipp = save; } } /* * Traverse the (now) ordered list of system initialization tasks. * Perform each task, and continue on to the next task. */ for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) { if ((*sipp)->subsystem == SI_SUB_DUMMY) continue; /* skip dummy task(s)*/ switch ((*sipp)->type) { case SI_TYPE_DEFAULT: /* no special processing*/ (*((*sipp)->func))((*sipp)->udata); break; default: panic("linker_file_sysuninit: unrecognized uninit type"); } } }
int kldunload(struct proc* p, struct kldunload_args* uap) { linker_file_t lf; int error = 0; if (securelevel > 0) return EPERM; if (error = suser(p->p_ucred, &p->p_acflag)) return error; lf = linker_find_file_by_id(SCARG(uap, fileid)); if (lf) { KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); if (lf->userrefs == 0) { printf("linkerunload: attempt to unload file which was not loaded by user\n"); error = EBUSY; goto out; } error = linker_file_unload(lf); if (error) goto out; lf->userrefs--; } else error = ENOENT; out: return error; }
linker_file_t linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops) { linker_file_t lf = 0; int namelen; const char *filename; filename = rindex(pathname, '/'); if (filename && filename[1]) filename++; else filename = pathname; KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename)); lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc); namelen = strlen(filename) + 1; lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK); if (!lf) goto out; bzero(lf, sizeof(*lf)); lf->refs = 1; lf->userrefs = 0; lf->filename = (char*) (lf + 1); strcpy(lf->filename, filename); lf->id = next_file_id++; lf->ndeps = 0; lf->deps = NULL; STAILQ_INIT(&lf->common); TAILQ_INIT(&lf->modules); lf->priv = priv; lf->ops = ops; TAILQ_INSERT_TAIL(&files, lf, link); out: lockmgr(&lock, LK_RELEASE, 0, curproc); return lf; }
static void linker_file_sysinit(linker_file_t lf) { struct linker_set* sysinits; struct sysinit** sipp; struct sysinit** xipp; struct sysinit* save; moduledata_t *moddata; KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", lf->filename)); sysinits = (struct linker_set*) linker_file_lookup_symbol(lf, "sysinit_set", 0); KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits)); if (!sysinits) return; /* HACK ALERT! */ for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { if ((*sipp)->func == module_register_init) { moddata = (*sipp)->udata; moddata->_file = lf; } } /* * Perform a bubble sort of the system initialization objects by * their subsystem (primary key) and order (secondary key). * * Since some things care about execution order, this is the * operation which ensures continued function. */ for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { for (xipp = sipp + 1; *xipp; xipp++) { if ((*sipp)->subsystem <= (*xipp)->subsystem || ((*sipp)->subsystem == (*xipp)->subsystem && (*sipp)->order <= (*xipp)->order)) continue; /* skip*/ save = *sipp; *sipp = *xipp; *xipp = save; } } /* * Traverse the (now) ordered list of system initialization tasks. * Perform each task, and continue on to the next task. */ for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { if ((*sipp)->subsystem == SI_SUB_DUMMY) continue; /* skip dummy task(s)*/ switch ((*sipp)->type) { case SI_TYPE_DEFAULT: /* no special processing*/ (*((*sipp)->func))((*sipp)->udata); break; case SI_TYPE_KTHREAD: #if !defined(SMP) /* kernel thread*/ if (fork1(&proc0, RFFDG|RFPROC|RFMEM)) panic("fork kernel thread"); cpu_set_fork_handler(pfind(proc0.p_retval[0]), (*sipp)->func, (*sipp)->udata); break; #endif case SI_TYPE_KPROCESS: /* kernel thread*/ if (fork1(&proc0, RFFDG|RFPROC)) panic("fork kernel process"); cpu_set_fork_handler(pfind(proc0.p_retval[0]), (*sipp)->func, (*sipp)->udata); break; default: panic ("linker_file_sysinit: unrecognized init type"); } } }
caddr_t linker_file_lookup_symbol(linker_file_t file, const char* name, int deps) { linker_sym_t sym; linker_symval_t symval; linker_file_t lf; caddr_t address; size_t common_size = 0; int i; KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n", file, name, deps)); if (file->ops->lookup_symbol(file, name, &sym) == 0) { file->ops->symbol_values(file, sym, &symval); if (symval.value == 0) /* * For commons, first look them up in the dependancies and * only allocate space if not found there. */ common_size = symval.size; else { KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value)); return symval.value; } } if (deps) { for (i = 0; i < file->ndeps; i++) { address = linker_file_lookup_symbol(file->deps[i], name, 0); if (address) { KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address)); return address; } } /* If we have not found it in the dependencies, search globally */ for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { /* But skip the current file if it's on the list */ if (lf == file) continue; /* And skip the files we searched above */ for (i = 0; i < file->ndeps; i++) if (lf == file->deps[i]) break; if (i < file->ndeps) continue; address = linker_file_lookup_symbol(lf, name, 0); if (address) { KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", address)); return address; } } } if (common_size > 0) { /* * This is a common symbol which was not found in the * dependancies. We maintain a simple common symbol table in * the file object. */ struct common_symbol* cp; for (cp = STAILQ_FIRST(&file->common); cp; cp = STAILQ_NEXT(cp, link)) if (!strcmp(cp->name, name)) { KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address)); return cp->address; } /* * Round the symbol size up to align. */ common_size = (common_size + sizeof(int) - 1) & -sizeof(int); cp = malloc(sizeof(struct common_symbol) + common_size + strlen(name) + 1, M_LINKER, M_WAITOK); if (!cp) { KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n")); return 0; } bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1); cp->address = (caddr_t) (cp + 1); cp->name = cp->address + common_size; strcpy(cp->name, name); bzero(cp->address, common_size); STAILQ_INSERT_TAIL(&file->common, cp, link); KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address)); return cp->address; } KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); return 0; }