source: rtems/cpukit/score/cpu/powerpc/rtems/score/cpu.h @ ae3e6af8

5
Last change on this file since ae3e6af8 was ae3e6af8, checked in by Sebastian Huber <sebastian.huber@…>, on 06/20/17 at 08:39:54

powerpc: Fix TLS support

Do not zero the GPR2 in the thread context via dcbz instructions. Bug
was introduced by 32b4a0c42704f0076da8e2d5411290f55d1b2965.

  • Property mode set to 100644
File size: 35.7 KB
Line 
1/**
2 * @file
3 *
4 * @brief PowerPC CPU Department Source
5 */
6
7/*
8 *  COPYRIGHT (c) 1989-2012.
9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  COPYRIGHT (c) 1995 i-cubed ltd.
12 *
13 *  To anyone who acknowledges that this file is provided "AS IS"
14 *  without any express or implied warranty:
15 *      permission to use, copy, modify, and distribute this file
16 *      for any purpose is hereby granted without fee, provided that
17 *      the above copyright notice and this notice appears in all
18 *      copies, and that the name of i-cubed limited not be used in
19 *      advertising or publicity pertaining to distribution of the
20 *      software without specific, written prior permission.
21 *      i-cubed limited makes no representations about the suitability
22 *      of this software for any purpose.
23 *
24 *  Copyright (c) 2001 Andy Dachs <a.dachs@sstl.co.uk>.
25 *
26 *  Copyright (c) 2001 Surrey Satellite Technology Limited (SSTL).
27 *
28 *  Copyright (c) 2010, 2016 embedded brains GmbH.
29 *
30 *  The license and distribution terms for this file may be
31 *  found in the file LICENSE in this distribution or at
32 *  http://www.rtems.org/license/LICENSE.
33 */
34
35#ifndef _RTEMS_SCORE_CPU_H
36#define _RTEMS_SCORE_CPU_H
37
38#include <rtems/score/types.h>
39#include <rtems/score/powerpc.h>
40#include <rtems/powerpc/registers.h>
41
42#ifndef ASM
43  #include <string.h> /* for memset() */
44#endif
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50/* conditional compilation parameters */
51
52/*
53 *  Does this port provide a CPU dependent IDLE task implementation?
54 *
55 *  If TRUE, then the routine _CPU_Thread_Idle_body
56 *  must be provided and is the default IDLE thread body instead of
57 *  _CPU_Thread_Idle_body.
58 *
59 *  If FALSE, then use the generic IDLE thread body if the BSP does
60 *  not provide one.
61 *
62 *  This is intended to allow for supporting processors which have
63 *  a low power or idle mode.  When the IDLE thread is executed, then
64 *  the CPU can be powered down.
65 *
66 *  The order of precedence for selecting the IDLE thread body is:
67 *
68 *    1.  BSP provided
69 *    2.  CPU dependent (if provided)
70 *    3.  generic (if no BSP and no CPU dependent)
71 */
72
73#define CPU_PROVIDES_IDLE_THREAD_BODY    FALSE
74
75/*
76 *  Does the stack grow up (toward higher addresses) or down
77 *  (toward lower addresses)?
78 *
79 *  If TRUE, then the grows upward.
80 *  If FALSE, then the grows toward smaller addresses.
81 */
82
83#define CPU_STACK_GROWS_UP               FALSE
84
85#define CPU_CACHE_LINE_BYTES PPC_STRUCTURE_ALIGNMENT
86
87#define CPU_STRUCTURE_ALIGNMENT RTEMS_ALIGNED( CPU_CACHE_LINE_BYTES )
88
89/*
90 *  Does the CPU have hardware floating point?
91 *
92 *  If TRUE, then the RTEMS_FLOATING_POINT task attribute is supported.
93 *  If FALSE, then the RTEMS_FLOATING_POINT task attribute is ignored.
94 *
95 *  If there is a FP coprocessor such as the i387 or mc68881, then
96 *  the answer is TRUE.
97 *
98 *  The macro name "PPC_HAS_FPU" should be made CPU specific.
99 *  It indicates whether or not this CPU model has FP support.  For
100 *  example, it would be possible to have an i386_nofp CPU model
101 *  which set this to false to indicate that you have an i386 without
102 *  an i387 and wish to leave floating point support out of RTEMS.
103 */
104
105#if ( PPC_HAS_FPU == 1 )
106#define CPU_HARDWARE_FP     TRUE
107#define CPU_SOFTWARE_FP     FALSE
108#else
109#define CPU_HARDWARE_FP     FALSE
110#define CPU_SOFTWARE_FP     FALSE
111#endif
112
113/*
114 *  Are all tasks RTEMS_FLOATING_POINT tasks implicitly?
115 *
116 *  If TRUE, then the RTEMS_FLOATING_POINT task attribute is assumed.
117 *  If FALSE, then the RTEMS_FLOATING_POINT task attribute is followed.
118 *
119 *  If CPU_HARDWARE_FP is FALSE, then this should be FALSE as well.
120 *
121 *  PowerPC Note: It appears the GCC can implicitly generate FPU
122 *  and Altivec instructions when you least expect them.  So make
123 *  all tasks floating point.
124 */
125
126#define CPU_ALL_TASKS_ARE_FP CPU_HARDWARE_FP
127
128/*
129 *  Should the IDLE task have a floating point context?
130 *
131 *  If TRUE, then the IDLE task is created as a RTEMS_FLOATING_POINT task
132 *  and it has a floating point context which is switched in and out.
133 *  If FALSE, then the IDLE task does not have a floating point context.
134 *
135 *  Setting this to TRUE negatively impacts the time required to preempt
136 *  the IDLE task from an interrupt because the floating point context
137 *  must be saved as part of the preemption.
138 */
139
140#define CPU_IDLE_TASK_IS_FP      FALSE
141
142#define CPU_MAXIMUM_PROCESSORS 32
143
144/*
145 *  Processor defined structures required for cpukit/score.
146 */
147
148/*
149 * Contexts
150 *
151 *  Generally there are 2 types of context to save.
152 *     1. Interrupt registers to save
153 *     2. Task level registers to save
154 *
155 *  This means we have the following 3 context items:
156 *     1. task level context stuff::  Context_Control
157 *     2. floating point task stuff:: Context_Control_fp
158 *     3. special interrupt level context :: Context_Control_interrupt
159 *
160 *  On some processors, it is cost-effective to save only the callee
161 *  preserved registers during a task context switch.  This means
162 *  that the ISR code needs to save those registers which do not
163 *  persist across function calls.  It is not mandatory to make this
164 *  distinctions between the caller/callee saves registers for the
165 *  purpose of minimizing context saved during task switch and on interrupts.
166 *  If the cost of saving extra registers is minimal, simplicity is the
167 *  choice.  Save the same context on interrupt entry as for tasks in
168 *  this case.
169 *
170 *  Additionally, if gdb is to be made aware of RTEMS tasks for this CPU, then
171 *  care should be used in designing the context area.
172 *
173 *  On some CPUs with hardware floating point support, the Context_Control_fp
174 *  structure will not be used or it simply consist of an array of a
175 *  fixed number of bytes.   This is done when the floating point context
176 *  is dumped by a "FP save context" type instruction and the format
177 *  is not really defined by the CPU.  In this case, there is no need
178 *  to figure out the exact format -- only the size.  Of course, although
179 *  this is enough information for RTEMS, it is probably not enough for
180 *  a debugger such as gdb.  But that is another problem.
181 */
182
183#ifndef __SPE__
184  #define PPC_GPR_TYPE uint32_t
185  #define PPC_GPR_SIZE 4
186  #define PPC_GPR_LOAD lwz
187  #define PPC_GPR_STORE stw
188#else
189  #define PPC_GPR_TYPE uint64_t
190  #define PPC_GPR_SIZE 8
191  #define PPC_GPR_LOAD evldd
192  #define PPC_GPR_STORE evstdd
193#endif
194
195#ifndef ASM
196
197/*
198 * Non-volatile context according to E500ABIUG, EABI and 32-bit TLS (according
199 * to "Power Architecture 32-bit Application Binary Interface Supplement 1.0 -
200 * Linux and Embedded")
201 */
202typedef struct {
203  uint32_t gpr1;
204  uint32_t msr;
205  uint32_t lr;
206  uint32_t cr;
207  PPC_GPR_TYPE gpr14;
208  PPC_GPR_TYPE gpr15;
209  PPC_GPR_TYPE gpr16;
210  PPC_GPR_TYPE gpr17;
211  PPC_GPR_TYPE gpr18;
212  PPC_GPR_TYPE gpr19;
213  PPC_GPR_TYPE gpr20;
214  PPC_GPR_TYPE gpr21;
215  PPC_GPR_TYPE gpr22;
216  PPC_GPR_TYPE gpr23;
217  PPC_GPR_TYPE gpr24;
218  PPC_GPR_TYPE gpr25;
219  PPC_GPR_TYPE gpr26;
220  PPC_GPR_TYPE gpr27;
221  PPC_GPR_TYPE gpr28;
222  PPC_GPR_TYPE gpr29;
223  PPC_GPR_TYPE gpr30;
224  PPC_GPR_TYPE gpr31;
225  uint32_t isr_dispatch_disable;
226  uint32_t reserved_for_alignment;
227  #if defined(PPC_MULTILIB_ALTIVEC)
228    uint8_t v20[16];
229    uint8_t v21[16];
230    uint8_t v22[16];
231    uint8_t v23[16];
232    uint8_t v24[16];
233    uint8_t v25[16];
234    uint8_t v26[16];
235    uint8_t v27[16];
236    uint8_t v28[16];
237    uint8_t v29[16];
238    uint8_t v30[16];
239    uint8_t v31[16];
240    uint32_t vrsave;
241  #elif defined(__ALTIVEC__)
242    /*
243     * 12 non-volatile vector registers, cache-aligned area for vscr/vrsave
244     * and padding to ensure cache-alignment.  Unfortunately, we can't verify
245     * the cache line size here in the cpukit but altivec support code will
246     * produce an error if this is ever different from 32 bytes.
247     *
248     * Note: it is the BSP/CPU-support's responsibility to save/restore
249     *       volatile vregs across interrupts and exceptions.
250     */
251    uint8_t altivec[16*12 + 32 + PPC_DEFAULT_CACHE_LINE_SIZE];
252  #endif
253  #if defined(PPC_MULTILIB_FPU)
254    double f14;
255    double f15;
256    double f16;
257    double f17;
258    double f18;
259    double f19;
260    double f20;
261    double f21;
262    double f22;
263    double f23;
264    double f24;
265    double f25;
266    double f26;
267    double f27;
268    double f28;
269    double f29;
270    double f30;
271    double f31;
272  #endif
273  /*
274   * The following items are at the structure end, so that we can use dcbz for
275   * the previous items to optimize the context switch.  We must not set the
276   * following items to zero via the dcbz.
277   */
278  uint32_t gpr2;
279  #if defined(RTEMS_SMP)
280    volatile uint32_t is_executing;
281  #endif
282} ppc_context;
283
284typedef struct {
285  uint8_t context [
286    PPC_DEFAULT_CACHE_LINE_SIZE
287      + sizeof(ppc_context)
288      + (sizeof(ppc_context) % PPC_DEFAULT_CACHE_LINE_SIZE == 0
289        ? 0
290          : PPC_DEFAULT_CACHE_LINE_SIZE
291            - sizeof(ppc_context) % PPC_DEFAULT_CACHE_LINE_SIZE)
292  ];
293} Context_Control;
294
295static inline ppc_context *ppc_get_context( const Context_Control *context )
296{
297  uintptr_t clsz = PPC_DEFAULT_CACHE_LINE_SIZE;
298  uintptr_t mask = clsz - 1;
299  uintptr_t addr = (uintptr_t) context;
300
301  return (ppc_context *) ((addr & ~mask) + clsz);
302}
303
304#define _CPU_Context_Get_SP( _context ) \
305  ppc_get_context(_context)->gpr1
306
307#ifdef RTEMS_SMP
308  static inline bool _CPU_Context_Get_is_executing(
309    const Context_Control *context
310  )
311  {
312    return ppc_get_context(context)->is_executing;
313  }
314
315  static inline void _CPU_Context_Set_is_executing(
316    Context_Control *context,
317    bool is_executing
318  )
319  {
320    ppc_get_context(context)->is_executing = is_executing;
321  }
322#endif
323#endif /* ASM */
324
325#define PPC_CONTEXT_OFFSET_GPR1 (PPC_DEFAULT_CACHE_LINE_SIZE + 0)
326#define PPC_CONTEXT_OFFSET_MSR (PPC_DEFAULT_CACHE_LINE_SIZE + 4)
327#define PPC_CONTEXT_OFFSET_LR (PPC_DEFAULT_CACHE_LINE_SIZE + 8)
328#define PPC_CONTEXT_OFFSET_CR (PPC_DEFAULT_CACHE_LINE_SIZE + 12)
329
330#define PPC_CONTEXT_GPR_OFFSET( gpr ) \
331  (((gpr) - 14) * PPC_GPR_SIZE + PPC_DEFAULT_CACHE_LINE_SIZE + 16)
332
333#define PPC_CONTEXT_OFFSET_GPR14 PPC_CONTEXT_GPR_OFFSET( 14 )
334#define PPC_CONTEXT_OFFSET_GPR15 PPC_CONTEXT_GPR_OFFSET( 15 )
335#define PPC_CONTEXT_OFFSET_GPR16 PPC_CONTEXT_GPR_OFFSET( 16 )
336#define PPC_CONTEXT_OFFSET_GPR17 PPC_CONTEXT_GPR_OFFSET( 17 )
337#define PPC_CONTEXT_OFFSET_GPR18 PPC_CONTEXT_GPR_OFFSET( 18 )
338#define PPC_CONTEXT_OFFSET_GPR19 PPC_CONTEXT_GPR_OFFSET( 19 )
339#define PPC_CONTEXT_OFFSET_GPR20 PPC_CONTEXT_GPR_OFFSET( 20 )
340#define PPC_CONTEXT_OFFSET_GPR21 PPC_CONTEXT_GPR_OFFSET( 21 )
341#define PPC_CONTEXT_OFFSET_GPR22 PPC_CONTEXT_GPR_OFFSET( 22 )
342#define PPC_CONTEXT_OFFSET_GPR23 PPC_CONTEXT_GPR_OFFSET( 23 )
343#define PPC_CONTEXT_OFFSET_GPR24 PPC_CONTEXT_GPR_OFFSET( 24 )
344#define PPC_CONTEXT_OFFSET_GPR25 PPC_CONTEXT_GPR_OFFSET( 25 )
345#define PPC_CONTEXT_OFFSET_GPR26 PPC_CONTEXT_GPR_OFFSET( 26 )
346#define PPC_CONTEXT_OFFSET_GPR27 PPC_CONTEXT_GPR_OFFSET( 27 )
347#define PPC_CONTEXT_OFFSET_GPR28 PPC_CONTEXT_GPR_OFFSET( 28 )
348#define PPC_CONTEXT_OFFSET_GPR29 PPC_CONTEXT_GPR_OFFSET( 29 )
349#define PPC_CONTEXT_OFFSET_GPR30 PPC_CONTEXT_GPR_OFFSET( 30 )
350#define PPC_CONTEXT_OFFSET_GPR31 PPC_CONTEXT_GPR_OFFSET( 31 )
351#define PPC_CONTEXT_OFFSET_ISR_DISPATCH_DISABLE PPC_CONTEXT_GPR_OFFSET( 32 )
352
353#ifdef PPC_MULTILIB_ALTIVEC
354  #define PPC_CONTEXT_OFFSET_V( v ) \
355    ( ( ( v ) - 20 ) * 16 + PPC_DEFAULT_CACHE_LINE_SIZE + 96 )
356  #define PPC_CONTEXT_OFFSET_V20 PPC_CONTEXT_OFFSET_V( 20 )
357  #define PPC_CONTEXT_OFFSET_V21 PPC_CONTEXT_OFFSET_V( 21 )
358  #define PPC_CONTEXT_OFFSET_V22 PPC_CONTEXT_OFFSET_V( 22 )
359  #define PPC_CONTEXT_OFFSET_V23 PPC_CONTEXT_OFFSET_V( 23 )
360  #define PPC_CONTEXT_OFFSET_V24 PPC_CONTEXT_OFFSET_V( 24 )
361  #define PPC_CONTEXT_OFFSET_V25 PPC_CONTEXT_OFFSET_V( 25 )
362  #define PPC_CONTEXT_OFFSET_V26 PPC_CONTEXT_OFFSET_V( 26 )
363  #define PPC_CONTEXT_OFFSET_V27 PPC_CONTEXT_OFFSET_V( 27 )
364  #define PPC_CONTEXT_OFFSET_V28 PPC_CONTEXT_OFFSET_V( 28 )
365  #define PPC_CONTEXT_OFFSET_V29 PPC_CONTEXT_OFFSET_V( 29 )
366  #define PPC_CONTEXT_OFFSET_V30 PPC_CONTEXT_OFFSET_V( 30 )
367  #define PPC_CONTEXT_OFFSET_V31 PPC_CONTEXT_OFFSET_V( 31 )
368  #define PPC_CONTEXT_OFFSET_VRSAVE PPC_CONTEXT_OFFSET_V( 32 )
369  #define PPC_CONTEXT_OFFSET_F( f ) \
370    ( ( ( f ) - 14 ) * 8 + PPC_DEFAULT_CACHE_LINE_SIZE + 296 )
371#else
372  #define PPC_CONTEXT_OFFSET_F( f ) \
373    ( ( ( f ) - 14 ) * 8 + PPC_DEFAULT_CACHE_LINE_SIZE + 96 )
374#endif
375
376#ifdef PPC_MULTILIB_FPU
377  #define PPC_CONTEXT_OFFSET_F14 PPC_CONTEXT_OFFSET_F( 14 )
378  #define PPC_CONTEXT_OFFSET_F15 PPC_CONTEXT_OFFSET_F( 15 )
379  #define PPC_CONTEXT_OFFSET_F16 PPC_CONTEXT_OFFSET_F( 16 )
380  #define PPC_CONTEXT_OFFSET_F17 PPC_CONTEXT_OFFSET_F( 17 )
381  #define PPC_CONTEXT_OFFSET_F18 PPC_CONTEXT_OFFSET_F( 18 )
382  #define PPC_CONTEXT_OFFSET_F19 PPC_CONTEXT_OFFSET_F( 19 )
383  #define PPC_CONTEXT_OFFSET_F20 PPC_CONTEXT_OFFSET_F( 20 )
384  #define PPC_CONTEXT_OFFSET_F21 PPC_CONTEXT_OFFSET_F( 21 )
385  #define PPC_CONTEXT_OFFSET_F22 PPC_CONTEXT_OFFSET_F( 22 )
386  #define PPC_CONTEXT_OFFSET_F23 PPC_CONTEXT_OFFSET_F( 23 )
387  #define PPC_CONTEXT_OFFSET_F24 PPC_CONTEXT_OFFSET_F( 24 )
388  #define PPC_CONTEXT_OFFSET_F25 PPC_CONTEXT_OFFSET_F( 25 )
389  #define PPC_CONTEXT_OFFSET_F26 PPC_CONTEXT_OFFSET_F( 26 )
390  #define PPC_CONTEXT_OFFSET_F27 PPC_CONTEXT_OFFSET_F( 27 )
391  #define PPC_CONTEXT_OFFSET_F28 PPC_CONTEXT_OFFSET_F( 28 )
392  #define PPC_CONTEXT_OFFSET_F29 PPC_CONTEXT_OFFSET_F( 29 )
393  #define PPC_CONTEXT_OFFSET_F30 PPC_CONTEXT_OFFSET_F( 30 )
394  #define PPC_CONTEXT_OFFSET_F31 PPC_CONTEXT_OFFSET_F( 31 )
395#endif
396
397#if defined(PPC_MULTILIB_FPU)
398  #define PPC_CONTEXT_VOLATILE_SIZE PPC_CONTEXT_OFFSET_F( 32 )
399#elif defined(PPC_MULTILIB_ALTIVEC)
400  #define PPC_CONTEXT_VOLATILE_SIZE (PPC_CONTEXT_OFFSET_VRSAVE + 4)
401#else
402  #define PPC_CONTEXT_VOLATILE_SIZE (PPC_CONTEXT_GPR_OFFSET( 32 ) + 8)
403#endif
404
405#define PPC_CONTEXT_OFFSET_GPR2 PPC_CONTEXT_VOLATILE_SIZE
406
407#ifdef RTEMS_SMP
408  #define PPC_CONTEXT_OFFSET_IS_EXECUTING (PPC_CONTEXT_VOLATILE_SIZE + 4)
409#endif
410
411#ifndef ASM
412typedef struct {
413#if (PPC_HAS_FPU == 1)
414    /* The ABIs (PowerOpen/SVR4/EABI) only require saving f14-f31 over
415     * procedure calls.  However, this would mean that the interrupt
416     * frame had to hold f0-f13, and the fpscr.  And as the majority
417     * of tasks will not have an FP context, we will save the whole
418     * context here.
419     */
420#if (PPC_HAS_DOUBLE == 1)
421    double      f[32];
422    uint64_t    fpscr;
423#else
424    float       f[32];
425    uint32_t    fpscr;
426#endif
427#endif /* (PPC_HAS_FPU == 1) */
428} Context_Control_fp;
429
430#endif /* ASM */
431
432/*
433 *  Does the CPU follow the simple vectored interrupt model?
434 *
435 *  If TRUE, then RTEMS allocates the vector table it internally manages.
436 *  If FALSE, then the BSP is assumed to allocate and manage the vector
437 *  table
438 *
439 *  PowerPC Specific Information:
440 *
441 *  The PowerPC and x86 were the first to use the PIC interrupt model.
442 *  They do not use the simple vectored interrupt model.
443 */
444#define CPU_SIMPLE_VECTORED_INTERRUPTS FALSE
445
446/*
447 *  Does RTEMS manage a dedicated interrupt stack in software?
448 *
449 *  If TRUE, then a stack is allocated in _ISR_Handler_initialization.
450 *  If FALSE, nothing is done.
451 *
452 *  If the CPU supports a dedicated interrupt stack in hardware,
453 *  then it is generally the responsibility of the BSP to allocate it
454 *  and set it up.
455 *
456 *  If the CPU does not support a dedicated interrupt stack, then
457 *  the porter has two options: (1) execute interrupts on the
458 *  stack of the interrupted task, and (2) have RTEMS manage a dedicated
459 *  interrupt stack.
460 *
461 *  If this is TRUE, CPU_ALLOCATE_INTERRUPT_STACK should also be TRUE.
462 *
463 *  Only one of CPU_HAS_SOFTWARE_INTERRUPT_STACK and
464 *  CPU_HAS_HARDWARE_INTERRUPT_STACK should be set to TRUE.  It is
465 *  possible that both are FALSE for a particular CPU.  Although it
466 *  is unclear what that would imply about the interrupt processing
467 *  procedure on that CPU.
468 */
469
470#define CPU_HAS_SOFTWARE_INTERRUPT_STACK TRUE
471
472/*
473 *  Does this CPU have hardware support for a dedicated interrupt stack?
474 *
475 *  If TRUE, then it must be installed during initialization.
476 *  If FALSE, then no installation is performed.
477 *
478 *  If this is TRUE, CPU_ALLOCATE_INTERRUPT_STACK should also be TRUE.
479 *
480 *  Only one of CPU_HAS_SOFTWARE_INTERRUPT_STACK and
481 *  CPU_HAS_HARDWARE_INTERRUPT_STACK should be set to TRUE.  It is
482 *  possible that both are FALSE for a particular CPU.  Although it
483 *  is unclear what that would imply about the interrupt processing
484 *  procedure on that CPU.
485 */
486
487#define CPU_HAS_HARDWARE_INTERRUPT_STACK FALSE
488
489/*
490 *  Does RTEMS allocate a dedicated interrupt stack in the Interrupt Manager?
491 *
492 *  If TRUE, then the memory is allocated during initialization.
493 *  If FALSE, then the memory is allocated during initialization.
494 *
495 *  This should be TRUE is CPU_HAS_SOFTWARE_INTERRUPT_STACK is TRUE.
496 */
497
498#define CPU_ALLOCATE_INTERRUPT_STACK TRUE
499
500/*
501 *  Does the RTEMS invoke the user's ISR with the vector number and
502 *  a pointer to the saved interrupt frame (1) or just the vector
503 *  number (0)?
504 */
505
506#define CPU_ISR_PASSES_FRAME_POINTER FALSE
507
508/*
509 *  Should the saving of the floating point registers be deferred
510 *  until a context switch is made to another different floating point
511 *  task?
512 *
513 *  If TRUE, then the floating point context will not be stored until
514 *  necessary.  It will remain in the floating point registers and not
515 *  disturned until another floating point task is switched to.
516 *
517 *  If FALSE, then the floating point context is saved when a floating
518 *  point task is switched out and restored when the next floating point
519 *  task is restored.  The state of the floating point registers between
520 *  those two operations is not specified.
521 *
522 *  If the floating point context does NOT have to be saved as part of
523 *  interrupt dispatching, then it should be safe to set this to TRUE.
524 *
525 *  Setting this flag to TRUE results in using a different algorithm
526 *  for deciding when to save and restore the floating point context.
527 *  The deferred FP switch algorithm minimizes the number of times
528 *  the FP context is saved and restored.  The FP context is not saved
529 *  until a context switch is made to another, different FP task.
530 *  Thus in a system with only one FP task, the FP context will never
531 *  be saved or restored.
532 *
533 *  Note, however that compilers may use floating point registers/
534 *  instructions for optimization or they may save/restore FP registers
535 *  on the stack. You must not use deferred switching in these cases
536 *  and on the PowerPC attempting to do so will raise a "FP unavailable"
537 *  exception.
538 */
539/*
540 *  ACB Note:  This could make debugging tricky..
541 */
542
543/* conservative setting (FALSE); probably doesn't affect performance too much */
544#define CPU_USE_DEFERRED_FP_SWITCH       FALSE
545
546#define CPU_ENABLE_ROBUST_THREAD_DISPATCH FALSE
547
548/*
549 *  Processor defined structures required for cpukit/score.
550 */
551
552#ifndef ASM
553
554/*
555 *  This variable is optional.  It is used on CPUs on which it is difficult
556 *  to generate an "uninitialized" FP context.  It is filled in by
557 *  _CPU_Initialize and copied into the task's FP context area during
558 *  _CPU_Context_Initialize.
559 */
560
561/* EXTERN Context_Control_fp  _CPU_Null_fp_context; */
562
563#endif /* ndef ASM */
564
565/*
566 *  This defines the number of levels and the mask used to pick those
567 *  bits out of a thread mode.
568 */
569
570#define CPU_MODES_INTERRUPT_LEVEL  0x00000001 /* interrupt level in mode */
571#define CPU_MODES_INTERRUPT_MASK   0x00000001 /* interrupt level in mode */
572
573/*
574 *  The size of the floating point context area.  On some CPUs this
575 *  will not be a "sizeof" because the format of the floating point
576 *  area is not defined -- only the size is.  This is usually on
577 *  CPUs with a "floating point save context" instruction.
578 */
579
580#define CPU_CONTEXT_FP_SIZE sizeof( Context_Control_fp )
581
582/*
583 * (Optional) # of bytes for libmisc/stackchk to check
584 * If not specifed, then it defaults to something reasonable
585 * for most architectures.
586 */
587
588#define CPU_STACK_CHECK_PATTERN_INITIALIZER \
589  { 0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
590    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
591    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
592    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
593    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
594    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
595    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06, \
596    0xFEEDF00D, 0x0BAD0D06, 0xDEADF00D, 0x600D0D06 }
597
598/*
599 *  Amount of extra stack (above minimum stack size) required by
600 *  MPCI receive server thread.  Remember that in a multiprocessor
601 *  system this thread must exist and be able to process all directives.
602 */
603
604#define CPU_MPCI_RECEIVE_SERVER_EXTRA_STACK 0
605
606/*
607 *  This is defined if the port has a special way to report the ISR nesting
608 *  level.  Most ports maintain the variable _ISR_Nest_level. Note that
609 *  this is not an option - RTEMS/score _relies_ on _ISR_Nest_level
610 *  being maintained (e.g. watchdog queues).
611 */
612
613#define CPU_PROVIDES_ISR_IS_IN_PROGRESS FALSE
614
615/*
616 *  ISR handler macros
617 */
618
619/*
620 *  Disable all interrupts for an RTEMS critical section.  The previous
621 *  level is returned in _isr_cookie.
622 */
623
624#ifndef ASM
625
626RTEMS_INLINE_ROUTINE bool _CPU_ISR_Is_enabled( uint32_t level )
627{
628  return ( level & MSR_EE ) != 0;
629}
630
631static inline uint32_t   _CPU_ISR_Get_level( void )
632{
633  register unsigned int msr;
634  _CPU_MSR_GET(msr);
635  if (msr & MSR_EE) return 0;
636  else  return 1;
637}
638
639static inline void _CPU_ISR_Set_level( uint32_t   level )
640{
641  register unsigned int msr;
642  _CPU_MSR_GET(msr);
643  if (!(level & CPU_MODES_INTERRUPT_MASK)) {
644    msr |= ppc_interrupt_get_disable_mask();
645  }
646  else {
647    msr &= ~ppc_interrupt_get_disable_mask();
648  }
649  _CPU_MSR_SET(msr);
650}
651
652void BSP_panic(char *);
653
654/* Fatal Error manager macros */
655
656/*
657 *  This routine copies _error into a known place -- typically a stack
658 *  location or a register, optionally disables interrupts, and
659 *  halts/stops the CPU.
660 */
661
662void _BSP_Fatal_error(unsigned int);
663
664#endif /* ASM */
665
666#define _CPU_Fatal_halt( _source, _error ) \
667  _BSP_Fatal_error(_error)
668
669/* end of Fatal Error manager macros */
670
671/*
672 *  Should be large enough to run all RTEMS tests.  This ensures
673 *  that a "reasonable" small application should not have any problems.
674 */
675
676#define CPU_STACK_MINIMUM_SIZE          (1024*8)
677
678#define CPU_SIZEOF_POINTER 4
679
680/*
681 *  CPU's worst alignment requirement for data types on a byte boundary.  This
682 *  alignment does not take into account the requirements for the stack.
683 */
684
685#define CPU_ALIGNMENT              (PPC_ALIGNMENT)
686
687/*
688 *  This number corresponds to the byte alignment requirement for the
689 *  heap handler.  This alignment requirement may be stricter than that
690 *  for the data types alignment specified by CPU_ALIGNMENT.  It is
691 *  common for the heap to follow the same alignment requirement as
692 *  CPU_ALIGNMENT.  If the CPU_ALIGNMENT is strict enough for the heap,
693 *  then this should be set to CPU_ALIGNMENT.
694 *
695 *  NOTE:  This does not have to be a power of 2.  It does have to
696 *         be greater or equal to than CPU_ALIGNMENT.
697 */
698
699#define CPU_HEAP_ALIGNMENT         (PPC_ALIGNMENT)
700
701/*
702 *  This number corresponds to the byte alignment requirement for memory
703 *  buffers allocated by the partition manager.  This alignment requirement
704 *  may be stricter than that for the data types alignment specified by
705 *  CPU_ALIGNMENT.  It is common for the partition to follow the same
706 *  alignment requirement as CPU_ALIGNMENT.  If the CPU_ALIGNMENT is strict
707 *  enough for the partition, then this should be set to CPU_ALIGNMENT.
708 *
709 *  NOTE:  This does not have to be a power of 2.  It does have to
710 *         be greater or equal to than CPU_ALIGNMENT.
711 */
712
713#define CPU_PARTITION_ALIGNMENT    (PPC_ALIGNMENT)
714
715/*
716 *  This number corresponds to the byte alignment requirement for the
717 *  stack.  This alignment requirement may be stricter than that for the
718 *  data types alignment specified by CPU_ALIGNMENT.  If the CPU_ALIGNMENT
719 *  is strict enough for the stack, then this should be set to 0.
720 *
721 *  NOTE:  This must be a power of 2 either 0 or greater than CPU_ALIGNMENT.
722 */
723
724#define CPU_STACK_ALIGNMENT        (PPC_STACK_ALIGNMENT)
725
726#ifndef ASM
727/*  The following routine swaps the endian format of an unsigned int.
728 *  It must be static because it is referenced indirectly.
729 *
730 *  This version will work on any processor, but if there is a better
731 *  way for your CPU PLEASE use it.  The most common way to do this is to:
732 *
733 *     swap least significant two bytes with 16-bit rotate
734 *     swap upper and lower 16-bits
735 *     swap most significant two bytes with 16-bit rotate
736 *
737 *  Some CPUs have special instructions which swap a 32-bit quantity in
738 *  a single instruction (e.g. i486).  It is probably best to avoid
739 *  an "endian swapping control bit" in the CPU.  One good reason is
740 *  that interrupts would probably have to be disabled to ensure that
741 *  an interrupt does not try to access the same "chunk" with the wrong
742 *  endian.  Another good reason is that on some CPUs, the endian bit
743 *  endianness for ALL fetches -- both code and data -- so the code
744 *  will be fetched incorrectly.
745 */
746
747static inline uint32_t CPU_swap_u32(
748  uint32_t value
749)
750{
751  uint32_t   swapped;
752
753  __asm__ volatile("rlwimi %0,%1,8,24,31;"
754               "rlwimi %0,%1,24,16,23;"
755               "rlwimi %0,%1,8,8,15;"
756               "rlwimi %0,%1,24,0,7;" :
757               "=&r" ((swapped)) : "r" ((value)));
758
759  return( swapped );
760}
761
762#define CPU_swap_u16( value ) \
763  (((value&0xff) << 8) | ((value >> 8)&0xff))
764
765typedef uint32_t CPU_Counter_ticks;
766
767static inline CPU_Counter_ticks _CPU_Counter_read( void )
768{
769  CPU_Counter_ticks value;
770
771#if defined(__PPC_CPU_E6500__)
772  /* Use Alternate Time Base */
773  __asm__ volatile( "mfspr %0, 526" : "=r" (value) );
774#else
775  __asm__ volatile( "mfspr %0, 268" : "=r" (value) );
776#endif
777
778  return value;
779}
780
781static inline CPU_Counter_ticks _CPU_Counter_difference(
782  CPU_Counter_ticks second,
783  CPU_Counter_ticks first
784)
785{
786  return second - first;
787}
788
789#endif /* ASM */
790
791
792#ifndef ASM
793/* Context handler macros */
794
795/*
796 *  Initialize the context to a state suitable for starting a
797 *  task after a context restore operation.  Generally, this
798 *  involves:
799 *
800 *     - setting a starting address
801 *     - preparing the stack
802 *     - preparing the stack and frame pointers
803 *     - setting the proper interrupt level in the context
804 *     - initializing the floating point context
805 *
806 *  This routine generally does not set any unnecessary register
807 *  in the context.  The state of the "general data" registers is
808 *  undefined at task start time.
809 */
810
811void _CPU_Context_Initialize(
812  Context_Control  *the_context,
813  uint32_t         *stack_base,
814  uint32_t          size,
815  uint32_t          new_level,
816  void             *entry_point,
817  bool              is_fp,
818  void             *tls_area
819);
820
821/*
822 *  This routine is responsible for somehow restarting the currently
823 *  executing task.  If you are lucky, then all that is necessary
824 *  is restoring the context.  Otherwise, there will need to be
825 *  a special assembly routine which does something special in this
826 *  case.  Context_Restore should work most of the time.  It will
827 *  not work if restarting self conflicts with the stack frame
828 *  assumptions of restoring a context.
829 */
830
831#define _CPU_Context_Restart_self( _the_context ) \
832   _CPU_Context_restore( (_the_context) );
833
834/*
835 *  This routine initializes the FP context area passed to it to.
836 *  There are a few standard ways in which to initialize the
837 *  floating point context.  The code included for this macro assumes
838 *  that this is a CPU in which a "initial" FP context was saved into
839 *  _CPU_Null_fp_context and it simply copies it to the destination
840 *  context passed to it.
841 *
842 *  Other models include (1) not doing anything, and (2) putting
843 *  a "null FP status word" in the correct place in the FP context.
844 */
845
846#define _CPU_Context_Initialize_fp( _destination ) \
847  memset( *(_destination), 0, sizeof( **(_destination) ) )
848
849/* end of Context handler macros */
850#endif /* ASM */
851
852#ifndef ASM
853/* Bitfield handler macros */
854
855#define CPU_USE_GENERIC_BITFIELD_CODE FALSE
856
857/*
858 *  This routine sets _output to the bit number of the first bit
859 *  set in _value.  _value is of CPU dependent type Priority_bit_map_Word.
860 *  This type may be either 16 or 32 bits wide although only the 16
861 *  least significant bits will be used.
862 *
863 *  There are a number of variables in using a "find first bit" type
864 *  instruction.
865 *
866 *    (1) What happens when run on a value of zero?
867 *    (2) Bits may be numbered from MSB to LSB or vice-versa.
868 *    (3) The numbering may be zero or one based.
869 *    (4) The "find first bit" instruction may search from MSB or LSB.
870 *
871 *  RTEMS guarantees that (1) will never happen so it is not a concern.
872 *  (2),(3), (4) are handled by the macros _CPU_Priority_mask() and
873 *  _CPU_Priority_Bits_index().  These three form a set of routines
874 *  which must logically operate together.  Bits in the _value are
875 *  set and cleared based on masks built by _CPU_Priority_mask().
876 *  The basic major and minor values calculated by _Priority_Major()
877 *  and _Priority_Minor() are "massaged" by _CPU_Priority_Bits_index()
878 *  to properly range between the values returned by the "find first bit"
879 *  instruction.  This makes it possible for _Priority_Get_highest() to
880 *  calculate the major and directly index into the minor table.
881 *  This mapping is necessary to ensure that 0 (a high priority major/minor)
882 *  is the first bit found.
883 *
884 *  This entire "find first bit" and mapping process depends heavily
885 *  on the manner in which a priority is broken into a major and minor
886 *  components with the major being the 4 MSB of a priority and minor
887 *  the 4 LSB.  Thus (0 << 4) + 0 corresponds to priority 0 -- the highest
888 *  priority.  And (15 << 4) + 14 corresponds to priority 254 -- the next
889 *  to the lowest priority.
890 *
891 *  If your CPU does not have a "find first bit" instruction, then
892 *  there are ways to make do without it.  Here are a handful of ways
893 *  to implement this in software:
894 *
895 *    - a series of 16 bit test instructions
896 *    - a "binary search using if's"
897 *    - _number = 0
898 *      if _value > 0x00ff
899 *        _value >>=8
900 *        _number = 8;
901 *
902 *      if _value > 0x0000f
903 *        _value >=8
904 *        _number += 4
905 *
906 *      _number += bit_set_table[ _value ]
907 *
908 *    where bit_set_table[ 16 ] has values which indicate the first
909 *      bit set
910 */
911
912#define _CPU_Bitfield_Find_first_bit( _value, _output ) \
913  { \
914    __asm__ volatile ("cntlzw %0, %1" : "=r" ((_output)), "=r" ((_value)) : \
915                  "1" ((_value))); \
916    (_output) = (_output) - 16; \
917  }
918
919/* end of Bitfield handler macros */
920
921/*
922 *  This routine builds the mask which corresponds to the bit fields
923 *  as searched by _CPU_Bitfield_Find_first_bit().  See the discussion
924 *  for that routine.
925 */
926
927#define _CPU_Priority_Mask( _bit_number ) \
928  ( 0x8000u >> (_bit_number) )
929
930/*
931 *  This routine translates the bit numbers returned by
932 *  _CPU_Bitfield_Find_first_bit() into something suitable for use as
933 *  a major or minor component of a priority.  See the discussion
934 *  for that routine.
935 */
936
937#define _CPU_Priority_bits_index( _priority ) \
938  (_priority)
939
940/* end of Priority handler macros */
941#endif /* ASM */
942
943/* functions */
944
945#ifndef ASM
946
947/*
948 *  _CPU_Initialize
949 *
950 *  This routine performs CPU dependent initialization.
951 */
952
953void _CPU_Initialize(void);
954
955/*
956 *  _CPU_ISR_install_vector
957 *
958 *  This routine installs an interrupt vector.
959 */
960
961void _CPU_ISR_install_vector(
962  uint32_t    vector,
963  proc_ptr    new_handler,
964  proc_ptr   *old_handler
965);
966
967/*
968 *  _CPU_Context_switch
969 *
970 *  This routine switches from the run context to the heir context.
971 */
972
973void _CPU_Context_switch(
974  Context_Control  *run,
975  Context_Control  *heir
976);
977
978/*
979 *  _CPU_Context_restore
980 *
981 *  This routine is generallu used only to restart self in an
982 *  efficient manner.  It may simply be a label in _CPU_Context_switch.
983 *
984 *  NOTE: May be unnecessary to reload some registers.
985 */
986
987void _CPU_Context_restore(
988  Context_Control *new_context
989) RTEMS_NO_RETURN;
990
991/*
992 *  _CPU_Context_save_fp
993 *
994 *  This routine saves the floating point context passed to it.
995 */
996
997void _CPU_Context_save_fp(
998  Context_Control_fp **fp_context_ptr
999);
1000
1001/*
1002 *  _CPU_Context_restore_fp
1003 *
1004 *  This routine restores the floating point context passed to it.
1005 */
1006
1007void _CPU_Context_restore_fp(
1008  Context_Control_fp **fp_context_ptr
1009);
1010
1011void _CPU_Context_volatile_clobber( uintptr_t pattern );
1012
1013void _CPU_Context_validate( uintptr_t pattern );
1014
1015#ifdef RTEMS_SMP
1016  uint32_t _CPU_SMP_Initialize( void );
1017
1018  bool _CPU_SMP_Start_processor( uint32_t cpu_index );
1019
1020  void _CPU_SMP_Finalize_initialization( uint32_t cpu_count );
1021
1022  void _CPU_SMP_Prepare_start_multitasking( void );
1023
1024  static inline uint32_t _CPU_SMP_Get_current_processor( void )
1025  {
1026    uint32_t pir;
1027
1028    /* Use Book E Processor ID Register (PIR) */
1029    __asm__ volatile (
1030      "mfspr %[pir], 286"
1031      : [pir] "=&r" (pir)
1032    );
1033
1034    return pir;
1035  }
1036
1037  void _CPU_SMP_Send_interrupt( uint32_t target_processor_index );
1038
1039  static inline void _CPU_SMP_Processor_event_broadcast( void )
1040  {
1041    __asm__ volatile ( "" : : : "memory" );
1042  }
1043
1044  static inline void _CPU_SMP_Processor_event_receive( void )
1045  {
1046    __asm__ volatile ( "" : : : "memory" );
1047  }
1048#endif
1049
1050typedef struct {
1051  uint32_t EXC_SRR0;
1052  uint32_t EXC_SRR1;
1053  uint32_t _EXC_number;
1054  uint32_t EXC_CR;
1055  uint32_t EXC_CTR;
1056  uint32_t EXC_XER;
1057  uint32_t EXC_LR;
1058  #ifdef __SPE__
1059    uint32_t EXC_SPEFSCR;
1060    uint64_t EXC_ACC;
1061  #endif
1062  PPC_GPR_TYPE GPR0;
1063  PPC_GPR_TYPE GPR1;
1064  PPC_GPR_TYPE GPR2;
1065  PPC_GPR_TYPE GPR3;
1066  PPC_GPR_TYPE GPR4;
1067  PPC_GPR_TYPE GPR5;
1068  PPC_GPR_TYPE GPR6;
1069  PPC_GPR_TYPE GPR7;
1070  PPC_GPR_TYPE GPR8;
1071  PPC_GPR_TYPE GPR9;
1072  PPC_GPR_TYPE GPR10;
1073  PPC_GPR_TYPE GPR11;
1074  PPC_GPR_TYPE GPR12;
1075  PPC_GPR_TYPE GPR13;
1076  PPC_GPR_TYPE GPR14;
1077  PPC_GPR_TYPE GPR15;
1078  PPC_GPR_TYPE GPR16;
1079  PPC_GPR_TYPE GPR17;
1080  PPC_GPR_TYPE GPR18;
1081  PPC_GPR_TYPE GPR19;
1082  PPC_GPR_TYPE GPR20;
1083  PPC_GPR_TYPE GPR21;
1084  PPC_GPR_TYPE GPR22;
1085  PPC_GPR_TYPE GPR23;
1086  PPC_GPR_TYPE GPR24;
1087  PPC_GPR_TYPE GPR25;
1088  PPC_GPR_TYPE GPR26;
1089  PPC_GPR_TYPE GPR27;
1090  PPC_GPR_TYPE GPR28;
1091  PPC_GPR_TYPE GPR29;
1092  PPC_GPR_TYPE GPR30;
1093  PPC_GPR_TYPE GPR31;
1094  #if defined(PPC_MULTILIB_ALTIVEC) || defined(PPC_MULTILIB_FPU)
1095    uint32_t reserved_for_alignment;
1096  #endif
1097  #ifdef PPC_MULTILIB_ALTIVEC
1098    uint32_t VRSAVE;
1099
1100    /* This field must take stvewx/lvewx requirements into account */
1101    uint32_t VSCR;
1102
1103    uint8_t V0[16];
1104    uint8_t V1[16];
1105    uint8_t V2[16];
1106    uint8_t V3[16];
1107    uint8_t V4[16];
1108    uint8_t V5[16];
1109    uint8_t V6[16];
1110    uint8_t V7[16];
1111    uint8_t V8[16];
1112    uint8_t V9[16];
1113    uint8_t V10[16];
1114    uint8_t V11[16];
1115    uint8_t V12[16];
1116    uint8_t V13[16];
1117    uint8_t V14[16];
1118    uint8_t V15[16];
1119    uint8_t V16[16];
1120    uint8_t V17[16];
1121    uint8_t V18[16];
1122    uint8_t V19[16];
1123    uint8_t V20[16];
1124    uint8_t V21[16];
1125    uint8_t V22[16];
1126    uint8_t V23[16];
1127    uint8_t V24[16];
1128    uint8_t V25[16];
1129    uint8_t V26[16];
1130    uint8_t V27[16];
1131    uint8_t V28[16];
1132    uint8_t V29[16];
1133    uint8_t V30[16];
1134    uint8_t V31[16];
1135  #endif
1136  #ifdef PPC_MULTILIB_FPU
1137    double F0;
1138    double F1;
1139    double F2;
1140    double F3;
1141    double F4;
1142    double F5;
1143    double F6;
1144    double F7;
1145    double F8;
1146    double F9;
1147    double F10;
1148    double F11;
1149    double F12;
1150    double F13;
1151    double F14;
1152    double F15;
1153    double F16;
1154    double F17;
1155    double F18;
1156    double F19;
1157    double F20;
1158    double F21;
1159    double F22;
1160    double F23;
1161    double F24;
1162    double F25;
1163    double F26;
1164    double F27;
1165    double F28;
1166    double F29;
1167    double F30;
1168    double F31;
1169    uint64_t FPSCR;
1170  #endif
1171} CPU_Exception_frame;
1172
1173void _CPU_Exception_frame_print( const CPU_Exception_frame *frame );
1174
1175/*
1176 * _CPU_Initialize_altivec()
1177 *
1178 * Global altivec-related initialization.
1179 */
1180void
1181_CPU_Initialize_altivec(void);
1182
1183/*
1184 * _CPU_Context_switch_altivec
1185 *
1186 * This routine switches the altivec contexts passed to it.
1187 */
1188
1189void
1190_CPU_Context_switch_altivec(
1191  ppc_context *from,
1192  ppc_context *to
1193);
1194
1195/*
1196 * _CPU_Context_restore_altivec
1197 *
1198 * This routine restores the altivec context passed to it.
1199 */
1200
1201void
1202_CPU_Context_restore_altivec(
1203  ppc_context *ctxt
1204);
1205
1206/*
1207 * _CPU_Context_initialize_altivec
1208 *
1209 * This routine initializes the altivec context passed to it.
1210 */
1211
1212void
1213_CPU_Context_initialize_altivec(
1214  ppc_context *ctxt
1215);
1216
1217void _CPU_Fatal_error(
1218  uint32_t   _error
1219);
1220
1221#endif /* ASM */
1222
1223#ifdef __cplusplus
1224}
1225#endif
1226
1227#endif /* _RTEMS_SCORE_CPU_H */
Note: See TracBrowser for help on using the repository browser.