1 // SPDX-License-Identifier: GPL-2.0
2 /* Kernel module help for sparc64.
4 * Copyright (C) 2001 Rusty Russell.
5 * Copyright (C) 2002 David S. Miller.
8 #include <linux/moduleloader.h>
9 #include <linux/kernel.h>
10 #include <linux/elf.h>
11 #include <linux/vmalloc.h>
13 #include <linux/gfp.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
18 #include <asm/processor.h>
19 #include <asm/spitfire.h>
20 #include <asm/cacheflush.h>
26 #include <linux/jump_label.h>
28 static void *module_map(unsigned long size
)
30 if (PAGE_ALIGN(size
) > MODULES_LEN
)
32 return __vmalloc_node_range(size
, 1, MODULES_VADDR
, MODULES_END
,
33 GFP_KERNEL
, PAGE_KERNEL
, 0, NUMA_NO_NODE
,
34 __builtin_return_address(0));
37 static void *module_map(unsigned long size
)
41 #endif /* CONFIG_SPARC64 */
43 void *module_alloc(unsigned long size
)
47 ret
= module_map(size
);
54 /* Make generic code ignore STT_REGISTER dummy undefined symbols. */
55 int module_frob_arch_sections(Elf_Ehdr
*hdr
,
65 for (symidx
= 0; sechdrs
[symidx
].sh_type
!= SHT_SYMTAB
; symidx
++) {
66 if (symidx
== hdr
->e_shnum
-1) {
67 printk("%s: no symtab found.\n", mod
->name
);
71 sym
= (Elf_Sym
*)sechdrs
[symidx
].sh_addr
;
72 strtab
= (char *)sechdrs
[sechdrs
[symidx
].sh_link
].sh_addr
;
74 for (i
= 1; i
< sechdrs
[symidx
].sh_size
/ sizeof(Elf_Sym
); i
++) {
75 if (sym
[i
].st_shndx
== SHN_UNDEF
) {
76 if (ELF_ST_TYPE(sym
[i
].st_info
) == STT_REGISTER
)
77 sym
[i
].st_shndx
= SHN_ABS
;
83 int apply_relocate_add(Elf_Shdr
*sechdrs
,
85 unsigned int symindex
,
90 Elf_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
95 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
98 /* This is where to make the change */
99 location
= (u8
*)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
101 loc32
= (u32
*) location
;
103 #ifdef CONFIG_SPARC64
104 BUG_ON(((u64
)location
>> (u64
)32) != (u64
)0);
105 #endif /* CONFIG_SPARC64 */
107 /* This is the symbol it is referring to. Note that all
108 undefined symbols have been resolved. */
109 sym
= (Elf_Sym
*)sechdrs
[symindex
].sh_addr
110 + ELF_R_SYM(rel
[i
].r_info
);
111 v
= sym
->st_value
+ rel
[i
].r_addend
;
113 switch (ELF_R_TYPE(rel
[i
].r_info
) & 0xff) {
115 v
-= (Elf_Addr
) location
;
118 #ifdef CONFIG_SPARC64
120 location
[0] = v
>> 56;
121 location
[1] = v
>> 48;
122 location
[2] = v
>> 40;
123 location
[3] = v
>> 32;
124 location
[4] = v
>> 24;
125 location
[5] = v
>> 16;
126 location
[6] = v
>> 8;
127 location
[7] = v
>> 0;
130 case R_SPARC_WDISP19
:
131 v
-= (Elf_Addr
) location
;
132 *loc32
= (*loc32
& ~0x7ffff) |
133 ((v
>> 2) & 0x7ffff);
137 *loc32
= (*loc32
& ~0x1fff) |
139 (ELF_R_TYPE(rel
[i
].r_info
) >> 8))
142 #endif /* CONFIG_SPARC64 */
146 location
[0] = v
>> 24;
147 location
[1] = v
>> 16;
148 location
[2] = v
>> 8;
149 location
[3] = v
>> 0;
152 case R_SPARC_WDISP30
:
153 v
-= (Elf_Addr
) location
;
154 *loc32
= (*loc32
& ~0x3fffffff) |
155 ((v
>> 2) & 0x3fffffff);
158 case R_SPARC_WDISP22
:
159 v
-= (Elf_Addr
) location
;
160 *loc32
= (*loc32
& ~0x3fffff) |
161 ((v
>> 2) & 0x3fffff);
165 *loc32
= (*loc32
& ~0x3ff) | (v
& 0x3ff);
169 *loc32
= (*loc32
& ~0x3fffff) |
170 ((v
>> 10) & 0x3fffff);
174 printk(KERN_ERR
"module %s: Unknown relocation: %x\n",
176 (int) (ELF_R_TYPE(rel
[i
].r_info
) & 0xff));
183 #ifdef CONFIG_SPARC64
184 static void do_patch_sections(const Elf_Ehdr
*hdr
,
185 const Elf_Shdr
*sechdrs
)
187 const Elf_Shdr
*s
, *sun4v_1insn
= NULL
, *sun4v_2insn
= NULL
;
188 char *secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
190 for (s
= sechdrs
; s
< sechdrs
+ hdr
->e_shnum
; s
++) {
191 if (!strcmp(".sun4v_1insn_patch", secstrings
+ s
->sh_name
))
193 if (!strcmp(".sun4v_2insn_patch", secstrings
+ s
->sh_name
))
197 if (sun4v_1insn
&& tlb_type
== hypervisor
) {
198 void *p
= (void *) sun4v_1insn
->sh_addr
;
199 sun4v_patch_1insn_range(p
, p
+ sun4v_1insn
->sh_size
);
201 if (sun4v_2insn
&& tlb_type
== hypervisor
) {
202 void *p
= (void *) sun4v_2insn
->sh_addr
;
203 sun4v_patch_2insn_range(p
, p
+ sun4v_2insn
->sh_size
);
207 int module_finalize(const Elf_Ehdr
*hdr
,
208 const Elf_Shdr
*sechdrs
,
211 /* make jump label nops */
212 jump_label_apply_nops(me
);
214 do_patch_sections(hdr
, sechdrs
);
216 /* Cheetah's I-cache is fully coherent. */
217 if (tlb_type
== spitfire
) {
221 for (va
= 0; va
< (PAGE_SIZE
<< 1); va
+= 32)
222 spitfire_put_icache_tag(va
, 0x0);
223 __asm__
__volatile__("flush %g6");
228 #endif /* CONFIG_SPARC64 */