From: Harald Freudenberger <freude@linux.ibm.com>
To: Ilya Leoshkevich <iii@linux.ibm.com>
Cc: Andrew Holden <andy0x48@linux.ibm.com>,
qemu-devel@nongnu.org, qemu-s390x@nongnu.org,
richard.henderson@linaro.org, david@kernel.org,
cohuck@redhat.com, farman@linux.ibm.com, mjrosato@linux.ibm.com
Subject: Re: [PATCH v2 3/3] tests/s390x/tcg: Add cpacf clear key register update tests
Date: Tue, 01 Dec 2026 09:34:42 +0100 [thread overview]
Message-ID: <59cc7cf7db5cc00e07f5e2c87305cd2c@linux.ibm.com> (raw)
In-Reply-To: <829af07e-9d11-4ad5-9818-94a12a4a1407@linux.ibm.com>
On 2026-11-25 17:41, Ilya Leoshkevich wrote:
> On 11/20/26 14:14, Andrew Holden wrote:
>> From: Harald Freudenberger <freude@linux.ibm.com>
>>
>> Add simple test cases for the 4 clear key cpacf implementation
>> functions cpacf_aes_ecb(), cpacf_aes_cbc(), cpacf_aes_ctr() and
>> cpacf_aes_xts() which shall test if the register update and memory
>> update during processing of data within the cpacf instruction (km,
>> kmc, kmctr) emulation is correct. See details about how the test cases
>> are designed and actually work testcase code. Note that as there is a
>> signal handler evaluating the registers on the enforced address
>> violation the cpacf.h inline function needs to use pinned registers.
>>
>> Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
>> Signed-off-by: Andrew Holden <andy0x48@linux.ibm.com>
>> ---
>> tests/tcg/s390x/cpacf-km-ecb-fault.c | 368 +++++++++++++++++
>> tests/tcg/s390x/cpacf-km-xts-fault.c | 511
>> ++++++++++++++++++++++++
>> tests/tcg/s390x/cpacf-kmc-cbc-fault.c | 472 ++++++++++++++++++++++
>> tests/tcg/s390x/cpacf-kmctr-ctr-fault.c | 414 +++++++++++++++++++
>> tests/tcg/s390x/cpacf.h | 103 +++--
>> tests/tcg/s390x/meson.build | 4 +
>> 6 files changed, 1846 insertions(+), 26 deletions(-)
>> create mode 100644 tests/tcg/s390x/cpacf-km-ecb-fault.c
>> create mode 100644 tests/tcg/s390x/cpacf-km-xts-fault.c
>> create mode 100644 tests/tcg/s390x/cpacf-kmc-cbc-fault.c
>> create mode 100644 tests/tcg/s390x/cpacf-kmctr-ctr-fault.c
>>
>> diff --git a/tests/tcg/s390x/cpacf-km-ecb-fault.c
>> b/tests/tcg/s390x/cpacf-km-ecb-fault.c
>> new file mode 100644
>> index 0000000000..3fe7df1f04
>> --- /dev/null
>> +++ b/tests/tcg/s390x/cpacf-km-ecb-fault.c
>> @@ -0,0 +1,368 @@
>> +/*
>> + * SPDX-License-Identifier: GPL-2.0-or-later
>> + *
>> + * Test for register update correctness in cpacf_aes_ecb (KM AES-128
>> ECB).
>> + *
>> + * All the CPACF instructions are designed in a way that on exit
>> + * the registers are updated so that on incomplete processing
>> (partial
>> + * completion) the instruction can just be re-invoked. The QEMU
>> emulation
>> + * of the CPACF instructions needs to obey that rule as well.
>> + * This testcase checks the correct update of the register values by
>> + * triggering a page protection fault in the middle of the data. The
>> + * signal handler then checks for the expected register values. After
>> + * that the page is enabled for reading, causing the OS to restart
>> the
>> + * instruction which continues to process the remaining data. Finally
>> + * the encryption result is checked to match the expectations.
>> + *
>> + * Test sequence:
>> + * 1. Allocate two contiguous, page-aligned source pages filled with
>> + * pseudo-random data and two matching destination pages.
>> + * 2. Reference run: encrypt both source pages with cpacf_km AES-128
>> ECB
>> + * into the reference destination buffer (no faults).
>> + * 3. Fault run:
>> + * a. Protect the second source page (PROT_NONE).
>> + * b. Install a SIGSEGV handler.
>> + * c. Call cpacf_km over both pages (2 * PAGE_SIZE bytes).
>> + * d. On the SIGSEGV verify:
>> + * - si_addr lies within the second source page.
>> + * - R4 (src address) equals exactly the start of the second
>> source
>> + * page (all blocks of page 1 were already consumed).
>> + * - R5 (remaining src length) equals exactly PAGE_SIZE.
>> + * - R2 (dst address) equals exactly dst_fault + PAGE_SIZE.
>> + * e. Restore PROT_READ on the second source page and return from
>> the
>> + * handler so KM re-executes from the updated registers and
>> finishes.
>> + * 4. Compare the fault-run output to the reference output; they
>> must match.
>> + *
>> + * cpacf_km() in cpacf.h uses pinned registers R2/R4/R5 for its
>> operands,
>> + * so the signal handler can read them reliably via ucontext_t.
>> + *
>> + * Usage: cpacf-km-ecb-fault [-v]
>> + * -v verbose: print details about each step and every register
>> check.
>> + */
>> +
>> +#include <signal.h>
>> +#include <stdio.h>
>> +#include <stdint.h>
>> +#include <stdlib.h>
>> +#include <string.h>
>> +#include <sys/mman.h>
>> +#include <ucontext.h>
>> +#include <unistd.h>
>> +#include "cpacf.h"
>> +
>> +/* AES-128 key used for all encryptions in this test */
>> +static const uint8_t aes128key[16] = {
>> + 0xed, 0xfd, 0xb2, 0x57, 0xcb, 0x37, 0xcd, 0xf1,
>> + 0x82, 0xc5, 0x45, 0x5b, 0x0c, 0x0e, 0xfe, 0xbb
>> +};
>> +
>> +/* Page size, determined at runtime */
>> +static long page_size;
>> +
>> +/* Verbose flag: set by -v on the command line */
>> +static int verbose;
>> +
>> +/*
>> + * State shared between main() and the SIGSEGV handler.
>> + * volatile prevents the compiler from optimising away accesses.
>> + */
>> +static volatile int sigsegv_count; /* how many times
>> handler fired */
>> +static volatile int sigsegv_pass; /* 1 = all register
>> checks fine */
>> +static volatile unsigned long expected_src; /* exact expected R4
>> value */
>> +static volatile unsigned long expected_dst; /* exact expected R2
>> value */
>> +
>> +/*
>> + * SIGSEGV handler.
>> + *
>> + * At fault time the KM instruction has already advanced its operand
>> registers
>> + * past all fully processed blocks. For the second-page fault:
>> + * R2 = dst_fault + PAGE_SIZE (destination pointer into second
>> output page)
>> + * R4 = src_pages + PAGE_SIZE (source pointer at start of second
>> source page)
>> + * R5 = PAGE_SIZE (exactly one page of source data
>> remaining)
>> + * Note that this requires using the inline function cpacf_km() from
>> cpacf.h
>> + * which uses pinned registers for the parameters and this has to be
>> in
>> + * alignment with the code here.
>> + *
>> + * After verifying these values the handler restores read permission
>> on the
>> + * second source page and returns. The signal return mechanism
>> re-executes
>> + * the KM instruction from the updated register state, completing the
>> run.
>> + */
>> +static void sigsegv_handler(int sig, siginfo_t *si, void *uctx_arg)
>> +{
>> + /*
>> + * No printf within signal handler, but write() can be used. So
>> this
>> + * little sig_printf() macro is a replacement to fprintf(stderr,
>> ...)
>> + */
>> +#define sig_printf(...) do { \
>> + char printfbuf[256]; \
>> + int len = snprintf(printfbuf, sizeof(printfbuf),
>> __VA_ARGS__); \
>
> I think this also allocates memory and thus is also not in the
> "man signal-safety" list.
>
Yep you are right. Looks like there is no printf style like function
allowed within a signal hander. Let me see how to rework this...
>> + write(STDERR_FILENO, printfbuf, len); \
>> + } while (0)
>> +
>> + ucontext_t *uc = (ucontext_t *)uctx_arg;
>> + unsigned long r2, r4, r5;
>> + int pass = 1;
>> +
>> + sigsegv_count++;
>> +
>> + /*
>> + * On s390x, glibc's mcontext_t exposes the GPRs as gregs[].
>> + */
>> + r2 = uc->uc_mcontext.gregs[2]; /* KM destination address
>> register */
>> + r4 = uc->uc_mcontext.gregs[4]; /* KM source address register
>> */
>> + r5 = uc->uc_mcontext.gregs[5]; /* KM source length register
>> */
>> +
>> + if (verbose) {
>> + sig_printf(" SIGSEGV #%d: si_addr=0x%lx\n",
>> + sigsegv_count, (unsigned long)si->si_addr);
>> + sig_printf(" R2 (dst addr): 0x%lx (expected 0x%lx)\n",
>> + r2, expected_dst);
>> + sig_printf(" R4 (src addr): 0x%lx (expected 0x%lx)\n",
>> + r4, expected_src);
>> + sig_printf(" R5 (src len): 0x%lx (expected 0x%lx =
>> PAGE_SIZE)\n",
>> + r5, (unsigned long)page_size);
>> + }
>> +
>> + /* R4 must point exactly to the start of the second source page
>> */
>> + if (r4 != expected_src) {
>> + sig_printf("FAIL: R4 (src addr) = 0x%lx, expected 0x%lx\n",
>> + r4, expected_src);
>> + pass = 0;
>> + } else if (verbose) {
>> + sig_printf(" R4 check: PASS\n");
>> + }
>> +
>> + /* R5 must equal exactly PAGE_SIZE (one full page remaining) */
>> + if (r5 != (unsigned long)page_size) {
>> + sig_printf("FAIL: R5 (src len) = 0x%lx, expected PAGE_SIZE =
>> 0x%lx\n",
>> + r5, (unsigned long)page_size);
>> + pass = 0;
>> + } else if (verbose) {
>> + sig_printf(" R5 check: PASS\n");
>> + }
>> +
>> + /* R2 must point exactly to dst_fault + PAGE_SIZE */
>> + if (r2 != expected_dst) {
>> + sig_printf("FAIL: R2 (dst addr) = 0x%lx, expected 0x%lx\n",
>> + r2, expected_dst);
>> + pass = 0;
>> + } else if (verbose) {
>> + sig_printf(" R2 check: PASS\n");
>> + }
>> +
>> + sigsegv_pass = pass;
>> +
>> + if (verbose) {
>> + sig_printf(" restoring PROT_READ on page at 0x%lx, resuming
>> KM\n",
>> + expected_src);
>> + }
>> +
>> + /* Restore read access so the restarted KM can read the second
>> page */
>> + mprotect((void *)expected_src, page_size, PROT_READ);
>> +
>> +#undef sig_printf
>> +}
>> +
>> +int main(int argc, char *argv[])
>> +{
>> + struct sigaction sa, old_sa;
>> + uint8_t *src_pages, *dst_ref, *dst_fault;
>> + uint8_t param[16]; /* AES-128 parameter block (key only for
>> ECB) */
>> + unsigned long cc;
>> + int rc = 0;
>> +
>> + if (argc == 2 && strcmp(argv[1], "-v") == 0) {
>> + verbose = 1;
>> + } else if (argc > 1) {
>> + fprintf(stderr, "usage: %s [-v]\n", argv[0]);
>> + return EXIT_FAILURE;
>> + }
>
> I would make it always verbose and drop parameterization.
> Suppose it fails it GitLab CI; we want to have as much data as
> possible.
>
Yes, will do.
>> +
>> + page_size = getpagesize();
>> +
>> + if (verbose) {
>> + printf("page_size = %ld (0x%lx)\n", page_size, page_size);
>> + }
>> +
>> + /*
>> + * Allocate two contiguous page-aligned source pages and fill
>> them with
>> + * a deterministic pseudo-random pattern so the test is
>> reproducible.
>> + */
>> + src_pages = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
>> + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
>> + if (src_pages == MAP_FAILED) {
>> + perror("mmap src_pages");
>> + return EXIT_FAILURE;
>> + }
>> + for (long i = 0; i < 2 * page_size; i++) {
>> + src_pages[i] = (uint8_t)(i * 37 + 13);
>> + }
>> +
>> + /* Reference destination: two contiguous pages, initially zeroed
>> */
>> + dst_ref = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
>> + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
>> + if (dst_ref == MAP_FAILED) {
>> + perror("mmap dst_ref");
>> + munmap(src_pages, 2 * page_size);
>> + return EXIT_FAILURE;
>> + }
>> +
>> + /* Fault-run destination: two contiguous pages, initially zeroed
>> */
>> + dst_fault = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
>> + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
>> + if (dst_fault == MAP_FAILED) {
>> + perror("mmap dst_fault");
>> + munmap(src_pages, 2 * page_size);
>> + munmap(dst_ref, 2 * page_size);
>> + return EXIT_FAILURE;
>> + }
>> +
>> + if (verbose) {
>> + printf("src_pages: [0x%lx, 0x%lx) page1=[0x%lx, 0x%lx)
>> page2=[0x%lx, 0x%lx)\n",
>> + (unsigned long)src_pages,
>> + (unsigned long)(src_pages + 2 * page_size),
>> + (unsigned long)src_pages,
>> + (unsigned long)(src_pages + page_size),
>> + (unsigned long)(src_pages + page_size),
>> + (unsigned long)(src_pages + 2 * page_size));
>> + printf("dst_ref: [0x%lx, 0x%lx)\n",
>> + (unsigned long)dst_ref,
>> + (unsigned long)(dst_ref + 2 * page_size));
>> + printf("dst_fault: [0x%lx, 0x%lx)\n",
>> + (unsigned long)dst_fault,
>> + (unsigned long)(dst_fault + 2 * page_size));
>> + }
>
> The tests have quite a lot of duplication; I understand that sometimes
> things are subtly different, e.g., tweak vs no-tweak versions, but
> maybe
> there is still some chance to share at least some code between them?
That's always a trade-off between clear and understandable code and
code duplication. In general I prefer more readable and cleaner code
over highly optimized and duplicate free code. As we are now in the
area of sashiko these AI tools do a very good job on keeping similar
code together.
>
>
> The cover letter mentions this already, so - can we have similar tests
> for cpacf_paes_xts() and friends?
>
As of now we focus on the clear key algorithms. And honestly, the
treating
of the memory and registers in both clear key and protected key
algorithm
functions is the same. However, when done with the clear key part, then
let's extend this to the protected key things - either in the same tests
or have new tests - whatever fits better.
prev parent reply other threads:[~2026-12-01 8:35 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 13:14 [PATCH v2 0/3] Fix AES/PAES CPACF register update on faults Andrew Holden
2026-11-20 13:14 ` [PATCH v2 1/3] target/s390x: export access_prepare() and byte accessors Andrew Holden
2026-11-20 13:14 ` [PATCH v2 2/3] target/s390x/tcg: update regs and IV/tweak per block in CPACF AES/PAES Andrew Holden
2026-11-25 16:32 ` Ilya Leoshkevich
2026-11-26 16:03 ` Andrew Holden
2026-11-20 13:14 ` [PATCH v2 3/3] tests/s390x/tcg: Add cpacf clear key register update tests Andrew Holden
2026-11-25 16:41 ` Ilya Leoshkevich
2026-12-01 8:34 ` Harald Freudenberger [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=59cc7cf7db5cc00e07f5e2c87305cd2c@linux.ibm.com \
--to=freude@linux.ibm.com \
--cc=andy0x48@linux.ibm.com \
--cc=cohuck@redhat.com \
--cc=david@kernel.org \
--cc=farman@linux.ibm.com \
--cc=iii@linux.ibm.com \
--cc=mjrosato@linux.ibm.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-s390x@nongnu.org \
--cc=richard.henderson@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.