1 /* 2 * QEMU SEV support 3 * 4 * Copyright Advanced Micro Devices 2016-2018 5 * 6 * Author: 7 * Brijesh Singh <brijesh.singh@amd.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 * 12 */ 13 14 #include "qemu/osdep.h" 15 #include "qapi/error.h" 16 #include "qom/object_interfaces.h" 17 #include "qemu/base64.h" 18 #include "sysemu/kvm.h" 19 #include "sev_i386.h" 20 #include "sysemu/sysemu.h" 21 22 #define DEFAULT_GUEST_POLICY 0x1 /* disable debug */ 23 #define DEFAULT_SEV_DEVICE "/dev/sev" 24 25 static void 26 qsev_guest_finalize(Object *obj) 27 { 28 } 29 30 static char * 31 qsev_guest_get_session_file(Object *obj, Error **errp) 32 { 33 QSevGuestInfo *s = QSEV_GUEST_INFO(obj); 34 35 return s->session_file ? g_strdup(s->session_file) : NULL; 36 } 37 38 static void 39 qsev_guest_set_session_file(Object *obj, const char *value, Error **errp) 40 { 41 QSevGuestInfo *s = QSEV_GUEST_INFO(obj); 42 43 s->session_file = g_strdup(value); 44 } 45 46 static char * 47 qsev_guest_get_dh_cert_file(Object *obj, Error **errp) 48 { 49 QSevGuestInfo *s = QSEV_GUEST_INFO(obj); 50 51 return g_strdup(s->dh_cert_file); 52 } 53 54 static void 55 qsev_guest_set_dh_cert_file(Object *obj, const char *value, Error **errp) 56 { 57 QSevGuestInfo *s = QSEV_GUEST_INFO(obj); 58 59 s->dh_cert_file = g_strdup(value); 60 } 61 62 static char * 63 qsev_guest_get_sev_device(Object *obj, Error **errp) 64 { 65 QSevGuestInfo *sev = QSEV_GUEST_INFO(obj); 66 67 return g_strdup(sev->sev_device); 68 } 69 70 static void 71 qsev_guest_set_sev_device(Object *obj, const char *value, Error **errp) 72 { 73 QSevGuestInfo *sev = QSEV_GUEST_INFO(obj); 74 75 sev->sev_device = g_strdup(value); 76 } 77 78 static void 79 qsev_guest_class_init(ObjectClass *oc, void *data) 80 { 81 object_class_property_add_str(oc, "sev-device", 82 qsev_guest_get_sev_device, 83 qsev_guest_set_sev_device, 84 NULL); 85 object_class_property_set_description(oc, "sev-device", 86 "SEV device to use", NULL); 87 object_class_property_add_str(oc, "dh-cert-file", 88 qsev_guest_get_dh_cert_file, 89 qsev_guest_set_dh_cert_file, 90 NULL); 91 object_class_property_set_description(oc, "dh-cert-file", 92 "guest owners DH certificate (encoded with base64)", NULL); 93 object_class_property_add_str(oc, "session-file", 94 qsev_guest_get_session_file, 95 qsev_guest_set_session_file, 96 NULL); 97 object_class_property_set_description(oc, "session-file", 98 "guest owners session parameters (encoded with base64)", NULL); 99 } 100 101 static void 102 qsev_guest_set_handle(Object *obj, Visitor *v, const char *name, 103 void *opaque, Error **errp) 104 { 105 QSevGuestInfo *sev = QSEV_GUEST_INFO(obj); 106 uint32_t value; 107 108 visit_type_uint32(v, name, &value, errp); 109 sev->handle = value; 110 } 111 112 static void 113 qsev_guest_set_policy(Object *obj, Visitor *v, const char *name, 114 void *opaque, Error **errp) 115 { 116 QSevGuestInfo *sev = QSEV_GUEST_INFO(obj); 117 uint32_t value; 118 119 visit_type_uint32(v, name, &value, errp); 120 sev->policy = value; 121 } 122 123 static void 124 qsev_guest_set_cbitpos(Object *obj, Visitor *v, const char *name, 125 void *opaque, Error **errp) 126 { 127 QSevGuestInfo *sev = QSEV_GUEST_INFO(obj); 128 uint32_t value; 129 130 visit_type_uint32(v, name, &value, errp); 131 sev->cbitpos = value; 132 } 133 134 static void 135 qsev_guest_set_reduced_phys_bits(Object *obj, Visitor *v, const char *name, 136 void *opaque, Error **errp) 137 { 138 QSevGuestInfo *sev = QSEV_GUEST_INFO(obj); 139 uint32_t value; 140 141 visit_type_uint32(v, name, &value, errp); 142 sev->reduced_phys_bits = value; 143 } 144 145 static void 146 qsev_guest_get_policy(Object *obj, Visitor *v, const char *name, 147 void *opaque, Error **errp) 148 { 149 uint32_t value; 150 QSevGuestInfo *sev = QSEV_GUEST_INFO(obj); 151 152 value = sev->policy; 153 visit_type_uint32(v, name, &value, errp); 154 } 155 156 static void 157 qsev_guest_get_handle(Object *obj, Visitor *v, const char *name, 158 void *opaque, Error **errp) 159 { 160 uint32_t value; 161 QSevGuestInfo *sev = QSEV_GUEST_INFO(obj); 162 163 value = sev->handle; 164 visit_type_uint32(v, name, &value, errp); 165 } 166 167 static void 168 qsev_guest_get_cbitpos(Object *obj, Visitor *v, const char *name, 169 void *opaque, Error **errp) 170 { 171 uint32_t value; 172 QSevGuestInfo *sev = QSEV_GUEST_INFO(obj); 173 174 value = sev->cbitpos; 175 visit_type_uint32(v, name, &value, errp); 176 } 177 178 static void 179 qsev_guest_get_reduced_phys_bits(Object *obj, Visitor *v, const char *name, 180 void *opaque, Error **errp) 181 { 182 uint32_t value; 183 QSevGuestInfo *sev = QSEV_GUEST_INFO(obj); 184 185 value = sev->reduced_phys_bits; 186 visit_type_uint32(v, name, &value, errp); 187 } 188 189 static void 190 qsev_guest_init(Object *obj) 191 { 192 QSevGuestInfo *sev = QSEV_GUEST_INFO(obj); 193 194 sev->sev_device = g_strdup(DEFAULT_SEV_DEVICE); 195 sev->policy = DEFAULT_GUEST_POLICY; 196 object_property_add(obj, "policy", "uint32", qsev_guest_get_policy, 197 qsev_guest_set_policy, NULL, NULL, NULL); 198 object_property_add(obj, "handle", "uint32", qsev_guest_get_handle, 199 qsev_guest_set_handle, NULL, NULL, NULL); 200 object_property_add(obj, "cbitpos", "uint32", qsev_guest_get_cbitpos, 201 qsev_guest_set_cbitpos, NULL, NULL, NULL); 202 object_property_add(obj, "reduced-phys-bits", "uint32", 203 qsev_guest_get_reduced_phys_bits, 204 qsev_guest_set_reduced_phys_bits, NULL, NULL, NULL); 205 } 206 207 /* sev guest info */ 208 static const TypeInfo qsev_guest_info = { 209 .parent = TYPE_OBJECT, 210 .name = TYPE_QSEV_GUEST_INFO, 211 .instance_size = sizeof(QSevGuestInfo), 212 .instance_finalize = qsev_guest_finalize, 213 .class_size = sizeof(QSevGuestInfoClass), 214 .class_init = qsev_guest_class_init, 215 .instance_init = qsev_guest_init, 216 .interfaces = (InterfaceInfo[]) { 217 { TYPE_USER_CREATABLE }, 218 { } 219 } 220 }; 221 222 static void 223 sev_register_types(void) 224 { 225 type_register_static(&qsev_guest_info); 226 } 227 228 type_init(sev_register_types); 229