Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20210330' into...
[qemu/ar7.git] / hw / s390x / pv.c
blob93eccfc05d5ddc4f6903dc71519f2448b9effb9b
1 /*
2 * Protected Virtualization functions
4 * Copyright IBM Corp. 2020
5 * Author(s):
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
10 * directory.
12 #include "qemu/osdep.h"
14 #include <linux/kvm.h>
16 #include "cpu.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
19 #include "sysemu/kvm.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"
25 static int __s390_pv_cmd(uint32_t cmd, const char *cmdname, void *data)
27 struct kvm_pv_cmd pv_cmd = {
28 .cmd = cmd,
29 .data = (uint64_t)data,
31 int rc;
33 do {
34 rc = kvm_vm_ioctl(kvm_state, KVM_S390_PV_COMMAND, &pv_cmd);
35 } while (rc == -EINTR);
37 if (rc) {
38 error_report("KVM PV command %d (%s) failed: header rc %x rrc %x "
39 "IOCTL rc: %d", cmd, cmdname, pv_cmd.rc, pv_cmd.rrc,
40 rc);
42 return rc;
46 * This macro lets us pass the command as a string to the function so
47 * we can print it on an error.
49 #define s390_pv_cmd(cmd, data) __s390_pv_cmd(cmd, #cmd, data);
50 #define s390_pv_cmd_exit(cmd, data) \
51 { \
52 int rc; \
54 rc = __s390_pv_cmd(cmd, #cmd, data);\
55 if (rc) { \
56 exit(1); \
57 } \
60 int s390_pv_vm_enable(void)
62 return s390_pv_cmd(KVM_PV_ENABLE, NULL);
65 void s390_pv_vm_disable(void)
67 s390_pv_cmd_exit(KVM_PV_DISABLE, NULL);
70 int s390_pv_set_sec_parms(uint64_t origin, uint64_t length)
72 struct kvm_s390_pv_sec_parm args = {
73 .origin = origin,
74 .length = length,
77 return s390_pv_cmd(KVM_PV_SET_SEC_PARMS, &args);
81 * Called for each component in the SE type IPL parameter block 0.
83 int s390_pv_unpack(uint64_t addr, uint64_t size, uint64_t tweak)
85 struct kvm_s390_pv_unp args = {
86 .addr = addr,
87 .size = size,
88 .tweak = tweak,
91 return s390_pv_cmd(KVM_PV_UNPACK, &args);
94 void s390_pv_prep_reset(void)
96 s390_pv_cmd_exit(KVM_PV_PREP_RESET, NULL);
99 int s390_pv_verify(void)
101 return s390_pv_cmd(KVM_PV_VERIFY, NULL);
104 void s390_pv_unshare(void)
106 s390_pv_cmd_exit(KVM_PV_UNSHARE_ALL, NULL);
109 void s390_pv_inject_reset_error(CPUState *cs)
111 int r1 = (cs->kvm_run->s390_sieic.ipa & 0x00f0) >> 4;
112 CPUS390XState *env = &S390_CPU(cs)->env;
114 /* Report that we are unable to enter protected mode */
115 env->regs[r1 + 1] = DIAG_308_RC_INVAL_FOR_PV;
118 #define TYPE_S390_PV_GUEST "s390-pv-guest"
119 OBJECT_DECLARE_SIMPLE_TYPE(S390PVGuest, S390_PV_GUEST)
122 * S390PVGuest:
124 * The S390PVGuest object is basically a dummy used to tell the
125 * confidential guest support system to use s390's PV mechanism.
127 * # $QEMU \
128 * -object s390-pv-guest,id=pv0 \
129 * -machine ...,confidential-guest-support=pv0
131 struct S390PVGuest {
132 ConfidentialGuestSupport parent_obj;
135 typedef struct S390PVGuestClass S390PVGuestClass;
137 struct S390PVGuestClass {
138 ConfidentialGuestSupportClass parent_class;
141 int s390_pv_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
143 if (!object_dynamic_cast(OBJECT(cgs), TYPE_S390_PV_GUEST)) {
144 return 0;
147 if (!s390_has_feat(S390_FEAT_UNPACK)) {
148 error_setg(errp,
149 "CPU model does not support Protected Virtualization");
150 return -1;
153 cgs->ready = true;
155 return 0;
158 OBJECT_DEFINE_TYPE_WITH_INTERFACES(S390PVGuest,
159 s390_pv_guest,
160 S390_PV_GUEST,
161 CONFIDENTIAL_GUEST_SUPPORT,
162 { TYPE_USER_CREATABLE },
163 { NULL })
165 static void s390_pv_guest_class_init(ObjectClass *oc, void *data)
169 static void s390_pv_guest_init(Object *obj)
173 static void s390_pv_guest_finalize(Object *obj)