treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / sparc / kernel / module.c
blobdf39580f398d3a6171fba5868b611d84bba7b8c6
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.
6 */
8 #include <linux/moduleloader.h>
9 #include <linux/kernel.h>
10 #include <linux/elf.h>
11 #include <linux/vmalloc.h>
12 #include <linux/fs.h>
13 #include <linux/gfp.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
16 #include <linux/mm.h>
18 #include <asm/processor.h>
19 #include <asm/spitfire.h>
20 #include <asm/cacheflush.h>
22 #include "entry.h"
24 #ifdef CONFIG_SPARC64
26 #include <linux/jump_label.h>
28 static void *module_map(unsigned long size)
30 if (PAGE_ALIGN(size) > MODULES_LEN)
31 return NULL;
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));
36 #else
37 static void *module_map(unsigned long size)
39 return vmalloc(size);
41 #endif /* CONFIG_SPARC64 */
43 void *module_alloc(unsigned long size)
45 void *ret;
47 ret = module_map(size);
48 if (ret)
49 memset(ret, 0, size);
51 return ret;
54 /* Make generic code ignore STT_REGISTER dummy undefined symbols. */
55 int module_frob_arch_sections(Elf_Ehdr *hdr,
56 Elf_Shdr *sechdrs,
57 char *secstrings,
58 struct module *mod)
60 unsigned int symidx;
61 Elf_Sym *sym;
62 char *strtab;
63 int i;
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);
68 return -ENOEXEC;
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;
80 return 0;
83 int apply_relocate_add(Elf_Shdr *sechdrs,
84 const char *strtab,
85 unsigned int symindex,
86 unsigned int relsec,
87 struct module *me)
89 unsigned int i;
90 Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
91 Elf_Sym *sym;
92 u8 *location;
93 u32 *loc32;
95 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
96 Elf_Addr v;
98 /* This is where to make the change */
99 location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
100 + rel[i].r_offset;
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) {
114 case R_SPARC_DISP32:
115 v -= (Elf_Addr) location;
116 *loc32 = v;
117 break;
118 #ifdef CONFIG_SPARC64
119 case R_SPARC_64:
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;
128 break;
130 case R_SPARC_WDISP19:
131 v -= (Elf_Addr) location;
132 *loc32 = (*loc32 & ~0x7ffff) |
133 ((v >> 2) & 0x7ffff);
134 break;
136 case R_SPARC_OLO10:
137 *loc32 = (*loc32 & ~0x1fff) |
138 (((v & 0x3ff) +
139 (ELF_R_TYPE(rel[i].r_info) >> 8))
140 & 0x1fff);
141 break;
142 #endif /* CONFIG_SPARC64 */
144 case R_SPARC_32:
145 case R_SPARC_UA32:
146 location[0] = v >> 24;
147 location[1] = v >> 16;
148 location[2] = v >> 8;
149 location[3] = v >> 0;
150 break;
152 case R_SPARC_WDISP30:
153 v -= (Elf_Addr) location;
154 *loc32 = (*loc32 & ~0x3fffffff) |
155 ((v >> 2) & 0x3fffffff);
156 break;
158 case R_SPARC_WDISP22:
159 v -= (Elf_Addr) location;
160 *loc32 = (*loc32 & ~0x3fffff) |
161 ((v >> 2) & 0x3fffff);
162 break;
164 case R_SPARC_LO10:
165 *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
166 break;
168 case R_SPARC_HI22:
169 *loc32 = (*loc32 & ~0x3fffff) |
170 ((v >> 10) & 0x3fffff);
171 break;
173 default:
174 printk(KERN_ERR "module %s: Unknown relocation: %x\n",
175 me->name,
176 (int) (ELF_R_TYPE(rel[i].r_info) & 0xff));
177 return -ENOEXEC;
180 return 0;
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))
192 sun4v_1insn = s;
193 if (!strcmp(".sun4v_2insn_patch", secstrings + s->sh_name))
194 sun4v_2insn = s;
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,
209 struct module *me)
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) {
218 unsigned long va;
220 flushw_all();
221 for (va = 0; va < (PAGE_SIZE << 1); va += 32)
222 spitfire_put_icache_tag(va, 0x0);
223 __asm__ __volatile__("flush %g6");
226 return 0;
228 #endif /* CONFIG_SPARC64 */