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