target/i386: Cleanup read_crN, write_crN, lmsw
[qemu/ar7.git] / hw / s390x / pv.c
blob401b63d6cb659c412f4f34d2d05d8157e6ea8ded
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 "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"
24 static int __s390_pv_cmd(uint32_t cmd, const char *cmdname, void *data)
26 struct kvm_pv_cmd pv_cmd = {
27 .cmd = cmd,
28 .data = (uint64_t)data,
30 int rc;
32 do {
33 rc = kvm_vm_ioctl(kvm_state, KVM_S390_PV_COMMAND, &pv_cmd);
34 } while (rc == -EINTR);
36 if (rc) {
37 error_report("KVM PV command %d (%s) failed: header rc %x rrc %x "
38 "IOCTL rc: %d", cmd, cmdname, pv_cmd.rc, pv_cmd.rrc,
39 rc);
41 return rc;
45 * This macro lets us pass the command as a string to the function so
46 * we can print it on an error.
48 #define s390_pv_cmd(cmd, data) __s390_pv_cmd(cmd, #cmd, data);
49 #define s390_pv_cmd_exit(cmd, data) \
50 { \
51 int rc; \
53 rc = __s390_pv_cmd(cmd, #cmd, data);\
54 if (rc) { \
55 exit(1); \
56 } \
59 int s390_pv_vm_enable(void)
61 return s390_pv_cmd(KVM_PV_ENABLE, NULL);
64 void s390_pv_vm_disable(void)
66 s390_pv_cmd_exit(KVM_PV_DISABLE, NULL);
69 int s390_pv_set_sec_parms(uint64_t origin, uint64_t length)
71 struct kvm_s390_pv_sec_parm args = {
72 .origin = origin,
73 .length = length,
76 return s390_pv_cmd(KVM_PV_SET_SEC_PARMS, &args);
80 * Called for each component in the SE type IPL parameter block 0.
82 int s390_pv_unpack(uint64_t addr, uint64_t size, uint64_t tweak)
84 struct kvm_s390_pv_unp args = {
85 .addr = addr,
86 .size = size,
87 .tweak = tweak,
90 return s390_pv_cmd(KVM_PV_UNPACK, &args);
93 void s390_pv_prep_reset(void)
95 s390_pv_cmd_exit(KVM_PV_PREP_RESET, NULL);
98 int s390_pv_verify(void)
100 return s390_pv_cmd(KVM_PV_VERIFY, NULL);
103 void s390_pv_unshare(void)
105 s390_pv_cmd_exit(KVM_PV_UNSHARE_ALL, NULL);
108 void s390_pv_inject_reset_error(CPUState *cs)
110 int r1 = (cs->kvm_run->s390_sieic.ipa & 0x00f0) >> 4;
111 CPUS390XState *env = &S390_CPU(cs)->env;
113 /* Report that we are unable to enter protected mode */
114 env->regs[r1 + 1] = DIAG_308_RC_INVAL_FOR_PV;
117 #define TYPE_S390_PV_GUEST "s390-pv-guest"
118 OBJECT_DECLARE_SIMPLE_TYPE(S390PVGuest, S390_PV_GUEST)
121 * S390PVGuest:
123 * The S390PVGuest object is basically a dummy used to tell the
124 * confidential guest support system to use s390's PV mechanism.
126 * # $QEMU \
127 * -object s390-pv-guest,id=pv0 \
128 * -machine ...,confidential-guest-support=pv0
130 struct S390PVGuest {
131 ConfidentialGuestSupport parent_obj;
134 typedef struct S390PVGuestClass S390PVGuestClass;
136 struct S390PVGuestClass {
137 ConfidentialGuestSupportClass parent_class;
140 int s390_pv_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
142 if (!object_dynamic_cast(OBJECT(cgs), TYPE_S390_PV_GUEST)) {
143 return 0;
146 if (!s390_has_feat(S390_FEAT_UNPACK)) {
147 error_setg(errp,
148 "CPU model does not support Protected Virtualization");
149 return -1;
152 cgs->ready = true;
154 return 0;
157 OBJECT_DEFINE_TYPE_WITH_INTERFACES(S390PVGuest,
158 s390_pv_guest,
159 S390_PV_GUEST,
160 CONFIDENTIAL_GUEST_SUPPORT,
161 { TYPE_USER_CREATABLE },
162 { NULL })
164 static void s390_pv_guest_class_init(ObjectClass *oc, void *data)
168 static void s390_pv_guest_init(Object *obj)
172 static void s390_pv_guest_finalize(Object *obj)