module.c 39.9 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 168 169 170
			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) {
					*crc = symversion(mod->crcs, i);
					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
#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
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
	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);
	/* 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
237 238
		}
	}
239 240
	DEBUGP("%s does not use %s!\n", a->name, b->name);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
241 242
}

243 244
/* Module a uses b */
static int use_module(struct module *a, struct module *b)
Linus Torvalds's avatar
Linus Torvalds committed
245
{
246 247 248
	struct module_use *use;
	if (b == NULL || already_uses(a, b)) return 1;

Rusty Russell's avatar
Rusty Russell committed
249 250 251
	if (!strong_try_module_get(b))
		return 0;

252 253 254 255
	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
256
		module_put(b);
257
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
258
	}
259 260 261 262

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

265 266
/* Clear the unload stuff of the module. */
static void module_unload_free(struct module *mod)
Linus Torvalds's avatar
Linus Torvalds committed
267
{
268 269 270 271 272 273 274 275 276 277 278 279 280 281
	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
282 283 284 285
		}
	}
}

286 287 288 289 290 291 292 293
#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
294

295 296 297
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
298

299 300 301 302
static int stopref(void *cpu)
{
	int irqs_disabled = 0;
	int prepared = 0;
Linus Torvalds's avatar
Linus Torvalds committed
303

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

306 307 308 309 310
	/* 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
311
	set_cpus_allowed(current, 1UL << (unsigned long)cpu);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

	/* 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
334

335 336
	/* Ack: we are exiting. */
	atomic_inc(&stopref_thread_ack);
Linus Torvalds's avatar
Linus Torvalds committed
337

338 339 340 341
	if (irqs_disabled)
		local_irq_enable();
	if (prepared)
		preempt_enable();
Linus Torvalds's avatar
Linus Torvalds committed
342

343 344
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
345

346 347
/* Change the thread state */
static void stopref_set_state(enum stopref_state state, int sleep)
Linus Torvalds's avatar
Linus Torvalds committed
348
{
349 350 351 352 353 354 355 356 357
	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
358 359
}

360 361
/* Stop the machine.  Disables irqs. */
static int stop_refcounts(void)
Linus Torvalds's avatar
Linus Torvalds committed
362
{
363 364 365
	unsigned int i, cpu;
	unsigned long old_allowed;
	int ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
366

367 368
	/* One thread per cpu.  We'll do our own. */
	cpu = smp_processor_id();
Linus Torvalds's avatar
Linus Torvalds committed
369

370 371
	/* FIXME: racy with set_cpus_allowed. */
	old_allowed = current->cpus_allowed;
372
	set_cpus_allowed(current, 1UL << (unsigned long)cpu);
Linus Torvalds's avatar
Linus Torvalds committed
373

374 375 376 377 378 379 380 381 382 383
	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;
384
		ret = kernel_thread(stopref, (void *)(long)i, CLONE_KERNEL);
385 386 387
		if (ret < 0)
			break;
		stopref_num_threads++;
Linus Torvalds's avatar
Linus Torvalds committed
388
	}
389 390 391 392 393 394 395 396 397 398

	/* 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
399 400
	}

401 402
	/* Don't schedule us away at this point, please. */
	preempt_disable();
Linus Torvalds's avatar
Linus Torvalds committed
403

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

407 408
	/* Make them disable irqs. */
	stopref_set_state(STOPREF_DISABLE_IRQ, 0);
Linus Torvalds's avatar
Linus Torvalds committed
409

410 411
	local_irq_disable();
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
412 413
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
/* 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

static unsigned int module_refcount(struct module *mod)
{
	unsigned int i, total = 0;

	for (i = 0; i < NR_CPUS; i++)
		total += atomic_read(&mod->ref[i].count);
	return total;
}

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

Rusty Russell's avatar
Rusty Russell committed
446 447 448 449 450 451 452 453 454 455 456 457
#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 */

458 459 460 461 462 463
/* Stub function for modules which don't have an exitfn */
void cleanup_module(void)
{
}
EXPORT_SYMBOL(cleanup_module);

Linus Torvalds's avatar
Linus Torvalds committed
464
asmlinkage long
465
sys_delete_module(const char *name_user, unsigned int flags)
Linus Torvalds's avatar
Linus Torvalds committed
466
{
467 468
	struct module *mod;
	char name[MODULE_NAME_LEN];
Rusty Russell's avatar
Rusty Russell committed
469
	int ret, forced = 0;
Linus Torvalds's avatar
Linus Torvalds committed
470 471 472 473

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

474 475 476
	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
477

478 479
	if (down_interruptible(&module_mutex) != 0)
		return -EINTR;
Linus Torvalds's avatar
Linus Torvalds committed
480

481 482 483 484
	mod = find_module(name);
	if (!mod) {
		ret = -ENOENT;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
485 486
	}

Rusty Russell's avatar
Rusty Russell committed
487 488 489 490 491 492
	if (!list_empty(&mod->modules_which_use_me)) {
		/* Other modules depend on us: get rid of them first. */
		ret = -EWOULDBLOCK;
		goto out;
	}

493
	/* Already dying? */
494
	if (mod->state == MODULE_STATE_GOING) {
Rusty Russell's avatar
Rusty Russell committed
495 496
		/* FIXME: if (force), slam module count and wake up
                   waiter --RR */
497 498 499
		DEBUGP("%s already dying\n", mod->name);
		ret = -EBUSY;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
500 501
	}

502 503 504 505 506 507 508 509 510 511
	/* Coming up?  Allow force on stuck modules. */
	if (mod->state == MODULE_STATE_COMING) {
		forced = try_force(flags);
		if (!forced) {
			/* This module can't be removed */
			ret = -EBUSY;
			goto out;
		}
	}

512
	/* If it has an init func, it must have an exit func to unload */
513 514
	if ((mod->init != init_module && mod->exit == cleanup_module)
	    || mod->unsafe) {
Rusty Russell's avatar
Rusty Russell committed
515 516 517 518 519 520
		forced = try_force(flags);
		if (!forced) {
			/* This module can't be removed */
			ret = -EBUSY;
			goto out;
		}
Linus Torvalds's avatar
Linus Torvalds committed
521
	}
522 523 524 525 526
	/* 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
527

528
	/* If it's not unused, quit unless we are told to block. */
Rusty Russell's avatar
Rusty Russell committed
529 530 531 532 533
	if ((flags & O_NONBLOCK) && module_refcount(mod) != 0) {
		forced = try_force(flags);
		if (!forced)
			ret = -EWOULDBLOCK;
	} else {
534
		mod->waiter = current;
535
		mod->state = MODULE_STATE_GOING;
Linus Torvalds's avatar
Linus Torvalds committed
536
	}
537
	restart_refcounts();
Linus Torvalds's avatar
Linus Torvalds committed
538

539 540
	if (ret != 0)
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
541

Rusty Russell's avatar
Rusty Russell committed
542 543 544
	if (forced)
		goto destroy;

545 546 547 548 549 550 551 552
	/* 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();
Linus Torvalds's avatar
Linus Torvalds committed
553
	}
554
	current->state = TASK_RUNNING;
Linus Torvalds's avatar
Linus Torvalds committed
555

556 557
	DEBUGP("Regrabbing mutex...\n");
	down(&module_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
558

Rusty Russell's avatar
Rusty Russell committed
559
 destroy:
560
	/* Final destruction now noone is using it. */
561
	mod->exit();
562
	free_module(mod);
Linus Torvalds's avatar
Linus Torvalds committed
563

564 565 566 567
 out:
	up(&module_mutex);
	return ret;
}
Linus Torvalds's avatar
Linus Torvalds committed
568

569 570 571
static void print_unload_info(struct seq_file *m, struct module *mod)
{
	struct module_use *use;
Rusty Russell's avatar
Rusty Russell committed
572
	int printed_something = 0;
Linus Torvalds's avatar
Linus Torvalds committed
573

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

Rusty Russell's avatar
Rusty Russell committed
576 577 578 579 580 581
	/* 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
582

Rusty Russell's avatar
Rusty Russell committed
583 584 585 586
	if (mod->unsafe) {
		printed_something = 1;
		seq_printf(m, "[unsafe],");
	}
Linus Torvalds's avatar
Linus Torvalds committed
587

Rusty Russell's avatar
Rusty Russell committed
588 589 590 591
	if (mod->init != init_module && mod->exit == cleanup_module) {
		printed_something = 1;
		seq_printf(m, "[permanent],");
	}
Linus Torvalds's avatar
Linus Torvalds committed
592

Rusty Russell's avatar
Rusty Russell committed
593 594
	if (!printed_something)
		seq_printf(m, "-");
Linus Torvalds's avatar
Linus Torvalds committed
595 596
}

597
void __symbol_put(const char *symbol)
Linus Torvalds's avatar
Linus Torvalds committed
598
{
Rusty Russell's avatar
Rusty Russell committed
599
	struct module *owner;
600
	unsigned long flags;
Rusty Russell's avatar
Rusty Russell committed
601
	const unsigned long *crc;
602 603

	spin_lock_irqsave(&modlist_lock, flags);
Rusty Russell's avatar
Rusty Russell committed
604
	if (!__find_symbol(symbol, &owner, &crc, 1))
605
		BUG();
Rusty Russell's avatar
Rusty Russell committed
606
	module_put(owner);
607
	spin_unlock_irqrestore(&modlist_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
608
}
609
EXPORT_SYMBOL(__symbol_put);
Linus Torvalds's avatar
Linus Torvalds committed
610

611 612 613 614 615
void symbol_put_addr(void *addr)
{
	unsigned long flags;

	spin_lock_irqsave(&modlist_lock, flags);
616 617
	if (!kernel_text_address((unsigned long)addr))
		BUG();
618

619
	module_put(module_text_address((unsigned long)addr));
620 621 622 623
	spin_unlock_irqrestore(&modlist_lock, flags);
}
EXPORT_SYMBOL_GPL(symbol_put_addr);

624 625
#else /* !CONFIG_MODULE_UNLOAD */
static void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds's avatar
Linus Torvalds committed
626
{
Rusty Russell's avatar
Rusty Russell committed
627 628
	/* We don't know the usage count, or what modules are using. */
	seq_printf(m, " - -");
Linus Torvalds's avatar
Linus Torvalds committed
629 630
}

631
static inline void module_unload_free(struct module *mod)
Linus Torvalds's avatar
Linus Torvalds committed
632 633 634
{
}

635
static inline int use_module(struct module *a, struct module *b)
Linus Torvalds's avatar
Linus Torvalds committed
636
{
637
	return strong_try_module_get(b);
638
}
Linus Torvalds's avatar
Linus Torvalds committed
639

640 641
static inline void module_unload_init(struct module *mod)
{
Linus Torvalds's avatar
Linus Torvalds committed
642 643
}

644 645
asmlinkage long
sys_delete_module(const char *name_user, unsigned int flags)
Linus Torvalds's avatar
Linus Torvalds committed
646
{
647 648
	return -ENOSYS;
}
Linus Torvalds's avatar
Linus Torvalds committed
649

650
#endif /* CONFIG_MODULE_UNLOAD */
Linus Torvalds's avatar
Linus Torvalds committed
651

652 653 654 655 656 657 658 659 660 661 662 663 664 665
#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;
}

666 667 668 669 670 671 672
/* Bounds checking done below */
static int obsparm_copy_string(const char *val, struct kernel_param *kp)
{
	strcpy(kp->arg, val);
	return 0;
}

673 674 675
extern int set_obsolete(const char *val, struct kernel_param *kp)
{
	unsigned int min, max;
676 677 678
	unsigned int size, maxsize;
	char *endp;
	const char *p;
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
	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':
710 711
		return param_array(kp->name, val, min, max, obsparm->addr,
				   sizeof(char *), param_set_charp);
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

	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);
729 730 731
	}
	printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
	return -EINVAL;
732 733 734 735
 oversize:
	printk(KERN_ERR
	       "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
	return -EINVAL;
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
}

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++) {
755 756 757 758 759
		char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];

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

760 761 762 763 764 765
		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,
766
						    sym_name);
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
		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 */

797 798
static const char vermagic[] = VERMAGIC_STRING;

799
#ifdef CONFIG_MODVERSIONS
800 801 802 803
static int check_version(Elf_Shdr *sechdrs,
			 unsigned int versindex,
			 const char *symname,
			 struct module *mod, 
Rusty Russell's avatar
Rusty Russell committed
804
			 const unsigned long *crc)
805 806 807 808
{
	unsigned int i, num_versions;
	struct modversion_info *versions;

Rusty Russell's avatar
Rusty Russell committed
809
	/* Exporting module didn't supply crcs?  OK, we're already tainted. */
Rusty Russell's avatar
Rusty Russell committed
810
	if (!crc)
Rusty Russell's avatar
Rusty Russell committed
811
		return 1;
812 813 814 815 816 817 818 819 820

	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
821
		if (versions[i].crc == *crc)
822 823 824 825
			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
826
		       *crc, versions[i].crc);
827 828 829
		return 0;
	}
	/* Not in module's version table.  OK, but that taints the kernel. */
Rusty Russell's avatar
Rusty Russell committed
830 831 832 833 834
	if (!(tainted & TAINT_FORCED_MODULE)) {
		printk("%s: no version for \"%s\" found: kernel tainted.\n",
		       mod->name, symname);
		tainted |= TAINT_FORCED_MODULE;
	}
835 836
	return 1;
}
837

838 839 840 841
static inline int check_modstruct_version(Elf_Shdr *sechdrs,
					  unsigned int versindex,
					  struct module *mod)
{
842 843
	const unsigned long *crc;
	struct module *owner;
844

845
	if (!__find_symbol("struct_module", &owner, &crc, 1))
846
		BUG();
847 848
	return check_version(sechdrs, versindex, "struct_module", mod,
			     crc);
849 850
}

851 852 853 854 855 856 857
/* 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;
}
858 859 860 861 862
#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
863
				const unsigned long *crc)
864 865 866
{
	return 1;
}
867

868 869 870 871 872 873 874
static inline int check_modstruct_version(Elf_Shdr *sechdrs,
					  unsigned int versindex,
					  struct module *mod)
{
	return 1;
}

875 876 877 878
static inline int same_magic(const char *amagic, const char *bmagic)
{
	return strcmp(amagic, bmagic) == 0;
}
879
#endif /* CONFIG_MODVERSIONS */
880

881 882 883
/* 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,
884
				    unsigned int versindex,
885 886
				    const char *name,
				    struct module *mod)
887
{
Rusty Russell's avatar
Rusty Russell committed
888
	struct module *owner;
889
	unsigned long ret;
Rusty Russell's avatar
Rusty Russell committed
890
	const unsigned long *crc;
Linus Torvalds's avatar
Linus Torvalds committed
891

892
	spin_lock_irq(&modlist_lock);
Rusty Russell's avatar
Rusty Russell committed
893
	ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
894
	if (ret) {
895
		/* use_module can fail due to OOM, or module unloading */
Rusty Russell's avatar
Rusty Russell committed
896 897
		if (!check_version(sechdrs, versindex, name, mod, crc) ||
		    !use_module(mod, owner))
898 899 900 901
			ret = 0;
	}
	spin_unlock_irq(&modlist_lock);
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
902 903
}

904 905
/* 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
906
{
907 908
	/* Delete from various lists */
	spin_lock_irq(&modlist_lock);
909
	list_del(&mod->list);
910 911 912 913 914
	spin_unlock_irq(&modlist_lock);

	/* Module unload stuff */
	module_unload_free(mod);

915 916 917 918 919 920
	/* This may be NULL, but that's OK */
	module_free(mod, mod->module_init);
	kfree(mod->args);

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

923 924
void *__symbol_get(const char *symbol)
{
Rusty Russell's avatar
Rusty Russell committed
925
	struct module *owner;
926
	unsigned long value, flags;
Rusty Russell's avatar
Rusty Russell committed
927
	const unsigned long *crc;
Linus Torvalds's avatar
Linus Torvalds committed
928

929
	spin_lock_irqsave(&modlist_lock, flags);
Rusty Russell's avatar
Rusty Russell committed
930 931
	value = __find_symbol(symbol, &owner, &crc, 1);
	if (value && !strong_try_module_get(owner))
932 933
		value = 0;
	spin_unlock_irqrestore(&modlist_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
934

935 936 937
	return (void *)value;
}
EXPORT_SYMBOL_GPL(__symbol_get);
Linus Torvalds's avatar
Linus Torvalds committed
938

939
/* Change all symbols so that sh_value encodes the pointer directly. */
940 941 942
static int simplify_symbols(Elf_Shdr *sechdrs,
			    unsigned int symindex,
			    unsigned int strindex,
943
			    unsigned int versindex,
944
			    struct module *mod)
945
{
946 947 948 949 950 951
	Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
	const char *strtab = (char *)sechdrs[strindex].sh_addr;
	unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
	int ret = 0;

	for (i = 1; i < n; i++) {
952 953
		switch (sym[i].st_shndx) {
		case SHN_COMMON:
954 955 956
			/* We compiled with -fno-common.  These are not
			   supposed to happen.  */
			DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
957 958
			ret = -ENOEXEC;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
959

960 961 962 963 964
		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
965

966
		case SHN_UNDEF:
967
			sym[i].st_value
968
			  = resolve_symbol(sechdrs, versindex,
969 970 971 972 973 974 975 976 977 978 979 980
					   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;
981
			break;
Linus Torvalds's avatar
Linus Torvalds committed
982

983 984 985
		default:
			sym[i].st_value 
				= (unsigned long)
986
				(sechdrs[sym[i].st_shndx].sh_addr
987
				 + sym[i].st_value);
988
			break;
989 990
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
991

992
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
993 994
}

995 996
/* Update size with this section: return offset. */
static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
Linus Torvalds's avatar
Linus Torvalds committed
997
{
998
	long ret;
999

1000 1001 1002 1003
	ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
	*size = ret + sechdr->sh_size;
	return ret;
}
1004

1005 1006
/* 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
1007
   sizes, and place the offsets into sh_entsize fields: high bit means it
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
   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++)
1023
		sechdrs[i].sh_entsize = ~0UL;
1024 1025 1026 1027 1028 1029 1030 1031

	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])
1032
			    || s->sh_entsize != ~0UL
1033 1034
			    || strstr(secstrings + s->sh_name, ".init"))
				continue;
1035
			s->sh_entsize = get_offset(&mod->core_size, s);
1036
			DEBUGP("\t%s\n", secstrings + s->sh_name);
1037
		}
Linus Torvalds's avatar
Linus Torvalds committed
1038 1039
	}

1040 1041 1042 1043 1044 1045 1046
	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])
1047
			    || s->sh_entsize != ~0UL
1048 1049
			    || !strstr(secstrings + s->sh_name, ".init"))
				continue;
1050 1051
			s->sh_entsize = (get_offset(&mod->init_size, s)
					 | INIT_OFFSET_MASK);
1052
			DEBUGP("\t%s\n", secstrings + s->sh_name);
1053 1054
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1055 1056
}

1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
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);
}

static void set_license(struct module *mod, Elf_Shdr *sechdrs, int licenseidx)
{
	char *license;

	if (licenseidx) 
		license = (char *)sechdrs[licenseidx].sh_addr;
	else
		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;
	}
}

/* Allocate and load the module: note that size of section 0 is always
   zero, and we rely on this for optional sections. */
1085 1086 1087
static struct module *load_module(void *umod,
				  unsigned long len,
				  const char *uargs)
Linus Torvalds's avatar
Linus Torvalds committed
1088
{
1089 1090
	Elf_Ehdr *hdr;
	Elf_Shdr *sechdrs;
1091
	char *secstrings, *args;
1092
	unsigned int i, symindex, exportindex, strindex, setupindex, exindex,
1093
		modindex, obsparmindex, licenseindex, gplindex, vmagindex,
1094
		crcindex, gplcrcindex, versindex;
1095 1096
	long arglen;
	struct module *mod;
1097
	long err = 0;
1098
	void *ptr = NULL; /* Stops spurious gcc uninitialized warning */
Linus Torvalds's avatar
Linus Torvalds committed
1099

1100 1101 1102 1103
	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
1104

1105 1106 1107 1108 1109 1110 1111
	/* 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
1112 1113
	}

1114 1115 1116 1117 1118 1119 1120 1121
	/* 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
1122 1123
	}

1124 1125 1126 1127
	/* Convenience variables */
	sechdrs = (void *)hdr + hdr->e_shoff;
	secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;

1128 1129 1130
	/* And these should exist, but gcc whinges if we don't init them */
	symindex = strindex = 0;

1131
	for (i = 1; i < hdr->e_shnum; i++) {
1132 1133 1134 1135
		/* 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
1136
		/* Internal symbols and strings. */
1137 1138
		if (sechdrs[i].sh_type == SHT_SYMTAB) {
			symindex = i;
1139
			strindex = sechdrs[i].sh_link;
1140 1141 1142 1143 1144 1145
		}
#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
1146 1147
	}

Rusty Russell's avatar
Rusty Russell committed
1148 1149 1150 1151 1152 1153 1154 1155
#ifdef CONFIG_KALLSYMS
	/* Keep symbol and string tables for decoding later. */
	sechdrs[symindex].sh_flags |= SHF_ALLOC;
	sechdrs[strindex].sh_flags |= SHF_ALLOC;
#endif

	modindex = find_sec(hdr, sechdrs, secstrings,
			    ".gnu.linkonce.this_module");
1156 1157
	if (!modindex) {
		printk(KERN_WARNING "No module found in object\n");
1158 1159 1160
		err = -ENOEXEC;
		goto free_hdr;
	}
1161
	mod = (void *)sechdrs[modindex].sh_addr;
Linus Torvalds's avatar
Linus Torvalds committed
1162

Rusty Russell's avatar
Rusty Russell committed
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	/* 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");
	licenseindex = find_sec(hdr, sechdrs, secstrings, ".init.license");
	vmagindex = find_sec(hdr, sechdrs, secstrings, "__vermagic");
	versindex = find_sec(hdr, sechdrs, secstrings, "__versions");

1175 1176 1177 1178 1179 1180
	/* Check module struct version now, before we try to use module. */
	if (!check_modstruct_version(sechdrs, versindex, mod)) {
		err = -ENOEXEC;
		goto free_hdr;
	}

1181
	/* This is allowed: modprobe --force will invalidate it. */
Kai Germaschewski's avatar
Kai Germaschewski committed
1182 1183 1184 1185
	if (!vmagindex) {
		tainted |= TAINT_FORCED_MODULE;
		printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
		       mod->name);
1186
	} else if (!same_magic((char *)sechdrs[vmagindex].sh_addr, vermagic)) {
Kai Germaschewski's avatar
Kai Germaschewski committed
1187 1188 1189 1190 1191 1192
		printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
		       mod->name, (char*)sechdrs[vmagindex].sh_addr, vermagic);
		err = -ENOEXEC;
		goto free_hdr;
	}

1193
	/* Now copy in args */
1194 1195 1196
	arglen = strlen_user(uargs);
	if (!arglen) {
		err = -EFAULT;
1197
		goto free_hdr;
1198 1199
	}
	args = kmalloc(arglen, GFP_KERNEL);
1200
	if (!args) {
1201 1202
		err = -ENOMEM;
		goto free_hdr;
Linus Torvalds's avatar
Linus Torvalds committed
1203
	}
1204
	if (copy_from_user(args, uargs, arglen) != 0) {
1205 1206 1207
		err = -EFAULT;
		goto free_mod;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1208

1209 1210 1211 1212
	if (find_module(mod->name)) {
		err = -EEXIST;
		goto free_mod;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1213

1214
	mod->state = MODULE_STATE_COMING;
1215

1216 1217
	/* Allow arches to frob section contents and sizes.  */
	err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1218
	if (err < 0)
1219 1220
		goto free_mod;

1221
	/* Determine total sizes, and put offsets in sh_entsize.  For now
1222 1223 1224
	   this is done generically; there doesn't appear to be any
	   special cases for the architectures. */
	layout_sections(mod, hdr, sechdrs, secstrings);
1225 1226 1227 1228 1229 1230 1231 1232

	/* Do the allocs. */
	ptr = module_alloc(mod->core_size);
	if (!ptr) {
		err = -ENOMEM;
		goto free_mod;
	}
	memset(ptr, 0, mod->core_size);
1233 1234
	mod->module_core = ptr;

1235
	ptr = module_alloc(mod->init_size);
1236
	if (!ptr && mod->init_size) {
1237
		err = -ENOMEM;
1238
		goto free_core;
1239 1240
	}
	memset(ptr, 0, mod->init_size);
1241 1242
	mod->module_init = ptr;

1243 1244 1245 1246 1247 1248 1249
	/* Transfer each section which specifies SHF_ALLOC */
	for (i = 0; i < hdr->e_shnum; i++) {
		void *dest;

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

1250
		if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1251
			dest = mod->module_init
1252
				+ (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1253
		else
1254
			dest = mod->module_core + sechdrs[i].sh_entsize;
1255 1256 1257 1258 1259 1260

		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;
1261
	}
1262 1263
	/* Module has been moved. */
	mod = (void *)sechdrs[modindex].sh_addr;
Linus Torvalds's avatar
Linus Torvalds committed
1264

1265 1266 1267
	/* Now we've moved module, initialize linked lists, etc. */
	module_unload_init(mod);

1268 1269 1270
	/* Set up license info based on contents of section */
	set_license(mod, sechdrs, licenseindex);

1271
	/* Fix up syms, so that st_value is a pointer to location. */
1272
	err = simplify_symbols(sechdrs, symindex, strindex, versindex, mod);
1273 1274
	if (err < 0)
		goto cleanup;
Linus Torvalds's avatar
Linus Torvalds committed
1275

1276
	/* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
Rusty Russell's avatar
Rusty Russell committed
1277 1278
	mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
	mod->syms = (void *)sechdrs[exportindex].sh_addr;
Rusty Russell's avatar
Rusty Russell committed
1279
	if (crcindex)
Rusty Russell's avatar
Rusty Russell committed
1280 1281 1282
		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
1283
	if (gplcrcindex)
Rusty Russell's avatar
Rusty Russell committed
1284
		mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
Rusty Russell's avatar
Rusty Russell committed
1285 1286

#ifdef CONFIG_MODVERSIONS
1287 1288
	if ((mod->num_syms && !crcindex) || 
	    (mod->num_gpl_syms && !gplcrcindex)) {
Rusty Russell's avatar
Rusty Russell committed
1289 1290 1291 1292 1293
		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
1294

Rusty Russell's avatar
Rusty Russell committed
1295 1296 1297
  	/* 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
1298

Rusty Russell's avatar
Rusty Russell committed
1299
	/* Now do relocations. */
1300
	for (i = 1; i < hdr->e_shnum; i++) {
1301
		const char *strtab = (char *)sechdrs[strindex].sh_addr;
Rusty Russell's avatar
Rusty Russell committed
1302 1303 1304 1305 1306 1307
		if (sechdrs[i].sh_type == SHT_REL)
			err = apply_relocate(sechdrs, strtab, symindex, i,
					     mod);
		else if (sechdrs[i].sh_type == SHT_RELA)
			err = apply_relocate_add(sechdrs, strtab, symindex, i,
						 mod);
1308 1309
		if (err < 0)
			goto cleanup;
Linus Torvalds's avatar
Linus Torvalds committed
1310 1311
	}

1312
#ifdef CONFIG_KALLSYMS
1313
	mod->symtab = (void *)sechdrs[symindex].sh_addr;
1314
	mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1315
	mod->strtab = (void *)sechdrs[strindex].sh_addr;
1316
#endif
1317 1318 1319 1320
	err = module_finalize(hdr, sechdrs, mod);
	if (err < 0)
		goto cleanup;

1321
	mod->args = args;
1322 1323 1324
	if (obsparmindex) {
		err = obsolete_params(mod->name, mod->args,
				      (struct obsolete_modparm *)
1325
				      sechdrs[obsparmindex].sh_addr,
1326 1327 1328
				      sechdrs[obsparmindex].sh_size
				      / sizeof(struct obsolete_modparm),
				      sechdrs, symindex,
1329
				      (char *)sechdrs[strindex].sh_addr);
1330 1331 1332 1333
	} else {
		/* Size of section 0 is 0, so this works well if no params */
		err = parse_args(mod->name, mod->args,
				 (struct kernel_param *)
1334
				 sechdrs[setupindex].sh_addr,
1335 1336 1337 1338
				 sechdrs[setupindex].sh_size
				 / sizeof(struct kernel_param),
				 NULL);
	}
1339 1340
	if (err < 0)
		goto cleanup;
Linus Torvalds's avatar
Linus Torvalds committed
1341

1342 1343
	/* Get rid of temporary copy */
	vfree(hdr);
Linus Torvalds's avatar
Linus Torvalds committed
1344

1345 1346
	/* Done! */
	return mod;
Linus Torvalds's avatar
Linus Torvalds committed
1347

1348 1349 1350 1351 1352 1353
 cleanup:
	module_unload_free(mod);
	module_free(mod, mod->module_init);
 free_core:
	module_free(mod, mod->module_core);
 free_mod:
1354
	kfree(args);
1355 1356 1357 1358 1359
 free_hdr:
	vfree(hdr);
	if (err < 0) return ERR_PTR(err);
	else return ptr;
}
Ingo Molnar's avatar
Ingo Molnar committed
1360

1361 1362 1363 1364 1365
/* This is where the real work happens */
asmlinkage long
sys_init_module(void *umod,
		unsigned long len,
		const char *uargs)
Ingo Molnar's avatar
Ingo Molnar committed
1366
{
1367 1368
	struct module *mod;
	int ret;
Ingo Molnar's avatar
Ingo Molnar committed
1369

1370 1371 1372
	/* Must have permission */
	if (!capable(CAP_SYS_MODULE))
		return -EPERM;
Linus Torvalds's avatar
Linus Torvalds committed
1373

1374 1375 1376
	/* Only one module load at a time, please */
	if (down_interruptible(&module_mutex) != 0)
		return -EINTR;
Linus Torvalds's avatar
Linus Torvalds committed
1377

1378 1379 1380 1381 1382 1383
	/* 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
1384

1385 1386 1387 1388 1389 1390 1391 1392
	/* 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);

1393 1394
	/* Now sew it into the lists.  They won't access us, since
           strong_try_module_get() will fail. */
1395
	spin_lock_irq(&modlist_lock);
1396
	list_add(&mod->list, &modules);
1397
	spin_unlock_irq(&modlist_lock);
1398 1399 1400

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

1402 1403 1404 1405
	down(&notify_mutex);
	notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
	up(&notify_mutex);

1406
	/* Start the module */
1407
	ret = mod->init();
1408 1409 1410
	if (ret < 0) {
		/* Init routine failed: abort.  Try to protect us from
                   buggy refcounters. */
1411
		mod->state = MODULE_STATE_GOING;
1412
		synchronize_kernel();
1413
		if (mod->unsafe)
1414 1415
			printk(KERN_ERR "%s: module is now stuck!\n",
			       mod->name);
1416 1417
		else {
			down(&module_mutex);
1418
			free_module(mod);
1419
			up(&module_mutex);
1420
		}
1421 1422
		return ret;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1423

1424
	/* Now it's a first class citizen! */
Rusty Russell's avatar
Rusty Russell committed
1425
	down(&module_mutex);
1426
	mod->state = MODULE_STATE_LIVE;
1427 1428
	module_free(mod, mod->module_init);
	mod->module_init = NULL;
1429
	mod->init_size = 0;
Rusty Russell's avatar
Rusty Russell committed
1430
	up(&module_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1431

1432
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1433 1434
}

1435
static inline int within(unsigned long addr, void *start, unsigned long size)
1436
{
1437
	return ((void *)addr >= start && (void *)addr < start + size);
1438 1439
}

1440
#ifdef CONFIG_KALLSYMS
1441 1442 1443 1444 1445
static const char *get_ksymbol(struct module *mod,
			       unsigned long addr,
			       unsigned long *size,
			       unsigned long *offset)
{
1446 1447 1448 1449
	unsigned int i, best = 0;
	unsigned long nextval;

	/* At worse, next value is at end of module */
1450
	if (within(addr, mod->module_init, mod->init_size))
1451
		nextval = (unsigned long)mod->module_init + mod->init_size;
1452
	else 
1453
		nextval = (unsigned long)mod->module_core + mod->core_size;
1454 1455 1456

	/* Scan for closest preceeding symbol, and next symbol. (ELF
           starts real symbols at 1). */
1457
	for (i = 1; i < mod->num_symtab; i++) {
1458 1459 1460 1461 1462 1463 1464
		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
1465 1466
		    && mod->symtab[i].st_value < nextval)
			nextval = mod->symtab[i].st_value;
1467 1468 1469 1470 1471
	}

	if (!best)
		return NULL;

1472
	*size = nextval - mod->symtab[best].st_value;
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
	*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) {
1488 1489
		if (within(addr, mod->module_init, mod->init_size)
		    || within(addr, mod->module_core, mod->core_size)) {
1490 1491 1492 1493 1494 1495 1496 1497 1498
			*modname = mod->name;
			return get_ksymbol(mod, addr, size, offset);
		}
	}
	return NULL;
}
#endif /* CONFIG_KALLSYMS */

/* Called by the /proc file system to return a list of modules. */
1499
static void *m_start(struct seq_file *m, loff_t *pos)
Linus Torvalds's avatar
Linus Torvalds committed
1500
{
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
	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
1512 1513
}

1514
static void *m_next(struct seq_file *m, void *p, loff_t *pos)
Linus Torvalds's avatar
Linus Torvalds committed
1515
{
1516 1517 1518 1519 1520
	struct list_head *i = p;
	(*pos)++;
	if (i->next == &modules)
		return NULL;
	return i->next;
Linus Torvalds's avatar
Linus Torvalds committed
1521 1522
}

1523
static void m_stop(struct seq_file *m, void *p)
Ingo Molnar's avatar
Ingo Molnar committed
1524
{
1525
	up(&module_mutex);
Ingo Molnar's avatar
Ingo Molnar committed
1526 1527
}

1528
static int m_show(struct seq_file *m, void *p)
Ingo Molnar's avatar
Ingo Molnar committed
1529
{
1530 1531 1532 1533
	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);
1534 1535 1536 1537 1538 1539 1540 1541 1542

	/* 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
1543
	seq_printf(m, "\n");
1544
	return 0;
Ingo Molnar's avatar
Ingo Molnar committed
1545
}
Rusty Russell's avatar
Rusty Russell committed
1546

1547
/* Format: modulename size refcount deps address
Rusty Russell's avatar
Rusty Russell committed
1548 1549 1550 1551

   Where refcount is a number or -, and deps is a comma-separated list
   of depends or -.
*/
1552 1553 1554 1555 1556 1557
struct seq_operations modules_op = {
	.start	= m_start,
	.next	= m_next,
	.stop	= m_stop,
	.show	= m_show
};
Ingo Molnar's avatar
Ingo Molnar committed
1558

1559 1560 1561 1562 1563
/* 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
1564
	struct module *mod;
1565 1566

	spin_lock_irqsave(&modlist_lock, flags);
Rusty Russell's avatar
Rusty Russell committed
1567 1568
	list_for_each_entry(mod, &modules, list) {
		if (mod->num_exentries == 0)
1569 1570
			continue;
				
Rusty Russell's avatar
Rusty Russell committed
1571 1572 1573
		e = search_extable(mod->extable,
				   mod->extable + mod->num_exentries - 1,
				   addr);
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
		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;
}

1584
/* Is this a valid kernel address?  We don't grab the lock: we are oopsing. */
1585
struct module *module_text_address(unsigned long addr)
1586 1587 1588 1589 1590 1591
{
	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))
1592 1593
			return mod;
	return NULL;
1594 1595
}

1596 1597 1598 1599 1600
#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