source: rtems/cpukit/score/cpu/sparc/cpu.c @ 76030c7

4.115
Last change on this file since 76030c7 was 76030c7, checked in by Sebastian Huber <sebastian.huber@…>, on 05/26/15 at 12:13:44

sparc: Add static assertion

  • Property mode set to 100644
File size: 10.3 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief SPARC CPU Dependent Source
5 */
6
7/*
8 *  COPYRIGHT (c) 1989-2007.
9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  The license and distribution terms for this file may be
12 *  found in the file LICENSE in this distribution or at
13 *  http://www.rtems.org/license/LICENSE.
14 */
15
16#ifdef HAVE_CONFIG_H
17#include "config.h"
18#endif
19
20#include <rtems/system.h>
21#include <rtems/score/isr.h>
22#include <rtems/score/percpu.h>
23#include <rtems/score/tls.h>
24#include <rtems/rtems/cache.h>
25
26RTEMS_STATIC_ASSERT(
27  offsetof( Per_CPU_Control, cpu_per_cpu.isr_dispatch_disable)
28    == SPARC_PER_CPU_ISR_DISPATCH_DISABLE,
29  SPARC_PER_CPU_ISR_DISPATCH_DISABLE
30);
31
32#define SPARC_ASSERT_OFFSET(field, off) \
33  RTEMS_STATIC_ASSERT( \
34    offsetof(Context_Control, field) == off ## _OFFSET, \
35    Context_Control_offset_ ## field \
36  )
37
38SPARC_ASSERT_OFFSET(g5, G5);
39SPARC_ASSERT_OFFSET(g7, G7);
40
41RTEMS_STATIC_ASSERT(
42  offsetof(Context_Control, l0_and_l1) == L0_OFFSET,
43  Context_Control_offset_L0
44);
45
46RTEMS_STATIC_ASSERT(
47  offsetof(Context_Control, l0_and_l1) + 4 == L1_OFFSET,
48  Context_Control_offset_L1
49);
50
51SPARC_ASSERT_OFFSET(l2, L2);
52SPARC_ASSERT_OFFSET(l3, L3);
53SPARC_ASSERT_OFFSET(l4, L4);
54SPARC_ASSERT_OFFSET(l5, L5);
55SPARC_ASSERT_OFFSET(l6, L6);
56SPARC_ASSERT_OFFSET(l7, L7);
57SPARC_ASSERT_OFFSET(i0, I0);
58SPARC_ASSERT_OFFSET(i1, I1);
59SPARC_ASSERT_OFFSET(i2, I2);
60SPARC_ASSERT_OFFSET(i3, I3);
61SPARC_ASSERT_OFFSET(i4, I4);
62SPARC_ASSERT_OFFSET(i5, I5);
63SPARC_ASSERT_OFFSET(i6_fp, I6_FP);
64SPARC_ASSERT_OFFSET(i7, I7);
65SPARC_ASSERT_OFFSET(o6_sp, O6_SP);
66SPARC_ASSERT_OFFSET(o7, O7);
67SPARC_ASSERT_OFFSET(psr, PSR);
68SPARC_ASSERT_OFFSET(isr_dispatch_disable, ISR_DISPATCH_DISABLE_STACK);
69
70#if defined(RTEMS_SMP)
71SPARC_ASSERT_OFFSET(is_executing, SPARC_CONTEXT_CONTROL_IS_EXECUTING);
72#endif
73
74#define SPARC_ASSERT_ISF_OFFSET(field, off) \
75  RTEMS_STATIC_ASSERT( \
76    offsetof(CPU_Interrupt_frame, field) == ISF_ ## off ## _OFFSET, \
77    CPU_Interrupt_frame_offset_ ## field \
78  )
79
80SPARC_ASSERT_ISF_OFFSET(psr, PSR);
81SPARC_ASSERT_ISF_OFFSET(pc, PC);
82SPARC_ASSERT_ISF_OFFSET(npc, NPC);
83SPARC_ASSERT_ISF_OFFSET(g1, G1);
84SPARC_ASSERT_ISF_OFFSET(g2, G2);
85SPARC_ASSERT_ISF_OFFSET(g3, G3);
86SPARC_ASSERT_ISF_OFFSET(g4, G4);
87SPARC_ASSERT_ISF_OFFSET(g5, G5);
88SPARC_ASSERT_ISF_OFFSET(g7, G7);
89SPARC_ASSERT_ISF_OFFSET(i0, I0);
90SPARC_ASSERT_ISF_OFFSET(i1, I1);
91SPARC_ASSERT_ISF_OFFSET(i2, I2);
92SPARC_ASSERT_ISF_OFFSET(i3, I3);
93SPARC_ASSERT_ISF_OFFSET(i4, I4);
94SPARC_ASSERT_ISF_OFFSET(i5, I5);
95SPARC_ASSERT_ISF_OFFSET(i6_fp, I6_FP);
96SPARC_ASSERT_ISF_OFFSET(i7, I7);
97SPARC_ASSERT_ISF_OFFSET(y, Y);
98SPARC_ASSERT_ISF_OFFSET(tpc, TPC);
99
100RTEMS_STATIC_ASSERT(
101  sizeof(CPU_Interrupt_frame) == CONTEXT_CONTROL_INTERRUPT_FRAME_SIZE,
102  CPU_Interrupt_frame_size
103);
104
105/* https://devel.rtems.org/ticket/2352 */
106RTEMS_STATIC_ASSERT(
107  sizeof(CPU_Interrupt_frame) % CPU_ALIGNMENT == 0,
108  CPU_Interrupt_frame_alignment
109);
110
111/*
112 *  _CPU_Initialize
113 *
114 *  This routine performs processor dependent initialization.
115 *
116 *  INPUT PARAMETERS: NONE
117 *
118 *  Output Parameters: NONE
119 *
120 *  NOTE: There is no need to save the pointer to the thread dispatch routine.
121 *        The SPARC's assembly code can reference it directly with no problems.
122 */
123
124void _CPU_Initialize(void)
125{
126#if (SPARC_HAS_FPU == 1)
127  Context_Control_fp *pointer;
128
129  /*
130   *  This seems to be the most appropriate way to obtain an initial
131   *  FP context on the SPARC.  The NULL fp context is copied it to
132   *  the task's FP context during Context_Initialize.
133   */
134
135  pointer = &_CPU_Null_fp_context;
136  _CPU_Context_save_fp( &pointer );
137#endif
138}
139
140uint32_t   _CPU_ISR_Get_level( void )
141{
142  uint32_t   level;
143
144  sparc_get_interrupt_level( level );
145
146  return level;
147}
148
149/*
150 *  _CPU_ISR_install_raw_handler
151 *
152 *  This routine installs the specified handler as a "raw" non-executive
153 *  supported trap handler (a.k.a. interrupt service routine).
154 *
155 *  Input Parameters:
156 *    vector      - trap table entry number plus synchronous
157 *                    vs. asynchronous information
158 *    new_handler - address of the handler to be installed
159 *    old_handler - pointer to an address of the handler previously installed
160 *
161 *  Output Parameters: NONE
162 *    *new_handler - address of the handler previously installed
163 *
164 *  NOTE:
165 *
166 *  On the SPARC, there are really only 256 vectors.  However, the executive
167 *  has no easy, fast, reliable way to determine which traps are synchronous
168 *  and which are asynchronous.  By default, synchronous traps return to the
169 *  instruction which caused the interrupt.  So if you install a software
170 *  trap handler as an executive interrupt handler (which is desirable since
171 *  RTEMS takes care of window and register issues), then the executive needs
172 *  to know that the return address is to the trap rather than the instruction
173 *  following the trap.
174 *
175 *  So vectors 0 through 255 are treated as regular asynchronous traps which
176 *  provide the "correct" return address.  Vectors 256 through 512 are assumed
177 *  by the executive to be synchronous and to require that the return address
178 *  be fudged.
179 *
180 *  If you use this mechanism to install a trap handler which must reexecute
181 *  the instruction which caused the trap, then it should be installed as
182 *  an asynchronous trap.  This will avoid the executive changing the return
183 *  address.
184 */
185
186void _CPU_ISR_install_raw_handler(
187  uint32_t    vector,
188  proc_ptr    new_handler,
189  proc_ptr   *old_handler
190)
191{
192  uint32_t               real_vector;
193  CPU_Trap_table_entry  *tbr;
194  CPU_Trap_table_entry  *slot;
195  uint32_t               u32_tbr;
196  uint32_t               u32_handler;
197
198  /*
199   *  Get the "real" trap number for this vector ignoring the synchronous
200   *  versus asynchronous indicator included with our vector numbers.
201   */
202
203  real_vector = SPARC_REAL_TRAP_NUMBER( vector );
204
205  /*
206   *  Get the current base address of the trap table and calculate a pointer
207   *  to the slot we are interested in.
208   */
209
210  sparc_get_tbr( u32_tbr );
211
212  u32_tbr &= 0xfffff000;
213
214  tbr = (CPU_Trap_table_entry *) u32_tbr;
215
216  slot = &tbr[ real_vector ];
217
218  /*
219   *  Get the address of the old_handler from the trap table.
220   *
221   *  NOTE: The old_handler returned will be bogus if it does not follow
222   *        the RTEMS model.
223   */
224
225#define HIGH_BITS_MASK   0xFFFFFC00
226#define HIGH_BITS_SHIFT  10
227#define LOW_BITS_MASK    0x000003FF
228
229  if ( slot->mov_psr_l0 == _CPU_Trap_slot_template.mov_psr_l0 ) {
230    u32_handler =
231      (slot->sethi_of_handler_to_l4 << HIGH_BITS_SHIFT) |
232      (slot->jmp_to_low_of_handler_plus_l4 & LOW_BITS_MASK);
233    *old_handler = (proc_ptr) u32_handler;
234  } else
235    *old_handler = 0;
236
237  /*
238   *  Copy the template to the slot and then fix it.
239   */
240
241  *slot = _CPU_Trap_slot_template;
242
243  u32_handler = (uint32_t) new_handler;
244
245  slot->mov_vector_l3 |= vector;
246  slot->sethi_of_handler_to_l4 |=
247    (u32_handler & HIGH_BITS_MASK) >> HIGH_BITS_SHIFT;
248  slot->jmp_to_low_of_handler_plus_l4 |= (u32_handler & LOW_BITS_MASK);
249
250  /*
251   * There is no instruction cache snooping, so we need to invalidate
252   * the instruction cache to make sure that the processor sees the
253   * changes to the trap table. This step is required on both single-
254   * and multiprocessor systems.
255   *
256   * In a SMP configuration a change to the trap table might be
257   * missed by other cores. If the system state is up, the other
258   * cores can be notified using SMP messages that they need to
259   * flush their icache. If the up state has not been reached
260   * there is no need to notify other cores. They will do an
261   * automatic flush of the icache just after entering the up
262   * state, but before enabling interrupts.
263   */
264  rtems_cache_invalidate_entire_instruction();
265}
266
267void _CPU_ISR_install_vector(
268  uint32_t    vector,
269  proc_ptr    new_handler,
270  proc_ptr   *old_handler
271)
272{
273   uint32_t   real_vector;
274   proc_ptr   ignored;
275
276  /*
277   *  Get the "real" trap number for this vector ignoring the synchronous
278   *  versus asynchronous indicator included with our vector numbers.
279   */
280
281   real_vector = SPARC_REAL_TRAP_NUMBER( vector );
282
283   /*
284    *  Return the previous ISR handler.
285    */
286
287   *old_handler = _ISR_Vector_table[ real_vector ];
288
289   /*
290    *  Install the wrapper so this ISR can be invoked properly.
291    */
292
293   _CPU_ISR_install_raw_handler( vector, _ISR_Handler, &ignored );
294
295   /*
296    *  We put the actual user ISR address in '_ISR_vector_table'.  This will
297    *  be used by the _ISR_Handler so the user gets control.
298    */
299
300    _ISR_Vector_table[ real_vector ] = new_handler;
301}
302
303void _CPU_Context_Initialize(
304  Context_Control  *the_context,
305  uint32_t         *stack_base,
306  uint32_t          size,
307  uint32_t          new_level,
308  void             *entry_point,
309  bool              is_fp,
310  void             *tls_area
311)
312{
313    uint32_t     stack_high;  /* highest "stack aligned" address */
314    uint32_t     tmp_psr;
315
316    /*
317     *  On CPUs with stacks which grow down (i.e. SPARC), we build the stack
318     *  based on the stack_high address.
319     */
320
321    stack_high = ((uint32_t)(stack_base) + size);
322    stack_high &= ~(CPU_STACK_ALIGNMENT - 1);
323
324    /*
325     *  See the README in this directory for a diagram of the stack.
326     */
327
328    the_context->o7    = ((uint32_t) entry_point) - 8;
329    the_context->o6_sp = stack_high - CPU_MINIMUM_STACK_FRAME_SIZE;
330    the_context->i6_fp = 0;
331
332    /*
333     *  Build the PSR for the task.  Most everything can be 0 and the
334     *  CWP is corrected during the context switch.
335     *
336     *  The EF bit determines if the floating point unit is available.
337     *  The FPU is ONLY enabled if the context is associated with an FP task
338     *  and this SPARC model has an FPU.
339     */
340
341    sparc_get_psr( tmp_psr );
342    tmp_psr &= ~SPARC_PSR_PIL_MASK;
343    tmp_psr |= (new_level << 8) & SPARC_PSR_PIL_MASK;
344    tmp_psr &= ~SPARC_PSR_EF_MASK;      /* disabled by default */
345
346#if (SPARC_HAS_FPU == 1)
347    /*
348     *  If this bit is not set, then a task gets a fault when it accesses
349     *  a floating point register.  This is a nice way to detect floating
350     *  point tasks which are not currently declared as such.
351     */
352
353    if ( is_fp )
354      tmp_psr |= SPARC_PSR_EF_MASK;
355#endif
356    the_context->psr = tmp_psr;
357
358  /*
359   *  Since THIS thread is being created, there is no way that THIS
360   *  thread can have an _ISR_Dispatch stack frame on its stack.
361   */
362    the_context->isr_dispatch_disable = 0;
363
364  if ( tls_area != NULL ) {
365    void *tcb = _TLS_TCB_after_TLS_block_initialize( tls_area );
366
367    the_context->g7 = (uintptr_t) tcb;
368  }
369}
Note: See TracBrowser for help on using the repository browser.