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