Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp27232rwl; Thu, 6 Apr 2023 14:09:51 -0700 (PDT) X-Google-Smtp-Source: AKy350a5ohHmrVbzVihIOYeS4tg9O6tAUka0lvKkawsn80IC1aDsd92u7N7Ic+Z4M38lLa3lnBfr X-Received: by 2002:a17:906:4d0f:b0:93e:3127:fc28 with SMTP id r15-20020a1709064d0f00b0093e3127fc28mr177454eju.39.1680815391323; Thu, 06 Apr 2023 14:09:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680815391; cv=none; d=google.com; s=arc-20160816; b=Amf3pVjtidoGFoCxhDTWhBcjSOOpoQlH5gZHjhzQpRB5D+P2gH9LkXVUdkhoIDlZ1X gEUqg1kzF1CcZ7r4dCjGaInfofnAu9fcR74B//p0h8LID+vPYLCHiJkM7rxrlyTuwAZL 4CT2jyz6ZTrKnyfmzS23HdKemKDJvVefe/G1QqaepzSqb7YEglB8vjZ2bgaUcGioe2E/ 5wtju/i/oXgfwn4ScB2uqwJDxKXH/jwmsOm1R5euvam4jfqPxIHhuDDorq0EUwInVN/V b3SBPgvHqxVeKzbRAnyZCeERFReMxX7zrPoIDHurt9a71tWEZsq4CJy5jx7ObQz5jlZz jZcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=3hwN/YQlmfu+j1gvDC0ozNOHSiaSepyoqpyTVyTKngc=; b=0yFEaSo9TLxQanU6HoVN60hE+meZQKgJwidkKfrdyxaN1uJb/qRWBcZpBmwI2K/vH1 aJfrf32KkaTTNv5c5fk2HBZCcKir+TSd5J/ej5hFsAcVlJwyvLGZ/TWi8DzdwoeRPlzW FAYr7CDophX0GkG6fvrmkUad5/xAVcbb++ODLnsd84EpzAlIbKBoCjwhW+ShOR0cNMQC TblD1etgEwbA6PoN967Z3YCnVdq/0PegMT3SI3KWHZx0us2JrXub/Ua8Az0oGucMaYIZ qyFQWGcwoq0VCT/DfwwP55aVbsQF+tsYXILUxKBmMQ/f+Le8BdERZVKfvkn5/9mrgsqg tUqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=DUYMZP3I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bq5-20020a170906d0c500b00939c375fa76si1900103ejb.279.2023.04.06.14.09.22; Thu, 06 Apr 2023 14:09:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=DUYMZP3I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236795AbjDFVC2 (ORCPT + 99 others); Thu, 6 Apr 2023 17:02:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240516AbjDFVCO (ORCPT ); Thu, 6 Apr 2023 17:02:14 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E89A97695; Thu, 6 Apr 2023 14:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=3hwN/YQlmfu+j1gvDC0ozNOHSiaSepyoqpyTVyTKngc=; b=DUYMZP3I6MAIdN+RaNwDZ2GO2M 5NAnKV9hWWYVWs/YitgvsXX6dYPXVrEmh2Zwb0IwaETdO29FjClCC3FbCdSijw6piJkmtoy4RhXpT k2SmpsJWtKgEe+bbcNekOFVcTYWimPIqglnBi7AagNQzuAmIl4rhHCXWD2xn2RQCU7ppq9ABLNJa+ B14hrLYbBZTH32p5BAoxzhH/9/pyJg9I25GMEh5NkRCqumVuDlM70Z3KC9iCXscPjcJdXoXuy+xCy cPaXvFWTreAX9Y8z/N/bUupIxRySGoylhHq87LnPsGjZHI2PlXsJOO3Me86hbJOYd0BSR1aSOTsg5 krH13tbQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1pkWjO-00AfU4-35; Thu, 06 Apr 2023 21:01:20 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 92276300274; Thu, 6 Apr 2023 23:01:16 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 669982692D852; Thu, 6 Apr 2023 23:01:16 +0200 (CEST) Date: Thu, 6 Apr 2023 23:01:16 +0200 From: Peter Zijlstra To: Lai Jiangshan Cc: linux-kernel@vger.kernel.org, Lai Jiangshan , "H. Peter Anvin" , Andi Kleen , Andrew Cooper , Andy Lutomirski , Asit Mallick , Cfir Cohen , Dan Williams , Dave Hansen , David Kaplan , David Rientjes , Dirk Hohndel , Erdem Aktas , Jan Kiszka , Jiri Slaby , Joerg Roedel , Juergen Gross , Kees Cook , Kirill Shutemov , Kuppuswamy Sathyanarayanan , Linus Torvalds , Mike Stunes , Raj Ashok , Sean Christopherson , Thomas Gleixner , Tom Lendacky , Tony Luck , kvm@vger.kernel.org, linux-coco@lists.linux.dev, x86@kernel.org, Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Josh Poimboeuf , Arnd Bergmann Subject: Re: [RFC PATCH 3/7] x86/entry: Implement atomic-IST-entry Message-ID: <20230406210116.GG405948@hirez.programming.kicks-ass.net> References: <20230403140605.540512-1-jiangshanlai@gmail.com> <20230403140605.540512-4-jiangshanlai@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230403140605.540512-4-jiangshanlai@gmail.com> X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 03, 2023 at 10:06:01PM +0800, Lai Jiangshan wrote: > diff --git a/arch/x86/entry/Makefile b/arch/x86/entry/Makefile > index ca2fe186994b..7cc1254ca519 100644 > --- a/arch/x86/entry/Makefile > +++ b/arch/x86/entry/Makefile > @@ -8,11 +8,14 @@ UBSAN_SANITIZE := n > KCOV_INSTRUMENT := n > > CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE) > +CFLAGS_REMOVE_ist_entry.o = $(CC_FLAGS_FTRACE) $(RETHUNK_CFLAGS) This ^^^ > diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S > index 49ddc4dd3117..50a24cc83581 100644 > --- a/arch/x86/entry/entry_64.S > +++ b/arch/x86/entry/entry_64.S > @@ -443,6 +443,184 @@ SYM_CODE_END(\asmsym) > +.macro idtentry_ist vector asmsym cfunc user_cfunc has_error_code:req, stack_offset:req > +SYM_CODE_START(\asmsym) > + UNWIND_HINT_IRET_REGS offset=\has_error_code*8 > + ENDBR > + > + /* > + * Clear X86_EFLAGS_AC, X86_EFLAGS_DF and set a default ORIG_RAX. > + * > + * The code setting ORIG_RAX will not be replicated if interrupted. > + */ > + ASM_CLAC > + cld > + > + .if \has_error_code == 0 > + pushq $-1 /* ORIG_RAX: no syscall to restart */ > + .endif > + > + /* > + * No register can be touched except %rsp,%rflags,%rip before > + * pushing all the registers. It is indispensable for nested > + * atomic-IST-entry to replicate pushing the registers. > + */ > + PUSH_REGS > + > + /* > + * Finished pushing register, all registers can be touched by now. > + * > + * Clear registers for the C function ist_copy_regs_to_main_stack() > + * and the handler to avoid any possible exploitation of any > + * speculation attack. > + */ > + CLEAR_REGS > + > + /* > + * Copy the pt_regs to the IST main stack including the pt_regs of > + * the interrupted atomic-IST-entris, if any, by replicating. > + */ > + movq %rsp, %rdi /* pt_regs pointer on its own IST stack */ > + leaq PTREGS_SIZE-\stack_offset(%rsp), %rsi /* struct cea_exception_stacks pointer */ > + call ist_copy_regs_to_main_stack IIUC you do a CALL+RET here, before you call paranoid_entry ... > + > + /* > + * Commit stage. > + */ > +SYM_INNER_LABEL(start_commit_\asmsym, SYM_L_GLOBAL) > + /* > + * Switches to the IST main stack. Before the switching is done, > + * %rax is the copied pt_regs pointer in IST main stack. > + */ > + movq %rax, %rsp > + > + /* > + * The label should be immediate after the instruction that switches > + * the stack since there is code assuming there is only one single > + * instruction in the commit stage and the code assumes "%rsp in the > + * IST main stack is also the sign of ending a atomic-IST-entry". > + * (The code will be removed in future when %rip-based identifying > + * is added.) > + */ > +SYM_INNER_LABEL(commit_\asmsym, SYM_L_GLOBAL) > + > + /* > + * Now, it is on the IST main stack. For the whole kernel, the entries > + * of the IST exceptions can be seen from here because the inside > + * of the atomic-IST-entry can not be seen from the whole kernel > + * except in the atomic-IST-entry or #DF. > + */ > + UNWIND_HINT_REGS > + ENCODE_FRAME_POINTER > + > + /* > + * The code setting ORIG_RAX will not be replicated if interrupted. > + * So redo it here. > + */ > + .if \has_error_code == 0 > + movq $-1, ORIG_RAX(%rsp) /* ORIG_RAX: no syscall to restart */ > + .endif > + > + /* > + * If the entry is from userspace, switch stacks and treat it as > + * a normal entry. > + */ > + testb $3, CS(%rsp) > + jnz .Lfrom_usermode_switch_stack_\@ > + > + /* > + * paranoid_entry returns GS/CR3/SPEC_CTL information for > + * paranoid_exit in RBX/R14/R15. > + */ > + call paranoid_entry ... all the way down here, which will do: IBRS_ENTER; UNTRAIN_RET_FROM_CALL; Which thus breaks the whole RetBleed mess, since that must not do RET before that happens.