1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Macros for Flexible Return and Event Delivery (FRED)
4 */
5
6#ifndef ASM_X86_FRED_H
7#define ASM_X86_FRED_H
8
9#include <linux/const.h>
10
11#include <asm/asm.h>
12#include <asm/trapnr.h>
13
14/*
15 * FRED event return instruction opcodes for ERET{S,U}; supported in
16 * binutils >= 2.41.
17 */
18#define ERETS			_ASM_BYTES(0xf2,0x0f,0x01,0xca)
19#define ERETU			_ASM_BYTES(0xf3,0x0f,0x01,0xca)
20
21/*
22 * RSP is aligned to a 64-byte boundary before used to push a new stack frame
23 */
24#define FRED_STACK_FRAME_RSP_MASK	_AT(unsigned long, (~0x3f))
25
26/*
27 * Used for the return address for call emulation during code patching,
28 * and measured in 64-byte cache lines.
29 */
30#define FRED_CONFIG_REDZONE_AMOUNT	1
31#define FRED_CONFIG_REDZONE		(_AT(unsigned long, FRED_CONFIG_REDZONE_AMOUNT) << 6)
32#define FRED_CONFIG_INT_STKLVL(l)	(_AT(unsigned long, l) << 9)
33#define FRED_CONFIG_ENTRYPOINT(p)	_AT(unsigned long, (p))
34
35#ifndef __ASSEMBLY__
36
37#ifdef CONFIG_X86_FRED
38#include <linux/kernel.h>
39
40#include <asm/ptrace.h>
41
42struct fred_info {
43	/* Event data: CR2, DR6, ... */
44	unsigned long edata;
45	unsigned long resv;
46};
47
48/* Full format of the FRED stack frame */
49struct fred_frame {
50	struct pt_regs   regs;
51	struct fred_info info;
52};
53
54static __always_inline struct fred_info *fred_info(struct pt_regs *regs)
55{
56	return &container_of(regs, struct fred_frame, regs)->info;
57}
58
59static __always_inline unsigned long fred_event_data(struct pt_regs *regs)
60{
61	return fred_info(regs)->edata;
62}
63
64void asm_fred_entrypoint_user(void);
65void asm_fred_entrypoint_kernel(void);
66void asm_fred_entry_from_kvm(struct fred_ss);
67
68__visible void fred_entry_from_user(struct pt_regs *regs);
69__visible void fred_entry_from_kernel(struct pt_regs *regs);
70__visible void __fred_entry_from_kvm(struct pt_regs *regs);
71
72/* Can be called from noinstr code, thus __always_inline */
73static __always_inline void fred_entry_from_kvm(unsigned int type, unsigned int vector)
74{
75	struct fred_ss ss = {
76		.ss     =__KERNEL_DS,
77		.type   = type,
78		.vector = vector,
79		.nmi    = type == EVENT_TYPE_NMI,
80		.lm     = 1,
81	};
82
83	asm_fred_entry_from_kvm(ss);
84}
85
86void cpu_init_fred_exceptions(void);
87void fred_complete_exception_setup(void);
88
89#else /* CONFIG_X86_FRED */
90static __always_inline unsigned long fred_event_data(struct pt_regs *regs) { return 0; }
91static inline void cpu_init_fred_exceptions(void) { }
92static inline void fred_complete_exception_setup(void) { }
93static __always_inline void fred_entry_from_kvm(unsigned int type, unsigned int vector) { }
94#endif /* CONFIG_X86_FRED */
95#endif /* !__ASSEMBLY__ */
96
97#endif /* ASM_X86_FRED_H */
98