source: rtems/cpukit/score/cpu/sparc/cpu.c @ 97cf623d

4.115
Last change on this file since 97cf623d was 97cf623d, checked in by Sebastian Huber <sebastian.huber@…>, on 02/05/14 at 10:36:05

sparc: Save/restore only non-volatile context

The _CPU_Context_switch() is a normal function call. The following
registers are volatile (the caller must assume that the register
contents are destroyed by the callee) according to "SYSTEM V APPLICATION
BINARY INTERFACE - SPARC Processor Supplement", Third Edition: g1, o0,
o1, o2, o3, o4, o5. Drop these registers from the context.

Ensure that offset defines match the structure offsets.

  • Property mode set to 100644
File size: 9.2 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.com/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(g2_g3, G2);
39SPARC_ASSERT_OFFSET(g4, G4);
40SPARC_ASSERT_OFFSET(g5, G5);
41SPARC_ASSERT_OFFSET(g6, G6);
42SPARC_ASSERT_OFFSET(g7, G7);
43SPARC_ASSERT_OFFSET(l0, L0);
44SPARC_ASSERT_OFFSET(l1, L1);
45SPARC_ASSERT_OFFSET(l2, L2);
46SPARC_ASSERT_OFFSET(l3, L3);
47SPARC_ASSERT_OFFSET(l4, L4);
48SPARC_ASSERT_OFFSET(l5, L5);
49SPARC_ASSERT_OFFSET(l6, L6);
50SPARC_ASSERT_OFFSET(l7, L7);
51SPARC_ASSERT_OFFSET(i0, I0);
52SPARC_ASSERT_OFFSET(i1, I1);
53SPARC_ASSERT_OFFSET(i2, I2);
54SPARC_ASSERT_OFFSET(i3, I3);
55SPARC_ASSERT_OFFSET(i4, I4);
56SPARC_ASSERT_OFFSET(i5, I5);
57SPARC_ASSERT_OFFSET(i6_fp, I6_FP);
58SPARC_ASSERT_OFFSET(i7, I7);
59SPARC_ASSERT_OFFSET(o6_sp, O6_SP);
60SPARC_ASSERT_OFFSET(o7, O7);
61SPARC_ASSERT_OFFSET(psr, PSR);
62SPARC_ASSERT_OFFSET(isr_dispatch_disable, ISR_DISPATCH_DISABLE_STACK);
63
64RTEMS_STATIC_ASSERT(
65  (offsetof(Context_Control, g2_g3)
66     + offsetof(Context_Control, g4)) / 2 == G3_OFFSET,
67  Context_Control_offset_G3
68);
69
70/*
71 *  This initializes the set of opcodes placed in each trap
72 *  table entry.  The routine which installs a handler is responsible
73 *  for filling in the fields for the _handler address and the _vector
74 *  trap type.
75 *
76 *  The constants following this structure are masks for the fields which
77 *  must be filled in when the handler is installed.
78 */
79
80const CPU_Trap_table_entry _CPU_Trap_slot_template = {
81  0xa1480000,      /* mov   %psr, %l0           */
82  0x29000000,      /* sethi %hi(_handler), %l4  */
83  0x81c52000,      /* jmp   %l4 + %lo(_handler) */
84  0xa6102000       /* mov   _vector, %l3        */
85};
86
87/*
88 *  _CPU_Initialize
89 *
90 *  This routine performs processor dependent initialization.
91 *
92 *  INPUT PARAMETERS: NONE
93 *
94 *  Output Parameters: NONE
95 *
96 *  NOTE: There is no need to save the pointer to the thread dispatch routine.
97 *        The SPARC's assembly code can reference it directly with no problems.
98 */
99
100void _CPU_Initialize(void)
101{
102#if (SPARC_HAS_FPU == 1)
103  Context_Control_fp *pointer;
104
105  /*
106   *  This seems to be the most appropriate way to obtain an initial
107   *  FP context on the SPARC.  The NULL fp context is copied it to
108   *  the task's FP context during Context_Initialize.
109   */
110
111  pointer = &_CPU_Null_fp_context;
112  _CPU_Context_save_fp( &pointer );
113#endif
114}
115
116uint32_t   _CPU_ISR_Get_level( void )
117{
118  uint32_t   level;
119
120  sparc_get_interrupt_level( level );
121
122  return level;
123}
124
125/*
126 *  _CPU_ISR_install_raw_handler
127 *
128 *  This routine installs the specified handler as a "raw" non-executive
129 *  supported trap handler (a.k.a. interrupt service routine).
130 *
131 *  Input Parameters:
132 *    vector      - trap table entry number plus synchronous
133 *                    vs. asynchronous information
134 *    new_handler - address of the handler to be installed
135 *    old_handler - pointer to an address of the handler previously installed
136 *
137 *  Output Parameters: NONE
138 *    *new_handler - address of the handler previously installed
139 *
140 *  NOTE:
141 *
142 *  On the SPARC, there are really only 256 vectors.  However, the executive
143 *  has no easy, fast, reliable way to determine which traps are synchronous
144 *  and which are asynchronous.  By default, synchronous traps return to the
145 *  instruction which caused the interrupt.  So if you install a software
146 *  trap handler as an executive interrupt handler (which is desirable since
147 *  RTEMS takes care of window and register issues), then the executive needs
148 *  to know that the return address is to the trap rather than the instruction
149 *  following the trap.
150 *
151 *  So vectors 0 through 255 are treated as regular asynchronous traps which
152 *  provide the "correct" return address.  Vectors 256 through 512 are assumed
153 *  by the executive to be synchronous and to require that the return address
154 *  be fudged.
155 *
156 *  If you use this mechanism to install a trap handler which must reexecute
157 *  the instruction which caused the trap, then it should be installed as
158 *  an asynchronous trap.  This will avoid the executive changing the return
159 *  address.
160 */
161
162void _CPU_ISR_install_raw_handler(
163  uint32_t    vector,
164  proc_ptr    new_handler,
165  proc_ptr   *old_handler
166)
167{
168  uint32_t               real_vector;
169  CPU_Trap_table_entry  *tbr;
170  CPU_Trap_table_entry  *slot;
171  uint32_t               u32_tbr;
172  uint32_t               u32_handler;
173
174  /*
175   *  Get the "real" trap number for this vector ignoring the synchronous
176   *  versus asynchronous indicator included with our vector numbers.
177   */
178
179  real_vector = SPARC_REAL_TRAP_NUMBER( vector );
180
181  /*
182   *  Get the current base address of the trap table and calculate a pointer
183   *  to the slot we are interested in.
184   */
185
186  sparc_get_tbr( u32_tbr );
187
188  u32_tbr &= 0xfffff000;
189
190  tbr = (CPU_Trap_table_entry *) u32_tbr;
191
192  slot = &tbr[ real_vector ];
193
194  /*
195   *  Get the address of the old_handler from the trap table.
196   *
197   *  NOTE: The old_handler returned will be bogus if it does not follow
198   *        the RTEMS model.
199   */
200
201#define HIGH_BITS_MASK   0xFFFFFC00
202#define HIGH_BITS_SHIFT  10
203#define LOW_BITS_MASK    0x000003FF
204
205  if ( slot->mov_psr_l0 == _CPU_Trap_slot_template.mov_psr_l0 ) {
206    u32_handler =
207      (slot->sethi_of_handler_to_l4 << HIGH_BITS_SHIFT) |
208      (slot->jmp_to_low_of_handler_plus_l4 & LOW_BITS_MASK);
209    *old_handler = (proc_ptr) u32_handler;
210  } else
211    *old_handler = 0;
212
213  /*
214   *  Copy the template to the slot and then fix it.
215   */
216
217  *slot = _CPU_Trap_slot_template;
218
219  u32_handler = (uint32_t) new_handler;
220
221  slot->mov_vector_l3 |= vector;
222  slot->sethi_of_handler_to_l4 |=
223    (u32_handler & HIGH_BITS_MASK) >> HIGH_BITS_SHIFT;
224  slot->jmp_to_low_of_handler_plus_l4 |= (u32_handler & LOW_BITS_MASK);
225
226  /* need to flush icache after this !!! */
227
228  rtems_cache_invalidate_entire_instruction();
229
230}
231
232void _CPU_ISR_install_vector(
233  uint32_t    vector,
234  proc_ptr    new_handler,
235  proc_ptr   *old_handler
236)
237{
238   uint32_t   real_vector;
239   proc_ptr   ignored;
240
241  /*
242   *  Get the "real" trap number for this vector ignoring the synchronous
243   *  versus asynchronous indicator included with our vector numbers.
244   */
245
246   real_vector = SPARC_REAL_TRAP_NUMBER( vector );
247
248   /*
249    *  Return the previous ISR handler.
250    */
251
252   *old_handler = _ISR_Vector_table[ real_vector ];
253
254   /*
255    *  Install the wrapper so this ISR can be invoked properly.
256    */
257
258   _CPU_ISR_install_raw_handler( vector, _ISR_Handler, &ignored );
259
260   /*
261    *  We put the actual user ISR address in '_ISR_vector_table'.  This will
262    *  be used by the _ISR_Handler so the user gets control.
263    */
264
265    _ISR_Vector_table[ real_vector ] = new_handler;
266}
267
268void _CPU_Context_Initialize(
269  Context_Control  *the_context,
270  uint32_t         *stack_base,
271  uint32_t          size,
272  uint32_t          new_level,
273  void             *entry_point,
274  bool              is_fp,
275  void             *tls_area
276)
277{
278    uint32_t     stack_high;  /* highest "stack aligned" address */
279    uint32_t     tmp_psr;
280
281    /*
282     *  On CPUs with stacks which grow down (i.e. SPARC), we build the stack
283     *  based on the stack_high address.
284     */
285
286    stack_high = ((uint32_t)(stack_base) + size);
287    stack_high &= ~(CPU_STACK_ALIGNMENT - 1);
288
289    /*
290     *  See the README in this directory for a diagram of the stack.
291     */
292
293    the_context->o7    = ((uint32_t) entry_point) - 8;
294    the_context->o6_sp = stack_high - CPU_MINIMUM_STACK_FRAME_SIZE;
295    the_context->i6_fp = 0;
296
297    /*
298     *  Build the PSR for the task.  Most everything can be 0 and the
299     *  CWP is corrected during the context switch.
300     *
301     *  The EF bit determines if the floating point unit is available.
302     *  The FPU is ONLY enabled if the context is associated with an FP task
303     *  and this SPARC model has an FPU.
304     */
305
306    sparc_get_psr( tmp_psr );
307    tmp_psr &= ~SPARC_PSR_PIL_MASK;
308    tmp_psr |= (new_level << 8) & SPARC_PSR_PIL_MASK;
309    tmp_psr &= ~SPARC_PSR_EF_MASK;      /* disabled by default */
310
311#if (SPARC_HAS_FPU == 1)
312    /*
313     *  If this bit is not set, then a task gets a fault when it accesses
314     *  a floating point register.  This is a nice way to detect floating
315     *  point tasks which are not currently declared as such.
316     */
317
318    if ( is_fp )
319      tmp_psr |= SPARC_PSR_EF_MASK;
320#endif
321    the_context->psr = tmp_psr;
322
323  /*
324   *  Since THIS thread is being created, there is no way that THIS
325   *  thread can have an _ISR_Dispatch stack frame on its stack.
326   */
327    the_context->isr_dispatch_disable = 0;
328
329  if ( tls_area != NULL ) {
330    void *tcb = _TLS_TCB_after_tls_block_initialize( tls_area );
331
332    the_context->g7 = (uintptr_t) tcb;
333  }
334}
Note: See TracBrowser for help on using the repository browser.