add packet bound checks as an option
This commit is contained in:
parent
a3f4bd2378
commit
4f8fc7bdfd
45
lib/maca.c
45
lib/maca.c
|
@ -10,6 +10,15 @@
|
||||||
#define PRINTF(...) printf(__VA_ARGS__)
|
#define PRINTF(...) printf(__VA_ARGS__)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef MACA_BOUND_CHECK
|
||||||
|
#define MACA_BOUND_CHECK 0
|
||||||
|
#endif
|
||||||
|
#if (MACA_BOUND_CHECK == 0)
|
||||||
|
#define BOUND_CHECK(x)
|
||||||
|
#else
|
||||||
|
#define BOUND_CHECK(x) bound_check(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef NUM_PACKETS
|
#ifndef NUM_PACKETS
|
||||||
#define NUM_PACKETS 8
|
#define NUM_PACKETS 8
|
||||||
#endif
|
#endif
|
||||||
|
@ -101,6 +110,20 @@ void Print_Packets(char *s) {
|
||||||
printf("found %d packets\n\r",i);
|
printf("found %d packets\n\r",i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void bad_packet_bounds(void) {
|
||||||
|
while(1) { continue; }
|
||||||
|
}
|
||||||
|
|
||||||
|
void bound_check(volatile packet_t *p) {
|
||||||
|
volatile int i;
|
||||||
|
if((p == 0) ||
|
||||||
|
(p == &dummy_ack)) { return; }
|
||||||
|
for(i=0; i < NUM_PACKETS; i++) {
|
||||||
|
if(p == &packet_pool[i]) { return; }
|
||||||
|
}
|
||||||
|
bad_packet_bounds();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* public packet routines */
|
/* public packet routines */
|
||||||
/* heads are to the right */
|
/* heads are to the right */
|
||||||
|
@ -108,13 +131,19 @@ void Print_Packets(char *s) {
|
||||||
void free_packet(volatile packet_t *p) {
|
void free_packet(volatile packet_t *p) {
|
||||||
safe_irq_disable(MACA);
|
safe_irq_disable(MACA);
|
||||||
|
|
||||||
|
BOUND_CHECK(p);
|
||||||
|
|
||||||
if(!p) { PRINTF("free_packet passed packet 0\n\r"); return; }
|
if(!p) { PRINTF("free_packet passed packet 0\n\r"); return; }
|
||||||
if(p == &dummy_ack) { return; }
|
if(p == &dummy_ack) { return; }
|
||||||
|
|
||||||
|
BOUND_CHECK(free_head);
|
||||||
|
|
||||||
p->length = 0; p->offset = 0;
|
p->length = 0; p->offset = 0;
|
||||||
p->left = free_head; p->right = 0;
|
p->left = free_head; p->right = 0;
|
||||||
free_head = p;
|
free_head = p;
|
||||||
|
|
||||||
|
BOUND_CHECK(free_head);
|
||||||
|
|
||||||
irq_restore();
|
irq_restore();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -124,12 +153,16 @@ volatile packet_t* get_free_packet(void) {
|
||||||
|
|
||||||
safe_irq_disable(MACA);
|
safe_irq_disable(MACA);
|
||||||
|
|
||||||
|
BOUND_CHECK(free_head);
|
||||||
|
|
||||||
p = free_head;
|
p = free_head;
|
||||||
if( p != 0 ) {
|
if( p != 0 ) {
|
||||||
free_head = p->left;
|
free_head = p->left;
|
||||||
free_head->right = 0;
|
free_head->right = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOUND_CHECK(free_head);
|
||||||
|
|
||||||
// print_packets("get_free_packet");
|
// print_packets("get_free_packet");
|
||||||
irq_restore();
|
irq_restore();
|
||||||
return p;
|
return p;
|
||||||
|
@ -154,6 +187,8 @@ void post_receive(void) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
BOUND_CHECK(dma_rx);
|
||||||
|
BOUND_CHECK(dma_tx);
|
||||||
*MACA_DMARX = (uint32_t)&(dma_rx->data[0]);
|
*MACA_DMARX = (uint32_t)&(dma_rx->data[0]);
|
||||||
/* with timeout */
|
/* with timeout */
|
||||||
*MACA_SFTCLK = *MACA_CLK + RECV_SOFTIMEOUT; /* soft timeout */
|
*MACA_SFTCLK = *MACA_CLK + RECV_SOFTIMEOUT; /* soft timeout */
|
||||||
|
@ -176,6 +211,8 @@ volatile packet_t* rx_packet(void) {
|
||||||
volatile packet_t *p;
|
volatile packet_t *p;
|
||||||
safe_irq_disable(MACA);
|
safe_irq_disable(MACA);
|
||||||
|
|
||||||
|
BOUND_CHECK(rx_head);
|
||||||
|
|
||||||
p = rx_head;
|
p = rx_head;
|
||||||
if( p != 0 ) {
|
if( p != 0 ) {
|
||||||
rx_head = p->left;
|
rx_head = p->left;
|
||||||
|
@ -203,6 +240,8 @@ void post_tx(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
BOUND_CHECK(dma_rx);
|
||||||
|
BOUND_CHECK(dma_tx);
|
||||||
*MACA_DMARX = (uint32_t)&(dma_rx->data[0]);
|
*MACA_DMARX = (uint32_t)&(dma_rx->data[0]);
|
||||||
/* disable soft timeout clock */
|
/* disable soft timeout clock */
|
||||||
/* disable start clock */
|
/* disable start clock */
|
||||||
|
@ -227,6 +266,8 @@ void post_tx(void) {
|
||||||
void tx_packet(volatile packet_t *p) {
|
void tx_packet(volatile packet_t *p) {
|
||||||
safe_irq_disable(MACA);
|
safe_irq_disable(MACA);
|
||||||
|
|
||||||
|
BOUND_CHECK(p);
|
||||||
|
|
||||||
if(!p) { PRINTF("tx_packet passed packet 0\n\r"); return; }
|
if(!p) { PRINTF("tx_packet passed packet 0\n\r"); return; }
|
||||||
if(tx_head == 0) {
|
if(tx_head == 0) {
|
||||||
/* start a new queue if empty */
|
/* start a new queue if empty */
|
||||||
|
@ -266,6 +307,8 @@ void free_tx_head(void) {
|
||||||
volatile packet_t *p;
|
volatile packet_t *p;
|
||||||
safe_irq_disable(MACA);
|
safe_irq_disable(MACA);
|
||||||
|
|
||||||
|
BOUND_CHECK(tx_head);
|
||||||
|
|
||||||
p = tx_head;
|
p = tx_head;
|
||||||
tx_head = tx_head->left;
|
tx_head = tx_head->left;
|
||||||
if(tx_head == 0) { tx_end = 0; }
|
if(tx_head == 0) { tx_end = 0; }
|
||||||
|
@ -279,6 +322,8 @@ void free_tx_head(void) {
|
||||||
void add_to_rx(volatile packet_t *p) {
|
void add_to_rx(volatile packet_t *p) {
|
||||||
safe_irq_disable(MACA);
|
safe_irq_disable(MACA);
|
||||||
|
|
||||||
|
BOUND_CHECK(p);
|
||||||
|
|
||||||
if(!p) { PRINTF("add_to_rx passed packet 0\n\r"); return; }
|
if(!p) { PRINTF("add_to_rx passed packet 0\n\r"); return; }
|
||||||
p->offset = 1; /* first byte is the length */
|
p->offset = 1; /* first byte is the length */
|
||||||
if(rx_head == 0) {
|
if(rx_head == 0) {
|
||||||
|
|
Loading…
Reference in a new issue