Subversion Repositories Kolibri OS

Rev

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

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