Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9E41BCA0EDC for ; Thu, 14 Aug 2025 20:45:12 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.1082503.1442331 (Exim 4.92) (envelope-from ) id 1umeke-0008T7-He; Thu, 14 Aug 2025 20:40:44 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1082503.1442331; Thu, 14 Aug 2025 20:40:44 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1umeke-0008T0-Dq; Thu, 14 Aug 2025 20:40:44 +0000 Received: by outflank-mailman (input) for mailman id 1082503; Thu, 14 Aug 2025 20:40:43 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1umekd-0008Su-4P for xen-devel@lists.xenproject.org; Thu, 14 Aug 2025 20:40:43 +0000 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [2a00:1450:4864:20::435]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id f113e329-794e-11f0-b898-0df219b8e170; Thu, 14 Aug 2025 22:40:40 +0200 (CEST) Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-3b9dc5c6521so767803f8f.1 for ; Thu, 14 Aug 2025 13:40:40 -0700 (PDT) Received: from [192.168.1.183] (host-195-149-20-212.as13285.net. [195.149.20.212]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-45a1c6bc932sm36123625e9.3.2025.08.14.13.40.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 14 Aug 2025 13:40:39 -0700 (PDT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f113e329-794e-11f0-b898-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1755204040; x=1755808840; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=EM+HNoQBt+VGaLjN4m1AxiSkl0/PgB3AVdnRqyt9mYU=; b=PqDIHu47e6VsnmudeJMFNm1GC/LowgxfeDIqjpijG4+SkCVp8TW140DEkAwlqFio8p CU7q+b0TSTAa921vm8QWOS1UwFdu/Zxz5iMmcpTU9i+FG8r2iI+fLTj4gGAuXkWO8MCW EYTMPtxdQg8UKFDftt1q2gWnhEiCe/YViZZ5Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755204040; x=1755808840; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=EM+HNoQBt+VGaLjN4m1AxiSkl0/PgB3AVdnRqyt9mYU=; b=cd7Q/au6wlbcqVjsZ5v22geQXGj2lHcazDTEWsuk/Vl6+KzKfAVSJ/GFxk3lGPuMky vQ6A6ur4XF8PWDyMHxEnDvpJVWwMioB2nlYL3Vx+XJvUhvftaBca0AQrvigT1dXSBLaw O+pCxlG/Ho/F5VrQKs23HwzAUPBQHh2UGHgBJVvl2jV/0aQ5+pLjYFeaBO7i4fFCp+n/ /QUuILpi89mOA32MPxvyZi2Vp/EH8O9JmcpF9+515QxeD0dz8tyZnEEC0ZPOAhwi1Fdl RKS4T97X+KeJUmzNkyZ6ZiOxIoEPFnF/6R0f0wT43LwbxsnRYlmyElj31TXa+EVEi6V4 eXLg== X-Forwarded-Encrypted: i=1; AJvYcCWDV1NZZZbu9y3obpEqChnB/BrPnepcQD6JRmRLza+0Vsvuwq9T6em23uaNJDQcZi3akSl98q65MM4=@lists.xenproject.org X-Gm-Message-State: AOJu0Yyvz0YiHwHq2iv/KlaBbKs/qUYlDOpg6CqEKWyACVE/jJJ85PTF CgjKFJd5Dv9UkltwNtCYrxh5YqNIAwRoepFoCkkFCvidu2+CE+qNvIVZLIhCdtGZNjs= X-Gm-Gg: ASbGncvwPkPBSyrVsiFEgRsofdUZCNln4SJvFmmR/BE8BBjE6PUGk31N6FhL5qMVizQ +YqKsySMI2/PKPWLXc/MT+efkbX5HwWK+qjGX22HSBaHkCKDiVERt094R5cwFkUeZj2cWO2tqcE a8C4zemRVGlVKJq/x8T5kkGTJ8fyZQ/10wC2BRaZhov/krOkr1YqQT0kbTloFpAPNWkDPT6lHRa 1UrfTJF+E7KyksQ75WKz1dliMVqu1EaeE8+tZpQrKzSoVKe84KpWGl57kUsNHsdZs2msqPSt2yd BNbP482neZ7ONIJT4HDTDt5H70d9YYaqb1tQIRWzsQj8zoHj522FAluLencXjhORyUXsX0KsPWz UdWl/qFBqptRZE2bPVZBlPzuDQigc3mrHooJoBhc+b0EsAXfnOnKHQULqbxF1xfYBOprT X-Google-Smtp-Source: AGHT+IGxC4LPnlikozzrfsQ6uwwai3SKTcqJCMLrNKAYvDH0Kv6qKwpJ2otRVgi+cmHIJIK8cGJd4w== X-Received: by 2002:a05:6000:2302:b0:3a3:63d3:369a with SMTP id ffacd0b85a97d-3bb2483ccf3mr101660f8f.25.1755204040162; Thu, 14 Aug 2025 13:40:40 -0700 (PDT) Message-ID: <37cb95df-520e-45b3-9003-b638296d1001@citrix.com> Date: Thu, 14 Aug 2025 21:40:38 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 21/22] x86/traps: Introduce FRED entrypoints To: Jan Beulich Cc: =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= , Xen-devel References: <20250808202314.1045968-1-andrew.cooper3@citrix.com> <20250808202314.1045968-22-andrew.cooper3@citrix.com> <53028a31-e07d-43f1-b320-2775c5560a09@suse.com> Content-Language: en-GB From: Andrew Cooper Autocrypt: addr=andrew.cooper3@citrix.com; keydata= xsFNBFLhNn8BEADVhE+Hb8i0GV6mihnnr/uiQQdPF8kUoFzCOPXkf7jQ5sLYeJa0cQi6Penp VtiFYznTairnVsN5J+ujSTIb+OlMSJUWV4opS7WVNnxHbFTPYZVQ3erv7NKc2iVizCRZ2Kxn srM1oPXWRic8BIAdYOKOloF2300SL/bIpeD+x7h3w9B/qez7nOin5NzkxgFoaUeIal12pXSR Q354FKFoy6Vh96gc4VRqte3jw8mPuJQpfws+Pb+swvSf/i1q1+1I4jsRQQh2m6OTADHIqg2E ofTYAEh7R5HfPx0EXoEDMdRjOeKn8+vvkAwhviWXTHlG3R1QkbE5M/oywnZ83udJmi+lxjJ5 YhQ5IzomvJ16H0Bq+TLyVLO/VRksp1VR9HxCzItLNCS8PdpYYz5TC204ViycobYU65WMpzWe LFAGn8jSS25XIpqv0Y9k87dLbctKKA14Ifw2kq5OIVu2FuX+3i446JOa2vpCI9GcjCzi3oHV e00bzYiHMIl0FICrNJU0Kjho8pdo0m2uxkn6SYEpogAy9pnatUlO+erL4LqFUO7GXSdBRbw5 gNt25XTLdSFuZtMxkY3tq8MFss5QnjhehCVPEpE6y9ZjI4XB8ad1G4oBHVGK5LMsvg22PfMJ ISWFSHoF/B5+lHkCKWkFxZ0gZn33ju5n6/FOdEx4B8cMJt+cWwARAQABzSlBbmRyZXcgQ29v cGVyIDxhbmRyZXcuY29vcGVyM0BjaXRyaXguY29tPsLBegQTAQgAJAIbAwULCQgHAwUVCgkI CwUWAgMBAAIeAQIXgAUCWKD95wIZAQAKCRBlw/kGpdefoHbdD/9AIoR3k6fKl+RFiFpyAh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-8 Content-Transfer-Encoding: 8bit On 14/08/2025 4:57 pm, Jan Beulich wrote: > On 08.08.2025 22:23, Andrew Cooper wrote: >> --- a/xen/arch/x86/include/asm/asm_defns.h >> +++ b/xen/arch/x86/include/asm/asm_defns.h >> @@ -315,6 +315,71 @@ static always_inline void stac(void) >> subq $-(UREGS_error_code-UREGS_r15+\adj), %rsp >> .endm >> >> +/* >> + * Push and clear GPRs >> + */ >> +.macro PUSH_AND_CLEAR_GPRS >> + push %rdi >> + xor %edi, %edi >> + push %rsi >> + xor %esi, %esi >> + push %rdx >> + xor %edx, %edx >> + push %rcx >> + xor %ecx, %ecx >> + push %rax >> + xor %eax, %eax >> + push %r8 >> + xor %r8d, %r8d >> + push %r9 >> + xor %r9d, %r9d >> + push %r10 >> + xor %r10d, %r10d >> + push %r11 >> + xor %r11d, %r11d >> + push %rbx >> + xor %ebx, %ebx >> + push %rbp >> +#ifdef CONFIG_FRAME_POINTER >> +/* Indicate special exception stack frame by inverting the frame pointer. */ >> + mov %rsp, %rbp >> + notq %rbp >> +#else >> + xor %ebp, %ebp >> +#endif >> + push %r12 >> + xor %r12d, %r12d >> + push %r13 >> + xor %r13d, %r13d >> + push %r14 >> + xor %r14d, %r14d >> + push %r15 >> + xor %r15d, %r15d >> +.endm >> + >> +/* >> + * POP GPRs from a UREGS_* frame on the stack. Does not modify flags. >> + * >> + * @rax: Alternative destination for the %rax value on the stack. >> + */ >> +.macro POP_GPRS rax=%rax >> + pop %r15 >> + pop %r14 >> + pop %r13 >> + pop %r12 >> + pop %rbp >> + pop %rbx >> + pop %r11 >> + pop %r10 >> + pop %r9 >> + pop %r8 >> + pop \rax >> + pop %rcx >> + pop %rdx >> + pop %rsi >> + pop %rdi >> +.endm > Hmm, yes, differences are apparently large enough to warrant the redundancy > with SAVE_ALL / RESTORE_ALL. You may recall this construct from prior attempts I've had to remove SAVE_ALL/RESTORE_ALL, even with the \rax parameter for SVM.  I still intend to complete that work at some point. > >> --- a/xen/arch/x86/include/asm/msr.h >> +++ b/xen/arch/x86/include/asm/msr.h >> @@ -202,9 +202,9 @@ static inline unsigned long read_gs_base(void) >> >> static inline unsigned long read_gs_shadow(void) >> { >> - unsigned long base; >> + unsigned long base, cr4 = read_cr4(); >> >> - if ( read_cr4() & X86_CR4_FSGSBASE ) >> + if ( !(cr4 & X86_CR4_FRED) && (cr4 & X86_CR4_FSGSBASE) ) >> { >> asm volatile ( "swapgs" ); >> base = __rdgsbase(); >> @@ -234,7 +234,9 @@ static inline void write_gs_base(unsigned long base) >> >> static inline void write_gs_shadow(unsigned long base) >> { >> - if ( read_cr4() & X86_CR4_FSGSBASE ) >> + unsigned long cr4 = read_cr4(); >> + >> + if ( !(cr4 & X86_CR4_FRED) && (cr4 & X86_CR4_FSGSBASE) ) >> { >> asm volatile ( "swapgs\n\t" >> "wrgsbase %0\n\t" > I don't quite get how these changes fit into this patch. Without the change, read_registers() suffers #UD because of the SWAPGS. This recurses until hitting the guard page, then repeats the same on the #DF stack.  And because stacks work nicely under FRED, you eventually hit #DF's guard page. Strictly speaking it's only read_gs_shadow() which we need to change to make exception handling work, but I fixed both at the same time. That said, I have actually cleaned this codepath up with the MSR work because the code gen in read_registers() is terrible.  Due to no-strict-aliasing, every store into state-> forces a recalculation of get_cpu_info(), meaning that read_cr4() cannot be hoisted, and there's a branch in every helper. I'm still not sure how best to fit it into this series. > >> --- a/xen/arch/x86/traps.c >> +++ b/xen/arch/x86/traps.c >> @@ -1013,6 +1013,32 @@ void show_execution_state_nmi(const cpumask_t *mask, bool show_all) >> printk("Non-responding CPUs: {%*pbl}\n", CPUMASK_PR(&show_state_mask)); >> } >> >> +static const char *x86_et_name(unsigned int type) >> +{ >> + static const char *const names[] = { >> + [X86_ET_EXT_INTR] = "EXT_INTR", >> + [X86_ET_NMI] = "NMI", >> + [X86_ET_HW_EXC] = "HW_EXC", >> + [X86_ET_SW_INT] = "SW_INT", >> + [X86_ET_PRIV_SW_EXC] = "PRIV_SW_EXEC", >> + [X86_ET_SW_EXC] = "SW_EXEC", >> + [X86_ET_OTHER] = "OTHER", >> + }; >> + >> + return (type < ARRAY_SIZE(names) && names[type]) ? names[type] : "???"; >> +} >> + >> +static const char *x86_et_other_name(unsigned int vec) > This isn't really a vector, is it? Well - you are decoding the field name regs->fred_ss.vector. Also I see I've typo'd EXEC in two of those names. > >> +{ >> + static const char *const names[] = { >> + [0] = "MTF", >> + [1] = "SYSCALL", >> + [2] = "SYSENTER", >> + }; >> + >> + return (vec < ARRAY_SIZE(names) && names[vec][0]) ? names[vec] : "???"; > Did you mean to check names[ves] for being NULL? Or is this a leftover > from the array being something like names[][10]? Oh, bad copy&paste.  Will fix. > >> --- a/xen/arch/x86/x86_64/Makefile >> +++ b/xen/arch/x86/x86_64/Makefile >> @@ -1,6 +1,7 @@ >> obj-$(CONFIG_PV32) += compat/ >> >> obj-bin-y += entry.o >> +obj-bin-y += entry-fred.o > For the ordering here, ... > >> --- /dev/null >> +++ b/xen/arch/x86/x86_64/entry-fred.S >> @@ -0,0 +1,35 @@ >> +/* SPDX-License-Identifier: GPL-2.0-or-later */ >> + >> + .file "x86_64/entry-fred.S" >> + >> +#include >> +#include >> + >> + .section .text.entry, "ax", @progbits >> + >> + /* The Ring3 entry point is required to be 4k aligned. */ >> + >> +FUNC(entry_FRED_R3, 4096) > ... doesn't this 4k-alignment requirement suggest we want to put > entry-fred.o first? Perhaps, but that is quite subtle.  I did also consider a .text.entry.page_aligned section, but .text.entry only matters for XPTI which (as agreed), I'm not intending to implement in FRED mode unless it proves to be necessary. Also IIRC there's still a symbol bug where _sentrytext takes priority over entry_FRED_R3, so the backtrace is effectively wrong. (These are all bad excuses, but some parts of this series are rather old.) > Also, might it be more natural to use PAGE_SIZE > here? I did debate that, but the spec uses 0xfff, not pages, even if the pipline surely does have an optimisation for chopping 12 metadata bits off the bottom of a pointer. ~Andrew