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