1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "misc.h"
4 #include "sev.h"
5
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <asm/insn.h>
9 #include <asm/pgtable_types.h>
10 #include <asm/ptrace.h>
11 #include <asm/sev.h>
12 #include <asm/trapnr.h>
13 #include <asm/trap_pf.h>
14 #include <asm/fpu/xcr.h>
15
16 #define __BOOT_COMPRESSED
17
18 /* Basic instruction decoding support needed */
19 #include "../../lib/inat.c"
20 #include "../../lib/insn.c"
21
22 /*
23 * Copy a version of this function here - insn-eval.c can't be used in
24 * pre-decompression code.
25 */
insn_has_rep_prefix(struct insn * insn)26 bool insn_has_rep_prefix(struct insn *insn)
27 {
28 insn_byte_t p;
29 int i;
30
31 insn_get_prefixes(insn);
32
33 for_each_insn_prefix(insn, i, p) {
34 if (p == 0xf2 || p == 0xf3)
35 return true;
36 }
37
38 return false;
39 }
40
vc_decode_insn(struct es_em_ctxt * ctxt)41 enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
42 {
43 char buffer[MAX_INSN_SIZE];
44 int ret;
45
46 memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
47
48 ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
49 if (ret < 0)
50 return ES_DECODE_FAILED;
51
52 return ES_OK;
53 }
54
55 extern void sev_insn_decode_init(void) __alias(inat_init_tables);
56
57 /*
58 * Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and
59 * doesn't use segments.
60 */
insn_get_seg_base(struct pt_regs * regs,int seg_reg_idx)61 static unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
62 {
63 return 0UL;
64 }
65
vc_write_mem(struct es_em_ctxt * ctxt,void * dst,char * buf,size_t size)66 static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
67 void *dst, char *buf, size_t size)
68 {
69 memcpy(dst, buf, size);
70
71 return ES_OK;
72 }
73
vc_read_mem(struct es_em_ctxt * ctxt,void * src,char * buf,size_t size)74 static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
75 void *src, char *buf, size_t size)
76 {
77 memcpy(buf, src, size);
78
79 return ES_OK;
80 }
81
vc_ioio_check(struct es_em_ctxt * ctxt,u16 port,size_t size)82 static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size)
83 {
84 return ES_OK;
85 }
86
fault_in_kernel_space(unsigned long address)87 static bool fault_in_kernel_space(unsigned long address)
88 {
89 return false;
90 }
91
92 #define sev_printk(fmt, ...)
93
94 #include "../../coco/sev/vc-shared.c"
95
do_boot_stage2_vc(struct pt_regs * regs,unsigned long exit_code)96 void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
97 {
98 struct es_em_ctxt ctxt;
99 enum es_result result;
100
101 if (!boot_ghcb && !early_setup_ghcb())
102 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
103
104 vc_ghcb_invalidate(boot_ghcb);
105 result = vc_init_em_ctxt(&ctxt, regs, exit_code);
106 if (result != ES_OK)
107 goto finish;
108
109 result = vc_check_opcode_bytes(&ctxt, exit_code);
110 if (result != ES_OK)
111 goto finish;
112
113 switch (exit_code) {
114 case SVM_EXIT_RDTSC:
115 case SVM_EXIT_RDTSCP:
116 result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code);
117 break;
118 case SVM_EXIT_IOIO:
119 result = vc_handle_ioio(boot_ghcb, &ctxt);
120 break;
121 case SVM_EXIT_CPUID:
122 result = vc_handle_cpuid(boot_ghcb, &ctxt);
123 break;
124 default:
125 result = ES_UNSUPPORTED;
126 break;
127 }
128
129 finish:
130 if (result == ES_OK)
131 vc_finish_insn(&ctxt);
132 else if (result != ES_RETRY)
133 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
134 }
135