ddf19c
From 5a8b40c3fdafeb49072f8643210bea00ce1478c4 Mon Sep 17 00:00:00 2001
ddf19c
From: Thomas Huth <thuth@redhat.com>
ddf19c
Date: Fri, 29 May 2020 05:54:10 -0400
ddf19c
Subject: [PATCH 28/42] s390x: protvirt: SCLP interpretation
ddf19c
ddf19c
RH-Author: Thomas Huth <thuth@redhat.com>
ddf19c
Message-id: <20200529055420.16855-29-thuth@redhat.com>
ddf19c
Patchwork-id: 97053
ddf19c
O-Subject: [RHEL-8.3.0 qemu-kvm PATCH v2 28/38] s390x: protvirt: SCLP interpretation
ddf19c
Bugzilla: 1828317
ddf19c
RH-Acked-by: Claudio Imbrenda <cimbrend@redhat.com>
ddf19c
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
ddf19c
RH-Acked-by: David Hildenbrand <david@redhat.com>
ddf19c
ddf19c
From: Janosch Frank <frankja@linux.ibm.com>
ddf19c
ddf19c
SCLP for a protected guest is done over the SIDAD, so we need to use
ddf19c
the s390_cpu_pv_mem_* functions to access the SIDAD instead of guest
ddf19c
memory when reading/writing SCBs.
ddf19c
ddf19c
To not confuse the sclp emulation, we set 0x4000 as the SCCB address,
ddf19c
since the function that injects the sclp external interrupt would
ddf19c
reject a zero sccb address.
ddf19c
ddf19c
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
ddf19c
Reviewed-by: David Hildenbrand <david@redhat.com>
ddf19c
Reviewed-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
ddf19c
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
ddf19c
Reviewed-by: Christian Borntraeger <borntraeger@de.ibm.com>
ddf19c
Message-Id: <20200319131921.2367-10-frankja@linux.ibm.com>
ddf19c
Signed-off-by: Cornelia Huck <cohuck@redhat.com>
ddf19c
(cherry picked from commit 0f73c5b30b8ba6c0828608be496d2f59a5427539)
ddf19c
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ddf19c
---
ddf19c
 hw/s390x/sclp.c         | 56 +++++++++++++++++++++++++++++++++--------
ddf19c
 include/hw/s390x/sclp.h |  2 ++
ddf19c
 target/s390x/kvm.c      | 25 ++++++++++++++----
ddf19c
 3 files changed, 67 insertions(+), 16 deletions(-)
ddf19c
ddf19c
diff --git a/hw/s390x/sclp.c b/hw/s390x/sclp.c
ddf19c
index f57ce7b739..1c380a49cc 100644
ddf19c
--- a/hw/s390x/sclp.c
ddf19c
+++ b/hw/s390x/sclp.c
ddf19c
@@ -33,6 +33,22 @@ static inline SCLPDevice *get_sclp_device(void)
ddf19c
     return sclp;
ddf19c
 }
ddf19c
 
ddf19c
+static inline bool sclp_command_code_valid(uint32_t code)
ddf19c
+{
ddf19c
+    switch (code & SCLP_CMD_CODE_MASK) {
ddf19c
+    case SCLP_CMDW_READ_SCP_INFO:
ddf19c
+    case SCLP_CMDW_READ_SCP_INFO_FORCED:
ddf19c
+    case SCLP_CMDW_READ_CPU_INFO:
ddf19c
+    case SCLP_CMDW_CONFIGURE_IOA:
ddf19c
+    case SCLP_CMDW_DECONFIGURE_IOA:
ddf19c
+    case SCLP_CMD_READ_EVENT_DATA:
ddf19c
+    case SCLP_CMD_WRITE_EVENT_DATA:
ddf19c
+    case SCLP_CMD_WRITE_EVENT_MASK:
ddf19c
+        return true;
ddf19c
+    }
ddf19c
+    return false;
ddf19c
+}
ddf19c
+
ddf19c
 static void prepare_cpu_entries(SCLPDevice *sclp, CPUEntry *entry, int *count)
ddf19c
 {
ddf19c
     MachineState *ms = MACHINE(qdev_get_machine());
ddf19c
@@ -193,6 +209,34 @@ static void sclp_execute(SCLPDevice *sclp, SCCB *sccb, uint32_t code)
ddf19c
     }
ddf19c
 }
ddf19c
 
ddf19c
+/*
ddf19c
+ * We only need the address to have something valid for the
ddf19c
+ * service_interrupt call.
ddf19c
+ */
ddf19c
+#define SCLP_PV_DUMMY_ADDR 0x4000
ddf19c
+int sclp_service_call_protected(CPUS390XState *env, uint64_t sccb,
ddf19c
+                                uint32_t code)
ddf19c
+{
ddf19c
+    SCLPDevice *sclp = get_sclp_device();
ddf19c
+    SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
ddf19c
+    SCCB work_sccb;
ddf19c
+    hwaddr sccb_len = sizeof(SCCB);
ddf19c
+
ddf19c
+    s390_cpu_pv_mem_read(env_archcpu(env), 0, &work_sccb, sccb_len);
ddf19c
+
ddf19c
+    if (!sclp_command_code_valid(code)) {
ddf19c
+        work_sccb.h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
ddf19c
+        goto out_write;
ddf19c
+    }
ddf19c
+
ddf19c
+    sclp_c->execute(sclp, &work_sccb, code);
ddf19c
+out_write:
ddf19c
+    s390_cpu_pv_mem_write(env_archcpu(env), 0, &work_sccb,
ddf19c
+                          be16_to_cpu(work_sccb.h.length));
ddf19c
+    sclp_c->service_interrupt(sclp, SCLP_PV_DUMMY_ADDR);
ddf19c
+    return 0;
ddf19c
+}
ddf19c
+
ddf19c
 int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code)
ddf19c
 {
ddf19c
     SCLPDevice *sclp = get_sclp_device();
ddf19c
@@ -230,17 +274,7 @@ int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code)
ddf19c
         goto out;
ddf19c
     }
ddf19c
 
ddf19c
-    switch (code & SCLP_CMD_CODE_MASK) {
ddf19c
-    case SCLP_CMDW_READ_SCP_INFO:
ddf19c
-    case SCLP_CMDW_READ_SCP_INFO_FORCED:
ddf19c
-    case SCLP_CMDW_READ_CPU_INFO:
ddf19c
-    case SCLP_CMDW_CONFIGURE_IOA:
ddf19c
-    case SCLP_CMDW_DECONFIGURE_IOA:
ddf19c
-    case SCLP_CMD_READ_EVENT_DATA:
ddf19c
-    case SCLP_CMD_WRITE_EVENT_DATA:
ddf19c
-    case SCLP_CMD_WRITE_EVENT_MASK:
ddf19c
-        break;
ddf19c
-    default:
ddf19c
+    if (!sclp_command_code_valid(code)) {
ddf19c
         work_sccb.h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
ddf19c
         goto out_write;
ddf19c
     }
ddf19c
diff --git a/include/hw/s390x/sclp.h b/include/hw/s390x/sclp.h
ddf19c
index c54413b78c..c0a3faa37d 100644
ddf19c
--- a/include/hw/s390x/sclp.h
ddf19c
+++ b/include/hw/s390x/sclp.h
ddf19c
@@ -217,5 +217,7 @@ void s390_sclp_init(void);
ddf19c
 void sclp_service_interrupt(uint32_t sccb);
ddf19c
 void raise_irq_cpu_hotplug(void);
ddf19c
 int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code);
ddf19c
+int sclp_service_call_protected(CPUS390XState *env, uint64_t sccb,
ddf19c
+                                uint32_t code);
ddf19c
 
ddf19c
 #endif
ddf19c
diff --git a/target/s390x/kvm.c b/target/s390x/kvm.c
ddf19c
index 6809a5ac40..56fe60c49c 100644
ddf19c
--- a/target/s390x/kvm.c
ddf19c
+++ b/target/s390x/kvm.c
ddf19c
@@ -1230,12 +1230,27 @@ static void kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
ddf19c
     sccb = env->regs[ipbh0 & 0xf];
ddf19c
     code = env->regs[(ipbh0 & 0xf0) >> 4];
ddf19c
 
ddf19c
-    r = sclp_service_call(env, sccb, code);
ddf19c
-    if (r < 0) {
ddf19c
-        kvm_s390_program_interrupt(cpu, -r);
ddf19c
-        return;
ddf19c
+    switch (run->s390_sieic.icptcode) {
ddf19c
+    case ICPT_PV_INSTR_NOTIFICATION:
ddf19c
+        g_assert(s390_is_pv());
ddf19c
+        /* The notification intercepts are currently handled by KVM */
ddf19c
+        error_report("unexpected SCLP PV notification");
ddf19c
+        exit(1);
ddf19c
+        break;
ddf19c
+    case ICPT_PV_INSTR:
ddf19c
+        g_assert(s390_is_pv());
ddf19c
+        sclp_service_call_protected(env, sccb, code);
ddf19c
+        /* Setting the CC is done by the Ultravisor. */
ddf19c
+        break;
ddf19c
+    case ICPT_INSTRUCTION:
ddf19c
+        g_assert(!s390_is_pv());
ddf19c
+        r = sclp_service_call(env, sccb, code);
ddf19c
+        if (r < 0) {
ddf19c
+            kvm_s390_program_interrupt(cpu, -r);
ddf19c
+            return;
ddf19c
+        }
ddf19c
+        setcc(cpu, r);
ddf19c
     }
ddf19c
-    setcc(cpu, r);
ddf19c
 }
ddf19c
 
ddf19c
 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
ddf19c
-- 
ddf19c
2.27.0
ddf19c