ASSERT_VALID_PTR(): Make bottom of memory a per-CPU constant
[bertos.git] / bertos / cpu / attr.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 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2004 Giovanni Bajo
31  *
32  * -->
33  *
34  * \brief CPU-specific attributes.
35  *
36  * \author Giovanni Bajo <rasky@develer.com>
37  * \author Bernie Innocenti <bernie@codewiz.org>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  * \author Francesco Sacchi <batt@develer.com>
40  */
41 #ifndef CPU_ATTR_H
42 #define CPU_ATTR_H
43
44 #include "detect.h"
45
46 #include "cfg/cfg_attr.h"      /* CONFIG_FAST_MEM */
47 #include "cfg/cfg_arch.h"      /* ARCH_EMUL */
48 #include <cfg/compiler.h>      /* for uintXX_t */
49
50
51 /**
52  * \name Macros for determining CPU endianness.
53  * \{
54  */
55 #define CPU_BIG_ENDIAN    0x1234
56 #define CPU_LITTLE_ENDIAN 0x3412 /* Look twice, pal. This is not a bug. */
57 /*\}*/
58
59 /** Macro to include cpu-specific versions of the headers. */
60 #define CPU_HEADER(module)          PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).h)
61
62 /** Macro to include cpu-specific versions of implementation files. */
63 #define CPU_CSOURCE(module)         PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).c)
64
65
66 #if CPU_I196
67
68         #define NOP                     nop_instruction()
69
70         #define CPU_REG_BITS            16
71         #define CPU_REGS_CNT            16
72         #define CPU_STACK_GROWS_UPWARD  0
73         #define CPU_SP_ON_EMPTY_SLOT    0
74         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
75         #define CPU_HARVARD             0
76
77         /// Valid pointers should be >= than this value (used for debug)
78         #define CPU_RAM_START           0x100
79
80 #elif CPU_X86
81
82         #define NOP                     asm volatile ("nop")
83
84         #define CPU_REGS_CNT            7
85         #define CPU_SAVED_REGS_CNT      7
86         #define CPU_STACK_GROWS_UPWARD  0
87         #define CPU_SP_ON_EMPTY_SLOT    0
88         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
89         #define CPU_HARVARD             0
90
91         #if CPU_X86_64
92                 #define CPU_REG_BITS    64
93
94                 #ifdef __WIN64__
95                         /* WIN64 is an IL32-P64 weirdo. */
96                         #define SIZEOF_LONG  4
97                 #endif
98         #else
99                 #define CPU_REG_BITS    32
100         #endif
101
102         /// Valid pointers should be >= than this value (used for debug)
103         #define CPU_RAM_START           0x1000
104
105 #elif CPU_ARM
106
107         /* Register counts include SREG too */
108         #define CPU_REG_BITS           32
109         #define CPU_REGS_CNT           16
110         #define CPU_SAVED_REGS_CNT     9
111         #define CPU_STACK_GROWS_UPWARD 0
112         #define CPU_SP_ON_EMPTY_SLOT   0
113         #define CPU_HARVARD            0
114
115         /// Valid pointers should be >= than this value (used for debug)
116         #define CPU_RAM_START           0x200
117
118         #ifdef __IAR_SYSTEMS_ICC__
119                 #warning Check CPU_BYTE_ORDER
120                 #define CPU_BYTE_ORDER (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
121
122                 #define NOP            __no_operation()
123
124         #else /* GCC and compatibles */
125
126                 #if defined(__ARMEB__)
127                         #define CPU_BYTE_ORDER CPU_BIG_ENDIAN
128                 #elif defined(__ARMEL__)
129                         #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
130                 #else
131                         #error Unable to detect ARM endianness!
132                 #endif
133
134                 #define NOP            asm volatile ("mov r0,r0" ::)
135
136                 /**
137                  * Initialization value for registers in stack frame.
138                  * The register index is not directly corrispondent to CPU
139                  * register numbers, but is related to how are pushed to
140                  * stack (\see asm_switch_context).
141                  * Index (CPU_SAVED_REGS_CNT - 1) is the CPSR register,
142                  * the initial value is set to:
143                  * - All flags (N, Z, C, V) set to 0.
144                  * - IRQ and FIQ enabled.
145                  * - ARM state.
146                  * - CPU in Supervisor Mode (SVC).
147                  */
148                 #define CPU_REG_INIT_VALUE(reg) (reg == (CPU_SAVED_REGS_CNT - 1) ? 0x13 : 0)
149
150                 #if CONFIG_FAST_MEM
151                         /**
152                          * Function attribute for use with performance critical code.
153                          *
154                          * On the AT91 family, code residing in flash has wait states.
155                          * Moving functions to the data section is a quick & dirty way
156                          * to get them transparently copied to SRAM for zero-wait-state
157                          * operation.
158                          */
159                         #define FAST_FUNC __attribute__((section(".data")))
160
161                         /**
162                          * Data attribute to move constant data to fast memory storage.
163                          *
164                          * \see FAST_FUNC
165                          */
166                         #define FAST_RODATA __attribute__((section(".data")))
167
168                 #else // !CONFIG_FAST_MEM
169                         #define FAST_RODATA /**/
170                         #define FAST_FUNC /**/
171                 #endif
172
173                 /**
174                  * Function attribute to declare an interrupt service routine.
175                  */
176                 #define ISR_FUNC __attribute__((interrupt))
177
178         #endif /* !__IAR_SYSTEMS_ICC_ */
179
180 #elif CPU_PPC
181         #define NOP                 asm volatile ("nop" ::)
182
183         /* Register counts include SREG too */
184         #define CPU_REG_BITS           (CPU_PPC32 ? 32 : 64)
185         #define CPU_REGS_CNT           FIXME
186         #define CPU_SAVED_REGS_CNT     1  // FIXME
187         #define CPU_STACK_GROWS_UPWARD 0  //FIXME
188         #define CPU_SP_ON_EMPTY_SLOT   0  //FIXME
189         #define CPU_BYTE_ORDER         (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
190         #define CPU_HARVARD            0
191
192         /// Valid pointers should be >= than this value (used for debug)
193         #define CPU_RAM_START           0x1000
194
195 #elif CPU_DSP56K
196
197         #define NOP                     asm(nop)
198
199         #define CPU_REG_BITS            16
200         #define CPU_REGS_CNT            FIXME
201         #define CPU_SAVED_REGS_CNT      8
202         #define CPU_STACK_GROWS_UPWARD  1
203         #define CPU_SP_ON_EMPTY_SLOT    0
204         #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
205         #define CPU_HARVARD             1
206
207         /* Memory is word-addessed in the DSP56K */
208         #define CPU_BITS_PER_CHAR  16
209         #define SIZEOF_SHORT        1
210         #define SIZEOF_INT          1
211         #define SIZEOF_LONG         2
212         #define SIZEOF_PTR          1
213
214         /// Valid pointers should be >= than this value (used for debug)
215         #define CPU_RAM_START           0x200
216
217 #elif CPU_AVR
218
219         #define NOP           asm volatile ("nop" ::)
220
221         /* Register counts include SREG too */
222         #define CPU_REG_BITS            8
223         #define CPU_REGS_CNT           33
224         #define CPU_SAVED_REGS_CNT     19
225         #define CPU_STACK_GROWS_UPWARD  0
226         #define CPU_SP_ON_EMPTY_SLOT    1
227         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
228         #define CPU_HARVARD             1
229
230         /**
231          * Initialization value for registers in stack frame.
232          * The register index is not directly corrispondent to CPU
233          * register numbers. Index 0 is the SREG register: the initial
234          * value is all 0 but the interrupt bit (bit 7).
235          */
236         #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
237
238         /// Valid pointers should be >= than this value (used for debug)
239         #define CPU_RAM_START           0x100
240
241 #else
242         #error No CPU_... defined.
243 #endif
244
245 /// Default for macro not defined in the right arch section
246 #ifndef CPU_REG_INIT_VALUE
247         #define CPU_REG_INIT_VALUE(reg)     0
248 #endif
249
250 #ifndef CPU_STACK_GROWS_UPWARD
251         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
252 #endif
253
254 #ifndef CPU_SP_ON_EMPTY_SLOT
255         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
256 #endif
257
258 #ifndef FAST_FUNC
259         /**
260          * Function attribute for use with performance critical code.
261          */
262         #define FAST_FUNC /* */
263 #endif
264
265 #ifndef FAST_RODATA
266         /**
267          * Data attribute to move constant data to fast memory storage.
268          */
269         #define FAST_RODATA /* */
270 #endif
271
272 /*
273  * Support stack handling peculiarities of a few CPUs.
274  *
275  * Most processors let their stack grow downward and
276  * keep SP pointing at the last pushed value.
277  */
278 #if !CPU_STACK_GROWS_UPWARD
279         #if !CPU_SP_ON_EMPTY_SLOT
280                 /* Most microprocessors (x86, m68k...) */
281                 #define CPU_PUSH_WORD(sp, data) \
282                         do { *--(sp) = (data); } while (0)
283                 #define CPU_POP_WORD(sp) \
284                         (*(sp)++)
285         #else
286                 /* AVR insanity */
287                 #define CPU_PUSH_WORD(sp, data) \
288                         do { *(sp)-- = (data); } while (0)
289                 #define CPU_POP_WORD(sp) \
290                         (*++(sp))
291         #endif
292
293 #else /* CPU_STACK_GROWS_UPWARD */
294
295         #if !CPU_SP_ON_EMPTY_SLOT
296                 /* DSP56K and other weirdos */
297                 #define CPU_PUSH_WORD(sp, data) \
298                         do { *++(sp) = (cpustack_t)(data); } while (0)
299                 #define CPU_POP_WORD(sp) \
300                         (*(sp)--)
301         #else
302                 #error I bet you cannot find a CPU like this
303         #endif
304 #endif
305
306
307 #if CPU_DSP56K
308         /*
309          * DSP56k pushes both PC and SR to the stack in the JSR instruction, but
310          * RTS discards SR while returning (it does not restore it). So we push
311          * 0 to fake the same context.
312          */
313         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
314                 do { \
315                         CPU_PUSH_WORD((sp), (func)); \
316                         CPU_PUSH_WORD((sp), 0x100); \
317                 } while (0);
318
319 #elif CPU_AVR
320         /*
321          * In AVR, the addresses are pushed into the stack as little-endian, while
322          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
323          * no natural endianess).
324          */
325         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
326                 do { \
327                         uint16_t funcaddr = (uint16_t)(func); \
328                         CPU_PUSH_WORD((sp), funcaddr); \
329                         CPU_PUSH_WORD((sp), funcaddr>>8); \
330                 } while (0)
331
332         /*
333          * If the kernel is in idle-spinning, the processor executes:
334          *
335          * IRQ_ENABLE;
336          * CPU_IDLE;
337          * IRQ_DISABLE;
338          *
339          * IRQ_ENABLE is translated in asm as "sei" and IRQ_DISABLE as "cli".
340          * We could define CPU_IDLE to expand to none, so the resulting
341          * asm code would be:
342          *
343          * sei;
344          * cli;
345          *
346          * But Atmel datasheet states:
347          * "When using the SEI instruction to enable interrupts,
348          * the instruction following SEI will be executed *before*
349          * any pending interrupts", so "cli" is executed before any
350          * pending interrupt with the result that IRQs will *NOT*
351          * be enabled!
352          * To ensure that IRQ will run a NOP is required.
353          */
354         #define CPU_IDLE NOP
355
356 #else
357         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
358                 CPU_PUSH_WORD((sp), (cpustack_t)(func))
359 #endif
360
361 /**
362  * \def CPU_IDLE
363  *
364  * \brief Invoked by the scheduler to stop the CPU when idle.
365  *
366  * This hook can be redefined to put the CPU in low-power mode, or to
367  * profile system load with an external strobe, or to save CPU cycles
368  * in hosted environments such as emulators.
369  */
370 #ifndef CPU_IDLE
371         #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
372                 /* This emulator hook should yield the CPU to the host.  */
373                 EXTERN_C_BEGIN
374                 void emul_idle(void);
375                 EXTERN_C_END
376                 #define CPU_IDLE emul_idle()
377         #else /* !ARCH_EMUL */
378                 #define CPU_IDLE do { /* nothing */ } while (0)
379         #endif /* !ARCH_EMUL */
380 #endif /* !CPU_IDLE */
381
382 #endif /* CPU_ATTR_H */