1 // SPDX-License-Identifier: GPL-2.0
3 * AMD Encrypted Register State Support
5 * Author: Joerg Roedel <jroedel@suse.de>
9 * misc.h needs to be first because it knows how to include the other kernel
10 * headers in the pre-decompression code in a way that does not break
15 #include <asm/pgtable_types.h>
16 #include <asm/sev-es.h>
17 #include <asm/trapnr.h>
18 #include <asm/trap_pf.h>
19 #include <asm/msr-index.h>
20 #include <asm/fpu/xcr.h>
21 #include <asm/ptrace.h>
26 struct ghcb boot_ghcb_page
__aligned(PAGE_SIZE
);
27 struct ghcb
*boot_ghcb
;
30 * Copy a version of this function here - insn-eval.c can't be used in
31 * pre-decompression code.
33 static bool insn_has_rep_prefix(struct insn
*insn
)
38 insn_get_prefixes(insn
);
40 for_each_insn_prefix(insn
, i
, p
) {
41 if (p
== 0xf2 || p
== 0xf3)
49 * Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and
50 * doesn't use segments.
52 static unsigned long insn_get_seg_base(struct pt_regs
*regs
, int seg_reg_idx
)
57 static inline u64
sev_es_rd_ghcb_msr(void)
59 unsigned long low
, high
;
61 asm volatile("rdmsr" : "=a" (low
), "=d" (high
) :
62 "c" (MSR_AMD64_SEV_ES_GHCB
));
64 return ((high
<< 32) | low
);
67 static inline void sev_es_wr_ghcb_msr(u64 val
)
71 low
= val
& 0xffffffffUL
;
74 asm volatile("wrmsr" : : "c" (MSR_AMD64_SEV_ES_GHCB
),
75 "a"(low
), "d" (high
) : "memory");
78 static enum es_result
vc_decode_insn(struct es_em_ctxt
*ctxt
)
80 char buffer
[MAX_INSN_SIZE
];
83 memcpy(buffer
, (unsigned char *)ctxt
->regs
->ip
, MAX_INSN_SIZE
);
85 insn_init(&ctxt
->insn
, buffer
, MAX_INSN_SIZE
, 1);
86 insn_get_length(&ctxt
->insn
);
88 ret
= ctxt
->insn
.immediate
.got
? ES_OK
: ES_DECODE_FAILED
;
93 static enum es_result
vc_write_mem(struct es_em_ctxt
*ctxt
,
94 void *dst
, char *buf
, size_t size
)
96 memcpy(dst
, buf
, size
);
101 static enum es_result
vc_read_mem(struct es_em_ctxt
*ctxt
,
102 void *src
, char *buf
, size_t size
)
104 memcpy(buf
, src
, size
);
112 #define __pa(x) ((unsigned long)(x))
114 #define __BOOT_COMPRESSED
116 /* Basic instruction decoding support needed */
117 #include "../../lib/inat.c"
118 #include "../../lib/insn.c"
120 /* Include code for early handlers */
121 #include "../../kernel/sev-es-shared.c"
123 static bool early_setup_sev_es(void)
125 if (!sev_es_negotiate_protocol())
126 sev_es_terminate(GHCB_SEV_ES_REASON_PROTOCOL_UNSUPPORTED
);
128 if (set_page_decrypted((unsigned long)&boot_ghcb_page
))
131 /* Page is now mapped decrypted, clear it */
132 memset(&boot_ghcb_page
, 0, sizeof(boot_ghcb_page
));
134 boot_ghcb
= &boot_ghcb_page
;
136 /* Initialize lookup tables for the instruction decoder */
142 void sev_es_shutdown_ghcb(void)
147 if (!sev_es_check_cpu_features())
148 error("SEV-ES CPU Features missing.");
151 * GHCB Page must be flushed from the cache and mapped encrypted again.
152 * Otherwise the running kernel will see strange cache effects when
153 * trying to use that page.
155 if (set_page_encrypted((unsigned long)&boot_ghcb_page
))
156 error("Can't map GHCB page encrypted");
159 * GHCB page is mapped encrypted again and flushed from the cache.
160 * Mark it non-present now to catch bugs when #VC exceptions trigger
163 if (set_page_non_present((unsigned long)&boot_ghcb_page
))
164 error("Can't unmap GHCB page");
167 bool sev_es_check_ghcb_fault(unsigned long address
)
169 /* Check whether the fault was on the GHCB page */
170 return ((address
& PAGE_MASK
) == (unsigned long)&boot_ghcb_page
);
173 void do_boot_stage2_vc(struct pt_regs
*regs
, unsigned long exit_code
)
175 struct es_em_ctxt ctxt
;
176 enum es_result result
;
178 if (!boot_ghcb
&& !early_setup_sev_es())
179 sev_es_terminate(GHCB_SEV_ES_REASON_GENERAL_REQUEST
);
181 vc_ghcb_invalidate(boot_ghcb
);
182 result
= vc_init_em_ctxt(&ctxt
, regs
, exit_code
);
188 case SVM_EXIT_RDTSCP
:
189 result
= vc_handle_rdtsc(boot_ghcb
, &ctxt
, exit_code
);
192 result
= vc_handle_ioio(boot_ghcb
, &ctxt
);
195 result
= vc_handle_cpuid(boot_ghcb
, &ctxt
);
198 result
= ES_UNSUPPORTED
;
203 if (result
== ES_OK
) {
204 vc_finish_insn(&ctxt
);
205 } else if (result
!= ES_RETRY
) {
207 * For now, just halt the machine. That makes debugging easier,
208 * later we just call sev_es_terminate() here.
211 asm volatile("hlt\n");