Subversion Repositories Kolibri OS

Rev

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