Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2455 mario79 3
;; Copyright (C) KolibriOS team 2004-2011. All rights reserved. ;;
2288 clevermous 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
 
14
$Revision: 4313 $
15
 
16
align 4 ;3A08
17
build_interrupt_table:
18
        mov     edi, idts
19
        mov     esi, sys_int
20
        mov     ecx, 0x40
21
        mov     eax, (10001110b shl 24) + os_code
22
  @@:
23
        movsw   ;low word of code-entry
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
31
 
32
iglobal
33
  align 4
34
  sys_int:
35
    ;exception handlers addresses (for interrupt gate construction)
36
        dd      e0,e1,e2,e3,e4,e5,e6,except_7 ; SEE: core/fpu.inc
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
40
 
41
    ;interrupt handlers addresses (for interrupt gate construction)
42
        ; 0x20 .. 0x2F - IRQ handlers
43
        dd      irq0, irq_serv.irq_1, irq_serv.irq_2
44
        dd      irq_serv.irq_3, irq_serv.irq_4
45
        dd      irq_serv.irq_5,  irq_serv.irq_6,  irq_serv.irq_7
46
        dd      irq_serv.irq_8,  irq_serv.irq_9,  irq_serv.irq_10
47
        dd      irq_serv.irq_11, irq_serv.irq_12, irqD, irq_serv.irq_14, irq_serv.irq_15
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
56
 
57
    times 32 - IRQ_RESERVED dd unknown_interrupt
58
    ;int_0x40 gate trap (for directly copied)
59
        dw      i40 and 0xFFFF, os_code, 11101111b shl 8, i40 shr 16
60
 
61
  idtreg: ; data for LIDT instruction (!!! must be immediately below sys_int data)
62
        dw      2*($-sys_int-4)-1
63
        dd      idts ;0x8000B100
3539 clevermous 64
        dw      0    ;просто выравнивание
2288 clevermous 65
 
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
68
 
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
76
 
3342 yogev_ezra 77
  if lang eq sp
3344 yogev_ezra 78
    include 'core/sys32-sp.inc'
3342 yogev_ezra 79
  else
80
    msg_sel_ker   db "kernel", 0
81
    msg_sel_app   db "application", 0
3309 esevece 82
  end if
2288 clevermous 83
 
84
endg
85
 
86
macro save_ring3_context {
87
        pushad
88
}
89
macro restore_ring3_context {
90
        popad
91
}
92
macro exc_wo_code [num] {
93
  e#num :
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
98
 
99
macro exc_w_code [num] {
100
  e#num :
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
106
 
107
 
108
uglobal
109
  pf_err_code   dd ?
110
endg
111
 
3539 clevermous 112
page_fault_exc:                 ; дуракоусточивость: селекторы испорчены...
113
        pop     [ss:pf_err_code]; действительно до следующего #PF
2288 clevermous 114
        save_ring3_context
115
        mov     bl, 14
116
 
3539 clevermous 117
exc_c:                          ; исключения (все, кроме 7-го - #NM)
118
; Фрэйм стека при исключении/прерывании из 3-го кольца + pushad (т.е., именно здесь)
2288 clevermous 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
3539 clevermous 124
 ; это фрэйм от pushad
2288 clevermous 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
 
3539 clevermous 134
        mov     ax, app_data        ;исключение
135
        mov     ds, ax                  ;загрузим правильные значения
136
        mov     es, ax                  ;в регистры
137
        cld                     ; и приводим DF к стандарту
2288 clevermous 138
        movzx   ebx, bl
139
; redirect to V86 manager? (EFLAGS & 0x20000) != 0?
140
        test    byte[reg_eflags+2], 2
141
        jnz     v86_exc_c
142
        cmp     bl, 14          ; #PF
143
        jne     @f
144
        call    page_fault_handler ; SEE: core/memory.inc
145
  @@:
146
        mov     esi, [current_slot]
147
        btr     [esi+APPDATA.except_mask], ebx
148
        jnc     @f
149
        mov     eax, [esi+APPDATA.exc_handler]
150
        test    eax, eax
151
        jnz     IRetToUserHook
152
  @@:
153
        cli
154
        mov     eax, [esi+APPDATA.debugger_slot]
155
        test    eax, eax
156
        jnz     .debug
157
        sti
158
; not debuggee => say error and terminate
159
        call    show_error_parameters ;; only ONE using, inline ???
160
       ;mov     edx, [TASK_BASE]
161
        mov     [edx + TASKDATA.state], byte 4 ; terminate
3534 clevermous 162
        call    wakeup_osloop
3615 clevermous 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
2288 clevermous 168
.debug:
169
; we are debugged process, notify debugger and suspend ourself
170
; eax=debugger PID
171
        mov     ecx, 1          ; debug_message code=other_exception
172
        cmp     bl, 1           ; #DB
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
179
  .l1:
180
        shl     dl, 2
181
        jc      @f
182
        and     bl, cl
183
  @@:
184
        sar     cl, 1
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
194
        add     esp, 12
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
200
 
201
IRetToUserHook:
202
        xchg    eax, [reg_eip]
203
        sub     dword[reg_esp3], 8
204
        mov     edi, [reg_esp3]
205
        stosd
206
        mov     [edi], ebx
207
        restore_ring3_context
208
; simply return control to interrupted process
209
unknown_interrupt:
210
        iretd
211
 
212
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
213
; bl - error vector
214
show_error_parameters:
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:
223
        mov     edx, [TASK_BASE];not scratched below
3309 esevece 224
        if lang eq sp
225
        DEBUGF  1, "K : Proceso - terminado forzado PID: %x [%s]\n", [edx+TASKDATA.pid], [current_slot]
226
        else
3294 clevermous 227
        DEBUGF  1, "K : Process - forced terminate PID: %x [%s]\n", [edx+TASKDATA.pid], [current_slot]
3309 esevece 228
        end if
2288 clevermous 229
        cmp     bl, 0x08
230
        jb      .l0
231
        cmp     bl, 0x0e
232
        jbe     .l1
233
  .l0:
234
        mov     bl, 0x09
235
  .l1:
236
        mov     eax, [msg_fault_sel+ebx*4 - 0x08*4]
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]
245
    @@:
246
        DEBUGF  1, "K : EAX : %x EBX : %x ECX : %x\n", [reg_eax+4], [reg_ebx+4], [reg_ecx+4]
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
3911 mario79 250
 
3909 mario79 251
        DEBUGF  1, "K : Stack dump:\n"
3911 mario79 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
2288 clevermous 298
        ret
3911 mario79 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
2288 clevermous 328
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
329
 
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
343
 
344
 
345
align 4
3534 clevermous 346
lock_application_table:
347
        push    eax ecx edx
348
        mov     ecx, application_table_mutex
349
        call    mutex_lock
2288 clevermous 350
 
351
        mov     eax, [CURRENT_TASK]
352
        shl     eax, 5
353
        add     eax, CURRENT_TASK+TASKDATA.pid
354
        mov     eax, [eax]
355
 
3534 clevermous 356
        mov     [application_table_owner], eax
2288 clevermous 357
 
3534 clevermous 358
        pop     edx ecx eax
2288 clevermous 359
 
360
        ret
361
 
362
align 4
3534 clevermous 363
unlock_application_table:
364
        push    eax ecx edx
2288 clevermous 365
 
3534 clevermous 366
        mov     [application_table_owner], 0
367
        mov     ecx, application_table_mutex
368
        call    mutex_unlock
2288 clevermous 369
 
3534 clevermous 370
        pop     edx ecx eax
2288 clevermous 371
 
372
        ret
373
 
3539 clevermous 374
;  * eax = 64 - номер функции
375
;  * ebx = 1 - единственная подфункция
376
;  * ecx = новый размер памяти
377
;Возвращаемое значение:
378
;  * eax = 0 - успешно
379
;  * eax = 1 - недостаточно памяти
2288 clevermous 380
 
381
align 4
382
sys_resize_app_memory:
383
        ; ebx = 1 - resize
384
        ; ecx = new amount of memory
385
 
386
;        cmp    eax,1
387
        dec     ebx
388
        jnz     .no_application_mem_resize
4313 mario79 389
 
390
        mov     eax, [pg_data.pages_free]
391
        shl     eax, 12
392
        cmp     eax, ecx
393
        jae     @f
394
 
395
        xor     eax, eax
396
        inc     eax
397
        jmp     .store_result
398
@@:
2288 clevermous 399
        stdcall new_mem_resize, ecx
4313 mario79 400
.store_result:
2288 clevermous 401
        mov     [esp+32], eax
402
.no_application_mem_resize:
403
        ret
404
 
405
iglobal
406
;  process_terminating  db 'K : Process - terminating',13,10,0
407
;  process_terminated   db 'K : Process - done',13,10,0
408
  msg_obj_destroy       db 'K : destroy app object',13,10,0
409
endg
410
 
411
; param
412
;  esi= slot
413
 
414
align 4
415
terminate: ; terminate application
416
 
417
           .slot equ esp   ;locals
418
 
419
        push    esi        ;save .slot
420
 
421
        shl     esi, 8
422
        cmp     [SLOT_BASE+esi+APPDATA.dir_table], 0
423
        jne     @F
424
        pop     esi
425
        shl     esi, 5
426
        mov     [CURRENT_TASK+esi+TASKDATA.state], 9
427
        ret
428
@@:
3534 clevermous 429
        lea     edx, [SLOT_BASE+esi]
430
        call    scheduler_remove_thread
2288 clevermous 431
           ;mov    esi,process_terminating
432
           ;call   sys_msg_board_str
3534 clevermous 433
        call    lock_application_table
2288 clevermous 434
 
435
; if the process is in V86 mode...
436
        mov     eax, [.slot]
437
        shl     eax, 8
438
        mov     esi, [eax+SLOT_BASE+APPDATA.pl0_stack]
439
        add     esi, RING0_STACK_SIZE
440
        cmp     [eax+SLOT_BASE+APPDATA.saved_esp0], esi
441
        jz      .nov86
442
; ...it has page directory for V86 mode
443
        mov     esi, [eax+SLOT_BASE+APPDATA.saved_esp0]
444
        mov     ecx, [esi+4]
445
        mov     [eax+SLOT_BASE+APPDATA.dir_table], ecx
446
; ...and I/O permission map for V86 mode
447
        mov     ecx, [esi+12]
448
        mov     [eax+SLOT_BASE+APPDATA.io_map], ecx
449
        mov     ecx, [esi+8]
450
        mov     [eax+SLOT_BASE+APPDATA.io_map+4], ecx
451
.nov86:
452
 
453
        mov     esi, [.slot]
454
        shl     esi, 8
455
        add     esi, SLOT_BASE+APP_OBJ_OFFSET
456
@@:
457
        mov     eax, [esi+APPOBJ.fd]
458
        test    eax, eax
459
        jz      @F
460
 
461
        cmp     eax, esi
462
        je      @F
463
 
464
        push    esi
465
        call    [eax+APPOBJ.destroy]
466
           DEBUGF 1,"%s",msg_obj_destroy
467
        pop     esi
468
        jmp     @B
469
@@:
470
 
471
        mov     eax, [.slot]
472
        shl     eax, 8
473
        stdcall destroy_app_space, [SLOT_BASE+eax+APPDATA.dir_table], [SLOT_BASE+eax+APPDATA.dlls_list_ptr]
474
 
475
        mov     esi, [.slot]
3534 clevermous 476
        cmp     [fpu_owner], esi ; if user fpu last -> fpu user = 2
2288 clevermous 477
        jne     @F
478
 
3534 clevermous 479
        mov     [fpu_owner], 2
480
        mov     eax, [256*2+SLOT_BASE+APPDATA.fpu_state]
2288 clevermous 481
        clts
482
        bt      [cpu_caps], CAPS_SSE
483
        jnc     .no_SSE
484
        fxrstor [eax]
485
        jmp     @F
486
.no_SSE:
487
        fnclex
488
        frstor  [eax]
489
@@:
490
 
491
        mov     [KEY_COUNT], byte 0    ; empty keyboard buffer
492
        mov     [BTN_COUNT], byte 0    ; empty button buffer
493
 
494
 
495
; remove defined hotkeys
496
        mov     eax, hotkey_list
497
.loop:
498
        cmp     [eax+8], esi
499
        jnz     .cont
500
        mov     ecx, [eax]
501
        jecxz   @f
502
        push    dword [eax+12]
503
        pop     dword [ecx+12]
504
@@:
505
        mov     ecx, [eax+12]
506
        push    dword [eax]
507
        pop     dword [ecx]
508
        xor     ecx, ecx
509
        mov     [eax], ecx
510
        mov     [eax+4], ecx
511
        mov     [eax+8], ecx
512
        mov     [eax+12], ecx
513
.cont:
514
        add     eax, 16
515
        cmp     eax, hotkey_list+256*16
516
        jb      .loop
2709 mario79 517
; get process PID
518
        mov     eax, esi
519
        shl     eax, 5
520
        mov     eax, [eax+CURRENT_TASK+TASKDATA.pid]
521
; compare current lock input with process PID
522
        cmp     eax, [PID_lock_input]
523
        jne     @f
524
 
525
        xor     eax, eax
526
        mov     [PID_lock_input], eax
527
@@:
2288 clevermous 528
; remove hotkeys in buffer
529
        mov     eax, hotkey_buffer
530
.loop2:
531
        cmp     [eax], esi
532
        jnz     .cont2
533
        and     dword [eax+4], 0
534
        and     dword [eax], 0
535
.cont2:
536
        add     eax, 8
537
        cmp     eax, hotkey_buffer+120*8
538
        jb      .loop2
539
 
540
        mov     ecx, esi          ; remove buttons
541
  bnewba2:
542
        mov     edi, [BTN_ADDR]
543
        mov     eax, edi
544
        cld
545
        movzx   ebx, word [edi]
546
        inc     bx
547
  bnewba:
548
        dec     bx
549
        jz      bnmba
550
        add     eax, 0x10
551
        cmp     cx, [eax]
552
        jnz     bnewba
553
        pusha
554
        mov     ecx, ebx
555
        inc     ecx
556
        shl     ecx, 4
557
        mov     ebx, eax
558
        add     eax, 0x10
559
        call    memmove
560
        dec     dword [edi]
561
        popa
562
        jmp     bnewba2
563
  bnmba:
564
 
565
        pusha   ; save window coordinates for window restoring
566
        cld
567
        shl     esi, 5
568
        add     esi, window_data
569
        mov     eax, [esi+WDATA.box.left]
570
        mov     [draw_limits.left], eax
571
        add     eax, [esi+WDATA.box.width]
572
        mov     [draw_limits.right], eax
573
        mov     eax, [esi+WDATA.box.top]
574
        mov     [draw_limits.top], eax
575
        add     eax, [esi+WDATA.box.height]
576
        mov     [draw_limits.bottom], eax
577
 
578
        xor     eax, eax
579
        mov     [esi+WDATA.box.left], eax
580
        mov     [esi+WDATA.box.width], eax
581
        mov     [esi+WDATA.box.top], eax
582
        mov     [esi+WDATA.box.height], eax
583
        mov     [esi+WDATA.cl_workarea], eax
584
        mov     [esi+WDATA.cl_titlebar], eax
585
        mov     [esi+WDATA.cl_frames], eax
586
        mov     dword [esi+WDATA.reserved], eax; clear all flags: wstate, redraw, wdrawn
587
        lea     edi, [esi-window_data+draw_data]
588
        mov     ecx, 32/4
589
        rep stosd
590
        popa
591
 
592
; debuggee test
593
        pushad
594
        mov     edi, esi
595
        shl     edi, 5
596
        mov     eax, [SLOT_BASE+edi*8+APPDATA.debugger_slot]
597
        test    eax, eax
598
        jz      .nodebug
3598 clevermous 599
        movi    ecx, 8
2288 clevermous 600
        push    dword [CURRENT_TASK+edi+TASKDATA.pid]; PID
601
        push    2
602
        call    debugger_notify
603
        pop     ecx
604
        pop     ecx
605
.nodebug:
606
        popad
607
 
608
        mov     ebx, [.slot]
609
        shl     ebx, 8
610
        push    ebx
611
        mov     ebx, [SLOT_BASE+ebx+APPDATA.pl0_stack]
612
 
613
        stdcall kernel_free, ebx
614
 
615
        pop     ebx
616
        mov     ebx, [SLOT_BASE+ebx+APPDATA.cur_dir]
617
        stdcall kernel_free, ebx
618
 
619
        mov     edi, [.slot]
620
        shl     edi, 8
621
        add     edi, SLOT_BASE
622
 
623
        mov     eax, [edi+APPDATA.io_map]
624
        cmp     eax, [SLOT_BASE+256+APPDATA.io_map]
625
        je      @F
626
        call    free_page
627
@@:
628
        mov     eax, [edi+APPDATA.io_map+4]
629
        cmp     eax, [SLOT_BASE+256+APPDATA.io_map+4]
630
        je      @F
631
        call    free_page
632
@@:
633
        mov     eax, 0x20202020
634
        stosd
635
        stosd
636
        stosd
637
        mov     ecx, 244/4
638
        xor     eax, eax
639
        rep stosd
640
 
641
  ; activate window
642
        movzx   eax, word [WIN_STACK + esi*2]
643
        cmp     eax, [TASK_COUNT]
644
        jne     .dont_activate
645
        pushad
646
 .check_next_window:
647
        dec     eax
648
        cmp     eax, 1
649
        jbe     .nothing_to_activate
650
        lea     esi, [WIN_POS+eax*2]
651
        movzx   edi, word [esi]              ; edi = process
652
        shl     edi, 5
653
        cmp     [CURRENT_TASK + edi + TASKDATA.state], byte 9 ; skip dead slots
654
        je      .check_next_window
655
        add     edi, window_data
656
; \begin{diamond}[19.09.2006]
657
; skip minimized windows
658
        test    [edi + WDATA.fl_wstate], WSTATE_MINIMIZED
659
        jnz     .check_next_window
660
; \end{diamond}
661
        call    waredraw
662
 .nothing_to_activate:
663
        popad
664
 .dont_activate:
665
 
666
        push    esi     ; remove hd1 & cd & flp reservation
667
        shl     esi, 5
668
        mov     esi, [esi+CURRENT_TASK+TASKDATA.pid]
669
        cmp     [cd_status], esi
670
        jnz     @f
671
        call    free_cd_channel
672
        and     [cd_status], 0
673
@@:
674
        pop     esi
675
        cmp     [bgrlockpid], esi
676
        jnz     @f
677
        and     [bgrlockpid], 0
678
        and     [bgrlock], 0
679
@@:
680
 
681
        pusha                 ; remove all port reservations
682
        mov     edx, esi
683
        shl     edx, 5
684
        add     edx, CURRENT_TASK
685
        mov     edx, [edx+TASKDATA.pid]
686
 
687
  rmpr0:
688
 
689
        mov     esi, [RESERVED_PORTS]
690
 
691
        test    esi, esi
692
        jz      rmpr9
693
 
694
  rmpr3:
695
 
696
        mov     edi, esi
697
        shl     edi, 4
698
        add     edi, RESERVED_PORTS
699
 
700
        cmp     edx, [edi]
701
        je      rmpr4
702
 
703
        dec     esi
704
        jnz     rmpr3
705
 
706
        jmp     rmpr9
707
 
708
  rmpr4:
709
 
710
        mov     ecx, 256
711
        sub     ecx, esi
712
        shl     ecx, 4
713
 
714
        mov     esi, edi
715
        add     esi, 16
716
        cld
717
        rep movsb
718
 
719
        dec     dword [RESERVED_PORTS]
720
 
721
        jmp     rmpr0
722
 
723
  rmpr9:
724
 
725
        popa
726
        mov     edi, esi ; do not run this process slot
727
        shl     edi, 5
728
        mov     [edi+CURRENT_TASK + TASKDATA.state], byte 9
729
; debugger test - terminate all debuggees
730
        mov     eax, 2
731
        mov     ecx, SLOT_BASE+2*0x100+APPDATA.debugger_slot
732
.xd0:
733
        cmp     eax, [TASK_COUNT]
734
        ja      .xd1
735
        cmp     dword [ecx], esi
736
        jnz     @f
737
        and     dword [ecx], 0
738
        pushad
739
        xchg    eax, ecx
740
        mov     ebx, 2
741
        call    sys_system
742
        popad
743
@@:
744
        inc     eax
745
        add     ecx, 0x100
746
        jmp     .xd0
747
.xd1:
748
;    call  systest
749
        sti     ; .. and life goes on
750
 
751
        mov     eax, [draw_limits.left]
752
        mov     ebx, [draw_limits.top]
753
        mov     ecx, [draw_limits.right]
754
        mov     edx, [draw_limits.bottom]
755
        call    calculatescreen
756
        xor     eax, eax
757
        xor     esi, esi
758
        call    redrawscreen
759
 
3534 clevermous 760
        call    unlock_application_table
2288 clevermous 761
    ;mov   esi,process_terminated
762
    ;call  sys_msg_board_str
763
        add     esp, 4
764
        ret
765
restore .slot
766
 
2642 mario79 767
;build_scheduler:
768
;        mov     esi, boot_sched_1
769
;        call    boot_log
770
;        call   build_process_gdt_tss_pointer
2288 clevermous 771
 
2642 mario79 772
;        mov    esi,boot_sched_2
773
;        call   boot_log
774
;        ret
3296 clevermous 775
 
776
; Three following procedures are used to guarantee that
777
; some part of kernel code will not be terminated from outside
778
; while it is running.
779
; Note: they do not protect a thread from terminating due to errors inside
780
; the thread; accessing a nonexisting memory would still terminate it.
781
 
782
; First two procedures must be used in pair by thread-to-be-protected
783
; to signal the beginning and the end of an important part.
784
; It is OK to have nested areas.
785
 
786
; The last procedure must be used by outside wanna-be-terminators;
787
; if it is safe to terminate the given thread immediately, it returns eax=1;
788
; otherwise, it returns eax=0 and notifies the target thread that it should
789
; terminate itself when leaving a critical area (the last critical area if
790
; they are nested).
791
 
792
; Implementation. Those procedures use one dword in APPDATA for the thread,
793
; APPDATA.terminate_protection.
794
; * The upper bit is 1 during normal operations and 0 when terminate is requested.
795
; * Other bits form a number = depth of critical regions,
796
;   plus 1 if the upper bit is 1.
797
; * When this dword goes to zero, the thread should be destructed,
798
;   and the procedure in which it happened becomes responsible for destruction.
799
 
800
; Enter critical area. Called by thread which wants to be protected.
801
proc protect_from_terminate
802
        mov     edx, [current_slot]
803
; Atomically increment depth of critical areas and get the old value.
804
        mov     eax, 1
805
        lock xadd [edx+APPDATA.terminate_protection], eax
806
; If the old value was zero, somebody has started to terminate us,
807
; so we are destructing and cannot do anything protected.
808
; Otherwise, return to the caller.
809
        test    eax, eax
810
        jz      @f
811
        ret
812
@@:
813
; Wait for somebody to finish us.
814
        call    change_task
815
        jmp     @b
816
endp
817
 
818
; Leave critical area. Called by thread which wants to be protected.
819
proc unprotect_from_terminate
820
        mov     edx, [current_slot]
821
; Atomically decrement depth of critical areas.
822
        lock dec [edx+APPDATA.terminate_protection]
823
; If the result of decrement is zero, somebody has requested termination,
824
; but at that moment we were inside a critical area; terminate now.
825
        jz      sys_end
826
; Otherwise, return to the caller.
827
        ret
828
endp
829
 
830
; Request termination of thread identified by edx = SLOT_BASE + slot*256.
831
; Called by anyone.
832
proc request_terminate
833
        xor     eax, eax        ; set return value
834
; Atomically clear the upper bit. If it was already zero, then
835
; somebody has requested termination before us, so just exit.
836
        lock btr [edx+APPDATA.terminate_protection], 31
837
        jnc     .unsafe
838
; Atomically decrement depth of critical areas.
839
        lock dec [edx+APPDATA.terminate_protection]
840
; If the result of decrement is nonzero, the target thread is inside a
841
; critical area; leave termination to leaving that area.
842
        jnz     .unsafe
843
; Otherwise, it is safe to kill the target now and the caller is responsible
844
; for this. Return eax=1.
845
        inc     eax
846
.unsafe:
847
        ret
848
endp
849