master
c 144 lines 3.39 KB
Raw
1 /*
2 * PEF (Protected Execution Facility) for POWER support
3 *
4 * Copyright Red Hat.
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 *
9 */
10
11 #include "qemu/osdep.h"
12
13 #include "qapi/error.h"
14 #include "qom/object_interfaces.h"
15 #include "system/kvm.h"
16 #include "migration/blocker.h"
17 #include "system/confidential-guest-support.h"
18
19 #define TYPE_PEF_GUEST "pef-guest"
20 OBJECT_DECLARE_SIMPLE_TYPE(PefGuest, PEF_GUEST)
21
22 typedef struct PefGuestClass PefGuestClass;
23
24 struct PefGuestClass {
25 ConfidentialGuestSupportClass parent_class;
26 };
27
28 /**
29 * PefGuest:
30 *
31 * The PefGuest object is used for creating and managing a PEF
32 * guest.
33 *
34 * # $QEMU \
35 * -object pef-guest,id=pef0 \
36 * -machine ...,confidential-guest-support=pef0
37 */
38 struct PefGuest {
39 ConfidentialGuestSupport parent_obj;
40 };
41
42 static int kvmppc_svm_init(ConfidentialGuestSupport *cgs, Error **errp)
43 {
44 #ifdef CONFIG_KVM
45 static Error *pef_mig_blocker;
46
47 if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_SECURE_GUEST)) {
48 error_setg(errp,
49 "KVM implementation does not support Secure VMs (is an ultravisor running?)");
50 return -1;
51 } else {
52 int ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_SECURE_GUEST, 0, 1);
53
54 if (ret < 0) {
55 error_setg(errp,
56 "Error enabling PEF with KVM");
57 return -1;
58 }
59 }
60
61 /* add migration blocker */
62 error_setg(&pef_mig_blocker, "PEF: Migration is not implemented");
63 /* NB: This can fail if --only-migratable is used */
64 migrate_add_blocker(&pef_mig_blocker, &error_fatal);
65
66 cgs->ready = true;
67
68 return 0;
69 #else
70 g_assert_not_reached();
71 #endif
72 }
73
74 /*
75 * Don't set error if KVM_PPC_SVM_OFF ioctl is invoked on kernels
76 * that don't support this ioctl.
77 */
78 static int kvmppc_svm_off(Error **errp)
79 {
80 #ifdef CONFIG_KVM
81 int rc;
82
83 rc = kvm_vm_ioctl(KVM_STATE(current_accel()), KVM_PPC_SVM_OFF);
84 if (rc && rc != -ENOTTY) {
85 error_setg_errno(errp, -rc, "KVM_PPC_SVM_OFF ioctl failed");
86 return rc;
87 }
88 return 0;
89 #else
90 g_assert_not_reached();
91 #endif
92 }
93
94 static int pef_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
95 {
96 if (!object_dynamic_cast(OBJECT(cgs), TYPE_PEF_GUEST)) {
97 return 0;
98 }
99
100 if (!kvm_enabled()) {
101 error_setg(errp, "PEF requires KVM");
102 return -1;
103 }
104
105 return kvmppc_svm_init(cgs, errp);
106 }
107
108 static int pef_kvm_reset(ConfidentialGuestSupport *cgs, Error **errp)
109 {
110 if (!object_dynamic_cast(OBJECT(cgs), TYPE_PEF_GUEST)) {
111 return 0;
112 }
113
114 /*
115 * If we don't have KVM we should never have been able to
116 * initialize PEF, so we should never get this far
117 */
118 assert(kvm_enabled());
119
120 return kvmppc_svm_off(errp);
121 }
122
123 OBJECT_DEFINE_TYPE_WITH_INTERFACES(PefGuest,
124 pef_guest,
125 PEF_GUEST,
126 CONFIDENTIAL_GUEST_SUPPORT,
127 { TYPE_USER_CREATABLE },
128 { NULL })
129
130 static void pef_guest_class_init(ObjectClass *oc, const void *data)
131 {
132 ConfidentialGuestSupportClass *klass = CONFIDENTIAL_GUEST_SUPPORT_CLASS(oc);
133
134 klass->kvm_init = pef_kvm_init;
135 klass->kvm_reset = pef_kvm_reset;
136 }
137
138 static void pef_guest_init(Object *obj)
139 {
140 }
141
142 static void pef_guest_finalize(Object *obj)
143 {
144 }