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