Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2008. 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: 2010 $
  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.   @@:   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.     if  USE_COM_IRQ
  45.         dd      irq_serv.irq_3, irq_serv.irq_4
  46.     else
  47.         dd      p_irq3, p_irq4 ;??? íåñòûêîâêà
  48.     end if
  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.         ; I don`t known how to use IRQ_RESERVE
  53. if IRQ_RESERVE > 16
  54.         dd irq_serv.irq_16
  55.         dd irq_serv.irq_17
  56.         dd irq_serv.irq_18
  57.         dd irq_serv.irq_19
  58.         dd irq_serv.irq_20
  59.         dd irq_serv.irq_21
  60.         dd irq_serv.irq_22
  61.         dd irq_serv.irq_23
  62. end if
  63.  
  64.         times 32 - IRQ_RESERVE dd unknown_interrupt
  65.     ;int_0x40 gate trap (for directly copied)
  66.         dw      i40 and 0xFFFF, os_code, 11101111b shl 8, i40 shr 16
  67.  
  68.   idtreg: ; data for LIDT instruction (!!! must be immediately below sys_int data)
  69.         dw      2*($-sys_int-4)-1
  70.         dd      idts ;0x8000B100
  71.         dw      0    ;ïðîñòî âûðàâíèâàíèå
  72.  
  73.   msg_fault_sel dd  msg_exc_8,msg_exc_u,msg_exc_a,msg_exc_b
  74.                 dd  msg_exc_c,msg_exc_d,msg_exc_e
  75.  
  76.   msg_exc_8     db "Double fault", 0
  77.   msg_exc_u     db "Undefined Exception", 0
  78.   msg_exc_a     db "Invalid TSS", 0
  79.   msg_exc_b     db "Segment not present", 0
  80.   msg_exc_c     db "Stack fault", 0
  81.   msg_exc_d     db "General protection fault", 0
  82.   msg_exc_e     db "Page fault", 0
  83.  
  84.   msg_sel_ker   db "kernel", 0
  85.   msg_sel_app   db "application", 0
  86.  
  87. endg
  88.  
  89. macro save_ring3_context {
  90.         pushad
  91. }
  92. macro restore_ring3_context {
  93.         popad
  94. }
  95. macro exc_wo_code [num] {
  96.   e#num :
  97.         save_ring3_context
  98.         mov     bl, num
  99.         jmp     exc_c
  100. } exc_wo_code   0,1,2,3,4,5,6,15,16,19
  101.  
  102. macro exc_w_code [num] {
  103.   e#num :
  104.         add     esp, 4
  105.         save_ring3_context
  106.         mov     bl, num
  107.         jmp     exc_c
  108. } exc_w_code    8,9,10,11,12,13,17,18
  109.  
  110.  
  111. uglobal
  112.   pf_err_code   dd ?
  113. endg
  114.  
  115. page_fault_exc:                 ; äóðàêîóñòî÷èâîñòü: ñåëåêòîðû èñïîð÷åíû...
  116.         pop     [ss:pf_err_code]; äåéñòâèòåëüíî äî ñëåäóþùåãî #PF
  117.         save_ring3_context
  118.         mov     bl,14
  119.  
  120. exc_c:                          ; èñêëþ÷åíèÿ (âñå, êðîìå 7-ãî - #NM)
  121. ; Ôðýéì ñòåêà ïðè èñêëþ÷åíèè/ïðåðûâàíèè èç 3-ãî êîëüöà + pushad (ò.å., èìåííî çäåñü)
  122.   reg_ss        equ esp+0x30
  123.   reg_esp3      equ esp+0x2C
  124.   reg_eflags    equ esp+0x28
  125.   reg_cs3       equ esp+0x24
  126.   reg_eip       equ esp+0x20
  127.  ; ýòî ôðýéì îò pushad
  128.   reg_eax       equ esp+0x1C
  129.   reg_ecx       equ esp+0x18
  130.   reg_edx       equ esp+0x14
  131.   reg_ebx       equ esp+0x10
  132.   reg_esp0      equ esp+0x0C
  133.   reg_ebp       equ esp+0x08
  134.   reg_esi       equ esp+0x04
  135.   reg_edi       equ esp+0x00
  136.  
  137.         mov     ax, app_data    ;èñêëþ÷åíèå
  138.         mov     ds, ax          ;çàãðóçèì ïðàâèëüíûå çíà÷åíè
  139.         mov     es, ax          ;â ðåãèñòðû
  140.         cld                     ; è ïðèâîäèì DF ê ñòàíäàðòó
  141.         movzx   ebx,bl
  142. ; redirect to V86 manager? (EFLAGS & 0x20000) != 0?
  143.         test    byte[reg_eflags+2],2
  144.         jnz     v86_exc_c
  145.         cmp     bl,14           ; #PF
  146.         jne     @f
  147.         call    page_fault_handler ; SEE: core/memory.inc
  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.   @@:   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.        ;mov     edx, [TASK_BASE]
  162.         mov     [edx + TASKDATA.state], byte 4 ; terminate
  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:  shl     dl,2
  176.         jc      @f
  177.         and     bl, cl
  178.   @@:   sar     cl,1
  179.         jc      .l1
  180.         mov     cl, 3           ; debug_message code=debug_exception
  181. .notify:
  182.         push    ebx             ; debug_message data
  183.         mov     ebx, [TASK_BASE]
  184.         push    [ebx+TASKDATA.pid] ; PID
  185.         push    ecx             ; debug_message code ((here: ecx==1/3))
  186.         mov     cl, 12          ; debug_message size
  187.         call    debugger_notify ;; only ONE using, inline ??? SEE: core/debug.inc
  188.         add     esp,12
  189.         mov     edx, [TASK_BASE]
  190.         mov     byte [edx+TASKDATA.state], 1 ; suspended
  191.         call    change_task     ; SEE: core/shed.inc
  192.         restore_ring3_context
  193.         iretd
  194.  
  195. IRetToUserHook:
  196.         xchg    eax, [reg_eip]
  197.         sub     dword[reg_esp3], 8
  198.         mov     edi, [reg_esp3]
  199.         stosd
  200.         mov     [edi], ebx
  201.         restore_ring3_context
  202. ; simply return control to interrupted process
  203. unknown_interrupt:
  204.         iretd
  205.  
  206. ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  207. ; bl - error vector
  208. show_error_parameters:
  209.         cmp     bl, 0x06
  210.         jnz     .no_ud
  211.         push    ebx
  212.         mov     ebx, ud_user_message
  213.         mov     ebp, notifyapp
  214.         call    fs_execute_from_sysdir_param
  215.         pop     ebx
  216. .no_ud:
  217.         mov     edx,[TASK_BASE] ;not scratched below
  218.         DEBUGF  1, "K : Process - forced terminate PID: %x\n", [edx+TASKDATA.pid]
  219.         cmp     bl, 0x08
  220.         jb      .l0
  221.         cmp     bl, 0x0e
  222.         jbe     .l1
  223.   .l0:  mov     bl, 0x09
  224.   .l1:  mov     eax,[msg_fault_sel+ebx*4 - 0x08*4]
  225.         DEBUGF  1, "K : %s\n", eax
  226.         mov     eax, [reg_cs3+4]
  227.         mov     edi, msg_sel_app
  228.         mov     ebx, [reg_esp3+4]
  229.         cmp     eax, app_code
  230.         je      @f
  231.         mov     edi, msg_sel_ker
  232.         mov     ebx, [reg_esp0+4]
  233.     @@: DEBUGF  1, "K : EAX : %x EBX : %x ECX : %x\n", [reg_eax+4], [reg_ebx+4], [reg_ecx+4]
  234.         DEBUGF  1, "K : EDX : %x ESI : %x EDI : %x\n", [reg_edx+4], [reg_esi+4], [reg_edi+4]
  235.         DEBUGF  1, "K : EBP : %x EIP : %x ESP : %x\n", [reg_ebp+4], [reg_eip+4], ebx
  236.         DEBUGF  1, "K : Flags : %x CS : %x (%s)\n", [reg_eflags+4], eax, edi
  237.         ret
  238. ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  239.  
  240.   restore  reg_ss
  241.   restore  reg_esp3
  242.   restore  reg_eflags
  243.   restore  reg_cs
  244.   restore  reg_eip
  245.   restore  reg_eax
  246.   restore  reg_ecx
  247.   restore  reg_edx
  248.   restore  reg_ebx
  249.   restore  reg_esp0
  250.   restore  reg_ebp
  251.   restore  reg_esi
  252.   restore  reg_edi
  253.  
  254. ; IRQ runtime
  255.  
  256. ; irq1  ->  hid/keyboard.inc
  257. macro irqh [num] {
  258.   p_irq#num :
  259.         mov     edi, num
  260.         jmp     irqhandler
  261. }
  262.  
  263. irqh 2,3,4,5,7,8,9,10,11
  264. ; I don`t known how to use IRQ_RESERVE
  265. if IRQ_RESERVE > 16
  266. irqh 16, 17, 18, 19, 20, 21, 22, 23
  267. end if
  268.  
  269. p_irq6: ; FDC
  270.         save_ring3_context
  271.         mov     ax, app_data  ;os_data
  272.         mov     ds, ax
  273.         mov     es, ax
  274.         mov     edi, 6
  275.         cmp     [v86_irqhooks+edi*8], 0
  276.         jnz     v86_irq2
  277.         call    fdc_irq
  278.         mov     [check_idle_semaphore], 5
  279.         mov     al, 6
  280.         call    IRQ_EOI
  281.         restore_ring3_context
  282.         iret
  283.  
  284. irq13:  ; Math coprocessor
  285.         save_ring3_context
  286.         mov     ax, app_data  ;os_data
  287.         mov     ds, ax
  288.         mov     es, ax
  289.  
  290.         mov     dx, 0xf0
  291.         mov     al, 0
  292.         out     dx, al
  293.  
  294.         mov     al, 13
  295.         call    IRQ_EOI
  296.         restore_ring3_context
  297.         iret
  298.  
  299. p_irq14: ; HDD
  300.         save_ring3_context
  301.         mov     ax, app_data  ;os_data
  302.         mov     ds, ax
  303.         mov     es, ax
  304.         mov     edi, 14
  305.         cmp     [v86_irqhooks+edi*8], 0
  306.         jnz     v86_irq2
  307. ;       mov     byte [BOOT_VAR + 0x48E], 0xFF
  308.         call    [irq14_func]
  309.         mov     [check_idle_semaphore], 5
  310.         mov     al, 14
  311.         call    IRQ_EOI
  312.         restore_ring3_context
  313.         iret
  314.  
  315. p_irq15: ; HDD2
  316.         save_ring3_context
  317.         mov     ax, app_data  ;os_data
  318.         mov     ds, ax
  319.         mov     es, ax
  320.         mov     edi, 15
  321.         cmp     [v86_irqhooks+edi*8], 0
  322.         jnz     v86_irq2
  323. ;       mov     byte [BOOT_VAR + 0x48E], 0xFF
  324.         call    [irq15_func]
  325.         mov     [check_idle_semaphore], 5
  326.         mov     al, 15
  327.         call    IRQ_EOI
  328.         restore_ring3_context
  329.         iret
  330.  
  331. ready_for_next_irq:
  332.         mov     eax,5
  333.         mov     [check_idle_semaphore],eax
  334. ;        mov     al, 0x20
  335.         add     eax,(0x20-0x5)
  336.  
  337.         out     0x20, al
  338.         ret
  339. ;destroy eax
  340. ready_for_next_irq_1:
  341.         mov     eax,5
  342.         mov     [check_idle_semaphore],eax
  343. ;        mov     al, 0x20
  344.         add     eax,(0x20-0x5)
  345.         out     0xa0,al
  346.         out     0x20, al
  347.         ret
  348.  
  349. irqD:
  350.         push  eax
  351.         xor   eax,eax
  352.         out   0xf0,al
  353.         mov   al,0x20
  354.         out   0xa0,al
  355.         out   0x20,al
  356.         pop   eax
  357.         iret
  358.  
  359.  
  360. align 4
  361.  
  362. irqhandler:
  363.  
  364.      mov    esi,edi          ; 1
  365.      shl    esi,6            ; 1
  366.      add    esi,irq00read    ; 1
  367.      shl    edi,12           ; 1
  368.      add    edi,IRQ_SAVE
  369.      mov    ecx,16
  370.  
  371.    irqnewread:
  372.      dec    ecx
  373.      js     irqover
  374.  
  375.      movzx  edx, word [esi]        ; 2+
  376.  
  377.      test   edx, edx               ; 1
  378.      jz     irqover
  379.  
  380.  
  381.      mov    ebx, [edi]             ; address of begin of buffer in edi      ; + 0x0 dword - data size
  382.      mov    eax, 4000                                                       ; + 0x4 dword - data begin offset
  383.      cmp    ebx, eax
  384.      je     irqfull
  385.      add    ebx, [edi + 0x4]       ; add data size to data begin offset
  386.      cmp    ebx, eax               ; if end of buffer, begin cycle again
  387.      jb     @f
  388.  
  389.      xor    ebx, ebx
  390.  
  391.   @@:
  392.      add    ebx, edi
  393.      movzx  eax, byte[esi + 3]     ; get type of data being received 1 - byte, 2 - word
  394.      dec    eax
  395.      jz     irqbyte
  396.      dec    eax
  397.      jnz    noirqword
  398.  
  399.      in     ax,dx
  400.      cmp    ebx, 3999              ; check for address odd in the end of buffer
  401.      jne    .odd
  402.      mov    [ebx + 0x10], ax
  403.      jmp    .add_size
  404.   .odd:
  405.      mov    [ebx + 0x10], al       ; I could make mistake here :)
  406.      mov    [edi + 0x10], ah
  407.   .add_size:
  408.      add    dword [edi], 2
  409.      jmp    nextport
  410.  
  411.  
  412.   irqbyte:
  413.      in     al,dx
  414.      mov    [ebx + 0x10],al
  415.      inc    dword [edi]
  416.   nextport:
  417.      add    esi,4
  418.      jmp    irqnewread
  419.  
  420.  
  421.    noirqword:
  422.    irqfull:
  423.    irqover:
  424.  
  425.      ret
  426.  
  427.  
  428.  
  429. set_application_table_status:
  430.         push eax
  431.  
  432.         mov  eax,[CURRENT_TASK]
  433.         shl  eax, 5
  434.         add  eax,CURRENT_TASK+TASKDATA.pid
  435.         mov  eax,[eax]
  436.  
  437.         mov  [application_table_status],eax
  438.  
  439.         pop  eax
  440.  
  441.         ret
  442.  
  443.  
  444. clear_application_table_status:
  445.         push eax
  446.  
  447.         mov  eax,[CURRENT_TASK]
  448.         shl  eax, 5
  449.         add  eax,CURRENT_TASK+TASKDATA.pid
  450.         mov  eax,[eax]
  451.  
  452.         cmp  eax,[application_table_status]
  453.         jne  apptsl1
  454.         xor  eax,eax
  455.         mov  [application_table_status],eax
  456.       apptsl1:
  457.  
  458.         pop  eax
  459.  
  460.         ret
  461.  
  462. ;  * eax = 64 - íîìåð ôóíêöèè
  463. ;  * ebx = 1 - åäèíñòâåííàÿ ïîäôóíêöèÿ
  464. ;  * ecx = íîâûé ðàçìåð ïàìÿòè
  465. ;Âîçâðàùàåìîå çíà÷åíèå:
  466. ;  * eax = 0 - óñïåøíî
  467. ;  * eax = 1 - íåäîñòàòî÷íî ïàìÿòè
  468.  
  469. sys_resize_app_memory:
  470.         ; ebx = 1 - resize
  471.         ; ecx = new amount of memory
  472.  
  473. ;        cmp    eax,1
  474.         dec     ebx
  475.         jnz    .no_application_mem_resize
  476.         stdcall new_mem_resize, ecx
  477.         mov [esp+32], eax
  478. .no_application_mem_resize:
  479.         ret
  480.  
  481. iglobal
  482. ;  process_terminating  db 'K : Process - terminating',13,10,0
  483. ;  process_terminated   db 'K : Process - done',13,10,0
  484.   msg_obj_destroy       db 'K : destroy app object',13,10,0
  485. endg
  486.  
  487. ; param
  488. ;  esi= slot
  489.  
  490. terminate: ; terminate application
  491.  
  492.            .slot equ esp   ;locals
  493.  
  494.            push   esi      ;save .slot
  495.  
  496.            shl esi, 8
  497.            cmp [SLOT_BASE+esi+APPDATA.dir_table], 0
  498.            jne @F
  499.            pop    esi
  500.            shl    esi, 5
  501.            mov    [CURRENT_TASK+esi+TASKDATA.state], 9
  502.            ret
  503. @@:
  504.            ;mov    esi,process_terminating
  505.            ;call   sys_msg_board_str
  506. @@:
  507.            cli
  508.            cmp   [application_table_status],0
  509.            je    term9
  510.            sti
  511.            call  change_task
  512.            jmp   @b
  513. term9:
  514.            call  set_application_table_status
  515.  
  516. ; if the process is in V86 mode...
  517.         mov     eax, [.slot]
  518.         shl     eax, 8
  519.         mov     esi, [eax+SLOT_BASE+APPDATA.pl0_stack]
  520.         add     esi, RING0_STACK_SIZE
  521.         cmp     [eax+SLOT_BASE+APPDATA.saved_esp0], esi
  522.         jz      .nov86
  523. ; ...it has page directory for V86 mode
  524.         mov     esi, [eax+SLOT_BASE+APPDATA.saved_esp0]
  525.         mov     ecx, [esi+4]
  526.         mov     [eax+SLOT_BASE+APPDATA.dir_table], ecx
  527. ; ...and I/O permission map for V86 mode
  528.         mov     ecx, [esi+12]
  529.         mov     [eax+SLOT_BASE+APPDATA.io_map], ecx
  530.         mov     ecx, [esi+8]
  531.         mov     [eax+SLOT_BASE+APPDATA.io_map+4], ecx
  532. .nov86:
  533.  
  534.            mov esi, [.slot]
  535.            shl esi,8
  536.            add esi, SLOT_BASE+APP_OBJ_OFFSET
  537. @@:
  538.            mov eax, [esi+APPOBJ.fd]
  539.            test eax, eax
  540.            jz @F
  541.  
  542.            cmp eax, esi
  543.            je @F
  544.  
  545.            push esi
  546.            call [eax+APPOBJ.destroy]
  547.            DEBUGF 1,"%s",msg_obj_destroy
  548.            pop esi
  549.            jmp @B
  550. @@:
  551.  
  552.            mov eax, [.slot]
  553.            shl eax, 8
  554.            stdcall destroy_app_space, [SLOT_BASE+eax+APPDATA.dir_table], [SLOT_BASE+eax+APPDATA.dlls_list_ptr]
  555.  
  556.            mov esi, [.slot]
  557.            cmp [fpu_owner],esi   ; if user fpu last -> fpu user = 1
  558.            jne @F
  559.  
  560.            mov [fpu_owner],1
  561.            mov eax, [256+SLOT_BASE+APPDATA.fpu_state]
  562.            clts
  563.            bt [cpu_caps], CAPS_SSE
  564.            jnc .no_SSE
  565.            fxrstor [eax]
  566.            jmp @F
  567. .no_SSE:
  568.            fnclex
  569.            frstor [eax]
  570. @@:
  571.  
  572.     mov   [KEY_COUNT],byte 0           ; empty keyboard buffer
  573.     mov   [BTN_COUNT],byte 0           ; empty button buffer
  574.  
  575.  
  576. ; remove defined hotkeys
  577.         mov     eax, hotkey_list
  578. .loop:
  579.         cmp     [eax+8], esi
  580.         jnz     .cont
  581.         mov     ecx, [eax]
  582.         jecxz   @f
  583.         push    dword [eax+12]
  584.         pop     dword [ecx+12]
  585. @@:
  586.         mov     ecx, [eax+12]
  587.         push    dword [eax]
  588.         pop     dword [ecx]
  589.         xor     ecx, ecx
  590.         mov     [eax], ecx
  591.         mov     [eax+4], ecx
  592.         mov     [eax+8], ecx
  593.         mov     [eax+12], ecx
  594. .cont:
  595.         add     eax, 16
  596.         cmp     eax, hotkey_list+256*16
  597.         jb      .loop
  598. ; remove hotkeys in buffer
  599.         mov     eax, hotkey_buffer
  600. .loop2:
  601.         cmp     [eax], esi
  602.         jnz     .cont2
  603.         and     dword [eax+4], 0
  604.         and     dword [eax], 0
  605. .cont2:
  606.         add     eax, 8
  607.         cmp     eax, hotkey_buffer+120*8
  608.         jb      .loop2
  609.  
  610.     mov   ecx,esi                 ; remove buttons
  611.   bnewba2:
  612.     mov   edi,[BTN_ADDR]
  613.     mov   eax,edi
  614.     cld
  615.     movzx ebx,word [edi]
  616.     inc   bx
  617.   bnewba:
  618.     dec   bx
  619.     jz    bnmba
  620.     add   eax,0x10
  621.     cmp   cx,[eax]
  622.     jnz   bnewba
  623.     pusha
  624.     mov   ecx,ebx
  625.     inc   ecx
  626.     shl   ecx,4
  627.     mov   ebx,eax
  628.     add   eax,0x10
  629.     call  memmove
  630.     dec   dword [edi]
  631.     popa
  632.     jmp   bnewba2
  633.   bnmba:
  634.  
  635.     pusha     ; save window coordinates for window restoring
  636.     cld
  637.     shl   esi,5
  638.     add   esi,window_data
  639.     mov   eax,[esi+WDATA.box.left]
  640.     mov   [draw_limits.left],eax
  641.     add   eax,[esi+WDATA.box.width]
  642.     mov   [draw_limits.right],eax
  643.     mov   eax,[esi+WDATA.box.top]
  644.     mov   [draw_limits.top],eax
  645.     add   eax,[esi+WDATA.box.height]
  646.     mov   [draw_limits.bottom],eax
  647.  
  648.     xor   eax, eax
  649.     mov   [esi+WDATA.box.left],eax
  650.     mov   [esi+WDATA.box.width],eax
  651.     mov   [esi+WDATA.box.top],eax
  652.     mov   [esi+WDATA.box.height],eax
  653.     mov   [esi+WDATA.cl_workarea],eax
  654.     mov   [esi+WDATA.cl_titlebar],eax
  655.     mov   [esi+WDATA.cl_frames],eax
  656.     mov   dword [esi+WDATA.reserved],eax ; clear all flags: wstate, redraw, wdrawn
  657.     lea   edi, [esi-window_data+draw_data]
  658.     mov   ecx,32/4
  659.     rep   stosd
  660.     popa
  661.  
  662. ; debuggee test
  663.     pushad
  664.     mov  edi, esi
  665.     shl  edi, 5
  666.     mov  eax, [SLOT_BASE+edi*8+APPDATA.debugger_slot]
  667.     test eax, eax
  668.     jz   .nodebug
  669.     push 8
  670.     pop  ecx
  671.     push dword [CURRENT_TASK+edi+TASKDATA.pid]   ; PID
  672.     push 2
  673.     call debugger_notify
  674.     pop  ecx
  675.     pop  ecx
  676. .nodebug:
  677.     popad
  678.  
  679.            mov ebx, [.slot]
  680.            shl ebx, 8
  681.            push ebx
  682.            mov ebx,[SLOT_BASE+ebx+APPDATA.pl0_stack]
  683.  
  684.            stdcall kernel_free, ebx
  685.  
  686.            pop ebx
  687.            mov ebx,[SLOT_BASE+ebx+APPDATA.cur_dir]
  688.            stdcall kernel_free, ebx
  689.  
  690.            mov edi, [.slot]
  691.            shl edi,8
  692.            add edi,SLOT_BASE
  693.  
  694.            mov eax, [edi+APPDATA.io_map]
  695.            cmp eax, [SLOT_BASE+256+APPDATA.io_map]
  696.            je @F
  697.            call free_page
  698. @@:
  699.            mov eax, [edi+APPDATA.io_map+4]
  700.            cmp eax, [SLOT_BASE+256+APPDATA.io_map+4]
  701.            je @F
  702.            call free_page
  703. @@:
  704.            mov eax, 0x20202020
  705.            stosd
  706.            stosd
  707.            stosd
  708.            mov ecx,244/4
  709.            xor eax, eax
  710.            rep stosd
  711.  
  712.   ; activate window
  713.         movzx  eax, word [WIN_STACK + esi*2]
  714.         cmp    eax, [TASK_COUNT]
  715.         jne    .dont_activate
  716.         pushad
  717.  .check_next_window:
  718.         dec    eax
  719.         cmp    eax, 1
  720.         jbe    .nothing_to_activate
  721.         lea    esi, [WIN_POS+eax*2]
  722.         movzx  edi, word [esi]               ; edi = process
  723.         shl    edi, 5
  724.         cmp    [CURRENT_TASK + edi + TASKDATA.state], byte 9  ; skip dead slots
  725.         je     .check_next_window
  726.         add    edi, window_data
  727. ; \begin{diamond}[19.09.2006]
  728. ; skip minimized windows
  729.         test   [edi + WDATA.fl_wstate], WSTATE_MINIMIZED
  730.         jnz    .check_next_window
  731. ; \end{diamond}
  732.         call   waredraw
  733.  .nothing_to_activate:
  734.         popad
  735.  .dont_activate:
  736.  
  737.         push    esi     ; remove hd1 & cd & flp reservation
  738.         shl     esi, 5
  739.         mov     esi, [esi+CURRENT_TASK+TASKDATA.pid]
  740.         cmp     [hd1_status], esi
  741.         jnz     @f
  742.         call    free_hd_channel
  743.         and     [hd1_status], 0
  744. @@:
  745.         cmp     [cd_status], esi
  746.         jnz     @f
  747.         call    free_cd_channel
  748.         and     [cd_status], 0
  749. @@:
  750.         cmp     [flp_status], esi
  751.         jnz     @f
  752.         and     [flp_status], 0
  753. @@:
  754.         pop     esi
  755.         cmp     [bgrlockpid], esi
  756.         jnz     @f
  757.         and     [bgrlockpid], 0
  758.         and     [bgrlock], 0
  759. @@:
  760.  
  761.     pusha ; remove all irq reservations
  762.     mov   eax,esi
  763.     shl   eax, 5
  764.     mov   eax,[eax+CURRENT_TASK+TASKDATA.pid]
  765.     mov   edi,irq_owner
  766.     xor   ebx, ebx
  767.     xor   edx, edx
  768.   newirqfree:
  769.     cmp   [edi + 4 * ebx], eax
  770.     jne   nofreeirq
  771.     mov   [edi + 4 * ebx], edx                          ; remove irq reservation
  772.     mov   [irq_tab + 4 * ebx], edx                      ; remove irq handler
  773.     mov   [irq_rights + 4 * ebx], edx                   ; set access rights to full access
  774.   nofreeirq:
  775.     inc   ebx
  776.     cmp   ebx, 16
  777.     jb    newirqfree
  778.     popa
  779.  
  780.     pusha                     ; remove all port reservations
  781.     mov   edx,esi
  782.     shl   edx, 5
  783.     add   edx,CURRENT_TASK
  784.     mov   edx,[edx+TASKDATA.pid]
  785.  
  786.   rmpr0:
  787.  
  788.     mov   esi,[RESERVED_PORTS]
  789.  
  790.     test  esi,esi
  791.     jz    rmpr9
  792.  
  793.   rmpr3:
  794.  
  795.     mov   edi,esi
  796.     shl   edi,4
  797.     add   edi,RESERVED_PORTS
  798.  
  799.     cmp   edx,[edi]
  800.     je    rmpr4
  801.  
  802.     dec   esi
  803.     jnz   rmpr3
  804.  
  805.     jmp   rmpr9
  806.  
  807.   rmpr4:
  808.  
  809.     mov   ecx,256
  810.     sub   ecx,esi
  811.     shl   ecx,4
  812.  
  813.     mov   esi,edi
  814.     add   esi,16
  815.     cld
  816.     rep   movsb
  817.  
  818.     dec   dword [RESERVED_PORTS]
  819.  
  820.     jmp   rmpr0
  821.  
  822.   rmpr9:
  823.  
  824.     popa
  825.     mov  edi,esi         ; do not run this process slot
  826.     shl  edi, 5
  827.     mov  [edi+CURRENT_TASK + TASKDATA.state],byte 9
  828. ; debugger test - terminate all debuggees
  829.     mov  eax, 2
  830.     mov  ecx, SLOT_BASE+2*0x100+APPDATA.debugger_slot
  831. .xd0:
  832.     cmp  eax, [TASK_COUNT]
  833.     ja   .xd1
  834.     cmp  dword [ecx], esi
  835.     jnz  @f
  836.     and  dword [ecx], 0
  837.     pushad
  838.     xchg eax, ecx
  839.     mov  ebx, 2
  840.     call sys_system
  841.     popad
  842. @@:
  843.     inc  eax
  844.     add  ecx, 0x100
  845.     jmp  .xd0
  846. .xd1:
  847. ;    call  systest
  848.     sti  ; .. and life goes on
  849.  
  850.     mov   eax, [draw_limits.left]
  851.     mov   ebx, [draw_limits.top]
  852.     mov   ecx, [draw_limits.right]
  853.     mov   edx, [draw_limits.bottom]
  854.     call  calculatescreen
  855.     xor   eax, eax
  856.     xor   esi, esi
  857.     call  redrawscreen
  858.  
  859.     mov   [MOUSE_BACKGROUND],byte 0  ; no mouse background
  860.     mov   [DONT_DRAW_MOUSE],byte 0  ; draw mouse
  861.  
  862.     and   [application_table_status],0
  863.     ;mov   esi,process_terminated
  864.     ;call  sys_msg_board_str
  865.     add esp, 4
  866.     ret
  867. restore .slot
  868.  
  869. iglobal
  870.   boot_sched_1    db   'Building gdt tss pointer',0
  871.   boot_sched_2    db   'Building IDT table',0
  872. endg
  873.  
  874.  
  875. build_scheduler:
  876.  
  877.         mov    esi,boot_sched_1
  878.         call   boot_log
  879.   ;      call   build_process_gdt_tss_pointer
  880.  
  881.   ;      mov    esi,boot_sched_2
  882.   ;      call   boot_log
  883.  
  884.         ret
  885.