Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
[linux-2.6] / arch / blackfin / kernel / module.c
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
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 {
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          */
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) ||
70                     ((strcmp(".text", secstrings + s->sh_name) == 0) &&
71                      (hdr->e_flags & EF_BFIN_CODE_IN_L1) && (s->sh_size > 0))) {
72                         dest = l1_inst_sram_alloc(s->sh_size);
73                         mod->arch.text_l1 = dest;
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                 }
84                 if ((strcmp(".l1.data", secstrings + s->sh_name) == 0) ||
85                     ((strcmp(".data", secstrings + s->sh_name) == 0) &&
86                      (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
87                         dest = l1_data_sram_alloc(s->sh_size);
88                         mod->arch.data_a_l1 = dest;
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 ||
100                     ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
101                      (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
102                         dest = l1_data_sram_alloc(s->sh_size);
103                         mod->arch.bss_a_l1 = dest;
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) {
115                         dest = l1_data_B_sram_alloc(s->sh_size);
116                         mod->arch.data_b_l1 = dest;
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) {
128                         dest = l1_data_B_sram_alloc(s->sh_size);
129                         mod->arch.bss_b_l1 = dest;
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                 }
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                 }
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.                                                   */
204 /*            R_byte and R_byte2 are also not handled as the gas         */
205 /*            does not generate it.                                      */
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);
232                 value = sym->st_value;
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));
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
244                 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;
255                         if ((value & 0xFF000000) != 0 &&
256                             (value & 0xFF000000) != 0xFF000000) {
257                                 printk(KERN_ERR "module %s: relocation overflow\n",
258                                        mod->name);
259                                 return -ENOEXEC;
260                         }
261                         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);
284                         if ((unsigned long)location16 >= L1_CODE_START) {
285                                 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);
293                         if ((unsigned long)location16 >= L1_CODE_START) {
294                                 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;
305                 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
314 int
315 module_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;
320         long err = 0;
321
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) &&
341                     ((strcmp(".rela.l2.text", secstrings + sechdrs[i].sh_name) == 0) ||
342                     (strcmp(".rela.l1.text", secstrings + sechdrs[i].sh_name) == 0) ||
343                     ((strcmp(".rela.text", secstrings + sechdrs[i].sh_name) == 0) &&
344                         (hdr->e_flags & (EF_BFIN_CODE_IN_L1|EF_BFIN_CODE_IN_L2))))) {
345                         err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
346                                            symindex, i, mod);
347                         if (err < 0)
348                                 return -ENOEXEC;
349                 }
350         }
351         return 0;
352 }
353
354 void module_arch_cleanup(struct module *mod)
355 {
356         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);
364 }