Add X86_64 support.
[bertos.git] / cfg / cpu.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2004 Giovanni Bajo
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \brief CPU-specific definitions
10  *
11  * \version $Id$
12  *
13  * \author Giovanni Bajo <rasky@develer.com>
14  * \author Bernardo Innocenti <bernie@develer.com>
15  * \author Stefano Fedrigo <aleph@develer.com>
16  */
17
18 /*#*
19  *#* $Log$
20  *#* Revision 1.4  2005/06/14 06:15:10  bernie
21  *#* Add X86_64 support.
22  *#*
23  *#* Revision 1.3  2005/04/12 04:06:17  bernie
24  *#* Catch missing CPU earlier.
25  *#*
26  *#* Revision 1.2  2005/04/11 19:10:27  bernie
27  *#* Include top-level headers from cfg/ subdir.
28  *#*
29  *#* Revision 1.1  2005/04/11 19:04:13  bernie
30  *#* Move top-level headers to cfg/ subdir.
31  *#*
32  *#* Revision 1.30  2005/03/15 00:20:09  bernie
33  *#* BREAKPOINT, IRQ_RUNNING(), IRQ_GETSTATE(): New DSP56K macros.
34  *#*
35  *#* Revision 1.29  2005/02/16 20:33:24  bernie
36  *#* Preliminary PPC support.
37  *#*
38  *#* Revision 1.28  2004/12/31 17:39:41  bernie
39  *#* Fix documentation.
40  *#*
41  *#* Revision 1.27  2004/12/31 17:02:47  bernie
42  *#* IRQ_SAVE_DISABLE(), IRQ_RESTORE(): Add null stubs for x86.
43  *#*
44  *#* Revision 1.26  2004/12/13 12:08:12  bernie
45  *#* DISABLE_IRQSAVE, ENABLE_IRQRESTORE, DISABLE_INTS, ENABLE_INTS: Remove obsolete macros.
46  *#*
47  *#* Revision 1.25  2004/12/08 08:31:02  bernie
48  *#* CPU_HARVARD: Define to 1 for AVR and DSP56K.
49  *#*/
50 #ifndef DEVLIB_CPU_H
51 #define DEVLIB_CPU_H
52
53 #include <cfg/compiler.h> /* for uintXX_t */
54
55
56 /*!
57  * \name Macros for determining CPU endianness.
58  * \{
59  */
60 #define CPU_BIG_ENDIAN    0x1234
61 #define CPU_LITTLE_ENDIAN 0x3412
62 /*\}*/
63
64 /*! Macro to include cpu-specific versions of the headers. */
65 #define CPU_HEADER(module)          PP_STRINGIZE(PP_CAT3(module, _, CPU_ID).h)
66
67
68 #if CPU_I196
69
70         #define NOP                     nop_instruction()
71         #define IRQ_DISABLE             disable_interrupt()
72         #define IRQ_ENABLE              enable_interrupt()
73
74         typedef uint16_t cpuflags_t; // FIXME
75         typedef unsigned int cpustack_t;
76
77         #define CPU_REG_BITS            16
78         #define CPU_REGS_CNT            16
79         #define CPU_STACK_GROWS_UPWARD  0
80         #define CPU_SP_ON_EMPTY_SLOT    0
81         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
82         #define CPU_HARVARD             0
83
84 #elif CPU_X86
85
86         #define NOP                     asm volatile ("nop")
87         #define IRQ_DISABLE             FIXME
88         #define IRQ_ENABLE              FIXME
89         #define IRQ_SAVE_DISABLE(x)     FIXME
90         #define IRQ_RESTORE(x)          FIXME
91
92         typedef uint32_t cpuflags_t; // FIXME
93
94         #define CPU_REGS_CNT            7
95         #define CPU_STACK_GROWS_UPWARD  0
96         #define CPU_SP_ON_EMPTY_SLOT    0
97         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
98         #define CPU_HARVARD             0
99
100         #if CPU_X86_64
101                 typedef uint64_t cpustack_t;
102                 #define CPU_REG_BITS    64
103
104                 #ifdef __WIN64__
105                         /* WIN64 is an IL32-P64 weirdo. */
106                         #define SIZEOF_LONG  4
107                 #endif
108         #else
109                 typedef uint32_t cpustack_t;
110                 #define CPU_REG_BITS    32
111         #endif
112
113 #elif CPU_PPC
114         #define NOP                 asm volatile ("nop" ::)
115         #define IRQ_DISABLE         FIXME
116         #define IRQ_ENABLE          FIXME
117         #define IRQ_SAVE_DISABLE(x) FIXME
118         #define IRQ_RESTORE(x)      FIXME
119         #define IRQ_GETSTATE()      FIXME
120
121         typedef uint32_t cpuflags_t; // FIXME
122         typedef uint32_t cpustack_t; // FIXME
123
124         /* Register counts include SREG too */
125         #define CPU_REG_BITS           (CPU_PPC32 ? 32 : 64)
126         #define CPU_REGS_CNT           FIXME
127         #define CPU_SAVED_REGS_CNT     FIXME
128         #define CPU_STACK_GROWS_UPWARD 0  //FIXME
129         #define CPU_SP_ON_EMPTY_SLOT   0  //FIXME
130         #define CPU_BYTE_ORDER         (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
131         #define CPU_HARVARD            0
132
133 #elif CPU_DSP56K
134
135         #define NOP                     asm(nop)
136         #define BREAKPOINT              asm(debug)
137         #define IRQ_DISABLE             do { asm(bfset #0x0200,SR); asm(nop); } while (0)
138         #define IRQ_ENABLE              do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
139
140         #define IRQ_SAVE_DISABLE(x)  \
141                 do { (void)x; asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
142         #define IRQ_RESTORE(x)  \
143                 do { (void)x; asm(move x,SR); } while (0)
144
145         static inline bool irq_running(void)
146         {
147                 extern void *user_sp;
148                 return !!user_sp;
149         }
150         #define IRQ_RUNNING() irq_running()
151
152         static inline bool irq_getstate(void)
153         {
154                 uint16_t x;
155                 asm(move SR,x);
156                 return !(x & 0x0200);
157         }
158         #define IRQ_GETSTATE() irq_getstate()
159
160
161
162         typedef uint16_t cpuflags_t;
163         typedef unsigned int cpustack_t;
164
165         #define CPU_REG_BITS            16
166         #define CPU_REGS_CNT            FIXME
167         #define CPU_SAVED_REGS_CNT      8
168         #define CPU_STACK_GROWS_UPWARD  1
169         #define CPU_SP_ON_EMPTY_SLOT    0
170         #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
171         #define CPU_HARVARD             1
172
173         /* Memory is word-addessed in the DSP56K */
174         #define CPU_BITS_PER_CHAR  16
175         #define SIZEOF_SHORT        1
176         #define SIZEOF_INT          1
177         #define SIZEOF_LONG         2
178         #define SIZEOF_PTR          1
179
180 #elif CPU_AVR
181
182         #define NOP           asm volatile ("nop" ::)
183         #define IRQ_DISABLE   asm volatile ("cli" ::)
184         #define IRQ_ENABLE    asm volatile ("sei" ::)
185
186         #define IRQ_SAVE_DISABLE(x) \
187         do { \
188                 __asm__ __volatile__( \
189                         "in %0,__SREG__\n\t" \
190                         "cli" \
191                         : "=r" (x) : /* no inputs */ : "cc" \
192                 ); \
193         } while (0)
194
195         #define IRQ_RESTORE(x) \
196         do { \
197                 __asm__ __volatile__( \
198                         "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
199                 ); \
200         } while (0)
201
202         #define IRQ_GETSTATE() \
203         ({ \
204                 uint8_t sreg; \
205                 __asm__ __volatile__( \
206                         "in %0,__SREG__\n\t" \
207                         : "=r" (sreg)  /* no inputs & no clobbers */ \
208                 ); \
209                 (bool)(sreg & 0x80); \
210         })
211
212         typedef uint8_t cpuflags_t;
213         typedef uint8_t cpustack_t;
214
215         /* Register counts include SREG too */
216         #define CPU_REG_BITS            8
217         #define CPU_REGS_CNT           33
218         #define CPU_SAVED_REGS_CNT     19
219         #define CPU_STACK_GROWS_UPWARD  0
220         #define CPU_SP_ON_EMPTY_SLOT    1
221         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
222         #define CPU_HARVARD             1
223
224         /*!
225          * Initialization value for registers in stack frame.
226          * The register index is not directly corrispondent to CPU
227          * register numbers. Index 0 is the SREG register: the initial
228          * value is all 0 but the interrupt bit (bit 7).
229          */
230         #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
231
232 #else
233         #error No CPU_... defined.
234 #endif
235
236 /*!
237  * Execute \a CODE atomically with respect to interrupts.
238  *
239  * \see IRQ_SAVE_DISABLE IRQ_RESTORE
240  */
241 #define ATOMIC(CODE) \
242         do { \
243                 cpuflags_t __flags; \
244                 IRQ_SAVE_DISABLE(__flags); \
245                 CODE; \
246                 IRQ_RESTORE(__flags); \
247         } while (0)
248
249
250 //! Default for macro not defined in the right arch section
251 #ifndef CPU_REG_INIT_VALUE
252         #define CPU_REG_INIT_VALUE(reg)     0
253 #endif
254
255
256 #ifndef CPU_STACK_GROWS_UPWARD
257         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
258 #endif
259
260 #ifndef CPU_SP_ON_EMPTY_SLOT
261         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
262 #endif
263
264 /*
265  * Support stack handling peculiarities of a few CPUs.
266  *
267  * Most processors let their stack grow downward and
268  * keep SP pointing at the last pushed value.
269  */
270 #if !CPU_STACK_GROWS_UPWARD
271         #if !CPU_SP_ON_EMPTY_SLOT
272                 /* Most microprocessors (x86, m68k...) */
273                 #define CPU_PUSH_WORD(sp, data) \
274                         do { *--(sp) = (data); } while (0)
275                 #define CPU_POP_WORD(sp) \
276                         (*(sp)++)
277         #else
278                 /* AVR insanity */
279                 #define CPU_PUSH_WORD(sp, data) \
280                         do { *(sp)-- = (data); } while (0)
281                 #define CPU_POP_WORD(sp) \
282                         (*++(sp))
283         #endif
284
285 #else /* CPU_STACK_GROWS_UPWARD */
286
287         #if !CPU_SP_ON_EMPTY_SLOT
288                 /* DSP56K and other weirdos */
289                 #define CPU_PUSH_WORD(sp, data) \
290                         do { *++(sp) = (cpustack_t)(data); } while (0)
291                 #define CPU_POP_WORD(sp) \
292                         (*(sp)--)
293         #else
294                 #error I bet you cannot find a CPU like this
295         #endif
296 #endif
297
298
299 #if CPU_DSP56K
300         /*
301          * DSP56k pushes both PC and SR to the stack in the JSR instruction, but
302          * RTS discards SR while returning (it does not restore it). So we push
303          * 0 to fake the same context.
304          */
305         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
306                 do { \
307                         CPU_PUSH_WORD((sp), (func)); \
308                         CPU_PUSH_WORD((sp), 0x100); \
309                 } while (0);
310
311 #elif CPU_AVR
312         /*
313          * In AVR, the addresses are pushed into the stack as little-endian, while
314          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
315          * no natural endianess).
316          */
317         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
318                 do { \
319                         uint16_t funcaddr = (uint16_t)(func); \
320                         CPU_PUSH_WORD((sp), funcaddr); \
321                         CPU_PUSH_WORD((sp), funcaddr>>8); \
322                 } while (0)
323
324 #else
325         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
326                 CPU_PUSH_WORD((sp), (func))
327 #endif
328
329
330 /*!
331  * \name Default type sizes.
332  *
333  * These defaults are reasonable for most 16/32bit machines.
334  * Some of these macros may be overridden by CPU-specific code above.
335  *
336  * ANSI C requires that the following equations be true:
337  * \code
338  *   sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
339  *   sizeof(float) <= sizeof(double)
340  *   CPU_BITS_PER_CHAR  >= 8
341  *   CPU_BITS_PER_SHORT >= 8
342  *   CPU_BITS_PER_INT   >= 16
343  *   CPU_BITS_PER_LONG  >= 32
344  * \endcode
345  * \{
346  */
347 #ifndef SIZEOF_CHAR
348 #define SIZEOF_CHAR  1
349 #endif
350
351 #ifndef SIZEOF_SHORT
352 #define SIZEOF_SHORT  2
353 #endif
354
355 #ifndef SIZEOF_INT
356 #if CPU_REG_BITS < 32
357         #define SIZEOF_INT  2
358 #else
359         #define SIZEOF_INT  4
360 #endif
361 #endif /* !SIZEOF_INT */
362
363 #ifndef SIZEOF_LONG
364 #if CPU_REG_BITS > 32
365         #define SIZEOF_LONG  8
366 #else
367         #define SIZEOF_LONG  4
368 #endif
369 #endif
370
371 #ifndef SIZEOF_PTR
372 #if CPU_REG_BITS < 32
373         #define SIZEOF_PTR   2
374 #elif CPU_REG_BITS == 32
375         #define SIZEOF_PTR   4
376 #else /* CPU_REG_BITS > 32 */
377         #define SIZEOF_PTR   8
378 #endif
379
380 #ifndef CPU_BITS_PER_CHAR
381 #define CPU_BITS_PER_CHAR   (SIZEOF_CHAR * 8)
382 #endif
383
384 #ifndef CPU_BITS_PER_SHORT
385 #define CPU_BITS_PER_SHORT  (SIZEOF_SHORT * CPU_BITS_PER_CHAR)
386 #endif
387
388 #ifndef CPU_BITS_PER_INT
389 #define CPU_BITS_PER_INT    (SIZEOF_INT * CPU_BITS_PER_CHAR)
390 #endif
391
392 #ifndef CPU_BITS_PER_LONG
393 #define CPU_BITS_PER_LONG   (SIZEOF_LONG * CPU_BITS_PER_CHAR)
394 #endif
395
396 #ifndef CPU_BITS_PER_PTR
397 #define CPU_BITS_PER_PTR    (SIZEOF_PTR * CPU_BITS_PER_CHAR)
398 #endif
399
400 #ifndef BREAKPOINT
401 #define BREAKPOINT /* nop */
402 #endif
403
404 /*\}*/
405
406 /* Sanity checks for the above definitions */
407 STATIC_ASSERT(sizeof(char) == SIZEOF_CHAR);
408 STATIC_ASSERT(sizeof(short) == SIZEOF_SHORT);
409 STATIC_ASSERT(sizeof(long) == SIZEOF_LONG);
410 STATIC_ASSERT(sizeof(int) == SIZEOF_INT);
411 STATIC_ASSERT(sizeof(void *) == SIZEOF_PTR);
412 STATIC_ASSERT(sizeof(int8_t) * CPU_BITS_PER_CHAR == 8);
413 STATIC_ASSERT(sizeof(uint8_t) * CPU_BITS_PER_CHAR == 8);
414 STATIC_ASSERT(sizeof(int16_t) * CPU_BITS_PER_CHAR == 16);
415 STATIC_ASSERT(sizeof(uint16_t) * CPU_BITS_PER_CHAR == 16);
416 STATIC_ASSERT(sizeof(int32_t) * CPU_BITS_PER_CHAR == 32);
417 STATIC_ASSERT(sizeof(uint32_t) * CPU_BITS_PER_CHAR == 32);
418 #ifdef __HAS_INT64_T__
419 STATIC_ASSERT(sizeof(int64_t) * CPU_BITS_PER_CHAR == 64);
420 STATIC_ASSERT(sizeof(uint64_t) * CPU_BITS_PER_CHAR == 64);
421 #endif
422
423 /*!
424  * \def CPU_IDLE
425  *
426  * \brief Invoked by the scheduler to stop the CPU when idle.
427  *
428  * This hook can be redefined to put the CPU in low-power mode, or to
429  * profile system load with an external strobe, or to save CPU cycles
430  * in hosted environments such as emulators.
431  */
432 #ifndef CPU_IDLE
433         #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
434                 /* This emulator hook should yield the CPU to the host.  */
435                 EXTERN_C_BEGIN
436                 void SchedulerIdle(void);
437                 EXTERN_C_END
438                 #define CPU_IDLE SchedulerIdle()
439         #else /* !ARCH_EMUL */
440                 #define CPU_IDLE do { /* nothing */ } while (0)
441         #endif /* !ARCH_EMUL */
442 #endif /* !CPU_IDLE */
443
444 /* OBSOLETE */
445 #define SCHEDULER_IDLE CPU_IDLE
446
447 #endif /* DEVLIB_CPU_H */