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 "qom/object_interfaces.h"
20 #include "exec/confidential-guest-support.h"
21 #include "hw/s390x/ipl.h"
22 #include "hw/s390x/pv.h"
23 #include "target/s390x/kvm/kvm_s390x.h"
25 static bool info_valid
;
26 static struct kvm_s390_pv_info_vm info_vm
;
27 static struct kvm_s390_pv_info_dump info_dump
;
29 static int __s390_pv_cmd(uint32_t cmd
, const char *cmdname
, void *data
)
31 struct kvm_pv_cmd pv_cmd
= {
33 .data
= (uint64_t)data
,
38 rc
= kvm_vm_ioctl(kvm_state
, KVM_S390_PV_COMMAND
, &pv_cmd
);
39 } while (rc
== -EINTR
);
42 error_report("KVM PV command %d (%s) failed: header rc %x rrc %x "
43 "IOCTL rc: %d", cmd
, cmdname
, pv_cmd
.rc
, pv_cmd
.rrc
,
50 * This macro lets us pass the command as a string to the function so
51 * we can print it on an error.
53 #define s390_pv_cmd(cmd, data) __s390_pv_cmd(cmd, #cmd, data)
54 #define s390_pv_cmd_exit(cmd, data) \
58 rc = __s390_pv_cmd(cmd, #cmd, data);\
64 int s390_pv_query_info(void)
66 struct kvm_s390_pv_info info
= {
67 .header
.id
= KVM_PV_INFO_VM
,
68 .header
.len_max
= sizeof(info
.header
) + sizeof(info
.vm
),
72 /* Info API's first user is dump so they are bundled */
73 if (!kvm_s390_get_protected_dump()) {
77 rc
= s390_pv_cmd(KVM_PV_INFO
, &info
);
79 error_report("KVM PV INFO cmd %x failed: %s",
80 info
.header
.id
, strerror(-rc
));
83 memcpy(&info_vm
, &info
.vm
, sizeof(info
.vm
));
85 info
.header
.id
= KVM_PV_INFO_DUMP
;
86 info
.header
.len_max
= sizeof(info
.header
) + sizeof(info
.dump
);
87 rc
= s390_pv_cmd(KVM_PV_INFO
, &info
);
89 error_report("KVM PV INFO cmd %x failed: %s",
90 info
.header
.id
, strerror(-rc
));
94 memcpy(&info_dump
, &info
.dump
, sizeof(info
.dump
));
100 int s390_pv_vm_enable(void)
102 return s390_pv_cmd(KVM_PV_ENABLE
, NULL
);
105 void s390_pv_vm_disable(void)
107 s390_pv_cmd_exit(KVM_PV_DISABLE
, NULL
);
110 int s390_pv_set_sec_parms(uint64_t origin
, uint64_t length
)
112 struct kvm_s390_pv_sec_parm args
= {
117 return s390_pv_cmd(KVM_PV_SET_SEC_PARMS
, &args
);
121 * Called for each component in the SE type IPL parameter block 0.
123 int s390_pv_unpack(uint64_t addr
, uint64_t size
, uint64_t tweak
)
125 struct kvm_s390_pv_unp args
= {
131 return s390_pv_cmd(KVM_PV_UNPACK
, &args
);
134 void s390_pv_prep_reset(void)
136 s390_pv_cmd_exit(KVM_PV_PREP_RESET
, NULL
);
139 int s390_pv_verify(void)
141 return s390_pv_cmd(KVM_PV_VERIFY
, NULL
);
144 void s390_pv_unshare(void)
146 s390_pv_cmd_exit(KVM_PV_UNSHARE_ALL
, NULL
);
149 void s390_pv_inject_reset_error(CPUState
*cs
)
151 int r1
= (cs
->kvm_run
->s390_sieic
.ipa
& 0x00f0) >> 4;
152 CPUS390XState
*env
= &S390_CPU(cs
)->env
;
154 /* Report that we are unable to enter protected mode */
155 env
->regs
[r1
+ 1] = DIAG_308_RC_INVAL_FOR_PV
;
158 uint64_t kvm_s390_pv_dmp_get_size_cpu(void)
160 return info_dump
.dump_cpu_buffer_len
;
163 uint64_t kvm_s390_pv_dmp_get_size_completion_data(void)
165 return info_dump
.dump_config_finalize_len
;
168 uint64_t kvm_s390_pv_dmp_get_size_mem_state(void)
170 return info_dump
.dump_config_mem_buffer_per_1m
;
173 bool kvm_s390_pv_info_basic_valid(void)
178 static int s390_pv_dump_cmd(uint64_t subcmd
, uint64_t uaddr
, uint64_t gaddr
,
181 struct kvm_s390_pv_dmp dmp
= {
189 ret
= s390_pv_cmd(KVM_PV_DUMP
, (void *)&dmp
);
191 error_report("KVM DUMP command %ld failed", subcmd
);
196 int kvm_s390_dump_cpu(S390CPU
*cpu
, void *buff
)
198 struct kvm_s390_pv_dmp dmp
= {
199 .subcmd
= KVM_PV_DUMP_CPU
,
200 .buff_addr
= (uint64_t)buff
,
202 .buff_len
= info_dump
.dump_cpu_buffer_len
,
204 struct kvm_pv_cmd pv
= {
206 .data
= (uint64_t)&dmp
,
209 return kvm_vcpu_ioctl(CPU(cpu
), KVM_S390_PV_CPU_COMMAND
, &pv
);
212 int kvm_s390_dump_init(void)
214 return s390_pv_dump_cmd(KVM_PV_DUMP_INIT
, 0, 0, 0);
217 int kvm_s390_dump_mem_state(uint64_t gaddr
, size_t len
, void *dest
)
219 return s390_pv_dump_cmd(KVM_PV_DUMP_CONFIG_STOR_STATE
, (uint64_t)dest
,
223 int kvm_s390_dump_completion_data(void *buff
)
225 return s390_pv_dump_cmd(KVM_PV_DUMP_COMPLETE
, (uint64_t)buff
, 0,
226 info_dump
.dump_config_finalize_len
);
229 #define TYPE_S390_PV_GUEST "s390-pv-guest"
230 OBJECT_DECLARE_SIMPLE_TYPE(S390PVGuest
, S390_PV_GUEST
)
235 * The S390PVGuest object is basically a dummy used to tell the
236 * confidential guest support system to use s390's PV mechanism.
239 * -object s390-pv-guest,id=pv0 \
240 * -machine ...,confidential-guest-support=pv0
243 ConfidentialGuestSupport parent_obj
;
246 typedef struct S390PVGuestClass S390PVGuestClass
;
248 struct S390PVGuestClass
{
249 ConfidentialGuestSupportClass parent_class
;
252 int s390_pv_kvm_init(ConfidentialGuestSupport
*cgs
, Error
**errp
)
254 if (!object_dynamic_cast(OBJECT(cgs
), TYPE_S390_PV_GUEST
)) {
258 if (!s390_has_feat(S390_FEAT_UNPACK
)) {
260 "CPU model does not support Protected Virtualization");
269 OBJECT_DEFINE_TYPE_WITH_INTERFACES(S390PVGuest
,
272 CONFIDENTIAL_GUEST_SUPPORT
,
273 { TYPE_USER_CREATABLE
},
276 static void s390_pv_guest_class_init(ObjectClass
*oc
, void *data
)
280 static void s390_pv_guest_init(Object
*obj
)
284 static void s390_pv_guest_finalize(Object
*obj
)