module.c 5.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/* Kernel module help for sparc64.
 *
 * Copyright (C) 2001 Rusty Russell.
 * Copyright (C) 2002 David S. Miller.
 */

#include <linux/moduleloader.h>
#include <linux/kernel.h>
#include <linux/elf.h>
#include <linux/vmalloc.h>
#include <linux/fs.h>
12
#include <linux/gfp.h>
L
Linus Torvalds 已提交
13
#include <linux/string.h>
S
Sam Ravnborg 已提交
14
#include <linux/ctype.h>
L
Linus Torvalds 已提交
15 16 17 18 19
#include <linux/mm.h>

#include <asm/processor.h>
#include <asm/spitfire.h>

20 21
#include "entry.h"

22
#ifdef CONFIG_SPARC64
23 24 25

#include <linux/jump_label.h>

L
Linus Torvalds 已提交
26 27
static void *module_map(unsigned long size)
{
28
	if (PAGE_ALIGN(size) > MODULES_LEN)
L
Linus Torvalds 已提交
29
		return NULL;
30 31 32
	return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
				GFP_KERNEL, PAGE_KERNEL, -1,
				__builtin_return_address(0));
L
Linus Torvalds 已提交
33 34
}

35 36 37 38
static char *dot2underscore(char *name)
{
	return name;
}
S
Sam Ravnborg 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#else
static void *module_map(unsigned long size)
{
	return vmalloc(size);
}

/* Replace references to .func with _Func */
static char *dot2underscore(char *name)
{
	if (name[0] == '.') {
		name[0] = '_';
                name[1] = toupper(name[1]);
	}
	return name;
}
54 55
#endif /* CONFIG_SPARC64 */

L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
void *module_alloc(unsigned long size)
{
	void *ret;

	/* We handle the zero case fine, unlike vmalloc */
	if (size == 0)
		return NULL;

	ret = module_map(size);
	if (!ret)
		ret = ERR_PTR(-ENOMEM);
	else
		memset(ret, 0, size);

	return ret;
}

/* Make generic code ignore STT_REGISTER dummy undefined symbols.  */
int module_frob_arch_sections(Elf_Ehdr *hdr,
			      Elf_Shdr *sechdrs,
			      char *secstrings,
			      struct module *mod)
{
	unsigned int symidx;
80
	Elf_Sym *sym;
81
	char *strtab;
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89
	int i;

	for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) {
		if (symidx == hdr->e_shnum-1) {
			printk("%s: no symtab found.\n", mod->name);
			return -ENOEXEC;
		}
	}
90
	sym = (Elf_Sym *)sechdrs[symidx].sh_addr;
L
Linus Torvalds 已提交
91 92 93
	strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr;

	for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) {
94 95 96 97 98 99 100 101
		if (sym[i].st_shndx == SHN_UNDEF) {
			if (ELF_ST_TYPE(sym[i].st_info) == STT_REGISTER) {
				sym[i].st_shndx = SHN_ABS;
			} else {
				char *name = strtab + sym[i].st_name;
				dot2underscore(name);
			}
		}
L
Linus Torvalds 已提交
102 103 104 105
	}
	return 0;
}

106
int apply_relocate_add(Elf_Shdr *sechdrs,
L
Linus Torvalds 已提交
107 108 109 110 111 112
		       const char *strtab,
		       unsigned int symindex,
		       unsigned int relsec,
		       struct module *me)
{
	unsigned int i;
113 114
	Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
	Elf_Sym *sym;
L
Linus Torvalds 已提交
115 116 117 118
	u8 *location;
	u32 *loc32;

	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
119
		Elf_Addr v;
L
Linus Torvalds 已提交
120 121 122 123 124 125

		/* This is where to make the change */
		location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
			+ rel[i].r_offset;
		loc32 = (u32 *) location;

126
#ifdef CONFIG_SPARC64
L
Linus Torvalds 已提交
127
		BUG_ON(((u64)location >> (u64)32) != (u64)0);
128
#endif /* CONFIG_SPARC64 */
L
Linus Torvalds 已提交
129 130 131

		/* This is the symbol it is referring to.  Note that all
		   undefined symbols have been resolved.  */
132 133
		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
			+ ELF_R_SYM(rel[i].r_info);
L
Linus Torvalds 已提交
134 135
		v = sym->st_value + rel[i].r_addend;

136
		switch (ELF_R_TYPE(rel[i].r_info) & 0xff) {
137
#ifdef CONFIG_SPARC64
L
Linus Torvalds 已提交
138 139 140 141 142 143 144 145 146 147 148
		case R_SPARC_64:
			location[0] = v >> 56;
			location[1] = v >> 48;
			location[2] = v >> 40;
			location[3] = v >> 32;
			location[4] = v >> 24;
			location[5] = v >> 16;
			location[6] = v >>  8;
			location[7] = v >>  0;
			break;

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		case R_SPARC_DISP32:
			v -= (Elf_Addr) location;
			*loc32 = v;
			break;

		case R_SPARC_WDISP19:
			v -= (Elf_Addr) location;
			*loc32 = (*loc32 & ~0x7ffff) |
				((v >> 2) & 0x7ffff);
			break;

		case R_SPARC_OLO10:
			*loc32 = (*loc32 & ~0x1fff) |
				(((v & 0x3ff) +
				  (ELF_R_TYPE(rel[i].r_info) >> 8))
				 & 0x1fff);
			break;
#endif /* CONFIG_SPARC64 */

L
Linus Torvalds 已提交
168
		case R_SPARC_32:
S
Sam Ravnborg 已提交
169
		case R_SPARC_UA32:
L
Linus Torvalds 已提交
170 171 172 173 174 175 176
			location[0] = v >> 24;
			location[1] = v >> 16;
			location[2] = v >>  8;
			location[3] = v >>  0;
			break;

		case R_SPARC_WDISP30:
177
			v -= (Elf_Addr) location;
L
Linus Torvalds 已提交
178 179 180 181 182
			*loc32 = (*loc32 & ~0x3fffffff) |
				((v >> 2) & 0x3fffffff);
			break;

		case R_SPARC_WDISP22:
183
			v -= (Elf_Addr) location;
L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
			*loc32 = (*loc32 & ~0x3fffff) |
				((v >> 2) & 0x3fffff);
			break;

		case R_SPARC_LO10:
			*loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
			break;

		case R_SPARC_HI22:
			*loc32 = (*loc32 & ~0x3fffff) |
				((v >> 10) & 0x3fffff);
			break;

		default:
			printk(KERN_ERR "module %s: Unknown relocation: %x\n",
			       me->name,
200
			       (int) (ELF_R_TYPE(rel[i].r_info) & 0xff));
L
Linus Torvalds 已提交
201
			return -ENOEXEC;
202
		}
L
Linus Torvalds 已提交
203 204 205 206
	}
	return 0;
}

207
#ifdef CONFIG_SPARC64
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
static void do_patch_sections(const Elf_Ehdr *hdr,
			      const Elf_Shdr *sechdrs)
{
	const Elf_Shdr *s, *sun4v_1insn = NULL, *sun4v_2insn = NULL;
	char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;

	for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
		if (!strcmp(".sun4v_1insn_patch", secstrings + s->sh_name))
			sun4v_1insn = s;
		if (!strcmp(".sun4v_2insn_patch", secstrings + s->sh_name))
			sun4v_2insn = s;
	}

	if (sun4v_1insn && tlb_type == hypervisor) {
		void *p = (void *) sun4v_1insn->sh_addr;
		sun4v_patch_1insn_range(p, p + sun4v_1insn->sh_size);
	}
	if (sun4v_2insn && tlb_type == hypervisor) {
		void *p = (void *) sun4v_2insn->sh_addr;
		sun4v_patch_2insn_range(p, p + sun4v_2insn->sh_size);
	}
}

L
Linus Torvalds 已提交
231 232 233 234
int module_finalize(const Elf_Ehdr *hdr,
		    const Elf_Shdr *sechdrs,
		    struct module *me)
{
235 236 237
	/* make jump label nops */
	jump_label_apply_nops(me);

238 239
	do_patch_sections(hdr, sechdrs);

L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247 248 249 250 251
	/* Cheetah's I-cache is fully coherent.  */
	if (tlb_type == spitfire) {
		unsigned long va;

		flushw_all();
		for (va =  0; va < (PAGE_SIZE << 1); va += 32)
			spitfire_put_icache_tag(va, 0x0);
		__asm__ __volatile__("flush %g6");
	}

	return 0;
}
252
#endif /* CONFIG_SPARC64 */