@samitouri / QOSamiQemu / commits / 65b61969a7

s390x/diag: Implement DIAG 320 subcode 1

DIAG 320 subcode 1 provides information needed to determine the amount of storage to store one or more certificates from the certificate store. Upon successful completion, this subcode returns information of the current cert store, such as the number of certificates stored and allowed in the cert store, amount of space may need to be allocate to store a certificate, etc for verification-certificate blocks (VCBs). The subcode value is denoted by setting the left-most bit of an 8-byte field. The verification-certificate-storage-size block (VCSSB) contains the output data when the operation completes successfully. A VCSSB length of 4 indicates that no certificate are available in the cert store. Signed-off-by: Zhuoying Cai <zycai@linux.ibm.com> Reviewed-by: Farhan Ali <alifm@linux.ibm.com> Reviewed-by: Collin Walling <walling@linux.ibm.com> Reviewed-by: Eric Farman <farman@linux.ibm.com> Link: https://lore.kernel.org/qemu-devel/20260730214624.2328883-8-zycai@linux.ibm.com Signed-off-by: Eric Farman <farman@linux.ibm.com>

Zhuoying Cai committed Jul 30, 2026 at 17:45 UTC 65b61969a7ad62d6a6ca490ecc340f99503ae9d2
4 files changed +130 -3
docs/specs/s390x-secure-ipl.rst
+12
@@ -30,3 +30,15 @@ Subcode 0 - query installed subcodes
30 Returns a 256-bit installed subcodes mask (ISM) stored in the installed
31 subcodes block (ISB). This mask indicates which subcodes are currently
32 installed and available for use.
33 +
34 +Subcode 1 - query verification certificate storage information
35 + Provides the information required to determine the amount of memory needed
36 + to store one or more verification-certificates (VCs) from the certificate
37 + store (CS).
38 +
39 + Upon successful completion, this subcode returns various storage size values
40 + for verification-certificate blocks (VCBs).
41 +
42 + The output is returned in the verification-certificate-storage-size block
43 + (VCSSB). A VCSSB length of 4 indicates that no certificates are available
44 + in the CS.
hw/s390x/cert-store.h
+1 -2
@@ -11,10 +11,9 @@
11 #define HW_S390_CERT_STORE_H
12
13 #include "hw/s390x/ipl/qipl.h"
14 +#include "hw/s390x/ipl/diag320.h"
15 #include "crypto/x509-utils.h"
16
16 -#define CERT_NAME_MAX_LEN 64
17 -
17 #define CERT_KEY_ID_LEN QCRYPTO_HASH_DIGEST_LEN_SHA256
18 #define CERT_HASH_LEN QCRYPTO_HASH_DIGEST_LEN_SHA256
19
include/hw/s390x/ipl/diag320.h
+57
@@ -11,10 +11,67 @@
11 #define S390X_DIAG320_H
12
13 #define DIAG_320_SUBC_QUERY_ISM 0
14 +#define DIAG_320_SUBC_QUERY_VCSI 1
15
16 #define DIAG_320_RC_OK 0x0001
17 #define DIAG_320_RC_NOT_SUPPORTED 0x0102
18 +#define DIAG_320_RC_INVAL_VCSSB_LEN 0x0202
19
20 #define DIAG_320_ISM_QUERY_SUBCODES 0x80000000
21 +#define DIAG_320_ISM_QUERY_VCSI 0x40000000
22 +
23 +#define VCSSB_NO_VC 4
24 +#define VCSSB_LEN_VALID 128
25 +
26 +#define CERT_NAME_MAX_LEN 64
27 +
28 +struct VCStorageSizeBlock {
29 + uint32_t length;
30 + uint8_t reserved0[3];
31 + uint8_t version;
32 + uint32_t reserved1[6];
33 + uint16_t total_vc_ct;
34 + uint16_t max_vc_ct;
35 + uint32_t reserved3[11];
36 + uint32_t max_single_vcb_len;
37 + uint32_t total_vcb_len;
38 + uint32_t reserved4[10];
39 +};
40 +typedef struct VCStorageSizeBlock VCStorageSizeBlock;
41 +
42 +struct VCEntryHeader {
43 + uint32_t len;
44 + uint8_t flags;
45 + uint8_t key_type;
46 + uint16_t cert_idx;
47 + uint8_t name[CERT_NAME_MAX_LEN];
48 + uint8_t format;
49 + uint8_t reserved0;
50 + uint16_t keyid_len;
51 + uint8_t reserved1;
52 + uint8_t hash_type;
53 + uint16_t hash_len;
54 + uint32_t reserved2;
55 + uint32_t cert_len;
56 + uint32_t reserved3[2];
57 + uint16_t hash_offset;
58 + uint16_t cert_offset;
59 + uint32_t reserved4[7];
60 +};
61 +typedef struct VCEntryHeader VCEntryHeader;
62 +
63 +struct VCBlockHeader {
64 + uint32_t in_len;
65 + uint32_t reserved0;
66 + uint16_t first_vc_index;
67 + uint16_t last_vc_index;
68 + uint32_t reserved1[5];
69 + uint32_t out_len;
70 + uint8_t reserved2[4];
71 + uint16_t stored_ct;
72 + uint16_t remain_ct;
73 + uint32_t reserved3[5];
74 +};
75 +typedef struct VCBlockHeader VCBlockHeader;
76
77 #endif
target/s390x/diag.c
+60 -1
@@ -205,12 +205,53 @@ out:
205 }
206 }
207
208 +static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1,
209 + uintptr_t ra, S390IPLCertificateStore *cs)
210 +{
211 + g_autofree VCStorageSizeBlock *vcssb = NULL;
212 +
213 + vcssb = g_new0(VCStorageSizeBlock, 1);
214 + if (s390_cpu_virt_mem_read(cpu, addr, r1, vcssb, sizeof(*vcssb))) {
215 + s390_cpu_virt_mem_handle_exc(cpu, ra);
216 + return -1;
217 + }
218 +
219 + if (be32_to_cpu(vcssb->length) != VCSSB_LEN_VALID) {
220 + return DIAG_320_RC_INVAL_VCSSB_LEN;
221 + }
222 +
223 + if (!cs->count) {
224 + vcssb->length = cpu_to_be32(VCSSB_NO_VC);
225 + } else {
226 + vcssb->version = 0;
227 + vcssb->total_vc_ct = cpu_to_be16(cs->count);
228 + vcssb->max_vc_ct = cpu_to_be16(MAX_CERTIFICATES);
229 + vcssb->max_single_vcb_len = cpu_to_be32(sizeof(VCBlockHeader) +
230 + sizeof(VCEntryHeader) +
231 + cs->largest_cert_size);
232 + vcssb->total_vcb_len = cpu_to_be32(sizeof(VCBlockHeader) +
233 + cs->count * sizeof(VCEntryHeader) +
234 + cs->total_bytes);
235 + }
236 +
237 + if (s390_cpu_virt_mem_write(cpu, addr, r1, vcssb, be32_to_cpu(vcssb->length))) {
238 + s390_cpu_virt_mem_handle_exc(cpu, ra);
239 + return -1;
240 + }
241 + return DIAG_320_RC_OK;
242 +}
243 +
244 +QEMU_BUILD_BUG_MSG(sizeof(VCStorageSizeBlock) != VCSSB_LEN_VALID,
245 + "size of VCStorageSizeBlock is wrong");
246 +
247 void handle_diag_320(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra)
248 {
249 S390CPU *cpu = env_archcpu(env);
250 + S390IPLCertificateStore *cs = s390_ipl_get_certificate_store();
251 uint64_t subcode = env->regs[r3];
252 uint64_t addr = env->regs[r1];
253 uint32_t ism_word0;
254 + int rc;
255
256 if (env->psw.mask & PSW_MASK_PSTATE) {
257 s390_program_interrupt(env, PGM_PRIVILEGED, ra);
@@ -231,7 +272,8 @@ void handle_diag_320(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra)
272 * but the current set of subcodes can fit within a single word
273 * for now.
274 */
234 - ism_word0 = cpu_to_be32(DIAG_320_ISM_QUERY_SUBCODES);
275 + ism_word0 = cpu_to_be32(DIAG_320_ISM_QUERY_SUBCODES |
276 + DIAG_320_ISM_QUERY_VCSI);
277
278 if (s390_cpu_virt_mem_write(cpu, addr, r1, &ism_word0, sizeof(ism_word0))) {
279 s390_cpu_virt_mem_handle_exc(cpu, ra);
@@ -240,6 +282,23 @@ void handle_diag_320(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra)
282
283 env->regs[r1 + 1] = DIAG_320_RC_OK;
284 break;
285 + case DIAG_320_SUBC_QUERY_VCSI:
286 + if (addr & 0x7) {
287 + s390_program_interrupt(env, PGM_SPECIFICATION, ra);
288 + return;
289 + }
290 +
291 + if (!diag_parm_addr_valid(addr, sizeof(VCStorageSizeBlock), true)) {
292 + s390_program_interrupt(env, PGM_ADDRESSING, ra);
293 + return;
294 + }
295 +
296 + rc = handle_diag320_query_vcsi(cpu, addr, r1, ra, cs);
297 + if (rc == -1) {
298 + return;
299 + }
300 + env->regs[r1 + 1] = rc;
301 + break;
302 default:
303 env->regs[r1 + 1] = DIAG_320_RC_NOT_SUPPORTED;
304 break;