source: rtems/cpukit/score/cpu/i386/cpu.c @ 020363d

4.104.11
Last change on this file since 020363d was 020363d, checked in by Till Straumann <strauman@…>, on Oct 20, 2009 at 2:51:37 PM

2009-10-20 Till Straumann <strauman@…>

  • score/cpu/i386/cpu.c, score/cpu/i386/cpu.h: let the default exception handler print a stack trace.
  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 *  Intel i386 Dependent Source
3 *
4 *
5 *  COPYRIGHT (c) 1989-1999.
6 *  On-Line Applications Research Corporation (OAR).
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.rtems.com/license/LICENSE.
11 *
12 *  $Id$
13 */
14
15#include <rtems.h>
16#include <rtems/system.h>
17#include <rtems/score/types.h>
18#include <rtems/score/isr.h>
19#include <rtems/score/idtr.h>
20
21#include <rtems/bspIo.h>
22#include <rtems/score/thread.h>
23
24/*  _CPU_Initialize
25 *
26 *  This routine performs processor dependent initialization.
27 *
28 *  INPUT PARAMETERS: NONE
29 */
30
31void _CPU_Initialize(void)
32{
33#if CPU_HARDWARE_FP
34  register uint16_t             fp_status asm ("ax");
35  register Context_Control_fp  *fp_context;
36#endif
37
38  /*
39   *  The following code saves a NULL i387 context which is given
40   *  to each task at start and restart time.  The following code
41   *  is based upon that provided in the i386 Programmer's
42   *  Manual and should work on any coprocessor greater than
43   *  the i80287.
44   *
45   *  NOTE: The NO WAIT form of the coprocessor instructions
46   *        MUST be used in case there is not a coprocessor
47   *        to wait for.
48   */
49
50#if CPU_HARDWARE_FP
51  fp_status = 0xa5a5;
52  asm volatile( "fninit" );
53  asm volatile( "fnstsw %0" : "=a" (fp_status) : "0" (fp_status) );
54
55  if ( fp_status ==  0 ) {
56
57    fp_context = &_CPU_Null_fp_context;
58
59    asm volatile( "fsave (%0)" : "=r" (fp_context)
60                               : "0"  (fp_context)
61                );
62  }
63#endif
64}
65
66/*PAGE
67 *
68 *  _CPU_ISR_Get_level
69 */
70
71uint32_t   _CPU_ISR_Get_level( void )
72{
73  uint32_t   level;
74
75  i386_get_interrupt_level( level );
76
77  return level;
78}
79
80void *_CPU_Thread_Idle_body( uintptr_t ignored )
81{
82  while(1){
83    asm volatile ("hlt");
84  }
85  return NULL;
86}
87
88struct Frame_ {
89        struct Frame_  *up;
90        uintptr_t               pc;
91};
92
93void _defaultExcHandler (CPU_Exception_frame *ctx)
94{
95  unsigned int faultAddr = 0;
96  printk("----------------------------------------------------------\n");
97  printk("Exception %d caught at PC %x by thread %d\n",
98         ctx->idtIndex,
99         ctx->eip,
100         _Thread_Executing->Object.id);
101  printk("----------------------------------------------------------\n");
102  printk("Processor execution context at time of the fault was  :\n");
103  printk("----------------------------------------------------------\n");
104  printk(" EAX = %x     EBX = %x        ECX = %x        EDX = %x\n",
105         ctx->eax, ctx->ebx, ctx->ecx, ctx->edx);
106  printk(" ESI = %x     EDI = %x        EBP = %x        ESP = %x\n",
107         ctx->esi, ctx->edi, ctx->ebp, ctx->esp0);
108  printk("----------------------------------------------------------\n");
109  printk("Error code pushed by processor itself (if not 0) = %x\n",
110         ctx->faultCode);
111  printk("----------------------------------------------------------\n");
112  if (ctx->idtIndex == I386_EXCEPTION_PAGE_FAULT){
113    faultAddr = i386_get_cr2();
114    printk("Page fault linear address (CR2) = %x\n", faultAddr);
115    printk("----------------------------------------------------------\n\n");
116  }
117 if (_ISR_Nest_level > 0) {
118    /*
119     * In this case we shall not delete the task interrupted as
120     * it has nothing to do with the fault. We cannot return either
121     * because the eip points to the faulty instruction so...
122     */
123    printk("Exception while executing ISR!!!. System locked\n");
124    _CPU_Fatal_halt(faultAddr);
125  }
126  else {
127        struct Frame_ *fp = (struct Frame_*)ctx->ebp;
128        int           i;
129
130        printk("Call Stack Trace of EIP:\n");
131        if ( fp ) {
132                for ( i=1; fp->up; fp=fp->up, i++ ) {
133                        printk("0x%08x ",fp->pc);
134                        if ( ! (i&3) )
135                                printk("\n");
136                }
137        }
138        printk("\n");
139    /*
140     * OK I could probably use a simplified version but at least this
141     * should work.
142     */
143    printk(" ************ FAULTY THREAD WILL BE SUSPENDED **************\n");
144    rtems_task_suspend(_Thread_Executing->Object.id);
145  }
146}
147
148cpuExcHandlerType _currentExcHandler = _defaultExcHandler;
149
150extern void rtems_exception_prologue_0(void);
151extern void rtems_exception_prologue_1(void);
152extern void rtems_exception_prologue_2(void);
153extern void rtems_exception_prologue_3(void);
154extern void rtems_exception_prologue_4(void);
155extern void rtems_exception_prologue_5(void);
156extern void rtems_exception_prologue_6(void);
157extern void rtems_exception_prologue_7(void);
158extern void rtems_exception_prologue_8(void);
159extern void rtems_exception_prologue_9(void);
160extern void rtems_exception_prologue_10(void);
161extern void rtems_exception_prologue_11(void);
162extern void rtems_exception_prologue_12(void);
163extern void rtems_exception_prologue_13(void);
164extern void rtems_exception_prologue_14(void);
165extern void rtems_exception_prologue_16(void);
166extern void rtems_exception_prologue_17(void);
167extern void rtems_exception_prologue_18(void);
168
169static rtems_raw_irq_hdl tbl[] = {
170         rtems_exception_prologue_0,
171         rtems_exception_prologue_1,
172         rtems_exception_prologue_2,
173         rtems_exception_prologue_3,
174         rtems_exception_prologue_4,
175         rtems_exception_prologue_5,
176         rtems_exception_prologue_6,
177         rtems_exception_prologue_7,
178         rtems_exception_prologue_8,
179         rtems_exception_prologue_9,
180         rtems_exception_prologue_10,
181         rtems_exception_prologue_11,
182         rtems_exception_prologue_12,
183         rtems_exception_prologue_13,
184         rtems_exception_prologue_14,
185         rtems_exception_prologue_16,
186         rtems_exception_prologue_17,
187         rtems_exception_prologue_18,
188};
189
190void rtems_exception_init_mngt(void)
191{
192      size_t                     i,j;
193      interrupt_gate_descriptor  *currentIdtEntry;
194      unsigned                   limit;
195      unsigned                   level;
196
197      i = sizeof(tbl) / sizeof (rtems_raw_irq_hdl);
198
199      i386_get_info_from_IDTR (&currentIdtEntry, &limit);
200
201      _CPU_ISR_Disable(level);
202      for (j = 0; j < i; j++) {
203        create_interrupt_gate_descriptor (&currentIdtEntry[j], tbl[j]);
204      }
205      _CPU_ISR_Enable(level);
206}
Note: See TracBrowser for help on using the repository browser.