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