blob: f11868e865fa0910182cc11275bf9a8516cfb466 [file] [log] [blame]
Janosch Frankc3347ed2020-03-23 04:36:06 -04001/*
2 * Protected Virtualization functions
3 *
4 * Copyright IBM Corp. 2020
5 * Author(s):
6 * Janosch Frank <frankja@linux.ibm.com>
7 *
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.
11 */
12#include "qemu/osdep.h"
13
14#include <linux/kvm.h>
15
Christian Borntraegerfbc13842020-04-06 06:01:58 -040016#include "cpu.h"
Janosch Frankc3347ed2020-03-23 04:36:06 -040017#include "qemu/error-report.h"
18#include "sysemu/kvm.h"
Christian Borntraegerfbc13842020-04-06 06:01:58 -040019#include "hw/s390x/ipl.h"
Janosch Frankc3347ed2020-03-23 04:36:06 -040020#include "hw/s390x/pv.h"
21
22static int __s390_pv_cmd(uint32_t cmd, const char *cmdname, void *data)
23{
24 struct kvm_pv_cmd pv_cmd = {
25 .cmd = cmd,
26 .data = (uint64_t)data,
27 };
Christian Borntraegere8d12a52020-03-27 08:46:16 -040028 int rc;
29
30 do {
31 rc = kvm_vm_ioctl(kvm_state, KVM_S390_PV_COMMAND, &pv_cmd);
32 } while (rc == -EINTR);
Janosch Frankc3347ed2020-03-23 04:36:06 -040033
34 if (rc) {
35 error_report("KVM PV command %d (%s) failed: header rc %x rrc %x "
36 "IOCTL rc: %d", cmd, cmdname, pv_cmd.rc, pv_cmd.rrc,
37 rc);
38 }
39 return rc;
40}
41
42/*
43 * This macro lets us pass the command as a string to the function so
44 * we can print it on an error.
45 */
46#define s390_pv_cmd(cmd, data) __s390_pv_cmd(cmd, #cmd, data);
47#define s390_pv_cmd_exit(cmd, data) \
48{ \
49 int rc; \
50 \
51 rc = __s390_pv_cmd(cmd, #cmd, data);\
52 if (rc) { \
53 exit(1); \
54 } \
55}
56
57int s390_pv_vm_enable(void)
58{
59 return s390_pv_cmd(KVM_PV_ENABLE, NULL);
60}
61
62void s390_pv_vm_disable(void)
63{
64 s390_pv_cmd_exit(KVM_PV_DISABLE, NULL);
65}
66
67int s390_pv_set_sec_parms(uint64_t origin, uint64_t length)
68{
69 struct kvm_s390_pv_sec_parm args = {
70 .origin = origin,
71 .length = length,
72 };
73
74 return s390_pv_cmd(KVM_PV_SET_SEC_PARMS, &args);
75}
76
77/*
78 * Called for each component in the SE type IPL parameter block 0.
79 */
80int s390_pv_unpack(uint64_t addr, uint64_t size, uint64_t tweak)
81{
82 struct kvm_s390_pv_unp args = {
83 .addr = addr,
84 .size = size,
85 .tweak = tweak,
86 };
87
88 return s390_pv_cmd(KVM_PV_UNPACK, &args);
89}
90
91void s390_pv_perf_clear_reset(void)
92{
93 s390_pv_cmd_exit(KVM_PV_PREP_RESET, NULL);
94}
95
96int s390_pv_verify(void)
97{
98 return s390_pv_cmd(KVM_PV_VERIFY, NULL);
99}
100
101void s390_pv_unshare(void)
102{
103 s390_pv_cmd_exit(KVM_PV_UNSHARE_ALL, NULL);
104}
Christian Borntraegerfbc13842020-04-06 06:01:58 -0400105
106void s390_pv_inject_reset_error(CPUState *cs)
107{
108 int r1 = (cs->kvm_run->s390_sieic.ipa & 0x00f0) >> 4;
109 CPUS390XState *env = &S390_CPU(cs)->env;
110
111 /* Report that we are unable to enter protected mode */
112 env->regs[r1 + 1] = DIAG_308_RC_INVAL_FOR_PV;
113}