2008-09-30 14:26:46 +02:00
|
|
|
/****************************************************************************
|
2020-04-30 23:19:35 +02:00
|
|
|
* arch/arm/src/armv7-m/arm_initialstate.c
|
2007-04-28 21:39:18 +02:00
|
|
|
*
|
2020-04-22 21:26:09 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-04-28 21:39:18 +02:00
|
|
|
*
|
2020-04-22 21:26:09 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-04-28 21:39:18 +02:00
|
|
|
*
|
2020-04-22 21:26:09 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-04-28 21:39:18 +02:00
|
|
|
*
|
2008-09-30 14:26:46 +02:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2008-09-30 14:26:46 +02:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Included Files
|
2008-09-30 14:26:46 +02:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-16 21:05:51 +01:00
|
|
|
|
2007-04-28 21:39:18 +02:00
|
|
|
#include <sys/types.h>
|
2009-12-16 21:05:51 +01:00
|
|
|
#include <stdint.h>
|
2007-04-28 21:39:18 +02:00
|
|
|
#include <string.h>
|
2009-05-19 00:14:40 +02:00
|
|
|
|
2007-04-28 21:39:18 +02:00
|
|
|
#include <nuttx/arch.h>
|
2018-12-04 00:41:59 +01:00
|
|
|
#include <arch/armv7-m/nvicpri.h>
|
2009-05-19 00:14:40 +02:00
|
|
|
|
2020-05-01 03:20:29 +02:00
|
|
|
#include "arm_internal.h"
|
2009-05-19 19:16:17 +02:00
|
|
|
#include "psr.h"
|
2011-04-08 03:33:21 +02:00
|
|
|
#include "exc_return.h"
|
2009-05-19 00:14:40 +02:00
|
|
|
|
2008-09-30 14:26:46 +02:00
|
|
|
/****************************************************************************
|
2008-11-06 19:15:35 +01:00
|
|
|
* Public Functions
|
2008-09-30 14:26:46 +02:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2008-09-30 14:26:46 +02:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Name: up_initial_state
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A new thread is being started and a new TCB
|
|
|
|
* has been created. This function is called to initialize
|
|
|
|
* the processor specific portions of the new TCB.
|
|
|
|
*
|
2019-08-04 22:50:28 +02:00
|
|
|
* This function must setup the initial architecture registers
|
2007-04-28 21:39:18 +02:00
|
|
|
* and/or stack so that execution will begin at tcb->start
|
|
|
|
* on the next context switch.
|
|
|
|
*
|
2008-09-30 14:26:46 +02:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2013-02-04 19:46:28 +01:00
|
|
|
void up_initial_state(struct tcb_s *tcb)
|
2007-04-28 21:39:18 +02:00
|
|
|
{
|
|
|
|
struct xcptcontext *xcp = &tcb->xcp;
|
|
|
|
|
2022-02-28 18:06:24 +01:00
|
|
|
/* Initialize the initial exception register context structure */
|
|
|
|
|
|
|
|
memset(xcp, 0, sizeof(struct xcptcontext));
|
|
|
|
|
2020-07-05 07:37:48 +02:00
|
|
|
/* Initialize the idle thread stack */
|
|
|
|
|
2022-03-21 23:47:09 +01:00
|
|
|
if (tcb->pid == IDLE_PROCESS_ID)
|
2020-07-05 07:37:48 +02:00
|
|
|
{
|
2020-10-23 05:46:53 +02:00
|
|
|
tcb->stack_alloc_ptr = (void *)(g_idle_topstack -
|
|
|
|
CONFIG_IDLETHREAD_STACKSIZE);
|
2021-06-08 11:55:04 +02:00
|
|
|
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
|
2020-10-23 05:46:53 +02:00
|
|
|
tcb->adj_stack_size = CONFIG_IDLETHREAD_STACKSIZE;
|
2021-06-08 11:55:04 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
|
|
|
/* If stack debug is enabled, then fill the stack with a
|
|
|
|
* recognizable value that we can use later to test for high
|
|
|
|
* water marks.
|
|
|
|
*/
|
|
|
|
|
|
|
|
arm_stack_color(tcb->stack_alloc_ptr, 0);
|
|
|
|
#endif /* CONFIG_STACK_COLORATION */
|
2022-02-28 18:06:24 +01:00
|
|
|
|
|
|
|
return;
|
2020-07-05 07:37:48 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 18:06:24 +01:00
|
|
|
/* Initialize the context registers to stack top */
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2022-04-17 07:57:58 +02:00
|
|
|
xcp->regs = (void *)((uint32_t)tcb->stack_base_ptr +
|
|
|
|
tcb->adj_stack_size -
|
|
|
|
XCPTCONTEXT_SIZE);
|
2022-02-28 18:06:24 +01:00
|
|
|
|
|
|
|
/* Initialize the xcp registers */
|
|
|
|
|
|
|
|
memset(xcp->regs, 0, XCPTCONTEXT_SIZE);
|
2009-05-19 00:14:40 +02:00
|
|
|
|
2009-05-19 19:16:17 +02:00
|
|
|
/* Save the initial stack pointer */
|
2009-05-19 00:14:40 +02:00
|
|
|
|
2022-03-17 17:06:12 +01:00
|
|
|
xcp->regs[REG_SP] = (uint32_t)tcb->stack_base_ptr +
|
|
|
|
tcb->adj_stack_size;
|
2009-05-13 18:19:05 +02:00
|
|
|
|
2015-04-12 14:26:50 +02:00
|
|
|
#ifdef CONFIG_ARMV7M_STACKCHECK
|
|
|
|
/* Set the stack limit value */
|
|
|
|
|
|
|
|
xcp->regs[REG_R10] = (uint32_t)tcb->stack_alloc_ptr + 64;
|
|
|
|
#endif
|
|
|
|
|
2009-05-19 00:14:40 +02:00
|
|
|
/* Save the task entry point (stripping off the thumb bit) */
|
|
|
|
|
2009-12-16 21:05:51 +01:00
|
|
|
xcp->regs[REG_PC] = (uint32_t)tcb->start & ~1;
|
2013-12-21 18:03:38 +01:00
|
|
|
|
2009-05-19 00:14:40 +02:00
|
|
|
/* Specify thumb mode */
|
|
|
|
|
2011-08-05 23:57:49 +02:00
|
|
|
xcp->regs[REG_XPSR] = ARMV7M_XPSR_T;
|
2009-05-19 00:14:40 +02:00
|
|
|
|
2009-06-18 01:38:05 +02:00
|
|
|
/* If this task is running PIC, then set the PIC base register to the
|
|
|
|
* address of the allocated D-Space region.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_PIC
|
|
|
|
if (tcb->dspace != NULL)
|
|
|
|
{
|
|
|
|
/* Set the PIC base register (probably R10) to the address of the
|
|
|
|
* alloacated D-Space region.
|
|
|
|
*/
|
|
|
|
|
2009-12-16 21:05:51 +01:00
|
|
|
xcp->regs[REG_PIC] = (uint32_t)tcb->dspace->region;
|
2009-06-18 01:38:05 +02:00
|
|
|
}
|
2009-06-26 03:18:24 +02:00
|
|
|
|
2013-03-15 02:32:47 +01:00
|
|
|
#ifdef CONFIG_NXFLAT
|
2009-06-26 03:18:24 +02:00
|
|
|
/* Make certain that bit 0 is set in the main entry address. This
|
|
|
|
* is only an issue when NXFLAT is enabled. NXFLAT doesn't know
|
|
|
|
* anything about thumb; the addresses that NXFLAT sets are based
|
|
|
|
* on file header info and won't have bit 0 set.
|
|
|
|
*/
|
|
|
|
|
2009-12-16 21:05:51 +01:00
|
|
|
tcb->entry.main = (main_t)((uint32_t)tcb->entry.main | 1);
|
2009-06-26 03:18:24 +02:00
|
|
|
#endif
|
2012-02-22 19:14:18 +01:00
|
|
|
#endif /* CONFIG_PIC */
|
|
|
|
|
2018-06-20 20:30:37 +02:00
|
|
|
#if !defined(CONFIG_ARMV7M_LAZYFPU) || defined(CONFIG_BUILD_PROTECTED)
|
2013-03-14 23:44:06 +01:00
|
|
|
/* All tasks start via a stub function in kernel space. So all
|
2014-08-29 22:47:22 +02:00
|
|
|
* tasks must start in privileged thread mode. If CONFIG_BUILD_PROTECTED
|
2013-03-14 23:44:06 +01:00
|
|
|
* is defined, then that stub function will switch to unprivileged
|
|
|
|
* mode before transferring control to the user task.
|
2012-02-22 19:14:18 +01:00
|
|
|
*/
|
|
|
|
|
2013-03-14 23:44:06 +01:00
|
|
|
xcp->regs[REG_EXC_RETURN] = EXC_RETURN_PRIVTHR;
|
2012-02-22 19:14:18 +01:00
|
|
|
|
2018-06-20 20:30:37 +02:00
|
|
|
#endif /* !CONFIG_ARMV7M_LAZYFPU || CONFIG_BUILD_PROTECTED */
|
2013-03-15 01:27:26 +01:00
|
|
|
|
2018-06-20 20:30:37 +02:00
|
|
|
#if !defined(CONFIG_ARMV7M_LAZYFPU) && defined(CONFIG_ARCH_FPU)
|
2012-02-22 19:14:18 +01:00
|
|
|
|
2015-10-07 00:23:32 +02:00
|
|
|
xcp->regs[REG_FPSCR] = 0; /* REVISIT: Initial FPSCR should be configurable */
|
2020-04-22 21:55:46 +02:00
|
|
|
xcp->regs[REG_FP_RESERVED] = 0;
|
2012-02-22 19:14:18 +01:00
|
|
|
|
2018-06-20 20:30:37 +02:00
|
|
|
#endif /* !CONFIG_ARMV7M_LAZYFPU && CONFIG_ARCH_FPU */
|
2011-04-08 03:33:21 +02:00
|
|
|
|
2009-05-19 00:14:40 +02:00
|
|
|
/* Enable or disable interrupts, based on user configuration */
|
|
|
|
|
2012-02-22 19:14:18 +01:00
|
|
|
#ifdef CONFIG_SUPPRESS_INTERRUPTS
|
2014-07-24 23:37:13 +02:00
|
|
|
|
2013-01-22 15:37:17 +01:00
|
|
|
#ifdef CONFIG_ARMV7M_USEBASEPRI
|
|
|
|
xcp->regs[REG_BASEPRI] = NVIC_SYSH_DISABLE_PRIORITY;
|
|
|
|
#else
|
2009-05-13 18:19:05 +02:00
|
|
|
xcp->regs[REG_PRIMASK] = 1;
|
2012-02-22 19:14:18 +01:00
|
|
|
#endif
|
2014-07-24 23:37:13 +02:00
|
|
|
|
|
|
|
#else /* CONFIG_SUPPRESS_INTERRUPTS */
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARMV7M_USEBASEPRI
|
2014-07-25 00:51:07 +02:00
|
|
|
xcp->regs[REG_BASEPRI] = NVIC_SYSH_PRIORITY_MIN;
|
2014-07-24 23:37:13 +02:00
|
|
|
#endif
|
|
|
|
|
2013-01-22 15:37:17 +01:00
|
|
|
#endif /* CONFIG_SUPPRESS_INTERRUPTS */
|
2007-04-28 21:39:18 +02:00
|
|
|
}
|