Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                               ;;
  3. ;;  MenuetOS process management, protected ring3                 ;;
  4. ;;                                                               ;;
  5. ;;  Distributed under GPL. See file COPYING for details.         ;;
  6. ;;  Copyright 2003 Ville Turjanmaa                               ;;
  7. ;;                                                               ;;
  8. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  9.  
  10. align 32
  11.  
  12. ; GDT TABLE
  13.  
  14. gdts:
  15.  
  16.         dw     gdte-$-1
  17.         dd     gdts
  18.         dw     0
  19.  
  20. int_code_l:
  21. os_code_l:
  22.  
  23.         dw     0xffff
  24.         dw     0x0000
  25.         db     0x00
  26.         dw     11011111b *256 +10011010b
  27.         db     0x00
  28.  
  29. int_data_l:
  30. os_data_l:
  31.  
  32.         dw     0xffff
  33.         dw     0x0000
  34.         db     0x00
  35.         dw     11011111b *256 +10010010b
  36.         db     0x00
  37.  
  38. ; --------------- APM ---------------------
  39. apm_code_32:
  40.         dw     0x10        ; limit 64kb
  41.         db     0, 0, 0
  42.         dw     11011111b *256 +10011010b
  43.         db     0x00
  44. apm_code_16:
  45.         dw     0x10
  46.         db     0, 0, 0
  47.         dw     10011111b *256 +10011010b
  48.         db     0x00
  49. apm_data_16:
  50.         dw     0x10
  51.         db     0, 0, 0
  52.         dw     10011111b *256 +10010010b
  53.         db     0x00
  54. ; -----------------------------------------
  55.  
  56. app_code_l:
  57.       dw ((0x80000000-std_application_base_address) shr 12) and 0xffff
  58.       dw 0
  59.       db 0
  60.       dw 11010000b*256+11111010b+256*((0x80000000-std_application_base_address) shr 28)
  61.       db std_application_base_address shr 24
  62.  
  63. app_data_l:
  64.       dw (0x80000000-std_application_base_address) shr 12 and 0xffff
  65.       dw 0
  66.       db 0
  67.       dw 11010000b*256+11110010b+256*((0x80000000-std_application_base_address) shr 28)
  68.       db std_application_base_address shr 24
  69.  
  70. graph_data_l:
  71.  
  72.         dw     0x3ff
  73.         dw     0x0000
  74.         db     0x00
  75.         dw     11010000b *256 +11110010b
  76.         db     0x00
  77.  
  78. tss0_l:
  79.       times (max_processes+10) dd 0,0
  80.  
  81. gdte:
  82.  
  83.  
  84.  
  85. idtreg:
  86.      dw   8*0x41-1
  87.      dd   idts+8
  88. label idts at 0xB100-8
  89.  
  90.  
  91.  
  92. uglobal
  93.  tss_sceleton:
  94.   l.back   dw 0,0
  95.   l.esp0   dd 0
  96.   l.ss0    dw 0,0
  97.   l.esp1   dd 0
  98.   l.ss1    dw 0,0
  99.   l.esp2   dd 0
  100.   l.ss2    dw 0,0
  101.   l.cr3    dd 0
  102.   l.eip    dd 0
  103.   l.eflags dd 0
  104.   l.eax    dd 0
  105.   l.ecx    dd 0
  106.   l.edx    dd 0
  107.   l.ebx    dd 0
  108.   l.esp    dd 0
  109.   l.ebp    dd 0
  110.   l.esi    dd 0
  111.   l.edi    dd 0
  112.   l.es     dw 0,0
  113.   l.cs     dw 0,0
  114.   l.ss     dw 0,0
  115.   l.ds     dw 0,0
  116.   l.fs     dw 0,0
  117.   l.gs     dw 0,0
  118.   l.ldt    dw 0,0
  119.   l.trap   dw 0
  120.   l.io     dw 0
  121. endg
  122.  
  123.  
  124. build_process_gdt_tss_pointer:
  125.  
  126.         mov    ecx,tss_data
  127.         mov    edi,0
  128.       setgdtl2:
  129.         mov    [edi+gdts+ tss0 +0], word tss_step
  130.         mov    [edi+gdts+ tss0 +2], cx
  131.         mov    eax,ecx
  132.         shr    eax,16
  133.         mov    [edi+gdts+ tss0 +4], al
  134.         mov    [edi+gdts+ tss0 +7], ah
  135.         mov    [edi+gdts+ tss0 +5], word 01010000b *256 +11101001b
  136.         add    ecx,tss_step
  137.         add    edi,8
  138.         cmp    edi,8*(max_processes+5)
  139.         jbe    setgdtl2
  140.  
  141.         ret
  142.  
  143.  
  144. build_interrupt_table:
  145.  
  146.         mov    edi, idts+8
  147.         mov    esi, sys_int
  148.         mov    ecx, 0x40
  149.      @@:
  150.         mov    eax, [esi]
  151.         mov    [edi],   ax           ; lower part of offset
  152.         mov    [edi+2], word os_code ; segment selector
  153.         shr    eax, 16
  154.         mov    [edi+4], word 10001110b shl 8 ; interrupt descriptor
  155.         mov    [edi+6], ax
  156.         add    esi, 4
  157.         add    edi, 8
  158.         dec    ecx
  159.         jnz    @b
  160.        
  161.         ;mov    edi,8*0x40+idts+8
  162.         mov    [edi + 0], word (i40 and ((1 shl 16)-1))
  163.         mov    [edi + 2], word os_code
  164.         mov    [edi + 4], word 11101110b*256
  165.         mov    [edi + 6], word (i40 shr 16)
  166.  
  167.         ret
  168.  
  169.  
  170.  
  171. iglobal
  172.   sys_int:
  173.     dd   e0,debug_exc,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12,e13,e14,e15
  174.     dd   e16,e17
  175.     times 14 dd unknown_interrupt
  176.  
  177.     dd   irq0  ,irq1  ,p_irq2 ,p_irq3 ,p_irq4 ,p_irq5,p_irq6 ,p_irq7
  178.     dd   p_irq8,p_irq9,p_irq10,p_irq11,p_irq12,irqD  ,p_irq14,p_irq15
  179.  
  180.     times 16 dd unknown_interrupt
  181.  
  182.     dd   i40
  183. endg
  184.  
  185. macro save_ring3_context
  186. {
  187.     push    ds es
  188.     pushad
  189. }
  190. macro restore_ring3_context
  191. {
  192.     popad
  193.     pop    es ds
  194. }
  195.  
  196. ; simply return control to interrupted process
  197. unknown_interrupt:
  198.      iret
  199.  
  200. macro exc_wo_code [num]
  201. {
  202.   forward
  203.   e#num :
  204.       save_ring3_context
  205.       mov bl, num
  206.       jmp exc_c
  207. }
  208.  
  209. macro exc_w_code [num]
  210. {
  211.   forward
  212.   e#num :
  213.       add esp, 4
  214.       save_ring3_context
  215.       mov bl, num
  216.       jmp exc_c
  217. }
  218.  
  219. exc_wo_code 0, 1, 2, 3, 4, 5, 6, 9, 15, 16 ; 18, 19
  220. exc_w_code 8, 10, 11, 12, 13, 14, 17
  221.  
  222. exc_c:
  223.         mov   ax, os_data
  224.         mov   ds, ax
  225.         mov   es, ax
  226.  
  227. ; test if debugging
  228.         cli
  229.         mov   eax, [0x3000]
  230.         shl   eax, 8
  231.         mov   eax, [0x80000+eax+APPDATA.debugger_slot]
  232.         test  eax, eax
  233.         jnz   .debug
  234.         sti
  235. ; not debuggee => say error and terminate
  236.         add   esp, 28h
  237.         movzx eax, bl        
  238.         mov   [error_interrupt], eax
  239.         call  show_error_parameters
  240.        
  241.         mov   edx, [0x3010]
  242.         mov   [edx + TASKDATA.state], byte 4
  243.        
  244.         jmp   change_task
  245.  
  246. .debug:
  247. ; we are debugged process, notify debugger and suspend ourself
  248. ; eax=debugger PID
  249.         cld
  250.         movzx ecx, bl
  251.         push  ecx
  252.         mov   ecx, [0x3010]
  253.         push  dword [ecx+TASKDATA.pid]    ; PID of current process
  254.         push  12
  255.         pop   ecx
  256.         push  1        ; 1=exception
  257.         call  debugger_notify
  258.         pop   ecx
  259.         pop   ecx
  260.         pop   ecx
  261.         mov   edx, [0x3010]
  262.         mov   byte [edx+TASKDATA.state], 1        ; suspended
  263.         call  change_task
  264.         restore_ring3_context
  265.         iretd
  266.  
  267. ;;;;;;;;;;;;;;;;;;;;;;;
  268. ;; FPU ERROR HANDLER ;;
  269. ;;;;;;;;;;;;;;;;;;;;;;;
  270.  
  271. align 4
  272. e7:
  273.         save_ring3_context
  274.         clts
  275.         mov   ax, os_data
  276.         mov   ds, ax
  277.         mov   es, ax
  278.        
  279.         mov   eax, [prev_user_of_fpu]
  280.         shl   eax, 8
  281.         add   eax, 0x80000 + APPDATA.fpu_save_area
  282.         fsave [eax]
  283.        
  284.         mov   eax, [0x3000]
  285.         mov   [prev_user_of_fpu], eax
  286.         shl   eax, 8
  287.         add   eax, 0x80000
  288.         cmp   [eax + APPDATA.is_fpu_saved], 0
  289.         je    @f
  290.         frstor [eax+APPDATA.fpu_save_area]
  291.      @@:
  292.         mov   [eax + APPDATA.is_fpu_saved], 1
  293.         restore_ring3_context
  294.         iret
  295.        
  296. iglobal
  297.   prev_user_of_fpu dd 1
  298. endg
  299.  
  300.  
  301. writehex:
  302.       pusha
  303.      
  304.       mov  edi, [write_error_to]
  305.       mov  esi, 8
  306.     @@:
  307.       mov  ecx, eax
  308.       and  ecx, 0xf
  309.  
  310.       mov  cl,[ecx+hexletters]
  311.       mov  [edi],cl
  312.       dec  edi
  313.  
  314.       shr  eax,4
  315.       dec  esi
  316.       jnz  @b
  317.  
  318.       popa
  319.       ret
  320.  
  321. iglobal
  322.   hexletters  db '0123456789ABCDEF'
  323.  
  324.   error_interrupt         dd  -1
  325.  
  326.   process_error  db 'K : Process - forced terminate INT: 00000000',13,10,0
  327.   process_pid    db 'K : Process - forced terminate PID: 00000000',13,10,0
  328.   process_eip    db 'K : Process - forced terminate EIP: 00000000',13,10,0
  329.   system_error   db 'K : Kernel error',13,10,0
  330. endg
  331.  
  332. uglobal
  333.   write_error_to  dd  0x0
  334. endg
  335.  
  336. show_error_parameters:
  337.        
  338.         mov    [write_error_to],process_pid+43
  339.         mov    eax,[0x3000]
  340.         shl    eax, 5
  341.         mov    eax,[0x3000+TASKDATA.pid+eax]
  342.         call   writehex
  343.        
  344.         mov    [write_error_to],process_error+43
  345.         mov    eax,[error_interrupt]
  346.         call   writehex
  347.  
  348.         cmp    dword [esp+4+4], os_code ; CS
  349.         jnz    @f
  350.         mov    esi,system_error
  351.         call   sys_msg_board_str
  352.       @@:
  353.         mov    eax, [esp+4] ; EIP
  354.  
  355.         mov    [write_error_to],process_eip+43
  356.         call   writehex
  357.  
  358.         mov    esi,process_error
  359.         call   sys_msg_board_str
  360.  
  361.         mov    esi,process_pid
  362.         call   sys_msg_board_str
  363.  
  364.         mov    esi,process_eip
  365.         call   sys_msg_board_str
  366.  
  367.         ret
  368.  
  369.  
  370.  
  371. ; irq1  ->  hid/keyboard.inc
  372.  
  373.  
  374. macro irqh [num]
  375. {
  376.   forward
  377.   p_irq#num :
  378.      save_ring3_context
  379.      mov   edi, num
  380.      jmp   irq_c
  381. }
  382.  
  383. irqh 2,5,7,8,9,10,11,14,15
  384.  
  385.  irq_c:
  386.      mov   ax, os_data
  387.      mov   ds, ax
  388.      mov   es, ax
  389.      call  irqhandler
  390.      restore_ring3_context
  391.      iret
  392.  
  393. p_irq6:
  394.      save_ring3_context
  395.      mov   ax, os_data
  396.      mov   ds, ax
  397.      mov   es, ax
  398.      call  fdc_irq
  399.      call  ready_for_next_irq
  400.      restore_ring3_context
  401.      iret
  402.  
  403. p_irq3:
  404.      save_ring3_context
  405.      mov   ax, os_data
  406.      mov   ds, ax
  407.      mov   es, ax
  408.      cmp   [com2_mouse_detected],0
  409.      je    old_irq3_handler
  410.      call  check_mouse_data_com2
  411.      jmp   p_irq3_1
  412.  old_irq3_handler:
  413.      mov   edi,3
  414.      call  irqhandler
  415.   p_irq3_1:
  416.      restore_ring3_context
  417.      iret
  418.  
  419. p_irq4:
  420.      save_ring3_context
  421.      mov   ax, os_data
  422.      mov   ds, ax
  423.      mov   es, ax
  424.      cmp   [com1_mouse_detected],0
  425.      je    old_irq4_handler
  426.      call  check_mouse_data_com1
  427.      jmp   p_irq4_1
  428.  old_irq4_handler:
  429.      mov   edi,4
  430.      call  irqhandler
  431.   p_irq4_1:  
  432.      restore_ring3_context
  433.      iret
  434.  
  435. p_irq12:
  436.      save_ring3_context
  437.      mov   ax, os_data
  438.      mov   ds, ax
  439.      mov   es, ax
  440.      call  check_mouse_data_ps2
  441.      restore_ring3_context
  442.      iret
  443.  
  444. ready_for_next_irq:
  445.      mov    [check_idle_semaphore],5
  446.      mov   al, 0x20
  447.      out   0x20, al
  448.      ret
  449.  
  450. ready_for_next_irq_1:
  451.      mov    [check_idle_semaphore],5
  452.      mov   al, 0x20
  453.      out    0xa0,al
  454.      out   0x20, al
  455.      ret
  456.  
  457. irqD:
  458.      save_ring3_context
  459.      mov   ax, os_data
  460.      mov   ds, ax
  461.      mov   es, ax
  462.      
  463.      mov   dx,0xf0
  464.      mov   al,0
  465.      out   dx,al
  466.  
  467.      mov   dx,0xa0
  468.      mov   al,0x20
  469.      out   dx,al
  470.      mov   dx,0x20
  471.      out   dx,al
  472.  
  473.      restore_ring3_context
  474.      
  475.      iret
  476.  
  477.  
  478. irqhandler:
  479.  
  480.      push   edi
  481.  
  482.      mov    esi,edi          ; 1
  483.      shl    esi,6            ; 1
  484.      add    esi,irq00read    ; 1
  485.      shl    edi,12           ; 1
  486.      add    edi,0x2E0000
  487.      mov    ecx,16
  488.  
  489.      mov    [check_idle_semaphore],5
  490.  
  491.    irqnewread:
  492.      dec    ecx
  493.      js     irqover
  494.  
  495.      mov    dx,[esi]         ; 2+
  496.  
  497.      cmp    dx,0             ; 1
  498.      jz     irqover
  499.      cmp    [esi+3],byte 1   ; 2     ; byte read
  500.      jne    noirqbyte        ; 4-11
  501.  
  502.      in     al,dx
  503.  
  504.      mov    edx,[edi]
  505.      cmp    edx,4000
  506.      je     irqfull
  507.      mov    ebx,edi
  508.      add    ebx,0x10
  509.      add    ebx,edx
  510.      mov    [ebx],al
  511.      inc    edx
  512.      mov    [edi],edx
  513.  
  514.      add    esi,4
  515.      jmp    irqnewread
  516.  
  517.    noirqbyte:
  518.  
  519.  
  520.      cmp    [esi+3],byte 2     ; word read
  521.      jne    noirqword
  522.  
  523.      in     ax,dx
  524.  
  525.      mov    edx,[edi]
  526.      cmp    edx,4000
  527.      je     irqfull
  528.      mov    ebx,edi
  529.      add    ebx,0x10
  530.      add    ebx,edx
  531.      mov    [ebx],ax
  532.      add    edx,2
  533.      mov    [edi],edx
  534.      add    esi,4
  535.      jmp    irqnewread
  536.  
  537.    noirqword:
  538.    irqfull:
  539.    irqover:
  540.  
  541.      mov    al,0x20            ; ready for next irq
  542.      out    0x20,al
  543.  
  544.      pop    ebx
  545.      cmp    ebx,7
  546.      jbe    noa0
  547.      out    0xa0,al
  548.    noa0:
  549.  
  550.      ret
  551.  
  552.  
  553.  
  554. set_application_table_status:
  555.         push eax
  556.  
  557.         mov  eax,[0x3000]
  558.         shl  eax, 5
  559.         add  eax,0x3000+TASKDATA.pid
  560.         mov  eax,[eax]
  561.  
  562.         mov  [application_table_status],eax
  563.  
  564.         pop  eax
  565.  
  566.         ret
  567.  
  568.  
  569. clear_application_table_status:
  570.         push eax
  571.  
  572.         mov  eax,[0x3000]
  573.         shl  eax, 5
  574.         add  eax,0x3000+TASKDATA.pid
  575.         mov  eax,[eax]
  576.  
  577.         cmp  eax,[application_table_status]
  578.         jne  apptsl1
  579.         mov  [application_table_status],0
  580.       apptsl1:
  581.  
  582.         pop  eax
  583.  
  584.         ret
  585.  
  586.  
  587.  
  588. sys_resize_app_memory:
  589.         ; eax = 1 - resize
  590.         ;     ebx = new amount of memory
  591.  
  592.         cmp    eax,1
  593.         jne    .no_application_mem_resize
  594.        
  595.         jmp    new_mem_resize ;resize for new type of processes
  596.  
  597.  
  598.      .no_application_mem_resize:
  599.  
  600.         ret
  601.  
  602.  
  603.  
  604. get_app_params:
  605.  
  606.     push eax
  607.  
  608.     cmp  [0x90000+6],word '00'
  609.     jne  no_00_header
  610.  
  611.     mov  eax,[0x90000+12]
  612.     mov  [app_start],eax
  613.     mov  eax,[0x90000+16]
  614.     mov  [app_i_end],eax
  615.     mov  eax,[0x90000+20]
  616.     mov  [app_mem],eax
  617.     shr  eax,1
  618.     sub  eax,0x10
  619.     mov  [app_esp],eax
  620.     mov  eax,[0x90000+24]
  621.     mov  [app_i_param],eax
  622.     mov  [app_i_icon],dword 0
  623.  
  624.     pop  eax
  625.     mov  esi,1
  626.     ret
  627.  
  628.   no_00_header:
  629.  
  630.  
  631.     cmp  [0x90000+6],word '01'
  632.     jne  no_01_header
  633.  
  634.     mov  eax,[0x90000+12]
  635.     mov  [app_start],eax
  636.     mov  eax,[0x90000+16]
  637.     mov  [app_i_end],eax
  638.     mov  eax,[0x90000+20]
  639.     mov  [app_mem],eax
  640.     mov  eax,[0x90000+24]
  641.     mov  [app_esp],eax
  642.     mov  eax,[0x90000+28]
  643.     mov  [app_i_param],eax
  644.     mov  eax,[0x90000+32]
  645.     mov  [app_i_icon],eax
  646.  
  647.     pop  eax
  648.     mov  esi,1
  649.     ret
  650.  
  651.    no_01_header:
  652.  
  653.     pop  eax
  654.     mov  esi,0
  655.     ret
  656.  
  657.  
  658. start_application_fl:
  659.     jmp new_start_application_fl
  660.  
  661. ;************************************************************************
  662.  
  663. start_application_floppy:
  664.     jmp  new_start_application_floppy
  665.  
  666. ;********************************************************************
  667.  
  668. start_application_hd:
  669.     jmp   new_start_application_hd
  670.  
  671. uglobal
  672.   new_process_place  dd  0x0
  673.   app_start    dd  0x0
  674.   app_i_end    dd  0x0
  675.   app_mem      dd  0x0
  676.   app_esp      dd  0x0
  677.   app_i_param  dd  0x0
  678.   app_i_icon   dd  0x0
  679.   ;app_mem_pos  dd  0x0
  680.   appl_path        dd 0x0
  681.   appl_path_size   dd 0x0        
  682. endg
  683.  
  684. ;iglobal
  685.   ;hd_app_string      db  'HDAPP       '
  686.   ;process_loading    db 'K : Process - loading ',13,10,0
  687.   ;process_running    db 'K : Process - done',13,10,0
  688.   ;first_gdt_search   dd 0x2
  689. ;endg
  690.  
  691.  
  692. sys_threads:
  693.  
  694. ; eax=1 create thread
  695. ;
  696. ;   ebx=thread start
  697. ;   ecx=thread stack value
  698. ;
  699. ; on return : eax = pid
  700. jmp new_sys_threads
  701.  
  702. iglobal
  703.   process_terminating   db 'K : Process - terminating',13,10,0
  704.   process_terminated    db 'K : Process - done',13,10,0
  705. endg
  706.  
  707.  
  708. terminate: ; terminate application
  709.     push   esi
  710.     mov    esi,process_terminating
  711.     call   sys_msg_board_str
  712.     pop    esi
  713.  
  714. @@:
  715.     cli
  716.     cmp   [application_table_status],0
  717.     je    term9
  718.     sti
  719.     call  change_task
  720.     jmp   @b
  721.   term9:
  722.  
  723.     call  set_application_table_status
  724.    
  725.     mov    eax,esi
  726.     call   dispose_app_cr3_table
  727.  
  728.     cmp   [prev_user_of_fpu],esi   ; if user fpu last -> fpu user = 1
  729.     jne   fpu_ok_1
  730.     mov   [prev_user_of_fpu],1
  731.   fpu_ok_1:
  732.  
  733.     mov   [0xf400],byte 0           ; empty keyboard buffer
  734.     mov   [0xf500],byte 0           ; empty button buffer
  735.  
  736.  
  737. ; remove defined hotkeys
  738.         mov     eax, hotkey_list
  739. .loop:
  740.         cmp     [eax+8], esi
  741.         jnz     .cont
  742.         mov     ecx, [eax]
  743.         jecxz   @f
  744.         push    dword [eax+12]
  745.         pop     dword [ecx+12]
  746. @@:
  747.         mov     ecx, [eax+12]
  748.         push    dword [eax]
  749.         pop     dword [ecx]
  750.         xor     ecx, ecx
  751.         mov     [eax], ecx
  752.         mov     [eax+4], ecx
  753.         mov     [eax+8], ecx
  754.         mov     [eax+12], ecx
  755. .cont:
  756.         add     eax, 16
  757.         cmp     eax, hotkey_list+256*16
  758.         jb      .loop
  759. ; remove hotkeys in buffer
  760.         mov     eax, hotkey_buffer
  761. .loop2:
  762.         cmp     [eax], esi
  763.         jnz     .cont2
  764.         and     dword [eax+4], 0
  765.         and     dword [eax], 0
  766. .cont2:
  767.         add     eax, 8
  768.         cmp     eax, hotkey_buffer+120*8
  769.         jb      .loop2
  770.  
  771.     mov   ecx,esi                 ; remove buttons
  772.   bnewba2:
  773.     mov   edi,[0xfe88]
  774.     mov   eax,edi
  775.     cld
  776.     movzx ebx,word [edi]
  777.     inc   bx
  778.   bnewba:
  779.     dec   bx
  780.     jz    bnmba
  781.     add   eax,0x10
  782.     cmp   cx,[eax]
  783.     jnz   bnewba
  784.     pusha
  785.     mov   ecx,ebx
  786.     inc   ecx
  787.     shl   ecx,4
  788.     mov   ebx,eax
  789.     add   eax,0x10
  790.     call  memmove
  791.     dec   dword [edi]
  792.     popa
  793.     jmp   bnewba2
  794.   bnmba:
  795.  
  796.     pusha     ; save window coordinates for window restoring
  797.     cld
  798.     shl   esi,5
  799.     add   esi,window_data
  800.     mov   eax,[esi+WDATA.box.left]
  801.     mov   [dlx],eax
  802.     add   eax,[esi+WDATA.box.width]
  803.     mov   [dlxe],eax
  804.     mov   eax,[esi+WDATA.box.top]
  805.     mov   [dly],eax
  806.     add   eax,[esi+WDATA.box.height]
  807.     mov   [dlye],eax
  808.  
  809.     mov   [esi+WDATA.box.left], 0
  810.     mov   [esi+WDATA.box.width], 5
  811.     mov   eax,[0xFE04]
  812.     mov   [esi+WDATA.box.top],eax
  813.     mov   [esi+WDATA.box.height], 5
  814.     xor   eax, eax
  815.     mov   [esi+WDATA.cl_workarea],eax
  816.     mov   [esi+WDATA.cl_titlebar],eax
  817.     mov   [esi+WDATA.cl_frames],eax
  818.     mov   dword [esi+WDATA.reserved],eax ; clear all flags: wstate, redraw, wdrawn
  819.     lea   edi, [esi-window_data+draw_data]
  820.     mov   ecx,32/4
  821.     rep   stosd
  822.     popa
  823.  
  824. ; debuggee test
  825.     pushad
  826.     mov  edi, esi
  827.     shl  edi, 5
  828.     mov  eax, [0x80000+edi*8+APPDATA.debugger_slot]
  829.     test eax, eax
  830.     jz   .nodebug
  831.     push 8
  832.     pop  ecx
  833.     push dword [0x3000+edi+TASKDATA.pid]   ; PID
  834.     push 2
  835.     call debugger_notify
  836.     pop  ecx
  837.     pop  ecx
  838. .nodebug:
  839.     popad
  840.  
  841.     pusha         ; at 0x80000+
  842.     mov   edi,esi
  843.     shl   edi,8
  844.     add   edi,0x80000
  845.     mov   ecx,256/4
  846.     xor   eax, eax
  847.     rep   stosd
  848.     popa
  849.  
  850.     pusha          ; name to spaces
  851.     mov   edi,esi
  852.     shl   edi,8
  853.     add   edi,0x80000+APPDATA.app_name
  854.     mov   ecx,11
  855.     mov   eax,' '
  856.     rep   stosb
  857.     popa
  858.  
  859.  
  860.   ; activate window
  861.         movzx  eax, word [0xC000 + esi*2]
  862.         cmp    eax, [0x3004]
  863.         jne    .dont_activate
  864.         pushad
  865.  .check_next_window:
  866.         dec    eax
  867.         cmp    eax, 1
  868.         jbe    .nothing_to_activate
  869.         lea    esi, [0xc400+eax*2]
  870.         movzx  edi, word [esi]               ; edi = process
  871.         shl    edi, 5
  872.         cmp    [0x3000 + edi + TASKDATA.state], byte 9  ; skip dead slots
  873.         je     .check_next_window
  874.         add    edi, window_data
  875.         call   waredraw
  876.  .nothing_to_activate:
  877.         popad
  878.  .dont_activate:
  879.  
  880.         push    esi     ; remove hd1 & cd & flp reservation
  881.         shl     esi, 5
  882.         mov     esi, [esi+0x3004]
  883.         cmp     [hd1_status], esi
  884.         jnz     @f
  885.         mov     [hd1_status], 0
  886. @@:
  887.         cmp     [cd_status], esi
  888.         jnz     @f
  889.         mov     [cd_status], 0
  890. @@:
  891.         cmp     [flp_status], esi
  892.         jnz     @f
  893.         mov     [flp_status], 0
  894. @@:
  895.         pop     esi
  896.  
  897.     pusha ; remove all irq reservations
  898.     mov   eax,esi
  899.     shl   eax, 5
  900.     mov   eax,[edx+0x3000+TASKDATA.pid]
  901.     mov   edi,irq_owner
  902.     mov   ecx,16
  903.   newirqfree:
  904.     scasd
  905.     jne   nofreeirq
  906.     mov   [edi-4],dword 0
  907.   nofreeirq:
  908.     loop   newirqfree
  909.     popa
  910.  
  911.  
  912.     pusha                     ; remove all port reservations
  913.     mov   edx,esi
  914.     shl   edx, 5
  915.     add   edx,0x3000
  916.     mov   edx,[edx+TASKDATA.pid]
  917.  
  918.   rmpr0:
  919.  
  920.     mov   esi,[0x2d0000]
  921.  
  922.     cmp   esi,0
  923.     je    rmpr9
  924.  
  925.   rmpr3:
  926.  
  927.     mov   edi,esi
  928.     shl   edi,4
  929.     add   edi,0x2d0000
  930.  
  931.     cmp   edx,[edi]
  932.     je    rmpr4
  933.  
  934.     dec   esi
  935.     jnz   rmpr3
  936.  
  937.     jmp   rmpr9
  938.  
  939.   rmpr4:
  940.  
  941.     mov   ecx,256
  942.     sub   ecx,esi
  943.     shl   ecx,4
  944.  
  945.     mov   esi,edi
  946.     add   esi,16
  947.     cld
  948.     rep   movsb
  949.  
  950.     dec   dword [0x2d0000]
  951.  
  952.     jmp   rmpr0
  953.  
  954.   rmpr9:
  955.  
  956.     popa
  957.     mov  edi,esi         ; do not run this process slot
  958.     shl  edi, 5
  959.     mov  [edi+0x3000 + TASKDATA.state],byte 9
  960. ; debugger test - terminate all debuggees
  961.     mov  eax, 2
  962.     mov  ecx, 0x80000+2*0x100+APPDATA.debugger_slot
  963. .xd0:
  964.     cmp  eax, [0x3004]
  965.     ja   .xd1
  966.     cmp  dword [ecx], esi
  967.     jnz  @f
  968.     and  dword [ecx], 0
  969.     pushad
  970.     xchg eax, ebx
  971.     mov  eax, 2
  972.     call sys_system
  973.     popad
  974. @@:
  975.     inc  eax
  976.     add  ecx, 0x100
  977.     jmp  .xd0
  978. .xd1:
  979. ;    call  systest
  980.     sti  ; .. and life goes on
  981.  
  982.     mov   eax, [dlx]
  983.     mov   ebx, [dly]
  984.     mov   ecx, [dlxe]
  985.     mov   edx, [dlye]
  986.     call  calculatescreen
  987.     xor   eax, eax
  988.     xor   esi, esi
  989.     call  redrawscreen
  990.  
  991.     mov   [0xfff4],byte 0  ; no mouse background
  992.     mov   [0xfff5],byte 0  ; draw mouse
  993.  
  994.     mov   [application_table_status],0
  995.     mov   esi,process_terminated
  996.     call  sys_msg_board_str
  997.  
  998.     ret
  999.  
  1000. iglobal
  1001.   boot_sched_1    db   'Building gdt tss pointer',0
  1002.   boot_sched_2    db   'Building IDT table',0
  1003. endg
  1004.  
  1005.  
  1006. build_scheduler:
  1007.  
  1008.         mov    esi,boot_sched_1
  1009.         call   boot_log
  1010.         call   build_process_gdt_tss_pointer
  1011.  
  1012.         mov    esi,boot_sched_2
  1013.         call   boot_log
  1014.         call   build_interrupt_table
  1015.  
  1016.         ret
  1017.  
  1018.