module.c 47.7 KB
Newer Older
1
/* Rewritten by Rusty Russell, on the backs of many others...
2
   Copyright (C) 2002 Richard Henderson
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
   Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
Linus Torvalds's avatar
Linus Torvalds committed
19 20
#include <linux/config.h>
#include <linux/module.h>
21
#include <linux/moduleloader.h>
Linus Torvalds's avatar
Linus Torvalds committed
22 23
#include <linux/init.h>
#include <linux/slab.h>
24 25
#include <linux/vmalloc.h>
#include <linux/elf.h>
Linus Torvalds's avatar
Linus Torvalds committed
26
#include <linux/seq_file.h>
27 28 29
#include <linux/fcntl.h>
#include <linux/rcupdate.h>
#include <linux/cpu.h>
30
#include <linux/moduleparam.h>
31 32
#include <linux/errno.h>
#include <linux/err.h>
33
#include <linux/vermagic.h>
34
#include <linux/notifier.h>
35 36 37
#include <asm/uaccess.h>
#include <asm/semaphore.h>
#include <asm/pgalloc.h>
38
#include <asm/cacheflush.h>
Linus Torvalds's avatar
Linus Torvalds committed
39

40 41 42
#if 0
#define DEBUGP printk
#else
43
#define DEBUGP(fmt , a...)
44
#endif
Linus Torvalds's avatar
Linus Torvalds committed
45

46 47 48 49 50 51 52
#ifndef ARCH_SHF_SMALL
#define ARCH_SHF_SMALL 0
#endif

/* If this is set, the section belongs in the init part of the module */
#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))

Rusty Russell's avatar
Rusty Russell committed
53 54 55
#define symbol_is(literal, string)				\
	(strcmp(MODULE_SYMBOL_PREFIX literal, (string)) == 0)

Rusty Russell's avatar
Rusty Russell committed
56
/* Protects module list */
57 58
static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;

59
/* List of modules, protected by module_mutex AND modlist_lock */
60
static DECLARE_MUTEX(module_mutex);
61
static LIST_HEAD(modules);
Linus Torvalds's avatar
Linus Torvalds committed
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static DECLARE_MUTEX(notify_mutex);
static struct notifier_block * module_notify_list;

int register_module_notifier(struct notifier_block * nb)
{
	int err;
	down(&notify_mutex);
	err = notifier_chain_register(&module_notify_list, nb);
	up(&notify_mutex);
	return err;
}
EXPORT_SYMBOL(register_module_notifier);

int unregister_module_notifier(struct notifier_block * nb)
{
	int err;
	down(&notify_mutex);
	err = notifier_chain_unregister(&module_notify_list, nb);
	up(&notify_mutex);
	return err;
}
EXPORT_SYMBOL(unregister_module_notifier);

86 87 88 89 90 91 92 93
/* We require a truly strong try_module_get() */
static inline int strong_try_module_get(struct module *mod)
{
	if (mod && mod->state == MODULE_STATE_COMING)
		return 0;
	return try_module_get(mod);
}

94 95 96 97 98 99 100
/* Stub function for modules which don't have an initfn */
int init_module(void)
{
	return 0;
}
EXPORT_SYMBOL(init_module);

Rusty Russell's avatar
Rusty Russell committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/* Find a module section: 0 means not found. */
static unsigned int find_sec(Elf_Ehdr *hdr,
			     Elf_Shdr *sechdrs,
			     const char *secstrings,
			     const char *name)
{
	unsigned int i;

	for (i = 1; i < hdr->e_shnum; i++)
		/* Alloc bit cleared means "ignore it." */
		if ((sechdrs[i].sh_flags & SHF_ALLOC)
		    && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
			return i;
	return 0;
}

Rusty Russell's avatar
Rusty Russell committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/* Provided by the linker */
extern const struct kernel_symbol __start___ksymtab[];
extern const struct kernel_symbol __stop___ksymtab[];
extern const struct kernel_symbol __start___ksymtab_gpl[];
extern const struct kernel_symbol __stop___ksymtab_gpl[];
extern const unsigned long __start___kcrctab[];
extern const unsigned long __start___kcrctab_gpl[];

#ifndef CONFIG_MODVERSIONS
#define symversion(base, idx) NULL
#else
#define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
#endif

/* Find a symbol, return value, crc and module which owns it */
132
static unsigned long __find_symbol(const char *name,
Rusty Russell's avatar
Rusty Russell committed
133 134
				   struct module **owner,
				   const unsigned long **crc,
135
				   int gplok)
136
{
Rusty Russell's avatar
Rusty Russell committed
137 138
	struct module *mod;
	unsigned int i;
Linus Torvalds's avatar
Linus Torvalds committed
139

Rusty Russell's avatar
Rusty Russell committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	/* Core kernel first. */ 
	*owner = NULL;
	for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
		if (strcmp(__start___ksymtab[i].name, name) == 0) {
			*crc = symversion(__start___kcrctab, i);
			return __start___ksymtab[i].value;
		}
	}
	if (gplok) {
		for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
			if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
				*crc = symversion(__start___kcrctab_gpl, i);
				return __start___ksymtab_gpl[i].value;
			}
	}

	/* Now try modules. */ 
	list_for_each_entry(mod, &modules, list) {
		*owner = mod;
159
		for (i = 0; i < mod->num_syms; i++)
Rusty Russell's avatar
Rusty Russell committed
160 161 162 163 164 165 166 167
			if (strcmp(mod->syms[i].name, name) == 0) {
				*crc = symversion(mod->crcs, i);
				return mod->syms[i].value;
			}

		if (gplok) {
			for (i = 0; i < mod->num_gpl_syms; i++) {
				if (strcmp(mod->gpl_syms[i].name, name) == 0) {
David Mosberger's avatar
David Mosberger committed
168
					*crc = symversion(mod->gpl_crcs, i);
Rusty Russell's avatar
Rusty Russell committed
169 170
					return mod->gpl_syms[i].value;
				}
171
			}
Linus Torvalds's avatar
Linus Torvalds committed
172 173
		}
	}
174 175
	DEBUGP("Failed to find symbol %s\n", name);
 	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
176 177
}

178 179 180 181 182
/* Find a symbol in this elf symbol table */
static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
				       unsigned int symindex,
				       const char *strtab,
				       const char *name)
Linus Torvalds's avatar
Linus Torvalds committed
183
{
184
	unsigned int i;
185
	Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
186 187 188 189 190 191

	/* Search (defined) internal symbols first. */
	for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
		if (sym[i].st_shndx != SHN_UNDEF
		    && strcmp(name, strtab + sym[i].st_name) == 0)
			return sym[i].st_value;
Linus Torvalds's avatar
Linus Torvalds committed
192
	}
193 194 195 196 197 198 199 200 201 202 203
	return 0;
}

/* Search for module by name: must hold module_mutex. */
static struct module *find_module(const char *name)
{
	struct module *mod;

	list_for_each_entry(mod, &modules, list) {
		if (strcmp(mod->name, name) == 0)
			return mod;
Linus Torvalds's avatar
Linus Torvalds committed
204
	}
205
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
206 207
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
#ifdef CONFIG_SMP
/* Number of blocks used and allocated. */
static unsigned int pcpu_num_used, pcpu_num_allocated;
/* Size of each block.  -ve means used. */
static int *pcpu_size;

static int split_block(unsigned int i, unsigned short size)
{
	/* Reallocation required? */
	if (pcpu_num_used + 1 > pcpu_num_allocated) {
		int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
				   GFP_KERNEL);
		if (!new)
			return 0;

		memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
		pcpu_num_allocated *= 2;
		kfree(pcpu_size);
		pcpu_size = new;
	}

	/* Insert a new subblock */
	memmove(&pcpu_size[i+1], &pcpu_size[i],
		sizeof(pcpu_size[0]) * (pcpu_num_used - i));
	pcpu_num_used++;

	pcpu_size[i+1] -= size;
	pcpu_size[i] = size;
	return 1;
}

static inline unsigned int block_size(int val)
{
	if (val < 0)
		return -val;
	return val;
}

/* Created by linker magic */
extern char __per_cpu_start[], __per_cpu_end[];

static void *percpu_modalloc(unsigned long size, unsigned long align)
{
	unsigned long extra;
	unsigned int i;
	void *ptr;

	BUG_ON(align > SMP_CACHE_BYTES);

	ptr = __per_cpu_start;
	for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
		/* Extra for alignment requirement. */
		extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
		BUG_ON(i == 0 && extra != 0);

		if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
			continue;

		/* Transfer extra to previous block. */
		if (pcpu_size[i-1] < 0)
			pcpu_size[i-1] -= extra;
		else
			pcpu_size[i-1] += extra;
		pcpu_size[i] -= extra;
		ptr += extra;

		/* Split block if warranted */
		if (pcpu_size[i] - size > sizeof(unsigned long))
			if (!split_block(i, size))
				return NULL;

		/* Mark allocated */
		pcpu_size[i] = -pcpu_size[i];
		return ptr;
	}

	printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
	       size);
	return NULL;
}

static void percpu_modfree(void *freeme)
{
	unsigned int i;
	void *ptr = __per_cpu_start + block_size(pcpu_size[0]);

	/* First entry is core kernel percpu data. */
	for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
		if (ptr == freeme) {
			pcpu_size[i] = -pcpu_size[i];
			goto free;
		}
	}
	BUG();

 free:
	/* Merge with previous? */
	if (pcpu_size[i-1] >= 0) {
		pcpu_size[i-1] += pcpu_size[i];
		pcpu_num_used--;
		memmove(&pcpu_size[i], &pcpu_size[i+1],
			(pcpu_num_used - i) * sizeof(pcpu_size[0]));
		i--;
	}
	/* Merge with next? */
	if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
		pcpu_size[i] += pcpu_size[i+1];
		pcpu_num_used--;
		memmove(&pcpu_size[i+1], &pcpu_size[i+2],
			(pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
	}
}

static unsigned int find_pcpusec(Elf_Ehdr *hdr,
				 Elf_Shdr *sechdrs,
				 const char *secstrings)
{
	return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
}

static int percpu_modinit(void)
{
	pcpu_num_used = 2;
	pcpu_num_allocated = 2;
	pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
			    GFP_KERNEL);
	/* Static in-kernel percpu data (used). */
	pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
	/* Free room. */
	pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
	if (pcpu_size[1] < 0) {
		printk(KERN_ERR "No per-cpu room for modules.\n");
		pcpu_num_used = 1;
	}

	return 0;
}	
__initcall(percpu_modinit);
#else /* ... !CONFIG_SMP */
static inline void *percpu_modalloc(unsigned long size, unsigned long align)
{
	return NULL;
}
static inline void percpu_modfree(void *pcpuptr)
{
	BUG();
}
static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
					Elf_Shdr *sechdrs,
					const char *secstrings)
{
	return 0;
}
static inline void percpu_modcopy(void *pcpudst, const void *src,
				  unsigned long size)
{
	/* pcpusec should be 0, and size of that section should be 0. */
	BUG_ON(size != 0);
}
#endif /* CONFIG_SMP */

369 370 371
#ifdef CONFIG_MODULE_UNLOAD
/* Init the unload section of the module. */
static void module_unload_init(struct module *mod)
Linus Torvalds's avatar
Linus Torvalds committed
372
{
373 374 375 376 377
	unsigned int i;

	INIT_LIST_HEAD(&mod->modules_which_use_me);
	for (i = 0; i < NR_CPUS; i++)
		atomic_set(&mod->ref[i].count, 0);
378 379
	/* Hold reference count during initialization. */
	atomic_set(&mod->ref[smp_processor_id()].count, 1);
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	/* Backwards compatibility macros put refcount during init. */
	mod->waiter = current;
}

/* modules using other modules */
struct module_use
{
	struct list_head list;
	struct module *module_which_uses;
};

/* Does a already use b? */
static int already_uses(struct module *a, struct module *b)
{
	struct module_use *use;

	list_for_each_entry(use, &b->modules_which_use_me, list) {
		if (use->module_which_uses == a) {
			DEBUGP("%s uses %s!\n", a->name, b->name);
			return 1;
Linus Torvalds's avatar
Linus Torvalds committed
400 401
		}
	}
402 403
	DEBUGP("%s does not use %s!\n", a->name, b->name);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
404 405
}

406 407
/* Module a uses b */
static int use_module(struct module *a, struct module *b)
Linus Torvalds's avatar
Linus Torvalds committed
408
{
409 410 411
	struct module_use *use;
	if (b == NULL || already_uses(a, b)) return 1;

Rusty Russell's avatar
Rusty Russell committed
412 413 414
	if (!strong_try_module_get(b))
		return 0;

415 416 417 418
	DEBUGP("Allocating new usage for %s.\n", a->name);
	use = kmalloc(sizeof(*use), GFP_ATOMIC);
	if (!use) {
		printk("%s: out of memory loading\n", a->name);
Rusty Russell's avatar
Rusty Russell committed
419
		module_put(b);
420
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
421
	}
422 423 424 425

	use->module_which_uses = a;
	list_add(&use->list, &b->modules_which_use_me);
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
426 427
}

428 429
/* Clear the unload stuff of the module. */
static void module_unload_free(struct module *mod)
Linus Torvalds's avatar
Linus Torvalds committed
430
{
431 432 433 434 435 436 437 438 439 440 441 442 443 444
	struct module *i;

	list_for_each_entry(i, &modules, list) {
		struct module_use *use;

		list_for_each_entry(use, &i->modules_which_use_me, list) {
			if (use->module_which_uses == mod) {
				DEBUGP("%s unusing %s\n", mod->name, i->name);
				module_put(i);
				list_del(&use->list);
				kfree(use);
				/* There can be at most one match. */
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
445 446 447 448
		}
	}
}

449 450 451 452 453 454 455 456
#ifdef CONFIG_SMP
/* Thread to stop each CPU in user context. */
enum stopref_state {
	STOPREF_WAIT,
	STOPREF_PREPARE,
	STOPREF_DISABLE_IRQ,
	STOPREF_EXIT,
};
Linus Torvalds's avatar
Linus Torvalds committed
457

458 459 460
static enum stopref_state stopref_state;
static unsigned int stopref_num_threads;
static atomic_t stopref_thread_ack;
Linus Torvalds's avatar
Linus Torvalds committed
461

462 463 464 465
static int stopref(void *cpu)
{
	int irqs_disabled = 0;
	int prepared = 0;
Linus Torvalds's avatar
Linus Torvalds committed
466

467
	sprintf(current->comm, "kmodule%lu\n", (unsigned long)cpu);
Linus Torvalds's avatar
Linus Torvalds committed
468

469 470 471 472 473
	/* Highest priority we can manage, and move to right CPU. */
#if 0 /* FIXME */
	struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
	setscheduler(current->pid, SCHED_FIFO, &param);
#endif
474
	set_cpus_allowed(current, 1UL << (unsigned long)cpu);
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496

	/* Ack: we are alive */
	atomic_inc(&stopref_thread_ack);

	/* Simple state machine */
	while (stopref_state != STOPREF_EXIT) {
		if (stopref_state == STOPREF_DISABLE_IRQ && !irqs_disabled) {
			local_irq_disable();
			irqs_disabled = 1;
			/* Ack: irqs disabled. */
			atomic_inc(&stopref_thread_ack);
		} else if (stopref_state == STOPREF_PREPARE && !prepared) {
			/* Everyone is in place, hold CPU. */
			preempt_disable();
			prepared = 1;
			atomic_inc(&stopref_thread_ack);
		}
		if (irqs_disabled || prepared)
			cpu_relax();
		else
			yield();
	}
Linus Torvalds's avatar
Linus Torvalds committed
497

498 499
	/* Ack: we are exiting. */
	atomic_inc(&stopref_thread_ack);
Linus Torvalds's avatar
Linus Torvalds committed
500

501 502 503 504
	if (irqs_disabled)
		local_irq_enable();
	if (prepared)
		preempt_enable();
Linus Torvalds's avatar
Linus Torvalds committed
505

506 507
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
508

509 510
/* Change the thread state */
static void stopref_set_state(enum stopref_state state, int sleep)
Linus Torvalds's avatar
Linus Torvalds committed
511
{
512 513 514 515 516 517 518 519 520
	atomic_set(&stopref_thread_ack, 0);
	wmb();
	stopref_state = state;
	while (atomic_read(&stopref_thread_ack) != stopref_num_threads) {
		if (sleep)
			yield();
		else
			cpu_relax();
	}
Linus Torvalds's avatar
Linus Torvalds committed
521 522
}

523 524
/* Stop the machine.  Disables irqs. */
static int stop_refcounts(void)
Linus Torvalds's avatar
Linus Torvalds committed
525
{
526 527 528
	unsigned int i, cpu;
	unsigned long old_allowed;
	int ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
529

530 531
	/* One thread per cpu.  We'll do our own. */
	cpu = smp_processor_id();
Linus Torvalds's avatar
Linus Torvalds committed
532

533 534
	/* FIXME: racy with set_cpus_allowed. */
	old_allowed = current->cpus_allowed;
535
	set_cpus_allowed(current, 1UL << (unsigned long)cpu);
Linus Torvalds's avatar
Linus Torvalds committed
536

537 538 539 540 541 542 543 544 545 546
	atomic_set(&stopref_thread_ack, 0);
	stopref_num_threads = 0;
	stopref_state = STOPREF_WAIT;

	/* No CPUs can come up or down during this. */
	down(&cpucontrol);

	for (i = 0; i < NR_CPUS; i++) {
		if (i == cpu || !cpu_online(i))
			continue;
547
		ret = kernel_thread(stopref, (void *)(long)i, CLONE_KERNEL);
548 549 550
		if (ret < 0)
			break;
		stopref_num_threads++;
Linus Torvalds's avatar
Linus Torvalds committed
551
	}
552 553 554 555 556 557 558 559 560 561

	/* Wait for them all to come to life. */
	while (atomic_read(&stopref_thread_ack) != stopref_num_threads)
		yield();

	/* If some failed, kill them all. */
	if (ret < 0) {
		stopref_set_state(STOPREF_EXIT, 1);
		up(&cpucontrol);
		return ret;
Linus Torvalds's avatar
Linus Torvalds committed
562 563
	}

564 565
	/* Don't schedule us away at this point, please. */
	preempt_disable();
Linus Torvalds's avatar
Linus Torvalds committed
566

567 568
	/* Now they are all scheduled, make them hold the CPUs, ready. */
	stopref_set_state(STOPREF_PREPARE, 0);
Linus Torvalds's avatar
Linus Torvalds committed
569

570 571
	/* Make them disable irqs. */
	stopref_set_state(STOPREF_DISABLE_IRQ, 0);
Linus Torvalds's avatar
Linus Torvalds committed
572

573 574
	local_irq_disable();
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
575 576
}

577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
/* Restart the machine.  Re-enables irqs. */
static void restart_refcounts(void)
{
	stopref_set_state(STOPREF_EXIT, 0);
	local_irq_enable();
	preempt_enable();
	up(&cpucontrol);
}
#else /* ...!SMP */
static inline int stop_refcounts(void)
{
	local_irq_disable();
	return 0;
}
static inline void restart_refcounts(void)
{
	local_irq_enable();
}
#endif

Rusty Russell's avatar
Rusty Russell committed
597
unsigned int module_refcount(struct module *mod)
598 599 600 601 602 603 604
{
	unsigned int i, total = 0;

	for (i = 0; i < NR_CPUS; i++)
		total += atomic_read(&mod->ref[i].count);
	return total;
}
Rusty Russell's avatar
Rusty Russell committed
605
EXPORT_SYMBOL(module_refcount);
606 607 608

/* This exists whether we can unload or not */
static void free_module(struct module *mod);
Linus Torvalds's avatar
Linus Torvalds committed
609

Rusty Russell's avatar
Rusty Russell committed
610 611 612 613 614 615 616 617 618 619 620 621
#ifdef CONFIG_MODULE_FORCE_UNLOAD
static inline int try_force(unsigned int flags)
{
	return (flags & O_TRUNC);
}
#else
static inline int try_force(unsigned int flags)
{
	return 0;
}
#endif /* CONFIG_MODULE_FORCE_UNLOAD */

622 623 624 625 626 627
/* Stub function for modules which don't have an exitfn */
void cleanup_module(void)
{
}
EXPORT_SYMBOL(cleanup_module);

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
static void wait_for_zero_refcount(struct module *mod)
{
	/* Since we might sleep for some time, drop the semaphore first */
	up(&module_mutex);
	for (;;) {
		DEBUGP("Looking at refcount...\n");
		set_current_state(TASK_UNINTERRUPTIBLE);
		if (module_refcount(mod) == 0)
			break;
		schedule();
	}
	current->state = TASK_RUNNING;
	down(&module_mutex);
}

Linus Torvalds's avatar
Linus Torvalds committed
643
asmlinkage long
644
sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds's avatar
Linus Torvalds committed
645
{
646 647
	struct module *mod;
	char name[MODULE_NAME_LEN];
Rusty Russell's avatar
Rusty Russell committed
648
	int ret, forced = 0;
Linus Torvalds's avatar
Linus Torvalds committed
649 650 651 652

	if (!capable(CAP_SYS_MODULE))
		return -EPERM;

653 654 655
	if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
		return -EFAULT;
	name[MODULE_NAME_LEN-1] = '\0';
Linus Torvalds's avatar
Linus Torvalds committed
656

657 658
	if (down_interruptible(&module_mutex) != 0)
		return -EINTR;
Linus Torvalds's avatar
Linus Torvalds committed
659

660 661 662 663
	mod = find_module(name);
	if (!mod) {
		ret = -ENOENT;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
664 665
	}

Rusty Russell's avatar
Rusty Russell committed
666 667 668 669 670 671
	if (!list_empty(&mod->modules_which_use_me)) {
		/* Other modules depend on us: get rid of them first. */
		ret = -EWOULDBLOCK;
		goto out;
	}

672
	/* Already dying? */
673
	if (mod->state == MODULE_STATE_GOING) {
Rusty Russell's avatar
Rusty Russell committed
674 675
		/* FIXME: if (force), slam module count and wake up
                   waiter --RR */
676 677 678
		DEBUGP("%s already dying\n", mod->name);
		ret = -EBUSY;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
679 680
	}

681
	/* If it has an init func, it must have an exit func to unload */
682 683
	if ((mod->init != init_module && mod->exit == cleanup_module)
	    || mod->unsafe) {
Rusty Russell's avatar
Rusty Russell committed
684 685 686 687 688 689
		forced = try_force(flags);
		if (!forced) {
			/* This module can't be removed */
			ret = -EBUSY;
			goto out;
		}
Linus Torvalds's avatar
Linus Torvalds committed
690
	}
691 692 693 694 695
	/* Stop the machine so refcounts can't move: irqs disabled. */
	DEBUGP("Stopping refcounts...\n");
	ret = stop_refcounts();
	if (ret != 0)
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
696

697
	/* If it's not unused, quit unless we are told to block. */
Rusty Russell's avatar
Rusty Russell committed
698 699
	if ((flags & O_NONBLOCK) && module_refcount(mod) != 0) {
		forced = try_force(flags);
700
		if (!forced) {
Rusty Russell's avatar
Rusty Russell committed
701
			ret = -EWOULDBLOCK;
702 703 704
			restart_refcounts();
			goto out;
		}
Linus Torvalds's avatar
Linus Torvalds committed
705 706
	}

707 708 709 710
	/* Mark it as dying. */
	mod->waiter = current;
	mod->state = MODULE_STATE_GOING;
	restart_refcounts();
Linus Torvalds's avatar
Linus Torvalds committed
711

712 713 714
	/* Never wait if forced. */
	if (!forced && module_refcount(mod) != 0)
		wait_for_zero_refcount(mod);
Linus Torvalds's avatar
Linus Torvalds committed
715

716
	/* Final destruction now noone is using it. */
717
	mod->exit();
718
	free_module(mod);
Linus Torvalds's avatar
Linus Torvalds committed
719

720 721 722 723
 out:
	up(&module_mutex);
	return ret;
}
Linus Torvalds's avatar
Linus Torvalds committed
724

725 726 727
static void print_unload_info(struct seq_file *m, struct module *mod)
{
	struct module_use *use;
Rusty Russell's avatar
Rusty Russell committed
728
	int printed_something = 0;
Linus Torvalds's avatar
Linus Torvalds committed
729

Rusty Russell's avatar
Rusty Russell committed
730
	seq_printf(m, " %u ", module_refcount(mod));
Linus Torvalds's avatar
Linus Torvalds committed
731

Rusty Russell's avatar
Rusty Russell committed
732 733 734 735 736 737
	/* Always include a trailing , so userspace can differentiate
           between this and the old multi-field proc format. */
	list_for_each_entry(use, &mod->modules_which_use_me, list) {
		printed_something = 1;
		seq_printf(m, "%s,", use->module_which_uses->name);
	}
Linus Torvalds's avatar
Linus Torvalds committed
738

Rusty Russell's avatar
Rusty Russell committed
739 740 741 742
	if (mod->unsafe) {
		printed_something = 1;
		seq_printf(m, "[unsafe],");
	}
Linus Torvalds's avatar
Linus Torvalds committed
743

Rusty Russell's avatar
Rusty Russell committed
744 745 746 747
	if (mod->init != init_module && mod->exit == cleanup_module) {
		printed_something = 1;
		seq_printf(m, "[permanent],");
	}
Linus Torvalds's avatar
Linus Torvalds committed
748

Rusty Russell's avatar
Rusty Russell committed
749 750
	if (!printed_something)
		seq_printf(m, "-");
Linus Torvalds's avatar
Linus Torvalds committed
751 752
}

753
void __symbol_put(const char *symbol)
Linus Torvalds's avatar
Linus Torvalds committed
754
{
Rusty Russell's avatar
Rusty Russell committed
755
	struct module *owner;
756
	unsigned long flags;
Rusty Russell's avatar
Rusty Russell committed
757
	const unsigned long *crc;
758 759

	spin_lock_irqsave(&modlist_lock, flags);
Rusty Russell's avatar
Rusty Russell committed
760
	if (!__find_symbol(symbol, &owner, &crc, 1))
761
		BUG();
Rusty Russell's avatar
Rusty Russell committed
762
	module_put(owner);
763
	spin_unlock_irqrestore(&modlist_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
764
}
765
EXPORT_SYMBOL(__symbol_put);
Linus Torvalds's avatar
Linus Torvalds committed
766

767 768 769 770 771
void symbol_put_addr(void *addr)
{
	unsigned long flags;

	spin_lock_irqsave(&modlist_lock, flags);
772 773
	if (!kernel_text_address((unsigned long)addr))
		BUG();
774

775
	module_put(module_text_address((unsigned long)addr));
776 777 778 779
	spin_unlock_irqrestore(&modlist_lock, flags);
}
EXPORT_SYMBOL_GPL(symbol_put_addr);

780 781
#else /* !CONFIG_MODULE_UNLOAD */
static void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds's avatar
Linus Torvalds committed
782
{
Rusty Russell's avatar
Rusty Russell committed
783 784
	/* We don't know the usage count, or what modules are using. */
	seq_printf(m, " - -");
Linus Torvalds's avatar
Linus Torvalds committed
785 786
}

787
static inline void module_unload_free(struct module *mod)
Linus Torvalds's avatar
Linus Torvalds committed
788 789 790
{
}

791
static inline int use_module(struct module *a, struct module *b)
Linus Torvalds's avatar
Linus Torvalds committed
792
{
793
	return strong_try_module_get(b);
794
}
Linus Torvalds's avatar
Linus Torvalds committed
795

796 797
static inline void module_unload_init(struct module *mod)
{
Linus Torvalds's avatar
Linus Torvalds committed
798 799
}

800 801
asmlinkage long
sys_delete_module(const char *name_user, unsigned int flags)
Linus Torvalds's avatar
Linus Torvalds committed
802
{
803 804
	return -ENOSYS;
}
Linus Torvalds's avatar
Linus Torvalds committed
805

806
#endif /* CONFIG_MODULE_UNLOAD */
Linus Torvalds's avatar
Linus Torvalds committed
807

808 809 810 811 812 813 814 815 816 817 818 819 820 821
#ifdef CONFIG_OBSOLETE_MODPARM
static int param_set_byte(const char *val, struct kernel_param *kp)  
{
	char *endp;
	long l;

	if (!val) return -EINVAL;
	l = simple_strtol(val, &endp, 0);
	if (endp == val || *endp || ((char)l != l))
		return -EINVAL;
	*((char *)kp->arg) = l;
	return 0;
}

822 823 824 825 826 827 828
/* Bounds checking done below */
static int obsparm_copy_string(const char *val, struct kernel_param *kp)
{
	strcpy(kp->arg, val);
	return 0;
}

829
int set_obsolete(const char *val, struct kernel_param *kp)
830 831
{
	unsigned int min, max;
832 833 834
	unsigned int size, maxsize;
	char *endp;
	const char *p;
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
	struct obsolete_modparm *obsparm = kp->arg;

	if (!val) {
		printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
		return -EINVAL;
	}

	/* type is: [min[-max]]{b,h,i,l,s} */
	p = obsparm->type;
	min = simple_strtol(p, &endp, 10);
	if (endp == obsparm->type)
		min = max = 1;
	else if (*endp == '-') {
		p = endp+1;
		max = simple_strtol(p, &endp, 10);
	} else
		max = min;
	switch (*endp) {
	case 'b':
		return param_array(kp->name, val, min, max, obsparm->addr,
				   1, param_set_byte);
	case 'h':
		return param_array(kp->name, val, min, max, obsparm->addr,
				   sizeof(short), param_set_short);
	case 'i':
		return param_array(kp->name, val, min, max, obsparm->addr,
				   sizeof(int), param_set_int);
	case 'l':
		return param_array(kp->name, val, min, max, obsparm->addr,
				   sizeof(long), param_set_long);
	case 's':
866 867
		return param_array(kp->name, val, min, max, obsparm->addr,
				   sizeof(char *), param_set_charp);
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884

	case 'c':
		/* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
		   and the decl is "char xxx[5][50];" */
		p = endp+1;
		maxsize = simple_strtol(p, &endp, 10);
		/* We check lengths here (yes, this is a hack). */
		p = val;
		while (p[size = strcspn(p, ",")]) {
			if (size >= maxsize) 
				goto oversize;
			p += size+1;
		}
		if (size >= maxsize) 
			goto oversize;
		return param_array(kp->name, val, min, max, obsparm->addr,
				   maxsize, obsparm_copy_string);
885 886 887
	}
	printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
	return -EINVAL;
888 889 890 891
 oversize:
	printk(KERN_ERR
	       "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
	return -EINVAL;
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
}

static int obsolete_params(const char *name,
			   char *args,
			   struct obsolete_modparm obsparm[],
			   unsigned int num,
			   Elf_Shdr *sechdrs,
			   unsigned int symindex,
			   const char *strtab)
{
	struct kernel_param *kp;
	unsigned int i;
	int ret;

	kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
	if (!kp)
		return -ENOMEM;

	for (i = 0; i < num; i++) {
911 912 913 914 915
		char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];

		snprintf(sym_name, sizeof(sym_name), "%s%s",
			 MODULE_SYMBOL_PREFIX, obsparm[i].name);

916 917 918 919 920 921
		kp[i].name = obsparm[i].name;
		kp[i].perm = 000;
		kp[i].set = set_obsolete;
		kp[i].get = NULL;
		obsparm[i].addr
			= (void *)find_local_symbol(sechdrs, symindex, strtab,
922
						    sym_name);
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
		if (!obsparm[i].addr) {
			printk("%s: falsely claims to have parameter %s\n",
			       name, obsparm[i].name);
			ret = -EINVAL;
			goto out;
		}
		kp[i].arg = &obsparm[i];
	}

	ret = parse_args(name, args, kp, num, NULL);
 out:
	kfree(kp);
	return ret;
}
#else
static int obsolete_params(const char *name,
			   char *args,
			   struct obsolete_modparm obsparm[],
			   unsigned int num,
			   Elf_Shdr *sechdrs,
			   unsigned int symindex,
			   const char *strtab)
{
	if (num != 0)
		printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
		       name);
	return 0;
}
#endif /* CONFIG_OBSOLETE_MODPARM */

953 954
static const char vermagic[] = VERMAGIC_STRING;

955
#ifdef CONFIG_MODVERSIONS
956 957 958 959
static int check_version(Elf_Shdr *sechdrs,
			 unsigned int versindex,
			 const char *symname,
			 struct module *mod, 
Rusty Russell's avatar
Rusty Russell committed
960
			 const unsigned long *crc)
961 962 963 964
{
	unsigned int i, num_versions;
	struct modversion_info *versions;

Rusty Russell's avatar
Rusty Russell committed
965
	/* Exporting module didn't supply crcs?  OK, we're already tainted. */
Rusty Russell's avatar
Rusty Russell committed
966
	if (!crc)
Rusty Russell's avatar
Rusty Russell committed
967
		return 1;
968 969 970 971 972 973 974 975 976

	versions = (void *) sechdrs[versindex].sh_addr;
	num_versions = sechdrs[versindex].sh_size
		/ sizeof(struct modversion_info);

	for (i = 0; i < num_versions; i++) {
		if (strcmp(versions[i].name, symname) != 0)
			continue;

Rusty Russell's avatar
Rusty Russell committed
977
		if (versions[i].crc == *crc)
978 979 980 981
			return 1;
		printk("%s: disagrees about version of symbol %s\n",
		       mod->name, symname);
		DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russell's avatar
Rusty Russell committed
982
		       *crc, versions[i].crc);
983 984 985
		return 0;
	}
	/* Not in module's version table.  OK, but that taints the kernel. */
Rusty Russell's avatar
Rusty Russell committed
986 987 988 989 990
	if (!(tainted & TAINT_FORCED_MODULE)) {
		printk("%s: no version for \"%s\" found: kernel tainted.\n",
		       mod->name, symname);
		tainted |= TAINT_FORCED_MODULE;
	}
991 992
	return 1;
}
993

994 995 996 997
static inline int check_modstruct_version(Elf_Shdr *sechdrs,
					  unsigned int versindex,
					  struct module *mod)
{
998 999
	const unsigned long *crc;
	struct module *owner;
1000

1001
	if (!__find_symbol("struct_module", &owner, &crc, 1))
1002
		BUG();
1003 1004
	return check_version(sechdrs, versindex, "struct_module", mod,
			     crc);
1005 1006
}

1007 1008 1009 1010 1011 1012 1013
/* First part is kernel version, which we ignore. */
static inline int same_magic(const char *amagic, const char *bmagic)
{
	amagic += strcspn(amagic, " ");
	bmagic += strcspn(bmagic, " ");
	return strcmp(amagic, bmagic) == 0;
}
1014 1015 1016 1017 1018
#else
static inline int check_version(Elf_Shdr *sechdrs,
				unsigned int versindex,
				const char *symname,
				struct module *mod, 
Rusty Russell's avatar
Rusty Russell committed
1019
				const unsigned long *crc)
1020 1021 1022
{
	return 1;
}
1023

1024 1025 1026 1027 1028 1029 1030
static inline int check_modstruct_version(Elf_Shdr *sechdrs,
					  unsigned int versindex,
					  struct module *mod)
{
	return 1;
}

1031 1032 1033 1034
static inline int same_magic(const char *amagic, const char *bmagic)
{
	return strcmp(amagic, bmagic) == 0;
}
1035
#endif /* CONFIG_MODVERSIONS */
1036

1037 1038 1039
/* Resolve a symbol for this module.  I.e. if we find one, record usage.
   Must be holding module_mutex. */
static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1040
				    unsigned int versindex,
1041 1042
				    const char *name,
				    struct module *mod)
1043
{
Rusty Russell's avatar
Rusty Russell committed
1044
	struct module *owner;
1045
	unsigned long ret;
Rusty Russell's avatar
Rusty Russell committed
1046
	const unsigned long *crc;
Linus Torvalds's avatar
Linus Torvalds committed
1047

1048
	spin_lock_irq(&modlist_lock);
Rusty Russell's avatar
Rusty Russell committed
1049
	ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
1050
	if (ret) {
1051
		/* use_module can fail due to OOM, or module unloading */
Rusty Russell's avatar
Rusty Russell committed
1052 1053
		if (!check_version(sechdrs, versindex, name, mod, crc) ||
		    !use_module(mod, owner))
1054 1055 1056 1057
			ret = 0;
	}
	spin_unlock_irq(&modlist_lock);
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1058 1059
}

1060 1061
/* Free a module, remove from lists, etc (must hold module mutex). */
static void free_module(struct module *mod)
Linus Torvalds's avatar
Linus Torvalds committed
1062
{
1063 1064
	/* Delete from various lists */
	spin_lock_irq(&modlist_lock);
1065
	list_del(&mod->list);
1066 1067
	spin_unlock_irq(&modlist_lock);

1068 1069 1070
	/* Arch-specific cleanup. */
	module_arch_cleanup(mod);

1071 1072 1073
	/* Module unload stuff */
	module_unload_free(mod);

1074 1075 1076
	/* This may be NULL, but that's OK */
	module_free(mod, mod->module_init);
	kfree(mod->args);
1077 1078
	if (mod->percpu)
		percpu_modfree(mod->percpu);
1079 1080 1081

	/* Finally, free the core (containing the module structure) */
	module_free(mod, mod->module_core);
1082
}
Linus Torvalds's avatar
Linus Torvalds committed
1083

1084 1085
void *__symbol_get(const char *symbol)
{
Rusty Russell's avatar
Rusty Russell committed
1086
	struct module *owner;
1087
	unsigned long value, flags;
Rusty Russell's avatar
Rusty Russell committed
1088
	const unsigned long *crc;
Linus Torvalds's avatar
Linus Torvalds committed
1089

1090
	spin_lock_irqsave(&modlist_lock, flags);
Rusty Russell's avatar
Rusty Russell committed
1091 1092
	value = __find_symbol(symbol, &owner, &crc, 1);
	if (value && !strong_try_module_get(owner))
1093 1094
		value = 0;
	spin_unlock_irqrestore(&modlist_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1095

1096 1097 1098
	return (void *)value;
}
EXPORT_SYMBOL_GPL(__symbol_get);
Linus Torvalds's avatar
Linus Torvalds committed
1099

1100
/* Change all symbols so that sh_value encodes the pointer directly. */
1101 1102
static int simplify_symbols(Elf_Shdr *sechdrs,
			    unsigned int symindex,
1103
			    const char *strtab,
1104
			    unsigned int versindex,
1105
			    unsigned int pcpuindex,
1106
			    struct module *mod)
1107
{
1108
	Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1109
	unsigned long secbase;
1110 1111 1112 1113
	unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
	int ret = 0;

	for (i = 1; i < n; i++) {
1114 1115
		switch (sym[i].st_shndx) {
		case SHN_COMMON:
1116 1117 1118
			/* We compiled with -fno-common.  These are not
			   supposed to happen.  */
			DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1119 1120
			ret = -ENOEXEC;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1121

1122 1123 1124 1125 1126
		case SHN_ABS:
			/* Don't need to do anything */
			DEBUGP("Absolute symbol: 0x%08lx\n",
			       (long)sym[i].st_value);
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1127

1128
		case SHN_UNDEF:
1129
			sym[i].st_value
1130
			  = resolve_symbol(sechdrs, versindex,
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
					   strtab + sym[i].st_name, mod);

			/* Ok if resolved.  */
			if (sym[i].st_value != 0)
				break;
			/* Ok if weak.  */
			if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
				break;

			printk(KERN_WARNING "%s: Unknown symbol %s\n",
			       mod->name, strtab + sym[i].st_name);
			ret = -ENOENT;
1143
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1144

1145
		default:
1146 1147 1148 1149 1150 1151
			/* Divert to percpu allocation if a percpu var. */
			if (sym[i].st_shndx == pcpuindex)
				secbase = (unsigned long)mod->percpu;
			else
				secbase = sechdrs[sym[i].st_shndx].sh_addr;
			sym[i].st_value += secbase;
1152
			break;
1153 1154
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1155

1156
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1157 1158
}

1159 1160
/* Update size with this section: return offset. */
static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
Linus Torvalds's avatar
Linus Torvalds committed
1161
{
1162
	long ret;
1163

1164 1165 1166 1167
	ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
	*size = ret + sechdr->sh_size;
	return ret;
}
1168

1169 1170
/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
   might -- code, read-only data, read-write data, small data.  Tally
1171
   sizes, and place the offsets into sh_entsize fields: high bit means it
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
   belongs in init. */
static void layout_sections(struct module *mod,
			    const Elf_Ehdr *hdr,
			    Elf_Shdr *sechdrs,
			    const char *secstrings)
{
	static unsigned long const masks[][2] = {
		{ SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
		{ SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
		{ SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
		{ ARCH_SHF_SMALL | SHF_ALLOC, 0 }
	};
	unsigned int m, i;

	for (i = 0; i < hdr->e_shnum; i++)
1187
		sechdrs[i].sh_entsize = ~0UL;
1188 1189 1190 1191 1192 1193 1194 1195

	DEBUGP("Core section allocation order:\n");
	for (m = 0; m < ARRAY_SIZE(masks); ++m) {
		for (i = 0; i < hdr->e_shnum; ++i) {
			Elf_Shdr *s = &sechdrs[i];

			if ((s->sh_flags & masks[m][0]) != masks[m][0]
			    || (s->sh_flags & masks[m][1])
1196
			    || s->sh_entsize != ~0UL
1197 1198
			    || strstr(secstrings + s->sh_name, ".init"))
				continue;
1199
			s->sh_entsize = get_offset(&mod->core_size, s);
1200
			DEBUGP("\t%s\n", secstrings + s->sh_name);
1201
		}
Linus Torvalds's avatar
Linus Torvalds committed
1202 1203
	}

1204 1205 1206 1207 1208 1209 1210
	DEBUGP("Init section allocation order:\n");
	for (m = 0; m < ARRAY_SIZE(masks); ++m) {
		for (i = 0; i < hdr->e_shnum; ++i) {
			Elf_Shdr *s = &sechdrs[i];

			if ((s->sh_flags & masks[m][0]) != masks[m][0]
			    || (s->sh_flags & masks[m][1])
1211
			    || s->sh_entsize != ~0UL
1212 1213
			    || !strstr(secstrings + s->sh_name, ".init"))
				continue;
1214 1215
			s->sh_entsize = (get_offset(&mod->init_size, s)
					 | INIT_OFFSET_MASK);
1216
			DEBUGP("\t%s\n", secstrings + s->sh_name);
1217 1218
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1219 1220
}

1221 1222 1223 1224 1225 1226 1227 1228 1229
static inline int license_is_gpl_compatible(const char *license)
{
	return (strcmp(license, "GPL") == 0
		|| strcmp(license, "GPL v2") == 0
		|| strcmp(license, "GPL and additional rights") == 0
		|| strcmp(license, "Dual BSD/GPL") == 0
		|| strcmp(license, "Dual MPL/GPL") == 0);
}

1230
static void set_license(struct module *mod, const char *license)
1231
{
1232
	if (!license)
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
		license = "unspecified";

	mod->license_gplok = license_is_gpl_compatible(license);
	if (!mod->license_gplok) {
		printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
		       mod->name, license);
		tainted |= TAINT_PROPRIETARY_MODULE;
	}
}

1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
/* Parse tag=value strings from .modinfo section */
static char *next_string(char *string, unsigned long *secsize)
{
	/* Skip non-zero chars */
	while (string[0]) {
		string++;
		if ((*secsize)-- <= 1)
			return NULL;
	}

	/* Skip any zero padding. */
	while (!string[0]) {
		string++;
		if ((*secsize)-- <= 1)
			return NULL;
	}
	return string;
}

static char *get_modinfo(Elf_Shdr *sechdrs,
			 unsigned int info,
			 const char *tag)
{
	char *p;
	unsigned int taglen = strlen(tag);
	unsigned long size = sechdrs[info].sh_size;

	for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
		if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
			return p + taglen + 1;
	}
	return NULL;
}

Rusty Russell's avatar
Rusty Russell committed
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
#ifdef CONFIG_KALLSYMS
int is_exported(const char *name, const struct module *mod)
{
	unsigned int i;

	if (!mod) {
		for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
			if (strcmp(__start___ksymtab[i].name, name) == 0)
				return 1;
		return 0;
	}
	for (i = 0; i < mod->num_syms; i++)
		if (strcmp(mod->syms[i].name, name) == 0)
			return 1;
	return 0;
}

/* As per nm */
static char elf_type(const Elf_Sym *sym,
		     Elf_Shdr *sechdrs,
		     const char *secstrings,
		     struct module *mod)
{
	if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
		if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
			return 'v';
		else
			return 'w';
	}
	if (sym->st_shndx == SHN_UNDEF)
		return 'U';
	if (sym->st_shndx == SHN_ABS)
		return 'a';
	if (sym->st_shndx >= SHN_LORESERVE)
		return '?';
	if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
		return 't';
	if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
	    && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
		if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
			return 'r';
		else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
			return 'g';
		else
			return 'd';
	}
	if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
		if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
			return 's';
		else
			return 'b';
	}
	if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
		    ".debug", strlen(".debug")) == 0)
		return 'n';
	return '?';
}

static void add_kallsyms(struct module *mod,
			 Elf_Shdr *sechdrs,
			 unsigned int symindex,
			 unsigned int strindex,
			 const char *secstrings)
{
	unsigned int i;

	mod->symtab = (void *)sechdrs[symindex].sh_addr;
	mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
	mod->strtab = (void *)sechdrs[strindex].sh_addr;

	/* Set types up while we still have access to sections. */
	for (i = 0; i < mod->num_symtab; i++)
		mod->symtab[i].st_info
			= elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
}
#endif

1354 1355
/* Allocate and load the module: note that size of section 0 is always
   zero, and we rely on this for optional sections. */
1356
static struct module *load_module(void __user *umod,
1357
				  unsigned long len,
1358
				  const char __user *uargs)
Linus Torvalds's avatar
Linus Torvalds committed
1359
{
1360 1361
	Elf_Ehdr *hdr;
	Elf_Shdr *sechdrs;
1362 1363 1364
	char *secstrings, *args, *modmagic, *strtab = NULL;
	unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
		exportindex, modindex, obsparmindex, infoindex, gplindex,
1365
		crcindex, gplcrcindex, versindex, pcpuindex;
1366 1367
	long arglen;
	struct module *mod;
1368
	long err = 0;
1369
	void *ptr = NULL; /* Stops spurious gcc uninitialized warning */
Linus Torvalds's avatar
Linus Torvalds committed
1370

1371 1372 1373 1374
	DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
	       umod, len, uargs);
	if (len < sizeof(*hdr))
		return ERR_PTR(-ENOEXEC);
Linus Torvalds's avatar
Linus Torvalds committed
1375

1376 1377 1378 1379 1380 1381 1382
	/* Suck in entire file: we'll want most of it. */
	/* vmalloc barfs on "unusual" numbers.  Check here */
	if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
		return ERR_PTR(-ENOMEM);
	if (copy_from_user(hdr, umod, len) != 0) {
		err = -EFAULT;
		goto free_hdr;
Linus Torvalds's avatar
Linus Torvalds committed
1383 1384
	}

1385 1386 1387 1388 1389 1390 1391 1392
	/* Sanity checks against insmoding binaries or wrong arch,
           weird elf version */
	if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
	    || hdr->e_type != ET_REL
	    || !elf_check_arch(hdr)
	    || hdr->e_shentsize != sizeof(*sechdrs)) {
		err = -ENOEXEC;
		goto free_hdr;
Linus Torvalds's avatar
Linus Torvalds committed
1393 1394
	}

1395 1396 1397
	/* Convenience variables */
	sechdrs = (void *)hdr + hdr->e_shoff;
	secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1398
	sechdrs[0].sh_addr = 0;
1399

1400 1401 1402
	/* And these should exist, but gcc whinges if we don't init them */
	symindex = strindex = 0;

1403
	for (i = 1; i < hdr->e_shnum; i++) {
1404 1405 1406 1407
		/* Mark all sections sh_addr with their address in the
		   temporary image. */
		sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;

Rusty Russell's avatar
Rusty Russell committed
1408
		/* Internal symbols and strings. */
1409 1410
		if (sechdrs[i].sh_type == SHT_SYMTAB) {
			symindex = i;
1411
			strindex = sechdrs[i].sh_link;
1412
			strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1413 1414 1415 1416 1417 1418
		}
#ifndef CONFIG_MODULE_UNLOAD
		/* Don't load .exit sections */
		if (strstr(secstrings+sechdrs[i].sh_name, ".exit"))
			sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1419 1420
	}

Rusty Russell's avatar
Rusty Russell committed
1421 1422
	modindex = find_sec(hdr, sechdrs, secstrings,
			    ".gnu.linkonce.this_module");
1423 1424
	if (!modindex) {
		printk(KERN_WARNING "No module found in object\n");
1425 1426 1427
		err = -ENOEXEC;
		goto free_hdr;
	}
1428
	mod = (void *)sechdrs[modindex].sh_addr;
Linus Torvalds's avatar
Linus Torvalds committed
1429

Rusty Russell's avatar
Rusty Russell committed
1430 1431 1432 1433 1434 1435 1436 1437 1438
	/* Optional sections */
	exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
	gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
	crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
	gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
	setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
	exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
	obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
	versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1439
	infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1440
	pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1441 1442 1443 1444 1445 1446 1447 1448

	/* Don't keep modinfo section */
	sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
#ifdef CONFIG_KALLSYMS
	/* Keep symbol and string tables for decoding later. */
	sechdrs[symindex].sh_flags |= SHF_ALLOC;
	sechdrs[strindex].sh_flags |= SHF_ALLOC;
#endif
Rusty Russell's avatar
Rusty Russell committed
1449

1450 1451 1452 1453 1454 1455
	/* Check module struct version now, before we try to use module. */
	if (!check_modstruct_version(sechdrs, versindex, mod)) {
		err = -ENOEXEC;
		goto free_hdr;
	}

1456
	modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1457
	/* This is allowed: modprobe --force will invalidate it. */
1458
	if (!modmagic) {
Kai Germaschewski's avatar
Kai Germaschewski committed
1459 1460 1461
		tainted |= TAINT_FORCED_MODULE;
		printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
		       mod->name);
1462
	} else if (!same_magic(modmagic, vermagic)) {
Kai Germaschewski's avatar
Kai Germaschewski committed
1463
		printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1464
		       mod->name, modmagic, vermagic);
Kai Germaschewski's avatar
Kai Germaschewski committed
1465 1466 1467 1468
		err = -ENOEXEC;
		goto free_hdr;
	}

1469
	/* Now copy in args */
1470 1471 1472
	arglen = strlen_user(uargs);
	if (!arglen) {
		err = -EFAULT;
1473
		goto free_hdr;
1474 1475
	}
	args = kmalloc(arglen, GFP_KERNEL);
1476
	if (!args) {
1477 1478
		err = -ENOMEM;
		goto free_hdr;
Linus Torvalds's avatar
Linus Torvalds committed
1479
	}
1480
	if (copy_from_user(args, uargs, arglen) != 0) {
1481 1482 1483
		err = -EFAULT;
		goto free_mod;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1484

1485 1486 1487 1488
	if (find_module(mod->name)) {
		err = -EEXIST;
		goto free_mod;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1489

1490
	mod->state = MODULE_STATE_COMING;
1491

1492 1493
	/* Allow arches to frob section contents and sizes.  */
	err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1494
	if (err < 0)
1495 1496
		goto free_mod;

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
	if (pcpuindex) {
		/* We have a special allocation for this section. */
		mod->percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
					      sechdrs[pcpuindex].sh_addralign);
		if (!mod->percpu) {
			err = -ENOMEM;
			goto free_mod;
		}
		sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
	}

1508
	/* Determine total sizes, and put offsets in sh_entsize.  For now
1509 1510 1511
	   this is done generically; there doesn't appear to be any
	   special cases for the architectures. */
	layout_sections(mod, hdr, sechdrs, secstrings);
1512 1513 1514 1515 1516

	/* Do the allocs. */
	ptr = module_alloc(mod->core_size);
	if (!ptr) {
		err = -ENOMEM;
1517
		goto free_percpu;
1518 1519
	}
	memset(ptr, 0, mod->core_size);
1520 1521
	mod->module_core = ptr;

1522
	ptr = module_alloc(mod->init_size);
1523
	if (!ptr && mod->init_size) {
1524
		err = -ENOMEM;
1525
		goto free_core;
1526 1527
	}
	memset(ptr, 0, mod->init_size);
1528 1529
	mod->module_init = ptr;

1530
	/* Transfer each section which specifies SHF_ALLOC */
1531
	DEBUGP("final section addresses:\n");
1532 1533 1534 1535 1536 1537
	for (i = 0; i < hdr->e_shnum; i++) {
		void *dest;

		if (!(sechdrs[i].sh_flags & SHF_ALLOC))
			continue;

1538
		if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1539
			dest = mod->module_init
1540
				+ (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1541
		else
1542
			dest = mod->module_core + sechdrs[i].sh_entsize;
1543 1544 1545 1546 1547 1548

		if (sechdrs[i].sh_type != SHT_NOBITS)
			memcpy(dest, (void *)sechdrs[i].sh_addr,
			       sechdrs[i].sh_size);
		/* Update sh_addr to point to copy in image. */
		sechdrs[i].sh_addr = (unsigned long)dest;
1549
		DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1550
	}
1551 1552
	/* Module has been moved. */
	mod = (void *)sechdrs[modindex].sh_addr;
Linus Torvalds's avatar
Linus Torvalds committed
1553

1554 1555 1556
	/* Now we've moved module, initialize linked lists, etc. */
	module_unload_init(mod);

1557 1558
	/* Set up license info based on the info section */
	set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1559

1560
	/* Fix up syms, so that st_value is a pointer to location. */
1561 1562
	err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
			       mod);
1563 1564
	if (err < 0)
		goto cleanup;
Linus Torvalds's avatar
Linus Torvalds committed
1565

1566
	/* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
Rusty Russell's avatar
Rusty Russell committed
1567 1568
	mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
	mod->syms = (void *)sechdrs[exportindex].sh_addr;
Rusty Russell's avatar
Rusty Russell committed
1569
	if (crcindex)
Rusty Russell's avatar
Rusty Russell committed
1570 1571 1572
		mod->crcs = (void *)sechdrs[crcindex].sh_addr;
	mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
	mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
Rusty Russell's avatar
Rusty Russell committed
1573
	if (gplcrcindex)
Rusty Russell's avatar
Rusty Russell committed
1574
		mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
Rusty Russell's avatar
Rusty Russell committed
1575 1576

#ifdef CONFIG_MODVERSIONS
1577 1578
	if ((mod->num_syms && !crcindex) || 
	    (mod->num_gpl_syms && !gplcrcindex)) {
Rusty Russell's avatar
Rusty Russell committed
1579 1580 1581 1582 1583
		printk(KERN_WARNING "%s: No versions for exported symbols."
		       " Tainting kernel.\n", mod->name);
		tainted |= TAINT_FORCED_MODULE;
	}
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1584

Rusty Russell's avatar
Rusty Russell committed
1585 1586 1587
  	/* Set up exception table */
	mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
	mod->extable = (void *)sechdrs[exindex].sh_addr;
Linus Torvalds's avatar
Linus Torvalds committed
1588

Rusty Russell's avatar
Rusty Russell committed
1589
	/* Now do relocations. */
1590
	for (i = 1; i < hdr->e_shnum; i++) {
1591
		const char *strtab = (char *)sechdrs[strindex].sh_addr;
Rusty Russell's avatar
Rusty Russell committed
1592
		if (sechdrs[i].sh_type == SHT_REL)
1593
			err = apply_relocate(sechdrs, strtab, symindex, i,mod);
Rusty Russell's avatar
Rusty Russell committed
1594 1595 1596
		else if (sechdrs[i].sh_type == SHT_RELA)
			err = apply_relocate_add(sechdrs, strtab, symindex, i,
						 mod);
1597 1598
		if (err < 0)
			goto cleanup;
Linus Torvalds's avatar
Linus Torvalds committed
1599 1600
	}

1601 1602 1603 1604
	/* Finally, copy percpu area over. */
	percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
		       sechdrs[pcpuindex].sh_size);

1605 1606 1607 1608
	err = module_finalize(hdr, sechdrs, mod);
	if (err < 0)
		goto cleanup;

Rusty Russell's avatar
Rusty Russell committed
1609 1610
	add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);

1611
	mod->args = args;
1612 1613 1614
	if (obsparmindex) {
		err = obsolete_params(mod->name, mod->args,
				      (struct obsolete_modparm *)
1615
				      sechdrs[obsparmindex].sh_addr,
1616 1617 1618
				      sechdrs[obsparmindex].sh_size
				      / sizeof(struct obsolete_modparm),
				      sechdrs, symindex,
1619
				      (char *)sechdrs[strindex].sh_addr);
1620 1621 1622 1623
	} else {
		/* Size of section 0 is 0, so this works well if no params */
		err = parse_args(mod->name, mod->args,
				 (struct kernel_param *)
1624
				 sechdrs[setupindex].sh_addr,
1625 1626 1627 1628
				 sechdrs[setupindex].sh_size
				 / sizeof(struct kernel_param),
				 NULL);
	}
1629 1630
	if (err < 0)
		goto cleanup;
Linus Torvalds's avatar
Linus Torvalds committed
1631

1632 1633
	/* Get rid of temporary copy */
	vfree(hdr);
Linus Torvalds's avatar
Linus Torvalds committed
1634

1635 1636
	/* Done! */
	return mod;
Linus Torvalds's avatar
Linus Torvalds committed
1637

1638 1639 1640 1641 1642
 cleanup:
	module_unload_free(mod);
	module_free(mod, mod->module_init);
 free_core:
	module_free(mod, mod->module_core);
1643 1644 1645
 free_percpu:
	if (mod->percpu)
		percpu_modfree(mod->percpu);
1646
 free_mod:
1647
	kfree(args);
1648 1649 1650 1651 1652
 free_hdr:
	vfree(hdr);
	if (err < 0) return ERR_PTR(err);
	else return ptr;
}
Ingo Molnar's avatar
Ingo Molnar committed
1653

1654 1655
/* This is where the real work happens */
asmlinkage long
1656
sys_init_module(void __user *umod,
1657
		unsigned long len,
1658
		const char __user *uargs)
Ingo Molnar's avatar
Ingo Molnar committed
1659
{
1660 1661
	struct module *mod;
	int ret;
Ingo Molnar's avatar
Ingo Molnar committed
1662

1663 1664 1665
	/* Must have permission */
	if (!capable(CAP_SYS_MODULE))
		return -EPERM;
Linus Torvalds's avatar
Linus Torvalds committed
1666

1667 1668 1669
	/* Only one module load at a time, please */
	if (down_interruptible(&module_mutex) != 0)
		return -EINTR;
Linus Torvalds's avatar
Linus Torvalds committed
1670

1671 1672 1673 1674 1675 1676
	/* Do all the hard work */
	mod = load_module(umod, len, uargs);
	if (IS_ERR(mod)) {
		up(&module_mutex);
		return PTR_ERR(mod);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1677

1678 1679 1680 1681 1682 1683 1684 1685
	/* Flush the instruction cache, since we've played with text */
	if (mod->module_init)
		flush_icache_range((unsigned long)mod->module_init,
				   (unsigned long)mod->module_init
				   + mod->init_size);
	flush_icache_range((unsigned long)mod->module_core,
			   (unsigned long)mod->module_core + mod->core_size);

1686 1687
	/* Now sew it into the lists.  They won't access us, since
           strong_try_module_get() will fail. */
1688
	spin_lock_irq(&modlist_lock);
1689
	list_add(&mod->list, &modules);
1690
	spin_unlock_irq(&modlist_lock);
1691 1692 1693

	/* Drop lock so they can recurse */
	up(&module_mutex);
1694

1695 1696 1697 1698
	down(&notify_mutex);
	notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
	up(&notify_mutex);

1699
	/* Start the module */
1700
	ret = mod->init();
1701 1702 1703
	if (ret < 0) {
		/* Init routine failed: abort.  Try to protect us from
                   buggy refcounters. */
1704
		mod->state = MODULE_STATE_GOING;
1705
		synchronize_kernel();
1706
		if (mod->unsafe)
1707 1708
			printk(KERN_ERR "%s: module is now stuck!\n",
			       mod->name);
1709
		else {
1710
			module_put(mod);
1711
			down(&module_mutex);
1712
			free_module(mod);
1713
			up(&module_mutex);
1714
		}
1715 1716
		return ret;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1717

1718
	/* Now it's a first class citizen! */
Rusty Russell's avatar
Rusty Russell committed
1719
	down(&module_mutex);
1720
	mod->state = MODULE_STATE_LIVE;
1721 1722
	/* Drop initial reference. */
	module_put(mod);
1723 1724
	module_free(mod, mod->module_init);
	mod->module_init = NULL;
1725
	mod->init_size = 0;
Rusty Russell's avatar
Rusty Russell committed
1726
	up(&module_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1727

1728
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1729 1730
}

1731
static inline int within(unsigned long addr, void *start, unsigned long size)
1732
{
1733
	return ((void *)addr >= start && (void *)addr < start + size);
1734 1735
}

1736
#ifdef CONFIG_KALLSYMS
1737 1738 1739 1740 1741
static const char *get_ksymbol(struct module *mod,
			       unsigned long addr,
			       unsigned long *size,
			       unsigned long *offset)
{
1742 1743 1744 1745
	unsigned int i, best = 0;
	unsigned long nextval;

	/* At worse, next value is at end of module */
1746
	if (within(addr, mod->module_init, mod->init_size))
1747
		nextval = (unsigned long)mod->module_init + mod->init_size;
1748
	else 
1749
		nextval = (unsigned long)mod->module_core + mod->core_size;
1750 1751 1752

	/* Scan for closest preceeding symbol, and next symbol. (ELF
           starts real symbols at 1). */
1753
	for (i = 1; i < mod->num_symtab; i++) {
1754 1755 1756 1757 1758 1759 1760
		if (mod->symtab[i].st_shndx == SHN_UNDEF)
			continue;

		if (mod->symtab[i].st_value <= addr
		    && mod->symtab[i].st_value > mod->symtab[best].st_value)
			best = i;
		if (mod->symtab[i].st_value > addr
1761 1762
		    && mod->symtab[i].st_value < nextval)
			nextval = mod->symtab[i].st_value;
1763 1764 1765 1766 1767
	}

	if (!best)
		return NULL;

1768
	*size = nextval - mod->symtab[best].st_value;
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
	*offset = addr - mod->symtab[best].st_value;
	return mod->strtab + mod->symtab[best].st_name;
}

/* For kallsyms to ask for address resolution.  NULL means not found.
   We don't lock, as this is used for oops resolution and races are a
   lesser concern. */
const char *module_address_lookup(unsigned long addr,
				  unsigned long *size,
				  unsigned long *offset,
				  char **modname)
{
	struct module *mod;

	list_for_each_entry(mod, &modules, list) {
1784 1785
		if (within(addr, mod->module_init, mod->init_size)
		    || within(addr, mod->module_core, mod->core_size)) {
1786 1787 1788 1789 1790 1791
			*modname = mod->name;
			return get_ksymbol(mod, addr, size, offset);
		}
	}
	return NULL;
}
Rusty Russell's avatar
Rusty Russell committed
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815

struct module *module_get_kallsym(unsigned int symnum,
				  unsigned long *value,
				  char *type,
				  char namebuf[128])
{
	struct module *mod;

	down(&module_mutex);
	list_for_each_entry(mod, &modules, list) {
		if (symnum < mod->num_symtab) {
			*value = mod->symtab[symnum].st_value;
			*type = mod->symtab[symnum].st_info;
			strncpy(namebuf,
				mod->strtab + mod->symtab[symnum].st_name,
				127);
			up(&module_mutex);
			return mod;
		}
		symnum -= mod->num_symtab;
	}
	up(&module_mutex);
	return NULL;
}
1816 1817 1818
#endif /* CONFIG_KALLSYMS */

/* Called by the /proc file system to return a list of modules. */
1819
static void *m_start(struct seq_file *m, loff_t *pos)
Linus Torvalds's avatar
Linus Torvalds committed
1820
{
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
	struct list_head *i;
	loff_t n = 0;

	down(&module_mutex);
	list_for_each(i, &modules) {
		if (n++ == *pos)
			break;
	}
	if (i == &modules)
		return NULL;
	return i;
Linus Torvalds's avatar
Linus Torvalds committed
1832 1833
}

1834
static void *m_next(struct seq_file *m, void *p, loff_t *pos)
Linus Torvalds's avatar
Linus Torvalds committed
1835
{
1836 1837 1838 1839 1840
	struct list_head *i = p;
	(*pos)++;
	if (i->next == &modules)
		return NULL;
	return i->next;
Linus Torvalds's avatar
Linus Torvalds committed
1841 1842
}

1843
static void m_stop(struct seq_file *m, void *p)
Ingo Molnar's avatar
Ingo Molnar committed
1844
{
1845
	up(&module_mutex);
Ingo Molnar's avatar
Ingo Molnar committed
1846 1847
}

1848
static int m_show(struct seq_file *m, void *p)
Ingo Molnar's avatar
Ingo Molnar committed
1849
{
1850 1851 1852 1853
	struct module *mod = list_entry(p, struct module, list);
	seq_printf(m, "%s %lu",
		   mod->name, mod->init_size + mod->core_size);
	print_unload_info(m, mod);
1854 1855 1856 1857 1858 1859 1860 1861 1862

	/* Informative for users. */
	seq_printf(m, " %s",
		   mod->state == MODULE_STATE_GOING ? "Unloading":
		   mod->state == MODULE_STATE_COMING ? "Loading":
		   "Live");
	/* Used by oprofile and other similar tools. */
	seq_printf(m, " 0x%p", mod->module_core);

Rusty Russell's avatar
Rusty Russell committed
1863
	seq_printf(m, "\n");
1864
	return 0;
Ingo Molnar's avatar
Ingo Molnar committed
1865
}
Rusty Russell's avatar
Rusty Russell committed
1866

1867
/* Format: modulename size refcount deps address
Rusty Russell's avatar
Rusty Russell committed
1868 1869 1870 1871

   Where refcount is a number or -, and deps is a comma-separated list
   of depends or -.
*/
1872 1873 1874 1875 1876 1877
struct seq_operations modules_op = {
	.start	= m_start,
	.next	= m_next,
	.stop	= m_stop,
	.show	= m_show
};
Ingo Molnar's avatar
Ingo Molnar committed
1878

1879 1880 1881 1882 1883
/* Given an address, look for it in the module exception tables. */
const struct exception_table_entry *search_module_extables(unsigned long addr)
{
	unsigned long flags;
	const struct exception_table_entry *e = NULL;
Rusty Russell's avatar
Rusty Russell committed
1884
	struct module *mod;
1885 1886

	spin_lock_irqsave(&modlist_lock, flags);
Rusty Russell's avatar
Rusty Russell committed
1887 1888
	list_for_each_entry(mod, &modules, list) {
		if (mod->num_exentries == 0)
1889 1890
			continue;
				
Rusty Russell's avatar
Rusty Russell committed
1891 1892 1893
		e = search_extable(mod->extable,
				   mod->extable + mod->num_exentries - 1,
				   addr);
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
		if (e)
			break;
	}
	spin_unlock_irqrestore(&modlist_lock, flags);

	/* Now, if we found one, we are running inside it now, hence
           we cannot unload the module, hence no refcnt needed. */
	return e;
}

1904
/* Is this a valid kernel address?  We don't grab the lock: we are oopsing. */
1905
struct module *module_text_address(unsigned long addr)
1906 1907 1908 1909 1910 1911
{
	struct module *mod;

	list_for_each_entry(mod, &modules, list)
		if (within(addr, mod->module_init, mod->init_size)
		    || within(addr, mod->module_core, mod->core_size))
1912 1913
			return mod;
	return NULL;
1914 1915
}

1916 1917 1918 1919 1920
#ifdef CONFIG_MODVERSIONS
/* Generate the signature for struct module here, too, for modversions. */
void struct_module(struct module *mod) { return; }
EXPORT_SYMBOL(struct_module);
#endif