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