Subversion Repositories Kolibri OS

Rev

Rev 400 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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