Subversion Repositories Kolibri OS

Rev

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