1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
* Protected Virtualization functions
*
* Copyright IBM Corp. 2020
* Author(s):
* Janosch Frank <frankja@linux.ibm.com>
*
* This work is licensed under the terms of the GNU GPL, version 2 or (at
* your option) any later version. See the COPYING file in the top-level
* directory.
*/
#include "qemu/osdep.h"
#include <linux/kvm.h>
#include "qemu/units.h"
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "sysemu/kvm.h"
#include "sysemu/cpus.h"
#include "qom/object_interfaces.h"
#include "exec/confidential-guest-support.h"
#include "hw/s390x/ipl.h"
#include "hw/s390x/pv.h"
#include "hw/s390x/sclp.h"
#include "target/s390x/kvm/kvm_s390x.h"
static bool info_valid;
static struct kvm_s390_pv_info_vm info_vm;
static struct kvm_s390_pv_info_dump info_dump;
static int __s390_pv_cmd(uint32_t cmd, const char *cmdname, void *data)
{
struct kvm_pv_cmd pv_cmd = {
.cmd = cmd,
.data = (uint64_t)data,
};
int rc;
do {
rc = kvm_vm_ioctl(kvm_state, KVM_S390_PV_COMMAND, &pv_cmd);
} while (rc == -EINTR);
if (rc) {
error_report("KVM PV command %d (%s) failed: header rc %x rrc %x "
"IOCTL rc: %d", cmd, cmdname, pv_cmd.rc, pv_cmd.rrc,
rc);
}
return rc;
}
/*
* This macro lets us pass the command as a string to the function so
* we can print it on an error.
*/
#define s390_pv_cmd(cmd, data) __s390_pv_cmd(cmd, #cmd, data)
#define s390_pv_cmd_exit(cmd, data) \
{ \
int rc; \
\
rc = __s390_pv_cmd(cmd, #cmd, data);\
if (rc) { \
exit(1); \
} \
}
int s390_pv_query_info(void)
{
struct kvm_s390_pv_info info = {
.header.id = KVM_PV_INFO_VM,
.header.len_max = sizeof(info.header) + sizeof(info.vm),
};
int rc;
/* Info API's first user is dump so they are bundled */
if (!kvm_s390_get_protected_dump()) {
return 0;
}
rc = s390_pv_cmd(KVM_PV_INFO, &info);
if (rc) {
error_report("KVM PV INFO cmd %x failed: %s",
info.header.id, strerror(-rc));
return rc;
}
memcpy(&info_vm, &info.vm, sizeof(info.vm));
info.header.id = KVM_PV_INFO_DUMP;
info.header.len_max = sizeof(info.header) + sizeof(info.dump);
rc = s390_pv_cmd(KVM_PV_INFO, &info);
if (rc) {
error_report("KVM PV INFO cmd %x failed: %s",
info.header.id, strerror(-rc));
return rc;
}
memcpy(&info_dump, &info.dump, sizeof(info.dump));
info_valid = true;
return rc;
}
int s390_pv_vm_enable(void)
{
return s390_pv_cmd(KVM_PV_ENABLE, NULL);
}
void s390_pv_vm_disable(void)
{
s390_pv_cmd_exit(KVM_PV_DISABLE, NULL);
}
static void *s390_pv_do_unprot_async_fn(void *p)
{
s390_pv_cmd_exit(KVM_PV_ASYNC_CLEANUP_PERFORM, NULL);
return NULL;
}
bool s390_pv_vm_try_disable_async(S390CcwMachineState *ms)
{
/*
* t is only needed to create the thread; once qemu_thread_create
* returns, it can safely be discarded.
*/
QemuThread t;
/*
* If the feature is not present or if the VM is not larger than 2 GiB,
* KVM_PV_ASYNC_CLEANUP_PREPARE fill fail; no point in attempting it.
*/
if ((MACHINE(ms)->maxram_size <= 2 * GiB) ||
!kvm_check_extension(kvm_state, KVM_CAP_S390_PROTECTED_ASYNC_DISABLE)) {
return false;
}
if (s390_pv_cmd(KVM_PV_ASYNC_CLEANUP_PREPARE, NULL) != 0) {
return false;
}
qemu_thread_create(&t, "async_cleanup", s390_pv_do_unprot_async_fn, NULL,
QEMU_THREAD_DETACHED);
return true;
}
int s390_pv_set_sec_parms(uint64_t origin, uint64_t length)
{
struct kvm_s390_pv_sec_parm args = {
.origin = origin,
.length = length,
};
return s390_pv_cmd(KVM_PV_SET_SEC_PARMS, &args);
}
/*
* Called for each component in the SE type IPL parameter block 0.
*/
int s390_pv_unpack(uint64_t addr, uint64_t size, uint64_t tweak)
{
struct kvm_s390_pv_unp args = {
.addr = addr,
.size = size,
.tweak = tweak,
};
return s390_pv_cmd(KVM_PV_UNPACK, &args);
}
void s390_pv_prep_reset(void)
{
s390_pv_cmd_exit(KVM_PV_PREP_RESET, NULL);
}
int s390_pv_verify(void)
{
return s390_pv_cmd(KVM_PV_VERIFY, NULL);
}
void s390_pv_unshare(void)
{
s390_pv_cmd_exit(KVM_PV_UNSHARE_ALL, NULL);
}
void s390_pv_inject_reset_error(CPUState *cs)
{
int r1 = (cs->kvm_run->s390_sieic.ipa & 0x00f0) >> 4;
CPUS390XState *env = &S390_CPU(cs)->env;
/* Report that we are unable to enter protected mode */
env->regs[r1 + 1] = DIAG_308_RC_INVAL_FOR_PV;
}
uint64_t kvm_s390_pv_dmp_get_size_cpu(void)
{
return info_dump.dump_cpu_buffer_len;
}
uint64_t kvm_s390_pv_dmp_get_size_completion_data(void)
{
return info_dump.dump_config_finalize_len;
}
uint64_t kvm_s390_pv_dmp_get_size_mem_state(void)
{
return info_dump.dump_config_mem_buffer_per_1m;
}
bool kvm_s390_pv_info_basic_valid(void)
{
return info_valid;
}
static int s390_pv_dump_cmd(uint64_t subcmd, uint64_t uaddr, uint64_t gaddr,
uint64_t len)
{
struct kvm_s390_pv_dmp dmp = {
.subcmd = subcmd,
.buff_addr = uaddr,
.buff_len = len,
.gaddr = gaddr,
};
int ret;
ret = s390_pv_cmd(KVM_PV_DUMP, (void *)&dmp);
if (ret) {
error_report("KVM DUMP command %ld failed", subcmd);
}
return ret;
}
int kvm_s390_dump_cpu(S390CPU *cpu, void *buff)
{
struct kvm_s390_pv_dmp dmp = {
.subcmd = KVM_PV_DUMP_CPU,
.buff_addr = (uint64_t)buff,
.gaddr = 0,
.buff_len = info_dump.dump_cpu_buffer_len,
};
struct kvm_pv_cmd pv = {
.cmd = KVM_PV_DUMP,
.data = (uint64_t)&dmp,
};
return kvm_vcpu_ioctl(CPU(cpu), KVM_S390_PV_CPU_COMMAND, &pv);
}
int kvm_s390_dump_init(void)
{
return s390_pv_dump_cmd(KVM_PV_DUMP_INIT, 0, 0, 0);
}
int kvm_s390_dump_mem_state(uint64_t gaddr, size_t len, void *dest)
{
return s390_pv_dump_cmd(KVM_PV_DUMP_CONFIG_STOR_STATE, (uint64_t)dest,
gaddr, len);
}
int kvm_s390_dump_completion_data(void *buff)
{
return s390_pv_dump_cmd(KVM_PV_DUMP_COMPLETE, (uint64_t)buff, 0,
info_dump.dump_config_finalize_len);
}
#define TYPE_S390_PV_GUEST "s390-pv-guest"
OBJECT_DECLARE_SIMPLE_TYPE(S390PVGuest, S390_PV_GUEST)
/**
* S390PVGuest:
*
* The S390PVGuest object is basically a dummy used to tell the
* confidential guest support system to use s390's PV mechanism.
*
* # $QEMU \
* -object s390-pv-guest,id=pv0 \
* -machine ...,confidential-guest-support=pv0
*/
struct S390PVGuest {
ConfidentialGuestSupport parent_obj;
};
typedef struct S390PVGuestClass S390PVGuestClass;
struct S390PVGuestClass {
ConfidentialGuestSupportClass parent_class;
};
/*
* If protected virtualization is enabled, the amount of data that the
* Read SCP Info Service Call can use is limited to one page. The
* available space also depends on the Extended-Length SCCB (ELS)
* feature which can take more buffer space to store feature
* information. This impacts the maximum number of CPUs supported in
* the machine.
*/
static uint32_t s390_pv_get_max_cpus(void)
{
int offset_cpu = s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB) ?
offsetof(ReadInfo, entries) : SCLP_READ_SCP_INFO_FIXED_CPU_OFFSET;
return (TARGET_PAGE_SIZE - offset_cpu) / sizeof(CPUEntry);
}
static bool s390_pv_check_cpus(Error **errp)
{
MachineState *ms = MACHINE(qdev_get_machine());
uint32_t pv_max_cpus = s390_pv_get_max_cpus();
if (ms->smp.max_cpus > pv_max_cpus) {
error_setg(errp, "Protected VMs support a maximum of %d CPUs",
pv_max_cpus);
return false;
}
return true;
}
static bool s390_pv_guest_check(ConfidentialGuestSupport *cgs, Error **errp)
{
return s390_pv_check_cpus(errp);
}
int s390_pv_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
{
if (!object_dynamic_cast(OBJECT(cgs), TYPE_S390_PV_GUEST)) {
return 0;
}
if (!s390_has_feat(S390_FEAT_UNPACK)) {
error_setg(errp,
"CPU model does not support Protected Virtualization");
return -1;
}
if (!s390_pv_guest_check(cgs, errp)) {
return -1;
}
cgs->ready = true;
return 0;
}
OBJECT_DEFINE_TYPE_WITH_INTERFACES(S390PVGuest,
s390_pv_guest,
S390_PV_GUEST,
CONFIDENTIAL_GUEST_SUPPORT,
{ TYPE_USER_CREATABLE },
{ NULL })
static void s390_pv_guest_class_init(ObjectClass *oc, void *data)
{
}
static void s390_pv_guest_init(Object *obj)
{
}
static void s390_pv_guest_finalize(Object *obj)
{
}
|