Rev 5363 | Rev 8840 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2288 | clevermous | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
5363 | yogev_ezra | 3 | ;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;; |
2288 | clevermous | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
7 | |||
8 | $Revision: 7124 $ |
||
9 | |||
10 | |||
11 | ; diamond, 2006 |
||
12 | sys_debug_services: |
||
13 | cmp ebx, 9 |
||
14 | ja @f |
||
15 | jmp dword [sys_debug_services_table+ebx*4] |
||
16 | @@: |
||
17 | ret |
||
18 | iglobal |
||
19 | align 4 |
||
20 | sys_debug_services_table: |
||
21 | dd debug_set_event_data |
||
22 | dd debug_getcontext |
||
23 | dd debug_setcontext |
||
24 | dd debug_detach |
||
25 | dd debug_suspend |
||
26 | dd debug_resume |
||
27 | dd debug_read_process_memory |
||
28 | dd debug_write_process_memory |
||
29 | dd debug_terminate |
||
30 | dd debug_set_drx |
||
31 | endg |
||
32 | debug_set_event_data: |
||
33 | ; in: ecx = pointer |
||
34 | ; destroys eax |
||
35 | mov eax, [current_slot] |
||
36 | mov [eax+APPDATA.dbg_event_mem], ecx |
||
37 | ret |
||
38 | |||
39 | get_debuggee_slot: |
||
40 | ; in: ecx=PID |
||
41 | ; out: CF=1 if error |
||
42 | ; CF=0 and eax=slot*0x20 if ok |
||
43 | ; out: interrupts disabled |
||
44 | cli |
||
45 | mov eax, ecx |
||
46 | call pid_to_slot |
||
47 | test eax, eax |
||
48 | jz .ret_bad |
||
49 | shl eax, 5 |
||
50 | push ebx |
||
51 | mov ebx, [CURRENT_TASK] |
||
52 | cmp [SLOT_BASE+eax*8+APPDATA.debugger_slot], ebx |
||
53 | pop ebx |
||
54 | jnz .ret_bad |
||
55 | ; clc ; automatically |
||
56 | ret |
||
57 | .ret_bad: |
||
58 | stc |
||
59 | ret |
||
60 | |||
61 | debug_detach: |
||
62 | ; in: ecx=pid |
||
63 | ; destroys eax,ebx |
||
64 | call get_debuggee_slot |
||
65 | jc .ret |
||
66 | and dword [eax*8+SLOT_BASE+APPDATA.debugger_slot], 0 |
||
67 | call do_resume |
||
68 | .ret: |
||
69 | sti |
||
70 | ret |
||
71 | |||
72 | debug_terminate: |
||
73 | ; in: ecx=pid |
||
74 | call get_debuggee_slot |
||
75 | jc debug_detach.ret |
||
76 | mov ecx, eax |
||
77 | shr ecx, 5 |
||
78 | ; push 2 |
||
79 | ; pop ebx |
||
80 | mov edx, esi |
||
81 | jmp sysfn_terminate |
||
82 | |||
83 | debug_suspend: |
||
84 | ; in: ecx=pid |
||
85 | ; destroys eax,ecx |
||
86 | cli |
||
87 | mov eax, ecx |
||
88 | call pid_to_slot |
||
89 | shl eax, 5 |
||
90 | jz .ret |
||
91 | mov cl, [CURRENT_TASK+eax+TASKDATA.state] ; process state |
||
92 | test cl, cl |
||
93 | jz .1 |
||
94 | cmp cl, 5 |
||
95 | jnz .ret |
||
96 | mov cl, 2 |
||
97 | .2: |
||
98 | mov [CURRENT_TASK+eax+TASKDATA.state], cl |
||
99 | .ret: |
||
100 | sti |
||
101 | ret |
||
102 | .1: |
||
103 | inc ecx |
||
104 | jmp .2 |
||
105 | |||
106 | do_resume: |
||
107 | mov cl, [CURRENT_TASK+eax+TASKDATA.state] |
||
108 | cmp cl, 1 |
||
109 | jz .1 |
||
110 | cmp cl, 2 |
||
111 | jnz .ret |
||
112 | mov cl, 5 |
||
113 | .2: |
||
114 | mov [CURRENT_TASK+eax+TASKDATA.state], cl |
||
115 | .ret: |
||
116 | ret |
||
117 | .1: |
||
118 | dec ecx |
||
119 | jmp .2 |
||
120 | |||
121 | debug_resume: |
||
122 | ; in: ecx=pid |
||
123 | ; destroys eax,ebx |
||
124 | cli |
||
125 | mov eax, ecx |
||
126 | call pid_to_slot |
||
127 | shl eax, 5 |
||
128 | jz .ret |
||
129 | call do_resume |
||
130 | .ret: |
||
131 | sti |
||
132 | ret |
||
133 | |||
134 | debug_getcontext: |
||
135 | ; in: |
||
136 | ; ecx=pid |
||
137 | ; edx=sizeof(CONTEXT) |
||
138 | ; esi->CONTEXT |
||
4893 | Serge | 139 | ; destroys eax,ebx,ecx,edx,esi,edi |
140 | |||
141 | xor ebx, ebx ; 0 - get only gp regs |
||
142 | cmp edx, 40 |
||
143 | je .std_ctx |
||
144 | |||
145 | cmp edx, 48+288 |
||
146 | jne .ret |
||
147 | |||
148 | inc ebx ; 1 - get sse context |
||
149 | ; TODO legacy 32-bit FPU/MMX context |
||
150 | .std_ctx: |
||
2288 | clevermous | 151 | ; push ecx |
152 | ; mov ecx, esi |
||
153 | call check_region |
||
154 | ; pop ecx |
||
155 | dec eax |
||
156 | jnz .ret |
||
157 | call get_debuggee_slot |
||
158 | jc .ret |
||
4893 | Serge | 159 | |
160 | shr eax, 5 |
||
161 | cmp eax, [fpu_owner] |
||
162 | jne @f |
||
163 | inc bh ; set swap context flag |
||
164 | @@: |
||
165 | shl eax, 8 |
||
2288 | clevermous | 166 | mov edi, esi |
4893 | Serge | 167 | mov eax, [eax+SLOT_BASE+APPDATA.pl0_stack] |
2288 | clevermous | 168 | lea esi, [eax+RING0_STACK_SIZE] |
169 | |||
170 | .ring0: |
||
171 | ; note that following code assumes that all interrupt/exception handlers |
||
7124 | dunkaist | 172 | ; save ring-3 context by pushad in this order |
2288 | clevermous | 173 | ; top of ring0 stack: ring3 stack ptr (ss+esp), iret data (cs+eip+eflags), pushad |
174 | sub esi, 8+12+20h |
||
175 | lodsd ;edi |
||
176 | mov [edi+24h], eax |
||
177 | lodsd ;esi |
||
178 | mov [edi+20h], eax |
||
179 | lodsd ; ebp |
||
180 | mov [edi+1Ch], eax |
||
181 | lodsd ;esp |
||
182 | lodsd ;ebx |
||
183 | mov [edi+14h], eax |
||
184 | lodsd ;edx |
||
185 | mov [edi+10h], eax |
||
186 | lodsd ;ecx |
||
187 | mov [edi+0Ch], eax |
||
188 | lodsd ;eax |
||
189 | mov [edi+8], eax |
||
190 | lodsd ;eip |
||
191 | mov [edi], eax |
||
192 | lodsd ;cs |
||
193 | lodsd ;eflags |
||
194 | mov [edi+4], eax |
||
195 | lodsd ;esp |
||
196 | mov [edi+18h], eax |
||
4893 | Serge | 197 | |
198 | dec bl |
||
199 | js .ret |
||
200 | dec bl |
||
201 | jns .ret |
||
202 | |||
203 | test bh, bh ; check swap flag |
||
204 | jz @F |
||
205 | |||
206 | ffree st0 ; swap context |
||
207 | @@: |
||
208 | |||
209 | add esi, 4 ;top of ring0 stack |
||
210 | ;fpu/sse context saved here |
||
211 | add edi, 40 |
||
212 | mov eax, 1 ;sse context |
||
213 | stosd |
||
214 | xor eax, eax ;reserved dword |
||
215 | stosd |
||
216 | |||
217 | mov ecx, 288/4 |
||
218 | rep movsd ;copy sse context |
||
219 | |||
2288 | clevermous | 220 | .ret: |
221 | sti |
||
222 | ret |
||
223 | |||
224 | debug_setcontext: |
||
225 | ; in: |
||
226 | ; ecx=pid |
||
227 | ; edx=sizeof(CONTEXT) |
||
228 | ; esi->CONTEXT |
||
229 | ; destroys eax,ecx,edx,esi,edi |
||
230 | cmp edx, 28h |
||
231 | jnz .ret |
||
232 | ; push ebx |
||
233 | ; mov ebx, edx |
||
234 | call check_region |
||
235 | ; pop ebx |
||
236 | dec eax |
||
237 | jnz .ret |
||
238 | call get_debuggee_slot |
||
239 | jc .stiret |
||
240 | ; mov esi, edx |
||
241 | mov eax, [eax*8+SLOT_BASE+APPDATA.pl0_stack] |
||
242 | lea edi, [eax+RING0_STACK_SIZE] |
||
243 | |||
244 | .ring0: |
||
245 | sub edi, 8+12+20h |
||
246 | mov eax, [esi+24h] ;edi |
||
247 | stosd |
||
248 | mov eax, [esi+20h] ;esi |
||
249 | stosd |
||
250 | mov eax, [esi+1Ch] ;ebp |
||
251 | stosd |
||
252 | scasd |
||
253 | mov eax, [esi+14h] ;ebx |
||
254 | stosd |
||
255 | mov eax, [esi+10h] ;edx |
||
256 | stosd |
||
257 | mov eax, [esi+0Ch] ;ecx |
||
258 | stosd |
||
259 | mov eax, [esi+8] ;eax |
||
260 | stosd |
||
261 | mov eax, [esi] ;eip |
||
262 | stosd |
||
263 | scasd |
||
264 | mov eax, [esi+4] ;eflags |
||
265 | stosd |
||
266 | mov eax, [esi+18h] ;esp |
||
267 | stosd |
||
268 | .stiret: |
||
269 | sti |
||
270 | .ret: |
||
271 | ret |
||
272 | |||
273 | debug_set_drx: |
||
274 | call get_debuggee_slot |
||
275 | jc .errret |
||
276 | mov ebp, eax |
||
277 | lea eax, [eax*8+SLOT_BASE+APPDATA.dbg_regs] |
||
278 | ; [eax]=dr0, [eax+4]=dr1, [eax+8]=dr2, [eax+C]=dr3 |
||
279 | ; [eax+10]=dr7 |
||
280 | cmp esi, OS_BASE |
||
281 | jae .errret |
||
282 | cmp dl, 3 |
||
283 | ja .errret |
||
284 | mov ecx, dr7 |
||
285 | ;fix me |
||
286 | xchg ecx, edx |
||
287 | shr edx, cl |
||
288 | shr edx, cl |
||
289 | xchg ecx, edx |
||
290 | |||
291 | test ecx, 2 ; bit 1+2*index = G0..G3, global break enable |
||
292 | jnz .errret2 |
||
293 | test dh, dh |
||
294 | jns .new |
||
295 | ; clear breakpoint |
||
296 | movzx edx, dl |
||
297 | add edx, edx |
||
298 | and dword [eax+edx*2], 0 ; clear DR |
||
299 | btr dword [eax+10h], edx ; clear L bit |
||
300 | test byte [eax+10h], 55h |
||
301 | jnz .okret |
||
302 | ; imul eax, ebp, tss_step/32 |
||
303 | ; and byte [eax + tss_data + TSS._trap], not 1 |
||
304 | and [ebp*8 + SLOT_BASE+APPDATA.dbg_state], not 1 |
||
305 | .okret: |
||
306 | and dword [esp+32], 0 |
||
307 | sti |
||
308 | ret |
||
309 | .errret: |
||
310 | sti |
||
311 | mov dword [esp+32], 1 |
||
312 | ret |
||
313 | .errret2: |
||
314 | sti |
||
315 | mov dword [esp+32], 2 |
||
316 | ret |
||
317 | .new: |
||
318 | ; add new breakpoint |
||
319 | ; dl=index; dh=flags; esi=address |
||
320 | test dh, 0xF0 |
||
321 | jnz .errret |
||
322 | mov cl, dh |
||
323 | and cl, 3 |
||
324 | cmp cl, 2 |
||
325 | jz .errret |
||
326 | mov cl, dh |
||
327 | shr cl, 2 |
||
328 | cmp cl, 2 |
||
329 | jz .errret |
||
330 | |||
331 | mov ebx, esi |
||
332 | test bl, dl |
||
333 | |||
334 | jnz .errret |
||
335 | or byte [eax+10h+1], 3 ; set GE and LE flags |
||
336 | |||
337 | movzx edx, dh |
||
338 | movzx ecx, dl |
||
339 | add ecx, ecx |
||
340 | bts dword [eax+10h], ecx ; set L flag |
||
341 | add ecx, ecx |
||
342 | mov [eax+ecx], ebx;esi ; set DR |
||
343 | shl edx, cl |
||
344 | mov ebx, 0xF |
||
345 | shl ebx, cl |
||
346 | not ebx |
||
347 | and [eax+10h+2], bx |
||
348 | or [eax+10h+2], dx ; set R/W and LEN fields |
||
349 | ; imul eax, ebp, tss_step/32 |
||
350 | ; or byte [eax + tss_data + TSS._trap], 1 |
||
351 | or [ebp*8 + SLOT_BASE+APPDATA.dbg_state], 1 |
||
352 | jmp .okret |
||
353 | |||
354 | debug_read_process_memory: |
||
355 | ; in: |
||
356 | ; ecx=pid |
||
357 | ; edx=length |
||
358 | ; edi->buffer in debugger |
||
359 | ; esi=address in debuggee |
||
360 | ; out: [esp+36]=sizeof(read) |
||
361 | ; destroys all |
||
362 | ; push ebx |
||
363 | ; mov ebx, esi |
||
364 | call check_region |
||
365 | ; pop ebx |
||
366 | dec eax |
||
367 | jnz .err |
||
368 | call get_debuggee_slot |
||
369 | jc .err |
||
370 | shr eax, 5 |
||
371 | mov ecx, edi |
||
372 | call read_process_memory |
||
373 | sti |
||
374 | mov dword [esp+32], eax |
||
375 | ret |
||
376 | .err: |
||
377 | or dword [esp+32], -1 |
||
378 | ret |
||
379 | |||
380 | debug_write_process_memory: |
||
381 | ; in: |
||
382 | ; ecx=pid |
||
383 | ; edx=length |
||
384 | ; edi->buffer in debugger |
||
385 | ; esi=address in debuggee |
||
386 | ; out: [esp+36]=sizeof(write) |
||
387 | ; destroys all |
||
388 | ; push ebx |
||
389 | ; mov ebx, esi |
||
390 | call check_region |
||
391 | ; pop ebx |
||
392 | dec eax |
||
393 | jnz debug_read_process_memory.err |
||
394 | call get_debuggee_slot |
||
395 | jc debug_read_process_memory.err |
||
396 | shr eax, 5 |
||
397 | mov ecx, edi |
||
398 | call write_process_memory |
||
399 | sti |
||
400 | mov [esp+32], eax |
||
401 | ret |
||
402 | |||
403 | debugger_notify: |
||
404 | ; in: eax=debugger slot |
||
405 | ; ecx=size of debug message |
||
406 | ; [esp+4]..[esp+4+ecx]=message |
||
407 | ; interrupts must be disabled! |
||
408 | ; destroys all general registers |
||
409 | ; interrupts remain disabled |
||
410 | xchg ebp, eax |
||
411 | mov edi, [timer_ticks] |
||
412 | add edi, 500 ; 5 sec timeout |
||
413 | .1: |
||
414 | mov eax, ebp |
||
415 | shl eax, 8 |
||
416 | mov esi, [SLOT_BASE+eax+APPDATA.dbg_event_mem] |
||
417 | test esi, esi |
||
418 | jz .ret |
||
419 | ; read buffer header |
||
420 | push ecx |
||
421 | push eax |
||
422 | push eax |
||
423 | mov eax, ebp |
||
424 | mov ecx, esp |
||
425 | mov edx, 8 |
||
426 | call read_process_memory |
||
427 | cmp eax, edx |
||
428 | jz @f |
||
429 | add esp, 12 |
||
430 | jmp .ret |
||
431 | @@: |
||
432 | cmp dword [ecx], 0 |
||
433 | jg @f |
||
434 | .2: |
||
435 | pop ecx |
||
436 | pop ecx |
||
437 | pop ecx |
||
438 | cmp dword [CURRENT_TASK], 1 |
||
439 | jnz .notos |
||
440 | cmp [timer_ticks], edi |
||
441 | jae .ret |
||
442 | .notos: |
||
443 | sti |
||
444 | call change_task |
||
445 | cli |
||
446 | jmp .1 |
||
447 | @@: |
||
448 | mov edx, [ecx+8] |
||
449 | add edx, [ecx+4] |
||
450 | cmp edx, [ecx] |
||
451 | ja .2 |
||
452 | ; advance buffer position |
||
453 | push edx |
||
454 | mov edx, 4 |
||
455 | sub ecx, edx |
||
456 | mov eax, ebp |
||
457 | add esi, edx |
||
458 | call write_process_memory |
||
459 | pop eax |
||
460 | ; write message |
||
461 | mov eax, ebp |
||
462 | add esi, edx |
||
463 | add esi, [ecx+8] |
||
464 | add ecx, 20 |
||
465 | pop edx |
||
466 | pop edx |
||
467 | pop edx |
||
468 | call write_process_memory |
||
469 | ; new debug event |
||
470 | mov eax, ebp |
||
471 | shl eax, 8 |
||
472 | or byte [SLOT_BASE+eax+APPDATA.event_mask+1], 1 ; set flag 100h |
||
473 | .ret: |
||
474 | ret |