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
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
10 KBUILD_AFLAGS_MODULE += -mlong-calls
11 KBUILD_CFLAGS_MODULE += -mlong-calls
13 +KBUILD_AFLAGS_MODULE += -mno-long-calls
14 +KBUILD_CFLAGS_MODULE += -mno-long-calls
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
24 const struct exception_table_entry *dbe_start;
25 const struct exception_table_entry *dbe_end;
26 struct mips_hi16 *r_mips_hi16_list;
30 + unsigned int phys_plt_offset;
31 + unsigned int virt_plt_offset;
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
40 static LIST_HEAD(dbe_list);
41 static DEFINE_SPINLOCK(dbe_lock);
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.
50 +get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
51 + const char *secstrings, unsigned int symindex, bool is_init)
53 + unsigned long ret = 0;
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;
61 + if (sechdrs[i].sh_type != SHT_REL
62 + && sechdrs[i].sh_type != SHT_RELA)
65 + /* Not a valid relocation section? */
66 + if (info >= hdr->e_shnum)
69 + /* Don't bother with non-allocated sections */
70 + if (!(sechdrs[info].sh_flags & SHF_ALLOC))
73 + /* If it's called *.init*, and we're not init, we're
75 + if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
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);
84 + for (j = 0; j < size; ++j) {
87 + if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
90 + sym = syms + ELF_MIPS_R_SYM(rel[j]);
91 + if (!is_init && sym->st_shndx != SHN_UNDEF)
94 + ret += 4 * sizeof(int);
97 + Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
98 + unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
100 + for (j = 0; j < size; ++j) {
103 + if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
106 + sym = syms + ELF_MIPS_R_SYM(rela[j]);
107 + if (!is_init && sym->st_shndx != SHN_UNDEF)
110 + ret += 4 * sizeof(int);
118 +#ifndef MODULE_START
119 +static void *alloc_phys(unsigned long size)
125 + size = PAGE_ALIGN(size);
126 + order = get_order(size);
128 + page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
129 + __GFP_THISNODE, order);
133 + split_page(page, order);
135 + for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
138 + return page_address(page);
142 +static void free_phys(void *ptr, unsigned long size)
147 + page = virt_to_page(ptr);
148 + end = page + (PAGE_ALIGN(size) >> PAGE_SHIFT);
150 + for (; page < end; ++page)
155 void *module_alloc(unsigned long size)
158 return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
159 GFP_KERNEL, PAGE_KERNEL, -1,
160 __builtin_return_address(0));
167 + ptr = alloc_phys(size);
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 */
173 + return vmalloc(size);
179 +static inline bool is_phys_addr(void *ptr)
182 + return (KSEGX((unsigned long)ptr) == CKSEG0);
184 + return (KSEGX(ptr) == KSEG0);
188 +/* Free memory returned from module_alloc */
189 +void module_free(struct module *mod, void *module_region)
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);
199 + vfree(module_region);
203 +static void *__module_alloc(int size, bool phys)
208 + ptr = kmalloc(size, GFP_KERNEL);
210 + ptr = vmalloc(size);
214 +static void __module_free(void *ptr)
216 + if (is_phys_addr(ptr))
222 +int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
223 + char *secstrings, struct module *mod)
225 + unsigned int symindex = 0;
226 + unsigned int core_size, init_size;
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;
234 + if (IS_ENABLED(CONFIG_64BIT))
237 + for (i = 1; i < hdr->e_shnum; i++)
238 + if (sechdrs[i].sh_type == SHT_SYMTAB)
241 + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
242 + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
244 + if ((core_size + init_size) == 0)
247 + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
248 + if (!mod->arch.phys_plt_tbl)
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;
261 int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v)
267 +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
268 + void *start, Elf_Addr v)
270 + unsigned *tramp = start + *plt_offset;
271 + *plt_offset += 4 * sizeof(int);
273 + /* adjust carry for addiu */
274 + if (v & 0x00008000)
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 */
282 + return (Elf_Addr) tramp;
285 +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
287 + if (is_phys_addr(location))
288 + return add_plt_entry_to(&me->arch.phys_plt_offset,
289 + me->arch.phys_plt_tbl, v);
291 + return add_plt_entry_to(&me->arch.virt_plt_offset,
292 + me->arch.virt_plt_tbl, v);
296 static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
298 + u32 ofs = *location & 0x03ffffff;
301 pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
306 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
308 - "module %s: relocation overflow\n",
311 + v = add_plt_entry(me, location, v + (ofs << 2));
314 + "module %s: relocation overflow\n", me->name);
320 *location = (*location & ~0x03ffffff) |
321 - ((*location + (v >> 2)) & 0x03ffffff);
322 + ((ofs + (v >> 2)) & 0x03ffffff);
326 @@ -286,11 +528,32 @@
327 list_add(&me->arch.dbe_list, &dbe_list);
328 spin_unlock_irq(&dbe_lock);
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;
337 + if (me->arch.virt_plt_offset == 0) {
338 + __module_free(me->arch.virt_plt_tbl);
339 + me->arch.virt_plt_tbl = NULL;
345 void module_arch_cleanup(struct module *mod)
347 + if (mod->arch.phys_plt_tbl) {
348 + __module_free(mod->arch.phys_plt_tbl);
349 + mod->arch.phys_plt_tbl = NULL;
351 + if (mod->arch.virt_plt_tbl) {
352 + __module_free(mod->arch.virt_plt_tbl);
353 + mod->arch.virt_plt_tbl = NULL;
356 spin_lock_irq(&dbe_lock);
357 list_del(&mod->arch.dbe_list);
358 spin_unlock_irq(&dbe_lock);