Subversion Repositories Kolibri OS

Rev

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