Subversion Repositories Kolibri OS

Rev

Rev 9692 | Rev 9715 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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