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