2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* common/up_assert.c
|
|
|
|
*
|
2008-01-01 15:54:10 +01:00
|
|
|
* Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
|
2007-04-28 21:39:18 +02:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* 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.
|
2008-01-01 15:54:10 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-04-28 21:39:18 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Included Files
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-11-03 00:05:53 +01:00
|
|
|
|
2007-04-28 21:39:18 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
2007-11-03 00:05:53 +01:00
|
|
|
|
2007-04-28 21:39:18 +02:00
|
|
|
#include <nuttx/irq.h>
|
2007-11-03 00:05:53 +01:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
|
2007-04-28 21:39:18 +02:00
|
|
|
#include "up_arch.h"
|
|
|
|
#include "os_internal.h"
|
|
|
|
#include "up_internal.h"
|
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Definitions
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
/* Output debug info if stack dump is selected -- even if
|
|
|
|
* debug is not selected.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_STACKDUMP
|
|
|
|
# undef lldbg
|
|
|
|
# define lldbg lib_lowprintf
|
|
|
|
#endif
|
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Private Data
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Private Functions
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Name: up_getsp
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
/* I don't know if the builtin to get SP is enabled */
|
|
|
|
|
|
|
|
static inline uint32 up_getsp(void)
|
|
|
|
{
|
|
|
|
uint32 sp;
|
|
|
|
__asm__
|
|
|
|
(
|
|
|
|
"\tmov %0, sp\n\t"
|
|
|
|
: "=r"(sp)
|
|
|
|
);
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Name: up_stackdump
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_STACKDUMP
|
2008-10-06 18:20:53 +02:00
|
|
|
static void up_stackdump(uint32 sp, uint32 stack_base)
|
|
|
|
{
|
|
|
|
uint32 stack ;
|
|
|
|
|
|
|
|
for (stack = sp & ~0x1f; stack < stack_base; stack += 32)
|
|
|
|
{
|
|
|
|
uint32 *ptr = (uint32*)stack;
|
|
|
|
lldbg("%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
|
|
|
|
stack, ptr[0], ptr[1], ptr[2], ptr[3],
|
|
|
|
ptr[4], ptr[5], ptr[6], ptr[7]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define up_stackdump()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: up_registerdump
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_STACKDUMP
|
|
|
|
static inline void up_registerdump(void)
|
|
|
|
{
|
|
|
|
/* Are user registers available from interrupt processing? */
|
|
|
|
|
|
|
|
if (current_regs)
|
|
|
|
{
|
|
|
|
int regs;
|
|
|
|
|
|
|
|
/* Yes.. dump the interrupt registers */
|
|
|
|
|
|
|
|
for (regs = REG_R0; regs <= REG_R15; regs += 8)
|
|
|
|
{
|
|
|
|
uint32 *ptr = (uint32*)¤t_regs[regs];
|
|
|
|
lldbg("R%d: %08x %08x %08x %08x %08x %08x %08x %08x\n",
|
|
|
|
regs, ptr[0], ptr[1], ptr[2], ptr[3],
|
|
|
|
ptr[4], ptr[5], ptr[6], ptr[7]);
|
|
|
|
}
|
2009-05-13 16:29:22 +02:00
|
|
|
|
|
|
|
#ifdef __thumb2__
|
2009-05-13 18:19:05 +02:00
|
|
|
lldbg("xPSR: %08x PRIMASK: %08x\n",
|
|
|
|
current_regs[REG_XPSR], current_regs[REG_PRIMASK]);
|
2009-05-13 16:29:22 +02:00
|
|
|
#else
|
2008-10-06 18:20:53 +02:00
|
|
|
lldbg("CPSR: %08x\n", current_regs[REG_CPSR]);
|
2009-05-13 16:29:22 +02:00
|
|
|
#endif
|
2008-10-06 18:20:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define up_registerdump()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: up_dumpstate
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_STACKDUMP
|
|
|
|
static void up_dumpstate(void)
|
2007-04-28 21:39:18 +02:00
|
|
|
{
|
|
|
|
_TCB *rtcb = (_TCB*)g_readytorun.head;
|
|
|
|
uint32 sp = up_getsp();
|
2008-10-06 18:20:53 +02:00
|
|
|
uint32 ustackbase;
|
|
|
|
uint32 ustacksize;
|
|
|
|
#if CONFIG_ARCH_INTERRUPTSTACK > 3
|
|
|
|
uint32 istackbase;
|
|
|
|
uint32 istacksize;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get the limits on the user stack memory */
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
if (rtcb->pid == 0)
|
|
|
|
{
|
2008-10-06 18:20:53 +02:00
|
|
|
ustackbase = g_heapbase - 4;
|
2008-11-13 21:37:02 +01:00
|
|
|
ustacksize = CONFIG_IDLETHREAD_STACKSIZE;
|
2007-04-28 21:39:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-10-06 18:20:53 +02:00
|
|
|
ustackbase = (uint32)rtcb->adj_stack_ptr;
|
|
|
|
ustacksize = (uint32)rtcb->adj_stack_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the limits on the interrupt stack memory */
|
|
|
|
|
|
|
|
#if CONFIG_ARCH_INTERRUPTSTACK > 3
|
|
|
|
istackbase = (uint32)&g_userstack;
|
|
|
|
istacksize = (CONFIG_ARCH_INTERRUPTSTACK & ~3) - 4;
|
|
|
|
|
|
|
|
/* Show interrupt stack info */
|
|
|
|
|
|
|
|
lldbg("sp: %08x\n", sp);
|
|
|
|
lldbg("IRQ stack:\n");
|
|
|
|
lldbg(" base: %08x\n", istackbase);
|
|
|
|
lldbg(" size: %08x\n", istacksize);
|
|
|
|
|
|
|
|
/* Does the current stack pointer lie within the interrupt
|
|
|
|
* stack?
|
|
|
|
*/
|
|
|
|
|
2008-10-08 01:05:56 +02:00
|
|
|
if (sp <= istackbase && sp > istackbase - istacksize)
|
2008-10-06 18:20:53 +02:00
|
|
|
{
|
|
|
|
/* Yes.. dump the interrupt stack */
|
|
|
|
|
|
|
|
up_stackdump(sp, istackbase);
|
|
|
|
|
|
|
|
/* Extract the user stack pointer which should lie
|
|
|
|
* at the base of the interrupt stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sp = g_userstack;
|
|
|
|
lldbg("sp: %08x\n", sp);
|
2007-04-28 21:39:18 +02:00
|
|
|
}
|
|
|
|
|
2008-10-06 18:20:53 +02:00
|
|
|
/* Show user stack info */
|
|
|
|
|
|
|
|
lldbg("User stack:\n");
|
|
|
|
lldbg(" base: %08x\n", ustackbase);
|
|
|
|
lldbg(" size: %08x\n", ustacksize);
|
|
|
|
#else
|
2007-04-28 21:39:18 +02:00
|
|
|
lldbg("sp: %08x\n", sp);
|
2008-10-06 18:20:53 +02:00
|
|
|
lldbg("stack base: %08x\n", ustackbase);
|
|
|
|
lldbg("stack size: %08x\n", ustacksize);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Dump the user stack if the stack pointer lies within the allocated user
|
|
|
|
* stack memory.
|
|
|
|
*/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2008-10-08 01:05:56 +02:00
|
|
|
if (sp > ustackbase || sp <= ustackbase - ustacksize)
|
2007-04-28 21:39:18 +02:00
|
|
|
{
|
2008-10-06 18:20:53 +02:00
|
|
|
#if !defined(CONFIG_ARCH_INTERRUPTSTACK) || CONFIG_ARCH_INTERRUPTSTACK < 4
|
2007-04-28 21:39:18 +02:00
|
|
|
lldbg("ERROR: Stack pointer is not within allocated stack\n");
|
2008-10-06 18:20:53 +02:00
|
|
|
#endif
|
2007-04-28 21:39:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-10-06 18:20:53 +02:00
|
|
|
up_stackdump(sp, ustackbase);
|
2007-04-28 21:39:18 +02:00
|
|
|
}
|
|
|
|
|
2008-10-06 18:20:53 +02:00
|
|
|
/* Then dump the registers (if available) */
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2008-10-06 18:20:53 +02:00
|
|
|
up_registerdump();
|
2007-04-28 21:39:18 +02:00
|
|
|
}
|
|
|
|
#else
|
2008-10-06 18:20:53 +02:00
|
|
|
# define up_dumpstate()
|
2007-04-28 21:39:18 +02:00
|
|
|
#endif
|
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Name: _up_assert
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
static void _up_assert(int errorcode) /* __attribute__ ((noreturn)) */
|
|
|
|
{
|
|
|
|
/* Are we in an interrupt handler or the idle task? */
|
|
|
|
|
|
|
|
if (current_regs || ((_TCB*)g_readytorun.head)->pid == 0)
|
|
|
|
{
|
|
|
|
(void)irqsave();
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_ARCH_LEDS
|
|
|
|
up_ledon(LED_PANIC);
|
2007-11-03 00:05:53 +01:00
|
|
|
up_mdelay(250);
|
2007-04-28 21:39:18 +02:00
|
|
|
up_ledoff(LED_PANIC);
|
2007-11-03 00:05:53 +01:00
|
|
|
up_mdelay(250);
|
2007-04-28 21:39:18 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
exit(errorcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Name: up_assert
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
void up_assert(const ubyte *filename, int lineno)
|
|
|
|
{
|
2008-09-01 17:22:54 +02:00
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_DEBUG)
|
2007-04-28 21:39:18 +02:00
|
|
|
_TCB *rtcb = (_TCB*)g_readytorun.head;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
up_ledon(LED_ASSERTION);
|
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
|
|
|
lldbg("Assertion failed at file:%s line: %d task: %s\n",
|
|
|
|
filename, lineno, rtcb->name);
|
|
|
|
#else
|
|
|
|
lldbg("Assertion failed at file:%s line: %d\n",
|
|
|
|
filename, lineno);
|
|
|
|
#endif
|
2008-10-06 18:20:53 +02:00
|
|
|
up_dumpstate();
|
2007-04-28 21:39:18 +02:00
|
|
|
_up_assert(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Name: up_assert_code
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
void up_assert_code(const ubyte *filename, int lineno, int errorcode)
|
|
|
|
{
|
2008-09-01 17:22:54 +02:00
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_DEBUG)
|
2007-04-28 21:39:18 +02:00
|
|
|
_TCB *rtcb = (_TCB*)g_readytorun.head;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
up_ledon(LED_ASSERTION);
|
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
|
|
|
lldbg("Assertion failed at file:%s line: %d task: %s error code: %d\n",
|
|
|
|
filename, lineno, rtcb->name, errorcode);
|
|
|
|
#else
|
|
|
|
lldbg("Assertion failed at file:%s line: %d error code: %d\n",
|
|
|
|
filename, lineno, errorcode);
|
|
|
|
#endif
|
2008-10-06 18:20:53 +02:00
|
|
|
up_dumpstate();
|
2007-04-28 21:39:18 +02:00
|
|
|
_up_assert(errorcode);
|
|
|
|
}
|