1 --- a/arch/mips/Makefile
2 +++ b/arch/mips/Makefile
3 @@ -83,7 +83,7 @@ all-$(CONFIG_BOOT_ELF64) := $(vmlinux-64
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
10 cflags-y += -ffreestanding
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;
21 + unsigned int phys_plt_offset;
22 + unsigned int virt_plt_offset;
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,116 @@ static struct mips_hi16 *mips_hi16_list;
29 static LIST_HEAD(dbe_list);
30 static DEFINE_SPINLOCK(dbe_lock);
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.
38 +get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
39 + const char *secstrings, unsigned int symindex, bool is_init)
41 + unsigned long ret = 0;
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;
49 + if (sechdrs[i].sh_type != SHT_REL
50 + && sechdrs[i].sh_type != SHT_RELA)
53 + /* Not a valid relocation section? */
54 + if (info >= hdr->e_shnum)
57 + /* Don't bother with non-allocated sections */
58 + if (!(sechdrs[info].sh_flags & SHF_ALLOC))
61 + /* If it's called *.init*, and we're not init, we're
63 + if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
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);
72 + for (j = 0; j < size; ++j) {
75 + if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
78 + sym = syms + ELF_MIPS_R_SYM(rel[j]);
79 + if (!is_init && sym->st_shndx != SHN_UNDEF)
82 + ret += 4 * sizeof(int);
85 + Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
86 + unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
88 + for (j = 0; j < size; ++j) {
91 + if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
94 + sym = syms + ELF_MIPS_R_SYM(rela[j]);
95 + if (!is_init && sym->st_shndx != SHN_UNDEF)
98 + ret += 4 * sizeof(int);
106 +#ifndef MODULE_START
107 +static void *alloc_phys(unsigned long size)
113 + size = PAGE_ALIGN(size);
114 + order = get_order(size);
116 + page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
117 + __GFP_THISNODE, order);
121 + split_page(page, order);
123 + for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
126 + return page_address(page);
130 +static void free_phys(void *ptr, unsigned long size)
135 + page = virt_to_page(ptr);
136 + end = page + (PAGE_ALIGN(size) >> PAGE_SHIFT);
138 + for (; page < end; ++page)
142 void *module_alloc(unsigned long size)
145 @@ -58,23 +168,101 @@ void *module_alloc(unsigned long size)
147 return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL);
153 - return vmalloc(size);
155 + ptr = alloc_phys(size);
157 + /* If we failed to allocate physically contiguous memory,
158 + * fall back to regular vmalloc. The module loader code will
159 + * create jump tables to handle long jumps */
161 + return vmalloc(size);
167 +static inline bool is_phys_addr(void *ptr)
170 + return (KSEGX((unsigned long)ptr) == CKSEG0);
172 + return (KSEGX(ptr) == KSEG0);
176 /* Free memory returned from module_alloc */
177 void module_free(struct module *mod, void *module_region)
179 - vfree(module_region);
180 + if (is_phys_addr(module_region)) {
181 + if (mod->module_init == module_region)
182 + free_phys(module_region, mod->init_size);
183 + else if (mod->module_core == module_region)
184 + free_phys(module_region, mod->core_size);
188 + vfree(module_region);
190 /* FIXME: If module_region == mod->init_region, trim exception
194 +static void *__module_alloc(int size, bool phys)
199 + ptr = kmalloc(size, GFP_KERNEL);
201 + ptr = vmalloc(size);
205 +static void __module_free(void *ptr)
207 + if (is_phys_addr(ptr))
213 int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
214 char *secstrings, struct module *mod)
216 + unsigned int symindex = 0;
217 + unsigned int core_size, init_size;
220 + for (i = 1; i < hdr->e_shnum; i++)
221 + if (sechdrs[i].sh_type == SHT_SYMTAB)
224 + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
225 + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
227 + mod->arch.phys_plt_offset = 0;
228 + mod->arch.virt_plt_offset = 0;
229 + mod->arch.phys_plt_tbl = NULL;
230 + mod->arch.virt_plt_tbl = NULL;
232 + if ((core_size + init_size) == 0)
235 + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
236 + if (!mod->arch.phys_plt_tbl)
239 + mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
240 + if (!mod->arch.virt_plt_tbl) {
241 + __module_free(mod->arch.phys_plt_tbl);
242 + mod->arch.phys_plt_tbl = NULL;
249 @@ -97,27 +285,37 @@ static int apply_r_mips_32_rela(struct m
253 -static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
254 +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
255 + void *start, Elf_Addr v)
258 - printk(KERN_ERR "module %s: dangerous relocation\n", me->name);
261 + unsigned *tramp = start + *plt_offset;
263 - if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
265 - "module %s: relocation overflow\n",
269 + *plt_offset += 4 * sizeof(int);
271 - *location = (*location & ~0x03ffffff) |
272 - ((*location + (v >> 2)) & 0x03ffffff);
273 + /* adjust carry for addiu */
274 + if (v & 0x00008000)
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 */
283 + return (Elf_Addr) tramp;
286 -static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
287 +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
289 + if (is_phys_addr(location))
290 + return add_plt_entry_to(&me->arch.phys_plt_offset,
291 + me->arch.phys_plt_tbl, v);
293 + return add_plt_entry_to(&me->arch.virt_plt_offset,
294 + me->arch.virt_plt_tbl, v);
298 +static int set_r_mips_26(struct module *me, u32 *location, u32 ofs, Elf_Addr v)
301 printk(KERN_ERR "module %s: dangerous relocation\n", me->name);
302 @@ -125,17 +323,31 @@ static int apply_r_mips_26_rela(struct m
305 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
307 + v = add_plt_entry(me, location, v + (ofs << 2));
310 "module %s: relocation overflow\n",
318 - *location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
319 + *location = (*location & ~0x03ffffff) | ((ofs + (v >> 2)) & 0x03ffffff);
324 +static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
326 + return set_r_mips_26(me, location, *location & 0x03ffffff, v);
329 +static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
331 + return set_r_mips_26(me, location, 0, v);
334 static int apply_r_mips_hi16_rel(struct module *me, u32 *location, Elf_Addr v)
337 @@ -400,11 +612,32 @@ int module_finalize(const Elf_Ehdr *hdr,
338 list_add(&me->arch.dbe_list, &dbe_list);
339 spin_unlock_irq(&dbe_lock);
342 + /* Get rid of the fixup trampoline if we're running the module
343 + * from physically mapped address space */
344 + if (me->arch.phys_plt_offset == 0) {
345 + __module_free(me->arch.phys_plt_tbl);
346 + me->arch.phys_plt_tbl = NULL;
348 + if (me->arch.virt_plt_offset == 0) {
349 + __module_free(me->arch.virt_plt_tbl);
350 + me->arch.virt_plt_tbl = NULL;
356 void module_arch_cleanup(struct module *mod)
358 + if (mod->arch.phys_plt_tbl) {
359 + __module_free(mod->arch.phys_plt_tbl);
360 + mod->arch.phys_plt_tbl = NULL;
362 + if (mod->arch.virt_plt_tbl) {
363 + __module_free(mod->arch.virt_plt_tbl);
364 + mod->arch.virt_plt_tbl = NULL;
367 spin_lock_irq(&dbe_lock);
368 list_del(&mod->arch.dbe_list);
369 spin_unlock_irq(&dbe_lock);