Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / blackfin / kernel / module.c
blob15af5768c40393ef0204aab271f2695d20cb7c97
1 /*
2 * Copyright 2004-2009 Analog Devices Inc.
4 * Licensed under the GPL-2 or later
5 */
7 #include <linux/moduleloader.h>
8 #include <linux/elf.h>
9 #include <linux/vmalloc.h>
10 #include <linux/fs.h>
11 #include <linux/string.h>
12 #include <linux/kernel.h>
13 #include <asm/dma.h>
14 #include <asm/cacheflush.h>
15 #include <linux/uaccess.h>
17 #define mod_err(mod, fmt, ...) \
18 pr_err("module %s: " fmt, (mod)->name, ##__VA_ARGS__)
19 #define mod_debug(mod, fmt, ...) \
20 pr_debug("module %s: " fmt, (mod)->name, ##__VA_ARGS__)
22 /* Transfer the section to the L1 memory */
23 int
24 module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
25 char *secstrings, struct module *mod)
28 * XXX: sechdrs are vmalloced in kernel/module.c
29 * and would be vfreed just after module is loaded,
30 * so we hack to keep the only information we needed
31 * in mod->arch to correctly free L1 I/D sram later.
32 * NOTE: this breaks the semantic of mod->arch structure.
34 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
35 void *dest;
37 for (s = sechdrs; s < sechdrs_end; ++s) {
38 const char *shname = secstrings + s->sh_name;
40 if (s->sh_size == 0)
41 continue;
43 if (!strcmp(".l1.text", shname) ||
44 (!strcmp(".text", shname) &&
45 (hdr->e_flags & EF_BFIN_CODE_IN_L1))) {
47 dest = l1_inst_sram_alloc(s->sh_size);
48 mod->arch.text_l1 = dest;
49 if (dest == NULL) {
50 mod_err(mod, "L1 inst memory allocation failed\n");
51 return -1;
53 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
55 } else if (!strcmp(".l1.data", shname) ||
56 (!strcmp(".data", shname) &&
57 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
59 dest = l1_data_sram_alloc(s->sh_size);
60 mod->arch.data_a_l1 = dest;
61 if (dest == NULL) {
62 mod_err(mod, "L1 data memory allocation failed\n");
63 return -1;
65 memcpy(dest, (void *)s->sh_addr, s->sh_size);
67 } else if (!strcmp(".l1.bss", shname) ||
68 (!strcmp(".bss", shname) &&
69 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
71 dest = l1_data_sram_zalloc(s->sh_size);
72 mod->arch.bss_a_l1 = dest;
73 if (dest == NULL) {
74 mod_err(mod, "L1 data memory allocation failed\n");
75 return -1;
78 } else if (!strcmp(".l1.data.B", shname)) {
80 dest = l1_data_B_sram_alloc(s->sh_size);
81 mod->arch.data_b_l1 = dest;
82 if (dest == NULL) {
83 mod_err(mod, "L1 data memory allocation failed\n");
84 return -1;
86 memcpy(dest, (void *)s->sh_addr, s->sh_size);
88 } else if (!strcmp(".l1.bss.B", shname)) {
90 dest = l1_data_B_sram_alloc(s->sh_size);
91 mod->arch.bss_b_l1 = dest;
92 if (dest == NULL) {
93 mod_err(mod, "L1 data memory allocation failed\n");
94 return -1;
96 memset(dest, 0, s->sh_size);
98 } else if (!strcmp(".l2.text", shname) ||
99 (!strcmp(".text", shname) &&
100 (hdr->e_flags & EF_BFIN_CODE_IN_L2))) {
102 dest = l2_sram_alloc(s->sh_size);
103 mod->arch.text_l2 = dest;
104 if (dest == NULL) {
105 mod_err(mod, "L2 SRAM allocation failed\n");
106 return -1;
108 memcpy(dest, (void *)s->sh_addr, s->sh_size);
110 } else if (!strcmp(".l2.data", shname) ||
111 (!strcmp(".data", shname) &&
112 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
114 dest = l2_sram_alloc(s->sh_size);
115 mod->arch.data_l2 = dest;
116 if (dest == NULL) {
117 mod_err(mod, "L2 SRAM allocation failed\n");
118 return -1;
120 memcpy(dest, (void *)s->sh_addr, s->sh_size);
122 } else if (!strcmp(".l2.bss", shname) ||
123 (!strcmp(".bss", shname) &&
124 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
126 dest = l2_sram_zalloc(s->sh_size);
127 mod->arch.bss_l2 = dest;
128 if (dest == NULL) {
129 mod_err(mod, "L2 SRAM allocation failed\n");
130 return -1;
133 } else
134 continue;
136 s->sh_flags &= ~SHF_ALLOC;
137 s->sh_addr = (unsigned long)dest;
140 return 0;
143 /*************************************************************************/
144 /* FUNCTION : apply_relocate_add */
145 /* ABSTRACT : Blackfin specific relocation handling for the loadable */
146 /* modules. Modules are expected to be .o files. */
147 /* Arithmetic relocations are handled. */
148 /* We do not expect LSETUP to be split and hence is not */
149 /* handled. */
150 /* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
151 /* gas does not generate it. */
152 /*************************************************************************/
154 apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
155 unsigned int symindex, unsigned int relsec,
156 struct module *mod)
158 unsigned int i;
159 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
160 Elf32_Sym *sym;
161 unsigned long location, value, size;
163 mod_debug(mod, "applying relocate section %u to %u\n",
164 relsec, sechdrs[relsec].sh_info);
166 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
167 /* This is where to make the change */
168 location = sechdrs[sechdrs[relsec].sh_info].sh_addr +
169 rel[i].r_offset;
171 /* This is the symbol it is referring to. Note that all
172 undefined symbols have been resolved. */
173 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
174 + ELF32_R_SYM(rel[i].r_info);
175 value = sym->st_value;
176 value += rel[i].r_addend;
178 #ifdef CONFIG_SMP
179 if (location >= COREB_L1_DATA_A_START) {
180 mod_err(mod, "cannot relocate in L1: %u (SMP kernel)\n",
181 ELF32_R_TYPE(rel[i].r_info));
182 return -ENOEXEC;
184 #endif
186 mod_debug(mod, "location is %lx, value is %lx type is %d\n",
187 location, value, ELF32_R_TYPE(rel[i].r_info));
189 switch (ELF32_R_TYPE(rel[i].r_info)) {
191 case R_BFIN_HUIMM16:
192 value >>= 16;
193 case R_BFIN_LUIMM16:
194 case R_BFIN_RIMM16:
195 size = 2;
196 break;
197 case R_BFIN_BYTE4_DATA:
198 size = 4;
199 break;
201 case R_BFIN_PCREL24:
202 case R_BFIN_PCREL24_JUMP_L:
203 case R_BFIN_PCREL12_JUMP:
204 case R_BFIN_PCREL12_JUMP_S:
205 case R_BFIN_PCREL10:
206 mod_err(mod, "unsupported relocation: %u (no -mlong-calls?)\n",
207 ELF32_R_TYPE(rel[i].r_info));
208 return -ENOEXEC;
210 default:
211 mod_err(mod, "unknown relocation: %u\n",
212 ELF32_R_TYPE(rel[i].r_info));
213 return -ENOEXEC;
216 switch (bfin_mem_access_type(location, size)) {
217 case BFIN_MEM_ACCESS_CORE:
218 case BFIN_MEM_ACCESS_CORE_ONLY:
219 memcpy((void *)location, &value, size);
220 break;
221 case BFIN_MEM_ACCESS_DMA:
222 dma_memcpy((void *)location, &value, size);
223 break;
224 case BFIN_MEM_ACCESS_ITEST:
225 isram_memcpy((void *)location, &value, size);
226 break;
227 default:
228 mod_err(mod, "invalid relocation for %#lx\n", location);
229 return -ENOEXEC;
233 return 0;
237 module_finalize(const Elf_Ehdr * hdr,
238 const Elf_Shdr * sechdrs, struct module *mod)
240 unsigned int i, strindex = 0, symindex = 0;
241 char *secstrings;
242 long err = 0;
244 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
246 for (i = 1; i < hdr->e_shnum; i++) {
247 /* Internal symbols and strings. */
248 if (sechdrs[i].sh_type == SHT_SYMTAB) {
249 symindex = i;
250 strindex = sechdrs[i].sh_link;
254 for (i = 1; i < hdr->e_shnum; i++) {
255 const char *strtab = (char *)sechdrs[strindex].sh_addr;
256 unsigned int info = sechdrs[i].sh_info;
257 const char *shname = secstrings + sechdrs[i].sh_name;
259 /* Not a valid relocation section? */
260 if (info >= hdr->e_shnum)
261 continue;
263 /* Only support RELA relocation types */
264 if (sechdrs[i].sh_type != SHT_RELA)
265 continue;
267 if (!strcmp(".rela.l2.text", shname) ||
268 !strcmp(".rela.l1.text", shname) ||
269 (!strcmp(".rela.text", shname) &&
270 (hdr->e_flags & (EF_BFIN_CODE_IN_L1 | EF_BFIN_CODE_IN_L2)))) {
272 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
273 symindex, i, mod);
274 if (err < 0)
275 return -ENOEXEC;
279 return 0;
282 void module_arch_cleanup(struct module *mod)
284 l1_inst_sram_free(mod->arch.text_l1);
285 l1_data_A_sram_free(mod->arch.data_a_l1);
286 l1_data_A_sram_free(mod->arch.bss_a_l1);
287 l1_data_B_sram_free(mod->arch.data_b_l1);
288 l1_data_B_sram_free(mod->arch.bss_b_l1);
289 l2_sram_free(mod->arch.text_l2);
290 l2_sram_free(mod->arch.data_l2);
291 l2_sram_free(mod->arch.bss_l2);