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