Merge branch "preempt" in "trunk".
[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         #if CPU_X86_32
55
56                 #define CPU_SAVED_REGS_CNT      4
57                 #define CPU_STACK_GROWS_UPWARD  0
58                 #define CPU_SP_ON_EMPTY_SLOT    0
59
60         #elif CPU_X86_64
61
62                 #define CPU_SAVED_REGS_CNT      8
63                 #define CPU_STACK_GROWS_UPWARD  0
64                 #define CPU_SP_ON_EMPTY_SLOT    0
65         #else
66                 #error "unknown CPU"
67         #endif
68
69 #elif CPU_ARM
70
71         #define CPU_SAVED_REGS_CNT     8
72         #define CPU_STACK_GROWS_UPWARD 0
73         #define CPU_SP_ON_EMPTY_SLOT   0
74
75 #elif CPU_PPC
76
77         #define CPU_SAVED_REGS_CNT     1
78         #define CPU_STACK_GROWS_UPWARD 0
79         #define CPU_SP_ON_EMPTY_SLOT   1
80
81 #elif CPU_DSP56K
82
83         #define CPU_SAVED_REGS_CNT      8
84         #define CPU_STACK_GROWS_UPWARD  1
85         #define CPU_SP_ON_EMPTY_SLOT    0
86
87 #elif CPU_AVR
88
89         #define CPU_SAVED_REGS_CNT     18
90         #define CPU_STACK_GROWS_UPWARD  0
91         #define CPU_SP_ON_EMPTY_SLOT    1
92
93 #else
94         #error No CPU_... defined.
95 #endif
96
97 #ifndef CPU_STACK_GROWS_UPWARD
98         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
99 #endif
100
101 #ifndef CPU_SP_ON_EMPTY_SLOT
102         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
103 #endif
104
105 /// Default for macro not defined in the right arch section
106 #ifndef CPU_REG_INIT_VALUE
107         #define CPU_REG_INIT_VALUE(reg)     (reg)
108 #endif
109
110 /*
111  * Support stack handling peculiarities of a few CPUs.
112  *
113  * Most processors let their stack grow downward and
114  * keep SP pointing at the last pushed value.
115  */
116 #if !CPU_STACK_GROWS_UPWARD
117         #if !CPU_SP_ON_EMPTY_SLOT
118                 /* Most microprocessors (x86, m68k...) */
119                 #define CPU_PUSH_WORD(sp, data) \
120                         do { *--(sp) = (data); } while (0)
121                 #define CPU_POP_WORD(sp) \
122                         (*(sp)++)
123         #else
124                 /* AVR insanity */
125                 #define CPU_PUSH_WORD(sp, data) \
126                         do { *(sp)-- = (data); } while (0)
127                 #define CPU_POP_WORD(sp) \
128                         (*++(sp))
129         #endif
130
131 #else /* CPU_STACK_GROWS_UPWARD */
132
133         #if !CPU_SP_ON_EMPTY_SLOT
134                 /* DSP56K and other weirdos */
135                 #define CPU_PUSH_WORD(sp, data) \
136                         do { *++(sp) = (cpu_stack_t)(data); } while (0)
137                 #define CPU_POP_WORD(sp) \
138                         (*(sp)--)
139         #else
140                 #error I bet you cannot find a CPU like this
141         #endif
142 #endif
143
144
145 #if CPU_DSP56K
146         /*
147          * DSP56k pushes both PC and SR to the stack in the JSR instruction, but
148          * RTS discards SR while returning (it does not restore it). So we push
149          * 0 to fake the same context.
150          */
151         #define CPU_PUSH_CALL_FRAME(sp, func) \
152                 do { \
153                         CPU_PUSH_WORD((sp), (func)); \
154                         CPU_PUSH_WORD((sp), 0x100); \
155                 } while (0);
156
157 #elif CPU_AVR
158         /*
159          * On AVR, addresses are pushed into the stack as little-endian, while
160          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
161          * no natural endianess).
162          */
163         #define CPU_PUSH_CALL_FRAME(sp, func) \
164                 do { \
165                         uint16_t funcaddr = (uint16_t)(func); \
166                         CPU_PUSH_WORD((sp), funcaddr); \
167                         CPU_PUSH_WORD((sp), funcaddr>>8); \
168                 } while (0)
169
170         /*
171          * If the kernel is in idle-spinning, the processor executes:
172          *
173          * IRQ_ENABLE;
174          * CPU_IDLE;
175          * IRQ_DISABLE;
176          *
177          * IRQ_ENABLE is translated in asm as "sei" and IRQ_DISABLE as "cli".
178          * We could define CPU_IDLE to expand to none, so the resulting
179          * asm code would be:
180          *
181          * sei;
182          * cli;
183          *
184          * But Atmel datasheet states:
185          * "When using the SEI instruction to enable interrupts,
186          * the instruction following SEI will be executed *before*
187          * any pending interrupts", so "cli" is executed before any
188          * pending interrupt with the result that IRQs will *NOT*
189          * be enabled!
190          * To ensure that IRQ will run a NOP is required.
191          */
192         #define CPU_IDLE NOP
193
194 #elif CPU_PPC
195
196         #define CPU_PUSH_CALL_FRAME(sp, func) \
197                 do { \
198                         CPU_PUSH_WORD((sp), (cpu_stack_t)(func)); /* LR -> 8(SP) */ \
199                         CPU_PUSH_WORD((sp), 0);                  /* CR -> 4(SP) */ \
200                 } while (0)
201
202 #else
203         #define CPU_PUSH_CALL_FRAME(sp, func) \
204                 CPU_PUSH_WORD((sp), (cpu_stack_t)(func))
205 #endif
206
207 /**
208  * \def CPU_IDLE
209  *
210  * \brief Invoked by the scheduler to stop the CPU when idle.
211  *
212  * This hook can be redefined to put the CPU in low-power mode, or to
213  * profile system load with an external strobe, or to save CPU cycles
214  * in hosted environments such as emulators.
215  */
216 #ifndef CPU_IDLE
217         #if defined(ARCH_QT) && (ARCH & ARCH_QT)
218                 /* This emulator hook should yield the CPU to the host.  */
219                 EXTERN_C_BEGIN
220                 void emul_idle(void);
221                 EXTERN_C_END
222                 #define CPU_IDLE emul_idle()
223         #else /* !ARCH_EMUL */
224                 #define CPU_IDLE do { /* nothing */ } while (0)
225         #endif /* !ARCH_EMUL */
226 #endif /* !CPU_IDLE */
227
228 /**
229  * Default macro for creating a new Process stack
230  */
231 #ifndef CPU_CREATE_NEW_STACK
232
233         #define CPU_CREATE_NEW_STACK(stack) \
234                 do { \
235                         size_t i; \
236                         /* Initialize process stack frame */ \
237                         CPU_PUSH_CALL_FRAME(stack, proc_entry); \
238                         /* Push a clean set of CPU registers for asm_switch_context() */ \
239                         for (i = 0; i < CPU_SAVED_REGS_CNT; i++) \
240                                 CPU_PUSH_WORD(stack, CPU_REG_INIT_VALUE(i)); \
241                 } while (0)
242 #endif
243
244 #endif /* CPU_ATTR_H */