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