[POWERPC] Remove ioremap64 and fixup_bigphys_addr
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / powerpc / kernel / module_32.c
CommitLineData
1da177e4
LT
1/* Kernel module help for PPC.
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/module.h>
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/cache.h>
26
21c4ff80
BH
27#include "setup.h"
28
1da177e4
LT
29#if 0
30#define DEBUGP printk
31#else
32#define DEBUGP(fmt , ...)
33#endif
34
35LIST_HEAD(module_bug_list);
36
37void *module_alloc(unsigned long size)
38{
39 if (size == 0)
40 return NULL;
41 return vmalloc(size);
42}
43
44/* Free memory returned from module_alloc */
45void module_free(struct module *mod, void *module_region)
46{
47 vfree(module_region);
48 /* FIXME: If module_region == mod->init_region, trim exception
49 table entries. */
50}
51
52/* Count how many different relocations (different symbol, different
53 addend) */
54static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
55{
56 unsigned int i, j, ret = 0;
57
58 /* Sure, this is order(n^2), but it's usually short, and not
59 time critical */
60 for (i = 0; i < num; i++) {
61 for (j = 0; j < i; j++) {
62 /* If this addend appeared before, it's
63 already been counted */
64 if (ELF32_R_SYM(rela[i].r_info)
65 == ELF32_R_SYM(rela[j].r_info)
66 && rela[i].r_addend == rela[j].r_addend)
67 break;
68 }
69 if (j == i) ret++;
70 }
71 return ret;
72}
73
74/* Get the potential trampolines size required of the init and
75 non-init sections */
76static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
77 const Elf32_Shdr *sechdrs,
78 const char *secstrings,
79 int is_init)
80{
81 unsigned long ret = 0;
82 unsigned i;
83
84 /* Everything marked ALLOC (this includes the exported
85 symbols) */
86 for (i = 1; i < hdr->e_shnum; i++) {
87 /* If it's called *.init*, and we're not init, we're
88 not interested */
89 if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
90 != is_init)
91 continue;
92
93 /* We don't want to look at debug sections. */
94 if (strstr(secstrings + sechdrs[i].sh_name, ".debug") != 0)
95 continue;
96
97 if (sechdrs[i].sh_type == SHT_RELA) {
98 DEBUGP("Found relocations in section %u\n", i);
99 DEBUGP("Ptr: %p. Number: %u\n",
100 (void *)hdr + sechdrs[i].sh_offset,
101 sechdrs[i].sh_size / sizeof(Elf32_Rela));
102 ret += count_relocs((void *)hdr
103 + sechdrs[i].sh_offset,
104 sechdrs[i].sh_size
105 / sizeof(Elf32_Rela))
106 * sizeof(struct ppc_plt_entry);
107 }
108 }
109
110 return ret;
111}
112
113int module_frob_arch_sections(Elf32_Ehdr *hdr,
114 Elf32_Shdr *sechdrs,
115 char *secstrings,
116 struct module *me)
117{
118 unsigned int i;
119
120 /* Find .plt and .init.plt sections */
121 for (i = 0; i < hdr->e_shnum; i++) {
122 if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
123 me->arch.init_plt_section = i;
124 else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
125 me->arch.core_plt_section = i;
126 }
127 if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
128 printk("Module doesn't contain .plt or .init.plt sections.\n");
129 return -ENOEXEC;
130 }
131
132 /* Override their sizes */
133 sechdrs[me->arch.core_plt_section].sh_size
134 = get_plt_size(hdr, sechdrs, secstrings, 0);
135 sechdrs[me->arch.init_plt_section].sh_size
136 = get_plt_size(hdr, sechdrs, secstrings, 1);
137 return 0;
138}
139
140int apply_relocate(Elf32_Shdr *sechdrs,
141 const char *strtab,
142 unsigned int symindex,
143 unsigned int relsec,
144 struct module *module)
145{
146 printk(KERN_ERR "%s: Non-ADD RELOCATION unsupported\n",
147 module->name);
148 return -ENOEXEC;
149}
150
151static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
152{
153 if (entry->jump[0] == 0x3d600000 + ((val + 0x8000) >> 16)
154 && entry->jump[1] == 0x396b0000 + (val & 0xffff))
155 return 1;
156 return 0;
157}
158
159/* Set up a trampoline in the PLT to bounce us to the distant function */
160static uint32_t do_plt_call(void *location,
161 Elf32_Addr val,
162 Elf32_Shdr *sechdrs,
163 struct module *mod)
164{
165 struct ppc_plt_entry *entry;
166
167 DEBUGP("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
168 /* Init, or core PLT? */
169 if (location >= mod->module_core
170 && location < mod->module_core + mod->core_size)
171 entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
172 else
173 entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
174
175 /* Find this entry, or if that fails, the next avail. entry */
176 while (entry->jump[0]) {
177 if (entry_matches(entry, val)) return (uint32_t)entry;
178 entry++;
179 }
180
181 /* Stolen from Paul Mackerras as well... */
182 entry->jump[0] = 0x3d600000+((val+0x8000)>>16); /* lis r11,sym@ha */
183 entry->jump[1] = 0x396b0000 + (val&0xffff); /* addi r11,r11,sym@l*/
184 entry->jump[2] = 0x7d6903a6; /* mtctr r11 */
185 entry->jump[3] = 0x4e800420; /* bctr */
186
187 DEBUGP("Initialized plt for 0x%x at %p\n", val, entry);
188 return (uint32_t)entry;
189}
190
191int apply_relocate_add(Elf32_Shdr *sechdrs,
192 const char *strtab,
193 unsigned int symindex,
194 unsigned int relsec,
195 struct module *module)
196{
197 unsigned int i;
198 Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
199 Elf32_Sym *sym;
200 uint32_t *location;
201 uint32_t value;
202
203 DEBUGP("Applying ADD relocate section %u to %u\n", relsec,
204 sechdrs[relsec].sh_info);
205 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
206 /* This is where to make the change */
207 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
208 + rela[i].r_offset;
209 /* This is the symbol it is referring to. Note that all
210 undefined symbols have been resolved. */
211 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
212 + ELF32_R_SYM(rela[i].r_info);
213 /* `Everything is relative'. */
214 value = sym->st_value + rela[i].r_addend;
215
216 switch (ELF32_R_TYPE(rela[i].r_info)) {
217 case R_PPC_ADDR32:
218 /* Simply set it */
219 *(uint32_t *)location = value;
220 break;
221
222 case R_PPC_ADDR16_LO:
223 /* Low half of the symbol */
224 *(uint16_t *)location = value;
225 break;
226
227 case R_PPC_ADDR16_HA:
228 /* Sign-adjusted lower 16 bits: PPC ELF ABI says:
229 (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF.
230 This is the same, only sane.
231 */
232 *(uint16_t *)location = (value + 0x8000) >> 16;
233 break;
234
235 case R_PPC_REL24:
236 if ((int)(value - (uint32_t)location) < -0x02000000
237 || (int)(value - (uint32_t)location) >= 0x02000000)
238 value = do_plt_call(location, value,
239 sechdrs, module);
240
241 /* Only replace bits 2 through 26 */
242 DEBUGP("REL24 value = %08X. location = %08X\n",
243 value, (uint32_t)location);
244 DEBUGP("Location before: %08X.\n",
245 *(uint32_t *)location);
246 *(uint32_t *)location
247 = (*(uint32_t *)location & ~0x03fffffc)
248 | ((value - (uint32_t)location)
249 & 0x03fffffc);
250 DEBUGP("Location after: %08X.\n",
251 *(uint32_t *)location);
252 DEBUGP("ie. jump to %08X+%08X = %08X\n",
253 *(uint32_t *)location & 0x03fffffc,
254 (uint32_t)location,
255 (*(uint32_t *)location & 0x03fffffc)
256 + (uint32_t)location);
257 break;
258
259 case R_PPC_REL32:
260 /* 32-bit relative jump. */
261 *(uint32_t *)location = value - (uint32_t)location;
262 break;
263
264 default:
265 printk("%s: unknown ADD relocation: %u\n",
266 module->name,
267 ELF32_R_TYPE(rela[i].r_info));
268 return -ENOEXEC;
269 }
270 }
271 return 0;
272}
273
21c4ff80
BH
274static const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
275 const Elf_Shdr *sechdrs,
276 const char *name)
277{
278 char *secstrings;
279 unsigned int i;
280
281 secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
282 for (i = 1; i < hdr->e_shnum; i++)
283 if (strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
284 return &sechdrs[i];
285 return NULL;
286}
287
1da177e4
LT
288int module_finalize(const Elf_Ehdr *hdr,
289 const Elf_Shdr *sechdrs,
290 struct module *me)
291{
21c4ff80 292 const Elf_Shdr *sect;
1da177e4
LT
293
294 me->arch.bug_table = NULL;
295 me->arch.num_bugs = 0;
296
297 /* Find the __bug_table section, if present */
21c4ff80
BH
298 sect = find_section(hdr, sechdrs, "__bug_table");
299 if (sect != NULL) {
300 me->arch.bug_table = (void *) sect->sh_addr;
301 me->arch.num_bugs = sect->sh_size / sizeof(struct bug_entry);
1da177e4
LT
302 }
303
21c4ff80 304 /*
1da177e4
LT
305 * Strictly speaking this should have a spinlock to protect against
306 * traversals, but since we only traverse on BUG()s, a spinlock
307 * could potentially lead to deadlock and thus be counter-productive.
308 */
309 list_add(&me->arch.bug_list, &module_bug_list);
310
21c4ff80
BH
311 /* Apply feature fixups */
312 sect = find_section(hdr, sechdrs, "__ftr_fixup");
313 if (sect != NULL)
314 do_feature_fixups(cur_cpu_spec->cpu_features,
315 (void *)sect->sh_addr,
316 (void *)sect->sh_addr + sect->sh_size);
317
1da177e4
LT
318 return 0;
319}
320
321void module_arch_cleanup(struct module *mod)
322{
323 list_del(&mod->arch.bug_list);
324}
325
326struct bug_entry *module_find_bug(unsigned long bugaddr)
327{
328 struct mod_arch_specific *mod;
329 unsigned int i;
330 struct bug_entry *bug;
331
332 list_for_each_entry(mod, &module_bug_list, bug_list) {
333 bug = mod->bug_table;
334 for (i = 0; i < mod->num_bugs; ++i, ++bug)
335 if (bugaddr == bug->bug_addr)
336 return bug;
337 }
338 return NULL;
339}