1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// hal_arch.h
|
4 |
|
|
//
|
5 |
|
|
// Architecture specific abstractions
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License along
|
23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
28 |
|
|
// with other works to produce a work based on this file, this file does not
|
29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
30 |
|
|
// License. However the source code for this file must still be made available
|
31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
34 |
|
|
// this file might be covered by the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
37 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
40 |
|
|
//==========================================================================
|
41 |
|
|
//#####DESCRIPTIONBEGIN####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): sfurman
|
44 |
|
|
// Contributors:
|
45 |
|
|
// Date: 2003-01-17
|
46 |
|
|
// Purpose: Define architecture abstractions
|
47 |
|
|
// Usage: #include <cyg/hal/hal_arch.h>
|
48 |
|
|
//
|
49 |
|
|
//####DESCRIPTIONEND####
|
50 |
|
|
//
|
51 |
|
|
//==========================================================================
|
52 |
|
|
|
53 |
|
|
#ifndef CYGONCE_HAL_HAL_ARCH_H
|
54 |
|
|
#define CYGONCE_HAL_HAL_ARCH_H
|
55 |
|
|
|
56 |
|
|
// Include macros to access special-purpose registers (SPRs)
|
57 |
|
|
#include <cyg/hal/spr_defs.h>
|
58 |
|
|
|
59 |
|
|
#define CYG_HAL_OPENRISC_REG_SIZE 4
|
60 |
|
|
|
61 |
|
|
#ifndef __ASSEMBLER__
|
62 |
|
|
#include <pkgconf/hal.h>
|
63 |
|
|
#include <cyg/infra/cyg_type.h>
|
64 |
|
|
|
65 |
|
|
//--------------------------------------------------------------------------
|
66 |
|
|
// Processor saved states:
|
67 |
|
|
// The layout of this structure is also defined in "arch.inc", for assembly
|
68 |
|
|
// code. Do not change this without changing that (or vice versa).
|
69 |
|
|
|
70 |
|
|
#define CYG_HAL_OPENRISC_REG CYG_WORD32
|
71 |
|
|
|
72 |
|
|
typedef struct
|
73 |
|
|
{
|
74 |
|
|
// These are common to all saved states
|
75 |
|
|
CYG_HAL_OPENRISC_REG r[32]; // GPR regs
|
76 |
|
|
CYG_HAL_OPENRISC_REG machi; // High and low words of
|
77 |
|
|
CYG_HAL_OPENRISC_REG maclo; // multiply/accumulate reg
|
78 |
|
|
|
79 |
|
|
// These are only saved for exceptions and interrupts
|
80 |
|
|
CYG_WORD32 vector; /* Vector number */
|
81 |
|
|
CYG_WORD32 sr; /* Status Reg */
|
82 |
|
|
CYG_HAL_OPENRISC_REG pc; /* Program Counter */
|
83 |
|
|
|
84 |
|
|
// Saved only for exceptions, and not restored when continued:
|
85 |
|
|
// Effective address of instruction/data access that caused exception
|
86 |
|
|
CYG_HAL_OPENRISC_REG eear; /* Exception effective address reg */
|
87 |
|
|
} HAL_SavedRegisters;
|
88 |
|
|
|
89 |
|
|
//--------------------------------------------------------------------------
|
90 |
|
|
// Utilities
|
91 |
|
|
|
92 |
|
|
// Move from architecture special register (SPR)
|
93 |
|
|
#define MFSPR(_spr_) \
|
94 |
|
|
({ CYG_HAL_OPENRISC_REG _result_; \
|
95 |
|
|
asm volatile ("l.mfspr %0, r0, %1;" \
|
96 |
|
|
: "=r"(_result_) \
|
97 |
|
|
: "K"(_spr_) \
|
98 |
|
|
); \
|
99 |
|
|
_result_;})
|
100 |
|
|
|
101 |
|
|
// Move data to architecture special registers (SPR)
|
102 |
|
|
#define MTSPR(_spr_, _val_) \
|
103 |
|
|
CYG_MACRO_START \
|
104 |
|
|
CYG_HAL_OPENRISC_REG val = _val_; \
|
105 |
|
|
asm volatile ("l.mtspr r0, %0, %1;" \
|
106 |
|
|
: \
|
107 |
|
|
: "r"(val), "K"(_spr_) \
|
108 |
|
|
); \
|
109 |
|
|
CYG_MACRO_END
|
110 |
|
|
|
111 |
|
|
//--------------------------------------------------------------------------
|
112 |
|
|
// Exception handling function.
|
113 |
|
|
// This function is defined by the kernel according to this prototype. It is
|
114 |
|
|
// invoked from the HAL to deal with any CPU exceptions that the HAL does
|
115 |
|
|
// not want to deal with itself. It usually invokes the kernel's exception
|
116 |
|
|
// delivery mechanism.
|
117 |
|
|
|
118 |
|
|
externC void cyg_hal_deliver_exception( CYG_WORD code, CYG_ADDRWORD data );
|
119 |
|
|
|
120 |
|
|
//--------------------------------------------------------------------------
|
121 |
|
|
// Bit manipulation macros
|
122 |
|
|
|
123 |
|
|
externC cyg_uint32 hal_lsbit_index(cyg_uint32 mask);
|
124 |
|
|
externC cyg_uint32 hal_msbit_index(cyg_uint32 mask);
|
125 |
|
|
|
126 |
|
|
#define HAL_LSBIT_INDEX(index, mask) index = hal_lsbit_index(mask);
|
127 |
|
|
|
128 |
|
|
// NOTE - Below can be optimized with l.ff1 instruction if that optional
|
129 |
|
|
// instruction is implemented in HW. OR12k does not implement
|
130 |
|
|
// it at this time, however.
|
131 |
|
|
#define HAL_MSBIT_INDEX(index, mask) index = hal_msbit_index(mask);
|
132 |
|
|
|
133 |
|
|
//--------------------------------------------------------------------------
|
134 |
|
|
// Context Initialization
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
// Initialize the context of a thread.
|
138 |
|
|
// Arguments:
|
139 |
|
|
// _sparg_ name of variable containing current sp, will be written with new sp
|
140 |
|
|
// _thread_ thread object address, passed as argument to entry point
|
141 |
|
|
// _entry_ entry point address.
|
142 |
|
|
// _id_ bit pattern used in initializing registers, for debugging.
|
143 |
|
|
#define HAL_THREAD_INIT_CONTEXT( _sparg_, _thread_, _entry_, _id_ ) \
|
144 |
|
|
{ \
|
145 |
|
|
int _i_; \
|
146 |
|
|
register CYG_WORD _sp_ = ((CYG_WORD)_sparg_); \
|
147 |
|
|
register HAL_SavedRegisters *_regs_; \
|
148 |
|
|
_regs_ = (HAL_SavedRegisters *)(((_sp_) - sizeof(HAL_SavedRegisters)) & ~(CYGARC_ALIGNMENT));\
|
149 |
|
|
_sp_ &= ~(CYGARC_ALIGNMENT); \
|
150 |
|
|
for( _i_ = 1; _i_ < 32; _i_++ ) (_regs_)->r[_i_] = (_id_)|_i_; \
|
151 |
|
|
(_regs_)->r[1] = (CYG_HAL_OPENRISC_REG)(_sp_); /* SP = top of stack */ \
|
152 |
|
|
(_regs_)->r[2] = (CYG_HAL_OPENRISC_REG)(_sp_); /* FP = top of stack */ \
|
153 |
|
|
(_regs_)->r[3] = (CYG_HAL_OPENRISC_REG)(_thread_); /* R3 = arg1 = thread ptr */ \
|
154 |
|
|
(_regs_)->maclo = 0; /* MACLO = 0 */ \
|
155 |
|
|
(_regs_)->machi = 0; /* MACHI = 0 */ \
|
156 |
|
|
(_regs_)->sr = (SPR_SR_TEE|SPR_SR_IEE); /* Interrupts enabled */ \
|
157 |
|
|
(_regs_)->pc = (CYG_HAL_OPENRISC_REG)(_entry_); /* PC = entry point */ \
|
158 |
|
|
(_regs_)->r[9] = (CYG_HAL_OPENRISC_REG)(_entry_); /* PC = entry point */ \
|
159 |
|
|
_sparg_ = (CYG_ADDRESS)_regs_; \
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
//--------------------------------------------------------------------------
|
163 |
|
|
// Context switch macros.
|
164 |
|
|
|
165 |
|
|
// The arguments to these macros are *pointers* to locations where the
|
166 |
|
|
// stack pointer of the thread is to be stored/retrieved, i.e. *not*
|
167 |
|
|
// the value of the stack pointer itself.
|
168 |
|
|
|
169 |
|
|
externC void hal_thread_switch_context( CYG_ADDRESS to, CYG_ADDRESS from );
|
170 |
|
|
externC void hal_thread_load_context( CYG_ADDRESS to )
|
171 |
|
|
__attribute__ ((noreturn));
|
172 |
|
|
|
173 |
|
|
#define HAL_THREAD_SWITCH_CONTEXT(_fspptr_,_tspptr_) \
|
174 |
|
|
hal_thread_switch_context( (CYG_ADDRESS)_tspptr_, \
|
175 |
|
|
(CYG_ADDRESS)_fspptr_);
|
176 |
|
|
|
177 |
|
|
#define HAL_THREAD_LOAD_CONTEXT(_tspptr_) \
|
178 |
|
|
hal_thread_load_context( (CYG_ADDRESS)_tspptr_ );
|
179 |
|
|
|
180 |
|
|
// Translate a stack pointer as saved by the thread context macros above into
|
181 |
|
|
// a pointer to a HAL_SavedRegisters structure.
|
182 |
|
|
#define HAL_THREAD_GET_SAVED_REGISTERS( _sp_, _regs_ ) \
|
183 |
|
|
(_regs_) = (HAL_SavedRegisters *)(_sp_)
|
184 |
|
|
|
185 |
|
|
//--------------------------------------------------------------------------
|
186 |
|
|
// Execution reorder barrier.
|
187 |
|
|
// When optimizing the compiler can reorder code. In multithreaded systems
|
188 |
|
|
// where the order of actions is vital, this can sometimes cause problems.
|
189 |
|
|
// This macro may be inserted into places where reordering should not happen.
|
190 |
|
|
// The "memory" keyword is potentially unnecessary, but it is harmless to
|
191 |
|
|
// keep it.
|
192 |
|
|
|
193 |
|
|
#define HAL_REORDER_BARRIER() asm volatile ( "" : : : "memory" )
|
194 |
|
|
|
195 |
|
|
//--------------------------------------------------------------------------
|
196 |
|
|
// Breakpoint support
|
197 |
|
|
// HAL_BREAKPOINT() is a code sequence that will cause a breakpoint to
|
198 |
|
|
// occur if executed.
|
199 |
|
|
// HAL_BREAKINST is the value of the breakpoint instruction and...
|
200 |
|
|
// HAL_BREAKINST_SIZE is its size in bytes and...
|
201 |
|
|
// HAL_BREAKINST_TYPE is its type.
|
202 |
|
|
|
203 |
|
|
#define HAL_BREAKPOINT(_label_) \
|
204 |
|
|
asm volatile (" .globl _" #_label_ ";" \
|
205 |
|
|
"_" #_label_ ":" \
|
206 |
|
|
" l.trap 1;" \
|
207 |
|
|
);
|
208 |
|
|
|
209 |
|
|
#define HAL_BREAKINST (0x21000001) // l.trap 1 instruction
|
210 |
|
|
|
211 |
|
|
#define HAL_BREAKINST_SIZE 4
|
212 |
|
|
|
213 |
|
|
#define HAL_BREAKINST_TYPE cyg_uint32
|
214 |
|
|
|
215 |
|
|
//--------------------------------------------------------------------------
|
216 |
|
|
// Thread register state manipulation for GDB support.
|
217 |
|
|
|
218 |
|
|
// Default to a 32 bit register size for GDB register dumps.
|
219 |
|
|
#ifndef CYG_HAL_GDB_REG
|
220 |
|
|
#define CYG_HAL_GDB_REG CYG_WORD32
|
221 |
|
|
#endif
|
222 |
|
|
|
223 |
|
|
// Register layout expected by GDB
|
224 |
|
|
typedef struct
|
225 |
|
|
{
|
226 |
|
|
CYG_HAL_OPENRISC_REG r[32]; // GPR regs
|
227 |
|
|
CYG_HAL_OPENRISC_REG pc; // Program Counter
|
228 |
|
|
CYG_HAL_OPENRISC_REG sr; // Supervisor/Status Reg
|
229 |
|
|
} GDB_Registers;
|
230 |
|
|
|
231 |
|
|
// Copy a set of registers from a HAL_SavedRegisters structure into a
|
232 |
|
|
// GDB_Registers structure.
|
233 |
|
|
#define HAL_GET_GDB_REGISTERS( _aregval_, _regs_ ) \
|
234 |
|
|
CYG_MACRO_START \
|
235 |
|
|
GDB_Registers *_gdb_ = (GDB_Registers *)(_aregval_); \
|
236 |
|
|
int _i_; \
|
237 |
|
|
\
|
238 |
|
|
for( _i_ = 0; _i_ < 32; _i_++ ) { \
|
239 |
|
|
_gdb_->r[_i_] = (_regs_)->r[_i_]; \
|
240 |
|
|
} \
|
241 |
|
|
\
|
242 |
|
|
_gdb_->pc = (_regs_)->pc; \
|
243 |
|
|
_gdb_->sr = (_regs_)->sr; \
|
244 |
|
|
CYG_MACRO_END
|
245 |
|
|
|
246 |
|
|
// Copy a set of registers from a GDB_Registers structure into a
|
247 |
|
|
// HAL_SavedRegisters structure.
|
248 |
|
|
#define HAL_SET_GDB_REGISTERS( _regs_ , _aregval_ ) \
|
249 |
|
|
CYG_MACRO_START \
|
250 |
|
|
GDB_Registers *_gdb_ = (GDB_Registers *)(_aregval_); \
|
251 |
|
|
int _i_; \
|
252 |
|
|
\
|
253 |
|
|
for( _i_ = 0; _i_ < 32; _i_++ ) \
|
254 |
|
|
(_regs_)->r[_i_] = _gdb_->r[_i_]; \
|
255 |
|
|
\
|
256 |
|
|
(_regs_)->pc = _gdb_->pc; \
|
257 |
|
|
(_regs_)->sr = _gdb_->sr; \
|
258 |
|
|
CYG_MACRO_END
|
259 |
|
|
|
260 |
|
|
//--------------------------------------------------------------------------
|
261 |
|
|
// HAL setjmp
|
262 |
|
|
// Note: These definitions are repeated in context.S. If changes are
|
263 |
|
|
// required remember to update both sets.
|
264 |
|
|
|
265 |
|
|
#define CYGARC_JMP_BUF_R1 0
|
266 |
|
|
#define CYGARC_JMP_BUF_R2 1
|
267 |
|
|
#define CYGARC_JMP_BUF_R9 2
|
268 |
|
|
#define CYGARC_JMP_BUF_R10 3
|
269 |
|
|
#define CYGARC_JMP_BUF_R12 4
|
270 |
|
|
#define CYGARC_JMP_BUF_R14 5
|
271 |
|
|
#define CYGARC_JMP_BUF_R16 6
|
272 |
|
|
#define CYGARC_JMP_BUF_R18 7
|
273 |
|
|
#define CYGARC_JMP_BUF_R20 8
|
274 |
|
|
#define CYGARC_JMP_BUF_R22 9
|
275 |
|
|
#define CYGARC_JMP_BUF_R24 10
|
276 |
|
|
#define CYGARC_JMP_BUF_R26 11
|
277 |
|
|
#define CYGARC_JMP_BUF_R28 12
|
278 |
|
|
#define CYGARC_JMP_BUF_R30 13
|
279 |
|
|
|
280 |
|
|
#define CYGARC_JMP_BUF_SIZE 14
|
281 |
|
|
|
282 |
|
|
typedef CYG_HAL_OPENRISC_REG hal_jmp_buf[CYGARC_JMP_BUF_SIZE];
|
283 |
|
|
|
284 |
|
|
externC int hal_setjmp(hal_jmp_buf env);
|
285 |
|
|
externC void hal_longjmp(hal_jmp_buf env, int val);
|
286 |
|
|
|
287 |
|
|
//-------------------------------------------------------------------------
|
288 |
|
|
// Idle thread code.
|
289 |
|
|
// This macro is called in the idle thread loop, and gives the HAL the
|
290 |
|
|
// chance to run code when no threads are runnable. Typical idle
|
291 |
|
|
// thread behaviour might be to halt the processor.
|
292 |
|
|
|
293 |
|
|
externC void hal_idle_thread_action(cyg_uint32 loop_count);
|
294 |
|
|
|
295 |
|
|
#define HAL_IDLE_THREAD_ACTION(_count_) hal_idle_thread_action(_count_)
|
296 |
|
|
|
297 |
|
|
//--------------------------------------------------------------------------
|
298 |
|
|
// Minimal and sensible stack sizes: the intention is that applications
|
299 |
|
|
// will use these to provide a stack size in the first instance prior to
|
300 |
|
|
// proper analysis. Idle thread stack should be this big.
|
301 |
|
|
|
302 |
|
|
// *** THESE ARE NOT INTENDED TO BE GUARANTEED SUFFICIENT STACK SIZES ***
|
303 |
|
|
// They are, however, enough to start programming.
|
304 |
|
|
// You might, for example, need to make your stacks larger if you have
|
305 |
|
|
// large "auto" variables.
|
306 |
|
|
|
307 |
|
|
// This is not a config option because it should not be adjusted except
|
308 |
|
|
// under "enough rope to hang yourself" sort of disclaimers.
|
309 |
|
|
|
310 |
|
|
// Typical case stack frame size: return link + 10 caller-saved temporaries + 4 locals.
|
311 |
|
|
#define CYGNUM_HAL_STACK_FRAME_SIZE (15 * CYG_HAL_OPENRISC_REG_SIZE)
|
312 |
|
|
|
313 |
|
|
// Stack needed for a context switch:
|
314 |
|
|
#define CYGNUM_HAL_STACK_CONTEXT_SIZE (38 * 4) // sizeof(HAL_SavedRegisters)
|
315 |
|
|
|
316 |
|
|
// Interrupt + call to ISR, interrupt_end() and the DSR
|
317 |
|
|
#define CYGNUM_HAL_STACK_INTERRUPT_SIZE (CYGNUM_HAL_STACK_CONTEXT_SIZE + 2*CYGNUM_HAL_STACK_FRAME_SIZE)
|
318 |
|
|
|
319 |
|
|
// We define a minimum stack size as the minimum any thread could ever
|
320 |
|
|
// legitimately get away with. We can throw asserts if users ask for less
|
321 |
|
|
// than this. Allow enough for three interrupt sources - clock, serial and
|
322 |
|
|
// one other
|
323 |
|
|
|
324 |
|
|
// If interrupts are segregated onto their own stack...
|
325 |
|
|
#ifdef CYGIMP_HAL_COMMON_INTERRUPTS_USE_INTERRUPT_STACK
|
326 |
|
|
|
327 |
|
|
// An interrupt stack which is large enough for all possible interrupt
|
328 |
|
|
// conditions (and only used for that purpose) exists. "User" stacks
|
329 |
|
|
// can therefore be much smaller
|
330 |
|
|
// NOTE - interrupt stack sizes can be smaller if we don't allow interrupts
|
331 |
|
|
// to nest.
|
332 |
|
|
|
333 |
|
|
# define CYGNUM_HAL_STACK_SIZE_MINIMUM \
|
334 |
|
|
((3 * 5)*CYGNUM_HAL_STACK_FRAME_SIZE + 2*CYGNUM_HAL_STACK_INTERRUPT_SIZE)
|
335 |
|
|
|
336 |
|
|
#else
|
337 |
|
|
|
338 |
|
|
// No separate interrupt stack exists. Make sure all threads contain
|
339 |
|
|
// a stack sufficiently large
|
340 |
|
|
# define CYGNUM_HAL_STACK_SIZE_MINIMUM \
|
341 |
|
|
(( 3*CYGNUM_HAL_STACK_INTERRUPT_SIZE) + \
|
342 |
|
|
(25*CYGNUM_HAL_STACK_FRAME_SIZE))
|
343 |
|
|
#endif
|
344 |
|
|
|
345 |
|
|
// Now make a reasonable choice for a typical thread size. Pluck figures
|
346 |
|
|
// from thin air and say 40 call frames
|
347 |
|
|
#define CYGNUM_HAL_STACK_SIZE_TYPICAL \
|
348 |
|
|
(CYGNUM_HAL_STACK_SIZE_MINIMUM + \
|
349 |
|
|
40 * (CYGNUM_HAL_STACK_FRAME_SIZE))
|
350 |
|
|
|
351 |
|
|
#endif /* __ASSEMBLER__ */
|
352 |
|
|
|
353 |
|
|
//--------------------------------------------------------------------------
|
354 |
|
|
// Macros for switching context between two eCos instances (jump from
|
355 |
|
|
// code in ROM to code in RAM or vice versa).
|
356 |
|
|
// These are NOP's in the case of OpenRISC.
|
357 |
|
|
#define CYGARC_HAL_SAVE_GP()
|
358 |
|
|
#define CYGARC_HAL_RESTORE_GP()
|
359 |
|
|
|
360 |
|
|
//--------------------------------------------------------------------------
|
361 |
|
|
// Macro for finding return address of current function
|
362 |
|
|
#define CYGARC_HAL_GET_RETURN_ADDRESS(_x_, _dummy_) \
|
363 |
|
|
asm volatile ( "l.ori %0,r9,0;" : "=r" (_x_) )
|
364 |
|
|
|
365 |
|
|
#define CYGARC_HAL_GET_RETURN_ADDRESS_BACKUP(_dummy_)
|
366 |
|
|
|
367 |
|
|
//--------------------------------------------------------------------------
|
368 |
|
|
#endif // CYGONCE_HAL_HAL_ARCH_H
|
369 |
|
|
// End of hal_arch.h
|