Skip to contentSkip to conversation

target/s390x - DIAG 308 extend subcode 10 to return UVC cmd id, RC and RRC values upon failure to enter secure mode

PATCH Superseded Gautam Gala 2 participants 7 messages Apr 11, 2025 – Apr 14, 2025

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover target/s390x - DIAG 308 extend subcode 10 to return UVC cmd id, RC and RRC values upon failure to enter secure mode

DIAG 308 (subcode 10 - performing secure execution unpack) response code when the configuration is unable to enter secure mode has limited usability as it is a fixed value (0xa02) for variety of different reasons. The aim is to extend this DIAG to return UVC command ID, RC and RRC values in addition to the diag response code. This feature can be used by the stage3a bootloader (s390-tools/rust/pvimg/boot) to read these new values from the corresponding register and print an appropriate error message to help pin point the cause.

The response code, UVC RC, RRC, and command ID are returned in bit positions 48-63, 32-47, 16-31, and 0-15 of register R1 + 1 if the function does not complete successfully (Previously, only the response code was returned in bits 48-63).

Gautam Gala (3):
target/s390x: Introduce constant when checking if PV header couldn't
be decrypted
target/s390x: introduce function when exiting PV target/s390x: Return UVC cmd code, RC and RRC value when DIAG 308
Subcode 10 fails to enter secure mode

 hw/s390x/ipl.c             | 11 ++++---
 hw/s390x/ipl.h             |  5 +--
 hw/s390x/s390-virtio-ccw.c | 24 +++++++++++----
 target/s390x/kvm/pv.c      | 62 ++++++++++++++++++++++++--------------
 target/s390x/kvm/pv.h      | 27 ++++++++++++-----
 5 files changed, 86 insertions(+), 43 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/3 target/s390x: Introduce constant when checking if PV header couldn't be decrypted

Introduce a named constant when checking the Set Secure Configuration parameters UV call return code for the case where no valid host key was found and therefore the PV header couldn't be decrypted (0x108).

---
 target/s390x/kvm/pv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/s390x/kvm/pv.c +2 -1
index b191a4a68a..3a0a971f0b 100644
@@ -147,6 +147,7 @@ bool s390_pv_vm_try_disable_async(S390CcwMachineState *ms)
     return true;
 }
 
+#define DIAG_308_UV_RC_INVAL_HOSTKEY    0x0108
 int s390_pv_set_sec_parms(uint64_t origin, uint64_t length, Error **errp)
 {
     int ret, pvrc;
@@ -158,7 +159,7 @@ int s390_pv_set_sec_parms(uint64_t origin, uint64_t length, Error **errp)
     ret = s390_pv_cmd_pvrc(KVM_PV_SET_SEC_PARMS, &args, &pvrc);
     if (ret) {
         error_setg(errp, "Failed to set secure execution parameters");
-        if (pvrc == 0x108) {
+        if (pvrc == DIAG_308_UV_RC_INVAL_HOSTKEY) {
             error_append_hint(errp, "Please check whether the image is "
                                     "correctly encrypted for this host\n");
         }
Signature · 1 line
2.49.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH v1 1/3] target/s390x: Introduce constant when checking if PV header couldn't be decrypted

Quoted 5 lines · Gautam Gala

Introduce a named constant when checking the Set Secure Configuration parameters UV call return code for the case where no valid host key was found and therefore the PV header couldn't be decrypted (0x108).

#3 lore raw

PATCH 2/3 target/s390x: introduce function when exiting PV

In reply to Gautam Gala

introduce a static function when exiting PV. The function replaces an existing macro (s390_pv_cmd_exit).

---
 target/s390x/kvm/pv.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
target/s390x/kvm/pv.c +9 -8
index 3a0a971f0b..b4abda2cef 100644
@@ -59,14 +59,15 @@ static int __s390_pv_cmd(uint32_t cmd, const char *cmdname, void *data,
  */
 #define s390_pv_cmd(cmd, data) __s390_pv_cmd(cmd, #cmd, data, NULL)
 #define s390_pv_cmd_pvrc(cmd, data, pvrc) __s390_pv_cmd(cmd, #cmd, data, pvrc)
-#define s390_pv_cmd_exit(cmd, data)    \
-{                                      \
-    int rc;                            \
-                                       \
-    rc = __s390_pv_cmd(cmd, #cmd, data, NULL); \
-    if (rc) {                          \
-        exit(1);                       \
-    }                                  \
+
+static void s390_pv_cmd_exit(uint32_t cmd, void *data)
+{
+    int rc;
+
+    rc = s390_pv_cmd(cmd, data);
+    if (rc) {
+        exit(1);
+    }
 }
 
 int s390_pv_query_info(void)
Signature · 1 line
2.49.0
#4 lore raw

Re: [PATCH v1 2/3] target/s390x: introduce function when exiting PV

Quoted 21 lines · Gautam Gala

introduce a static function when exiting PV. The function replaces an existing macro (s390_pv_cmd_exit).

---
 target/s390x/kvm/pv.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
target/s390x/kvm/pv.c +0 -5
index 3a0a971f0b..b4abda2cef 100644
@@ -59,14 +59,15 @@ static int __s390_pv_cmd(uint32_t cmd, const char *cmdname, void *data,
  */
 #define s390_pv_cmd(cmd, data) __s390_pv_cmd(cmd, #cmd, data, NULL)
 #define s390_pv_cmd_pvrc(cmd, data, pvrc) __s390_pv_cmd(cmd, #cmd, data, pvrc)
-#define s390_pv_cmd_exit(cmd, data)    \
-{                                      \
-    int rc;                            \
-                                       \
-    rc = __s390_pv_cmd(cmd, #cmd, data, NULL); \

target/s390x/kvm/pv.c static int __s390_pv_cmd(uint32_t cmd, const char *cmdname, void *data, · line 68

Quoted 10 lines
target/s390x/kvm/pv.c +7 -3
-    if (rc) {                          \
-        exit(1);                       \
-    }                                  \
+
+static void s390_pv_cmd_exit(uint32_t cmd, void *data)
+{
+    int rc;
+
+    rc = s390_pv_cmd(cmd, data);
+    if (rc) {

I am not sure if that violates Qemu coding style but you could inline the rc variable here to reduce LoC.

Quoted 9 lines
target/s390x/kvm/pv.c +2 -0
+        exit(1);
+    }
 }
 
 int s390_pv_query_info(void)
Signature · 1 line
2.49.0
#5 lore raw

PATCH 3/3 target/s390x: Return UVC cmd code, RC and RRC value when DIAG 308 Subcode 10 fails to enter secure mode

In reply to Gautam Gala

Extend DIAG308 subcode 10 to return the UVC RC, RRC and command code in bit positions 32-47, 16-31, and 0-15 of register R1 + 1 if the function does not complete successfully (in addition to the previously returned diag response code in bit position 47-63).

---
 hw/s390x/ipl.c             | 11 ++++++----
 hw/s390x/ipl.h             |  5 +++--
 hw/s390x/s390-virtio-ccw.c | 24 +++++++++++++++------
 target/s390x/kvm/pv.c      | 44 +++++++++++++++++++++++++-------------
 target/s390x/kvm/pv.h      | 27 ++++++++++++++++-------
 5 files changed, 76 insertions(+), 35 deletions(-)
hw/s390x/ipl.c +7 -4
index ce6f6078d7..4f3e3945f1 100644
@@ -676,7 +676,8 @@ static void s390_ipl_prepare_qipl(S390CPU *cpu)
     cpu_physical_memory_unmap(addr, len, 1, len);
 }
 
-int s390_ipl_prepare_pv_header(Error **errp)
+int s390_ipl_prepare_pv_header(Error **errp, uint16_t *pv_cmd,
+                               uint16_t *pv_rc, uint16_t *pv_rrc)
 {
     IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
     IPLBlockPV *ipib_pv = &ipib->pv;
@@ -685,12 +686,13 @@ int s390_ipl_prepare_pv_header(Error **errp)
 
     cpu_physical_memory_read(ipib_pv->pv_header_addr, hdr,
                              ipib_pv->pv_header_len);
-    rc = s390_pv_set_sec_parms((uintptr_t)hdr, ipib_pv->pv_header_len, errp);
+    rc = s390_pv_set_sec_parms((uintptr_t)hdr, ipib_pv->pv_header_len,
+                               errp, pv_cmd, pv_rc, pv_rrc);
     g_free(hdr);
     return rc;
 }
 
-int s390_ipl_pv_unpack(void)
+int s390_ipl_pv_unpack(uint16_t *pv_cmd, uint16_t *pv_rc, uint16_t *pv_rrc)
 {
     IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
     IPLBlockPV *ipib_pv = &ipib->pv;
@@ -699,7 +701,8 @@ int s390_ipl_pv_unpack(void)
     for (i = 0; i < ipib_pv->num_comp; i++) {
         rc = s390_pv_unpack(ipib_pv->components[i].addr,
                             TARGET_PAGE_ALIGN(ipib_pv->components[i].size),
-                            ipib_pv->components[i].tweak_pref);
+                            ipib_pv->components[i].tweak_pref,
+                            pv_cmd, pv_rc, pv_rrc);
         if (rc) {
             break;
         }
hw/s390x/ipl.h +3 -2
index 8e3882d506..e021b2431f 100644
@@ -26,8 +26,9 @@ void s390_ipl_convert_loadparm(char *ascii_lp, uint8_t *ebcdic_lp);
 void s390_ipl_fmt_loadparm(uint8_t *loadparm, char *str, Error **errp);
 void s390_rebuild_iplb(uint16_t index, IplParameterBlock *iplb);
 void s390_ipl_update_diag308(IplParameterBlock *iplb);
-int s390_ipl_prepare_pv_header(Error **errp);
-int s390_ipl_pv_unpack(void);
+int s390_ipl_prepare_pv_header(Error **errp, uint16_t *pv_cmd,
+                               uint16_t *pv_rc, uint16_t *pv_rrc);
+int s390_ipl_pv_unpack(uint16_t *pv_cmd, uint16_t *pv_rc, uint16_t *pv_rrc);
 void s390_ipl_prepare_cpu(S390CPU *cpu);
 IplParameterBlock *s390_ipl_get_iplb(void);
 IplParameterBlock *s390_ipl_get_iplb_pv(void);
hw/s390x/s390-virtio-ccw.c +18 -6
index d9e683c5b4..0faf2841d6 100644
@@ -53,6 +53,13 @@
 
 static Error *pv_mig_blocker;
 
+struct diag308response {
+    uint16_t pv_cmd;
+    uint16_t pv_rrc;
+    uint16_t pv_rc;
+    uint16_t diag_rc;
+};
+
 static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id,
                               Error **errp)
 {
@@ -364,7 +371,10 @@ static void s390_machine_unprotect(S390CcwMachineState *ms)
     ram_block_discard_disable(false);
 }
 
-static int s390_machine_protect(S390CcwMachineState *ms)
+static int s390_machine_protect(S390CcwMachineState *ms,
+                                uint16_t *pv_cmd,
+                                uint16_t *pv_rc,
+                                uint16_t *pv_rrc)
 {
     Error *local_err = NULL;
     int rc;
@@ -407,19 +417,19 @@ static int s390_machine_protect(S390CcwMachineState *ms)
     }
 
     /* Set SE header and unpack */
-    rc = s390_ipl_prepare_pv_header(&local_err);
+    rc = s390_ipl_prepare_pv_header(&local_err, pv_cmd, pv_rc, pv_rrc);
     if (rc) {
         goto out_err;
     }
 
     /* Decrypt image */
-    rc = s390_ipl_pv_unpack();
+    rc = s390_ipl_pv_unpack(pv_cmd, pv_rc, pv_rrc);
     if (rc) {
         goto out_err;
     }
 
     /* Verify integrity */
-    rc = s390_pv_verify();
+    rc = s390_pv_verify(pv_cmd, pv_rc, pv_rrc);
     if (rc) {
         goto out_err;
     }
@@ -452,6 +462,7 @@ static void s390_machine_reset(MachineState *machine, ResetType type)
 {
     S390CcwMachineState *ms = S390_CCW_MACHINE(machine);
     enum s390_reset reset_type;
+    struct diag308response resp;
     CPUState *cs, *t;
     S390CPU *cpu;
 
@@ -539,8 +550,9 @@ static void s390_machine_reset(MachineState *machine, ResetType type)
         }
         run_on_cpu(cs, s390_do_cpu_reset, RUN_ON_CPU_NULL);
 
-        if (s390_machine_protect(ms)) {
-            s390_pv_inject_reset_error(cs);
+        if (s390_machine_protect(ms, &resp.pv_cmd, &resp.pv_rc, &resp.pv_rrc)) {
+            resp.diag_rc = DIAG_308_RC_INVAL_FOR_PV;
+            s390_pv_inject_reset_error(cs, (uint64_t *)(&resp));
             /*
              * Continue after the diag308 so the guest knows something
              * went wrong.
target/s390x/kvm/pv.c +29 -15

107 lines not on this page · raw message

target/s390x/kvm/pv.h +19 -8
index 5e9c8bd351..7c003d1e28 100644
@@ -42,12 +42,16 @@ int s390_pv_query_info(void);
 int s390_pv_vm_enable(void);
 void s390_pv_vm_disable(void);
 bool s390_pv_vm_try_disable_async(S390CcwMachineState *ms);
-int s390_pv_set_sec_parms(uint64_t origin, uint64_t length, Error **errp);
-int s390_pv_unpack(uint64_t addr, uint64_t size, uint64_t tweak);
+int s390_pv_set_sec_parms(uint64_t origin, uint64_t length,
+                          Error **errp, uint16_t *pv_cmd,
+                          uint16_t *pv_rc, uint16_t *pv_rrc);
+int s390_pv_unpack(uint64_t addr, uint64_t size,
+                   uint64_t tweak, uint16_t *pv_cmd,
+                   uint16_t *pv_rc, uint16_t *pv_rrc);
 void s390_pv_prep_reset(void);
-int s390_pv_verify(void);
+int s390_pv_verify(uint16_t *pv_cmd, uint16_t *pv_rc, uint16_t *pv_rrc);
 void s390_pv_unshare(void);
-void s390_pv_inject_reset_error(CPUState *cs);
+void s390_pv_inject_reset_error(CPUState *cs, uint64_t *resp);
 uint64_t kvm_s390_pv_dmp_get_size_cpu(void);
 uint64_t kvm_s390_pv_dmp_get_size_mem_state(void);
 uint64_t kvm_s390_pv_dmp_get_size_completion_data(void);
@@ -63,12 +67,19 @@ static inline int s390_pv_vm_enable(void) { return 0; }
 static inline void s390_pv_vm_disable(void) {}
 static inline bool s390_pv_vm_try_disable_async(S390CcwMachineState *ms) { return false; }
 static inline int s390_pv_set_sec_parms(uint64_t origin, uint64_t length,
-                                        Error **errp) { return 0; }
-static inline int s390_pv_unpack(uint64_t addr, uint64_t size, uint64_t tweak) { return 0; }
+                                        Error **errp, uint16_t *pv_cmd,
+                                        uint16_t *pv_rc, uint16_t *pv_rrc)
+                                        { return 0; }
+static inline int s390_pv_unpack(uint64_t addr, uint64_t size,
+                                 uint64_t tweak, uint16_t *pv_cmd,
+                                 uint16_t *pv_rc, uint16_t *pv_rrc)
+                                 { return 0; }
 static inline void s390_pv_prep_reset(void) {}
-static inline int s390_pv_verify(void) { return 0; }
+static inline int s390_pv_verify(uint16_t *pv_cmd,
+                                 uint16_t *pv_rc,
+                                 uint16_t *pv_rrc) { return 0; }
 static inline void s390_pv_unshare(void) {}
-static inline void s390_pv_inject_reset_error(CPUState *cs) {};
+static inline void s390_pv_inject_reset_error(CPUState *cs, uint64_t *resp) {};
 static inline uint64_t kvm_s390_pv_dmp_get_size_cpu(void) { return 0; }
 static inline uint64_t kvm_s390_pv_dmp_get_size_mem_state(void) { return 0; }
 static inline uint64_t kvm_s390_pv_dmp_get_size_completion_data(void) { return 0; }
Signature · 1 line
2.49.0
#6 lore raw

Re: [PATCH v1 3/3] target/s390x: Return UVC cmd code, RC and RRC value when DIAG 308 Subcode 10 fails to enter secure mode

Quoted 19 lines · Gautam Gala

Extend DIAG308 subcode 10 to return the UVC RC, RRC and command code in bit positions 32-47, 16-31, and 0-15 of register R1 + 1 if the function does not complete successfully (in addition to the previously returned diag response code in bit position 47-63).

---
 hw/s390x/ipl.c             | 11 ++++++----
 hw/s390x/ipl.h             |  5 +++--
 hw/s390x/s390-virtio-ccw.c | 24 +++++++++++++++------
 target/s390x/kvm/pv.c      | 44 +++++++++++++++++++++++++-------------
 target/s390x/kvm/pv.h      | 27 ++++++++++++++++-------
 5 files changed, 76 insertions(+), 35 deletions(-)
hw/s390x/ipl.c +0 -0
index ce6f6078d7..4f3e3945f1 100644

hw/s390x/ipl.c static void s390_ipl_prepare_qipl(S390CPU *cpu) · line 680

Quoted 6 lines
hw/s390x/ipl.c +2 -1
@@ -676,7 +676,8 @@ static void s390_ipl_prepare_qipl(S390CPU *cpu)
     cpu_physical_memory_unmap(addr, len, 1, len);
 }
 
-int s390_ipl_prepare_pv_header(Error **errp)
+int s390_ipl_prepare_pv_header(Error **errp, uint16_t *pv_cmd,
+                               uint16_t *pv_rc, uint16_t *pv_rrc)

Error **errp should be the last argument. (multiple locations)

...

hw/s390x/ipl.c static void s390_ipl_prepare_qipl(S390CPU *cpu)

Quoted 7 lines
hw/s390x/ipl.c +7 -0
+struct diag308response {
+    uint16_t pv_cmd;
+    uint16_t pv_rrc;
+    uint16_t pv_rc;
+    uint16_t diag_rc;
+};
+

I think Qemu style wants you to use CamelCase here. See e.g 390CcwMachineState below

Quoted 7 lines

static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id,
Error **errp)
{

hw/s390x/ipl.c +0 -0
@@ -364,7 +371,10 @@ static void s390_machine_unprotect(S390CcwMachineState *ms)
     ram_block_discard_disable(false);
 }
 

...

hw/s390x/ipl.c static void s390_machine_reset(MachineState *machine, ResetType type) · line 465

Quoted 5 lines
hw/s390x/ipl.c +1 -0
@@ -452,6 +462,7 @@ static void s390_machine_reset(MachineState *machine, ResetType type)
 {
     S390CcwMachineState *ms = S390_CCW_MACHINE(machine);
     enum s390_reset reset_type;
+    struct diag308response resp;

Reverse Christmas tree.

Quoted 3 lines
hw/s390x/ipl.c +0 -0
     CPUState *cs, *t;
     S390CPU *cpu;
 

...