Subversion Repositories Kolibri OS

Rev

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