Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
10051 ace_dent 3
;; Copyright (C) KolibriOS team 2004-2024. All rights reserved. ;;
6793 pathoswith 4
;;  Distributed under terms of the GNU General Public License.  ;;
2288 clevermous 5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
 
9975 Jurgen 9
struct EXCEPT_STACK
10
    RegEIP     dd ?
11
    ExcCode    dd ? ; only exception 12 overflow stack
12
    OldESP     dd ?
13
    RegCR2     dd ?
14
    LockAccess dd ?
15
ends
16
 
2288 clevermous 17
align 4 ;3A08
18
build_interrupt_table:
19
        mov     edi, idts
20
        mov     esi, sys_int
21
        mov     ecx, 0x40
22
        mov     eax, (10001110b shl 24) + os_code
23
  @@:
8050 rgimad 24
        movsw   ; low word of code-entry
25
        stosd   ; interrupt gate type : os_code selector
26
        movsw   ; high word of code-entry
2288 clevermous 27
        loop    @b
8050 rgimad 28
        movsd   ; copy low  dword of trap gate for int 0x40
29
        movsd   ; copy high dword of trap gate for int 0x40
7733 dunkaist 30
        mov     ecx, 23
31
        mov     eax, (10001110b shl 24) + os_code
32
  @@:
8050 rgimad 33
        movsw   ; low word of code-entry
34
        stosd   ; interrupt gate type : os_code selector
35
        movsw   ; high word of code-entry
7733 dunkaist 36
        loop    @b
2288 clevermous 37
        lidt    [esi]
38
        ret
39
 
40
iglobal
41
  align 4
42
  sys_int:
8050 rgimad 43
    ; exception handlers addresses (for interrupt gate construction)
2288 clevermous 44
        dd      e0,e1,e2,e3,e4,e5,e6,except_7 ; SEE: core/fpu.inc
45
        dd      e8,e9,e10,e11,e12,e13,page_fault_exc,e15
46
        dd      e16, e17,e18, e19
47
        times   12 dd unknown_interrupt ;int_20..int_31
48
 
8050 rgimad 49
    ; interrupt handlers addresses (for interrupt gate construction)
7733 dunkaist 50
        ; 0x20+ are IRQ handlers
51
        dd irq0
52
        rept 12 irqn:1  \{dd irq_serv.irq_\#irqn\}
53
        dd irqD
54
        rept 18 irqn:14 \{dd irq_serv.irq_\#irqn\}
2288 clevermous 55
 
7733 dunkaist 56
        ; int_0x40 gate trap (for directly copied)
57
        dw i40 and 0xFFFF, os_code, 11101111b shl 8, i40 shr 16
2288 clevermous 58
 
7733 dunkaist 59
        rept 23 irqn:33 \{dd irq_serv.irq_\#irqn\}
60
 
2288 clevermous 61
  idtreg: ; data for LIDT instruction (!!! must be immediately below sys_int data)
62
        dw      2*($-sys_int-4)-1
8050 rgimad 63
        dd      idts ; 0x8000B100
64
        dw      0    ; alignment
2288 clevermous 65
 
66
  msg_fault_sel dd  msg_exc_8,msg_exc_u,msg_exc_a,msg_exc_b
6817 dunkaist 67
                dd  msg_exc_c,msg_exc_d,msg_exc_e,msg_exc_u
68
                dd  msg_exc_u,msg_exc_11
2288 clevermous 69
 
70
  msg_exc_8     db "Double fault", 0
71
  msg_exc_u     db "Undefined Exception", 0
72
  msg_exc_a     db "Invalid TSS", 0
73
  msg_exc_b     db "Segment not present", 0
74
  msg_exc_c     db "Stack fault", 0
75
  msg_exc_d     db "General protection fault", 0
76
  msg_exc_e     db "Page fault", 0
6817 dunkaist 77
  msg_exc_11    db "Alignment Check", 0
2288 clevermous 78
 
3342 yogev_ezra 79
  if lang eq sp
3344 yogev_ezra 80
    include 'core/sys32-sp.inc'
3342 yogev_ezra 81
  else
82
    msg_sel_ker   db "kernel", 0
83
    msg_sel_app   db "application", 0
3309 esevece 84
  end if
2288 clevermous 85
 
86
endg
87
 
88
macro save_ring3_context {
89
        pushad
90
}
91
macro restore_ring3_context {
92
        popad
93
}
94
macro exc_wo_code [num] {
95
  e#num :
96
        save_ring3_context
97
        mov     bl, num
98
        jmp     exc_c
99
} exc_wo_code   0,1,2,3,4,5,6,15,16,19
100
 
101
macro exc_w_code [num] {
102
  e#num :
103
        add     esp, 4
104
        save_ring3_context
105
        mov     bl, num
106
        jmp     exc_c
107
} exc_w_code    8,9,10,11,12,13,17,18
108
 
109
 
110
uglobal
111
  pf_err_code   dd ?
112
endg
113
 
8050 rgimad 114
page_fault_exc:                   ; foolproof: selectors are clobbered ...
115
        pop     [ss:pf_err_code]  ; actually, until the next #PF
10002 Jurgen 116
 
117
        cmp     edi, CONTROL_EXCEPTION ; equ 'EXPT'
118
        jne     .no_ctrl_exc
119
        bt      dword [esp], 31
120
        jc      .setret
121
        test    esi, esi
122
        jl      .no_ctrl_exc
123
.setret:
124
        mov     [esp], esi
125
        iret
126
 
127
.no_ctrl_exc:
2288 clevermous 128
        save_ring3_context
129
        mov     bl, 14
130
 
8050 rgimad 131
exc_c:                            ; exceptions (all but 7th - #NM)
132
 ; stack frame when exception/interrupt from ring3 + pushad (i.e right here)
2288 clevermous 133
  reg_ss        equ esp+0x30
134
  reg_esp3      equ esp+0x2C
135
  reg_eflags    equ esp+0x28
136
  reg_cs3       equ esp+0x24
137
  reg_eip       equ esp+0x20
8050 rgimad 138
 ; this if frame from pushad
2288 clevermous 139
  reg_eax       equ esp+0x1C
140
  reg_ecx       equ esp+0x18
141
  reg_edx       equ esp+0x14
142
  reg_ebx       equ esp+0x10
143
  reg_esp0      equ esp+0x0C
144
  reg_ebp       equ esp+0x08
145
  reg_esi       equ esp+0x04
146
  reg_edi       equ esp+0x00
147
 
8050 rgimad 148
        mov     ax, app_data       ; exception
149
        mov     ds, ax             ; load proper values
150
        mov     es, ax             ; to registers
8051 rgimad 151
        cld                        ; clear the direction flag
2288 clevermous 152
        movzx   ebx, bl
153
; redirect to V86 manager? (EFLAGS & 0x20000) != 0?
154
        test    byte[reg_eflags+2], 2
155
        jnz     v86_exc_c
8050 rgimad 156
        cmp     bl, 14             ; #PF
2288 clevermous 157
        jne     @f
158
        call    page_fault_handler ; SEE: core/memory.inc
159
  @@:
160
        mov     esi, [current_slot]
9715 Doczom 161
        btr     [esi + APPDATA.except_mask], ebx
2288 clevermous 162
        jnc     @f
9715 Doczom 163
        mov     eax, [esi + APPDATA.exc_handler]
2288 clevermous 164
        test    eax, eax
165
        jnz     IRetToUserHook
166
  @@:
167
        cli
9715 Doczom 168
        mov     eax, [esi + APPDATA.debugger_slot]
2288 clevermous 169
        test    eax, eax
170
        jnz     .debug
6793 pathoswith 171
; not debuggee => say error and terminate
9709 Doczom 172
        call    show_error_parameters  ; this function output in edx = current_slot
2288 clevermous 173
        sti
9709 Doczom 174
        mov     [edx + APPDATA.state], TSTATE_TERMINATING
3534 clevermous 175
        call    wakeup_osloop
3615 clevermous 176
        call    change_task
177
; If we're here, then the main OS thread has crashed before initializing IDLE thread.
178
; Or they both have crashed. Anyway, things are hopelessly broken.
179
        hlt
180
        jmp     $-1
2288 clevermous 181
.debug:
182
; we are debugged process, notify debugger and suspend ourself
183
; eax=debugger PID
184
        mov     ecx, 1          ; debug_message code=other_exception
185
        cmp     bl, 1           ; #DB
186
        jne     .notify         ; notify debugger and suspend ourself
187
        mov     ebx, dr6        ; debug_message data=DR6_image
188
        xor     edx, edx
189
        mov     dr6, edx
190
        mov     edx, dr7
191
        mov     cl, not 8
192
  .l1:
193
        shl     dl, 2
194
        jc      @f
195
        and     bl, cl
196
  @@:
197
        sar     cl, 1
198
        jc      .l1
199
        mov     cl, 3           ; debug_message code=debug_exception
200
.notify:
201
        push    ebx             ; debug_message data
9692 Doczom 202
        mov     ebx, [current_slot]
203
        push    [ebx + APPDATA.tid] ; PID
2288 clevermous 204
        push    ecx             ; debug_message code ((here: ecx==1/3))
205
        mov     cl, 12          ; debug_message size
206
        call    debugger_notify ;; only ONE using, inline ??? SEE: core/debug.inc
207
        add     esp, 12
9709 Doczom 208
        mov     edx, [current_slot]
209
        mov     [edx + APPDATA.state], TSTATE_RUN_SUSPENDED
2288 clevermous 210
        call    change_task     ; SEE: core/shed.inc
211
        restore_ring3_context
212
        iretd
213
 
214
IRetToUserHook:
9976 Jurgen 215
        cmp     ebx, 12
216
        je      .ex_stack
217
        cmp     ebx, 14
218
        jne     .nostack
219
        mov     ecx, cr2
220
        sub     ecx, [reg_esp3]
221
        jg      .nostack
222
        add     ecx, 1000h
223
        jl      .nostack
224
.ex_stack:
10002 Jurgen 225
        mov     ecx, [esi+APPDATA.exc_reserve_stack]
9976 Jurgen 226
        test    ecx, ecx
227
        jle     .nostack
228
        xchg    edi, eax
229
        sub     ecx, sizeof.EXCEPT_STACK
230
        push    ebx
231
        push    1
232
        pop     ebx
233
.lock:
234
        lock bts [ecx+EXCEPT_STACK.LockAccess], 0
235
        jnc     .lock1
236
        call    delay_hs_unprotected
237
        jmp     .lock
238
.lock1:
239
        pop     ebx
240
        cmp     ebx, 14
241
        jne     .ex12
242
        btr     [esi+APPDATA.except_mask], 12
243
        jc      .ex_stack1
244
        xchg    eax, edi
245
        jmp     .nostack
246
.ex_stack1:
247
        bts     [esi+APPDATA.except_mask], ebx
248
        dec     ebx
249
        dec     ebx
250
.ex12:
251
        mov     [ecx+EXCEPT_STACK.ExcCode], ebx
252
        mov     eax, ecx
253
        xchg    [reg_esp3], eax
254
        mov     [ecx+EXCEPT_STACK.OldESP], eax
255
        mov     eax, cr2
256
        mov     [ecx+EXCEPT_STACK.RegCR2], eax
257
        xchg    edi, [reg_eip]
258
        mov     [ecx+EXCEPT_STACK.RegEIP], edi
259
        jmp     .end
260
.nostack:
2288 clevermous 261
        xchg    eax, [reg_eip]
262
        sub     dword[reg_esp3], 8
263
        mov     edi, [reg_esp3]
264
        stosd
265
        mov     [edi], ebx
9976 Jurgen 266
.end:
2288 clevermous 267
        restore_ring3_context
268
; simply return control to interrupted process
269
unknown_interrupt:
270
        iretd
271
 
272
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
273
; bl - error vector
274
show_error_parameters:
275
        cmp     bl, 0x06
276
        jnz     .no_ud
277
        push    ebx
278
        mov     ebx, ud_user_message
279
        mov     ebp, notifyapp
280
        call    fs_execute_from_sysdir_param
281
        pop     ebx
282
.no_ud:
9692 Doczom 283
        mov     edx, [current_slot];not scratched below
3309 esevece 284
        if lang eq sp
9692 Doczom 285
        DEBUGF  1, "K : Proceso - terminado forzado PID: %x [%s]\n", [edx + APPDATA.tid], [current_slot]
3309 esevece 286
        else
9692 Doczom 287
        DEBUGF  1, "K : Process - forced terminate PID: %x [%s]\n", [edx + APPDATA.tid], [current_slot]
3309 esevece 288
        end if
2288 clevermous 289
        cmp     bl, 0x08
290
        jb      .l0
6817 dunkaist 291
        cmp     bl, 0x11
2288 clevermous 292
        jbe     .l1
293
  .l0:
294
        mov     bl, 0x09
295
  .l1:
296
        mov     eax, [msg_fault_sel+ebx*4 - 0x08*4]
297
        DEBUGF  1, "K : %s\n", eax
298
        mov     eax, [reg_cs3+4]
299
        mov     edi, msg_sel_app
300
        mov     ebx, [reg_esp3+4]
301
        cmp     eax, app_code
302
        je      @f
303
        mov     edi, msg_sel_ker
304
        mov     ebx, [reg_esp0+4]
305
    @@:
306
        DEBUGF  1, "K : EAX : %x EBX : %x ECX : %x\n", [reg_eax+4], [reg_ebx+4], [reg_ecx+4]
307
        DEBUGF  1, "K : EDX : %x ESI : %x EDI : %x\n", [reg_edx+4], [reg_esi+4], [reg_edi+4]
308
        DEBUGF  1, "K : EBP : %x EIP : %x ESP : %x\n", [reg_ebp+4], [reg_eip+4], ebx
309
        DEBUGF  1, "K : Flags : %x CS : %x (%s)\n", [reg_eflags+4], eax, edi
3911 mario79 310
 
3909 mario79 311
        DEBUGF  1, "K : Stack dump:\n"
3911 mario79 312
        push    eax ebx ecx edx
313
        call    .check_ESP
314
        test    eax, eax
315
        jnz     .error_ESP
316
        DEBUGF  1, "K : [ESP+00]: %x",[ebx]
317
        add     ebx, 4
318
        call    .check_ESP
319
        test    eax, eax
320
        jnz     .error_ESP
321
        DEBUGF  1, " [ESP+04]: %x",[ebx]
322
        add     ebx, 4
323
        call    .check_ESP
324
        test    eax, eax
325
        jnz     .error_ESP
5356 serge 326
        DEBUGF  1, " [ESP+08]: %x\n",[ebx]
3911 mario79 327
        add     ebx, 4
328
        call    .check_ESP
329
        test    eax, eax
330
        jnz     .error_ESP
331
        DEBUGF  1, "K : [ESP+12]: %x",[ebx]
332
        add     ebx, 4
333
        call    .check_ESP
334
        test    eax, eax
335
        jnz     .error_ESP
336
        DEBUGF  1, " [ESP+16]: %x",[ebx]
337
        add     ebx, 4
338
        call    .check_ESP
339
        test    eax, eax
340
        jnz     .error_ESP
341
        DEBUGF  1, " [ESP+20]: %x\n",[ebx]
342
        add     ebx, 4
343
        call    .check_ESP
344
        test    eax, eax
345
        jnz     .error_ESP
346
        DEBUGF  1, "K : [ESP+24]: %x",[ebx]
347
        add     ebx, 4
348
        call    .check_ESP
349
        test    eax, eax
350
        jnz     .error_ESP
351
        DEBUGF  1, " [ESP+28]: %x",[ebx]
352
        add     ebx, 4
353
        call    .check_ESP
354
        test    eax, eax
355
        jnz     .error_ESP
5356 serge 356
        DEBUGF  1, " [ESP+32]: %x\n",[ebx]
3911 mario79 357
        pop     edx ecx ebx eax
2288 clevermous 358
        ret
3911 mario79 359
.error_ESP:
360
        pop     edx ecx ebx eax
361
        DEBUGF  1, "\n"
362
        DEBUGF  1, "K : Unexpected end of the stack\n"
363
        ret
364
;--------------------------------------
365
.check_ESP:
366
        push    ebx
367
        shr     ebx, 12
368
        mov     ecx, ebx
369
        shr     ecx, 10
9715 Doczom 370
        mov     edx, [master_tab + ecx*4]
5356 serge 371
        test    edx, PG_READ
8050 rgimad 372
        jz      .fail             ; page table is not created
373
                                  ; incorrect address in the program
3911 mario79 374
 
9715 Doczom 375
        mov     eax, [page_tabs + ebx*4]
3911 mario79 376
        test    eax, 2
8050 rgimad 377
        jz      .fail             ; address not reserved for use. error
5356 serge 378
 
379
        pop     ebx
3911 mario79 380
        xor     eax, eax
5356 serge 381
        ret
382
 
3911 mario79 383
.fail:
384
        pop     ebx
385
        xor     eax, eax
386
        dec     eax
387
        ret
2288 clevermous 388
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
389
 
390
  restore  reg_ss
391
  restore  reg_esp3
392
  restore  reg_eflags
393
  restore  reg_cs
394
  restore  reg_eip
395
  restore  reg_eax
396
  restore  reg_ecx
397
  restore  reg_edx
398
  restore  reg_ebx
399
  restore  reg_esp0
400
  restore  reg_ebp
401
  restore  reg_esi
402
  restore  reg_edi
403
 
404
 
405
align 4
3534 clevermous 406
lock_application_table:
407
        push    eax ecx edx
408
        mov     ecx, application_table_mutex
409
        call    mutex_lock
2288 clevermous 410
 
9828 Doczom 411
        mov     eax, [current_slot]
412
        mov     eax, [eax + APPDATA.tid]
2288 clevermous 413
 
3534 clevermous 414
        mov     [application_table_owner], eax
2288 clevermous 415
 
3534 clevermous 416
        pop     edx ecx eax
2288 clevermous 417
 
418
        ret
419
 
420
align 4
3534 clevermous 421
unlock_application_table:
422
        push    eax ecx edx
2288 clevermous 423
 
3534 clevermous 424
        mov     [application_table_owner], 0
425
        mov     ecx, application_table_mutex
426
        call    mutex_unlock
2288 clevermous 427
 
3534 clevermous 428
        pop     edx ecx eax
2288 clevermous 429
 
430
        ret
431
 
8050 rgimad 432
; sysfn 64 implementation
2288 clevermous 433
align 4
434
sys_resize_app_memory:
8050 rgimad 435
; in:   eax = 64 - function number
436
;       ebx = 1 - number of its only subfunction
437
;       ecx = new amount of memory
438
; out:
439
;       eax = 0 - success
440
;       eax = 1 - out of memory
2288 clevermous 441
 
442
;        cmp    eax,1
443
        dec     ebx
444
        jnz     .no_application_mem_resize
4313 mario79 445
 
446
        mov     eax, [pg_data.pages_free]
447
        shl     eax, 12
448
        cmp     eax, ecx
449
        jae     @f
450
 
451
        xor     eax, eax
452
        inc     eax
453
        jmp     .store_result
454
@@:
2288 clevermous 455
        stdcall new_mem_resize, ecx
4313 mario79 456
.store_result:
9831 dunkaist 457
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 458
.no_application_mem_resize:
459
        ret
460
 
461
iglobal
462
;  process_terminating  db 'K : Process - terminating',13,10,0
463
;  process_terminated   db 'K : Process - done',13,10,0
464
  msg_obj_destroy       db 'K : destroy app object',13,10,0
465
endg
466
 
467
; param
468
;  esi= slot
469
 
470
align 4
471
terminate: ; terminate application
5130 serge 472
destroy_thread:
2288 clevermous 473
 
5130 serge 474
        .slot     equ esp+4             ;locals
475
        .process  equ esp               ;ptr to parent process
2288 clevermous 476
 
5130 serge 477
 
2288 clevermous 478
        push    esi        ;save .slot
479
 
9709 Doczom 480
        shl     esi, BSF sizeof.APPDATA
9715 Doczom 481
        mov     edx, [SLOT_BASE + esi + APPDATA.process]
5130 serge 482
        test    edx, edx
483
        jnz     @F
9709 Doczom 484
        mov     [SLOT_BASE + esi + APPDATA.state], TSTATE_FREE
2288 clevermous 485
        pop     esi
486
        ret
487
@@:
5130 serge 488
        push    edx                     ;save .process
9715 Doczom 489
        lea     edx, [SLOT_BASE + esi]
3534 clevermous 490
        call    scheduler_remove_thread
491
        call    lock_application_table
2288 clevermous 492
 
493
; if the process is in V86 mode...
494
        mov     eax, [.slot]
9709 Doczom 495
        shl     eax, BSF sizeof.APPDATA
9715 Doczom 496
        mov     esi, [SLOT_BASE + eax + APPDATA.pl0_stack]
2288 clevermous 497
        add     esi, RING0_STACK_SIZE
9715 Doczom 498
        cmp     [SLOT_BASE + eax + APPDATA.saved_esp0], esi
2288 clevermous 499
        jz      .nov86
500
; ...it has page directory for V86 mode
9715 Doczom 501
        mov     esi, [SLOT_BASE + eax + APPDATA.saved_esp0]
2288 clevermous 502
        mov     ecx, [esi+4]
9715 Doczom 503
        mov     [SLOT_BASE + eax + APPDATA.process], ecx
2288 clevermous 504
; ...and I/O permission map for V86 mode
505
        mov     ecx, [esi+12]
9715 Doczom 506
        mov     [SLOT_BASE + eax + APPDATA.io_map], ecx
2288 clevermous 507
        mov     ecx, [esi+8]
9715 Doczom 508
        mov     [SLOT_BASE + eax + APPDATA.io_map+4], ecx
2288 clevermous 509
.nov86:
8050 rgimad 510
; destroy per-thread kernel objects
2288 clevermous 511
        mov     esi, [.slot]
9709 Doczom 512
        shl     esi, BSF sizeof.APPDATA
9715 Doczom 513
        add     esi, SLOT_BASE + APP_OBJ_OFFSET
2288 clevermous 514
@@:
9715 Doczom 515
        mov     eax, [esi + APPOBJ.fd]
2288 clevermous 516
        test    eax, eax
517
        jz      @F
518
 
519
        cmp     eax, esi
520
        je      @F
521
 
522
        push    esi
9715 Doczom 523
        call    [eax + APPOBJ.destroy]
2288 clevermous 524
           DEBUGF 1,"%s",msg_obj_destroy
525
        pop     esi
526
        jmp     @B
527
@@:
528
        mov     esi, [.slot]
3534 clevermous 529
        cmp     [fpu_owner], esi ; if user fpu last -> fpu user = 2
2288 clevermous 530
        jne     @F
531
 
3534 clevermous 532
        mov     [fpu_owner], 2
9715 Doczom 533
        mov     eax, [SLOT_BASE + sizeof.APPDATA*2 + APPDATA.fpu_state]
2288 clevermous 534
        clts
535
        bt      [cpu_caps], CAPS_SSE
536
        jnc     .no_SSE
537
        fxrstor [eax]
538
        jmp     @F
539
.no_SSE:
540
        fnclex
541
        frstor  [eax]
542
@@:
543
 
544
        mov     [KEY_COUNT], byte 0    ; empty keyboard buffer
545
        mov     [BTN_COUNT], byte 0    ; empty button buffer
546
 
547
 
548
; remove defined hotkeys
549
        mov     eax, hotkey_list
550
.loop:
551
        cmp     [eax+8], esi
552
        jnz     .cont
553
        mov     ecx, [eax]
554
        jecxz   @f
555
        push    dword [eax+12]
556
        pop     dword [ecx+12]
557
@@:
558
        mov     ecx, [eax+12]
559
        push    dword [eax]
560
        pop     dword [ecx]
561
        xor     ecx, ecx
562
        mov     [eax], ecx
563
        mov     [eax+4], ecx
564
        mov     [eax+8], ecx
565
        mov     [eax+12], ecx
566
.cont:
567
        add     eax, 16
568
        cmp     eax, hotkey_list+256*16
569
        jb      .loop
2709 mario79 570
; get process PID
571
        mov     eax, esi
9692 Doczom 572
        shl     eax, BSF sizeof.APPDATA
573
        mov     eax, [eax + SLOT_BASE + APPDATA.tid]
2709 mario79 574
; compare current lock input with process PID
575
        cmp     eax, [PID_lock_input]
576
        jne     @f
577
 
578
        xor     eax, eax
579
        mov     [PID_lock_input], eax
580
@@:
2288 clevermous 581
; remove hotkeys in buffer
582
        mov     eax, hotkey_buffer
583
.loop2:
584
        cmp     [eax], esi
585
        jnz     .cont2
586
        and     dword [eax+4], 0
587
        and     dword [eax], 0
588
.cont2:
589
        add     eax, 8
590
        cmp     eax, hotkey_buffer+120*8
591
        jb      .loop2
592
 
593
        mov     ecx, esi          ; remove buttons
9910 Doczom 594
  .bnewba2:
2288 clevermous 595
        mov     edi, [BTN_ADDR]
596
        mov     eax, edi
597
        cld
598
        movzx   ebx, word [edi]
599
        inc     bx
9910 Doczom 600
  .bnewba:
2288 clevermous 601
        dec     bx
9910 Doczom 602
        jz      .bnmba
2288 clevermous 603
        add     eax, 0x10
604
        cmp     cx, [eax]
9910 Doczom 605
        jnz     .bnewba
2288 clevermous 606
        pusha
607
        mov     ecx, ebx
608
        inc     ecx
609
        shl     ecx, 4
610
        mov     ebx, eax
611
        add     eax, 0x10
612
        call    memmove
613
        dec     dword [edi]
614
        popa
9910 Doczom 615
        jmp     .bnewba2
616
  .bnmba:
2288 clevermous 617
 
618
        pusha   ; save window coordinates for window restoring
619
        cld
8858 rgimad 620
        shl     esi, BSF sizeof.WDATA
2288 clevermous 621
        add     esi, window_data
9715 Doczom 622
        mov     eax, [esi + WDATA.box.left]
2288 clevermous 623
        mov     [draw_limits.left], eax
9715 Doczom 624
        add     eax, [esi + WDATA.box.width]
2288 clevermous 625
        mov     [draw_limits.right], eax
9715 Doczom 626
        mov     eax, [esi + WDATA.box.top]
2288 clevermous 627
        mov     [draw_limits.top], eax
9715 Doczom 628
        add     eax, [esi + WDATA.box.height]
2288 clevermous 629
        mov     [draw_limits.bottom], eax
630
 
631
        xor     eax, eax
8858 rgimad 632
        mov     edi, esi
633
        mov     ecx, sizeof.WDATA/4
634
        rep stosd
2288 clevermous 635
        popa
636
 
637
; debuggee test
638
        pushad
639
        mov     edi, esi
9692 Doczom 640
        shl     edi, BSF sizeof.APPDATA
641
        mov     eax, [SLOT_BASE + edi + APPDATA.debugger_slot]
2288 clevermous 642
        test    eax, eax
643
        jz      .nodebug
3598 clevermous 644
        movi    ecx, 8
9692 Doczom 645
        push    dword [SLOT_BASE + edi + APPDATA.tid]; PID
2288 clevermous 646
        push    2
647
        call    debugger_notify
648
        pop     ecx
649
        pop     ecx
650
.nodebug:
651
        popad
652
 
653
        mov     ebx, [.slot]
9709 Doczom 654
        shl     ebx, BSF sizeof.APPDATA
2288 clevermous 655
        push    ebx
9715 Doczom 656
        mov     ebx, [SLOT_BASE + ebx + APPDATA.pl0_stack]
2288 clevermous 657
 
658
        stdcall kernel_free, ebx
659
 
660
        pop     ebx
9715 Doczom 661
        mov     ebx, [SLOT_BASE + ebx + APPDATA.cur_dir]
2288 clevermous 662
        stdcall kernel_free, ebx
663
 
664
        mov     edi, [.slot]
9709 Doczom 665
        shl     edi, BSF sizeof.APPDATA
2288 clevermous 666
        add     edi, SLOT_BASE
667
 
9715 Doczom 668
        mov     eax, [edi + APPDATA.io_map]
669
        cmp     eax, [SLOT_BASE + sizeof.APPDATA+APPDATA.io_map]
2288 clevermous 670
        je      @F
671
        call    free_page
672
@@:
673
        mov     eax, [edi+APPDATA.io_map+4]
8093 dunkaist 674
        cmp     eax, [SLOT_BASE+sizeof.APPDATA+APPDATA.io_map+4]
2288 clevermous 675
        je      @F
676
        call    free_page
677
@@:
9715 Doczom 678
        lea     ebx, [edi + APPDATA.list]
5130 serge 679
        list_del ebx                    ;destroys edx, ecx
680
 
2288 clevermous 681
  ; activate window
682
        movzx   eax, word [WIN_STACK + esi*2]
8866 rgimad 683
        cmp     eax, [thread_count]
2288 clevermous 684
        jne     .dont_activate
685
        pushad
686
 .check_next_window:
687
        dec     eax
688
        cmp     eax, 1
689
        jbe     .nothing_to_activate
9715 Doczom 690
        lea     esi, [WIN_POS + eax*2]
2288 clevermous 691
        movzx   edi, word [esi]              ; edi = process
9709 Doczom 692
        shl     edi, BSF sizeof.APPDATA
693
        cmp     [SLOT_BASE + edi + APPDATA.state], TSTATE_FREE ; skip free slots
2288 clevermous 694
        je      .check_next_window
9926 Doczom 695
        shr     edi, (BSF sizeof.APPDATA - BSF sizeof.WDATA)
2288 clevermous 696
        add     edi, window_data
697
; \begin{diamond}[19.09.2006]
698
; skip minimized windows
699
        test    [edi + WDATA.fl_wstate], WSTATE_MINIMIZED
700
        jnz     .check_next_window
701
; \end{diamond}
702
        call    waredraw
703
 .nothing_to_activate:
704
        popad
705
 .dont_activate:
706
 
707
        push    esi     ; remove hd1 & cd & flp reservation
9692 Doczom 708
        shl     esi, BSF sizeof.APPDATA
9715 Doczom 709
        mov     esi, [SLOT_BASE + esi + APPDATA.tid]
2288 clevermous 710
        cmp     [cd_status], esi
711
        jnz     @f
712
        call    free_cd_channel
713
        and     [cd_status], 0
714
@@:
715
        pop     esi
716
        cmp     [bgrlockpid], esi
717
        jnz     @f
718
        and     [bgrlockpid], 0
719
        and     [bgrlock], 0
720
@@:
721
 
722
        pusha                 ; remove all port reservations
723
        mov     edx, esi
9692 Doczom 724
        shl     edx, BSF sizeof.APPDATA
9715 Doczom 725
        mov     edx, [SLOT_BASE + edx + APPDATA.tid]
2288 clevermous 726
 
9910 Doczom 727
  .rmpr0:
2288 clevermous 728
        mov     esi, [RESERVED_PORTS]
729
 
730
        test    esi, esi
9910 Doczom 731
        jz      .rmpr9
2288 clevermous 732
 
9910 Doczom 733
  .rmpr3:
2288 clevermous 734
 
735
        mov     edi, esi
736
        shl     edi, 4
737
        add     edi, RESERVED_PORTS
738
 
739
        cmp     edx, [edi]
9910 Doczom 740
        je      .rmpr4
2288 clevermous 741
 
742
        dec     esi
9910 Doczom 743
        jnz     .rmpr3
2288 clevermous 744
 
9910 Doczom 745
        jmp     .rmpr9
2288 clevermous 746
 
9910 Doczom 747
  .rmpr4:
2288 clevermous 748
 
749
        mov     ecx, 256
750
        sub     ecx, esi
751
        shl     ecx, 4
752
 
753
        mov     esi, edi
754
        add     esi, 16
755
        cld
756
        rep movsb
757
 
758
        dec     dword [RESERVED_PORTS]
759
 
9910 Doczom 760
        jmp     .rmpr0
2288 clevermous 761
 
9910 Doczom 762
  .rmpr9:
9743 Doczom 763
        popa
2288 clevermous 764
 
9743 Doczom 765
; clearing APPDATA structure this thread
766
        pushad
767
        mov     edi, esi
768
        shl     edi, BSF sizeof.APPDATA
769
        add     edi, SLOT_BASE
770
        mov     eax, 0x20202020
771
        stosd
772
        stosd
773
        stosd
774
        mov     ecx, 244/4
775
        xor     eax, eax
776
        rep stosd
777
        popad
778
 
2288 clevermous 779
        mov     edi, esi ; do not run this process slot
9709 Doczom 780
        shl     edi, BSF sizeof.APPDATA
9715 Doczom 781
        mov     [SLOT_BASE + edi + APPDATA.state], TSTATE_FREE
2288 clevermous 782
; debugger test - terminate all debuggees
783
        mov     eax, 2
9715 Doczom 784
        mov     ecx, SLOT_BASE + 2*sizeof.APPDATA + APPDATA.debugger_slot
2288 clevermous 785
.xd0:
8866 rgimad 786
        cmp     eax, [thread_count]
2288 clevermous 787
        ja      .xd1
788
        cmp     dword [ecx], esi
789
        jnz     @f
790
        and     dword [ecx], 0
791
        pushad
792
        xchg    eax, ecx
793
        mov     ebx, 2
794
        call    sys_system
795
        popad
796
@@:
797
        inc     eax
9692 Doczom 798
        add     ecx, sizeof.APPDATA
2288 clevermous 799
        jmp     .xd0
800
.xd1:
5130 serge 801
;release slot
802
 
803
        bts     [thr_slot_map], esi
804
 
805
        mov     ecx, [.process]
9715 Doczom 806
        lea     eax, [ecx + PROC.thr_list]
807
        cmp     eax, [eax + LHEAD.next]
5130 serge 808
        jne     @F
809
 
810
        call    destroy_process.internal
811
@@:
2288 clevermous 812
        sti     ; .. and life goes on
813
 
814
        mov     eax, [draw_limits.left]
815
        mov     ebx, [draw_limits.top]
816
        mov     ecx, [draw_limits.right]
817
        mov     edx, [draw_limits.bottom]
818
        call    calculatescreen
819
        xor     eax, eax
820
        xor     esi, esi
821
        call    redrawscreen
822
 
3534 clevermous 823
        call    unlock_application_table
2288 clevermous 824
    ;mov   esi,process_terminated
825
    ;call  sys_msg_board_str
5130 serge 826
        add     esp, 8
2288 clevermous 827
        ret
828
restore .slot
5130 serge 829
restore .process
2288 clevermous 830
 
3296 clevermous 831
; Three following procedures are used to guarantee that
832
; some part of kernel code will not be terminated from outside
833
; while it is running.
834
; Note: they do not protect a thread from terminating due to errors inside
835
; the thread; accessing a nonexisting memory would still terminate it.
836
 
837
; First two procedures must be used in pair by thread-to-be-protected
838
; to signal the beginning and the end of an important part.
839
; It is OK to have nested areas.
840
 
841
; The last procedure must be used by outside wanna-be-terminators;
842
; if it is safe to terminate the given thread immediately, it returns eax=1;
843
; otherwise, it returns eax=0 and notifies the target thread that it should
844
; terminate itself when leaving a critical area (the last critical area if
845
; they are nested).
846
 
847
; Implementation. Those procedures use one dword in APPDATA for the thread,
848
; APPDATA.terminate_protection.
849
; * The upper bit is 1 during normal operations and 0 when terminate is requested.
850
; * Other bits form a number = depth of critical regions,
851
;   plus 1 if the upper bit is 1.
852
; * When this dword goes to zero, the thread should be destructed,
853
;   and the procedure in which it happened becomes responsible for destruction.
854
 
855
; Enter critical area. Called by thread which wants to be protected.
856
proc protect_from_terminate
857
        mov     edx, [current_slot]
858
; Atomically increment depth of critical areas and get the old value.
859
        mov     eax, 1
9715 Doczom 860
        lock xadd [edx + APPDATA.terminate_protection], eax
3296 clevermous 861
; If the old value was zero, somebody has started to terminate us,
862
; so we are destructing and cannot do anything protected.
863
; Otherwise, return to the caller.
864
        test    eax, eax
865
        jz      @f
866
        ret
867
@@:
868
; Wait for somebody to finish us.
869
        call    change_task
870
        jmp     @b
871
endp
872
 
873
; Leave critical area. Called by thread which wants to be protected.
874
proc unprotect_from_terminate
875
        mov     edx, [current_slot]
876
; Atomically decrement depth of critical areas.
9715 Doczom 877
        lock dec [edx + APPDATA.terminate_protection]
3296 clevermous 878
; If the result of decrement is zero, somebody has requested termination,
879
; but at that moment we were inside a critical area; terminate now.
880
        jz      sys_end
881
; Otherwise, return to the caller.
882
        ret
883
endp
884
 
8093 dunkaist 885
; Request termination of thread identified by edx = SLOT_BASE + slot*sizeof.APPDATA.
3296 clevermous 886
; Called by anyone.
887
proc request_terminate
888
        xor     eax, eax        ; set return value
889
; Atomically clear the upper bit. If it was already zero, then
890
; somebody has requested termination before us, so just exit.
9715 Doczom 891
        lock btr [edx + APPDATA.terminate_protection], 31
3296 clevermous 892
        jnc     .unsafe
893
; Atomically decrement depth of critical areas.
9715 Doczom 894
        lock dec [edx + APPDATA.terminate_protection]
3296 clevermous 895
; If the result of decrement is nonzero, the target thread is inside a
896
; critical area; leave termination to leaving that area.
897
        jnz     .unsafe
898
; Otherwise, it is safe to kill the target now and the caller is responsible
899
; for this. Return eax=1.
900
        inc     eax
901
.unsafe:
902
        ret
903
endp
904