Blame SOURCES/kvm-target-s390x-kvm-Honor-storage-keys-during-emulation.patch

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