1 // SPDX-License-Identifier: GPL-2.0
3 * AMD Encrypted Register State Support
5 * Author: Joerg Roedel <jroedel@suse.de>
7 * This file is not compiled stand-alone. It contains code shared
8 * between the pre-decompression boot code and the running Linux kernel
9 * and is included directly into both code-bases.
12 #ifndef __BOOT_COMPRESSED
13 #define error(v) pr_err(v)
14 #define has_cpuflag(f) boot_cpu_has(f)
17 static bool __init sev_es_check_cpu_features(void)
19 if (!has_cpuflag(X86_FEATURE_RDRAND)) {
20 error("RDRAND instruction not supported - no trusted source of randomness available\n");
27 static void sev_es_terminate(unsigned int reason)
29 u64 val = GHCB_SEV_TERMINATE;
32 * Tell the hypervisor what went wrong - only reason-set 0 is
33 * currently supported.
35 val |= GHCB_SEV_TERMINATE_REASON(0, reason);
37 /* Request Guest Termination from Hypvervisor */
38 sev_es_wr_ghcb_msr(val);
42 asm volatile("hlt\n" : : : "memory");
45 static bool sev_es_negotiate_protocol(void)
49 /* Do the GHCB protocol version negotiation */
50 sev_es_wr_ghcb_msr(GHCB_SEV_INFO_REQ);
52 val = sev_es_rd_ghcb_msr();
54 if (GHCB_INFO(val) != GHCB_SEV_INFO)
57 if (GHCB_PROTO_MAX(val) < GHCB_PROTO_OUR ||
58 GHCB_PROTO_MIN(val) > GHCB_PROTO_OUR)
64 static __always_inline void vc_ghcb_invalidate(struct ghcb *ghcb)
66 memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
69 static bool vc_decoding_needed(unsigned long exit_code)
71 /* Exceptions don't require to decode the instruction */
72 return !(exit_code >= SVM_EXIT_EXCP_BASE &&
73 exit_code <= SVM_EXIT_LAST_EXCP);
76 static enum es_result vc_init_em_ctxt(struct es_em_ctxt *ctxt,
78 unsigned long exit_code)
80 enum es_result ret = ES_OK;
82 memset(ctxt, 0, sizeof(*ctxt));
85 if (vc_decoding_needed(exit_code))
86 ret = vc_decode_insn(ctxt);
91 static void vc_finish_insn(struct es_em_ctxt *ctxt)
93 ctxt->regs->ip += ctxt->insn.length;
96 static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
97 struct es_em_ctxt *ctxt,
98 u64 exit_code, u64 exit_info_1,
103 /* Fill in protocol and format specifiers */
104 ghcb->protocol_version = GHCB_PROTOCOL_MAX;
105 ghcb->ghcb_usage = GHCB_DEFAULT_USAGE;
107 ghcb_set_sw_exit_code(ghcb, exit_code);
108 ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
109 ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
111 sev_es_wr_ghcb_msr(__pa(ghcb));
114 if ((ghcb->save.sw_exit_info_1 & 0xffffffff) == 1) {
115 u64 info = ghcb->save.sw_exit_info_2;
118 info = ghcb->save.sw_exit_info_2;
119 v = info & SVM_EVTINJ_VEC_MASK;
121 /* Check if exception information from hypervisor is sane. */
122 if ((info & SVM_EVTINJ_VALID) &&
123 ((v == X86_TRAP_GP) || (v == X86_TRAP_UD)) &&
124 ((info & SVM_EVTINJ_TYPE_MASK) == SVM_EVTINJ_TYPE_EXEPT)) {
126 if (info & SVM_EVTINJ_VALID_ERR)
127 ctxt->fi.error_code = info >> 32;
140 * Boot VC Handler - This is the first VC handler during boot, there is no GHCB
141 * page yet, so it only supports the MSR based communication with the
142 * hypervisor and only the CPUID exit-code.
144 void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code)
146 unsigned int fn = lower_bits(regs->ax, 32);
149 /* Only CPUID is supported via MSR protocol */
150 if (exit_code != SVM_EXIT_CPUID)
153 sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_EAX));
155 val = sev_es_rd_ghcb_msr();
156 if (GHCB_SEV_GHCB_RESP_CODE(val) != GHCB_SEV_CPUID_RESP)
158 regs->ax = val >> 32;
160 sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_EBX));
162 val = sev_es_rd_ghcb_msr();
163 if (GHCB_SEV_GHCB_RESP_CODE(val) != GHCB_SEV_CPUID_RESP)
165 regs->bx = val >> 32;
167 sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_ECX));
169 val = sev_es_rd_ghcb_msr();
170 if (GHCB_SEV_GHCB_RESP_CODE(val) != GHCB_SEV_CPUID_RESP)
172 regs->cx = val >> 32;
174 sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_EDX));
176 val = sev_es_rd_ghcb_msr();
177 if (GHCB_SEV_GHCB_RESP_CODE(val) != GHCB_SEV_CPUID_RESP)
179 regs->dx = val >> 32;
182 * This is a VC handler and the #VC is only raised when SEV-ES is
183 * active, which means SEV must be active too. Do sanity checks on the
184 * CPUID results to make sure the hypervisor does not trick the kernel
185 * into the no-sev path. This could map sensitive data unencrypted and
186 * make it accessible to the hypervisor.
188 * In particular, check for:
189 * - Availability of CPUID leaf 0x8000001f
192 * The hypervisor might still report the wrong C-bit position, but this
193 * can't be checked here.
196 if (fn == 0x80000000 && (regs->ax < 0x8000001f))
199 else if ((fn == 0x8000001f && !(regs->ax & BIT(1))))
203 /* Skip over the CPUID two-byte opcode */
209 sev_es_wr_ghcb_msr(GHCB_SEV_TERMINATE);
212 /* Shouldn't get here - if we do halt the machine */
214 asm volatile("hlt\n");
217 static enum es_result vc_insn_string_read(struct es_em_ctxt *ctxt,
218 void *src, char *buf,
219 unsigned int data_size,
223 int i, b = backwards ? -1 : 1;
224 enum es_result ret = ES_OK;
226 for (i = 0; i < count; i++) {
227 void *s = src + (i * data_size * b);
228 char *d = buf + (i * data_size);
230 ret = vc_read_mem(ctxt, s, d, data_size);
238 static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt,
239 void *dst, char *buf,
240 unsigned int data_size,
244 int i, s = backwards ? -1 : 1;
245 enum es_result ret = ES_OK;
247 for (i = 0; i < count; i++) {
248 void *d = dst + (i * data_size * s);
249 char *b = buf + (i * data_size);
251 ret = vc_write_mem(ctxt, d, b, data_size);
259 #define IOIO_TYPE_STR BIT(2)
260 #define IOIO_TYPE_IN 1
261 #define IOIO_TYPE_INS (IOIO_TYPE_IN | IOIO_TYPE_STR)
262 #define IOIO_TYPE_OUT 0
263 #define IOIO_TYPE_OUTS (IOIO_TYPE_OUT | IOIO_TYPE_STR)
265 #define IOIO_REP BIT(3)
267 #define IOIO_ADDR_64 BIT(9)
268 #define IOIO_ADDR_32 BIT(8)
269 #define IOIO_ADDR_16 BIT(7)
271 #define IOIO_DATA_32 BIT(6)
272 #define IOIO_DATA_16 BIT(5)
273 #define IOIO_DATA_8 BIT(4)
275 #define IOIO_SEG_ES (0 << 10)
276 #define IOIO_SEG_DS (3 << 10)
278 static enum es_result vc_ioio_exitinfo(struct es_em_ctxt *ctxt, u64 *exitinfo)
280 struct insn *insn = &ctxt->insn;
283 switch (insn->opcode.bytes[0]) {
287 *exitinfo |= IOIO_TYPE_INS;
288 *exitinfo |= IOIO_SEG_ES;
289 *exitinfo |= (ctxt->regs->dx & 0xffff) << 16;
295 *exitinfo |= IOIO_TYPE_OUTS;
296 *exitinfo |= IOIO_SEG_DS;
297 *exitinfo |= (ctxt->regs->dx & 0xffff) << 16;
300 /* IN immediate opcodes */
303 *exitinfo |= IOIO_TYPE_IN;
304 *exitinfo |= (u8)insn->immediate.value << 16;
307 /* OUT immediate opcodes */
310 *exitinfo |= IOIO_TYPE_OUT;
311 *exitinfo |= (u8)insn->immediate.value << 16;
314 /* IN register opcodes */
317 *exitinfo |= IOIO_TYPE_IN;
318 *exitinfo |= (ctxt->regs->dx & 0xffff) << 16;
321 /* OUT register opcodes */
324 *exitinfo |= IOIO_TYPE_OUT;
325 *exitinfo |= (ctxt->regs->dx & 0xffff) << 16;
329 return ES_DECODE_FAILED;
332 switch (insn->opcode.bytes[0]) {
339 /* Single byte opcodes */
340 *exitinfo |= IOIO_DATA_8;
343 /* Length determined by instruction parsing */
344 *exitinfo |= (insn->opnd_bytes == 2) ? IOIO_DATA_16
347 switch (insn->addr_bytes) {
349 *exitinfo |= IOIO_ADDR_16;
352 *exitinfo |= IOIO_ADDR_32;
355 *exitinfo |= IOIO_ADDR_64;
359 if (insn_has_rep_prefix(insn))
360 *exitinfo |= IOIO_REP;
365 static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
367 struct pt_regs *regs = ctxt->regs;
368 u64 exit_info_1, exit_info_2;
371 ret = vc_ioio_exitinfo(ctxt, &exit_info_1);
375 if (exit_info_1 & IOIO_TYPE_STR) {
379 bool df = ((regs->flags & X86_EFLAGS_DF) == X86_EFLAGS_DF);
380 unsigned int io_bytes, exit_bytes;
381 unsigned int ghcb_count, op_count;
382 unsigned long es_base;
386 * For the string variants with rep prefix the amount of in/out
387 * operations per #VC exception is limited so that the kernel
388 * has a chance to take interrupts and re-schedule while the
389 * instruction is emulated.
391 io_bytes = (exit_info_1 >> 4) & 0x7;
392 ghcb_count = sizeof(ghcb->shared_buffer) / io_bytes;
394 op_count = (exit_info_1 & IOIO_REP) ? regs->cx : 1;
395 exit_info_2 = min(op_count, ghcb_count);
396 exit_bytes = exit_info_2 * io_bytes;
398 es_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_ES);
400 /* Read bytes of OUTS into the shared buffer */
401 if (!(exit_info_1 & IOIO_TYPE_IN)) {
402 ret = vc_insn_string_read(ctxt,
403 (void *)(es_base + regs->si),
404 ghcb->shared_buffer, io_bytes,
411 * Issue an VMGEXIT to the HV to consume the bytes from the
412 * shared buffer or to have it write them into the shared buffer
413 * depending on the instruction: OUTS or INS.
415 sw_scratch = __pa(ghcb) + offsetof(struct ghcb, shared_buffer);
416 ghcb_set_sw_scratch(ghcb, sw_scratch);
417 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO,
418 exit_info_1, exit_info_2);
422 /* Read bytes from shared buffer into the guest's destination. */
423 if (exit_info_1 & IOIO_TYPE_IN) {
424 ret = vc_insn_string_write(ctxt,
425 (void *)(es_base + regs->di),
426 ghcb->shared_buffer, io_bytes,
432 regs->di -= exit_bytes;
434 regs->di += exit_bytes;
437 regs->si -= exit_bytes;
439 regs->si += exit_bytes;
442 if (exit_info_1 & IOIO_REP)
443 regs->cx -= exit_info_2;
445 ret = regs->cx ? ES_RETRY : ES_OK;
449 /* IN/OUT into/from rAX */
451 int bits = (exit_info_1 & 0x70) >> 1;
454 if (!(exit_info_1 & IOIO_TYPE_IN))
455 rax = lower_bits(regs->ax, bits);
457 ghcb_set_rax(ghcb, rax);
459 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO, exit_info_1, 0);
463 if (exit_info_1 & IOIO_TYPE_IN) {
464 if (!ghcb_rax_is_valid(ghcb))
466 regs->ax = lower_bits(ghcb->save.rax, bits);
473 static enum es_result vc_handle_cpuid(struct ghcb *ghcb,
474 struct es_em_ctxt *ctxt)
476 struct pt_regs *regs = ctxt->regs;
477 u32 cr4 = native_read_cr4();
480 ghcb_set_rax(ghcb, regs->ax);
481 ghcb_set_rcx(ghcb, regs->cx);
483 if (cr4 & X86_CR4_OSXSAVE)
484 /* Safe to read xcr0 */
485 ghcb_set_xcr0(ghcb, xgetbv(XCR_XFEATURE_ENABLED_MASK));
487 /* xgetbv will cause #GP - use reset value for xcr0 */
488 ghcb_set_xcr0(ghcb, 1);
490 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_CPUID, 0, 0);
494 if (!(ghcb_rax_is_valid(ghcb) &&
495 ghcb_rbx_is_valid(ghcb) &&
496 ghcb_rcx_is_valid(ghcb) &&
497 ghcb_rdx_is_valid(ghcb)))
500 regs->ax = ghcb->save.rax;
501 regs->bx = ghcb->save.rbx;
502 regs->cx = ghcb->save.rcx;
503 regs->dx = ghcb->save.rdx;
508 static enum es_result vc_handle_rdtsc(struct ghcb *ghcb,
509 struct es_em_ctxt *ctxt,
510 unsigned long exit_code)
512 bool rdtscp = (exit_code == SVM_EXIT_RDTSCP);
515 ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0);
519 if (!(ghcb_rax_is_valid(ghcb) && ghcb_rdx_is_valid(ghcb) &&
520 (!rdtscp || ghcb_rcx_is_valid(ghcb))))
523 ctxt->regs->ax = ghcb->save.rax;
524 ctxt->regs->dx = ghcb->save.rdx;
526 ctxt->regs->cx = ghcb->save.rcx;