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