Subversion Repositories Kolibri OS

Rev

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