Subversion Repositories Kolibri OS

Rev

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