Commit | Line | Data |
---|---|---|
1394f032 BW |
1 | /* |
2 | * File: arch/blackfin/kernel/module.c | |
3 | * Based on: | |
4 | * Author: | |
5 | * | |
6 | * Created: | |
7 | * Description: | |
8 | * | |
9 | * Modified: | |
10 | * Copyright 2004-2006 Analog Devices Inc. | |
11 | * | |
12 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2 of the License, or | |
17 | * (at your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, see the file COPYING, or write | |
26 | * to the Free Software Foundation, Inc., | |
27 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
28 | */ | |
29 | ||
30 | ||
31 | #include <linux/moduleloader.h> | |
32 | #include <linux/elf.h> | |
33 | #include <linux/vmalloc.h> | |
34 | #include <linux/fs.h> | |
35 | #include <linux/string.h> | |
36 | #include <linux/kernel.h> | |
37 | #include <asm/dma.h> | |
38 | #include <asm/cacheflush.h> | |
39 | ||
1394f032 BW |
40 | void *module_alloc(unsigned long size) |
41 | { | |
42 | if (size == 0) | |
43 | return NULL; | |
44 | return vmalloc(size); | |
45 | } | |
46 | ||
47 | /* Free memory returned from module_alloc */ | |
48 | void module_free(struct module *mod, void *module_region) | |
49 | { | |
50 | vfree(module_region); | |
51 | } | |
52 | ||
53 | /* Transfer the section to the L1 memory */ | |
54 | int | |
55 | module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs, | |
56 | char *secstrings, struct module *mod) | |
57 | { | |
96a87e2f MF |
58 | /* |
59 | * XXX: sechdrs are vmalloced in kernel/module.c | |
60 | * and would be vfreed just after module is loaded, | |
61 | * so we hack to keep the only information we needed | |
62 | * in mod->arch to correctly free L1 I/D sram later. | |
63 | * NOTE: this breaks the semantic of mod->arch structure. | |
64 | */ | |
1394f032 BW |
65 | Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum; |
66 | void *dest = NULL; | |
67 | ||
68 | for (s = sechdrs; s < sechdrs_end; ++s) { | |
69 | if ((strcmp(".l1.text", secstrings + s->sh_name) == 0) || | |
1f83b8f1 | 70 | ((strcmp(".text", secstrings + s->sh_name) == 0) && |
262c3825 | 71 | (hdr->e_flags & EF_BFIN_CODE_IN_L1) && (s->sh_size > 0))) { |
1394f032 | 72 | dest = l1_inst_sram_alloc(s->sh_size); |
96a87e2f | 73 | mod->arch.text_l1 = dest; |
1394f032 BW |
74 | if (dest == NULL) { |
75 | printk(KERN_ERR | |
76 | "module %s: L1 instruction memory allocation failed\n", | |
77 | mod->name); | |
78 | return -1; | |
79 | } | |
80 | dma_memcpy(dest, (void *)s->sh_addr, s->sh_size); | |
81 | s->sh_flags &= ~SHF_ALLOC; | |
82 | s->sh_addr = (unsigned long)dest; | |
83 | } | |
1f83b8f1 MF |
84 | if ((strcmp(".l1.data", secstrings + s->sh_name) == 0) || |
85 | ((strcmp(".data", secstrings + s->sh_name) == 0) && | |
262c3825 | 86 | (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) { |
1394f032 | 87 | dest = l1_data_sram_alloc(s->sh_size); |
96a87e2f | 88 | mod->arch.data_a_l1 = dest; |
1394f032 BW |
89 | if (dest == NULL) { |
90 | printk(KERN_ERR | |
91 | "module %s: L1 data memory allocation failed\n", | |
92 | mod->name); | |
93 | return -1; | |
94 | } | |
95 | memcpy(dest, (void *)s->sh_addr, s->sh_size); | |
96 | s->sh_flags &= ~SHF_ALLOC; | |
97 | s->sh_addr = (unsigned long)dest; | |
98 | } | |
99 | if (strcmp(".l1.bss", secstrings + s->sh_name) == 0 || | |
1f83b8f1 | 100 | ((strcmp(".bss", secstrings + s->sh_name) == 0) && |
262c3825 | 101 | (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) { |
1394f032 | 102 | dest = l1_data_sram_alloc(s->sh_size); |
96a87e2f | 103 | mod->arch.bss_a_l1 = dest; |
1394f032 BW |
104 | if (dest == NULL) { |
105 | printk(KERN_ERR | |
106 | "module %s: L1 data memory allocation failed\n", | |
107 | mod->name); | |
108 | return -1; | |
109 | } | |
110 | memset(dest, 0, s->sh_size); | |
111 | s->sh_flags &= ~SHF_ALLOC; | |
112 | s->sh_addr = (unsigned long)dest; | |
113 | } | |
114 | if (strcmp(".l1.data.B", secstrings + s->sh_name) == 0) { | |
1394f032 | 115 | dest = l1_data_B_sram_alloc(s->sh_size); |
96a87e2f | 116 | mod->arch.data_b_l1 = dest; |
1394f032 BW |
117 | if (dest == NULL) { |
118 | printk(KERN_ERR | |
119 | "module %s: L1 data memory allocation failed\n", | |
120 | mod->name); | |
121 | return -1; | |
122 | } | |
123 | memcpy(dest, (void *)s->sh_addr, s->sh_size); | |
124 | s->sh_flags &= ~SHF_ALLOC; | |
125 | s->sh_addr = (unsigned long)dest; | |
126 | } | |
127 | if (strcmp(".l1.bss.B", secstrings + s->sh_name) == 0) { | |
1394f032 | 128 | dest = l1_data_B_sram_alloc(s->sh_size); |
96a87e2f | 129 | mod->arch.bss_b_l1 = dest; |
1394f032 BW |
130 | if (dest == NULL) { |
131 | printk(KERN_ERR | |
132 | "module %s: L1 data memory allocation failed\n", | |
133 | mod->name); | |
134 | return -1; | |
135 | } | |
136 | memset(dest, 0, s->sh_size); | |
137 | s->sh_flags &= ~SHF_ALLOC; | |
138 | s->sh_addr = (unsigned long)dest; | |
139 | } | |
262c3825 SZ |
140 | if ((strcmp(".l2.text", secstrings + s->sh_name) == 0) || |
141 | ((strcmp(".text", secstrings + s->sh_name) == 0) && | |
142 | (hdr->e_flags & EF_BFIN_CODE_IN_L2) && (s->sh_size > 0))) { | |
143 | dest = l2_sram_alloc(s->sh_size); | |
144 | mod->arch.text_l2 = dest; | |
145 | if (dest == NULL) { | |
146 | printk(KERN_ERR | |
147 | "module %s: L2 SRAM allocation failed\n", | |
148 | mod->name); | |
149 | return -1; | |
150 | } | |
151 | memcpy(dest, (void *)s->sh_addr, s->sh_size); | |
152 | s->sh_flags &= ~SHF_ALLOC; | |
153 | s->sh_addr = (unsigned long)dest; | |
154 | } | |
155 | if ((strcmp(".l2.data", secstrings + s->sh_name) == 0) || | |
156 | ((strcmp(".data", secstrings + s->sh_name) == 0) && | |
157 | (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) { | |
158 | dest = l2_sram_alloc(s->sh_size); | |
159 | mod->arch.data_l2 = dest; | |
160 | if (dest == NULL) { | |
161 | printk(KERN_ERR | |
162 | "module %s: L2 SRAM allocation failed\n", | |
163 | mod->name); | |
164 | return -1; | |
165 | } | |
166 | memcpy(dest, (void *)s->sh_addr, s->sh_size); | |
167 | s->sh_flags &= ~SHF_ALLOC; | |
168 | s->sh_addr = (unsigned long)dest; | |
169 | } | |
170 | if (strcmp(".l2.bss", secstrings + s->sh_name) == 0 || | |
171 | ((strcmp(".bss", secstrings + s->sh_name) == 0) && | |
172 | (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) { | |
173 | dest = l2_sram_alloc(s->sh_size); | |
174 | mod->arch.bss_l2 = dest; | |
175 | if (dest == NULL) { | |
176 | printk(KERN_ERR | |
177 | "module %s: L2 SRAM allocation failed\n", | |
178 | mod->name); | |
179 | return -1; | |
180 | } | |
181 | memset(dest, 0, s->sh_size); | |
182 | s->sh_flags &= ~SHF_ALLOC; | |
183 | s->sh_addr = (unsigned long)dest; | |
184 | } | |
1394f032 BW |
185 | } |
186 | return 0; | |
187 | } | |
188 | ||
189 | int | |
190 | apply_relocate(Elf_Shdr * sechdrs, const char *strtab, | |
191 | unsigned int symindex, unsigned int relsec, struct module *me) | |
192 | { | |
193 | printk(KERN_ERR "module %s: .rel unsupported\n", me->name); | |
194 | return -ENOEXEC; | |
195 | } | |
196 | ||
197 | /*************************************************************************/ | |
198 | /* FUNCTION : apply_relocate_add */ | |
199 | /* ABSTRACT : Blackfin specific relocation handling for the loadable */ | |
200 | /* modules. Modules are expected to be .o files. */ | |
201 | /* Arithmetic relocations are handled. */ | |
202 | /* We do not expect LSETUP to be split and hence is not */ | |
203 | /* handled. */ | |
595d681f MF |
204 | /* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */ |
205 | /* gas does not generate it. */ | |
1394f032 BW |
206 | /*************************************************************************/ |
207 | int | |
208 | apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab, | |
209 | unsigned int symindex, unsigned int relsec, | |
210 | struct module *mod) | |
211 | { | |
212 | unsigned int i; | |
213 | unsigned short tmp; | |
214 | Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr; | |
215 | Elf32_Sym *sym; | |
216 | uint32_t *location32; | |
217 | uint16_t *location16; | |
218 | uint32_t value; | |
219 | ||
220 | pr_debug("Applying relocate section %u to %u\n", relsec, | |
221 | sechdrs[relsec].sh_info); | |
222 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { | |
223 | /* This is where to make the change */ | |
224 | location16 = | |
225 | (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].sh_addr + | |
226 | rel[i].r_offset); | |
227 | location32 = (uint32_t *) location16; | |
228 | /* This is the symbol it is referring to. Note that all | |
229 | undefined symbols have been resolved. */ | |
230 | sym = (Elf32_Sym *) sechdrs[symindex].sh_addr | |
231 | + ELF32_R_SYM(rel[i].r_info); | |
d1a85305 | 232 | value = sym->st_value; |
1394f032 BW |
233 | value += rel[i].r_addend; |
234 | pr_debug("location is %x, value is %x type is %d \n", | |
235 | (unsigned int) location32, value, | |
236 | ELF32_R_TYPE(rel[i].r_info)); | |
8f65873e GY |
237 | #ifdef CONFIG_SMP |
238 | if ((unsigned long)location16 >= COREB_L1_DATA_A_START) { | |
239 | printk(KERN_ERR "module %s: cannot relocate in L1: %u (SMP kernel)", | |
240 | mod->name, ELF32_R_TYPE(rel[i].r_info)); | |
241 | return -ENOEXEC; | |
242 | } | |
243 | #endif | |
1394f032 BW |
244 | switch (ELF32_R_TYPE(rel[i].r_info)) { |
245 | ||
595d681f | 246 | case R_BFIN_LUIMM16: |
1394f032 BW |
247 | pr_debug("before %x after %x\n", *location16, |
248 | (value & 0xffff)); | |
249 | tmp = (value & 0xffff); | |
1f83b8f1 | 250 | if ((unsigned long)location16 >= L1_CODE_START) { |
1394f032 BW |
251 | dma_memcpy(location16, &tmp, 2); |
252 | } else | |
253 | *location16 = tmp; | |
254 | break; | |
595d681f | 255 | case R_BFIN_HUIMM16: |
1394f032 BW |
256 | pr_debug("before %x after %x\n", *location16, |
257 | ((value >> 16) & 0xffff)); | |
258 | tmp = ((value >> 16) & 0xffff); | |
1f83b8f1 | 259 | if ((unsigned long)location16 >= L1_CODE_START) { |
1394f032 BW |
260 | dma_memcpy(location16, &tmp, 2); |
261 | } else | |
262 | *location16 = tmp; | |
263 | break; | |
595d681f | 264 | case R_BFIN_RIMM16: |
1394f032 BW |
265 | *location16 = (value & 0xffff); |
266 | break; | |
595d681f | 267 | case R_BFIN_BYTE4_DATA: |
1394f032 BW |
268 | pr_debug("before %x after %x\n", *location32, value); |
269 | *location32 = value; | |
270 | break; | |
22532578 RG |
271 | case R_BFIN_PCREL24: |
272 | case R_BFIN_PCREL24_JUMP_L: | |
273 | case R_BFIN_PCREL12_JUMP: | |
274 | case R_BFIN_PCREL12_JUMP_S: | |
275 | case R_BFIN_PCREL10: | |
276 | printk(KERN_ERR "module %s: Unsupported relocation: %u (no -mlong-calls?)\n" | |
277 | mod->name, ELF32_R_TYPE(rel[i].r_info)); | |
278 | return -ENOEXEC; | |
1394f032 BW |
279 | default: |
280 | printk(KERN_ERR "module %s: Unknown relocation: %u\n", | |
281 | mod->name, ELF32_R_TYPE(rel[i].r_info)); | |
282 | return -ENOEXEC; | |
283 | } | |
284 | } | |
285 | return 0; | |
286 | } | |
287 | ||
288 | int | |
289 | module_finalize(const Elf_Ehdr * hdr, | |
290 | const Elf_Shdr * sechdrs, struct module *mod) | |
291 | { | |
292 | unsigned int i, strindex = 0, symindex = 0; | |
293 | char *secstrings; | |
8f65873e | 294 | long err = 0; |
1394f032 BW |
295 | |
296 | secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; | |
297 | ||
298 | for (i = 1; i < hdr->e_shnum; i++) { | |
299 | /* Internal symbols and strings. */ | |
300 | if (sechdrs[i].sh_type == SHT_SYMTAB) { | |
301 | symindex = i; | |
302 | strindex = sechdrs[i].sh_link; | |
303 | } | |
304 | } | |
305 | ||
306 | for (i = 1; i < hdr->e_shnum; i++) { | |
307 | const char *strtab = (char *)sechdrs[strindex].sh_addr; | |
308 | unsigned int info = sechdrs[i].sh_info; | |
309 | ||
310 | /* Not a valid relocation section? */ | |
311 | if (info >= hdr->e_shnum) | |
312 | continue; | |
313 | ||
314 | if ((sechdrs[i].sh_type == SHT_RELA) && | |
262c3825 SZ |
315 | ((strcmp(".rela.l2.text", secstrings + sechdrs[i].sh_name) == 0) || |
316 | (strcmp(".rela.l1.text", secstrings + sechdrs[i].sh_name) == 0) || | |
1f83b8f1 | 317 | ((strcmp(".rela.text", secstrings + sechdrs[i].sh_name) == 0) && |
262c3825 | 318 | (hdr->e_flags & (EF_BFIN_CODE_IN_L1|EF_BFIN_CODE_IN_L2))))) { |
8f65873e | 319 | err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab, |
1394f032 | 320 | symindex, i, mod); |
8f65873e GY |
321 | if (err < 0) |
322 | return -ENOEXEC; | |
1394f032 BW |
323 | } |
324 | } | |
325 | return 0; | |
326 | } | |
327 | ||
328 | void module_arch_cleanup(struct module *mod) | |
329 | { | |
262c3825 SZ |
330 | l1_inst_sram_free(mod->arch.text_l1); |
331 | l1_data_A_sram_free(mod->arch.data_a_l1); | |
332 | l1_data_A_sram_free(mod->arch.bss_a_l1); | |
333 | l1_data_B_sram_free(mod->arch.data_b_l1); | |
334 | l1_data_B_sram_free(mod->arch.bss_b_l1); | |
335 | l2_sram_free(mod->arch.text_l2); | |
336 | l2_sram_free(mod->arch.data_l2); | |
337 | l2_sram_free(mod->arch.bss_l2); | |
1394f032 | 338 | } |