diff --git a/arch/Kconfig b/arch/Kconfig index 43c7915d7f..fd61f2fbf5 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -55,6 +55,7 @@ config ARCH_SH config ARCH_SIM bool "Simulation" select ARCH_HAVE_MULTICPU + select ARCH_HAVE_TLS select ARCH_HAVE_TICKLESS select ARCH_HAVE_POWEROFF ---help--- diff --git a/arch/sim/include/tls.h b/arch/sim/include/tls.h new file mode 100644 index 0000000000..ae2cf3b85a --- /dev/null +++ b/arch/sim/include/tls.h @@ -0,0 +1,88 @@ +/**************************************************************************** + * arch/sin/include/tls.h + * + * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __ARCH_SIM_INCLUDE_TLS_H +#define __ARCH_SIM_INCLUDE_TLS_H 1 + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include + +#ifdef CONFIG_TLS + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_tls_info + * + * Description: + * Return the TLS information structure for the currently executing thread. + * When TLS is enabled, up_createstack() will align allocated stacks to + * the TLS_STACK_ALIGN value. An instance of the following structure will + * be implicitly positioned at the "lower" end of the stack. Assuming a + * "push down" stack, this is at the "far" end of the stack (and can be + * clobbered if the stack overflows). + * + * If an MCU has a "push up" then that TLS structure will lie at the top + * of the stack and stack allocation and initialization logic must take + * care to preserve this structure content. + * + * The stack memory is fully accessible to user mode threads. + * + * Input Parameters: + * None + * + * Returned Value: + * A pointer to TLS info structure at the beginning of the STACK memory + * allocation. This is essentially an application of the TLS_INFO(sp) + * macro and has a platform dependency only in the manner in which the + * stack pointer (sp) is obtained and interpreted. + * + ****************************************************************************/ + +static inline FAR struct tls_info_s *up_tls_info(void) +{ + return TLS_INFO((uintptr_t)__builtin_frame_address(0)); +} + +#endif /* CONFIG_TLS */ +#endif /* __ARCH_SIM_INCLUDE_TLS_H */ diff --git a/arch/sim/src/up_createstack.c b/arch/sim/src/up_createstack.c index f8a4630cf6..d7438e0edc 100644 --- a/arch/sim/src/up_createstack.c +++ b/arch/sim/src/up_createstack.c @@ -1,7 +1,7 @@ /**************************************************************************** * arch/sim/src/up_createstack.c * - * Copyright (C) 2007-2009, 2013 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2013, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -41,8 +41,10 @@ #include #include +#include #include +#include #include #include "up_internal.h" @@ -88,6 +90,22 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) uint32_t *stack_alloc_ptr; int ret = ERROR; +#ifdef CONFIG_TLS + /* Add the size of the TLS information structure */ + + stack_size += sizeof(struct tls_info_s); + + /* The allocated stack size must not exceed the maximum possible for the + * TLS feature. + */ + + DEBUGASSERT(stack_size <= TLS_MAXSTACK); + if (stack_size >= TLS_MAXSTACK) + { + stack_size = TLS_MAXSTACK; + } +#endif + /* Move up to next even word boundary if necessary */ size_t adj_stack_size = (stack_size + 3) & ~3; @@ -95,7 +113,11 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) /* Allocate the memory for the stack */ +#ifdef CONFIG_TLS + stack_alloc_ptr = (FAR uint32_t *)kumm_memalign(TLS_STACK_ALIGN, adj_stack_size); +#else /* CONFIG_TLS */ stack_alloc_ptr = (FAR uint32_t *)kumm_malloc(adj_stack_size); +#endif /* CONFIG_TLS */ /* Was the allocation successful? */ @@ -103,13 +125,20 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) { /* This is the address of the last word in the allocation */ - void *adj_stack_ptr = &stack_alloc_ptr[adj_stack_words - 1]; + FAR void *adj_stack_ptr = &stack_alloc_ptr[adj_stack_words - 1]; /* Save the values in the TCB */ tcb->adj_stack_size = adj_stack_size; tcb->stack_alloc_ptr = stack_alloc_ptr; tcb->adj_stack_ptr = adj_stack_ptr; + +#ifdef CONFIG_TLS + /* Initialize the TLS data structure */ + + memset(stack_alloc_ptr, 0, sizeof(struct tls_info_s)); +#endif + ret = OK; } diff --git a/arch/sim/src/up_usestack.c b/arch/sim/src/up_usestack.c index 3474a2342b..2e55fed257 100644 --- a/arch/sim/src/up_usestack.c +++ b/arch/sim/src/up_usestack.c @@ -1,7 +1,7 @@ /**************************************************************************** * arch/sim/src/up_usestack.c * - * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -38,9 +38,14 @@ ****************************************************************************/ #include + #include +#include #include + #include +#include + #include "up_internal.h" /**************************************************************************** @@ -75,21 +80,37 @@ * ****************************************************************************/ -int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size) +int up_use_stack(FAR struct tcb_s *tcb, FAR void *stack, size_t stack_size) { + FAR size_t *adj_stack_ptr; + size_t adj_stack_size; + size_t adj_stack_words; + +#ifdef CONFIG_TLS + /* Make certain that the user provided stack is properly aligned */ + + DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0); +#endif /* Move up to next even word boundary if necessary */ - size_t adj_stack_size = stack_size & ~3; - size_t adj_stack_words = adj_stack_size >> 2; + adj_stack_size = stack_size & ~3; + adj_stack_words = adj_stack_size >> 2; /* This is the address of the last word in the allocation */ - FAR size_t *adj_stack_ptr = &((FAR size_t *)stack)[adj_stack_words - 1]; + adj_stack_ptr = &((FAR size_t *)stack)[adj_stack_words - 1]; /* Save the values in the TCB */ tcb->adj_stack_size = adj_stack_size; tcb->stack_alloc_ptr = stack; tcb->adj_stack_ptr = adj_stack_ptr; + +#ifdef CONFIG_TLS + /* Initialize the TLS data structure */ + + memset(stack, 0, sizeof(struct tls_info_s)); +#endif + return OK; }