[PATCH] x86-64: Don't trust boot_cpu_id in the mptable.
[pandora-kernel.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 #ifndef CONFIG_UML
34 void module_free(struct module *mod, void *module_region)
35 {
36         vfree(module_region);
37         /* FIXME: If module_region == mod->init_region, trim exception
38            table entries. */
39 }
40
41 void *module_alloc(unsigned long size)
42 {
43         struct vm_struct *area;
44
45         if (!size)
46                 return NULL;
47         size = PAGE_ALIGN(size);
48         if (size > MODULES_LEN)
49                 return NULL;
50
51         area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
52         if (!area)
53                 return NULL;
54
55         return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
56 }
57 #endif
58
59 /* We don't need anything special. */
60 int module_frob_arch_sections(Elf_Ehdr *hdr,
61                               Elf_Shdr *sechdrs,
62                               char *secstrings,
63                               struct module *mod)
64 {
65         return 0;
66 }
67
68 int apply_relocate_add(Elf64_Shdr *sechdrs,
69                    const char *strtab,
70                    unsigned int symindex,
71                    unsigned int relsec,
72                    struct module *me)
73 {
74         unsigned int i;
75         Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
76         Elf64_Sym *sym;
77         void *loc;
78         u64 val; 
79
80         DEBUGP("Applying relocate section %u to %u\n", relsec,
81                sechdrs[relsec].sh_info);
82         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
83                 /* This is where to make the change */
84                 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
85                         + rel[i].r_offset;
86
87                 /* This is the symbol it is referring to.  Note that all
88                    undefined symbols have been resolved.  */
89                 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
90                         + ELF64_R_SYM(rel[i].r_info);
91
92                 DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
93                        (int)ELF64_R_TYPE(rel[i].r_info), 
94                        sym->st_value, rel[i].r_addend, (u64)loc);
95
96                 val = sym->st_value + rel[i].r_addend; 
97
98                 switch (ELF64_R_TYPE(rel[i].r_info)) {
99                 case R_X86_64_NONE:
100                         break;
101                 case R_X86_64_64:
102                         *(u64 *)loc = val;
103                         break;
104                 case R_X86_64_32:
105                         *(u32 *)loc = val;
106                         if (val != *(u32 *)loc)
107                                 goto overflow;
108                         break;
109                 case R_X86_64_32S:
110                         *(s32 *)loc = val;
111                         if ((s64)val != *(s32 *)loc)
112                                 goto overflow;
113                         break;
114                 case R_X86_64_PC32: 
115                         val -= (u64)loc;
116                         *(u32 *)loc = val;
117 #if 0
118                         if ((s64)val != *(s32 *)loc)
119                                 goto overflow; 
120 #endif
121                         break;
122                 default:
123                         printk(KERN_ERR "module %s: Unknown rela relocation: %Lu\n",
124                                me->name, ELF64_R_TYPE(rel[i].r_info));
125                         return -ENOEXEC;
126                 }
127         }
128         return 0;
129
130 overflow:
131         printk(KERN_ERR "overflow in relocation type %d val %Lx\n", 
132                (int)ELF64_R_TYPE(rel[i].r_info), val);
133         printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
134                me->name);
135         return -ENOEXEC;
136 }
137
138 int apply_relocate(Elf_Shdr *sechdrs,
139                    const char *strtab,
140                    unsigned int symindex,
141                    unsigned int relsec,
142                    struct module *me)
143 {
144         printk("non add relocation not supported\n");
145         return -ENOSYS;
146
147
148 extern void apply_alternatives(void *start, void *end); 
149
150 int module_finalize(const Elf_Ehdr *hdr,
151                     const Elf_Shdr *sechdrs,
152                     struct module *me)
153 {
154         const Elf_Shdr *s;
155         char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
156
157         /* look for .altinstructions to patch */ 
158         for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { 
159                 void *seg;              
160                 if (strcmp(".altinstructions", secstrings + s->sh_name))
161                         continue;
162                 seg = (void *)s->sh_addr; 
163                 apply_alternatives(seg, seg + s->sh_size); 
164         }       
165         return 0;
166 }
167
168 void module_arch_cleanup(struct module *mod)
169 {
170 }