processor.h 11.2 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0-only */
C
Catalin Marinas 已提交
2 3 4 5 6 7 8 9 10
/*
 * Based on arch/arm/include/asm/processor.h
 *
 * Copyright (C) 1995-1999 Russell King
 * Copyright (C) 2012 ARM Ltd.
 */
#ifndef __ASM_PROCESSOR_H
#define __ASM_PROCESSOR_H

11 12 13 14 15 16 17 18
/*
 * On arm64 systems, unaligned accesses by the CPU are cheap, and so there is
 * no point in shifting all network buffers by 2 bytes just to make some IP
 * header fields appear aligned in memory, potentially sacrificing some DMA
 * performance on some platforms.
 */
#define NET_IP_ALIGN	0

19 20 21
#define MTE_CTRL_GCR_USER_EXCL_SHIFT	0
#define MTE_CTRL_GCR_USER_EXCL_MASK	0xffff

22 23
#define MTE_CTRL_TCF_SYNC		(1UL << 16)
#define MTE_CTRL_TCF_ASYNC		(1UL << 17)
24
#define MTE_CTRL_TCF_ASYMM		(1UL << 18)
25

26
#ifndef __ASSEMBLY__
C
Catalin Marinas 已提交
27

28
#include <linux/build_bug.h>
29 30
#include <linux/cache.h>
#include <linux/init.h>
31
#include <linux/stddef.h>
C
Catalin Marinas 已提交
32
#include <linux/string.h>
33
#include <linux/thread_info.h>
C
Catalin Marinas 已提交
34

35 36
#include <vdso/processor.h>

37
#include <asm/alternative.h>
38
#include <asm/cpufeature.h>
C
Catalin Marinas 已提交
39
#include <asm/hw_breakpoint.h>
40
#include <asm/kasan.h>
41
#include <asm/lse.h>
42
#include <asm/pgtable-hwdef.h>
43
#include <asm/pointer_auth.h>
C
Catalin Marinas 已提交
44
#include <asm/ptrace.h>
45
#include <asm/spectre.h>
C
Catalin Marinas 已提交
46 47
#include <asm/types.h>

48 49 50 51
/*
 * TASK_SIZE - the maximum size of a user space task.
 * TASK_UNMAPPED_BASE - the lower boundary of the mmap VM area.
 */
52

S
Steve Capper 已提交
53
#define DEFAULT_MAP_WINDOW_64	(UL(1) << VA_BITS_MIN)
S
Steve Capper 已提交
54
#define TASK_SIZE_64		(UL(1) << vabits_actual)
M
Mark Rutland 已提交
55
#define TASK_SIZE_MAX		(UL(1) << VA_BITS)
56

57
#ifdef CONFIG_COMPAT
58
#if defined(CONFIG_ARM64_64K_PAGES) && defined(CONFIG_KUSER_HELPERS)
59 60 61 62
/*
 * With CONFIG_ARM64_64K_PAGES enabled, the last page is occupied
 * by the compat vectors page.
 */
63
#define TASK_SIZE_32		UL(0x100000000)
64 65 66
#else
#define TASK_SIZE_32		(UL(0x100000000) - PAGE_SIZE)
#endif /* CONFIG_ARM64_64K_PAGES */
67 68 69 70
#define TASK_SIZE		(test_thread_flag(TIF_32BIT) ? \
				TASK_SIZE_32 : TASK_SIZE_64)
#define TASK_SIZE_OF(tsk)	(test_tsk_thread_flag(tsk, TIF_32BIT) ? \
				TASK_SIZE_32 : TASK_SIZE_64)
71 72
#define DEFAULT_MAP_WINDOW	(test_thread_flag(TIF_32BIT) ? \
				TASK_SIZE_32 : DEFAULT_MAP_WINDOW_64)
73 74
#else
#define TASK_SIZE		TASK_SIZE_64
75
#define DEFAULT_MAP_WINDOW	DEFAULT_MAP_WINDOW_64
76 77
#endif /* CONFIG_COMPAT */

78 79 80 81
#ifdef CONFIG_ARM64_FORCE_52BIT
#define STACK_TOP_MAX		TASK_SIZE_64
#define TASK_UNMAPPED_BASE	(PAGE_ALIGN(TASK_SIZE / 4))
#else
82
#define STACK_TOP_MAX		DEFAULT_MAP_WINDOW_64
83 84
#define TASK_UNMAPPED_BASE	(PAGE_ALIGN(DEFAULT_MAP_WINDOW / 4))
#endif /* CONFIG_ARM64_FORCE_52BIT */
85

C
Catalin Marinas 已提交
86 87 88 89 90 91 92
#ifdef CONFIG_COMPAT
#define AARCH32_VECTORS_BASE	0xffff0000
#define STACK_TOP		(test_thread_flag(TIF_32BIT) ? \
				AARCH32_VECTORS_BASE : STACK_TOP_MAX)
#else
#define STACK_TOP		STACK_TOP_MAX
#endif /* CONFIG_COMPAT */
93

94
#ifndef CONFIG_ARM64_FORCE_52BIT
95 96 97 98 99 100
#define arch_get_mmap_end(addr) ((addr > DEFAULT_MAP_WINDOW) ? TASK_SIZE :\
				DEFAULT_MAP_WINDOW)

#define arch_get_mmap_base(addr, base) ((addr > DEFAULT_MAP_WINDOW) ? \
					base + TASK_SIZE - DEFAULT_MAP_WINDOW :\
					base)
101
#endif /* CONFIG_ARM64_FORCE_52BIT */
102

103
extern phys_addr_t arm64_dma_phys_limit;
104
#define ARCH_LOW_ADDRESS_LIMIT	(arm64_dma_phys_limit - 1)
C
Catalin Marinas 已提交
105 106

struct debug_info {
107
#ifdef CONFIG_HAVE_HW_BREAKPOINT
C
Catalin Marinas 已提交
108 109 110 111 112 113 114 115
	/* Have we suspended stepping by a debugger? */
	int			suspended_step;
	/* Allow breakpoints and watchpoints to be disabled for this thread. */
	int			bps_disabled;
	int			wps_disabled;
	/* Hardware breakpoints pinned to this task. */
	struct perf_event	*hbp_break[ARM_MAX_BRP];
	struct perf_event	*hbp_watch[ARM_MAX_WRP];
116
#endif
C
Catalin Marinas 已提交
117 118
};

119 120
enum vec_type {
	ARM64_VEC_SVE = 0,
121
	ARM64_VEC_SME,
122 123 124
	ARM64_VEC_MAX,
};

C
Catalin Marinas 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
struct cpu_context {
	unsigned long x19;
	unsigned long x20;
	unsigned long x21;
	unsigned long x22;
	unsigned long x23;
	unsigned long x24;
	unsigned long x25;
	unsigned long x26;
	unsigned long x27;
	unsigned long x28;
	unsigned long fp;
	unsigned long sp;
	unsigned long pc;
};

struct thread_struct {
	struct cpu_context	cpu_context;	/* cpu context */
143 144 145 146 147 148 149 150 151 152 153 154

	/*
	 * Whitelisted fields for hardened usercopy:
	 * Maintainers must ensure manually that this contains no
	 * implicit padding.
	 */
	struct {
		unsigned long	tp_value;	/* TLS register */
		unsigned long	tp2_value;
		struct user_fpsimd_state fpsimd_state;
	} uw;

155
	unsigned int		fpsimd_cpu;
156
	void			*sve_state;	/* SVE registers, if any */
157 158
	unsigned int		vl[ARM64_VEC_MAX];	/* vector length */
	unsigned int		vl_onexec[ARM64_VEC_MAX]; /* vl after next exec */
C
Catalin Marinas 已提交
159
	unsigned long		fault_address;	/* fault info */
160
	unsigned long		fault_code;	/* ESR_EL1 value */
C
Catalin Marinas 已提交
161
	struct debug_info	debug;		/* debugging */
162
#ifdef CONFIG_ARM64_PTR_AUTH
163
	struct ptrauth_keys_user	keys_user;
164
#ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
165
	struct ptrauth_keys_kernel	keys_kernel;
166
#endif
167
#endif
168
#ifdef CONFIG_ARM64_MTE
169
	u64			mte_ctrl;
170
#endif
171
	u64			sctlr_user;
172
	u64			svcr;
173
	u64			tpidr2_el0;
C
Catalin Marinas 已提交
174 175
};

176 177 178 179 180 181
static inline unsigned int thread_get_vl(struct thread_struct *thread,
					 enum vec_type type)
{
	return thread->vl[type];
}

182 183
static inline unsigned int thread_get_sve_vl(struct thread_struct *thread)
{
184 185 186
	return thread_get_vl(thread, ARM64_VEC_SVE);
}

187 188 189 190 191
static inline unsigned int thread_get_sme_vl(struct thread_struct *thread)
{
	return thread_get_vl(thread, ARM64_VEC_SME);
}

192 193 194 195 196 197 198 199 200 201 202
unsigned int task_get_vl(const struct task_struct *task, enum vec_type type);
void task_set_vl(struct task_struct *task, enum vec_type type,
		 unsigned long vl);
void task_set_vl_onexec(struct task_struct *task, enum vec_type type,
			unsigned long vl);
unsigned int task_get_vl_onexec(const struct task_struct *task,
				enum vec_type type);

static inline unsigned int task_get_sve_vl(const struct task_struct *task)
{
	return task_get_vl(task, ARM64_VEC_SVE);
203 204
}

205 206 207 208 209
static inline unsigned int task_get_sme_vl(const struct task_struct *task)
{
	return task_get_vl(task, ARM64_VEC_SME);
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
static inline void task_set_sve_vl(struct task_struct *task, unsigned long vl)
{
	task_set_vl(task, ARM64_VEC_SVE, vl);
}

static inline unsigned int task_get_sve_vl_onexec(const struct task_struct *task)
{
	return task_get_vl_onexec(task, ARM64_VEC_SVE);
}

static inline void task_set_sve_vl_onexec(struct task_struct *task,
					  unsigned long vl)
{
	task_set_vl_onexec(task, ARM64_VEC_SVE, vl);
}
225

226 227 228
#define SCTLR_USER_MASK                                                        \
	(SCTLR_ELx_ENIA | SCTLR_ELx_ENIB | SCTLR_ELx_ENDA | SCTLR_ELx_ENDB |   \
	 SCTLR_EL1_TCF0_MASK)
229

230 231 232
static inline void arch_thread_struct_whitelist(unsigned long *offset,
						unsigned long *size)
{
233 234 235 236 237 238 239 240
	/* Verify that there is no padding among the whitelisted fields: */
	BUILD_BUG_ON(sizeof_field(struct thread_struct, uw) !=
		     sizeof_field(struct thread_struct, uw.tp_value) +
		     sizeof_field(struct thread_struct, uw.tp2_value) +
		     sizeof_field(struct thread_struct, uw.fpsimd_state));

	*offset = offsetof(struct thread_struct, uw);
	*size = sizeof_field(struct thread_struct, uw);
241 242
}

243 244 245 246 247
#ifdef CONFIG_COMPAT
#define task_user_tls(t)						\
({									\
	unsigned long *__tls;						\
	if (is_compat_thread(task_thread_info(t)))			\
248
		__tls = &(t)->thread.uw.tp2_value;			\
249
	else								\
250
		__tls = &(t)->thread.uw.tp_value;			\
251 252 253
	__tls;								\
 })
#else
254
#define task_user_tls(t)	(&(t)->thread.uw.tp_value)
255 256
#endif

257 258 259
/* Sync TPIDR_EL0 back to thread_struct for current */
void tls_preserve_current_state(void);

260 261 262
#define INIT_THREAD {				\
	.fpsimd_cpu = NR_CPUS,			\
}
C
Catalin Marinas 已提交
263 264 265 266

static inline void start_thread_common(struct pt_regs *regs, unsigned long pc)
{
	memset(regs, 0, sizeof(*regs));
267
	forget_syscall(regs);
C
Catalin Marinas 已提交
268
	regs->pc = pc;
269 270 271

	if (system_uses_irq_prio_masking())
		regs->pmr_save = GIC_PRIO_IRQON;
C
Catalin Marinas 已提交
272 273 274 275 276 277 278
}

static inline void start_thread(struct pt_regs *regs, unsigned long pc,
				unsigned long sp)
{
	start_thread_common(regs, pc);
	regs->pstate = PSR_MODE_EL0t;
279
	spectre_v4_enable_task_mitigation(current);
C
Catalin Marinas 已提交
280 281 282 283 284 285 286 287
	regs->sp = sp;
}

#ifdef CONFIG_COMPAT
static inline void compat_start_thread(struct pt_regs *regs, unsigned long pc,
				       unsigned long sp)
{
	start_thread_common(regs, pc);
M
Mark Rutland 已提交
288
	regs->pstate = PSR_AA32_MODE_USR;
C
Catalin Marinas 已提交
289
	if (pc & 1)
M
Mark Rutland 已提交
290
		regs->pstate |= PSR_AA32_T_BIT;
291 292

#ifdef __AARCH64EB__
M
Mark Rutland 已提交
293
	regs->pstate |= PSR_AA32_E_BIT;
294 295
#endif

296
	spectre_v4_enable_task_mitigation(current);
C
Catalin Marinas 已提交
297 298 299 300
	regs->compat_sp = sp;
}
#endif

301 302 303 304 305 306 307 308 309 310 311 312
static inline bool is_ttbr0_addr(unsigned long addr)
{
	/* entry assembly clears tags for TTBR0 addrs */
	return addr < TASK_SIZE;
}

static inline bool is_ttbr1_addr(unsigned long addr)
{
	/* TTBR1 addresses may have a tag if KASAN_SW_TAGS is in use */
	return arch_kasan_reset_tag(addr) >= PAGE_OFFSET;
}

C
Catalin Marinas 已提交
313 314 315 316 317 318
/* Forward declaration, a strange C thing */
struct task_struct;

/* Free all resources held by a thread. */
extern void release_thread(struct task_struct *);

319
unsigned long __get_wchan(struct task_struct *p);
C
Catalin Marinas 已提交
320

321
void update_sctlr_el1(u64 sctlr);
322

C
Catalin Marinas 已提交
323 324 325 326 327
/* Thread switching */
extern struct task_struct *cpu_switch_to(struct task_struct *prev,
					 struct task_struct *next);

#define task_pt_regs(p) \
328
	((struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1)
C
Catalin Marinas 已提交
329

330
#define KSTK_EIP(tsk)	((unsigned long)task_pt_regs(tsk)->pc)
331
#define KSTK_ESP(tsk)	user_stack_pointer(task_pt_regs(tsk))
C
Catalin Marinas 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

/*
 * Prefetching support
 */
#define ARCH_HAS_PREFETCH
static inline void prefetch(const void *ptr)
{
	asm volatile("prfm pldl1keep, %a0\n" : : "p" (ptr));
}

#define ARCH_HAS_PREFETCHW
static inline void prefetchw(const void *ptr)
{
	asm volatile("prfm pstl1keep, %a0\n" : : "p" (ptr));
}

#define ARCH_HAS_SPINLOCK_PREFETCH
349
static inline void spin_lock_prefetch(const void *ptr)
C
Catalin Marinas 已提交
350
{
351 352 353
	asm volatile(ARM64_LSE_ATOMIC_INSN(
		     "prfm pstl1strm, %a0",
		     "nop") : : "p" (ptr));
C
Catalin Marinas 已提交
354 355
}

356 357 358
extern unsigned long __ro_after_init signal_minsigstksz; /* sigframe size */
extern void __init minsigstksz_setup(void);

359 360 361 362 363 364 365 366 367 368 369
/*
 * Not at the top of the file due to a direct #include cycle between
 * <asm/fpsimd.h> and <asm/processor.h>.  Deferring this #include
 * ensures that contents of processor.h are visible to fpsimd.h even if
 * processor.h is included first.
 *
 * These prctl helpers are the only things in this file that require
 * fpsimd.h.  The core code expects them to be in this header.
 */
#include <asm/fpsimd.h>

370
/* Userspace interface for PR_S[MV]E_{SET,GET}_VL prctl()s: */
371 372
#define SVE_SET_VL(arg)	sve_set_current_vl(arg)
#define SVE_GET_VL()	sve_get_current_vl()
373 374
#define SME_SET_VL(arg)	sme_set_current_vl(arg)
#define SME_GET_VL()	sme_get_current_vl()
375

376 377 378
/* PR_PAC_RESET_KEYS prctl */
#define PAC_RESET_KEYS(tsk, arg)	ptrauth_prctl_reset_keys(tsk, arg)

379 380 381 382 383
/* PR_PAC_{SET,GET}_ENABLED_KEYS prctl */
#define PAC_SET_ENABLED_KEYS(tsk, keys, enabled)				\
	ptrauth_set_enabled_keys(tsk, keys, enabled)
#define PAC_GET_ENABLED_KEYS(tsk) ptrauth_get_enabled_keys(tsk)

384 385
#ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
/* PR_{SET,GET}_TAGGED_ADDR_CTRL prctl */
386 387 388 389
long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg);
long get_tagged_addr_ctrl(struct task_struct *task);
#define SET_TAGGED_ADDR_CTRL(arg)	set_tagged_addr_ctrl(current, arg)
#define GET_TAGGED_ADDR_CTRL()		get_tagged_addr_ctrl(current)
390 391
#endif

392 393 394 395 396 397 398
/*
 * For CONFIG_GCC_PLUGIN_STACKLEAK
 *
 * These need to be macros because otherwise we get stuck in a nightmare
 * of header definitions for the use of task_stack_page.
 */

399 400 401 402 403
#define current_top_of_stack()								\
({											\
	struct stack_info _info;							\
	BUG_ON(!on_accessible_stack(current, current_stack_pointer, 1, &_info));	\
	_info.high;									\
404
})
405
#define on_thread_stack()	(on_task_stack(current, current_stack_pointer, 1, NULL))
406

407
#endif /* __ASSEMBLY__ */
C
Catalin Marinas 已提交
408
#endif /* __ASM_PROCESSOR_H */