SIM: Add TLS support to to the simulator

This commit is contained in:
Gregory Nutt 2016-03-11 14:03:27 -06:00
parent 4d484399a9
commit a74c19bbae
4 changed files with 146 additions and 7 deletions

View file

@ -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---

88
arch/sim/include/tls.h Normal file
View file

@ -0,0 +1,88 @@
/****************************************************************************
* arch/sin/include/tls.h
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#include <assert.h>
#include <nuttx/arch.h>
#include <nuttx/tls.h>
#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 */

View file

@ -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 <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -41,8 +41,10 @@
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <nuttx/arch.h>
#include <nuttx/tls.h>
#include <nuttx/kmalloc.h>
#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;
}

View file

@ -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 <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -38,9 +38,14 @@
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <string.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/tls.h>
#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;
}