public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
@ 2020-07-14 21:21 carlos at redhat dot com
  2020-07-15  6:10 ` [Bug middle-end/96200] " fw at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: carlos at redhat dot com @ 2020-07-14 21:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200

            Bug ID: 96200
           Summary: Implement __builtin_thread_pointer() and
                    __builtin_set_thread_pointer() if TLS is supported
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carlos at redhat dot com
  Target Milestone: ---

In glibc we implement the equivalent of __builtin_thread_pointer() and
__builtin_set_thread_pointer() to access data that is at a constant offset from
the thread pointer.

For many of the architectures that have the builtins their tls.h implementation
in glibc is greatly simplified because they can use the builtin.

We have some ABIs that are defined as having data of a certain size and
alignment at a fixed offset from the thread pointer e.g. stack_guard,
pointer_guard, TM ABI, __private_ss (split stack) etc.

In relation to restartable sequences in the linux kernel it was discussed that
it might be interesting to have an ABI that also uses TP + offset, where glibc
provides the "offset" via an API, but the user is left to do the "TP + offset"
calculation on their own.

https://lore.kernel.org/linux-api/2452161.11491.1594732791558.JavaMail.zimbra@efficios.com/
"Is there an arch-agnostic way to get the thread pointer from user-space code ?
That would be needed by all rseq critical section implementations."

We should support these two builtins for all targets that support TLS.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/96200] Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
  2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
@ 2020-07-15  6:10 ` fw at gcc dot gnu.org
  2020-07-15 19:13 ` carlos at redhat dot com
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: fw at gcc dot gnu.org @ 2020-07-15  6:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200

Florian Weimer <fw at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fw at gcc dot gnu.org

--- Comment #1 from Florian Weimer <fw at gcc dot gnu.org> ---
__builtin_set_thread_pointer has little value from a glibc perspective because
when used in application code, it will always result in undefined behavior.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/96200] Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
  2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
  2020-07-15  6:10 ` [Bug middle-end/96200] " fw at gcc dot gnu.org
@ 2020-07-15 19:13 ` carlos at redhat dot com
  2020-08-28 23:17 ` hjl.tools at gmail dot com
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: carlos at redhat dot com @ 2020-07-15 19:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200

--- Comment #2 from Carlos O'Donell <carlos at redhat dot com> ---
(In reply to Florian Weimer from comment #1)
> __builtin_set_thread_pointer has little value from a glibc perspective
> because when used in application code, it will always result in undefined
> behavior.

Correct, but it's useful for a C library implemetnation. In glibc the aarch64
tls.h is made quite clean by the use of __builtin_thread_pointer and
__builtin_set_thread_pointer.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/96200] Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
  2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
  2020-07-15  6:10 ` [Bug middle-end/96200] " fw at gcc dot gnu.org
  2020-07-15 19:13 ` carlos at redhat dot com
@ 2020-08-28 23:17 ` hjl.tools at gmail dot com
  2020-08-29  0:49 ` hjl.tools at gmail dot com
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: hjl.tools at gmail dot com @ 2020-08-28 23:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200

H.J. Lu <hjl.tools at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |crazylht at gmail dot com,
                   |                            |hjl.tools at gmail dot com,
                   |                            |wwwhhhyyy333 at gmail dot com

--- Comment #3 from H.J. Lu <hjl.tools at gmail dot com> ---
We will take a look at x86 backend.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/96200] Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
  2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
                   ` (2 preceding siblings ...)
  2020-08-28 23:17 ` hjl.tools at gmail dot com
@ 2020-08-29  0:49 ` hjl.tools at gmail dot com
  2020-08-29  2:07 ` hjl.tools at gmail dot com
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: hjl.tools at gmail dot com @ 2020-08-29  0:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200

--- Comment #4 from H.J. Lu <hjl.tools at gmail dot com> ---
On Linux/i386 and Linux/x86-64, thread pointer access is done via syscall.
On Linux/x86-64, __builtin_thread_pointer and __builtin_set_thread_pointer
may be implemented with FSGSBASE ISA.  Is it possible to implement these
builtins on Linux/i386 and Linux/x86-64 for all processors?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/96200] Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
  2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
                   ` (3 preceding siblings ...)
  2020-08-29  0:49 ` hjl.tools at gmail dot com
@ 2020-08-29  2:07 ` hjl.tools at gmail dot com
  2020-08-29  5:48 ` fw at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: hjl.tools at gmail dot com @ 2020-08-29  2:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200

--- Comment #5 from H.J. Lu <hjl.tools at gmail dot com> ---
But

CR4.FSGSBASE

FSGSBASE-Enable Bit (bit 16 of CR4) — Enables the instructions RDFSBASE,
RDGSBASE, WRFSBASE, and WRGSBASE.

>From user space, we can't tell if FSGSBASE is enabled.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/96200] Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
  2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
                   ` (4 preceding siblings ...)
  2020-08-29  2:07 ` hjl.tools at gmail dot com
@ 2020-08-29  5:48 ` fw at gcc dot gnu.org
  2020-08-29 12:23 ` hjl.tools at gmail dot com
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: fw at gcc dot gnu.org @ 2020-08-29  5:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200

--- Comment #6 from Florian Weimer <fw at gcc dot gnu.org> ---
(In reply to H.J. Lu from comment #4)
> On Linux/i386 and Linux/x86-64, thread pointer access is done via syscall.
> On Linux/x86-64, __builtin_thread_pointer and __builtin_set_thread_pointer
> may be implemented with FSGSBASE ISA.  Is it possible to implement these
> builtins on Linux/i386 and Linux/x86-64 for all processors?

It's effectively part of the x86-64 ABI, but I think it's currently
undocumented. On x86-64, it looks like this:

static inline void *
thread_pointer (void)
{
  void *result;
  asm ("mov %%fs:0, %0" : "=r" (result));
  return result;
}

i386 is similar, but with %gs, I think.

This is ABI since the early NPTL days, and GCC knows about this very
explicitly, to implement the -mno-tls-direct-seg-refs option.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/96200] Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
  2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
                   ` (5 preceding siblings ...)
  2020-08-29  5:48 ` fw at gcc dot gnu.org
@ 2020-08-29 12:23 ` hjl.tools at gmail dot com
  2020-08-29 12:57 ` fw at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: hjl.tools at gmail dot com @ 2020-08-29 12:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200

--- Comment #7 from H.J. Lu <hjl.tools at gmail dot com> ---
(In reply to Florian Weimer from comment #6)
> (In reply to H.J. Lu from comment #4)
> > On Linux/i386 and Linux/x86-64, thread pointer access is done via syscall.
> > On Linux/x86-64, __builtin_thread_pointer and __builtin_set_thread_pointer
> > may be implemented with FSGSBASE ISA.  Is it possible to implement these
> > builtins on Linux/i386 and Linux/x86-64 for all processors?
> 
> It's effectively part of the x86-64 ABI, but I think it's currently
> undocumented. On x86-64, it looks like this:
> 
> static inline void *
> thread_pointer (void)
> {
>   void *result;
>   asm ("mov %%fs:0, %0" : "=r" (result));
>   return result;
> }
> 
> i386 is similar, but with %gs, I think.
> 
> This is ABI since the early NPTL days, and GCC knows about this very
> explicitly, to implement the -mno-tls-direct-seg-refs option.

Give that the tcb field is setup by the C run-time on Linux/x86, should
it be provided by a run-time header file?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/96200] Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
  2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
                   ` (6 preceding siblings ...)
  2020-08-29 12:23 ` hjl.tools at gmail dot com
@ 2020-08-29 12:57 ` fw at gcc dot gnu.org
  2020-08-29 15:34 ` hjl.tools at gmail dot com
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: fw at gcc dot gnu.org @ 2020-08-29 12:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200

--- Comment #8 from Florian Weimer <fw at gcc dot gnu.org> ---
(In reply to H.J. Lu from comment #7)
> Give that the tcb field is setup by the C run-time on Linux/x86, should
> it be provided by a run-time header file?

Yes, it seems reasonable to me. Ideally, it would be documented in the ABI
manual as well.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/96200] Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
  2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
                   ` (7 preceding siblings ...)
  2020-08-29 12:57 ` fw at gcc dot gnu.org
@ 2020-08-29 15:34 ` hjl.tools at gmail dot com
  2020-09-07 14:02 ` jakub at gcc dot gnu.org
  2020-09-09 11:05 ` hjl.tools at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: hjl.tools at gmail dot com @ 2020-08-29 15:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200

--- Comment #9 from H.J. Lu <hjl.tools at gmail dot com> ---
(In reply to Florian Weimer from comment #8)
> (In reply to H.J. Lu from comment #7)
> > Give that the tcb field is setup by the C run-time on Linux/x86, should
> > it be provided by a run-time header file?
> 
> Yes, it seems reasonable to me. Ideally, it would be documented in the ABI
> manual as well.

https://gitlab.com/x86-psABIs/x86-64-ABI/-/merge_requests/14

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/96200] Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
  2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
                   ` (8 preceding siblings ...)
  2020-08-29 15:34 ` hjl.tools at gmail dot com
@ 2020-09-07 14:02 ` jakub at gcc dot gnu.org
  2020-09-09 11:05 ` hjl.tools at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-07 14:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Florian Weimer from comment #6)
> (In reply to H.J. Lu from comment #4)
> > On Linux/i386 and Linux/x86-64, thread pointer access is done via syscall.
> > On Linux/x86-64, __builtin_thread_pointer and __builtin_set_thread_pointer
> > may be implemented with FSGSBASE ISA.  Is it possible to implement these
> > builtins on Linux/i386 and Linux/x86-64 for all processors?
> 
> It's effectively part of the x86-64 ABI, but I think it's currently
> undocumented. On x86-64, it looks like this:
> 
> static inline void *
> thread_pointer (void)
> {
>   void *result;
>   asm ("mov %%fs:0, %0" : "=r" (result));
>   return result;
> }
> 
> i386 is similar, but with %gs, I think.
> 
> This is ABI since the early NPTL days, and GCC knows about this very
> explicitly, to implement the -mno-tls-direct-seg-refs option.

It is documented, see https://akkadia.org/drepper/tls.pdf
"The only requirement about this register is that the actual thread pointer tpt
can be loaded from the absolute address 0 via the %gs register. The following
code would load the thread pointer in the %eax register:
movl %gs:0, %eax"
for ia32.  For x86_64, it is not spelled explicitly, but it is implicit from:
"The x86-64 ABI is at its base virtually the same as the IA-32 ABI. The
difference is mainly in different size of variables containing pointers and
that it only provides one variant which closely matches the IA-32 GNU variant.
Instead of segment register %gs it uses the %fs segment register."
as well as all the explicit insn sequences that use it.  The psABI doesn't have
TLS details I believe, so Ulrich's tls.pdf is the ABI document that covers
that.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/96200] Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported
  2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
                   ` (9 preceding siblings ...)
  2020-09-07 14:02 ` jakub at gcc dot gnu.org
@ 2020-09-09 11:05 ` hjl.tools at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: hjl.tools at gmail dot com @ 2020-09-09 11:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96200
Bug 96200 depends on bug 96955, which changed state.

Bug 96955 Summary: Implement __builtin_thread_pointer for x86 TLS
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96955

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |FIXED

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2020-09-09 11:05 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-14 21:21 [Bug middle-end/96200] New: Implement __builtin_thread_pointer() and __builtin_set_thread_pointer() if TLS is supported carlos at redhat dot com
2020-07-15  6:10 ` [Bug middle-end/96200] " fw at gcc dot gnu.org
2020-07-15 19:13 ` carlos at redhat dot com
2020-08-28 23:17 ` hjl.tools at gmail dot com
2020-08-29  0:49 ` hjl.tools at gmail dot com
2020-08-29  2:07 ` hjl.tools at gmail dot com
2020-08-29  5:48 ` fw at gcc dot gnu.org
2020-08-29 12:23 ` hjl.tools at gmail dot com
2020-08-29 12:57 ` fw at gcc dot gnu.org
2020-08-29 15:34 ` hjl.tools at gmail dot com
2020-09-07 14:02 ` jakub at gcc dot gnu.org
2020-09-09 11:05 ` hjl.tools at gmail dot com

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).