Merge branch 'x86-setup-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6] / arch / sparc / kernel / module.c
1 /* Kernel module help for sparc64.
2  *
3  * Copyright (C) 2001 Rusty Russell.
4  * Copyright (C) 2002 David S. Miller.
5  */
6
7 #include <linux/moduleloader.h>
8 #include <linux/kernel.h>
9 #include <linux/elf.h>
10 #include <linux/vmalloc.h>
11 #include <linux/fs.h>
12 #include <linux/string.h>
13 #include <linux/ctype.h>
14 #include <linux/slab.h>
15 #include <linux/mm.h>
16
17 #include <asm/processor.h>
18 #include <asm/spitfire.h>
19
20 #ifdef CONFIG_SPARC64
21 static void *module_map(unsigned long size)
22 {
23         struct vm_struct *area;
24
25         size = PAGE_ALIGN(size);
26         if (!size || size > MODULES_LEN)
27                 return NULL;
28
29         area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
30         if (!area)
31                 return NULL;
32
33         return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL);
34 }
35
36 static char *dot2underscore(char *name)
37 {
38         return name;
39 }
40 #else
41 static void *module_map(unsigned long size)
42 {
43         return vmalloc(size);
44 }
45
46 /* Replace references to .func with _Func */
47 static char *dot2underscore(char *name)
48 {
49         if (name[0] == '.') {
50                 name[0] = '_';
51                 name[1] = toupper(name[1]);
52         }
53         return name;
54 }
55 #endif /* CONFIG_SPARC64 */
56
57 void *module_alloc(unsigned long size)
58 {
59         void *ret;
60
61         /* We handle the zero case fine, unlike vmalloc */
62         if (size == 0)
63                 return NULL;
64
65         ret = module_map(size);
66         if (!ret)
67                 ret = ERR_PTR(-ENOMEM);
68         else
69                 memset(ret, 0, size);
70
71         return ret;
72 }
73
74 /* Free memory returned from module_core_alloc/module_init_alloc */
75 void module_free(struct module *mod, void *module_region)
76 {
77         vfree(module_region);
78         /* FIXME: If module_region == mod->init_region, trim exception
79            table entries. */
80 }
81
82 /* Make generic code ignore STT_REGISTER dummy undefined symbols.  */
83 int module_frob_arch_sections(Elf_Ehdr *hdr,
84                               Elf_Shdr *sechdrs,
85                               char *secstrings,
86                               struct module *mod)
87 {
88         unsigned int symidx;
89         Elf_Sym *sym;
90         char *strtab;
91         int i;
92
93         for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) {
94                 if (symidx == hdr->e_shnum-1) {
95                         printk("%s: no symtab found.\n", mod->name);
96                         return -ENOEXEC;
97                 }
98         }
99         sym = (Elf_Sym *)sechdrs[symidx].sh_addr;
100         strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr;
101
102         for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) {
103                 if (sym[i].st_shndx == SHN_UNDEF) {
104                         if (ELF_ST_TYPE(sym[i].st_info) == STT_REGISTER) {
105                                 sym[i].st_shndx = SHN_ABS;
106                         } else {
107                                 char *name = strtab + sym[i].st_name;
108                                 dot2underscore(name);
109                         }
110                 }
111         }
112         return 0;
113 }
114
115 int apply_relocate(Elf_Shdr *sechdrs,
116                    const char *strtab,
117                    unsigned int symindex,
118                    unsigned int relsec,
119                    struct module *me)
120 {
121         printk(KERN_ERR "module %s: non-ADD RELOCATION unsupported\n",
122                me->name);
123         return -ENOEXEC;
124 }
125
126 int apply_relocate_add(Elf_Shdr *sechdrs,
127                        const char *strtab,
128                        unsigned int symindex,
129                        unsigned int relsec,
130                        struct module *me)
131 {
132         unsigned int i;
133         Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
134         Elf_Sym *sym;
135         u8 *location;
136         u32 *loc32;
137
138         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
139                 Elf_Addr v;
140
141                 /* This is where to make the change */
142                 location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
143                         + rel[i].r_offset;
144                 loc32 = (u32 *) location;
145
146 #ifdef CONFIG_SPARC64
147                 BUG_ON(((u64)location >> (u64)32) != (u64)0);
148 #endif /* CONFIG_SPARC64 */
149
150                 /* This is the symbol it is referring to.  Note that all
151                    undefined symbols have been resolved.  */
152                 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
153                         + ELF_R_SYM(rel[i].r_info);
154                 v = sym->st_value + rel[i].r_addend;
155
156                 switch (ELF_R_TYPE(rel[i].r_info) & 0xff) {
157 #ifdef CONFIG_SPARC64
158                 case R_SPARC_64:
159                         location[0] = v >> 56;
160                         location[1] = v >> 48;
161                         location[2] = v >> 40;
162                         location[3] = v >> 32;
163                         location[4] = v >> 24;
164                         location[5] = v >> 16;
165                         location[6] = v >>  8;
166                         location[7] = v >>  0;
167                         break;
168
169                 case R_SPARC_DISP32:
170                         v -= (Elf_Addr) location;
171                         *loc32 = v;
172                         break;
173
174                 case R_SPARC_WDISP19:
175                         v -= (Elf_Addr) location;
176                         *loc32 = (*loc32 & ~0x7ffff) |
177                                 ((v >> 2) & 0x7ffff);
178                         break;
179
180                 case R_SPARC_OLO10:
181                         *loc32 = (*loc32 & ~0x1fff) |
182                                 (((v & 0x3ff) +
183                                   (ELF_R_TYPE(rel[i].r_info) >> 8))
184                                  & 0x1fff);
185                         break;
186 #endif /* CONFIG_SPARC64 */
187
188                 case R_SPARC_32:
189                 case R_SPARC_UA32:
190                         location[0] = v >> 24;
191                         location[1] = v >> 16;
192                         location[2] = v >>  8;
193                         location[3] = v >>  0;
194                         break;
195
196                 case R_SPARC_WDISP30:
197                         v -= (Elf_Addr) location;
198                         *loc32 = (*loc32 & ~0x3fffffff) |
199                                 ((v >> 2) & 0x3fffffff);
200                         break;
201
202                 case R_SPARC_WDISP22:
203                         v -= (Elf_Addr) location;
204                         *loc32 = (*loc32 & ~0x3fffff) |
205                                 ((v >> 2) & 0x3fffff);
206                         break;
207
208                 case R_SPARC_LO10:
209                         *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
210                         break;
211
212                 case R_SPARC_HI22:
213                         *loc32 = (*loc32 & ~0x3fffff) |
214                                 ((v >> 10) & 0x3fffff);
215                         break;
216
217                 default:
218                         printk(KERN_ERR "module %s: Unknown relocation: %x\n",
219                                me->name,
220                                (int) (ELF_R_TYPE(rel[i].r_info) & 0xff));
221                         return -ENOEXEC;
222                 };
223         }
224         return 0;
225 }
226
227 #ifdef CONFIG_SPARC64
228 int module_finalize(const Elf_Ehdr *hdr,
229                     const Elf_Shdr *sechdrs,
230                     struct module *me)
231 {
232         /* Cheetah's I-cache is fully coherent.  */
233         if (tlb_type == spitfire) {
234                 unsigned long va;
235
236                 flushw_all();
237                 for (va =  0; va < (PAGE_SIZE << 1); va += 32)
238                         spitfire_put_icache_tag(va, 0x0);
239                 __asm__ __volatile__("flush %g6");
240         }
241
242         return 0;
243 }
244 #else
245 int module_finalize(const Elf_Ehdr *hdr,
246                     const Elf_Shdr *sechdrs,
247                     struct module *me)
248 {
249         return 0;
250 }
251 #endif /* CONFIG_SPARC64 */
252
253 void module_arch_cleanup(struct module *mod)
254 {
255 }