97168e
From 8d1a60069cddcc69ef1a6f50f2b55343de348b57 Mon Sep 17 00:00:00 2001
97168e
From: Janis Schoetterl-Glausch <scgl@linux.ibm.com>
97168e
Date: Fri, 6 May 2022 17:39:56 +0200
97168e
Subject: [PATCH 2/3] target/s390x: kvm: Honor storage keys during emulation
97168e
MIME-Version: 1.0
97168e
Content-Type: text/plain; charset=UTF-8
97168e
Content-Transfer-Encoding: 8bit
97168e
97168e
RH-Author: Cédric Le Goater <None>
97168e
RH-MergeRequest: 220: s390x: Fix skey test in kvm_unit_test
97168e
RH-Bugzilla: 2124757
97168e
RH-Acked-by: Thomas Huth <thuth@redhat.com>
97168e
RH-Acked-by: David Hildenbrand <david@redhat.com>
97168e
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
97168e
RH-Commit: [2/2] 980dbb4eba8d2f1da7cf4113230d0a6483cffc4f
97168e
97168e
Storage key controlled protection is currently not honored when
97168e
emulating instructions.
97168e
If available, enable key protection for the MEM_OP ioctl, thereby
97168e
enabling it for the s390_cpu_virt_mem_* functions, when using kvm.
97168e
As a result, the emulation of the following instructions honors storage
97168e
keys:
97168e
97168e
* CLP
97168e
  	The Synch I/O CLP command would need special handling in order
97168e
  	to support storage keys, but is currently not supported.
97168e
* CHSC
97168e
	Performing commands asynchronously would require special
97168e
	handling, but commands are currently always synchronous.
97168e
* STSI
97168e
* TSCH
97168e
	Must (and does) not change channel if terminated due to
97168e
	protection.
97168e
* MSCH
97168e
	Suppressed on protection, works because fetching instruction.
97168e
* SSCH
97168e
	Suppressed on protection, works because fetching instruction.
97168e
* STSCH
97168e
* STCRW
97168e
	Suppressed on protection, this works because no partial store is
97168e
	possible, because the operand cannot span multiple pages.
97168e
* PCISTB
97168e
* MPCIFC
97168e
* STPCIFC
97168e
97168e
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2124757
97168e
97168e
Signed-off-by: Janis Schoetterl-Glausch <scgl@linux.ibm.com>
97168e
Message-Id: <20220506153956.2217601-3-scgl@linux.ibm.com>
97168e
Signed-off-by: Thomas Huth <thuth@redhat.com>
97168e
(cherry picked from commit 54354861d21b69ec0781f43e67b8d4f6edad7e3f)
97168e
Signed-off-by: Cédric Le Goater <clg@redhat.com>
97168e
---
97168e
 target/s390x/kvm/kvm.c | 9 +++++++++
97168e
 1 file changed, 9 insertions(+)
97168e
97168e
diff --git a/target/s390x/kvm/kvm.c b/target/s390x/kvm/kvm.c
97168e
index c52434985b..ba04997da1 100644
97168e
--- a/target/s390x/kvm/kvm.c
97168e
+++ b/target/s390x/kvm/kvm.c
97168e
@@ -152,12 +152,15 @@ const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
97168e
 static int cap_sync_regs;
97168e
 static int cap_async_pf;
97168e
 static int cap_mem_op;
97168e
+static int cap_mem_op_extension;
97168e
 static int cap_s390_irq;
97168e
 static int cap_ri;
97168e
 static int cap_hpage_1m;
97168e
 static int cap_vcpu_resets;
97168e
 static int cap_protected;
97168e
 
97168e
+static bool mem_op_storage_key_support;
97168e
+
97168e
 static int active_cmma;
97168e
 
97168e
 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
97168e
@@ -355,6 +358,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
97168e
     cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
97168e
     cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
97168e
     cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
97168e
+    cap_mem_op_extension = kvm_check_extension(s, KVM_CAP_S390_MEM_OP_EXTENSION);
97168e
+    mem_op_storage_key_support = cap_mem_op_extension > 0;
97168e
     cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
97168e
     cap_vcpu_resets = kvm_check_extension(s, KVM_CAP_S390_VCPU_RESETS);
97168e
     cap_protected = kvm_check_extension(s, KVM_CAP_S390_PROTECTED);
97168e
@@ -843,6 +848,7 @@ int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
97168e
                        : KVM_S390_MEMOP_LOGICAL_READ,
97168e
         .buf = (uint64_t)hostbuf,
97168e
         .ar = ar,
97168e
+        .key = (cpu->env.psw.mask & PSW_MASK_KEY) >> PSW_SHIFT_KEY,
97168e
     };
97168e
     int ret;
97168e
 
97168e
@@ -852,6 +858,9 @@ int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
97168e
     if (!hostbuf) {
97168e
         mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
97168e
     }
97168e
+    if (mem_op_storage_key_support) {
97168e
+        mem_op.flags |= KVM_S390_MEMOP_F_SKEY_PROTECTION;
97168e
+    }
97168e
 
97168e
     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
97168e
     if (ret < 0) {
97168e
-- 
97168e
2.35.3
97168e