summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZong Li <zong@andestech.com>2018-03-15 04:50:41 -0400
committerPalmer Dabbelt <palmer@sifive.com>2018-04-02 23:00:54 -0400
commitab1ef68e54019937cf859f2c86c9ead6f3e62f19 (patch)
tree15fd30ddaaf18d61caf77f8748e291f3802987b5
parent0adb32858b0bddf4ada5f364a84ed60b196dbcda (diff)
RISC-V: Add sections of PLT and GOT for kernel module
The address of external symbols will locate more than 32-bit offset in 64-bit kernel with sv39 or sv48 virtual addressing. Module loader emits the GOT and PLT entries for data symbols and function symbols respectively. The PLT entry is a trampoline code for jumping to the 64-bit real address. The GOT entry is just the data symbol address. Signed-off-by: Zong Li <zong@andestech.com> Signed-off-by: Palmer Dabbelt <palmer@sifive.com>
-rw-r--r--arch/riscv/Kconfig5
-rw-r--r--arch/riscv/Makefile5
-rw-r--r--arch/riscv/include/asm/module.h103
-rw-r--r--arch/riscv/kernel/Makefile1
-rw-r--r--arch/riscv/kernel/module-sections.c139
-rw-r--r--arch/riscv/kernel/module.lds7
6 files changed, 260 insertions, 0 deletions
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 04807c7f64cc..90ff52059794 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -131,6 +131,10 @@ choice
131 bool "medium any code model" 131 bool "medium any code model"
132endchoice 132endchoice
133 133
134config MODULE_SECTIONS
135 bool
136 select HAVE_MOD_ARCH_SPECIFIC
137
134choice 138choice
135 prompt "Maximum Physical Memory" 139 prompt "Maximum Physical Memory"
136 default MAXPHYSMEM_2GB if 32BIT 140 default MAXPHYSMEM_2GB if 32BIT
@@ -141,6 +145,7 @@ choice
141 bool "2GiB" 145 bool "2GiB"
142 config MAXPHYSMEM_128GB 146 config MAXPHYSMEM_128GB
143 depends on 64BIT && CMODEL_MEDANY 147 depends on 64BIT && CMODEL_MEDANY
148 select MODULE_SECTIONS if MODULES
144 bool "128GiB" 149 bool "128GiB"
145endchoice 150endchoice
146 151
diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index 6719dd30ec5b..c72d408c05c0 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -56,6 +56,11 @@ endif
56ifeq ($(CONFIG_CMODEL_MEDANY),y) 56ifeq ($(CONFIG_CMODEL_MEDANY),y)
57 KBUILD_CFLAGS += -mcmodel=medany 57 KBUILD_CFLAGS += -mcmodel=medany
58endif 58endif
59ifeq ($(CONFIG_MODULE_SECTIONS),y)
60 KBUILD_LDFLAGS_MODULE += -T $(srctree)/arch/riscv/kernel/module.lds
61endif
62
63KBUILD_CFLAGS_MODULE += $(call cc-option,-mno-relax)
59 64
60# GCC versions that support the "-mstrict-align" option default to allowing 65# GCC versions that support the "-mstrict-align" option default to allowing
61# unaligned accesses. While unaligned accesses are explicitly allowed in the 66# unaligned accesses. While unaligned accesses are explicitly allowed in the
diff --git a/arch/riscv/include/asm/module.h b/arch/riscv/include/asm/module.h
new file mode 100644
index 000000000000..e61d73f82d4d
--- /dev/null
+++ b/arch/riscv/include/asm/module.h
@@ -0,0 +1,103 @@
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
11u64 module_emit_got_entry(struct module *mod, u64 val);
12u64 module_emit_plt_entry(struct module *mod, u64 val);
13
14#ifdef CONFIG_MODULE_SECTIONS
15struct mod_section {
16 struct elf64_shdr *shdr;
17 int num_entries;
18 int max_entries;
19};
20
21struct mod_arch_specific {
22 struct mod_section got;
23 struct mod_section plt;
24};
25
26struct got_entry {
27 u64 symbol_addr; /* the real variable address */
28};
29
30static inline struct got_entry emit_got_entry(u64 val)
31{
32 return (struct got_entry) {val};
33}
34
35static inline struct got_entry *get_got_entry(u64 val,
36 const struct mod_section *sec)
37{
38 struct got_entry *got = (struct got_entry *)sec->shdr->sh_addr;
39 int i;
40 for (i = 0; i < sec->num_entries; i++) {
41 if (got[i].symbol_addr == val)
42 return &got[i];
43 }
44 return NULL;
45}
46
47struct plt_entry {
48 /*
49 * Trampoline code to real target address. The return address
50 * should be the original (pc+4) before entring plt entry.
51 * For 8 byte alignment of symbol_addr,
52 * don't pack structure to remove the padding.
53 */
54 u32 insn_auipc; /* auipc t0, 0x0 */
55 u32 insn_ld; /* ld t1, 0x10(t0) */
56 u32 insn_jr; /* jr t1 */
57 u64 symbol_addr; /* the real jump target address */
58};
59
60#define OPC_AUIPC 0x0017
61#define OPC_LD 0x3003
62#define OPC_JALR 0x0067
63#define REG_T0 0x5
64#define REG_T1 0x6
65#define IMM_OFFSET 0x10
66
67static inline struct plt_entry emit_plt_entry(u64 val)
68{
69 /*
70 * U-Type encoding:
71 * +------------+----------+----------+
72 * | imm[31:12] | rd[11:7] | opc[6:0] |
73 * +------------+----------+----------+
74 *
75 * I-Type encoding:
76 * +------------+------------+--------+----------+----------+
77 * | imm[31:20] | rs1[19:15] | funct3 | rd[11:7] | opc[6:0] |
78 * +------------+------------+--------+----------+----------+
79 *
80 */
81 return (struct plt_entry) {
82 OPC_AUIPC | (REG_T0 << 7),
83 OPC_LD | (IMM_OFFSET << 20) | (REG_T0 << 15) | (REG_T1 << 7),
84 OPC_JALR | (REG_T1 << 15),
85 val
86 };
87}
88
89static inline struct plt_entry *get_plt_entry(u64 val,
90 const struct mod_section *sec)
91{
92 struct plt_entry *plt = (struct plt_entry *)sec->shdr->sh_addr;
93 int i;
94 for (i = 0; i < sec->num_entries; i++) {
95 if (plt[i].symbol_addr == val)
96 return &plt[i];
97 }
98 return NULL;
99}
100
101#endif /* CONFIG_MODULE_SECTIONS */
102
103#endif /* _ASM_RISCV_MODULE_H */
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index 196f62ffc428..d355e3c18278 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -34,6 +34,7 @@ CFLAGS_setup.o := -mcmodel=medany
34obj-$(CONFIG_SMP) += smpboot.o 34obj-$(CONFIG_SMP) += smpboot.o
35obj-$(CONFIG_SMP) += smp.o 35obj-$(CONFIG_SMP) += smp.o
36obj-$(CONFIG_MODULES) += module.o 36obj-$(CONFIG_MODULES) += module.o
37obj-$(CONFIG_MODULE_SECTIONS) += module-sections.o
37obj-$(CONFIG_FUNCTION_TRACER) += mcount.o 38obj-$(CONFIG_FUNCTION_TRACER) += mcount.o
38obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o 39obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
39 40
diff --git a/arch/riscv/kernel/module-sections.c b/arch/riscv/kernel/module-sections.c
new file mode 100644
index 000000000000..94ba1551eac3
--- /dev/null
+++ b/arch/riscv/kernel/module-sections.c
@@ -0,0 +1,139 @@
1/* SPDX-License-Identifier: GPL-2.0
2 *
3 * Copyright (C) 2014-2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
4 *
5 * Copyright (C) 2018 Andes Technology Corporation <zong@andestech.com>
6 */
7
8#include <linux/elf.h>
9#include <linux/kernel.h>
10#include <linux/module.h>
11
12u64 module_emit_got_entry(struct module *mod, u64 val)
13{
14 struct mod_section *got_sec = &mod->arch.got;
15 int i = got_sec->num_entries;
16 struct got_entry *got = get_got_entry(val, got_sec);
17
18 if (got)
19 return (u64)got;
20
21 /* There is no duplicate entry, create a new one */
22 got = (struct got_entry *)got_sec->shdr->sh_addr;
23 got[i] = emit_got_entry(val);
24
25 got_sec->num_entries++;
26 BUG_ON(got_sec->num_entries > got_sec->max_entries);
27
28 return (u64)&got[i];
29}
30
31u64 module_emit_plt_entry(struct module *mod, u64 val)
32{
33 struct mod_section *plt_sec = &mod->arch.plt;
34 struct plt_entry *plt = get_plt_entry(val, plt_sec);
35 int i = plt_sec->num_entries;
36
37 if (plt)
38 return (u64)plt;
39
40 /* There is no duplicate entry, create a new one */
41 plt = (struct plt_entry *)plt_sec->shdr->sh_addr;
42 plt[i] = emit_plt_entry(val);
43
44 plt_sec->num_entries++;
45 BUG_ON(plt_sec->num_entries > plt_sec->max_entries);
46
47 return (u64)&plt[i];
48}
49
50static int is_rela_equal(const Elf64_Rela *x, const Elf64_Rela *y)
51{
52 return x->r_info == y->r_info && x->r_addend == y->r_addend;
53}
54
55static bool duplicate_rela(const Elf64_Rela *rela, int idx)
56{
57 int i;
58 for (i = 0; i < idx; i++) {
59 if (is_rela_equal(&rela[i], &rela[idx]))
60 return true;
61 }
62 return false;
63}
64
65static void count_max_entries(Elf64_Rela *relas, int num,
66 unsigned int *plts, unsigned int *gots)
67{
68 unsigned int type, i;
69
70 for (i = 0; i < num; i++) {
71 type = ELF64_R_TYPE(relas[i].r_info);
72 if (type == R_RISCV_CALL_PLT) {
73 if (!duplicate_rela(relas, i))
74 (*plts)++;
75 } else if (type == R_RISCV_GOT_HI20) {
76 if (!duplicate_rela(relas, i))
77 (*gots)++;
78 }
79 }
80}
81
82int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
83 char *secstrings, struct module *mod)
84{
85 unsigned int num_plts = 0;
86 unsigned int num_gots = 0;
87 int i;
88
89 /*
90 * Find the empty .got and .plt sections.
91 */
92 for (i = 0; i < ehdr->e_shnum; i++) {
93 if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
94 mod->arch.plt.shdr = sechdrs + i;
95 else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got"))
96 mod->arch.got.shdr = sechdrs + i;
97 }
98
99 if (!mod->arch.plt.shdr) {
100 pr_err("%s: module PLT section(s) missing\n", mod->name);
101 return -ENOEXEC;
102 }
103 if (!mod->arch.got.shdr) {
104 pr_err("%s: module GOT section(s) missing\n", mod->name);
105 return -ENOEXEC;
106 }
107
108 /* Calculate the maxinum number of entries */
109 for (i = 0; i < ehdr->e_shnum; i++) {
110 Elf64_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset;
111 int num_rela = sechdrs[i].sh_size / sizeof(Elf64_Rela);
112 Elf64_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info;
113
114 if (sechdrs[i].sh_type != SHT_RELA)
115 continue;
116
117 /* ignore relocations that operate on non-exec sections */
118 if (!(dst_sec->sh_flags & SHF_EXECINSTR))
119 continue;
120
121 count_max_entries(relas, num_rela, &num_plts, &num_gots);
122 }
123
124 mod->arch.plt.shdr->sh_type = SHT_NOBITS;
125 mod->arch.plt.shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
126 mod->arch.plt.shdr->sh_addralign = L1_CACHE_BYTES;
127 mod->arch.plt.shdr->sh_size = (num_plts + 1) * sizeof(struct plt_entry);
128 mod->arch.plt.num_entries = 0;
129 mod->arch.plt.max_entries = num_plts;
130
131 mod->arch.got.shdr->sh_type = SHT_NOBITS;
132 mod->arch.got.shdr->sh_flags = SHF_ALLOC;
133 mod->arch.got.shdr->sh_addralign = L1_CACHE_BYTES;
134 mod->arch.got.shdr->sh_size = (num_gots + 1) * sizeof(struct got_entry);
135 mod->arch.got.num_entries = 0;
136 mod->arch.got.max_entries = num_gots;
137
138 return 0;
139}
diff --git a/arch/riscv/kernel/module.lds b/arch/riscv/kernel/module.lds
new file mode 100644
index 000000000000..7ef580e62883
--- /dev/null
+++ b/arch/riscv/kernel/module.lds
@@ -0,0 +1,7 @@
1/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright (C) 2017 Andes Technology Corporation */
3
4SECTIONS {
5 .plt (NOLOAD) : { BYTE(0) }
6 .got (NOLOAD) : { BYTE(0) }
7}