blob: 1bd7f2d018a85892c92ebcb8277e7547727c71fe [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
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
Bryan Wu1394f032007-05-06 14:50:22 -070040void *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 */
48void module_free(struct module *mod, void *module_region)
49{
50 vfree(module_region);
51}
52
53/* Transfer the section to the L1 memory */
54int
55module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
56 char *secstrings, struct module *mod)
57{
Meihui Fan96a87e22008-05-07 11:41:26 +080058 /*
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 */
Bryan Wu1394f032007-05-06 14:50:22 -070065 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) ||
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080070 ((strcmp(".text", secstrings + s->sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +080071 (hdr->e_flags & EF_BFIN_CODE_IN_L1) && (s->sh_size > 0))) {
Bryan Wu1394f032007-05-06 14:50:22 -070072 dest = l1_inst_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080073 mod->arch.text_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070074 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 }
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080084 if ((strcmp(".l1.data", secstrings + s->sh_name) == 0) ||
85 ((strcmp(".data", secstrings + s->sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +080086 (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
Bryan Wu1394f032007-05-06 14:50:22 -070087 dest = l1_data_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080088 mod->arch.data_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070089 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 ||
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800100 ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +0800101 (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
Bryan Wu1394f032007-05-06 14:50:22 -0700102 dest = l1_data_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800103 mod->arch.bss_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700104 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) {
Bryan Wu1394f032007-05-06 14:50:22 -0700115 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800116 mod->arch.data_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700117 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) {
Bryan Wu1394f032007-05-06 14:50:22 -0700128 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800129 mod->arch.bss_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700130 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 }
Sonic Zhang262c3822008-07-19 15:42:41 +0800140 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 }
Bryan Wu1394f032007-05-06 14:50:22 -0700185 }
186 return 0;
187}
188
189int
190apply_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. */
204/* R_byte and R_byte2 are also not handled as the gas */
205/* does not generate it. */
206/*************************************************************************/
207int
208apply_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);
Bernd Schmidtd1a85302009-01-07 23:14:39 +0800232 value = sym->st_value;
Bryan Wu1394f032007-05-06 14:50:22 -0700233 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));
Graf Yang8f658732008-11-18 17:48:22 +0800237#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
Bryan Wu1394f032007-05-06 14:50:22 -0700244 switch (ELF32_R_TYPE(rel[i].r_info)) {
245
246 case R_pcrel24:
247 case R_pcrel24_jump_l:
248 /* Add the value, subtract its postition */
249 location16 =
250 (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].
251 sh_addr + rel[i].r_offset - 2);
252 location32 = (uint32_t *) location16;
253 value -= (uint32_t) location32;
254 value >>= 1;
Bernd Schmidtd1a85302009-01-07 23:14:39 +0800255 if ((value & 0xFF000000) != 0 &&
256 (value & 0xFF000000) != 0xFF000000) {
257 printk(KERN_ERR "module %s: relocation overflow\n",
258 mod->name);
259 return -ENOEXEC;
260 }
Bryan Wu1394f032007-05-06 14:50:22 -0700261 pr_debug("value is %x, before %x-%x after %x-%x\n", value,
262 *location16, *(location16 + 1),
263 (*location16 & 0xff00) | (value >> 16 & 0x00ff),
264 value & 0xffff);
265 *location16 =
266 (*location16 & 0xff00) | (value >> 16 & 0x00ff);
267 *(location16 + 1) = value & 0xffff;
268 break;
269 case R_pcrel12_jump:
270 case R_pcrel12_jump_s:
271 value -= (uint32_t) location32;
272 value >>= 1;
273 *location16 = (value & 0xfff);
274 break;
275 case R_pcrel10:
276 value -= (uint32_t) location32;
277 value >>= 1;
278 *location16 = (value & 0x3ff);
279 break;
280 case R_luimm16:
281 pr_debug("before %x after %x\n", *location16,
282 (value & 0xffff));
283 tmp = (value & 0xffff);
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800284 if ((unsigned long)location16 >= L1_CODE_START) {
Bryan Wu1394f032007-05-06 14:50:22 -0700285 dma_memcpy(location16, &tmp, 2);
286 } else
287 *location16 = tmp;
288 break;
289 case R_huimm16:
290 pr_debug("before %x after %x\n", *location16,
291 ((value >> 16) & 0xffff));
292 tmp = ((value >> 16) & 0xffff);
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800293 if ((unsigned long)location16 >= L1_CODE_START) {
Bryan Wu1394f032007-05-06 14:50:22 -0700294 dma_memcpy(location16, &tmp, 2);
295 } else
296 *location16 = tmp;
297 break;
298 case R_rimm16:
299 *location16 = (value & 0xffff);
300 break;
301 case R_byte4_data:
302 pr_debug("before %x after %x\n", *location32, value);
303 *location32 = value;
304 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700305 default:
306 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
307 mod->name, ELF32_R_TYPE(rel[i].r_info));
308 return -ENOEXEC;
309 }
310 }
311 return 0;
312}
313
314int
315module_finalize(const Elf_Ehdr * hdr,
316 const Elf_Shdr * sechdrs, struct module *mod)
317{
318 unsigned int i, strindex = 0, symindex = 0;
319 char *secstrings;
Graf Yang8f658732008-11-18 17:48:22 +0800320 long err = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700321
322 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
323
324 for (i = 1; i < hdr->e_shnum; i++) {
325 /* Internal symbols and strings. */
326 if (sechdrs[i].sh_type == SHT_SYMTAB) {
327 symindex = i;
328 strindex = sechdrs[i].sh_link;
329 }
330 }
331
332 for (i = 1; i < hdr->e_shnum; i++) {
333 const char *strtab = (char *)sechdrs[strindex].sh_addr;
334 unsigned int info = sechdrs[i].sh_info;
335
336 /* Not a valid relocation section? */
337 if (info >= hdr->e_shnum)
338 continue;
339
340 if ((sechdrs[i].sh_type == SHT_RELA) &&
Sonic Zhang262c3822008-07-19 15:42:41 +0800341 ((strcmp(".rela.l2.text", secstrings + sechdrs[i].sh_name) == 0) ||
342 (strcmp(".rela.l1.text", secstrings + sechdrs[i].sh_name) == 0) ||
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800343 ((strcmp(".rela.text", secstrings + sechdrs[i].sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +0800344 (hdr->e_flags & (EF_BFIN_CODE_IN_L1|EF_BFIN_CODE_IN_L2))))) {
Graf Yang8f658732008-11-18 17:48:22 +0800345 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700346 symindex, i, mod);
Graf Yang8f658732008-11-18 17:48:22 +0800347 if (err < 0)
348 return -ENOEXEC;
Bryan Wu1394f032007-05-06 14:50:22 -0700349 }
350 }
351 return 0;
352}
353
354void module_arch_cleanup(struct module *mod)
355{
Sonic Zhang262c3822008-07-19 15:42:41 +0800356 l1_inst_sram_free(mod->arch.text_l1);
357 l1_data_A_sram_free(mod->arch.data_a_l1);
358 l1_data_A_sram_free(mod->arch.bss_a_l1);
359 l1_data_B_sram_free(mod->arch.data_b_l1);
360 l1_data_B_sram_free(mod->arch.bss_b_l1);
361 l2_sram_free(mod->arch.text_l2);
362 l2_sram_free(mod->arch.data_l2);
363 l2_sram_free(mod->arch.bss_l2);
Bryan Wu1394f032007-05-06 14:50:22 -0700364}