blob: 2ad805e67afc9cc8f57125d26a1b78ff43f355fa [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
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +000030#define pr_fmt(fmt) "module %s: " fmt
Bryan Wu1394f032007-05-06 14:50:22 -070031
32#include <linux/moduleloader.h>
33#include <linux/elf.h>
34#include <linux/vmalloc.h>
35#include <linux/fs.h>
36#include <linux/string.h>
37#include <linux/kernel.h>
38#include <asm/dma.h>
39#include <asm/cacheflush.h>
40
Bryan Wu1394f032007-05-06 14:50:22 -070041void *module_alloc(unsigned long size)
42{
43 if (size == 0)
44 return NULL;
45 return vmalloc(size);
46}
47
48/* Free memory returned from module_alloc */
49void module_free(struct module *mod, void *module_region)
50{
51 vfree(module_region);
52}
53
54/* Transfer the section to the L1 memory */
55int
Mike Frysinger459fec92009-06-26 00:48:33 +000056module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
Bryan Wu1394f032007-05-06 14:50:22 -070057 char *secstrings, struct module *mod)
58{
Meihui Fan96a87e22008-05-07 11:41:26 +080059 /*
60 * XXX: sechdrs are vmalloced in kernel/module.c
61 * and would be vfreed just after module is loaded,
62 * so we hack to keep the only information we needed
63 * in mod->arch to correctly free L1 I/D sram later.
64 * NOTE: this breaks the semantic of mod->arch structure.
65 */
Bryan Wu1394f032007-05-06 14:50:22 -070066 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
Mike Frysinger459fec92009-06-26 00:48:33 +000067 void *dest;
Bryan Wu1394f032007-05-06 14:50:22 -070068
69 for (s = sechdrs; s < sechdrs_end; ++s) {
Mike Frysinger459fec92009-06-26 00:48:33 +000070 const char *shname = secstrings + s->sh_name;
71
72 if (s->sh_size == 0)
73 continue;
74
75 if (!strcmp(".l1.text", shname) ||
76 (!strcmp(".text", shname) &&
77 (hdr->e_flags & EF_BFIN_CODE_IN_L1))) {
78
Bryan Wu1394f032007-05-06 14:50:22 -070079 dest = l1_inst_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080080 mod->arch.text_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070081 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +000082 pr_err("L1 inst memory allocation failed\n",
83 mod->name);
Bryan Wu1394f032007-05-06 14:50:22 -070084 return -1;
85 }
86 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +000087
88 } else if (!strcmp(".l1.data", shname) ||
89 (!strcmp(".data", shname) &&
90 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
91
Bryan Wu1394f032007-05-06 14:50:22 -070092 dest = l1_data_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080093 mod->arch.data_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070094 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +000095 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -070096 mod->name);
97 return -1;
98 }
99 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000100
101 } else if (!strcmp(".l1.bss", shname) ||
102 (!strcmp(".bss", shname) &&
103 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
104
Mike Frysinger70deca92009-06-26 00:37:40 +0000105 dest = l1_data_sram_zalloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800106 mod->arch.bss_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700107 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000108 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -0700109 mod->name);
110 return -1;
111 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000112
113 } else if (!strcmp(".l1.data.B", shname)) {
114
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) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000118 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -0700119 mod->name);
120 return -1;
121 }
122 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000123
124 } else if (!strcmp(".l1.bss.B", shname)) {
125
Bryan Wu1394f032007-05-06 14:50:22 -0700126 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800127 mod->arch.bss_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700128 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000129 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -0700130 mod->name);
131 return -1;
132 }
133 memset(dest, 0, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000134
135 } else if (!strcmp(".l2.text", shname) ||
136 (!strcmp(".text", shname) &&
137 (hdr->e_flags & EF_BFIN_CODE_IN_L2))) {
138
Sonic Zhang262c3822008-07-19 15:42:41 +0800139 dest = l2_sram_alloc(s->sh_size);
140 mod->arch.text_l2 = dest;
141 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000142 pr_err("L2 SRAM allocation failed\n",
143 mod->name);
Sonic Zhang262c3822008-07-19 15:42:41 +0800144 return -1;
145 }
146 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000147
148 } else if (!strcmp(".l2.data", shname) ||
149 (!strcmp(".data", shname) &&
150 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
151
Sonic Zhang262c3822008-07-19 15:42:41 +0800152 dest = l2_sram_alloc(s->sh_size);
153 mod->arch.data_l2 = dest;
154 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000155 pr_err("L2 SRAM allocation failed\n",
Sonic Zhang262c3822008-07-19 15:42:41 +0800156 mod->name);
157 return -1;
158 }
159 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000160
161 } else if (!strcmp(".l2.bss", shname) ||
162 (!strcmp(".bss", shname) &&
163 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
164
Mike Frysinger70deca92009-06-26 00:37:40 +0000165 dest = l2_sram_zalloc(s->sh_size);
Sonic Zhang262c3822008-07-19 15:42:41 +0800166 mod->arch.bss_l2 = dest;
167 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000168 pr_err("L2 SRAM allocation failed\n",
Sonic Zhang262c3822008-07-19 15:42:41 +0800169 mod->name);
170 return -1;
171 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000172
173 } else
174 continue;
175
176 s->sh_flags &= ~SHF_ALLOC;
177 s->sh_addr = (unsigned long)dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700178 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000179
Bryan Wu1394f032007-05-06 14:50:22 -0700180 return 0;
181}
182
183int
184apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
185 unsigned int symindex, unsigned int relsec, struct module *me)
186{
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000187 pr_err(".rel unsupported\n", me->name);
Bryan Wu1394f032007-05-06 14:50:22 -0700188 return -ENOEXEC;
189}
190
191/*************************************************************************/
192/* FUNCTION : apply_relocate_add */
193/* ABSTRACT : Blackfin specific relocation handling for the loadable */
194/* modules. Modules are expected to be .o files. */
195/* Arithmetic relocations are handled. */
196/* We do not expect LSETUP to be split and hence is not */
197/* handled. */
Mike Frysinger595d6812009-06-02 00:35:33 +0000198/* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
199/* gas does not generate it. */
Bryan Wu1394f032007-05-06 14:50:22 -0700200/*************************************************************************/
201int
202apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab,
203 unsigned int symindex, unsigned int relsec,
204 struct module *mod)
205{
206 unsigned int i;
207 unsigned short tmp;
208 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
209 Elf32_Sym *sym;
210 uint32_t *location32;
211 uint16_t *location16;
212 uint32_t value;
213
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000214 pr_debug("applying relocate section %u to %u\n", mod->name,
215 relsec, sechdrs[relsec].sh_info);
Bryan Wu1394f032007-05-06 14:50:22 -0700216 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
217 /* This is where to make the change */
218 location16 =
219 (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].sh_addr +
220 rel[i].r_offset);
221 location32 = (uint32_t *) location16;
222 /* This is the symbol it is referring to. Note that all
223 undefined symbols have been resolved. */
224 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
225 + ELF32_R_SYM(rel[i].r_info);
Bernd Schmidtd1a85302009-01-07 23:14:39 +0800226 value = sym->st_value;
Bryan Wu1394f032007-05-06 14:50:22 -0700227 value += rel[i].r_addend;
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000228
Graf Yang8f658732008-11-18 17:48:22 +0800229#ifdef CONFIG_SMP
230 if ((unsigned long)location16 >= COREB_L1_DATA_A_START) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000231 pr_err("cannot relocate in L1: %u (SMP kernel)",
232 mod->name, ELF32_R_TYPE(rel[i].r_info));
Graf Yang8f658732008-11-18 17:48:22 +0800233 return -ENOEXEC;
234 }
235#endif
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000236
237 pr_debug("location is %lx, value is %x type is %d\n",
238 mod->name, (unsigned long)location32, value,
239 ELF32_R_TYPE(rel[i].r_info));
240
Bryan Wu1394f032007-05-06 14:50:22 -0700241 switch (ELF32_R_TYPE(rel[i].r_info)) {
242
Mike Frysinger595d6812009-06-02 00:35:33 +0000243 case R_BFIN_LUIMM16:
Bryan Wu1394f032007-05-06 14:50:22 -0700244 tmp = (value & 0xffff);
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800245 if ((unsigned long)location16 >= L1_CODE_START) {
Bryan Wu1394f032007-05-06 14:50:22 -0700246 dma_memcpy(location16, &tmp, 2);
247 } else
248 *location16 = tmp;
249 break;
Mike Frysinger595d6812009-06-02 00:35:33 +0000250 case R_BFIN_HUIMM16:
Bryan Wu1394f032007-05-06 14:50:22 -0700251 tmp = ((value >> 16) & 0xffff);
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800252 if ((unsigned long)location16 >= L1_CODE_START) {
Bryan Wu1394f032007-05-06 14:50:22 -0700253 dma_memcpy(location16, &tmp, 2);
254 } else
255 *location16 = tmp;
256 break;
Mike Frysinger595d6812009-06-02 00:35:33 +0000257 case R_BFIN_RIMM16:
Bryan Wu1394f032007-05-06 14:50:22 -0700258 *location16 = (value & 0xffff);
259 break;
Mike Frysinger595d6812009-06-02 00:35:33 +0000260 case R_BFIN_BYTE4_DATA:
Bryan Wu1394f032007-05-06 14:50:22 -0700261 *location32 = value;
262 break;
Robin Getz22532572009-06-25 15:49:38 +0000263 case R_BFIN_PCREL24:
264 case R_BFIN_PCREL24_JUMP_L:
265 case R_BFIN_PCREL12_JUMP:
266 case R_BFIN_PCREL12_JUMP_S:
267 case R_BFIN_PCREL10:
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000268 pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
Robin Getz22532572009-06-25 15:49:38 +0000269 mod->name, ELF32_R_TYPE(rel[i].r_info));
270 return -ENOEXEC;
Bryan Wu1394f032007-05-06 14:50:22 -0700271 default:
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000272 pr_err("unknown relocation: %u\n", mod->name,
273 ELF32_R_TYPE(rel[i].r_info));
Bryan Wu1394f032007-05-06 14:50:22 -0700274 return -ENOEXEC;
275 }
276 }
277 return 0;
278}
279
280int
281module_finalize(const Elf_Ehdr * hdr,
282 const Elf_Shdr * sechdrs, struct module *mod)
283{
284 unsigned int i, strindex = 0, symindex = 0;
285 char *secstrings;
Graf Yang8f658732008-11-18 17:48:22 +0800286 long err = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700287
288 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
289
290 for (i = 1; i < hdr->e_shnum; i++) {
291 /* Internal symbols and strings. */
292 if (sechdrs[i].sh_type == SHT_SYMTAB) {
293 symindex = i;
294 strindex = sechdrs[i].sh_link;
295 }
296 }
297
298 for (i = 1; i < hdr->e_shnum; i++) {
299 const char *strtab = (char *)sechdrs[strindex].sh_addr;
300 unsigned int info = sechdrs[i].sh_info;
Mike Frysinger459fec92009-06-26 00:48:33 +0000301 const char *shname = secstrings + sechdrs[i].sh_name;
Bryan Wu1394f032007-05-06 14:50:22 -0700302
303 /* Not a valid relocation section? */
304 if (info >= hdr->e_shnum)
305 continue;
306
Mike Frysinger459fec92009-06-26 00:48:33 +0000307 /* Only support RELA relocation types */
308 if (sechdrs[i].sh_type != SHT_RELA)
309 continue;
310
311 if (!strcmp(".rela.l2.text", shname) ||
312 !strcmp(".rela.l1.text", shname) ||
313 (!strcmp(".rela.text", shname) &&
314 (hdr->e_flags & (EF_BFIN_CODE_IN_L1 | EF_BFIN_CODE_IN_L2)))) {
315
Graf Yang8f658732008-11-18 17:48:22 +0800316 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700317 symindex, i, mod);
Graf Yang8f658732008-11-18 17:48:22 +0800318 if (err < 0)
319 return -ENOEXEC;
Bryan Wu1394f032007-05-06 14:50:22 -0700320 }
321 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000322
Bryan Wu1394f032007-05-06 14:50:22 -0700323 return 0;
324}
325
326void module_arch_cleanup(struct module *mod)
327{
Sonic Zhang262c3822008-07-19 15:42:41 +0800328 l1_inst_sram_free(mod->arch.text_l1);
329 l1_data_A_sram_free(mod->arch.data_a_l1);
330 l1_data_A_sram_free(mod->arch.bss_a_l1);
331 l1_data_B_sram_free(mod->arch.data_b_l1);
332 l1_data_B_sram_free(mod->arch.bss_b_l1);
333 l2_sram_free(mod->arch.text_l2);
334 l2_sram_free(mod->arch.data_l2);
335 l2_sram_free(mod->arch.bss_l2);
Bryan Wu1394f032007-05-06 14:50:22 -0700336}