Subversion Repositories Kolibri OS

Rev

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