x86/sev: Allow for use of the early boot GHCB for PSC requests
[linux-2.6-block.git] / arch / x86 / include / asm / sev.h
CommitLineData
29dcc60f
JR
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * AMD Encrypted Register State Support
4 *
5 * Author: Joerg Roedel <jroedel@suse.de>
6 */
7
8#ifndef __ASM_ENCRYPTED_STATE_H
9#define __ASM_ENCRYPTED_STATE_H
10
11#include <linux/types.h>
0144e3b8
DG
12#include <linux/sev-guest.h>
13
597cfe48 14#include <asm/insn.h>
b81fc74d 15#include <asm/sev-common.h>
c01fce9c 16#include <asm/bootparam.h>
29dcc60f 17
2ea29c5a 18#define GHCB_PROTOCOL_MIN 1ULL
cbd3d4f7 19#define GHCB_PROTOCOL_MAX 2ULL
b81fc74d 20#define GHCB_DEFAULT_USAGE 0ULL
29dcc60f 21
29dcc60f
JR
22#define VMGEXIT() { asm volatile("rep; vmmcall\n\r"); }
23
597cfe48
JR
24enum es_result {
25 ES_OK, /* All good */
26 ES_UNSUPPORTED, /* Requested operation not supported */
27 ES_VMM_ERROR, /* Unexpected state from the VMM */
28 ES_DECODE_FAILED, /* Instruction decoding failed */
29 ES_EXCEPTION, /* Instruction caused exception */
30 ES_RETRY, /* Retry instruction emulation */
31};
32
33struct es_fault_info {
34 unsigned long vector;
35 unsigned long error_code;
36 unsigned long cr2;
37};
38
39struct pt_regs;
40
41/* ES instruction emulation context */
42struct es_em_ctxt {
43 struct pt_regs *regs;
44 struct insn insn;
45 struct es_fault_info fi;
46};
47
5ea98e01
BS
48/*
49 * AMD SEV Confidential computing blob structure. The structure is
50 * defined in OVMF UEFI firmware header:
51 * https://github.com/tianocore/edk2/blob/master/OvmfPkg/Include/Guid/ConfidentialComputingSevSnpBlob.h
52 */
53#define CC_BLOB_SEV_HDR_MAGIC 0x45444d41
54struct cc_blob_sev_info {
55 u32 magic;
56 u16 version;
57 u16 reserved;
58 u64 secrets_phys;
59 u32 secrets_len;
60 u32 rsvd1;
61 u64 cpuid_phys;
62 u32 cpuid_len;
63 u32 rsvd2;
64} __packed;
65
29dcc60f
JR
66void do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code);
67
68static inline u64 lower_bits(u64 val, unsigned int bits)
69{
70 u64 mask = (1ULL << bits) - 1;
71
72 return (val & mask);
73}
74
8940ac9c
TL
75struct real_mode_header;
76enum stack_type;
77
74d8d9d5
JR
78/* Early IDT entry points for #VC handler */
79extern void vc_no_ghcb(void);
1aa9aa8e
JR
80extern void vc_boot_ghcb(void);
81extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
74d8d9d5 82
0bd6f1e5
BS
83/* Software defined (when rFlags.CF = 1) */
84#define PVALIDATE_FAIL_NOUPDATE 255
85
81cc3df9
BS
86/* RMP page size */
87#define RMP_PG_SIZE_4K 0
88
0afb6b66
TL
89#define RMPADJUST_VMSA_PAGE_BIT BIT(16)
90
d5af44dd
BS
91/* SNP Guest message request */
92struct snp_req_data {
93 unsigned long req_gpa;
94 unsigned long resp_gpa;
95 unsigned long data_gpa;
96 unsigned int data_npages;
97};
98
2bf93ffb 99struct sev_guest_platform_data {
3a45b375
BS
100 u64 secrets_gpa;
101};
102
c2106a23
BS
103/*
104 * The secrets page contains 96-bytes of reserved field that can be used by
105 * the guest OS. The guest OS uses the area to save the message sequence
106 * number for each VMPCK.
107 *
108 * See the GHCB spec section Secret page layout for the format for this area.
109 */
110struct secrets_os_area {
111 u32 msg_seqno_0;
112 u32 msg_seqno_1;
113 u32 msg_seqno_2;
114 u32 msg_seqno_3;
115 u64 ap_jump_table_pa;
116 u8 rsvd[40];
117 u8 guest_usage[32];
118} __packed;
119
120#define VMPCK_KEY_LEN 32
121
122/* See the SNP spec version 0.9 for secrets page format */
123struct snp_secrets_page_layout {
124 u32 version;
125 u32 imien : 1,
126 rsvd1 : 31;
127 u32 fms;
128 u32 rsvd2;
129 u8 gosvw[16];
130 u8 vmpck0[VMPCK_KEY_LEN];
131 u8 vmpck1[VMPCK_KEY_LEN];
132 u8 vmpck2[VMPCK_KEY_LEN];
133 u8 vmpck3[VMPCK_KEY_LEN];
134 struct secrets_os_area os_area;
135 u8 rsvd3[3840];
136} __packed;
137
315562c9
JR
138#ifdef CONFIG_AMD_MEM_ENCRYPT
139extern struct static_key_false sev_es_enable_key;
140extern void __sev_es_ist_enter(struct pt_regs *regs);
141extern void __sev_es_ist_exit(void);
142static __always_inline void sev_es_ist_enter(struct pt_regs *regs)
143{
144 if (static_branch_unlikely(&sev_es_enable_key))
145 __sev_es_ist_enter(regs);
146}
147static __always_inline void sev_es_ist_exit(void)
148{
149 if (static_branch_unlikely(&sev_es_enable_key))
150 __sev_es_ist_exit();
151}
8940ac9c 152extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh);
4ca68e02
JR
153extern void __sev_es_nmi_complete(void);
154static __always_inline void sev_es_nmi_complete(void)
155{
156 if (static_branch_unlikely(&sev_es_enable_key))
157 __sev_es_nmi_complete();
158}
39336f4f 159extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
5bb6c1d1 160
81cc3df9
BS
161static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs)
162{
163 int rc;
164
165 /* "rmpadjust" mnemonic support in binutils 2.36 and newer */
166 asm volatile(".byte 0xF3,0x0F,0x01,0xFE\n\t"
167 : "=a"(rc)
168 : "a"(vaddr), "c"(rmp_psize), "d"(attrs)
169 : "memory", "cc");
170
171 return rc;
172}
0bd6f1e5
BS
173static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate)
174{
175 bool no_rmpupdate;
176 int rc;
177
178 /* "pvalidate" mnemonic support in binutils 2.36 and newer */
179 asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFF\n\t"
180 CC_SET(c)
181 : CC_OUT(c) (no_rmpupdate), "=a"(rc)
182 : "a"(vaddr), "c"(rmp_psize), "d"(validate)
183 : "memory", "cc");
184
185 if (no_rmpupdate)
186 return PVALIDATE_FAIL_NOUPDATE;
187
188 return rc;
189}
0144e3b8
DG
190
191struct snp_guest_request_ioctl;
192
95d33bfa 193void setup_ghcb(void);
5e5ccff6 194void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr,
5dee19b6 195 unsigned long npages);
5e5ccff6 196void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr,
5dee19b6 197 unsigned long npages);
5e5ccff6 198void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op);
5dee19b6
TL
199void snp_set_memory_shared(unsigned long vaddr, unsigned long npages);
200void snp_set_memory_private(unsigned long vaddr, unsigned long npages);
0afb6b66 201void snp_set_wakeup_secondary_cpu(void);
c01fce9c 202bool snp_init(struct boot_params *bp);
c93c296f 203void __init __noreturn snp_abort(void);
0144e3b8 204int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio);
315562c9
JR
205#else
206static inline void sev_es_ist_enter(struct pt_regs *regs) { }
207static inline void sev_es_ist_exit(void) { }
8940ac9c 208static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
4ca68e02 209static inline void sev_es_nmi_complete(void) { }
39336f4f 210static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
0bd6f1e5 211static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) { return 0; }
81cc3df9 212static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { return 0; }
95d33bfa 213static inline void setup_ghcb(void) { }
5e5ccff6 214static inline void __init
5dee19b6 215early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned long npages) { }
5e5ccff6 216static inline void __init
5dee19b6 217early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned long npages) { }
5e5ccff6 218static inline void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op) { }
5dee19b6
TL
219static inline void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) { }
220static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npages) { }
0afb6b66 221static inline void snp_set_wakeup_secondary_cpu(void) { }
c01fce9c 222static inline bool snp_init(struct boot_params *bp) { return false; }
b190a043 223static inline void snp_abort(void) { }
0144e3b8 224static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
d5af44dd
BS
225{
226 return -ENOTTY;
227}
315562c9
JR
228#endif
229
29dcc60f 230#endif