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