Subversion Repositories Kolibri OS

Rev

Rev 3309 | Rev 3342 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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