47 lines
855 B
C
47 lines
855 B
C
#include "stack.h"
|
|
#include <stdint.h>
|
|
#include <avr/interrupt.h>
|
|
|
|
extern uint8_t _end;
|
|
extern uint8_t __stack;
|
|
|
|
void __stack_canary_init(void)
|
|
__attribute__ ((naked))
|
|
__attribute__ ((section (".init1")));
|
|
void __stack_canary_init(void)
|
|
{
|
|
__asm volatile (" ldi r30,lo8(_end)\n"
|
|
" ldi r31,hi8(_end)\n"
|
|
" ldi r24,lo8(0xaa)\n" // canary
|
|
" ldi r25,hi8(__stack)\n"
|
|
" rjmp 2f\n"
|
|
"1:\n"
|
|
" wdr\n"
|
|
" st Z+,r24\n"
|
|
"2:\n"
|
|
" cpi r30,lo8(__stack)\n"
|
|
" cpc r31,r25\n"
|
|
" brlo 1b\n"
|
|
" breq 1b"
|
|
:
|
|
:
|
|
: "memory");
|
|
}
|
|
|
|
/* return low level mark of free stack space */
|
|
uint16_t stack_min(void)
|
|
{
|
|
const uint8_t *p;
|
|
uint16_t c = 0;
|
|
|
|
for (p = &_end; *p == 0xaa && p <= &__stack; p++)
|
|
c++;
|
|
return c;
|
|
}
|
|
|
|
/* return total amount of stack */
|
|
uint16_t stack_total(void)
|
|
{
|
|
return &__stack - &_end;
|
|
}
|