Subversion Repositories Kolibri OS

Rev

Rev 33 | Rev 58 | 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,e1,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.      call  check_mouse_data_com2
  390.      restore_ring3_context
  391.      iret
  392.  
  393. p_irq4:
  394.      save_ring3_context
  395.      mov   ax, os_data
  396.      mov   ds, ax
  397.      mov   es, ax
  398.      call  check_mouse_data_com1
  399.      restore_ring3_context
  400.      iret
  401.  
  402. p_irq12:
  403.      save_ring3_context
  404.      mov   ax, os_data
  405.      mov   ds, ax
  406.      mov   es, ax
  407.      call  check_mouse_data_ps2
  408.      restore_ring3_context
  409.      iret
  410.  
  411. ready_for_next_irq:
  412.      mov    [check_idle_semaphore],5
  413.      mov   al, 0x20
  414.      out   0x20, al
  415.      ret
  416.  
  417. ready_for_next_irq_1:
  418.      mov    [check_idle_semaphore],5
  419.      mov   al, 0x20
  420.      out    0xa0,al
  421.      out   0x20, al
  422.      ret
  423.  
  424. irqD:
  425.      save_ring3_context
  426.      mov   ax, os_data
  427.      mov   ds, ax
  428.      mov   es, ax
  429.      
  430.      mov   dx,0xf0
  431.      mov   al,0
  432.      out   dx,al
  433.  
  434.      mov   dx,0xa0
  435.      mov   al,0x20
  436.      out   dx,al
  437.      mov   dx,0x20
  438.      out   dx,al
  439.  
  440.      restore_ring3_context
  441.      
  442.      iret
  443.  
  444.  
  445. irqhandler:
  446.  
  447.      push   edi
  448.  
  449.      mov    esi,edi          ; 1
  450.      shl    esi,6            ; 1
  451.      add    esi,irq00read    ; 1
  452.      shl    edi,12           ; 1
  453.      add    edi,0x2E0000
  454.  
  455.      mov    [check_idle_semaphore],5
  456.  
  457.    irqnewread:
  458.  
  459.      mov    dx,[esi]         ; 2+
  460.  
  461.      cmp    dx,0             ; 1
  462.      jz     irqover
  463.      cmp    [esi+3],byte 1   ; 2     ; byte read
  464.      jne    noirqbyte        ; 4-11
  465.  
  466.      in     al,dx
  467.  
  468.      mov    edx,[edi]
  469.      cmp    edx,4000
  470.      je     irqfull
  471.      mov    ebx,edi
  472.      add    ebx,0x10
  473.      add    ebx,edx
  474.      mov    [ebx],al
  475.      inc    edx
  476.      mov    [edi],edx
  477.  
  478.      add    esi,4
  479.      jmp    irqnewread
  480.  
  481.    noirqbyte:
  482.  
  483.  
  484.      cmp    [esi+3],byte 2     ; word read
  485.      jne    noirqword
  486.  
  487.      in     ax,dx
  488.  
  489.      mov    edx,[edi]
  490.      cmp    edx,4000
  491.      je     irqfull
  492.      mov    ebx,edi
  493.      add    ebx,0x10
  494.      add    ebx,edx
  495.      mov    [ebx],ax
  496.      add    edx,2
  497.      mov    [edi],edx
  498.      add    esi,4
  499.      jmp    irqnewread
  500.  
  501.    noirqword:
  502.    irqfull:
  503.    irqover:
  504.  
  505.      mov    al,0x20            ; ready for next irq
  506.      out    0x20,al
  507.  
  508.      pop    ebx
  509.      cmp    ebx,7
  510.      jbe    noa0
  511.      out    0xa0,al
  512.    noa0:
  513.  
  514.      ret
  515.  
  516.  
  517.  
  518. set_application_table_status:
  519.         push eax
  520.  
  521.         mov  eax,[0x3000]
  522.         shl  eax, 5
  523.         add  eax,0x3000+4
  524.         mov  eax,[eax]
  525.  
  526.         mov  [application_table_status],eax
  527.  
  528.         pop  eax
  529.  
  530.         ret
  531.  
  532.  
  533. clear_application_table_status:
  534.         push eax
  535.  
  536.         mov  eax,[0x3000]
  537.         shl  eax, 5
  538.         add  eax,0x3000+4
  539.         mov  eax,[eax]
  540.  
  541.         cmp  eax,[application_table_status]
  542.         jne  apptsl1
  543.         mov  [application_table_status],0
  544.       apptsl1:
  545.  
  546.         pop  eax
  547.  
  548.         ret
  549.  
  550.  
  551.  
  552. sys_resize_app_memory:
  553.         ; eax = 1 - resize
  554.         ;     ebx = new amount of memory
  555.  
  556.         cmp    eax,1
  557.         jne    .no_application_mem_resize
  558.        
  559.         jmp    new_mem_resize ;resize for new type of processes
  560.  
  561.  
  562.      .no_application_mem_resize:
  563.  
  564.         ret
  565.  
  566.  
  567.  
  568. get_app_params:
  569.  
  570.     push eax
  571.  
  572.     cmp  [0x90000+6],word '00'
  573.     jne  no_00_header
  574.  
  575.     mov  eax,[0x90000+12]
  576.     mov  [app_start],eax
  577.     mov  eax,[0x90000+16]
  578.     mov  [app_i_end],eax
  579.     mov  eax,[0x90000+20]
  580.     mov  [app_mem],eax
  581.     shr  eax,1
  582.     sub  eax,0x10
  583.     mov  [app_esp],eax
  584.     mov  eax,[0x90000+24]
  585.     mov  [app_i_param],eax
  586.     mov  [app_i_icon],dword 0
  587.  
  588.     pop  eax
  589.     mov  esi,1
  590.     ret
  591.  
  592.   no_00_header:
  593.  
  594.  
  595.     cmp  [0x90000+6],word '01'
  596.     jne  no_01_header
  597.  
  598.     mov  eax,[0x90000+12]
  599.     mov  [app_start],eax
  600.     mov  eax,[0x90000+16]
  601.     mov  [app_i_end],eax
  602.     mov  eax,[0x90000+20]
  603.     mov  [app_mem],eax
  604.     mov  eax,[0x90000+24]
  605.     mov  [app_esp],eax
  606.     mov  eax,[0x90000+28]
  607.     mov  [app_i_param],eax
  608.     mov  eax,[0x90000+32]
  609.     mov  [app_i_icon],eax
  610.  
  611.     pop  eax
  612.     mov  esi,1
  613.     ret
  614.  
  615.    no_01_header:
  616.  
  617.     pop  eax
  618.     mov  esi,0
  619.     ret
  620.  
  621.  
  622. start_application_fl:
  623.     jmp new_start_application_fl
  624.  
  625. ;************************************************************************
  626.  
  627. start_application_floppy:
  628.     jmp  new_start_application_floppy
  629.  
  630. ;********************************************************************
  631.  
  632. start_application_hd:
  633.     jmp   new_start_application_hd
  634.  
  635. uglobal
  636.   new_process_place  dd  0x0
  637.   app_start    dd  0x0
  638.   app_i_end    dd  0x0
  639.   app_mem      dd  0x0
  640.   app_esp      dd  0x0
  641.   app_i_param  dd  0x0
  642.   app_i_icon   dd  0x0
  643.   app_mem_pos  dd  0x0
  644.   appl_path        dd 0x0
  645.   appl_path_size   dd 0x0        
  646. endg
  647.  
  648. iglobal
  649.   hd_app_string      db  'HDAPP       '
  650.   process_loading    db 'K : Process - loading ',13,10,0
  651.   process_running    db 'K : Process - done',13,10,0
  652.   first_gdt_search   dd 0x2
  653. endg
  654.  
  655.  
  656. sys_threads:
  657.  
  658. ; eax=1 create thread
  659. ;
  660. ;   ebx=thread start
  661. ;   ecx=thread stack value
  662. ;
  663. ; on return : eax = pid
  664. jmp new_sys_threads
  665.  
  666. iglobal
  667.   process_terminating   db 'K : Process - terminating',13,10,0
  668.   process_terminated    db 'K : Process - done',13,10,0
  669. endg
  670.  
  671.  
  672. terminate: ; terminate application
  673.     push   esi
  674.     mov    esi,process_terminating
  675.     call   sys_msg_board_str
  676.     pop    esi
  677.  
  678. @@:
  679.     cli
  680.     cmp   [application_table_status],0
  681.     je    term9
  682.     sti
  683.     call  change_task
  684.     jmp   @b
  685.   term9:
  686.  
  687.     call  set_application_table_status
  688.    
  689.     mov    eax,esi
  690.     call   dispose_app_cr3_table
  691.  
  692.     cmp   [prev_user_of_fpu],esi   ; if user fpu last -> fpu user = 1
  693.     jne   fpu_ok_1
  694.     mov   [prev_user_of_fpu],1
  695.   fpu_ok_1:
  696.  
  697.     mov   [0xf400],byte 0           ; empty keyboard buffer
  698.     mov   [0xf500],byte 0           ; empty button buffer
  699.  
  700.  
  701.     mov   ecx,esi                 ; remove buttons
  702.   bnewba2:
  703.     mov   edi,[0xfe88]
  704.     mov   eax,edi
  705.     cld
  706.     movzx ebx,word [edi]
  707.     inc   bx
  708.   bnewba:
  709.     dec   bx
  710.     jz    bnmba
  711.     add   eax,0x10
  712.     cmp   cx,[eax]
  713.     jnz   bnewba
  714.     pusha
  715.     mov   ecx,ebx
  716.     inc   ecx
  717.     shl   ecx,4
  718.     mov   ebx,eax
  719.     add   eax,0x10
  720.     call  memmove
  721.     dec   dword [edi]
  722.     popa
  723.     jmp   bnewba2
  724.   bnmba:
  725.  
  726.     pusha     ; save window coordinates for window restoring
  727.     cld
  728.     shl   esi,5
  729.     add   esi,window_data
  730.     mov   ax,[esi+0]
  731.     mov   word [dlx],ax
  732.     mov   bx,[esi+8]
  733.     add   ax,bx
  734.     mov   word [dlxe],ax
  735.     mov   ax,[esi+4]
  736.     mov   word [dly],ax
  737.     mov   bx,[esi+12]
  738.     add   ax,bx
  739.     mov   word [dlye],ax
  740.     mov   [esi+0],word 0
  741.     mov   [esi+8],word 5
  742.     mov   ax,[0xFE04]
  743.     mov   [esi+4],ax
  744.     mov   [esi+12],word 5
  745.     xor   eax, eax
  746.     mov   [esi+16],eax;dword 0
  747.     mov   [esi+20],eax;dword 0
  748.     mov   [esi+24],eax;dword 0
  749.     mov   [esi+28],eax;dword 0
  750.     popa
  751.  
  752.     pusha
  753.     mov   edi,esi
  754.     shl   edi,5
  755.     add   edi,window_data
  756.     mov   ecx,32/4
  757.     xor   eax, eax
  758.   ;  cld
  759.     rep   stosd
  760.  
  761.     mov   eax,[0xFE04]      ; set window to start from maxy+1
  762.     add   eax,2
  763.  
  764.     mov   edi,esi
  765.     shl   edi,5
  766.     add   edi,window_data
  767.     mov   [edi+4],eax
  768.  
  769.     popa
  770.  
  771.     pusha
  772.     mov   edi,esi
  773.     shl   edi,5
  774.     add   edi,draw_data
  775.     mov   ecx,32/4
  776.     xor   eax, eax
  777.   ;  cld
  778.     rep   stosd
  779.     popa
  780.  
  781. ; debuggee test
  782.     pushad
  783.     mov  edi, esi
  784.     shl  edi, 5
  785.     mov  eax, [0x80000+edi*8+0xAC]
  786.     test eax, eax
  787.     jz   .nodebug
  788.     push 8
  789.     pop  ecx
  790.     push dword [0x3000+edi+0x4]   ; PID
  791.     push 2
  792.     call debugger_notify
  793.     pop  ecx
  794.     pop  ecx
  795. .nodebug:
  796.     popad
  797.  
  798.     pusha         ; at 0x80000+
  799.     mov   edi,esi
  800.     shl   edi,8
  801.     add   edi,0x80000
  802.     mov   ecx,256/4
  803.     xor   eax, eax
  804.   ;  cld
  805.     rep   stosd
  806.     popa
  807.  
  808.     pusha          ; name to spaces
  809.     mov   edi,esi
  810.     shl   edi,8
  811.     add   edi,0x80000
  812.     mov   ecx,11
  813.     mov   eax,' '
  814.   ;  cld
  815.     rep   stosb
  816.     popa
  817.  
  818.     pusha                ; C000 --> C400
  819.     mov   eax, 0xc000
  820.     mov   esi, 0
  821.   nlc40:
  822.     add   eax, 2
  823.     inc   esi
  824.     cmp   esi, [0x3004]
  825.     jae   nlc41
  826.     movzx ecx, word [eax]
  827.     mov   [0xC400 + ecx*2], si
  828.     jmp   nlc40
  829.   nlc41:
  830.     popa
  831.  
  832.     pusha ; remove hd1 reservation
  833.     mov   edx,esi
  834.     shl   edx, 5 ;imul  edx,0x20
  835.     add   edx,0x3000
  836.     mov   edx,[edx+4]
  837.     cmp   [hd1_status],edx
  838.     jne   no_hd1_s_remove
  839.     mov   [hd1_status],0
  840.   no_hd1_s_remove:
  841.     popa
  842.  
  843.     pusha ; remove all irq reservations
  844.     mov   edx,esi
  845.     shl   edx, 5 ;imul  edx,0x20
  846.     add   edx,0x3000
  847.     mov   edx,[edx+4]
  848.     mov   edi,irq_owner
  849.     mov   ecx,16
  850.   newirqfree:
  851.     cmp   [edi],edx
  852.     jne   nofreeirq
  853.     mov   [edi],dword 0
  854.   nofreeirq:
  855.     add    edi,4
  856.     loop   newirqfree
  857.     popa
  858.  
  859.  
  860.     pusha                     ; remove all port reservations
  861.     mov   [deleted_process],esi
  862.     mov   edx,esi
  863.     shl   edx, 5 ;imul  edx,0x20
  864.     add   edx,0x3000
  865.     mov   edx,[edx+4]
  866.  
  867.   rmpr0:
  868.  
  869.     mov   esi,[0x2d0000]
  870.  
  871.     cmp   esi,0
  872.     je    rmpr9
  873.  
  874.   rmpr3:
  875.  
  876.     mov   edi,esi
  877.     shl   edi,4
  878.     add   edi,0x2d0000
  879.  
  880.     cmp   edx,[edi]
  881.     je    rmpr4
  882.  
  883.     dec   esi
  884.     jnz   rmpr3
  885.  
  886.     jmp   rmpr9
  887.  
  888.   rmpr4:
  889.  
  890.     mov   ecx,256
  891.     sub   ecx,esi
  892.     shl   ecx,4
  893.  
  894.     mov   esi,edi
  895.     add   esi,16
  896.     cld
  897.     rep   movsb
  898.  
  899.     dec   dword [0x2d0000]
  900.  
  901.     jmp   rmpr0
  902.  
  903.   rmpr9:
  904.  
  905.     popa
  906.     mov  edi,esi         ; do not run this process slot
  907.     shl  edi, 5
  908.     mov  [edi+0x300A],byte 9
  909. ; debugger test - terminate all debuggees
  910.     mov  eax, 2
  911.     mov  ecx, 0x80000+2*0x100+0xAC
  912. .xd0:
  913.     cmp  eax, [0x3004]
  914.     ja   .xd1
  915.     cmp  dword [ecx], esi
  916.     jnz  @f
  917.     and  dword [ecx], 0
  918.     pushad
  919.     xchg eax, ebx
  920.     mov  eax, 2
  921.     call sys_system
  922.     popad
  923. @@:
  924.     inc  eax
  925.     add  ecx, 0x100
  926.     jmp  .xd0
  927. .xd1:
  928. ;    call  systest
  929.     sti  ; .. and life goes on
  930.  
  931. ;    movzx eax,word [dlx]
  932. ;    movzx ebx,word [dly]
  933. ;    movzx ecx,word [dlxe]
  934. ;    movzx edx,word [dlye]
  935.     call  calculatescreen
  936.     cli
  937.     mov  eax,[deleted_process]
  938.     cmp  eax,[active_process]
  939.     jne  no_activate_process
  940.     call read_active_process_stack
  941.   no_activate_process:
  942.     sti
  943.     xor   eax, eax
  944.     xor   esi, esi
  945.     call  redrawscreen
  946.  
  947.     mov   [0xfff4],byte 0  ; no mouse background
  948.     mov   [0xfff5],byte 0  ; draw mouse
  949.  
  950.     mov   [application_table_status],0
  951.     mov   esi,process_terminated
  952.     call  sys_msg_board_str
  953.     ret
  954.  
  955. save_active_process_stack:
  956.     cmp   [active_proc_stack_coun],0xa400+0x400-4
  957.     jne   @f
  958.     mov   [active_proc_stack_coun],0xa400-4
  959.   @@:
  960.     push  eax
  961.     push  ebx
  962.     mov   eax,[active_process]
  963.     shl   eax,5
  964.     add   eax,0x3000
  965.     mov   [eax-twdw+31],byte 1
  966.     add   [active_proc_stack_coun],4
  967.     mov   eax,[active_process]
  968.     mov   ebx,[active_proc_stack_coun]
  969.     mov   [ebx],eax
  970.     pop   ebx
  971.     pop   eax
  972.     ret
  973.  
  974. read_active_process_stack:
  975.     cmp   [active_proc_stack_coun],0xa400-4
  976.     jne   @f
  977.     mov   [active_proc_stack_coun],0xa400+0x400-4
  978.   @@:
  979.     push  eax
  980.     push  ebx
  981.     mov   ebx,[active_proc_stack_coun]
  982.     mov   eax,[ebx]
  983.     mov   [0xff01],eax     ; activate
  984.     sub   [active_proc_stack_coun],4
  985.     mov   [active_process_flag],1
  986.     pop   ebx
  987.     pop   eax
  988.     ret
  989.  
  990. iglobal
  991.   boot_sched_1    db   'Building gdt tss pointer',0
  992.   boot_sched_2    db   'Building IDT table',0
  993. endg
  994.  
  995.  
  996. build_scheduler:
  997.  
  998.         mov    esi,boot_sched_1
  999.         call   boot_log
  1000.         call   build_process_gdt_tss_pointer
  1001.  
  1002.         mov    esi,boot_sched_2
  1003.         call   boot_log
  1004.         call   build_interrupt_table
  1005.  
  1006.         ret
  1007.  
  1008.