From patchwork Wed Jan 31 21:14:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 194941 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:106:209c:c626 with SMTP id mn5csp29933dyc; Wed, 31 Jan 2024 13:17:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IE8VitOUTeHEucoB2Ili6zagyVGJU7f3cScrrOhHu3nsdSL8w6UJMu7HIyZyVgVkXeSF3GJ X-Received: by 2002:a05:6214:2a8d:b0:68c:5a53:cbfd with SMTP id jr13-20020a0562142a8d00b0068c5a53cbfdmr537501qvb.43.1706735862890; Wed, 31 Jan 2024 13:17:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706735862; cv=pass; d=google.com; s=arc-20160816; b=onkLRErggE2lE4X7FF+RKD4P24s3HK6Kqd9RxrEfCkE62yBJSJn8pE2cj0m2HVfgP7 au18KfDBlsqaM6rn6pkjT1URaoEg1YFDvNvlzR7RZ2KbQq+B0UTrvuU/dfUsELTDVokL dwc6mcx/lQ9LZAu5gcJNh2+mVYQQIDGz9NCcGHiyUwx6swF5As5v18ktDHaaaFlfya4k 9WwUXWWZyfFT+kdOnaS7qKXYGLk1Nt8qpMRCguWGxnKKFS/lMfnV8aFX5rpZpkFpzVKW dQhr+HKXbT4YTWulHNii4qrUq6nYMS4l3Ybuyg/gaSREpuMvS5si3IvmJmk0Zjwa3ZLJ p/KQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:precedence:robot-unsubscribe:robot-id :message-id:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:in-reply-to:cc:subject:to:reply-to:sender :from:dkim-signature:dkim-signature:date; bh=SWKkeqhILh/m+STXtUXEgopq3WJoCcdgvBHPaDrkSU8=; fh=nK7QVzctvVw6yDPjf46rkcagkcqEci/CLNBEv6+pNro=; b=br+HH48y3H7a8DJmJwA1ql927O/vwlPvBe3FKsxz9ZN10RLNt2EJ5p+LhJuEr5dGk8 7+0HNRiHf40N2W5PEAmmiQh/l4+FUxYGbYApVBANwYB3PefwiZtKBEvGuj3iTsOEKp+K CzGmAwPalxazn6N+k6iZNDgFjREKjr9mvdf45wGvArrhM3qPX72alRXFvj56yidCaduQ C4ijbDH6X6KRrgyQ/hGjj/xnxJb9X0vRc4IJukKTAt6m9KP/KZTqk/70WBXSll3Nk+rH K09sXSpTKWOHs23ZPyvirr83F3JFn3OJPDzSl1sfiCD7vkaXdyoehDEPSvrga2LzuZeL Q/3Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=pnd564XJ; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-47124-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47124-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de X-Forwarded-Encrypted: i=1; AJvYcCWWfBgIbRtxC2MtDJeJmZEo/yr/Tem4/AGaekz0Xz8aRHodJnySuFIsAnpQhFY/+q9IDVyrX7J73Wx4VxLLffC2SnR1ig== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id g7-20020a0562140ac700b006841e1bbc0esi14164283qvi.320.2024.01.31.13.17.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 13:17:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-47124-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=pnd564XJ; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-47124-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47124-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 9E4EF1C22BEB for ; Wed, 31 Jan 2024 21:17:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6FFDA3BB20; Wed, 31 Jan 2024 21:14:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="pnd564XJ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="CwPuddIn" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 881FE39AF8; Wed, 31 Jan 2024 21:14:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706735687; cv=none; b=LFgij3HEVTOyopGz9uvNSzyQRtdegJdXzAvzOWwWGEZma/CrQwwmW6gNWNPcrBe4QZ4g5NdQMQb1ymvy1IBDmLXbxfA5vVgl0ngO9CI154/2uJdk4UMU/oTjUCUjngHE1gpWXYrH9DHX7moulI3it/Ij3DLet7Q6s8eK3i8NA8Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706735687; c=relaxed/simple; bh=MnBjWZg+5DJKdhC1lftf/5cSIW13CWPbkyHkxStEohE=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=gZBBfhgJsBJg0UX9L6aV2IcrkLVILwfgDuHbTbZUk73lZJs4DiWHHT36X7Q5OpL8TU2HzQB1XNlLaq0wHBGvGgIgB30pPhuffMoKGrzK03QJtUj3euVA0YrolFtM3Cv+0D5XtGxqRDrcKjSmp7DPITpc5YInyiOqHDe3EFC6n/4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=pnd564XJ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=CwPuddIn; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Date: Wed, 31 Jan 2024 21:14:42 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1706735683; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SWKkeqhILh/m+STXtUXEgopq3WJoCcdgvBHPaDrkSU8=; b=pnd564XJPyqxPog+ZJbjF967zn6fqcBQ0G8+jRgYEt/bfeWa9pK754uV1BhCZqSLuPTcXF ym5OJqGdCuGONrtHzb5iZ/3HDSWz1Nk6+YcSGBCIqnGQ8z4JyOsiYcRXH42UDxD70neFGw 1r/TvAzUXkylHlMNW2ZExhGvmPHmoGTHPnWbTA+vpjWOulozxTPzOK2MZp2ql6O8Uyroya Xd61iehlUa0Ly/bXkMHGAheDjFoCtvmm9Uy+cJ9dCltuFCzQ9MWzNCiAjvR/rQqglsLJ3D QK548rft75yGjIEZyxxOJZy/rLNuHSQlR2qHeBDmk1sSkMkfs+U1KnJBHd1AMQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1706735683; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SWKkeqhILh/m+STXtUXEgopq3WJoCcdgvBHPaDrkSU8=; b=CwPuddInlpA0+virtBTMGxIetwtE67ln9TqQtFzrn6zNM08FYmg0uhnwZQyheTM73nctt/ HSi3nklxSyjN0/Dw== From: "tip-bot2 for Xin Li" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/fred] x86/fred: Fixup fault on ERETU by jumping to fred_entrypoint_user Cc: "H. Peter Anvin (Intel)" , Xin Li , Thomas Gleixner , "Borislav Petkov (AMD)" , Shan Kang , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20231205105030.8698-30-xin3.li@intel.com> References: <20231205105030.8698-30-xin3.li@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <170673568259.398.13788198953979010060.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784440834634349456 X-GMAIL-MSGID: 1789642264370165869 The following commit has been merged into the x86/fred branch of tip: Commit-ID: 5105e7687ad3dffde77f6e4393b5530e83d672dc Gitweb: https://git.kernel.org/tip/5105e7687ad3dffde77f6e4393b5530e83d672dc Author: Xin Li AuthorDate: Tue, 05 Dec 2023 02:50:18 -08:00 Committer: Borislav Petkov (AMD) CommitterDate: Wed, 31 Jan 2024 22:03:04 +01:00 x86/fred: Fixup fault on ERETU by jumping to fred_entrypoint_user If the stack frame contains an invalid user context (e.g. due to invalid SS, a non-canonical RIP, etc.) the ERETU instruction will trap (#SS or #GP). >From a Linux point of view, this really should be considered a user space failure, so use the standard fault fixup mechanism to intercept the fault, fix up the exception frame, and redirect execution to fred_entrypoint_user. The end result is that it appears just as if the hardware had taken the exception immediately after completing the transition to user space. Suggested-by: H. Peter Anvin (Intel) Signed-off-by: Xin Li Signed-off-by: Thomas Gleixner Signed-off-by: Borislav Petkov (AMD) Tested-by: Shan Kang Link: https://lore.kernel.org/r/20231205105030.8698-30-xin3.li@intel.com --- arch/x86/entry/entry_64_fred.S | 5 +- arch/x86/include/asm/extable_fixup_types.h | 4 +- arch/x86/mm/extable.c | 78 +++++++++++++++++++++- 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/arch/x86/entry/entry_64_fred.S b/arch/x86/entry/entry_64_fred.S index 2271a1c..7fe2722 100644 --- a/arch/x86/entry/entry_64_fred.S +++ b/arch/x86/entry/entry_64_fred.S @@ -3,6 +3,7 @@ * The actual FRED entry points. */ +#include #include #include "calling.h" @@ -34,7 +35,9 @@ SYM_CODE_START_NOALIGN(asm_fred_entrypoint_user) call fred_entry_from_user SYM_INNER_LABEL(asm_fred_exit_user, SYM_L_GLOBAL) FRED_EXIT - ERETU +1: ERETU + + _ASM_EXTABLE_TYPE(1b, asm_fred_entrypoint_user, EX_TYPE_ERETU) SYM_CODE_END(asm_fred_entrypoint_user) /* diff --git a/arch/x86/include/asm/extable_fixup_types.h b/arch/x86/include/asm/extable_fixup_types.h index fe63120..7acf038 100644 --- a/arch/x86/include/asm/extable_fixup_types.h +++ b/arch/x86/include/asm/extable_fixup_types.h @@ -64,6 +64,8 @@ #define EX_TYPE_UCOPY_LEN4 (EX_TYPE_UCOPY_LEN | EX_DATA_IMM(4)) #define EX_TYPE_UCOPY_LEN8 (EX_TYPE_UCOPY_LEN | EX_DATA_IMM(8)) -#define EX_TYPE_ZEROPAD 20 /* longword load with zeropad on fault */ +#define EX_TYPE_ZEROPAD 20 /* longword load with zeropad on fault */ + +#define EX_TYPE_ERETU 21 #endif diff --git a/arch/x86/mm/extable.c b/arch/x86/mm/extable.c index 271dcb2..b522933 100644 --- a/arch/x86/mm/extable.c +++ b/arch/x86/mm/extable.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -223,6 +224,79 @@ static bool ex_handler_ucopy_len(const struct exception_table_entry *fixup, return ex_handler_uaccess(fixup, regs, trapnr, fault_address); } +#ifdef CONFIG_X86_FRED +static bool ex_handler_eretu(const struct exception_table_entry *fixup, + struct pt_regs *regs, unsigned long error_code) +{ + struct pt_regs *uregs = (struct pt_regs *)(regs->sp - offsetof(struct pt_regs, orig_ax)); + unsigned short ss = uregs->ss; + unsigned short cs = uregs->cs; + + /* + * Move the NMI bit from the invalid stack frame, which caused ERETU + * to fault, to the fault handler's stack frame, thus to unblock NMI + * with the fault handler's ERETS instruction ASAP if NMI is blocked. + */ + regs->fred_ss.nmi = uregs->fred_ss.nmi; + + /* + * Sync event information to uregs, i.e., the ERETU return frame, but + * is it safe to write to the ERETU return frame which is just above + * current event stack frame? + * + * The RSP used by FRED to push a stack frame is not the value in %rsp, + * it is calculated from %rsp with the following 2 steps: + * 1) RSP = %rsp - (IA32_FRED_CONFIG & 0x1c0) // Reserve N*64 bytes + * 2) RSP = RSP & ~0x3f // Align to a 64-byte cache line + * when an event delivery doesn't trigger a stack level change. + * + * Here is an example with N*64 (N=1) bytes reserved: + * + * 64-byte cache line ==> ______________ + * |___Reserved___| + * |__Event_data__| + * |_____SS_______| + * |_____RSP______| + * |_____FLAGS____| + * |_____CS_______| + * |_____IP_______| + * 64-byte cache line ==> |__Error_code__| <== ERETU return frame + * |______________| + * |______________| + * |______________| + * |______________| + * |______________| + * |______________| + * |______________| + * 64-byte cache line ==> |______________| <== RSP after step 1) and 2) + * |___Reserved___| + * |__Event_data__| + * |_____SS_______| + * |_____RSP______| + * |_____FLAGS____| + * |_____CS_______| + * |_____IP_______| + * 64-byte cache line ==> |__Error_code__| <== ERETS return frame + * + * Thus a new FRED stack frame will always be pushed below a previous + * FRED stack frame ((N*64) bytes may be reserved between), and it is + * safe to write to a previous FRED stack frame as they never overlap. + */ + fred_info(uregs)->edata = fred_event_data(regs); + uregs->ssx = regs->ssx; + uregs->fred_ss.ss = ss; + /* The NMI bit was moved away above */ + uregs->fred_ss.nmi = 0; + uregs->csx = regs->csx; + uregs->fred_cs.sl = 0; + uregs->fred_cs.wfe = 0; + uregs->cs = cs; + uregs->orig_ax = error_code; + + return ex_handler_default(fixup, regs); +} +#endif + int ex_get_fixup_type(unsigned long ip) { const struct exception_table_entry *e = search_exception_tables(ip); @@ -300,6 +374,10 @@ int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code, return ex_handler_ucopy_len(e, regs, trapnr, fault_addr, reg, imm); case EX_TYPE_ZEROPAD: return ex_handler_zeropad(e, regs, fault_addr); +#ifdef CONFIG_X86_FRED + case EX_TYPE_ERETU: + return ex_handler_eretu(e, regs, error_code); +#endif } BUG(); }