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