public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: Alexander Monakov <amonakov@ispras.ru>
Cc: Uros Bizjak <ubizjak@gmail.com>, Jakub Jelinek <jakub@redhat.com>,
	 GCC Development <gcc@gcc.gnu.org>, X86 ML <x86@kernel.org>,
	Andy Lutomirski <luto@amacapital.net>
Subject: Re: typeof and operands in named address spaces
Date: Thu, 5 Nov 2020 13:24:30 +0100	[thread overview]
Message-ID: <CAFiYyc3HcJBwmN+EmVKW4_R0VVQM6dsBtHsGwjyfsKza5QNFcw@mail.gmail.com> (raw)
In-Reply-To: <alpine.LNX.2.20.13.2011051506090.9902@monopod.intra.ispras.ru>

On Thu, Nov 5, 2020 at 1:16 PM Alexander Monakov via Gcc
<gcc@gcc.gnu.org> wrote:
>
>
>
> On Thu, 5 Nov 2020, Uros Bizjak wrote:
>
> > On Thu, Nov 5, 2020 at 12:38 PM Alexander Monakov <amonakov@ispras.ru> wrote:
> > >
> > > On Thu, 5 Nov 2020, Uros Bizjak via Gcc wrote:
> > >
> > > > > What is the usecase for stripping the address space for asm operands?
> > > >
> > > > Please see the end of [2], where the offset to <mem> is passed in %rsi
> > > > to the call to this_cpu_cmpxchg16b_emu. this_cpu_cmpxchg16b_emu
> > > > implements access with PER_CPU_VAR((%rsi)), which expands to
> > > > %gs:(%rsi), so it is the same as %gs:<mem> in cmpxchg16b alternative.
> > > > The offset is loaded by lea <mem>, %rsi to %rsi reg.
> > >
> > > I see, thanks. But then with the typeof-stripping-address-space solution
> > > you'd be making a very evil cast (producing address of an object that
> > > does not actually exist in the generic address space). I can write such
> > > a solution, but it is clearly Undefined Behavior:
> > >
> > > #define strip_as(mem) (*(__typeof(0?(mem):(mem))*)(intptr_t)&(mem))
> > >
> > > void foo(__seg_fs int *x)
> > > {
> > >   asm("# %0" :: "m"(x[1]));
> > >   asm("# %0" :: "m"(strip_as(x[1])));
> > > }
> > >
> > > yields
> > >
> > > foo:
> > >         # %fs:4(%rdi)
> > >         # 4(%rdi)
> > >         ret
> > >
> > >
> > > I think a clean future solution is adding a operand modifier that would
> > > print the memory operand without the segment prefix.
> >
> > I was also thinking of introducing of operand modifier, but Richi
> > advises the following:
> >
> > --cut here--
> > typedef __UINTPTR_TYPE__ uintptr_t;
> >
> > __seg_fs int x;
> >
> > uintptr_t test (void)
> > {
> >  uintptr_t *p = (uintptr_t *)(uintptr_t) &x;
> >  uintptr_t addr;
> >
> >  asm volatile ("lea %1, %0" : "=r"(addr) : "m"(*p));
> >
> >  return addr;
> > }
>
> This is even worse undefined behavior compared to my solution above:
> this code references memory in uintptr_t type, while mine preserves the
> original type via __typeof. So this can visibly break with TBAA (though
> the kernel uses -fno-strict-aliasing, so this particular concern wouldn't
> apply there).
>
> If you don't care about preserving sizeof and type you can use a cast to char:
>
> #define strip_as(mem) (*(char *)(intptr_t)&(mem))

But in the end, on x86 the (uintptr_t)&x cast yields you exactly
the offset from the segment register, no?  The casting back
to (uintrptr_t *) and the "dereference" is just because the
inline asm is not able to build the lea otherwise?  that said,
sth like

 asm volatile ("lea fs:%1, %0" : "=r"(addr) : "r" ((uintptr_t)&x));

with the proper asm template should likely be used.

Of course in case the kernel wants transparent handling of
non-fs and fs-based cases that will be off.

Richard.

>
> Alexander

  reply	other threads:[~2020-11-05 12:24 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-04 18:31 Uros Bizjak
2020-11-05  7:26 ` Richard Biener
2020-11-05  8:56   ` Uros Bizjak
2020-11-05  9:36     ` Alexander Monakov
2020-11-05 10:33       ` Uros Bizjak
2020-11-05 11:38         ` Alexander Monakov
2020-11-05 12:00           ` Uros Bizjak
2020-11-05 12:14             ` Alexander Monakov
2020-11-05 12:24               ` Richard Biener [this message]
2020-11-05 12:32                 ` Uros Bizjak
2020-11-05 12:35                   ` Uros Bizjak
2020-11-05 13:22                     ` Alexander Monakov
2020-11-05 13:39                       ` Alexander Monakov
2020-11-05 13:46                         ` Uros Bizjak
2020-11-05 12:26               ` Uros Bizjak
2020-11-05 15:27                 ` Andy Lutomirski
2020-11-05 11:03       ` Uros Bizjak
2020-11-05  9:45     ` Richard Biener
2020-11-05  9:51       ` Jakub Jelinek
2020-11-09 12:47 ` Peter Zijlstra
2020-11-09 19:38   ` Segher Boessenkool
2020-11-09 19:50     ` Nick Desaulniers
2020-11-10  7:57       ` Peter Zijlstra
2020-11-10 18:42         ` Nick Desaulniers
2020-11-10 20:11           ` Peter Zijlstra
2020-11-12  0:40             ` Segher Boessenkool
2020-11-12  0:47         ` Segher Boessenkool
2020-11-10  7:52     ` Peter Zijlstra

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=CAFiYyc3HcJBwmN+EmVKW4_R0VVQM6dsBtHsGwjyfsKza5QNFcw@mail.gmail.com \
    --to=richard.guenther@gmail.com \
    --cc=amonakov@ispras.ru \
    --cc=gcc@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=luto@amacapital.net \
    --cc=ubizjak@gmail.com \
    --cc=x86@kernel.org \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).