vserver 1.9.5.x5
[linux-2.6.git] / arch / x86_64 / kernel / module.c
1 /*  Kernel module help for x86-64
2     Copyright (C) 2001 Rusty Russell.
3     Copyright (C) 2002,2003 Andi Kleen, SuSE Labs.
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 #include <linux/moduleloader.h>
20 #include <linux/elf.h>
21 #include <linux/vmalloc.h>
22 #include <linux/fs.h>
23 #include <linux/string.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26
27 #include <asm/system.h>
28 #include <asm/page.h>
29 #include <asm/pgtable.h>
30
31 #define DEBUGP(fmt...) 
32  
33 static struct vm_struct *mod_vmlist;
34
35 void module_free(struct module *mod, void *module_region)
36 {
37         struct vm_struct **prevp, *map;
38         int i;
39         unsigned long addr = (unsigned long)module_region;
40
41         if (!addr)
42                 return;
43         write_lock(&vmlist_lock); 
44         for (prevp = &mod_vmlist ; (map = *prevp) ; prevp = &map->next) {
45                 if ((unsigned long)map->addr == addr) {
46                         *prevp = map->next;
47                         goto found;
48                 }
49         }
50         write_unlock(&vmlist_lock); 
51         printk("Trying to unmap nonexistent module vm area (%lx)\n", addr);
52         return;
53  found:
54         unmap_vm_area(map);
55         write_unlock(&vmlist_lock); 
56         if (map->pages) {
57                 for (i = 0; i < map->nr_pages; i++)
58                         if (map->pages[i])
59                                 __free_page(map->pages[i]);     
60                 kfree(map->pages);
61         }
62         kfree(map);                                     
63 }
64
65 void *module_alloc(unsigned long size)
66 {
67         struct vm_struct **p, *tmp, *area;
68         struct page **pages;
69         void *addr;
70         unsigned int nr_pages, array_size, i;
71
72         if (!size)
73                 return NULL; 
74         size = PAGE_ALIGN(size);
75         if (size > MODULES_LEN)
76                 return NULL;
77
78         area = (struct vm_struct *) kmalloc(sizeof(*area), GFP_KERNEL);
79         if (!area)
80                 return NULL;
81         memset(area, 0, sizeof(struct vm_struct));
82
83         write_lock(&vmlist_lock);
84         addr = (void *) MODULES_VADDR;
85         for (p = &mod_vmlist; (tmp = *p); p = &tmp->next) {
86                 void *next; 
87                 DEBUGP("vmlist %p %lu addr %p\n", tmp->addr, tmp->size, addr);
88                 if (size + (unsigned long) addr + PAGE_SIZE < (unsigned long) tmp->addr)
89                         break;
90                 next = (void *) (tmp->size + (unsigned long) tmp->addr);
91                 if (next > addr) 
92                         addr = next;
93         }
94
95         if ((unsigned long)addr + size >= MODULES_END) {
96                 write_unlock(&vmlist_lock);
97                 kfree(area); 
98                 return NULL;
99         }
100         DEBUGP("addr %p\n", addr);
101
102         area->next = *p;
103         *p = area;
104         area->size = size + PAGE_SIZE;
105         area->addr = addr;
106         write_unlock(&vmlist_lock);
107
108         nr_pages = size >> PAGE_SHIFT;
109         array_size = (nr_pages * sizeof(struct page *));
110
111         area->nr_pages = nr_pages;
112         area->pages = pages = kmalloc(array_size, GFP_KERNEL);
113         if (!area->pages) 
114                 goto fail;
115
116         memset(area->pages, 0, array_size);
117         for (i = 0; i < nr_pages; i++) {
118                 area->pages[i] = alloc_page(GFP_KERNEL);
119                 if (area->pages[i] == NULL)
120                         goto fail;
121         }
122         
123         if (map_vm_area(area, PAGE_KERNEL_EXEC, &pages))
124                 goto fail;
125         
126         memset(addr, 0, size);
127         DEBUGP("module_alloc size %lu = %p\n", size, addr);
128         return addr;
129
130 fail:
131         module_free(NULL, addr);
132         return NULL;
133 }
134
135 /* We don't need anything special. */
136 int module_frob_arch_sections(Elf_Ehdr *hdr,
137                               Elf_Shdr *sechdrs,
138                               char *secstrings,
139                               struct module *mod)
140 {
141         return 0;
142 }
143
144 int apply_relocate_add(Elf64_Shdr *sechdrs,
145                    const char *strtab,
146                    unsigned int symindex,
147                    unsigned int relsec,
148                    struct module *me)
149 {
150         unsigned int i;
151         Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
152         Elf64_Sym *sym;
153         void *loc;
154         u64 val; 
155
156         DEBUGP("Applying relocate section %u to %u\n", relsec,
157                sechdrs[relsec].sh_info);
158         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
159                 /* This is where to make the change */
160                 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
161                         + rel[i].r_offset;
162
163                 /* This is the symbol it is referring to.  Note that all
164                    undefined symbols have been resolved.  */
165                 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
166                         + ELF64_R_SYM(rel[i].r_info);
167
168                 DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
169                        (int)ELF64_R_TYPE(rel[i].r_info), 
170                        sym->st_value, rel[i].r_addend, (u64)loc);
171
172                 val = sym->st_value + rel[i].r_addend; 
173
174                 switch (ELF64_R_TYPE(rel[i].r_info)) {
175                 case R_X86_64_NONE:
176                         break;
177                 case R_X86_64_64:
178                         *(u64 *)loc = val;
179                         break;
180                 case R_X86_64_32:
181                         *(u32 *)loc = val;
182                         if (val != *(u32 *)loc)
183                                 goto overflow;
184                         break;
185                 case R_X86_64_32S:
186                         *(s32 *)loc = val;
187                         if ((s64)val != *(s32 *)loc)
188                                 goto overflow;
189                         break;
190                 case R_X86_64_PC32: 
191                         val -= (u64)loc;
192                         *(u32 *)loc = val;
193 #if 0
194                         if ((s64)val != *(s32 *)loc)
195                                 goto overflow; 
196 #endif
197                         break;
198                 default:
199                         printk(KERN_ERR "module %s: Unknown rela relocation: %Lu\n",
200                                me->name, ELF64_R_TYPE(rel[i].r_info));
201                         return -ENOEXEC;
202                 }
203         }
204         return 0;
205
206 overflow:
207         printk(KERN_ERR "overflow in relocation type %d val %Lx\n", 
208                (int)ELF64_R_TYPE(rel[i].r_info), val);
209         printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
210                me->name);
211         return -ENOEXEC;
212 }
213
214 int apply_relocate(Elf_Shdr *sechdrs,
215                    const char *strtab,
216                    unsigned int symindex,
217                    unsigned int relsec,
218                    struct module *me)
219 {
220         printk("non add relocation not supported\n");
221         return -ENOSYS;
222
223
224 extern void apply_alternatives(void *start, void *end); 
225
226 int module_finalize(const Elf_Ehdr *hdr,
227                     const Elf_Shdr *sechdrs,
228                     struct module *me)
229 {
230         const Elf_Shdr *s;
231         char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
232
233         /* look for .altinstructions to patch */ 
234         for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { 
235                 void *seg;              
236                 if (strcmp(".altinstructions", secstrings + s->sh_name))
237                         continue;
238                 seg = (void *)s->sh_addr; 
239                 apply_alternatives(seg, seg + s->sh_size); 
240         }       
241         return 0;
242 }
243
244 void module_arch_cleanup(struct module *mod)
245 {
246 }