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