xref: /openbmc/linux/arch/riscv/include/asm/module.h (revision 71844fac)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2017 Andes Technology Corporation */
3 
4 #ifndef _ASM_RISCV_MODULE_H
5 #define _ASM_RISCV_MODULE_H
6 
7 #include <asm-generic/module.h>
8 
9 #define MODULE_ARCH_VERMAGIC    "riscv"
10 
11 struct module;
12 u64 module_emit_got_entry(struct module *mod, u64 val);
13 u64 module_emit_plt_entry(struct module *mod, u64 val);
14 
15 #ifdef CONFIG_MODULE_SECTIONS
16 struct mod_section {
17 	struct elf64_shdr *shdr;
18 	int num_entries;
19 	int max_entries;
20 };
21 
22 struct mod_arch_specific {
23 	struct mod_section got;
24 	struct mod_section plt;
25 	struct mod_section got_plt;
26 };
27 
28 struct got_entry {
29 	u64 symbol_addr;	/* the real variable address */
30 };
31 
32 static inline struct got_entry emit_got_entry(u64 val)
33 {
34 	return (struct got_entry) {val};
35 }
36 
37 static inline struct got_entry *get_got_entry(u64 val,
38 					      const struct mod_section *sec)
39 {
40 	struct got_entry *got = (struct got_entry *)sec->shdr->sh_addr;
41 	int i;
42 	for (i = 0; i < sec->num_entries; i++) {
43 		if (got[i].symbol_addr == val)
44 			return &got[i];
45 	}
46 	return NULL;
47 }
48 
49 struct plt_entry {
50 	/*
51 	 * Trampoline code to real target address. The return address
52 	 * should be the original (pc+4) before entring plt entry.
53 	 */
54 	u32 insn_auipc;		/* auipc t0, 0x0                       */
55 	u32 insn_ld;		/* ld    t1, 0x10(t0)                  */
56 	u32 insn_jr;		/* jr    t1                            */
57 };
58 
59 #define OPC_AUIPC  0x0017
60 #define OPC_LD     0x3003
61 #define OPC_JALR   0x0067
62 #define REG_T0     0x5
63 #define REG_T1     0x6
64 
65 static inline struct plt_entry emit_plt_entry(u64 val, u64 plt, u64 got_plt)
66 {
67 	/*
68 	 * U-Type encoding:
69 	 * +------------+----------+----------+
70 	 * | imm[31:12] | rd[11:7] | opc[6:0] |
71 	 * +------------+----------+----------+
72 	 *
73 	 * I-Type encoding:
74 	 * +------------+------------+--------+----------+----------+
75 	 * | imm[31:20] | rs1[19:15] | funct3 | rd[11:7] | opc[6:0] |
76 	 * +------------+------------+--------+----------+----------+
77 	 *
78 	 */
79 	u64 offset = got_plt - plt;
80 	u32 hi20 = (offset + 0x800) & 0xfffff000;
81 	u32 lo12 = (offset - hi20);
82 	return (struct plt_entry) {
83 		OPC_AUIPC | (REG_T0 << 7) | hi20,
84 		OPC_LD | (lo12 << 20) | (REG_T0 << 15) | (REG_T1 << 7),
85 		OPC_JALR | (REG_T1 << 15)
86 	};
87 }
88 
89 static inline int get_got_plt_idx(u64 val, const struct mod_section *sec)
90 {
91 	struct got_entry *got_plt = (struct got_entry *)sec->shdr->sh_addr;
92 	int i;
93 	for (i = 0; i < sec->num_entries; i++) {
94 		if (got_plt[i].symbol_addr == val)
95 			return i;
96 	}
97 	return -1;
98 }
99 
100 static inline struct plt_entry *get_plt_entry(u64 val,
101 				      const struct mod_section *sec_plt,
102 				      const struct mod_section *sec_got_plt)
103 {
104 	struct plt_entry *plt = (struct plt_entry *)sec_plt->shdr->sh_addr;
105 	int got_plt_idx = get_got_plt_idx(val, sec_got_plt);
106 	if (got_plt_idx >= 0)
107 		return plt + got_plt_idx;
108 	else
109 		return NULL;
110 }
111 
112 #endif /* CONFIG_MODULE_SECTIONS */
113 
114 #endif /* _ASM_RISCV_MODULE_H */
115