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