2 * Protected Virtualization functions
4 * Copyright IBM Corp. 2020
6 * Janosch Frank <frankja@linux.ibm.com>
8 * This work is licensed under the terms of the GNU GPL, version 2 or (at
9 * your option) any later version. See the COPYING file in the top-level
12 #include "qemu/osdep.h"
14 #include <linux/kvm.h>
16 #include "qapi/error.h"
17 #include "qemu/error-report.h"
18 #include "sysemu/kvm.h"
19 #include "sysemu/cpus.h"
20 #include "qom/object_interfaces.h"
21 #include "exec/confidential-guest-support.h"
22 #include "hw/s390x/ipl.h"
23 #include "hw/s390x/pv.h"
24 #include "hw/s390x/sclp.h"
25 #include "target/s390x/kvm/kvm_s390x.h"
27 static bool info_valid
;
28 static struct kvm_s390_pv_info_vm info_vm
;
29 static struct kvm_s390_pv_info_dump info_dump
;
31 static int __s390_pv_cmd(uint32_t cmd
, const char *cmdname
, void *data
)
33 struct kvm_pv_cmd pv_cmd
= {
35 .data
= (uint64_t)data
,
40 rc
= kvm_vm_ioctl(kvm_state
, KVM_S390_PV_COMMAND
, &pv_cmd
);
41 } while (rc
== -EINTR
);
44 error_report("KVM PV command %d (%s) failed: header rc %x rrc %x "
45 "IOCTL rc: %d", cmd
, cmdname
, pv_cmd
.rc
, pv_cmd
.rrc
,
52 * This macro lets us pass the command as a string to the function so
53 * we can print it on an error.
55 #define s390_pv_cmd(cmd, data) __s390_pv_cmd(cmd, #cmd, data)
56 #define s390_pv_cmd_exit(cmd, data) \
60 rc = __s390_pv_cmd(cmd, #cmd, data);\
66 int s390_pv_query_info(void)
68 struct kvm_s390_pv_info info
= {
69 .header
.id
= KVM_PV_INFO_VM
,
70 .header
.len_max
= sizeof(info
.header
) + sizeof(info
.vm
),
74 /* Info API's first user is dump so they are bundled */
75 if (!kvm_s390_get_protected_dump()) {
79 rc
= s390_pv_cmd(KVM_PV_INFO
, &info
);
81 error_report("KVM PV INFO cmd %x failed: %s",
82 info
.header
.id
, strerror(-rc
));
85 memcpy(&info_vm
, &info
.vm
, sizeof(info
.vm
));
87 info
.header
.id
= KVM_PV_INFO_DUMP
;
88 info
.header
.len_max
= sizeof(info
.header
) + sizeof(info
.dump
);
89 rc
= s390_pv_cmd(KVM_PV_INFO
, &info
);
91 error_report("KVM PV INFO cmd %x failed: %s",
92 info
.header
.id
, strerror(-rc
));
96 memcpy(&info_dump
, &info
.dump
, sizeof(info
.dump
));
102 int s390_pv_vm_enable(void)
104 return s390_pv_cmd(KVM_PV_ENABLE
, NULL
);
107 void s390_pv_vm_disable(void)
109 s390_pv_cmd_exit(KVM_PV_DISABLE
, NULL
);
112 static void *s390_pv_do_unprot_async_fn(void *p
)
114 s390_pv_cmd_exit(KVM_PV_ASYNC_CLEANUP_PERFORM
, NULL
);
118 bool s390_pv_vm_try_disable_async(void)
121 * t is only needed to create the thread; once qemu_thread_create
122 * returns, it can safely be discarded.
126 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_PROTECTED_ASYNC_DISABLE
)) {
129 if (s390_pv_cmd(KVM_PV_ASYNC_CLEANUP_PREPARE
, NULL
) != 0) {
133 qemu_thread_create(&t
, "async_cleanup", s390_pv_do_unprot_async_fn
, NULL
,
134 QEMU_THREAD_DETACHED
);
139 int s390_pv_set_sec_parms(uint64_t origin
, uint64_t length
)
141 struct kvm_s390_pv_sec_parm args
= {
146 return s390_pv_cmd(KVM_PV_SET_SEC_PARMS
, &args
);
150 * Called for each component in the SE type IPL parameter block 0.
152 int s390_pv_unpack(uint64_t addr
, uint64_t size
, uint64_t tweak
)
154 struct kvm_s390_pv_unp args
= {
160 return s390_pv_cmd(KVM_PV_UNPACK
, &args
);
163 void s390_pv_prep_reset(void)
165 s390_pv_cmd_exit(KVM_PV_PREP_RESET
, NULL
);
168 int s390_pv_verify(void)
170 return s390_pv_cmd(KVM_PV_VERIFY
, NULL
);
173 void s390_pv_unshare(void)
175 s390_pv_cmd_exit(KVM_PV_UNSHARE_ALL
, NULL
);
178 void s390_pv_inject_reset_error(CPUState
*cs
)
180 int r1
= (cs
->kvm_run
->s390_sieic
.ipa
& 0x00f0) >> 4;
181 CPUS390XState
*env
= &S390_CPU(cs
)->env
;
183 /* Report that we are unable to enter protected mode */
184 env
->regs
[r1
+ 1] = DIAG_308_RC_INVAL_FOR_PV
;
187 uint64_t kvm_s390_pv_dmp_get_size_cpu(void)
189 return info_dump
.dump_cpu_buffer_len
;
192 uint64_t kvm_s390_pv_dmp_get_size_completion_data(void)
194 return info_dump
.dump_config_finalize_len
;
197 uint64_t kvm_s390_pv_dmp_get_size_mem_state(void)
199 return info_dump
.dump_config_mem_buffer_per_1m
;
202 bool kvm_s390_pv_info_basic_valid(void)
207 static int s390_pv_dump_cmd(uint64_t subcmd
, uint64_t uaddr
, uint64_t gaddr
,
210 struct kvm_s390_pv_dmp dmp
= {
218 ret
= s390_pv_cmd(KVM_PV_DUMP
, (void *)&dmp
);
220 error_report("KVM DUMP command %ld failed", subcmd
);
225 int kvm_s390_dump_cpu(S390CPU
*cpu
, void *buff
)
227 struct kvm_s390_pv_dmp dmp
= {
228 .subcmd
= KVM_PV_DUMP_CPU
,
229 .buff_addr
= (uint64_t)buff
,
231 .buff_len
= info_dump
.dump_cpu_buffer_len
,
233 struct kvm_pv_cmd pv
= {
235 .data
= (uint64_t)&dmp
,
238 return kvm_vcpu_ioctl(CPU(cpu
), KVM_S390_PV_CPU_COMMAND
, &pv
);
241 int kvm_s390_dump_init(void)
243 return s390_pv_dump_cmd(KVM_PV_DUMP_INIT
, 0, 0, 0);
246 int kvm_s390_dump_mem_state(uint64_t gaddr
, size_t len
, void *dest
)
248 return s390_pv_dump_cmd(KVM_PV_DUMP_CONFIG_STOR_STATE
, (uint64_t)dest
,
252 int kvm_s390_dump_completion_data(void *buff
)
254 return s390_pv_dump_cmd(KVM_PV_DUMP_COMPLETE
, (uint64_t)buff
, 0,
255 info_dump
.dump_config_finalize_len
);
258 #define TYPE_S390_PV_GUEST "s390-pv-guest"
259 OBJECT_DECLARE_SIMPLE_TYPE(S390PVGuest
, S390_PV_GUEST
)
264 * The S390PVGuest object is basically a dummy used to tell the
265 * confidential guest support system to use s390's PV mechanism.
268 * -object s390-pv-guest,id=pv0 \
269 * -machine ...,confidential-guest-support=pv0
272 ConfidentialGuestSupport parent_obj
;
275 typedef struct S390PVGuestClass S390PVGuestClass
;
277 struct S390PVGuestClass
{
278 ConfidentialGuestSupportClass parent_class
;
282 * If protected virtualization is enabled, the amount of data that the
283 * Read SCP Info Service Call can use is limited to one page. The
284 * available space also depends on the Extended-Length SCCB (ELS)
285 * feature which can take more buffer space to store feature
286 * information. This impacts the maximum number of CPUs supported in
289 static uint32_t s390_pv_get_max_cpus(void)
291 int offset_cpu
= s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB
) ?
292 offsetof(ReadInfo
, entries
) : SCLP_READ_SCP_INFO_FIXED_CPU_OFFSET
;
294 return (TARGET_PAGE_SIZE
- offset_cpu
) / sizeof(CPUEntry
);
297 static bool s390_pv_check_cpus(Error
**errp
)
299 MachineState
*ms
= MACHINE(qdev_get_machine());
300 uint32_t pv_max_cpus
= s390_pv_get_max_cpus();
302 if (ms
->smp
.max_cpus
> pv_max_cpus
) {
303 error_setg(errp
, "Protected VMs support a maximum of %d CPUs",
311 static bool s390_pv_guest_check(ConfidentialGuestSupport
*cgs
, Error
**errp
)
313 return s390_pv_check_cpus(errp
);
316 int s390_pv_kvm_init(ConfidentialGuestSupport
*cgs
, Error
**errp
)
318 if (!object_dynamic_cast(OBJECT(cgs
), TYPE_S390_PV_GUEST
)) {
322 if (!s390_has_feat(S390_FEAT_UNPACK
)) {
324 "CPU model does not support Protected Virtualization");
328 if (!s390_pv_guest_check(cgs
, errp
)) {
337 OBJECT_DEFINE_TYPE_WITH_INTERFACES(S390PVGuest
,
340 CONFIDENTIAL_GUEST_SUPPORT
,
341 { TYPE_USER_CREATABLE
},
344 static void s390_pv_guest_class_init(ObjectClass
*oc
, void *data
)
348 static void s390_pv_guest_init(Object
*obj
)
352 static void s390_pv_guest_finalize(Object
*obj
)