2007-12-29 02:05:26 +01:00
|
|
|
/****************************************************************************
|
2009-12-13 19:01:46 +01:00
|
|
|
* arch/z80/src/common/up_createstack.c
|
2007-12-29 02:05:26 +01:00
|
|
|
*
|
2015-02-28 00:19:38 +01:00
|
|
|
* Copyright (C) 2007-2009, 2013, 2015 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-12-29 02:05:26 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-16 15:15:34 +01:00
|
|
|
|
2007-12-29 02:05:26 +01:00
|
|
|
#include <sys/types.h>
|
2009-12-16 15:15:34 +01:00
|
|
|
#include <stdint.h>
|
2007-12-29 02:05:26 +01:00
|
|
|
#include <sched.h>
|
|
|
|
#include <debug.h>
|
2013-02-28 16:31:58 +01:00
|
|
|
|
2007-12-29 02:05:26 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/arch.h>
|
2015-02-28 00:19:38 +01:00
|
|
|
#include <nuttx/board.h>
|
2009-12-16 15:15:34 +01:00
|
|
|
|
2007-12-29 02:05:26 +01:00
|
|
|
#include "up_arch.h"
|
|
|
|
#include "up_internal.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 01:42:29 +02:00
|
|
|
* Public Functions
|
2007-12-29 02:05:26 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: up_create_stack
|
|
|
|
*
|
|
|
|
* Description:
|
2013-03-20 19:22:21 +01:00
|
|
|
* Allocate a stack for a new thread and setup up stack-related information
|
|
|
|
* in the TCB.
|
2007-12-29 02:05:26 +01:00
|
|
|
*
|
2013-03-20 19:22:21 +01:00
|
|
|
* The following TCB fields must be initialized by this function:
|
|
|
|
*
|
|
|
|
* - adj_stack_size: Stack size after adjustment for hardware, processor,
|
2013-02-28 16:31:58 +01:00
|
|
|
* etc. This value is retained only for debug purposes.
|
2013-03-20 19:22:21 +01:00
|
|
|
* - stack_alloc_ptr: Pointer to allocated stack
|
|
|
|
* - adj_stack_ptr: Adjusted stack_alloc_ptr for HW. The initial value of
|
2013-02-28 16:31:58 +01:00
|
|
|
* the stack pointer.
|
2007-12-29 02:05:26 +01:00
|
|
|
*
|
2013-03-20 19:22:21 +01:00
|
|
|
* Inputs:
|
|
|
|
* - tcb: The TCB of new task
|
|
|
|
* - stack_size: The requested stack size. At least this much
|
|
|
|
* must be allocated.
|
|
|
|
* - ttype: The thread type. This may be one of following (defined in
|
|
|
|
* include/nuttx/sched.h):
|
|
|
|
*
|
|
|
|
* TCB_FLAG_TTYPE_TASK Normal user task
|
|
|
|
* TCB_FLAG_TTYPE_PTHREAD User pthread
|
|
|
|
* TCB_FLAG_TTYPE_KERNEL Kernel thread
|
|
|
|
*
|
|
|
|
* This thread type is normally available in the flags field of the TCB,
|
|
|
|
* however, there are certain contexts where the TCB may not be fully
|
|
|
|
* initialized when up_create_stack is called.
|
|
|
|
*
|
2014-08-29 22:47:22 +02:00
|
|
|
* If CONFIG_BUILD_KERNEL is defined, then this thread type may affect
|
2013-03-20 19:22:21 +01:00
|
|
|
* how the stack is allocated. For example, kernel thread stacks should
|
|
|
|
* be allocated from protected kernel memory. Stacks for user tasks and
|
|
|
|
* threads must come from memory that is accessible to user code.
|
2013-02-28 16:31:58 +01:00
|
|
|
*
|
2007-12-29 02:05:26 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2013-03-20 19:22:21 +01:00
|
|
|
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
2007-12-29 02:05:26 +01:00
|
|
|
{
|
2013-03-20 21:27:08 +01:00
|
|
|
/* Is there already a stack allocated of a different size? Because of
|
|
|
|
* alignment issues, stack_size might erroneously appear to be of a
|
|
|
|
* different size. Fortunately, this is not a critical operation.
|
|
|
|
*/
|
2013-02-28 16:31:58 +01:00
|
|
|
|
|
|
|
if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size)
|
2007-12-29 02:05:26 +01:00
|
|
|
{
|
2013-03-20 21:27:08 +01:00
|
|
|
/* Yes.. Release the old stack */
|
2013-02-28 16:31:58 +01:00
|
|
|
|
2013-03-20 21:27:08 +01:00
|
|
|
up_release_stack(tcb, ttype);
|
2007-12-29 02:05:26 +01:00
|
|
|
}
|
|
|
|
|
2013-03-20 21:27:08 +01:00
|
|
|
/* Do we need to allocate a new stack? */
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2013-02-28 16:31:58 +01:00
|
|
|
if (!tcb->stack_alloc_ptr)
|
|
|
|
{
|
|
|
|
/* Allocate the stack. If DEBUG is enabled (but not stack debug),
|
|
|
|
* then create a zeroed stack to make stack dumps easier to trace.
|
|
|
|
*/
|
|
|
|
|
2014-08-29 22:47:22 +02:00
|
|
|
#if defined(CONFIG_BUILD_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
|
2013-03-20 21:27:08 +01:00
|
|
|
/* Use the kernel allocator if this is a kernel thread */
|
|
|
|
|
|
|
|
if (ttype == TCB_FLAG_TTYPE_KERNEL)
|
|
|
|
{
|
2014-09-01 01:26:36 +02:00
|
|
|
tcb->stack_alloc_ptr = (uint32_t *)kmm_malloc(stack_size);
|
2013-03-20 21:27:08 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Use the user-space allocator if this is a task or pthread */
|
|
|
|
|
2014-09-01 00:24:24 +02:00
|
|
|
tcb->stack_alloc_ptr = (uint32_t *)kumm_malloc(stack_size);
|
2013-03-20 21:27:08 +01:00
|
|
|
}
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2013-03-20 21:27:08 +01:00
|
|
|
/* Was the allocation successful? */
|
|
|
|
|
2013-02-28 16:31:58 +01:00
|
|
|
if (!tcb->stack_alloc_ptr)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
serr("ERROR: Failed to allocate stack, size %d\n", stack_size);
|
2013-02-28 16:31:58 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Did we successfully allocate a stack? */
|
|
|
|
|
|
|
|
if (tcb->stack_alloc_ptr)
|
|
|
|
{
|
|
|
|
size_t top_of_stack;
|
|
|
|
size_t size_of_stack;
|
|
|
|
|
|
|
|
/* Yes.. If stack debug is enabled, then fill the stack with a
|
|
|
|
* recognizable value that we can use later to test for high
|
|
|
|
* water marks.
|
|
|
|
*/
|
2007-12-29 02:05:26 +01:00
|
|
|
|
2015-01-24 13:49:51 +01:00
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2013-02-28 16:31:58 +01:00
|
|
|
memset(tcb->stack_alloc_ptr, 0xaa, stack_size);
|
|
|
|
#endif
|
2007-12-29 02:05:26 +01:00
|
|
|
|
2013-02-28 16:31:58 +01:00
|
|
|
/* The Z80 family uses a push-down stack: the stack grows
|
|
|
|
* toward loweraddresses in memory. The stack pointer
|
|
|
|
* register, points to the lowest, valid work address
|
|
|
|
* (the "top" of the stack). Items on the stack are
|
|
|
|
* referenced as positive word offsets from sp.
|
|
|
|
*/
|
2007-12-29 02:05:26 +01:00
|
|
|
|
2013-02-28 16:31:58 +01:00
|
|
|
top_of_stack = (uint32_t)tcb->stack_alloc_ptr + stack_size - 4;
|
2007-12-29 02:05:26 +01:00
|
|
|
|
2013-02-28 16:31:58 +01:00
|
|
|
/* The Z80 stack does not need to be aligned. Here is is aligned at
|
|
|
|
* word (4 byte) boundary.
|
|
|
|
*/
|
2007-12-29 02:05:26 +01:00
|
|
|
|
2013-02-28 16:31:58 +01:00
|
|
|
top_of_stack &= ~3;
|
|
|
|
size_of_stack = top_of_stack - (uint32_t)tcb->stack_alloc_ptr + 4;
|
2007-12-29 02:05:26 +01:00
|
|
|
|
2013-02-28 16:31:58 +01:00
|
|
|
/* Save the adjusted stack values in the struct tcb_s */
|
2007-12-29 02:05:26 +01:00
|
|
|
|
2013-02-28 16:31:58 +01:00
|
|
|
tcb->adj_stack_ptr = (uint32_t*)top_of_stack;
|
|
|
|
tcb->adj_stack_size = size_of_stack;
|
2007-12-29 02:05:26 +01:00
|
|
|
|
2015-11-01 16:07:06 +01:00
|
|
|
board_autoled_on(LED_STACKCREATED);
|
2013-02-28 16:31:58 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2007-12-29 02:05:26 +01:00
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|