Subversion Repositories Kolibri OS

Rev

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