Subversion Repositories Kolibri OS

Rev

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

  1. GREEDY_KERNEL  equ 0
  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 edi, [slot]
  608.            mov esi, [fpu_data]
  609.            bt [cpu_caps], CAPS_SSE
  610.            jnc .no_SSE
  611.  
  612.            shl edi, 8
  613.            mov eax, edi
  614.            lea edi, [esi+edi*2]
  615.            mov [eax+PROC_BASE+APPDATA.fpu_state], edi
  616.            mov [eax+PROC_BASE+APPDATA.fpu_handler], 0
  617.            mov [eax+PROC_BASE+APPDATA.sse_handler], 0
  618.            mov ecx, 512/4
  619.            jmp @F
  620. .no_SSE:
  621.            mov eax, edi
  622.            shl eax, 8
  623.            mov ebx, edi
  624.            shl edi, 7
  625.            shl ebx, 4
  626.            sub edi, ebx       ;edi*=112
  627.            add edi, esi
  628.            mov [eax+PROC_BASE+APPDATA.fpu_state], edi
  629.            mov [eax+PROC_BASE+APPDATA.fpu_handler], 0
  630.            mov [eax+PROC_BASE+APPDATA.sse_handler], 0
  631.            mov ecx, 112/4
  632. @@:
  633.            rep movsd
  634.  
  635.            mov    ebx,[slot]
  636.            cmp    ebx,[TASK_COUNT]
  637.            jle    .noinc
  638.            inc    dword [TASK_COUNT]       ;update number of processes
  639. .noinc:
  640.            shl    ebx,8
  641.            mov    eax,[app_mem]
  642.            mov    [PROC_BASE+APPDATA.mem_size+ebx],eax
  643.  
  644.            mov    ecx, [def_cursor]
  645.            mov    [PROC_BASE+APPDATA.cursor+ebx],ecx
  646.  
  647.            shr    ebx,3
  648.            mov eax, new_app_base
  649.            mov    dword [CURRENT_TASK+ebx+0x10],eax
  650.  
  651. .add_command_line:
  652.            mov    edx,[app_i_param]
  653.            test   edx,edx
  654.            jz     .no_command_line      ;application don't need parameters
  655.            mov    eax,[cmd_line]
  656.            test   eax,eax
  657.            jz     .no_command_line      ;no parameters specified
  658. ;calculate parameter length
  659.            xor    ecx,ecx
  660. .command_line_len:
  661.            cmp    byte [eax],0
  662.            jz     .command_line_len_end
  663.            inc    eax
  664.            inc    ecx
  665.            cmp    ecx,255
  666.            jl     .command_line_len
  667.  
  668. .command_line_len_end:
  669. ;ecx - parameter length
  670. ;edx - address of parameters in new process address space
  671.            inc ecx
  672.            mov edi, [img_base]
  673.            add edi, edx
  674.            mov esi, [cmd_line]
  675.            rep movsb
  676.  
  677. .no_command_line:
  678.  
  679.             mov    edx,[app_i_icon]
  680.             test   edx,edx
  681.             jz     .no_command_line_1      ;application don't need path of file
  682.             mov    esi,[app_path]
  683.             test esi, esi
  684.             jz     .no_command_line_1      ;application don't need path of file
  685.             mov    ecx, 64
  686.             mov    edi, [img_base]
  687.             add edi, edx
  688.             rep movsb
  689.  
  690. .no_command_line_1:
  691.            mov    ebx,[slot]
  692.            mov    eax,ebx
  693.            shl    ebx,5
  694. ; set window state to 'normal' (non-minimized/maximized/rolled-up) state
  695.         mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
  696.         mov     [ebx+window_data+WDATA.fl_redraw], 1
  697.            add    ebx,CURRENT_TASK            ;ebx - pointer to information about process
  698.            mov    [ebx+TASKDATA.wnd_number],al;set window number on screen = process slot
  699.  
  700.            mov    [ebx+TASKDATA.event_mask],dword 1+2+4 ;set default event flags (see 40 function)
  701.  
  702.            inc    dword [process_number]
  703.            mov    eax,[process_number]
  704.            mov    [ebx+4],eax           ;set PID
  705.  
  706.            mov    ecx,ebx
  707.            add    ecx,(draw_data-CURRENT_TASK)  ;ecx - pointer to draw data
  708. ;set draw data to full screen
  709.  
  710.            mov    [ecx+0],dword 0
  711.            mov    [ecx+4],dword 0
  712.            mov    eax,[SCR_X_SIZE]
  713.            mov    [ecx+8],eax
  714.            mov    eax,[SCR_Y_SIZE]
  715.            mov    [ecx+12],eax
  716. ;set cr3 register in TSS of application
  717.  
  718.            mov    ecx,[slot]
  719.            shl    ecx,8
  720.            mov    eax,[PROC_BASE+0xB8+ecx]
  721.            ;or     eax,  PG_NOCACHE
  722.            mov    [l.cr3],eax
  723.  
  724.            mov    eax,[app_start]
  725.            mov    [l.eip],eax           ;set eip in TSS
  726.            mov    eax,[app_esp]
  727.            mov    [l.esp],eax           ;set stack in TSS
  728.  
  729. ;gdt
  730.            mov    ax,app_code           ;ax - selector of code segment
  731.            mov    [l.cs],ax
  732.            mov    ax,app_data
  733.            mov    [l.ss],ax
  734.            mov    [l.ds],ax
  735.            mov    [l.es],ax
  736.            mov    [l.fs],ax
  737.            mov    ax,graph_data         ;ax - selector of graphic segment
  738.            mov    [l.gs],ax
  739.            mov    [l.io],word 128
  740.            mov    [l.eflags],dword 0x1202
  741.  
  742.            mov    [l.ss0],os_data
  743.            mov    ebx,[slot]
  744.            shl    ebx,12
  745.            add    ebx,sysint_stack_data+4096
  746.            mov    [l.esp0],ebx
  747.  
  748. ;copy tss to it place
  749.            mov    eax,tss_sceleton
  750.            mov    ebx,[slot]
  751.            imul   ebx,tss_step
  752.            add    ebx,tss_data          ;ebx - address of application TSS
  753.            mov    ecx,120
  754.            call   memmove
  755.  
  756. ;Add IO access table - bit array of permitted ports
  757.            or     eax,-1
  758.            mov    edi,[slot]
  759.            imul   edi,tss_step
  760.            add    edi,tss_data+128
  761.            mov    ecx,2048
  762.            cld
  763.            rep    stosd                 ;full access to 2048*8=16384 ports
  764.  
  765.            mov    ecx,ebx               ;ecx - address of application TSS
  766.            mov    edi,[slot]
  767.            shl    edi,3
  768. ;set TSS descriptor
  769.            mov    [edi+gdts+tss0+0],word tss_step ;limit (size)
  770.            mov    [edi+gdts+tss0+2],cx  ;part of offset
  771.            mov    eax,ecx
  772.            shr    eax,16
  773.            mov    [edi+gdts+tss0+4],al  ;part of offset
  774.            mov    [edi+gdts+tss0+7],ah  ;part of offset
  775.            mov    [edi+gdts+tss0+5],word 01010000b*256+11101001b ;system flags
  776.  
  777. ;flush keyboard and buttons queue
  778.            mov    [KEY_COUNT],byte 0
  779.            mov    [BTN_COUNT],byte 0
  780.  
  781.            mov    edi,[slot]
  782.            shl    edi,5
  783.            add    edi,window_data
  784.            mov    ebx,[slot]
  785.            movzx  esi,word [WIN_STACK+ebx*2]
  786.            lea    esi,[WIN_POS+esi*2]
  787.            call   windowactivate        ;gui initialization
  788.  
  789.            mov    ebx,[slot]
  790.            shl    ebx,5
  791.            mov    [CURRENT_TASK+ebx+0xa],byte 0 ;set process state - running
  792. ; set if debuggee
  793.            mov eax, [flags]
  794.            test   byte [flags], 1
  795.            jz     .no_debug
  796.            mov    [CURRENT_TASK+ebx+0xa],byte 1 ;set process state - suspended
  797.            mov    eax,[CURRENT_TASK]
  798.            mov    [PROC_BASE+ebx*8+0xac],eax ;set debugger PID - current
  799. .no_debug:
  800.  
  801.            mov    esi,new_process_running
  802.            call   sys_msg_board_str     ;output information about succefull startup
  803.  
  804.            ret
  805. endp
  806.  
  807. pid_to_slot:
  808. ;Input:
  809. ;  eax - pid of process
  810. ;Output:
  811. ;  eax - slot of process or 0 if process don't exists
  812. ;Search process by PID.
  813.     push   ebx
  814.     push   ecx
  815.     mov    ebx,[TASK_COUNT]
  816.     shl    ebx,5
  817.     mov    ecx,2*32
  818.  
  819. .loop:
  820. ;ecx=offset of current process info entry
  821. ;ebx=maximum permitted offset
  822.     cmp    byte [CURRENT_TASK+ecx+0xa],9
  823.     jz     .endloop              ;skip empty slots
  824.     cmp    [CURRENT_TASK+ecx+0x4],eax ;check PID
  825.     jz     .pid_found
  826. .endloop:
  827.     add    ecx,32
  828.     cmp    ecx,ebx
  829.     jle    .loop
  830.  
  831.     pop    ecx
  832.     pop    ebx
  833.     xor    eax,eax
  834.     ret
  835.  
  836. .pid_found:
  837.     shr    ecx,5
  838.     mov    eax,ecx               ;convert offset to index of slot
  839.     pop    ecx
  840.     pop    ebx
  841.     ret
  842.  
  843. check_region:
  844. ;input:
  845. ;  ebx - start of buffer
  846. ;  ecx - size of buffer
  847. ;result:
  848. ;  eax = 1 region lays in app memory
  849. ;  eax = 0 region don't lays in app memory
  850.      mov  eax,[CURRENT_TASK]
  851.      jmp  check_process_region
  852. ;-----------------------------------------------------------------------------
  853. check_process_region:
  854. ;input:
  855. ;  eax - slot
  856. ;  ebx - start of buffer
  857. ;  ecx - size of buffer
  858. ;result:
  859. ;  eax = 1 region lays in app memory
  860. ;  eax = 0 region don't lays in app memory
  861.  
  862.      test ecx,ecx
  863.      jle  .ok
  864.      shl  eax,5
  865.      cmp  word [CURRENT_TASK+eax+0xa],0
  866.      jnz  .failed
  867.      shl  eax,3
  868.      mov  eax,[PROC_BASE+eax+0xb8]
  869.      test eax,eax
  870.      jz   .failed
  871.  
  872.      mov  eax,1
  873.      ret
  874.  
  875.  
  876. ;    call MEM_Get_Linear_Address
  877. ;    push ebx
  878. ;    push ecx
  879. ;    push edx
  880. ;    mov  edx,ebx
  881. ;    and  edx,not (4096-1)
  882. ;    sub  ebx,edx
  883. ;    add  ecx,ebx
  884. ;    mov  ebx,edx
  885. ;    add  ecx,(4096-1)
  886. ;    and  ecx,not (4096-1)
  887. ;.loop:
  888. ;;eax - linear address of page directory
  889. ;;ebx - current page
  890. ;;ecx - current size
  891. ;    mov  edx,ebx
  892. ;    shr  edx,22
  893. ;    mov  edx,[eax+4*edx]
  894. ;    and  edx,not (4096-1)
  895. ;    test edx,edx
  896. ;    jz   .failed1
  897. ;    push eax
  898. ;    mov  eax,edx
  899. ;    call MEM_Get_Linear_Address
  900. ;    mov  edx,ebx
  901. ;    shr  edx,12
  902. ;    and  edx,(1024-1)
  903. ;    mov  eax,[eax+4*edx]
  904. ;    and  eax,not (4096-1)
  905. ;    test eax,eax
  906. ;    pop  eax
  907. ;    jz   .failed1
  908. ;    add  ebx,4096
  909. ;    sub  ecx,4096
  910. ;    jg   .loop
  911. ;    pop  edx
  912. ;    pop  ecx
  913. ;    pop  ebx
  914. .ok:
  915.     mov  eax,1
  916.     ret
  917. ;
  918. ;.failed1:
  919. ;    pop  edx
  920. ;    pop  ecx
  921. ;    pop  ebx
  922. .failed:
  923.     xor  eax,eax
  924.     ret
  925.  
  926. align 4
  927. proc read_process_memory
  928. ;Input:
  929. ;  eax - process slot
  930. ;  ebx - buffer address
  931. ;  ecx - buffer size
  932. ;  edx - start address in other process
  933. ;Output:
  934. ;  eax - number of bytes read.
  935.            locals
  936.              slot       dd ?
  937.              buff       dd ?
  938.              r_count    dd ?
  939.              offset     dd ?
  940.              tmp_r_cnt  dd ?
  941.            endl
  942.  
  943.            mov [slot], eax
  944.            mov [buff], ebx
  945.            mov [r_count], ecx
  946.            mov [tmp_r_cnt], ecx
  947.            mov [offset], edx
  948.  
  949.            pushad
  950. .read_mem:
  951.            mov edx, [offset]
  952.            mov ebx, [tmp_r_cnt]
  953.  
  954.            mov ecx, 0x400000
  955.            and edx, 0x3FFFFF
  956.            sub ecx, edx
  957.            cmp ecx, ebx
  958.            jbe @f
  959.            mov ecx, ebx
  960. @@:
  961.            cmp ecx, 0x8000
  962.            jna @F
  963.            mov ecx, 0x8000
  964. @@:
  965.            mov eax, [slot]
  966.            shl  eax,8
  967.            mov ebx, [offset]
  968.            add ebx, new_app_base
  969.            push ecx
  970.            stdcall map_memEx, [proc_mem_map],\
  971.                               [PROC_BASE+eax+0xB8],\
  972.                               ebx, ecx
  973.            pop ecx
  974.  
  975.            mov esi, [offset]
  976.            and esi, 0xfff
  977.            add esi, [proc_mem_map]
  978.            mov edi, [buff]
  979.            mov edx, ecx
  980.            rep movsb
  981.  
  982.            add [offset], edx
  983.            sub [tmp_r_cnt], edx
  984.            jnz .read_mem
  985.  
  986.            popad
  987.            mov eax, [r_count]
  988.            ret
  989.  
  990. endp
  991.  
  992. align 4
  993. proc write_process_memory
  994. ;Input:
  995. ;  eax - process slot
  996. ;  ebx - buffer address
  997. ;  ecx - buffer size
  998. ;  edx - start address in other process
  999. ;Output:
  1000. ;  eax - number of bytes written
  1001.  
  1002.            locals
  1003.              slot       dd ?
  1004.              buff       dd ?
  1005.              w_count    dd ?
  1006.              offset     dd ?
  1007.              tmp_w_cnt  dd ?
  1008.            endl
  1009.  
  1010.            mov [slot], eax
  1011.            mov [buff], ebx
  1012.            mov [w_count], ecx
  1013.            mov [tmp_w_cnt], ecx
  1014.            mov [offset], edx
  1015.  
  1016.            pushad
  1017. .read_mem:
  1018.            mov edx, [offset]
  1019.            mov ebx, [tmp_w_cnt]
  1020.  
  1021.            mov ecx, 0x400000
  1022.            and edx, 0x3FFFFF
  1023.            sub ecx, edx
  1024.            cmp ecx, ebx
  1025.            jbe @f
  1026.            mov ecx, ebx
  1027. @@:
  1028.            cmp ecx, 0x8000
  1029.            jna @F
  1030.            mov ecx, 0x8000
  1031. @@:
  1032.            mov eax, [slot]
  1033.            shl  eax,8
  1034.            mov ebx, [offset]
  1035.            add ebx, new_app_base
  1036.            push ecx
  1037.            stdcall map_memEx, [proc_mem_map],\
  1038.                               [PROC_BASE+eax+0xB8],\
  1039.                               ebx, ecx
  1040.            pop ecx
  1041.  
  1042.            mov edi, [offset]
  1043.            and edi, 0xfff
  1044.            add edi, [proc_mem_map]
  1045.            mov esi, [buff]
  1046.            mov edx, ecx
  1047.            rep movsb
  1048.  
  1049.            add [offset], edx
  1050.            sub [tmp_w_cnt], edx
  1051.            jnz .read_mem
  1052.  
  1053.            popad
  1054.            mov eax, [w_count]
  1055.            ret
  1056. endp
  1057.  
  1058.  
  1059. align 4
  1060. proc new_sys_threads
  1061.            locals
  1062.              thread_start  dd ?
  1063.              thread_stack  dd ?
  1064.              params        dd ?
  1065.              slot          dd ?
  1066.            endl
  1067.  
  1068.            mov [thread_start], ebx
  1069.            mov [thread_stack], ecx
  1070.            mov [params], 0
  1071.  
  1072.            xor    edx,edx      ; flags=0
  1073.  
  1074.            cmp    eax,1
  1075.            jnz    .failed                  ;other subfunctions
  1076.            mov    esi,new_process_loading
  1077.            call   sys_msg_board_str
  1078.  
  1079. .wait_lock:
  1080.            cmp [application_table_status],0
  1081.            je .get_lock
  1082.            call   change_task
  1083.            jmp .wait_lock
  1084.  
  1085. .get_lock:
  1086.            mov eax, 1
  1087.            xchg eax, [application_table_status]
  1088.            cmp eax, 0
  1089.            jne .wait_lock
  1090.  
  1091.            call   set_application_table_status
  1092.  
  1093.            call get_new_process_place
  1094.            test eax, eax
  1095.            jz .failed
  1096.  
  1097.            mov [slot], eax
  1098.  
  1099.            xor    eax,eax
  1100.            mov    [app_i_param],eax
  1101.            mov    [app_i_icon],eax
  1102.  
  1103.            mov ebx, [thread_start]
  1104.            mov ecx, [thread_stack]
  1105.  
  1106.            mov    [app_start],ebx
  1107.            mov    [app_esp],ecx
  1108.  
  1109.            mov    esi,[CURRENT_TASK]
  1110.            shl    esi,8
  1111.            add    esi,PROC_BASE
  1112.            mov    ebx,esi               ;ebx=esi - pointer to extended information about current thread
  1113.  
  1114.            mov    edi,[slot]
  1115.            shl    edi,8
  1116.            add    edi,PROC_BASE
  1117.            mov    edx,edi               ;edx=edi - pointer to extended infomation about new thread
  1118.            mov    ecx,256/4
  1119.            rep    stosd                 ;clean extended information about new thread
  1120.            mov    edi,edx
  1121.            mov    ecx,11
  1122.            rep    movsb                 ;copy process name
  1123.            mov    eax,[ebx+APPDATA.heap_base]
  1124.            mov    [edx+APPDATA.heap_base], eax
  1125.            mov    ecx,[ebx+APPDATA.heap_top]
  1126.            mov    [edx+APPDATA.heap_top], ecx
  1127.            mov    eax,[ebx+APPDATA.mem_size]
  1128.            mov    [app_mem],eax         ;set memory size
  1129.            mov    eax,[ebx+0xb8]
  1130.            mov    [edx+0xb8],eax        ;copy page directory
  1131.  
  1132.            stdcall add_app_parameters, [slot], new_app_base,\
  1133.                                         [params], dword 0,dword 0
  1134.  
  1135.            mov    esi,new_process_running
  1136.            call   sys_msg_board_str     ;output information about succefull startup
  1137.  
  1138.            mov    [application_table_status],0 ;unlock application_table_status mutex
  1139.            mov    eax,[process_number]  ;set result
  1140.            ret
  1141. .failed:
  1142.            mov    [application_table_status],0
  1143.            mov    eax,-1
  1144.            ret
  1145. endp
  1146.  
  1147. align 4
  1148. proc wait_mutex stdcall, mutex:dword
  1149.            mov ebx, [mutex]
  1150. .wait_lock:
  1151.            cmp dword [ebx],0
  1152.            je .get_lock
  1153.            push ebx
  1154.            call change_task
  1155.            pop ebx
  1156.            jmp .wait_lock
  1157.  
  1158. .get_lock:
  1159.            mov eax, 1
  1160.            xchg eax, [ebx]
  1161.            test eax, eax
  1162.            jnz .wait_lock
  1163.            ret
  1164. endp
  1165.  
  1166.  
  1167. include "debug.inc"
  1168.  
  1169. iglobal
  1170.     new_process_loading db 'K : New Process - loading',13,10,0
  1171.     new_process_running db 'K : New Process - done',13,10,0
  1172.     start_not_enough_memory db 'K : New Process - not enough memory',13,10,0
  1173. endg
  1174.  
  1175.