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