CM3: add support to the IAR Embedded Workbench(TM) compiler
[bertos.git] / bertos / cpu / cortex-m3 / hw / switch_ctx_cm3.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Cortex-M3 context switch
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include <cfg/compiler.h>
39 #include <cfg/cfg_proc.h> /* CONFIG_KERN_PREEMPT */
40 #include <cpu/irq.h> /* IRQ_PRIO_DISABLED */
41 #include <cpu/types.h> /* cpu_stack_t */
42 #include <kern/proc_p.h> /* asm_switch_context() prototype */
43 #include <kern/proc.h> /* proc_preempt() */
44 #include "switch_ctx_cm3.h"
45
46 #if CONFIG_KERN_PREEMPT
47 /*
48  * Kernel preemption: implementation details.
49  *
50  * The kernel preemption is implemented using the PendSV IRQ. Inside the
51  * SysTick handler when a process needs to be interrupted (expires its time
52  * quantum or a high-priority process is awakend) a pending PendSV call is
53  * triggered.
54  *
55  * The PendSV handler is called immediately after the SysTick handler, using
56  * the architecture's tail-chaining functionality (an ISR call without the
57  * overhead of state saving and restoration between different IRQs). Inside the
58  * PendSV handler we perform the stack-switching between the old and new
59  * processes.
60  *
61  * Voluntary context switch is implemented as a soft-interrupt call (SVCall),
62  * so any process is always suspended and resumed from an interrupt context.
63  *
64  * NOTE: interrupts must be disabled or enabled when resuming a process context
65  * depending of the type of the previous suspension. If a process was suspended
66  * by a voluntary context switch IRQs must be disabled on resume (voluntary
67  * context switch always happen with IRQs disabled). Instead, if a process was
68  * suspended by the kernel preemption IRQs must be always re-enabled, because
69  * the PendSV handler resumes directly the process context. To keep track of
70  * this, we save the state of the IRQ priority in register r3 before performing
71  * the context switch.
72  *
73  * If CONFIG_KERN_PREEMPT is not enabled the cooperative implementation
74  * fallbacks to the default stack-switching mechanism, performed directly in
75  * thread-mode and implemented as a normal function call.
76  */
77
78 /*
79  * Voluntary context switch handler.
80  */
81 void NAKED svcall_handler(void)
82 {
83         asm volatile (
84         /* Save context */
85                 "mrs r3, basepri\n\t"
86                 "mrs ip, psp\n\t"
87                 "stmdb ip!, {r3-r11, lr}\n\t"
88         /* Stack switch */
89                 "str ip, [r1]\n\t"
90                 "ldr ip, [r0]\n\t"
91         /* Restore context */
92                 "ldmia ip!, {r3-r11, lr}\n\t"
93                 "msr psp, ip\n\t"
94                 "msr basepri, r3\n\t"
95                 "bx lr" : : : "memory");
96 }
97
98 /*
99  * Preemptible context switch handler.
100  */
101 void NAKED pendsv_handler(void)
102 {
103         register cpu_stack_t *stack asm("ip");
104
105         asm volatile (
106                 "mrs r3, basepri\n\t"
107                 "mov %0, %2\n\t"
108                 "msr basepri, %0\n\t"
109                 "mrs %0, psp\n\t"
110                 "stmdb %0!, {r3-r11, lr}\n\t"
111                 : "=r"(stack)
112                 : "r"(stack), "i"(IRQ_PRIO_DISABLED)
113                 : "r3", "memory");
114         proc_current()->stack = stack;
115         proc_preempt();
116         stack = proc_current()->stack;
117         asm volatile (
118                 "ldmia %0!, {r3-r11, lr}\n\t"
119                 "msr psp, %0\n\t"
120                 "msr basepri, r3\n\t"
121                 "bx lr"
122                 : "=r"(stack) : "r"(stack)
123                 : "memory");
124 }
125 #else /* !CONFIG_KERN_PREEMPT */
126 #ifdef __IAR_SYSTEMS_ICC__
127 #else /* __IAR_SYSTEMS_ICC__ */
128 void NAKED asm_switch_context(cpu_stack_t **new_sp, cpu_stack_t **old_sp)
129 {
130         register cpu_stack_t **_new_sp asm("r0") = new_sp;
131         register cpu_stack_t **_old_sp asm("r1") = old_sp;
132
133         asm volatile (
134                 "mrs ip, psp\n\t"
135                 /* Save registers */
136                 "stmdb ip!, {r4-r11, lr}\n\t"
137                 /* Save old stack pointer */
138                 "str ip, [%1]\n\t"
139                 /* Load new stack pointer */
140                 "ldr ip, [%0]\n\t"
141                 /* Load new registers */
142                 "ldmia ip!, {r4-r11, lr}\n\t"
143                 "msr psp, ip\n\t"
144                 "bx lr"
145                 : : "r"(_new_sp), "r"(_old_sp) : "ip", "memory");
146 }
147 #endif /* __IAR_SYSTEMS_ICC__ */
148 #endif /* CONFIG_KERN_PREEMPT */