Subversion Repositories Kolibri OS

Rev

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

  1. GREEDY_KERNEL  equ 0; 1
  2.  
  3.  
  4. struc APP_HEADER_00
  5. { .banner      dq ?
  6.   .version     dd ?    ;+8
  7.   .start       dd ?    ;+12
  8.   .i_end       dd ?    ;+16
  9.   .mem_size    dd ?    ;+20
  10.   .i_param     dd ?    ;+24
  11. }
  12.  
  13. struc APP_HEADER_01
  14. { .banner      dq ?
  15.   .version     dd ?    ;+8
  16.   .start       dd ?    ;+12
  17.   .i_end       dd ?    ;+16
  18.   .mem_size    dd ?    ;+20
  19.   .stack_top   dd ?    ;+24
  20.   .i_param     dd ?    ;+28
  21.   .i_icon      dd ?    ;+32
  22. }
  23.  
  24. align 4
  25. proc test_app_header stdcall, header:dword
  26.            virtual at ebx
  27.              APP_HEADER_00 APP_HEADER_00
  28.            end virtual
  29.  
  30.            mov ebx, [header]
  31.            cmp [ebx+6], word '00'
  32.            jne  .check_01_header
  33.  
  34.            mov  eax,[APP_HEADER_00.start]
  35.            mov  [app_start],eax
  36.            mov  eax,[APP_HEADER_00.i_end]
  37.            mov  [app_i_end],eax
  38.            mov  eax,[APP_HEADER_00.mem_size]
  39.            mov  [app_mem],eax
  40.            shr  eax,1
  41.            sub  eax,0x10
  42.            mov  [app_esp],eax
  43.            mov  eax,[APP_HEADER_00.i_param]
  44.            mov  [app_i_param],eax
  45.            mov  [app_i_icon],dword 0
  46.  
  47.            mov  eax,1
  48.            ret
  49.  
  50.  .check_01_header:
  51.            virtual at ebx
  52.              APP_HEADER_01 APP_HEADER_01
  53.            end virtual
  54.  
  55.            cmp  [ebx+6],word '01'
  56.            jne  .no_01_header
  57.  
  58.            mov  eax,[APP_HEADER_01.start]
  59.            mov  [app_start],eax
  60.            mov  eax,[APP_HEADER_01.i_end]
  61.            mov  [app_i_end],eax
  62.            mov  eax,[APP_HEADER_01.mem_size]
  63.            mov  [app_mem],eax
  64.            mov  eax,[APP_HEADER_01.stack_top]
  65.            mov  [app_esp],eax
  66.            mov  eax,[APP_HEADER_01.i_param]
  67.            mov  [app_i_param],eax
  68.            mov  eax,[APP_HEADER_01.i_icon]
  69.            mov  [app_i_icon],eax
  70.  
  71.            mov  eax,1
  72.            ret
  73.  
  74. .no_01_header:
  75.  
  76.            xor eax, eax
  77.            ret
  78. endp
  79.  
  80. align 4
  81. proc get_new_process_place
  82. ;input:
  83. ;  none
  84. ;result:
  85. ;  eax=[new_process_place]<>0 - ok
  86. ;      0 - failed.
  87. ;This function find least empty slot.
  88. ;It doesn't increase [TASK_COUNT]!
  89.            mov    eax,CURRENT_TASK
  90.            mov    ebx,[TASK_COUNT]
  91.            inc    ebx
  92.            shl    ebx,5
  93.            add    ebx,eax               ;ebx - address of process information for (last+1) slot
  94. .newprocessplace:
  95. ;eax = address of process information for current slot
  96.            cmp    eax,ebx
  97.            jz     .endnewprocessplace   ;empty slot after high boundary
  98.            add    eax,0x20
  99.            cmp    word [eax+0xa],9      ;check process state, 9 means that process slot is empty
  100.            jnz    .newprocessplace
  101. .endnewprocessplace:
  102.            mov    ebx,eax
  103.            sub    eax,CURRENT_TASK
  104.            shr    eax,5                 ;calculate slot index
  105.            cmp    eax,256
  106.            jge    .failed               ;it should be <256
  107.            mov    word [ebx+0xa],9      ;set process state to 9 (for slot after hight boundary)
  108.         ;   mov [new_process_place], eax
  109.            ret
  110.  
  111. .failed:
  112.            xor    eax,eax
  113.            ret
  114. endp
  115.  
  116. align 4
  117. proc create_app_space stdcall, app_size:dword,img_size:dword
  118.            locals
  119.              app_pages   dd ?
  120.              img_pages   dd ?
  121.              dir_addr    dd ?
  122.              app_tabs    dd ?
  123.            endl
  124.  
  125.            stdcall wait_mutex, pg_data.pg_mutex
  126.  
  127.            xor eax, eax
  128.            mov [dir_addr], eax
  129.  
  130.            mov eax, [app_size]
  131.            add eax, 4095
  132.            and eax, NOT(4095)
  133.            mov [app_size], eax
  134.            mov ebx, eax
  135.            shr eax, 12
  136.            mov [app_pages], eax
  137.  
  138.            add ebx, 0x3FFFFF
  139.            and ebx, NOT(0x3FFFFF)
  140.            shr ebx, 22
  141.            mov [app_tabs], ebx
  142.  
  143.            mov ecx, [img_size]
  144.            add ecx, 4095
  145.            and ecx, NOT(4095)
  146.  
  147.            mov [img_size], ecx
  148.            shr ecx, 12
  149.            mov [img_pages], ecx
  150.  
  151.      if GREEDY_KERNEL
  152.            lea eax, [ecx+ebx+2]    ;only image size
  153.      else
  154.            lea eax, [eax+ebx+2]    ;all requested memory
  155.      end if
  156.            cmp eax, [pg_data.pages_free]
  157.            ja .fail
  158.  
  159.            call alloc_page
  160.            test eax, eax
  161.            jz .fail
  162.            mov [dir_addr], eax
  163.            stdcall map_page,[tmp_task_pdir],eax,dword PG_SW
  164.  
  165.            mov esi, sys_pgdir
  166.            mov edi, [tmp_task_pdir]
  167.            mov ecx, 384
  168.            cld
  169.            rep movsd
  170.  
  171.            mov ecx, 384
  172.            xor eax, eax
  173.            cld
  174.            rep stosd
  175.  
  176.            mov ecx, 256
  177.            mov esi, sys_pgdir+0xc00
  178.            rep movsd
  179.  
  180.            mov eax, [dir_addr]
  181.            or eax, PG_SW
  182.            mov ebx, [tmp_task_pdir]
  183.            mov [ebx+0x600], eax
  184.  
  185.            mov eax, [dir_addr]
  186.            call set_cr3
  187.  
  188.            mov edx, [app_tabs]
  189.            mov edi, new_app_base
  190. @@:
  191.            call alloc_page
  192.            test eax, eax
  193.            jz .fail
  194.  
  195.            stdcall map_page_table, edi, eax
  196.            add edi, 0x00400000
  197.            dec edx
  198.            jnz @B
  199.  
  200.            mov edi, new_app_base
  201.            shr edi, 10
  202.            add edi, pages_tab
  203.            mov ecx, [app_tabs]
  204.            shl ecx, 10
  205.            xor eax, eax
  206.            rep stosd
  207.  
  208.            mov edx, new_app_base
  209. .alloc:
  210.            call alloc_page
  211.            test eax, eax
  212.            jz .fail
  213.  
  214.            stdcall map_page,edx,eax,dword PG_UW
  215.            add edx, 0x1000
  216.            sub [app_pages], 1
  217.            sub [img_pages], 1
  218.            jnz .alloc
  219.  
  220.            mov ecx, [app_pages]
  221.            and ecx, ecx
  222.            jz .next
  223.  
  224.            mov ebx, edx
  225.            shr edx, 12
  226. .reserve:
  227.            mov dword [pages_tab+edx*4], 0x02
  228.            invlpg [ebx]
  229.            inc edx
  230.            dec ecx
  231.            jnz .reserve
  232. .next:
  233.            mov edi, new_app_base
  234.            mov ecx, [img_size]
  235.            shr ecx, 2
  236.            xor eax, eax
  237.            cld
  238.            rep stosd
  239.  
  240.            stdcall map_page,[tmp_task_pdir],dword 0,dword PG_UNMAP
  241.  
  242.            dec [pg_data.pg_mutex]
  243.            mov eax, [dir_addr]
  244.            ret
  245. .fail:
  246.            dec [pg_data.pg_mutex]
  247.            cmp [dir_addr], 0
  248.            jz @f
  249.            stdcall destroy_app_space, [dir_addr]
  250. @@:
  251.            xor eax, eax
  252.            ret
  253. endp
  254.  
  255. align 4
  256. set_cr3:
  257.            mov esi, [CURRENT_TASK]
  258.            mov ebx, esi
  259.            shl esi,8
  260.            mov [PROC_BASE+esi+0xB8],eax
  261.            imul   ebx,tss_step
  262.            add    ebx,tss_data
  263.            mov [ebx+28], eax
  264.            mov cr3, eax
  265.            ret
  266.  
  267. align 4
  268. proc destroy_page_table stdcall, pg_tab:dword
  269.  
  270.            push esi
  271.  
  272.            mov esi, [pg_tab]
  273.            mov ecx, 1024
  274. .free:
  275.            mov eax, [esi]
  276.            test eax, 1
  277.            jz .next
  278.            call free_page
  279. .next:
  280.            add esi, 4
  281.            dec ecx
  282.            jnz .free
  283.            pop esi
  284.            ret
  285. endp
  286.  
  287. align 4
  288. proc destroy_app_space stdcall, pg_dir:dword
  289.  
  290.            stdcall wait_mutex, pg_data.pg_mutex
  291.  
  292.            xor   edx,edx
  293.            mov   eax,0x2
  294.            mov ebx, [pg_dir]
  295.  
  296. .loop:
  297. ;eax = current slot of process
  298.            mov   ecx,eax
  299.            shl   ecx,5
  300.            cmp   byte [CURRENT_TASK+ecx+0xa],9  ;if process running?
  301.            jz    @f                              ;skip empty slots
  302.            shl   ecx,3
  303.            cmp   [PROC_BASE+ecx+0xB8],ebx       ;compare page directory addresses
  304.            jnz   @f
  305.            inc   edx                            ;thread found
  306. @@:
  307.            inc   eax
  308.            cmp   eax,[TASK_COUNT]               ;exit loop if we look through all processes
  309.            jle   .loop
  310.  
  311. ;edx = number of threads
  312. ;our process is zombi so it isn't counted
  313.            cmp   edx,1
  314.            jg    .exit
  315. ;if there isn't threads then clear memory.
  316.  
  317.            mov eax, [pg_dir]
  318.            and eax, not 0xFFF
  319.            stdcall map_page,[tmp_task_pdir],eax,dword PG_SW
  320.            mov esi, [tmp_task_pdir]
  321.            add esi, 0x604
  322.            mov edi, 383
  323. .destroy:
  324.            mov eax, [esi]
  325.            test eax, 1
  326.            jz .next
  327.            and eax, not 0xFFF
  328.            stdcall map_page,[tmp_task_ptab],eax,dword PG_SW
  329.            stdcall destroy_page_table, [tmp_task_ptab]
  330.            mov eax, [esi]
  331.            call free_page
  332. .next:
  333.            add esi, 4
  334.            dec edi
  335.            jnz .destroy
  336.  
  337.            mov eax, [pg_dir]
  338.            call free_page
  339. .exit:
  340.            stdcall map_page,[tmp_task_ptab],dword 0,dword PG_UNMAP
  341.            stdcall map_page,[tmp_task_pdir],dword 0,dword PG_UNMAP
  342.            dec [pg_data.pg_mutex]
  343.            ret
  344. endp
  345.  
  346. align 4
  347. proc fs_execute
  348.  
  349. ;fn_read:dword, file_size:dword, cluster:dword
  350.  
  351. ; ebx - cmdline
  352. ; edx - flags
  353. ; ebp - full filename
  354. ; [esp+4] = procedure DoRead, [esp+8] = filesize & [esp+12]... - arguments for it
  355.  
  356.            locals
  357.              cmdline    dd ?
  358.              flags      dd ?
  359.              filename   dd ?
  360.              retval     dd ?
  361.            endl
  362.  
  363.            pushad
  364.  
  365.            mov [cmdline], ebx
  366.            mov [flags], edx
  367.            mov eax, [ebp]
  368.            mov [filename], eax
  369.  
  370.            stdcall wait_mutex, pg_data.tmp_task_mutex
  371.  
  372.            mov edi, [tmp_task_data]
  373.            mov ecx, (2048+256)/4
  374.            xor eax, eax
  375.            rep stosd
  376.  
  377.            mov esi, [filename]
  378.            mov edi, [tmp_task_data]
  379.            add edi, TMP_FILE_NAME
  380.            mov ecx, 1024
  381.            rep movsb
  382.  
  383.            mov esi, [filename]
  384.            mov edi, [tmp_task_data]
  385.            add edi, TMP_ICON_OFFS
  386.            mov ecx, 1024
  387.            rep movsb
  388.  
  389.            mov esi, [cmdline]
  390.            test esi, esi
  391.            jz @f
  392.            mov edi, [tmp_task_data]
  393.            add edi, TMP_CMD_LINE
  394.            mov ecx, 256
  395.            rep movsb
  396. @@:
  397.            mov eax, TMP_FILE_NAME
  398.            add eax, [tmp_task_data]
  399.            mov ebx, [tmp_task_data]    ;cmd line
  400.            add ebx, TMP_CMD_LINE
  401.  
  402.            stdcall fs_exec, eax, ebx, [flags], [ebp+8],\
  403.                             [ebp+12], [ebp+16],[ebp+20]
  404.            mov [retval], eax
  405.            popad
  406.            mov [pg_data.tmp_task_mutex], 0
  407.            mov eax, [retval]
  408.            ret
  409.  
  410. endp
  411.  
  412. align 4
  413. proc fs_exec stdcall file_name:dword, cmd_line:dword, flags:dword,\
  414.                        fn_read:dword, file_size:dword,\
  415.                        cluster:dword, some_data:dword
  416.  
  417.            locals
  418.              slot          dd ?
  419.              app_path_size dd ?
  420.              save_cr3      dd ?
  421.              img_size      dd ?
  422.            endl
  423.  
  424. ; check filename length - with terminating NULL must be no more than 1024 symbols
  425.  
  426.            mov edi, [file_name]
  427.            mov ecx, 1024
  428.            xor eax, eax
  429.            repnz scasb
  430.            jz @f
  431.            mov     eax, -ERROR_FILE_NOT_FOUND
  432.            ret
  433. @@:
  434.            sub edi, [file_name]
  435.            mov [app_path_size], edi
  436.  
  437.            mov esi, new_process_loading
  438.            call sys_msg_board_str       ; write message to message board
  439.  
  440.            pushfd
  441.            cli
  442.  
  443. .wait_lock:
  444.            cmp [application_table_status],0
  445.            je .get_lock
  446.            call   change_task
  447.            jmp .wait_lock
  448.  
  449. .get_lock:
  450.            mov eax, 1
  451.            xchg eax, [application_table_status]
  452.            cmp eax, 0
  453.            jne .wait_lock
  454.  
  455.            call   set_application_table_status
  456.  
  457.            call get_new_process_place
  458.            test eax, eax
  459.            mov ecx, -0x20      ; too many processes
  460.            jz .err
  461.            mov [slot], eax
  462.  
  463.            mov    edi,eax
  464.            shl    edi,8
  465.            add    edi,PROC_BASE
  466.            mov    ecx,256/4
  467.            xor    eax,eax
  468.            cld
  469.            rep    stosd              ;clean extended information about process
  470.  
  471. ; write application name
  472.  
  473.            mov edi, [file_name]
  474.            mov ecx, [app_path_size]
  475.            add edi, ecx
  476.            dec edi
  477.            std
  478.            mov al, '/'
  479.            repnz scasb
  480.            cld
  481.            jnz @f
  482.            inc edi
  483. @@:
  484.            inc edi
  485. ; now edi points to name without path
  486.  
  487.            mov esi, edi
  488.            mov ecx, 8  ; 8 chars for name
  489.            mov edi, [slot]
  490.            shl edi, cl
  491.            add edi, PROC_BASE
  492. .copy_process_name_loop:
  493.            lodsb
  494.            cmp al, '.'
  495.            jz .copy_process_name_done
  496.            test al, al
  497.            jz .copy_process_name_done
  498.            stosb
  499.            loop .copy_process_name_loop
  500. .copy_process_name_done:
  501.            mov al, ' '
  502.            rep stosb
  503.            pop eax
  504.            mov cl, 3   ; 3 chars for extension
  505.            dec esi
  506. @@:
  507.            dec eax
  508.            cmp eax, esi
  509.            jbe .copy_process_ext_done
  510.            cmp byte [eax], '.'
  511.            jnz @b
  512.            lea esi, [eax+1]
  513. .copy_process_ext_loop:
  514.            lodsb
  515.            test al, al
  516.            jz  .copy_process_ext_done
  517.            stosb
  518.            loop .copy_process_ext_loop
  519. .copy_process_ext_done:
  520.            mov al, ' '
  521.            rep stosb
  522.  
  523. ; read header
  524.            lea  eax, [file_size]
  525.            mov ebx, [eax]
  526.            mov [img_size], ebx
  527.            mov  edi, TMP_BUFF
  528.            call [fn_read]
  529.  
  530.            test eax, eax
  531.            jnz .err
  532.  
  533. ; check menuet signature
  534.  
  535.            mov ecx, -0x1F
  536. ;check MENUET signature
  537.            cmp    [TMP_BUFF],dword 'MENU'
  538.            jnz    .err
  539.            cmp    [TMP_BUFF+4],word 'ET'
  540.            jnz    .err
  541.  
  542.            stdcall test_app_header, TMP_BUFF
  543.            test eax, eax
  544.            jz .err
  545.  
  546.            mov eax, cr3
  547.            mov [save_cr3], eax
  548.      if GREEDY_KERNEL
  549.            stdcall create_app_space,[app_mem],[img_size]
  550.      else
  551.            stdcall create_app_space,[app_mem],[app_mem]
  552.      end if
  553.            test eax, eax
  554.            jz .failed
  555.  
  556.            mov    ebx,[slot]
  557.            shl   ebx,8
  558.            mov   [PROC_BASE+ebx+0xB8],eax
  559.  
  560.            mov esi, TMP_BUFF
  561.            mov edi, new_app_base
  562.            mov ecx, 512/4
  563.            cld
  564.            rep movsd
  565.  
  566. ;read file
  567. @@:
  568.            lea  eax, [file_size]
  569.            cmp dword [eax], 0
  570.            jz .done
  571.            push edi
  572.            call [fn_read]
  573.            pop edi
  574.            add edi, 512
  575.            test eax, eax
  576.            jz  @b
  577.            cmp ebx, 6
  578.            jne .failed
  579. .done:
  580.            stdcall add_app_parameters, [slot], new_app_base,\
  581.                                        [cmd_line],[file_name],[flags]
  582.  
  583.            mov eax, [save_cr3]
  584.            call set_cr3
  585.  
  586.            xor eax, eax
  587.            mov    [application_table_status],eax ;unlock application_table_status mutex
  588.            popfd
  589.            mov    eax,[process_number]  ;set result
  590.            ret
  591.  
  592. .failed:
  593.            mov eax, [save_cr3]
  594.            call set_cr3
  595. .err:
  596.  
  597.            popfd
  598.            xor eax, eax
  599.            mov [application_table_status],eax
  600.            ret
  601. endp
  602.  
  603. align 4
  604. proc add_app_parameters stdcall,slot:dword,img_base:dword,\
  605.                         cmd_line:dword, app_path:dword, flags:dword
  606.  
  607.            mov eax,[slot]
  608.            bt [cpu_caps], CAPS_SSE
  609.            jnc .no_SSE
  610.            shl eax, 8
  611.            mov ebx, eax
  612.            add eax, eax
  613.            add eax, [fpu_data]
  614.            mov [ebx+PROC_BASE+APPDATA.fpu_state], eax
  615.            mov [ebx+PROC_BASE+APPDATA.fpu_handler], 0
  616.            mov [ebx+PROC_BASE+APPDATA.sse_handler], 0
  617.            jmp .m1
  618. .no_SSE:
  619.            mov ecx, eax
  620.            mov ebx, eax
  621.            shl eax, 7
  622.            shl ebx, 4
  623.            sub eax, ebx       ;eax*=112
  624.            add eax, [fpu_data]
  625.            shl ecx, 8
  626.            mov [ecx+PROC_BASE+APPDATA.fpu_state], eax
  627.            mov [ecx+PROC_BASE+APPDATA.fpu_handler], 0
  628.            mov [ecx+PROC_BASE+APPDATA.sse_handler], 0
  629. .m1:
  630.            mov    ebx,[slot]
  631.            cmp    ebx,[TASK_COUNT]
  632.            jle    .noinc
  633.            inc    dword [TASK_COUNT]       ;update number of processes
  634. .noinc:
  635.            shl    ebx,8
  636.            mov    eax,[app_mem]
  637.            mov    [PROC_BASE+0x8c+ebx],eax
  638.  
  639.            shr    ebx,3
  640.            mov eax, new_app_base
  641.            mov    dword [CURRENT_TASK+ebx+0x10],eax
  642.  
  643. .add_command_line:
  644.            mov    edx,[app_i_param]
  645.            test   edx,edx
  646.            jz     .no_command_line      ;application don't need parameters
  647.            mov    eax,[cmd_line]
  648.            test   eax,eax
  649.            jz     .no_command_line      ;no parameters specified
  650. ;calculate parameter length
  651.            xor    ecx,ecx
  652. .command_line_len:
  653.            cmp    byte [eax],0
  654.            jz     .command_line_len_end
  655.            inc    eax
  656.            inc    ecx
  657.            cmp    ecx,255
  658.            jl     .command_line_len
  659.  
  660. .command_line_len_end:
  661. ;ecx - parameter length
  662. ;edx - address of parameters in new process address space
  663.            inc ecx
  664.            mov edi, [img_base]
  665.            add edi, edx
  666.            mov esi, [cmd_line]
  667.            rep movsb
  668.  
  669. .no_command_line:
  670.  
  671.             mov    edx,[app_i_icon]
  672.             test   edx,edx
  673.             jz     .no_command_line_1      ;application don't need path of file
  674.             mov    esi,[app_path]
  675.             test esi, esi
  676.             jz     .no_command_line_1      ;application don't need path of file
  677.             mov    ecx, 64
  678.             mov    edi, [img_base]
  679.             add edi, edx
  680.             rep movsb
  681.  
  682. .no_command_line_1:
  683.            mov    ebx,[slot]
  684.            mov    eax,ebx
  685.            shl    ebx,5
  686. ; set window state to 'normal' (non-minimized/maximized/rolled-up) state
  687.         mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
  688.         mov     [ebx+window_data+WDATA.fl_redraw], 1
  689.            add    ebx,CURRENT_TASK            ;ebx - pointer to information about process
  690.            mov    [ebx+TASKDATA.wnd_number],al;set window number on screen = process slot
  691.  
  692.            mov    [ebx+TASKDATA.event_mask],dword 1+2+4 ;set default event flags (see 40 function)
  693.  
  694.            inc    dword [process_number]
  695.            mov    eax,[process_number]
  696.            mov    [ebx+4],eax           ;set PID
  697.  
  698.            mov    ecx,ebx
  699.            add    ecx,(draw_data-CURRENT_TASK)  ;ecx - pointer to draw data
  700. ;set draw data to full screen
  701.  
  702.            mov    [ecx+0],dword 0
  703.            mov    [ecx+4],dword 0
  704.            mov    eax,[SCR_X_SIZE]
  705.            mov    [ecx+8],eax
  706.            mov    eax,[SCR_Y_SIZE]
  707.            mov    [ecx+12],eax
  708. ;set cr3 register in TSS of application
  709.  
  710.            mov    ecx,[slot]
  711.            shl    ecx,8
  712.            mov    eax,[PROC_BASE+0xB8+ecx]
  713.            ;or     eax,  PG_NOCACHE
  714.            mov    [l.cr3],eax
  715.  
  716.            mov    eax,[app_start]
  717.            mov    [l.eip],eax           ;set eip in TSS
  718.            mov    eax,[app_esp]
  719.            mov    [l.esp],eax           ;set stack in TSS
  720.  
  721. ;gdt
  722.            mov    ax,app_code           ;ax - selector of code segment
  723.            mov    [l.cs],ax
  724.            mov    ax,app_data
  725.            mov    [l.ss],ax
  726.            mov    [l.ds],ax
  727.            mov    [l.es],ax
  728.            mov    [l.fs],ax
  729.            mov    ax,graph_data         ;ax - selector of graphic segment
  730.            mov    [l.gs],ax
  731.            mov    [l.io],word 128
  732.            mov    [l.eflags],dword 0x1202
  733.  
  734.            mov    [l.ss0],os_data
  735.            mov    ebx,[slot]
  736.            shl    ebx,12
  737.            add    ebx,sysint_stack_data+4096
  738.            mov    [l.esp0],ebx
  739.  
  740. ;copy tss to it place
  741.            mov    eax,tss_sceleton
  742.            mov    ebx,[slot]
  743.            imul   ebx,tss_step
  744.            add    ebx,tss_data          ;ebx - address of application TSS
  745.            mov    ecx,120
  746.            call   memmove
  747.  
  748. ;Add IO access table - bit array of permitted ports
  749.            or     eax,-1
  750.            mov    edi,[slot]
  751.            imul   edi,tss_step
  752.            add    edi,tss_data+128
  753.            mov    ecx,2048
  754.            cld
  755.            rep    stosd                 ;full access to 2048*8=16384 ports
  756.  
  757.            mov    ecx,ebx               ;ecx - address of application TSS
  758.            mov    edi,[slot]
  759.            shl    edi,3
  760. ;set TSS descriptor
  761.            mov    [edi+gdts+tss0+0],word tss_step ;limit (size)
  762.            mov    [edi+gdts+tss0+2],cx  ;part of offset
  763.            mov    eax,ecx
  764.            shr    eax,16
  765.            mov    [edi+gdts+tss0+4],al  ;part of offset
  766.            mov    [edi+gdts+tss0+7],ah  ;part of offset
  767.            mov    [edi+gdts+tss0+5],word 01010000b*256+11101001b ;system flags
  768.  
  769. ;flush keyboard and buttons queue
  770.            mov    [KEY_COUNT],byte 0
  771.            mov    [BTN_COUNT],byte 0
  772.  
  773.            mov    edi,[slot]
  774.            shl    edi,5
  775.            add    edi,window_data
  776.            mov    ebx,[slot]
  777.            movzx  esi,word [WIN_STACK+ebx*2]
  778.            lea    esi,[WIN_POS+esi*2]
  779.            call   windowactivate        ;gui initialization
  780.  
  781.            mov    ebx,[slot]
  782.            shl    ebx,5
  783.            mov    [CURRENT_TASK+ebx+0xa],byte 0 ;set process state - running
  784. ; set if debuggee
  785.            mov eax, [flags]
  786.            test   byte [flags], 1
  787.            jz     .no_debug
  788.            mov    [CURRENT_TASK+ebx+0xa],byte 1 ;set process state - suspended
  789.            mov    eax,[CURRENT_TASK]
  790.            mov    [PROC_BASE+ebx*8+0xac],eax ;set debugger PID - current
  791. .no_debug:
  792.  
  793.            mov    esi,new_process_running
  794.            call   sys_msg_board_str     ;output information about succefull startup
  795.  
  796.            ret
  797. endp
  798.  
  799. pid_to_slot:
  800. ;Input:
  801. ;  eax - pid of process
  802. ;Output:
  803. ;  eax - slot of process or 0 if process don't exists
  804. ;Search process by PID.
  805.     push   ebx
  806.     push   ecx
  807.     mov    ebx,[TASK_COUNT]
  808.     shl    ebx,5
  809.     mov    ecx,2*32
  810.  
  811. .loop:
  812. ;ecx=offset of current process info entry
  813. ;ebx=maximum permitted offset
  814.     cmp    byte [CURRENT_TASK+ecx+0xa],9
  815.     jz     .endloop              ;skip empty slots
  816.     cmp    [CURRENT_TASK+ecx+0x4],eax ;check PID
  817.     jz     .pid_found
  818. .endloop:
  819.     add    ecx,32
  820.     cmp    ecx,ebx
  821.     jle    .loop
  822.  
  823.     pop    ecx
  824.     pop    ebx
  825.     xor    eax,eax
  826.     ret
  827.  
  828. .pid_found:
  829.     shr    ecx,5
  830.     mov    eax,ecx               ;convert offset to index of slot
  831.     pop    ecx
  832.     pop    ebx
  833.     ret
  834.  
  835. check_region:
  836. ;input:
  837. ;  ebx - start of buffer
  838. ;  ecx - size of buffer
  839. ;result:
  840. ;  eax = 1 region lays in app memory
  841. ;  eax = 0 region don't lays in app memory
  842.      mov  eax,[CURRENT_TASK]
  843.      jmp  check_process_region
  844. ;-----------------------------------------------------------------------------
  845. check_process_region:
  846. ;input:
  847. ;  eax - slot
  848. ;  ebx - start of buffer
  849. ;  ecx - size of buffer
  850. ;result:
  851. ;  eax = 1 region lays in app memory
  852. ;  eax = 0 region don't lays in app memory
  853.  
  854.      test ecx,ecx
  855.      jle  .ok
  856.      shl  eax,5
  857.      cmp  word [CURRENT_TASK+eax+0xa],0
  858.      jnz  .failed
  859.      shl  eax,3
  860.      mov  eax,[PROC_BASE+eax+0xb8]
  861.      test eax,eax
  862.      jz   .failed
  863.  
  864.      mov  eax,1
  865.      ret
  866.  
  867.  
  868. ;    call MEM_Get_Linear_Address
  869. ;    push ebx
  870. ;    push ecx
  871. ;    push edx
  872. ;    mov  edx,ebx
  873. ;    and  edx,not (4096-1)
  874. ;    sub  ebx,edx
  875. ;    add  ecx,ebx
  876. ;    mov  ebx,edx
  877. ;    add  ecx,(4096-1)
  878. ;    and  ecx,not (4096-1)
  879. ;.loop:
  880. ;;eax - linear address of page directory
  881. ;;ebx - current page
  882. ;;ecx - current size
  883. ;    mov  edx,ebx
  884. ;    shr  edx,22
  885. ;    mov  edx,[eax+4*edx]
  886. ;    and  edx,not (4096-1)
  887. ;    test edx,edx
  888. ;    jz   .failed1
  889. ;    push eax
  890. ;    mov  eax,edx
  891. ;    call MEM_Get_Linear_Address
  892. ;    mov  edx,ebx
  893. ;    shr  edx,12
  894. ;    and  edx,(1024-1)
  895. ;    mov  eax,[eax+4*edx]
  896. ;    and  eax,not (4096-1)
  897. ;    test eax,eax
  898. ;    pop  eax
  899. ;    jz   .failed1
  900. ;    add  ebx,4096
  901. ;    sub  ecx,4096
  902. ;    jg   .loop
  903. ;    pop  edx
  904. ;    pop  ecx
  905. ;    pop  ebx
  906. .ok:
  907.     mov  eax,1
  908.     ret
  909. ;
  910. ;.failed1:
  911. ;    pop  edx
  912. ;    pop  ecx
  913. ;    pop  ebx
  914. .failed:
  915.     xor  eax,eax
  916.     ret
  917.  
  918. align 4
  919. proc read_process_memory
  920. ;Input:
  921. ;  eax - process slot
  922. ;  ebx - buffer address
  923. ;  ecx - buffer size
  924. ;  edx - start address in other process
  925. ;Output:
  926. ;  eax - number of bytes read.
  927.            locals
  928.              slot       dd ?
  929.              buff       dd ?
  930.              r_count    dd ?
  931.              offset     dd ?
  932.              tmp_r_cnt  dd ?
  933.            endl
  934.  
  935.            mov [slot], eax
  936.            mov [buff], ebx
  937.            mov [r_count], ecx
  938.            mov [tmp_r_cnt], ecx
  939.            mov [offset], edx
  940.  
  941.            pushad
  942. .read_mem:
  943.            mov edx, [offset]
  944.            mov ebx, [tmp_r_cnt]
  945.  
  946.            mov ecx, 0x400000
  947.            and edx, 0x3FFFFF
  948.            sub ecx, edx
  949.            cmp ecx, ebx
  950.            jbe @f
  951.            mov ecx, ebx
  952. @@:
  953.            cmp ecx, 0x8000
  954.            jna @F
  955.            mov ecx, 0x8000
  956. @@:
  957.            mov eax, [slot]
  958.            shl  eax,8
  959.            mov ebx, [offset]
  960.            add ebx, new_app_base
  961.            push ecx
  962.            stdcall map_memEx, [proc_mem_map],\
  963.                               [PROC_BASE+eax+0xB8],\
  964.                               ebx, ecx
  965.            pop ecx
  966.  
  967.            mov esi, [offset]
  968.            and esi, 0xfff
  969.            add esi, [proc_mem_map]
  970.            mov edi, [buff]
  971.            mov edx, ecx
  972.            rep movsb
  973.  
  974.            add [offset], edx
  975.            sub [tmp_r_cnt], edx
  976.            jnz .read_mem
  977.  
  978.            popad
  979.            mov eax, [r_count]
  980.            ret
  981.  
  982. endp
  983.  
  984. align 4
  985. proc write_process_memory
  986. ;Input:
  987. ;  eax - process slot
  988. ;  ebx - buffer address
  989. ;  ecx - buffer size
  990. ;  edx - start address in other process
  991. ;Output:
  992. ;  eax - number of bytes written
  993.  
  994.            locals
  995.              slot       dd ?
  996.              buff       dd ?
  997.              w_count    dd ?
  998.              offset     dd ?
  999.              tmp_w_cnt  dd ?
  1000.            endl
  1001.  
  1002.            mov [slot], eax
  1003.            mov [buff], ebx
  1004.            mov [w_count], ecx
  1005.            mov [tmp_w_cnt], ecx
  1006.            mov [offset], edx
  1007.  
  1008.            pushad
  1009. .read_mem:
  1010.            mov edx, [offset]
  1011.            mov ebx, [tmp_w_cnt]
  1012.  
  1013.            mov ecx, 0x400000
  1014.            and edx, 0x3FFFFF
  1015.            sub ecx, edx
  1016.            cmp ecx, ebx
  1017.            jbe @f
  1018.            mov ecx, ebx
  1019. @@:
  1020.            cmp ecx, 0x8000
  1021.            jna @F
  1022.            mov ecx, 0x8000
  1023. @@:
  1024.            mov eax, [slot]
  1025.            shl  eax,8
  1026.            mov ebx, [offset]
  1027.            add ebx, new_app_base
  1028.            push ecx
  1029.            stdcall map_memEx, [proc_mem_map],\
  1030.                               [PROC_BASE+eax+0xB8],\
  1031.                               ebx, ecx
  1032.            pop ecx
  1033.  
  1034.            mov edi, [offset]
  1035.            and edi, 0xfff
  1036.            add edi, [proc_mem_map]
  1037.            mov esi, [buff]
  1038.            mov edx, ecx
  1039.            rep movsb
  1040.  
  1041.            add [offset], edx
  1042.            sub [tmp_w_cnt], edx
  1043.            jnz .read_mem
  1044.  
  1045.            popad
  1046.            mov eax, [w_count]
  1047.            ret
  1048. endp
  1049.  
  1050.  
  1051. align 4
  1052. proc new_sys_threads
  1053.            locals
  1054.              thread_start  dd ?
  1055.              thread_stack  dd ?
  1056.              params        dd ?
  1057.              slot          dd ?
  1058.            endl
  1059.  
  1060.            mov [thread_start], ebx
  1061.            mov [thread_stack], ecx
  1062.            mov [params], 0
  1063.  
  1064.            xor    edx,edx      ; flags=0
  1065.  
  1066.            cmp    eax,1
  1067.            jnz    .failed                  ;other subfunctions
  1068.            mov    esi,new_process_loading
  1069.            call   sys_msg_board_str
  1070.  
  1071. .wait_lock:
  1072.            cmp [application_table_status],0
  1073.            je .get_lock
  1074.            call   change_task
  1075.            jmp .wait_lock
  1076.  
  1077. .get_lock:
  1078.            mov eax, 1
  1079.            xchg eax, [application_table_status]
  1080.            cmp eax, 0
  1081.            jne .wait_lock
  1082.  
  1083.            call   set_application_table_status
  1084.  
  1085.            call get_new_process_place
  1086.            test eax, eax
  1087.            jz .failed
  1088.  
  1089.            mov [slot], eax
  1090.  
  1091.            xor    eax,eax
  1092.            mov    [app_i_param],eax
  1093.            mov    [app_i_icon],eax
  1094.  
  1095.            mov ebx, [thread_start]
  1096.            mov ecx, [thread_stack]
  1097.  
  1098.            mov    [app_start],ebx
  1099.            mov    [app_esp],ecx
  1100.  
  1101.            mov    esi,[CURRENT_TASK]
  1102.            shl    esi,8
  1103.            add    esi,PROC_BASE
  1104.            mov    ebx,esi               ;ebx=esi - pointer to extended information about current thread
  1105.  
  1106.            mov    edi,[slot]
  1107.            shl    edi,8
  1108.            add    edi,PROC_BASE
  1109.            mov    edx,edi               ;edx=edi - pointer to extended infomation about new thread
  1110.            mov    ecx,256/4
  1111.            rep    stosd                 ;clean extended information about new thread
  1112.            mov    edi,edx
  1113.            mov    ecx,11
  1114.            rep    movsb                 ;copy process name
  1115.            mov    eax,[ebx+APPDATA.heap_base]
  1116.            mov    [edx+APPDATA.heap_base], eax
  1117.            mov    ecx,[ebx+APPDATA.heap_top]
  1118.            mov    [edx+APPDATA.heap_top], ecx
  1119.            mov    eax,[ebx+APPDATA.mem_size]
  1120.            mov    [app_mem],eax         ;set memory size
  1121.            mov    eax,[ebx+0xb8]
  1122.            mov    [edx+0xb8],eax        ;copy page directory
  1123.  
  1124.            stdcall add_app_parameters, [slot], new_app_base,\
  1125.                                         [params], dword 0,dword 0
  1126.  
  1127.            mov    esi,new_process_running
  1128.            call   sys_msg_board_str     ;output information about succefull startup
  1129.  
  1130.            mov    [application_table_status],0 ;unlock application_table_status mutex
  1131.            mov    eax,[process_number]  ;set result
  1132.            ret
  1133. .failed:
  1134.            mov    [application_table_status],0
  1135.            mov    eax,-1
  1136.            ret
  1137. endp
  1138.  
  1139. align 4
  1140. proc wait_mutex stdcall, mutex:dword
  1141.            mov ebx, [mutex]
  1142. .wait_lock:
  1143.            cmp dword [ebx],0
  1144.            je .get_lock
  1145.            push ebx
  1146.            call change_task
  1147.            pop ebx
  1148.            jmp .wait_lock
  1149.  
  1150. .get_lock:
  1151.            mov eax, 1
  1152.            xchg eax, [ebx]
  1153.            test eax, eax
  1154.            jnz .wait_lock
  1155.            ret
  1156. endp
  1157.  
  1158.  
  1159. include "debug.inc"
  1160.  
  1161. iglobal
  1162.     new_process_loading db 'K : New Process - loading',13,10,0
  1163.     new_process_running db 'K : New Process - done',13,10,0
  1164.     start_not_enough_memory db 'K : New Process - not enough memory',13,10,0
  1165. endg
  1166.  
  1167.