Subversion Repositories Kolibri OS

Rev

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

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