Subversion Repositories Kolibri OS

Rev

Rev 388 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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