1 | /* |
---|
2 | ************************************************************************** |
---|
3 | * |
---|
4 | * Component = |
---|
5 | * |
---|
6 | * Synopsis = rdbg/i386/excep.c |
---|
7 | * |
---|
8 | ************************************************************************** |
---|
9 | */ |
---|
10 | |
---|
11 | #include <rtems.h> |
---|
12 | #include <rtems/error.h> |
---|
13 | #include <rdbg/rdbg_f.h> |
---|
14 | #include <assert.h> |
---|
15 | #include <errno.h> |
---|
16 | #include <rdbg/rdbg.h> |
---|
17 | #include <rdbg/servrpc.h> |
---|
18 | |
---|
19 | |
---|
20 | extern rtems_id serializeSemId; |
---|
21 | extern rtems_id wakeupEventSemId; |
---|
22 | |
---|
23 | |
---|
24 | unsigned int NbExceptCtx; |
---|
25 | volatile unsigned int NbSerializedCtx; |
---|
26 | Exception_context *FirstCtx = NULL; |
---|
27 | Exception_context *LastCtx = NULL; |
---|
28 | |
---|
29 | CPU_Exception_frame SavedContext; |
---|
30 | |
---|
31 | |
---|
32 | /********* Save an exception context at the end of a list *****/ |
---|
33 | |
---|
34 | int PushExceptCtx ( Objects_Id Id, Objects_Id semId, CPU_Exception_frame *ctx ) { |
---|
35 | |
---|
36 | Exception_context *SaveCtx; |
---|
37 | |
---|
38 | SaveCtx = (Exception_context *)malloc(sizeof(Exception_context)); |
---|
39 | if (SaveCtx == NULL) |
---|
40 | rtems_panic("Can't allocate memory to save Exception context"); |
---|
41 | |
---|
42 | SaveCtx->id = Id; |
---|
43 | SaveCtx->ctx = ctx; |
---|
44 | SaveCtx->semaphoreId = semId; |
---|
45 | SaveCtx->previous = NULL; |
---|
46 | SaveCtx->next = NULL; |
---|
47 | |
---|
48 | if (FirstCtx == NULL){ /* initialization */ |
---|
49 | FirstCtx = SaveCtx; |
---|
50 | LastCtx = SaveCtx; |
---|
51 | NbExceptCtx = 1; |
---|
52 | } |
---|
53 | else { |
---|
54 | NbExceptCtx ++; |
---|
55 | LastCtx->next = SaveCtx; |
---|
56 | SaveCtx->previous = LastCtx; |
---|
57 | LastCtx = SaveCtx; |
---|
58 | } |
---|
59 | return 0; |
---|
60 | } |
---|
61 | |
---|
62 | /********* Save an temporary exception context in a ******/ |
---|
63 | /********* global variable ******/ |
---|
64 | |
---|
65 | int PushSavedExceptCtx ( Objects_Id Id, CPU_Exception_frame *ctx ) { |
---|
66 | |
---|
67 | memcpy (&(SavedContext), ctx, sizeof(CPU_Exception_frame)); |
---|
68 | return 0; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | /****** Remove the context of the specified Id thread *********/ |
---|
73 | /****** If Id = -1, then return the first context *********/ |
---|
74 | |
---|
75 | |
---|
76 | int PopExceptCtx ( Objects_Id Id ) { |
---|
77 | |
---|
78 | Exception_context *ExtractCtx; |
---|
79 | |
---|
80 | if (FirstCtx == NULL) return -1; |
---|
81 | |
---|
82 | if (Id == -1) { |
---|
83 | ExtractCtx = LastCtx; |
---|
84 | LastCtx = LastCtx->previous; |
---|
85 | free(ExtractCtx); |
---|
86 | NbExceptCtx --; |
---|
87 | return 0; |
---|
88 | } |
---|
89 | |
---|
90 | ExtractCtx = LastCtx; |
---|
91 | |
---|
92 | while (ExtractCtx->id != Id && ExtractCtx != NULL) { |
---|
93 | ExtractCtx = ExtractCtx->previous; |
---|
94 | } |
---|
95 | |
---|
96 | if (ExtractCtx == NULL) |
---|
97 | return -1; |
---|
98 | |
---|
99 | if ( ExtractCtx->previous != NULL) |
---|
100 | (ExtractCtx->previous)->next = ExtractCtx->next; |
---|
101 | |
---|
102 | if ( ExtractCtx->next != NULL) |
---|
103 | (ExtractCtx->next)->previous = ExtractCtx->previous; |
---|
104 | |
---|
105 | if (ExtractCtx == FirstCtx) |
---|
106 | FirstCtx = FirstCtx->next; |
---|
107 | else |
---|
108 | if (ExtractCtx == LastCtx) |
---|
109 | LastCtx = LastCtx->previous; |
---|
110 | |
---|
111 | free(ExtractCtx); |
---|
112 | NbExceptCtx --; |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|
116 | /****** Return the context of the specified Id thread *********/ |
---|
117 | /****** If Id = -1, then return the first context *********/ |
---|
118 | |
---|
119 | |
---|
120 | Exception_context *GetExceptCtx ( Objects_Id Id ) { |
---|
121 | |
---|
122 | Exception_context *ExtractCtx; |
---|
123 | |
---|
124 | if (FirstCtx == NULL) return NULL; |
---|
125 | |
---|
126 | if (Id == -1) { |
---|
127 | return LastCtx; |
---|
128 | } |
---|
129 | |
---|
130 | ExtractCtx = LastCtx; |
---|
131 | |
---|
132 | while (ExtractCtx->id != Id && ExtractCtx != NULL) { |
---|
133 | ExtractCtx = ExtractCtx->previous; |
---|
134 | } |
---|
135 | |
---|
136 | if (ExtractCtx == NULL) |
---|
137 | return NULL; |
---|
138 | |
---|
139 | return ExtractCtx; |
---|
140 | } |
---|
141 | |
---|
142 | /*----- Breakpoint Exception management -----*/ |
---|
143 | |
---|
144 | /* |
---|
145 | * Handler for Breakpoint Exceptions : |
---|
146 | * software breakpoints. |
---|
147 | */ |
---|
148 | |
---|
149 | void |
---|
150 | BreakPointExcHdl(CPU_Exception_frame *ctx) |
---|
151 | { |
---|
152 | rtems_status_code status; |
---|
153 | rtems_id continueSemId; |
---|
154 | |
---|
155 | if ( (justSaveContext) && (ctx->idtIndex == I386_EXCEPTION_ENTER_RDBG) ) { |
---|
156 | PushSavedExceptCtx (_Thread_Executing->Object.id, ctx); |
---|
157 | justSaveContext = 0; |
---|
158 | } |
---|
159 | else { |
---|
160 | if (ctx->idtIndex != I386_EXCEPTION_DEBUG){ |
---|
161 | NbSerializedCtx++; |
---|
162 | rtems_semaphore_obtain(serializeSemId, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
163 | NbSerializedCtx--; |
---|
164 | } |
---|
165 | |
---|
166 | currentTargetThread = _Thread_Executing->Object.id; |
---|
167 | |
---|
168 | #ifdef DDEBUG |
---|
169 | printk("----------------------------------------------------------\n"); |
---|
170 | printk("Exception %d caught at PC %x by thread %d\n", |
---|
171 | ctx->idtIndex, |
---|
172 | ctx->eip, |
---|
173 | _Thread_Executing->Object.id); |
---|
174 | printk("----------------------------------------------------------\n"); |
---|
175 | printk("Processor execution context at time of the fault was :\n"); |
---|
176 | printk("----------------------------------------------------------\n"); |
---|
177 | printk(" EAX = %x EBX = %x ECX = %x EDX = %x\n", |
---|
178 | ctx->eax, ctx->ebx, ctx->ecx, ctx->edx); |
---|
179 | printk(" ESI = %x EDI = %x EBP = %x ESP = %x\n", |
---|
180 | ctx->esi, ctx->edi, ctx->ebp, ctx->esp0); |
---|
181 | printk("----------------------------------------------------------\n"); |
---|
182 | printk("Error code pushed by processor itself (if not 0) = %x\n", |
---|
183 | ctx->faultCode); |
---|
184 | printk("----------------------------------------------------------\n\n"); |
---|
185 | #endif |
---|
186 | |
---|
187 | status = rtems_semaphore_create (rtems_build_name('D', 'B', 'G', 'c'), |
---|
188 | 0, |
---|
189 | RTEMS_FIFO | |
---|
190 | RTEMS_COUNTING_SEMAPHORE | |
---|
191 | RTEMS_NO_INHERIT_PRIORITY | |
---|
192 | RTEMS_NO_PRIORITY_CEILING | |
---|
193 | RTEMS_LOCAL, |
---|
194 | 0, |
---|
195 | &continueSemId); |
---|
196 | if (status != RTEMS_SUCCESSFUL) |
---|
197 | rtems_panic ("Can't create continue semaphore: `%s'\n",rtems_status_text(status)); |
---|
198 | |
---|
199 | PushExceptCtx (_Thread_Executing->Object.id, continueSemId, ctx); |
---|
200 | |
---|
201 | switch (ctx->idtIndex){ |
---|
202 | case I386_EXCEPTION_DEBUG: |
---|
203 | DPRINTF((" DEBUG EXCEPTION !!!\n")); |
---|
204 | ctx->eflags &= ~EFLAGS_TF; |
---|
205 | ExitForSingleStep-- ; |
---|
206 | rtems_semaphore_release( wakeupEventSemId ); |
---|
207 | break; |
---|
208 | |
---|
209 | case I386_EXCEPTION_BREAKPOINT: |
---|
210 | DPRINTF((" BREAKPOINT EXCEPTION !!!\n")); |
---|
211 | rtems_semaphore_release( wakeupEventSemId ); |
---|
212 | break; |
---|
213 | |
---|
214 | case I386_EXCEPTION_ENTER_RDBG: |
---|
215 | DPRINTF((" ENTER RDBG !!!\n")); |
---|
216 | rtems_semaphore_release( wakeupEventSemId ); |
---|
217 | break; |
---|
218 | |
---|
219 | default: |
---|
220 | DPRINTF((" OTHER EXCEPTION !!!\n")); |
---|
221 | rtems_semaphore_release( wakeupEventSemId ); |
---|
222 | break; |
---|
223 | } |
---|
224 | |
---|
225 | rtems_semaphore_obtain(continueSemId, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
226 | |
---|
227 | PopExceptCtx (_Thread_Executing->Object.id); |
---|
228 | rtems_semaphore_delete(continueSemId); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | |
---|