From patchwork Wed Feb 28 13:51:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 13575483 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 3BE86C47DD9 for ; Wed, 28 Feb 2024 13:52:01 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.686637.1068875 (Exim 4.92) (envelope-from ) id 1rfKLi-0000es-Hg; Wed, 28 Feb 2024 13:51:54 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 686637.1068875; Wed, 28 Feb 2024 13:51:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rfKLi-0000el-EN; Wed, 28 Feb 2024 13:51:54 +0000 Received: by outflank-mailman (input) for mailman id 686637; Wed, 28 Feb 2024 13:51:53 +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 1rfKLh-0000eZ-Iy for xen-devel@lists.xenproject.org; Wed, 28 Feb 2024 13:51:53 +0000 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [2a00:1450:4864:20::534]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 8656b08a-d640-11ee-a1ee-f123f15fe8a2; Wed, 28 Feb 2024 14:51:51 +0100 (CET) Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-5640fef9fa6so7149996a12.0 for ; Wed, 28 Feb 2024 05:51:51 -0800 (PST) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id s24-20020a170906bc5800b00a3efa4e033asm1870713ejv.151.2024.02.28.05.51.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Feb 2024 05:51:50 -0800 (PST) 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: 8656b08a-d640-11ee-a1ee-f123f15fe8a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1709128311; x=1709733111; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :references:cc:to:from:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=w+KEn+hcWSFN2S28B1lVdeEMQiVbpABucnYzaxqMQm8=; b=Qux/xgWvGZjujzYDSY9JylKPOdCk9vfXcDbtCRNcu/qhlMpWSRfq/YKSiostT2hCqL ENy3V6+6Xzot+bPS/zjeWFY+MqpUHaBwkkISI9qBZcdMo0SLjCOAhExVbYKs8x0BRu7u B1/8Q2gv8+2ntl+CBI23f2cXMWbXHr6Q+Hzb2pgf3mlpaBTKVWHu/SUmh982igB9+uBo +latjEivpfsyuZMqYROVfk+jKmUPbZi5oSTDMAyXYGsqTaslaG/izfx5Th52IemrIGOn A6X/YSgK11Hg1TSPqKlAKlGR3BBZ2iQZGkjvkEym7KriRmBFed9wxi3B9Rstz1Z7QB1f T/eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709128311; x=1709733111; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :references:cc:to:from:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=w+KEn+hcWSFN2S28B1lVdeEMQiVbpABucnYzaxqMQm8=; b=xClN4FF4yGscz0hTU+E0GgX9MEu8xab0dtU3VLJJtHXZ+Av4lUxkjCUA6dtp28Ojz0 4gt89nlgM/npsL+HKSY2xZ1rIFCRREavpeg+8i/DXgdMRLEqAWnVGIWs0Q/daoB546i0 4i8EcJWSqYabUQAVUZvqHBVDk5ISCjKn90o0W2qB+9JNg05oTsa+5isTs+Qj+adKGiZT YaQM0mKmcCUQefDuJPfQFrD64L2ythphe/qBtl7ejPgn2mwWI2e3IEpNJDi8zJNkYk5e uz7zKTx7N45IIVhNhzQDhMDoAal3gprc9aghyhqeLA1L21FKblUX7BZGwu4JzbR3gUch Edig== X-Gm-Message-State: AOJu0YxC2X+kkMDIJmlauChEoMrqqaPhrBWQWVCFoVwPKjfsjlzkSw4E TIfycmr6rZxzt8/MCEts6SJSIInDZlStu69Lsxb6DRe2Cw7JrAnuyQW3UU5p1uDANtpgjU1d2r8 = X-Google-Smtp-Source: AGHT+IGEAk3oZI5VJ2IkQIs92O6kgtOshVr0bXPkF4PYNTxa8IWW377FE7w/kvyHwVWECxGBjJJyOw== X-Received: by 2002:a17:907:bb99:b0:a44:15c3:c8e9 with SMTP id xo25-20020a170907bb9900b00a4415c3c8e9mr714367ejc.28.1709128311157; Wed, 28 Feb 2024 05:51:51 -0800 (PST) Message-ID: Date: Wed, 28 Feb 2024 14:51:49 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 1/4] x86: remove redundant XEN_SHSTK check from reinit_bsp_stack() From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Wei Liu , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <33da6f8b-af22-48c8-acce-3aa55c0e0414@suse.com> Content-Language: en-US Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9Uf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z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: <33da6f8b-af22-48c8-acce-3aa55c0e0414@suse.com> As of 72d51813d631 ("x86: amend cpu_has_xen_{ibt,shstk}") this has been integrated into cpu_has_xen_shstk. Signed-off-by: Jan Beulich Acked-by: Andrew Cooper --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -810,7 +810,7 @@ static void __init noreturn reinit_bsp_s if ( rc ) panic("Error %d setting up PV root page table\n", rc); - if ( IS_ENABLED(CONFIG_XEN_SHSTK) && cpu_has_xen_shstk ) + if ( cpu_has_xen_shstk ) { wrmsrl(MSR_PL0_SSP, (unsigned long)stack + (PRIMARY_SHSTK_SLOT + 1) * PAGE_SIZE - 8); From patchwork Wed Feb 28 13:52:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 13575484 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 92F1AC47DD9 for ; Wed, 28 Feb 2024 13:52:20 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.686639.1068885 (Exim 4.92) (envelope-from ) id 1rfKLz-00011v-Of; Wed, 28 Feb 2024 13:52:11 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 686639.1068885; Wed, 28 Feb 2024 13:52:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rfKLz-00011m-Ls; Wed, 28 Feb 2024 13:52:11 +0000 Received: by outflank-mailman (input) for mailman id 686639; Wed, 28 Feb 2024 13:52:10 +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 1rfKLy-0000eZ-Ax for xen-devel@lists.xenproject.org; Wed, 28 Feb 2024 13:52:10 +0000 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [2a00:1450:4864:20::633]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 90714bbd-d640-11ee-a1ee-f123f15fe8a2; Wed, 28 Feb 2024 14:52:08 +0100 (CET) Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-a26ed1e05c7so878413366b.2 for ; Wed, 28 Feb 2024 05:52:08 -0800 (PST) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id s24-20020a170906bc5800b00a3efa4e033asm1870713ejv.151.2024.02.28.05.52.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Feb 2024 05:52:07 -0800 (PST) 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: 90714bbd-d640-11ee-a1ee-f123f15fe8a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1709128328; x=1709733128; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=gaX7a4NCHOeGdWCkg8kUSL8PRj5hzXXfo73pbxAntJ8=; b=bwZLe610hPexq3V1s2hT6jb5Wkg6RJ3QLjqeyoR1QAVC3Wqd5Wm/IFnMS5QDEAFuNl yG9t1lqEYfusKKMRT+RV8e3I0AVK0vUVjtFUCJUOBUdFVb9kJNqrpbD+vJMF8iBCS+z4 NKKwV15nWYLVfnbIsmE3EhHoJthM9ReuJA4hhlrttgu0tEwX2xzjnlKRSfeiDXuRxkAu mO2vgO81Mj0o9M1TZOCs7HLcKyYLwLQie59Q323tJbmQEvC8kTmxjVj2iD7I7TJut/Ub Jl3+47gHgBzqUtOSobDuiQQcdUrhWjHgvR+GMWMUYqNTMY9F54sFZjS1fem5QN5T3S1Z te3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709128328; x=1709733128; h=content-transfer-encoding:in-reply-to:autocrypt:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gaX7a4NCHOeGdWCkg8kUSL8PRj5hzXXfo73pbxAntJ8=; b=JJIAvTxvRsMagmKhooOXwBP/QKIuprxgrijJO9tcWX0PbKdJ9PhnR57gTXg7nnSNmJ 0mDy5LQ9z7EjYmvxfP+7QI+LEbMx1k64LEd4OJjS7jffnVrnfvLoNAsKTo+fcU47pbsA sd3EvJUWbuS42AZ3NnVdgwvBuVwZD6bIxiydjB9vgarnQzkiXKNtbY1vBbucarAFvfsy uhobAyy0kB2qg5P8YO6s4B6NoOeNYlPqY96WJOK7Bhhdkv8dm4feu9tqz+EZIZJep6x3 ZMpyo2aPPcXUAF1Ogm9KjrYAvawk/Ia9ojY8+1GJuH76PWnlnYGaBR/HJLka5qQ+0RVb bKgg== X-Gm-Message-State: AOJu0YwtlNmfkPuILH0lbhRLtKDcwMWT2BBr1lYY5cfhZ2fRsbQYF1tf rpkFnxUMtqScaWl6qKDD6BXgO6ipPmgjYESCP+sknPqYxPryp3NlWFbhaNLdYxrjb3XX1FITZuM = X-Google-Smtp-Source: AGHT+IF962mt3eRDy21WIUNAu+Y3BfGUsdDO1T8wRDs+WGYmPyqGL5DdVNU2BOS3piXY0SkY/N1xRw== X-Received: by 2002:a17:906:70c:b0:a40:189d:c5bb with SMTP id y12-20020a170906070c00b00a40189dc5bbmr8228552ejb.0.1709128328039; Wed, 28 Feb 2024 05:52:08 -0800 (PST) Message-ID: <0ad4543b-8eed-4147-b32d-b68d21fade98@suse.com> Date: Wed, 28 Feb 2024 14:52:07 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 2/4] x86: record SSP at non-guest entry points Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Wei Liu , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <33da6f8b-af22-48c8-acce-3aa55c0e0414@suse.com> Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHA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Sui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: <33da6f8b-af22-48c8-acce-3aa55c0e0414@suse.com> We will want to use that value for call trace generation, and likely also to eliminate the somewhat fragile shadow stack searching done in fixup_exception_return(). For those purposes, guest-only entry points do not need to record that value. To keep the saving code simple, record our own SSP that corresponds to an exception frame, pointing to the top of the shadow stack counterpart of what the CPU has saved on the regular stack. Consuming code can then work its way from there. Signed-off-by: Jan Beulich --- To record the full 64-bit value, some of the unused bits in the %cs slot could be used. Sadly that slot has saved_upcall_mask in an unhelpful location, otherwise simply storing low and high 32 bits in those two separate half-slots would be a pretty obvious choice. As long as "entry_ssp" is used in non-guest-entry frames only, we could of course put half of it into a union with saved_upcall_mask ... Else may want to put a BUILD_BUG_ON(VADDR_BITS > 48) somewhere, but I'm afraid I can't really identify a good place for such to live. Leveraging that the CPU stores zero in the upper bits of the selector register slots, the save sequence could also be shl $16, %rcx or %rcx, UREGS_entry_ssp-2(%rsp) That's shorter and avoids a 16-bit operation, but may be less desirable, for being a read-modify-write access. --- a/xen/arch/x86/domctl.c +++ b/xen/arch/x86/domctl.c @@ -1354,6 +1354,7 @@ void arch_get_info_guest(struct vcpu *v, if ( !compat ) { memcpy(&c.nat->user_regs, &v->arch.user_regs, sizeof(c.nat->user_regs)); + c.nat->user_regs.entry_ssp = 0; if ( is_pv_domain(d) ) memcpy(c.nat->trap_ctxt, v->arch.pv.trap_ctxt, sizeof(c.nat->trap_ctxt)); --- a/xen/arch/x86/hvm/svm/entry.S +++ b/xen/arch/x86/hvm/svm/entry.S @@ -94,7 +94,7 @@ __UNLIKELY_END(nsvm_hap) sti vmrun - SAVE_ALL + SAVE_ALL ssp=0 GET_CURRENT(bx) --- a/xen/arch/x86/hvm/vmx/entry.S +++ b/xen/arch/x86/hvm/vmx/entry.S @@ -25,7 +25,7 @@ #define VMLAUNCH .byte 0x0f,0x01,0xc2 ENTRY(vmx_asm_vmexit_handler) - SAVE_ALL + SAVE_ALL ssp=0 mov %cr2,%rax GET_CURRENT(bx) @@ -119,7 +119,7 @@ UNLIKELY_END(realmode) .Lvmx_vmentry_fail: sti - SAVE_ALL + SAVE_ALL ssp=0 /* * SPEC_CTRL_ENTRY notes --- a/xen/arch/x86/include/asm/asm_defns.h +++ b/xen/arch/x86/include/asm/asm_defns.h @@ -221,7 +221,7 @@ static always_inline void stac(void) #endif #ifdef __ASSEMBLY__ -.macro SAVE_ALL compat=0 +.macro SAVE_ALL compat=0 ssp=IS_ENABLED(CONFIG_XEN_SHSTK) addq $-(UREGS_error_code-UREGS_r15), %rsp cld movq %rdi,UREGS_rdi(%rsp) @@ -235,6 +235,9 @@ static always_inline void stac(void) movq %rax,UREGS_rax(%rsp) xor %eax, %eax .if !\compat +.if \ssp + rdsspq %rcx +.endif movq %r8,UREGS_r8(%rsp) movq %r9,UREGS_r9(%rsp) movq %r10,UREGS_r10(%rsp) @@ -264,6 +267,11 @@ static always_inline void stac(void) xor %r13d, %r13d xor %r14d, %r14d xor %r15d, %r15d +.if \ssp && !\compat + mov %cx, UREGS_entry_ssp(%rsp) + shr $16, %rcx + mov %ecx, UREGS_entry_ssp+2(%rsp) +.endif .endm #define LOAD_ONE_REG(reg, compat) \ --- a/xen/arch/x86/x86_64/asm-offsets.c +++ b/xen/arch/x86/x86_64/asm-offsets.c @@ -48,6 +48,7 @@ void __dummy__(void) OFFSET(UREGS_eflags, struct cpu_user_regs, rflags); OFFSET(UREGS_rsp, struct cpu_user_regs, rsp); OFFSET(UREGS_ss, struct cpu_user_regs, ss); + OFFSET(UREGS_entry_ssp, struct cpu_user_regs, entry_ssp); OFFSET(UREGS_kernel_sizeof, struct cpu_user_regs, es); BLANK(); --- a/xen/arch/x86/x86_64/entry.S +++ b/xen/arch/x86/x86_64/entry.S @@ -257,7 +257,7 @@ FUNC(lstar_enter) pushq $0 BUILD_BUG_ON(TRAP_syscall & 0xff) movb $TRAP_syscall >> 8, EFRAME_entry_vector + 1(%rsp) - SAVE_ALL + SAVE_ALL ssp=0 SPEC_CTRL_ENTRY_FROM_PV /* Req: %rsp=regs/cpuinfo, %rdx=0, Clob: acd */ /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */ @@ -296,7 +296,7 @@ FUNC(cstar_enter) pushq $0 BUILD_BUG_ON(TRAP_syscall & 0xff) movb $TRAP_syscall >> 8, EFRAME_entry_vector + 1(%rsp) - SAVE_ALL + SAVE_ALL ssp=0 SPEC_CTRL_ENTRY_FROM_PV /* Req: %rsp=regs/cpuinfo, %rdx=0, Clob: acd */ /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */ @@ -339,7 +339,7 @@ LABEL(sysenter_eflags_saved, 0) pushq $0 BUILD_BUG_ON(TRAP_syscall & 0xff) movb $TRAP_syscall >> 8, EFRAME_entry_vector + 1(%rsp) - SAVE_ALL + SAVE_ALL ssp=0 SPEC_CTRL_ENTRY_FROM_PV /* Req: %rsp=regs/cpuinfo, %rdx=0, Clob: acd */ /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */ @@ -394,7 +394,7 @@ FUNC(entry_int80) ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP pushq $0 movb $0x80, EFRAME_entry_vector(%rsp) - SAVE_ALL + SAVE_ALL ssp=0 SPEC_CTRL_ENTRY_FROM_PV /* Req: %rsp=regs/cpuinfo, %rdx=0, Clob: acd */ /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */ --- a/xen/include/public/arch-x86/xen-x86_64.h +++ b/xen/include/public/arch-x86/xen-x86_64.h @@ -183,7 +183,19 @@ struct cpu_user_regs { uint8_t _pad1[3]; __DECL_REG_LO16(flags); /* rflags.IF == !saved_upcall_mask */ __DECL_REG_LO8(sp); - uint16_t ss, _pad2[3]; + uint16_t ss; +#if !defined(__XEN__) + uint16_t _pad2[3]; +#elif defined(COMPILE_OFFSETS) + uint16_t entry_ssp[3]; +#else + /* + * This points _at_ the corresponding shadow stack frame; it is _not_ the + * outer context's SSP. That, if the outer context has CET-SS enabled, + * is stored in the top slot of the pointed to shadow stack frame. + */ + signed long entry_ssp:48; +#endif uint16_t es, _pad3[3]; uint16_t ds, _pad4[3]; uint16_t fs, _pad5[3]; From patchwork Wed Feb 28 13:52:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 13575485 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 7AF0EC5478C for ; Wed, 28 Feb 2024 13:52:43 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.686641.1068895 (Exim 4.92) (envelope-from ) id 1rfKMO-0001cE-1J; Wed, 28 Feb 2024 13:52:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 686641.1068895; Wed, 28 Feb 2024 13:52:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rfKMN-0001c7-Tm; Wed, 28 Feb 2024 13:52:35 +0000 Received: by outflank-mailman (input) for mailman id 686641; Wed, 28 Feb 2024 13:52:34 +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 1rfKMM-0000eZ-49 for xen-devel@lists.xenproject.org; Wed, 28 Feb 2024 13:52:34 +0000 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [2a00:1450:4864:20::636]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 9ea85554-d640-11ee-a1ee-f123f15fe8a2; Wed, 28 Feb 2024 14:52:32 +0100 (CET) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-a43dba50bb7so162533766b.0 for ; Wed, 28 Feb 2024 05:52:32 -0800 (PST) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id s24-20020a170906bc5800b00a3efa4e033asm1870713ejv.151.2024.02.28.05.52.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Feb 2024 05:52:31 -0800 (PST) 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: 9ea85554-d640-11ee-a1ee-f123f15fe8a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1709128352; x=1709733152; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=q/QgX3d2GmmEaeKazC02JMPPR+cdwuaWvIKU6akMN0U=; b=UHGSrD3sxscb+9I8SDvCFpl7XcCK7zxkzdQgPy5rNcF8VEV7/pEbatmfEsco78Lm6F gdQjtxHkE+8R6FX4KTW/leivPFH8Spc+6Gd+awyy1ksoy3Qn7SCxa3XqZJqplshqyf4N 5P7P2kSZp0nehIXtHqCQTJCBBlhdfQDlm/S2vgT6FcKd5hsCNuKyvbk1Ko5QQnKxSAfF tcw+fvBCSxtZwYzQ8SQa5zqqNgsjq6AGCyDa1LAyP9XJinpSaSarWKx4TiNitohq8yhG 2y7xJ+leIpWL2kPeIC+jMhY8ZB5NIBLa4tHokIlGt3RGEeYnp2Q3a0oDPPQgohbrBk9E L+IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709128352; x=1709733152; h=content-transfer-encoding:in-reply-to:autocrypt:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=q/QgX3d2GmmEaeKazC02JMPPR+cdwuaWvIKU6akMN0U=; b=Ogdh2pCWGWaoeu2mafflGFdmYi/VVCebOGldbCbJhO9yrH72X1sJH43KPTw8q+r0Ac oWzxFDwAJP7zye9fCWtfhHTOdgavqBH8TGGO0/MRkaQb99a5LfWETfq1mvfyviAZWNbe rRkA59wmol1iGRVbugbOs0W0TzwwRfo3/9CQcA1DIEix5fTT00qBEUJfusycwCwC/4uw UuMyTdsUPFCSFsXo29ooTCL70BAKJArVMXk3URk3ejxTLY89f4GFBo7t+bw8gZrEM5XJ jigr9VEUggXJ6Iacr7dGBRX7L/EM3Pcj31IA5lDtK6t2brc8zrSFP9wFGIYgTzGIHrXn lfPg== X-Gm-Message-State: AOJu0YzWLSHogjE3LhPLJ3sEpVwnjphp/nm/Y/Ev054AuJV4Deo2v1SP 5QgpsezdR7TdDI9u52gjLso0LcCVM/nCfTqlZ83h9MOQ3IvzPMOe3OAKtPNfRHE9NBBozuQ5r54 = X-Google-Smtp-Source: AGHT+IHw+/IuBLaeeo8x6NbzlhNGLECq9A1xf3dIcqlbJd62TTy6LJ4i9ZBlmf4YPKGTOwnwHCbpJQ== X-Received: by 2002:a17:906:6d45:b0:a43:4c0a:5360 with SMTP id a5-20020a1709066d4500b00a434c0a5360mr5894721ejt.32.1709128351933; Wed, 28 Feb 2024 05:52:31 -0800 (PST) Message-ID: <45f89d82-bdc6-44b6-a784-bcdfdcca403d@suse.com> Date: Wed, 28 Feb 2024 14:52:31 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 3/4] x86/traps: use entry_ssp in fixup_exception_return() Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Wei Liu , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <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xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: <33da6f8b-af22-48c8-acce-3aa55c0e0414@suse.com> With the value recorded on entry there's no need anymore to go hunt for the respective exception frame on the shadow stack. By deriving "ptr" from that field (without any offset), it then ends up pointin one slot lower than before. Therefore all array indexes need incrementing, nicely doing away with all the negative ones. Signed-off-by: Jan Beulich --- Indentation of the prior inner (but not innermost) if()'s body is deliberately left untouched, to aid review. It'll be adjusted in a separate follow-on patch. --- a/xen/arch/x86/traps.c +++ b/xen/arch/x86/traps.c @@ -434,18 +434,6 @@ unsigned long get_stack_trace_bottom(uns } } -static unsigned long get_shstk_bottom(unsigned long sp) -{ - switch ( get_stack_page(sp) ) - { -#ifdef CONFIG_XEN_SHSTK - case 0: return ROUNDUP(sp, IST_SHSTK_SIZE) - sizeof(unsigned long); - case 5: return ROUNDUP(sp, PAGE_SIZE) - sizeof(unsigned long); -#endif - default: return sp - sizeof(unsigned long); - } -} - unsigned long get_stack_dump_bottom(unsigned long sp) { switch ( get_stack_page(sp) ) @@ -837,24 +825,26 @@ static void fixup_exception_return(struc { if ( IS_ENABLED(CONFIG_XEN_SHSTK) ) { - unsigned long ssp, *ptr, *base; + unsigned long ssp = rdssp(); - if ( (ssp = rdssp()) == SSP_NO_SHSTK ) - goto shstk_done; + if ( ssp != SSP_NO_SHSTK ) + { + unsigned long *ptr = _p(regs->entry_ssp); + unsigned long primary_shstk = + (ssp & ~(STACK_SIZE - 1)) + + (PRIMARY_SHSTK_SLOT + 1) * PAGE_SIZE - 8; - ptr = _p(ssp); - base = _p(get_shstk_bottom(ssp)); + BUG_ON((regs->entry_ssp ^ primary_shstk) >> PAGE_SHIFT); - for ( ; ptr < base; ++ptr ) - { /* - * Search for %rip. The shstk currently looks like this: + * The shstk currently looks like this: * * tok [Supervisor token, == &tok | BUSY, only with FRED inactive] * ... [Pointed to by SSP for most exceptions, empty in IST cases] * %cs [== regs->cs] * %rip [== regs->rip] - * SSP [Likely points to 3 slots higher, above %cs] + * SSP [Pointed to by entry_ssp; Likely points to 3 slots + * higher, above %cs] * ... [call tree to this function, likely 2/3 slots] * * and we want to overwrite %rip with fixup. There are two @@ -867,13 +857,10 @@ static void fixup_exception_return(struc * * Check for both regs->rip and regs->cs matching. */ - if ( ptr[0] == regs->rip && ptr[1] == regs->cs ) - { - unsigned long primary_shstk = - (ssp & ~(STACK_SIZE - 1)) + - (PRIMARY_SHSTK_SLOT + 1) * PAGE_SIZE - 8; + BUG_ON(ptr[1] != regs->rip || ptr[2] != regs->cs); - wrss(fixup, ptr); + { + wrss(fixup, &ptr[1]); if ( !stub_ra ) goto shstk_done; @@ -890,7 +877,7 @@ static void fixup_exception_return(struc * - if we're on an IST stack, we need to increment the * original SSP. */ - BUG_ON((ptr[-1] ^ primary_shstk) >> PAGE_SHIFT); + BUG_ON((ptr[0] ^ primary_shstk) >> PAGE_SHIFT); if ( (ssp ^ primary_shstk) >> PAGE_SHIFT ) { @@ -899,37 +886,27 @@ static void fixup_exception_return(struc * addresses actually match. Then increment the interrupted * context's SSP. */ - BUG_ON(stub_ra != *(unsigned long*)ptr[-1]); - wrss(ptr[-1] + 8, &ptr[-1]); + BUG_ON(stub_ra != *(unsigned long*)ptr[0]); + wrss(ptr[0] + 8, &ptr[0]); goto shstk_done; } /* Make sure the two return addresses actually match. */ - BUG_ON(stub_ra != ptr[2]); + BUG_ON(stub_ra != ptr[3]); /* Move exception frame, updating SSP there. */ - wrss(ptr[1], &ptr[2]); /* %cs */ - wrss(ptr[0], &ptr[1]); /* %rip */ - wrss(ptr[-1] + 8, &ptr[0]); /* SSP */ + wrss(ptr[2], &ptr[3]); /* %cs */ + wrss(ptr[1], &ptr[2]); /* %rip */ + wrss(ptr[0] + 8, &ptr[1]); /* SSP */ /* Move all newer entries. */ - while ( --ptr != _p(ssp) ) - wrss(ptr[-1], &ptr[0]); + while ( ptr-- != _p(ssp) ) + wrss(ptr[0], &ptr[1]); /* Finally account for our own stack having shifted up. */ asm volatile ( "incsspd %0" :: "r" (2) ); - - goto shstk_done; } } - - /* - * We failed to locate and fix up the shadow IRET frame. This could - * be due to shadow stack corruption, or bad logic above. We cannot - * continue executing the interrupted context. - */ - BUG(); - } shstk_done: From patchwork Wed Feb 28 13:53:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 13575486 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 05AE1C47DD9 for ; Wed, 28 Feb 2024 13:53:34 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.686644.1068904 (Exim 4.92) (envelope-from ) id 1rfKNC-0002LE-DA; Wed, 28 Feb 2024 13:53:26 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 686644.1068904; Wed, 28 Feb 2024 13:53:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rfKNC-0002L7-Ad; Wed, 28 Feb 2024 13:53:26 +0000 Received: by outflank-mailman (input) for mailman id 686644; Wed, 28 Feb 2024 13:53:24 +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 1rfKNA-0002Kr-CK for xen-devel@lists.xenproject.org; Wed, 28 Feb 2024 13:53:24 +0000 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [2a00:1450:4864:20::62b]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id bc70ed05-d640-11ee-a1ee-f123f15fe8a2; Wed, 28 Feb 2024 14:53:22 +0100 (CET) Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-a3122b70439so694660466b.3 for ; Wed, 28 Feb 2024 05:53:22 -0800 (PST) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id s24-20020a170906bc5800b00a3efa4e033asm1870713ejv.151.2024.02.28.05.53.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Feb 2024 05:53:21 -0800 (PST) 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: bc70ed05-d640-11ee-a1ee-f123f15fe8a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1709128402; x=1709733202; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=8+WqFSaalzUAe+nFKzuTogcSanw3XdVfQLlmS0YufqE=; b=WcbTao1+xDJZDr5rHAtpwwYDm/khjGu5DwDNyCOdvR/GrfV7WbmsLwBGPjl2XXYhUu 8pOFe+yKlNhdSh8k9CViBC/EchGht2418mfV0MkvZvPApGsTbmakJkpDbcm2J0N1TaCm jQeFCyT4Ep1Wt/4MOI8XeeomF7v4gYndEoWK1YQ66uRsHz1xnbysNMxrs8EQoFfShDcg Zy9A9QrmF6MAn1ZOQUS/pP71bQMg4iai0xB1tcoisliXMSRonUoAPdnnFdioD9S4Qvga UVttXBH81PXxJIyC4zUXY+uMU6cGll8XqUHZHaRhFAYzb2kGE9L54nIQGyTFsGDgMle1 2gQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709128402; x=1709733202; h=content-transfer-encoding:in-reply-to:autocrypt:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8+WqFSaalzUAe+nFKzuTogcSanw3XdVfQLlmS0YufqE=; b=sQ2Wlqf4kpv+2DRT0oWjjSVNIbqvr69yaRbGmJVoHsquR07D+0APbe8DmSHvpvcQZ/ Daab6jF3NvnilWEssuQ69EDhV8kqMdoyRVG75mcSSuPnDOY191QA5xI2n66cJ7L+dTFu LIBBY8gMmrHmJgT7MdYexinOV5qhXGE1VJADPUkw+ZVKBFyhjLTPYWn61x1pSD1J49iZ toURCU0aFhEYmE64FpBYAQ0WiAZVQodr6A5ZOiuaxHbTW6nt1CtaNvlJjcT+1uEzj7JG KYxZH2Rr6QK6QZQxgeOz3f2d30xsLp1x6XZYUy0QHFAtBwtFbA/+CIx64VN4GVBGDImh D85w== X-Gm-Message-State: AOJu0Ywg2G7WgmHy7x2xAfRCEfTZpHsYy6RtXNLO30OISlnAMBXDEQwP ulitziYCE1iTZwKWloZMHsSMqfV6/kYW4f00bWaHqzK4G4F0PZqI8++aEMEPqdxjAjkv0XtAMGk = X-Google-Smtp-Source: AGHT+IFXBj6+nUIuDVTPACCxjqiwBVHNhpXEO8GKSg4nnFMo8PfQlrLEa79p1XwGxbyquvxdvyeu3A== X-Received: by 2002:a17:906:1996:b0:a43:f22e:57a6 with SMTP id g22-20020a170906199600b00a43f22e57a6mr1415733ejd.67.1709128401845; Wed, 28 Feb 2024 05:53:21 -0800 (PST) Message-ID: Date: Wed, 28 Feb 2024 14:53:21 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 4/4] x86: prefer shadow stack for producing call traces Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Wei Liu , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <33da6f8b-af22-48c8-acce-3aa55c0e0414@suse.com> Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: <33da6f8b-af22-48c8-acce-3aa55c0e0414@suse.com> Shadow stacks contain little more than return addresses, and they in particular allow precise call traces also without FRAME_POINTER. Signed-off-by: Jan Beulich --- While the 'E' for exception frames is probably okay, I'm not overly happy with the 'C' (for CET). I would have preferred 'S' (for shadow), but we use that character already. As an alternative to suppressing output for the top level exception frame, adding the new code ahead of the 'R' output line (and then also ahead of the stack top read) could be considered. Perhaps having a printk() for the PV entry case is meaningless, for - no frame being pushed when entered from CPL=3 (64-bit PV), - no entry possible from CPL<3 (32-bit PV disabled when CET is active)? In which case the comment probably should just be "Bogus." and the code merely be "break;". Quite likely a number of other uses of is_active_kernel_text() also want amending with in_stub(). --- a/xen/arch/x86/traps.c +++ b/xen/arch/x86/traps.c @@ -449,6 +449,11 @@ unsigned long get_stack_dump_bottom(unsi } } +static bool in_stub(unsigned long addr) +{ + return !((this_cpu(stubs.addr) ^ addr) >> STUB_BUF_SHIFT); +} + #if !defined(CONFIG_FRAME_POINTER) /* @@ -539,6 +544,50 @@ static void show_trace(const struct cpu_ !is_active_kernel_text(tos) ) printk(" [<%p>] R %pS\n", _p(regs->rip), _p(regs->rip)); + if ( IS_ENABLED(CONFIG_XEN_SHSTK) && rdssp() != SSP_NO_SHSTK ) + { + const unsigned long *ptr = _p(regs->entry_ssp); + unsigned int n; + + for ( n = 0; (unsigned long)ptr & (PAGE_SIZE - sizeof(*ptr)); ++n ) + { + unsigned long val = *ptr; + + if ( is_active_kernel_text(val) || in_stub(val) ) + { + /* Normal return address entry. */ + printk(" [<%p>] C %pS\n", _p(val), _p(val)); + ++ptr; + } + else if ( !((val ^ *ptr) >> (PAGE_SHIFT + STACK_ORDER)) ) + { + if ( val & (sizeof(val) - 1) ) + { + /* Most likely a supervisor token. */ + break; + } + + /* + * Ought to be a hypervisor interruption frame. But don't + * (re)log the current frame's %rip. + */ + if ( n || ptr[1] != regs->rip ) + printk(" [<%p>] E %pS\n", _p(ptr[1]), _p(ptr[1])); + ptr = _p(val); + } + else + { + /* Ought to be a PV guest hypercall/interruption frame. */ + printk(" %04lx:[<%p>] E\n", ptr[2], _p(ptr[1])); + ptr = 0; + } + } + + /* Fall back to legacy stack trace if nothing was logged at all. */ + if ( n ) + return; + } + if ( fault ) { printk(" [Fault on access]\n");