xref: /openbmc/linux/arch/loongarch/kernel/module.c (revision fcdfe9d2)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Author: Hanlu Li <lihanlu@loongson.cn>
4  *         Huacai Chen <chenhuacai@loongson.cn>
5  *
6  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
7  */
8 
9 #define pr_fmt(fmt) "kmod: " fmt
10 
11 #include <linux/moduleloader.h>
12 #include <linux/elf.h>
13 #include <linux/mm.h>
14 #include <linux/vmalloc.h>
15 #include <linux/slab.h>
16 #include <linux/fs.h>
17 #include <linux/string.h>
18 #include <linux/kernel.h>
19 
20 static inline bool signed_imm_check(long val, unsigned int bit)
21 {
22 	return -(1L << (bit - 1)) <= val && val < (1L << (bit - 1));
23 }
24 
25 static inline bool unsigned_imm_check(unsigned long val, unsigned int bit)
26 {
27 	return val < (1UL << bit);
28 }
29 
30 static int rela_stack_push(s64 stack_value, s64 *rela_stack, size_t *rela_stack_top)
31 {
32 	if (*rela_stack_top >= RELA_STACK_DEPTH)
33 		return -ENOEXEC;
34 
35 	rela_stack[(*rela_stack_top)++] = stack_value;
36 	pr_debug("%s stack_value = 0x%llx\n", __func__, stack_value);
37 
38 	return 0;
39 }
40 
41 static int rela_stack_pop(s64 *stack_value, s64 *rela_stack, size_t *rela_stack_top)
42 {
43 	if (*rela_stack_top == 0)
44 		return -ENOEXEC;
45 
46 	*stack_value = rela_stack[--(*rela_stack_top)];
47 	pr_debug("%s stack_value = 0x%llx\n", __func__, *stack_value);
48 
49 	return 0;
50 }
51 
52 static int apply_r_larch_none(struct module *mod, u32 *location, Elf_Addr v,
53 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
54 {
55 	return 0;
56 }
57 
58 static int apply_r_larch_error(struct module *me, u32 *location, Elf_Addr v,
59 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
60 {
61 	pr_err("%s: Unsupport relocation type %u, please add its support.\n", me->name, type);
62 	return -EINVAL;
63 }
64 
65 static int apply_r_larch_32(struct module *mod, u32 *location, Elf_Addr v,
66 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
67 {
68 	*location = v;
69 	return 0;
70 }
71 
72 static int apply_r_larch_64(struct module *mod, u32 *location, Elf_Addr v,
73 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
74 {
75 	*(Elf_Addr *)location = v;
76 	return 0;
77 }
78 
79 static int apply_r_larch_sop_push_pcrel(struct module *mod, u32 *location, Elf_Addr v,
80 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
81 {
82 	return rela_stack_push(v - (u64)location, rela_stack, rela_stack_top);
83 }
84 
85 static int apply_r_larch_sop_push_absolute(struct module *mod, u32 *location, Elf_Addr v,
86 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
87 {
88 	return rela_stack_push(v, rela_stack, rela_stack_top);
89 }
90 
91 static int apply_r_larch_sop_push_dup(struct module *mod, u32 *location, Elf_Addr v,
92 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
93 {
94 	int err = 0;
95 	s64 opr1;
96 
97 	err = rela_stack_pop(&opr1, rela_stack, rela_stack_top);
98 	if (err)
99 		return err;
100 	err = rela_stack_push(opr1, rela_stack, rela_stack_top);
101 	if (err)
102 		return err;
103 	err = rela_stack_push(opr1, rela_stack, rela_stack_top);
104 	if (err)
105 		return err;
106 
107 	return 0;
108 }
109 
110 static int apply_r_larch_sop_push_plt_pcrel(struct module *mod, u32 *location, Elf_Addr v,
111 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
112 {
113 	ptrdiff_t offset = (void *)v - (void *)location;
114 
115 	if (offset >= SZ_128M)
116 		v = module_emit_plt_entry(mod, v);
117 
118 	if (offset < -SZ_128M)
119 		v = module_emit_plt_entry(mod, v);
120 
121 	return apply_r_larch_sop_push_pcrel(mod, location, v, rela_stack, rela_stack_top, type);
122 }
123 
124 static int apply_r_larch_sop(struct module *mod, u32 *location, Elf_Addr v,
125 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
126 {
127 	int err = 0;
128 	s64 opr1, opr2, opr3;
129 
130 	if (type == R_LARCH_SOP_IF_ELSE) {
131 		err = rela_stack_pop(&opr3, rela_stack, rela_stack_top);
132 		if (err)
133 			return err;
134 	}
135 
136 	err = rela_stack_pop(&opr2, rela_stack, rela_stack_top);
137 	if (err)
138 		return err;
139 	err = rela_stack_pop(&opr1, rela_stack, rela_stack_top);
140 	if (err)
141 		return err;
142 
143 	switch (type) {
144 	case R_LARCH_SOP_AND:
145 		err = rela_stack_push(opr1 & opr2, rela_stack, rela_stack_top);
146 		break;
147 	case R_LARCH_SOP_ADD:
148 		err = rela_stack_push(opr1 + opr2, rela_stack, rela_stack_top);
149 		break;
150 	case R_LARCH_SOP_SUB:
151 		err = rela_stack_push(opr1 - opr2, rela_stack, rela_stack_top);
152 		break;
153 	case R_LARCH_SOP_SL:
154 		err = rela_stack_push(opr1 << opr2, rela_stack, rela_stack_top);
155 		break;
156 	case R_LARCH_SOP_SR:
157 		err = rela_stack_push(opr1 >> opr2, rela_stack, rela_stack_top);
158 		break;
159 	case R_LARCH_SOP_IF_ELSE:
160 		err = rela_stack_push(opr1 ? opr2 : opr3, rela_stack, rela_stack_top);
161 		break;
162 	default:
163 		pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
164 		return -EINVAL;
165 	}
166 
167 	return err;
168 }
169 
170 static int apply_r_larch_sop_imm_field(struct module *mod, u32 *location, Elf_Addr v,
171 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
172 {
173 	int err = 0;
174 	s64 opr1;
175 	union loongarch_instruction *insn = (union loongarch_instruction *)location;
176 
177 	err = rela_stack_pop(&opr1, rela_stack, rela_stack_top);
178 	if (err)
179 		return err;
180 
181 	switch (type) {
182 	case R_LARCH_SOP_POP_32_U_10_12:
183 		if (!unsigned_imm_check(opr1, 12))
184 			goto overflow;
185 
186 		/* (*(uint32_t *) PC) [21 ... 10] = opr [11 ... 0] */
187 		insn->reg2i12_format.immediate = opr1 & 0xfff;
188 		return 0;
189 	case R_LARCH_SOP_POP_32_S_10_12:
190 		if (!signed_imm_check(opr1, 12))
191 			goto overflow;
192 
193 		insn->reg2i12_format.immediate = opr1 & 0xfff;
194 		return 0;
195 	case R_LARCH_SOP_POP_32_S_10_16:
196 		if (!signed_imm_check(opr1, 16))
197 			goto overflow;
198 
199 		insn->reg2i16_format.immediate = opr1 & 0xffff;
200 		return 0;
201 	case R_LARCH_SOP_POP_32_S_10_16_S2:
202 		if (opr1 % 4)
203 			goto unaligned;
204 
205 		if (!signed_imm_check(opr1, 18))
206 			goto overflow;
207 
208 		insn->reg2i16_format.immediate = (opr1 >> 2) & 0xffff;
209 		return 0;
210 	case R_LARCH_SOP_POP_32_S_5_20:
211 		if (!signed_imm_check(opr1, 20))
212 			goto overflow;
213 
214 		insn->reg1i20_format.immediate = (opr1) & 0xfffff;
215 		return 0;
216 	case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
217 		if (opr1 % 4)
218 			goto unaligned;
219 
220 		if (!signed_imm_check(opr1, 23))
221 			goto overflow;
222 
223 		opr1 >>= 2;
224 		insn->reg1i21_format.immediate_l = opr1 & 0xffff;
225 		insn->reg1i21_format.immediate_h = (opr1 >> 16) & 0x1f;
226 		return 0;
227 	case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
228 		if (opr1 % 4)
229 			goto unaligned;
230 
231 		if (!signed_imm_check(opr1, 28))
232 			goto overflow;
233 
234 		opr1 >>= 2;
235 		insn->reg0i26_format.immediate_l = opr1 & 0xffff;
236 		insn->reg0i26_format.immediate_h = (opr1 >> 16) & 0x3ff;
237 		return 0;
238 	case R_LARCH_SOP_POP_32_U:
239 		if (!unsigned_imm_check(opr1, 32))
240 			goto overflow;
241 
242 		/* (*(uint32_t *) PC) = opr */
243 		*location = (u32)opr1;
244 		return 0;
245 	default:
246 		pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
247 		return -EINVAL;
248 	}
249 
250 overflow:
251 	pr_err("module %s: opr1 = 0x%llx overflow! dangerous %s (%u) relocation\n",
252 		mod->name, opr1, __func__, type);
253 	return -ENOEXEC;
254 
255 unaligned:
256 	pr_err("module %s: opr1 = 0x%llx unaligned! dangerous %s (%u) relocation\n",
257 		mod->name, opr1, __func__, type);
258 	return -ENOEXEC;
259 }
260 
261 static int apply_r_larch_add_sub(struct module *mod, u32 *location, Elf_Addr v,
262 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
263 {
264 	switch (type) {
265 	case R_LARCH_ADD32:
266 		*(s32 *)location += v;
267 		return 0;
268 	case R_LARCH_ADD64:
269 		*(s64 *)location += v;
270 		return 0;
271 	case R_LARCH_SUB32:
272 		*(s32 *)location -= v;
273 		return 0;
274 	case R_LARCH_SUB64:
275 		*(s64 *)location -= v;
276 		return 0;
277 	default:
278 		pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
279 		return -EINVAL;
280 	}
281 }
282 
283 /*
284  * reloc_handlers_rela() - Apply a particular relocation to a module
285  * @mod: the module to apply the reloc to
286  * @location: the address at which the reloc is to be applied
287  * @v: the value of the reloc, with addend for RELA-style
288  * @rela_stack: the stack used for store relocation info, LOCAL to THIS module
289  * @rela_stac_top: where the stack operation(pop/push) applies to
290  *
291  * Return: 0 upon success, else -ERRNO
292  */
293 typedef int (*reloc_rela_handler)(struct module *mod, u32 *location, Elf_Addr v,
294 			s64 *rela_stack, size_t *rela_stack_top, unsigned int type);
295 
296 /* The handlers for known reloc types */
297 static reloc_rela_handler reloc_rela_handlers[] = {
298 	[R_LARCH_NONE ... R_LARCH_SUB64]		     = apply_r_larch_error,
299 
300 	[R_LARCH_NONE]					     = apply_r_larch_none,
301 	[R_LARCH_32]					     = apply_r_larch_32,
302 	[R_LARCH_64]					     = apply_r_larch_64,
303 	[R_LARCH_MARK_LA]				     = apply_r_larch_none,
304 	[R_LARCH_MARK_PCREL]				     = apply_r_larch_none,
305 	[R_LARCH_SOP_PUSH_PCREL]			     = apply_r_larch_sop_push_pcrel,
306 	[R_LARCH_SOP_PUSH_ABSOLUTE]			     = apply_r_larch_sop_push_absolute,
307 	[R_LARCH_SOP_PUSH_DUP]				     = apply_r_larch_sop_push_dup,
308 	[R_LARCH_SOP_PUSH_PLT_PCREL]			     = apply_r_larch_sop_push_plt_pcrel,
309 	[R_LARCH_SOP_SUB ... R_LARCH_SOP_IF_ELSE] 	     = apply_r_larch_sop,
310 	[R_LARCH_SOP_POP_32_S_10_5 ... R_LARCH_SOP_POP_32_U] = apply_r_larch_sop_imm_field,
311 	[R_LARCH_ADD32 ... R_LARCH_SUB64]		     = apply_r_larch_add_sub,
312 };
313 
314 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
315 		       unsigned int symindex, unsigned int relsec,
316 		       struct module *mod)
317 {
318 	int i, err;
319 	unsigned int type;
320 	s64 rela_stack[RELA_STACK_DEPTH];
321 	size_t rela_stack_top = 0;
322 	reloc_rela_handler handler;
323 	void *location;
324 	Elf_Addr v;
325 	Elf_Sym *sym;
326 	Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
327 
328 	pr_debug("%s: Applying relocate section %u to %u\n", __func__, relsec,
329 	       sechdrs[relsec].sh_info);
330 
331 	rela_stack_top = 0;
332 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
333 		/* This is where to make the change */
334 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr + rel[i].r_offset;
335 		/* This is the symbol it is referring to */
336 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr + ELF_R_SYM(rel[i].r_info);
337 		if (IS_ERR_VALUE(sym->st_value)) {
338 			/* Ignore unresolved weak symbol */
339 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
340 				continue;
341 			pr_warn("%s: Unknown symbol %s\n", mod->name, strtab + sym->st_name);
342 			return -ENOENT;
343 		}
344 
345 		type = ELF_R_TYPE(rel[i].r_info);
346 
347 		if (type < ARRAY_SIZE(reloc_rela_handlers))
348 			handler = reloc_rela_handlers[type];
349 		else
350 			handler = NULL;
351 
352 		if (!handler) {
353 			pr_err("%s: Unknown relocation type %u\n", mod->name, type);
354 			return -EINVAL;
355 		}
356 
357 		pr_debug("type %d st_value %llx r_addend %llx loc %llx\n",
358 		       (int)ELF_R_TYPE(rel[i].r_info),
359 		       sym->st_value, rel[i].r_addend, (u64)location);
360 
361 		v = sym->st_value + rel[i].r_addend;
362 		err = handler(mod, location, v, rela_stack, &rela_stack_top, type);
363 		if (err)
364 			return err;
365 	}
366 
367 	return 0;
368 }
369 
370 void *module_alloc(unsigned long size)
371 {
372 	return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
373 			GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE, __builtin_return_address(0));
374 }
375