Skip to content

Commit 2129a23

Browse files
RISC-V: ELF and module implementation
This patch contains the code that interfaces with ELF objects on RISC-V systems, the vast majority of which is present to load kernel modules. Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 5d8544e commit 2129a23

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

arch/riscv/include/asm/compat.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) 2012 ARM Ltd.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
#ifndef __ASM_COMPAT_H
17+
#define __ASM_COMPAT_H
18+
#ifdef CONFIG_COMPAT
19+
20+
#if defined(CONFIG_64BIT)
21+
#define COMPAT_UTS_MACHINE "riscv64\0\0"
22+
#elif defined(CONFIG_32BIT)
23+
#define COMPAT_UTS_MACHINE "riscv32\0\0"
24+
#else
25+
#error "Unknown RISC-V base ISA"
26+
#endif
27+
28+
#endif /*CONFIG_COMPAT*/
29+
#endif /*__ASM_COMPAT_H*/

arch/riscv/include/asm/elf.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (C) 2003 Matjaz Breskvar <[email protected]>
3+
* Copyright (C) 2010-2011 Jonas Bonn <[email protected]>
4+
* Copyright (C) 2012 Regents of the University of California
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*/
11+
12+
#ifndef _ASM_RISCV_ELF_H
13+
#define _ASM_RISCV_ELF_H
14+
15+
#include <uapi/asm/elf.h>
16+
#include <asm/auxvec.h>
17+
#include <asm/byteorder.h>
18+
19+
/* TODO: Move definition into include/uapi/linux/elf-em.h */
20+
#define EM_RISCV 0xF3
21+
22+
/*
23+
* These are used to set parameters in the core dumps.
24+
*/
25+
#define ELF_ARCH EM_RISCV
26+
27+
#ifdef CONFIG_64BIT
28+
#define ELF_CLASS ELFCLASS64
29+
#else
30+
#define ELF_CLASS ELFCLASS32
31+
#endif
32+
33+
#if defined(__LITTLE_ENDIAN)
34+
#define ELF_DATA ELFDATA2LSB
35+
#elif defined(__BIG_ENDIAN)
36+
#define ELF_DATA ELFDATA2MSB
37+
#else
38+
#error "Unknown endianness"
39+
#endif
40+
41+
/*
42+
* This is used to ensure we don't load something for the wrong architecture.
43+
*/
44+
#define elf_check_arch(x) ((x)->e_machine == EM_RISCV)
45+
46+
#define CORE_DUMP_USE_REGSET
47+
#define ELF_EXEC_PAGESIZE (PAGE_SIZE)
48+
49+
/*
50+
* This is the location that an ET_DYN program is loaded if exec'ed. Typical
51+
* use of this is to invoke "./ld.so someprog" to test out a new version of
52+
* the loader. We need to make sure that it is out of the way of the program
53+
* that it will "exec", and that there is sufficient room for the brk.
54+
*/
55+
#define ELF_ET_DYN_BASE ((TASK_SIZE / 3) * 2)
56+
57+
/*
58+
* This yields a mask that user programs can use to figure out what
59+
* instruction set this CPU supports. This could be done in user space,
60+
* but it's not easy, and we've already done it here.
61+
*/
62+
#define ELF_HWCAP (elf_hwcap)
63+
extern unsigned long elf_hwcap;
64+
65+
/*
66+
* This yields a string that ld.so will use to load implementation
67+
* specific libraries for optimization. This is more specific in
68+
* intent than poking at uname or /proc/cpuinfo.
69+
*/
70+
#define ELF_PLATFORM (NULL)
71+
72+
#define ARCH_DLINFO \
73+
do { \
74+
NEW_AUX_ENT(AT_SYSINFO_EHDR, \
75+
(elf_addr_t)current->mm->context.vdso); \
76+
} while (0)
77+
78+
79+
#define ARCH_HAS_SETUP_ADDITIONAL_PAGES
80+
struct linux_binprm;
81+
extern int arch_setup_additional_pages(struct linux_binprm *bprm,
82+
int uses_interp);
83+
84+
#endif /* _ASM_RISCV_ELF_H */

arch/riscv/include/asm/hwcap.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copied from arch/arm64/include/asm/hwcap.h
3+
*
4+
* Copyright (C) 2012 ARM Ltd.
5+
* Copyright (C) 2017 SiFive
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License version 2 as
9+
* published by the Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#ifndef __ASM_HWCAP_H
20+
#define __ASM_HWCAP_H
21+
22+
#include <uapi/asm/hwcap.h>
23+
24+
#ifndef __ASSEMBLY__
25+
/*
26+
* This yields a mask that user programs can use to figure out what
27+
* instruction set this cpu supports.
28+
*/
29+
#define ELF_HWCAP (elf_hwcap)
30+
31+
enum {
32+
CAP_HWCAP = 1,
33+
};
34+
35+
extern unsigned long elf_hwcap;
36+
#endif
37+
#endif

arch/riscv/mm/extable.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
3+
* Lennox Wu <[email protected]>
4+
* Chen Liqin <[email protected]>
5+
* Copyright (C) 2013 Regents of the University of California
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, see the file COPYING, or write
19+
* to the Free Software Foundation, Inc.,
20+
*/
21+
22+
23+
#include <linux/extable.h>
24+
#include <linux/module.h>
25+
#include <linux/uaccess.h>
26+
27+
int fixup_exception(struct pt_regs *regs)
28+
{
29+
const struct exception_table_entry *fixup;
30+
31+
fixup = search_exception_tables(regs->sepc);
32+
if (fixup) {
33+
regs->sepc = fixup->fixup;
34+
return 1;
35+
}
36+
return 0;
37+
}

0 commit comments

Comments
 (0)