ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / arch / sparc / kernel / module.c
blob8172c18d844fadcc56e364b331cefe37f6c08701
1 /* Kernel module help for sparc64.
3 * Copyright (C) 2001 Rusty Russell.
4 * Copyright (C) 2002 David S. Miller.
5 */
7 #include <linux/moduleloader.h>
8 #include <linux/kernel.h>
9 #include <linux/elf.h>
10 #include <linux/vmalloc.h>
11 #include <linux/fs.h>
12 #include <linux/gfp.h>
13 #include <linux/string.h>
14 #include <linux/ctype.h>
15 #include <linux/mm.h>
17 #include <asm/processor.h>
18 #include <asm/spitfire.h>
20 #include "entry.h"
22 #ifdef CONFIG_SPARC64
24 #include <linux/jump_label.h>
26 static void *module_map(unsigned long size)
28 if (PAGE_ALIGN(size) > MODULES_LEN)
29 return NULL;
30 return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
31 GFP_KERNEL, PAGE_KERNEL, -1,
32 __builtin_return_address(0));
35 static char *dot2underscore(char *name)
37 return name;
39 #else
40 static void *module_map(unsigned long size)
42 return vmalloc(size);
45 /* Replace references to .func with _Func */
46 static char *dot2underscore(char *name)
48 if (name[0] == '.') {
49 name[0] = '_';
50 name[1] = toupper(name[1]);
52 return name;
54 #endif /* CONFIG_SPARC64 */
56 void *module_alloc(unsigned long size)
58 void *ret;
60 /* We handle the zero case fine, unlike vmalloc */
61 if (size == 0)
62 return NULL;
64 ret = module_map(size);
65 if (!ret)
66 ret = ERR_PTR(-ENOMEM);
67 else
68 memset(ret, 0, size);
70 return ret;
73 /* Free memory returned from module_core_alloc/module_init_alloc */
74 void module_free(struct module *mod, void *module_region)
76 vfree(module_region);
79 /* Make generic code ignore STT_REGISTER dummy undefined symbols. */
80 int module_frob_arch_sections(Elf_Ehdr *hdr,
81 Elf_Shdr *sechdrs,
82 char *secstrings,
83 struct module *mod)
85 unsigned int symidx;
86 Elf_Sym *sym;
87 char *strtab;
88 int i;
90 for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) {
91 if (symidx == hdr->e_shnum-1) {
92 printk("%s: no symtab found.\n", mod->name);
93 return -ENOEXEC;
96 sym = (Elf_Sym *)sechdrs[symidx].sh_addr;
97 strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr;
99 for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) {
100 if (sym[i].st_shndx == SHN_UNDEF) {
101 if (ELF_ST_TYPE(sym[i].st_info) == STT_REGISTER) {
102 sym[i].st_shndx = SHN_ABS;
103 } else {
104 char *name = strtab + sym[i].st_name;
105 dot2underscore(name);
109 return 0;
112 int apply_relocate(Elf_Shdr *sechdrs,
113 const char *strtab,
114 unsigned int symindex,
115 unsigned int relsec,
116 struct module *me)
118 printk(KERN_ERR "module %s: non-ADD RELOCATION unsupported\n",
119 me->name);
120 return -ENOEXEC;
123 int apply_relocate_add(Elf_Shdr *sechdrs,
124 const char *strtab,
125 unsigned int symindex,
126 unsigned int relsec,
127 struct module *me)
129 unsigned int i;
130 Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
131 Elf_Sym *sym;
132 u8 *location;
133 u32 *loc32;
135 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
136 Elf_Addr v;
138 /* This is where to make the change */
139 location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
140 + rel[i].r_offset;
141 loc32 = (u32 *) location;
143 #ifdef CONFIG_SPARC64
144 BUG_ON(((u64)location >> (u64)32) != (u64)0);
145 #endif /* CONFIG_SPARC64 */
147 /* This is the symbol it is referring to. Note that all
148 undefined symbols have been resolved. */
149 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
150 + ELF_R_SYM(rel[i].r_info);
151 v = sym->st_value + rel[i].r_addend;
153 switch (ELF_R_TYPE(rel[i].r_info) & 0xff) {
154 #ifdef CONFIG_SPARC64
155 case R_SPARC_64:
156 location[0] = v >> 56;
157 location[1] = v >> 48;
158 location[2] = v >> 40;
159 location[3] = v >> 32;
160 location[4] = v >> 24;
161 location[5] = v >> 16;
162 location[6] = v >> 8;
163 location[7] = v >> 0;
164 break;
166 case R_SPARC_DISP32:
167 v -= (Elf_Addr) location;
168 *loc32 = v;
169 break;
171 case R_SPARC_WDISP19:
172 v -= (Elf_Addr) location;
173 *loc32 = (*loc32 & ~0x7ffff) |
174 ((v >> 2) & 0x7ffff);
175 break;
177 case R_SPARC_OLO10:
178 *loc32 = (*loc32 & ~0x1fff) |
179 (((v & 0x3ff) +
180 (ELF_R_TYPE(rel[i].r_info) >> 8))
181 & 0x1fff);
182 break;
183 #endif /* CONFIG_SPARC64 */
185 case R_SPARC_32:
186 case R_SPARC_UA32:
187 location[0] = v >> 24;
188 location[1] = v >> 16;
189 location[2] = v >> 8;
190 location[3] = v >> 0;
191 break;
193 case R_SPARC_WDISP30:
194 v -= (Elf_Addr) location;
195 *loc32 = (*loc32 & ~0x3fffffff) |
196 ((v >> 2) & 0x3fffffff);
197 break;
199 case R_SPARC_WDISP22:
200 v -= (Elf_Addr) location;
201 *loc32 = (*loc32 & ~0x3fffff) |
202 ((v >> 2) & 0x3fffff);
203 break;
205 case R_SPARC_LO10:
206 *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
207 break;
209 case R_SPARC_HI22:
210 *loc32 = (*loc32 & ~0x3fffff) |
211 ((v >> 10) & 0x3fffff);
212 break;
214 default:
215 printk(KERN_ERR "module %s: Unknown relocation: %x\n",
216 me->name,
217 (int) (ELF_R_TYPE(rel[i].r_info) & 0xff));
218 return -ENOEXEC;
221 return 0;
224 #ifdef CONFIG_SPARC64
225 static void do_patch_sections(const Elf_Ehdr *hdr,
226 const Elf_Shdr *sechdrs)
228 const Elf_Shdr *s, *sun4v_1insn = NULL, *sun4v_2insn = NULL;
229 char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
231 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
232 if (!strcmp(".sun4v_1insn_patch", secstrings + s->sh_name))
233 sun4v_1insn = s;
234 if (!strcmp(".sun4v_2insn_patch", secstrings + s->sh_name))
235 sun4v_2insn = s;
238 if (sun4v_1insn && tlb_type == hypervisor) {
239 void *p = (void *) sun4v_1insn->sh_addr;
240 sun4v_patch_1insn_range(p, p + sun4v_1insn->sh_size);
242 if (sun4v_2insn && tlb_type == hypervisor) {
243 void *p = (void *) sun4v_2insn->sh_addr;
244 sun4v_patch_2insn_range(p, p + sun4v_2insn->sh_size);
248 int module_finalize(const Elf_Ehdr *hdr,
249 const Elf_Shdr *sechdrs,
250 struct module *me)
252 /* make jump label nops */
253 jump_label_apply_nops(me);
255 do_patch_sections(hdr, sechdrs);
257 /* Cheetah's I-cache is fully coherent. */
258 if (tlb_type == spitfire) {
259 unsigned long va;
261 flushw_all();
262 for (va = 0; va < (PAGE_SIZE << 1); va += 32)
263 spitfire_put_icache_tag(va, 0x0);
264 __asm__ __volatile__("flush %g6");
267 return 0;
269 #else
270 int module_finalize(const Elf_Ehdr *hdr,
271 const Elf_Shdr *sechdrs,
272 struct module *me)
274 return 0;
276 #endif /* CONFIG_SPARC64 */
278 void module_arch_cleanup(struct module *mod)