1 --- a/arch/mips/Makefile
2 +++ b/arch/mips/Makefile
3 @@ -93,8 +93,8 @@ 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 -KBUILD_AFLAGS_MODULE += -mlong-calls
8 -KBUILD_CFLAGS_MODULE += -mlong-calls
9 +KBUILD_AFLAGS_MODULE += -mno-long-calls
10 +KBUILD_CFLAGS_MODULE += -mno-long-calls
12 cflags-y += -ffreestanding
14 --- a/arch/mips/include/asm/module.h
15 +++ b/arch/mips/include/asm/module.h
16 @@ -9,6 +9,11 @@ struct mod_arch_specific {
17 struct list_head dbe_list;
18 const struct exception_table_entry *dbe_start;
19 const struct exception_table_entry *dbe_end;
23 + unsigned int phys_plt_offset;
24 + unsigned int virt_plt_offset;
27 typedef uint8_t Elf64_Byte; /* Type for a 8-bit quantity. */
28 --- a/arch/mips/kernel/module.c
29 +++ b/arch/mips/kernel/module.c
30 @@ -43,6 +43,117 @@ static struct mips_hi16 *mips_hi16_list;
31 static LIST_HEAD(dbe_list);
32 static DEFINE_SPINLOCK(dbe_lock);
35 + * Get the potential max trampolines size required of the init and
36 + * non-init sections. Only used if we cannot find enough contiguous
37 + * physically mapped memory to put the module into.
40 +get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
41 + const char *secstrings, unsigned int symindex, bool is_init)
43 + unsigned long ret = 0;
47 + /* Everything marked ALLOC (this includes the exported symbols) */
48 + for (i = 1; i < hdr->e_shnum; ++i) {
49 + unsigned int info = sechdrs[i].sh_info;
51 + if (sechdrs[i].sh_type != SHT_REL
52 + && sechdrs[i].sh_type != SHT_RELA)
55 + /* Not a valid relocation section? */
56 + if (info >= hdr->e_shnum)
59 + /* Don't bother with non-allocated sections */
60 + if (!(sechdrs[info].sh_flags & SHF_ALLOC))
63 + /* If it's called *.init*, and we're not init, we're
65 + if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
69 + syms = (Elf_Sym *) sechdrs[symindex].sh_addr;
70 + if (sechdrs[i].sh_type == SHT_REL) {
71 + Elf_Mips_Rel *rel = (void *) sechdrs[i].sh_addr;
72 + unsigned int size = sechdrs[i].sh_size / sizeof(*rel);
74 + for (j = 0; j < size; ++j) {
77 + if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
80 + sym = syms + ELF_MIPS_R_SYM(rel[j]);
81 + if (!is_init && sym->st_shndx != SHN_UNDEF)
84 + ret += 4 * sizeof(int);
87 + Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
88 + unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
90 + for (j = 0; j < size; ++j) {
93 + if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
96 + sym = syms + ELF_MIPS_R_SYM(rela[j]);
97 + if (!is_init && sym->st_shndx != SHN_UNDEF)
100 + ret += 4 * sizeof(int);
108 +#ifndef MODULE_START
109 +static void *alloc_phys(unsigned long size)
115 + size = PAGE_ALIGN(size);
116 + order = get_order(size);
118 + page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
119 + __GFP_THISNODE, order);
123 + split_page(page, order);
125 + for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
128 + return page_address(page);
132 +static void free_phys(void *ptr, unsigned long size)
137 + page = virt_to_page(ptr);
138 + end = page + (PAGE_ALIGN(size) >> PAGE_SHIFT);
140 + for (; page < end; ++page)
145 void *module_alloc(unsigned long size)
148 @@ -58,21 +169,99 @@ void *module_alloc(unsigned long size)
150 return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL);
156 - return vmalloc(size);
158 + ptr = alloc_phys(size);
160 + /* If we failed to allocate physically contiguous memory,
161 + * fall back to regular vmalloc. The module loader code will
162 + * create jump tables to handle long jumps */
164 + return vmalloc(size);
170 +static inline bool is_phys_addr(void *ptr)
173 + return (KSEGX((unsigned long)ptr) == CKSEG0);
175 + return (KSEGX(ptr) == KSEG0);
179 /* Free memory returned from module_alloc */
180 void module_free(struct module *mod, void *module_region)
182 - vfree(module_region);
183 + if (is_phys_addr(module_region)) {
184 + if (mod->module_init == module_region)
185 + free_phys(module_region, mod->init_size);
186 + else if (mod->module_core == module_region)
187 + free_phys(module_region, mod->core_size);
191 + vfree(module_region);
195 +static void *__module_alloc(int size, bool phys)
200 + ptr = kmalloc(size, GFP_KERNEL);
202 + ptr = vmalloc(size);
206 +static void __module_free(void *ptr)
208 + if (is_phys_addr(ptr))
214 int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
215 char *secstrings, struct module *mod)
217 + unsigned int symindex = 0;
218 + unsigned int core_size, init_size;
221 + for (i = 1; i < hdr->e_shnum; i++)
222 + if (sechdrs[i].sh_type == SHT_SYMTAB)
225 + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
226 + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
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;
233 + if ((core_size + init_size) == 0)
236 + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
237 + if (!mod->arch.phys_plt_tbl)
240 + mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
241 + if (!mod->arch.virt_plt_tbl) {
242 + __module_free(mod->arch.phys_plt_tbl);
243 + mod->arch.phys_plt_tbl = NULL;
250 @@ -95,28 +284,36 @@ static int apply_r_mips_32_rela(struct m
254 -static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
255 +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
256 + void *start, Elf_Addr v)
259 - pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
263 + unsigned *tramp = start + *plt_offset;
264 + *plt_offset += 4 * sizeof(int);
266 - if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
268 - "module %s: relocation overflow\n",
272 + /* adjust carry for addiu */
273 + if (v & 0x00008000)
276 - *location = (*location & ~0x03ffffff) |
277 - ((*location + (v >> 2)) & 0x03ffffff);
278 + tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
279 + tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
280 + tramp[2] = 0x03200008; /* jr t9 */
281 + tramp[3] = 0x00000000; /* nop */
284 + return (Elf_Addr) tramp;
287 -static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
288 +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
290 + if (is_phys_addr(location))
291 + return add_plt_entry_to(&me->arch.phys_plt_offset,
292 + me->arch.phys_plt_tbl, v);
294 + return add_plt_entry_to(&me->arch.virt_plt_offset,
295 + me->arch.virt_plt_tbl, v);
299 +static int set_r_mips_26(struct module *me, u32 *location, u32 ofs, Elf_Addr v)
302 pr_err("module %s: dangerous R_MIPS_26 RELArelocation\n",
303 @@ -125,17 +322,31 @@ static int apply_r_mips_26_rela(struct m
306 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
308 + v = add_plt_entry(me, location, v + (ofs << 2));
311 "module %s: relocation overflow\n",
319 - *location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
320 + *location = (*location & ~0x03ffffff) | ((ofs + (v >> 2)) & 0x03ffffff);
325 +static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
327 + return set_r_mips_26(me, location, *location & 0x03ffffff, v);
330 +static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
332 + return set_r_mips_26(me, location, 0, v);
335 static int apply_r_mips_hi16_rel(struct module *me, u32 *location, Elf_Addr v)
338 @@ -400,11 +611,32 @@ int module_finalize(const Elf_Ehdr *hdr,
339 list_add(&me->arch.dbe_list, &dbe_list);
340 spin_unlock_irq(&dbe_lock);
343 + /* Get rid of the fixup trampoline if we're running the module
344 + * from physically mapped address space */
345 + if (me->arch.phys_plt_offset == 0) {
346 + __module_free(me->arch.phys_plt_tbl);
347 + me->arch.phys_plt_tbl = NULL;
349 + if (me->arch.virt_plt_offset == 0) {
350 + __module_free(me->arch.virt_plt_tbl);
351 + me->arch.virt_plt_tbl = NULL;
357 void module_arch_cleanup(struct module *mod)
359 + if (mod->arch.phys_plt_tbl) {
360 + __module_free(mod->arch.phys_plt_tbl);
361 + mod->arch.phys_plt_tbl = NULL;
363 + if (mod->arch.virt_plt_tbl) {
364 + __module_free(mod->arch.virt_plt_tbl);
365 + mod->arch.virt_plt_tbl = NULL;
368 spin_lock_irq(&dbe_lock);
369 list_del(&mod->arch.dbe_list);
370 spin_unlock_irq(&dbe_lock);