2011-08-31 21:50:31 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
/*
|
2012-08-17 03:22:40 -03:00
|
|
|
* note that we use a 32 bit sentinel to reduce the chance
|
|
|
|
* of false positives with uninitialised stack variables
|
2011-08-31 21:50:31 -03:00
|
|
|
*/
|
|
|
|
|
2012-11-30 21:49:14 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2012-12-14 15:56:29 -04:00
|
|
|
#include <AP_HAL_Boards.h>
|
2011-08-31 21:50:31 -03:00
|
|
|
|
|
|
|
static const uint32_t *stack_low;
|
|
|
|
extern unsigned __brkval;
|
|
|
|
|
|
|
|
#define STACK_OFFSET 2
|
2011-09-04 18:23:06 -03:00
|
|
|
#define SENTINEL 0x28021967
|
2011-08-31 21:50:31 -03:00
|
|
|
|
|
|
|
/*
|
2012-08-17 03:22:40 -03:00
|
|
|
* return the current stack pointer
|
2011-08-31 21:50:31 -03:00
|
|
|
*/
|
2012-08-17 03:22:40 -03:00
|
|
|
static __attribute__((noinline)) const uint32_t *current_stackptr(void)
|
2011-08-31 21:50:31 -03:00
|
|
|
{
|
|
|
|
return (const uint32_t *)__builtin_frame_address(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-08-17 03:22:40 -03:00
|
|
|
* this can be added in deeply nested code to ensure we catch
|
|
|
|
* deep stack usage. It should be caught by the sentinel, but this
|
|
|
|
* is an added protection
|
2011-08-31 21:50:31 -03:00
|
|
|
*/
|
|
|
|
void memcheck_update_stackptr(void)
|
|
|
|
{
|
|
|
|
if (current_stackptr() < stack_low) {
|
2012-08-09 03:19:39 -03:00
|
|
|
uintptr_t s = (uintptr_t)(current_stackptr() - STACK_OFFSET);
|
2011-08-31 21:50:31 -03:00
|
|
|
stack_low = (uint32_t *)(s & ~3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-08-17 03:22:40 -03:00
|
|
|
* initialise memcheck, setting up the sentinels
|
2011-08-31 21:50:31 -03:00
|
|
|
*/
|
|
|
|
void memcheck_init(void)
|
|
|
|
{
|
2013-01-01 23:44:29 -04:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_APM1 || CONFIG_HAL_BOARD == HAL_BOARD_APM2
|
2011-08-31 21:50:31 -03:00
|
|
|
uint32_t *p;
|
|
|
|
free(malloc(1)); // ensure heap is initialised
|
|
|
|
stack_low = current_stackptr();
|
|
|
|
memcheck_update_stackptr();
|
|
|
|
for (p=(uint32_t *)(stack_low-1); p>(uint32_t *)__brkval; p--) {
|
2011-09-04 18:23:06 -03:00
|
|
|
*p = SENTINEL;
|
2011-08-31 21:50:31 -03:00
|
|
|
}
|
2011-10-07 19:16:30 -03:00
|
|
|
#endif
|
2011-08-31 21:50:31 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-08-17 03:22:40 -03:00
|
|
|
* this returns the real amount of free memory by looking for
|
|
|
|
* overwrites of the stack sentinel values
|
2011-08-31 21:50:31 -03:00
|
|
|
*/
|
|
|
|
unsigned memcheck_available_memory(void)
|
|
|
|
{
|
2013-01-01 23:44:29 -04:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_APM1 || CONFIG_HAL_BOARD == HAL_BOARD_APM2
|
2011-08-31 21:50:31 -03:00
|
|
|
memcheck_update_stackptr();
|
2011-09-04 18:23:06 -03:00
|
|
|
while (*stack_low != SENTINEL && stack_low > (const uint32_t *)__brkval) {
|
2011-08-31 21:50:31 -03:00
|
|
|
stack_low--;
|
|
|
|
}
|
2011-10-07 19:16:30 -03:00
|
|
|
return (uintptr_t)(stack_low) - __brkval;
|
2013-01-01 23:44:29 -04:00
|
|
|
#else
|
|
|
|
return 0x1000;
|
2011-10-07 19:16:30 -03:00
|
|
|
#endif
|
2011-08-31 21:50:31 -03:00
|
|
|
}
|