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