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