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