driver: add module teardown to driver struct
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
3abfd32fbb
commit
6f201ed370
3 changed files with 48 additions and 72 deletions
|
|
@ -22,52 +22,6 @@ static const char elf_sig[4] = {0x7f, 'E', 'L', 'F'};
|
|||
extern char __kernel_end[];
|
||||
static vaddr_t mod_next_vaddr;
|
||||
|
||||
struct loaded_mod {
|
||||
struct loaded_mod *next;
|
||||
char name[32];
|
||||
void *base;
|
||||
size_t size;
|
||||
void (*exit)(void);
|
||||
};
|
||||
|
||||
static struct loaded_mod *mod_list;
|
||||
|
||||
static struct loaded_mod *mod_add(const char *name, void *base, size_t size, void (*exit)(void))
|
||||
{
|
||||
struct loaded_mod *m = kalloc(sizeof(struct loaded_mod), ALLOC_KERN);
|
||||
if (!m)
|
||||
return NULL;
|
||||
strncpy(m->name, name, sizeof(m->name) - 1);
|
||||
m->name[sizeof(m->name) - 1] = 0;
|
||||
m->base = base;
|
||||
m->size = size;
|
||||
m->exit = exit;
|
||||
m->next = mod_list;
|
||||
mod_list = m;
|
||||
return m;
|
||||
}
|
||||
|
||||
static struct loaded_mod *mod_find(const char *name)
|
||||
{
|
||||
for (struct loaded_mod *m = mod_list; m; m = m->next)
|
||||
if (strcmp(m->name, name) == 0)
|
||||
return m;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void mod_remove(struct loaded_mod *m)
|
||||
{
|
||||
struct loaded_mod **pp = &mod_list;
|
||||
while (*pp) {
|
||||
if (*pp == m) {
|
||||
*pp = m->next;
|
||||
kfree(m);
|
||||
return;
|
||||
}
|
||||
pp = &(*pp)->next;
|
||||
}
|
||||
}
|
||||
|
||||
static void *alloc_module(size_t size)
|
||||
{
|
||||
if (!mod_next_vaddr)
|
||||
|
|
@ -288,19 +242,25 @@ static int elf_load(const char *name, const char *file, size_t fsize)
|
|||
|
||||
void (*exit)(void) = NULL;
|
||||
for (size_t i = 0; i < n_sec; i++) {
|
||||
if (!secs[i].loadable)
|
||||
continue;
|
||||
if (!str_prefix(shstrtab + shdr[i].sh_name, ".exitcall"))
|
||||
continue;
|
||||
if (!secs[i].loadable) continue;
|
||||
if (!str_prefix(shstrtab + shdr[i].sh_name, ".exitcall")) continue;
|
||||
void (**fn)(void) = (void (**)(void))(base + secs[i].load_off);
|
||||
if (fn[0])
|
||||
exit = fn[0];
|
||||
if (fn[0]) exit = fn[0];
|
||||
break;
|
||||
}
|
||||
|
||||
call_initcalls(shdr, secs, n_sec, shstrtab, base);
|
||||
if (exit || 1) {
|
||||
struct driver *d = kzalloc(sizeof(struct driver), ALLOC_KERN);
|
||||
if (d) {
|
||||
strncpy(d->name, name, sizeof(d->name) - 1);
|
||||
d->exit = exit;
|
||||
d->base = base;
|
||||
d->size = total;
|
||||
list_insert(&pending_drivers, d);
|
||||
}
|
||||
}
|
||||
|
||||
mod_add(name, base, total, exit);
|
||||
call_initcalls(shdr, secs, n_sec, shstrtab, base);
|
||||
|
||||
kfree(sym_val);
|
||||
kfree(secs);
|
||||
|
|
@ -336,19 +296,5 @@ int driver_load(const char *name)
|
|||
|
||||
int driver_unload(const char *name)
|
||||
{
|
||||
if (!name)
|
||||
return -EINVAL;
|
||||
|
||||
struct loaded_mod *m = mod_find(name);
|
||||
if (!m)
|
||||
return -ENOENT;
|
||||
|
||||
if (m->exit)
|
||||
m->exit();
|
||||
|
||||
page_unmap(kpgd, (vaddr_t)m->base, m->size, NULL);
|
||||
tlb_flush();
|
||||
|
||||
mod_remove(m);
|
||||
return 0;
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue