RTEMS  5.0.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
cpu.h
Go to the documentation of this file.
1 
9 /*
10  * Copyright (c) 2018.
11  * Amaan Cheval <amaan.cheval@gmail.com>
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef _RTEMS_SCORE_CPU_H
36 #define _RTEMS_SCORE_CPU_H
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #include <rtems/score/basedefs.h>
43 #include <rtems/score/cpu_asm.h>
44 #include <rtems/score/x86_64.h>
45 
46 #define CPU_SIMPLE_VECTORED_INTERRUPTS FALSE
47 #define CPU_ISR_PASSES_FRAME_POINTER FALSE
48 // XXX: Enable FPU support
49 #define CPU_HARDWARE_FP FALSE
50 #define CPU_SOFTWARE_FP FALSE
51 #define CPU_ALL_TASKS_ARE_FP FALSE
52 #define CPU_IDLE_TASK_IS_FP FALSE
53 #define CPU_USE_DEFERRED_FP_SWITCH TRUE
54 #define CPU_ENABLE_ROBUST_THREAD_DISPATCH FALSE
55 #define CPU_STACK_GROWS_UP FALSE
56 
57 #define CPU_STRUCTURE_ALIGNMENT RTEMS_ALIGNED(64)
58 #define CPU_CACHE_LINE_BYTES 64
59 #define CPU_MODES_INTERRUPT_MASK 0x00000001
60 #define CPU_MAXIMUM_PROCESSORS 32
61 
62 #define CPU_EFLAGS_INTERRUPTS_ON 0x00003202
63 #define CPU_EFLAGS_INTERRUPTS_OFF 0x00003002
64 
65 #ifndef ASM
66 
67 typedef struct {
68  uint64_t rflags;
69 
74  uint64_t rbx;
75  void *rsp;
76  void *rbp;
77  uint64_t r12;
78  uint64_t r13;
79  uint64_t r14;
80  uint64_t r15;
81 
82  // XXX: FS segment descriptor for TLS
83 
84 #ifdef RTEMS_SMP
85  volatile bool is_executing;
86 #endif
88 
89 #define _CPU_Context_Get_SP( _context ) \
90  (_context)->rsp
91 
92 typedef struct {
93  /* XXX: MMX, XMM, others?
94  *
95  * All x87 registers are caller-saved, so callees that make use of the MMX
96  * registers may use the faster femms instruction
97  */
98 
100  double some_float_register;
102 
103 /*
104  * Caller-saved registers for interrupt frames
105  */
106 typedef struct {
115  uint64_t rax;
116  uint64_t rcx;
117  uint64_t rdx;
118  uint64_t rsi;
119  uint64_t r8;
120  uint64_t r9;
121  uint64_t r10;
122  uint64_t r11;
123 
124  /*
125  * This holds the rsp just before _ISR_Handler is called; it's needed because
126  * in the handler, we align the stack to make further calls, and we're not
127  * sure how alignment may move the stack-pointer around, leaving no way to get
128  * back to the stack, and therefore the interrupt frame.
129  */
130  uint64_t saved_rsp;
131 
132  /* XXX:
133  * - FS segment selector for TLS
134  * - x87 status word?
135  * - MMX?
136  * - XMM?
137  */
139 
140 #endif /* !ASM */
141 
142 #define CPU_INTERRUPT_FRAME_SIZE 72
143 
144 /*
145  * When SMP is enabled, percpuasm.c has a similar assert, but since we use the
146  * interrupt frame regardless of SMP, we'll confirm it here.
147  */
148 #ifndef ASM
149  RTEMS_STATIC_ASSERT(
152  );
153 #endif
154 
155 #define CPU_CONTEXT_FP_SIZE sizeof( Context_Control_fp )
156 #define CPU_MPCI_RECEIVE_SERVER_EXTRA_STACK 0
157 #define CPU_PROVIDES_ISR_IS_IN_PROGRESS FALSE
158 #define CPU_STACK_MINIMUM_SIZE (1024*4)
159 #define CPU_SIZEOF_POINTER 8
160 #define CPU_ALIGNMENT 8
161 #define CPU_HEAP_ALIGNMENT CPU_ALIGNMENT
162 #define CPU_STACK_ALIGNMENT 16
163 #define CPU_INTERRUPT_STACK_ALIGNMENT CPU_CACHE_LINE_BYTES
164 
165 /*
166  * ISR handler macros
167  */
168 
169 #ifndef ASM
170 
171 #define _CPU_Initialize_vectors()
172 
173 #define _CPU_ISR_Enable(_level) \
174 { \
175  amd64_enable_interrupts(); \
176  _level = 0; \
177  (void) _level; /* Prevent -Wunused-but-set-variable */ \
178 }
179 
180 #define _CPU_ISR_Disable(_level) \
181 { \
182  amd64_enable_interrupts(); \
183  _level = 1; \
184  (void) _level; /* Prevent -Wunused-but-set-variable */ \
185 }
186 
187 #define _CPU_ISR_Flash(_level) \
188 { \
189  amd64_enable_interrupts(); \
190  amd64_disable_interrupts(); \
191  _level = 1; \
192  (void) _level; /* Prevent -Wunused-but-set-variable */ \
193 }
194 
196 {
197  return (level & EFLAGS_INTR_ENABLE) != 0;
198 }
199 
200 RTEMS_INLINE_ROUTINE void _CPU_ISR_Set_level(uint32_t new_level)
201 {
202  if ( new_level ) {
203  amd64_disable_interrupts();
204  }
205  else {
206  amd64_enable_interrupts();
207  }
208 }
209 
211 {
212  uint64_t rflags;
213 
214  __asm__ volatile ( "pushf; \
215  popq %0"
216  : "=rm" (rflags)
217  );
218 
219  uint32_t level = (rflags & EFLAGS_INTR_ENABLE) ? 0 : 1;
220  return level;
221 }
222 
223 /* end of ISR handler macros */
224 
225 /* Context handler macros */
226 #define _CPU_Context_Destroy( _the_thread, _the_context ) \
227  { \
228  }
229 
231  Context_Control *the_context,
232  void *stack_area_begin,
233  size_t stack_area_size,
234  uint32_t new_level,
235  void (*entry_point)( void ),
236  bool is_fp,
237  void *tls_area
238 );
239 
240 #define _CPU_Context_Restart_self( _the_context ) \
241  _CPU_Context_restore( (_the_context) );
242 
243 #define _CPU_Context_Initialize_fp( _destination ) \
244  { \
245  *(*(_destination)) = _CPU_Null_fp_context; \
246  }
247 
248 /* end of Context handler macros */
249 
250 /* Fatal Error manager macros */
251 
252 #define _CPU_Fatal_halt( _source, _error ) \
253  { \
254  }
255 
256 /* end of Fatal Error manager macros */
257 
258 /* Bitfield handler macros */
259 
260 #define CPU_USE_GENERIC_BITFIELD_CODE TRUE
261 
262 #if (CPU_USE_GENERIC_BITFIELD_CODE == FALSE)
263 #define _CPU_Bitfield_Find_first_bit( _value, _output ) \
264  { \
265  (_output) = 0; /* do something to prevent warnings */ \
266  }
267 #endif
268 
269 /* end of Bitfield handler macros */
270 
271 #if (CPU_USE_GENERIC_BITFIELD_CODE == FALSE)
272 #define _CPU_Priority_Mask( _bit_number ) \
273  ( 1 << (_bit_number) )
274 #endif
275 
276 #if (CPU_USE_GENERIC_BITFIELD_CODE == FALSE)
277 #define _CPU_Priority_bits_index( _priority ) \
278  (_priority)
279 #endif
280 
281 /* end of Priority handler macros */
282 
283 /* functions */
284 
285 void _CPU_Initialize(void);
286 
287 void *_CPU_Thread_Idle_body( uintptr_t ignored );
288 
290  Context_Control *run,
291  Context_Control *heir
292 );
293 
295  Context_Control *new_context
297 
299  Context_Control_fp **fp_context_ptr
300 );
301 
303  Context_Control_fp **fp_context_ptr
304 );
305 
306 typedef struct {
307  uint32_t processor_state_register;
308  uint32_t integer_registers [1];
309  double float_registers [1];
311 
313 
314 static inline uint32_t CPU_swap_u32(
315  uint32_t value
316 )
317 {
318  uint32_t byte1, byte2, byte3, byte4, swapped;
319 
320  byte4 = (value >> 24) & 0xff;
321  byte3 = (value >> 16) & 0xff;
322  byte2 = (value >> 8) & 0xff;
323  byte1 = value & 0xff;
324 
325  swapped = (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4;
326  return swapped;
327 }
328 
329 #define CPU_swap_u16( value ) \
330  (((value&0xff) << 8) | ((value >> 8)&0xff))
331 
332 typedef uint32_t CPU_Counter_ticks;
333 
334 uint32_t _CPU_Counter_frequency( void );
335 
336 CPU_Counter_ticks _CPU_Counter_read( void );
337 
338 
339 static inline CPU_Counter_ticks _CPU_Counter_difference(
340  CPU_Counter_ticks second,
341  CPU_Counter_ticks first
342 )
343 {
344  return second - first;
345 }
346 
347 #ifdef RTEMS_SMP
348  *
349  uint32_t _CPU_SMP_Initialize( void );
350 
351  bool _CPU_SMP_Start_processor( uint32_t cpu_index );
352 
353  void _CPU_SMP_Finalize_initialization( uint32_t cpu_count );
354 
355  void _CPU_SMP_Prepare_start_multitasking( void );
356 
357  static inline uint32_t _CPU_SMP_Get_current_processor( void )
358  {
359  return 123;
360  }
361 
362  void _CPU_SMP_Send_interrupt( uint32_t target_processor_index );
363 
364  static inline void _CPU_SMP_Processor_event_broadcast( void )
365  {
366  __asm__ volatile ( "" : : : "memory" );
367  }
368 
369  static inline void _CPU_SMP_Processor_event_receive( void )
370  {
371  __asm__ volatile ( "" : : : "memory" );
372  }
373 
374  static inline bool _CPU_Context_Get_is_executing(
375  const Context_Control *context
376  )
377  return context->is_executing;
378  }
379 
380  static inline void _CPU_Context_Set_is_executing(
382  bool is_executing
383  )
384  {
385  }
386 
387 #endif /* RTEMS_SMP */
388 
389 typedef uintptr_t CPU_Uint32ptr;
390 
391 #ifdef __cplusplus
392 }
393 #endif
394 
395 #endif /* ASM */
396 
397 #endif /* _RTEMS_SCORE_CPU_H */
void _CPU_Exception_frame_print(const CPU_Exception_frame *frame)
Prints the exception frame via printk().
Definition: vectorexceptions.c:45
void _CPU_ISR_Set_level(uint32_t level)
Sets the hardware interrupt level by the level value.
Definition: cpu.c:57
CPU_Counter_ticks _CPU_Counter_read(void)
Returns the current CPU counter value.
Definition: system-clocks.c:117
Thread register context.
Definition: cpu.h:196
void * _CPU_Thread_Idle_body(uintptr_t ignored)
Definition: idle-mcf5272.c:20
#define RTEMS_INLINE_ROUTINE
Definition: basedefs.h:65
void _CPU_Context_restore_fp(Context_Control_fp **fp_context_ptr)
Definition: cpu.c:207
Interrupt stack frame (ISF).
Definition: cpu.h:306
void _CPU_Context_Initialize(Context_Control *context, void *stack_area_begin, size_t stack_area_size, uint32_t new_level, void(*entry_point)(void), bool is_fp, void *tls_area)
Initializes the CPU context.
Definition: epiphany-context-initialize.c:40
void _CPU_Context_switch(Context_Control *run, Context_Control *heir)
CPU switch context.
Definition: cpu_asm.c:91
void _CPU_Initialize(void)
CPU initialization.
Definition: cpu.c:45
SPARC basic context.
Definition: cpu.h:242
register struct Per_CPU_Control *_SPARC_Per_CPU_current __asm__("g6")
The pointer to the current per-CPU control is available via register g6.
uint64_t rax
Definition: cpu.h:115
uint32_t CPU_Counter_ticks
Unsigned integer type for CPU counter values.
Definition: cpu.h:1202
#define RTEMS_NO_RETURN
Definition: basedefs.h:101
uint32_t _CPU_ISR_Get_level(void)
Definition: cpu.c:88
unsigned context
Definition: tlb.h:108
bool _CPU_ISR_Is_enabled(uint32_t level)
Returns true if interrupts are enabled in the specified ISR level, otherwise returns false...
Definition: cpu.h:381
void _CPU_Context_restore(Context_Control *new_context) RTEMS_NO_RETURN
Definition: cpu_asm.c:111
uintptr_t CPU_Uint32ptr
Definition: cpu.h:668
uint32_t _CPU_Counter_frequency(void)
Returns the current CPU counter frequency in Hz.
Definition: system-clocks.c:112
Basic Definitions.
void _CPU_Context_save_fp(Context_Control_fp **fp_context_ptr)
Definition: cpu.c:198
The set of registers that specifies the complete processor state.
Definition: cpu.h:635
uint64_t rbx
Definition: cpu.h:74
#define CPU_INTERRUPT_FRAME_SIZE
Definition: cpuimpl.h:78