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