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/cpufeature.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
40extern 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_FDPIC_CORE_EFLAGS	0
45#define ELF_EXEC_PAGESIZE	(PAGE_SIZE)
46
47/*
48 * This is the location that an ET_DYN program is loaded if exec'ed.  Typical
49 * use of this is to invoke "./ld.so someprog" to test out a new version of
50 * the loader.  We need to make sure that it is out of the way of the program
51 * that it will "exec", and that there is sufficient room for the brk.
52 */
53#define ELF_ET_DYN_BASE		((DEFAULT_MAP_WINDOW / 3) * 2)
54
55#ifdef CONFIG_64BIT
56#define STACK_RND_MASK		(is_compat_task() ? \
57				 0x7ff >> (PAGE_SHIFT - 12) : \
58				 0x3ffff >> (PAGE_SHIFT - 12))
59#endif
60
61/*
62 * Provides information on the availiable set of ISA extensions to userspace,
63 * via a bitmap that coorespends to each single-letter ISA extension.  This is
64 * essentially defunct, but will remain for compatibility with userspace.
65 */
66#define ELF_HWCAP	riscv_get_elf_hwcap()
67extern unsigned long elf_hwcap;
68
69#define ELF_FDPIC_PLAT_INIT(_r, _exec_map_addr, _interp_map_addr, dynamic_addr) \
70	do { \
71		(_r)->a1 = _exec_map_addr; \
72		(_r)->a2 = _interp_map_addr; \
73		(_r)->a3 = dynamic_addr; \
74	} while (0)
75
76/*
77 * This yields a string that ld.so will use to load implementation
78 * specific libraries for optimization.  This is more specific in
79 * intent than poking at uname or /proc/cpuinfo.
80 */
81#define ELF_PLATFORM	(NULL)
82
83#define COMPAT_ELF_PLATFORM	(NULL)
84
85#define ARCH_DLINFO						\
86do {								\
87	/*							\
88	 * Note that we add ulong after elf_addr_t because	\
89	 * casting current->mm->context.vdso triggers a cast	\
90	 * warning of cast from pointer to integer for		\
91	 * COMPAT ELFCLASS32.					\
92	 */							\
93	NEW_AUX_ENT(AT_SYSINFO_EHDR,				\
94		(elf_addr_t)(ulong)current->mm->context.vdso);	\
95	NEW_AUX_ENT(AT_L1I_CACHESIZE,				\
96		get_cache_size(1, CACHE_TYPE_INST));		\
97	NEW_AUX_ENT(AT_L1I_CACHEGEOMETRY,			\
98		get_cache_geometry(1, CACHE_TYPE_INST));	\
99	NEW_AUX_ENT(AT_L1D_CACHESIZE,				\
100		get_cache_size(1, CACHE_TYPE_DATA));		\
101	NEW_AUX_ENT(AT_L1D_CACHEGEOMETRY,			\
102		get_cache_geometry(1, CACHE_TYPE_DATA));	\
103	NEW_AUX_ENT(AT_L2_CACHESIZE,				\
104		get_cache_size(2, CACHE_TYPE_UNIFIED));		\
105	NEW_AUX_ENT(AT_L2_CACHEGEOMETRY,			\
106		get_cache_geometry(2, CACHE_TYPE_UNIFIED));	\
107	NEW_AUX_ENT(AT_L3_CACHESIZE,				\
108		get_cache_size(3, CACHE_TYPE_UNIFIED));		\
109	NEW_AUX_ENT(AT_L3_CACHEGEOMETRY,			\
110		get_cache_geometry(3, CACHE_TYPE_UNIFIED));	\
111	/*							 \
112	 * Should always be nonzero unless there's a kernel bug. \
113	 * If we haven't determined a sensible value to give to	 \
114	 * userspace, omit the entry:				 \
115	 */							 \
116	if (likely(signal_minsigstksz))				 \
117		NEW_AUX_ENT(AT_MINSIGSTKSZ, signal_minsigstksz); \
118	else							 \
119		NEW_AUX_ENT(AT_IGNORE, 0);			 \
120} while (0)
121
122#ifdef CONFIG_MMU
123#define ARCH_HAS_SETUP_ADDITIONAL_PAGES
124struct linux_binprm;
125extern int arch_setup_additional_pages(struct linux_binprm *bprm,
126	int uses_interp);
127#endif /* CONFIG_MMU */
128
129#define ELF_CORE_COPY_REGS(dest, regs)			\
130do {							\
131	*(struct user_regs_struct *)&(dest) =		\
132		*(struct user_regs_struct *)regs;	\
133} while (0);
134
135#ifdef CONFIG_COMPAT
136
137#define SET_PERSONALITY(ex)					\
138do {	set_compat_task((ex).e_ident[EI_CLASS] == ELFCLASS32);	\
139	if (personality(current->personality) != PER_LINUX32)	\
140		set_personality(PER_LINUX |			\
141			(current->personality & (~PER_MASK)));	\
142} while (0)
143
144#define COMPAT_ELF_ET_DYN_BASE		((TASK_SIZE_32 / 3) * 2)
145
146/* rv32 registers */
147typedef compat_ulong_t			compat_elf_greg_t;
148typedef compat_elf_greg_t		compat_elf_gregset_t[ELF_NGREG];
149
150extern int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
151					      int uses_interp);
152#define compat_arch_setup_additional_pages \
153				compat_arch_setup_additional_pages
154
155#endif /* CONFIG_COMPAT */
156#endif /* _ASM_RISCV_ELF_H */
157