blob: b92fbcf48609fe0cc3eda0cd8fb45ecc82960e1f [file] [log] [blame]
Amerigo Wang2d5bf282009-06-03 21:46:09 -04001/* Kernel module help for x86.
2 Copyright (C) 2001 Rusty Russell.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18#include <linux/moduleloader.h>
19#include <linux/elf.h>
20#include <linux/vmalloc.h>
21#include <linux/fs.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/bug.h>
25#include <linux/mm.h>
26
27#include <asm/system.h>
28#include <asm/page.h>
29#include <asm/pgtable.h>
30
31#if 0
32#define DEBUGP printk
33#else
34#define DEBUGP(fmt...)
35#endif
36
Amerigo Wang0fdc83b2009-06-03 21:46:19 -040037#if defined(CONFIG_UML) || defined(CONFIG_X86_32)
38void *module_alloc(unsigned long size)
39{
40 if (size == 0)
41 return NULL;
42 return vmalloc_exec(size);
43}
44#else /*X86_64*/
45void *module_alloc(unsigned long size)
46{
47 struct vm_struct *area;
48
49 if (!size)
50 return NULL;
51 size = PAGE_ALIGN(size);
52 if (size > MODULES_LEN)
53 return NULL;
54
55 area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
56 if (!area)
57 return NULL;
58
59 return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
60}
61#endif
62
Amerigo Wang2d5bf282009-06-03 21:46:09 -040063/* Free memory returned from module_alloc */
64void module_free(struct module *mod, void *module_region)
65{
66 vfree(module_region);
67 /* FIXME: If module_region == mod->init_region, trim exception
68 table entries. */
69}
70
71/* We don't need anything special. */
72int module_frob_arch_sections(Elf_Ehdr *hdr,
73 Elf_Shdr *sechdrs,
74 char *secstrings,
75 struct module *mod)
76{
77 return 0;
78}
79
Amerigo Wang0fdc83b2009-06-03 21:46:19 -040080#ifdef CONFIG_X86_32
81int apply_relocate(Elf32_Shdr *sechdrs,
82 const char *strtab,
83 unsigned int symindex,
84 unsigned int relsec,
85 struct module *me)
86{
87 unsigned int i;
88 Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
89 Elf32_Sym *sym;
90 uint32_t *location;
91
92 DEBUGP("Applying relocate section %u to %u\n", relsec,
93 sechdrs[relsec].sh_info);
94 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
95 /* This is where to make the change */
96 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
97 + rel[i].r_offset;
98 /* This is the symbol it is referring to. Note that all
99 undefined symbols have been resolved. */
100 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
101 + ELF32_R_SYM(rel[i].r_info);
102
103 switch (ELF32_R_TYPE(rel[i].r_info)) {
104 case R_386_32:
105 /* We add the value into the location given */
106 *location += sym->st_value;
107 break;
108 case R_386_PC32:
109 /* Add the value, subtract its postition */
110 *location += sym->st_value - (uint32_t)location;
111 break;
112 default:
113 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
114 me->name, ELF32_R_TYPE(rel[i].r_info));
115 return -ENOEXEC;
116 }
117 }
118 return 0;
119}
120
121int apply_relocate_add(Elf32_Shdr *sechdrs,
122 const char *strtab,
123 unsigned int symindex,
124 unsigned int relsec,
125 struct module *me)
126{
127 printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
128 me->name);
129 return -ENOEXEC;
130}
131#else /*X86_64*/
132int apply_relocate_add(Elf64_Shdr *sechdrs,
133 const char *strtab,
134 unsigned int symindex,
135 unsigned int relsec,
136 struct module *me)
137{
138 unsigned int i;
139 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
140 Elf64_Sym *sym;
141 void *loc;
142 u64 val;
143
144 DEBUGP("Applying relocate section %u to %u\n", relsec,
145 sechdrs[relsec].sh_info);
146 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
147 /* This is where to make the change */
148 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
149 + rel[i].r_offset;
150
151 /* This is the symbol it is referring to. Note that all
152 undefined symbols have been resolved. */
153 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
154 + ELF64_R_SYM(rel[i].r_info);
155
156 DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
157 (int)ELF64_R_TYPE(rel[i].r_info),
158 sym->st_value, rel[i].r_addend, (u64)loc);
159
160 val = sym->st_value + rel[i].r_addend;
161
162 switch (ELF64_R_TYPE(rel[i].r_info)) {
163 case R_X86_64_NONE:
164 break;
165 case R_X86_64_64:
166 *(u64 *)loc = val;
167 break;
168 case R_X86_64_32:
169 *(u32 *)loc = val;
170 if (val != *(u32 *)loc)
171 goto overflow;
172 break;
173 case R_X86_64_32S:
174 *(s32 *)loc = val;
175 if ((s64)val != *(s32 *)loc)
176 goto overflow;
177 break;
178 case R_X86_64_PC32:
179 val -= (u64)loc;
180 *(u32 *)loc = val;
181#if 0
182 if ((s64)val != *(s32 *)loc)
183 goto overflow;
184#endif
185 break;
186 default:
187 printk(KERN_ERR "module %s: Unknown rela relocation: %llu\n",
188 me->name, ELF64_R_TYPE(rel[i].r_info));
189 return -ENOEXEC;
190 }
191 }
192 return 0;
193
194overflow:
195 printk(KERN_ERR "overflow in relocation type %d val %Lx\n",
196 (int)ELF64_R_TYPE(rel[i].r_info), val);
197 printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
198 me->name);
199 return -ENOEXEC;
200}
201
202int apply_relocate(Elf_Shdr *sechdrs,
203 const char *strtab,
204 unsigned int symindex,
205 unsigned int relsec,
206 struct module *me)
207{
208 printk(KERN_ERR "non add relocation not supported\n");
209 return -ENOSYS;
210}
211
212#endif
213
Amerigo Wang2d5bf282009-06-03 21:46:09 -0400214int module_finalize(const Elf_Ehdr *hdr,
215 const Elf_Shdr *sechdrs,
216 struct module *me)
217{
218 const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
219 *para = NULL;
220 char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
221
222 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
223 if (!strcmp(".text", secstrings + s->sh_name))
224 text = s;
225 if (!strcmp(".altinstructions", secstrings + s->sh_name))
226 alt = s;
227 if (!strcmp(".smp_locks", secstrings + s->sh_name))
228 locks = s;
229 if (!strcmp(".parainstructions", secstrings + s->sh_name))
230 para = s;
231 }
232
233 if (alt) {
234 /* patch .altinstructions */
235 void *aseg = (void *)alt->sh_addr;
236 apply_alternatives(aseg, aseg + alt->sh_size);
237 }
238 if (locks && text) {
239 void *lseg = (void *)locks->sh_addr;
240 void *tseg = (void *)text->sh_addr;
241 alternatives_smp_module_add(me, me->name,
242 lseg, lseg + locks->sh_size,
243 tseg, tseg + text->sh_size);
244 }
245
246 if (para) {
247 void *pseg = (void *)para->sh_addr;
248 apply_paravirt(pseg, pseg + para->sh_size);
249 }
250
251 return module_bug_finalize(hdr, sechdrs, me);
252}
253
254void module_arch_cleanup(struct module *mod)
255{
256 alternatives_smp_module_del(mod);
257 module_bug_cleanup(mod);
258}