[PATCH 1/2] score: Add _CPU_Get_TLS_thread_pointer()

Chris Johns chrisj at rtems.org
Sat Sep 16 23:00:10 UTC 2023


Looks good and thank you. I appreciate you handling this for me.

Chris

On 15/9/2023 5:07 pm, Sebastian Huber wrote:
> Add _CPU_Get_TLS_thread_pointer() to get the thread pointer which is
> used to get the address of thread-local storage objects associated with
> a thread.
> 
> Update #4920.
> ---
>  .../cpu/aarch64/include/rtems/score/cpuimpl.h |  7 ++++
>  .../cpu/arm/include/rtems/score/cpuimpl.h     |  7 ++++
>  .../cpu/bfin/include/rtems/score/cpuimpl.h    |  8 +++++
>  .../cpu/i386/include/rtems/score/cpuimpl.h    |  7 ++++
>  .../cpu/lm32/include/rtems/score/cpuimpl.h    |  8 +++++
>  .../cpu/m68k/include/rtems/score/cpuimpl.h    |  7 ++++
>  .../microblaze/include/rtems/score/cpuimpl.h  |  7 ++++
>  .../cpu/mips/include/rtems/score/cpuimpl.h    |  8 +++++
>  .../cpu/moxie/include/rtems/score/cpuimpl.h   |  8 +++++
>  .../cpu/nios2/include/rtems/score/cpuimpl.h   |  7 ++++
>  .../cpu/no_cpu/include/rtems/score/cpuimpl.h  | 16 +++++++++
>  .../cpu/or1k/include/rtems/score/cpuimpl.h    |  8 +++++
>  .../cpu/powerpc/include/rtems/score/cpuimpl.h |  7 ++++
>  .../cpu/riscv/include/rtems/score/cpuimpl.h   |  7 ++++
>  .../cpu/sh/include/rtems/score/cpuimpl.h      |  8 +++++
>  .../cpu/sparc/include/rtems/score/cpuimpl.h   |  7 ++++
>  .../cpu/sparc64/include/rtems/score/cpuimpl.h |  8 +++++
>  .../cpu/v850/include/rtems/score/cpuimpl.h    |  8 +++++
>  .../cpu/x86_64/include/rtems/score/cpuimpl.h  |  8 +++++
>  testsuites/sptests/sptls01/init.c             | 33 +++++++++++++++++++
>  20 files changed, 184 insertions(+)
> 
> diff --git a/cpukit/score/cpu/aarch64/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/aarch64/include/rtems/score/cpuimpl.h
> index b520e8bffb..095c5db5b9 100644
> --- a/cpukit/score/cpu/aarch64/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/aarch64/include/rtems/score/cpuimpl.h
> @@ -174,6 +174,13 @@ static inline void _CPU_Use_thread_local_storage(
>    );
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  return (void *) context->thread_id;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/arm/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/arm/include/rtems/score/cpuimpl.h
> index f9b40889f5..04d23f0ea7 100644
> --- a/cpukit/score/cpu/arm/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/arm/include/rtems/score/cpuimpl.h
> @@ -178,6 +178,13 @@ static inline void _CPU_Use_thread_local_storage(
>  #endif
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  return (void *) context->thread_id;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/bfin/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/bfin/include/rtems/score/cpuimpl.h
> index aaf34a3ce0..5a445d9420 100644
> --- a/cpukit/score/cpu/bfin/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/bfin/include/rtems/score/cpuimpl.h
> @@ -72,6 +72,14 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  (void) context;
> +  return NULL;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/i386/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/i386/include/rtems/score/cpuimpl.h
> index ba5d820016..da38ecacf7 100644
> --- a/cpukit/score/cpu/i386/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/i386/include/rtems/score/cpuimpl.h
> @@ -112,6 +112,13 @@ static inline void _CPU_Use_thread_local_storage(
>    );
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  return (void *) &context->gs;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/lm32/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/lm32/include/rtems/score/cpuimpl.h
> index 870640864c..70a1db1d4d 100644
> --- a/cpukit/score/cpu/lm32/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/lm32/include/rtems/score/cpuimpl.h
> @@ -71,6 +71,14 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  (void) context;
> +  return NULL;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/m68k/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/m68k/include/rtems/score/cpuimpl.h
> index 4bee157215..521e9fc4c2 100644
> --- a/cpukit/score/cpu/m68k/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/m68k/include/rtems/score/cpuimpl.h
> @@ -95,6 +95,13 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  return (void *) context->thread_pointer;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/microblaze/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/microblaze/include/rtems/score/cpuimpl.h
> index 4e315e856d..760ebbfbbb 100644
> --- a/cpukit/score/cpu/microblaze/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/microblaze/include/rtems/score/cpuimpl.h
> @@ -100,6 +100,13 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  return (void *) context->thread_pointer;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/mips/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/mips/include/rtems/score/cpuimpl.h
> index d619eb6213..98ed1c492f 100644
> --- a/cpukit/score/cpu/mips/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/mips/include/rtems/score/cpuimpl.h
> @@ -91,6 +91,14 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  (void) context;
> +  return NULL;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/moxie/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/moxie/include/rtems/score/cpuimpl.h
> index 30c2c75f2b..44c70cc56d 100644
> --- a/cpukit/score/cpu/moxie/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/moxie/include/rtems/score/cpuimpl.h
> @@ -91,6 +91,14 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  (void) context;
> +  return NULL;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/nios2/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/nios2/include/rtems/score/cpuimpl.h
> index e1beaca74c..3c9c6734ec 100644
> --- a/cpukit/score/cpu/nios2/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/nios2/include/rtems/score/cpuimpl.h
> @@ -84,6 +84,13 @@ static inline void _CPU_Use_thread_local_storage(
>     __asm__ volatile ( "" : : "r" ( r23 ) );
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  return (void *) context->r23;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/no_cpu/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/no_cpu/include/rtems/score/cpuimpl.h
> index 61afcdd014..61f1ab7ba5 100644
> --- a/cpukit/score/cpu/no_cpu/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/no_cpu/include/rtems/score/cpuimpl.h
> @@ -201,6 +201,22 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +/**
> + * @brief Gets the thread pointer of the context.
> + *
> + * The thread pointer is used to get the address of thread-local storage
> + * objects associated with a thread.
> + *
> + * @param context is the processor context containing the thread pointer.
> + */
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  (void) context;
> +  return NULL;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/or1k/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/or1k/include/rtems/score/cpuimpl.h
> index 5e6865eb1f..9b58b1b77a 100644
> --- a/cpukit/score/cpu/or1k/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/or1k/include/rtems/score/cpuimpl.h
> @@ -79,6 +79,14 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  (void) context;
> +  return NULL;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/powerpc/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/powerpc/include/rtems/score/cpuimpl.h
> index 6d984b92e4..68b7165546 100644
> --- a/cpukit/score/cpu/powerpc/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/powerpc/include/rtems/score/cpuimpl.h
> @@ -301,6 +301,13 @@ static inline void _CPU_Use_thread_local_storage(
>     __asm__ volatile ( "" : : "r" ( tp ) );
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  return (void *) ppc_get_context( context )->tp;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/riscv/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/riscv/include/rtems/score/cpuimpl.h
> index 47cf2e338c..13fd60ed8c 100644
> --- a/cpukit/score/cpu/riscv/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/riscv/include/rtems/score/cpuimpl.h
> @@ -454,6 +454,13 @@ static inline void _CPU_Use_thread_local_storage(
>     __asm__ volatile ( "" : : "r" ( tp ) );
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  return (void *) context->tp;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/sh/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/sh/include/rtems/score/cpuimpl.h
> index ca87c61c2b..e5f45eb363 100644
> --- a/cpukit/score/cpu/sh/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/sh/include/rtems/score/cpuimpl.h
> @@ -72,6 +72,14 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  (void) context;
> +  return NULL;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/sparc/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/sparc/include/rtems/score/cpuimpl.h
> index e15be48e9e..459b981ae5 100644
> --- a/cpukit/score/cpu/sparc/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/sparc/include/rtems/score/cpuimpl.h
> @@ -251,6 +251,13 @@ static inline void _CPU_Use_thread_local_storage(
>     __asm__ volatile ( "" : : "r" ( g7 ) );
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  return (void *) context->g7;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/sparc64/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/sparc64/include/rtems/score/cpuimpl.h
> index a29a519644..25a362c350 100644
> --- a/cpukit/score/cpu/sparc64/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/sparc64/include/rtems/score/cpuimpl.h
> @@ -91,6 +91,14 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  (void) context;
> +  return NULL;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/v850/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/v850/include/rtems/score/cpuimpl.h
> index 3985c3cdb9..5bc1c42459 100644
> --- a/cpukit/score/cpu/v850/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/v850/include/rtems/score/cpuimpl.h
> @@ -91,6 +91,14 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  (void) context;
> +  return NULL;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/cpukit/score/cpu/x86_64/include/rtems/score/cpuimpl.h b/cpukit/score/cpu/x86_64/include/rtems/score/cpuimpl.h
> index 726a515d4e..d4b7a71009 100644
> --- a/cpukit/score/cpu/x86_64/include/rtems/score/cpuimpl.h
> +++ b/cpukit/score/cpu/x86_64/include/rtems/score/cpuimpl.h
> @@ -75,6 +75,14 @@ static inline void _CPU_Use_thread_local_storage(
>    (void) context;
>  }
>  
> +static inline void *_CPU_Get_TLS_thread_pointer(
> +  const Context_Control *context
> +)
> +{
> +  (void) context;
> +  return NULL;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/testsuites/sptests/sptls01/init.c b/testsuites/sptests/sptls01/init.c
> index acb5d43a5e..d3bd8b74c3 100644
> --- a/testsuites/sptests/sptls01/init.c
> +++ b/testsuites/sptests/sptls01/init.c
> @@ -32,6 +32,8 @@
>  #include <rtems/bspIo.h>
>  #include <rtems/stackchk.h>
>  #include <rtems/sysinit.h>
> +#include <rtems/score/cpuimpl.h>
> +#include <rtems/score/threadimpl.h>
>  #include <rtems/score/tls.h>
>  
>  #include "tmacros.h"
> @@ -57,6 +59,7 @@ static void task(rtems_task_argument arg)
>    rtems_status_code sc;
>  
>    check_tls_item(123);
> +  tls_item = 42;
>  
>    sc = rtems_event_transient_send(master_task);
>    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
> @@ -83,10 +86,27 @@ static void check_tls_size(void)
>    }
>  }
>  
> +static Thread_Control *get_thread(rtems_id id)
> +{
> +  Thread_Control *the_thread;
> +  ISR_lock_Context lock_context;
> +
> +  the_thread = _Thread_Get(id, &lock_context);
> +  _ISR_lock_ISR_enable(&lock_context);
> +  return the_thread;
> +}
> +
>  static void test(void)
>  {
>    rtems_id id;
>    rtems_status_code sc;
> +  Thread_Control *self;
> +  Thread_Control *other;
> +  char *self_tp;
> +  char *other_tp;
> +  uintptr_t tls_item_offset;
> +  char *self_tls_item;
> +  char *other_tls_item;
>  
>    master_task = rtems_task_self();
>  
> @@ -109,9 +129,22 @@ static void test(void)
>    sc = rtems_task_start(id, task, 0);
>    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
>  
> +  self = get_thread(master_task);
> +  other = get_thread(id);
> +  self_tp = _CPU_Get_TLS_thread_pointer(&self->Registers);
> +  other_tp = _CPU_Get_TLS_thread_pointer(&other->Registers);
> +  tls_item_offset = (uintptr_t) (&tls_item - self_tp);
> +  self_tls_item = self_tp + tls_item_offset;
> +  other_tls_item = other_tp + tls_item_offset;
> +  rtems_test_assert(*self_tls_item == 5);
> +  rtems_test_assert(*other_tls_item == 123);
> +
>    sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
>    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
>  
> +  rtems_test_assert(*self_tls_item == 5);
> +  rtems_test_assert(*other_tls_item == 42);
> +
>    sc = rtems_task_delete(id);
>    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
>  


More information about the devel mailing list