Subversion Repositories Kolibri OS

Rev

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