Using Thread Local Storage on riscv

776 views
Skip to first unread message

Anitha Quest

unread,
Jul 6, 2021, 2:48:30 AM7/6/21
to RISC-V SW Dev
Hi All, 
I am trying to use Thread Local Storage, understand the usage on riscv 

Below is what I am doing : 

referring to TLS section in below doc

1) I have the appropriate attributes for TLS variables 

__thread int TLS_var1 __attribute__((tls_model("local-exec")));
__thread int TLS_var2 __attribute__((tls_model("local-exec")))
posix_thread1(){
TLS_var1++;
..
}
posix_thread1(){
TLS_var2++;
..
}

2) linker script has the following sections 

/* Thread Local Storage sections  */
  .tdata  :
   {
     PROVIDE_HIDDEN (__tdata_start = .);
     *(.tdata .tdata.* .gnu.linkonce.td.*)
   }
  .tbss  : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }

3) I am building the code with compiler options 
-mcmodel=medany -static -march=rv64imac -mabi=lp64 -ggdb -Og -ftls-model=local-exec

Questions : 
I was not sure what should $tp (thread pointer) be assigned to ? 
 How to find out the start of thread control block area ?

Please let me know, what I am missing to get a basic program using TLS working. 
Thanks,
Anitha


Jim Wilson

unread,
Jul 6, 2021, 7:12:13 PM7/6/21
to Anitha Quest, RISC-V SW Dev
On Mon, Jul 5, 2021 at 11:48 PM Anitha Quest <anitha....@gmail.com> wrote:
I am trying to use Thread Local Storage, understand the usage on riscv 
Questions : 
I was not sure what should $tp (thread pointer) be assigned to ? 
 How to find out the start of thread control block area ?

The OS creates the thread control block and sets $tp when starting a process.  If you are using a linux compiler and linux simulator/hardware, then TLS should just work.  If you are using an embedded elf compiler, then you need to write some basic OS support to manage processes and TLS which is probably a fair amount of work.

Jim

Sober Liu

unread,
Jul 6, 2021, 8:25:03 PM7/6/21
to Jim Wilson, Anitha Quest, RISC-V SW Dev

You may find example in riscv-test.

To init tp and sp, https://github.com/riscv/riscv-tests/blob/master/benchmarks/common/crt.S#L119

To copy tls data for each hart: https://github.com/riscv/riscv-tests/blob/master/benchmarks/common/syscalls.c#L96

 

 

From: Jim Wilson <ji...@sifive.com>
Sent: 202177 7:12
To: Anitha Quest <anitha....@gmail.com>
Cc: RISC-V SW Dev <sw-...@groups.riscv.org>
Subject: Re: [sw-dev] Using Thread Local Storage on riscv

 

External email: Use caution opening links or attachments

 

--
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+un...@groups.riscv.org.
To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/CAFyWVaZsWt8Cc8_E2NsPbJXoPwN9XZYkDQUnPTRrSK26Zun-gQ%40mail.gmail.com.

Anitha Quest

unread,
Jul 22, 2021, 1:12:59 PM7/22/21
to Sober Liu, Jim Wilson, RISC-V SW Dev
Thanks for the info Liu and Jim, 
I have a follow on question. 
Local Exec , Initial Exec ..., what is the default TLS model when no compiler flag is specified ? i am guessing its Local Exec as i using static linking ? 

Let me know, 
Anee


Tommy Murphy

unread,
Jul 22, 2021, 5:32:24 PM7/22/21
to Sober Liu, Anitha Quest, Jim Wilson, RISC-V SW Dev
Any use?

-ftls-model=model
Alter the thread-local storage model to be used (see Thread-Local). The model argument should be one of ‘global-dynamic’, ‘local-dynamic’, ‘initial-exec’ or ‘local-exec’. Note that the choice is subject to optimization: the compiler may use a more efficient model for symbols not visible outside of the translation unit, or if -fpic is not given on the command line.

The default without -fpic is ‘initial-exec’; with -fpic the default is ‘global-dynamic’.

From: Anitha Quest <anitha....@gmail.com>
Sent: Thursday, July 22, 2021 6:12:44 PM
To: Sober Liu <sob...@nvidia.com>
Cc: Jim Wilson <ji...@sifive.com>; RISC-V SW Dev <sw-...@groups.riscv.org>

Jim Wilson

unread,
Jul 22, 2021, 5:56:52 PM7/22/21
to Anitha Quest, Sober Liu, RISC-V SW Dev
On Thu, Jul 22, 2021 at 10:12 AM Anitha Quest <anitha....@gmail.com> wrote:
Local Exec , Initial Exec ..., what is the default TLS model when no compiler flag is specified ? i am guessing its Local Exec as i using static linking ? 

The ultimate reference for TLS is
It is very technical but explains how everything works.

GCC defaults to the global exec model, but optimizes to a more efficient tls model if it can.  The code is
So if not a shared library and local, then local exec model.  If not a shared library and not local, then initial exec model.  If optimizing and local, then local dynamic.  Otherwise, global dynamic.  And -ftls-model= sets the default which is global dynamic if the option is not used.  The code will then be further optimized at link time if possible.

Jim

Anitha Quest

unread,
Jul 26, 2021, 2:31:56 AM7/26/21
to Jim Wilson, Sober Liu, RISC-V SW Dev
Thanks! Jim

Tommy Murphy

unread,
Jul 26, 2021, 4:19:24 AM7/26/21
to Jim Wilson, Anitha Quest, Sober Liu, RISC-V SW Dev
Hi Jim

GCC defaults to the global exec model, but optimizes to a more efficient tls model if it can. 

That's not what the GCC manual says.
Is it incorrect?
For RISC-V specifically?
In general?

Jim Wilson

unread,
Jul 26, 2021, 12:56:32 PM7/26/21
to Tommy Murphy, Anitha Quest, Sober Liu, RISC-V SW Dev
Everything is in agreement here.

-fpic means compile for a shared library.  So it is global-dynamic for a shared library and initial-exec if not a shared library.

Internally, gcc sets the default tls model to global-dynamic, and then optimizes it to initial-exec when not -fpic.  But exactly how the internal implementation works doesn't matter except to developers.  What the end user sees is that the default for pic is global-dynamic and the default for not-pic is initial-exec.

Jim

Tommy Murphy

unread,
Jul 26, 2021, 5:29:02 PM7/26/21
to Jim Wilson, Anitha Quest, Sober Liu, RISC-V SW Dev
Hi Jim

I'm confused by this:

Originally you said 

GCC defaults to the global exec model, but optimizes to a more efficient tls model if it can. 

But the manual says:

The model argument should be one of ‘global-dynamic’, ‘local-dynamic’, ‘initial-exec’ or ‘local-exec’.

When you said "global exec" did you actually mean "global dynamic"?

Thanks
Regards
Tommy

Jim Wilson

unread,
Jul 26, 2021, 5:30:52 PM7/26/21
to Tommy Murphy, Anitha Quest, Sober Liu, RISC-V SW Dev
On Mon, Jul 26, 2021 at 2:28 PM Tommy Murphy <tommy_...@hotmail.com> wrote:
When you said "global exec" did you actually mean "global dynamic"?

Yes, sorry, global dynamic.

Jim

Tommy Murphy

unread,
Jul 26, 2021, 5:57:47 PM7/26/21
to Jim Wilson, Anitha Quest, Sober Liu, RISC-V SW Dev
Thanks Jim.
Reply all
Reply to author
Forward
0 new messages