mips: fix dynamic ftrace
[openwrt/openwrt.git] / target / linux / generic / pending-3.18 / 305-mips_module_reloc.patch
1 --- a/arch/mips/Makefile
2 +++ b/arch/mips/Makefile
3 @@ -90,8 +90,18 @@ all-$(CONFIG_SYS_SUPPORTS_ZBOOT)+= vmlin
4 cflags-y += -G 0 -mno-abicalls -fno-pic -pipe -mno-branch-likely
5 cflags-y += -msoft-float
6 LDFLAGS_vmlinux += -G 0 -static -n -nostdlib --gc-sections
7 +ifdef CONFIG_64BIT
8 KBUILD_AFLAGS_MODULE += -mlong-calls
9 KBUILD_CFLAGS_MODULE += -mlong-calls
10 +else
11 + ifdef CONFIG_DYNAMIC_FTRACE
12 + KBUILD_AFLAGS_MODULE += -mlong-calls
13 + KBUILD_CFLAGS_MODULE += -mlong-calls
14 + else
15 + KBUILD_AFLAGS_MODULE += -mno-long-calls
16 + KBUILD_CFLAGS_MODULE += -mno-long-calls
17 + endif
18 +endif
19
20 ifndef CONFIG_FUNCTION_TRACER
21 KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
22 --- a/arch/mips/include/asm/module.h
23 +++ b/arch/mips/include/asm/module.h
24 @@ -11,6 +11,11 @@ struct mod_arch_specific {
25 const struct exception_table_entry *dbe_start;
26 const struct exception_table_entry *dbe_end;
27 struct mips_hi16 *r_mips_hi16_list;
28 +
29 + void *phys_plt_tbl;
30 + void *virt_plt_tbl;
31 + unsigned int phys_plt_offset;
32 + unsigned int virt_plt_offset;
33 };
34
35 typedef uint8_t Elf64_Byte; /* Type for a 8-bit quantity. */
36 --- a/arch/mips/kernel/module.c
37 +++ b/arch/mips/kernel/module.c
38 @@ -43,14 +43,222 @@ struct mips_hi16 {
39 static LIST_HEAD(dbe_list);
40 static DEFINE_SPINLOCK(dbe_lock);
41
42 -#ifdef MODULE_START
43 +/*
44 + * Get the potential max trampolines size required of the init and
45 + * non-init sections. Only used if we cannot find enough contiguous
46 + * physically mapped memory to put the module into.
47 + */
48 +static unsigned int
49 +get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
50 + const char *secstrings, unsigned int symindex, bool is_init)
51 +{
52 + unsigned long ret = 0;
53 + unsigned int i, j;
54 + Elf_Sym *syms;
55 +
56 + /* Everything marked ALLOC (this includes the exported symbols) */
57 + for (i = 1; i < hdr->e_shnum; ++i) {
58 + unsigned int info = sechdrs[i].sh_info;
59 +
60 + if (sechdrs[i].sh_type != SHT_REL
61 + && sechdrs[i].sh_type != SHT_RELA)
62 + continue;
63 +
64 + /* Not a valid relocation section? */
65 + if (info >= hdr->e_shnum)
66 + continue;
67 +
68 + /* Don't bother with non-allocated sections */
69 + if (!(sechdrs[info].sh_flags & SHF_ALLOC))
70 + continue;
71 +
72 + /* If it's called *.init*, and we're not init, we're
73 + not interested */
74 + if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
75 + != is_init)
76 + continue;
77 +
78 + syms = (Elf_Sym *) sechdrs[symindex].sh_addr;
79 + if (sechdrs[i].sh_type == SHT_REL) {
80 + Elf_Mips_Rel *rel = (void *) sechdrs[i].sh_addr;
81 + unsigned int size = sechdrs[i].sh_size / sizeof(*rel);
82 +
83 + for (j = 0; j < size; ++j) {
84 + Elf_Sym *sym;
85 +
86 + if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
87 + continue;
88 +
89 + sym = syms + ELF_MIPS_R_SYM(rel[j]);
90 + if (!is_init && sym->st_shndx != SHN_UNDEF)
91 + continue;
92 +
93 + ret += 4 * sizeof(int);
94 + }
95 + } else {
96 + Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
97 + unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
98 +
99 + for (j = 0; j < size; ++j) {
100 + Elf_Sym *sym;
101 +
102 + if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
103 + continue;
104 +
105 + sym = syms + ELF_MIPS_R_SYM(rela[j]);
106 + if (!is_init && sym->st_shndx != SHN_UNDEF)
107 + continue;
108 +
109 + ret += 4 * sizeof(int);
110 + }
111 + }
112 + }
113 +
114 + return ret;
115 +}
116 +
117 +#ifndef MODULE_START
118 +static void *alloc_phys(unsigned long size)
119 +{
120 + unsigned order;
121 + struct page *page;
122 + struct page *p;
123 +
124 + size = PAGE_ALIGN(size);
125 + order = get_order(size);
126 +
127 + page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
128 + __GFP_THISNODE, order);
129 + if (!page)
130 + return NULL;
131 +
132 + split_page(page, order);
133 +
134 + for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
135 + __free_page(p);
136 +
137 + return page_address(page);
138 +}
139 +#endif
140 +
141 +static void free_phys(void *ptr, unsigned long size)
142 +{
143 + struct page *page;
144 + struct page *end;
145 +
146 + page = virt_to_page(ptr);
147 + end = page + (PAGE_ALIGN(size) >> PAGE_SHIFT);
148 +
149 + for (; page < end; ++page)
150 + __free_page(page);
151 +}
152 +
153 +
154 void *module_alloc(unsigned long size)
155 {
156 +#ifdef MODULE_START
157 return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
158 GFP_KERNEL, PAGE_KERNEL, NUMA_NO_NODE,
159 __builtin_return_address(0));
160 +#else
161 + void *ptr;
162 +
163 + if (size == 0)
164 + return NULL;
165 +
166 + ptr = alloc_phys(size);
167 +
168 + /* If we failed to allocate physically contiguous memory,
169 + * fall back to regular vmalloc. The module loader code will
170 + * create jump tables to handle long jumps */
171 + if (!ptr)
172 + return vmalloc(size);
173 +
174 + return ptr;
175 +#endif
176 }
177 +
178 +static inline bool is_phys_addr(void *ptr)
179 +{
180 +#ifdef CONFIG_64BIT
181 + return (KSEGX((unsigned long)ptr) == CKSEG0);
182 +#else
183 + return (KSEGX(ptr) == KSEG0);
184 #endif
185 +}
186 +
187 +/* Free memory returned from module_alloc */
188 +void module_free(struct module *mod, void *module_region)
189 +{
190 + if (is_phys_addr(module_region)) {
191 + if (mod->module_init == module_region)
192 + free_phys(module_region, mod->init_size);
193 + else if (mod->module_core == module_region)
194 + free_phys(module_region, mod->core_size);
195 + else
196 + BUG();
197 + } else {
198 + vfree(module_region);
199 + }
200 +}
201 +
202 +static void *__module_alloc(int size, bool phys)
203 +{
204 + void *ptr;
205 +
206 + if (phys)
207 + ptr = kmalloc(size, GFP_KERNEL);
208 + else
209 + ptr = vmalloc(size);
210 + return ptr;
211 +}
212 +
213 +static void __module_free(void *ptr)
214 +{
215 + if (is_phys_addr(ptr))
216 + kfree(ptr);
217 + else
218 + vfree(ptr);
219 +}
220 +
221 +int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
222 + char *secstrings, struct module *mod)
223 +{
224 + unsigned int symindex = 0;
225 + unsigned int core_size, init_size;
226 + int i;
227 +
228 + mod->arch.phys_plt_offset = 0;
229 + mod->arch.virt_plt_offset = 0;
230 + mod->arch.phys_plt_tbl = NULL;
231 + mod->arch.virt_plt_tbl = NULL;
232 +
233 + if (IS_ENABLED(CONFIG_64BIT))
234 + return 0;
235 +
236 + for (i = 1; i < hdr->e_shnum; i++)
237 + if (sechdrs[i].sh_type == SHT_SYMTAB)
238 + symindex = i;
239 +
240 + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
241 + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
242 +
243 + if ((core_size + init_size) == 0)
244 + return 0;
245 +
246 + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
247 + if (!mod->arch.phys_plt_tbl)
248 + return -ENOMEM;
249 +
250 + mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
251 + if (!mod->arch.virt_plt_tbl) {
252 + __module_free(mod->arch.phys_plt_tbl);
253 + mod->arch.phys_plt_tbl = NULL;
254 + return -ENOMEM;
255 + }
256 +
257 + return 0;
258 +}
259
260 int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v)
261 {
262 @@ -64,8 +272,39 @@ static int apply_r_mips_32_rel(struct mo
263 return 0;
264 }
265
266 +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
267 + void *start, Elf_Addr v)
268 +{
269 + unsigned *tramp = start + *plt_offset;
270 + *plt_offset += 4 * sizeof(int);
271 +
272 + /* adjust carry for addiu */
273 + if (v & 0x00008000)
274 + v += 0x10000;
275 +
276 + tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
277 + tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
278 + tramp[2] = 0x03200008; /* jr t9 */
279 + tramp[3] = 0x00000000; /* nop */
280 +
281 + return (Elf_Addr) tramp;
282 +}
283 +
284 +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
285 +{
286 + if (is_phys_addr(location))
287 + return add_plt_entry_to(&me->arch.phys_plt_offset,
288 + me->arch.phys_plt_tbl, v);
289 + else
290 + return add_plt_entry_to(&me->arch.virt_plt_offset,
291 + me->arch.virt_plt_tbl, v);
292 +
293 +}
294 +
295 static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
296 {
297 + u32 ofs = *location & 0x03ffffff;
298 +
299 if (v % 4) {
300 pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
301 me->name);
302 @@ -73,14 +312,17 @@ static int apply_r_mips_26_rel(struct mo
303 }
304
305 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
306 - printk(KERN_ERR
307 - "module %s: relocation overflow\n",
308 - me->name);
309 - return -ENOEXEC;
310 + v = add_plt_entry(me, location, v + (ofs << 2));
311 + if (!v) {
312 + printk(KERN_ERR
313 + "module %s: relocation overflow\n", me->name);
314 + return -ENOEXEC;
315 + }
316 + ofs = 0;
317 }
318
319 *location = (*location & ~0x03ffffff) |
320 - ((*location + (v >> 2)) & 0x03ffffff);
321 + ((ofs + (v >> 2)) & 0x03ffffff);
322
323 return 0;
324 }
325 @@ -287,9 +529,36 @@ int module_finalize(const Elf_Ehdr *hdr,
326 list_add(&me->arch.dbe_list, &dbe_list);
327 spin_unlock_irq(&dbe_lock);
328 }
329 +
330 + /* Get rid of the fixup trampoline if we're running the module
331 + * from physically mapped address space */
332 + if (me->arch.phys_plt_offset == 0) {
333 + __module_free(me->arch.phys_plt_tbl);
334 + me->arch.phys_plt_tbl = NULL;
335 + }
336 + if (me->arch.virt_plt_offset == 0) {
337 + __module_free(me->arch.virt_plt_tbl);
338 + me->arch.virt_plt_tbl = NULL;
339 + }
340 +
341 return 0;
342 }
343
344 +void module_arch_freeing_init(struct module *mod)
345 +{
346 + if (mod->state == MODULE_STATE_LIVE)
347 + return;
348 +
349 + if (mod->arch.phys_plt_tbl) {
350 + __module_free(mod->arch.phys_plt_tbl);
351 + mod->arch.phys_plt_tbl = NULL;
352 + }
353 + if (mod->arch.virt_plt_tbl) {
354 + __module_free(mod->arch.virt_plt_tbl);
355 + mod->arch.virt_plt_tbl = NULL;
356 + }
357 +}
358 +
359 void module_arch_cleanup(struct module *mod)
360 {
361 spin_lock_irq(&dbe_lock);