sev-common.h (5997B)
1/* SPDX-License-Identifier: GPL-2.0 */ 2/* 3 * AMD SEV header common between the guest and the hypervisor. 4 * 5 * Author: Brijesh Singh <brijesh.singh@amd.com> 6 */ 7 8#ifndef __ASM_X86_SEV_COMMON_H 9#define __ASM_X86_SEV_COMMON_H 10 11#define GHCB_MSR_INFO_POS 0 12#define GHCB_DATA_LOW 12 13#define GHCB_MSR_INFO_MASK (BIT_ULL(GHCB_DATA_LOW) - 1) 14 15#define GHCB_DATA(v) \ 16 (((unsigned long)(v) & ~GHCB_MSR_INFO_MASK) >> GHCB_DATA_LOW) 17 18/* SEV Information Request/Response */ 19#define GHCB_MSR_SEV_INFO_RESP 0x001 20#define GHCB_MSR_SEV_INFO_REQ 0x002 21 22#define GHCB_MSR_SEV_INFO(_max, _min, _cbit) \ 23 /* GHCBData[63:48] */ \ 24 ((((_max) & 0xffff) << 48) | \ 25 /* GHCBData[47:32] */ \ 26 (((_min) & 0xffff) << 32) | \ 27 /* GHCBData[31:24] */ \ 28 (((_cbit) & 0xff) << 24) | \ 29 GHCB_MSR_SEV_INFO_RESP) 30 31#define GHCB_MSR_INFO(v) ((v) & 0xfffUL) 32#define GHCB_MSR_PROTO_MAX(v) (((v) >> 48) & 0xffff) 33#define GHCB_MSR_PROTO_MIN(v) (((v) >> 32) & 0xffff) 34 35/* CPUID Request/Response */ 36#define GHCB_MSR_CPUID_REQ 0x004 37#define GHCB_MSR_CPUID_RESP 0x005 38#define GHCB_MSR_CPUID_FUNC_POS 32 39#define GHCB_MSR_CPUID_FUNC_MASK 0xffffffff 40#define GHCB_MSR_CPUID_VALUE_POS 32 41#define GHCB_MSR_CPUID_VALUE_MASK 0xffffffff 42#define GHCB_MSR_CPUID_REG_POS 30 43#define GHCB_MSR_CPUID_REG_MASK 0x3 44#define GHCB_CPUID_REQ_EAX 0 45#define GHCB_CPUID_REQ_EBX 1 46#define GHCB_CPUID_REQ_ECX 2 47#define GHCB_CPUID_REQ_EDX 3 48#define GHCB_CPUID_REQ(fn, reg) \ 49 /* GHCBData[11:0] */ \ 50 (GHCB_MSR_CPUID_REQ | \ 51 /* GHCBData[31:12] */ \ 52 (((unsigned long)(reg) & 0x3) << 30) | \ 53 /* GHCBData[63:32] */ \ 54 (((unsigned long)fn) << 32)) 55 56/* AP Reset Hold */ 57#define GHCB_MSR_AP_RESET_HOLD_REQ 0x006 58#define GHCB_MSR_AP_RESET_HOLD_RESP 0x007 59#define GHCB_MSR_AP_RESET_HOLD_RESULT_POS 12 60#define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK GENMASK_ULL(51, 0) 61 62/* Preferred GHCB GPA Request */ 63#define GHCB_MSR_PREF_GPA_REQ 0x010 64#define GHCB_MSR_GPA_VALUE_POS 12 65#define GHCB_MSR_GPA_VALUE_MASK GENMASK_ULL(51, 0) 66 67#define GHCB_MSR_PREF_GPA_RESP 0x011 68#define GHCB_MSR_PREF_GPA_NONE 0xfffffffffffff 69 70/* GHCB GPA Register */ 71#define GHCB_MSR_REG_GPA_REQ 0x012 72#define GHCB_MSR_REG_GPA_REQ_VAL(v) \ 73 /* GHCBData[63:12] */ \ 74 (((u64)((v) & GENMASK_ULL(51, 0)) << 12) | \ 75 /* GHCBData[11:0] */ \ 76 GHCB_MSR_REG_GPA_REQ) 77 78#define GHCB_MSR_REG_GPA_RESP 0x013 79#define GHCB_MSR_REG_GPA_RESP_VAL(v) \ 80 /* GHCBData[63:12] */ \ 81 (((u64)(v) & GENMASK_ULL(63, 12)) >> 12) 82 83/* 84 * SNP Page State Change Operation 85 * 86 * GHCBData[55:52] - Page operation: 87 * 0x0001 Page assignment, Private 88 * 0x0002 Page assignment, Shared 89 */ 90enum psc_op { 91 SNP_PAGE_STATE_PRIVATE = 1, 92 SNP_PAGE_STATE_SHARED, 93}; 94 95#define GHCB_MSR_PSC_REQ 0x014 96#define GHCB_MSR_PSC_GFN_POS 12 97#define GHCB_MSR_PSC_GFN_MASK GENMASK_ULL(39, 0) 98#define GHCB_MSR_PSC_OP_POS 52 99#define GHCB_MSR_PSC_OP_MASK 0xf 100#define GHCB_MSR_PSC_REQ_GFN(gfn, op) \ 101 /* GHCBData[55:52] */ \ 102 (((u64)((op) & 0xf) << 52) | \ 103 /* GHCBData[51:12] */ \ 104 ((u64)((gfn) & GENMASK_ULL(39, 0)) << 12) | \ 105 /* GHCBData[11:0] */ \ 106 GHCB_MSR_PSC_REQ) 107 108#define GHCB_MSR_PSC_RESP 0x015 109#define GHCB_MSR_PSC_ERROR_POS 32 110#define GHCB_MSR_PSC_ERROR_MASK GENMASK_ULL(31, 0) 111#define GHCB_MSR_PSC_ERROR GENMASK_ULL(31, 0) 112#define GHCB_MSR_PSC_RSVD_POS 12 113#define GHCB_MSR_PSC_RSVD_MASK GENMASK_ULL(19, 0) 114#define GHCB_MSR_PSC_RESP_VAL(val) \ 115 /* GHCBData[63:32] */ \ 116 (((u64)(val) & GENMASK_ULL(63, 32)) >> 32) 117 118/* GHCB Hypervisor Feature Request/Response */ 119#define GHCB_MSR_HV_FT_REQ 0x080 120#define GHCB_MSR_HV_FT_RESP 0x081 121#define GHCB_MSR_HV_FT_POS 12 122#define GHCB_MSR_HV_FT_MASK GENMASK_ULL(51, 0) 123#define GHCB_MSR_HV_FT_RESP_VAL(v) \ 124 /* GHCBData[63:12] */ \ 125 (((u64)(v) & GENMASK_ULL(63, 12)) >> 12) 126 127#define GHCB_HV_FT_SNP BIT_ULL(0) 128#define GHCB_HV_FT_SNP_AP_CREATION BIT_ULL(1) 129 130/* SNP Page State Change NAE event */ 131#define VMGEXIT_PSC_MAX_ENTRY 253 132 133/* The page state change hdr structure in not valid */ 134#define PSC_INVALID_HDR 1 135/* The hdr.cur_entry or hdr.end_entry is not valid */ 136#define PSC_INVALID_ENTRY 2 137/* Page state change encountered undefined error */ 138#define PSC_UNDEF_ERR 3 139 140struct psc_hdr { 141 u16 cur_entry; 142 u16 end_entry; 143 u32 reserved; 144} __packed; 145 146struct psc_entry { 147 u64 cur_page : 12, 148 gfn : 40, 149 operation : 4, 150 pagesize : 1, 151 reserved : 7; 152} __packed; 153 154struct snp_psc_desc { 155 struct psc_hdr hdr; 156 struct psc_entry entries[VMGEXIT_PSC_MAX_ENTRY]; 157} __packed; 158 159/* Guest message request error code */ 160#define SNP_GUEST_REQ_INVALID_LEN BIT_ULL(32) 161 162#define GHCB_MSR_TERM_REQ 0x100 163#define GHCB_MSR_TERM_REASON_SET_POS 12 164#define GHCB_MSR_TERM_REASON_SET_MASK 0xf 165#define GHCB_MSR_TERM_REASON_POS 16 166#define GHCB_MSR_TERM_REASON_MASK 0xff 167 168#define GHCB_SEV_TERM_REASON(reason_set, reason_val) \ 169 /* GHCBData[15:12] */ \ 170 (((((u64)reason_set) & 0xf) << 12) | \ 171 /* GHCBData[23:16] */ \ 172 ((((u64)reason_val) & 0xff) << 16)) 173 174/* Error codes from reason set 0 */ 175#define SEV_TERM_SET_GEN 0 176#define GHCB_SEV_ES_GEN_REQ 0 177#define GHCB_SEV_ES_PROT_UNSUPPORTED 1 178#define GHCB_SNP_UNSUPPORTED 2 179 180/* Linux-specific reason codes (used with reason set 1) */ 181#define SEV_TERM_SET_LINUX 1 182#define GHCB_TERM_REGISTER 0 /* GHCB GPA registration failure */ 183#define GHCB_TERM_PSC 1 /* Page State Change failure */ 184#define GHCB_TERM_PVALIDATE 2 /* Pvalidate failure */ 185#define GHCB_TERM_NOT_VMPL0 3 /* SNP guest is not running at VMPL-0 */ 186#define GHCB_TERM_CPUID 4 /* CPUID-validation failure */ 187#define GHCB_TERM_CPUID_HV 5 /* CPUID failure during hypervisor fallback */ 188 189#define GHCB_RESP_CODE(v) ((v) & GHCB_MSR_INFO_MASK) 190 191/* 192 * Error codes related to GHCB input that can be communicated back to the guest 193 * by setting the lower 32-bits of the GHCB SW_EXITINFO1 field to 2. 194 */ 195#define GHCB_ERR_NOT_REGISTERED 1 196#define GHCB_ERR_INVALID_USAGE 2 197#define GHCB_ERR_INVALID_SCRATCH_AREA 3 198#define GHCB_ERR_MISSING_INPUT 4 199#define GHCB_ERR_INVALID_INPUT 5 200#define GHCB_ERR_INVALID_EVENT 6 201 202#endif