+ #if (CONFIG_KERN && CONFIG_KERN_PREEMPT)
+ EXTERN_C void asm_irq_switch_context(void);
+
+ /**
+ * At the beginning of any ISR immediately ajust the
+ * return address and store all the caller-save
+ * registers (the ISR may change these registers that
+ * are shared with the user-context).
+ */
+ #define IRQ_ENTRY() asm volatile ( \
+ "sub lr, lr, #4\n\t" \
+ "stmfd sp!, {r0-r3, ip, lr}\n\t")
+ #define IRQ_EXIT() asm volatile ( \
+ "b asm_irq_switch_context\n\t")
+ /**
+ * Function attribute to declare an interrupt service
+ * routine.
+ *
+ * An ISR function must be declared as naked because we
+ * want to add our IRQ_ENTRY() prologue and IRQ_EXIT()
+ * epilogue code to handle the context switch and save
+ * all the registers (not only the callee-save).
+ *
+ */
+ #define ISR_FUNC __attribute__((naked))
+
+ /**
+ * The compiler cannot establish which
+ * registers actually need to be saved, because
+ * the interrupt can happen at any time, so the
+ * "normal" prologue and epilogue used for a
+ * generic function call are not suitable for
+ * the ISR.
+ *
+ * Using a naked function has the drawback that
+ * the stack is not automatically adjusted at
+ * this point, like a "normal" function call.
+ *
+ * So, an ISR can _only_ contain other function
+ * calls and they can't use the stack in any
+ * other way.
+ *
+ * NOTE: we need to explicitly disable IRQs after
+ * IRQ_ENTRY(), because the IRQ status flag is not
+ * masked by the hardware and an IRQ ack inside the ISR
+ * may cause the triggering of another IRQ before
+ * exiting from the current ISR.
+ *
+ * The respective IRQ_ENABLE is not necessary, because
+ * IRQs will be automatically re-enabled when restoring
+ * the context of the user task.
+ */
+ #define DECLARE_ISR_CONTEXT_SWITCH(func) \
+ void ISR_FUNC func(void); \
+ static NOINLINE void __isr_##func(void); \
+ void ISR_FUNC func(void) \
+ { \
+ IRQ_ENTRY(); \
+ IRQ_DISABLE; \
+ __isr_##func(); \
+ IRQ_EXIT(); \
+ } \
+ static NOINLINE void __isr_##func(void)
+ /**
+ * Interrupt service routine prototype: can be used for
+ * forward declarations.
+ */
+ #define ISR_PROTO_CONTEXT_SWITCH(func) \
+ void ISR_FUNC func(void)
+ /**
+ * With task priorities enabled each ISR is used a point to
+ * check if we need to perform a context switch.
+ *
+ * Instead, without priorities a context switch can occur only
+ * when the running task expires its time quantum. In this last
+ * case, the context switch can only occur in the timer
+ * ISR, that must be always declared with the
+ * DECLARE_ISR_CONTEXT_SWITCH() macro.
+ */
+ #if CONFIG_KERN_PRI
+ #define DECLARE_ISR(func) \
+ DECLARE_ISR_CONTEXT_SWITCH(func)
+
+ #define ISR_PROTO(func) \
+ ISR_PROTO_CONTEXT_SWITCH(func)
+ #endif /* !CONFIG_KERN_PRI */
+ #endif /* CONFIG_KERN_PREEMPT */
+
+ #ifndef ISR_FUNC
+ #define ISR_FUNC __attribute__((naked))
+ #endif
+ #ifndef DECLARE_ISR
+ #define DECLARE_ISR(func) \
+ void ISR_FUNC func(void); \
+ /* \
+ * FIXME: avoid the inlining of this function. \
+ * \
+ * This is terribly inefficient, but it's a \
+ * reliable workaround to avoid gcc blowing \
+ * away the stack (see the bug below): \
+ * \
+ * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41999 \
+ */ \
+ static NOINLINE void __isr_##func(void); \
+ void ISR_FUNC func(void) \
+ { \
+ asm volatile ( \
+ "sub lr, lr, #4\n\t" \
+ "stmfd sp!, {r0-r3, ip, lr}\n\t"); \
+ __isr_##func(); \
+ asm volatile ( \
+ "ldmfd sp!, {r0-r3, ip, pc}^\n\t"); \
+ } \
+ static NOINLINE void __isr_##func(void)
+ #endif
+ #ifndef DECLARE_ISR_CONTEXT_SWITCH
+ #define DECLARE_ISR_CONTEXT_SWITCH(func) DECLARE_ISR(func)
+ #endif
+ #ifndef ISR_PROTO
+ #define ISR_PROTO(func) void ISR_FUNC func(void)
+ #endif
+ #ifndef ISR_PROTO_CONTEXT_SWITCH
+ #define ISR_PROTO_CONTEXT_SWITCH(func) ISR_PROTO(func)
+ #endif
+