ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 #include <linux/vmalloc.h>
27
28 #include <asm/system.h>
29 #include <asm/page.h>
30 #include <asm/pgtable.h>
31
32 #define DEBUGP(fmt...) 
33  
34 static struct vm_struct *mod_vmlist;
35
36 void module_free(struct module *mod, void *module_region)
37 {
38         struct vm_struct **prevp, *map;
39         int i;
40         unsigned long addr = (unsigned long)module_region;
41
42         if (!addr)
43                 return;
44         write_lock(&vmlist_lock); 
45         for (prevp = &mod_vmlist ; (map = *prevp) ; prevp = &map->next) {
46                 if ((unsigned long)map->addr == addr) {
47                         *prevp = map->next;
48                         goto found;
49                 }
50         }
51         write_unlock(&vmlist_lock); 
52         printk("Trying to unmap nonexistent module vm area (%lx)\n", addr);
53         return;
54  found:
55         unmap_vm_area(map);
56         write_unlock(&vmlist_lock); 
57         if (map->pages) {
58                 for (i = 0; i < map->nr_pages; i++)
59                         if (map->pages[i])
60                                 __free_page(map->pages[i]);     
61                 kfree(map->pages);
62         }
63         kfree(map);                                     
64 }
65
66 void *module_alloc(unsigned long size)
67 {
68         struct vm_struct **p, *tmp, *area;
69         struct page **pages;
70         void *addr;
71         unsigned int nr_pages, array_size, i;
72
73         if (!size)
74                 return NULL; 
75         size = PAGE_ALIGN(size);
76         if (size > MODULES_LEN)
77                 return NULL;
78
79         area = (struct vm_struct *) kmalloc(sizeof(*area), GFP_KERNEL);
80         if (!area)
81                 return NULL;
82         memset(area, 0, sizeof(struct vm_struct));
83
84         write_lock(&vmlist_lock);
85         addr = (void *) MODULES_VADDR;
86         for (p = &mod_vmlist; (tmp = *p); p = &tmp->next) {
87                 void *next; 
88                 DEBUGP("vmlist %p %lu addr %p\n", tmp->addr, tmp->size, addr);
89                 if (size + (unsigned long) addr + PAGE_SIZE < (unsigned long) tmp->addr)
90                         break;
91                 next = (void *) (tmp->size + (unsigned long) tmp->addr);
92                 if (next > addr) 
93                         addr = next;
94         }
95
96         if ((unsigned long)addr + size >= MODULES_END) {
97                 write_unlock(&vmlist_lock);
98                 kfree(area); 
99                 return NULL;
100         }
101         DEBUGP("addr %p\n", addr);
102
103         area->next = *p;
104         *p = area;
105         area->size = size + PAGE_SIZE;
106         area->addr = addr;
107         write_unlock(&vmlist_lock);
108
109         nr_pages = size >> PAGE_SHIFT;
110         array_size = (nr_pages * sizeof(struct page *));
111
112         area->nr_pages = nr_pages;
113         area->pages = pages = kmalloc(array_size, GFP_KERNEL);
114         if (!area->pages) 
115                 goto fail;
116
117         memset(area->pages, 0, array_size);
118         for (i = 0; i < nr_pages; i++) {
119                 area->pages[i] = alloc_page(GFP_KERNEL);
120                 if (area->pages[i] == NULL)
121                         goto fail;
122         }
123         
124         if (map_vm_area(area, PAGE_KERNEL_EXECUTABLE, &pages))
125                 goto fail;
126         
127         memset(addr, 0, size);
128         DEBUGP("module_alloc size %lu = %p\n", size, addr);
129         return addr;
130
131 fail:
132         module_free(NULL, addr);
133         return NULL;
134 }
135
136 /* We don't need anything special. */
137 int module_frob_arch_sections(Elf_Ehdr *hdr,
138                               Elf_Shdr *sechdrs,
139                               char *secstrings,
140                               struct module *mod)
141 {
142         return 0;
143 }
144
145 int apply_relocate_add(Elf64_Shdr *sechdrs,
146                    const char *strtab,
147                    unsigned int symindex,
148                    unsigned int relsec,
149                    struct module *me)
150 {
151         unsigned int i;
152         Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
153         Elf64_Sym *sym;
154         void *loc;
155         u64 val; 
156
157         DEBUGP("Applying relocate section %u to %u\n", relsec,
158                sechdrs[relsec].sh_info);
159         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
160                 /* This is where to make the change */
161                 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
162                         + rel[i].r_offset;
163
164                 /* This is the symbol it is referring to.  Note that all
165                    undefined symbols have been resolved.  */
166                 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
167                         + ELF64_R_SYM(rel[i].r_info);
168
169                 DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
170                        (int)ELF64_R_TYPE(rel[i].r_info), 
171                        sym->st_value, rel[i].r_addend, (u64)loc);
172
173                 val = sym->st_value + rel[i].r_addend; 
174
175                 switch (ELF64_R_TYPE(rel[i].r_info)) {
176                 case R_X86_64_NONE:
177                         break;
178                 case R_X86_64_64:
179                         *(u64 *)loc = val;
180                         break;
181                 case R_X86_64_32:
182                         *(u32 *)loc = val;
183                         if (val != *(u32 *)loc)
184                                 goto overflow;
185                         break;
186                 case R_X86_64_32S:
187                         *(s32 *)loc = val;
188                         if ((s64)val != *(s32 *)loc)
189                                 goto overflow;
190                         break;
191                 case R_X86_64_PC32: 
192                         val -= (u64)loc;
193                         *(u32 *)loc = val;
194 #if 0
195                         if ((s64)val != *(s32 *)loc)
196                                 goto overflow; 
197 #endif
198                         break;
199                 default:
200                         printk(KERN_ERR "module %s: Unknown rela relocation: %Lu\n",
201                                me->name, ELF64_R_TYPE(rel[i].r_info));
202                         return -ENOEXEC;
203                 }
204         }
205         return 0;
206
207 overflow:
208         printk(KERN_ERR "overflow in relocation type %d val %Lx\n", 
209                (int)ELF64_R_TYPE(rel[i].r_info), val);
210         printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
211                me->name);
212         return -ENOEXEC;
213 }
214
215 int apply_relocate(Elf_Shdr *sechdrs,
216                    const char *strtab,
217                    unsigned int symindex,
218                    unsigned int relsec,
219                    struct module *me)
220 {
221         printk("non add relocation not supported\n");
222         return -ENOSYS;
223
224
225 extern void apply_alternatives(void *start, void *end); 
226
227 int module_finalize(const Elf_Ehdr *hdr,
228                     const Elf_Shdr *sechdrs,
229                     struct module *me)
230 {
231         const Elf_Shdr *s;
232         char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
233
234         /* look for .altinstructions to patch */ 
235         for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { 
236                 void *seg;              
237                 if (strcmp(".altinstructions", secstrings + s->sh_name))
238                         continue;
239                 seg = (void *)s->sh_addr; 
240                 apply_alternatives(seg, seg + s->sh_size); 
241         }       
242         return 0;
243 }
244
245 void module_arch_cleanup(struct module *mod)
246 {
247 }