1 /* $NetBSD: module.h,v 1.34 2013/10/23 18:57:40 mbalmer 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 #ifndef _SYS_MODULE_H_ 30 #define _SYS_MODULE_H_ 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/cdefs.h> 35 #include <sys/queue.h> 36 #include <sys/uio.h> 37 38 #define MAXMODNAME 32 39 #define MAXMODDEPS 10 40 41 /* Module classes, provided only for system boot and cosmetic purposes. */ 42 typedef enum modclass { 43 MODULE_CLASS_ANY, 44 MODULE_CLASS_MISC, 45 MODULE_CLASS_VFS, 46 MODULE_CLASS_DRIVER, 47 MODULE_CLASS_EXEC, 48 MODULE_CLASS_SECMODEL 49 } modclass_t; 50 51 /* Module sources: where did it come from? */ 52 typedef enum modsrc { 53 MODULE_SOURCE_KERNEL, 54 MODULE_SOURCE_BOOT, 55 MODULE_SOURCE_FILESYS 56 } modsrc_t; 57 58 /* Commands passed to module control routine. */ 59 typedef enum modcmd { 60 MODULE_CMD_INIT, /* mandatory */ 61 MODULE_CMD_FINI, /* mandatory */ 62 MODULE_CMD_STAT, /* optional */ 63 MODULE_CMD_AUTOUNLOAD, /* optional */ 64 } modcmd_t; 65 66 #ifdef _KERNEL 67 68 #include <sys/mutex.h> 69 70 #include <prop/proplib.h> 71 72 /* Module header structure. */ 73 typedef struct modinfo { 74 u_int mi_version; 75 modclass_t mi_class; 76 int (*mi_modcmd)(modcmd_t, void *); 77 const char *mi_name; 78 const char *mi_required; 79 } const modinfo_t; 80 81 /* Per module information, maintained by kern_module.c */ 82 typedef struct module { 83 u_int mod_refcnt; 84 const modinfo_t *mod_info; 85 struct kobj *mod_kobj; 86 TAILQ_ENTRY(module) mod_chain; 87 struct module *mod_required[MAXMODDEPS]; 88 u_int mod_nrequired; 89 modsrc_t mod_source; 90 time_t mod_autotime; 91 void *mod_ctf; 92 u_int mod_fbtentries; /* DTrace FBT entry count */ 93 int mod_flags; 94 #define MODFLG_MUST_FORCE 0x01 95 #define MODFLG_AUTO_LOADED 0x02 96 97 } module_t; 98 99 /* 100 * Per-module linkage. Loadable modules have a `link_set_modules' section 101 * containing only one entry, pointing to the module's modinfo_t record. 102 * For the kernel, `link_set_modules' can contain multiple entries and 103 * records all modules built into the kernel at link time. 104 */ 105 #define MODULE(class, name, required) \ 106 static int name##_modcmd(modcmd_t, void *); \ 107 static const modinfo_t name##_modinfo = { \ 108 .mi_version = __NetBSD_Version__, \ 109 .mi_class = (class), \ 110 .mi_modcmd = name##_modcmd, \ 111 .mi_name = #name, \ 112 .mi_required = (required) \ 113 }; \ 114 __link_set_add_rodata(modules, name##_modinfo); 115 116 TAILQ_HEAD(modlist, module); 117 118 extern struct vm_map *module_map; 119 extern u_int module_count; 120 extern u_int module_builtinlist; 121 extern struct modlist module_list; 122 extern struct modlist module_builtins; 123 extern u_int module_gen; 124 125 void module_init(void); 126 void module_start_unload_thread(void); 127 void module_builtin_require_force(void); 128 void module_init_md(void); 129 void module_init_class(modclass_t); 130 int module_prime(const char *, void *, size_t); 131 132 bool module_compatible(int, int); 133 int module_load(const char *, int, prop_dictionary_t, modclass_t); 134 int module_builtin_add(modinfo_t * const *, size_t, bool); 135 int module_builtin_remove(modinfo_t *, bool); 136 int module_autoload(const char *, modclass_t); 137 int module_unload(const char *); 138 int module_hold(const char *); 139 void module_rele(const char *); 140 int module_find_section(const char *, void **, size_t *); 141 void module_thread_kick(void); 142 void module_load_vfs_init(void); 143 144 void module_whatis(uintptr_t, void (*)(const char *, ...) 145 __printflike(1, 2)); 146 void module_print_list(void (*)(const char *, ...) __printflike(1, 2)); 147 148 #ifdef _MODULE_INTERNAL 149 extern 150 int (*module_load_vfs_vec)(const char *, int, bool, module_t *, 151 prop_dictionary_t *); 152 int module_load_vfs(const char *, int, bool, module_t *, 153 prop_dictionary_t *); 154 void module_error(const char *, ...) __printflike(1, 2); 155 void module_print(const char *, ...) __printflike(1, 2); 156 #endif /* _MODULE_INTERNAL */ 157 158 #define MODULE_BASE_SIZE 64 159 extern char module_base[MODULE_BASE_SIZE]; 160 extern char *module_machine; 161 162 #else /* _KERNEL */ 163 164 #include <stdint.h> 165 166 #endif /* _KERNEL */ 167 168 typedef struct modctl_load { 169 const char *ml_filename; 170 171 #define MODCTL_NO_PROP 0x2 172 #define MODCTL_LOAD_FORCE 0x1 173 int ml_flags; 174 175 const char *ml_props; 176 size_t ml_propslen; 177 } modctl_load_t; 178 179 typedef enum modctl { 180 MODCTL_LOAD, /* modctl_load_t *ml */ 181 MODCTL_UNLOAD, /* char *name */ 182 MODCTL_STAT, /* struct iovec *buffer */ 183 MODCTL_EXISTS /* enum: 0: load, 1: autoload */ 184 } modctl_t; 185 186 /* 187 * This structure intentionally has the same layout for 32 and 64 188 * bit builds. 189 */ 190 typedef struct modstat { 191 char ms_name[MAXMODNAME]; 192 char ms_required[MAXMODNAME * MAXMODDEPS]; 193 uint64_t ms_addr; 194 modsrc_t ms_source; 195 modclass_t ms_class; 196 u_int ms_size; 197 u_int ms_refcnt; 198 u_int ms_reserved[4]; 199 } modstat_t; 200 201 int modctl(int, void *); 202 203 #endif /* !_SYS_MODULE_H_ */ 204