2013-02-16 16:32:19 +00:00
|
|
|
/****************************************************************************
|
2020-04-30 15:19:35 -06:00
|
|
|
* arch/arm/src/armv6-m/arm_hardfault.c
|
2013-02-16 16:32:19 +00:00
|
|
|
*
|
2024-12-05 14:05:04 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
2021-03-24 09:11:24 +01: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
|
2013-02-16 16:32:19 +00:00
|
|
|
*
|
2021-03-24 09:11:24 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-02-16 16:32:19 +00:00
|
|
|
*
|
2021-03-24 09:11:24 +01: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.
|
2013-02-16 16:32:19 +00:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Included Files
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
|
|
|
|
|
#include "nvic.h"
|
2020-04-30 19:20:29 -06:00
|
|
|
#include "arm_internal.h"
|
2013-02-16 16:32:19 +00:00
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Pre-processor Definitions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2018-07-30 06:46:44 -06:00
|
|
|
#ifdef CONFIG_DEBUG_HARDFAULT_ALERT
|
2023-05-20 06:32:34 +08:00
|
|
|
# define hfalert(format, ...) _alert(format, ##__VA_ARGS__)
|
2018-07-30 12:32:55 +00:00
|
|
|
#else
|
2023-05-20 06:32:34 +08:00
|
|
|
# define hfalert(x...)
|
2018-07-30 12:32:55 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_HARDFAULT_INFO
|
2023-05-20 06:32:34 +08:00
|
|
|
# define hfinfo(format, ...) _info(format, ##__VA_ARGS__)
|
2013-02-16 16:32:19 +00:00
|
|
|
#else
|
2023-05-20 06:32:34 +08:00
|
|
|
# define hfinfo(x...)
|
2013-02-16 16:32:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define INSN_SVC0 0xdf00 /* insn: svc 0 */
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Public Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-05-01 08:50:23 -06:00
|
|
|
* Name: arm_hardfault
|
2013-02-16 16:32:19 +00:00
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* This is Hard Fault exception handler. It also catches SVC call
|
|
|
|
|
* exceptions that are performed in bad contexts.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2022-04-17 13:57:58 +08:00
|
|
|
int arm_hardfault(int irq, void *context, void *arg)
|
2013-02-16 16:32:19 +00:00
|
|
|
{
|
2015-10-06 16:23:47 -06:00
|
|
|
uint32_t *regs = (uint32_t *)context;
|
2013-04-16 18:00:59 -06:00
|
|
|
|
|
|
|
|
/* Get the value of the program counter where the fault occurred */
|
|
|
|
|
|
2015-10-06 16:23:47 -06:00
|
|
|
uint16_t *pc = (uint16_t *)regs[REG_PC] - 1;
|
2013-04-16 18:00:59 -06:00
|
|
|
|
|
|
|
|
/* Check if the pc lies in known FLASH memory.
|
2013-04-16 18:29:01 -06:00
|
|
|
* REVISIT: What if the PC lies in "unknown" external memory?
|
2013-04-16 18:00:59 -06:00
|
|
|
*/
|
|
|
|
|
|
2014-08-29 14:47:22 -06:00
|
|
|
#ifdef CONFIG_BUILD_PROTECTED
|
2013-04-16 18:00:59 -06:00
|
|
|
/* In the kernel build, SVCalls are expected in either the base, kernel
|
|
|
|
|
* FLASH region or in the user FLASH region.
|
|
|
|
|
*/
|
2013-02-16 16:32:19 +00:00
|
|
|
|
2022-09-20 02:38:54 +08:00
|
|
|
if (((uintptr_t)pc >= (uintptr_t)_stext &&
|
|
|
|
|
(uintptr_t)pc < (uintptr_t)_etext) ||
|
2013-04-16 18:00:59 -06:00
|
|
|
((uintptr_t)pc >= (uintptr_t)USERSPACE->us_textstart &&
|
|
|
|
|
(uintptr_t)pc < (uintptr_t)USERSPACE->us_textend))
|
|
|
|
|
#else
|
|
|
|
|
/* SVCalls are expected only from the base, kernel FLASH region */
|
|
|
|
|
|
2022-09-20 02:38:54 +08:00
|
|
|
if ((uintptr_t)pc >= (uintptr_t)_stext &&
|
|
|
|
|
(uintptr_t)pc < (uintptr_t)_etext)
|
2013-04-16 18:00:59 -06:00
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
/* Fetch the instruction that caused the Hard fault */
|
|
|
|
|
|
|
|
|
|
uint16_t insn = *pc;
|
2016-06-17 16:44:50 -06:00
|
|
|
hfinfo(" PC: %p INSN: %04x\n", pc, insn);
|
2013-04-16 18:00:59 -06:00
|
|
|
|
|
|
|
|
/* If this was the instruction 'svc 0', then forward processing
|
|
|
|
|
* to the SVCall handler
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (insn == INSN_SVC0)
|
|
|
|
|
{
|
2016-06-17 16:44:50 -06:00
|
|
|
hfinfo("Forward SVCall\n");
|
2020-05-01 08:50:23 -06:00
|
|
|
return arm_svcall(irq, context, NULL);
|
2013-04-16 18:00:59 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-30 06:46:44 -06:00
|
|
|
#if defined(CONFIG_DEBUG_HARDFAULT_ALERT)
|
2013-02-16 16:32:19 +00:00
|
|
|
/* Dump some hard fault info */
|
|
|
|
|
|
2018-07-30 12:32:55 +00:00
|
|
|
hfalert("\nHard Fault:\n");
|
|
|
|
|
hfalert(" IRQ: %d regs: %p\n", irq, regs);
|
|
|
|
|
hfalert(" PRIMASK: %08x IPSR: %08x\n",
|
2018-07-30 06:46:44 -06:00
|
|
|
getprimask(), getipsr());
|
2013-02-23 15:04:49 +00:00
|
|
|
#endif
|
2013-02-16 16:32:19 +00:00
|
|
|
|
2020-01-02 10:49:34 -06:00
|
|
|
up_irq_save();
|
2018-07-30 12:32:55 +00:00
|
|
|
hfalert("PANIC!!! Hard fault\n");
|
2023-04-10 21:19:12 +08:00
|
|
|
PANIC_WITH_REGS("panic", context);
|
2013-04-25 15:19:59 -06:00
|
|
|
return OK; /* Won't get here */
|
2013-02-16 16:32:19 +00:00
|
|
|
}
|