xref: /linux/arch/riscv/include/asm/elf.h (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
4  * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
5  * Copyright (C) 2012 Regents of the University of California
6  */
7 
8 #ifndef _ASM_RISCV_ELF_H
9 #define _ASM_RISCV_ELF_H
10 
11 #include <uapi/linux/elf.h>
12 #include <linux/compat.h>
13 #include <uapi/asm/elf.h>
14 #include <asm/auxvec.h>
15 #include <asm/byteorder.h>
16 #include <asm/cacheinfo.h>
17 #include <asm/hwcap.h>
18 
19 /*
20  * These are used to set parameters in the core dumps.
21  */
22 #define ELF_ARCH	EM_RISCV
23 
24 #ifndef ELF_CLASS
25 #ifdef CONFIG_64BIT
26 #define ELF_CLASS	ELFCLASS64
27 #else
28 #define ELF_CLASS	ELFCLASS32
29 #endif
30 #endif
31 
32 #define ELF_DATA	ELFDATA2LSB
33 
34 /*
35  * This is used to ensure we don't load something for the wrong architecture.
36  */
37 #define elf_check_arch(x) (((x)->e_machine == EM_RISCV) && \
38 			   ((x)->e_ident[EI_CLASS] == ELF_CLASS))
39 
40 extern bool compat_elf_check_arch(Elf32_Ehdr *hdr);
41 #define compat_elf_check_arch	compat_elf_check_arch
42 
43 #define CORE_DUMP_USE_REGSET
44 #define ELF_EXEC_PAGESIZE	(PAGE_SIZE)
45 
46 /*
47  * This is the location that an ET_DYN program is loaded if exec'ed.  Typical
48  * use of this is to invoke "./ld.so someprog" to test out a new version of
49  * the loader.  We need to make sure that it is out of the way of the program
50  * that it will "exec", and that there is sufficient room for the brk.
51  */
52 #define ELF_ET_DYN_BASE		((TASK_SIZE / 3) * 2)
53 
54 #ifdef CONFIG_64BIT
55 #ifdef CONFIG_COMPAT
56 #define STACK_RND_MASK		(test_thread_flag(TIF_32BIT) ? \
57 				 0x7ff >> (PAGE_SHIFT - 12) : \
58 				 0x3ffff >> (PAGE_SHIFT - 12))
59 #else
60 #define STACK_RND_MASK		(0x3ffff >> (PAGE_SHIFT - 12))
61 #endif
62 #endif
63 
64 /*
65  * Provides information on the availiable set of ISA extensions to userspace,
66  * via a bitmap that coorespends to each single-letter ISA extension.  This is
67  * essentially defunct, but will remain for compatibility with userspace.
68  */
69 #define ELF_HWCAP	(elf_hwcap & ((1UL << RISCV_ISA_EXT_BASE) - 1))
70 extern unsigned long elf_hwcap;
71 
72 /*
73  * This yields a string that ld.so will use to load implementation
74  * specific libraries for optimization.  This is more specific in
75  * intent than poking at uname or /proc/cpuinfo.
76  */
77 #define ELF_PLATFORM	(NULL)
78 
79 #define COMPAT_ELF_PLATFORM	(NULL)
80 
81 #ifdef CONFIG_MMU
82 #define ARCH_DLINFO						\
83 do {								\
84 	/*							\
85 	 * Note that we add ulong after elf_addr_t because	\
86 	 * casting current->mm->context.vdso triggers a cast	\
87 	 * warning of cast from pointer to integer for		\
88 	 * COMPAT ELFCLASS32.					\
89 	 */							\
90 	NEW_AUX_ENT(AT_SYSINFO_EHDR,				\
91 		(elf_addr_t)(ulong)current->mm->context.vdso);	\
92 	NEW_AUX_ENT(AT_L1I_CACHESIZE,				\
93 		get_cache_size(1, CACHE_TYPE_INST));		\
94 	NEW_AUX_ENT(AT_L1I_CACHEGEOMETRY,			\
95 		get_cache_geometry(1, CACHE_TYPE_INST));	\
96 	NEW_AUX_ENT(AT_L1D_CACHESIZE,				\
97 		get_cache_size(1, CACHE_TYPE_DATA));		\
98 	NEW_AUX_ENT(AT_L1D_CACHEGEOMETRY,			\
99 		get_cache_geometry(1, CACHE_TYPE_DATA));	\
100 	NEW_AUX_ENT(AT_L2_CACHESIZE,				\
101 		get_cache_size(2, CACHE_TYPE_UNIFIED));		\
102 	NEW_AUX_ENT(AT_L2_CACHEGEOMETRY,			\
103 		get_cache_geometry(2, CACHE_TYPE_UNIFIED));	\
104 	NEW_AUX_ENT(AT_L3_CACHESIZE,				\
105 		get_cache_size(3, CACHE_TYPE_UNIFIED));		\
106 	NEW_AUX_ENT(AT_L3_CACHEGEOMETRY,			\
107 		get_cache_geometry(3, CACHE_TYPE_UNIFIED));	\
108 } while (0)
109 #define ARCH_HAS_SETUP_ADDITIONAL_PAGES
110 struct linux_binprm;
111 extern int arch_setup_additional_pages(struct linux_binprm *bprm,
112 	int uses_interp);
113 #endif /* CONFIG_MMU */
114 
115 #define ELF_CORE_COPY_REGS(dest, regs)			\
116 do {							\
117 	*(struct user_regs_struct *)&(dest) =		\
118 		*(struct user_regs_struct *)regs;	\
119 } while (0);
120 
121 #ifdef CONFIG_COMPAT
122 
123 #define SET_PERSONALITY(ex)					\
124 do {    if ((ex).e_ident[EI_CLASS] == ELFCLASS32)		\
125 		set_thread_flag(TIF_32BIT);			\
126 	else							\
127 		clear_thread_flag(TIF_32BIT);			\
128 	if (personality(current->personality) != PER_LINUX32)	\
129 		set_personality(PER_LINUX |			\
130 			(current->personality & (~PER_MASK)));	\
131 } while (0)
132 
133 #define COMPAT_ELF_ET_DYN_BASE		((TASK_SIZE_32 / 3) * 2)
134 
135 /* rv32 registers */
136 typedef compat_ulong_t			compat_elf_greg_t;
137 typedef compat_elf_greg_t		compat_elf_gregset_t[ELF_NGREG];
138 
139 extern int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
140 					      int uses_interp);
141 #define compat_arch_setup_additional_pages \
142 				compat_arch_setup_additional_pages
143 
144 #endif /* CONFIG_COMPAT */
145 #endif /* _ASM_RISCV_ELF_H */
146