Subversion Repositories Kolibri OS

Rev

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