Subversion Repositories Kolibri OS

Rev

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