OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [ecos-2.0/] [packages/] [hal/] [powerpc/] [arch/] [v2_0/] [src/] [ppc_stub.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1254 phoenix
//========================================================================
2
//
3
//      ppc_stub.c
4
//
5
//      Helper functions for stub, generic to all PowerPC processors
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):     Red Hat, jskov
44
// Contributors:  Red Hat, jskov, gthomas
45
// Date:          1998-08-20
46
// Purpose:       
47
// Description:   Helper functions for stub, generic to all PowerPC processors
48
// Usage:         
49
//
50
//####DESCRIPTIONEND####
51
//
52
//========================================================================
53
 
54
#include <stddef.h>
55
 
56
#include <pkgconf/hal.h>
57
 
58
#ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
59
 
60
#define CYGARC_HAL_COMMON_EXPORT_CPU_MACROS
61
#include <cyg/hal/ppc_regs.h>
62
 
63
#include <cyg/hal/hal_stub.h>
64
#include <cyg/hal/hal_arch.h>
65
#include <cyg/hal/hal_intr.h>
66
 
67
#ifdef CYGNUM_HAL_NO_VECTOR_TRACE
68
#define USE_BREAKPOINTS_FOR_SINGLE_STEP
69
#endif
70
 
71
#ifdef CYGDBG_HAL_DEBUG_GDB_THREAD_SUPPORT
72
#include <cyg/hal/dbg-threads-api.h>    // dbg_currthread_id
73
#endif
74
 
75
/* Given a trap value TRAP, return the corresponding signal. */
76
 
77
int __computeSignal (unsigned int trap_number)
78
{
79
    switch (trap_number)
80
    {
81
    case CYGNUM_HAL_VECTOR_MACHINE_CHECK:
82
        /* Machine check */
83
    case CYGNUM_HAL_VECTOR_DSI:
84
        /* Data access */
85
        return SIGSEGV;
86
 
87
    case CYGNUM_HAL_VECTOR_ISI:
88
        /* Instruction access (Ifetch) */
89
    case CYGNUM_HAL_VECTOR_ALIGNMENT:
90
        /* Data access */
91
        return SIGBUS;
92
 
93
    case CYGNUM_HAL_VECTOR_INTERRUPT:
94
        /* External interrupt */
95
      return SIGINT;
96
 
97
    case CYGNUM_HAL_VECTOR_TRACE:
98
        /* Instruction trace */
99
        return SIGTRAP;
100
 
101
    case CYGNUM_HAL_VECTOR_PROGRAM:
102
#ifdef CYGPKG_HAL_POWERPC_PPC40x
103
        // The 40x is b0rken, returning 0 for these bits. Translate to
104
        // SIGTRAP to allow thread debugging.
105
        return SIGTRAP;
106
#else
107
        // The register PS contains the value of SRR1 at the time of
108
        // exception entry. Bits 11-15 contain information about the
109
        // cause of the exception. Bits 16-31 the PS (MSR) state.
110
#ifdef USE_BREAKPOINTS_FOR_SINGLE_STEP
111
        if (__is_single_step(get_register(PC))) {
112
            return SIGTRAP;
113
        }
114
#endif
115
        switch ((get_register (PS) >> 17) & 0xf){
116
        case 1:                         /* trap */
117
            return SIGTRAP;
118
        case 2:                         /* privileged instruction */
119
        case 4:                         /* illegal instruction */
120
            return SIGILL;
121
        case 8:                         /* floating point */
122
            return SIGFPE;
123
        default:                        /* should never happen! */
124
            return SIGILL;
125
        }
126
#endif
127
 
128
    case CYGNUM_HAL_VECTOR_RESERVED_A:
129
    case CYGNUM_HAL_VECTOR_RESERVED_B:
130
        return SIGILL;
131
 
132
    case CYGNUM_HAL_VECTOR_FP_UNAVAILABLE:
133
        /* FPU disabled */
134
    case CYGNUM_HAL_VECTOR_FP_ASSIST:
135
        /* FPU assist */
136
        return SIGFPE;
137
 
138
    case CYGNUM_HAL_VECTOR_DECREMENTER:
139
        /* Decrementer alarm */
140
        return SIGALRM;
141
 
142
    case CYGNUM_HAL_VECTOR_SYSTEM_CALL:
143
        /* System call */
144
        return SIGSYS;
145
 
146
#if defined(CYGPKG_HAL_POWERPC_MPC8xx) || defined(CYGPKG_HAL_POWERPC_MPC5xx)
147
    case CYGNUM_HAL_VECTOR_SW_EMUL:
148
        /* A SW_EMUL is generated instead of PROGRAM for illegal
149
           instructions. */
150
        return SIGILL;
151
 
152
    case CYGNUM_HAL_VECTOR_DATA_BP:
153
    case CYGNUM_HAL_VECTOR_INSTRUCTION_BP:
154
    case CYGNUM_HAL_VECTOR_PERIPHERAL_BP:
155
    case CYGNUM_HAL_VECTOR_NMI:
156
        /* Developer port debugging exceptions. */
157
        return SIGTRAP;
158
 
159
#if defined(CYGNUM_HAL_VECTOR_ITLB_MISS)        
160
    case CYGNUM_HAL_VECTOR_ITLB_MISS:
161
        /* Software reload of TLB required. */
162
        return SIGTRAP;
163
#endif
164
#if defined(CYGNUM_HAL_VECTOR_DTLB_MISS)        
165
    case CYGNUM_HAL_VECTOR_DTLB_MISS:
166
        /* Software reload of TLB required. */
167
        return SIGTRAP;
168
#endif
169
    case CYGNUM_HAL_VECTOR_ITLB_ERROR:
170
        /* Invalid instruction access. */
171
        return SIGBUS;
172
 
173
    case CYGNUM_HAL_VECTOR_DTLB_ERROR:
174
        /* Invalid data access. */
175
        return SIGSEGV;
176
#endif // defined(CYGPKG_HAL_POWERPC_MPC8xx)
177
 
178
    default:
179
        return SIGTERM;
180
    }
181
}
182
 
183
 
184
/* Return the trap number corresponding to the last-taken trap. */
185
 
186
int __get_trap_number (void)
187
{
188
    // The vector is not not part of the GDB register set so get it
189
    // directly from the save context.
190
    return _hal_registers->vector >> 8;
191
}
192
 
193
/* Set the currently-saved pc register value to PC. This also updates NPC
194
   as needed. */
195
 
196
void set_pc (target_register_t pc)
197
{
198
    put_register (PC, pc);
199
}
200
 
201
 
202
/*----------------------------------------------------------------------
203
 * Single-step support
204
 */
205
 
206
/* Set things up so that the next user resume will execute one instruction.
207
   This may be done by setting breakpoints or setting a single step flag
208
   in the saved user registers, for example. */
209
 
210
#ifdef USE_BREAKPOINTS_FOR_SINGLE_STEP
211
 
212
#if (HAL_BREAKINST_SIZE == 1)
213
typedef cyg_uint8 t_inst;
214
#elif (HAL_BREAKINST_SIZE == 2)
215
typedef cyg_uint16 t_inst;
216
#elif (HAL_BREAKINST_SIZE == 4)
217
typedef cyg_uint32 t_inst;
218
#else
219
#error "Don't know how to handle that size"
220
#endif
221
 
222
typedef struct
223
{
224
  t_inst *targetAddr;
225
  t_inst savedInstr;
226
} instrBuffer;
227
 
228
static instrBuffer sstep_instr[2];
229
static target_register_t irq_state = 0;
230
 
231
static void
232
__insert_break(int indx, target_register_t pc)
233
{
234
    sstep_instr[indx].targetAddr = (t_inst *)pc;
235
    sstep_instr[indx].savedInstr = *(t_inst *)pc;
236
    *(t_inst*)pc = (t_inst)HAL_BREAKINST;
237
    __data_cache(CACHE_FLUSH);
238
    __instruction_cache(CACHE_FLUSH);
239
}
240
 
241
static void
242
__remove_break(int indx)
243
{
244
    if (sstep_instr[indx].targetAddr != 0) {
245
        *(sstep_instr[indx].targetAddr) = sstep_instr[indx].savedInstr;
246
        sstep_instr[indx].targetAddr = 0;
247
        __data_cache(CACHE_FLUSH);
248
        __instruction_cache(CACHE_FLUSH);
249
    }
250
}
251
 
252
int
253
__is_single_step(target_register_t pc)
254
{
255
    return (sstep_instr[0].targetAddr == pc) ||
256
        (sstep_instr[1].targetAddr == pc);
257
}
258
 
259
 
260
// Compute the target address for this instruction, if the instruction
261
// is some sort of branch/flow change.
262
 
263
struct xl_form {
264
    unsigned int op : 6;
265
    unsigned int bo : 5;
266
    unsigned int bi : 5;
267
    unsigned int reserved : 5;
268
    unsigned int xo : 10;
269
    unsigned int lk : 1;
270
};
271
 
272
struct i_form {
273
    unsigned int op : 6;
274
    signed   int li : 24;
275
    unsigned int aa : 1;
276
    unsigned int lk : 1;
277
};
278
 
279
struct b_form {
280
    unsigned int op : 6;
281
    unsigned int bo : 5;
282
    unsigned int bi : 5;
283
    signed   int bd : 14;
284
    unsigned int aa : 1;
285
    unsigned int lk : 1;
286
};
287
 
288
union ppc_insn {
289
    unsigned int   word;
290
    struct i_form  i;
291
    struct b_form  b;
292
    struct xl_form xl;
293
};
294
 
295
static target_register_t
296
__branch_pc(target_register_t pc)
297
{
298
    union ppc_insn insn;
299
 
300
    insn.word = *(t_inst *)pc;
301
 
302
    // Decode the instruction to determine the instruction which will follow
303
    // Note: there are holes in this process, but the important ones work
304
    switch (insn.i.op) {
305
    case 16:
306
        /* bcx */
307
        if (insn.b.aa) {
308
            return (target_register_t)(insn.b.bd << 2);
309
        } else {
310
            return (target_register_t)((insn.b.bd << 2) + (long)pc);
311
        }
312
    case 18:
313
        /* bx */
314
        if (insn.i.aa) {
315
            return (target_register_t)(insn.i.li << 2);
316
        } else {
317
            return (target_register_t)((insn.i.li << 2) + (long)pc);
318
        }
319
    case 19:
320
        if (insn.xl.reserved == 0) {
321
            if (insn.xl.xo == 528) {
322
                /* bcctrx */
323
                return (target_register_t)(get_register(CNT) & ~3);
324
            } else if (insn.xl.xo == 16) {
325
                /* bclrx */
326
                return (target_register_t)(get_register(LR) & ~3);
327
            }
328
        }
329
        break;
330
    default:
331
        break;
332
    }
333
    return (pc+4);
334
}
335
 
336
void __single_step(void)
337
{
338
    target_register_t msr = get_register(PS);
339
    target_register_t pc = get_register(PC);
340
    target_register_t next_pc = __branch_pc(pc);
341
 
342
    // Disable interrupts.
343
    irq_state = msr & MSR_EE;
344
    msr &= ~MSR_EE;
345
    put_register (PS, msr);
346
 
347
    // Set a breakpoint at the next instruction
348
    __insert_break(0, pc+4);
349
    if (next_pc != (pc+4)) {
350
        __insert_break(1, next_pc);
351
    }
352
}
353
 
354
/* Clear the single-step state. */
355
 
356
void __clear_single_step(void)
357
{
358
    target_register_t msr = get_register (PS);
359
 
360
    // Restore interrupt state.
361
    // FIXME: Should check whether the executed instruction changed the
362
    // interrupt state - or single-stepping a MSR changing instruction
363
    // may result in a wrong EE. Not a very likely scenario though.
364
    msr |= irq_state;
365
 
366
    // This function is called much more than its counterpart
367
    // __single_step.  Only re-enable interrupts if they where
368
    // disabled during the previous cal to __single_step. Otherwise,
369
    // this function only makes "extra sure" that no trace or branch
370
    // exception will happen.
371
    irq_state = 0;
372
 
373
    put_register (PS, msr);
374
 
375
    // Remove breakpoints
376
    __remove_break(0);
377
    __remove_break(1);
378
}
379
 
380
#else
381
 
382
static target_register_t irq_state = 0;
383
 
384
void __single_step (void)
385
{
386
    target_register_t msr = get_register (PS);
387
 
388
    // Set single-step flag in the exception context.
389
    msr |= (MSR_SE | MSR_BE);
390
    // Disable interrupts.
391
    irq_state = msr & MSR_EE;
392
    msr &= ~MSR_EE;
393
 
394
    put_register (PS, msr);
395
}
396
 
397
/* Clear the single-step state. */
398
 
399
void __clear_single_step (void)
400
{
401
    target_register_t msr = get_register (PS);
402
 
403
    // Clear single-step flag in the exception context.
404
    msr &= ~(MSR_SE | MSR_BE);
405
    // Restore interrupt state.
406
    // FIXME: Should check whether the executed instruction changed the
407
    // interrupt state - or single-stepping a MSR changing instruction
408
    // may result in a wrong EE. Not a very likely scenario though.
409
    msr |= irq_state;
410
 
411
    // This function is called much more than its counterpart
412
    // __single_step.  Only re-enable interrupts if they where
413
    // disabled during the previous cal to __single_step. Otherwise,
414
    // this function only makes "extra sure" that no trace or branch
415
    // exception will happen.
416
    irq_state = 0;
417
 
418
    put_register (PS, msr);
419
}
420
#endif
421
 
422
void __install_breakpoints (void)
423
{
424
    /* NOP since single-step HW exceptions are used instead of
425
       breakpoints. */
426
}
427
 
428
void __clear_breakpoints (void)
429
{
430
}
431
 
432
 
433
/* If the breakpoint we hit is in the breakpoint() instruction, return a
434
   non-zero value. */
435
 
436
int
437
__is_breakpoint_function ()
438
{
439
    return get_register (PC) == (target_register_t)&_breakinst;
440
}
441
 
442
 
443
/* Skip the current instruction.  Since this is only called by the
444
   stub when the PC points to a breakpoint or trap instruction,
445
   we can safely just skip 4. */
446
 
447
void __skipinst (void)
448
{
449
    put_register (PC, get_register (PC) + 4);
450
}
451
 
452
#endif // CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.