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

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