patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / sparc64 / 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/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/mm.h>
16
17 #include <asm/processor.h>
18 #include <asm/spitfire.h>
19
20 static struct vm_struct * modvmlist = NULL;
21
22 static void module_unmap(void * addr)
23 {
24         struct vm_struct **p, *tmp;
25         int i;
26
27         if (!addr)
28                 return;
29         if ((PAGE_SIZE-1) & (unsigned long) addr) {
30                 printk("Trying to unmap module with bad address (%p)\n", addr);
31                 return;
32         }
33
34         for (p = &modvmlist; (tmp = *p) != NULL; p = &tmp->next) {
35                 if (tmp->addr == addr) {
36                         *p = tmp->next;
37                         goto found;
38                 }
39         }
40         printk("Trying to unmap nonexistent module vm area (%p)\n", addr);
41         return;
42
43 found:
44         unmap_vm_area(tmp);
45         
46         for (i = 0; i < tmp->nr_pages; i++) {
47                 if (unlikely(!tmp->pages[i]))
48                         BUG();
49                 __free_page(tmp->pages[i]);
50         }
51
52         kfree(tmp->pages);
53         kfree(tmp);
54 }
55
56
57 static void *module_map(unsigned long size)
58 {
59         struct vm_struct **p, *tmp, *area;
60         struct page **pages;
61         void * addr;
62         unsigned int nr_pages, array_size, i;
63
64         size = PAGE_ALIGN(size);
65         if (!size || size > MODULES_LEN)
66                 return NULL;
67                 
68         addr = (void *) MODULES_VADDR;
69         for (p = &modvmlist; (tmp = *p) != NULL; p = &tmp->next) {
70                 if (size + (unsigned long) addr < (unsigned long) tmp->addr)
71                         break;
72                 addr = (void *) (tmp->size + (unsigned long) tmp->addr);
73         }
74         if ((unsigned long) addr + size >= MODULES_END)
75                 return NULL;
76         
77         area = (struct vm_struct *) kmalloc(sizeof(*area), GFP_KERNEL);
78         if (!area)
79                 return NULL;
80         area->size = size + PAGE_SIZE;
81         area->addr = addr;
82         area->next = *p;
83         area->pages = NULL;
84         area->nr_pages = 0;
85         area->phys_addr = 0;
86         *p = area;
87
88         nr_pages = size >> PAGE_SHIFT;
89         array_size = (nr_pages * sizeof(struct page *));
90
91         area->nr_pages = nr_pages;
92         area->pages = pages = kmalloc(array_size, GFP_KERNEL);
93         if (!area->pages)
94                 goto fail;
95
96         memset(area->pages, 0, array_size);
97
98         for (i = 0; i < area->nr_pages; i++) {
99                 area->pages[i] = alloc_page(GFP_KERNEL);
100                 if (unlikely(!area->pages[i]))
101                         goto fail;
102         }
103         
104         if (map_vm_area(area, PAGE_KERNEL, &pages)) {
105                 unmap_vm_area(area);
106                 goto fail;
107         }
108
109         return area->addr;
110
111 fail:
112         if (area->pages) {
113                 for (i = 0; i < area->nr_pages; i++) {
114                         if (area->pages[i])
115                                 __free_page(area->pages[i]);
116                 }
117                 kfree(area->pages);
118         }
119         kfree(area);
120
121         return NULL;
122 }
123
124 void *module_alloc(unsigned long size)
125 {
126         void *ret;
127
128         /* We handle the zero case fine, unlike vmalloc */
129         if (size == 0)
130                 return NULL;
131
132         ret = module_map(size);
133         if (!ret)
134                 ret = ERR_PTR(-ENOMEM);
135         else
136                 memset(ret, 0, size);
137
138         return ret;
139 }
140
141 /* Free memory returned from module_core_alloc/module_init_alloc */
142 void module_free(struct module *mod, void *module_region)
143 {
144         write_lock(&vmlist_lock);
145         module_unmap(module_region);
146         write_unlock(&vmlist_lock);
147         /* FIXME: If module_region == mod->init_region, trim exception
148            table entries. */
149 }
150
151 /* Make generic code ignore STT_REGISTER dummy undefined symbols.  */
152 int module_frob_arch_sections(Elf_Ehdr *hdr,
153                               Elf_Shdr *sechdrs,
154                               char *secstrings,
155                               struct module *mod)
156 {
157         unsigned int symidx;
158         Elf64_Sym *sym;
159         const char *strtab;
160         int i;
161
162         for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) {
163                 if (symidx == hdr->e_shnum-1) {
164                         printk("%s: no symtab found.\n", mod->name);
165                         return -ENOEXEC;
166                 }
167         }
168         sym = (Elf64_Sym *)sechdrs[symidx].sh_addr;
169         strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr;
170
171         for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) {
172                 if (sym[i].st_shndx == SHN_UNDEF &&
173                     ELF64_ST_TYPE(sym[i].st_info) == STT_REGISTER)
174                         sym[i].st_shndx = SHN_ABS;
175         }
176         return 0;
177 }
178
179 int apply_relocate(Elf64_Shdr *sechdrs,
180                    const char *strtab,
181                    unsigned int symindex,
182                    unsigned int relsec,
183                    struct module *me)
184 {
185         printk(KERN_ERR "module %s: non-ADD RELOCATION unsupported\n",
186                me->name);
187         return -ENOEXEC;
188 }
189
190 int apply_relocate_add(Elf64_Shdr *sechdrs,
191                        const char *strtab,
192                        unsigned int symindex,
193                        unsigned int relsec,
194                        struct module *me)
195 {
196         unsigned int i;
197         Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
198         Elf64_Sym *sym;
199         u8 *location;
200         u32 *loc32;
201
202         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
203                 Elf64_Addr v;
204
205                 /* This is where to make the change */
206                 location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
207                         + rel[i].r_offset;
208                 loc32 = (u32 *) location;
209
210                 BUG_ON(((u64)location >> (u64)32) != (u64)0);
211
212                 /* This is the symbol it is referring to.  Note that all
213                    undefined symbols have been resolved.  */
214                 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
215                         + ELF64_R_SYM(rel[i].r_info);
216                 v = sym->st_value + rel[i].r_addend;
217
218                 switch (ELF64_R_TYPE(rel[i].r_info) & 0xff) {
219                 case R_SPARC_64:
220                         location[0] = v >> 56;
221                         location[1] = v >> 48;
222                         location[2] = v >> 40;
223                         location[3] = v >> 32;
224                         location[4] = v >> 24;
225                         location[5] = v >> 16;
226                         location[6] = v >>  8;
227                         location[7] = v >>  0;
228                         break;
229
230                 case R_SPARC_32:
231                         location[0] = v >> 24;
232                         location[1] = v >> 16;
233                         location[2] = v >>  8;
234                         location[3] = v >>  0;
235                         break;
236
237                 case R_SPARC_WDISP30:
238                         v -= (Elf64_Addr) location;
239                         *loc32 = (*loc32 & ~0x3fffffff) |
240                                 ((v >> 2) & 0x3fffffff);
241                         break;
242
243                 case R_SPARC_WDISP22:
244                         v -= (Elf64_Addr) location;
245                         *loc32 = (*loc32 & ~0x3fffff) |
246                                 ((v >> 2) & 0x3fffff);
247                         break;
248
249                 case R_SPARC_WDISP19:
250                         v -= (Elf64_Addr) location;
251                         *loc32 = (*loc32 & ~0x7ffff) |
252                                 ((v >> 2) & 0x7ffff);
253                         break;
254
255                 case R_SPARC_LO10:
256                         *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
257                         break;
258
259                 case R_SPARC_HI22:
260                         *loc32 = (*loc32 & ~0x3fffff) |
261                                 ((v >> 10) & 0x3fffff);
262                         break;
263
264                 case R_SPARC_OLO10:
265                         *loc32 = (*loc32 & ~0x1fff) |
266                                 (((v & 0x3ff) +
267                                   (ELF64_R_TYPE(rel[i].r_info) >> 8))
268                                  & 0x1fff);
269                         break;
270
271                 default:
272                         printk(KERN_ERR "module %s: Unknown relocation: %x\n",
273                                me->name,
274                                (int) (ELF64_R_TYPE(rel[i].r_info) & 0xff));
275                         return -ENOEXEC;
276                 };
277         }
278         return 0;
279 }
280
281 int module_finalize(const Elf_Ehdr *hdr,
282                     const Elf_Shdr *sechdrs,
283                     struct module *me)
284 {
285         /* Cheetah's I-cache is fully coherent.  */
286         if (tlb_type == spitfire) {
287                 unsigned long va;
288
289                 flushw_all();
290                 for (va =  0; va < (PAGE_SIZE << 1); va += 32)
291                         spitfire_put_icache_tag(va, 0x0);
292                 __asm__ __volatile__("flush %g6");
293         }
294
295         return 0;
296 }
297
298 void module_arch_cleanup(struct module *mod)
299 {
300 }