Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ C and a linker script at build time.
- **IPC:** queuing ports with blocking send/receive and timeouts.
- **Health monitor:** per-partition recovery policy per fault class
(restart process, halt partition, ignore), including deadline misses.
When the kernel cannot continue (a fault it cannot attribute to a process,
or a failure of the recovery path) it calls `maxrtos_arch_fatal()`, a weak
function that halts by default. Define your own to log and reset. The kernel
does not own a watchdog: choose one, configure it, and feed it from your
`SysTick_Handler` or another place you control.

## Layout

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
* interface.
*
* HardFault_Handler recovers escalated configurable faults (HFSR.FORCED)
* through the same path and spins on any unattributable cause.
* through the same path. Any unattributable cause, and any failure of the
* recovery path itself, ends in maxrtos_arch_fatal().
*
* When a partition-halt action is selected, immediate selection of a
* process in another partition is deferred to the system's scheduling
Expand All @@ -46,6 +47,23 @@
#include "maxrtos/kernel/partition.h"
#include "maxrtos/kernel/process.h"

/**
* @brief The kernel cannot continue: a fault it cannot attribute to a process
* or recover from, or a failure of the recovery path itself.
*
* Does not return. The default stops the CPU (MAXRTOS_PORT_HALT: interrupts
* off, break forever), so a debugger sees g_maxrtos_arch_last_fault. It is a
* weak symbol: an application that must come back by itself defines its own
* maxrtos_arch_fatal() to log, then reset the system (SCB->AIRCR
* SYSRESETREQ) or let a watchdog it configures expire. It runs in the
* exception that found the fault, so it may not block or wait for a process.
*
* The kernel does not own a watchdog. Which one to use, its period and
* where it is fed (for example from the application's SysTick_Handler) are
* the application's choice.
*/
void maxrtos_arch_fatal( void );

/**
* @brief Enable configurable fault exceptions and divide-by-zero
* trapping.
Expand Down Expand Up @@ -84,8 +102,7 @@ extern volatile maxrtos_arch_fault_info_t g_maxrtos_arch_last_fault;
* Health-monitor instance. The caller shall ensure that the
* referenced object remains valid while fault handling is enabled.
*/
void maxrtos_arch_set_health_monitor(
maxrtos_health_monitor_t const * hm );
void maxrtos_arch_set_health_monitor( maxrtos_health_monitor_t const * hm );

/**
* @brief Register the partition table used by fault recovery.
Expand All @@ -94,8 +111,7 @@ void maxrtos_arch_set_health_monitor(
* Partition table. The caller shall ensure that the referenced
* object remains valid while fault handling is enabled.
*/
void maxrtos_arch_set_partition_table(
maxrtos_partition_table_t * table );
void maxrtos_arch_set_partition_table( maxrtos_partition_table_t * table );

/**
* @brief Retrieve the registered partition table used by fault recovery.
Expand Down
38 changes: 16 additions & 22 deletions maxrtos/arch/cortex_m7/src/fault_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@
static maxrtos_health_monitor_t const * s_hm = NULL;
static maxrtos_partition_table_t * s_table = NULL;

static void maxrtos_arch_halt( void )
__attribute__( ( weak ) ) void maxrtos_arch_fatal( void )
{
MAXRTOS_PORT_HALT();
}

void maxrtos_arch_set_health_monitor(
maxrtos_health_monitor_t const * hm )
void maxrtos_arch_set_health_monitor( maxrtos_health_monitor_t const * hm )
{
s_hm = hm;
}

void maxrtos_arch_set_partition_table(
maxrtos_partition_table_t * table )
void maxrtos_arch_set_partition_table( maxrtos_partition_table_t * table )
{
s_table = table;
}
Expand All @@ -56,21 +54,17 @@ void maxrtos_arch_handle_process_fault(

if( ( s_hm == NULL ) || ( s_table == NULL ) || ( faulting_pcb == NULL ) )
{
maxrtos_arch_halt();
maxrtos_arch_fatal();
}

current_pcb = maxrtos_arch_get_current_pcb();

status = maxrtos_fault_recovery_handle(
s_hm,
s_table,
faulting_pcb->id,
fault_type,
&action );
s_hm, s_table, faulting_pcb->id, fault_type, &action );

if( status != MAXRTOS_OK )
{
maxrtos_arch_halt();
maxrtos_arch_fatal();
}

switch( action )
Expand All @@ -89,27 +83,28 @@ void maxrtos_arch_handle_process_fault(

if( status != MAXRTOS_OK )
{
maxrtos_arch_halt();
maxrtos_arch_fatal();
}

if( faulting_pcb == current_pcb )
{
maxrtos_process_id_t next_id;
maxrtos_process_control_block_t * next_pcb;

if( maxrtos_partition_dispatch(
s_table,
faulting_pcb->partition_id,
&next_id ) != MAXRTOS_OK )
/* The restarted process starts afresh, so it goes behind
* its equal-priority peers. */
if( maxrtos_partition_yield(
s_table, faulting_pcb->partition_id, &next_id ) !=
MAXRTOS_OK )
{
maxrtos_arch_halt();
maxrtos_arch_fatal();
}

next_pcb = maxrtos_process_get( next_id );

if( next_pcb == NULL )
{
maxrtos_arch_halt();
maxrtos_arch_fatal();
}

/* The faulting context must not be saved. Its stack now
Expand Down Expand Up @@ -147,7 +142,7 @@ void maxrtos_arch_handle_process_fault(

default:
{
maxrtos_arch_halt();
maxrtos_arch_fatal();
break;
}
}
Expand All @@ -156,6 +151,5 @@ void maxrtos_arch_handle_process_fault(
void maxrtos_arch_handle_fault( maxrtos_fault_type_t fault_type )
{
maxrtos_arch_handle_process_fault(
maxrtos_arch_get_current_pcb(),
fault_type );
maxrtos_arch_get_current_pcb(), fault_type );
}
40 changes: 18 additions & 22 deletions maxrtos/arch/cortex_m7/src/fault_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@

/* CFSR.MMFSR.MMARVALID and CFSR.BFSR.BFARVALID: the fault address
* registers hold the faulting data address. */
#define MAXRTOS_CFSR_MMARVALID_BIT ( 1UL << 7U )
#define MAXRTOS_CFSR_BFARVALID_BIT ( 1UL << 15U )
#define MAXRTOS_CFSR_MMARVALID_BIT ( 1UL << 7U )
#define MAXRTOS_CFSR_BFARVALID_BIT ( 1UL << 15U )

/* CCR.DIV_0_TRP: enable UsageFault generation for integer
* divide-by-zero operations. */
#define MAXRTOS_CCR_DIVBYZERO_TRP_BIT ( 1UL << 4U )
#define MAXRTOS_CCR_DIVBYZERO_TRP_BIT ( 1UL << 4U )

/* SHCSR fault-handler enable bits. */
#define MAXRTOS_SHCSR_MEMFAULTENA_BIT ( 1UL << 16U )
#define MAXRTOS_SHCSR_BUSFAULTENA_BIT ( 1UL << 17U )
#define MAXRTOS_SHCSR_USGFAULTENA_BIT ( 1UL << 18U )
#define MAXRTOS_SHCSR_MEMFAULTENA_BIT ( 1UL << 16U )
#define MAXRTOS_SHCSR_BUSFAULTENA_BIT ( 1UL << 17U )
#define MAXRTOS_SHCSR_USGFAULTENA_BIT ( 1UL << 18U )

/* HFSR.FORCED: a configurable fault escalated to HardFault (its handler
* was disabled, or it occurred at equal or higher exception priority,
* e.g. inside an SVC handler). CFSR then identifies the original cause.
* HFSR.VECTTBL: vector table read failed; not attributable to a process. */
#define MAXRTOS_HFSR_FORCED_BIT ( 1UL << 30U )
#define MAXRTOS_HFSR_FORCED_BIT ( 1UL << 30U )

/* CFSR.UFSR.DIVBYZERO: indicates that an integer divide-by-zero
* operation caused the UsageFault exception. */
#define MAXRTOS_CFSR_UFSR_DIVBYZERO_BIT ( 1UL << 25U )

/* CFSR sub-register masks. */
#define MAXRTOS_CFSR_MMFSR_MASK ( 0x000000FFUL )
#define MAXRTOS_CFSR_BFSR_MASK ( 0x0000FF00UL )
#define MAXRTOS_CFSR_UFSR_MASK ( 0xFFFF0000UL )
#define MAXRTOS_CFSR_MMFSR_MASK ( 0x000000FFUL )
#define MAXRTOS_CFSR_BFSR_MASK ( 0x0000FF00UL )
#define MAXRTOS_CFSR_UFSR_MASK ( 0xFFFF0000UL )

/* The most recent fault, kept where a debugger can read it. */
volatile maxrtos_arch_fault_info_t g_maxrtos_arch_last_fault;
Expand All @@ -58,18 +58,18 @@ static void maxrtos_arch_record_fault( uint32_t cfsr )
g_maxrtos_arch_last_fault.cfsr = cfsr;
g_maxrtos_arch_last_fault.hfsr = MAXRTOS_SCB_HFSR;
g_maxrtos_arch_last_fault.mmfar =
( ( cfsr & MAXRTOS_CFSR_MMARVALID_BIT ) != 0U ) ? MAXRTOS_SCB_MMFAR : 0U;
( ( cfsr & MAXRTOS_CFSR_MMARVALID_BIT ) != 0U ) ? MAXRTOS_SCB_MMFAR
: 0U;
g_maxrtos_arch_last_fault.bfar =
( ( cfsr & MAXRTOS_CFSR_BFARVALID_BIT ) != 0U ) ? MAXRTOS_SCB_BFAR : 0U;
g_maxrtos_arch_last_fault.count++;
}

void maxrtos_arch_fault_handlers_init( void )
{
MAXRTOS_SCB_SHCSR |=
MAXRTOS_SHCSR_MEMFAULTENA_BIT |
MAXRTOS_SHCSR_BUSFAULTENA_BIT |
MAXRTOS_SHCSR_USGFAULTENA_BIT;
MAXRTOS_SCB_SHCSR |= MAXRTOS_SHCSR_MEMFAULTENA_BIT |
MAXRTOS_SHCSR_BUSFAULTENA_BIT |
MAXRTOS_SHCSR_USGFAULTENA_BIT;

MAXRTOS_SCB_CCR |= MAXRTOS_CCR_DIVBYZERO_TRP_BIT;
}
Expand All @@ -83,8 +83,7 @@ void MemManage_Handler( void )

MAXRTOS_SCB_CFSR = cfsr & MAXRTOS_CFSR_MMFSR_MASK;

maxrtos_arch_handle_fault(
MAXRTOS_FAULT_MEMORY_ACCESS );
maxrtos_arch_handle_fault( MAXRTOS_FAULT_MEMORY_ACCESS );
}

void BusFault_Handler( void )
Expand All @@ -96,8 +95,7 @@ void BusFault_Handler( void )

MAXRTOS_SCB_CFSR = cfsr & MAXRTOS_CFSR_BFSR_MASK;

maxrtos_arch_handle_fault(
MAXRTOS_FAULT_BUS_ERROR );
maxrtos_arch_handle_fault( MAXRTOS_FAULT_BUS_ERROR );
}

static maxrtos_fault_type_t maxrtos_arch_classify_usage_fault( uint32_t cfsr )
Expand Down Expand Up @@ -199,8 +197,6 @@ void HardFault_Handler( void )
}
else
{
for( ;; )
{
}
maxrtos_arch_fatal();
}
}
65 changes: 60 additions & 5 deletions maxrtos/arch/cortex_m7/src/svc.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ MAXRTOS_SVC_OUT_OF_LINE static void maxrtos_arch_svc_timed_wait(
MAXRTOS_SVC_OUT_OF_LINE static void maxrtos_arch_svc_time_get(
uint32_t * stacked_args );

MAXRTOS_SVC_OUT_OF_LINE static void maxrtos_arch_svc_preempt_check( void );

void maxrtos_arch_svc_dispatch(
uint32_t * stacked_args,
uint8_t svc_number )
Expand Down Expand Up @@ -188,13 +190,23 @@ void maxrtos_arch_svc_dispatch(
break;
}
}

/* A call can make a higher-priority process of the caller's partition
* READY: an IPC wake-up, a mutex release, a priority change. The caller
* is preempted at once. A yield only gives way, and reading the time
* changes nothing, so they skip the check. */
if( ( svc_number != ( uint8_t ) MAXRTOS_SVC_YIELD ) &&
( svc_number != ( uint8_t ) MAXRTOS_SVC_TIME_GET ) )
{
maxrtos_arch_svc_preempt_check();
}
}

/* There is no valid recovery action for a syscall gate that
* cannot identify what was requested. */
static void maxrtos_arch_svc_invalid( void )
{
MAXRTOS_PORT_HALT();
maxrtos_arch_fatal();
}

static void maxrtos_arch_svc_yield( uint32_t * stacked_args )
Expand Down Expand Up @@ -460,6 +472,9 @@ static void maxrtos_arch_svc_timed_wait( uint32_t * stacked_args )
maxrtos_process_id_t next_id;
maxrtos_status_t status;

maxrtos_tick_t delay_ticks;

delay_ticks = ( maxrtos_tick_t ) stacked_args[ 0 ];
current_pcb = maxrtos_arch_get_current_pcb();
partition_table = maxrtos_arch_get_partition_table();
next_id = MAXRTOS_INVALID_PROCESS_ID;
Expand All @@ -471,13 +486,53 @@ static void maxrtos_arch_svc_timed_wait( uint32_t * stacked_args )
else
{
status = maxrtos_kernel_timed_wait(
partition_table,
current_pcb->id,
( maxrtos_tick_t ) stacked_args[ 0 ],
&next_id );
partition_table, current_pcb->id, delay_ticks, &next_id );
}

maxrtos_arch_svc_finish_wait( stacked_args, status, next_id );

if( ( status == MAXRTOS_OK ) && ( delay_ticks == 0U ) )
{
/* A zero delay lets processes of the same priority take turns. The
* caller's result (OK) is already in its frame. */
maxrtos_arch_svc_yield( stacked_args );
}
}

static void maxrtos_arch_svc_preempt_check( void )
{
maxrtos_process_control_block_t const * caller;
maxrtos_partition_table_t * table;

caller = maxrtos_arch_get_current_pcb();
table = maxrtos_arch_get_partition_table();

/* Only a caller that is still RUNNING can be preempted by what the call
* made READY. One that blocked was replaced by the best READY process,
* so nothing in its partition outranks the process now running. */
if( ( caller != NULL ) && ( table != NULL ) &&
( caller->state == MAXRTOS_PROCESS_STATE_RUNNING ) &&
( caller->partition_id < MAXRTOS_MAX_PARTITIONS ) &&
maxrtos_scheduler_has_higher(
&table->scheduler_ctx[ caller->partition_id ], caller->priority ) )
{
maxrtos_process_id_t next_id;

if( ( maxrtos_partition_dispatch(
table, caller->partition_id, &next_id ) == MAXRTOS_OK ) &&
( next_id != caller->id ) )
{
maxrtos_process_control_block_t * next_pcb;

next_pcb = maxrtos_process_get( next_id );

if( next_pcb != NULL )
{
maxrtos_arch_set_next_pcb( next_pcb );
maxrtos_arch_request_context_switch();
}
}
}
}

static void maxrtos_arch_svc_time_get( uint32_t * stacked_args )
Expand Down
9 changes: 5 additions & 4 deletions maxrtos/include/maxrtos/timing.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
maxrtos_status_t maxrtos_periodic_wait( void );

/**
* @brief Wait for a number of ticks (ARINC 653 TIMED_WAIT).
* @brief Wait for a number of ticks.
*
* Blocks the calling process until the kernel tick counter has advanced by
* delay_ticks, counted the way an IPC timeout is: a wait of N ticks begun when
Expand All @@ -41,8 +41,9 @@ maxrtos_status_t maxrtos_periodic_wait( void );
* processes, or idles. The process runs again once it is READY and its
* partition's slot is on.
*
* A delay of 0 returns at once and does not give up the CPU; call
* maxrtos_yield() for that.
* A delay of 0 does not wait. It is a rescheduling point: READY processes of
* the same priority run first, and the caller goes behind them, exactly as
* for maxrtos_yield().
*
* @param[in] delay_ticks
* Ticks to wait. MAXRTOS_TIMEOUT_INFINITE is rejected.
Expand All @@ -55,7 +56,7 @@ maxrtos_status_t maxrtos_periodic_wait( void );
maxrtos_status_t maxrtos_timed_wait( maxrtos_tick_t delay_ticks );

/**
* @brief Read the kernel tick counter (ARINC 653 GET_TIME).
* @brief Read the kernel tick counter.
*
* The counter starts at 0 when the scheduler starts and advances by one on
* every tick.
Expand Down
Loading
Loading