1 /* Kernel module help for x86-64
2 Copyright (C) 2001 Rusty Russell.
3 Copyright (C) 2002,2003 Andi Kleen, SuSE Labs.
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.
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.
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
19 #include <linux/moduleloader.h>
20 #include <linux/elf.h>
21 #include <linux/vmalloc.h>
23 #include <linux/string.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
27 #include <asm/system.h>
29 #include <asm/pgtable.h>
31 #define DEBUGP(fmt...)
33 static struct vm_struct *mod_vmlist;
35 void module_free(struct module *mod, void *module_region)
37 struct vm_struct **prevp, *map;
39 unsigned long addr = (unsigned long)module_region;
43 write_lock(&vmlist_lock);
44 for (prevp = &mod_vmlist ; (map = *prevp) ; prevp = &map->next) {
45 if ((unsigned long)map->addr == addr) {
50 write_unlock(&vmlist_lock);
51 printk("Trying to unmap nonexistent module vm area (%lx)\n", addr);
55 write_unlock(&vmlist_lock);
57 for (i = 0; i < map->nr_pages; i++)
59 __free_page(map->pages[i]);
65 void *module_alloc(unsigned long size)
67 struct vm_struct **p, *tmp, *area;
70 unsigned int nr_pages, array_size, i;
74 size = PAGE_ALIGN(size);
75 if (size > MODULES_LEN)
78 area = (struct vm_struct *) kmalloc(sizeof(*area), GFP_KERNEL);
81 memset(area, 0, sizeof(struct vm_struct));
83 write_lock(&vmlist_lock);
84 addr = (void *) MODULES_VADDR;
85 for (p = &mod_vmlist; (tmp = *p); p = &tmp->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)
90 next = (void *) (tmp->size + (unsigned long) tmp->addr);
95 if ((unsigned long)addr + size >= MODULES_END) {
96 write_unlock(&vmlist_lock);
100 DEBUGP("addr %p\n", addr);
104 area->size = size + PAGE_SIZE;
106 write_unlock(&vmlist_lock);
108 nr_pages = size >> PAGE_SHIFT;
109 array_size = (nr_pages * sizeof(struct page *));
111 area->nr_pages = nr_pages;
112 area->pages = pages = kmalloc(array_size, GFP_KERNEL);
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)
123 if (map_vm_area(area, PAGE_KERNEL_EXEC, &pages))
126 memset(addr, 0, size);
127 DEBUGP("module_alloc size %lu = %p\n", size, addr);
131 module_free(NULL, addr);
135 /* We don't need anything special. */
136 int module_frob_arch_sections(Elf_Ehdr *hdr,
144 int apply_relocate_add(Elf64_Shdr *sechdrs,
146 unsigned int symindex,
151 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
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
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);
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);
172 val = sym->st_value + rel[i].r_addend;
174 switch (ELF64_R_TYPE(rel[i].r_info)) {
182 if (val != *(u32 *)loc)
187 if ((s64)val != *(s32 *)loc)
194 if ((s64)val != *(s32 *)loc)
199 printk(KERN_ERR "module %s: Unknown rela relocation: %Lu\n",
200 me->name, ELF64_R_TYPE(rel[i].r_info));
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",
214 int apply_relocate(Elf_Shdr *sechdrs,
216 unsigned int symindex,
220 printk("non add relocation not supported\n");
224 extern void apply_alternatives(void *start, void *end);
226 int module_finalize(const Elf_Ehdr *hdr,
227 const Elf_Shdr *sechdrs,
231 char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
233 /* look for .altinstructions to patch */
234 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
236 if (strcmp(".altinstructions", secstrings + s->sh_name))
238 seg = (void *)s->sh_addr;
239 apply_alternatives(seg, seg + s->sh_size);
244 void module_arch_cleanup(struct module *mod)