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