]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/hal/powerpc/arch/v2_0/src/ppc_stub.c
Initial revision
[karo-tx-redboot.git] / packages / hal / powerpc / arch / v2_0 / src / ppc_stub.c
1 //========================================================================
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 #ifndef OFFSETOF
76 #define OFFSETOF(_struct_, _member_) (int)((char *)(&(((_struct_*)0)->_member_))-(char *)((_struct_*)0))
77 #endif
78
79 /* Given a trap value TRAP, return the corresponding signal. */
80
81 int __computeSignal (unsigned int trap_number)
82 {
83     switch (trap_number)
84     {
85     case CYGNUM_HAL_VECTOR_MACHINE_CHECK:
86         /* Machine check */
87     case CYGNUM_HAL_VECTOR_DSI:
88         /* Data access */
89         return SIGSEGV;
90       
91     case CYGNUM_HAL_VECTOR_ISI:
92         /* Instruction access (Ifetch) */
93     case CYGNUM_HAL_VECTOR_ALIGNMENT:
94         /* Data access */
95         return SIGBUS;
96                     
97     case CYGNUM_HAL_VECTOR_INTERRUPT:
98         /* External interrupt */
99       return SIGINT;
100
101     case CYGNUM_HAL_VECTOR_TRACE:
102         /* Instruction trace */
103         return SIGTRAP;
104       
105     case CYGNUM_HAL_VECTOR_PROGRAM:
106 #ifdef CYGPKG_HAL_POWERPC_PPC40x
107         // The 40x is b0rken, returning 0 for these bits. Translate to
108         // SIGTRAP to allow thread debugging.
109         return SIGTRAP;
110 #else
111         // The register PS contains the value of SRR1 at the time of
112         // exception entry. Bits 11-15 contain information about the
113         // cause of the exception. Bits 16-31 the PS (MSR) state.
114 #ifdef USE_BREAKPOINTS_FOR_SINGLE_STEP
115         if (__is_single_step(get_register(PC))) {
116             return SIGTRAP;
117         }
118 #endif
119         switch ((get_register (PS) >> 17) & 0xf){
120         case 1:                         /* trap */
121             return SIGTRAP;
122         case 2:                         /* privileged instruction */
123         case 4:                         /* illegal instruction */
124             return SIGILL;
125         case 8:                         /* floating point */
126             return SIGFPE;
127         default:                        /* should never happen! */
128             return SIGILL;
129         }            
130 #endif
131
132     case CYGNUM_HAL_VECTOR_RESERVED_A:
133     case CYGNUM_HAL_VECTOR_RESERVED_B:
134         return SIGILL;
135
136     case CYGNUM_HAL_VECTOR_FP_UNAVAILABLE:
137         /* FPU disabled */
138     case CYGNUM_HAL_VECTOR_FP_ASSIST:
139         /* FPU assist */
140         return SIGFPE;
141
142     case CYGNUM_HAL_VECTOR_DECREMENTER:
143         /* Decrementer alarm */
144         return SIGALRM;
145
146     case CYGNUM_HAL_VECTOR_SYSTEM_CALL:
147         /* System call */
148         return SIGSYS;
149
150 #if defined(CYGPKG_HAL_POWERPC_MPC8xx) || defined(CYGPKG_HAL_POWERPC_MPC5xx)
151     case CYGNUM_HAL_VECTOR_SW_EMUL:
152         /* A SW_EMUL is generated instead of PROGRAM for illegal
153            instructions. */
154         return SIGILL;
155
156     case CYGNUM_HAL_VECTOR_DATA_BP:
157     case CYGNUM_HAL_VECTOR_INSTRUCTION_BP:
158     case CYGNUM_HAL_VECTOR_PERIPHERAL_BP:
159     case CYGNUM_HAL_VECTOR_NMI:
160         /* Developer port debugging exceptions. */
161         return SIGTRAP;
162
163 #if defined(CYGNUM_HAL_VECTOR_ITLB_MISS)        
164     case CYGNUM_HAL_VECTOR_ITLB_MISS:
165         /* Software reload of TLB required. */
166         return SIGTRAP;
167 #endif
168 #if defined(CYGNUM_HAL_VECTOR_DTLB_MISS)        
169     case CYGNUM_HAL_VECTOR_DTLB_MISS:
170         /* Software reload of TLB required. */
171         return SIGTRAP;
172 #endif
173     case CYGNUM_HAL_VECTOR_ITLB_ERROR:
174         /* Invalid instruction access. */
175         return SIGBUS;
176
177     case CYGNUM_HAL_VECTOR_DTLB_ERROR:
178         /* Invalid data access. */
179         return SIGSEGV;
180 #endif // defined(CYGPKG_HAL_POWERPC_MPC8xx)
181         
182     default:
183         return SIGTERM;
184     }
185 }
186
187
188 /* Return the trap number corresponding to the last-taken trap. */
189
190 int __get_trap_number (void)
191 {
192     // The vector is not not part of the GDB register set so get it
193     // directly from the save context.
194     return _hal_registers->vector >> 8;
195 }
196
197 /* Set the currently-saved pc register value to PC. This also updates NPC
198    as needed. */
199
200 void set_pc (target_register_t pc)
201 {
202     put_register (PC, pc);
203 }
204
205 #ifdef CYGHWR_HAL_POWERPC_FPU
206 static int
207 reg_offset(regnames_t reg)
208 {
209   // We let the compiler determine the offsets in order to avoid all
210   // possible alignment problems
211   int base_offset;
212   // 32 general purpose registers
213   if(reg < F0)   return reg * 4;
214
215   // first sixteen floating point regs
216   base_offset = OFFSETOF(GDB_Registers, f0);
217   if(reg < F16)  return base_offset + ((reg - F0) * 8);
218
219   // last sixteen floating point regs
220   base_offset = OFFSETOF(GDB_Registers, f16);
221   if(reg < PC)   return base_offset + ((reg - F16) * 8);
222
223   // Other 32 bit regs
224   if(reg < PS)   return(OFFSETOF(GDB_Registers, pc));
225   if(reg < CND)  return(OFFSETOF(GDB_Registers, msr));
226   if(reg < LR)   return(OFFSETOF(GDB_Registers, cr));
227   if(reg < CNT)  return(OFFSETOF(GDB_Registers, lr));
228   if(reg < XER)  return(OFFSETOF(GDB_Registers, ctr));
229   if(reg < MQ)   return(OFFSETOF(GDB_Registers, xer));
230   
231   return OFFSETOF(GDB_Registers, mq);
232 }
233
234 // Return the currently-saved value corresponding to register REG of
235 // the exception context.
236 target_register_t
237 get_register (regnames_t reg)
238 {
239    target_register_t val;
240    int offset = reg_offset(reg);
241
242    if (REGSIZE(reg) > sizeof(target_register_t))
243    return -1;
244
245    val = _registers[offset/sizeof(target_register_t)];
246
247    return val;
248 }
249
250 // Store VALUE in the register corresponding to WHICH in the exception
251 // context.
252 void
253 put_register (regnames_t which, target_register_t value)
254 {
255    int offset = reg_offset(which);
256
257    if (REGSIZE(which) > sizeof(target_register_t))
258    return;
259
260    _registers[offset/sizeof(target_register_t)] = value;
261 }
262
263 // Write the contents of register WHICH into VALUE as raw bytes. This
264 // is only used for registers larger than sizeof(target_register_t).
265 // Return non-zero if it is a valid register.
266 int
267 get_register_as_bytes (regnames_t which, char *value)
268 {
269   int offset = reg_offset(which);
270
271   memcpy (value, (char *)_registers + offset, REGSIZE(which));
272   return 1;
273 }
274
275 // Alter the contents of saved register WHICH to contain VALUE. This
276 // is only used for registers larger than sizeof(target_register_t).
277 // Return non-zero if it is a valid register.
278 int
279 put_register_as_bytes (regnames_t which, char *value)
280 {
281   int offset = reg_offset(which);
282
283   memcpy ((char *)_registers + offset, value, REGSIZE(which));
284   return 1;
285 }
286 #endif
287
288 /*----------------------------------------------------------------------
289  * Single-step support
290  */
291
292 /* Set things up so that the next user resume will execute one instruction.
293    This may be done by setting breakpoints or setting a single step flag
294    in the saved user registers, for example. */
295
296 #ifdef USE_BREAKPOINTS_FOR_SINGLE_STEP
297
298 #if (HAL_BREAKINST_SIZE == 1)
299 typedef cyg_uint8 t_inst;
300 #elif (HAL_BREAKINST_SIZE == 2)
301 typedef cyg_uint16 t_inst;
302 #elif (HAL_BREAKINST_SIZE == 4)
303 typedef cyg_uint32 t_inst;
304 #else
305 #error "Don't know how to handle that size"
306 #endif
307
308 typedef struct
309 {
310   t_inst *targetAddr;
311   t_inst savedInstr;
312 } instrBuffer;
313
314 static instrBuffer sstep_instr[2];
315 static target_register_t irq_state = 0;
316
317 static void 
318 __insert_break(int indx, target_register_t pc)
319 {
320     sstep_instr[indx].targetAddr = (t_inst *)pc;
321     sstep_instr[indx].savedInstr = *(t_inst *)pc;
322     *(t_inst*)pc = (t_inst)HAL_BREAKINST;
323     __data_cache(CACHE_FLUSH);
324     __instruction_cache(CACHE_FLUSH);
325 }
326
327 static void 
328 __remove_break(int indx)
329 {
330     if (sstep_instr[indx].targetAddr != 0) {
331         *(sstep_instr[indx].targetAddr) = sstep_instr[indx].savedInstr;
332         sstep_instr[indx].targetAddr = 0;
333         __data_cache(CACHE_FLUSH);
334         __instruction_cache(CACHE_FLUSH);
335     }
336 }
337
338 int
339 __is_single_step(target_register_t pc)
340 {
341     return (sstep_instr[0].targetAddr == (t_inst *)pc) ||
342         (sstep_instr[1].targetAddr == (t_inst *)pc);
343 }
344
345
346 // Compute the target address for this instruction, if the instruction
347 // is some sort of branch/flow change.
348
349 struct xl_form {
350     unsigned int op : 6;
351     unsigned int bo : 5;
352     unsigned int bi : 5;
353     unsigned int reserved : 5;
354     unsigned int xo : 10;
355     unsigned int lk : 1;
356 };
357
358 struct i_form {
359     unsigned int op : 6;
360     signed   int li : 24;
361     unsigned int aa : 1;
362     unsigned int lk : 1;
363 };
364
365 struct b_form {
366     unsigned int op : 6;
367     unsigned int bo : 5;
368     unsigned int bi : 5;
369     signed   int bd : 14;
370     unsigned int aa : 1;
371     unsigned int lk : 1;
372 };
373
374 union ppc_insn {
375     unsigned int   word;
376     struct i_form  i;
377     struct b_form  b;
378     struct xl_form xl;
379 };
380
381 static target_register_t
382 __branch_pc(target_register_t pc)
383 {
384     union ppc_insn insn;
385
386     insn.word = *(t_inst *)pc;
387
388     // Decode the instruction to determine the instruction which will follow
389     // Note: there are holes in this process, but the important ones work
390     switch (insn.i.op) {
391     case 16:
392         /* bcx */
393         if (insn.b.aa) {
394             return (target_register_t)(insn.b.bd << 2);
395         } else {
396             return (target_register_t)((insn.b.bd << 2) + (long)pc);
397         }
398     case 18:
399         /* bx */
400         if (insn.i.aa) {
401             return (target_register_t)(insn.i.li << 2);
402         } else {
403             return (target_register_t)((insn.i.li << 2) + (long)pc);
404         }
405     case 19:
406         if (insn.xl.reserved == 0) {
407             if (insn.xl.xo == 528) {
408                 /* bcctrx */
409                 return (target_register_t)(get_register(CNT) & ~3);
410             } else if (insn.xl.xo == 16) {
411                 /* bclrx */
412                 return (target_register_t)(get_register(LR) & ~3);
413             }
414         }
415         break;
416     default:
417         break;
418     }
419     return (pc+4);
420 }
421
422 void __single_step(void)
423 {
424     target_register_t msr = get_register(PS);
425     target_register_t pc = get_register(PC);
426     target_register_t next_pc = __branch_pc(pc);
427
428     // Disable interrupts.
429     irq_state = msr & MSR_EE;
430     msr &= ~MSR_EE;
431     put_register (PS, msr);
432
433     // Set a breakpoint at the next instruction
434     __insert_break(0, pc+4);
435     if (next_pc != (pc+4)) {
436         __insert_break(1, next_pc);
437     }
438 }
439
440 /* Clear the single-step state. */
441
442 void __clear_single_step(void)
443 {
444     target_register_t msr = get_register (PS);
445
446     // Restore interrupt state.
447     // FIXME: Should check whether the executed instruction changed the
448     // interrupt state - or single-stepping a MSR changing instruction
449     // may result in a wrong EE. Not a very likely scenario though.
450     msr |= irq_state;
451
452     // This function is called much more than its counterpart
453     // __single_step.  Only re-enable interrupts if they where
454     // disabled during the previous cal to __single_step. Otherwise,
455     // this function only makes "extra sure" that no trace or branch
456     // exception will happen.
457     irq_state = 0;
458
459     put_register (PS, msr);
460
461     // Remove breakpoints
462     __remove_break(0);
463     __remove_break(1);
464 }
465
466 #else
467
468 static target_register_t irq_state = 0;
469
470 void __single_step (void)
471 {
472     target_register_t msr = get_register (PS);
473
474     // Set single-step flag in the exception context.
475     msr |= (MSR_SE | MSR_BE);
476     // Disable interrupts.
477     irq_state = msr & MSR_EE;
478     msr &= ~MSR_EE;
479
480     put_register (PS, msr);
481 }
482
483 /* Clear the single-step state. */
484
485 void __clear_single_step (void)
486 {
487     target_register_t msr = get_register (PS);
488
489     // Clear single-step flag in the exception context.
490     msr &= ~(MSR_SE | MSR_BE);
491     // Restore interrupt state.
492     // FIXME: Should check whether the executed instruction changed the
493     // interrupt state - or single-stepping a MSR changing instruction
494     // may result in a wrong EE. Not a very likely scenario though.
495     msr |= irq_state;
496
497     // This function is called much more than its counterpart
498     // __single_step.  Only re-enable interrupts if they where
499     // disabled during the previous cal to __single_step. Otherwise,
500     // this function only makes "extra sure" that no trace or branch
501     // exception will happen.
502     irq_state = 0;
503
504     put_register (PS, msr);
505 }
506 #endif
507
508 void __install_breakpoints (void)
509 {
510     /* NOP since single-step HW exceptions are used instead of
511        breakpoints. */
512 }
513
514 void __clear_breakpoints (void)
515 {
516 }
517
518
519 /* If the breakpoint we hit is in the breakpoint() instruction, return a
520    non-zero value. */
521
522 int
523 __is_breakpoint_function ()
524 {
525     return get_register (PC) == (target_register_t)&_breakinst;
526 }
527
528
529 /* Skip the current instruction.  Since this is only called by the
530    stub when the PC points to a breakpoint or trap instruction,
531    we can safely just skip 4. */
532
533 void __skipinst (void)
534 {
535     put_register (PC, get_register (PC) + 4);
536 }
537
538 #endif // CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS