source: rtems/cpukit/score/cpu/i386/include/rtems/score/cpu.h @ 42f2fdfd

5
Last change on this file since 42f2fdfd was 42f2fdfd, checked in by Sebastian Huber <sebastian.huber@…>, on 07/20/18 at 05:56:43

score: Move context validation declarations

The context validation support functions _CPU_Context_validate() and
_CPU_Context_volatile_clobber() are used only by one test program
(spcontext01). Move the function declarations to the CPU port
implementation header file.

  • Property mode set to 100644
File size: 17.9 KB
Line 
1/**
2 * @file
3 *
4 * @brief Intel I386 CPU Dependent Source
5 *
6 * This include file contains information pertaining to the Intel
7 * i386 processor.
8 */
9
10/*
11 *  COPYRIGHT (c) 1989-2011.
12 *  On-Line Applications Research Corporation (OAR).
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.rtems.org/license/LICENSE.
17 */
18
19#ifndef _RTEMS_SCORE_CPU_H
20#define _RTEMS_SCORE_CPU_H
21
22#ifndef ASM
23#include <string.h> /* for memcpy */
24#endif
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#include <rtems/score/basedefs.h>
31#if defined(RTEMS_PARAVIRT)
32#include <rtems/score/paravirt.h>
33#endif
34#include <rtems/score/i386.h>
35
36/* conditional compilation parameters */
37
38/*
39 *  Does the CPU follow the simple vectored interrupt model?
40 *
41 *  If TRUE, then RTEMS allocates the vector table it internally manages.
42 *  If FALSE, then the BSP is assumed to allocate and manage the vector
43 *  table
44 *
45 *  PowerPC Specific Information:
46 *
47 *  The PowerPC and x86 were the first to use the PIC interrupt model.
48 *  They do not use the simple vectored interrupt model.
49 */
50#define CPU_SIMPLE_VECTORED_INTERRUPTS FALSE
51
52/*
53 *  Does the RTEMS invoke the user's ISR with the vector number and
54 *  a pointer to the saved interrupt frame (1) or just the vector
55 *  number (0)?
56 */
57
58#define CPU_ISR_PASSES_FRAME_POINTER FALSE
59
60/*
61 *  Some family members have no FP, some have an FPU such as the i387
62 *  for the i386, others have it built in (i486DX, Pentium).
63 */
64
65#ifdef __SSE__
66#define CPU_HARDWARE_FP                  TRUE
67#define CPU_SOFTWARE_FP                  FALSE
68
69#define CPU_ALL_TASKS_ARE_FP             TRUE
70#define CPU_IDLE_TASK_IS_FP              TRUE
71#define CPU_USE_DEFERRED_FP_SWITCH       FALSE
72#else /* __SSE__ */
73
74#if ( I386_HAS_FPU == 1 )
75#define CPU_HARDWARE_FP     TRUE    /* i387 for i386 */
76#else
77#define CPU_HARDWARE_FP     FALSE
78#endif
79#define CPU_SOFTWARE_FP     FALSE
80
81#define CPU_ALL_TASKS_ARE_FP             FALSE
82#define CPU_IDLE_TASK_IS_FP              FALSE
83#if defined(RTEMS_SMP)
84  #define CPU_USE_DEFERRED_FP_SWITCH     FALSE
85#else
86  #define CPU_USE_DEFERRED_FP_SWITCH     TRUE
87#endif
88#endif /* __SSE__ */
89
90#define CPU_ENABLE_ROBUST_THREAD_DISPATCH FALSE
91
92#define CPU_STACK_GROWS_UP               FALSE
93
94/* FIXME: The Pentium 4 used 128 bytes, it this processor still relevant? */
95#define CPU_CACHE_LINE_BYTES 64
96
97#define CPU_STRUCTURE_ALIGNMENT
98
99/*
100 *  Does this port provide a CPU dependent IDLE task implementation?
101 *
102 *  If TRUE, then the routine _CPU_Thread_Idle_body
103 *  must be provided and is the default IDLE thread body instead of
104 *  _CPU_Thread_Idle_body.
105 *
106 *  If FALSE, then use the generic IDLE thread body if the BSP does
107 *  not provide one.
108 */
109
110#define CPU_PROVIDES_IDLE_THREAD_BODY    FALSE
111
112#define CPU_MAXIMUM_PROCESSORS 32
113
114#define I386_CONTEXT_CONTROL_EFLAGS_OFFSET 0
115#define I386_CONTEXT_CONTROL_ESP_OFFSET 4
116#define I386_CONTEXT_CONTROL_EBP_OFFSET 8
117#define I386_CONTEXT_CONTROL_EBX_OFFSET 12
118#define I386_CONTEXT_CONTROL_ESI_OFFSET 16
119#define I386_CONTEXT_CONTROL_EDI_OFFSET 20
120#define I386_CONTEXT_CONTROL_GS_0_OFFSET 24
121#define I386_CONTEXT_CONTROL_GS_1_OFFSET 28
122
123#ifdef RTEMS_SMP
124  #define I386_CONTEXT_CONTROL_IS_EXECUTING_OFFSET 32
125#endif
126
127/* structures */
128
129#ifndef ASM
130
131/*
132 *  Basic integer context for the i386 family.
133 */
134
135typedef struct {
136  uint32_t    eflags;     /* extended flags register                   */
137  void       *esp;        /* extended stack pointer register           */
138  void       *ebp;        /* extended base pointer register            */
139  uint32_t    ebx;        /* extended bx register                      */
140  uint32_t    esi;        /* extended source index register            */
141  uint32_t    edi;        /* extended destination index flags register */
142  segment_descriptors gs; /* gs segment descriptor                     */
143#ifdef RTEMS_SMP
144  volatile bool is_executing;
145#endif
146}   Context_Control;
147
148#define _CPU_Context_Get_SP( _context ) \
149  (_context)->esp
150
151#ifdef RTEMS_SMP
152  static inline bool _CPU_Context_Get_is_executing(
153    const Context_Control *context
154  )
155  {
156    return context->is_executing;
157  }
158
159  static inline void _CPU_Context_Set_is_executing(
160    Context_Control *context,
161    bool is_executing
162  )
163  {
164    context->is_executing = is_executing;
165  }
166#endif
167
168/*
169 *  FP context save area for the i387 numeric coprocessors.
170 */
171#ifdef __SSE__
172/* All FPU and SSE registers are volatile; hence, as long
173 * as we are within normally executing C code (including
174 * a task switch) there is no need for saving/restoring
175 * any of those registers.
176 * We must save/restore the full FPU/SSE context across
177 * interrupts and exceptions, however:
178 *   -  after ISR execution a _Thread_Dispatch() may happen
179 *      and it is therefore necessary to save the FPU/SSE
180 *      registers to be restored when control is returned
181 *      to the interrupted task.
182 *   -  gcc may implicitly use FPU/SSE instructions in
183 *      an ISR.
184 *
185 * Even though there is no explicit mentioning of the FPU
186 * control word in the SYSV ABI (i386) being non-volatile
187 * we maintain MXCSR and the FPU control-word for each task.
188 */
189typedef struct {
190        uint32_t  mxcsr;
191        uint16_t  fpucw;
192} Context_Control_fp;
193
194#else
195
196typedef struct {
197  uint8_t     fp_save_area[108];    /* context size area for I80387 */
198                                    /*  28 bytes for environment    */
199} Context_Control_fp;
200
201#endif
202
203
204/*
205 *  The following structure defines the set of information saved
206 *  on the current stack by RTEMS upon receipt of execptions.
207 *
208 * idtIndex is either the interrupt number or the trap/exception number.
209 * faultCode is the code pushed by the processor on some exceptions.
210 *
211 * Since the first registers are directly pushed by the CPU they
212 * may not respect 16-byte stack alignment, which is, however,
213 * mandatory for the SSE register area.
214 * Therefore, these registers are stored at an aligned address
215 * and a pointer is stored in the CPU_Exception_frame.
216 * If the executive was compiled without SSE support then
217 * this pointer is NULL.
218 */
219
220struct Context_Control_sse;
221
222typedef struct {
223  struct Context_Control_sse *fp_ctxt;
224  uint32_t    edi;
225  uint32_t    esi;
226  uint32_t    ebp;
227  uint32_t    esp0;
228  uint32_t    ebx;
229  uint32_t    edx;
230  uint32_t    ecx;
231  uint32_t    eax;
232  uint32_t    idtIndex;
233  uint32_t    faultCode;
234  uint32_t    eip;
235  uint32_t    cs;
236  uint32_t    eflags;
237} CPU_Exception_frame;
238
239#ifdef __SSE__
240typedef struct Context_Control_sse {
241  uint16_t  fcw;
242  uint16_t  fsw;
243  uint8_t   ftw;
244  uint8_t   res_1;
245  uint16_t  fop;
246  uint32_t  fpu_ip;
247  uint16_t  cs;
248  uint16_t  res_2;
249  uint32_t  fpu_dp;
250  uint16_t  ds;
251  uint16_t  res_3;
252  uint32_t  mxcsr;
253  uint32_t  mxcsr_mask;
254  struct {
255        uint8_t fpreg[10];
256        uint8_t res_4[ 6];
257  } fp_mmregs[8];
258  uint8_t   xmmregs[8][16];
259  uint8_t   res_5[224];
260} Context_Control_sse
261__attribute__((aligned(16)))
262;
263#endif
264
265typedef void (*cpuExcHandlerType) (CPU_Exception_frame*);
266extern cpuExcHandlerType _currentExcHandler;
267extern void rtems_exception_init_mngt(void);
268
269#ifdef RTEMS_SMP
270  /* Throw compile-time error to indicate incomplete support */
271  #error "i386 targets do not support SMP.\
272 See: https://devel.rtems.org/ticket/3335"
273
274  /*
275   * This size must match the size of the CPU_Interrupt_frame, which must be
276   * used in the SMP context switch code, which is incomplete at the moment.
277   */
278  #define CPU_INTERRUPT_FRAME_SIZE 4
279#endif
280
281/*
282 * This port does not pass any frame info to the
283 * interrupt handler.
284 */
285
286typedef struct {
287  uint32_t todo_replace_with_apt_registers;
288} CPU_Interrupt_frame;
289
290typedef enum {
291  I386_EXCEPTION_DIVIDE_BY_ZERO      = 0,
292  I386_EXCEPTION_DEBUG               = 1,
293  I386_EXCEPTION_NMI                 = 2,
294  I386_EXCEPTION_BREAKPOINT          = 3,
295  I386_EXCEPTION_OVERFLOW            = 4,
296  I386_EXCEPTION_BOUND               = 5,
297  I386_EXCEPTION_ILLEGAL_INSTR       = 6,
298  I386_EXCEPTION_MATH_COPROC_UNAVAIL = 7,
299  I386_EXCEPTION_DOUBLE_FAULT        = 8,
300  I386_EXCEPTION_I386_COPROC_SEG_ERR = 9,
301  I386_EXCEPTION_INVALID_TSS         = 10,
302  I386_EXCEPTION_SEGMENT_NOT_PRESENT = 11,
303  I386_EXCEPTION_STACK_SEGMENT_FAULT = 12,
304  I386_EXCEPTION_GENERAL_PROT_ERR    = 13,
305  I386_EXCEPTION_PAGE_FAULT          = 14,
306  I386_EXCEPTION_INTEL_RES15         = 15,
307  I386_EXCEPTION_FLOAT_ERROR         = 16,
308  I386_EXCEPTION_ALIGN_CHECK         = 17,
309  I386_EXCEPTION_MACHINE_CHECK       = 18,
310  I386_EXCEPTION_ENTER_RDBG          = 50     /* to enter manually RDBG */
311
312} Intel_symbolic_exception_name;
313
314
315/*
316 *  context size area for floating point
317 *
318 *  NOTE:  This is out of place on the i386 to avoid a forward reference.
319 */
320
321#define CPU_CONTEXT_FP_SIZE sizeof( Context_Control_fp )
322
323/* variables */
324
325extern Context_Control_fp _CPU_Null_fp_context;
326
327#endif /* ASM */
328
329/* constants */
330
331/*
332 *  This defines the number of levels and the mask used to pick those
333 *  bits out of a thread mode.
334 */
335
336#define CPU_MODES_INTERRUPT_MASK   0x00000001 /* interrupt level in mode */
337
338/*
339 *  extra stack required by the MPCI receive server thread
340 */
341
342#define CPU_MPCI_RECEIVE_SERVER_EXTRA_STACK 1024
343
344/*
345 *  This is defined if the port has a special way to report the ISR nesting
346 *  level.  Most ports maintain the variable _ISR_Nest_level.
347 */
348
349#define CPU_PROVIDES_ISR_IS_IN_PROGRESS FALSE
350
351/*
352 *  Minimum size of a thread's stack.
353 */
354
355#define CPU_STACK_MINIMUM_SIZE          4096
356
357#define CPU_SIZEOF_POINTER 4
358
359/*
360 *  i386 is pretty tolerant of alignment.  Just put things on 4 byte boundaries.
361 */
362
363#define CPU_ALIGNMENT                    4
364#define CPU_HEAP_ALIGNMENT               CPU_ALIGNMENT
365#define CPU_PARTITION_ALIGNMENT          CPU_ALIGNMENT
366
367/*
368 *  On i386 thread stacks require no further alignment after allocation
369 *  from the Workspace. However, since gcc maintains 16-byte alignment
370 *  we try to respect that. If you find an option to let gcc squeeze
371 *  the stack more tightly then setting CPU_STACK_ALIGNMENT to 16 still
372 *  doesn't waste much space since this only determines the *initial*
373 *  alignment.
374 */
375
376#define CPU_STACK_ALIGNMENT             16
377
378#define CPU_INTERRUPT_STACK_ALIGNMENT CPU_CACHE_LINE_BYTES
379
380/* macros */
381
382#ifndef ASM
383/*
384 *  ISR handler macros
385 *
386 *  These macros perform the following functions:
387 *     + initialize the RTEMS vector table
388 *     + disable all maskable CPU interrupts
389 *     + restore previous interrupt level (enable)
390 *     + temporarily restore interrupts (flash)
391 *     + set a particular level
392 */
393
394#if !defined(I386_DISABLE_INLINE_ISR_DISABLE_ENABLE)
395#define _CPU_ISR_Disable( _level ) i386_disable_interrupts( _level )
396
397#define _CPU_ISR_Enable( _level )  i386_enable_interrupts( _level )
398
399#define _CPU_ISR_Flash( _level )   i386_flash_interrupts( _level )
400
401#define _CPU_ISR_Set_level( _new_level ) \
402  { \
403    if ( _new_level ) __asm__ volatile ( "cli" ); \
404    else              __asm__ volatile ( "sti" ); \
405  }
406#else
407#define _CPU_ISR_Disable( _level ) _level = i386_disable_interrupts()
408#define _CPU_ISR_Enable( _level ) i386_enable_interrupts( _level )
409#define _CPU_ISR_Flash( _level ) i386_flash_interrupts( _level )
410#define _CPU_ISR_Set_level( _new_level ) i386_set_interrupt_level(_new_level)
411#endif
412
413RTEMS_INLINE_ROUTINE bool _CPU_ISR_Is_enabled( uint32_t level )
414{
415  return ( level & EFLAGS_INTR_ENABLE ) != 0;
416}
417
418uint32_t   _CPU_ISR_Get_level( void );
419
420/*  Make sure interrupt stack has space for ISR
421 *  'vector' arg at the top and that it is aligned
422 *  properly.
423 */
424
425#define _CPU_Interrupt_stack_setup( _lo, _hi )  \
426        do {                                        \
427                _hi = (void*)(((uintptr_t)(_hi) - 4) & ~ (CPU_STACK_ALIGNMENT - 1)); \
428        } while (0)
429
430#endif /* ASM */
431
432/* end of ISR handler macros */
433
434/*
435 *  Context handler macros
436 *
437 *  These macros perform the following functions:
438 *     + initialize a context area
439 *     + restart the current thread
440 *     + calculate the initial pointer into a FP context area
441 *     + initialize an FP context area
442 */
443
444#define CPU_EFLAGS_INTERRUPTS_ON  0x00003202
445#define CPU_EFLAGS_INTERRUPTS_OFF 0x00003002
446
447#ifndef ASM
448
449void _CPU_Context_Initialize(
450  Context_Control *the_context,
451  void *stack_area_begin,
452  size_t stack_area_size,
453  uint32_t new_level,
454  void (*entry_point)( void ),
455  bool is_fp,
456  void *tls_area
457);
458
459#define _CPU_Context_Restart_self( _the_context ) \
460   _CPU_Context_restore( (_the_context) );
461
462#if defined(RTEMS_SMP)
463  uint32_t _CPU_SMP_Initialize( void );
464
465  bool _CPU_SMP_Start_processor( uint32_t cpu_index );
466
467  void _CPU_SMP_Finalize_initialization( uint32_t cpu_count );
468
469  void _CPU_SMP_Prepare_start_multitasking( void );
470
471  uint32_t _CPU_SMP_Get_current_processor( void );
472
473  void _CPU_SMP_Send_interrupt( uint32_t target_processor_index );
474
475  static inline void _CPU_SMP_Processor_event_broadcast( void )
476  {
477    __asm__ volatile ( "" : : : "memory" );
478  }
479
480  static inline void _CPU_SMP_Processor_event_receive( void )
481  {
482    __asm__ volatile ( "" : : : "memory" );
483  }
484#endif
485
486#define _CPU_Context_Initialize_fp( _fp_area ) \
487  { \
488    memcpy( *_fp_area, &_CPU_Null_fp_context, CPU_CONTEXT_FP_SIZE ); \
489  }
490
491/* end of Context handler macros */
492
493/*
494 *  Fatal Error manager macros
495 *
496 *  These macros perform the following functions:
497 *    + disable interrupts and halt the CPU
498 */
499
500extern void _CPU_Fatal_halt(uint32_t source, uint32_t error)
501  RTEMS_NO_RETURN;
502
503#endif /* ASM */
504
505/* end of Fatal Error manager macros */
506
507/*
508 *  Bitfield handler macros
509 *
510 *  These macros perform the following functions:
511 *     + scan for the highest numbered (MSB) set in a 16 bit bitfield
512 */
513
514#define CPU_USE_GENERIC_BITFIELD_CODE FALSE
515
516#define _CPU_Bitfield_Find_first_bit( _value, _output ) \
517  { \
518    uint16_t __value_in_register = ( _value ); \
519    uint16_t __output = 0; \
520    __asm__ volatile ( "bsfw    %0,%1 " \
521                    : "=r" ( __value_in_register ), "=r" ( __output ) \
522                    : "0"  ( __value_in_register ), "1"  ( __output ) \
523    ); \
524    ( _output ) = __output; \
525  }
526
527/* end of Bitfield handler macros */
528
529/*
530 *  Priority handler macros
531 *
532 *  These macros perform the following functions:
533 *    + return a mask with the bit for this major/minor portion of
534 *      of thread priority set.
535 *    + translate the bit number returned by "Bitfield_find_first_bit"
536 *      into an index into the thread ready chain bit maps
537 */
538
539#define _CPU_Priority_Mask( _bit_number ) \
540  ( 1 << (_bit_number) )
541
542#define _CPU_Priority_bits_index( _priority ) \
543  (_priority)
544
545/* functions */
546
547#ifndef ASM
548/*
549 *  _CPU_Initialize
550 *
551 *  This routine performs CPU dependent initialization.
552 */
553
554void _CPU_Initialize(void);
555
556/*
557 *  _CPU_ISR_install_raw_handler
558 *
559 *  This routine installs a "raw" interrupt handler directly into the
560 *  processor's vector table.
561 */
562
563void _CPU_ISR_install_raw_handler(
564  uint32_t    vector,
565  proc_ptr    new_handler,
566  proc_ptr   *old_handler
567);
568
569/*
570 *  _CPU_ISR_install_vector
571 *
572 *  This routine installs an interrupt vector.
573 */
574
575void _CPU_ISR_install_vector(
576  uint32_t    vector,
577  proc_ptr    new_handler,
578  proc_ptr   *old_handler
579);
580
581/*
582 *  _CPU_Thread_Idle_body
583 *
584 *  Use the halt instruction of low power mode of a particular i386 model.
585 */
586
587#if (CPU_PROVIDES_IDLE_THREAD_BODY == TRUE)
588
589void *_CPU_Thread_Idle_body( uintptr_t ignored );
590
591#endif /* CPU_PROVIDES_IDLE_THREAD_BODY */
592
593/*
594 *  _CPU_Context_switch
595 *
596 *  This routine switches from the run context to the heir context.
597 */
598
599void _CPU_Context_switch(
600  Context_Control  *run,
601  Context_Control  *heir
602);
603
604/*
605 *  _CPU_Context_restore
606 *
607 *  This routine is generally used only to restart self in an
608 *  efficient manner and avoid stack conflicts.
609 */
610
611void _CPU_Context_restore(
612  Context_Control *new_context
613) RTEMS_NO_RETURN;
614
615/*
616 *  _CPU_Context_save_fp
617 *
618 *  This routine saves the floating point context passed to it.
619 */
620
621#ifdef __SSE__
622#define _CPU_Context_save_fp(fp_context_pp) \
623  do {                                      \
624    __asm__ __volatile__(                   \
625      "fstcw %0"                            \
626      :"=m"((*(fp_context_pp))->fpucw)      \
627    );                                      \
628        __asm__ __volatile__(                   \
629      "stmxcsr %0"                          \
630      :"=m"((*(fp_context_pp))->mxcsr)      \
631    );                                      \
632  } while (0)
633#else
634void _CPU_Context_save_fp(
635  Context_Control_fp **fp_context_ptr
636);
637#endif
638
639/*
640 *  _CPU_Context_restore_fp
641 *
642 *  This routine restores the floating point context passed to it.
643 */
644#ifdef __SSE__
645#define _CPU_Context_restore_fp(fp_context_pp) \
646  do {                                         \
647    __asm__ __volatile__(                      \
648      "fldcw %0"                               \
649      ::"m"((*(fp_context_pp))->fpucw)         \
650      :"fpcr"                                  \
651    );                                         \
652    __builtin_ia32_ldmxcsr(_Thread_Executing->fp_context->mxcsr);  \
653  } while (0)
654#else
655void _CPU_Context_restore_fp(
656  Context_Control_fp **fp_context_ptr
657);
658#endif
659
660#ifdef __SSE__
661#define _CPU_Context_Initialization_at_thread_begin() \
662  do {                                                \
663    __asm__ __volatile__(                             \
664      "finit"                                         \
665      :                                               \
666      :                                               \
667      :"st","st(1)","st(2)","st(3)",                  \
668       "st(4)","st(5)","st(6)","st(7)",               \
669       "fpsr","fpcr"                                  \
670    );                                                \
671        if ( _Thread_Executing->fp_context ) {            \
672          _CPU_Context_restore_fp(&_Thread_Executing->fp_context); \
673   }                                                  \
674  } while (0)
675#endif
676
677void _CPU_Exception_frame_print( const CPU_Exception_frame *frame );
678
679typedef uint32_t CPU_Counter_ticks;
680
681uint32_t _CPU_Counter_frequency( void );
682
683CPU_Counter_ticks _CPU_Counter_read( void );
684
685static inline CPU_Counter_ticks _CPU_Counter_difference(
686  CPU_Counter_ticks second,
687  CPU_Counter_ticks first
688)
689{
690  return second - first;
691}
692
693/** Type that can store a 32-bit integer or a pointer. */
694typedef uintptr_t CPU_Uint32ptr;
695
696#endif /* ASM */
697
698#ifdef __cplusplus
699}
700#endif
701
702#endif
Note: See TracBrowser for help on using the repository browser.