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 static void sev_es_terminate(unsigned int reason)
14 u64 val = GHCB_SEV_TERMINATE;
17 * Tell the hypervisor what went wrong - only reason-set 0 is
18 * currently supported.
20 val |= GHCB_SEV_TERMINATE_REASON(0, reason);
22 /* Request Guest Termination from Hypvervisor */
23 sev_es_wr_ghcb_msr(val);
27 asm volatile("hlt\n" : : : "memory");
30 static bool sev_es_negotiate_protocol(void)
34 /* Do the GHCB protocol version negotiation */
35 sev_es_wr_ghcb_msr(GHCB_SEV_INFO_REQ);
37 val = sev_es_rd_ghcb_msr();
39 if (GHCB_INFO(val) != GHCB_SEV_INFO)
42 if (GHCB_PROTO_MAX(val) < GHCB_PROTO_OUR ||
43 GHCB_PROTO_MIN(val) > GHCB_PROTO_OUR)
49 static void vc_ghcb_invalidate(struct ghcb *ghcb)
51 memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
54 static bool vc_decoding_needed(unsigned long exit_code)
56 /* Exceptions don't require to decode the instruction */
57 return !(exit_code >= SVM_EXIT_EXCP_BASE &&
58 exit_code <= SVM_EXIT_LAST_EXCP);
61 static enum es_result vc_init_em_ctxt(struct es_em_ctxt *ctxt,
63 unsigned long exit_code)
65 enum es_result ret = ES_OK;
67 memset(ctxt, 0, sizeof(*ctxt));
70 if (vc_decoding_needed(exit_code))
71 ret = vc_decode_insn(ctxt);
76 static void vc_finish_insn(struct es_em_ctxt *ctxt)
78 ctxt->regs->ip += ctxt->insn.length;
81 static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
82 struct es_em_ctxt *ctxt,
83 u64 exit_code, u64 exit_info_1,
88 /* Fill in protocol and format specifiers */
89 ghcb->protocol_version = GHCB_PROTOCOL_MAX;
90 ghcb->ghcb_usage = GHCB_DEFAULT_USAGE;
92 ghcb_set_sw_exit_code(ghcb, exit_code);
93 ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
94 ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
96 sev_es_wr_ghcb_msr(__pa(ghcb));
99 if ((ghcb->save.sw_exit_info_1 & 0xffffffff) == 1) {
100 u64 info = ghcb->save.sw_exit_info_2;
103 info = ghcb->save.sw_exit_info_2;
104 v = info & SVM_EVTINJ_VEC_MASK;
106 /* Check if exception information from hypervisor is sane. */
107 if ((info & SVM_EVTINJ_VALID) &&
108 ((v == X86_TRAP_GP) || (v == X86_TRAP_UD)) &&
109 ((info & SVM_EVTINJ_TYPE_MASK) == SVM_EVTINJ_TYPE_EXEPT)) {
111 if (info & SVM_EVTINJ_VALID_ERR)
112 ctxt->fi.error_code = info >> 32;
125 * Boot VC Handler - This is the first VC handler during boot, there is no GHCB
126 * page yet, so it only supports the MSR based communication with the
127 * hypervisor and only the CPUID exit-code.
129 void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code)
131 unsigned int fn = lower_bits(regs->ax, 32);
134 /* Only CPUID is supported via MSR protocol */
135 if (exit_code != SVM_EXIT_CPUID)
138 sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_EAX));
140 val = sev_es_rd_ghcb_msr();
141 if (GHCB_SEV_GHCB_RESP_CODE(val) != GHCB_SEV_CPUID_RESP)
143 regs->ax = val >> 32;
145 sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_EBX));
147 val = sev_es_rd_ghcb_msr();
148 if (GHCB_SEV_GHCB_RESP_CODE(val) != GHCB_SEV_CPUID_RESP)
150 regs->bx = val >> 32;
152 sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_ECX));
154 val = sev_es_rd_ghcb_msr();
155 if (GHCB_SEV_GHCB_RESP_CODE(val) != GHCB_SEV_CPUID_RESP)
157 regs->cx = val >> 32;
159 sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_EDX));
161 val = sev_es_rd_ghcb_msr();
162 if (GHCB_SEV_GHCB_RESP_CODE(val) != GHCB_SEV_CPUID_RESP)
164 regs->dx = val >> 32;
166 /* Skip over the CPUID two-byte opcode */
172 sev_es_wr_ghcb_msr(GHCB_SEV_TERMINATE);
175 /* Shouldn't get here - if we do halt the machine */
177 asm volatile("hlt\n");
180 static enum es_result vc_insn_string_read(struct es_em_ctxt *ctxt,
181 void *src, char *buf,
182 unsigned int data_size,
186 int i, b = backwards ? -1 : 1;
187 enum es_result ret = ES_OK;
189 for (i = 0; i < count; i++) {
190 void *s = src + (i * data_size * b);
191 char *d = buf + (i * data_size);
193 ret = vc_read_mem(ctxt, s, d, data_size);
201 static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt,
202 void *dst, char *buf,
203 unsigned int data_size,
207 int i, s = backwards ? -1 : 1;
208 enum es_result ret = ES_OK;
210 for (i = 0; i < count; i++) {
211 void *d = dst + (i * data_size * s);
212 char *b = buf + (i * data_size);
214 ret = vc_write_mem(ctxt, d, b, data_size);
222 #define IOIO_TYPE_STR BIT(2)
223 #define IOIO_TYPE_IN 1
224 #define IOIO_TYPE_INS (IOIO_TYPE_IN | IOIO_TYPE_STR)
225 #define IOIO_TYPE_OUT 0
226 #define IOIO_TYPE_OUTS (IOIO_TYPE_OUT | IOIO_TYPE_STR)
228 #define IOIO_REP BIT(3)
230 #define IOIO_ADDR_64 BIT(9)
231 #define IOIO_ADDR_32 BIT(8)
232 #define IOIO_ADDR_16 BIT(7)
234 #define IOIO_DATA_32 BIT(6)
235 #define IOIO_DATA_16 BIT(5)
236 #define IOIO_DATA_8 BIT(4)
238 #define IOIO_SEG_ES (0 << 10)
239 #define IOIO_SEG_DS (3 << 10)
241 static enum es_result vc_ioio_exitinfo(struct es_em_ctxt *ctxt, u64 *exitinfo)
243 struct insn *insn = &ctxt->insn;
246 switch (insn->opcode.bytes[0]) {
250 *exitinfo |= IOIO_TYPE_INS;
251 *exitinfo |= IOIO_SEG_ES;
252 *exitinfo |= (ctxt->regs->dx & 0xffff) << 16;
258 *exitinfo |= IOIO_TYPE_OUTS;
259 *exitinfo |= IOIO_SEG_DS;
260 *exitinfo |= (ctxt->regs->dx & 0xffff) << 16;
263 /* IN immediate opcodes */
266 *exitinfo |= IOIO_TYPE_IN;
267 *exitinfo |= (u64)insn->immediate.value << 16;
270 /* OUT immediate opcodes */
273 *exitinfo |= IOIO_TYPE_OUT;
274 *exitinfo |= (u64)insn->immediate.value << 16;
277 /* IN register opcodes */
280 *exitinfo |= IOIO_TYPE_IN;
281 *exitinfo |= (ctxt->regs->dx & 0xffff) << 16;
284 /* OUT register opcodes */
287 *exitinfo |= IOIO_TYPE_OUT;
288 *exitinfo |= (ctxt->regs->dx & 0xffff) << 16;
292 return ES_DECODE_FAILED;
295 switch (insn->opcode.bytes[0]) {
302 /* Single byte opcodes */
303 *exitinfo |= IOIO_DATA_8;
306 /* Length determined by instruction parsing */
307 *exitinfo |= (insn->opnd_bytes == 2) ? IOIO_DATA_16
310 switch (insn->addr_bytes) {
312 *exitinfo |= IOIO_ADDR_16;
315 *exitinfo |= IOIO_ADDR_32;
318 *exitinfo |= IOIO_ADDR_64;
322 if (insn_has_rep_prefix(insn))
323 *exitinfo |= IOIO_REP;
328 static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
330 struct pt_regs *regs = ctxt->regs;
331 u64 exit_info_1, exit_info_2;
334 ret = vc_ioio_exitinfo(ctxt, &exit_info_1);
338 if (exit_info_1 & IOIO_TYPE_STR) {
342 bool df = ((regs->flags & X86_EFLAGS_DF) == X86_EFLAGS_DF);
343 unsigned int io_bytes, exit_bytes;
344 unsigned int ghcb_count, op_count;
345 unsigned long es_base;
349 * For the string variants with rep prefix the amount of in/out
350 * operations per #VC exception is limited so that the kernel
351 * has a chance to take interrupts and re-schedule while the
352 * instruction is emulated.
354 io_bytes = (exit_info_1 >> 4) & 0x7;
355 ghcb_count = sizeof(ghcb->shared_buffer) / io_bytes;
357 op_count = (exit_info_1 & IOIO_REP) ? regs->cx : 1;
358 exit_info_2 = min(op_count, ghcb_count);
359 exit_bytes = exit_info_2 * io_bytes;
361 es_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_ES);
363 /* Read bytes of OUTS into the shared buffer */
364 if (!(exit_info_1 & IOIO_TYPE_IN)) {
365 ret = vc_insn_string_read(ctxt,
366 (void *)(es_base + regs->si),
367 ghcb->shared_buffer, io_bytes,
374 * Issue an VMGEXIT to the HV to consume the bytes from the
375 * shared buffer or to have it write them into the shared buffer
376 * depending on the instruction: OUTS or INS.
378 sw_scratch = __pa(ghcb) + offsetof(struct ghcb, shared_buffer);
379 ghcb_set_sw_scratch(ghcb, sw_scratch);
380 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO,
381 exit_info_1, exit_info_2);
385 /* Read bytes from shared buffer into the guest's destination. */
386 if (exit_info_1 & IOIO_TYPE_IN) {
387 ret = vc_insn_string_write(ctxt,
388 (void *)(es_base + regs->di),
389 ghcb->shared_buffer, io_bytes,
395 regs->di -= exit_bytes;
397 regs->di += exit_bytes;
400 regs->si -= exit_bytes;
402 regs->si += exit_bytes;
405 if (exit_info_1 & IOIO_REP)
406 regs->cx -= exit_info_2;
408 ret = regs->cx ? ES_RETRY : ES_OK;
412 /* IN/OUT into/from rAX */
414 int bits = (exit_info_1 & 0x70) >> 1;
417 if (!(exit_info_1 & IOIO_TYPE_IN))
418 rax = lower_bits(regs->ax, bits);
420 ghcb_set_rax(ghcb, rax);
422 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO, exit_info_1, 0);
426 if (exit_info_1 & IOIO_TYPE_IN) {
427 if (!ghcb_rax_is_valid(ghcb))
429 regs->ax = lower_bits(ghcb->save.rax, bits);
436 static enum es_result vc_handle_cpuid(struct ghcb *ghcb,
437 struct es_em_ctxt *ctxt)
439 struct pt_regs *regs = ctxt->regs;
440 u32 cr4 = native_read_cr4();
443 ghcb_set_rax(ghcb, regs->ax);
444 ghcb_set_rcx(ghcb, regs->cx);
446 if (cr4 & X86_CR4_OSXSAVE)
447 /* Safe to read xcr0 */
448 ghcb_set_xcr0(ghcb, xgetbv(XCR_XFEATURE_ENABLED_MASK));
450 /* xgetbv will cause #GP - use reset value for xcr0 */
451 ghcb_set_xcr0(ghcb, 1);
453 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_CPUID, 0, 0);
457 if (!(ghcb_rax_is_valid(ghcb) &&
458 ghcb_rbx_is_valid(ghcb) &&
459 ghcb_rcx_is_valid(ghcb) &&
460 ghcb_rdx_is_valid(ghcb)))
463 regs->ax = ghcb->save.rax;
464 regs->bx = ghcb->save.rbx;
465 regs->cx = ghcb->save.rcx;
466 regs->dx = ghcb->save.rdx;