diff --git a/ports/mips/Makefile b/ports/mips/Makefile index aae07df..f92b505 100644 --- a/ports/mips/Makefile +++ b/ports/mips/Makefile @@ -75,8 +75,7 @@ CFLAGS= -g \ -finline-functions \ -nostdinc \ -fno-builtin \ - -fno-stack-protector \ - -DSTAND_ALONE + -fno-stack-protector # Enable stack-checking (disable if not required) ifeq ($(STACK_CHECK),true) diff --git a/ports/mips/atomport.h b/ports/mips/atomport.h index 679cc85..64ba0bc 100644 --- a/ports/mips/atomport.h +++ b/ports/mips/atomport.h @@ -42,7 +42,7 @@ typedef long long int64_t; typedef unsigned long size_t; #define UINT32 uint32_t - +#define STACK_ALIGN_SIZE sizeof(uint32_t) #define NULL ((void *)(0)) /** @@ -54,23 +54,40 @@ typedef unsigned long size_t; #include "printk.h" -/* Critical region protection */ -#define CRITICAL_STORE unsigned int status_reg -#define CRITICAL_START() \ - __asm__ __volatile__("di %0\t\n" \ - "ssnop\t\n" \ - "ssnop\t\n" \ - "ssnop\t\n" \ - "ehb\t\n" \ - :"=r"(status_reg)); +extern uint32_t at_preempt_count; -#define CRITICAL_END() \ - __asm__ __volatile__("ei %0\t\n" \ - "ssnop\t\n" \ - "ssnop\t\n" \ - "ssnop\t\n" \ - "ehb\t\n" \ - ::"r"(status_reg)); +/* Critical region protection */ +#define CRITICAL_STORE uint32_t status_reg +#define CRITICAL_START() \ + do { \ + extern uint32_t at_preempt_count; \ + __asm__ __volatile__("di %0\t\n" \ + "ehb\t\n" \ + :"=r"(status_reg)); \ + at_preempt_count++; \ + }while(0); + +#define CRITICAL_END() \ + do { \ + extern uint32_t at_preempt_count; \ + if (at_preempt_count == 0) { \ + printk("BUG: Preempt count is zero!\n"); \ + for(;;); \ + } \ + at_preempt_count--; \ + \ + if (at_preempt_count == 0) { \ + if (atomCurrentContext()) { \ + printk("+"); \ + __asm__ __volatile__("ei %0\t\n" \ + "ehb\t\n" \ + ::"r"(status_reg));\ + } else { \ + printk("."); \ + } \ + } \ + \ + }while(0); /* Uncomment to enable stack-checking */ /* #define ATOM_STACK_CHECKING */ diff --git a/ports/mips/tests-main.c b/ports/mips/tests-main.c index 6a9cabc..57347aa 100644 --- a/ports/mips/tests-main.c +++ b/ports/mips/tests-main.c @@ -130,6 +130,9 @@ static uint8_t idle_thread_stack[IDLE_STACK_SIZE_BYTES] __attribute__((aligned ( /* Forward declarations */ static void main_thread_func (uint32_t data); +/* Global Data */ +uint32_t at_preempt_count; + /** * \b main * @@ -143,6 +146,8 @@ int main ( void ) { int8_t status; + at_preempt_count = 0; + /** * Note: to protect OS structures and data during initialisation, * interrupts must remain disabled until the first thread @@ -164,8 +169,8 @@ int main ( void ) * If you are not reusing the idle thread's stack during startup then * you should pass in the correct size here. */ - status = atomOSInit(&idle_thread_stack[IDLE_STACK_SIZE_BYTES], - IDLE_STACK_SIZE_BYTES); + status = atomOSInit(&idle_thread_stack[0], + IDLE_STACK_SIZE_BYTES, 0); if (status == ATOM_OK) { /* FIXME: Enable the system tick timer */ @@ -175,8 +180,8 @@ int main ( void ) /* Create an application thread */ status = atomThreadCreate(&main_tcb, TEST_THREAD_PRIO, main_thread_func, 0, - &main_thread_stack[MAIN_STACK_SIZE_BYTES], - MAIN_STACK_SIZE_BYTES); + &main_thread_stack[0], + MAIN_STACK_SIZE_BYTES, 0); if (status == ATOM_OK) { mips_cpu_timer_enable();