blob: c23880b90b5c7d9f89ede70564ba741afc729ce5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Andrea Righi27ac7922008-07-23 21:28:13 -070025#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/slab.h>
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -080027#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/system.h>
30#include <asm/page.h>
31#include <asm/pgtable.h>
32
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +053033#define DEBUGP(fmt...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Paolo 'Blaisorblade' Giarrusso23352fc2005-05-05 16:15:16 -070035#ifndef CONFIG_UML
Linus Torvalds1da177e2005-04-16 15:20:36 -070036void module_free(struct module *mod, void *module_region)
37{
38 vfree(module_region);
Paolo 'Blaisorblade' Giarrusso23352fc2005-05-05 16:15:16 -070039 /* FIXME: If module_region == mod->init_region, trim exception
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +053040 table entries. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
43void *module_alloc(unsigned long size)
44{
45 struct vm_struct *area;
46
47 if (!size)
48 return NULL;
49 size = PAGE_ALIGN(size);
50 if (size > MODULES_LEN)
51 return NULL;
52
53 area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
54 if (!area)
55 return NULL;
56
57 return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
58}
Paolo 'Blaisorblade' Giarrusso23352fc2005-05-05 16:15:16 -070059#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* We don't need anything special. */
62int module_frob_arch_sections(Elf_Ehdr *hdr,
63 Elf_Shdr *sechdrs,
64 char *secstrings,
65 struct module *mod)
66{
67 return 0;
68}
69
70int apply_relocate_add(Elf64_Shdr *sechdrs,
71 const char *strtab,
72 unsigned int symindex,
73 unsigned int relsec,
74 struct module *me)
75{
76 unsigned int i;
77 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
78 Elf64_Sym *sym;
79 void *loc;
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +053080 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 DEBUGP("Applying relocate section %u to %u\n", relsec,
83 sechdrs[relsec].sh_info);
84 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
85 /* This is where to make the change */
86 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
87 + rel[i].r_offset;
88
89 /* This is the symbol it is referring to. Note that all
90 undefined symbols have been resolved. */
91 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
92 + ELF64_R_SYM(rel[i].r_info);
93
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +053094 DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
95 (int)ELF64_R_TYPE(rel[i].r_info),
96 sym->st_value, rel[i].r_addend, (u64)loc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +053098 val = sym->st_value + rel[i].r_addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 switch (ELF64_R_TYPE(rel[i].r_info)) {
101 case R_X86_64_NONE:
102 break;
103 case R_X86_64_64:
104 *(u64 *)loc = val;
105 break;
106 case R_X86_64_32:
107 *(u32 *)loc = val;
108 if (val != *(u32 *)loc)
109 goto overflow;
110 break;
111 case R_X86_64_32S:
112 *(s32 *)loc = val;
113 if ((s64)val != *(s32 *)loc)
114 goto overflow;
115 break;
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +0530116 case R_X86_64_PC32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 val -= (u64)loc;
118 *(u32 *)loc = val;
119#if 0
120 if ((s64)val != *(s32 *)loc)
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +0530121 goto overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
123 break;
124 default:
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +0530125 printk(KERN_ERR "module %s: Unknown rela relocation: %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 me->name, ELF64_R_TYPE(rel[i].r_info));
127 return -ENOEXEC;
128 }
129 }
130 return 0;
131
132overflow:
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +0530133 printk(KERN_ERR "overflow in relocation type %d val %Lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 (int)ELF64_R_TYPE(rel[i].r_info), val);
135 printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
136 me->name);
137 return -ENOEXEC;
138}
139
140int apply_relocate(Elf_Shdr *sechdrs,
141 const char *strtab,
142 unsigned int symindex,
143 unsigned int relsec,
144 struct module *me)
145{
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +0530146 printk(KERN_ERR "non add relocation not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return -ENOSYS;
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +0530148}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150int module_finalize(const Elf_Ehdr *hdr,
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +0530151 const Elf_Shdr *sechdrs,
152 struct module *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Anders H Kaseorg5e5a29b2008-06-28 18:25:41 -0400154 const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
155 *para = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
157
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200158 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
159 if (!strcmp(".text", secstrings + s->sh_name))
160 text = s;
161 if (!strcmp(".altinstructions", secstrings + s->sh_name))
162 alt = s;
163 if (!strcmp(".smp_locks", secstrings + s->sh_name))
Jaswinder Singh Rajput3b9dc9f2009-01-12 14:46:48 +0530164 locks = s;
Anders H Kaseorg5e5a29b2008-06-28 18:25:41 -0400165 if (!strcmp(".parainstructions", secstrings + s->sh_name))
166 para = s;
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200167 }
168
169 if (alt) {
170 /* patch .altinstructions */
171 void *aseg = (void *)alt->sh_addr;
172 apply_alternatives(aseg, aseg + alt->sh_size);
173 }
174 if (locks && text) {
175 void *lseg = (void *)locks->sh_addr;
176 void *tseg = (void *)text->sh_addr;
177 alternatives_smp_module_add(me, me->name,
178 lseg, lseg + locks->sh_size,
179 tseg, tseg + text->sh_size);
180 }
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800181
Anders H Kaseorg5e5a29b2008-06-28 18:25:41 -0400182 if (para) {
183 void *pseg = (void *)para->sh_addr;
184 apply_paravirt(pseg, pseg + para->sh_size);
185 }
186
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800187 return module_bug_finalize(hdr, sechdrs, me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190void module_arch_cleanup(struct module *mod)
191{
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200192 alternatives_smp_module_del(mod);
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800193 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}