Subversion Repositories Kolibri OS

Rev

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