Subversion Repositories Kolibri OS

Rev

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