Subversion Repositories Kolibri OS

Rev

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

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