linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ammar Faizi <ammarfaizi2@gnuweeb.org>
To: "H. Peter Anvin" <hpa@zytor.com>
Cc: Xin Li <xin3.li@intel.com>, Dave Hansen <dave.hansen@intel.com>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Andrew Cooper <Andrew.Cooper3@citrix.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Brian Gerst <brgerst@gmail.com>, Borislav Petkov <bp@alien8.de>,
	Shuah Khan <shuah@kernel.org>, Ingo Molnar <mingo@kernel.org>,
	Andy Lutomirski <luto@kernel.org>,
	x86 Mailing List <x86@kernel.org>,
	Linux Kselftest Mailing List  <linux-kselftest@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [RFC PATCH v1 1/2] selftests/x86: sysret_rip: Handle syscall in a FRED system
Date: Fri, 27 Jan 2023 03:16:25 +0700	[thread overview]
Message-ID: <Y9LfmQ/r1/pEP+uv@biznet-home.integral.gnuweeb.org> (raw)
In-Reply-To: <8f5c24df-514d-5d89-f58f-ec8c3eb1e049@zytor.com>


[ Resending, missed Xin Li from the Cc list... ]

On Mon, Jan 23, 2023 at 05:40:23PM -0800, H. Peter Anvin wrote:
> So as per Andrew's comment, add:
> 
> register void * rsp asm("%rsp");
> 
> ...
> 
> "+r" (rsp)	/* clobber the redzone */
> 
> ... as the right way to avoid redzone problems.

I played with this more. I found something wrong with this. This doesn't
work for me. The compiler still uses red zone despite I use "+r" (rsp).

What did I do wrong?

-----

ammarfaizi2@integral2:/tmp$ gcc -fno-stack-protector -O2 -Wall -Wextra test.c -o test
ammarfaizi2@integral2:/tmp$ objdump --no-show-raw-insn -d test | grep "a_leaf_func_with_red_zone>:" -A8
0000000000001180 <a_leaf_func_with_red_zone>:
    1180:  endbr64 
    1184:  mov    $0x1,%eax
    1189:  mov    %rax,-0x8(%rsp)   ## BUG!!!
    118e:  pushf  
    118f:  pop    %rax
    1190:  mov    -0x8(%rsp),%rax   ## BUG!!!
    1195:  ret


ammarfaizi2@integral2:/tmp$ clang -O2 -Wall -Wextra test.c -o test
ammarfaizi2@integral2:/tmp$ objdump --no-show-raw-insn -d test | grep "a_leaf_func_with_red_zone>:" -A6
0000000000001140 <a_leaf_func_with_red_zone>:
    1140:  mov    $0x1,%eax
    1145:  mov    %rax,-0x8(%rsp)   ## BUG!!!
    114a:  pushf  
    114b:  pop    %rax
    114c:  mov    -0x8(%rsp),%rax   ## BUG!!!
    1151:  ret


-----
ammarfaizi2@integral2:~$ gcc --version
gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ammarfaizi2@integral2:~$ clang --version
Ubuntu clang version 16.0.0 (++20230124031324+d63e492562f2-1~exp1~20230124151444.705)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin


-----
test.c:

#include <stdio.h>
static inline void clobber_redzone(void)
{
        register void *rsp __asm__("%rsp");
        unsigned long rflags;

        __asm__ volatile ("pushf; popq %1"
                          : "+r" (rsp), "=r" (rflags));
}

static inline void set_red_zone(long *mem, long val)
{
        __asm__ volatile ("movq %[val], %[mem]"
                           : [mem] "=m" (*mem)
                           : [val] "r" (val));
}

static inline long get_red_zone(long *mem)
{
        long ret;

        __asm__ volatile ("movq %[in], %[out]"
                           : [out] "=r" (ret)
                           : [in] "m" (*mem));
        return ret;
}

__attribute__((__noinline__))
long a_leaf_func_with_red_zone(void)
{
        long x;

        set_red_zone(&x, 1);
        clobber_redzone();
        /* The correct retval is 1 */
        return get_red_zone(&x);
}

int main(void)
{
        printf("ret = %ld\n", a_leaf_func_with_red_zone());
        return 0;
}

-- 
Ammar Faizi



  parent reply	other threads:[~2023-01-26 20:16 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <SA1PR11MB6734FA9139B9C9F6CC2ED123A8C59@SA1PR11MB6734.namprd11.prod.outlook.com>
2023-01-20 17:45 ` the x86 sysret_rip test fails on the Intel FRED architecture Dave Hansen
     [not found]   ` <eb81f7f2-d266-d999-b41a-e6eae086e731@citrix.com>
2023-01-20 20:50     ` H. Peter Anvin
2023-01-20 21:10       ` Andrew Cooper
2023-01-20 21:17         ` H. Peter Anvin
2023-01-20 21:29           ` Andrew Cooper
2023-01-21  4:59   ` H. Peter Anvin
2023-01-21 16:46     ` Dave Hansen
2023-01-21 21:47       ` Brian Gerst
2023-01-22  3:01         ` Li, Xin3
2023-01-22  3:28           ` H. Peter Anvin
2023-01-22  3:38     ` Li, Xin3
2023-01-22  4:34       ` Dave Hansen
2023-01-22  4:44         ` H. Peter Anvin
2023-01-22  8:22           ` Li, Xin3
2023-01-22  8:54             ` Ammar Faizi
2023-01-22  9:40               ` H. Peter Anvin
2023-01-22 23:45         ` H. Peter Anvin
2023-01-23  9:02           ` Ammar Faizi
2023-01-23 19:43             ` H. Peter Anvin
2023-01-23 23:43               ` Ammar Faizi
2023-01-23 23:58                 ` H. Peter Anvin
2023-01-24  0:26                   ` [RFC PATCH v1 0/2] selftests/x86: sysret_rip update for FRED system Ammar Faizi
2023-01-24  0:26                     ` [RFC PATCH v1 1/2] selftests/x86: sysret_rip: Handle syscall in a " Ammar Faizi
2023-01-24  1:40                       ` H. Peter Anvin
2023-01-24  2:31                         ` Ammar Faizi
2023-01-26 20:08                         ` Ammar Faizi
2023-02-15  9:17                           ` Andrew Cooper
2023-02-15 10:29                             ` Andrew Cooper
2023-02-15 10:44                               ` Ammar Faizi
2023-02-15 10:42                             ` Ammar Faizi
2023-01-26 20:16                         ` Ammar Faizi [this message]
2023-01-24  0:26                     ` [RFC PATCH v1 2/2] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-23 23:53             ` the x86 sysret_rip test fails on the Intel FRED architecture Andrew Cooper
2023-01-24  0:01               ` H. Peter Anvin
2023-01-24  2:27                 ` [RFC PATCH v2 0/2] selftests/x86: sysret_rip update for FRED system Ammar Faizi
2023-01-24  2:27                   ` [RFC PATCH v2 1/2] selftests/x86: sysret_rip: Handle syscall in a " Ammar Faizi
2023-01-24  5:44                     ` H. Peter Anvin
2023-01-24  2:27                   ` [RFC PATCH v2 2/2] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-24  6:16                     ` H. Peter Anvin
2023-01-24  6:41                       ` Ammar Faizi
2023-01-24  6:47                         ` Ammar Faizi
2023-01-24  9:07                         ` H. Peter Anvin
2023-01-24  9:12                           ` Ammar Faizi
2023-01-24 10:09                             ` [RFC PATCH v3 0/2] selftests/x86: sysret_rip update for FRED system Ammar Faizi
2023-01-24 10:09                               ` [RFC PATCH v3 1/2] selftests/x86: sysret_rip: Handle syscall in a " Ammar Faizi
2023-01-24 10:09                               ` [RFC PATCH v3 2/2] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-24 20:59                                 ` H. Peter Anvin
2023-01-25  3:29                                   ` Ammar Faizi
2023-01-24 21:32                               ` [RFC PATCH v3 0/2] selftests/x86: sysret_rip update for FRED system Li, Xin3
2023-01-24 21:37                                 ` H. Peter Anvin
2023-01-24 23:20                                   ` Li, Xin3
2023-01-25  3:27                                   ` Ammar Faizi
2023-01-24 21:51                                 ` Andrew Cooper
2023-01-24 23:58                                   ` Li, Xin3
2023-01-25  3:22                             ` [RFC PATCH v4 0/2] sysret_rip update for the Intel FRED architecture Ammar Faizi
2023-01-25  3:22                               ` [RFC PATCH v4 1/2] selftests/x86: sysret_rip: Handle syscall in a FRED system Ammar Faizi
2023-01-25  3:37                                 ` Ammar Faizi
2023-01-25  3:44                                   ` Ammar Faizi
2023-01-25  3:22                               ` [RFC PATCH v4 2/2] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-25  3:49                             ` [RFC PATCH v5 0/2] sysret_rip update for the Intel FRED architecture Ammar Faizi
2023-01-25  3:49                               ` [RFC PATCH v5 1/2] selftests/x86: sysret_rip: Handle syscall in a FRED system Ammar Faizi
2023-01-25  8:39                                 ` H. Peter Anvin
2023-01-25  8:53                                   ` Ammar Faizi
2023-01-25  9:57                                   ` Ammar Faizi
2023-01-25 10:01                                     ` Ammar Faizi
2023-01-25 10:17                                     ` H. Peter Anvin
2023-01-25 11:37                                       ` Ammar Faizi
2023-01-25 17:25                                         ` H. Peter Anvin
2023-01-25  3:49                               ` [RFC PATCH v5 2/2] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-25  8:22                               ` [RFC PATCH v5 0/2] sysret_rip update for the Intel FRED architecture Li, Xin3
2023-01-25  8:32                                 ` Ammar Faizi
2023-01-25 17:07                                   ` Li, Xin3
2023-01-25 17:24                                     ` H. Peter Anvin
2023-01-25 17:41                                       ` Ammar Faizi
2023-01-25 17:48                                         ` Li, Xin3
2023-02-15  7:42                                           ` Li, Xin3
2023-02-15  7:51                                             ` Ammar Faizi
2023-02-18  4:27                                             ` Ammar Faizi
2023-02-18  4:51                                               ` H. Peter Anvin
2023-01-25 21:17                             ` [RFC PATCH v6 0/3] " Ammar Faizi
2023-01-25 21:17                               ` [RFC PATCH v6 1/3] selftests/x86: sysret_rip: Handle syscall in a FRED system Ammar Faizi
2023-01-25 23:01                                 ` Ammar Faizi
2023-01-25 21:17                               ` [RFC PATCH v6 2/3] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-25 21:17                               ` [RFC PATCH v6 3/3] selftests/x86: sysret_rip: Test opportunistic SYSRET Ammar Faizi
2023-01-25 23:24                             ` [RFC PATCH v7 0/3] sysret_rip update for the Intel FRED architecture Ammar Faizi
2023-01-25 23:24                               ` [RFC PATCH v7 1/3] selftests/x86: sysret_rip: Handle syscall in a FRED system Ammar Faizi
2023-01-25 23:24                               ` [RFC PATCH v7 2/3] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Ammar Faizi
2023-01-25 23:24                               ` [RFC PATCH v7 3/3] selftests/x86: sysret_rip: Test SYSRET with a signal handler Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y9LfmQ/r1/pEP+uv@biznet-home.integral.gnuweeb.org \
    --to=ammarfaizi2@gnuweeb.org \
    --cc=Andrew.Cooper3@citrix.com \
    --cc=bp@alien8.de \
    --cc=brgerst@gmail.com \
    --cc=dave.hansen@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=shuah@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    --cc=xin3.li@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).