Subversion Repositories Kolibri OS

Rev

Rev 5 | Rev 8 | 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. graph_data_l:
  39.  
  40.         dw     0x3ff
  41.         dw     0x0000
  42.         db     0x00
  43.         dw     11010000b *256 +11110010b
  44.         db     0x00
  45.  
  46. tss0_l:
  47.       times (max_processes+10) dd 0,0
  48.  
  49. tss0i_l:
  50.       times 0x41 dq 0 ;(256+10) dd 0,0
  51.  
  52. app_code_l:
  53.       dw ((0x80000000-std_application_base_address) shr 12) and 0xffff
  54.       dw 0
  55.       db 0
  56.       dw 11010000b*256+11111010b+256*((0x80000000-std_application_base_address) shr 28)
  57.       db std_application_base_address shr 24
  58.  
  59. app_data_l:
  60.       dw (0x80000000-std_application_base_address) shr 12 and 0xffff
  61.       dw 0
  62.       db 0
  63.       dw 11010000b*256+11110010b+256*((0x80000000-std_application_base_address) shr 28)
  64.       db std_application_base_address shr 24
  65.  
  66.  
  67. gdte:
  68.  
  69.  
  70.  
  71. idtreg:
  72.      dw   8*0x41-1
  73.      dd   idts+8
  74.  
  75. label idts at 0xB100-8
  76. ;idte = idts + 8 + 0x60
  77.  
  78.  
  79.  
  80. build_process_gdt_tss_pointer:
  81.  
  82.         mov    ecx,tss_data
  83.         mov    edi,0
  84.       setgdtl2:
  85.         mov    [edi+gdts+ tss0 +0], word tss_step
  86.         mov    [edi+gdts+ tss0 +2], cx
  87.         mov    eax,ecx
  88.         shr    eax,16
  89.         mov    [edi+gdts+ tss0 +4], al
  90.         mov    [edi+gdts+ tss0 +7], ah
  91.         mov    [edi+gdts+ tss0 +5], word 01010000b *256 +11101001b
  92.         add    ecx,tss_step
  93.         add    edi,8
  94.         cmp    edi,8*(max_processes+5)
  95.         jbe    setgdtl2
  96.  
  97.         ret
  98.  
  99.  
  100. build_interrupt_table:
  101.  
  102.         mov  [l.eflags],dword 0x11002
  103.         mov  [l.ss0], int_data
  104.         ;mov  [l.ss1], ring1_data
  105.         ;mov  [l.ss2], ring2_data
  106.         mov  [l.esp0], 0x52000
  107.         mov  [l.esp1], 0x53000
  108.         mov  [l.esp2], 0x54000
  109.  
  110.         mov  eax,cr3
  111.         mov  [l.cr3],eax
  112.         mov  [l.cs],int_code
  113.         mov  [l.ss],int_data
  114.         mov  [l.ds],int_data
  115.         mov  [l.es],int_data
  116.         mov  [l.fs],int_data
  117.         mov  [l.gs],int_data
  118.  
  119.         mov  eax,sys_int
  120.         mov  [l.esp],0x720000
  121.         mov  edi,0x290000
  122.  
  123.      newint:
  124.         push edi
  125.         mov  ebx,[eax]
  126.         mov  [l.eip],ebx
  127.         mov  esi,tss_sceleton
  128.         mov  ecx,120/4
  129.         cld
  130.         rep  movsd
  131.         pop  edi
  132.  
  133.         add  edi,128
  134.         add  [l.esp],1024
  135.         add  eax,4
  136.  
  137.         cmp  eax,sys_int+4*0x40 ;0x60
  138.         jbe  newint  ;jb
  139.  
  140. ;;
  141.  
  142.         mov    esi,boot_sched_3_2
  143.         call   boot_log
  144.  
  145.         mov    ecx,0x290000
  146.         mov    edi,0
  147.       setgdtl2i:
  148.         mov    [edi+gdts+ tss0i +0], word 128
  149.         mov    [edi+gdts+ tss0i +2], cx
  150.         mov    eax,ecx
  151.         shr    eax,16
  152.         mov    [edi+gdts+ tss0i +4], al
  153.         mov    [edi+gdts+ tss0i +7], ah
  154.         mov    [edi+gdts+ tss0i +5], word 01010000b *256 +11101001b
  155.         add    ecx,128
  156.         add    edi,8
  157.         cmp    edi,8*0x40 ;0x60
  158.         jbe    setgdtl2i
  159.  
  160. ;;
  161.  
  162.         mov    esi,boot_sched_3_3
  163.         call   boot_log
  164.  
  165.         mov    edi,0
  166.         mov    edx,tss0i
  167.      @@:
  168.         mov    [edi+idts+ 8 +0], word 0
  169.         mov    [edi+idts+ 8 +2], dx
  170.         mov    [edi+idts+ 8 +4], word 10000101b*256 ; task gate DPL=0
  171.         mov    [edi+idts+ 8 +6], word 0
  172.         add    edx,8
  173.         add    edi,8
  174.  
  175.         cmp    edi,8*0x40
  176.         jb     @b
  177.        
  178.     ; Exceptions
  179.         mov    edi, idts+8
  180.         mov    esi, sys_int
  181.         mov    ecx, 32
  182.      @@:
  183.         mov    eax, [esi]
  184.         mov    [edi],   ax           ; lower part of offset
  185.         mov    [edi+2], word os_code ; segment selector
  186.         shr    eax, 16
  187.         mov    [edi+4], word 10001110b shl 8 ; interrupt descriptor
  188.         mov    [edi+6], ax
  189.         add    esi, 4
  190.         add    edi, 8
  191.         dec    ecx
  192.         jnz    @b
  193.  
  194.         ret
  195.  
  196. build_syscall_interrupt_table:
  197.  
  198.         ;mov    dx,tss0sys
  199.         mov    edi,8*0x40+idts+8
  200.         mov    [edi + 0], word (i40 and ((1 shl 16)-1))
  201.         mov    [edi + 2], word os_code
  202.         mov    [edi + 4], word 11101110b*256
  203.         mov    [edi + 6], word (i40 shr 16)
  204.  
  205.         ret
  206.  
  207.  
  208.  
  209. iglobal
  210.   sys_int:
  211.     dd   s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,sa,sb,sc,sd,se,sf
  212.  
  213.     dd   s10        ,s11      ;  ,i_unknown12,i_unknown13
  214. ;    dd   i_unknown14,i_unknown15,i_unknown16,i_unknown17
  215. ;    dd   i_unknown18,i_unknown19,i_unknown1a,i_unknown1b
  216. ;    dd   i_unknown1c,i_unknown1d,i_unknown1e,i_unknown1f
  217.     times 14 dd unknown_interrupt
  218.  
  219.     dd   irq0  ,irq1  ,p_irq2 ,p_irq3 ,p_irq4 ,p_irq5,p_irq6 ,p_irq7
  220.     dd   p_irq8,p_irq9,p_irq10,p_irq11,p_irq12,irqD  ,p_irq14,p_irq15
  221.  
  222.     ;dd   i_unknown30,i_unknown31,i_unknown32,i_unknown33
  223.     ;dd   i_unknown34,i_unknown35,i_unknown36,i_unknown37
  224.     ;dd   i_unknown38,i_unknown39,i_unknown3a,i_unknown3b
  225.     ;dd   i_unknown3c,i_unknown3d,i_unknown3e,i_unknown3f
  226.     times 16 dd unknown_interrupt
  227.  
  228.     dd   i40
  229. endg
  230.  
  231. uglobal
  232.  tss_sceleton:
  233.   l.back   dw 0,0
  234.   l.esp0   dd 0
  235.   l.ss0    dw 0,0
  236.   l.esp1   dd 0
  237.   l.ss1    dw 0,0
  238.   l.esp2   dd 0
  239.   l.ss2    dw 0,0
  240.   l.cr3    dd 0
  241.   l.eip    dd 0
  242.   l.eflags dd 0
  243.   l.eax    dd 0
  244.   l.ecx    dd 0
  245.   l.edx    dd 0
  246.   l.ebx    dd 0
  247.   l.esp    dd 0
  248.   l.ebp    dd 0
  249.   l.esi    dd 0
  250.   l.edi    dd 0
  251.   l.es     dw 0,0
  252.   l.cs     dw 0,0
  253.   l.ss     dw 0,0
  254.   l.ds     dw 0,0
  255.   l.fs     dw 0,0
  256.   l.gs     dw 0,0
  257.   l.ldt    dw 0,0
  258.   l.trap   dw 0
  259.   l.io     dw 0
  260. endg
  261.  
  262. s0:
  263.         mov   ax, os_data
  264.         mov   ds, ax
  265.         mov   es, ax
  266.  
  267.         mov   [error_interrupt],0x0
  268.         mov   [error_interrupt_entry],dword s0
  269.  
  270.         call  show_error_parameters
  271.  
  272.         mov   edx,[0x3010]
  273.         mov   [edx+0xa],byte 4
  274.  
  275.         jmp   change_task
  276.  
  277.  
  278. s1:
  279.         mov   ax, os_data
  280.         mov   ds, ax
  281.         mov   es, ax
  282.  
  283.         mov   [error_interrupt],0x1
  284.         mov   [error_interrupt_entry],dword s1
  285.  
  286.         call  show_error_parameters
  287.  
  288.         mov   edx,[0x3010]
  289.         mov   [edx+0xa],byte 4
  290.  
  291.         jmp   change_task
  292.  
  293. s2:
  294.         mov   ax, os_data
  295.         mov   ds, ax
  296.         mov   es, ax
  297.  
  298.         mov   [error_interrupt],0x2
  299.         mov   [error_interrupt_entry],dword s2
  300.  
  301.         call  show_error_parameters
  302.  
  303.         mov   edx,[0x3010]
  304.         mov   [edx+0xa],byte 4
  305.  
  306.         jmp   change_task
  307.  
  308. s3:
  309.         mov   ax, os_data
  310.         mov   ds, ax
  311.         mov   es, ax
  312.  
  313.         mov   [error_interrupt],0x3
  314.         mov   [error_interrupt_entry],dword s3
  315.  
  316.         call  show_error_parameters
  317.  
  318.         mov   edx,[0x3010]
  319.         mov   [edx+0xa],byte 4
  320.  
  321.         jmp   change_task
  322.  
  323. s4:
  324.         mov   ax, os_data
  325.         mov   ds, ax
  326.         mov   es, ax
  327.  
  328.         mov   [error_interrupt],0x4
  329.         mov   [error_interrupt_entry],dword s4
  330.  
  331.         call  show_error_parameters
  332.  
  333.         mov   edx,[0x3010]
  334.         mov   [edx+0xa],byte 4
  335.  
  336.         jmp   change_task
  337.  
  338. s5:
  339.         mov   ax, os_data
  340.         mov   ds, ax
  341.         mov   es, ax
  342.  
  343.         mov   [error_interrupt],0x5
  344.         mov   [error_interrupt_entry],dword s5
  345.  
  346.         call  show_error_parameters
  347.  
  348.         mov   edx,[0x3010]
  349.         mov   [edx+0xa],byte 4
  350.  
  351.         jmp   change_task
  352.  
  353. s6:
  354.         mov   ax, os_data
  355.         mov   ds, ax
  356.         mov   es, ax
  357.  
  358.         mov   [error_interrupt],0x6
  359.         mov   [error_interrupt_entry],dword s6
  360.  
  361.         call  show_error_parameters
  362.  
  363.         mov   edx,[0x3010]
  364.         mov   [edx+0xa],byte 4
  365.  
  366.         jmp   change_task
  367.  
  368.  
  369. ;;;;;;;;;;;;;;;;;;;;;;;
  370. ;; FPU ERROR HANDLER ;;
  371. ;;;;;;;;;;;;;;;;;;;;;;;
  372.  
  373. align 4
  374. s7:
  375.         clts
  376.          
  377.         push  eax
  378.         push  ds es
  379.        
  380.         mov   ax, os_data
  381.         mov   ds, ax
  382.         mov   es, ax
  383.        
  384.         mov   eax, [prev_user_of_fpu]
  385.         shl   eax, 8
  386.         add   eax, 0x80000 + 0x10
  387.         fsave [eax]
  388.        
  389.         mov   eax, [0x3000]
  390.         mov   [prev_user_of_fpu], eax
  391.         shl   eax, 8
  392.         add   eax, 0x80000
  393.         cmp   [eax + 0x7f], byte 0
  394.         je    @f
  395.         frstor [eax+0x10]
  396.      @@:
  397.         mov   [eax + 0x7f], byte 1
  398.        
  399.         pop   es ds
  400.         pop   eax
  401.        
  402.         iret
  403.        
  404. iglobal
  405.   prev_user_of_fpu dd 1
  406. endg
  407.  
  408.  
  409. s8:
  410.         add   esp, 4 ; zero on the stack!
  411.         mov   ax, os_data
  412.         mov   ds, ax
  413.         mov   es, ax
  414.  
  415.         mov   [error_interrupt],0x8
  416.         mov   [error_interrupt_entry],dword s8
  417.  
  418.         call  show_error_parameters
  419.  
  420.         mov   edx,[0x3010]
  421.         mov   [edx+0xa],byte 4
  422.  
  423.         jmp   change_task
  424.  
  425. s9:
  426.         mov   ax, os_data
  427.         mov   ds, ax
  428.         mov   es, ax
  429.  
  430.         mov   [error_interrupt],0x9
  431.         mov   [error_interrupt_entry],dword s9
  432.  
  433.         call  show_error_parameters
  434.  
  435.         mov   edx,[0x3010]
  436.         mov   [edx+0xa],byte 4
  437.  
  438.         jmp   change_task
  439.  
  440. ; Invalid TSS
  441. sa:
  442.         add   esp, 4 ; error code
  443.         mov   ax, os_data
  444.         mov   ds, ax
  445.         mov   es, ax
  446.  
  447.         mov   [error_interrupt],0xa
  448.         mov   [error_interrupt_entry],dword sa
  449.  
  450.         call  show_error_parameters
  451.  
  452.         mov   edx,[0x3010]
  453.         mov   [edx+0xa],byte 4
  454.  
  455.         jmp   change_task
  456.  
  457. ; Segment not present
  458. sb:
  459.         add   esp, 4
  460.         mov   ax, os_data
  461.         mov   ds, ax
  462.         mov   es, ax
  463.  
  464.         mov   [error_interrupt],0xb
  465.         mov   [error_interrupt_entry],dword sb
  466.  
  467.         call  show_error_parameters
  468.  
  469.         mov   edx,[0x3010]
  470.         mov   [edx+0xa],byte 4
  471.  
  472.         jmp   change_task
  473.  
  474. ; Stack fault exception
  475. sc:
  476.         add   esp, 4
  477.         mov   ax, os_data
  478.         mov   ds, ax
  479.         mov   es, ax
  480.  
  481.         mov   [error_interrupt],0xc
  482.         mov   [error_interrupt_entry],dword sc
  483.  
  484.         call  show_error_parameters
  485.  
  486.         mov   edx,[0x3010]
  487.         mov   [edx+0xa],byte 4
  488.  
  489.         jmp   change_task
  490.  
  491. ; General Protection Fault
  492. sd:
  493.         add   esp, 4
  494.         mov   ax, os_data
  495.         mov   ds, ax
  496.         mov   es, ax
  497.  
  498.         mov   [error_interrupt],0xd
  499.         mov   [error_interrupt_entry],dword sd
  500.  
  501.         call  show_error_parameters
  502.  
  503.         mov   edx,[0x3010]
  504.         mov   [edx+0xa],byte 4
  505.  
  506.         jmp   change_task
  507.  
  508. ; Page-Fault Exception
  509. se:
  510.         add   esp, 4
  511.         mov   ax, os_data
  512.         mov   ds, ax
  513.         mov   es, ax
  514.  
  515.         mov   [error_interrupt],0xe
  516.         mov   [error_interrupt_entry],dword se
  517.  
  518.         call  show_error_parameters
  519.  
  520.         mov   edx,[0x3010]
  521.         mov   [edx+0xa],byte 4
  522.  
  523.         jmp   change_task
  524.  
  525. ; ??
  526. sf:
  527.         mov   ax, os_data
  528.         mov   ds, ax
  529.         mov   es, ax
  530.  
  531.         mov   [error_interrupt],0xf
  532.         mov   [error_interrupt_entry],dword sf
  533.  
  534.         call  show_error_parameters
  535.  
  536.         mov   edx,[0x3010]
  537.         mov   [edx+0xa],byte 4
  538.  
  539.         jmp   change_task
  540.  
  541. ; x87 FPU Floating-Point Error
  542. s10:
  543.         mov   ax, os_data
  544.         mov   ds, ax
  545.         mov   es, ax
  546.  
  547.         mov   [error_interrupt],0x10
  548.         mov   [error_interrupt_entry],dword s10
  549.  
  550.         call  show_error_parameters
  551.  
  552.         mov   edx,[0x3010]
  553.         mov   [edx+0xa],byte 4
  554.  
  555.         jmp   change_task
  556.  
  557. ; Alignment Check Exception
  558. s11:
  559.         add   esp, 4
  560.         mov   ax, os_data
  561.         mov   ds, ax
  562.         mov   es, ax
  563.  
  564.         mov   [error_interrupt],0x11
  565.         mov   [error_interrupt_entry],dword s11
  566.  
  567.         call  show_error_parameters
  568.  
  569.         mov   edx,[0x3010]
  570.         mov   [edx+0xa],byte 4
  571.  
  572.         jmp   change_task
  573.  
  574.  
  575.  
  576.  
  577. writehex:
  578.  
  579.       pusha
  580.  
  581.       mov  ecx,eax
  582.       mov  ax,word [printerrorat]
  583.       shl  eax,16
  584.       mov  ax,[esp+32+4]
  585.       sub  ax,60
  586.       mov  edx,1
  587.       mov  esi,8
  588.       mov  ebx,0xffffff
  589.     whl1:
  590.       push ecx
  591.       and  ecx,0xf
  592.       add  ecx,hexletters
  593.       mov  edi,1
  594.  
  595.       mov  cl,[ecx]
  596.       mov  edi,[write_error_to]
  597.       mov  [edi],cl
  598.       dec  [write_error_to]
  599.  
  600.       pop  ecx
  601.       shr  ecx,4
  602.       sub  eax,6*65536
  603.       dec  esi
  604.       jnz  whl1
  605.  
  606.       popa
  607.       ret
  608.  
  609. iglobal
  610.   hexletters  db '0123456789ABCDEF'
  611.  
  612.   error_interrupt         dd  -1
  613.   error_interrupt_entry   dd  -1
  614.  
  615.   printerrorat            dd 300
  616.  
  617.   process_error  db 'K : Process - forced terminate INT: 00000000',13,10,0
  618.   process_pid    db 'K : Process - forced terminate PID: 00000000',13,10,0
  619.   process_eip    db 'K : Process - forced terminate EIP: 00000000',13,10,0
  620.   system_error   db 'K : Kernel error',13,10,0
  621. endg
  622.  
  623. uglobal
  624.   write_error_to  dd  0x0
  625. endg
  626.  
  627. show_error_parameters:
  628.        
  629.         mov    [write_error_to],process_pid+43
  630.         mov    eax,[0x3000]
  631.         shl    eax, 5
  632.         mov    eax,[0x3000+4+eax]
  633.         call   writehex
  634.        
  635.         mov    [write_error_to],process_error+43
  636.         mov    eax,[error_interrupt]
  637.         call   writehex
  638.        
  639.         mov    eax,[0x3000]
  640.         shl    eax,8
  641.         cmp    [0x80000+eax+0xB0],dword 0
  642.         jnz    .system_error
  643.  
  644.         mov    eax,[0x3000]
  645.         imul   eax,tss_step
  646.         mov    eax,[eax+tss_data+l.eip-tss_sceleton]
  647. .out_eip:        
  648.         mov    [write_error_to],process_eip+43
  649.         call   writehex
  650.  
  651.         mov    esi,process_error
  652.         call   sys_msg_board_str
  653.  
  654.         mov    esi,process_pid
  655.         call   sys_msg_board_str
  656.  
  657.         mov    esi,process_eip
  658.         call   sys_msg_board_str
  659.  
  660.         ret
  661.        
  662. .system_error:
  663.         mov    esi,system_error
  664.         call   sys_msg_board_str
  665.         mov    eax,[0x3000]
  666.         shl    eax,7
  667.         mov    eax,[eax+0x298000+l.eip-tss_sceleton]
  668.         jmp    .out_eip        
  669.  
  670.  
  671.  
  672.  
  673. ; irq1  ->  hid/keyboard.inc
  674.  
  675. p_irq2:
  676.  
  677.      call  restore_caller
  678.  
  679.      mov   edi,2               ; 1
  680.      call  irqhandler          ; 2/5
  681.  
  682.      call  return_to_caller
  683.  
  684.      jmp   p_irq2
  685.  
  686. p_irq3:
  687.  
  688.      call  restore_caller
  689.  
  690.      mov   edi,3
  691.      call  irqhandler
  692.  
  693.      call  return_to_caller
  694.  
  695.      jmp   p_irq3
  696.  
  697. p_irq4:
  698.  
  699.      call  restore_caller
  700.  
  701.      mov   edi,4
  702.      call  irqhandler
  703.  
  704.      call  return_to_caller
  705.  
  706.      jmp   p_irq4
  707.  
  708. p_irq5:
  709.  
  710.      call  restore_caller
  711.  
  712.      mov   edi,5
  713.      call  irqhandler
  714.  
  715.      call  return_to_caller
  716.  
  717.      jmp   p_irq5
  718.  
  719. p_irq6:
  720.  
  721.      call  restore_caller
  722.  
  723.      call  fdc_irq
  724.  
  725.      mov   edi,6
  726.      call  irqhandler
  727.  
  728.      call  return_to_caller
  729.  
  730.      jmp   p_irq6
  731.  
  732. p_irq7:
  733.  
  734.      call  restore_caller
  735.  
  736.      mov   edi,7
  737.      call  irqhandler
  738.  
  739.      call  return_to_caller
  740.  
  741.      jmp   p_irq7
  742.  
  743. p_irq8:
  744.  
  745.      call  restore_caller
  746.  
  747.      mov   edi,8
  748.      call  irqhandler
  749.  
  750.      call  return_to_caller
  751.  
  752.      jmp   p_irq8
  753.  
  754. p_irq9:
  755.  
  756.      call  restore_caller
  757.  
  758.      mov   edi,9
  759.      call  irqhandler
  760.  
  761.      call  return_to_caller
  762.  
  763.      jmp   p_irq9
  764.  
  765. p_irq10:
  766.  
  767.      call  restore_caller
  768.  
  769.      mov   edi,10
  770.      call  irqhandler
  771.  
  772.      call  return_to_caller
  773.  
  774.      jmp   p_irq10
  775.  
  776. p_irq11:
  777.  
  778.      call  restore_caller
  779.  
  780.      mov   edi,11
  781.      call  irqhandler
  782.  
  783.      call  return_to_caller
  784.  
  785.      jmp   p_irq11
  786.  
  787. p_irq12:
  788.  
  789.       call  restore_caller
  790.  
  791.       mov   edi,12
  792.       call  irqhandler
  793.  
  794.       call  return_to_caller
  795.  
  796.       jmp   p_irq12
  797.  
  798. irqD:
  799.      call   restore_caller
  800.  
  801.      mov   dx,0xf0
  802.      mov   al,0
  803.      out   dx,al
  804.  
  805.      mov   dx,0xa0
  806.      mov   al,0x20
  807.      out   dx,al
  808.      mov   dx,0x20
  809.      out   dx,al
  810.  
  811.      call  return_to_caller
  812.  
  813.      jmp   irqD
  814.      
  815.  
  816. p_irq14:
  817.  
  818.      call  restore_caller
  819.      
  820.      mov   edi,14
  821.      call  irqhandler
  822.  
  823.      call  return_to_caller
  824.  
  825.      jmp   p_irq14
  826.  
  827. p_irq15:
  828.  
  829.      call  restore_caller
  830.  
  831.      mov   edi,15
  832.      call  irqhandler
  833.  
  834.      call  return_to_caller
  835.  
  836.      jmp   p_irq15
  837.  
  838.  
  839.  
  840. align 4
  841. restore_caller:
  842.  
  843.       mov    edi,[0x3000]
  844.       shl    edi, 3
  845.       mov    [edi+gdts+ tss0 +5], word 01010000b *256 +11101001b
  846.  
  847.       ret
  848.  
  849. align 4
  850. return_to_caller:
  851.  
  852.       mov   ebx,[0x3000]
  853.       shl   bx,3
  854.       add   bx,tss0;t
  855.       mov   [tss_irq12],bx
  856.  
  857.            db    0xea
  858.            dd    0
  859. tss_irq12  dw    tss0;t
  860.  
  861.            ret
  862.  
  863. uglobal
  864.   irqh dd 0x0
  865. endg
  866.  
  867. irqhandler:
  868.  
  869.      push   edi
  870.  
  871.      mov    esi,edi          ; 1
  872.      shl    esi,6            ; 1
  873.      add    esi,irq00read    ; 1
  874.      shl    edi,12           ; 1
  875.      add    edi,0x2E0000
  876.  
  877.      mov    [check_idle_semaphore],5
  878.  
  879.    irqnewread:
  880.  
  881.      mov    dx,[esi]         ; 2+
  882.  
  883.      cmp    dx,0             ; 1
  884.      jz     irqover
  885.      cmp    [esi+3],byte 1   ; 2     ; byte read
  886.      jne    noirqbyte        ; 4-11
  887.  
  888.      in     al,dx
  889.  
  890.      mov    edx,[edi]
  891.      cmp    edx,4000
  892.      je     irqfull
  893.      mov    ebx,edi
  894.      add    ebx,0x10
  895.      add    ebx,edx
  896.      mov    [ebx],al
  897.      inc    edx
  898.      mov    [edi],edx
  899.  
  900.      add    esi,4
  901.      jmp    irqnewread
  902.  
  903.    noirqbyte:
  904.  
  905.  
  906.      cmp    [esi+3],byte 2     ; word read
  907.      jne    noirqword
  908.  
  909.      in     ax,dx
  910.  
  911.      mov    edx,[edi]
  912.      cmp    edx,4000
  913.      je     irqfull
  914.      mov    ebx,edi
  915.      add    ebx,0x10
  916.      add    ebx,edx
  917.      mov    [ebx],ax
  918.      add    edx,2
  919.      mov    [edi],edx
  920.      add    esi,4
  921.      jmp    irqnewread
  922.  
  923.    noirqword:
  924.    irqfull:
  925.    irqover:
  926.  
  927.      mov    al,0x20            ; ready for next irq
  928.      out    0x20,al
  929.  
  930.      pop    ebx
  931.      cmp    ebx,7
  932.      jbe    noa0
  933.      out    0xa0,al
  934.    noa0:
  935.  
  936.      ret
  937.  
  938.  
  939. ; simply return control to interrupted process
  940. unknown_interrupt:
  941.      iret
  942.  
  943.  
  944.  
  945.  
  946. set_application_table_status:
  947.         push eax
  948.  
  949.         mov  eax,[0x3000]
  950.         ;imul eax,32
  951.         shl  eax, 5
  952.         add  eax,0x3000+4
  953.         mov  eax,[eax]
  954.  
  955.         mov  [application_table_status],eax
  956.  
  957.         pop  eax
  958.  
  959.         ret
  960.  
  961.  
  962. clear_application_table_status:
  963.         push eax
  964.  
  965.         mov  eax,[0x3000]
  966.         ;imul eax,32
  967.         shl  eax, 5
  968.         add  eax,0x3000+4
  969.         mov  eax,[eax]
  970.  
  971.         cmp  eax,[application_table_status]
  972.         jne  apptsl1
  973.         mov  [application_table_status],0
  974.       apptsl1:
  975.  
  976.         pop  eax
  977.  
  978.         ret
  979.  
  980.  
  981.  
  982. sys_resize_app_memory:
  983.         ; eax = 1 - resize
  984.         ;     ebx = new amount of memory
  985.  
  986.         cmp    eax,1
  987.         jne    .no_application_mem_resize
  988.        
  989.         jmp    new_mem_resize ;resize for new type of processes
  990.  
  991.  
  992.      .no_application_mem_resize:
  993.  
  994.         ret
  995.  
  996.  
  997.  
  998. get_app_params:
  999.  
  1000.     push eax
  1001.  
  1002.     cmp  [0x90000+6],word '00'
  1003.     jne  no_00_header
  1004.  
  1005.     mov  eax,[0x90000+12]
  1006.     mov  [app_start],eax
  1007.     mov  eax,[0x90000+16]
  1008.     mov  [app_i_end],eax
  1009.     mov  eax,[0x90000+20]
  1010.     mov  [app_mem],eax
  1011.     shr  eax,1
  1012.     sub  eax,0x10
  1013.     mov  [app_esp],eax
  1014.     mov  eax,[0x90000+24]
  1015.     mov  [app_i_param],eax
  1016.     mov  [app_i_icon],dword 0
  1017.  
  1018.     pop  eax
  1019.     mov  esi,1
  1020.     ret
  1021.  
  1022.   no_00_header:
  1023.  
  1024.  
  1025.     cmp  [0x90000+6],word '01'
  1026.     jne  no_01_header
  1027.  
  1028.     mov  eax,[0x90000+12]
  1029.     mov  [app_start],eax
  1030.     mov  eax,[0x90000+16]
  1031.     mov  [app_i_end],eax
  1032.     mov  eax,[0x90000+20]
  1033.     mov  [app_mem],eax
  1034.     mov  eax,[0x90000+24]
  1035.     mov  [app_esp],eax
  1036.     mov  eax,[0x90000+28]
  1037.     mov  [app_i_param],eax
  1038.     mov  eax,[0x90000+32]
  1039.     mov  [app_i_icon],eax
  1040.  
  1041.     pop  eax
  1042.     mov  esi,1
  1043.     ret
  1044.  
  1045.    no_01_header:
  1046.  
  1047.     pop  eax
  1048.     mov  esi,0
  1049.     ret
  1050.  
  1051.  
  1052. start_application_fl:
  1053.     jmp new_start_application_fl
  1054.  
  1055. ;************************************************************************
  1056.  
  1057. start_application_floppy:
  1058.     jmp  new_start_application_floppy
  1059.  
  1060. ;********************************************************************
  1061.  
  1062. start_application_hd:
  1063.     jmp   new_start_application_hd
  1064.  
  1065. uglobal
  1066.   new_process_place  dd  0x0
  1067.   app_start    dd  0x0
  1068.   app_i_end    dd  0x0
  1069.   app_mem      dd  0x0
  1070.   app_esp      dd  0x0
  1071.   app_i_param  dd  0x0
  1072.   app_i_icon   dd  0x0
  1073.   app_mem_pos  dd  0x0
  1074.   appl_path        dd 0x0
  1075.   appl_path_size   dd 0x0        
  1076. endg
  1077.  
  1078. iglobal
  1079.   hd_app_string      db  'HDAPP       '
  1080.   process_loading    db 'K : Process - loading ',13,10,0
  1081.   process_running    db 'K : Process - done',13,10,0
  1082.   first_gdt_search   dd 0x2
  1083. endg
  1084.  
  1085.  
  1086. sys_threads:
  1087.  
  1088. ; eax=1 create thread
  1089. ;
  1090. ;   ebx=thread start
  1091. ;   ecx=thread stack value
  1092. ;
  1093. ; on return : eax = pid
  1094. jmp new_sys_threads
  1095.  
  1096. iglobal
  1097.   process_terminating   db 'K : Process - terminating',13,10,0
  1098.   process_terminated    db 'K : Process - done',13,10,0
  1099. endg
  1100.  
  1101.  
  1102. terminate: ; terminate application
  1103.     push   esi
  1104.     mov    esi,process_terminating
  1105.     call   sys_msg_board_str
  1106.     pop    esi
  1107. ;start memory manager code    
  1108. ;    mov    eax,esi
  1109. ;    call   MEM_Heap_Clean
  1110. ;end memory manager code
  1111.    
  1112.  
  1113.     cli
  1114.     cmp   [application_table_status],0
  1115.     je    term9
  1116.     sti
  1117.     call  change_task
  1118.     jmp   terminate
  1119.   term9:
  1120.  
  1121.     call  set_application_table_status
  1122.    
  1123.     mov    eax,esi
  1124.     call   dispose_app_cr3_table
  1125.  
  1126.     cmp   [prev_user_of_fpu],esi   ; if user fpu last -> fpu user = 1
  1127.     jne   fpu_ok_1
  1128.     mov   [prev_user_of_fpu],1
  1129.   fpu_ok_1:
  1130.  
  1131.     mov   [0xf400],byte 0           ; empty keyboard buffer
  1132.     mov   [0xf500],byte 0           ; empty button buffer
  1133.  
  1134.  
  1135.     mov   ecx,esi                 ; remove buttons
  1136.   bnewba2:
  1137.     mov   edi,[0xfe88]
  1138.     mov   eax,edi
  1139.     cld
  1140.     movzx ebx,word [edi]
  1141.     inc   bx
  1142.   bnewba:
  1143.     dec   bx
  1144.     jz    bnmba
  1145.     add   eax,0x10
  1146.     cmp   cx,[eax]
  1147.     jnz   bnewba
  1148.     pusha
  1149.     mov   ecx,ebx
  1150.     inc   ecx
  1151.     shl   ecx,4
  1152.     mov   ebx,eax
  1153.     add   eax,0x10
  1154.     call  memmove
  1155.     dec   dword [edi]
  1156.     popa
  1157.     jmp   bnewba2
  1158.   bnmba:
  1159.  
  1160.     pusha     ; save window coordinates for window restoring
  1161.     cld
  1162.     shl   esi,5
  1163.     add   esi,window_data
  1164.     mov   ax,[esi+0]
  1165.     mov   word [dlx],ax
  1166.     mov   bx,[esi+8]
  1167.     add   ax,bx
  1168.     mov   word [dlxe],ax
  1169.     mov   ax,[esi+4]
  1170.     mov   word [dly],ax
  1171.     mov   bx,[esi+12]
  1172.     add   ax,bx
  1173.     mov   word [dlye],ax
  1174.     mov   [esi+0],word 0
  1175.     mov   [esi+8],word 5
  1176.     mov   ax,[0xFE04]
  1177.     mov   [esi+4],ax
  1178.     mov   [esi+12],word 5
  1179.     xor   eax, eax
  1180.     mov   [esi+16],eax;dword 0
  1181.     mov   [esi+20],eax;dword 0
  1182.     mov   [esi+24],eax;dword 0
  1183.     mov   [esi+28],eax;dword 0
  1184.     popa
  1185.  
  1186.     pusha
  1187.     mov   edi,esi
  1188.     shl   edi,5
  1189.     add   edi,window_data
  1190.     mov   ecx,32/4
  1191.     xor   eax, eax
  1192.   ;  cld
  1193.     rep   stosd
  1194.  
  1195.     mov   eax,[0xFE04]      ; set window to start from maxy+1
  1196.     add   eax,2
  1197.  
  1198.     mov   edi,esi
  1199.     shl   edi,5
  1200.     add   edi,window_data
  1201.     mov   [edi+4],eax
  1202.  
  1203.     popa
  1204.  
  1205.     pusha
  1206.     mov   edi,esi
  1207.     shl   edi,5
  1208.     add   edi,draw_data
  1209.     mov   ecx,32/4
  1210.     xor   eax, eax
  1211.   ;  cld
  1212.     rep   stosd
  1213.     popa
  1214.  
  1215.     pusha         ; at 0x80000+
  1216.     mov   edi,esi
  1217.     shl   edi,8
  1218.     add   edi,0x80000
  1219.     mov   ecx,256/4
  1220.     xor   eax, eax
  1221.   ;  cld
  1222.     rep   stosd
  1223.     popa
  1224.  
  1225.     pusha          ; name to spaces
  1226.     mov   edi,esi
  1227.     shl   edi,8
  1228.     add   edi,0x80000
  1229.     mov   ecx,11
  1230.     mov   eax,' '
  1231.   ;  cld
  1232.     rep   stosb
  1233.     popa
  1234.  
  1235.     pusha                ; C000 --> C400
  1236.     mov   eax, 0xc000
  1237.     mov   esi, 0
  1238.   nlc40:
  1239.     add   eax, 2
  1240.     inc   esi
  1241.     cmp   esi, [0x3004]
  1242.     jae   nlc41
  1243.     movzx ecx, word [eax]
  1244.     mov   [0xC400 + ecx*2], si
  1245.     jmp   nlc40
  1246.   nlc41:
  1247.     popa
  1248.  
  1249.     pusha ; remove hd1 reservation
  1250.     mov   edx,esi
  1251.     shl   edx, 5 ;imul  edx,0x20
  1252.     add   edx,0x3000
  1253.     mov   edx,[edx+4]
  1254.     cmp   [hd1_status],edx
  1255.     jne   no_hd1_s_remove
  1256.     mov   [hd1_status],0
  1257.   no_hd1_s_remove:
  1258.     popa
  1259.  
  1260.     pusha ; remove all irq reservations
  1261.     mov   edx,esi
  1262.     shl   edx, 5 ;imul  edx,0x20
  1263.     add   edx,0x3000
  1264.     mov   edx,[edx+4]
  1265.     mov   edi,irq_owner
  1266.     mov   ecx,16
  1267.   newirqfree:
  1268.     cmp   [edi],edx
  1269.     jne   nofreeirq
  1270.     mov   [edi],dword 0
  1271.   nofreeirq:
  1272.     add    edi,4
  1273.     loop   newirqfree
  1274.     popa
  1275.  
  1276.  
  1277.     pusha                     ; remove all port reservations
  1278.  
  1279.     mov   edx,esi
  1280.     shl   edx, 5 ;imul  edx,0x20
  1281.     add   edx,0x3000
  1282.     mov   edx,[edx+4]
  1283.  
  1284.   rmpr0:
  1285.  
  1286.     mov   esi,[0x2d0000]
  1287.  
  1288.     cmp   esi,0
  1289.     je    rmpr9
  1290.  
  1291.   rmpr3:
  1292.  
  1293.     mov   edi,esi
  1294.     shl   edi,4
  1295.     add   edi,0x2d0000
  1296.  
  1297.     cmp   edx,[edi]
  1298.     je    rmpr4
  1299.  
  1300.     dec   esi
  1301.     jnz   rmpr3
  1302.  
  1303.     jmp   rmpr9
  1304.  
  1305.   rmpr4:
  1306.  
  1307.     mov   ecx,256
  1308.     sub   ecx,esi
  1309.     shl   ecx,4
  1310.  
  1311.     mov   esi,edi
  1312.     add   esi,16
  1313.     cld
  1314.     rep   movsb
  1315.  
  1316.     dec   dword [0x2d0000]
  1317.  
  1318.     jmp   rmpr0
  1319.  
  1320.   rmpr9:
  1321.  
  1322.     popa
  1323.     mov  edi,esi         ; do not run this process slot
  1324.     shl  edi, 5
  1325.     mov  [edi+0x300A],byte 9
  1326. ;    call  systest
  1327.     sti  ; .. and life goes on
  1328.  
  1329. ;    movzx eax,word [dlx]
  1330. ;    movzx ebx,word [dly]
  1331. ;    movzx ecx,word [dlxe]
  1332. ;    movzx edx,word [dlye]
  1333.     call  calculatescreen
  1334.  
  1335.     xor   eax, eax
  1336.     xor   esi, esi
  1337.     call  redrawscreen
  1338.  
  1339.     mov   [0xfff4],byte 0  ; no mouse background
  1340.     mov   [0xfff5],byte 0  ; draw mouse
  1341.  
  1342.     mov   [application_table_status],0
  1343.  
  1344.     mov   esi,process_terminated
  1345.     call  sys_msg_board_str
  1346.  
  1347. ;* start code - fix error redraw for terminate (2) - Mario79
  1348. ;     cmp  [draw_present],1
  1349. ;     je  no_draw_background_temp
  1350. ;     mov   [0xfff0],byte 1
  1351. ;no_draw_background_temp:
  1352. ;    mov   [draw_present],0
  1353.     ret
  1354. ;draw_present db 0
  1355. ;* end code - fix error redraw for terminate (2) - Mario79
  1356.  
  1357. iglobal
  1358.   boot_sched_1    db   'Building gdt tss pointer',0
  1359.   ;boot_sched_2    db   'Building gdt gate pointer',0
  1360.   boot_sched_3    db   'Building interrupt table - TSS',0
  1361.   boot_sched_3_2  db   'Building interrupt table - GDT',0
  1362.   boot_sched_3_3  db   'Building interrupt table - IDT',0
  1363.   boot_sched_4    db   'Building syscall interrupt table',0
  1364. endg
  1365.  
  1366.  
  1367. build_scheduler:
  1368.  
  1369.         mov    esi,boot_sched_1
  1370.         call   boot_log
  1371.         call   build_process_gdt_tss_pointer
  1372.  
  1373. ;        mov    esi,boot_sched_2
  1374. ;        call   boot_log
  1375. ;        call   build_process_gdt_gate_pointer
  1376.  
  1377.         mov    esi,boot_sched_3
  1378.         call   boot_log
  1379.         call   build_interrupt_table
  1380.  
  1381.         mov    esi,boot_sched_4
  1382.         call   boot_log
  1383.         call   build_syscall_interrupt_table
  1384.  
  1385.         ret
  1386.  
  1387.