DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 55RJdnan1560072 Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 55RJdnan1560072 Authentication-Results: delorie.com; dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=Vn+ItNr9 X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 848833856260 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1751053187; bh=TK2f8di4UPNLhugoDP9kaZeUyaM758k0Sr+RWdRgHWM=; h=Date:To:Subject:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Vn+ItNr9kOP7bjQiqwxYKJAeeM1tqUBBfQNLS3Dat5BsLNhcTV6tA1au4Jf3/iNYQ W7PtophgNvuqbnGFCJLxl5gTIS8tilaEMQFXU1I87tjsqRrONV6XKhpYv1FmdhED12 o2htnEniuCWAxOU4M+V4xqQvfU3duxJEAUtC57c0= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB46D3858408 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EB46D3858408 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1751053162; cv=none; b=NnU+4z4k5GemfdzeV+0gypMg/p8jzW3gC818WBQ3YuFfBBCceQPd368Mp8sfRmfGqX323WgAnnaSMyANxuNcQu8QYBG2vlb9rfcmmmDpniK0iDiGeRe6Yy1NlHJgqgIyYKr/mGGQyXXglSoEMGtA9mCy6sgV6jbxdFYVx0MrZfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1751053162; c=relaxed/simple; bh=E3ruDY+ECVKk2q5vCbWQ/6gXPxz640jcS9SMn1ND4DI=; h=DKIM-Signature:Date:To:From:Subject:Message-ID:MIME-Version; b=PQSDKZwsy2THlNAMaicAvuMpktJ7ayZ7VsStnywNZ+J/GIU4g/THBPGmwRc2cE4w56iza4OqemQ48HATC2Fo+QOOAPdP/o9X8/iM9B6EIX+UYNdsl1KMwKwOGgwVDZVoey/RrE+Z442aeGZk96VLppSGWh6+ssMPBblA55WUZmI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB46D3858408 Date: Fri, 27 Jun 2025 19:39:17 +0000 To: cygwin AT cygwin DOT com Subject: Re: XSAVE-related segfaults observed under wine Message-ID: <8734bl3rfg.fsf@protonmail.com> In-Reply-To: References: <87bjq9457k DOT fsf AT protonmail DOT com> Feedback-ID: 112775352:user:proton X-Pm-Message-ID: eea3f96a55f474eaaee9b13fbc8ac31e32af2ac6 MIME-Version: 1.0 X-BeenThere: cygwin AT cygwin DOT com X-Mailman-Version: 2.1.30 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Pip Cet via Cygwin Reply-To: Pip Cet Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" "Corinna Vinschen" writes: > Hi Pip, > > On Jun 27 14:41, Pip Cet via Cygwin wrote: >> My suggested fix is to align the value in %rbx to the next 64-byte >> multiple after the "cpuid" instruction is executed, by performing the >> assembler equivalent of $rbx += 63; $rbx &= -64; I tried this: >> >> diff --git a/winsup/cygwin/scripts/gendef b/winsup/cygwin/scripts/gendef >> index 861a2405b..d681fde3f 100755 >> --- a/winsup/cygwin/scripts/gendef >> +++ b/winsup/cygwin/scripts/gendef >> @@ -232,6 +232,8 @@ sigdelayed: >> movl \$0x0d,%eax >> xorl %ecx,%ecx >> cpuid # get necessary space for xsave >> + addq \$63, %rbx >> + andq \$-64, %rbx # align to next 64-byte multiple > > what about a oneliner instead by just aligning rsp? As in > >> movq %rbx,%rcx >> addq \$0x48,%rbx # 0x18 for alignment, 0x30 for additional space >> subq %rbx,%rsp > > + andq \$-64, %rsp We could do that, but it wouldn't be a one-liner, I'm afraid. Two reasons: 1. It's 0x30(%rsp) that needs to be aligned, not %rsp. Entirely solvable by making the extra space allocation 0x40 bytes instead of 0x30 bytes. 2. The current register push/pop code needs the stack pointer to be the same after the xsave/xrstor code as it was before, so we'd need to stash the old stack pointer somewhere. We could use the 16 bytes we just extended the extra space area by, I suppose, but I tried changing it and must have messed up somewhere; it didn't work. > Would you mind to create a git send-email compatible patch with a nice > commit message basically duplicating your great description of the > problem and matching "Fixes:" and "Signed-off-by:" lines? Sure; while I have tested this patch, it was all in the somewhat unusual setting of running the msys2-docker-experimental image: I built the new msys-2.0.dll on one Linux system (booted with "nopku"), then copied the DLL over to another Linux system (with PKU support), confirmed it fixes the bug when starting bash, and rebuilt the DLL on the PKU-enabled system from scratch. Testing on Windows should obviously happen before considering this patch further. The only minor change is that %ecx now contains the potentially-unaligned size; this means we clear only bytes that xsave64 might write to, not the alignment padding. Please let me know if anything needs changing! Thanks! Pip From f81fb23b9bd4db9af9942c1bf9059a62849d4b80 Mon Sep 17 00:00:00 2001 From: Pip Cet Subject: [PATCH] Cygwin: Fix segfault when XSAVE area sizes are unaligned On recent AMD and Intel CPUs, the PKU/PKRU feature reports an XSAVE storage area of just 8 bytes. This is exposed to Cygwin code when we run in Wine under Linux. The xsave64 instruction requires a memory area aligned to 64 bytes; in the setting above, we would no longer meet that requirement, causing segfaults and abnormal program termination. This fix aligns the size of the structure we allocate on the stack before performing an xsave64 to the next 64-byte multiple; the old code already guarantees that structures of such size are aligned properly for xsave64. This fixes the issue on Linux and hopefully avoids it on future Windows systems which expose such features. Fixes: c607889824 ("Cygwin: sigfe: Fix a bug that signal handler destroys fpu states") Signed-off-by: Pip Cet --- winsup/cygwin/scripts/gendef | 2 ++ 1 file changed, 2 insertions(+) diff --git a/winsup/cygwin/scripts/gendef b/winsup/cygwin/scripts/gendef index 861a2405b..6328fe2fb 100755 --- a/winsup/cygwin/scripts/gendef +++ b/winsup/cygwin/scripts/gendef @@ -233,6 +233,8 @@ sigdelayed: xorl %ecx,%ecx cpuid # get necessary space for xsave movq %rbx,%rcx + addq \$63, %rbx + andq \$-64, %rbx # align to next 64-byte multiple addq \$0x48,%rbx # 0x18 for alignment, 0x30 for additional space subq %rbx,%rsp movl %ebx,0x24(%rsp) -- 2.50.0 -- Problem reports: https://cygwin.com/problems.html FAQ: https://cygwin.com/faq/ Documentation: https://cygwin.com/docs.html Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple