Subversion Repositories Kolibri OS

Rev

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