1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// sh_stub.c
|
4 |
|
|
//
|
5 |
|
|
// GDB Stub code for SH
|
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): jskov
|
44 |
|
|
// Contributors: jskov, Ben Lee, Steve Chamberlain
|
45 |
|
|
// Date: 1999-05-18
|
46 |
|
|
// Description: GDB Stub support for sh CPU.
|
47 |
|
|
//
|
48 |
|
|
//####DESCRIPTIONEND####
|
49 |
|
|
//
|
50 |
|
|
//===========================================================================
|
51 |
|
|
|
52 |
|
|
#include <stddef.h>
|
53 |
|
|
|
54 |
|
|
#include <pkgconf/hal.h>
|
55 |
|
|
|
56 |
|
|
#ifdef CYGPKG_REDBOOT
|
57 |
|
|
#include <pkgconf/redboot.h>
|
58 |
|
|
#endif
|
59 |
|
|
|
60 |
|
|
#ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
|
61 |
|
|
|
62 |
|
|
#include <cyg/hal/sh_regs.h>
|
63 |
|
|
|
64 |
|
|
#include <cyg/hal/hal_stub.h>
|
65 |
|
|
#include <cyg/hal/hal_arch.h>
|
66 |
|
|
#include <cyg/hal/hal_intr.h>
|
67 |
|
|
|
68 |
|
|
#ifdef CYGDBG_HAL_DEBUG_GDB_THREAD_SUPPORT
|
69 |
|
|
#include <cyg/hal/dbg-threads-api.h> // dbg_currthread_id
|
70 |
|
|
#endif
|
71 |
|
|
|
72 |
|
|
/* Given a trap value TRAP, return the corresponding signal. */
|
73 |
|
|
|
74 |
|
|
int
|
75 |
|
|
__computeSignal(unsigned int trap_number)
|
76 |
|
|
{
|
77 |
|
|
switch( trap_number ) {
|
78 |
|
|
case CYGNUM_HAL_EXCEPTION_TRAP:
|
79 |
|
|
#ifdef CYGNUM_HAL_EXCEPTION_INSTRUCTION_BP
|
80 |
|
|
case CYGNUM_HAL_EXCEPTION_INSTRUCTION_BP:
|
81 |
|
|
#endif
|
82 |
|
|
return SIGTRAP;
|
83 |
|
|
|
84 |
|
|
case CYGNUM_HAL_EXCEPTION_POWERON:
|
85 |
|
|
case CYGNUM_HAL_EXCEPTION_RESET:
|
86 |
|
|
// Reset - given that the CPU resets if it gets confused we
|
87 |
|
|
// want to treat it as an interupt so the developer has a
|
88 |
|
|
// chance to find out what happened (as opposed to SIGTERM).
|
89 |
|
|
return SIGINT;
|
90 |
|
|
|
91 |
|
|
#ifdef CYGNUM_HAL_EXCEPTION_TLBMISS_ACCESS
|
92 |
|
|
case CYGNUM_HAL_EXCEPTION_TLBMISS_ACCESS:
|
93 |
|
|
case CYGNUM_HAL_EXCEPTION_TLBMISS_WRITE:
|
94 |
|
|
case CYGNUM_HAL_EXCEPTION_INITIAL_WRITE:
|
95 |
|
|
case CYGNUM_HAL_EXCEPTION_TLBERROR_ACCESS:
|
96 |
|
|
case CYGNUM_HAL_EXCEPTION_TLBERROR_WRITE:
|
97 |
|
|
return SIGSEGV;
|
98 |
|
|
|
99 |
|
|
case CYGNUM_HAL_EXCEPTION_DATA_ACCESS:
|
100 |
|
|
case CYGNUM_HAL_EXCEPTION_DATA_WRITE:
|
101 |
|
|
return SIGBUS;
|
102 |
|
|
#else
|
103 |
|
|
case CYGNUM_HAL_EXCEPTION_DATA_ACCESS:
|
104 |
|
|
case CYGNUM_HAL_EXCEPTION_DMA_DATA_ACCESS:
|
105 |
|
|
return SIGSEGV;
|
106 |
|
|
#endif
|
107 |
|
|
|
108 |
|
|
case CYGNUM_HAL_EXCEPTION_ILLEGAL_INSTRUCTION:
|
109 |
|
|
case CYGNUM_HAL_EXCEPTION_ILLEGAL_SLOT_INSTRUCTION:
|
110 |
|
|
return SIGILL;
|
111 |
|
|
|
112 |
|
|
default:
|
113 |
|
|
return SIGTERM;
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
/* Return the trap number corresponding to the last-taken trap. */
|
118 |
|
|
|
119 |
|
|
int
|
120 |
|
|
__get_trap_number(void)
|
121 |
|
|
{
|
122 |
|
|
// The vector is not not part of the GDB register set so get it
|
123 |
|
|
// directly from the save context.
|
124 |
|
|
return _hal_registers->event;
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
#if defined(CYGSEM_REDBOOT_BSP_SYSCALLS)
|
128 |
|
|
int __is_bsp_syscall(void)
|
129 |
|
|
{
|
130 |
|
|
if (_hal_registers->event == 34)
|
131 |
|
|
return 1;
|
132 |
|
|
else
|
133 |
|
|
return 0;
|
134 |
|
|
}
|
135 |
|
|
#endif
|
136 |
|
|
|
137 |
|
|
/* Set the currently-saved pc register value to PC. This also updates NPC
|
138 |
|
|
as needed. */
|
139 |
|
|
|
140 |
|
|
void
|
141 |
|
|
set_pc(target_register_t pc)
|
142 |
|
|
{
|
143 |
|
|
put_register(PC, pc);
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
#ifdef CYGARC_SH_MOD_UBC
|
147 |
|
|
|
148 |
|
|
// This implementation of the single-stepper relies on the User Break
|
149 |
|
|
// Controller which may not be available on all cores.
|
150 |
|
|
|
151 |
|
|
// Note: This should be enhanced to coorperate with either two regular
|
152 |
|
|
// breakpoints or watchpoints. Requires GDB to be aware of the stub's
|
153 |
|
|
// ability though, so for now just use channel A without further
|
154 |
|
|
// considerations.
|
155 |
|
|
|
156 |
|
|
/* Set things up so that the next user resume will execute one instruction.
|
157 |
|
|
This may be done by setting breakpoints or setting a single step flag
|
158 |
|
|
in the saved user registers, for example. */
|
159 |
|
|
|
160 |
|
|
void __single_step (void)
|
161 |
|
|
{
|
162 |
|
|
// The address of the instruction to execute.
|
163 |
|
|
HAL_WRITE_UINT32(CYGARC_REG_BARA, get_register(PC));
|
164 |
|
|
// Match entire address.
|
165 |
|
|
#if (CYGARC_SH_MOD_UBC == 1)
|
166 |
|
|
HAL_WRITE_UINT8(CYGARC_REG_BAMRA, CYGARC_REG_BAMRA_BARA_UNMASKED);
|
167 |
|
|
#elif (CYGARC_SH_MOD_UBC == 2)
|
168 |
|
|
// For some reason, matching all bits causes the system to hang
|
169 |
|
|
// (not just run amok - it appears to stop execution).
|
170 |
|
|
//HAL_WRITE_UINT32(CYGARC_REG_BAMRA, 0xffffffff);
|
171 |
|
|
HAL_WRITE_UINT32(CYGARC_REG_BAMRA, 0);
|
172 |
|
|
#endif
|
173 |
|
|
|
174 |
|
|
// Stop after instruction at matching address has executed.
|
175 |
|
|
#if (CYGARC_SH_MOD_UBC == 1)
|
176 |
|
|
HAL_WRITE_UINT16(CYGARC_REG_BRCR, CYGARC_REG_BRCR_ONE_STEP);
|
177 |
|
|
#else
|
178 |
|
|
HAL_WRITE_UINT32(CYGARC_REG_BRCR, CYGARC_REG_BRCR_ONE_STEP);
|
179 |
|
|
#endif
|
180 |
|
|
|
181 |
|
|
// Stop on IFETCH/READ
|
182 |
|
|
#if (CYGARC_SH_MOD_UBC == 1)
|
183 |
|
|
HAL_WRITE_UINT16(CYGARC_REG_BBRA,
|
184 |
|
|
CYGARC_REG_BBRA_IFETCH|CYGARC_REG_BBRA_READ);
|
185 |
|
|
#else
|
186 |
|
|
HAL_WRITE_UINT16(CYGARC_REG_BBRA,
|
187 |
|
|
CYGARC_REG_BBRA_CPU|CYGARC_REG_BBRA_IFETCH|CYGARC_REG_BBRA_READ);
|
188 |
|
|
#endif
|
189 |
|
|
|
190 |
|
|
#ifdef CYGPKG_HAL_SH_SH4
|
191 |
|
|
// Must execute at least 11 instructions before reaching
|
192 |
|
|
// any address that may be affected by the UBC settings.
|
193 |
|
|
asm volatile ("nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;");
|
194 |
|
|
#endif
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
/* Clear the single-step state. */
|
198 |
|
|
|
199 |
|
|
void __clear_single_step (void)
|
200 |
|
|
{
|
201 |
|
|
// Don't stop on any condition
|
202 |
|
|
HAL_WRITE_UINT16(CYGARC_REG_BBRA, 0);
|
203 |
|
|
// Clear status flags
|
204 |
|
|
HAL_WRITE_UINT16(CYGARC_REG_BRCR, 0);
|
205 |
|
|
|
206 |
|
|
#ifdef CYGPKG_HAL_SH_SH4
|
207 |
|
|
// Must execute at least 11 instructions before reaching
|
208 |
|
|
// any address that may be affected by the UBC settings.
|
209 |
|
|
asm volatile ("nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;");
|
210 |
|
|
#endif
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
#else // CYGARC_SH_MOD_UBC
|
214 |
|
|
|
215 |
|
|
/*----------------------------------------------------------------------
|
216 |
|
|
* Single-step support, copied from gdb/sh-stub.c, written by Ben Lee
|
217 |
|
|
* and Steve Chamberlain. Extended to handle braf, bsrf, bt/s and bf/s.
|
218 |
|
|
*/
|
219 |
|
|
|
220 |
|
|
#define COND_BRx_MASK 0xfb00
|
221 |
|
|
#define UCOND_DBR_MASK 0xe000
|
222 |
|
|
#define UCOND_RBR_MASK 0xf0df
|
223 |
|
|
#define TRAPA_MASK 0xff00
|
224 |
|
|
|
225 |
|
|
#define COND_DISP 0x00ff
|
226 |
|
|
#define UCOND_DISP 0x0fff
|
227 |
|
|
#define UCOND_REG 0x0f00
|
228 |
|
|
|
229 |
|
|
#define BFx_INSTR 0x8b00
|
230 |
|
|
#define BTx_INSTR 0x8900
|
231 |
|
|
#define BRA_INSTR 0xa000
|
232 |
|
|
#define BSR_INSTR 0xb000
|
233 |
|
|
#define JMP_INSTR 0x402b
|
234 |
|
|
#define JSR_INSTR 0x400b
|
235 |
|
|
#define RTS_INSTR 0x000b
|
236 |
|
|
#define RTE_INSTR 0x002b
|
237 |
|
|
#define TRAPA_INSTR 0xc300
|
238 |
|
|
#define BxxF_INSTR 0x0003
|
239 |
|
|
|
240 |
|
|
#define SSTEP_INSTR 0xc320
|
241 |
|
|
|
242 |
|
|
#define T_BIT_MASK 0x0001
|
243 |
|
|
|
244 |
|
|
typedef struct {
|
245 |
|
|
short *memAddr;
|
246 |
|
|
short oldInstr;
|
247 |
|
|
} stepData;
|
248 |
|
|
|
249 |
|
|
static stepData instrBuffer;
|
250 |
|
|
static char stepped;
|
251 |
|
|
|
252 |
|
|
/* Set things up so that the next user resume will execute one instruction.
|
253 |
|
|
This may be done by setting breakpoints or setting a single step flag
|
254 |
|
|
in the saved user registers, for example. */
|
255 |
|
|
|
256 |
|
|
void __single_step (void)
|
257 |
|
|
{
|
258 |
|
|
short *instrMem;
|
259 |
|
|
int displacement;
|
260 |
|
|
int reg;
|
261 |
|
|
unsigned short opcode;
|
262 |
|
|
|
263 |
|
|
cyg_uint32 pc = get_register(PC);
|
264 |
|
|
cyg_uint32 sr = get_register(SR);
|
265 |
|
|
|
266 |
|
|
instrMem = (short *) pc;
|
267 |
|
|
|
268 |
|
|
opcode = *instrMem;
|
269 |
|
|
stepped = 1;
|
270 |
|
|
|
271 |
|
|
if ((opcode & UCOND_RBR_MASK) == BxxF_INSTR) {
|
272 |
|
|
reg = (char) ((opcode & UCOND_REG) >> 8);
|
273 |
|
|
displacement = get_register(reg);
|
274 |
|
|
instrMem = (short *) (pc + displacement + 4);
|
275 |
|
|
/*
|
276 |
|
|
* Remember PC points to second instr.
|
277 |
|
|
* after PC of branch ... so add 4
|
278 |
|
|
*/
|
279 |
|
|
} else if ((opcode & COND_BRx_MASK) == BTx_INSTR) {
|
280 |
|
|
if (sr & T_BIT_MASK) {
|
281 |
|
|
displacement = (opcode & COND_DISP) << 1;
|
282 |
|
|
if (displacement & 0x80) {
|
283 |
|
|
displacement |= 0xffffff00;
|
284 |
|
|
}
|
285 |
|
|
/*
|
286 |
|
|
* Remember PC points to second instr.
|
287 |
|
|
* after PC of branch ... so add 4
|
288 |
|
|
*/
|
289 |
|
|
instrMem = (short *) (pc + displacement + 4);
|
290 |
|
|
} else {
|
291 |
|
|
instrMem += 1;
|
292 |
|
|
}
|
293 |
|
|
} else if ((opcode & COND_BRx_MASK) == BFx_INSTR) {
|
294 |
|
|
if (sr & T_BIT_MASK) {
|
295 |
|
|
instrMem += 1;
|
296 |
|
|
} else {
|
297 |
|
|
displacement = (opcode & COND_DISP) << 1;
|
298 |
|
|
if (displacement & 0x80)
|
299 |
|
|
displacement |= 0xffffff00;
|
300 |
|
|
/*
|
301 |
|
|
* Remember PC points to second instr.
|
302 |
|
|
* after PC of branch ... so add 4
|
303 |
|
|
*/
|
304 |
|
|
instrMem = (short *) (pc + displacement + 4);
|
305 |
|
|
}
|
306 |
|
|
} else if ((opcode & UCOND_DBR_MASK) == BRA_INSTR) {
|
307 |
|
|
displacement = (opcode & UCOND_DISP) << 1;
|
308 |
|
|
if (displacement & 0x0800) {
|
309 |
|
|
displacement |= 0xfffff000;
|
310 |
|
|
}
|
311 |
|
|
|
312 |
|
|
/*
|
313 |
|
|
* Remember PC points to second instr.
|
314 |
|
|
* after PC of branch ... so add 4
|
315 |
|
|
*/
|
316 |
|
|
instrMem = (short *) (pc + displacement + 4);
|
317 |
|
|
} else if ((opcode & UCOND_RBR_MASK) == JSR_INSTR) {
|
318 |
|
|
reg = (char) ((opcode & UCOND_REG) >> 8);
|
319 |
|
|
instrMem = (short *) get_register(reg);
|
320 |
|
|
} else if (opcode == RTS_INSTR) {
|
321 |
|
|
instrMem = (short *) get_register(PR);
|
322 |
|
|
} else if (opcode == RTE_INSTR) {
|
323 |
|
|
instrMem = (short *) get_register(SP);
|
324 |
|
|
} else if ((opcode & TRAPA_MASK) == TRAPA_INSTR) {
|
325 |
|
|
instrMem += 1; // skip traps
|
326 |
|
|
} else {
|
327 |
|
|
instrMem += 1;
|
328 |
|
|
}
|
329 |
|
|
|
330 |
|
|
instrBuffer.memAddr = instrMem;
|
331 |
|
|
instrBuffer.oldInstr = *instrMem;
|
332 |
|
|
*instrMem = SSTEP_INSTR;
|
333 |
|
|
}
|
334 |
|
|
|
335 |
|
|
/* Clear the single-step state. */
|
336 |
|
|
|
337 |
|
|
void __clear_single_step (void)
|
338 |
|
|
{
|
339 |
|
|
/* Undo the effect of a previous doSStep. If we single stepped,
|
340 |
|
|
restore the old instruction. */
|
341 |
|
|
|
342 |
|
|
if (stepped) {
|
343 |
|
|
short *instrMem;
|
344 |
|
|
instrMem = instrBuffer.memAddr;
|
345 |
|
|
*instrMem = instrBuffer.oldInstr;
|
346 |
|
|
}
|
347 |
|
|
stepped = 0;
|
348 |
|
|
}
|
349 |
|
|
|
350 |
|
|
#endif // CYGARC_SH_MOD_UBC
|
351 |
|
|
|
352 |
|
|
|
353 |
|
|
void __install_breakpoints (void)
|
354 |
|
|
{
|
355 |
|
|
/* NOP since single-step HW exceptions are used instead of
|
356 |
|
|
breakpoints. */
|
357 |
|
|
}
|
358 |
|
|
|
359 |
|
|
void __clear_breakpoints (void)
|
360 |
|
|
{
|
361 |
|
|
}
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
/* If the breakpoint we hit is in the breakpoint() instruction, return a
|
365 |
|
|
non-zero value. */
|
366 |
|
|
|
367 |
|
|
#ifdef linux
|
368 |
|
|
externC void _breakinst(void);
|
369 |
|
|
int
|
370 |
|
|
__is_breakpoint_function ()
|
371 |
|
|
{
|
372 |
|
|
return get_register(PC) == (target_register_t)&_breakinst;
|
373 |
|
|
}
|
374 |
|
|
#else
|
375 |
|
|
externC void breakinst(void);
|
376 |
|
|
int
|
377 |
|
|
__is_breakpoint_function ()
|
378 |
|
|
{
|
379 |
|
|
return get_register(PC) == (target_register_t)&breakinst;
|
380 |
|
|
}
|
381 |
|
|
#endif
|
382 |
|
|
|
383 |
|
|
/* Skip the current instruction. Since this is only called by the
|
384 |
|
|
stub when the PC points to a breakpoint or trap instruction,
|
385 |
|
|
we can safely just skip 2. */
|
386 |
|
|
void __skipinst (void)
|
387 |
|
|
{
|
388 |
|
|
put_register(PC, get_register (PC) + 2);
|
389 |
|
|
}
|
390 |
|
|
|
391 |
|
|
#endif // CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
|