Subversion Repositories Kolibri OS

Rev

Rev 9715 | Details | Compare with Previous | Last modification | View Log | RSS feed

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