source: rtems/cpukit/score/cpu/no_cpu/include/rtems/score/cpu.h @ 0a1f5df9

5
Last change on this file since 0a1f5df9 was 0a1f5df9, checked in by Sebastian Huber <sebastian.huber@…>, on 05/03/18 at 11:03:27

Simplify _CPU_Counter_difference()

In order to simplify the use of CPU counter values it is beneficial to
have monotonic increasing values within the range of the CPU counter
ticks data type, e.g. 32-bit unsigned integer. This eases the use of
CPU counter timestamps in external tools which do not know the details
of the CPU counter hardware. The CPU counter is the fastest way to get
a time on an RTEMS system.

Such a CPU counter may be also used as the timecounter. Use it on SPARC
for this purpose to simplify the clock drivers.

Update #3456.

  • Property mode set to 100644
File size: 42.5 KB
Line 
1/**
2 * @file rtems/score/cpu.h
3 *
4 * @brief NO_CPU Department Source
5 *
6 * This include file contains information pertaining to the NO_CPU
7 * processor.
8 */
9
10/*
11 *  This include file contains information pertaining to the XXX
12 *  processor.
13 *
14 *  @note This file is part of a porting template that is intended
15 *  to be used as the starting point when porting RTEMS to a new
16 *  CPU family.  The following needs to be done when using this as
17 *  the starting point for a new port:
18 *
19 *  + Anywhere there is an XXX, it should be replaced
20 *    with information about the CPU family being ported to.
21 *
22 *  + At the end of each comment section, there is a heading which
23 *    says "Port Specific Information:".  When porting to RTEMS,
24 *    add CPU family specific information in this section
25 */
26
27/*
28 *  COPYRIGHT (c) 1989-2008.
29 *  On-Line Applications Research Corporation (OAR).
30 *
31 *  The license and distribution terms for this file may be
32 *  found in the file LICENSE in this distribution or at
33 *  http://www.rtems.org/license/LICENSE.
34 */
35
36#ifndef _RTEMS_SCORE_CPU_H
37#define _RTEMS_SCORE_CPU_H
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43#include <rtems/score/basedefs.h>
44#include <rtems/score/no_cpu.h>
45
46/* conditional compilation parameters */
47
48/**
49 * Does the CPU follow the simple vectored interrupt model?
50 *
51 * If TRUE, then RTEMS allocates the vector table it internally manages.
52 * If FALSE, then the BSP is assumed to allocate and manage the vector
53 * table
54 *
55 * Port Specific Information:
56 *
57 * XXX document implementation including references if appropriate
58 */
59#define CPU_SIMPLE_VECTORED_INTERRUPTS TRUE
60
61/**
62 * Does the RTEMS invoke the user's ISR with the vector number and
63 * a pointer to the saved interrupt frame (1) or just the vector
64 * number (0)?
65 *
66 * Port Specific Information:
67 *
68 * XXX document implementation including references if appropriate
69 */
70#define CPU_ISR_PASSES_FRAME_POINTER FALSE
71
72/**
73 * @def CPU_HARDWARE_FP
74 *
75 * Does the CPU have hardware floating point?
76 *
77 * If TRUE, then the RTEMS_FLOATING_POINT task attribute is supported.
78 * If FALSE, then the RTEMS_FLOATING_POINT task attribute is ignored.
79 *
80 * If there is a FP coprocessor such as the i387 or mc68881, then
81 * the answer is TRUE.
82 *
83 * The macro name "NO_CPU_HAS_FPU" should be made CPU specific.
84 * It indicates whether or not this CPU model has FP support.  For
85 * example, it would be possible to have an i386_nofp CPU model
86 * which set this to false to indicate that you have an i386 without
87 * an i387 and wish to leave floating point support out of RTEMS.
88 */
89
90/**
91 * @def CPU_SOFTWARE_FP
92 *
93 * Does the CPU have no hardware floating point and GCC provides a
94 * software floating point implementation which must be context
95 * switched?
96 *
97 * This feature conditional is used to indicate whether or not there
98 * is software implemented floating point that must be context
99 * switched.  The determination of whether or not this applies
100 * is very tool specific and the state saved/restored is also
101 * compiler specific.
102 *
103 * Port Specific Information:
104 *
105 * XXX document implementation including references if appropriate
106 */
107#if ( NO_CPU_HAS_FPU == 1 )
108#define CPU_HARDWARE_FP     TRUE
109#else
110#define CPU_HARDWARE_FP     FALSE
111#endif
112#define CPU_SOFTWARE_FP     FALSE
113
114/**
115 * Are all tasks RTEMS_FLOATING_POINT tasks implicitly?
116 *
117 * If TRUE, then the RTEMS_FLOATING_POINT task attribute is assumed.
118 * If FALSE, then the RTEMS_FLOATING_POINT task attribute is followed.
119 *
120 * So far, the only CPUs in which this option has been used are the
121 * HP PA-RISC and PowerPC.  On the PA-RISC, The HP C compiler and
122 * gcc both implicitly used the floating point registers to perform
123 * integer multiplies.  Similarly, the PowerPC port of gcc has been
124 * seen to allocate floating point local variables and touch the FPU
125 * even when the flow through a subroutine (like vfprintf()) might
126 * not use floating point formats.
127 *
128 * If a function which you would not think utilize the FP unit DOES,
129 * then one can not easily predict which tasks will use the FP hardware.
130 * In this case, this option should be TRUE.
131 *
132 * If @ref CPU_HARDWARE_FP is FALSE, then this should be FALSE as well.
133 *
134 * Port Specific Information:
135 *
136 * XXX document implementation including references if appropriate
137 */
138#define CPU_ALL_TASKS_ARE_FP     TRUE
139
140/**
141 * Should the IDLE task have a floating point context?
142 *
143 * If TRUE, then the IDLE task is created as a RTEMS_FLOATING_POINT task
144 * and it has a floating point context which is switched in and out.
145 * If FALSE, then the IDLE task does not have a floating point context.
146 *
147 * Setting this to TRUE negatively impacts the time required to preempt
148 * the IDLE task from an interrupt because the floating point context
149 * must be saved as part of the preemption.
150 *
151 * Port Specific Information:
152 *
153 * XXX document implementation including references if appropriate
154 */
155#define CPU_IDLE_TASK_IS_FP      FALSE
156
157/**
158 * Should the saving of the floating point registers be deferred
159 * until a context switch is made to another different floating point
160 * task?
161 *
162 * If TRUE, then the floating point context will not be stored until
163 * necessary.  It will remain in the floating point registers and not
164 * disturned until another floating point task is switched to.
165 *
166 * If FALSE, then the floating point context is saved when a floating
167 * point task is switched out and restored when the next floating point
168 * task is restored.  The state of the floating point registers between
169 * those two operations is not specified.
170 *
171 * If the floating point context does NOT have to be saved as part of
172 * interrupt dispatching, then it should be safe to set this to TRUE.
173 *
174 * Setting this flag to TRUE results in using a different algorithm
175 * for deciding when to save and restore the floating point context.
176 * The deferred FP switch algorithm minimizes the number of times
177 * the FP context is saved and restored.  The FP context is not saved
178 * until a context switch is made to another, different FP task.
179 * Thus in a system with only one FP task, the FP context will never
180 * be saved or restored.
181 *
182 * Port Specific Information:
183 *
184 * XXX document implementation including references if appropriate
185 */
186#define CPU_USE_DEFERRED_FP_SWITCH       TRUE
187
188/**
189 * @brief Enables a robust thread dispatch if set to TRUE.
190 *
191 * In general, it is an application bug to call blocking operating system
192 * services with interrupts disabled.  In most situations this only increases
193 * the interrupt latency.  However, on SMP configurations or on some CPU port
194 * like ARM Cortex-M it leads to undefined system behaviour.  It order to ease
195 * the application development, this error condition is checked at run-time in
196 * case this CPU port option is defined to TRUE.
197 */
198#define CPU_ENABLE_ROBUST_THREAD_DISPATCH FALSE
199
200/**
201 * Does the stack grow up (toward higher addresses) or down
202 * (toward lower addresses)?
203 *
204 * If TRUE, then the grows upward.
205 * If FALSE, then the grows toward smaller addresses.
206 *
207 * Port Specific Information:
208 *
209 * XXX document implementation including references if appropriate
210 */
211#define CPU_STACK_GROWS_UP               TRUE
212
213/**
214 * The maximum cache line size in bytes.
215 *
216 * The actual processor may use no cache or a smaller cache line size.
217 */
218#define CPU_CACHE_LINE_BYTES 32
219
220/**
221 * The following is the variable attribute used to force alignment
222 * of critical RTEMS structures.  On some processors it may make
223 * sense to have these aligned on tighter boundaries than
224 * the minimum requirements of the compiler in order to have as
225 * much of the critical data area as possible in a cache line.
226 *
227 * Port Specific Information:
228 *
229 * XXX document implementation including references if appropriate
230 */
231#define CPU_STRUCTURE_ALIGNMENT RTEMS_ALIGNED( CPU_CACHE_LINE_BYTES )
232
233/**
234 * @ingroup CPUInterrupt
235 *
236 * The following defines the number of bits actually used in the
237 * interrupt field of the task mode.  How those bits map to the
238 * CPU interrupt levels is defined by the routine @ref _CPU_ISR_Set_level.
239 *
240 * Port Specific Information:
241 *
242 * XXX document implementation including references if appropriate
243 */
244#define CPU_MODES_INTERRUPT_MASK   0x00000001
245
246/**
247 * @brief Maximum number of processors of all systems supported by this CPU
248 * port.
249 */
250#define CPU_MAXIMUM_PROCESSORS 32
251
252/*
253 *  Processor defined structures required for cpukit/score.
254 *
255 *  Port Specific Information:
256 *
257 *  XXX document implementation including references if appropriate
258 */
259
260/* may need to put some structures here.  */
261
262/**
263 * @defgroup CPUContext Processor Dependent Context Management
264 *
265 * From the highest level viewpoint, there are 2 types of context to save.
266 *
267 *    -# Interrupt registers to save
268 *    -# Task level registers to save
269 *
270 * Since RTEMS handles integer and floating point contexts separately, this
271 * means we have the following 3 context items:
272 *
273 *    -# task level context stuff::  Context_Control
274 *    -# floating point task stuff:: Context_Control_fp
275 *    -# special interrupt level context :: CPU_Interrupt_frame
276 *
277 * On some processors, it is cost-effective to save only the callee
278 * preserved registers during a task context switch.  This means
279 * that the ISR code needs to save those registers which do not
280 * persist across function calls.  It is not mandatory to make this
281 * distinctions between the caller/callee saves registers for the
282 * purpose of minimizing context saved during task switch and on interrupts.
283 * If the cost of saving extra registers is minimal, simplicity is the
284 * choice.  Save the same context on interrupt entry as for tasks in
285 * this case.
286 *
287 * Additionally, if gdb is to be made aware of RTEMS tasks for this CPU, then
288 * care should be used in designing the context area.
289 *
290 * On some CPUs with hardware floating point support, the Context_Control_fp
291 * structure will not be used or it simply consist of an array of a
292 * fixed number of bytes.   This is done when the floating point context
293 * is dumped by a "FP save context" type instruction and the format
294 * is not really defined by the CPU.  In this case, there is no need
295 * to figure out the exact format -- only the size.  Of course, although
296 * this is enough information for RTEMS, it is probably not enough for
297 * a debugger such as gdb.  But that is another problem.
298 *
299 * Port Specific Information:
300 *
301 * XXX document implementation including references if appropriate
302 *
303 */
304/**@{**/
305
306/**
307 * @ingroup Management
308 * This defines the minimal set of integer and processor state registers
309 * that must be saved during a voluntary context switch from one thread
310 * to another.
311 */
312typedef struct {
313    /**
314     * This field is a hint that a port will have a number of integer
315     * registers that need to be saved at a context switch.
316     */
317    uint32_t   some_integer_register;
318    /**
319     * This field is a hint that a port will have a number of system
320     * registers that need to be saved at a context switch.
321     */
322    uint32_t   some_system_register;
323
324    /**
325     * This field is a hint that a port will have a register that
326     * is the stack pointer.
327     */
328    uint32_t   stack_pointer;
329
330#ifdef RTEMS_SMP
331    /**
332     * @brief On SMP configurations the thread context must contain a boolean
333     * indicator to signal if this context is executing on a processor.
334     *
335     * This field must be updated during a context switch.  The context switch
336     * to the heir must wait until the heir context indicates that it is no
337     * longer executing on a processor.  This indicator must be updated using
338     * an atomic test and set operation to ensure that at most one processor
339     * uses the heir context at the same time.  The context switch must also
340     * check for a potential new heir thread for this processor in case the
341     * heir context is not immediately available.  Update the executing thread
342     * for this processor only if necessary to avoid a cache line
343     * monopolization.
344     *
345     * @code
346     * void _CPU_Context_switch(
347     *   Context_Control *executing_context,
348     *   Context_Control *heir_context
349     * )
350     * {
351     *   save( executing_context );
352     *
353     *   executing_context->is_executing = false;
354     *   memory_barrier();
355     *
356     *   if ( test_and_set( &heir_context->is_executing ) ) {
357     *     do {
358     *       Per_CPU_Control *cpu_self = _Per_CPU_Get_snapshot();
359     *       Thread_Control *executing = cpu_self->executing;
360     *       Thread_Control *heir = cpu_self->heir;
361     *
362     *       if ( heir != executing ) {
363     *         cpu_self->executing = heir;
364     *         heir_context = (Context_Control *)
365     *           ((uintptr_t) heir + (uintptr_t) executing_context
366     *             - (uintptr_t) executing)
367     *       }
368     *     } while ( test_and_set( &heir_context->is_executing ) );
369     *   }
370     *
371     *   restore( heir_context );
372     * }
373     * @endcode
374     */
375    volatile bool is_executing;
376#endif
377} Context_Control;
378
379/**
380 * @ingroup Management
381 *
382 * This macro returns the stack pointer associated with @a _context.
383 *
384 * @param[in] _context is the thread context area to access
385 *
386 * @return This method returns the stack pointer.
387 */
388#define _CPU_Context_Get_SP( _context ) \
389  (_context)->stack_pointer
390
391/**
392 * @ingroup Management
393 *
394 * This defines the complete set of floating point registers that must
395 * be saved during any context switch from one thread to another.
396 */
397typedef struct {
398    /** FPU registers are listed here */
399    double      some_float_register;
400} Context_Control_fp;
401
402/**
403 * @ingroup Management
404 *
405 * This defines the set of integer and processor state registers that must
406 * be saved during an interrupt.  This set does not include any which are
407 * in @ref Context_Control.
408 */
409typedef struct {
410    /**
411     * This field is a hint that a port will have a number of integer
412     * registers that need to be saved when an interrupt occurs or
413     * when a context switch occurs at the end of an ISR.
414     */
415    uint32_t   special_interrupt_register;
416} CPU_Interrupt_frame;
417
418/**
419 * This variable is optional.  It is used on CPUs on which it is difficult
420 * to generate an "uninitialized" FP context.  It is filled in by
421 * @ref _CPU_Initialize and copied into the task's FP context area during
422 * @ref _CPU_Context_Initialize.
423 *
424 * Port Specific Information:
425 *
426 * XXX document implementation including references if appropriate
427 */
428extern Context_Control_fp _CPU_Null_fp_context;
429
430/** @} */
431
432/**
433 * @defgroup CPUInterrupt Processor Dependent Interrupt Management
434 *
435 * RTEMS supports a software managed interrupt stack.  The interrupt stacks
436 * are statically allocated by <rtems/confdefs.h> and the switch is performed
437 * by hardware or the interrupt processing code.  These variables contain
438 * pointers to the lowest and highest addresses in the chunk of memory
439 * allocated for the interrupt stack.  Since it is unknown whether the stack
440 * grows up or down (in general), this gives the CPU dependent code the option
441 * of picking the version it wants to use.
442 *
443 * Port Specific Information:
444 *
445 * XXX document implementation including references if appropriate
446 */
447
448/*
449 *  Nothing prevents the porter from declaring more CPU specific variables.
450 *
451 *  Port Specific Information:
452 *
453 *  XXX document implementation including references if appropriate
454 */
455
456/* XXX: if needed, put more variables here */
457
458/**
459 * @ingroup CPUContext
460 *
461 * The size of the floating point context area.  On some CPUs this
462 * will not be a "sizeof" because the format of the floating point
463 * area is not defined -- only the size is.  This is usually on
464 * CPUs with a "floating point save context" instruction.
465 *
466 * Port Specific Information:
467 *
468 * XXX document implementation including references if appropriate
469 */
470#define CPU_CONTEXT_FP_SIZE sizeof( Context_Control_fp )
471
472/**
473 * Amount of extra stack (above minimum stack size) required by
474 * MPCI receive server thread.  Remember that in a multiprocessor
475 * system this thread must exist and be able to process all directives.
476 *
477 * Port Specific Information:
478 *
479 * XXX document implementation including references if appropriate
480 */
481#define CPU_MPCI_RECEIVE_SERVER_EXTRA_STACK 0
482
483/**
484 * @ingroup CPUInterrupt
485 *
486 * This defines the number of entries in the _ISR_Vector_table managed by RTEMS
487 * in case CPU_SIMPLE_VECTORED_INTERRUPTS is defined to TRUE.  It must be a
488 * compile-time constant.
489 *
490 * It must be undefined in case CPU_SIMPLE_VECTORED_INTERRUPTS is defined to
491 * FALSE.
492 */
493#define CPU_INTERRUPT_NUMBER_OF_VECTORS      32
494
495/**
496 * @ingroup CPUInterrupt
497 *
498 * This defines the highest interrupt vector number for this port in case
499 * CPU_SIMPLE_VECTORED_INTERRUPTS is defined to TRUE.  It must be less than
500 * CPU_INTERRUPT_NUMBER_OF_VECTORS.  It may be not a compile-time constant.
501 *
502 * It must be undefined in case CPU_SIMPLE_VECTORED_INTERRUPTS is defined to
503 * FALSE.
504 */
505#define CPU_INTERRUPT_MAXIMUM_VECTOR_NUMBER  (CPU_INTERRUPT_NUMBER_OF_VECTORS - 1)
506
507/**
508 * @ingroup CPUInterrupt
509 *
510 * This is defined if the port has a special way to report the ISR nesting
511 * level.  Most ports maintain the variable @a _ISR_Nest_level.
512 */
513#define CPU_PROVIDES_ISR_IS_IN_PROGRESS FALSE
514
515/**
516 * @ingroup CPUContext
517 *
518 * Should be large enough to run all RTEMS tests.  This ensures
519 * that a "reasonable" small application should not have any problems.
520 *
521 * Port Specific Information:
522 *
523 * XXX document implementation including references if appropriate
524 */
525#define CPU_STACK_MINIMUM_SIZE          (1024*4)
526
527/**
528 * Size of a pointer.
529 *
530 * This must be an integer literal that can be used by the assembler.  This
531 * value will be used to calculate offsets of structure members.  These
532 * offsets will be used in assembler code.
533 */
534#define CPU_SIZEOF_POINTER         4
535
536/**
537 * CPU's worst alignment requirement for data types on a byte boundary.  This
538 * alignment does not take into account the requirements for the stack.  It
539 * must be a power of two greater than or equal to two.  The power of two
540 * requirement makes it possible to align values easily using simple bit
541 * operations.
542 *
543 * Port Specific Information:
544 *
545 * XXX document implementation including references if appropriate
546 */
547#define CPU_ALIGNMENT              8
548
549/**
550 * This number corresponds to the byte alignment requirement for the
551 * heap handler.  This alignment requirement may be stricter than that
552 * for the data types alignment specified by @ref CPU_ALIGNMENT.  It is
553 * common for the heap to follow the same alignment requirement as
554 * @ref CPU_ALIGNMENT.  If the @ref CPU_ALIGNMENT is strict enough for
555 * the heap, then this should be set to @ref CPU_ALIGNMENT.
556 *
557 * NOTE:  It must be a power of two greater than or equal to two.  The
558 *        requirement to be a multiple of two is because the heap uses the
559 *        least significant field of the front and back flags to indicate that
560 *        a block is in use or free.  So you do not want any odd length blocks
561 *        really putting length data in that bit.
562 *
563 *        On byte oriented architectures, @ref CPU_HEAP_ALIGNMENT normally will
564 *        have to be greater or equal to than @ref CPU_ALIGNMENT to ensure that
565 *        elements allocated from the heap meet all restrictions.
566 *
567 * Port Specific Information:
568 *
569 * XXX document implementation including references if appropriate
570 */
571#define CPU_HEAP_ALIGNMENT         CPU_ALIGNMENT
572
573/**
574 * This number corresponds to the byte alignment requirement for the
575 * stack.  This alignment requirement may be stricter than that for the
576 * data types alignment specified by @ref CPU_ALIGNMENT.  If the
577 * @ref CPU_ALIGNMENT is strict enough for the stack, then this should be
578 * set to 0.
579 *
580 * NOTE: This must be a power of 2 either 0 or greater than @ref CPU_ALIGNMENT.
581 *
582 * Port Specific Information:
583 *
584 * XXX document implementation including references if appropriate
585 */
586#define CPU_STACK_ALIGNMENT        0
587
588/**
589 * The alignment of the interrupt stack in bytes.
590 *
591 * The alignment should take the stack ABI and the cache line size into
592 * account.
593 */
594#define CPU_INTERRUPT_STACK_ALIGNMENT CPU_CACHE_LINE_BYTES
595
596/*
597 *  ISR handler macros
598 */
599
600/**
601 * @ingroup CPUInterrupt
602 *
603 * Support routine to initialize the RTEMS vector table after it is allocated.
604 *
605 * Port Specific Information:
606 *
607 * XXX document implementation including references if appropriate
608 */
609#define _CPU_Initialize_vectors()
610
611/**
612 * @ingroup CPUInterrupt
613 *
614 * Disable all interrupts for an RTEMS critical section.  The previous
615 * level is returned in @a _isr_cookie.
616 *
617 * @param[out] _isr_cookie will contain the previous level cookie
618 *
619 * Port Specific Information:
620 *
621 * XXX document implementation including references if appropriate
622 */
623#define _CPU_ISR_Disable( _isr_cookie ) \
624  { \
625    (_isr_cookie) = 0;   /* do something to prevent warnings */ \
626  }
627
628/**
629 * @ingroup CPUInterrupt
630 *
631 * Enable interrupts to the previous level (returned by _CPU_ISR_Disable).
632 * This indicates the end of an RTEMS critical section.  The parameter
633 * @a _isr_cookie is not modified.
634 *
635 * @param[in] _isr_cookie contain the previous level cookie
636 *
637 * Port Specific Information:
638 *
639 * XXX document implementation including references if appropriate
640 */
641#define _CPU_ISR_Enable( _isr_cookie )  \
642  { \
643  }
644
645/**
646 * @ingroup CPUInterrupt
647 *
648 * This temporarily restores the interrupt to @a _isr_cookie before immediately
649 * disabling them again.  This is used to divide long RTEMS critical
650 * sections into two or more parts.  The parameter @a _isr_cookie is not
651 * modified.
652 *
653 * @param[in] _isr_cookie contain the previous level cookie
654 *
655 * Port Specific Information:
656 *
657 * XXX document implementation including references if appropriate
658 */
659#define _CPU_ISR_Flash( _isr_cookie ) \
660  { \
661  }
662
663/**
664 * @brief Returns true if interrupts are enabled in the specified ISR level,
665 * otherwise returns false.
666 *
667 * @param[in] level The ISR level.
668 *
669 * @retval true Interrupts are enabled in the ISR level.
670 * @retval false Otherwise.
671 */
672RTEMS_INLINE_ROUTINE bool _CPU_ISR_Is_enabled( uint32_t level )
673{
674  return false;
675}
676
677/**
678 * @ingroup CPUInterrupt
679 *
680 * This routine and @ref _CPU_ISR_Get_level
681 * Map the interrupt level in task mode onto the hardware that the CPU
682 * actually provides.  Currently, interrupt levels which do not
683 * map onto the CPU in a generic fashion are undefined.  Someday,
684 * it would be nice if these were "mapped" by the application
685 * via a callout.  For example, m68k has 8 levels 0 - 7, levels
686 * 8 - 255 would be available for bsp/application specific meaning.
687 * This could be used to manage a programmable interrupt controller
688 * via the rtems_task_mode directive.
689 *
690 * Port Specific Information:
691 *
692 * XXX document implementation including references if appropriate
693 */
694#define _CPU_ISR_Set_level( new_level ) \
695  { \
696  }
697
698/**
699 * @ingroup CPUInterrupt
700 *
701 * Return the current interrupt disable level for this task in
702 * the format used by the interrupt level portion of the task mode.
703 *
704 * NOTE: This routine usually must be implemented as a subroutine.
705 *
706 * Port Specific Information:
707 *
708 * XXX document implementation including references if appropriate
709 */
710uint32_t   _CPU_ISR_Get_level( void );
711
712/* end of ISR handler macros */
713
714/* Context handler macros */
715
716/**
717 * @ingroup CPUContext
718 *
719 * @brief Destroys the context of the thread.
720 *
721 * It must be implemented as a macro and an implementation is optional.  The
722 * default implementation does nothing.
723 *
724 * @param[in] _the_thread The corresponding thread.
725 * @param[in] _the_context The context to destroy.
726 *
727 * Port Specific Information:
728 *
729 * XXX document implementation including references if appropriate
730 */
731#define _CPU_Context_Destroy( _the_thread, _the_context ) \
732  { \
733  }
734
735/**
736 *  @ingroup CPUContext
737 *
738 * Initialize the context to a state suitable for starting a
739 * task after a context restore operation.  Generally, this
740 * involves:
741 *
742 *    - setting a starting address
743 *    - preparing the stack
744 *    - preparing the stack and frame pointers
745 *    - setting the proper interrupt level in the context
746 *    - initializing the floating point context
747 *
748 * This routine generally does not set any unnecessary register
749 * in the context.  The state of the "general data" registers is
750 * undefined at task start time.
751 *
752 * The ISR dispatch disable field of the context must be cleared to zero if it
753 * is used by the CPU port.  Otherwise, a thread restart results in
754 * unpredictable behaviour.
755 *
756 * @param[in] _the_context is the context structure to be initialized
757 * @param[in] _stack_base is the lowest physical address of this task's stack
758 * @param[in] _size is the size of this task's stack
759 * @param[in] _isr is the interrupt disable level
760 * @param[in] _entry_point is the thread's entry point.  This is
761 *        always @a _Thread_Handler
762 * @param[in] _is_fp is TRUE if the thread is to be a floating
763 *       point thread.  This is typically only used on CPUs where the
764 *       FPU may be easily disabled by software such as on the SPARC
765 *       where the PSR contains an enable FPU bit.
766 * @param[in] _tls_area The thread-local storage (TLS) area.
767 *
768 * Port Specific Information:
769 *
770 * XXX document implementation including references if appropriate
771 */
772#define _CPU_Context_Initialize( _the_context, _stack_base, _size, \
773                                 _isr, _entry_point, _is_fp, _tls_area ) \
774  { \
775  }
776
777/**
778 * This routine is responsible for somehow restarting the currently
779 * executing task.  If you are lucky, then all that is necessary
780 * is restoring the context.  Otherwise, there will need to be
781 * a special assembly routine which does something special in this
782 * case.  For many ports, simply adding a label to the restore path
783 * of @ref _CPU_Context_switch will work.  On other ports, it may be
784 * possibly to load a few arguments and jump to the restore path. It will
785 * not work if restarting self conflicts with the stack frame
786 * assumptions of restoring a context.
787 *
788 * Port Specific Information:
789 *
790 * XXX document implementation including references if appropriate
791 */
792#define _CPU_Context_Restart_self( _the_context ) \
793   _CPU_Context_restore( (_the_context) );
794
795/**
796 * This routine initializes the FP context area passed to it to.
797 * There are a few standard ways in which to initialize the
798 * floating point context.  The code included for this macro assumes
799 * that this is a CPU in which a "initial" FP context was saved into
800 * @a _CPU_Null_fp_context and it simply copies it to the destination
801 * context passed to it.
802 *
803 * Other floating point context save/restore models include:
804 *   -# not doing anything, and
805 *   -# putting a "null FP status word" in the correct place in the FP context.
806 *
807 * @param[in] _destination is the floating point context area
808 *
809 * Port Specific Information:
810 *
811 * XXX document implementation including references if appropriate
812 */
813#define _CPU_Context_Initialize_fp( _destination ) \
814  { \
815   *(*(_destination)) = _CPU_Null_fp_context; \
816  }
817
818/* end of Context handler macros */
819
820/* Fatal Error manager macros */
821
822/**
823 * This routine copies _error into a known place -- typically a stack
824 * location or a register, optionally disables interrupts, and
825 * halts/stops the CPU.
826 *
827 * Port Specific Information:
828 *
829 * XXX document implementation including references if appropriate
830 */
831#define _CPU_Fatal_halt( _source, _error ) \
832  { \
833  }
834
835/* end of Fatal Error manager macros */
836
837/* Bitfield handler macros */
838
839/**
840 * @defgroup CPUBitfield Processor Dependent Bitfield Manipulation
841 *
842 * This set of routines are used to implement fast searches for
843 * the most important ready task.
844 *
845 */
846/**@{**/
847
848/**
849 * This definition is set to TRUE if the port uses the generic bitfield
850 * manipulation implementation.
851 */
852#define CPU_USE_GENERIC_BITFIELD_CODE TRUE
853
854/**
855 * This routine sets @a _output to the bit number of the first bit
856 * set in @a _value.  @a _value is of CPU dependent type
857 * @a Priority_bit_map_Word.  This type may be either 16 or 32 bits
858 * wide although only the 16 least significant bits will be used.
859 *
860 * There are a number of variables in using a "find first bit" type
861 * instruction.
862 *
863 *   -# What happens when run on a value of zero?
864 *   -# Bits may be numbered from MSB to LSB or vice-versa.
865 *   -# The numbering may be zero or one based.
866 *   -# The "find first bit" instruction may search from MSB or LSB.
867 *
868 * RTEMS guarantees that (1) will never happen so it is not a concern.
869 * (2),(3), (4) are handled by the macros @ref _CPU_Priority_Mask and
870 * @ref _CPU_Priority_bits_index.  These three form a set of routines
871 * which must logically operate together.  Bits in the _value are
872 * set and cleared based on masks built by @ref _CPU_Priority_Mask.
873 * The basic major and minor values calculated by @ref _Priority_Major
874 * and @ref _Priority_Minor are "massaged" by @ref _CPU_Priority_bits_index
875 * to properly range between the values returned by the "find first bit"
876 * instruction.  This makes it possible for @ref _Priority_Get_highest to
877 * calculate the major and directly index into the minor table.
878 * This mapping is necessary to ensure that 0 (a high priority major/minor)
879 * is the first bit found.
880 *
881 * This entire "find first bit" and mapping process depends heavily
882 * on the manner in which a priority is broken into a major and minor
883 * components with the major being the 4 MSB of a priority and minor
884 * the 4 LSB.  Thus (0 << 4) + 0 corresponds to priority 0 -- the highest
885 * priority.  And (15 << 4) + 14 corresponds to priority 254 -- the next
886 * to the lowest priority.
887 *
888 * If your CPU does not have a "find first bit" instruction, then
889 * there are ways to make do without it.  Here are a handful of ways
890 * to implement this in software:
891 *
892@verbatim
893      - a series of 16 bit test instructions
894      - a "binary search using if's"
895      - _number = 0
896        if _value > 0x00ff
897          _value >>=8
898          _number = 8;
899
900        if _value > 0x0000f
901          _value >=8
902          _number += 4
903
904        _number += bit_set_table[ _value ]
905@endverbatim
906
907 *   where bit_set_table[ 16 ] has values which indicate the first
908 *     bit set
909 *
910 * @param[in] _value is the value to be scanned
911 * @param[in] _output is the first bit set
912 *
913 * Port Specific Information:
914 *
915 * XXX document implementation including references if appropriate
916 */
917
918#if (CPU_USE_GENERIC_BITFIELD_CODE == FALSE)
919#define _CPU_Bitfield_Find_first_bit( _value, _output ) \
920  { \
921    (_output) = 0;   /* do something to prevent warnings */ \
922  }
923#endif
924
925/** @} */
926
927/* end of Bitfield handler macros */
928
929/**
930 * This routine builds the mask which corresponds to the bit fields
931 * as searched by @ref _CPU_Bitfield_Find_first_bit.  See the discussion
932 * for that routine.
933 *
934 * Port Specific Information:
935 *
936 * XXX document implementation including references if appropriate
937 */
938#if (CPU_USE_GENERIC_BITFIELD_CODE == FALSE)
939
940#define _CPU_Priority_Mask( _bit_number ) \
941  ( 1 << (_bit_number) )
942
943#endif
944
945/**
946 * @ingroup CPUBitfield
947 *
948 * This routine translates the bit numbers returned by
949 * @ref _CPU_Bitfield_Find_first_bit into something suitable for use as
950 * a major or minor component of a priority.  See the discussion
951 * for that routine.
952 *
953 * @param[in] _priority is the major or minor number to translate
954 *
955 * Port Specific Information:
956 *
957 * XXX document implementation including references if appropriate
958 */
959#if (CPU_USE_GENERIC_BITFIELD_CODE == FALSE)
960
961#define _CPU_Priority_bits_index( _priority ) \
962  (_priority)
963
964#endif
965
966/* end of Priority handler macros */
967
968/* functions */
969
970/**
971 * This routine performs CPU dependent initialization.
972 *
973 * Port Specific Information:
974 *
975 * XXX document implementation including references if appropriate
976 */
977void _CPU_Initialize(void);
978
979typedef void ( *CPU_ISR_raw_handler )( void );
980
981/**
982 * @ingroup CPUInterrupt
983 *
984 * This routine installs a "raw" interrupt handler directly into the
985 * processor's vector table.
986 *
987 * This routine is not used by architecture-independent code and thus optional.
988 *
989 * @param[in] vector is the vector number
990 * @param[in] new_handler is the raw ISR handler to install
991 * @param[in] old_handler is the previously installed ISR Handler
992 *
993 * Port Specific Information:
994 *
995 * XXX document implementation including references if appropriate
996 */
997void _CPU_ISR_install_raw_handler(
998  uint32_t             vector,
999  CPU_ISR_raw_handler  new_handler,
1000  CPU_ISR_raw_handler *old_handler
1001);
1002
1003typedef void ( *CPU_ISR_handler )( uint32_t );
1004
1005/**
1006 * @ingroup CPUInterrupt
1007 *
1008 * This routine installs an interrupt vector.
1009 *
1010 * This routine is only used by architecture-independent code if
1011 * CPU_SIMPLE_VECTORED_INTERRUPTS == TRUE, otherwise it is optional.
1012 *
1013 * @param[in] vector is the vector number
1014 * @param[in] new_handler is the RTEMS ISR handler to install
1015 * @param[in] old_handler is the previously installed ISR Handler
1016 *
1017 * Port Specific Information:
1018 *
1019 * XXX document implementation including references if appropriate
1020 */
1021void _CPU_ISR_install_vector(
1022  uint32_t         vector,
1023  CPU_ISR_handler  new_handler,
1024  CPU_ISR_handler *old_handler
1025);
1026
1027/**
1028 * This routine is the CPU dependent IDLE thread body.
1029 *
1030 * Port Specific Information:
1031 *
1032 * XXX document implementation including references if appropriate
1033 */
1034void *_CPU_Thread_Idle_body( uintptr_t ignored );
1035
1036/**
1037 * @ingroup CPUContext
1038 *
1039 * This routine switches from the run context to the heir context.
1040 *
1041 * @param[in] run points to the context of the currently executing task
1042 * @param[in] heir points to the context of the heir task
1043 *
1044 * Port Specific Information:
1045 *
1046 * XXX document implementation including references if appropriate
1047 */
1048void _CPU_Context_switch(
1049  Context_Control  *run,
1050  Context_Control  *heir
1051);
1052
1053/**
1054 * @ingroup CPUContext
1055 *
1056 * This routine is generally used only to restart self in an
1057 * efficient manner.  It may simply be a label in @ref _CPU_Context_switch.
1058 *
1059 * @param[in] new_context points to the context to be restored.
1060 *
1061 * NOTE: May be unnecessary to reload some registers.
1062 *
1063 * Port Specific Information:
1064 *
1065 * XXX document implementation including references if appropriate
1066 */
1067void _CPU_Context_restore(
1068  Context_Control *new_context
1069) RTEMS_NO_RETURN;
1070
1071/**
1072 * @ingroup CPUContext
1073 *
1074 * This routine saves the floating point context passed to it.
1075 *
1076 * @param[in] fp_context_ptr is a pointer to a pointer to a floating
1077 * point context area
1078 *
1079 * @return on output @a *fp_context_ptr will contain the address that
1080 * should be used with @ref _CPU_Context_restore_fp to restore this context.
1081 *
1082 * Port Specific Information:
1083 *
1084 * XXX document implementation including references if appropriate
1085 */
1086void _CPU_Context_save_fp(
1087  Context_Control_fp **fp_context_ptr
1088);
1089
1090/**
1091 * @ingroup CPUContext
1092 *
1093 * This routine restores the floating point context passed to it.
1094 *
1095 * @param[in] fp_context_ptr is a pointer to a pointer to a floating
1096 * point context area to restore
1097 *
1098 * @return on output @a *fp_context_ptr will contain the address that
1099 * should be used with @ref _CPU_Context_save_fp to save this context.
1100 *
1101 * Port Specific Information:
1102 *
1103 * XXX document implementation including references if appropriate
1104 */
1105void _CPU_Context_restore_fp(
1106  Context_Control_fp **fp_context_ptr
1107);
1108
1109/**
1110 * @brief The set of registers that specifies the complete processor state.
1111 *
1112 * The CPU exception frame may be available in fatal error conditions like for
1113 * example illegal opcodes, instruction fetch errors, or data access errors.
1114 *
1115 * @see rtems_fatal(), RTEMS_FATAL_SOURCE_EXCEPTION, and
1116 * rtems_exception_frame_print().
1117 */
1118typedef struct {
1119  uint32_t processor_state_register;
1120  uint32_t integer_registers [1];
1121  double float_registers [1];
1122} CPU_Exception_frame;
1123
1124/**
1125 * @brief Prints the exception frame via printk().
1126 *
1127 * @see rtems_fatal() and RTEMS_FATAL_SOURCE_EXCEPTION.
1128 */
1129void _CPU_Exception_frame_print( const CPU_Exception_frame *frame );
1130
1131/**
1132 * @ingroup CPUEndian
1133 *
1134 * The following routine swaps the endian format of an unsigned int.
1135 * It must be static because it is referenced indirectly.
1136 *
1137 * This version will work on any processor, but if there is a better
1138 * way for your CPU PLEASE use it.  The most common way to do this is to:
1139 *
1140 *    swap least significant two bytes with 16-bit rotate
1141 *    swap upper and lower 16-bits
1142 *    swap most significant two bytes with 16-bit rotate
1143 *
1144 * Some CPUs have special instructions which swap a 32-bit quantity in
1145 * a single instruction (e.g. i486).  It is probably best to avoid
1146 * an "endian swapping control bit" in the CPU.  One good reason is
1147 * that interrupts would probably have to be disabled to ensure that
1148 * an interrupt does not try to access the same "chunk" with the wrong
1149 * endian.  Another good reason is that on some CPUs, the endian bit
1150 * endianness for ALL fetches -- both code and data -- so the code
1151 * will be fetched incorrectly.
1152 *
1153 * @param[in] value is the value to be swapped
1154 * @return the value after being endian swapped
1155 *
1156 * Port Specific Information:
1157 *
1158 * XXX document implementation including references if appropriate
1159 */
1160static inline uint32_t CPU_swap_u32(
1161  uint32_t value
1162)
1163{
1164  uint32_t byte1, byte2, byte3, byte4, swapped;
1165
1166  byte4 = (value >> 24) & 0xff;
1167  byte3 = (value >> 16) & 0xff;
1168  byte2 = (value >> 8)  & 0xff;
1169  byte1 =  value        & 0xff;
1170
1171  swapped = (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4;
1172  return swapped;
1173}
1174
1175/**
1176 * @ingroup CPUEndian
1177 *
1178 * This routine swaps a 16 bir quantity.
1179 *
1180 * @param[in] value is the value to be swapped
1181 * @return the value after being endian swapped
1182 */
1183#define CPU_swap_u16( value ) \
1184  (((value&0xff) << 8) | ((value >> 8)&0xff))
1185
1186/**
1187 * @brief Unsigned integer type for CPU counter values.
1188 */
1189typedef uint32_t CPU_Counter_ticks;
1190
1191/**
1192 * @brief Returns the current CPU counter frequency in Hz.
1193 *
1194 * @return The current CPU counter frequency in Hz.
1195 */
1196uint32_t _CPU_Counter_frequency( void );
1197
1198/**
1199 * @brief Returns the current CPU counter value.
1200 *
1201 * A CPU counter is some free-running counter.  It ticks usually with a
1202 * frequency close to the CPU or system bus clock.  The board support package
1203 * must ensure that this function works before the RTEMS initialization.
1204 * Otherwise invalid profiling statistics will be gathered.
1205 *
1206 * @return The current CPU counter value.
1207 */
1208CPU_Counter_ticks _CPU_Counter_read( void );
1209
1210/**
1211 * @brief Returns the difference between the second and first CPU counter
1212 * value.
1213 *
1214 * This operation may be carried out as a modulo operation depending on the
1215 * range of the CPU counter device.
1216 *
1217 * @param[in] second The second CPU counter value.
1218 * @param[in] first The first CPU counter value.
1219 *
1220 * @return Returns second minus first modulo counter period.
1221 */
1222static inline CPU_Counter_ticks _CPU_Counter_difference(
1223  CPU_Counter_ticks second,
1224  CPU_Counter_ticks first
1225)
1226{
1227  return second - first;
1228}
1229
1230#ifdef RTEMS_SMP
1231  /**
1232   * @brief Performs CPU specific SMP initialization in the context of the boot
1233   * processor.
1234   *
1235   * This function is invoked on the boot processor during system
1236   * initialization.  All interrupt stacks are allocated at this point in case
1237   * the CPU port allocates the interrupt stacks.  This function is called
1238   * before _CPU_SMP_Start_processor() or _CPU_SMP_Finalize_initialization() is
1239   * used.
1240   *
1241   * @return The count of physically or virtually available processors.
1242   * Depending on the configuration the application may use not all processors.
1243   */
1244  uint32_t _CPU_SMP_Initialize( void );
1245
1246  /**
1247   * @brief Starts a processor specified by its index.
1248   *
1249   * This function is invoked on the boot processor during system
1250   * initialization.
1251   *
1252   * This function will be called after _CPU_SMP_Initialize().
1253   *
1254   * @param[in] cpu_index The processor index.
1255   *
1256   * @retval true Successful operation.
1257   * @retval false Unable to start this processor.
1258   */
1259  bool _CPU_SMP_Start_processor( uint32_t cpu_index );
1260
1261  /**
1262   * @brief Performs final steps of CPU specific SMP initialization in the
1263   * context of the boot processor.
1264   *
1265   * This function is invoked on the boot processor during system
1266   * initialization.
1267   *
1268   * This function will be called after all processors requested by the
1269   * application have been started.
1270   *
1271   * @param[in] cpu_count The minimum value of the count of processors
1272   * requested by the application configuration and the count of physically or
1273   * virtually available processors.
1274   */
1275  void _CPU_SMP_Finalize_initialization( uint32_t cpu_count );
1276
1277  /**
1278   * @brief Prepares a CPU to start multitasking in terms of SMP.
1279   *
1280   * This function is invoked on all processors requested by the application
1281   * during system initialization.
1282   *
1283   * This function will be called after all processors requested by the
1284   * application have been started right before the context switch to the first
1285   * thread takes place.
1286   */
1287  void _CPU_SMP_Prepare_start_multitasking( void );
1288
1289  /**
1290   * @brief Returns the index of the current processor.
1291   *
1292   * An architecture specific method must be used to obtain the index of the
1293   * current processor in the system.  The set of processor indices is the
1294   * range of integers starting with zero up to the processor count minus one.
1295   */
1296  static inline uint32_t _CPU_SMP_Get_current_processor( void )
1297  {
1298    return 123;
1299  }
1300
1301  /**
1302   * @brief Sends an inter-processor interrupt to the specified target
1303   * processor.
1304   *
1305   * This operation is undefined for target processor indices out of range.
1306   *
1307   * @param[in] target_processor_index The target processor index.
1308   */
1309  void _CPU_SMP_Send_interrupt( uint32_t target_processor_index );
1310
1311  /**
1312   * @brief Broadcasts a processor event.
1313   *
1314   * Some architectures provide a low-level synchronization primitive for
1315   * processors in a multi-processor environment.  Processors waiting for this
1316   * event may go into a low-power state and stop generating system bus
1317   * transactions.  This function must ensure that preceding store operations
1318   * can be observed by other processors.
1319   *
1320   * @see _CPU_SMP_Processor_event_receive().
1321   */
1322  static inline void _CPU_SMP_Processor_event_broadcast( void )
1323  {
1324    __asm__ volatile ( "" : : : "memory" );
1325  }
1326
1327  /**
1328   * @brief Receives a processor event.
1329   *
1330   * This function will wait for the processor event and may wait forever if no
1331   * such event arrives.
1332   *
1333   * @see _CPU_SMP_Processor_event_broadcast().
1334   */
1335  static inline void _CPU_SMP_Processor_event_receive( void )
1336  {
1337    __asm__ volatile ( "" : : : "memory" );
1338  }
1339
1340  /**
1341   * @brief Gets the is executing indicator of the thread context.
1342   *
1343   * @param[in] context The context.
1344   */
1345  static inline bool _CPU_Context_Get_is_executing(
1346    const Context_Control *context
1347  )
1348  {
1349    return context->is_executing;
1350  }
1351
1352  /**
1353   * @brief Sets the is executing indicator of the thread context.
1354   *
1355   * @param[in] context The context.
1356   * @param[in] is_executing The new value for the is executing indicator.
1357   */
1358  static inline void _CPU_Context_Set_is_executing(
1359    Context_Control *context,
1360    bool is_executing
1361  )
1362  {
1363    context->is_executing = is_executing;
1364  }
1365
1366/** Type that can store a 32-bit integer or a pointer. */
1367typedef uintptr_t CPU_Uint32ptr;
1368
1369#endif
1370
1371#ifdef __cplusplus
1372}
1373#endif
1374
1375#endif
Note: See TracBrowser for help on using the repository browser.