From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E126F606A31A for ; Tue, 1 Dec 2026 08:35:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=R4g8/90Q Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xYJKL-0007di-VG; Tue, 01 Dec 2026 03:35:06 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xYJKK-0007dH-2S; Tue, 01 Dec 2026 03:35:04 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xYJKH-0000PC-H6; Tue, 01 Dec 2026 03:35:03 -0500 Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B12ZLT2599849; Tue, 1 Dec 2026 08:34:47 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:reply-to:subject:to; s=pp1; bh=wuASIsSmbqGEgQVa+qLfh9k6lXh1plBRqXUtduUH8TM=; b=R4g8/90Qvbvj kEsI//Koda6jZVbtN2P3G7TrFZFczTGREfwxxImOidJ5i74ZekDLrlrrQLvGA6Jb W4Yg7mQzqkw9o8bPtU2gKoEXjWsKBqeyXMS9weyTyVmgpH85AagJVvHDNCqpYV08 c+bt8FK70v84MqBfNVrYtt2YKQh1j0F2G5k0PsWM45uMsTB/0cwwukcTCleRsgKr JGiiOuIhXoU8CLEzgmuwuW9lfirKZL9e6+q+DZ/AqfGoRHzdPZbjasfW2eRRPznT 8a754Ia5KAg0+8GdyBT7yjc+gepa21lotCsO9ebjzcc19wLmw+coZi1YLZYZEvza CY0Epq7V8A== Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4j7mckbhd3-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 01 Dec 2026 08:34:46 +0000 (GMT) Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.18.1.11/8.18.1.11) with ESMTP id 6B17jMRk3950061; Tue, 1 Dec 2026 08:34:45 GMT Received: from smtprelay02.dal12v.mail.ibm.com ([172.16.1.4]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 4j8bggg7bt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 01 Dec 2026 08:34:45 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (smtpav03.wdc07v.mail.ibm.com [10.39.53.230]) by smtprelay02.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 6B18YipI10093304 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 1 Dec 2026 08:34:44 GMT Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6E8705805D; Tue, 1 Dec 2026 08:34:44 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2B49F5805C; Tue, 1 Dec 2026 08:34:43 +0000 (GMT) Received: from ltc.linux.ibm.com (unknown [9.5.196.140]) by smtpav03.wdc07v.mail.ibm.com (Postfix) with ESMTP; Tue, 1 Dec 2026 08:34:43 +0000 (GMT) X-Archived-By: MIME-Version: 1.0 Date: Tue, 01 Dec 2026 09:34:42 +0100 From: Harald Freudenberger To: Ilya Leoshkevich Cc: Andrew Holden , 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 Mail-Reply-To: freude@linux.ibm.com In-Reply-To: <829af07e-9d11-4ad5-9818-94a12a4a1407@linux.ibm.com> References: <20261120131443.61606-1-andy0x48@linux.ibm.com> <20261120131443.61606-4-andy0x48@linux.ibm.com> <829af07e-9d11-4ad5-9818-94a12a4a1407@linux.ibm.com> Message-ID: <59cc7cf7db5cc00e07f5e2c87305cd2c@linux.ibm.com> X-Sender: freude@linux.ibm.com Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-Spam-Info: AW1haW4tMjYxMjAxMDAzNCBTYWx0ZWRfX1YgeOs/ybLRf gytLr93cm+2t5y8TnCftB7VSoW5jW2HG+nPL4wbYEDmXjeraRB9zhzZiW8qVNSCOm/mOX0AaLSe KZLDri1ywHs6i3eiLx25xeciGkLiexE= X-Authority-Analysis: v=2.4 cv=MtfHeGae c=1 sm=1 tr=0 ts=6b0e86a6 cx=c_pps a=AfN7/Ok6k8XGzOShvHwTGQ==:117 a=AfN7/Ok6k8XGzOShvHwTGQ==:17 a=kj9zAlcOel0A:10 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=Y2IxJ9c9Rs8Kov3niI8_:22 a=VnNF1IyMAAAA:8 a=80qoFcGU_rpTANWBoHYA:9 a=CjuIK1q_8ugA:10 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjAxMDAzNCBTYWx0ZWRfX4B76Yh66iyqZ OQ0J8dYs0e0XSZxqbaiKav33C0Jj3/nJOgRNXTr/FSz/GEiXoSb3SMj4mFmXTNIb9o/1g73GFd6 64jOWVvZcOt6PxpaxCd0QFslV/onK564bfbgPzbOGJBXrEdwr0zFCjX9BI2r7lFuNN+9sm6R/4t xMu0URGeu7EoWHl1gCG4qOHq1cgRD11TCvast+qy2jf/V/QTfrPKEiSLf0//Ltsj8F8Y95+bW3+ 0ZZKDKsFjUVjLfUFASBQkg6DVbE2f05et2UQqGzggCWdEKvAI92A+PnNyXt+bH9GoS/TfvCH+Q4 rqUCOYUh+858qvwSLcKqdfU4gL8lxOA4Er9ZWYFtn7nSrkijuCRtXhEuVHjnZKvteSM7h9/wgPF SAKOuIpuk1Y/PV+VggyCeanngRYhafmmLvIEhcJnPnxrhqdriwyJ2+THHqqb19G9VKzvh75p2H6 DZl+E8jUN+V7EirUA7A== X-Proofpoint-GUID: wmiRCqI9Xcj0P9ZUZHiPGGB1mDN8w-vD X-Proofpoint-ORIG-GUID: wmiRCqI9Xcj0P9ZUZHiPGGB1mDN8w-vD X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-01_01,2026-11-30_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 bulkscore=0 malwarescore=0 spamscore=0 phishscore=0 clxscore=1011 priorityscore=1501 lowpriorityscore=0 suspectscore=0 impostorscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612010034 Received-SPF: pass client-ip=148.163.158.5; envelope-from=freude@linux.ibm.com; helo=mx0b-001b2d01.pphosted.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: freude@linux.ibm.com Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On 2026-11-25 17:41, Ilya Leoshkevich wrote: > On 11/20/26 14:14, Andrew Holden wrote: >> From: Harald Freudenberger >> >> 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 >> Signed-off-by: Andrew Holden >> --- >> 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 >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#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.