1 // SPDX-License-Identifier: GPL-2.0-or-later !! 1 /* Kernel module help for sparc64. 2 /* Kernel module help for powerpc. !! 2 * 3 Copyright (C) 2001, 2003 Rusty Russell IBM !! 3 * Copyright (C) 2001 Rusty Russell. 4 Copyright (C) 2008 Freescale Semiconductor !! 4 * Copyright (C) 2002 David S. Miller. >> 5 */ 5 6 6 */ << 7 #include <linux/elf.h> << 8 #include <linux/moduleloader.h> 7 #include <linux/moduleloader.h> 9 #include <linux/err.h> !! 8 #include <linux/kernel.h> >> 9 #include <linux/elf.h> >> 10 #include <linux/vmalloc.h> >> 11 #include <linux/fs.h> >> 12 #include <linux/gfp.h> >> 13 #include <linux/string.h> >> 14 #include <linux/ctype.h> 10 #include <linux/mm.h> 15 #include <linux/mm.h> 11 #include <linux/bug.h> !! 16 12 #include <asm/module.h> !! 17 #include <asm/processor.h> 13 #include <linux/uaccess.h> !! 18 #include <asm/spitfire.h> 14 #include <asm/firmware.h> !! 19 #include <asm/cacheflush.h> 15 #include <linux/sort.h> !! 20 16 #include <asm/setup.h> !! 21 #include "entry.h" 17 #include <asm/sections.h> !! 22 18 !! 23 #ifdef CONFIG_SPARC64 19 static const Elf_Shdr *find_section(const Elf_ !! 24 20 const Elf_ !! 25 #include <linux/jump_label.h> 21 const char !! 26 >> 27 static void *module_map(unsigned long size) >> 28 { >> 29 if (PAGE_ALIGN(size) > MODULES_LEN) >> 30 return NULL; >> 31 return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END, >> 32 GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE, >> 33 __builtin_return_address(0)); >> 34 } >> 35 #else >> 36 static void *module_map(unsigned long size) >> 37 { >> 38 return vmalloc(size); >> 39 } >> 40 #endif /* CONFIG_SPARC64 */ >> 41 >> 42 void *module_alloc(unsigned long size) >> 43 { >> 44 void *ret; >> 45 >> 46 ret = module_map(size); >> 47 if (ret) >> 48 memset(ret, 0, size); >> 49 >> 50 return ret; >> 51 } >> 52 >> 53 /* Make generic code ignore STT_REGISTER dummy undefined symbols. */ >> 54 int module_frob_arch_sections(Elf_Ehdr *hdr, >> 55 Elf_Shdr *sechdrs, >> 56 char *secstrings, >> 57 struct module *mod) >> 58 { >> 59 unsigned int symidx; >> 60 Elf_Sym *sym; >> 61 char *strtab; >> 62 int i; >> 63 >> 64 for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) { >> 65 if (symidx == hdr->e_shnum-1) { >> 66 printk("%s: no symtab found.\n", mod->name); >> 67 return -ENOEXEC; >> 68 } >> 69 } >> 70 sym = (Elf_Sym *)sechdrs[symidx].sh_addr; >> 71 strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr; >> 72 >> 73 for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) { >> 74 if (sym[i].st_shndx == SHN_UNDEF) { >> 75 if (ELF_ST_TYPE(sym[i].st_info) == STT_REGISTER) >> 76 sym[i].st_shndx = SHN_ABS; >> 77 } >> 78 } >> 79 return 0; >> 80 } >> 81 >> 82 int apply_relocate_add(Elf_Shdr *sechdrs, >> 83 const char *strtab, >> 84 unsigned int symindex, >> 85 unsigned int relsec, >> 86 struct module *me) 22 { 87 { 23 char *secstrings; << 24 unsigned int i; 88 unsigned int i; >> 89 Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr; >> 90 Elf_Sym *sym; >> 91 u8 *location; >> 92 u32 *loc32; >> 93 >> 94 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { >> 95 Elf_Addr v; >> 96 >> 97 /* This is where to make the change */ >> 98 location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr >> 99 + rel[i].r_offset; >> 100 loc32 = (u32 *) location; >> 101 >> 102 #ifdef CONFIG_SPARC64 >> 103 BUG_ON(((u64)location >> (u64)32) != (u64)0); >> 104 #endif /* CONFIG_SPARC64 */ >> 105 >> 106 /* This is the symbol it is referring to. Note that all >> 107 undefined symbols have been resolved. */ >> 108 sym = (Elf_Sym *)sechdrs[symindex].sh_addr >> 109 + ELF_R_SYM(rel[i].r_info); >> 110 v = sym->st_value + rel[i].r_addend; >> 111 >> 112 switch (ELF_R_TYPE(rel[i].r_info) & 0xff) { >> 113 case R_SPARC_DISP32: >> 114 v -= (Elf_Addr) location; >> 115 *loc32 = v; >> 116 break; >> 117 #ifdef CONFIG_SPARC64 >> 118 case R_SPARC_64: >> 119 location[0] = v >> 56; >> 120 location[1] = v >> 48; >> 121 location[2] = v >> 40; >> 122 location[3] = v >> 32; >> 123 location[4] = v >> 24; >> 124 location[5] = v >> 16; >> 125 location[6] = v >> 8; >> 126 location[7] = v >> 0; >> 127 break; >> 128 >> 129 case R_SPARC_WDISP19: >> 130 v -= (Elf_Addr) location; >> 131 *loc32 = (*loc32 & ~0x7ffff) | >> 132 ((v >> 2) & 0x7ffff); >> 133 break; >> 134 >> 135 case R_SPARC_OLO10: >> 136 *loc32 = (*loc32 & ~0x1fff) | >> 137 (((v & 0x3ff) + >> 138 (ELF_R_TYPE(rel[i].r_info) >> 8)) >> 139 & 0x1fff); >> 140 break; >> 141 #endif /* CONFIG_SPARC64 */ >> 142 >> 143 case R_SPARC_32: >> 144 case R_SPARC_UA32: >> 145 location[0] = v >> 24; >> 146 location[1] = v >> 16; >> 147 location[2] = v >> 8; >> 148 location[3] = v >> 0; >> 149 break; >> 150 >> 151 case R_SPARC_WDISP30: >> 152 v -= (Elf_Addr) location; >> 153 *loc32 = (*loc32 & ~0x3fffffff) | >> 154 ((v >> 2) & 0x3fffffff); >> 155 break; >> 156 >> 157 case R_SPARC_WDISP22: >> 158 v -= (Elf_Addr) location; >> 159 *loc32 = (*loc32 & ~0x3fffff) | >> 160 ((v >> 2) & 0x3fffff); >> 161 break; >> 162 >> 163 case R_SPARC_LO10: >> 164 *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff); >> 165 break; >> 166 >> 167 case R_SPARC_HI22: >> 168 *loc32 = (*loc32 & ~0x3fffff) | >> 169 ((v >> 10) & 0x3fffff); >> 170 break; >> 171 >> 172 default: >> 173 printk(KERN_ERR "module %s: Unknown relocation: %x\n", >> 174 me->name, >> 175 (int) (ELF_R_TYPE(rel[i].r_info) & 0xff)); >> 176 return -ENOEXEC; >> 177 } >> 178 } >> 179 return 0; >> 180 } 25 181 26 secstrings = (char *)hdr + sechdrs[hdr !! 182 #ifdef CONFIG_SPARC64 27 for (i = 1; i < hdr->e_shnum; i++) !! 183 static void do_patch_sections(const Elf_Ehdr *hdr, 28 if (strcmp(secstrings+sechdrs[ !! 184 const Elf_Shdr *sechdrs) 29 return &sechdrs[i]; !! 185 { 30 return NULL; !! 186 const Elf_Shdr *s, *sun4v_1insn = NULL, *sun4v_2insn = NULL; >> 187 char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; >> 188 >> 189 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { >> 190 if (!strcmp(".sun4v_1insn_patch", secstrings + s->sh_name)) >> 191 sun4v_1insn = s; >> 192 if (!strcmp(".sun4v_2insn_patch", secstrings + s->sh_name)) >> 193 sun4v_2insn = s; >> 194 } >> 195 >> 196 if (sun4v_1insn && tlb_type == hypervisor) { >> 197 void *p = (void *) sun4v_1insn->sh_addr; >> 198 sun4v_patch_1insn_range(p, p + sun4v_1insn->sh_size); >> 199 } >> 200 if (sun4v_2insn && tlb_type == hypervisor) { >> 201 void *p = (void *) sun4v_2insn->sh_addr; >> 202 sun4v_patch_2insn_range(p, p + sun4v_2insn->sh_size); >> 203 } 31 } 204 } 32 205 33 int module_finalize(const Elf_Ehdr *hdr, 206 int module_finalize(const Elf_Ehdr *hdr, 34 const Elf_Shdr *sechdrs, struc !! 207 const Elf_Shdr *sechdrs, >> 208 struct module *me) 35 { 209 { 36 const Elf_Shdr *sect; !! 210 /* make jump label nops */ 37 int rc; !! 211 jump_label_apply_nops(me); 38 212 39 rc = module_finalize_ftrace(me, sechdr !! 213 do_patch_sections(hdr, sechdrs); 40 if (rc) !! 214 41 return rc; !! 215 /* Cheetah's I-cache is fully coherent. */ 42 !! 216 if (tlb_type == spitfire) { 43 /* Apply feature fixups */ !! 217 unsigned long va; 44 sect = find_section(hdr, sechdrs, "__f !! 218 45 if (sect != NULL) !! 219 flushw_all(); 46 do_feature_fixups(cur_cpu_spec !! 220 for (va = 0; va < (PAGE_SIZE << 1); va += 32) 47 (void *)sect !! 221 spitfire_put_icache_tag(va, 0x0); 48 (void *)sect !! 222 __asm__ __volatile__("flush %g6"); 49 !! 223 } 50 sect = find_section(hdr, sechdrs, "__m << 51 if (sect != NULL) << 52 do_feature_fixups(cur_cpu_spec << 53 (void *)sect << 54 (void *)sect << 55 << 56 #ifdef CONFIG_PPC64 << 57 sect = find_section(hdr, sechdrs, "__f << 58 if (sect != NULL) << 59 do_feature_fixups(powerpc_firm << 60 (void *)sect << 61 (void *)sect << 62 #endif /* CONFIG_PPC64 */ << 63 << 64 #ifdef CONFIG_PPC64_ELF_ABI_V1 << 65 sect = find_section(hdr, sechdrs, ".op << 66 if (sect != NULL) { << 67 me->arch.start_opd = sect->sh_ << 68 me->arch.end_opd = sect->sh_ad << 69 } << 70 #endif /* CONFIG_PPC64_ELF_ABI_V1 */ << 71 << 72 #ifdef CONFIG_PPC_BARRIER_NOSPEC << 73 sect = find_section(hdr, sechdrs, "__s << 74 if (sect != NULL) << 75 do_barrier_nospec_fixups_range << 76 (void *)sect << 77 (void *)sect << 78 #endif /* CONFIG_PPC_BARRIER_NOSPEC */ << 79 << 80 sect = find_section(hdr, sechdrs, "__l << 81 if (sect != NULL) << 82 do_lwsync_fixups(cur_cpu_spec- << 83 (void *)sect- << 84 (void *)sect- << 85 224 86 return 0; 225 return 0; 87 } 226 } >> 227 #endif /* CONFIG_SPARC64 */ 88 228
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.