Subversion Repositories Kolibri OS

Rev

Rev 9926 | Rev 9975 | Go to most recent revision | 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. ;;
6793 pathoswith 4
;;  Distributed under terms of the GNU General Public License.  ;;
2288 clevermous 5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
$Revision: 9930 $
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]
9715 Doczom 142
        btr     [esi + APPDATA.except_mask], ebx
2288 clevermous 143
        jnc     @f
9715 Doczom 144
        mov     eax, [esi + APPDATA.exc_handler]
2288 clevermous 145
        test    eax, eax
146
        jnz     IRetToUserHook
147
  @@:
148
        cli
9715 Doczom 149
        mov     eax, [esi + APPDATA.debugger_slot]
2288 clevermous 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]
3911 mario79 291
        pop     edx ecx ebx eax
2288 clevermous 292
        ret
3911 mario79 293
.error_ESP:
294
        pop     edx ecx ebx eax
295
        DEBUGF  1, "\n"
296
        DEBUGF  1, "K : Unexpected end of the stack\n"
297
        ret
298
;--------------------------------------
299
.check_ESP:
300
        push    ebx
301
        shr     ebx, 12
302
        mov     ecx, ebx
303
        shr     ecx, 10
9715 Doczom 304
        mov     edx, [master_tab + ecx*4]
5356 serge 305
        test    edx, PG_READ
8050 rgimad 306
        jz      .fail             ; page table is not created
307
                                  ; incorrect address in the program
3911 mario79 308
 
9715 Doczom 309
        mov     eax, [page_tabs + ebx*4]
3911 mario79 310
        test    eax, 2
8050 rgimad 311
        jz      .fail             ; address not reserved for use. error
5356 serge 312
 
313
        pop     ebx
3911 mario79 314
        xor     eax, eax
5356 serge 315
        ret
316
 
3911 mario79 317
.fail:
318
        pop     ebx
319
        xor     eax, eax
320
        dec     eax
321
        ret
2288 clevermous 322
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
323
 
324
  restore  reg_ss
325
  restore  reg_esp3
326
  restore  reg_eflags
327
  restore  reg_cs
328
  restore  reg_eip
329
  restore  reg_eax
330
  restore  reg_ecx
331
  restore  reg_edx
332
  restore  reg_ebx
333
  restore  reg_esp0
334
  restore  reg_ebp
335
  restore  reg_esi
336
  restore  reg_edi
337
 
338
 
339
align 4
3534 clevermous 340
lock_application_table:
341
        push    eax ecx edx
342
        mov     ecx, application_table_mutex
343
        call    mutex_lock
2288 clevermous 344
 
9828 Doczom 345
        mov     eax, [current_slot]
346
        mov     eax, [eax + APPDATA.tid]
2288 clevermous 347
 
3534 clevermous 348
        mov     [application_table_owner], eax
2288 clevermous 349
 
3534 clevermous 350
        pop     edx ecx eax
2288 clevermous 351
 
352
        ret
353
 
354
align 4
3534 clevermous 355
unlock_application_table:
356
        push    eax ecx edx
2288 clevermous 357
 
3534 clevermous 358
        mov     [application_table_owner], 0
359
        mov     ecx, application_table_mutex
360
        call    mutex_unlock
2288 clevermous 361
 
3534 clevermous 362
        pop     edx ecx eax
2288 clevermous 363
 
364
        ret
365
 
8050 rgimad 366
; sysfn 64 implementation
2288 clevermous 367
align 4
368
sys_resize_app_memory:
8050 rgimad 369
; in:   eax = 64 - function number
370
;       ebx = 1 - number of its only subfunction
371
;       ecx = new amount of memory
372
; out:
373
;       eax = 0 - success
374
;       eax = 1 - out of memory
2288 clevermous 375
 
376
;        cmp    eax,1
377
        dec     ebx
378
        jnz     .no_application_mem_resize
4313 mario79 379
 
380
        mov     eax, [pg_data.pages_free]
381
        shl     eax, 12
382
        cmp     eax, ecx
383
        jae     @f
384
 
385
        xor     eax, eax
386
        inc     eax
387
        jmp     .store_result
388
@@:
2288 clevermous 389
        stdcall new_mem_resize, ecx
4313 mario79 390
.store_result:
9831 dunkaist 391
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 392
.no_application_mem_resize:
393
        ret
394
 
395
iglobal
396
;  process_terminating  db 'K : Process - terminating',13,10,0
397
;  process_terminated   db 'K : Process - done',13,10,0
398
  msg_obj_destroy       db 'K : destroy app object',13,10,0
399
endg
400
 
401
; param
402
;  esi= slot
403
 
404
align 4
405
terminate: ; terminate application
5130 serge 406
destroy_thread:
2288 clevermous 407
 
5130 serge 408
        .slot     equ esp+4             ;locals
409
        .process  equ esp               ;ptr to parent process
2288 clevermous 410
 
5130 serge 411
 
2288 clevermous 412
        push    esi        ;save .slot
413
 
9709 Doczom 414
        shl     esi, BSF sizeof.APPDATA
9715 Doczom 415
        mov     edx, [SLOT_BASE + esi + APPDATA.process]
5130 serge 416
        test    edx, edx
417
        jnz     @F
9709 Doczom 418
        mov     [SLOT_BASE + esi + APPDATA.state], TSTATE_FREE
2288 clevermous 419
        pop     esi
420
        ret
421
@@:
5130 serge 422
        push    edx                     ;save .process
9715 Doczom 423
        lea     edx, [SLOT_BASE + esi]
3534 clevermous 424
        call    scheduler_remove_thread
425
        call    lock_application_table
2288 clevermous 426
 
427
; if the process is in V86 mode...
428
        mov     eax, [.slot]
9709 Doczom 429
        shl     eax, BSF sizeof.APPDATA
9715 Doczom 430
        mov     esi, [SLOT_BASE + eax + APPDATA.pl0_stack]
2288 clevermous 431
        add     esi, RING0_STACK_SIZE
9715 Doczom 432
        cmp     [SLOT_BASE + eax + APPDATA.saved_esp0], esi
2288 clevermous 433
        jz      .nov86
434
; ...it has page directory for V86 mode
9715 Doczom 435
        mov     esi, [SLOT_BASE + eax + APPDATA.saved_esp0]
2288 clevermous 436
        mov     ecx, [esi+4]
9715 Doczom 437
        mov     [SLOT_BASE + eax + APPDATA.process], ecx
2288 clevermous 438
; ...and I/O permission map for V86 mode
439
        mov     ecx, [esi+12]
9715 Doczom 440
        mov     [SLOT_BASE + eax + APPDATA.io_map], ecx
2288 clevermous 441
        mov     ecx, [esi+8]
9715 Doczom 442
        mov     [SLOT_BASE + eax + APPDATA.io_map+4], ecx
2288 clevermous 443
.nov86:
8050 rgimad 444
; destroy per-thread kernel objects
2288 clevermous 445
        mov     esi, [.slot]
9709 Doczom 446
        shl     esi, BSF sizeof.APPDATA
9715 Doczom 447
        add     esi, SLOT_BASE + APP_OBJ_OFFSET
2288 clevermous 448
@@:
9715 Doczom 449
        mov     eax, [esi + APPOBJ.fd]
2288 clevermous 450
        test    eax, eax
451
        jz      @F
452
 
453
        cmp     eax, esi
454
        je      @F
455
 
456
        push    esi
9715 Doczom 457
        call    [eax + APPOBJ.destroy]
2288 clevermous 458
           DEBUGF 1,"%s",msg_obj_destroy
459
        pop     esi
460
        jmp     @B
461
@@:
462
        mov     esi, [.slot]
3534 clevermous 463
        cmp     [fpu_owner], esi ; if user fpu last -> fpu user = 2
2288 clevermous 464
        jne     @F
465
 
3534 clevermous 466
        mov     [fpu_owner], 2
9715 Doczom 467
        mov     eax, [SLOT_BASE + sizeof.APPDATA*2 + APPDATA.fpu_state]
2288 clevermous 468
        clts
469
        bt      [cpu_caps], CAPS_SSE
470
        jnc     .no_SSE
471
        fxrstor [eax]
472
        jmp     @F
473
.no_SSE:
474
        fnclex
475
        frstor  [eax]
476
@@:
477
 
478
        mov     [KEY_COUNT], byte 0    ; empty keyboard buffer
479
        mov     [BTN_COUNT], byte 0    ; empty button buffer
480
 
481
 
482
; remove defined hotkeys
483
        mov     eax, hotkey_list
484
.loop:
485
        cmp     [eax+8], esi
486
        jnz     .cont
487
        mov     ecx, [eax]
488
        jecxz   @f
489
        push    dword [eax+12]
490
        pop     dword [ecx+12]
491
@@:
492
        mov     ecx, [eax+12]
493
        push    dword [eax]
494
        pop     dword [ecx]
495
        xor     ecx, ecx
496
        mov     [eax], ecx
497
        mov     [eax+4], ecx
498
        mov     [eax+8], ecx
499
        mov     [eax+12], ecx
500
.cont:
501
        add     eax, 16
502
        cmp     eax, hotkey_list+256*16
503
        jb      .loop
2709 mario79 504
; get process PID
505
        mov     eax, esi
9692 Doczom 506
        shl     eax, BSF sizeof.APPDATA
507
        mov     eax, [eax + SLOT_BASE + APPDATA.tid]
2709 mario79 508
; compare current lock input with process PID
509
        cmp     eax, [PID_lock_input]
510
        jne     @f
511
 
512
        xor     eax, eax
513
        mov     [PID_lock_input], eax
514
@@:
2288 clevermous 515
; remove hotkeys in buffer
516
        mov     eax, hotkey_buffer
517
.loop2:
518
        cmp     [eax], esi
519
        jnz     .cont2
520
        and     dword [eax+4], 0
521
        and     dword [eax], 0
522
.cont2:
523
        add     eax, 8
524
        cmp     eax, hotkey_buffer+120*8
525
        jb      .loop2
526
 
527
        mov     ecx, esi          ; remove buttons
9910 Doczom 528
  .bnewba2:
2288 clevermous 529
        mov     edi, [BTN_ADDR]
530
        mov     eax, edi
531
        cld
532
        movzx   ebx, word [edi]
533
        inc     bx
9910 Doczom 534
  .bnewba:
2288 clevermous 535
        dec     bx
9910 Doczom 536
        jz      .bnmba
2288 clevermous 537
        add     eax, 0x10
538
        cmp     cx, [eax]
9910 Doczom 539
        jnz     .bnewba
2288 clevermous 540
        pusha
541
        mov     ecx, ebx
542
        inc     ecx
543
        shl     ecx, 4
544
        mov     ebx, eax
545
        add     eax, 0x10
546
        call    memmove
547
        dec     dword [edi]
548
        popa
9910 Doczom 549
        jmp     .bnewba2
550
  .bnmba:
2288 clevermous 551
 
552
        pusha   ; save window coordinates for window restoring
553
        cld
8858 rgimad 554
        shl     esi, BSF sizeof.WDATA
2288 clevermous 555
        add     esi, window_data
9715 Doczom 556
        mov     eax, [esi + WDATA.box.left]
2288 clevermous 557
        mov     [draw_limits.left], eax
9715 Doczom 558
        add     eax, [esi + WDATA.box.width]
2288 clevermous 559
        mov     [draw_limits.right], eax
9715 Doczom 560
        mov     eax, [esi + WDATA.box.top]
2288 clevermous 561
        mov     [draw_limits.top], eax
9715 Doczom 562
        add     eax, [esi + WDATA.box.height]
2288 clevermous 563
        mov     [draw_limits.bottom], eax
564
 
565
        xor     eax, eax
8858 rgimad 566
        mov     edi, esi
567
        mov     ecx, sizeof.WDATA/4
568
        rep stosd
2288 clevermous 569
        popa
570
 
571
; debuggee test
572
        pushad
573
        mov     edi, esi
9692 Doczom 574
        shl     edi, BSF sizeof.APPDATA
575
        mov     eax, [SLOT_BASE + edi + APPDATA.debugger_slot]
2288 clevermous 576
        test    eax, eax
577
        jz      .nodebug
3598 clevermous 578
        movi    ecx, 8
9692 Doczom 579
        push    dword [SLOT_BASE + edi + APPDATA.tid]; PID
2288 clevermous 580
        push    2
581
        call    debugger_notify
582
        pop     ecx
583
        pop     ecx
584
.nodebug:
585
        popad
586
 
587
        mov     ebx, [.slot]
9709 Doczom 588
        shl     ebx, BSF sizeof.APPDATA
2288 clevermous 589
        push    ebx
9715 Doczom 590
        mov     ebx, [SLOT_BASE + ebx + APPDATA.pl0_stack]
2288 clevermous 591
 
592
        stdcall kernel_free, ebx
593
 
594
        pop     ebx
9715 Doczom 595
        mov     ebx, [SLOT_BASE + ebx + APPDATA.cur_dir]
2288 clevermous 596
        stdcall kernel_free, ebx
597
 
598
        mov     edi, [.slot]
9709 Doczom 599
        shl     edi, BSF sizeof.APPDATA
2288 clevermous 600
        add     edi, SLOT_BASE
601
 
9715 Doczom 602
        mov     eax, [edi + APPDATA.io_map]
603
        cmp     eax, [SLOT_BASE + sizeof.APPDATA+APPDATA.io_map]
2288 clevermous 604
        je      @F
605
        call    free_page
606
@@:
607
        mov     eax, [edi+APPDATA.io_map+4]
8093 dunkaist 608
        cmp     eax, [SLOT_BASE+sizeof.APPDATA+APPDATA.io_map+4]
2288 clevermous 609
        je      @F
610
        call    free_page
611
@@:
9715 Doczom 612
        lea     ebx, [edi + APPDATA.list]
5130 serge 613
        list_del ebx                    ;destroys edx, ecx
614
 
2288 clevermous 615
  ; activate window
616
        movzx   eax, word [WIN_STACK + esi*2]
8866 rgimad 617
        cmp     eax, [thread_count]
2288 clevermous 618
        jne     .dont_activate
619
        pushad
620
 .check_next_window:
621
        dec     eax
622
        cmp     eax, 1
623
        jbe     .nothing_to_activate
9715 Doczom 624
        lea     esi, [WIN_POS + eax*2]
2288 clevermous 625
        movzx   edi, word [esi]              ; edi = process
9709 Doczom 626
        shl     edi, BSF sizeof.APPDATA
627
        cmp     [SLOT_BASE + edi + APPDATA.state], TSTATE_FREE ; skip free slots
2288 clevermous 628
        je      .check_next_window
9926 Doczom 629
        shr     edi, (BSF sizeof.APPDATA - BSF sizeof.WDATA)
2288 clevermous 630
        add     edi, window_data
631
; \begin{diamond}[19.09.2006]
632
; skip minimized windows
633
        test    [edi + WDATA.fl_wstate], WSTATE_MINIMIZED
634
        jnz     .check_next_window
635
; \end{diamond}
636
        call    waredraw
637
 .nothing_to_activate:
638
        popad
639
 .dont_activate:
640
 
641
        push    esi     ; remove hd1 & cd & flp reservation
9692 Doczom 642
        shl     esi, BSF sizeof.APPDATA
9715 Doczom 643
        mov     esi, [SLOT_BASE + esi + APPDATA.tid]
2288 clevermous 644
        cmp     [cd_status], esi
645
        jnz     @f
646
        call    free_cd_channel
647
        and     [cd_status], 0
648
@@:
649
        pop     esi
650
        cmp     [bgrlockpid], esi
651
        jnz     @f
652
        and     [bgrlockpid], 0
653
        and     [bgrlock], 0
654
@@:
655
 
656
        pusha                 ; remove all port reservations
657
        mov     edx, esi
9692 Doczom 658
        shl     edx, BSF sizeof.APPDATA
9715 Doczom 659
        mov     edx, [SLOT_BASE + edx + APPDATA.tid]
2288 clevermous 660
 
9910 Doczom 661
  .rmpr0:
2288 clevermous 662
        mov     esi, [RESERVED_PORTS]
663
 
664
        test    esi, esi
9910 Doczom 665
        jz      .rmpr9
2288 clevermous 666
 
9910 Doczom 667
  .rmpr3:
2288 clevermous 668
 
669
        mov     edi, esi
670
        shl     edi, 4
671
        add     edi, RESERVED_PORTS
672
 
673
        cmp     edx, [edi]
9910 Doczom 674
        je      .rmpr4
2288 clevermous 675
 
676
        dec     esi
9910 Doczom 677
        jnz     .rmpr3
2288 clevermous 678
 
9910 Doczom 679
        jmp     .rmpr9
2288 clevermous 680
 
9910 Doczom 681
  .rmpr4:
2288 clevermous 682
 
683
        mov     ecx, 256
684
        sub     ecx, esi
685
        shl     ecx, 4
686
 
687
        mov     esi, edi
688
        add     esi, 16
689
        cld
690
        rep movsb
691
 
692
        dec     dword [RESERVED_PORTS]
693
 
9910 Doczom 694
        jmp     .rmpr0
2288 clevermous 695
 
9910 Doczom 696
  .rmpr9:
9743 Doczom 697
        popa
2288 clevermous 698
 
9743 Doczom 699
; clearing APPDATA structure this thread
700
        pushad
701
        mov     edi, esi
702
        shl     edi, BSF sizeof.APPDATA
703
        add     edi, SLOT_BASE
704
        mov     eax, 0x20202020
705
        stosd
706
        stosd
707
        stosd
708
        mov     ecx, 244/4
709
        xor     eax, eax
710
        rep stosd
711
        popad
712
 
2288 clevermous 713
        mov     edi, esi ; do not run this process slot
9709 Doczom 714
        shl     edi, BSF sizeof.APPDATA
9715 Doczom 715
        mov     [SLOT_BASE + edi + APPDATA.state], TSTATE_FREE
2288 clevermous 716
; debugger test - terminate all debuggees
717
        mov     eax, 2
9715 Doczom 718
        mov     ecx, SLOT_BASE + 2*sizeof.APPDATA + APPDATA.debugger_slot
2288 clevermous 719
.xd0:
8866 rgimad 720
        cmp     eax, [thread_count]
2288 clevermous 721
        ja      .xd1
722
        cmp     dword [ecx], esi
723
        jnz     @f
724
        and     dword [ecx], 0
725
        pushad
726
        xchg    eax, ecx
727
        mov     ebx, 2
728
        call    sys_system
729
        popad
730
@@:
731
        inc     eax
9692 Doczom 732
        add     ecx, sizeof.APPDATA
2288 clevermous 733
        jmp     .xd0
734
.xd1:
5130 serge 735
;release slot
736
 
737
        bts     [thr_slot_map], esi
738
 
739
        mov     ecx, [.process]
9715 Doczom 740
        lea     eax, [ecx + PROC.thr_list]
741
        cmp     eax, [eax + LHEAD.next]
5130 serge 742
        jne     @F
743
 
744
        call    destroy_process.internal
745
@@:
2288 clevermous 746
        sti     ; .. and life goes on
747
 
748
        mov     eax, [draw_limits.left]
749
        mov     ebx, [draw_limits.top]
750
        mov     ecx, [draw_limits.right]
751
        mov     edx, [draw_limits.bottom]
752
        call    calculatescreen
753
        xor     eax, eax
754
        xor     esi, esi
755
        call    redrawscreen
756
 
3534 clevermous 757
        call    unlock_application_table
2288 clevermous 758
    ;mov   esi,process_terminated
759
    ;call  sys_msg_board_str
5130 serge 760
        add     esp, 8
2288 clevermous 761
        ret
762
restore .slot
5130 serge 763
restore .process
2288 clevermous 764
 
3296 clevermous 765
; Three following procedures are used to guarantee that
766
; some part of kernel code will not be terminated from outside
767
; while it is running.
768
; Note: they do not protect a thread from terminating due to errors inside
769
; the thread; accessing a nonexisting memory would still terminate it.
770
 
771
; First two procedures must be used in pair by thread-to-be-protected
772
; to signal the beginning and the end of an important part.
773
; It is OK to have nested areas.
774
 
775
; The last procedure must be used by outside wanna-be-terminators;
776
; if it is safe to terminate the given thread immediately, it returns eax=1;
777
; otherwise, it returns eax=0 and notifies the target thread that it should
778
; terminate itself when leaving a critical area (the last critical area if
779
; they are nested).
780
 
781
; Implementation. Those procedures use one dword in APPDATA for the thread,
782
; APPDATA.terminate_protection.
783
; * The upper bit is 1 during normal operations and 0 when terminate is requested.
784
; * Other bits form a number = depth of critical regions,
785
;   plus 1 if the upper bit is 1.
786
; * When this dword goes to zero, the thread should be destructed,
787
;   and the procedure in which it happened becomes responsible for destruction.
788
 
789
; Enter critical area. Called by thread which wants to be protected.
790
proc protect_from_terminate
791
        mov     edx, [current_slot]
792
; Atomically increment depth of critical areas and get the old value.
793
        mov     eax, 1
9715 Doczom 794
        lock xadd [edx + APPDATA.terminate_protection], eax
3296 clevermous 795
; If the old value was zero, somebody has started to terminate us,
796
; so we are destructing and cannot do anything protected.
797
; Otherwise, return to the caller.
798
        test    eax, eax
799
        jz      @f
800
        ret
801
@@:
802
; Wait for somebody to finish us.
803
        call    change_task
804
        jmp     @b
805
endp
806
 
807
; Leave critical area. Called by thread which wants to be protected.
808
proc unprotect_from_terminate
809
        mov     edx, [current_slot]
810
; Atomically decrement depth of critical areas.
9715 Doczom 811
        lock dec [edx + APPDATA.terminate_protection]
3296 clevermous 812
; If the result of decrement is zero, somebody has requested termination,
813
; but at that moment we were inside a critical area; terminate now.
814
        jz      sys_end
815
; Otherwise, return to the caller.
816
        ret
817
endp
818
 
8093 dunkaist 819
; Request termination of thread identified by edx = SLOT_BASE + slot*sizeof.APPDATA.
3296 clevermous 820
; Called by anyone.
821
proc request_terminate
822
        xor     eax, eax        ; set return value
823
; Atomically clear the upper bit. If it was already zero, then
824
; somebody has requested termination before us, so just exit.
9715 Doczom 825
        lock btr [edx + APPDATA.terminate_protection], 31
3296 clevermous 826
        jnc     .unsafe
827
; Atomically decrement depth of critical areas.
9715 Doczom 828
        lock dec [edx + APPDATA.terminate_protection]
3296 clevermous 829
; If the result of decrement is nonzero, the target thread is inside a
830
; critical area; leave termination to leaving that area.
831
        jnz     .unsafe
832
; Otherwise, it is safe to kill the target now and the caller is responsible
833
; for this. Return eax=1.
834
        inc     eax
835
.unsafe:
836
        ret
837
endp
838