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