ia64/linux-2.6.18-xen.hg

annotate kernel/module.c @ 798:b02a90bf5bbc

ACPI: Backport missing part for T-State MSR support

Part of below kernel commit was missed while packporting T-State
support.

commit f79f06ab9f86d7203006d2ec8992ac80df36a34e
Author: Zhao Yakui <yakui.zhao@intel.com>
Date: Thu Nov 15 17:06:36 2007 +0800

ACPI: Enable MSR (FixedHW) support for T-States

Add throttling control via MSR when T-states uses
the FixHW Control Status registers.

Signed-off-by: Zhao Yakui <yakui.zhao@intel.com>
Signed-off-by: Li Shaohua <shaohua.li@intel.com>
Signed-off-by: Len Brown <len.brown@intel.com>

Signed-off-by: Wei Gang <gang.wei@intel.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Mar 02 10:53:59 2009 +0000 (2009-03-02)
parents 3e8752eb6d9c
children
rev   line source
ian@0 1 /*
ian@0 2 Copyright (C) 2002 Richard Henderson
ian@0 3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
ian@0 4
ian@0 5 This program is free software; you can redistribute it and/or modify
ian@0 6 it under the terms of the GNU General Public License as published by
ian@0 7 the Free Software Foundation; either version 2 of the License, or
ian@0 8 (at your option) any later version.
ian@0 9
ian@0 10 This program is distributed in the hope that it will be useful,
ian@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
ian@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ian@0 13 GNU General Public License for more details.
ian@0 14
ian@0 15 You should have received a copy of the GNU General Public License
ian@0 16 along with this program; if not, write to the Free Software
ian@0 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
ian@0 18 */
ian@0 19 #include <linux/module.h>
ian@0 20 #include <linux/moduleloader.h>
ian@0 21 #include <linux/init.h>
ian@0 22 #include <linux/kernel.h>
ian@0 23 #include <linux/slab.h>
ian@0 24 #include <linux/vmalloc.h>
ian@0 25 #include <linux/elf.h>
ian@0 26 #include <linux/seq_file.h>
ian@0 27 #include <linux/syscalls.h>
ian@0 28 #include <linux/fcntl.h>
ian@0 29 #include <linux/rcupdate.h>
ian@0 30 #include <linux/capability.h>
ian@0 31 #include <linux/cpu.h>
ian@0 32 #include <linux/moduleparam.h>
ian@0 33 #include <linux/errno.h>
ian@0 34 #include <linux/err.h>
ian@0 35 #include <linux/vermagic.h>
ian@0 36 #include <linux/notifier.h>
ian@0 37 #include <linux/stop_machine.h>
ian@0 38 #include <linux/device.h>
ian@0 39 #include <linux/string.h>
ian@0 40 #include <linux/sched.h>
ian@0 41 #include <linux/mutex.h>
ian@0 42 #include <linux/unwind.h>
ian@0 43 #include <asm/uaccess.h>
ian@0 44 #include <asm/semaphore.h>
ian@0 45 #include <asm/cacheflush.h>
ian@0 46 #include <linux/license.h>
ian@0 47
ian@0 48 #if 0
ian@0 49 #define DEBUGP printk
ian@0 50 #else
ian@0 51 #define DEBUGP(fmt , a...)
ian@0 52 #endif
ian@0 53
ian@0 54 #ifndef ARCH_SHF_SMALL
ian@0 55 #define ARCH_SHF_SMALL 0
ian@0 56 #endif
ian@0 57
ian@0 58 /* If this is set, the section belongs in the init part of the module */
ian@0 59 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
ian@0 60
ian@0 61 /* Protects module list */
ian@0 62 static DEFINE_SPINLOCK(modlist_lock);
ian@0 63
ian@0 64 /* List of modules, protected by module_mutex AND modlist_lock */
ian@0 65 static DEFINE_MUTEX(module_mutex);
ian@0 66 static LIST_HEAD(modules);
ian@0 67
ian@0 68 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
ian@0 69
ian@0 70 int register_module_notifier(struct notifier_block * nb)
ian@0 71 {
ian@0 72 return blocking_notifier_chain_register(&module_notify_list, nb);
ian@0 73 }
ian@0 74 EXPORT_SYMBOL(register_module_notifier);
ian@0 75
ian@0 76 int unregister_module_notifier(struct notifier_block * nb)
ian@0 77 {
ian@0 78 return blocking_notifier_chain_unregister(&module_notify_list, nb);
ian@0 79 }
ian@0 80 EXPORT_SYMBOL(unregister_module_notifier);
ian@0 81
ian@0 82 /* We require a truly strong try_module_get() */
ian@0 83 static inline int strong_try_module_get(struct module *mod)
ian@0 84 {
ian@0 85 if (mod && mod->state == MODULE_STATE_COMING)
ian@0 86 return 0;
ian@0 87 return try_module_get(mod);
ian@0 88 }
ian@0 89
ian@0 90 /* A thread that wants to hold a reference to a module only while it
ian@0 91 * is running can call ths to safely exit.
ian@0 92 * nfsd and lockd use this.
ian@0 93 */
ian@0 94 void __module_put_and_exit(struct module *mod, long code)
ian@0 95 {
ian@0 96 module_put(mod);
ian@0 97 do_exit(code);
ian@0 98 }
ian@0 99 EXPORT_SYMBOL(__module_put_and_exit);
ian@0 100
ian@0 101 /* Find a module section: 0 means not found. */
ian@0 102 static unsigned int find_sec(Elf_Ehdr *hdr,
ian@0 103 Elf_Shdr *sechdrs,
ian@0 104 const char *secstrings,
ian@0 105 const char *name)
ian@0 106 {
ian@0 107 unsigned int i;
ian@0 108
ian@0 109 for (i = 1; i < hdr->e_shnum; i++)
ian@0 110 /* Alloc bit cleared means "ignore it." */
ian@0 111 if ((sechdrs[i].sh_flags & SHF_ALLOC)
ian@0 112 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
ian@0 113 return i;
ian@0 114 return 0;
ian@0 115 }
ian@0 116
ian@0 117 /* Provided by the linker */
ian@0 118 extern const struct kernel_symbol __start___ksymtab[];
ian@0 119 extern const struct kernel_symbol __stop___ksymtab[];
ian@0 120 extern const struct kernel_symbol __start___ksymtab_gpl[];
ian@0 121 extern const struct kernel_symbol __stop___ksymtab_gpl[];
ian@0 122 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
ian@0 123 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
ian@0 124 extern const struct kernel_symbol __start___ksymtab_unused[];
ian@0 125 extern const struct kernel_symbol __stop___ksymtab_unused[];
ian@0 126 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
ian@0 127 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
ian@0 128 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
ian@0 129 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
ian@0 130 extern const unsigned long __start___kcrctab[];
ian@0 131 extern const unsigned long __start___kcrctab_gpl[];
ian@0 132 extern const unsigned long __start___kcrctab_gpl_future[];
ian@0 133 extern const unsigned long __start___kcrctab_unused[];
ian@0 134 extern const unsigned long __start___kcrctab_unused_gpl[];
ian@0 135
ian@0 136 #ifndef CONFIG_MODVERSIONS
ian@0 137 #define symversion(base, idx) NULL
ian@0 138 #else
ian@0 139 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
ian@0 140 #endif
ian@0 141
ian@0 142 /* lookup symbol in given range of kernel_symbols */
ian@0 143 static const struct kernel_symbol *lookup_symbol(const char *name,
ian@0 144 const struct kernel_symbol *start,
ian@0 145 const struct kernel_symbol *stop)
ian@0 146 {
ian@0 147 const struct kernel_symbol *ks = start;
ian@0 148 for (; ks < stop; ks++)
ian@0 149 if (strcmp(ks->name, name) == 0)
ian@0 150 return ks;
ian@0 151 return NULL;
ian@0 152 }
ian@0 153
ian@0 154 static void printk_unused_warning(const char *name)
ian@0 155 {
ian@0 156 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
ian@0 157 "however this module is using it.\n", name);
ian@0 158 printk(KERN_WARNING "This symbol will go away in the future.\n");
ian@0 159 printk(KERN_WARNING "Please evalute if this is the right api to use, "
ian@0 160 "and if it really is, submit a report the linux kernel "
ian@0 161 "mailinglist together with submitting your code for "
ian@0 162 "inclusion.\n");
ian@0 163 }
ian@0 164
ian@0 165 /* Find a symbol, return value, crc and module which owns it */
ian@0 166 static unsigned long __find_symbol(const char *name,
ian@0 167 struct module **owner,
ian@0 168 const unsigned long **crc,
ian@0 169 int gplok)
ian@0 170 {
ian@0 171 struct module *mod;
ian@0 172 const struct kernel_symbol *ks;
ian@0 173
ian@0 174 /* Core kernel first. */
ian@0 175 *owner = NULL;
ian@0 176 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
ian@0 177 if (ks) {
ian@0 178 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
ian@0 179 return ks->value;
ian@0 180 }
ian@0 181 if (gplok) {
ian@0 182 ks = lookup_symbol(name, __start___ksymtab_gpl,
ian@0 183 __stop___ksymtab_gpl);
ian@0 184 if (ks) {
ian@0 185 *crc = symversion(__start___kcrctab_gpl,
ian@0 186 (ks - __start___ksymtab_gpl));
ian@0 187 return ks->value;
ian@0 188 }
ian@0 189 }
ian@0 190 ks = lookup_symbol(name, __start___ksymtab_gpl_future,
ian@0 191 __stop___ksymtab_gpl_future);
ian@0 192 if (ks) {
ian@0 193 if (!gplok) {
ian@0 194 printk(KERN_WARNING "Symbol %s is being used "
ian@0 195 "by a non-GPL module, which will not "
ian@0 196 "be allowed in the future\n", name);
ian@0 197 printk(KERN_WARNING "Please see the file "
ian@0 198 "Documentation/feature-removal-schedule.txt "
ian@0 199 "in the kernel source tree for more "
ian@0 200 "details.\n");
ian@0 201 }
ian@0 202 *crc = symversion(__start___kcrctab_gpl_future,
ian@0 203 (ks - __start___ksymtab_gpl_future));
ian@0 204 return ks->value;
ian@0 205 }
ian@0 206
ian@0 207 ks = lookup_symbol(name, __start___ksymtab_unused,
ian@0 208 __stop___ksymtab_unused);
ian@0 209 if (ks) {
ian@0 210 printk_unused_warning(name);
ian@0 211 *crc = symversion(__start___kcrctab_unused,
ian@0 212 (ks - __start___ksymtab_unused));
ian@0 213 return ks->value;
ian@0 214 }
ian@0 215
ian@0 216 if (gplok)
ian@0 217 ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
ian@0 218 __stop___ksymtab_unused_gpl);
ian@0 219 if (ks) {
ian@0 220 printk_unused_warning(name);
ian@0 221 *crc = symversion(__start___kcrctab_unused_gpl,
ian@0 222 (ks - __start___ksymtab_unused_gpl));
ian@0 223 return ks->value;
ian@0 224 }
ian@0 225
ian@0 226 /* Now try modules. */
ian@0 227 list_for_each_entry(mod, &modules, list) {
ian@0 228 *owner = mod;
ian@0 229 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
ian@0 230 if (ks) {
ian@0 231 *crc = symversion(mod->crcs, (ks - mod->syms));
ian@0 232 return ks->value;
ian@0 233 }
ian@0 234
ian@0 235 if (gplok) {
ian@0 236 ks = lookup_symbol(name, mod->gpl_syms,
ian@0 237 mod->gpl_syms + mod->num_gpl_syms);
ian@0 238 if (ks) {
ian@0 239 *crc = symversion(mod->gpl_crcs,
ian@0 240 (ks - mod->gpl_syms));
ian@0 241 return ks->value;
ian@0 242 }
ian@0 243 }
ian@0 244 ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
ian@0 245 if (ks) {
ian@0 246 printk_unused_warning(name);
ian@0 247 *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
ian@0 248 return ks->value;
ian@0 249 }
ian@0 250
ian@0 251 if (gplok) {
ian@0 252 ks = lookup_symbol(name, mod->unused_gpl_syms,
ian@0 253 mod->unused_gpl_syms + mod->num_unused_gpl_syms);
ian@0 254 if (ks) {
ian@0 255 printk_unused_warning(name);
ian@0 256 *crc = symversion(mod->unused_gpl_crcs,
ian@0 257 (ks - mod->unused_gpl_syms));
ian@0 258 return ks->value;
ian@0 259 }
ian@0 260 }
ian@0 261 ks = lookup_symbol(name, mod->gpl_future_syms,
ian@0 262 (mod->gpl_future_syms +
ian@0 263 mod->num_gpl_future_syms));
ian@0 264 if (ks) {
ian@0 265 if (!gplok) {
ian@0 266 printk(KERN_WARNING "Symbol %s is being used "
ian@0 267 "by a non-GPL module, which will not "
ian@0 268 "be allowed in the future\n", name);
ian@0 269 printk(KERN_WARNING "Please see the file "
ian@0 270 "Documentation/feature-removal-schedule.txt "
ian@0 271 "in the kernel source tree for more "
ian@0 272 "details.\n");
ian@0 273 }
ian@0 274 *crc = symversion(mod->gpl_future_crcs,
ian@0 275 (ks - mod->gpl_future_syms));
ian@0 276 return ks->value;
ian@0 277 }
ian@0 278 }
ian@0 279 DEBUGP("Failed to find symbol %s\n", name);
ian@0 280 return 0;
ian@0 281 }
ian@0 282
ian@0 283 /* Search for module by name: must hold module_mutex. */
ian@0 284 static struct module *find_module(const char *name)
ian@0 285 {
ian@0 286 struct module *mod;
ian@0 287
ian@0 288 list_for_each_entry(mod, &modules, list) {
ian@0 289 if (strcmp(mod->name, name) == 0)
ian@0 290 return mod;
ian@0 291 }
ian@0 292 return NULL;
ian@0 293 }
ian@0 294
ian@0 295 #ifdef CONFIG_SMP
ian@0 296 /* Number of blocks used and allocated. */
ian@0 297 static unsigned int pcpu_num_used, pcpu_num_allocated;
ian@0 298 /* Size of each block. -ve means used. */
ian@0 299 static int *pcpu_size;
ian@0 300
ian@0 301 static int split_block(unsigned int i, unsigned short size)
ian@0 302 {
ian@0 303 /* Reallocation required? */
ian@0 304 if (pcpu_num_used + 1 > pcpu_num_allocated) {
ian@0 305 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
ian@0 306 GFP_KERNEL);
ian@0 307 if (!new)
ian@0 308 return 0;
ian@0 309
ian@0 310 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
ian@0 311 pcpu_num_allocated *= 2;
ian@0 312 kfree(pcpu_size);
ian@0 313 pcpu_size = new;
ian@0 314 }
ian@0 315
ian@0 316 /* Insert a new subblock */
ian@0 317 memmove(&pcpu_size[i+1], &pcpu_size[i],
ian@0 318 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
ian@0 319 pcpu_num_used++;
ian@0 320
ian@0 321 pcpu_size[i+1] -= size;
ian@0 322 pcpu_size[i] = size;
ian@0 323 return 1;
ian@0 324 }
ian@0 325
ian@0 326 static inline unsigned int block_size(int val)
ian@0 327 {
ian@0 328 if (val < 0)
ian@0 329 return -val;
ian@0 330 return val;
ian@0 331 }
ian@0 332
ian@0 333 /* Created by linker magic */
ian@0 334 extern char __per_cpu_start[], __per_cpu_end[];
ian@0 335
ian@0 336 static void *percpu_modalloc(unsigned long size, unsigned long align,
ian@0 337 const char *name)
ian@0 338 {
ian@0 339 unsigned long extra;
ian@0 340 unsigned int i;
ian@0 341 void *ptr;
ian@0 342
ian@0 343 if (align > SMP_CACHE_BYTES) {
ian@0 344 printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
ian@0 345 name, align, SMP_CACHE_BYTES);
ian@0 346 align = SMP_CACHE_BYTES;
ian@0 347 }
ian@0 348
ian@0 349 ptr = __per_cpu_start;
ian@0 350 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
ian@0 351 /* Extra for alignment requirement. */
ian@0 352 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
ian@0 353 BUG_ON(i == 0 && extra != 0);
ian@0 354
ian@0 355 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
ian@0 356 continue;
ian@0 357
ian@0 358 /* Transfer extra to previous block. */
ian@0 359 if (pcpu_size[i-1] < 0)
ian@0 360 pcpu_size[i-1] -= extra;
ian@0 361 else
ian@0 362 pcpu_size[i-1] += extra;
ian@0 363 pcpu_size[i] -= extra;
ian@0 364 ptr += extra;
ian@0 365
ian@0 366 /* Split block if warranted */
ian@0 367 if (pcpu_size[i] - size > sizeof(unsigned long))
ian@0 368 if (!split_block(i, size))
ian@0 369 return NULL;
ian@0 370
ian@0 371 /* Mark allocated */
ian@0 372 pcpu_size[i] = -pcpu_size[i];
ian@0 373 return ptr;
ian@0 374 }
ian@0 375
ian@0 376 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
ian@0 377 size);
ian@0 378 return NULL;
ian@0 379 }
ian@0 380
ian@0 381 static void percpu_modfree(void *freeme)
ian@0 382 {
ian@0 383 unsigned int i;
ian@0 384 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
ian@0 385
ian@0 386 /* First entry is core kernel percpu data. */
ian@0 387 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
ian@0 388 if (ptr == freeme) {
ian@0 389 pcpu_size[i] = -pcpu_size[i];
ian@0 390 goto free;
ian@0 391 }
ian@0 392 }
ian@0 393 BUG();
ian@0 394
ian@0 395 free:
ian@0 396 /* Merge with previous? */
ian@0 397 if (pcpu_size[i-1] >= 0) {
ian@0 398 pcpu_size[i-1] += pcpu_size[i];
ian@0 399 pcpu_num_used--;
ian@0 400 memmove(&pcpu_size[i], &pcpu_size[i+1],
ian@0 401 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
ian@0 402 i--;
ian@0 403 }
ian@0 404 /* Merge with next? */
ian@0 405 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
ian@0 406 pcpu_size[i] += pcpu_size[i+1];
ian@0 407 pcpu_num_used--;
ian@0 408 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
ian@0 409 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
ian@0 410 }
ian@0 411 }
ian@0 412
ian@0 413 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
ian@0 414 Elf_Shdr *sechdrs,
ian@0 415 const char *secstrings)
ian@0 416 {
ian@0 417 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
ian@0 418 }
ian@0 419
ian@0 420 static int percpu_modinit(void)
ian@0 421 {
ian@0 422 pcpu_num_used = 2;
ian@0 423 pcpu_num_allocated = 2;
ian@0 424 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
ian@0 425 GFP_KERNEL);
ian@0 426 /* Static in-kernel percpu data (used). */
ian@0 427 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
ian@0 428 /* Free room. */
ian@0 429 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
ian@0 430 if (pcpu_size[1] < 0) {
ian@0 431 printk(KERN_ERR "No per-cpu room for modules.\n");
ian@0 432 pcpu_num_used = 1;
ian@0 433 }
ian@0 434
ian@0 435 return 0;
ian@0 436 }
ian@0 437 __initcall(percpu_modinit);
ian@0 438 #else /* ... !CONFIG_SMP */
ian@0 439 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
ian@0 440 const char *name)
ian@0 441 {
ian@0 442 return NULL;
ian@0 443 }
ian@0 444 static inline void percpu_modfree(void *pcpuptr)
ian@0 445 {
ian@0 446 BUG();
ian@0 447 }
ian@0 448 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
ian@0 449 Elf_Shdr *sechdrs,
ian@0 450 const char *secstrings)
ian@0 451 {
ian@0 452 return 0;
ian@0 453 }
ian@0 454 static inline void percpu_modcopy(void *pcpudst, const void *src,
ian@0 455 unsigned long size)
ian@0 456 {
ian@0 457 /* pcpusec should be 0, and size of that section should be 0. */
ian@0 458 BUG_ON(size != 0);
ian@0 459 }
ian@0 460 #endif /* CONFIG_SMP */
ian@0 461
ian@0 462 #define MODINFO_ATTR(field) \
ian@0 463 static void setup_modinfo_##field(struct module *mod, const char *s) \
ian@0 464 { \
ian@0 465 mod->field = kstrdup(s, GFP_KERNEL); \
ian@0 466 } \
ian@0 467 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
ian@0 468 struct module *mod, char *buffer) \
ian@0 469 { \
ian@0 470 return sprintf(buffer, "%s\n", mod->field); \
ian@0 471 } \
ian@0 472 static int modinfo_##field##_exists(struct module *mod) \
ian@0 473 { \
ian@0 474 return mod->field != NULL; \
ian@0 475 } \
ian@0 476 static void free_modinfo_##field(struct module *mod) \
ian@0 477 { \
ian@0 478 kfree(mod->field); \
ian@0 479 mod->field = NULL; \
ian@0 480 } \
ian@0 481 static struct module_attribute modinfo_##field = { \
ian@0 482 .attr = { .name = __stringify(field), .mode = 0444, \
ian@0 483 .owner = THIS_MODULE }, \
ian@0 484 .show = show_modinfo_##field, \
ian@0 485 .setup = setup_modinfo_##field, \
ian@0 486 .test = modinfo_##field##_exists, \
ian@0 487 .free = free_modinfo_##field, \
ian@0 488 };
ian@0 489
ian@0 490 MODINFO_ATTR(version);
ian@0 491 MODINFO_ATTR(srcversion);
ian@0 492
ian@0 493 #ifdef CONFIG_MODULE_UNLOAD
ian@0 494 /* Init the unload section of the module. */
ian@0 495 static void module_unload_init(struct module *mod)
ian@0 496 {
ian@0 497 unsigned int i;
ian@0 498
ian@0 499 INIT_LIST_HEAD(&mod->modules_which_use_me);
ian@0 500 for (i = 0; i < NR_CPUS; i++)
ian@0 501 local_set(&mod->ref[i].count, 0);
ian@0 502 /* Hold reference count during initialization. */
ian@0 503 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
ian@0 504 /* Backwards compatibility macros put refcount during init. */
ian@0 505 mod->waiter = current;
ian@0 506 }
ian@0 507
ian@0 508 /* modules using other modules */
ian@0 509 struct module_use
ian@0 510 {
ian@0 511 struct list_head list;
ian@0 512 struct module *module_which_uses;
ian@0 513 };
ian@0 514
ian@0 515 /* Does a already use b? */
ian@0 516 static int already_uses(struct module *a, struct module *b)
ian@0 517 {
ian@0 518 struct module_use *use;
ian@0 519
ian@0 520 list_for_each_entry(use, &b->modules_which_use_me, list) {
ian@0 521 if (use->module_which_uses == a) {
ian@0 522 DEBUGP("%s uses %s!\n", a->name, b->name);
ian@0 523 return 1;
ian@0 524 }
ian@0 525 }
ian@0 526 DEBUGP("%s does not use %s!\n", a->name, b->name);
ian@0 527 return 0;
ian@0 528 }
ian@0 529
ian@0 530 /* Module a uses b */
ian@0 531 static int use_module(struct module *a, struct module *b)
ian@0 532 {
ian@0 533 struct module_use *use;
ian@0 534 if (b == NULL || already_uses(a, b)) return 1;
ian@0 535
ian@0 536 if (!strong_try_module_get(b))
ian@0 537 return 0;
ian@0 538
ian@0 539 DEBUGP("Allocating new usage for %s.\n", a->name);
ian@0 540 use = kmalloc(sizeof(*use), GFP_ATOMIC);
ian@0 541 if (!use) {
ian@0 542 printk("%s: out of memory loading\n", a->name);
ian@0 543 module_put(b);
ian@0 544 return 0;
ian@0 545 }
ian@0 546
ian@0 547 use->module_which_uses = a;
ian@0 548 list_add(&use->list, &b->modules_which_use_me);
ian@0 549 return 1;
ian@0 550 }
ian@0 551
ian@0 552 /* Clear the unload stuff of the module. */
ian@0 553 static void module_unload_free(struct module *mod)
ian@0 554 {
ian@0 555 struct module *i;
ian@0 556
ian@0 557 list_for_each_entry(i, &modules, list) {
ian@0 558 struct module_use *use;
ian@0 559
ian@0 560 list_for_each_entry(use, &i->modules_which_use_me, list) {
ian@0 561 if (use->module_which_uses == mod) {
ian@0 562 DEBUGP("%s unusing %s\n", mod->name, i->name);
ian@0 563 module_put(i);
ian@0 564 list_del(&use->list);
ian@0 565 kfree(use);
ian@0 566 /* There can be at most one match. */
ian@0 567 break;
ian@0 568 }
ian@0 569 }
ian@0 570 }
ian@0 571 }
ian@0 572
ian@0 573 #ifdef CONFIG_MODULE_FORCE_UNLOAD
ian@0 574 static inline int try_force_unload(unsigned int flags)
ian@0 575 {
ian@0 576 int ret = (flags & O_TRUNC);
ian@0 577 if (ret)
ian@0 578 add_taint(TAINT_FORCED_RMMOD);
ian@0 579 return ret;
ian@0 580 }
ian@0 581 #else
ian@0 582 static inline int try_force_unload(unsigned int flags)
ian@0 583 {
ian@0 584 return 0;
ian@0 585 }
ian@0 586 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
ian@0 587
ian@0 588 struct stopref
ian@0 589 {
ian@0 590 struct module *mod;
ian@0 591 int flags;
ian@0 592 int *forced;
ian@0 593 };
ian@0 594
ian@0 595 /* Whole machine is stopped with interrupts off when this runs. */
ian@0 596 static int __try_stop_module(void *_sref)
ian@0 597 {
ian@0 598 struct stopref *sref = _sref;
ian@0 599
ian@0 600 /* If it's not unused, quit unless we are told to block. */
ian@0 601 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
ian@0 602 if (!(*sref->forced = try_force_unload(sref->flags)))
ian@0 603 return -EWOULDBLOCK;
ian@0 604 }
ian@0 605
ian@0 606 /* Mark it as dying. */
ian@0 607 sref->mod->state = MODULE_STATE_GOING;
ian@0 608 return 0;
ian@0 609 }
ian@0 610
ian@0 611 static int try_stop_module(struct module *mod, int flags, int *forced)
ian@0 612 {
ian@0 613 struct stopref sref = { mod, flags, forced };
ian@0 614
ian@0 615 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
ian@0 616 }
ian@0 617
ian@0 618 unsigned int module_refcount(struct module *mod)
ian@0 619 {
ian@0 620 unsigned int i, total = 0;
ian@0 621
ian@0 622 for (i = 0; i < NR_CPUS; i++)
ian@0 623 total += local_read(&mod->ref[i].count);
ian@0 624 return total;
ian@0 625 }
ian@0 626 EXPORT_SYMBOL(module_refcount);
ian@0 627
ian@0 628 /* This exists whether we can unload or not */
ian@0 629 static void free_module(struct module *mod);
ian@0 630
ian@0 631 static void wait_for_zero_refcount(struct module *mod)
ian@0 632 {
ian@0 633 /* Since we might sleep for some time, drop the semaphore first */
ian@0 634 mutex_unlock(&module_mutex);
ian@0 635 for (;;) {
ian@0 636 DEBUGP("Looking at refcount...\n");
ian@0 637 set_current_state(TASK_UNINTERRUPTIBLE);
ian@0 638 if (module_refcount(mod) == 0)
ian@0 639 break;
ian@0 640 schedule();
ian@0 641 }
ian@0 642 current->state = TASK_RUNNING;
ian@0 643 mutex_lock(&module_mutex);
ian@0 644 }
ian@0 645
ian@0 646 asmlinkage long
ian@0 647 sys_delete_module(const char __user *name_user, unsigned int flags)
ian@0 648 {
ian@0 649 struct module *mod;
ian@0 650 char name[MODULE_NAME_LEN];
ian@0 651 int ret, forced = 0;
ian@0 652
ian@0 653 if (!capable(CAP_SYS_MODULE))
ian@0 654 return -EPERM;
ian@0 655
ian@0 656 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
ian@0 657 return -EFAULT;
ian@0 658 name[MODULE_NAME_LEN-1] = '\0';
ian@0 659
ian@0 660 if (mutex_lock_interruptible(&module_mutex) != 0)
ian@0 661 return -EINTR;
ian@0 662
ian@0 663 mod = find_module(name);
ian@0 664 if (!mod) {
ian@0 665 ret = -ENOENT;
ian@0 666 goto out;
ian@0 667 }
ian@0 668
ian@0 669 if (!list_empty(&mod->modules_which_use_me)) {
ian@0 670 /* Other modules depend on us: get rid of them first. */
ian@0 671 ret = -EWOULDBLOCK;
ian@0 672 goto out;
ian@0 673 }
ian@0 674
ian@0 675 /* Doing init or already dying? */
ian@0 676 if (mod->state != MODULE_STATE_LIVE) {
ian@0 677 /* FIXME: if (force), slam module count and wake up
ian@0 678 waiter --RR */
ian@0 679 DEBUGP("%s already dying\n", mod->name);
ian@0 680 ret = -EBUSY;
ian@0 681 goto out;
ian@0 682 }
ian@0 683
ian@0 684 /* If it has an init func, it must have an exit func to unload */
ian@0 685 if ((mod->init != NULL && mod->exit == NULL)
ian@0 686 || mod->unsafe) {
ian@0 687 forced = try_force_unload(flags);
ian@0 688 if (!forced) {
ian@0 689 /* This module can't be removed */
ian@0 690 ret = -EBUSY;
ian@0 691 goto out;
ian@0 692 }
ian@0 693 }
ian@0 694
ian@0 695 /* Set this up before setting mod->state */
ian@0 696 mod->waiter = current;
ian@0 697
ian@0 698 /* Stop the machine so refcounts can't move and disable module. */
ian@0 699 ret = try_stop_module(mod, flags, &forced);
ian@0 700 if (ret != 0)
ian@0 701 goto out;
ian@0 702
ian@0 703 /* Never wait if forced. */
ian@0 704 if (!forced && module_refcount(mod) != 0)
ian@0 705 wait_for_zero_refcount(mod);
ian@0 706
ian@0 707 /* Final destruction now noone is using it. */
ian@0 708 if (mod->exit != NULL) {
ian@0 709 mutex_unlock(&module_mutex);
ian@0 710 mod->exit();
ian@0 711 mutex_lock(&module_mutex);
ian@0 712 }
ian@0 713 free_module(mod);
ian@0 714
ian@0 715 out:
ian@0 716 mutex_unlock(&module_mutex);
ian@0 717 return ret;
ian@0 718 }
ian@0 719
ian@0 720 static void print_unload_info(struct seq_file *m, struct module *mod)
ian@0 721 {
ian@0 722 struct module_use *use;
ian@0 723 int printed_something = 0;
ian@0 724
ian@0 725 seq_printf(m, " %u ", module_refcount(mod));
ian@0 726
ian@0 727 /* Always include a trailing , so userspace can differentiate
ian@0 728 between this and the old multi-field proc format. */
ian@0 729 list_for_each_entry(use, &mod->modules_which_use_me, list) {
ian@0 730 printed_something = 1;
ian@0 731 seq_printf(m, "%s,", use->module_which_uses->name);
ian@0 732 }
ian@0 733
ian@0 734 if (mod->unsafe) {
ian@0 735 printed_something = 1;
ian@0 736 seq_printf(m, "[unsafe],");
ian@0 737 }
ian@0 738
ian@0 739 if (mod->init != NULL && mod->exit == NULL) {
ian@0 740 printed_something = 1;
ian@0 741 seq_printf(m, "[permanent],");
ian@0 742 }
ian@0 743
ian@0 744 if (!printed_something)
ian@0 745 seq_printf(m, "-");
ian@0 746 }
ian@0 747
ian@0 748 void __symbol_put(const char *symbol)
ian@0 749 {
ian@0 750 struct module *owner;
ian@0 751 unsigned long flags;
ian@0 752 const unsigned long *crc;
ian@0 753
ian@0 754 spin_lock_irqsave(&modlist_lock, flags);
ian@0 755 if (!__find_symbol(symbol, &owner, &crc, 1))
ian@0 756 BUG();
ian@0 757 module_put(owner);
ian@0 758 spin_unlock_irqrestore(&modlist_lock, flags);
ian@0 759 }
ian@0 760 EXPORT_SYMBOL(__symbol_put);
ian@0 761
ian@0 762 void symbol_put_addr(void *addr)
ian@0 763 {
ian@0 764 struct module *modaddr;
ian@0 765
ian@0 766 if (core_kernel_text((unsigned long)addr))
ian@0 767 return;
ian@0 768
ian@0 769 if (!(modaddr = module_text_address((unsigned long)addr)))
ian@0 770 BUG();
ian@0 771 module_put(modaddr);
ian@0 772 }
ian@0 773 EXPORT_SYMBOL_GPL(symbol_put_addr);
ian@0 774
ian@0 775 static ssize_t show_refcnt(struct module_attribute *mattr,
ian@0 776 struct module *mod, char *buffer)
ian@0 777 {
ian@0 778 /* sysfs holds a reference */
ian@0 779 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
ian@0 780 }
ian@0 781
ian@0 782 static struct module_attribute refcnt = {
ian@0 783 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
ian@0 784 .show = show_refcnt,
ian@0 785 };
ian@0 786
ian@0 787 #else /* !CONFIG_MODULE_UNLOAD */
ian@0 788 static void print_unload_info(struct seq_file *m, struct module *mod)
ian@0 789 {
ian@0 790 /* We don't know the usage count, or what modules are using. */
ian@0 791 seq_printf(m, " - -");
ian@0 792 }
ian@0 793
ian@0 794 static inline void module_unload_free(struct module *mod)
ian@0 795 {
ian@0 796 }
ian@0 797
ian@0 798 static inline int use_module(struct module *a, struct module *b)
ian@0 799 {
ian@0 800 return strong_try_module_get(b);
ian@0 801 }
ian@0 802
ian@0 803 static inline void module_unload_init(struct module *mod)
ian@0 804 {
ian@0 805 }
ian@0 806 #endif /* CONFIG_MODULE_UNLOAD */
ian@0 807
ian@0 808 static struct module_attribute *modinfo_attrs[] = {
ian@0 809 &modinfo_version,
ian@0 810 &modinfo_srcversion,
ian@0 811 #ifdef CONFIG_MODULE_UNLOAD
ian@0 812 &refcnt,
ian@0 813 #endif
ian@0 814 NULL,
ian@0 815 };
ian@0 816
ian@0 817 static const char vermagic[] = VERMAGIC_STRING;
ian@0 818
ian@0 819 #ifdef CONFIG_MODVERSIONS
ian@0 820 static int check_version(Elf_Shdr *sechdrs,
ian@0 821 unsigned int versindex,
ian@0 822 const char *symname,
ian@0 823 struct module *mod,
ian@0 824 const unsigned long *crc)
ian@0 825 {
ian@0 826 unsigned int i, num_versions;
ian@0 827 struct modversion_info *versions;
ian@0 828
ian@0 829 /* Exporting module didn't supply crcs? OK, we're already tainted. */
ian@0 830 if (!crc)
ian@0 831 return 1;
ian@0 832
ian@0 833 versions = (void *) sechdrs[versindex].sh_addr;
ian@0 834 num_versions = sechdrs[versindex].sh_size
ian@0 835 / sizeof(struct modversion_info);
ian@0 836
ian@0 837 for (i = 0; i < num_versions; i++) {
ian@0 838 if (strcmp(versions[i].name, symname) != 0)
ian@0 839 continue;
ian@0 840
ian@0 841 if (versions[i].crc == *crc)
ian@0 842 return 1;
ian@0 843 printk("%s: disagrees about version of symbol %s\n",
ian@0 844 mod->name, symname);
ian@0 845 DEBUGP("Found checksum %lX vs module %lX\n",
ian@0 846 *crc, versions[i].crc);
ian@0 847 return 0;
ian@0 848 }
ian@0 849 /* Not in module's version table. OK, but that taints the kernel. */
ian@0 850 if (!(tainted & TAINT_FORCED_MODULE)) {
ian@0 851 printk("%s: no version for \"%s\" found: kernel tainted.\n",
ian@0 852 mod->name, symname);
ian@0 853 add_taint(TAINT_FORCED_MODULE);
ian@0 854 }
ian@0 855 return 1;
ian@0 856 }
ian@0 857
ian@0 858 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
ian@0 859 unsigned int versindex,
ian@0 860 struct module *mod)
ian@0 861 {
ian@0 862 const unsigned long *crc;
ian@0 863 struct module *owner;
ian@0 864
ian@0 865 if (!__find_symbol("struct_module", &owner, &crc, 1))
ian@0 866 BUG();
ian@0 867 return check_version(sechdrs, versindex, "struct_module", mod,
ian@0 868 crc);
ian@0 869 }
ian@0 870
ian@0 871 /* First part is kernel version, which we ignore. */
ian@0 872 static inline int same_magic(const char *amagic, const char *bmagic)
ian@0 873 {
ian@0 874 amagic += strcspn(amagic, " ");
ian@0 875 bmagic += strcspn(bmagic, " ");
ian@0 876 return strcmp(amagic, bmagic) == 0;
ian@0 877 }
ian@0 878 #else
ian@0 879 static inline int check_version(Elf_Shdr *sechdrs,
ian@0 880 unsigned int versindex,
ian@0 881 const char *symname,
ian@0 882 struct module *mod,
ian@0 883 const unsigned long *crc)
ian@0 884 {
ian@0 885 return 1;
ian@0 886 }
ian@0 887
ian@0 888 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
ian@0 889 unsigned int versindex,
ian@0 890 struct module *mod)
ian@0 891 {
ian@0 892 return 1;
ian@0 893 }
ian@0 894
ian@0 895 static inline int same_magic(const char *amagic, const char *bmagic)
ian@0 896 {
ian@0 897 return strcmp(amagic, bmagic) == 0;
ian@0 898 }
ian@0 899 #endif /* CONFIG_MODVERSIONS */
ian@0 900
ian@0 901 /* Resolve a symbol for this module. I.e. if we find one, record usage.
ian@0 902 Must be holding module_mutex. */
ian@0 903 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
ian@0 904 unsigned int versindex,
ian@0 905 const char *name,
ian@0 906 struct module *mod)
ian@0 907 {
ian@0 908 struct module *owner;
ian@0 909 unsigned long ret;
ian@0 910 const unsigned long *crc;
ian@0 911
ian@0 912 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
ian@0 913 if (ret) {
ian@0 914 /* use_module can fail due to OOM, or module unloading */
ian@0 915 if (!check_version(sechdrs, versindex, name, mod, crc) ||
ian@0 916 !use_module(mod, owner))
ian@0 917 ret = 0;
ian@0 918 }
ian@0 919 return ret;
ian@0 920 }
ian@0 921
ian@0 922
ian@0 923 /*
ian@0 924 * /sys/module/foo/sections stuff
ian@0 925 * J. Corbet <corbet@lwn.net>
ian@0 926 */
ian@0 927 #ifdef CONFIG_KALLSYMS
ian@0 928 static ssize_t module_sect_show(struct module_attribute *mattr,
ian@0 929 struct module *mod, char *buf)
ian@0 930 {
ian@0 931 struct module_sect_attr *sattr =
ian@0 932 container_of(mattr, struct module_sect_attr, mattr);
ian@0 933 return sprintf(buf, "0x%lx\n", sattr->address);
ian@0 934 }
ian@0 935
ian@0 936 static void add_sect_attrs(struct module *mod, unsigned int nsect,
ian@0 937 char *secstrings, Elf_Shdr *sechdrs)
ian@0 938 {
ian@0 939 unsigned int nloaded = 0, i, size[2];
ian@0 940 struct module_sect_attrs *sect_attrs;
ian@0 941 struct module_sect_attr *sattr;
ian@0 942 struct attribute **gattr;
ian@0 943
ian@0 944 /* Count loaded sections and allocate structures */
ian@0 945 for (i = 0; i < nsect; i++)
ian@0 946 if (sechdrs[i].sh_flags & SHF_ALLOC)
ian@0 947 nloaded++;
ian@0 948 size[0] = ALIGN(sizeof(*sect_attrs)
ian@0 949 + nloaded * sizeof(sect_attrs->attrs[0]),
ian@0 950 sizeof(sect_attrs->grp.attrs[0]));
ian@0 951 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
ian@0 952 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
ian@0 953 return;
ian@0 954
ian@0 955 /* Setup section attributes. */
ian@0 956 sect_attrs->grp.name = "sections";
ian@0 957 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
ian@0 958
ian@0 959 sattr = &sect_attrs->attrs[0];
ian@0 960 gattr = &sect_attrs->grp.attrs[0];
ian@0 961 for (i = 0; i < nsect; i++) {
ian@0 962 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
ian@0 963 continue;
ian@0 964 sattr->address = sechdrs[i].sh_addr;
ian@0 965 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
ian@0 966 MODULE_SECT_NAME_LEN);
ian@0 967 sattr->mattr.show = module_sect_show;
ian@0 968 sattr->mattr.store = NULL;
ian@0 969 sattr->mattr.attr.name = sattr->name;
ian@0 970 sattr->mattr.attr.owner = mod;
ian@0 971 sattr->mattr.attr.mode = S_IRUGO;
ian@0 972 *(gattr++) = &(sattr++)->mattr.attr;
ian@0 973 }
ian@0 974 *gattr = NULL;
ian@0 975
ian@0 976 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
ian@0 977 goto out;
ian@0 978
ian@0 979 mod->sect_attrs = sect_attrs;
ian@0 980 return;
ian@0 981 out:
ian@0 982 kfree(sect_attrs);
ian@0 983 }
ian@0 984
ian@0 985 static void remove_sect_attrs(struct module *mod)
ian@0 986 {
ian@0 987 if (mod->sect_attrs) {
ian@0 988 sysfs_remove_group(&mod->mkobj.kobj,
ian@0 989 &mod->sect_attrs->grp);
ian@0 990 /* We are positive that no one is using any sect attrs
ian@0 991 * at this point. Deallocate immediately. */
ian@0 992 kfree(mod->sect_attrs);
ian@0 993 mod->sect_attrs = NULL;
ian@0 994 }
ian@0 995 }
ian@0 996
ian@0 997
ian@0 998 #else
ian@0 999 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
ian@0 1000 char *sectstrings, Elf_Shdr *sechdrs)
ian@0 1001 {
ian@0 1002 }
ian@0 1003
ian@0 1004 static inline void remove_sect_attrs(struct module *mod)
ian@0 1005 {
ian@0 1006 }
ian@0 1007 #endif /* CONFIG_KALLSYMS */
ian@0 1008
ian@0 1009 static int module_add_modinfo_attrs(struct module *mod)
ian@0 1010 {
ian@0 1011 struct module_attribute *attr;
ian@0 1012 struct module_attribute *temp_attr;
ian@0 1013 int error = 0;
ian@0 1014 int i;
ian@0 1015
ian@0 1016 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
ian@0 1017 (ARRAY_SIZE(modinfo_attrs) + 1)),
ian@0 1018 GFP_KERNEL);
ian@0 1019 if (!mod->modinfo_attrs)
ian@0 1020 return -ENOMEM;
ian@0 1021
ian@0 1022 temp_attr = mod->modinfo_attrs;
ian@0 1023 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
ian@0 1024 if (!attr->test ||
ian@0 1025 (attr->test && attr->test(mod))) {
ian@0 1026 memcpy(temp_attr, attr, sizeof(*temp_attr));
ian@0 1027 temp_attr->attr.owner = mod;
ian@0 1028 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
ian@0 1029 ++temp_attr;
ian@0 1030 }
ian@0 1031 }
ian@0 1032 return error;
ian@0 1033 }
ian@0 1034
ian@0 1035 static void module_remove_modinfo_attrs(struct module *mod)
ian@0 1036 {
ian@0 1037 struct module_attribute *attr;
ian@0 1038 int i;
ian@0 1039
ian@0 1040 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
ian@0 1041 /* pick a field to test for end of list */
ian@0 1042 if (!attr->attr.name)
ian@0 1043 break;
ian@0 1044 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
ian@0 1045 if (attr->free)
ian@0 1046 attr->free(mod);
ian@0 1047 }
ian@0 1048 kfree(mod->modinfo_attrs);
ian@0 1049 }
ian@0 1050
ian@0 1051 static int mod_sysfs_setup(struct module *mod,
ian@0 1052 struct kernel_param *kparam,
ian@0 1053 unsigned int num_params)
ian@0 1054 {
ian@0 1055 int err;
ian@0 1056
ian@240 1057 if (!module_subsys.kset.subsys) {
ian@240 1058 printk(KERN_ERR "%s: module_subsys not initialized\n",
ian@240 1059 mod->name);
ian@240 1060 err = -EINVAL;
ian@240 1061 goto out;
ian@240 1062 }
ian@0 1063 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
ian@0 1064 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
ian@0 1065 if (err)
ian@0 1066 goto out;
ian@0 1067 kobj_set_kset_s(&mod->mkobj, module_subsys);
ian@0 1068 mod->mkobj.mod = mod;
ian@0 1069 err = kobject_register(&mod->mkobj.kobj);
ian@0 1070 if (err)
ian@0 1071 goto out;
ian@0 1072
ian@0 1073 err = module_param_sysfs_setup(mod, kparam, num_params);
ian@0 1074 if (err)
ian@0 1075 goto out_unreg;
ian@0 1076
ian@0 1077 err = module_add_modinfo_attrs(mod);
ian@0 1078 if (err)
ian@0 1079 goto out_unreg;
ian@0 1080
ian@0 1081 return 0;
ian@0 1082
ian@0 1083 out_unreg:
ian@0 1084 kobject_unregister(&mod->mkobj.kobj);
ian@0 1085 out:
ian@0 1086 return err;
ian@0 1087 }
ian@0 1088
ian@0 1089 static void mod_kobject_remove(struct module *mod)
ian@0 1090 {
ian@0 1091 module_remove_modinfo_attrs(mod);
ian@0 1092 module_param_sysfs_remove(mod);
ian@0 1093
ian@0 1094 kobject_unregister(&mod->mkobj.kobj);
ian@0 1095 }
ian@0 1096
ian@0 1097 /*
ian@0 1098 * unlink the module with the whole machine is stopped with interrupts off
ian@0 1099 * - this defends against kallsyms not taking locks
ian@0 1100 */
ian@0 1101 static int __unlink_module(void *_mod)
ian@0 1102 {
ian@0 1103 struct module *mod = _mod;
ian@0 1104 list_del(&mod->list);
ian@0 1105 return 0;
ian@0 1106 }
ian@0 1107
ian@0 1108 /* Free a module, remove from lists, etc (must hold module mutex). */
ian@0 1109 static void free_module(struct module *mod)
ian@0 1110 {
ian@0 1111 /* Delete from various lists */
ian@0 1112 stop_machine_run(__unlink_module, mod, NR_CPUS);
ian@0 1113 remove_sect_attrs(mod);
ian@0 1114 mod_kobject_remove(mod);
ian@0 1115
ian@0 1116 unwind_remove_table(mod->unwind_info, 0);
ian@0 1117
ian@0 1118 /* Arch-specific cleanup. */
ian@0 1119 module_arch_cleanup(mod);
ian@0 1120
ian@0 1121 /* Module unload stuff */
ian@0 1122 module_unload_free(mod);
ian@0 1123
ian@0 1124 /* This may be NULL, but that's OK */
ian@0 1125 module_free(mod, mod->module_init);
ian@0 1126 kfree(mod->args);
ian@0 1127 if (mod->percpu)
ian@0 1128 percpu_modfree(mod->percpu);
ian@0 1129
ian@0 1130 /* Free lock-classes: */
ian@0 1131 lockdep_free_key_range(mod->module_core, mod->core_size);
ian@0 1132
ian@0 1133 /* Finally, free the core (containing the module structure) */
ian@0 1134 module_free(mod, mod->module_core);
ian@0 1135 }
ian@0 1136
ian@0 1137 void *__symbol_get(const char *symbol)
ian@0 1138 {
ian@0 1139 struct module *owner;
ian@0 1140 unsigned long value, flags;
ian@0 1141 const unsigned long *crc;
ian@0 1142
ian@0 1143 spin_lock_irqsave(&modlist_lock, flags);
ian@0 1144 value = __find_symbol(symbol, &owner, &crc, 1);
ian@0 1145 if (value && !strong_try_module_get(owner))
ian@0 1146 value = 0;
ian@0 1147 spin_unlock_irqrestore(&modlist_lock, flags);
ian@0 1148
ian@0 1149 return (void *)value;
ian@0 1150 }
ian@0 1151 EXPORT_SYMBOL_GPL(__symbol_get);
ian@0 1152
ian@0 1153 /*
ian@0 1154 * Ensure that an exported symbol [global namespace] does not already exist
ian@0 1155 * in the Kernel or in some other modules exported symbol table.
ian@0 1156 */
ian@0 1157 static int verify_export_symbols(struct module *mod)
ian@0 1158 {
ian@0 1159 const char *name = NULL;
ian@0 1160 unsigned long i, ret = 0;
ian@0 1161 struct module *owner;
ian@0 1162 const unsigned long *crc;
ian@0 1163
ian@0 1164 for (i = 0; i < mod->num_syms; i++)
ian@0 1165 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
ian@0 1166 name = mod->syms[i].name;
ian@0 1167 ret = -ENOEXEC;
ian@0 1168 goto dup;
ian@0 1169 }
ian@0 1170
ian@0 1171 for (i = 0; i < mod->num_gpl_syms; i++)
ian@0 1172 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
ian@0 1173 name = mod->gpl_syms[i].name;
ian@0 1174 ret = -ENOEXEC;
ian@0 1175 goto dup;
ian@0 1176 }
ian@0 1177
ian@0 1178 dup:
ian@0 1179 if (ret)
ian@0 1180 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
ian@0 1181 mod->name, name, module_name(owner));
ian@0 1182
ian@0 1183 return ret;
ian@0 1184 }
ian@0 1185
ian@0 1186 /* Change all symbols so that sh_value encodes the pointer directly. */
ian@0 1187 static int simplify_symbols(Elf_Shdr *sechdrs,
ian@0 1188 unsigned int symindex,
ian@0 1189 const char *strtab,
ian@0 1190 unsigned int versindex,
ian@0 1191 unsigned int pcpuindex,
ian@0 1192 struct module *mod)
ian@0 1193 {
ian@0 1194 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
ian@0 1195 unsigned long secbase;
ian@0 1196 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
ian@0 1197 int ret = 0;
ian@0 1198
ian@0 1199 for (i = 1; i < n; i++) {
ian@0 1200 switch (sym[i].st_shndx) {
ian@0 1201 case SHN_COMMON:
ian@0 1202 /* We compiled with -fno-common. These are not
ian@0 1203 supposed to happen. */
ian@0 1204 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
ian@0 1205 printk("%s: please compile with -fno-common\n",
ian@0 1206 mod->name);
ian@0 1207 ret = -ENOEXEC;
ian@0 1208 break;
ian@0 1209
ian@0 1210 case SHN_ABS:
ian@0 1211 /* Don't need to do anything */
ian@0 1212 DEBUGP("Absolute symbol: 0x%08lx\n",
ian@0 1213 (long)sym[i].st_value);
ian@0 1214 break;
ian@0 1215
ian@0 1216 case SHN_UNDEF:
ian@0 1217 sym[i].st_value
ian@0 1218 = resolve_symbol(sechdrs, versindex,
ian@0 1219 strtab + sym[i].st_name, mod);
ian@0 1220
ian@0 1221 /* Ok if resolved. */
ian@0 1222 if (sym[i].st_value != 0)
ian@0 1223 break;
ian@0 1224 /* Ok if weak. */
ian@0 1225 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
ian@0 1226 break;
ian@0 1227
ian@0 1228 printk(KERN_WARNING "%s: Unknown symbol %s\n",
ian@0 1229 mod->name, strtab + sym[i].st_name);
ian@0 1230 ret = -ENOENT;
ian@0 1231 break;
ian@0 1232
ian@0 1233 default:
ian@0 1234 /* Divert to percpu allocation if a percpu var. */
ian@0 1235 if (sym[i].st_shndx == pcpuindex)
ian@0 1236 secbase = (unsigned long)mod->percpu;
ian@0 1237 else
ian@0 1238 secbase = sechdrs[sym[i].st_shndx].sh_addr;
ian@0 1239 sym[i].st_value += secbase;
ian@0 1240 break;
ian@0 1241 }
ian@0 1242 }
ian@0 1243
ian@0 1244 return ret;
ian@0 1245 }
ian@0 1246
ian@0 1247 /* Update size with this section: return offset. */
ian@0 1248 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
ian@0 1249 {
ian@0 1250 long ret;
ian@0 1251
ian@0 1252 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
ian@0 1253 *size = ret + sechdr->sh_size;
ian@0 1254 return ret;
ian@0 1255 }
ian@0 1256
ian@0 1257 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
ian@0 1258 might -- code, read-only data, read-write data, small data. Tally
ian@0 1259 sizes, and place the offsets into sh_entsize fields: high bit means it
ian@0 1260 belongs in init. */
ian@0 1261 static void layout_sections(struct module *mod,
ian@0 1262 const Elf_Ehdr *hdr,
ian@0 1263 Elf_Shdr *sechdrs,
ian@0 1264 const char *secstrings)
ian@0 1265 {
ian@0 1266 static unsigned long const masks[][2] = {
ian@0 1267 /* NOTE: all executable code must be the first section
ian@0 1268 * in this array; otherwise modify the text_size
ian@0 1269 * finder in the two loops below */
ian@0 1270 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
ian@0 1271 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
ian@0 1272 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
ian@0 1273 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
ian@0 1274 };
ian@0 1275 unsigned int m, i;
ian@0 1276
ian@0 1277 for (i = 0; i < hdr->e_shnum; i++)
ian@0 1278 sechdrs[i].sh_entsize = ~0UL;
ian@0 1279
ian@0 1280 DEBUGP("Core section allocation order:\n");
ian@0 1281 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
ian@0 1282 for (i = 0; i < hdr->e_shnum; ++i) {
ian@0 1283 Elf_Shdr *s = &sechdrs[i];
ian@0 1284
ian@0 1285 if ((s->sh_flags & masks[m][0]) != masks[m][0]
ian@0 1286 || (s->sh_flags & masks[m][1])
ian@0 1287 || s->sh_entsize != ~0UL
ian@0 1288 || strncmp(secstrings + s->sh_name,
ian@0 1289 ".init", 5) == 0)
ian@0 1290 continue;
ian@0 1291 s->sh_entsize = get_offset(&mod->core_size, s);
ian@0 1292 DEBUGP("\t%s\n", secstrings + s->sh_name);
ian@0 1293 }
ian@0 1294 if (m == 0)
ian@0 1295 mod->core_text_size = mod->core_size;
ian@0 1296 }
ian@0 1297
ian@0 1298 DEBUGP("Init section allocation order:\n");
ian@0 1299 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
ian@0 1300 for (i = 0; i < hdr->e_shnum; ++i) {
ian@0 1301 Elf_Shdr *s = &sechdrs[i];
ian@0 1302
ian@0 1303 if ((s->sh_flags & masks[m][0]) != masks[m][0]
ian@0 1304 || (s->sh_flags & masks[m][1])
ian@0 1305 || s->sh_entsize != ~0UL
ian@0 1306 || strncmp(secstrings + s->sh_name,
ian@0 1307 ".init", 5) != 0)
ian@0 1308 continue;
ian@0 1309 s->sh_entsize = (get_offset(&mod->init_size, s)
ian@0 1310 | INIT_OFFSET_MASK);
ian@0 1311 DEBUGP("\t%s\n", secstrings + s->sh_name);
ian@0 1312 }
ian@0 1313 if (m == 0)
ian@0 1314 mod->init_text_size = mod->init_size;
ian@0 1315 }
ian@0 1316 }
ian@0 1317
ian@0 1318 static void set_license(struct module *mod, const char *license)
ian@0 1319 {
ian@0 1320 if (!license)
ian@0 1321 license = "unspecified";
ian@0 1322
ian@0 1323 mod->license_gplok = license_is_gpl_compatible(license);
ian@0 1324 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
ian@0 1325 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
ian@0 1326 mod->name, license);
ian@0 1327 add_taint(TAINT_PROPRIETARY_MODULE);
ian@0 1328 }
ian@0 1329 }
ian@0 1330
ian@0 1331 /* Parse tag=value strings from .modinfo section */
ian@0 1332 static char *next_string(char *string, unsigned long *secsize)
ian@0 1333 {
ian@0 1334 /* Skip non-zero chars */
ian@0 1335 while (string[0]) {
ian@0 1336 string++;
ian@0 1337 if ((*secsize)-- <= 1)
ian@0 1338 return NULL;
ian@0 1339 }
ian@0 1340
ian@0 1341 /* Skip any zero padding. */
ian@0 1342 while (!string[0]) {
ian@0 1343 string++;
ian@0 1344 if ((*secsize)-- <= 1)
ian@0 1345 return NULL;
ian@0 1346 }
ian@0 1347 return string;
ian@0 1348 }
ian@0 1349
ian@0 1350 static char *get_modinfo(Elf_Shdr *sechdrs,
ian@0 1351 unsigned int info,
ian@0 1352 const char *tag)
ian@0 1353 {
ian@0 1354 char *p;
ian@0 1355 unsigned int taglen = strlen(tag);
ian@0 1356 unsigned long size = sechdrs[info].sh_size;
ian@0 1357
ian@0 1358 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
ian@0 1359 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
ian@0 1360 return p + taglen + 1;
ian@0 1361 }
ian@0 1362 return NULL;
ian@0 1363 }
ian@0 1364
ian@0 1365 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
ian@0 1366 unsigned int infoindex)
ian@0 1367 {
ian@0 1368 struct module_attribute *attr;
ian@0 1369 int i;
ian@0 1370
ian@0 1371 for (i = 0; (attr = modinfo_attrs[i]); i++) {
ian@0 1372 if (attr->setup)
ian@0 1373 attr->setup(mod,
ian@0 1374 get_modinfo(sechdrs,
ian@0 1375 infoindex,
ian@0 1376 attr->attr.name));
ian@0 1377 }
ian@0 1378 }
ian@0 1379
ian@0 1380 #ifdef CONFIG_KALLSYMS
ian@0 1381 int is_exported(const char *name, const struct module *mod)
ian@0 1382 {
ian@0 1383 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
ian@0 1384 return 1;
ian@0 1385 else
ian@0 1386 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
ian@0 1387 return 1;
ian@0 1388 else
ian@0 1389 return 0;
ian@0 1390 }
ian@0 1391
ian@0 1392 /* As per nm */
ian@0 1393 static char elf_type(const Elf_Sym *sym,
ian@0 1394 Elf_Shdr *sechdrs,
ian@0 1395 const char *secstrings,
ian@0 1396 struct module *mod)
ian@0 1397 {
ian@0 1398 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
ian@0 1399 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
ian@0 1400 return 'v';
ian@0 1401 else
ian@0 1402 return 'w';
ian@0 1403 }
ian@0 1404 if (sym->st_shndx == SHN_UNDEF)
ian@0 1405 return 'U';
ian@0 1406 if (sym->st_shndx == SHN_ABS)
ian@0 1407 return 'a';
ian@0 1408 if (sym->st_shndx >= SHN_LORESERVE)
ian@0 1409 return '?';
ian@0 1410 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
ian@0 1411 return 't';
ian@0 1412 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
ian@0 1413 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
ian@0 1414 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
ian@0 1415 return 'r';
ian@0 1416 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
ian@0 1417 return 'g';
ian@0 1418 else
ian@0 1419 return 'd';
ian@0 1420 }
ian@0 1421 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
ian@0 1422 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
ian@0 1423 return 's';
ian@0 1424 else
ian@0 1425 return 'b';
ian@0 1426 }
ian@0 1427 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
ian@0 1428 ".debug", strlen(".debug")) == 0)
ian@0 1429 return 'n';
ian@0 1430 return '?';
ian@0 1431 }
ian@0 1432
ian@0 1433 static void add_kallsyms(struct module *mod,
ian@0 1434 Elf_Shdr *sechdrs,
ian@0 1435 unsigned int symindex,
ian@0 1436 unsigned int strindex,
ian@0 1437 const char *secstrings)
ian@0 1438 {
ian@0 1439 unsigned int i;
ian@0 1440
ian@0 1441 mod->symtab = (void *)sechdrs[symindex].sh_addr;
ian@0 1442 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
ian@0 1443 mod->strtab = (void *)sechdrs[strindex].sh_addr;
ian@0 1444
ian@0 1445 /* Set types up while we still have access to sections. */
ian@0 1446 for (i = 0; i < mod->num_symtab; i++)
ian@0 1447 mod->symtab[i].st_info
ian@0 1448 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
ian@0 1449 }
ian@0 1450 #else
ian@0 1451 static inline void add_kallsyms(struct module *mod,
ian@0 1452 Elf_Shdr *sechdrs,
ian@0 1453 unsigned int symindex,
ian@0 1454 unsigned int strindex,
ian@0 1455 const char *secstrings)
ian@0 1456 {
ian@0 1457 }
ian@0 1458 #endif /* CONFIG_KALLSYMS */
ian@0 1459
ian@0 1460 /* Allocate and load the module: note that size of section 0 is always
ian@0 1461 zero, and we rely on this for optional sections. */
ian@0 1462 static struct module *load_module(void __user *umod,
ian@0 1463 unsigned long len,
ian@0 1464 const char __user *uargs)
ian@0 1465 {
ian@0 1466 Elf_Ehdr *hdr;
ian@0 1467 Elf_Shdr *sechdrs;
ian@0 1468 char *secstrings, *args, *modmagic, *strtab = NULL;
ian@0 1469 unsigned int i;
ian@0 1470 unsigned int symindex = 0;
ian@0 1471 unsigned int strindex = 0;
ian@0 1472 unsigned int setupindex;
ian@0 1473 unsigned int exindex;
ian@0 1474 unsigned int exportindex;
ian@0 1475 unsigned int modindex;
ian@0 1476 unsigned int obsparmindex;
ian@0 1477 unsigned int infoindex;
ian@0 1478 unsigned int gplindex;
ian@0 1479 unsigned int crcindex;
ian@0 1480 unsigned int gplcrcindex;
ian@0 1481 unsigned int versindex;
ian@0 1482 unsigned int pcpuindex;
ian@0 1483 unsigned int gplfutureindex;
ian@0 1484 unsigned int gplfuturecrcindex;
ian@0 1485 unsigned int unwindex = 0;
ian@0 1486 unsigned int unusedindex;
ian@0 1487 unsigned int unusedcrcindex;
ian@0 1488 unsigned int unusedgplindex;
ian@0 1489 unsigned int unusedgplcrcindex;
ian@0 1490 struct module *mod;
ian@0 1491 long err = 0;
ian@0 1492 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
ian@0 1493 struct exception_table_entry *extable;
ian@0 1494 mm_segment_t old_fs;
ian@0 1495
ian@0 1496 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
ian@0 1497 umod, len, uargs);
ian@0 1498 if (len < sizeof(*hdr))
ian@0 1499 return ERR_PTR(-ENOEXEC);
ian@0 1500
ian@0 1501 /* Suck in entire file: we'll want most of it. */
ian@0 1502 /* vmalloc barfs on "unusual" numbers. Check here */
ian@0 1503 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
ian@0 1504 return ERR_PTR(-ENOMEM);
ian@0 1505 if (copy_from_user(hdr, umod, len) != 0) {
ian@0 1506 err = -EFAULT;
ian@0 1507 goto free_hdr;
ian@0 1508 }
ian@0 1509
ian@0 1510 /* Sanity checks against insmoding binaries or wrong arch,
ian@0 1511 weird elf version */
ian@0 1512 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
ian@0 1513 || hdr->e_type != ET_REL
ian@0 1514 || !elf_check_arch(hdr)
ian@0 1515 || hdr->e_shentsize != sizeof(*sechdrs)) {
ian@0 1516 err = -ENOEXEC;
ian@0 1517 goto free_hdr;
ian@0 1518 }
ian@0 1519
ian@0 1520 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
ian@0 1521 goto truncated;
ian@0 1522
ian@0 1523 /* Convenience variables */
ian@0 1524 sechdrs = (void *)hdr + hdr->e_shoff;
ian@0 1525 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
ian@0 1526 sechdrs[0].sh_addr = 0;
ian@0 1527
ian@0 1528 for (i = 1; i < hdr->e_shnum; i++) {
ian@0 1529 if (sechdrs[i].sh_type != SHT_NOBITS
ian@0 1530 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
ian@0 1531 goto truncated;
ian@0 1532
ian@0 1533 /* Mark all sections sh_addr with their address in the
ian@0 1534 temporary image. */
ian@0 1535 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
ian@0 1536
ian@0 1537 /* Internal symbols and strings. */
ian@0 1538 if (sechdrs[i].sh_type == SHT_SYMTAB) {
ian@0 1539 symindex = i;
ian@0 1540 strindex = sechdrs[i].sh_link;
ian@0 1541 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
ian@0 1542 }
ian@0 1543 #ifndef CONFIG_MODULE_UNLOAD
ian@0 1544 /* Don't load .exit sections */
ian@0 1545 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
ian@0 1546 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
ian@0 1547 #endif
ian@0 1548 }
ian@0 1549
ian@0 1550 modindex = find_sec(hdr, sechdrs, secstrings,
ian@0 1551 ".gnu.linkonce.this_module");
ian@0 1552 if (!modindex) {
ian@0 1553 printk(KERN_WARNING "No module found in object\n");
ian@0 1554 err = -ENOEXEC;
ian@0 1555 goto free_hdr;
ian@0 1556 }
ian@0 1557 mod = (void *)sechdrs[modindex].sh_addr;
ian@0 1558
ian@0 1559 if (symindex == 0) {
ian@0 1560 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
ian@0 1561 mod->name);
ian@0 1562 err = -ENOEXEC;
ian@0 1563 goto free_hdr;
ian@0 1564 }
ian@0 1565
ian@0 1566 /* Optional sections */
ian@0 1567 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
ian@0 1568 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
ian@0 1569 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
ian@0 1570 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
ian@0 1571 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
ian@0 1572 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
ian@0 1573 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
ian@0 1574 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
ian@0 1575 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
ian@0 1576 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
ian@0 1577 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
ian@0 1578 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
ian@0 1579 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
ian@0 1580 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
ian@0 1581 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
ian@0 1582 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
ian@0 1583 #ifdef ARCH_UNWIND_SECTION_NAME
ian@0 1584 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
ian@0 1585 #endif
ian@0 1586
ian@0 1587 /* Don't keep modinfo section */
ian@0 1588 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
ian@0 1589 #ifdef CONFIG_KALLSYMS
ian@0 1590 /* Keep symbol and string tables for decoding later. */
ian@0 1591 sechdrs[symindex].sh_flags |= SHF_ALLOC;
ian@0 1592 sechdrs[strindex].sh_flags |= SHF_ALLOC;
ian@0 1593 #endif
ian@0 1594 if (unwindex)
ian@0 1595 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
ian@0 1596
ian@0 1597 /* Check module struct version now, before we try to use module. */
ian@0 1598 if (!check_modstruct_version(sechdrs, versindex, mod)) {
ian@0 1599 err = -ENOEXEC;
ian@0 1600 goto free_hdr;
ian@0 1601 }
ian@0 1602
ian@0 1603 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
ian@0 1604 /* This is allowed: modprobe --force will invalidate it. */
ian@0 1605 if (!modmagic) {
ian@0 1606 add_taint(TAINT_FORCED_MODULE);
ian@0 1607 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
ian@0 1608 mod->name);
ian@0 1609 } else if (!same_magic(modmagic, vermagic)) {
ian@0 1610 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
ian@0 1611 mod->name, modmagic, vermagic);
ian@0 1612 err = -ENOEXEC;
ian@0 1613 goto free_hdr;
ian@0 1614 }
ian@0 1615
ian@0 1616 /* Now copy in args */
ian@0 1617 args = strndup_user(uargs, ~0UL >> 1);
ian@0 1618 if (IS_ERR(args)) {
ian@0 1619 err = PTR_ERR(args);
ian@0 1620 goto free_hdr;
ian@0 1621 }
ian@0 1622
ian@0 1623 if (find_module(mod->name)) {
ian@0 1624 err = -EEXIST;
ian@0 1625 goto free_mod;
ian@0 1626 }
ian@0 1627
ian@0 1628 mod->state = MODULE_STATE_COMING;
ian@0 1629
ian@0 1630 /* Allow arches to frob section contents and sizes. */
ian@0 1631 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
ian@0 1632 if (err < 0)
ian@0 1633 goto free_mod;
ian@0 1634
ian@0 1635 if (pcpuindex) {
ian@0 1636 /* We have a special allocation for this section. */
ian@0 1637 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
ian@0 1638 sechdrs[pcpuindex].sh_addralign,
ian@0 1639 mod->name);
ian@0 1640 if (!percpu) {
ian@0 1641 err = -ENOMEM;
ian@0 1642 goto free_mod;
ian@0 1643 }
ian@0 1644 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
ian@0 1645 mod->percpu = percpu;
ian@0 1646 }
ian@0 1647
ian@0 1648 /* Determine total sizes, and put offsets in sh_entsize. For now
ian@0 1649 this is done generically; there doesn't appear to be any
ian@0 1650 special cases for the architectures. */
ian@0 1651 layout_sections(mod, hdr, sechdrs, secstrings);
ian@0 1652
ian@0 1653 /* Do the allocs. */
ian@0 1654 ptr = module_alloc(mod->core_size);
ian@0 1655 if (!ptr) {
ian@0 1656 err = -ENOMEM;
ian@0 1657 goto free_percpu;
ian@0 1658 }
ian@0 1659 memset(ptr, 0, mod->core_size);
ian@0 1660 mod->module_core = ptr;
ian@0 1661
ian@0 1662 ptr = module_alloc(mod->init_size);
ian@0 1663 if (!ptr && mod->init_size) {
ian@0 1664 err = -ENOMEM;
ian@0 1665 goto free_core;
ian@0 1666 }
ian@0 1667 memset(ptr, 0, mod->init_size);
ian@0 1668 mod->module_init = ptr;
ian@0 1669
ian@0 1670 /* Transfer each section which specifies SHF_ALLOC */
ian@0 1671 DEBUGP("final section addresses:\n");
ian@0 1672 for (i = 0; i < hdr->e_shnum; i++) {
ian@0 1673 void *dest;
ian@0 1674
ian@0 1675 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
ian@0 1676 continue;
ian@0 1677
ian@0 1678 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
ian@0 1679 dest = mod->module_init
ian@0 1680 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
ian@0 1681 else
ian@0 1682 dest = mod->module_core + sechdrs[i].sh_entsize;
ian@0 1683
ian@0 1684 if (sechdrs[i].sh_type != SHT_NOBITS)
ian@0 1685 memcpy(dest, (void *)sechdrs[i].sh_addr,
ian@0 1686 sechdrs[i].sh_size);
ian@0 1687 /* Update sh_addr to point to copy in image. */
ian@0 1688 sechdrs[i].sh_addr = (unsigned long)dest;
ian@0 1689 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
ian@0 1690 }
ian@0 1691 /* Module has been moved. */
ian@0 1692 mod = (void *)sechdrs[modindex].sh_addr;
ian@0 1693
ian@0 1694 /* Now we've moved module, initialize linked lists, etc. */
ian@0 1695 module_unload_init(mod);
ian@0 1696
ian@0 1697 /* Set up license info based on the info section */
ian@0 1698 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
ian@0 1699
ian@0 1700 if (strcmp(mod->name, "ndiswrapper") == 0)
ian@0 1701 add_taint(TAINT_PROPRIETARY_MODULE);
ian@0 1702 if (strcmp(mod->name, "driverloader") == 0)
ian@0 1703 add_taint(TAINT_PROPRIETARY_MODULE);
ian@0 1704
ian@0 1705 /* Set up MODINFO_ATTR fields */
ian@0 1706 setup_modinfo(mod, sechdrs, infoindex);
ian@0 1707
ian@0 1708 /* Fix up syms, so that st_value is a pointer to location. */
ian@0 1709 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
ian@0 1710 mod);
ian@0 1711 if (err < 0)
ian@0 1712 goto cleanup;
ian@0 1713
ian@0 1714 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
ian@0 1715 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
ian@0 1716 mod->syms = (void *)sechdrs[exportindex].sh_addr;
ian@0 1717 if (crcindex)
ian@0 1718 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
ian@0 1719 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
ian@0 1720 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
ian@0 1721 if (gplcrcindex)
ian@0 1722 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
ian@0 1723 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
ian@0 1724 sizeof(*mod->gpl_future_syms);
ian@0 1725 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
ian@0 1726 sizeof(*mod->unused_syms);
ian@0 1727 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
ian@0 1728 sizeof(*mod->unused_gpl_syms);
ian@0 1729 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
ian@0 1730 if (gplfuturecrcindex)
ian@0 1731 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
ian@0 1732
ian@0 1733 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
ian@0 1734 if (unusedcrcindex)
ian@0 1735 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
ian@0 1736 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
ian@0 1737 if (unusedgplcrcindex)
ian@0 1738 mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
ian@0 1739
ian@0 1740 #ifdef CONFIG_MODVERSIONS
ian@0 1741 if ((mod->num_syms && !crcindex) ||
ian@0 1742 (mod->num_gpl_syms && !gplcrcindex) ||
ian@0 1743 (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
ian@0 1744 (mod->num_unused_syms && !unusedcrcindex) ||
ian@0 1745 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
ian@0 1746 printk(KERN_WARNING "%s: No versions for exported symbols."
ian@0 1747 " Tainting kernel.\n", mod->name);
ian@0 1748 add_taint(TAINT_FORCED_MODULE);
ian@0 1749 }
ian@0 1750 #endif
ian@0 1751
ian@0 1752 /* Now do relocations. */
ian@0 1753 for (i = 1; i < hdr->e_shnum; i++) {
ian@0 1754 const char *strtab = (char *)sechdrs[strindex].sh_addr;
ian@0 1755 unsigned int info = sechdrs[i].sh_info;
ian@0 1756
ian@0 1757 /* Not a valid relocation section? */
ian@0 1758 if (info >= hdr->e_shnum)
ian@0 1759 continue;
ian@0 1760
ian@0 1761 /* Don't bother with non-allocated sections */
ian@0 1762 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
ian@0 1763 continue;
ian@0 1764
ian@0 1765 if (sechdrs[i].sh_type == SHT_REL)
ian@0 1766 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
ian@0 1767 else if (sechdrs[i].sh_type == SHT_RELA)
ian@0 1768 err = apply_relocate_add(sechdrs, strtab, symindex, i,
ian@0 1769 mod);
ian@0 1770 if (err < 0)
ian@0 1771 goto cleanup;
ian@0 1772 }
ian@0 1773
ian@0 1774 /* Find duplicate symbols */
ian@0 1775 err = verify_export_symbols(mod);
ian@0 1776
ian@0 1777 if (err < 0)
ian@0 1778 goto cleanup;
ian@0 1779
ian@0 1780 /* Set up and sort exception table */
ian@0 1781 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
ian@0 1782 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
ian@0 1783 sort_extable(extable, extable + mod->num_exentries);
ian@0 1784
ian@0 1785 /* Finally, copy percpu area over. */
ian@0 1786 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
ian@0 1787 sechdrs[pcpuindex].sh_size);
ian@0 1788
ian@0 1789 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
ian@0 1790
ian@0 1791 err = module_finalize(hdr, sechdrs, mod);
ian@0 1792 if (err < 0)
ian@0 1793 goto cleanup;
ian@0 1794
ian@0 1795 /* flush the icache in correct context */
ian@0 1796 old_fs = get_fs();
ian@0 1797 set_fs(KERNEL_DS);
ian@0 1798
ian@0 1799 /*
ian@0 1800 * Flush the instruction cache, since we've played with text.
ian@0 1801 * Do it before processing of module parameters, so the module
ian@0 1802 * can provide parameter accessor functions of its own.
ian@0 1803 */
ian@0 1804 if (mod->module_init)
ian@0 1805 flush_icache_range((unsigned long)mod->module_init,
ian@0 1806 (unsigned long)mod->module_init
ian@0 1807 + mod->init_size);
ian@0 1808 flush_icache_range((unsigned long)mod->module_core,
ian@0 1809 (unsigned long)mod->module_core + mod->core_size);
ian@0 1810
ian@0 1811 set_fs(old_fs);
ian@0 1812
ian@0 1813 mod->args = args;
ian@0 1814 if (obsparmindex)
ian@0 1815 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
ian@0 1816 mod->name);
ian@0 1817
ian@0 1818 /* Size of section 0 is 0, so this works well if no params */
ian@0 1819 err = parse_args(mod->name, mod->args,
ian@0 1820 (struct kernel_param *)
ian@0 1821 sechdrs[setupindex].sh_addr,
ian@0 1822 sechdrs[setupindex].sh_size
ian@0 1823 / sizeof(struct kernel_param),
ian@0 1824 NULL);
ian@0 1825 if (err < 0)
ian@0 1826 goto arch_cleanup;
ian@0 1827
ian@0 1828 err = mod_sysfs_setup(mod,
ian@0 1829 (struct kernel_param *)
ian@0 1830 sechdrs[setupindex].sh_addr,
ian@0 1831 sechdrs[setupindex].sh_size
ian@0 1832 / sizeof(struct kernel_param));
ian@0 1833 if (err < 0)
ian@0 1834 goto arch_cleanup;
ian@0 1835 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
ian@0 1836
ian@0 1837 /* Size of section 0 is 0, so this works well if no unwind info. */
ian@0 1838 mod->unwind_info = unwind_add_table(mod,
ian@0 1839 (void *)sechdrs[unwindex].sh_addr,
ian@0 1840 sechdrs[unwindex].sh_size);
ian@0 1841
ian@0 1842 /* Get rid of temporary copy */
ian@0 1843 vfree(hdr);
ian@0 1844
ian@0 1845 /* Done! */
ian@0 1846 return mod;
ian@0 1847
ian@0 1848 arch_cleanup:
ian@0 1849 module_arch_cleanup(mod);
ian@0 1850 cleanup:
ian@0 1851 module_unload_free(mod);
ian@0 1852 module_free(mod, mod->module_init);
ian@0 1853 free_core:
ian@0 1854 module_free(mod, mod->module_core);
ian@0 1855 free_percpu:
ian@0 1856 if (percpu)
ian@0 1857 percpu_modfree(percpu);
ian@0 1858 free_mod:
ian@0 1859 kfree(args);
ian@0 1860 free_hdr:
ian@0 1861 vfree(hdr);
ian@0 1862 return ERR_PTR(err);
ian@0 1863
ian@0 1864 truncated:
ian@0 1865 printk(KERN_ERR "Module len %lu truncated\n", len);
ian@0 1866 err = -ENOEXEC;
ian@0 1867 goto free_hdr;
ian@0 1868 }
ian@0 1869
ian@0 1870 /*
ian@0 1871 * link the module with the whole machine is stopped with interrupts off
ian@0 1872 * - this defends against kallsyms not taking locks
ian@0 1873 */
ian@0 1874 static int __link_module(void *_mod)
ian@0 1875 {
ian@0 1876 struct module *mod = _mod;
ian@0 1877 list_add(&mod->list, &modules);
ian@0 1878 return 0;
ian@0 1879 }
ian@0 1880
ian@0 1881 /* This is where the real work happens */
ian@0 1882 asmlinkage long
ian@0 1883 sys_init_module(void __user *umod,
ian@0 1884 unsigned long len,
ian@0 1885 const char __user *uargs)
ian@0 1886 {
ian@0 1887 struct module *mod;
ian@0 1888 int ret = 0;
ian@0 1889
ian@0 1890 /* Must have permission */
ian@0 1891 if (!capable(CAP_SYS_MODULE))
ian@0 1892 return -EPERM;
ian@0 1893
ian@0 1894 /* Only one module load at a time, please */
ian@0 1895 if (mutex_lock_interruptible(&module_mutex) != 0)
ian@0 1896 return -EINTR;
ian@0 1897
ian@0 1898 /* Do all the hard work */
ian@0 1899 mod = load_module(umod, len, uargs);
ian@0 1900 if (IS_ERR(mod)) {
ian@0 1901 mutex_unlock(&module_mutex);
ian@0 1902 return PTR_ERR(mod);
ian@0 1903 }
ian@0 1904
ian@0 1905 /* Now sew it into the lists. They won't access us, since
ian@0 1906 strong_try_module_get() will fail. */
ian@0 1907 stop_machine_run(__link_module, mod, NR_CPUS);
ian@0 1908
ian@0 1909 /* Drop lock so they can recurse */
ian@0 1910 mutex_unlock(&module_mutex);
ian@0 1911
ian@0 1912 blocking_notifier_call_chain(&module_notify_list,
ian@0 1913 MODULE_STATE_COMING, mod);
ian@0 1914
ian@0 1915 /* Start the module */
ian@0 1916 if (mod->init != NULL)
ian@0 1917 ret = mod->init();
ian@0 1918 if (ret < 0) {
ian@0 1919 /* Init routine failed: abort. Try to protect us from
ian@0 1920 buggy refcounters. */
ian@0 1921 mod->state = MODULE_STATE_GOING;
ian@0 1922 synchronize_sched();
ian@0 1923 if (mod->unsafe)
ian@0 1924 printk(KERN_ERR "%s: module is now stuck!\n",
ian@0 1925 mod->name);
ian@0 1926 else {
ian@0 1927 module_put(mod);
ian@0 1928 mutex_lock(&module_mutex);
ian@0 1929 free_module(mod);
ian@0 1930 mutex_unlock(&module_mutex);
ian@0 1931 }
ian@0 1932 return ret;
ian@0 1933 }
ian@0 1934
ian@0 1935 /* Now it's a first class citizen! */
ian@0 1936 mutex_lock(&module_mutex);
ian@0 1937 mod->state = MODULE_STATE_LIVE;
ian@0 1938 /* Drop initial reference. */
ian@0 1939 module_put(mod);
ian@0 1940 unwind_remove_table(mod->unwind_info, 1);
ian@0 1941 module_free(mod, mod->module_init);
ian@0 1942 mod->module_init = NULL;
ian@0 1943 mod->init_size = 0;
ian@0 1944 mod->init_text_size = 0;
ian@0 1945 mutex_unlock(&module_mutex);
ian@0 1946
ian@0 1947 return 0;
ian@0 1948 }
ian@0 1949
ian@0 1950 static inline int within(unsigned long addr, void *start, unsigned long size)
ian@0 1951 {
ian@0 1952 return ((void *)addr >= start && (void *)addr < start + size);
ian@0 1953 }
ian@0 1954
ian@0 1955 #ifdef CONFIG_KALLSYMS
ian@0 1956 /*
ian@0 1957 * This ignores the intensely annoying "mapping symbols" found
ian@0 1958 * in ARM ELF files: $a, $t and $d.
ian@0 1959 */
ian@0 1960 static inline int is_arm_mapping_symbol(const char *str)
ian@0 1961 {
ian@0 1962 return str[0] == '$' && strchr("atd", str[1])
ian@0 1963 && (str[2] == '\0' || str[2] == '.');
ian@0 1964 }
ian@0 1965
ian@0 1966 static const char *get_ksymbol(struct module *mod,
ian@0 1967 unsigned long addr,
ian@0 1968 unsigned long *size,
ian@0 1969 unsigned long *offset)
ian@0 1970 {
ian@0 1971 unsigned int i, best = 0;
ian@0 1972 unsigned long nextval;
ian@0 1973
ian@0 1974 /* At worse, next value is at end of module */
ian@0 1975 if (within(addr, mod->module_init, mod->init_size))
ian@0 1976 nextval = (unsigned long)mod->module_init+mod->init_text_size;
ian@0 1977 else
ian@0 1978 nextval = (unsigned long)mod->module_core+mod->core_text_size;
ian@0 1979
ian@0 1980 /* Scan for closest preceeding symbol, and next symbol. (ELF
ian@0 1981 starts real symbols at 1). */
ian@0 1982 for (i = 1; i < mod->num_symtab; i++) {
ian@0 1983 if (mod->symtab[i].st_shndx == SHN_UNDEF)
ian@0 1984 continue;
ian@0 1985
ian@0 1986 /* We ignore unnamed symbols: they're uninformative
ian@0 1987 * and inserted at a whim. */
ian@0 1988 if (mod->symtab[i].st_value <= addr
ian@0 1989 && mod->symtab[i].st_value > mod->symtab[best].st_value
ian@0 1990 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
ian@0 1991 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
ian@0 1992 best = i;
ian@0 1993 if (mod->symtab[i].st_value > addr
ian@0 1994 && mod->symtab[i].st_value < nextval
ian@0 1995 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
ian@0 1996 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
ian@0 1997 nextval = mod->symtab[i].st_value;
ian@0 1998 }
ian@0 1999
ian@0 2000 if (!best)
ian@0 2001 return NULL;
ian@0 2002
ian@0 2003 *size = nextval - mod->symtab[best].st_value;
ian@0 2004 *offset = addr - mod->symtab[best].st_value;
ian@0 2005 return mod->strtab + mod->symtab[best].st_name;
ian@0 2006 }
ian@0 2007
ian@0 2008 /* For kallsyms to ask for address resolution. NULL means not found.
ian@0 2009 We don't lock, as this is used for oops resolution and races are a
ian@0 2010 lesser concern. */
ian@0 2011 const char *module_address_lookup(unsigned long addr,
ian@0 2012 unsigned long *size,
ian@0 2013 unsigned long *offset,
ian@0 2014 char **modname)
ian@0 2015 {
ian@0 2016 struct module *mod;
ian@0 2017
ian@0 2018 list_for_each_entry(mod, &modules, list) {
ian@0 2019 if (within(addr, mod->module_init, mod->init_size)
ian@0 2020 || within(addr, mod->module_core, mod->core_size)) {
ian@0 2021 *modname = mod->name;
ian@0 2022 return get_ksymbol(mod, addr, size, offset);
ian@0 2023 }
ian@0 2024 }
ian@0 2025 return NULL;
ian@0 2026 }
ian@0 2027
ian@0 2028 struct module *module_get_kallsym(unsigned int symnum, unsigned long *value,
ian@0 2029 char *type, char *name, size_t namelen)
ian@0 2030 {
ian@0 2031 struct module *mod;
ian@0 2032
ian@0 2033 mutex_lock(&module_mutex);
ian@0 2034 list_for_each_entry(mod, &modules, list) {
ian@0 2035 if (symnum < mod->num_symtab) {
ian@0 2036 *value = mod->symtab[symnum].st_value;
ian@0 2037 *type = mod->symtab[symnum].st_info;
ian@0 2038 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
ian@0 2039 namelen);
ian@0 2040 mutex_unlock(&module_mutex);
ian@0 2041 return mod;
ian@0 2042 }
ian@0 2043 symnum -= mod->num_symtab;
ian@0 2044 }
ian@0 2045 mutex_unlock(&module_mutex);
ian@0 2046 return NULL;
ian@0 2047 }
ian@0 2048
ian@0 2049 static unsigned long mod_find_symname(struct module *mod, const char *name)
ian@0 2050 {
ian@0 2051 unsigned int i;
ian@0 2052
ian@0 2053 for (i = 0; i < mod->num_symtab; i++)
ian@0 2054 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
ian@0 2055 mod->symtab[i].st_info != 'U')
ian@0 2056 return mod->symtab[i].st_value;
ian@0 2057 return 0;
ian@0 2058 }
ian@0 2059
ian@0 2060 /* Look for this name: can be of form module:name. */
ian@0 2061 unsigned long module_kallsyms_lookup_name(const char *name)
ian@0 2062 {
ian@0 2063 struct module *mod;
ian@0 2064 char *colon;
ian@0 2065 unsigned long ret = 0;
ian@0 2066
ian@0 2067 /* Don't lock: we're in enough trouble already. */
ian@0 2068 if ((colon = strchr(name, ':')) != NULL) {
ian@0 2069 *colon = '\0';
ian@0 2070 if ((mod = find_module(name)) != NULL)
ian@0 2071 ret = mod_find_symname(mod, colon+1);
ian@0 2072 *colon = ':';
ian@0 2073 } else {
ian@0 2074 list_for_each_entry(mod, &modules, list)
ian@0 2075 if ((ret = mod_find_symname(mod, name)) != 0)
ian@0 2076 break;
ian@0 2077 }
ian@0 2078 return ret;
ian@0 2079 }
ian@0 2080 #endif /* CONFIG_KALLSYMS */
ian@0 2081
ian@0 2082 /* Called by the /proc file system to return a list of modules. */
ian@0 2083 static void *m_start(struct seq_file *m, loff_t *pos)
ian@0 2084 {
ian@0 2085 struct list_head *i;
ian@0 2086 loff_t n = 0;
ian@0 2087
ian@0 2088 mutex_lock(&module_mutex);
ian@0 2089 list_for_each(i, &modules) {
ian@0 2090 if (n++ == *pos)
ian@0 2091 break;
ian@0 2092 }
ian@0 2093 if (i == &modules)
ian@0 2094 return NULL;
ian@0 2095 return i;
ian@0 2096 }
ian@0 2097
ian@0 2098 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
ian@0 2099 {
ian@0 2100 struct list_head *i = p;
ian@0 2101 (*pos)++;
ian@0 2102 if (i->next == &modules)
ian@0 2103 return NULL;
ian@0 2104 return i->next;
ian@0 2105 }
ian@0 2106
ian@0 2107 static void m_stop(struct seq_file *m, void *p)
ian@0 2108 {
ian@0 2109 mutex_unlock(&module_mutex);
ian@0 2110 }
ian@0 2111
ian@0 2112 static int m_show(struct seq_file *m, void *p)
ian@0 2113 {
ian@0 2114 struct module *mod = list_entry(p, struct module, list);
ian@0 2115 seq_printf(m, "%s %lu",
ian@0 2116 mod->name, mod->init_size + mod->core_size);
ian@0 2117 print_unload_info(m, mod);
ian@0 2118
ian@0 2119 /* Informative for users. */
ian@0 2120 seq_printf(m, " %s",
ian@0 2121 mod->state == MODULE_STATE_GOING ? "Unloading":
ian@0 2122 mod->state == MODULE_STATE_COMING ? "Loading":
ian@0 2123 "Live");
ian@0 2124 /* Used by oprofile and other similar tools. */
ian@0 2125 seq_printf(m, " 0x%p", mod->module_core);
ian@0 2126
ian@0 2127 seq_printf(m, "\n");
ian@0 2128 return 0;
ian@0 2129 }
ian@0 2130
ian@0 2131 /* Format: modulename size refcount deps address
ian@0 2132
ian@0 2133 Where refcount is a number or -, and deps is a comma-separated list
ian@0 2134 of depends or -.
ian@0 2135 */
ian@0 2136 struct seq_operations modules_op = {
ian@0 2137 .start = m_start,
ian@0 2138 .next = m_next,
ian@0 2139 .stop = m_stop,
ian@0 2140 .show = m_show
ian@0 2141 };
ian@0 2142
ian@0 2143 /* Given an address, look for it in the module exception tables. */
ian@0 2144 const struct exception_table_entry *search_module_extables(unsigned long addr)
ian@0 2145 {
ian@0 2146 unsigned long flags;
ian@0 2147 const struct exception_table_entry *e = NULL;
ian@0 2148 struct module *mod;
ian@0 2149
ian@0 2150 spin_lock_irqsave(&modlist_lock, flags);
ian@0 2151 list_for_each_entry(mod, &modules, list) {
ian@0 2152 if (mod->num_exentries == 0)
ian@0 2153 continue;
ian@0 2154
ian@0 2155 e = search_extable(mod->extable,
ian@0 2156 mod->extable + mod->num_exentries - 1,
ian@0 2157 addr);
ian@0 2158 if (e)
ian@0 2159 break;
ian@0 2160 }
ian@0 2161 spin_unlock_irqrestore(&modlist_lock, flags);
ian@0 2162
ian@0 2163 /* Now, if we found one, we are running inside it now, hence
ian@0 2164 we cannot unload the module, hence no refcnt needed. */
ian@0 2165 return e;
ian@0 2166 }
ian@0 2167
ian@0 2168 /*
ian@0 2169 * Is this a valid module address?
ian@0 2170 */
ian@0 2171 int is_module_address(unsigned long addr)
ian@0 2172 {
ian@0 2173 unsigned long flags;
ian@0 2174 struct module *mod;
ian@0 2175
ian@0 2176 spin_lock_irqsave(&modlist_lock, flags);
ian@0 2177
ian@0 2178 list_for_each_entry(mod, &modules, list) {
ian@0 2179 if (within(addr, mod->module_core, mod->core_size)) {
ian@0 2180 spin_unlock_irqrestore(&modlist_lock, flags);
ian@0 2181 return 1;
ian@0 2182 }
ian@0 2183 }
ian@0 2184
ian@0 2185 spin_unlock_irqrestore(&modlist_lock, flags);
ian@0 2186
ian@0 2187 return 0;
ian@0 2188 }
ian@0 2189
ian@0 2190
ian@0 2191 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
ian@0 2192 struct module *__module_text_address(unsigned long addr)
ian@0 2193 {
ian@0 2194 struct module *mod;
ian@0 2195
ian@0 2196 list_for_each_entry(mod, &modules, list)
ian@0 2197 if (within(addr, mod->module_init, mod->init_text_size)
ian@0 2198 || within(addr, mod->module_core, mod->core_text_size))
ian@0 2199 return mod;
ian@0 2200 return NULL;
ian@0 2201 }
ian@0 2202
ian@0 2203 struct module *module_text_address(unsigned long addr)
ian@0 2204 {
ian@0 2205 struct module *mod;
ian@0 2206 unsigned long flags;
ian@0 2207
ian@0 2208 spin_lock_irqsave(&modlist_lock, flags);
ian@0 2209 mod = __module_text_address(addr);
ian@0 2210 spin_unlock_irqrestore(&modlist_lock, flags);
ian@0 2211
ian@0 2212 return mod;
ian@0 2213 }
ian@0 2214
ian@0 2215 /* Don't grab lock, we're oopsing. */
ian@0 2216 void print_modules(void)
ian@0 2217 {
ian@0 2218 struct module *mod;
ian@0 2219
ian@0 2220 printk("Modules linked in:");
ian@0 2221 list_for_each_entry(mod, &modules, list)
ian@0 2222 printk(" %s", mod->name);
ian@0 2223 printk("\n");
ian@0 2224 }
ian@0 2225
ian@0 2226 void module_add_driver(struct module *mod, struct device_driver *drv)
ian@0 2227 {
ian@0 2228 if (!mod || !drv)
ian@0 2229 return;
ian@0 2230
ian@0 2231 /* Don't check return code; this call is idempotent */
ian@0 2232 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
ian@0 2233 }
ian@0 2234 EXPORT_SYMBOL(module_add_driver);
ian@0 2235
ian@0 2236 void module_remove_driver(struct device_driver *drv)
ian@0 2237 {
ian@0 2238 if (!drv)
ian@0 2239 return;
ian@0 2240 sysfs_remove_link(&drv->kobj, "module");
ian@0 2241 }
ian@0 2242 EXPORT_SYMBOL(module_remove_driver);
ian@0 2243
ian@0 2244 #ifdef CONFIG_MODVERSIONS
ian@0 2245 /* Generate the signature for struct module here, too, for modversions. */
ian@0 2246 void struct_module(struct module *mod) { return; }
ian@0 2247 EXPORT_SYMBOL(struct_module);
ian@0 2248 #endif