Subversion Repositories Kolibri OS

Rev

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

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