Subversion Repositories Kolibri OS

Rev

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