Subversion Repositories Kolibri OS

Rev

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

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