Subversion Repositories Kolibri OS

Rev

Rev 4265 | Rev 4423 | 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: 4287 $
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
3555 Serge 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
3908 Serge 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
 
3555 Serge 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
 
3555 Serge 117
exc_c:                          ; исключения (все, кроме 7-го - #NM)
118
; Фрэйм стека при исключении/прерывании из 3-го кольца + pushad (т.е., именно здесь)
1056 Galkov 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
3555 Serge 124
 ; это фрэйм от pushad
1056 Galkov 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
 
3555 Serge 134
        mov     ax, app_data        ;исключение
135
        mov     ds, ax                  ;загрузим правильные значения
136
        mov     es, ax                  ;в регистры
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
3555 Serge 162
        call    wakeup_osloop
3626 Serge 163
        call    change_task
164
; If we're here, then the main OS thread has crashed before initializing IDLE thread.
165
; Or they both have crashed. Anyway, things are hopelessly broken.
166
        hlt
167
        jmp     $-1
40 halyavin 168
.debug:
169
; we are debugged process, notify debugger and suspend ourself
170
; eax=debugger PID
2434 Serge 171
        mov     ecx, 1          ; debug_message code=other_exception
172
        cmp     bl, 1           ; #DB
1076 Galkov 173
        jne     .notify         ; notify debugger and suspend ourself
174
        mov     ebx, dr6        ; debug_message data=DR6_image
175
        xor     edx, edx
176
        mov     dr6, edx
177
        mov     edx, dr7
178
        mov     cl, not 8
2434 Serge 179
  .l1:
180
        shl     dl, 2
1076 Galkov 181
        jc      @f
182
        and     bl, cl
2434 Serge 183
  @@:
184
        sar     cl, 1
1076 Galkov 185
        jc      .l1
186
        mov     cl, 3           ; debug_message code=debug_exception
187
.notify:
188
        push    ebx             ; debug_message data
189
        mov     ebx, [TASK_BASE]
190
        push    [ebx+TASKDATA.pid] ; PID
191
        push    ecx             ; debug_message code ((here: ecx==1/3))
192
        mov     cl, 12          ; debug_message size
193
        call    debugger_notify ;; only ONE using, inline ??? SEE: core/debug.inc
2434 Serge 194
        add     esp, 12
1076 Galkov 195
        mov     edx, [TASK_BASE]
196
        mov     byte [edx+TASKDATA.state], 1 ; suspended
197
        call    change_task     ; SEE: core/shed.inc
198
        restore_ring3_context
199
        iretd
1 ha 200
 
1056 Galkov 201
IRetToUserHook:
202
        xchg    eax, [reg_eip]
1074 Galkov 203
        sub     dword[reg_esp3], 8
1056 Galkov 204
        mov     edi, [reg_esp3]
205
        stosd
1074 Galkov 206
        mov     [edi], ebx
1056 Galkov 207
        restore_ring3_context
1638 serge 208
; simply return control to interrupted process
1056 Galkov 209
unknown_interrupt:
210
        iretd
1 ha 211
 
658 Ghost 212
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
1638 serge 213
; bl - error vector
1 ha 214
show_error_parameters:
2010 serge 215
        cmp     bl, 0x06
216
        jnz     .no_ud
217
        push    ebx
218
        mov     ebx, ud_user_message
219
        mov     ebp, notifyapp
220
        call    fs_execute_from_sysdir_param
221
        pop     ebx
222
.no_ud:
2434 Serge 223
        mov     edx, [TASK_BASE];not scratched below
3500 Serge 224
        if lang eq sp
225
        DEBUGF  1, "K : Proceso - terminado forzado PID: %x [%s]\n", [edx+TASKDATA.pid], [current_slot]
226
        else
227
        DEBUGF  1, "K : Process - forced terminate PID: %x [%s]\n", [edx+TASKDATA.pid], [current_slot]
228
        end if
1056 Galkov 229
        cmp     bl, 0x08
230
        jb      .l0
231
        cmp     bl, 0x0e
232
        jbe     .l1
2434 Serge 233
  .l0:
234
        mov     bl, 0x09
235
  .l1:
236
        mov     eax, [msg_fault_sel+ebx*4 - 0x08*4]
1056 Galkov 237
        DEBUGF  1, "K : %s\n", eax
238
        mov     eax, [reg_cs3+4]
239
        mov     edi, msg_sel_app
240
        mov     ebx, [reg_esp3+4]
241
        cmp     eax, app_code
242
        je      @f
243
        mov     edi, msg_sel_ker
244
        mov     ebx, [reg_esp0+4]
2434 Serge 245
    @@:
246
        DEBUGF  1, "K : EAX : %x EBX : %x ECX : %x\n", [reg_eax+4], [reg_ebx+4], [reg_ecx+4]
1056 Galkov 247
        DEBUGF  1, "K : EDX : %x ESI : %x EDI : %x\n", [reg_edx+4], [reg_esi+4], [reg_edi+4]
248
        DEBUGF  1, "K : EBP : %x EIP : %x ESP : %x\n", [reg_ebp+4], [reg_eip+4], ebx
249
        DEBUGF  1, "K : Flags : %x CS : %x (%s)\n", [reg_eflags+4], eax, edi
4265 Serge 250
 
251
        DEBUGF  1, "K : Stack dump:\n"
252
        push    eax ebx ecx edx
253
        call    .check_ESP
254
        test    eax, eax
255
        jnz     .error_ESP
256
        DEBUGF  1, "K : [ESP+00]: %x",[ebx]
257
        add     ebx, 4
258
        call    .check_ESP
259
        test    eax, eax
260
        jnz     .error_ESP
261
        DEBUGF  1, " [ESP+04]: %x",[ebx]
262
        add     ebx, 4
263
        call    .check_ESP
264
        test    eax, eax
265
        jnz     .error_ESP
266
        DEBUGF  1, " [ESP+08]: %x\n",[ebx]
267
        add     ebx, 4
268
        call    .check_ESP
269
        test    eax, eax
270
        jnz     .error_ESP
271
        DEBUGF  1, "K : [ESP+12]: %x",[ebx]
272
        add     ebx, 4
273
        call    .check_ESP
274
        test    eax, eax
275
        jnz     .error_ESP
276
        DEBUGF  1, " [ESP+16]: %x",[ebx]
277
        add     ebx, 4
278
        call    .check_ESP
279
        test    eax, eax
280
        jnz     .error_ESP
281
        DEBUGF  1, " [ESP+20]: %x\n",[ebx]
282
        add     ebx, 4
283
        call    .check_ESP
284
        test    eax, eax
285
        jnz     .error_ESP
286
        DEBUGF  1, "K : [ESP+24]: %x",[ebx]
287
        add     ebx, 4
288
        call    .check_ESP
289
        test    eax, eax
290
        jnz     .error_ESP
291
        DEBUGF  1, " [ESP+28]: %x",[ebx]
292
        add     ebx, 4
293
        call    .check_ESP
294
        test    eax, eax
295
        jnz     .error_ESP
296
        DEBUGF  1, " [ESP+32]: %x\n",[ebx]
297
        pop     edx ecx ebx eax
1056 Galkov 298
        ret
4265 Serge 299
.error_ESP:
300
        pop     edx ecx ebx eax
301
        DEBUGF  1, "\n"
302
        DEBUGF  1, "K : Unexpected end of the stack\n"
303
        ret
304
;--------------------------------------
305
.check_ESP:
306
        push    ebx
307
        shr     ebx, 12
308
        mov     ecx, ebx
309
        shr     ecx, 10
310
        mov     edx, [master_tab+ecx*4]
311
        test    edx, PG_MAP
312
        jz      .fail             ;page table is not created
313
                                  ;incorrect address in the program
314
 
315
        mov     eax, [page_tabs+ebx*4]
316
        test    eax, 2
317
        jz      .fail             ;address not reserved for use. error
318
 
319
        pop     ebx
320
        xor     eax, eax
321
        ret
322
 
323
.fail:
324
        pop     ebx
325
        xor     eax, eax
326
        dec     eax
327
        ret
658 Ghost 328
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
164 serge 329
 
1056 Galkov 330
  restore  reg_ss
331
  restore  reg_esp3
332
  restore  reg_eflags
333
  restore  reg_cs
334
  restore  reg_eip
335
  restore  reg_eax
336
  restore  reg_ecx
337
  restore  reg_edx
338
  restore  reg_ebx
339
  restore  reg_esp0
340
  restore  reg_ebp
341
  restore  reg_esi
342
  restore  reg_edi
16 poddubny 343
 
1638 serge 344
 
2106 serge 345
align 4
3555 Serge 346
lock_application_table:
347
        push    eax ecx edx
348
        mov     ecx, application_table_mutex
349
        call    mutex_lock
1 ha 350
 
2434 Serge 351
        mov     eax, [CURRENT_TASK]
352
        shl     eax, 5
353
        add     eax, CURRENT_TASK+TASKDATA.pid
354
        mov     eax, [eax]
1 ha 355
 
3555 Serge 356
        mov     [application_table_owner], eax
1 ha 357
 
3555 Serge 358
        pop     edx ecx eax
1 ha 359
 
1056 Galkov 360
        ret
1 ha 361
 
2106 serge 362
align 4
3555 Serge 363
unlock_application_table:
364
        push    eax ecx edx
1 ha 365
 
3555 Serge 366
        mov     [application_table_owner], 0
367
        mov     ecx, application_table_mutex
368
        call    mutex_unlock
1 ha 369
 
3555 Serge 370
        pop     edx ecx eax
1 ha 371
 
1056 Galkov 372
        ret
1 ha 373
 
3555 Serge 374
;  * eax = 64 - номер функции
375
;  * ebx = 1 - единственная подфункция
376
;  * ecx = новый размер памяти
377
;Возвращаемое значение:
378
;  * eax = 0 - успешно
379
;  * eax = 1 - недостаточно памяти
1306 Lrz 380
 
2106 serge 381
align 4
1 ha 382
sys_resize_app_memory:
1306 Lrz 383
        ; ebx = 1 - resize
384
        ; ecx = new amount of memory
1 ha 385
 
1306 Lrz 386
;        cmp    eax,1
2434 Serge 387
        dec     ebx
388
        jnz     .no_application_mem_resize
1307 Lrz 389
        stdcall new_mem_resize, ecx
2434 Serge 390
        mov     [esp+32], eax
164 serge 391
.no_application_mem_resize:
1056 Galkov 392
        ret
1 ha 393
 
394
iglobal
1056 Galkov 395
;  process_terminating  db 'K : Process - terminating',13,10,0
396
;  process_terminated   db 'K : Process - done',13,10,0
397
  msg_obj_destroy       db 'K : destroy app object',13,10,0
1 ha 398
endg
399
 
329 serge 400
; param
401
;  esi= slot
1 ha 402
 
2106 serge 403
align 4
1 ha 404
terminate: ; terminate application
405
 
1056 Galkov 406
           .slot equ esp   ;locals
329 serge 407
 
2434 Serge 408
        push    esi        ;save .slot
334 serge 409
 
2434 Serge 410
        shl     esi, 8
411
        cmp     [SLOT_BASE+esi+APPDATA.dir_table], 0
412
        jne     @F
413
        pop     esi
414
        shl     esi, 5
415
        mov     [CURRENT_TASK+esi+TASKDATA.state], 9
416
        ret
334 serge 417
@@:
3555 Serge 418
        lea     edx, [SLOT_BASE+esi]
419
        call    scheduler_remove_thread
1056 Galkov 420
           ;mov    esi,process_terminating
421
           ;call   sys_msg_board_str
3555 Serge 422
        call    lock_application_table
1 ha 423
 
709 diamond 424
; if the process is in V86 mode...
1056 Galkov 425
        mov     eax, [.slot]
426
        shl     eax, 8
427
        mov     esi, [eax+SLOT_BASE+APPDATA.pl0_stack]
428
        add     esi, RING0_STACK_SIZE
429
        cmp     [eax+SLOT_BASE+APPDATA.saved_esp0], esi
430
        jz      .nov86
709 diamond 431
; ...it has page directory for V86 mode
1056 Galkov 432
        mov     esi, [eax+SLOT_BASE+APPDATA.saved_esp0]
433
        mov     ecx, [esi+4]
434
        mov     [eax+SLOT_BASE+APPDATA.dir_table], ecx
709 diamond 435
; ...and I/O permission map for V86 mode
1056 Galkov 436
        mov     ecx, [esi+12]
437
        mov     [eax+SLOT_BASE+APPDATA.io_map], ecx
438
        mov     ecx, [esi+8]
439
        mov     [eax+SLOT_BASE+APPDATA.io_map+4], ecx
709 diamond 440
.nov86:
441
 
2434 Serge 442
        mov     esi, [.slot]
443
        shl     esi, 8
444
        add     esi, SLOT_BASE+APP_OBJ_OFFSET
329 serge 445
@@:
2434 Serge 446
        mov     eax, [esi+APPOBJ.fd]
447
        test    eax, eax
448
        jz      @F
334 serge 449
 
2434 Serge 450
        cmp     eax, esi
451
        je      @F
164 serge 452
 
2434 Serge 453
        push    esi
454
        call    [eax+APPOBJ.destroy]
1056 Galkov 455
           DEBUGF 1,"%s",msg_obj_destroy
2434 Serge 456
        pop     esi
457
        jmp     @B
329 serge 458
@@:
1311 diamond 459
 
2434 Serge 460
        mov     eax, [.slot]
461
        shl     eax, 8
462
        stdcall destroy_app_space, [SLOT_BASE+eax+APPDATA.dir_table], [SLOT_BASE+eax+APPDATA.dlls_list_ptr]
1 ha 463
 
2434 Serge 464
        mov     esi, [.slot]
3555 Serge 465
        cmp     [fpu_owner], esi ; if user fpu last -> fpu user = 2
2434 Serge 466
        jne     @F
164 serge 467
 
3555 Serge 468
        mov     [fpu_owner], 2
469
        mov     eax, [256*2+SLOT_BASE+APPDATA.fpu_state]
2434 Serge 470
        clts
471
        bt      [cpu_caps], CAPS_SSE
472
        jnc     .no_SSE
473
        fxrstor [eax]
474
        jmp     @F
203 serge 475
.no_SSE:
2434 Serge 476
        fnclex
477
        frstor  [eax]
357 serge 478
@@:
203 serge 479
 
2434 Serge 480
        mov     [KEY_COUNT], byte 0    ; empty keyboard buffer
481
        mov     [BTN_COUNT], byte 0    ; empty button buffer
1 ha 482
 
483
 
92 diamond 484
; remove defined hotkeys
1056 Galkov 485
        mov     eax, hotkey_list
92 diamond 486
.loop:
1056 Galkov 487
        cmp     [eax+8], esi
488
        jnz     .cont
489
        mov     ecx, [eax]
490
        jecxz   @f
491
        push    dword [eax+12]
492
        pop     dword [ecx+12]
92 diamond 493
@@:
1056 Galkov 494
        mov     ecx, [eax+12]
495
        push    dword [eax]
496
        pop     dword [ecx]
497
        xor     ecx, ecx
498
        mov     [eax], ecx
499
        mov     [eax+4], ecx
500
        mov     [eax+8], ecx
501
        mov     [eax+12], ecx
92 diamond 502
.cont:
1056 Galkov 503
        add     eax, 16
504
        cmp     eax, hotkey_list+256*16
505
        jb      .loop
2987 Serge 506
; get process PID
507
        mov     eax, esi
508
        shl     eax, 5
509
        mov     eax, [eax+CURRENT_TASK+TASKDATA.pid]
510
; compare current lock input with process PID
511
        cmp     eax, [PID_lock_input]
512
        jne     @f
513
 
514
        xor     eax, eax
515
        mov     [PID_lock_input], eax
516
@@:
92 diamond 517
; remove hotkeys in buffer
1056 Galkov 518
        mov     eax, hotkey_buffer
92 diamond 519
.loop2:
1056 Galkov 520
        cmp     [eax], esi
521
        jnz     .cont2
522
        and     dword [eax+4], 0
523
        and     dword [eax], 0
92 diamond 524
.cont2:
1056 Galkov 525
        add     eax, 8
526
        cmp     eax, hotkey_buffer+120*8
527
        jb      .loop2
92 diamond 528
 
2434 Serge 529
        mov     ecx, esi          ; remove buttons
1 ha 530
  bnewba2:
2434 Serge 531
        mov     edi, [BTN_ADDR]
532
        mov     eax, edi
533
        cld
534
        movzx   ebx, word [edi]
535
        inc     bx
1 ha 536
  bnewba:
2434 Serge 537
        dec     bx
538
        jz      bnmba
539
        add     eax, 0x10
540
        cmp     cx, [eax]
541
        jnz     bnewba
542
        pusha
543
        mov     ecx, ebx
544
        inc     ecx
545
        shl     ecx, 4
546
        mov     ebx, eax
547
        add     eax, 0x10
548
        call    memmove
549
        dec     dword [edi]
550
        popa
551
        jmp     bnewba2
1 ha 552
  bnmba:
553
 
2434 Serge 554
        pusha   ; save window coordinates for window restoring
555
        cld
556
        shl     esi, 5
557
        add     esi, window_data
558
        mov     eax, [esi+WDATA.box.left]
559
        mov     [draw_limits.left], eax
560
        add     eax, [esi+WDATA.box.width]
561
        mov     [draw_limits.right], eax
562
        mov     eax, [esi+WDATA.box.top]
563
        mov     [draw_limits.top], eax
564
        add     eax, [esi+WDATA.box.height]
565
        mov     [draw_limits.bottom], eax
1 ha 566
 
2434 Serge 567
        xor     eax, eax
568
        mov     [esi+WDATA.box.left], eax
569
        mov     [esi+WDATA.box.width], eax
570
        mov     [esi+WDATA.box.top], eax
571
        mov     [esi+WDATA.box.height], eax
572
        mov     [esi+WDATA.cl_workarea], eax
573
        mov     [esi+WDATA.cl_titlebar], eax
574
        mov     [esi+WDATA.cl_frames], eax
575
        mov     dword [esi+WDATA.reserved], eax; clear all flags: wstate, redraw, wdrawn
576
        lea     edi, [esi-window_data+draw_data]
577
        mov     ecx, 32/4
578
        rep stosd
579
        popa
1 ha 580
 
40 halyavin 581
; debuggee test
2434 Serge 582
        pushad
583
        mov     edi, esi
584
        shl     edi, 5
585
        mov     eax, [SLOT_BASE+edi*8+APPDATA.debugger_slot]
586
        test    eax, eax
587
        jz      .nodebug
3626 Serge 588
        movi    ecx, 8
2434 Serge 589
        push    dword [CURRENT_TASK+edi+TASKDATA.pid]; PID
590
        push    2
591
        call    debugger_notify
592
        pop     ecx
593
        pop     ecx
40 halyavin 594
.nodebug:
2434 Serge 595
        popad
40 halyavin 596
 
2434 Serge 597
        mov     ebx, [.slot]
598
        shl     ebx, 8
599
        push    ebx
600
        mov     ebx, [SLOT_BASE+ebx+APPDATA.pl0_stack]
1 ha 601
 
2434 Serge 602
        stdcall kernel_free, ebx
1 ha 603
 
2434 Serge 604
        pop     ebx
605
        mov     ebx, [SLOT_BASE+ebx+APPDATA.cur_dir]
606
        stdcall kernel_free, ebx
521 diamond 607
 
2434 Serge 608
        mov     edi, [.slot]
609
        shl     edi, 8
610
        add     edi, SLOT_BASE
465 serge 611
 
2434 Serge 612
        mov     eax, [edi+APPDATA.io_map]
613
        cmp     eax, [SLOT_BASE+256+APPDATA.io_map]
614
        je      @F
615
        call    free_page
465 serge 616
@@:
2434 Serge 617
        mov     eax, [edi+APPDATA.io_map+4]
618
        cmp     eax, [SLOT_BASE+256+APPDATA.io_map+4]
619
        je      @F
620
        call    free_page
465 serge 621
@@:
2434 Serge 622
        mov     eax, 0x20202020
623
        stosd
624
        stosd
625
        stosd
626
        mov     ecx, 244/4
627
        xor     eax, eax
628
        rep stosd
1 ha 629
 
102 poddubny 630
  ; activate window
2434 Serge 631
        movzx   eax, word [WIN_STACK + esi*2]
632
        cmp     eax, [TASK_COUNT]
633
        jne     .dont_activate
1056 Galkov 634
        pushad
102 poddubny 635
 .check_next_window:
2434 Serge 636
        dec     eax
637
        cmp     eax, 1
638
        jbe     .nothing_to_activate
639
        lea     esi, [WIN_POS+eax*2]
640
        movzx   edi, word [esi]              ; edi = process
641
        shl     edi, 5
642
        cmp     [CURRENT_TASK + edi + TASKDATA.state], byte 9 ; skip dead slots
643
        je      .check_next_window
644
        add     edi, window_data
154 diamond 645
; \begin{diamond}[19.09.2006]
646
; skip minimized windows
2434 Serge 647
        test    [edi + WDATA.fl_wstate], WSTATE_MINIMIZED
648
        jnz     .check_next_window
154 diamond 649
; \end{diamond}
2434 Serge 650
        call    waredraw
102 poddubny 651
 .nothing_to_activate:
1056 Galkov 652
        popad
102 poddubny 653
 .dont_activate:
654
 
1056 Galkov 655
        push    esi     ; remove hd1 & cd & flp reservation
656
        shl     esi, 5
657
        mov     esi, [esi+CURRENT_TASK+TASKDATA.pid]
658
        cmp     [cd_status], esi
659
        jnz     @f
660
        call    free_cd_channel
1306 Lrz 661
        and     [cd_status], 0
92 diamond 662
@@:
1056 Galkov 663
        pop     esi
664
        cmp     [bgrlockpid], esi
665
        jnz     @f
666
        and     [bgrlockpid], 0
1306 Lrz 667
        and     [bgrlock], 0
546 diamond 668
@@:
1 ha 669
 
2434 Serge 670
        pusha                 ; remove all port reservations
671
        mov     edx, esi
672
        shl     edx, 5
673
        add     edx, CURRENT_TASK
674
        mov     edx, [edx+TASKDATA.pid]
1 ha 675
 
676
  rmpr0:
677
 
2434 Serge 678
        mov     esi, [RESERVED_PORTS]
1 ha 679
 
2434 Serge 680
        test    esi, esi
681
        jz      rmpr9
1 ha 682
 
683
  rmpr3:
684
 
2434 Serge 685
        mov     edi, esi
686
        shl     edi, 4
687
        add     edi, RESERVED_PORTS
1 ha 688
 
2434 Serge 689
        cmp     edx, [edi]
690
        je      rmpr4
1 ha 691
 
2434 Serge 692
        dec     esi
693
        jnz     rmpr3
1 ha 694
 
2434 Serge 695
        jmp     rmpr9
1 ha 696
 
697
  rmpr4:
698
 
2434 Serge 699
        mov     ecx, 256
700
        sub     ecx, esi
701
        shl     ecx, 4
1 ha 702
 
2434 Serge 703
        mov     esi, edi
704
        add     esi, 16
705
        cld
706
        rep movsb
1 ha 707
 
2434 Serge 708
        dec     dword [RESERVED_PORTS]
1 ha 709
 
2434 Serge 710
        jmp     rmpr0
1 ha 711
 
712
  rmpr9:
713
 
2434 Serge 714
        popa
715
        mov     edi, esi ; do not run this process slot
716
        shl     edi, 5
717
        mov     [edi+CURRENT_TASK + TASKDATA.state], byte 9
40 halyavin 718
; debugger test - terminate all debuggees
2434 Serge 719
        mov     eax, 2
720
        mov     ecx, SLOT_BASE+2*0x100+APPDATA.debugger_slot
40 halyavin 721
.xd0:
2434 Serge 722
        cmp     eax, [TASK_COUNT]
723
        ja      .xd1
724
        cmp     dword [ecx], esi
725
        jnz     @f
726
        and     dword [ecx], 0
727
        pushad
728
        xchg    eax, ecx
729
        mov     ebx, 2
730
        call    sys_system
731
        popad
40 halyavin 732
@@:
2434 Serge 733
        inc     eax
734
        add     ecx, 0x100
735
        jmp     .xd0
40 halyavin 736
.xd1:
1 ha 737
;    call  systest
2434 Serge 738
        sti     ; .. and life goes on
1 ha 739
 
2434 Serge 740
        mov     eax, [draw_limits.left]
741
        mov     ebx, [draw_limits.top]
742
        mov     ecx, [draw_limits.right]
743
        mov     edx, [draw_limits.bottom]
744
        call    calculatescreen
745
        xor     eax, eax
746
        xor     esi, esi
747
        call    redrawscreen
1 ha 748
 
3555 Serge 749
        call    unlock_application_table
506 mikedld 750
    ;mov   esi,process_terminated
751
    ;call  sys_msg_board_str
2434 Serge 752
        add     esp, 4
753
        ret
345 serge 754
restore .slot
1 ha 755
 
2987 Serge 756
;build_scheduler:
757
;        mov     esi, boot_sched_1
758
;        call    boot_log
759
;        call   build_process_gdt_tss_pointer
1 ha 760
 
2987 Serge 761
;        mov    esi,boot_sched_2
762
;        call   boot_log
763
;        ret
3500 Serge 764
 
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
794
        lock xadd [edx+APPDATA.terminate_protection], eax
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.
811
        lock dec [edx+APPDATA.terminate_protection]
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
 
819
; Request termination of thread identified by edx = SLOT_BASE + slot*256.
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.
825
        lock btr [edx+APPDATA.terminate_protection], 31
826
        jnc     .unsafe
827
; Atomically decrement depth of critical areas.
828
        lock dec [edx+APPDATA.terminate_protection]
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