Subversion Repositories Kolibri OS

Rev

Rev 887 | Rev 889 | 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: 888 $
  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.  
  420.            mov ecx, [app_tabs]
  421.            shl ecx, 10
  422.            xor eax, eax
  423.            rep stosd
  424.  
  425.            mov ecx, [app_pages]
  426.            xor ebx, ebx
  427. .alloc:
  428.            xor ecx, ecx
  429.            call @core_alloc@4
  430.            test eax, eax
  431.            jz .fail
  432.  
  433.            stdcall map_page,ebx,eax,dword PG_UW
  434.            add ebx, 0x1000
  435.            dec [app_pages]
  436.            jnz .alloc
  437.  
  438.            mov ecx, [img_size]                 ; FIXME remap md
  439.            mov esi, [img_base]
  440.            xor edi, edi
  441.  
  442.            rep movsb
  443.  
  444. .done:
  445.            dec [pg_data.pg_mutex]
  446.            mov eax, [dir_addr]
  447.            ret
  448. .fail:
  449.            dec [pg_data.pg_mutex]
  450.            cmp [dir_addr], 0
  451.            je @f
  452.            stdcall destroy_app_space, [dir_addr]
  453. @@:
  454.            xor eax, eax
  455.            ret
  456. endp
  457.  
  458. align 4
  459. set_cr3:
  460.  
  461.            mov ebx, [current_slot]
  462.            mov [ebx+APPDATA.dir_table], eax
  463.            mov cr3, eax
  464.            ret
  465.  
  466. align 4
  467. proc destroy_page_table stdcall, pg_tab:dword
  468.  
  469.            push ebx
  470.            push esi
  471.  
  472.            mov esi, [pg_tab]
  473.            mov ebx, 1024
  474. .free:
  475.            mov ecx, [esi]
  476.            test ecx, 1
  477.            jz .next
  478.  
  479.            test ecx, 1 shl 9
  480.            jnz .next                      ;skip shared pages
  481.  
  482.            call @core_free@4
  483. .next:
  484.            add esi, 4
  485.            dec ebx
  486.            jnz .free
  487.            pop esi
  488.            pop ebx
  489.            ret
  490. endp
  491.  
  492. align 4
  493. proc destroy_app_space stdcall, pg_dir:dword
  494.  
  495.            mov ebx, pg_data.pg_mutex
  496.            call wait_mutex   ;ebx
  497.  
  498.            xor   edx,edx
  499.            mov   eax,0x2
  500.            mov ebx, [pg_dir]
  501. .loop:
  502. ;eax = current slot of process
  503.            mov   ecx,eax
  504.            shl   ecx,5
  505.            cmp   byte [CURRENT_TASK+ecx+0xa],9  ;if process running?
  506.            jz    @f                             ;skip empty slots
  507.  
  508.            shl   ecx,3
  509.            cmp   [SLOT_BASE+ecx+0xB8],ebx       ;compare page directory addresses
  510.            jnz   @f
  511.  
  512.            inc   edx                            ;thread found
  513. @@:
  514.            inc   eax
  515.            cmp   eax,[TASK_COUNT]               ;exit loop if we look through all processes
  516.            jle   .loop
  517.  
  518. ;edx = number of threads
  519. ;our process is zombi so it isn't counted
  520.            cmp   edx,1
  521.            jg    .exit
  522. ;if there isn't threads then clear memory.
  523.  
  524.            mov eax, [pg_dir]
  525.            and eax, -4096
  526.            add eax, OS_BASE
  527.            mov [tmp_task_pdir], eax
  528.            mov esi, eax
  529.            mov edi, (HEAP_BASE shr 20)/4
  530. .destroy:
  531.            mov eax, [esi]
  532.            test eax, 1
  533.            jz .next
  534.            and eax, not 0xFFF
  535.            add eax, OS_BASE
  536.  
  537.            stdcall destroy_page_table, eax
  538.  
  539.            mov ecx, [esi]
  540.            call @core_free@4
  541. .next:
  542.            add esi, 4
  543.            dec edi
  544.            jnz .destroy
  545.  
  546.            mov ecx, [pg_dir]
  547.            call @core_free@4
  548. .exit:
  549.            dec [pg_data.pg_mutex]
  550.            ret
  551. endp
  552.  
  553. align 4
  554. get_pid:
  555.            mov eax, [TASK_BASE]
  556.            mov eax, [eax+TASKDATA.pid]
  557.            ret
  558.  
  559. pid_to_slot:
  560. ;Input:
  561. ;  eax - pid of process
  562. ;Output:
  563. ;  eax - slot of process or 0 if process don't exists
  564. ;Search process by PID.
  565.     push   ebx
  566.     push   ecx
  567.     mov    ebx,[TASK_COUNT]
  568.     shl    ebx,5
  569.     mov    ecx,2*32
  570.  
  571. .loop:
  572. ;ecx=offset of current process info entry
  573. ;ebx=maximum permitted offset
  574.     cmp    byte [CURRENT_TASK+ecx+0xa],9
  575.     jz     .endloop              ;skip empty slots
  576.     cmp    [CURRENT_TASK+ecx+0x4],eax ;check PID
  577.     jz     .pid_found
  578. .endloop:
  579.     add    ecx,32
  580.     cmp    ecx,ebx
  581.     jle    .loop
  582.  
  583.     pop    ecx
  584.     pop    ebx
  585.     xor    eax,eax
  586.     ret
  587.  
  588. .pid_found:
  589.     shr    ecx,5
  590.     mov    eax,ecx               ;convert offset to index of slot
  591.     pop    ecx
  592.     pop    ebx
  593.     ret
  594.  
  595. check_region:
  596. ;input:
  597. ;  ebx - start of buffer
  598. ;  ecx - size of buffer
  599. ;result:
  600. ;  eax = 1 region lays in app memory
  601. ;  eax = 0 region don't lays in app memory
  602.      mov  eax,[CURRENT_TASK]
  603.      jmp  check_process_region
  604. ;-----------------------------------------------------------------------------
  605. check_process_region:
  606. ;input:
  607. ;  eax - slot
  608. ;  ebx - start of buffer
  609. ;  ecx - size of buffer
  610. ;result:
  611. ;  eax = 1 region lays in app memory
  612. ;  eax = 0 region don't lays in app memory
  613.  
  614.      test ecx,ecx
  615.      jle  .ok
  616.      shl  eax,5
  617.      cmp  word [CURRENT_TASK+eax+0xa],0
  618.      jnz  .failed
  619.      shl  eax,3
  620.      mov  eax,[SLOT_BASE+eax+0xb8]
  621.      test eax,eax
  622.      jz   .failed
  623.  
  624.      mov  eax,1
  625.      ret
  626.  
  627.  
  628. ;    call MEM_Get_Linear_Address
  629. ;    push ebx
  630. ;    push ecx
  631. ;    push edx
  632. ;    mov  edx,ebx
  633. ;    and  edx,not (4096-1)
  634. ;    sub  ebx,edx
  635. ;    add  ecx,ebx
  636. ;    mov  ebx,edx
  637. ;    add  ecx,(4096-1)
  638. ;    and  ecx,not (4096-1)
  639. ;.loop:
  640. ;;eax - linear address of page directory
  641. ;;ebx - current page
  642. ;;ecx - current size
  643. ;    mov  edx,ebx
  644. ;    shr  edx,22
  645. ;    mov  edx,[eax+4*edx]
  646. ;    and  edx,not (4096-1)
  647. ;    test edx,edx
  648. ;    jz   .failed1
  649. ;    push eax
  650. ;    mov  eax,edx
  651. ;    call MEM_Get_Linear_Address
  652. ;    mov  edx,ebx
  653. ;    shr  edx,12
  654. ;    and  edx,(1024-1)
  655. ;    mov  eax,[eax+4*edx]
  656. ;    and  eax,not (4096-1)
  657. ;    test eax,eax
  658. ;    pop  eax
  659. ;    jz   .failed1
  660. ;    add  ebx,4096
  661. ;    sub  ecx,4096
  662. ;    jg   .loop
  663. ;    pop  edx
  664. ;    pop  ecx
  665. ;    pop  ebx
  666. .ok:
  667.     mov  eax,1
  668.     ret
  669. ;
  670. ;.failed1:
  671. ;    pop  edx
  672. ;    pop  ecx
  673. ;    pop  ebx
  674. .failed:
  675.     xor  eax,eax
  676.     ret
  677.  
  678. align 4
  679. proc read_process_memory
  680. ;Input:
  681. ;  eax - process slot
  682. ;  ebx - buffer address
  683. ;  ecx - buffer size
  684. ;  edx - start address in other process
  685. ;Output:
  686. ;  eax - number of bytes read.
  687.            locals
  688.              slot       dd ?
  689.              buff       dd ?
  690.              r_count    dd ?
  691.              offset     dd ?
  692.              tmp_r_cnt  dd ?
  693.            endl
  694.  
  695.            mov [slot], eax
  696.            mov [buff], ebx
  697.            and [r_count], 0
  698.            mov [tmp_r_cnt], ecx
  699.            mov [offset], edx
  700.  
  701.            pushad
  702. .read_mem:
  703.            mov edx, [offset]
  704.            mov ebx, [tmp_r_cnt]
  705.  
  706.            mov ecx, 0x400000
  707.            and edx, 0x3FFFFF
  708.            sub ecx, edx
  709.            cmp ecx, ebx
  710.            jbe @f
  711.            mov ecx, ebx
  712. @@:
  713.            cmp ecx, 0x8000
  714.            jna @F
  715.            mov ecx, 0x8000
  716. @@:
  717.            mov eax, [slot]
  718.            shl  eax,8
  719.            mov ebx, [offset]
  720.            push ecx
  721.            stdcall map_memEx, [proc_mem_map],\
  722.                               [SLOT_BASE+eax+0xB8],\
  723.                               ebx, ecx
  724.            pop ecx
  725.  
  726.            mov esi, [offset]
  727.            and esi, 0xfff
  728.            add esi, [proc_mem_map]
  729.            mov edi, [buff]
  730.            mov edx, ecx
  731.            rep movsb
  732.            add [r_count], edx
  733.  
  734.            add [offset], edx
  735.            sub [tmp_r_cnt], edx
  736.            jnz .read_mem
  737.  
  738.            popad
  739.            mov eax, [r_count]
  740.            ret
  741. endp
  742.  
  743. align 4
  744. proc write_process_memory
  745. ;Input:
  746. ;  eax - process slot
  747. ;  ebx - buffer address
  748. ;  ecx - buffer size
  749. ;  edx - start address in other process
  750. ;Output:
  751. ;  eax - number of bytes written
  752.  
  753.            locals
  754.              slot       dd ?
  755.              buff       dd ?
  756.              w_count    dd ?
  757.              offset     dd ?
  758.              tmp_w_cnt  dd ?
  759.            endl
  760.  
  761.            mov [slot], eax
  762.            mov [buff], ebx
  763.            and [w_count], 0
  764.            mov [tmp_w_cnt], ecx
  765.            mov [offset], edx
  766.  
  767.            pushad
  768. .read_mem:
  769.            mov edx, [offset]
  770.            mov ebx, [tmp_w_cnt]
  771.  
  772.            mov ecx, 0x400000
  773.            and edx, 0x3FFFFF
  774.            sub ecx, edx
  775.            cmp ecx, ebx
  776.            jbe @f
  777.            mov ecx, ebx
  778. @@:
  779.            cmp ecx, 0x8000
  780.            jna @F
  781.            mov ecx, 0x8000
  782. @@:
  783.            mov eax, [slot]
  784.            shl  eax,8
  785.            mov ebx, [offset]
  786.       ;     add ebx, new_app_base
  787.            push ecx
  788.            stdcall map_memEx, [proc_mem_map],\
  789.                               [SLOT_BASE+eax+0xB8],\
  790.                               ebx, ecx
  791.            pop ecx
  792.  
  793.            mov edi, [offset]
  794.            and edi, 0xfff
  795.            add edi, [proc_mem_map]
  796.            mov esi, [buff]
  797.            mov edx, ecx
  798.            rep movsb
  799.  
  800.            add [w_count], edx
  801.            add [offset], edx
  802.            sub [tmp_w_cnt], edx
  803.            jnz .read_mem
  804.  
  805.            popad
  806.            mov eax, [w_count]
  807.            ret
  808. endp
  809.  
  810. align 4
  811. proc new_sys_threads
  812.            locals
  813.              slot      dd ?
  814.              app_cmdline   dd ? ;0x00
  815.              app_path      dd ? ;0x04
  816.              app_eip       dd ? ;0x08
  817.              app_esp       dd ? ;0x0C
  818.              app_mem       dd ? ;0x10
  819.            endl
  820.  
  821.            cmp eax,1
  822.            jne .failed          ;other subfunctions
  823.  
  824.            xor  eax,eax
  825.            mov [app_cmdline], eax
  826.            mov [app_path], eax
  827.            mov [app_eip], ebx
  828.            mov [app_esp], ecx
  829.  
  830.            ;mov    esi,new_process_loading
  831.            ;call   sys_msg_board_str
  832.            DEBUGF 1,"%s",new_process_loading
  833. .wait_lock:
  834.            cmp [application_table_status],0
  835.            je .get_lock
  836.  
  837.            call   change_task
  838.            jmp .wait_lock
  839.  
  840. .get_lock:
  841.            mov eax, 1
  842.            xchg eax, [application_table_status]
  843.            cmp eax, 0
  844.            jne .wait_lock
  845.  
  846.            call   set_application_table_status
  847.  
  848.            call get_new_process_place
  849.            test eax, eax
  850.            jz .failed
  851.  
  852.            mov [slot], eax
  853.  
  854.            mov    esi,[current_slot]
  855.            mov    ebx,esi         ;ebx=esi - pointer to extended information about current thread
  856.  
  857.            mov    edi, eax
  858.            shl    edi,8
  859.            add    edi,SLOT_BASE
  860.            mov    edx,edi         ;edx=edi - pointer to extended infomation about new thread
  861.            mov    ecx,256/4
  862.            xor eax, eax
  863.            cld
  864.            rep    stosd           ;clean extended information about new thread
  865.            mov    esi,ebx
  866.            mov    edi,edx
  867.            mov    ecx,11
  868.            rep    movsb           ;copy process name
  869.  
  870.            mov eax,[ebx+APPDATA.heap_base]
  871.            mov [edx+APPDATA.heap_base], eax
  872.  
  873.            mov ecx,[ebx+APPDATA.heap_top]
  874.            mov [edx+APPDATA.heap_top], ecx
  875.  
  876.            mov eax,[ebx+APPDATA.mem_size]
  877.            mov [edx+APPDATA.mem_size], eax
  878.  
  879.            mov ecx,[ebx+APPDATA.dir_table]
  880.            mov [edx+APPDATA.dir_table],ecx  ;copy page directory
  881.  
  882.            lea eax, [app_cmdline]
  883.            stdcall set_app_params ,[slot],eax,dword 0,\
  884.                          dword 0,dword 0
  885.  
  886.            ;mov    esi,new_process_running
  887.            ;call   sys_msg_board_str     ;output information about succefull startup
  888.            DEBUGF 1,"%s",new_process_running
  889.  
  890.            mov    [application_table_status],0 ;unlock application_table_status mutex
  891.            mov    eax,[process_number]  ;set result
  892.            ret
  893. .failed:
  894.            mov    [application_table_status],0
  895.            mov    eax,-1
  896.            ret
  897. endp
  898.  
  899. ; param
  900. ;  ebx=mutex
  901.  
  902. align 4
  903. wait_mutex:
  904.            push eax
  905.            push ebx
  906. .do_wait:
  907.            cmp dword [ebx],0
  908.            je .get_lock
  909.  
  910.            call change_task
  911.            jmp .do_wait
  912. .get_lock:
  913.            mov eax, 1
  914.            xchg eax, [ebx]
  915.            test eax, eax
  916.            jnz .do_wait
  917.  
  918.            pop ebx
  919.            pop eax
  920.            ret
  921.  
  922. EFL_IF      equ 0x0200
  923. EFL_IOPL1   equ 0x1000
  924. EFL_IOPL2   equ 0x2000
  925. EFL_IOPL3   equ 0x3000
  926.  
  927.  
  928. align 4
  929. proc set_app_params stdcall,slot:dword, params:dword,\
  930.                         cmd_line:dword, app_path:dword, flags:dword
  931.  
  932.            locals
  933.              pl0_stack dd ?
  934.            endl
  935.  
  936.            mov ecx, 1            ;(RING0_STACK_SIZE+512) shr 12
  937.            call @core_alloc@4
  938.            add eax, OS_BASE
  939.            mov [pl0_stack], eax
  940.  
  941.            lea edi, [eax+RING0_STACK_SIZE]
  942.  
  943.            mov eax, [slot]
  944.            mov ebx, eax
  945.  
  946.            shl eax, 8
  947.            mov [eax+SLOT_BASE+APPDATA.fpu_state], edi
  948.            mov [eax+SLOT_BASE+APPDATA.fpu_handler], 0
  949.            mov [eax+SLOT_BASE+APPDATA.sse_handler], 0
  950.  
  951. ;set default io permission map
  952.            mov [eax+SLOT_BASE+APPDATA.io_map],\
  953.                (tss._io_map_0-OS_BASE+PG_MAP)
  954.            mov [eax+SLOT_BASE+APPDATA.io_map+4],\
  955.                (tss._io_map_1-OS_BASE+PG_MAP)
  956.  
  957.            mov esi, fpu_data
  958.            mov ecx, 512/4
  959.            rep movsd
  960.  
  961.            cmp    ebx,[TASK_COUNT]
  962.            jle    .noinc
  963.            inc    dword [TASK_COUNT]       ;update number of processes
  964. .noinc:
  965.            shl ebx,8
  966.            lea edx, [ebx+SLOT_BASE+APP_EV_OFFSET]
  967.            mov [SLOT_BASE+APPDATA.fd_ev+ebx],edx
  968.            mov [SLOT_BASE+APPDATA.bk_ev+ebx],edx
  969.  
  970.            add edx, APP_OBJ_OFFSET-APP_EV_OFFSET
  971.            mov [SLOT_BASE+APPDATA.fd_obj+ebx],edx
  972.            mov [SLOT_BASE+APPDATA.bk_obj+ebx],edx
  973.  
  974.            mov ecx, [def_cursor]
  975.            mov [SLOT_BASE+APPDATA.cursor+ebx],ecx
  976.            mov eax, [pl0_stack]
  977.            mov [SLOT_BASE+APPDATA.pl0_stack+ebx],eax
  978.            add eax, RING0_STACK_SIZE
  979.            mov [SLOT_BASE+APPDATA.saved_esp0+ebx], eax
  980.  
  981.            call _alloc_page
  982.            add eax, OS_BASE
  983.            mov esi,[current_slot]
  984.            mov esi,[esi+APPDATA.cur_dir]
  985.            mov ecx,0x1000/4
  986.            mov edi,eax
  987.            mov [ebx+SLOT_BASE+APPDATA.cur_dir],eax
  988.            rep movsd
  989.  
  990.            shr ebx,3
  991.            mov dword [CURRENT_TASK+ebx+0x10], 0
  992.  
  993. .add_command_line:
  994.            mov edx,[params]
  995.            mov edx,[edx]       ;app_cmdline
  996.            test edx,edx
  997.            jz @f           ;application doesn't need parameters
  998.  
  999.            mov     eax, edx
  1000.            add     eax, 256
  1001.            jc      @f
  1002.  
  1003.            cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
  1004.            ja      @f
  1005.  
  1006.            mov     byte [edx], 0   ;force empty string if no cmdline given
  1007.            mov     eax, [cmd_line]
  1008.            test    eax, eax
  1009.            jz      @f
  1010.            stdcall strncpy, edx, eax, 256
  1011. @@:
  1012.            mov edx,[params]
  1013.            mov edx, [edx+4]    ;app_path
  1014.            test edx,edx
  1015.            jz @F           ;application don't need path of file
  1016.            mov     eax, edx
  1017.            add     eax, 1024
  1018.            jc      @f
  1019.            cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
  1020.            ja      @f
  1021.            stdcall strncpy, edx, [app_path], 1024
  1022. @@:
  1023.            mov    ebx,[slot]
  1024.            mov    eax,ebx
  1025.            shl    ebx,5
  1026.            lea    ecx,[draw_data+ebx]  ;ecx - pointer to draw data
  1027.  
  1028. ; set window state to 'normal' (non-minimized/maximized/rolled-up) state
  1029.            mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
  1030.            mov     [ebx+window_data+WDATA.fl_redraw], 1
  1031.            add    ebx,CURRENT_TASK        ;ebx - pointer to information about process
  1032.            mov    [ebx+TASKDATA.wnd_number],al;set window number on screen = process slot
  1033.  
  1034.            mov    [ebx+TASKDATA.event_mask],dword 1+2+4 ;set default event flags (see 40 function)
  1035.  
  1036.            inc    dword [process_number]
  1037.            mov    eax,[process_number]
  1038.            mov    [ebx+4],eax       ;set PID
  1039.  
  1040. ;set draw data to full screen
  1041.  
  1042.            mov    [ecx+0],dword 0
  1043.            mov    [ecx+4],dword 0
  1044.            mov    eax,[Screen_Max_X]
  1045.            mov    [ecx+8],eax
  1046.            mov    eax,[Screen_Max_Y]
  1047.            mov    [ecx+12],eax
  1048.  
  1049.            mov ebx, [pl0_stack]
  1050.            mov esi,[params]
  1051.            lea ecx, [ebx+REG_EIP]
  1052.            xor eax, eax
  1053.  
  1054.            mov [ebx+REG_RET], dword irq0.return
  1055.            mov [ebx+REG_EDI], eax
  1056.            mov [ebx+REG_ESI], eax
  1057.            mov [ebx+REG_EBP], eax
  1058.            mov [ebx+REG_ESP], ecx   ;ebx+REG_EIP
  1059.            mov [ebx+REG_EBX], eax
  1060.            mov [ebx+REG_EDX], eax
  1061.            mov [ebx+REG_ECX], eax
  1062.            mov [ebx+REG_EAX], eax
  1063.  
  1064.            mov eax, [esi+0x08]       ;app_eip
  1065.            mov [ebx+REG_EIP],  eax   ;app_entry
  1066.            mov [ebx+REG_CS], dword app_code
  1067.                mov [ebx+REG_EFLAGS], dword EFL_IOPL3+EFL_IF
  1068.  
  1069.            mov eax, [esi+0x0C]       ;app_esp
  1070.            mov [ebx+REG_APP_ESP], eax    ;app_stack
  1071.            mov [ebx+REG_SS], dword app_data
  1072.  
  1073.            lea ecx, [ebx+REG_RET]
  1074.            mov ebx, [slot]
  1075.            shl ebx, 5
  1076.            mov [ebx*8+SLOT_BASE+APPDATA.saved_esp], ecx
  1077.  
  1078.            xor  ecx, ecx    ; process state - running
  1079. ; set if debuggee
  1080.            test byte [flags], 1
  1081.            jz   .no_debug
  1082.  
  1083.            inc  ecx     ; process state - suspended
  1084.            mov  eax,[CURRENT_TASK]
  1085.            mov  [SLOT_BASE+ebx*8+APPDATA.debugger_slot],eax
  1086. .no_debug:
  1087.            mov  [CURRENT_TASK+ebx+TASKDATA.state], cl
  1088.            DEBUGF 1,"%s",new_process_running
  1089.            ret
  1090. endp
  1091.  
  1092. include "debug.inc"
  1093.