Subversion Repositories Kolibri OS

Rev

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

  1. $Revision: 425 $
  2. GREEDY_KERNEL  equ 0
  3.  
  4.  
  5. struc APP_HEADER_00
  6. { .banner      dq ?
  7.   .version     dd ?    ;+8
  8.   .start       dd ?    ;+12
  9.   .i_end       dd ?    ;+16
  10.   .mem_size    dd ?    ;+20
  11.   .i_param     dd ?    ;+24
  12. }
  13.  
  14. struc APP_HEADER_01
  15. { .banner      dq ?
  16.   .version     dd ?    ;+8
  17.   .start       dd ?    ;+12
  18.   .i_end       dd ?    ;+16
  19.   .mem_size    dd ?    ;+20
  20.   .stack_top   dd ?    ;+24
  21.   .i_param     dd ?    ;+28
  22.   .i_icon      dd ?    ;+32
  23. }
  24.  
  25. struc TSS
  26. {
  27.   ._back   rw 2
  28.   ._esp0   rd 1
  29.   ._ss0    rw 2
  30.   ._esp1   rd 1
  31.   ._ss1    rw 2
  32.   ._esp2   rd 1
  33.   ._ss2    rw 2
  34.   ._cr3    rd 1
  35.   ._eip    rd 1
  36.   ._eflags rd 1
  37.   ._eax    rd 1
  38.   ._ecx    rd 1
  39.   ._edx    rd 1
  40.   ._ebx    rd 1
  41.   ._esp    rd 1
  42.   ._ebp    rd 1
  43.   ._esi    rd 1
  44.   ._edi    rd 1
  45.   ._es     rw 2
  46.   ._cs     rw 2
  47.   ._ss     rw 2
  48.   ._ds     rw 2
  49.   ._fs     rw 2
  50.   ._gs     rw 2
  51.   ._ldt    rw 2
  52.   ._trap   rw 1
  53.   ._io     rw 1
  54. }
  55.  
  56. virtual at 0
  57.   TSS  TSS
  58. end virtual
  59.  
  60. struc APP_PARAMS
  61. { .app_cmdline   ;0x00
  62.   .app_path      ;0x04
  63.   .app_eip       ;0x08
  64.   .app_esp       ;0x0C
  65.   .app_mem       ;0x10
  66. }
  67.  
  68. macro _clear_ op
  69. {  mov ecx, op/4
  70.    xor eax, eax
  71.    cld
  72.    rep stosd
  73. }
  74.  
  75.  
  76. align 4
  77. proc fs_execute
  78.  
  79. ;fn_read:dword, file_size:dword, cluster:dword
  80.  
  81. ; ebx - cmdline
  82. ; edx - flags
  83. ; ebp - full filename
  84. ; [esp+4] = procedure DoRead, [esp+8] = filesize & [esp+12]... - arguments for it
  85.  
  86.            locals
  87.              cmdline       rd 64        ;256/4
  88.              filename      rd 256       ;1024/4
  89.              flags         dd ?
  90.  
  91.              save_cr3      dd ?
  92.              slot          dd ?
  93.              slot_base     dd ?
  94.              file_base     dd ?
  95.              file_size     dd ?
  96.                                           ;app header data
  97.              hdr_cmdline   dd ? ;0x00
  98.              hdr_path      dd ? ;0x04
  99.              hdr_eip       dd ? ;0x08
  100.              hdr_esp       dd ? ;0x0C
  101.              hdr_mem       dd ? ;0x10
  102.              hdr_i_end     dd ? ;0x14
  103.            endl
  104.  
  105.            pushad
  106.  
  107.            mov [cmdline], ebx
  108.            mov [flags], edx
  109.  
  110. ; [ebp]  pointer to filename
  111.  
  112.            lea eax, [filename]
  113.            mov dword [eax+1020],0              ;force terminate
  114.                                                ;string
  115.            stdcall k_strncpy, eax, [ebp], 1023
  116.  
  117.            lea eax, [cmdline]
  118.            mov dword [eax+252], 0
  119.            stdcall k_strncpy, eax, [cmdline], 255
  120.  
  121.            lea eax, [filename]
  122.            stdcall load_file, eax
  123.            mov  ecx, -ERROR_FILE_NOT_FOUND
  124.            test eax, eax
  125.            jz .err_file
  126.  
  127.            mov [file_base], eax
  128.            mov [file_size], ebx
  129.  
  130.            lea ebx, [hdr_cmdline]
  131.            call test_app_header
  132.            mov ecx, -0x1F
  133.            test eax, eax
  134.            jz .err_hdr
  135.  
  136.            mov esi, new_process_loading
  137.            call sys_msg_board_str       ; write message to message board
  138.  
  139. .wait_lock:
  140.            cmp [application_table_status],0
  141.            je .get_lock
  142.            call   change_task
  143.            jmp .wait_lock
  144.  
  145. .get_lock:
  146.            mov eax, 1
  147.            xchg eax, [application_table_status]
  148.            cmp eax, 0
  149.            jne .wait_lock
  150.  
  151. ;           pushfd
  152. ;           cli
  153.  
  154.            call set_application_table_status
  155.  
  156.            call get_new_process_place
  157.            test eax, eax
  158.            mov ecx, -0x20      ; too many processes
  159.            jz .err
  160.  
  161.            mov [slot], eax
  162.            shl eax, 8
  163.            add eax, SLOT_BASE
  164.            mov [slot_base], eax
  165.            mov edi, eax
  166.            _clear_ 256     ;clean extended information about process
  167.  
  168. ; write application name
  169.            lea edi, [filename]
  170.            mov al, '/'
  171.            call k_strrchr  ; now eax points to name without path
  172.  
  173.            lea esi, [eax+1]
  174.            test eax, eax
  175.            jnz @F
  176.            lea esi, [filename]
  177. @@:
  178.            mov ecx, 8  ; 8 chars for name
  179.            mov edi, [slot_base]
  180. .copy_process_name_loop:
  181.            lodsb
  182.            cmp al, '.'
  183.            jz .copy_process_name_done
  184.            test al, al
  185.            jz .copy_process_name_done
  186.            stosb
  187.            loop .copy_process_name_loop
  188. .copy_process_name_done:
  189.  
  190.            mov ebx, cr3
  191.            mov [save_cr3], ebx
  192.  
  193.            stdcall create_app_space,[hdr_mem],[file_base],[file_size]
  194.            mov ecx, -30  ; no memory
  195.            test eax, eax
  196.            jz .failed
  197.  
  198.            mov   ebx,[slot_base]
  199.            mov   [ebx+APPDATA.dir_table],eax
  200.            mov   eax,[hdr_mem]
  201.            mov   [ebx+APPDATA.mem_size],eax
  202.  
  203. if GREEDY_KERNEL
  204. else
  205.            mov ecx, [hdr_mem]
  206.            mov edi, [file_size]
  207.            add edi, 4095
  208.            and edi, not 4095
  209.            sub ecx, edi
  210.            jna @F
  211.  
  212.            xor eax, eax
  213.            add edi, new_app_base
  214.            cld
  215.            rep stosb
  216. @@:
  217. end if
  218.  
  219. ; release only virtual space, not phisical memory
  220.  
  221.            stdcall free_kernel_space, [file_base]
  222.            lea eax, [hdr_cmdline]
  223.            lea ebx, [cmdline]
  224.            lea ecx, [filename]
  225.            stdcall set_app_params ,[slot],eax,ebx,ecx,[flags]
  226.  
  227.            mov eax, [save_cr3]
  228.            call set_cr3
  229.  
  230.            xor ebx, ebx
  231.            mov [application_table_status],ebx ;unlock application_table_status mutex
  232.            mov eax,[process_number]  ;set result
  233.            ret
  234. .failed:
  235.            mov eax, [save_cr3]
  236.            call set_cr3
  237. .err:
  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.  
  287. ; \begin{diamond}[20.08.2006]
  288. ; sanity check (functions 19,58 load app_i_end bytes and that must
  289. ; fit in allocated memory to prevent kernel faults)
  290.            cmp  edx,[APP_HEADER_01.i_end]
  291.            jb   .fail
  292. ; \end{diamond}[20.08.2006]
  293.  
  294.            mov  [ebx+0x10], edx                ;app_mem
  295.            mov  ecx,[APP_HEADER_01.stack_top]
  296.            mov  [ebx+0x0C], ecx                ;app_esp
  297.            mov  edx,[APP_HEADER_01.i_param]
  298.            mov  [ebx], edx                     ;app_cmdline
  299.            mov  ecx,[APP_HEADER_01.i_icon]
  300.            mov  [ebx+4], ecx                   ;app_path
  301.            mov  edx, [APP_HEADER_01.i_end]
  302.            mov  [ebx+0x14], edx
  303.            ret
  304. .fail:
  305.            xor eax, eax
  306.            ret
  307.  
  308. align 4
  309. proc get_new_process_place
  310. ;input:
  311. ;  none
  312. ;result:
  313. ;  eax=[new_process_place]<>0 - ok
  314. ;      0 - failed.
  315. ;This function find least empty slot.
  316. ;It doesn't increase [TASK_COUNT]!
  317.            mov    eax,CURRENT_TASK
  318.            mov    ebx,[TASK_COUNT]
  319.            inc    ebx
  320.            shl    ebx,5
  321.            add    ebx,eax               ;ebx - address of process information for (last+1) slot
  322. .newprocessplace:
  323. ;eax = address of process information for current slot
  324.            cmp    eax,ebx
  325.            jz     .endnewprocessplace   ;empty slot after high boundary
  326.            add    eax,0x20
  327.            cmp    word [eax+0xa],9      ;check process state, 9 means that process slot is empty
  328.            jnz    .newprocessplace
  329. .endnewprocessplace:
  330.            mov    ebx,eax
  331.            sub    eax,CURRENT_TASK
  332.            shr    eax,5                 ;calculate slot index
  333.            cmp    eax,256
  334.            jge    .failed               ;it should be <256
  335.            mov    word [ebx+0xa],9      ;set process state to 9 (for slot after hight boundary)
  336.            ret
  337. .failed:
  338.            xor    eax,eax
  339.            ret
  340. endp
  341.  
  342. align 4
  343. proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword
  344.            locals
  345.              app_pages   dd ?
  346.              img_pages   dd ?
  347.              dir_addr    dd ?
  348.              app_tabs    dd ?
  349.            endl
  350.  
  351.            mov ebx, pg_data.pg_mutex
  352.            call wait_mutex   ;ebx
  353.  
  354.            xor eax, eax
  355.            mov [dir_addr], eax
  356.  
  357.            mov eax, [app_size]
  358.            add eax, 4095
  359.            and eax, NOT(4095)
  360.            mov [app_size], eax
  361.            mov ebx, eax
  362.            shr eax, 12
  363.            mov [app_pages], eax
  364.  
  365.            add ebx, 0x3FFFFF
  366.            and ebx, NOT(0x3FFFFF)
  367.            shr ebx, 22
  368.            mov [app_tabs], ebx
  369.  
  370.            mov ecx, [img_size]
  371.            add ecx, 4095
  372.            and ecx, NOT(4095)
  373.  
  374.            mov [img_size], ecx
  375.            shr ecx, 12
  376.            mov [img_pages], ecx
  377.  
  378.      if GREEDY_KERNEL
  379.            lea eax, [ecx+ebx+2]    ;only image size
  380.      else
  381.            lea eax, [eax+ebx+2]    ;all requested memory
  382.      end if
  383.            cmp eax, [pg_data.pages_free]
  384.            ja .fail
  385.  
  386.            call alloc_page
  387.            test eax, eax
  388.            jz .fail
  389.            mov [dir_addr], eax
  390.            stdcall map_page,[tmp_task_pdir],eax,dword PG_SW
  391.  
  392.            mov esi, sys_pgdir
  393.            mov edi, [tmp_task_pdir]
  394.            mov ecx, (page_tabs shr 20)/4
  395.            cld
  396.            rep movsd
  397.  
  398.            mov eax, [dir_addr]
  399.            or eax, PG_SW
  400.            stosd                 ; [(page_tabs shr 20)]= eax
  401.  
  402.            mov ecx, 0x800/4
  403.            xor eax, eax
  404.            rep stosd
  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, page_tabs
  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, page_tabs
  438.            add edi, page_tabs
  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. end if
  472.  
  473. .done:
  474.            stdcall map_page,[tmp_task_pdir],dword 0,dword PG_UNMAP
  475.  
  476.            dec [pg_data.pg_mutex]
  477.            mov eax, [dir_addr]
  478.            ret
  479. .fail:
  480.            dec [pg_data.pg_mutex]
  481.            cmp [dir_addr], 0
  482.            je @f
  483.            stdcall destroy_app_space, [dir_addr]
  484. @@:
  485.            xor eax, eax
  486.            ret
  487. endp
  488.  
  489. align 4
  490. set_cr3:
  491.            mov esi, [CURRENT_TASK]
  492.            mov ebx, esi
  493.            shl esi,8
  494.            mov [SLOT_BASE+esi+0xB8],eax
  495.            imul   ebx,tss_step
  496.            add    ebx,tss_data
  497.            mov [ebx+28], eax
  498.            mov cr3, eax
  499.            ret
  500.  
  501. align 4
  502. proc destroy_page_table stdcall, pg_tab:dword
  503.  
  504.            push esi
  505.  
  506.            mov esi, [pg_tab]
  507.            mov ecx, 1024
  508. .free:
  509.            mov eax, [esi]
  510.            test eax, 1
  511.            jz .next
  512.            call free_page
  513. .next:
  514.            add esi, 4
  515.            dec ecx
  516.            jnz .free
  517.            pop esi
  518.            ret
  519. endp
  520.  
  521. align 4
  522. proc destroy_app_space stdcall, pg_dir:dword
  523.  
  524.            mov ebx, pg_data.pg_mutex
  525.            call wait_mutex   ;ebx
  526.  
  527.            xor   edx,edx
  528.            mov   eax,0x2
  529.            mov ebx, [pg_dir]
  530. .loop:
  531. ;eax = current slot of process
  532.            mov   ecx,eax
  533.            shl   ecx,5
  534.            cmp   byte [CURRENT_TASK+ecx+0xa],9  ;if process running?
  535.            jz    @f                              ;skip empty slots
  536.            shl   ecx,3
  537.            cmp   [SLOT_BASE+ecx+0xB8],ebx       ;compare page directory addresses
  538.            jnz   @f
  539.            inc   edx                            ;thread found
  540. @@:
  541.            inc   eax
  542.            cmp   eax,[TASK_COUNT]               ;exit loop if we look through all processes
  543.            jle   .loop
  544.  
  545. ;edx = number of threads
  546. ;our process is zombi so it isn't counted
  547.            cmp   edx,1
  548.            jg    .exit
  549. ;if there isn't threads then clear memory.
  550.  
  551.            mov eax, [pg_dir]
  552.            and eax, not 0xFFF
  553.            stdcall map_page,[tmp_task_pdir],eax,dword PG_SW
  554.            mov esi, [tmp_task_pdir]
  555.            add esi, 0x800
  556.            mov edi, 0x800/4
  557. .destroy:
  558.            mov eax, [esi]
  559.            test eax, 1
  560.            jz .next
  561.            and eax, not 0xFFF
  562.            stdcall map_page,[tmp_task_ptab],eax,dword PG_SW
  563.            stdcall destroy_page_table, [tmp_task_ptab]
  564.            mov eax, [esi]
  565.            call free_page
  566. .next:
  567.            add esi, 4
  568.            dec edi
  569.            jnz .destroy
  570.  
  571.            mov eax, [pg_dir]
  572.            call free_page
  573. .exit:
  574.            stdcall map_page,[tmp_task_ptab],dword 0,dword PG_UNMAP
  575.            stdcall map_page,[tmp_task_pdir],dword 0,dword PG_UNMAP
  576.            dec [pg_data.pg_mutex]
  577.            ret
  578. endp
  579.  
  580. pid_to_slot:
  581. ;Input:
  582. ;  eax - pid of process
  583. ;Output:
  584. ;  eax - slot of process or 0 if process don't exists
  585. ;Search process by PID.
  586.     push   ebx
  587.     push   ecx
  588.     mov    ebx,[TASK_COUNT]
  589.     shl    ebx,5
  590.     mov    ecx,2*32
  591.  
  592. .loop:
  593. ;ecx=offset of current process info entry
  594. ;ebx=maximum permitted offset
  595.     cmp    byte [CURRENT_TASK+ecx+0xa],9
  596.     jz     .endloop              ;skip empty slots
  597.     cmp    [CURRENT_TASK+ecx+0x4],eax ;check PID
  598.     jz     .pid_found
  599. .endloop:
  600.     add    ecx,32
  601.     cmp    ecx,ebx
  602.     jle    .loop
  603.  
  604.     pop    ecx
  605.     pop    ebx
  606.     xor    eax,eax
  607.     ret
  608.  
  609. .pid_found:
  610.     shr    ecx,5
  611.     mov    eax,ecx               ;convert offset to index of slot
  612.     pop    ecx
  613.     pop    ebx
  614.     ret
  615.  
  616. check_region:
  617. ;input:
  618. ;  ebx - start of buffer
  619. ;  ecx - size of buffer
  620. ;result:
  621. ;  eax = 1 region lays in app memory
  622. ;  eax = 0 region don't lays in app memory
  623.      mov  eax,[CURRENT_TASK]
  624.      jmp  check_process_region
  625. ;-----------------------------------------------------------------------------
  626. check_process_region:
  627. ;input:
  628. ;  eax - slot
  629. ;  ebx - start of buffer
  630. ;  ecx - size of buffer
  631. ;result:
  632. ;  eax = 1 region lays in app memory
  633. ;  eax = 0 region don't lays in app memory
  634.  
  635.      test ecx,ecx
  636.      jle  .ok
  637.      shl  eax,5
  638.      cmp  word [CURRENT_TASK+eax+0xa],0
  639.      jnz  .failed
  640.      shl  eax,3
  641.      mov  eax,[SLOT_BASE+eax+0xb8]
  642.      test eax,eax
  643.      jz   .failed
  644.  
  645.      mov  eax,1
  646.      ret
  647.  
  648.  
  649. ;    call MEM_Get_Linear_Address
  650. ;    push ebx
  651. ;    push ecx
  652. ;    push edx
  653. ;    mov  edx,ebx
  654. ;    and  edx,not (4096-1)
  655. ;    sub  ebx,edx
  656. ;    add  ecx,ebx
  657. ;    mov  ebx,edx
  658. ;    add  ecx,(4096-1)
  659. ;    and  ecx,not (4096-1)
  660. ;.loop:
  661. ;;eax - linear address of page directory
  662. ;;ebx - current page
  663. ;;ecx - current size
  664. ;    mov  edx,ebx
  665. ;    shr  edx,22
  666. ;    mov  edx,[eax+4*edx]
  667. ;    and  edx,not (4096-1)
  668. ;    test edx,edx
  669. ;    jz   .failed1
  670. ;    push eax
  671. ;    mov  eax,edx
  672. ;    call MEM_Get_Linear_Address
  673. ;    mov  edx,ebx
  674. ;    shr  edx,12
  675. ;    and  edx,(1024-1)
  676. ;    mov  eax,[eax+4*edx]
  677. ;    and  eax,not (4096-1)
  678. ;    test eax,eax
  679. ;    pop  eax
  680. ;    jz   .failed1
  681. ;    add  ebx,4096
  682. ;    sub  ecx,4096
  683. ;    jg   .loop
  684. ;    pop  edx
  685. ;    pop  ecx
  686. ;    pop  ebx
  687. .ok:
  688.     mov  eax,1
  689.     ret
  690. ;
  691. ;.failed1:
  692. ;    pop  edx
  693. ;    pop  ecx
  694. ;    pop  ebx
  695. .failed:
  696.     xor  eax,eax
  697.     ret
  698.  
  699. align 4
  700. proc read_process_memory
  701. ;Input:
  702. ;  eax - process slot
  703. ;  ebx - buffer address
  704. ;  ecx - buffer size
  705. ;  edx - start address in other process
  706. ;Output:
  707. ;  eax - number of bytes read.
  708.            locals
  709.              slot       dd ?
  710.              buff       dd ?
  711.              r_count    dd ?
  712.              offset     dd ?
  713.              tmp_r_cnt  dd ?
  714.            endl
  715.  
  716.            mov [slot], eax
  717.            mov [buff], ebx
  718.            mov [r_count], ecx
  719.            mov [tmp_r_cnt], ecx
  720.            mov [offset], edx
  721.  
  722.            pushad
  723. .read_mem:
  724.            mov edx, [offset]
  725.            mov ebx, [tmp_r_cnt]
  726.  
  727.            mov ecx, 0x400000
  728.            and edx, 0x3FFFFF
  729.            sub ecx, edx
  730.            cmp ecx, ebx
  731.            jbe @f
  732.            mov ecx, ebx
  733. @@:
  734.            cmp ecx, 0x8000
  735.            jna @F
  736.            mov ecx, 0x8000
  737. @@:
  738.            mov eax, [slot]
  739.            shl  eax,8
  740.            mov ebx, [offset]
  741.            add ebx, new_app_base
  742.            push ecx
  743.            stdcall map_memEx, [proc_mem_map],\
  744.                               [SLOT_BASE+eax+0xB8],\
  745.                               ebx, ecx
  746.            pop ecx
  747.  
  748.            mov esi, [offset]
  749.            and esi, 0xfff
  750.            add esi, [proc_mem_map]
  751.            mov edi, [buff]
  752.            mov edx, ecx
  753.            rep movsb
  754.  
  755.            add [offset], edx
  756.            sub [tmp_r_cnt], edx
  757.            jnz .read_mem
  758.  
  759.            popad
  760.            mov eax, [r_count]
  761.            ret
  762. endp
  763.  
  764. align 4
  765. proc write_process_memory
  766. ;Input:
  767. ;  eax - process slot
  768. ;  ebx - buffer address
  769. ;  ecx - buffer size
  770. ;  edx - start address in other process
  771. ;Output:
  772. ;  eax - number of bytes written
  773.  
  774.            locals
  775.              slot       dd ?
  776.              buff       dd ?
  777.              w_count    dd ?
  778.              offset     dd ?
  779.              tmp_w_cnt  dd ?
  780.            endl
  781.  
  782.            mov [slot], eax
  783.            mov [buff], ebx
  784.            mov [w_count], ecx
  785.            mov [tmp_w_cnt], ecx
  786.            mov [offset], edx
  787.  
  788.            pushad
  789. .read_mem:
  790.            mov edx, [offset]
  791.            mov ebx, [tmp_w_cnt]
  792.  
  793.            mov ecx, 0x400000
  794.            and edx, 0x3FFFFF
  795.            sub ecx, edx
  796.            cmp ecx, ebx
  797.            jbe @f
  798.            mov ecx, ebx
  799. @@:
  800.            cmp ecx, 0x8000
  801.            jna @F
  802.            mov ecx, 0x8000
  803. @@:
  804.            mov eax, [slot]
  805.            shl  eax,8
  806.            mov ebx, [offset]
  807.            add ebx, new_app_base
  808.            push ecx
  809.            stdcall map_memEx, [proc_mem_map],\
  810.                               [SLOT_BASE+eax+0xB8],\
  811.                               ebx, ecx
  812.            pop ecx
  813.  
  814.            mov edi, [offset]
  815.            and edi, 0xfff
  816.            add edi, [proc_mem_map]
  817.            mov esi, [buff]
  818.            mov edx, ecx
  819.            rep movsb
  820.  
  821.            add [offset], edx
  822.            sub [tmp_w_cnt], edx
  823.            jnz .read_mem
  824.  
  825.            popad
  826.            mov eax, [w_count]
  827.            ret
  828. endp
  829.  
  830. align 4
  831. proc new_sys_threads
  832.            locals
  833.              slot          dd ?
  834.              app_cmdline   dd ? ;0x00
  835.              app_path      dd ? ;0x04
  836.              app_eip       dd ? ;0x08
  837.              app_esp       dd ? ;0x0C
  838.              app_mem       dd ? ;0x10
  839.            endl
  840.  
  841.            cmp eax,1
  842.            jne .failed                  ;other subfunctions
  843.  
  844.            xor  eax,eax
  845.            mov [app_cmdline], eax
  846.            mov [app_path], eax
  847.            mov [app_eip], ebx
  848.            mov [app_esp], ecx
  849.  
  850.            mov    esi,new_process_loading
  851.            call   sys_msg_board_str
  852. .wait_lock:
  853.            cmp [application_table_status],0
  854.            je .get_lock
  855.            call   change_task
  856.            jmp .wait_lock
  857.  
  858. .get_lock:
  859.            mov eax, 1
  860.            xchg eax, [application_table_status]
  861.            cmp eax, 0
  862.            jne .wait_lock
  863.  
  864.            call   set_application_table_status
  865.  
  866.            call get_new_process_place
  867.            test eax, eax
  868.            jz .failed
  869.  
  870.            mov [slot], eax
  871.  
  872.            mov    esi,[CURRENT_TASK]
  873.            shl    esi,8
  874.            add    esi,SLOT_BASE
  875.            mov    ebx,esi             ;ebx=esi - pointer to extended information about current thread
  876.  
  877.            mov    edi, eax
  878.            shl    edi,8
  879.            add    edi,SLOT_BASE
  880.            mov    edx,edi             ;edx=edi - pointer to extended infomation about new thread
  881.            mov    ecx,256/4
  882.            xor eax, eax
  883.            cld
  884.            rep    stosd               ;clean extended information about new thread
  885.            mov    esi,ebx
  886.            mov    edi,edx
  887.            mov byte [edi], '@'
  888.            inc    edi
  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, RING0_STACK_SIZE+512
  950.            mov [pl0_stack], eax
  951.  
  952.            lea edi, [eax+RING0_STACK_SIZE]
  953.  
  954.            mov eax, [slot]
  955.            mov ebx, eax
  956.  
  957.            shl eax, 8
  958.            mov [eax+SLOT_BASE+APPDATA.fpu_state], edi
  959.            mov [eax+SLOT_BASE+APPDATA.fpu_handler], 0
  960.            mov [eax+SLOT_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+SLOT_BASE+APP_EV_OFFSET]
  972.            mov [SLOT_BASE+APPDATA.fd_ev+ebx],edx
  973.            mov [SLOT_BASE+APPDATA.bk_ev+ebx],edx
  974.  
  975.            add edx, APP_OBJ_OFFSET-APP_EV_OFFSET
  976.            mov [SLOT_BASE+APPDATA.fd_obj+ebx],edx
  977.            mov [SLOT_BASE+APPDATA.bk_obj+ebx],edx
  978.  
  979.            mov ecx, [def_cursor]
  980.            mov [SLOT_BASE+APPDATA.cursor+ebx],ecx
  981.            mov eax, [pl0_stack]
  982.            mov [SLOT_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, [SLOT_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, [SLOT_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,[ScreenWidth]
  1038.            mov    [ecx+8],eax
  1039.            mov    eax,[ScreenHeight]
  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,[SLOT_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, RING0_STACK_SIZE
  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  [SLOT_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.