Subversion Repositories Kolibri OS

Rev

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

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