module.c 25.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
Palmer Dabbelt's avatar
Palmer Dabbelt committed
2 3 4 5 6 7 8 9
/*
 *
 *  Copyright (C) 2017 Zihao Yu
 */

#include <linux/elf.h>
#include <linux/err.h>
#include <linux/errno.h>
10 11 12
#include <linux/hashtable.h>
#include <linux/kernel.h>
#include <linux/log2.h>
Palmer Dabbelt's avatar
Palmer Dabbelt committed
13
#include <linux/moduleloader.h>
14 15
#include <linux/vmalloc.h>
#include <linux/sizes.h>
16
#include <linux/pgtable.h>
17
#include <asm/alternative.h>
18
#include <asm/sections.h>
Palmer Dabbelt's avatar
Palmer Dabbelt committed
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
struct used_bucket {
	struct list_head head;
	struct hlist_head *bucket;
};

struct relocation_head {
	struct hlist_node node;
	struct list_head *rel_entry;
	void *location;
};

struct relocation_entry {
	struct list_head head;
	Elf_Addr value;
	unsigned int type;
};

struct relocation_handlers {
	int (*reloc_handler)(struct module *me, void *location, Elf_Addr v);
	int (*accumulate_handler)(struct module *me, void *location,
				  long buffer);
};

43 44 45 46 47 48 49 50 51 52 53 54 55
/*
 * The auipc+jalr instruction pair can reach any PC-relative offset
 * in the range [-2^31 - 2^11, 2^31 - 2^11)
 */
static bool riscv_insn_valid_32bit_offset(ptrdiff_t val)
{
#ifdef CONFIG_32BIT
	return true;
#else
	return (-(1L << 31) - (1L << 11)) <= val && val < ((1L << 31) - (1L << 11));
#endif
}

56 57
static int riscv_insn_rmw(void *location, u32 keep, u32 set)
{
58
	__le16 *parcel = location;
59 60 61 62 63 64 65 66 67 68 69 70
	u32 insn = (u32)le16_to_cpu(parcel[0]) | (u32)le16_to_cpu(parcel[1]) << 16;

	insn &= keep;
	insn |= set;

	parcel[0] = cpu_to_le16(insn);
	parcel[1] = cpu_to_le16(insn >> 16);
	return 0;
}

static int riscv_insn_rvc_rmw(void *location, u16 keep, u16 set)
{
71
	__le16 *parcel = location;
72 73 74 75 76 77 78 79 80 81
	u16 insn = le16_to_cpu(*parcel);

	insn &= keep;
	insn |= set;

	*parcel = cpu_to_le16(insn);
	return 0;
}

static int apply_r_riscv_32_rela(struct module *me, void *location, Elf_Addr v)
82 83 84
{
	if (v != (u32)v) {
		pr_err("%s: value %016llx out of range for 32-bit field\n",
85
		       me->name, (long long)v);
86 87
		return -EINVAL;
	}
88
	*(u32 *)location = v;
89 90 91
	return 0;
}

92
static int apply_r_riscv_64_rela(struct module *me, void *location, Elf_Addr v)
Palmer Dabbelt's avatar
Palmer Dabbelt committed
93 94 95 96 97
{
	*(u64 *)location = v;
	return 0;
}

98
static int apply_r_riscv_branch_rela(struct module *me, void *location,
Palmer Dabbelt's avatar
Palmer Dabbelt committed
99 100
				     Elf_Addr v)
{
101
	ptrdiff_t offset = (void *)v - location;
Palmer Dabbelt's avatar
Palmer Dabbelt committed
102 103 104 105 106
	u32 imm12 = (offset & 0x1000) << (31 - 12);
	u32 imm11 = (offset & 0x800) >> (11 - 7);
	u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
	u32 imm4_1 = (offset & 0x1e) << (11 - 4);

107
	return riscv_insn_rmw(location, 0x1fff07f, imm12 | imm11 | imm10_5 | imm4_1);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
108 109
}

110
static int apply_r_riscv_jal_rela(struct module *me, void *location,
Palmer Dabbelt's avatar
Palmer Dabbelt committed
111 112
				  Elf_Addr v)
{
113
	ptrdiff_t offset = (void *)v - location;
Palmer Dabbelt's avatar
Palmer Dabbelt committed
114 115 116 117 118
	u32 imm20 = (offset & 0x100000) << (31 - 20);
	u32 imm19_12 = (offset & 0xff000);
	u32 imm11 = (offset & 0x800) << (20 - 11);
	u32 imm10_1 = (offset & 0x7fe) << (30 - 10);

119
	return riscv_insn_rmw(location, 0xfff, imm20 | imm19_12 | imm11 | imm10_1);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
120 121
}

122
static int apply_r_riscv_rvc_branch_rela(struct module *me, void *location,
123 124
					 Elf_Addr v)
{
125
	ptrdiff_t offset = (void *)v - location;
126 127 128 129 130 131
	u16 imm8 = (offset & 0x100) << (12 - 8);
	u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
	u16 imm5 = (offset & 0x20) >> (5 - 2);
	u16 imm4_3 = (offset & 0x18) << (12 - 5);
	u16 imm2_1 = (offset & 0x6) << (12 - 10);

132 133
	return riscv_insn_rvc_rmw(location, 0xe383,
			imm8 | imm7_6 | imm5 | imm4_3 | imm2_1);
134 135
}

136
static int apply_r_riscv_rvc_jump_rela(struct module *me, void *location,
137 138
				       Elf_Addr v)
{
139
	ptrdiff_t offset = (void *)v - location;
140 141 142 143 144 145 146 147 148
	u16 imm11 = (offset & 0x800) << (12 - 11);
	u16 imm10 = (offset & 0x400) >> (10 - 8);
	u16 imm9_8 = (offset & 0x300) << (12 - 11);
	u16 imm7 = (offset & 0x80) >> (7 - 6);
	u16 imm6 = (offset & 0x40) << (12 - 11);
	u16 imm5 = (offset & 0x20) >> (5 - 2);
	u16 imm4 = (offset & 0x10) << (12 - 5);
	u16 imm3_1 = (offset & 0xe) << (12 - 10);

149 150
	return riscv_insn_rvc_rmw(location, 0xe003,
			imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1);
151 152
}

153
static int apply_r_riscv_pcrel_hi20_rela(struct module *me, void *location,
Palmer Dabbelt's avatar
Palmer Dabbelt committed
154 155
					 Elf_Addr v)
{
156
	ptrdiff_t offset = (void *)v - location;
Palmer Dabbelt's avatar
Palmer Dabbelt committed
157

158
	if (!riscv_insn_valid_32bit_offset(offset)) {
Palmer Dabbelt's avatar
Palmer Dabbelt committed
159 160
		pr_err(
		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
161
		  me->name, (long long)v, location);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
162 163 164
		return -EINVAL;
	}

165
	return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
166 167
}

168
static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, void *location,
Palmer Dabbelt's avatar
Palmer Dabbelt committed
169 170 171 172 173 174
					   Elf_Addr v)
{
	/*
	 * v is the lo12 value to fill. It is calculated before calling this
	 * handler.
	 */
175
	return riscv_insn_rmw(location, 0xfffff, (v & 0xfff) << 20);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
176 177
}

178
static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, void *location,
Palmer Dabbelt's avatar
Palmer Dabbelt committed
179 180 181 182 183 184 185 186 187
					   Elf_Addr v)
{
	/*
	 * v is the lo12 value to fill. It is calculated before calling this
	 * handler.
	 */
	u32 imm11_5 = (v & 0xfe0) << (31 - 11);
	u32 imm4_0 = (v & 0x1f) << (11 - 4);

188
	return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
189 190
}

191
static int apply_r_riscv_hi20_rela(struct module *me, void *location,
192 193
				   Elf_Addr v)
{
194
	if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) {
195 196
		pr_err(
		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
197
		  me->name, (long long)v, location);
198 199 200
		return -EINVAL;
	}

201
	return riscv_insn_rmw(location, 0xfff, ((s32)v + 0x800) & 0xfffff000);
202 203
}

204
static int apply_r_riscv_lo12_i_rela(struct module *me, void *location,
205 206 207 208 209
				     Elf_Addr v)
{
	/* Skip medlow checking because of filtering by HI20 already */
	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
	s32 lo12 = ((s32)v - hi20);
210 211

	return riscv_insn_rmw(location, 0xfffff, (lo12 & 0xfff) << 20);
212 213
}

214
static int apply_r_riscv_lo12_s_rela(struct module *me, void *location,
215 216 217 218 219 220 221
				     Elf_Addr v)
{
	/* Skip medlow checking because of filtering by HI20 already */
	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
	s32 lo12 = ((s32)v - hi20);
	u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
	u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
222 223

	return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0);
224 225
}

226
static int apply_r_riscv_got_hi20_rela(struct module *me, void *location,
227 228
				       Elf_Addr v)
{
229
	ptrdiff_t offset = (void *)v - location;
230 231 232

	/* Always emit the got entry */
	if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
233
		offset = (void *)module_emit_got_entry(me, v) - location;
234 235 236
	} else {
		pr_err(
		  "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
237
		  me->name, (long long)v, location);
238 239 240
		return -EINVAL;
	}

241
	return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000);
242 243
}

244
static int apply_r_riscv_call_plt_rela(struct module *me, void *location,
Palmer Dabbelt's avatar
Palmer Dabbelt committed
245 246
				       Elf_Addr v)
{
247
	ptrdiff_t offset = (void *)v - location;
Palmer Dabbelt's avatar
Palmer Dabbelt committed
248 249
	u32 hi20, lo12;

250
	if (!riscv_insn_valid_32bit_offset(offset)) {
251 252
		/* Only emit the plt entry if offset over 32-bit range */
		if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
253
			offset = (void *)module_emit_plt_entry(me, v) - location;
254 255 256
		} else {
			pr_err(
			  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
257
			  me->name, (long long)v, location);
258 259
			return -EINVAL;
		}
Palmer Dabbelt's avatar
Palmer Dabbelt committed
260 261 262 263
	}

	hi20 = (offset + 0x800) & 0xfffff000;
	lo12 = (offset - hi20) & 0xfff;
264 265
	riscv_insn_rmw(location, 0xfff, hi20);
	return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
266 267
}

268
static int apply_r_riscv_call_rela(struct module *me, void *location,
269 270
				   Elf_Addr v)
{
271
	ptrdiff_t offset = (void *)v - location;
272 273
	u32 hi20, lo12;

274
	if (!riscv_insn_valid_32bit_offset(offset)) {
275 276
		pr_err(
		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
277
		  me->name, (long long)v, location);
278 279 280 281 282
		return -EINVAL;
	}

	hi20 = (offset + 0x800) & 0xfffff000;
	lo12 = (offset - hi20) & 0xfff;
283 284
	riscv_insn_rmw(location, 0xfff, hi20);
	return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20);
285 286
}

287
static int apply_r_riscv_relax_rela(struct module *me, void *location,
Palmer Dabbelt's avatar
Palmer Dabbelt committed
288 289 290 291 292
				    Elf_Addr v)
{
	return 0;
}

293
static int apply_r_riscv_align_rela(struct module *me, void *location,
294 295 296 297 298 299 300 301
				    Elf_Addr v)
{
	pr_err(
	  "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
	  me->name, location);
	return -EINVAL;
}

302 303 304 305 306 307
static int apply_r_riscv_add8_rela(struct module *me, void *location, Elf_Addr v)
{
	*(u8 *)location += (u8)v;
	return 0;
}

308
static int apply_r_riscv_add16_rela(struct module *me, void *location,
309 310 311 312 313 314
				    Elf_Addr v)
{
	*(u16 *)location += (u16)v;
	return 0;
}

315
static int apply_r_riscv_add32_rela(struct module *me, void *location,
316 317
				    Elf_Addr v)
{
318
	*(u32 *)location += (u32)v;
319 320 321
	return 0;
}

322
static int apply_r_riscv_add64_rela(struct module *me, void *location,
323 324 325 326 327 328
				    Elf_Addr v)
{
	*(u64 *)location += (u64)v;
	return 0;
}

329 330 331 332 333 334
static int apply_r_riscv_sub8_rela(struct module *me, void *location, Elf_Addr v)
{
	*(u8 *)location -= (u8)v;
	return 0;
}

335
static int apply_r_riscv_sub16_rela(struct module *me, void *location,
336 337 338 339 340 341
				    Elf_Addr v)
{
	*(u16 *)location -= (u16)v;
	return 0;
}

342
static int apply_r_riscv_sub32_rela(struct module *me, void *location,
343 344
				    Elf_Addr v)
{
345
	*(u32 *)location -= (u32)v;
346 347 348
	return 0;
}

349
static int apply_r_riscv_sub64_rela(struct module *me, void *location,
350 351 352 353 354 355
				    Elf_Addr v)
{
	*(u64 *)location -= (u64)v;
	return 0;
}

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
static int dynamic_linking_not_supported(struct module *me, void *location,
					 Elf_Addr v)
{
	pr_err("%s: Dynamic linking not supported in kernel modules PC = %p\n",
	       me->name, location);
	return -EINVAL;
}

static int tls_not_supported(struct module *me, void *location, Elf_Addr v)
{
	pr_err("%s: Thread local storage not supported in kernel modules PC = %p\n",
	       me->name, location);
	return -EINVAL;
}

static int apply_r_riscv_sub6_rela(struct module *me, void *location, Elf_Addr v)
{
	u8 *byte = location;
	u8 value = v;

	*byte = (*byte - (value & 0x3f)) & 0x3f;
	return 0;
}

static int apply_r_riscv_set6_rela(struct module *me, void *location, Elf_Addr v)
{
	u8 *byte = location;
	u8 value = v;

	*byte = (*byte & 0xc0) | (value & 0x3f);
	return 0;
}

static int apply_r_riscv_set8_rela(struct module *me, void *location, Elf_Addr v)
{
	*(u8 *)location = (u8)v;
	return 0;
}

static int apply_r_riscv_set16_rela(struct module *me, void *location,
				    Elf_Addr v)
{
	*(u16 *)location = (u16)v;
	return 0;
}

static int apply_r_riscv_set32_rela(struct module *me, void *location,
				    Elf_Addr v)
{
	*(u32 *)location = (u32)v;
	return 0;
}

static int apply_r_riscv_32_pcrel_rela(struct module *me, void *location,
				       Elf_Addr v)
{
	*(u32 *)location = v - (uintptr_t)location;
	return 0;
}

static int apply_r_riscv_plt32_rela(struct module *me, void *location,
				    Elf_Addr v)
{
	ptrdiff_t offset = (void *)v - location;

	if (!riscv_insn_valid_32bit_offset(offset)) {
		/* Only emit the plt entry if offset over 32-bit range */
		if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
			offset = (void *)module_emit_plt_entry(me, v) - location;
		} else {
			pr_err("%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
			       me->name, (long long)v, location);
			return -EINVAL;
		}
	}

	*(u32 *)location = (u32)offset;
	return 0;
}

static int apply_r_riscv_set_uleb128(struct module *me, void *location, Elf_Addr v)
{
	*(long *)location = v;
	return 0;
}

static int apply_r_riscv_sub_uleb128(struct module *me, void *location, Elf_Addr v)
{
	*(long *)location -= v;
	return 0;
}

static int apply_6_bit_accumulation(struct module *me, void *location, long buffer)
{
	u8 *byte = location;
	u8 value = buffer;

	if (buffer > 0x3f) {
		pr_err("%s: value %ld out of range for 6-bit relocation.\n",
		       me->name, buffer);
		return -EINVAL;
	}

	*byte = (*byte & 0xc0) | (value & 0x3f);
	return 0;
}

static int apply_8_bit_accumulation(struct module *me, void *location, long buffer)
{
	if (buffer > U8_MAX) {
		pr_err("%s: value %ld out of range for 8-bit relocation.\n",
		       me->name, buffer);
		return -EINVAL;
	}
	*(u8 *)location = (u8)buffer;
	return 0;
}

static int apply_16_bit_accumulation(struct module *me, void *location, long buffer)
{
	if (buffer > U16_MAX) {
		pr_err("%s: value %ld out of range for 16-bit relocation.\n",
		       me->name, buffer);
		return -EINVAL;
	}
	*(u16 *)location = (u16)buffer;
	return 0;
}

static int apply_32_bit_accumulation(struct module *me, void *location, long buffer)
{
	if (buffer > U32_MAX) {
		pr_err("%s: value %ld out of range for 32-bit relocation.\n",
		       me->name, buffer);
		return -EINVAL;
	}
	*(u32 *)location = (u32)buffer;
	return 0;
}

static int apply_64_bit_accumulation(struct module *me, void *location, long buffer)
{
	*(u64 *)location = (u64)buffer;
	return 0;
}

static int apply_uleb128_accumulation(struct module *me, void *location, long buffer)
{
	/*
	 * ULEB128 is a variable length encoding. Encode the buffer into
	 * the ULEB128 data format.
	 */
	u8 *p = location;

	while (buffer != 0) {
		u8 value = buffer & 0x7f;

		buffer >>= 7;
		value |= (!!buffer) << 7;

		*p++ = value;
	}
	return 0;
}

/*
 * Relocations defined in the riscv-elf-psabi-doc.
 * This handles static linking only.
 */
static const struct relocation_handlers reloc_handlers[] = {
526 527 528 529 530 531 532 533 534 535 536
	[R_RISCV_32]		= { .reloc_handler = apply_r_riscv_32_rela },
	[R_RISCV_64]		= { .reloc_handler = apply_r_riscv_64_rela },
	[R_RISCV_RELATIVE]	= { .reloc_handler = dynamic_linking_not_supported },
	[R_RISCV_COPY]		= { .reloc_handler = dynamic_linking_not_supported },
	[R_RISCV_JUMP_SLOT]	= { .reloc_handler = dynamic_linking_not_supported },
	[R_RISCV_TLS_DTPMOD32]	= { .reloc_handler = dynamic_linking_not_supported },
	[R_RISCV_TLS_DTPMOD64]	= { .reloc_handler = dynamic_linking_not_supported },
	[R_RISCV_TLS_DTPREL32]	= { .reloc_handler = dynamic_linking_not_supported },
	[R_RISCV_TLS_DTPREL64]	= { .reloc_handler = dynamic_linking_not_supported },
	[R_RISCV_TLS_TPREL32]	= { .reloc_handler = dynamic_linking_not_supported },
	[R_RISCV_TLS_TPREL64]	= { .reloc_handler = dynamic_linking_not_supported },
537
	/* 12-15 undefined */
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
	[R_RISCV_BRANCH]	= { .reloc_handler = apply_r_riscv_branch_rela },
	[R_RISCV_JAL]		= { .reloc_handler = apply_r_riscv_jal_rela },
	[R_RISCV_CALL]		= { .reloc_handler = apply_r_riscv_call_rela },
	[R_RISCV_CALL_PLT]	= { .reloc_handler = apply_r_riscv_call_plt_rela },
	[R_RISCV_GOT_HI20]	= { .reloc_handler = apply_r_riscv_got_hi20_rela },
	[R_RISCV_TLS_GOT_HI20]	= { .reloc_handler = tls_not_supported },
	[R_RISCV_TLS_GD_HI20]	= { .reloc_handler = tls_not_supported },
	[R_RISCV_PCREL_HI20]	= { .reloc_handler = apply_r_riscv_pcrel_hi20_rela },
	[R_RISCV_PCREL_LO12_I]	= { .reloc_handler = apply_r_riscv_pcrel_lo12_i_rela },
	[R_RISCV_PCREL_LO12_S]	= { .reloc_handler = apply_r_riscv_pcrel_lo12_s_rela },
	[R_RISCV_HI20]		= { .reloc_handler = apply_r_riscv_hi20_rela },
	[R_RISCV_LO12_I]	= { .reloc_handler = apply_r_riscv_lo12_i_rela },
	[R_RISCV_LO12_S]	= { .reloc_handler = apply_r_riscv_lo12_s_rela },
	[R_RISCV_TPREL_HI20]	= { .reloc_handler = tls_not_supported },
	[R_RISCV_TPREL_LO12_I]	= { .reloc_handler = tls_not_supported },
	[R_RISCV_TPREL_LO12_S]	= { .reloc_handler = tls_not_supported },
	[R_RISCV_TPREL_ADD]	= { .reloc_handler = tls_not_supported },
	[R_RISCV_ADD8]		= { .reloc_handler = apply_r_riscv_add8_rela,
				    .accumulate_handler = apply_8_bit_accumulation },
	[R_RISCV_ADD16]		= { .reloc_handler = apply_r_riscv_add16_rela,
				    .accumulate_handler = apply_16_bit_accumulation },
	[R_RISCV_ADD32]		= { .reloc_handler = apply_r_riscv_add32_rela,
				    .accumulate_handler = apply_32_bit_accumulation },
	[R_RISCV_ADD64]		= { .reloc_handler = apply_r_riscv_add64_rela,
				    .accumulate_handler = apply_64_bit_accumulation },
	[R_RISCV_SUB8]		= { .reloc_handler = apply_r_riscv_sub8_rela,
				    .accumulate_handler = apply_8_bit_accumulation },
	[R_RISCV_SUB16]		= { .reloc_handler = apply_r_riscv_sub16_rela,
				    .accumulate_handler = apply_16_bit_accumulation },
	[R_RISCV_SUB32]		= { .reloc_handler = apply_r_riscv_sub32_rela,
				    .accumulate_handler = apply_32_bit_accumulation },
	[R_RISCV_SUB64]		= { .reloc_handler = apply_r_riscv_sub64_rela,
				    .accumulate_handler = apply_64_bit_accumulation },
571
	/* 41-42 reserved for future standard use */
572 573 574
	[R_RISCV_ALIGN]		= { .reloc_handler = apply_r_riscv_align_rela },
	[R_RISCV_RVC_BRANCH]	= { .reloc_handler = apply_r_riscv_rvc_branch_rela },
	[R_RISCV_RVC_JUMP]	= { .reloc_handler = apply_r_riscv_rvc_jump_rela },
575
	/* 46-50 reserved for future standard use */
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
	[R_RISCV_RELAX]		= { .reloc_handler = apply_r_riscv_relax_rela },
	[R_RISCV_SUB6]		= { .reloc_handler = apply_r_riscv_sub6_rela,
				    .accumulate_handler = apply_6_bit_accumulation },
	[R_RISCV_SET6]		= { .reloc_handler = apply_r_riscv_set6_rela,
				    .accumulate_handler = apply_6_bit_accumulation },
	[R_RISCV_SET8]		= { .reloc_handler = apply_r_riscv_set8_rela,
				    .accumulate_handler = apply_8_bit_accumulation },
	[R_RISCV_SET16]		= { .reloc_handler = apply_r_riscv_set16_rela,
				    .accumulate_handler = apply_16_bit_accumulation },
	[R_RISCV_SET32]		= { .reloc_handler = apply_r_riscv_set32_rela,
				    .accumulate_handler = apply_32_bit_accumulation },
	[R_RISCV_32_PCREL]	= { .reloc_handler = apply_r_riscv_32_pcrel_rela },
	[R_RISCV_IRELATIVE]	= { .reloc_handler = dynamic_linking_not_supported },
	[R_RISCV_PLT32]		= { .reloc_handler = apply_r_riscv_plt32_rela },
	[R_RISCV_SET_ULEB128]	= { .reloc_handler = apply_r_riscv_set_uleb128,
				    .accumulate_handler = apply_uleb128_accumulation },
	[R_RISCV_SUB_ULEB128]	= { .reloc_handler = apply_r_riscv_sub_uleb128,
				    .accumulate_handler = apply_uleb128_accumulation },
594 595
	/* 62-191 reserved for future standard use */
	/* 192-255 nonstandard ABI extensions  */
Palmer Dabbelt's avatar
Palmer Dabbelt committed
596 597
};

598 599 600 601
static void
process_accumulated_relocations(struct module *me,
				struct hlist_head **relocation_hashtable,
				struct list_head *used_buckets_list)
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
{
	/*
	 * Only ADD/SUB/SET/ULEB128 should end up here.
	 *
	 * Each bucket may have more than one relocation location. All
	 * relocations for a location are stored in a list in a bucket.
	 *
	 * Relocations are applied to a temp variable before being stored to the
	 * provided location to check for overflow. This also allows ULEB128 to
	 * properly decide how many entries are needed before storing to
	 * location. The final value is stored into location using the handler
	 * for the last relocation to an address.
	 *
	 * Three layers of indexing:
	 *	- Each of the buckets in use
	 *	- Groups of relocations in each bucket by location address
	 *	- Each relocation entry for a location address
	 */
	struct used_bucket *bucket_iter;
621
	struct used_bucket *bucket_iter_tmp;
622
	struct relocation_head *rel_head_iter;
623
	struct hlist_node *rel_head_iter_tmp;
624
	struct relocation_entry *rel_entry_iter;
625
	struct relocation_entry *rel_entry_iter_tmp;
626 627 628 629
	int curr_type;
	void *location;
	long buffer;

630 631 632 633
	list_for_each_entry_safe(bucket_iter, bucket_iter_tmp,
				 used_buckets_list, head) {
		hlist_for_each_entry_safe(rel_head_iter, rel_head_iter_tmp,
					  bucket_iter->bucket, node) {
634 635
			buffer = 0;
			location = rel_head_iter->location;
636 637 638 639
			list_for_each_entry_safe(rel_entry_iter,
						 rel_entry_iter_tmp,
						 rel_head_iter->rel_entry,
						 head) {
640 641 642 643 644 645 646 647 648 649 650 651
				curr_type = rel_entry_iter->type;
				reloc_handlers[curr_type].reloc_handler(
					me, &buffer, rel_entry_iter->value);
				kfree(rel_entry_iter);
			}
			reloc_handlers[curr_type].accumulate_handler(
				me, location, buffer);
			kfree(rel_head_iter);
		}
		kfree(bucket_iter);
	}

652
	kfree(*relocation_hashtable);
653 654
}

655 656 657 658 659
static int add_relocation_to_accumulate(struct module *me, int type,
					void *location,
					unsigned int hashtable_bits, Elf_Addr v,
					struct hlist_head *relocation_hashtable,
					struct list_head *used_buckets_list)
660 661 662 663 664 665 666 667
{
	struct relocation_entry *entry;
	struct relocation_head *rel_head;
	struct hlist_head *current_head;
	struct used_bucket *bucket;
	unsigned long hash;

	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
668 669 670 671

	if (!entry)
		return -ENOMEM;

672 673 674 675 676 677 678 679
	INIT_LIST_HEAD(&entry->head);
	entry->type = type;
	entry->value = v;

	hash = hash_min((uintptr_t)location, hashtable_bits);

	current_head = &relocation_hashtable[hash];

680 681 682 683
	/*
	 * Search for the relocation_head for the relocations that happen at the
	 * provided location
	 */
684 685 686 687 688 689 690 691 692 693 694
	bool found = false;
	struct relocation_head *rel_head_iter;

	hlist_for_each_entry(rel_head_iter, current_head, node) {
		if (rel_head_iter->location == location) {
			found = true;
			rel_head = rel_head_iter;
			break;
		}
	}

695 696 697 698 699
	/*
	 * If there has not yet been any relocations at the provided location,
	 * create a relocation_head for that location and populate it with this
	 * relocation_entry.
	 */
700 701
	if (!found) {
		rel_head = kmalloc(sizeof(*rel_head), GFP_KERNEL);
702 703 704 705 706 707

		if (!rel_head) {
			kfree(entry);
			return -ENOMEM;
		}

708 709
		rel_head->rel_entry =
			kmalloc(sizeof(struct list_head), GFP_KERNEL);
710 711 712 713 714 715 716

		if (!rel_head->rel_entry) {
			kfree(entry);
			kfree(rel_head);
			return -ENOMEM;
		}

717 718 719 720 721 722
		INIT_LIST_HEAD(rel_head->rel_entry);
		rel_head->location = location;
		INIT_HLIST_NODE(&rel_head->node);
		if (!current_head->first) {
			bucket =
				kmalloc(sizeof(struct used_bucket), GFP_KERNEL);
723 724 725 726 727 728 729 730

			if (!bucket) {
				kfree(entry);
				kfree(rel_head);
				kfree(rel_head->rel_entry);
				return -ENOMEM;
			}

731 732
			INIT_LIST_HEAD(&bucket->head);
			bucket->bucket = current_head;
733
			list_add(&bucket->head, used_buckets_list);
734 735 736 737 738 739 740 741 742 743
		}
		hlist_add_head(&rel_head->node, current_head);
	}

	/* Add relocation to head of discovered rel_head */
	list_add_tail(&entry->head, rel_head->rel_entry);

	return 0;
}

744 745 746
static unsigned int
initialize_relocation_hashtable(unsigned int num_relocations,
				struct hlist_head **relocation_hashtable)
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
{
	/* Can safely assume that bits is not greater than sizeof(long) */
	unsigned long hashtable_size = roundup_pow_of_two(num_relocations);
	unsigned int hashtable_bits = ilog2(hashtable_size);

	/*
	 * Double size of hashtable if num_relocations * 1.25 is greater than
	 * hashtable_size.
	 */
	int should_double_size = ((num_relocations + (num_relocations >> 2)) > (hashtable_size));

	hashtable_bits += should_double_size;

	hashtable_size <<= should_double_size;

762 763 764 765 766
	*relocation_hashtable = kmalloc_array(hashtable_size,
					      sizeof(*relocation_hashtable),
					      GFP_KERNEL);
	if (!*relocation_hashtable)
		return -ENOMEM;
767

768
	__hash_init(*relocation_hashtable, hashtable_size);
769 770 771 772

	return hashtable_bits;
}

Palmer Dabbelt's avatar
Palmer Dabbelt committed
773 774 775 776 777
int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
		       unsigned int symindex, unsigned int relsec,
		       struct module *me)
{
	Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
778
	int (*handler)(struct module *me, void *location, Elf_Addr v);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
779
	Elf_Sym *sym;
780
	void *location;
Palmer Dabbelt's avatar
Palmer Dabbelt committed
781 782 783
	unsigned int i, type;
	Elf_Addr v;
	int res;
784
	unsigned int num_relocations = sechdrs[relsec].sh_size / sizeof(*rel);
785 786 787 788 789 790 791 792 793 794 795
	struct hlist_head *relocation_hashtable;
	struct list_head used_buckets_list;
	unsigned int hashtable_bits;

	hashtable_bits = initialize_relocation_hashtable(num_relocations,
							 &relocation_hashtable);

	if (hashtable_bits < 0)
		return hashtable_bits;

	INIT_LIST_HEAD(&used_buckets_list);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
796 797 798 799

	pr_debug("Applying relocate section %u to %u\n", relsec,
	       sechdrs[relsec].sh_info);

800
	for (i = 0; i < num_relocations; i++) {
Palmer Dabbelt's avatar
Palmer Dabbelt committed
801 802 803 804 805 806 807 808 809 810
		/* This is where to make the change */
		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
			+ rel[i].r_offset;
		/* This is the symbol it is referring to */
		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
			+ ELF_RISCV_R_SYM(rel[i].r_info);
		if (IS_ERR_VALUE(sym->st_value)) {
			/* Ignore unresolved weak symbol */
			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
				continue;
811 812
			pr_warn("%s: Unknown symbol %s\n",
				me->name, strtab + sym->st_name);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
813 814 815 816 817
			return -ENOENT;
		}

		type = ELF_RISCV_R_TYPE(rel[i].r_info);

818 819
		if (type < ARRAY_SIZE(reloc_handlers))
			handler = reloc_handlers[type].reloc_handler;
Palmer Dabbelt's avatar
Palmer Dabbelt committed
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
		else
			handler = NULL;

		if (!handler) {
			pr_err("%s: Unknown relocation type %u\n",
			       me->name, type);
			return -EINVAL;
		}

		v = sym->st_value + rel[i].r_addend;

		if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
			unsigned int j;

			for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
835
				unsigned long hi20_loc =
Palmer Dabbelt's avatar
Palmer Dabbelt committed
836 837
					sechdrs[sechdrs[relsec].sh_info].sh_addr
					+ rel[j].r_offset;
838 839 840 841 842 843 844
				u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);

				/* Find the corresponding HI20 relocation entry */
				if (hi20_loc == sym->st_value
				    && (hi20_type == R_RISCV_PCREL_HI20
					|| hi20_type == R_RISCV_GOT_HI20)) {
					s32 hi20, lo12;
Palmer Dabbelt's avatar
Palmer Dabbelt committed
845 846 847
					Elf_Sym *hi20_sym =
						(Elf_Sym *)sechdrs[symindex].sh_addr
						+ ELF_RISCV_R_SYM(rel[j].r_info);
848
					unsigned long hi20_sym_val =
Palmer Dabbelt's avatar
Palmer Dabbelt committed
849 850
						hi20_sym->st_value
						+ rel[j].r_addend;
851

Palmer Dabbelt's avatar
Palmer Dabbelt committed
852
					/* Calculate lo12 */
853
					size_t offset = hi20_sym_val - hi20_loc;
854 855 856 857 858 859 860 861
					if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
					    && hi20_type == R_RISCV_GOT_HI20) {
						offset = module_emit_got_entry(
							 me, hi20_sym_val);
						offset = offset - hi20_loc;
					}
					hi20 = (offset + 0x800) & 0xfffff000;
					lo12 = offset - hi20;
Palmer Dabbelt's avatar
Palmer Dabbelt committed
862
					v = lo12;
863

Palmer Dabbelt's avatar
Palmer Dabbelt committed
864 865 866 867 868
					break;
				}
			}
			if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
				pr_err(
869
				  "%s: Can not find HI20 relocation information\n",
Palmer Dabbelt's avatar
Palmer Dabbelt committed
870 871 872 873 874
				  me->name);
				return -EINVAL;
			}
		}

875
		if (reloc_handlers[type].accumulate_handler)
876 877 878 879
			res = add_relocation_to_accumulate(me, type, location,
							   hashtable_bits, v,
							   relocation_hashtable,
							   &used_buckets_list);
880 881
		else
			res = handler(me, location, v);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
882 883 884 885
		if (res)
			return res;
	}

886 887
	process_accumulated_relocations(me, &relocation_hashtable,
					&used_buckets_list);
888

Palmer Dabbelt's avatar
Palmer Dabbelt committed
889 890
	return 0;
}
891 892 893 894

#if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
void *module_alloc(unsigned long size)
{
895 896
	return __vmalloc_node_range(size, 1, MODULES_VADDR,
				    MODULES_END, GFP_KERNEL,
897 898
				    PAGE_KERNEL, VM_FLUSH_RESET_PERMS,
				    NUMA_NO_NODE,
899 900 901
				    __builtin_return_address(0));
}
#endif
902 903 904 905 906 907 908 909 910 911 912 913 914

int module_finalize(const Elf_Ehdr *hdr,
		    const Elf_Shdr *sechdrs,
		    struct module *me)
{
	const Elf_Shdr *s;

	s = find_section(hdr, sechdrs, ".alternative");
	if (s)
		apply_module_alternatives((void *)s->sh_addr, s->sh_size);

	return 0;
}