New macro to fill a new process stack: in this way the stack is always aligned.
[bertos.git] / bertos / cpu / frame.h
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 2008 Bernie Innocenti <bernie@codewiz.org>
30  * Copyright 2004, 2005, 2006, 2007, 2008 Develer S.r.l. (http://www.develer.com/)
31  * Copyright 2004 Giovanni Bajo
32  *
33  * -->
34  *
35  * \brief CPU-specific stack frame handling macros.
36  *
37  * These are mainly used by the portable part of the scheduler
38  * to work with the process stack frames.
39  *
40  * \author Giovanni Bajo <rasky@develer.com>
41  * \author Bernie Innocenti <bernie@codewiz.org>
42  * \author Stefano Fedrigo <aleph@develer.com>
43  * \author Francesco Sacchi <batt@develer.com>
44  */
45 #ifndef CPU_FRAME_H
46 #define CPU_FRAME_H
47
48 #include <cpu/detect.h>
49
50 #include "cfg/cfg_arch.h"      /* ARCH_EMUL */
51 #include <cfg/compiler.h>      /* for uintXX_t */
52
53 #if CPU_X86
54
55         #define CPU_SAVED_REGS_CNT      7
56         #define CPU_STACK_GROWS_UPWARD  0
57         #define CPU_SP_ON_EMPTY_SLOT    0
58
59 #elif CPU_ARM
60
61         #define CPU_SAVED_REGS_CNT     10
62         #define CPU_STACK_GROWS_UPWARD 0
63         #define CPU_SP_ON_EMPTY_SLOT   0
64
65         /**
66          * Initialization value for registers in stack frame.
67          * For the CPSR register, the initial value is set to:
68          * - All flags (N, Z, C, V) set to 0.
69          * - IRQ and FIQ enabled.
70          * - ARM state.
71          * - CPU in Supervisor Mode (SVC).
72          */
73         #define CPU_CREATE_NEW_STACK(stack, entry, exit) \
74         do { \
75                 /* Process entry point */ \
76                 CPU_PUSH_CALL_FRAME(stack, entry); \
77                 /* LR (proc_exit) */ \
78                 CPU_PUSH_CALL_FRAME(stack, exit); \
79                 /* R11 */ \
80                 CPU_PUSH_WORD(stack, 0x11111111); \
81                 /* R10 */ \
82                 CPU_PUSH_WORD(stack, 0x10101010); \
83                 /* R9 */ \
84                 CPU_PUSH_WORD(stack, 0x09090909); \
85                 /* R8 */ \
86                 CPU_PUSH_WORD(stack, 0x08080808); \
87                 /* R7 */ \
88                 CPU_PUSH_WORD(stack, 0x07070707); \
89                 /* R6 */ \
90                 CPU_PUSH_WORD(stack, 0x06060606); \
91                 /* R5 */ \
92                 CPU_PUSH_WORD(stack, 0x05050505); \
93                 /* R4 */ \
94                 CPU_PUSH_WORD(stack, 0x04040404); \
95                 /* CPSR */ \
96                 CPU_PUSH_WORD(stack, 0x00000013); \
97         } while (0)
98
99 #elif CPU_PPC
100
101         #define CPU_SAVED_REGS_CNT     1
102         #define CPU_STACK_GROWS_UPWARD 0
103         #define CPU_SP_ON_EMPTY_SLOT   1
104
105 #elif CPU_DSP56K
106
107         #define CPU_SAVED_REGS_CNT      8
108         #define CPU_STACK_GROWS_UPWARD  1
109         #define CPU_SP_ON_EMPTY_SLOT    0
110
111 #elif CPU_AVR
112
113         #define CPU_SAVED_REGS_CNT     19
114         #define CPU_STACK_GROWS_UPWARD  0
115         #define CPU_SP_ON_EMPTY_SLOT    1
116
117         /**
118          * Initialization value for registers in stack frame.
119          * The register index is not directly corrispondent to CPU
120          * register numbers. Index 0 is the SREG register: the initial
121          * value is all 0 but the interrupt bit (bit 7).
122          */
123         #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
124
125 #else
126         #error No CPU_... defined.
127 #endif
128
129 #ifndef CPU_STACK_GROWS_UPWARD
130         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
131 #endif
132
133 #ifndef CPU_SP_ON_EMPTY_SLOT
134         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
135 #endif
136
137 /// Default for macro not defined in the right arch section
138 #ifndef CPU_REG_INIT_VALUE
139         #define CPU_REG_INIT_VALUE(reg)     0
140 #endif
141
142 /*
143  * Support stack handling peculiarities of a few CPUs.
144  *
145  * Most processors let their stack grow downward and
146  * keep SP pointing at the last pushed value.
147  */
148 #if !CPU_STACK_GROWS_UPWARD
149         #if !CPU_SP_ON_EMPTY_SLOT
150                 /* Most microprocessors (x86, m68k...) */
151                 #define CPU_PUSH_WORD(sp, data) \
152                         do { *--(sp) = (data); } while (0)
153                 #define CPU_POP_WORD(sp) \
154                         (*(sp)++)
155         #else
156                 /* AVR insanity */
157                 #define CPU_PUSH_WORD(sp, data) \
158                         do { *(sp)-- = (data); } while (0)
159                 #define CPU_POP_WORD(sp) \
160                         (*++(sp))
161         #endif
162
163 #else /* CPU_STACK_GROWS_UPWARD */
164
165         #if !CPU_SP_ON_EMPTY_SLOT
166                 /* DSP56K and other weirdos */
167                 #define CPU_PUSH_WORD(sp, data) \
168                         do { *++(sp) = (cpu_stack_t)(data); } while (0)
169                 #define CPU_POP_WORD(sp) \
170                         (*(sp)--)
171         #else
172                 #error I bet you cannot find a CPU like this
173         #endif
174 #endif
175
176
177 #if CPU_DSP56K
178         /*
179          * DSP56k pushes both PC and SR to the stack in the JSR instruction, but
180          * RTS discards SR while returning (it does not restore it). So we push
181          * 0 to fake the same context.
182          */
183         #define CPU_PUSH_CALL_FRAME(sp, func) \
184                 do { \
185                         CPU_PUSH_WORD((sp), (func)); \
186                         CPU_PUSH_WORD((sp), 0x100); \
187                 } while (0);
188
189 #elif CPU_AVR
190         /*
191          * On AVR, addresses are pushed into the stack as little-endian, while
192          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
193          * no natural endianess).
194          */
195         #define CPU_PUSH_CALL_FRAME(sp, func) \
196                 do { \
197                         uint16_t funcaddr = (uint16_t)(func); \
198                         CPU_PUSH_WORD((sp), funcaddr); \
199                         CPU_PUSH_WORD((sp), funcaddr>>8); \
200                 } while (0)
201
202         /*
203          * If the kernel is in idle-spinning, the processor executes:
204          *
205          * IRQ_ENABLE;
206          * CPU_IDLE;
207          * IRQ_DISABLE;
208          *
209          * IRQ_ENABLE is translated in asm as "sei" and IRQ_DISABLE as "cli".
210          * We could define CPU_IDLE to expand to none, so the resulting
211          * asm code would be:
212          *
213          * sei;
214          * cli;
215          *
216          * But Atmel datasheet states:
217          * "When using the SEI instruction to enable interrupts,
218          * the instruction following SEI will be executed *before*
219          * any pending interrupts", so "cli" is executed before any
220          * pending interrupt with the result that IRQs will *NOT*
221          * be enabled!
222          * To ensure that IRQ will run a NOP is required.
223          */
224         #define CPU_IDLE NOP
225
226 #elif CPU_PPC
227
228         #define CPU_PUSH_CALL_FRAME(sp, func) \
229                 do { \
230                         CPU_PUSH_WORD((sp), (cpu_stack_t)(func)); /* LR -> 8(SP) */ \
231                         CPU_PUSH_WORD((sp), 0);                  /* CR -> 4(SP) */ \
232                 } while (0)
233
234 #else
235         #define CPU_PUSH_CALL_FRAME(sp, func) \
236                 CPU_PUSH_WORD((sp), (cpu_stack_t)(func))
237 #endif
238
239 /**
240  * \def CPU_IDLE
241  *
242  * \brief Invoked by the scheduler to stop the CPU when idle.
243  *
244  * This hook can be redefined to put the CPU in low-power mode, or to
245  * profile system load with an external strobe, or to save CPU cycles
246  * in hosted environments such as emulators.
247  */
248 #ifndef CPU_IDLE
249         #if defined(ARCH_QT) && (ARCH & ARCH_QT)
250                 /* This emulator hook should yield the CPU to the host.  */
251                 EXTERN_C_BEGIN
252                 void emul_idle(void);
253                 EXTERN_C_END
254                 #define CPU_IDLE emul_idle()
255         #else /* !ARCH_EMUL */
256                 #define CPU_IDLE do { /* nothing */ } while (0)
257         #endif /* !ARCH_EMUL */
258 #endif /* !CPU_IDLE */
259
260 /**
261  * Default macro for creating a new Process stack
262  */ 
263 #ifndef CPU_CREATE_NEW_STACK
264
265         #define CPU_CREATE_NEW_STACK(stack, entry, exit) \
266                 do { \
267                         size_t i; \
268                         /* Initialize process stack frame */ \
269                         CPU_PUSH_CALL_FRAME(stack, exit); \
270                         CPU_PUSH_CALL_FRAME(stack, entry); \
271                         /* Push a clean set of CPU registers for asm_switch_context() */ \
272                         for (i = 0; i < CPU_SAVED_REGS_CNT; i++) \
273                                 CPU_PUSH_WORD(stack, CPU_REG_INIT_VALUE(i)); \
274                 } while (0)
275 #endif
276
277 #endif /* CPU_ATTR_H */