Subversion Repositories Kolibri OS

Rev

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

  1. $Revision: 519 $
  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. pid_to_slot:
  548. ;Input:
  549. ;  eax - pid of process
  550. ;Output:
  551. ;  eax - slot of process or 0 if process don't exists
  552. ;Search process by PID.
  553.     push   ebx
  554.     push   ecx
  555.     mov    ebx,[TASK_COUNT]
  556.     shl    ebx,5
  557.     mov    ecx,2*32
  558.  
  559. .loop:
  560. ;ecx=offset of current process info entry
  561. ;ebx=maximum permitted offset
  562.     cmp    byte [CURRENT_TASK+ecx+0xa],9
  563.     jz     .endloop              ;skip empty slots
  564.     cmp    [CURRENT_TASK+ecx+0x4],eax ;check PID
  565.     jz     .pid_found
  566. .endloop:
  567.     add    ecx,32
  568.     cmp    ecx,ebx
  569.     jle    .loop
  570.  
  571.     pop    ecx
  572.     pop    ebx
  573.     xor    eax,eax
  574.     ret
  575.  
  576. .pid_found:
  577.     shr    ecx,5
  578.     mov    eax,ecx               ;convert offset to index of slot
  579.     pop    ecx
  580.     pop    ebx
  581.     ret
  582.  
  583. check_region:
  584. ;input:
  585. ;  ebx - start of buffer
  586. ;  ecx - size of buffer
  587. ;result:
  588. ;  eax = 1 region lays in app memory
  589. ;  eax = 0 region don't lays in app memory
  590.      mov  eax,[CURRENT_TASK]
  591.      jmp  check_process_region
  592. ;-----------------------------------------------------------------------------
  593. check_process_region:
  594. ;input:
  595. ;  eax - slot
  596. ;  ebx - start of buffer
  597. ;  ecx - size of buffer
  598. ;result:
  599. ;  eax = 1 region lays in app memory
  600. ;  eax = 0 region don't lays in app memory
  601.  
  602.      test ecx,ecx
  603.      jle  .ok
  604.      shl  eax,5
  605.      cmp  word [CURRENT_TASK+eax+0xa],0
  606.      jnz  .failed
  607.      shl  eax,3
  608.      mov  eax,[SLOT_BASE+eax+0xb8]
  609.      test eax,eax
  610.      jz   .failed
  611.  
  612.      mov  eax,1
  613.      ret
  614.  
  615.  
  616. ;    call MEM_Get_Linear_Address
  617. ;    push ebx
  618. ;    push ecx
  619. ;    push edx
  620. ;    mov  edx,ebx
  621. ;    and  edx,not (4096-1)
  622. ;    sub  ebx,edx
  623. ;    add  ecx,ebx
  624. ;    mov  ebx,edx
  625. ;    add  ecx,(4096-1)
  626. ;    and  ecx,not (4096-1)
  627. ;.loop:
  628. ;;eax - linear address of page directory
  629. ;;ebx - current page
  630. ;;ecx - current size
  631. ;    mov  edx,ebx
  632. ;    shr  edx,22
  633. ;    mov  edx,[eax+4*edx]
  634. ;    and  edx,not (4096-1)
  635. ;    test edx,edx
  636. ;    jz   .failed1
  637. ;    push eax
  638. ;    mov  eax,edx
  639. ;    call MEM_Get_Linear_Address
  640. ;    mov  edx,ebx
  641. ;    shr  edx,12
  642. ;    and  edx,(1024-1)
  643. ;    mov  eax,[eax+4*edx]
  644. ;    and  eax,not (4096-1)
  645. ;    test eax,eax
  646. ;    pop  eax
  647. ;    jz   .failed1
  648. ;    add  ebx,4096
  649. ;    sub  ecx,4096
  650. ;    jg   .loop
  651. ;    pop  edx
  652. ;    pop  ecx
  653. ;    pop  ebx
  654. .ok:
  655.     mov  eax,1
  656.     ret
  657. ;
  658. ;.failed1:
  659. ;    pop  edx
  660. ;    pop  ecx
  661. ;    pop  ebx
  662. .failed:
  663.     xor  eax,eax
  664.     ret
  665.  
  666. align 4
  667. proc read_process_memory
  668. ;Input:
  669. ;  eax - process slot
  670. ;  ebx - buffer address
  671. ;  ecx - buffer size
  672. ;  edx - start address in other process
  673. ;Output:
  674. ;  eax - number of bytes read.
  675.            locals
  676.              slot       dd ?
  677.              buff       dd ?
  678.              r_count    dd ?
  679.              offset     dd ?
  680.              tmp_r_cnt  dd ?
  681.            endl
  682.  
  683.            mov [slot], eax
  684.            mov [buff], ebx
  685.            mov [r_count], ecx
  686.            mov [tmp_r_cnt], ecx
  687.            mov [offset], edx
  688.  
  689.            pushad
  690. .read_mem:
  691.            mov edx, [offset]
  692.            mov ebx, [tmp_r_cnt]
  693.  
  694.            mov ecx, 0x400000
  695.            and edx, 0x3FFFFF
  696.            sub ecx, edx
  697.            cmp ecx, ebx
  698.            jbe @f
  699.            mov ecx, ebx
  700. @@:
  701.            cmp ecx, 0x8000
  702.            jna @F
  703.            mov ecx, 0x8000
  704. @@:
  705.            mov eax, [slot]
  706.            shl  eax,8
  707.            mov ebx, [offset]
  708.      ;      add ebx, new_app_base
  709.            push ecx
  710.            stdcall map_memEx, [proc_mem_map],\
  711.                               [SLOT_BASE+eax+0xB8],\
  712.                               ebx, ecx
  713.            pop ecx
  714.  
  715.            mov esi, [offset]
  716.            and esi, 0xfff
  717.            add esi, [proc_mem_map]
  718.            mov edi, [buff]
  719.            mov edx, ecx
  720.            rep movsb
  721.  
  722.            add [offset], edx
  723.            sub [tmp_r_cnt], edx
  724.            jnz .read_mem
  725.  
  726.            popad
  727.            mov eax, [r_count]
  728.            ret
  729. endp
  730.  
  731. align 4
  732. proc write_process_memory
  733. ;Input:
  734. ;  eax - process slot
  735. ;  ebx - buffer address
  736. ;  ecx - buffer size
  737. ;  edx - start address in other process
  738. ;Output:
  739. ;  eax - number of bytes written
  740.  
  741.            locals
  742.              slot       dd ?
  743.              buff       dd ?
  744.              w_count    dd ?
  745.              offset     dd ?
  746.              tmp_w_cnt  dd ?
  747.            endl
  748.  
  749.            mov [slot], eax
  750.            mov [buff], ebx
  751.            mov [w_count], ecx
  752.            mov [tmp_w_cnt], ecx
  753.            mov [offset], edx
  754.  
  755.            pushad
  756. .read_mem:
  757.            mov edx, [offset]
  758.            mov ebx, [tmp_w_cnt]
  759.  
  760.            mov ecx, 0x400000
  761.            and edx, 0x3FFFFF
  762.            sub ecx, edx
  763.            cmp ecx, ebx
  764.            jbe @f
  765.            mov ecx, ebx
  766. @@:
  767.            cmp ecx, 0x8000
  768.            jna @F
  769.            mov ecx, 0x8000
  770. @@:
  771.            mov eax, [slot]
  772.            shl  eax,8
  773.            mov ebx, [offset]
  774.       ;     add ebx, new_app_base
  775.            push ecx
  776.            stdcall map_memEx, [proc_mem_map],\
  777.                               [SLOT_BASE+eax+0xB8],\
  778.                               ebx, ecx
  779.            pop ecx
  780.  
  781.            mov edi, [offset]
  782.            and edi, 0xfff
  783.            add edi, [proc_mem_map]
  784.            mov esi, [buff]
  785.            mov edx, ecx
  786.            rep movsb
  787.  
  788.            add [offset], edx
  789.            sub [tmp_w_cnt], edx
  790.            jnz .read_mem
  791.  
  792.            popad
  793.            mov eax, [w_count]
  794.            ret
  795. endp
  796.  
  797. align 4
  798. proc new_sys_threads
  799.            locals
  800.              slot          dd ?
  801.              app_cmdline   dd ? ;0x00
  802.              app_path      dd ? ;0x04
  803.              app_eip       dd ? ;0x08
  804.              app_esp       dd ? ;0x0C
  805.              app_mem       dd ? ;0x10
  806.            endl
  807.  
  808.            cmp eax,1
  809.            jne .failed                  ;other subfunctions
  810.  
  811.            xor  eax,eax
  812.            mov [app_cmdline], eax
  813.            mov [app_path], eax
  814.            mov [app_eip], ebx
  815.            mov [app_esp], ecx
  816.  
  817.            ;mov    esi,new_process_loading
  818.            ;call   sys_msg_board_str
  819.            DEBUGF 1,"%s",new_process_loading
  820. .wait_lock:
  821.            cmp [application_table_status],0
  822.            je .get_lock
  823.            call   change_task
  824.            jmp .wait_lock
  825.  
  826. .get_lock:
  827.            mov eax, 1
  828.            xchg eax, [application_table_status]
  829.            cmp eax, 0
  830.            jne .wait_lock
  831.  
  832.            call   set_application_table_status
  833.  
  834.            call get_new_process_place
  835.            test eax, eax
  836.            jz .failed
  837.  
  838.            mov [slot], eax
  839.  
  840.            mov    esi,[current_slot]
  841.            mov    ebx,esi             ;ebx=esi - pointer to extended information about current thread
  842.  
  843.            mov    edi, eax
  844.            shl    edi,8
  845.            add    edi,SLOT_BASE
  846.            mov    edx,edi             ;edx=edi - pointer to extended infomation about new thread
  847.            mov    ecx,256/4
  848.            xor eax, eax
  849.            cld
  850.            rep    stosd               ;clean extended information about new thread
  851.            mov    esi,ebx
  852.            mov    edi,edx
  853.            mov    ecx,11
  854.            rep    movsb               ;copy process name
  855.  
  856.            mov eax,[ebx+APPDATA.heap_base]
  857.            mov [edx+APPDATA.heap_base], eax
  858.  
  859.            mov ecx,[ebx+APPDATA.heap_top]
  860.            mov [edx+APPDATA.heap_top], ecx
  861.  
  862.            mov eax,[ebx+APPDATA.mem_size]
  863.            mov [edx+APPDATA.mem_size], eax
  864.  
  865.            mov ecx,[ebx+APPDATA.dir_table]
  866.            mov [edx+APPDATA.dir_table],ecx      ;copy page directory
  867.  
  868.            lea eax, [app_cmdline]
  869.            stdcall set_app_params ,[slot],eax,dword 0,\
  870.                                          dword 0,dword 0
  871.  
  872.            ;mov    esi,new_process_running
  873.            ;call   sys_msg_board_str     ;output information about succefull startup
  874.            DEBUGF 1,"%s",new_process_running
  875.  
  876.            mov    [application_table_status],0 ;unlock application_table_status mutex
  877.            mov    eax,[process_number]  ;set result
  878.            ret
  879. .failed:
  880.            mov    [application_table_status],0
  881.            mov    eax,-1
  882.            ret
  883. endp
  884.  
  885. ; param
  886. ;  ebx=mutex
  887.  
  888. align 4
  889. wait_mutex:
  890.            push eax
  891.            push ebx
  892. .do_wait:
  893.            cmp dword [ebx],0
  894.            je .get_lock
  895.            call change_task
  896.            jmp .do_wait
  897. .get_lock:
  898.            mov eax, 1
  899.            xchg eax, [ebx]
  900.            test eax, eax
  901.            jnz .do_wait
  902.            pop ebx
  903.            pop eax
  904.            ret
  905.  
  906. EFL_IF      equ 0x0200
  907. EFL_IOPL1   equ 0x1000
  908. EFL_IOPL2   equ 0x2000
  909. EFL_IOPL3   equ 0x3000
  910.  
  911.  
  912. align 4
  913. proc set_app_params stdcall,slot:dword, params:dword,\
  914.                         cmd_line:dword, app_path:dword, flags:dword
  915.  
  916.            locals
  917.              pl0_stack dd ?
  918.            endl
  919.  
  920.            stdcall kernel_alloc, RING0_STACK_SIZE+512
  921.            mov [pl0_stack], eax
  922.  
  923.            lea edi, [eax+RING0_STACK_SIZE]
  924.  
  925.            mov eax, [slot]
  926.            mov ebx, eax
  927.  
  928.            shl eax, 8
  929.            mov [eax+SLOT_BASE+APPDATA.fpu_state], edi
  930.            mov [eax+SLOT_BASE+APPDATA.fpu_handler], 0
  931.            mov [eax+SLOT_BASE+APPDATA.sse_handler], 0
  932.  
  933. ;set default io permission map
  934.            mov [eax+SLOT_BASE+APPDATA.io_map],\
  935.                   (tss._io_map_0-OS_BASE+PG_MAP)
  936.            mov [eax+SLOT_BASE+APPDATA.io_map+4],\
  937.                   (tss._io_map_1-OS_BASE+PG_MAP)
  938.  
  939.            mov esi, fpu_data
  940.            mov ecx, 512/4
  941.            rep movsd
  942.  
  943.            cmp    ebx,[TASK_COUNT]
  944.            jle    .noinc
  945.            inc    dword [TASK_COUNT]       ;update number of processes
  946. .noinc:
  947.            shl ebx,8
  948.            lea edx, [ebx+SLOT_BASE+APP_EV_OFFSET]
  949.            mov [SLOT_BASE+APPDATA.fd_ev+ebx],edx
  950.            mov [SLOT_BASE+APPDATA.bk_ev+ebx],edx
  951.  
  952.            add edx, APP_OBJ_OFFSET-APP_EV_OFFSET
  953.            mov [SLOT_BASE+APPDATA.fd_obj+ebx],edx
  954.            mov [SLOT_BASE+APPDATA.bk_obj+ebx],edx
  955.  
  956.            mov ecx, [def_cursor]
  957.            mov [SLOT_BASE+APPDATA.cursor+ebx],ecx
  958.            mov eax, [pl0_stack]
  959.            mov [SLOT_BASE+APPDATA.pl0_stack+ebx],eax
  960.  
  961.            shr ebx,3
  962.            mov eax, new_app_base
  963.            mov dword [CURRENT_TASK+ebx+0x10],eax
  964.  
  965. .add_command_line:
  966.            mov edx,[params]
  967.            mov edx,[edx]           ;app_cmdline
  968.            test edx, [cmd_line]    ;check both src & dst
  969.            jz @F                   ;application don't need parameters
  970.  
  971.            mov     eax, edx
  972.            add     eax, 256
  973.            jc      @f
  974.  
  975.            cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
  976.            ja      @f
  977.  
  978.            stdcall strncpy, edx, [cmd_line], 256
  979. @@:
  980.            mov edx,[params]
  981.            mov edx, [edx+4]        ;app_path
  982.            test edx,edx
  983.            jz @F                   ;application don't need path of file
  984.            mov     eax, edx
  985.            add     eax, 1024
  986.            jc      @f
  987.            cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
  988.            ja      @f
  989.            stdcall strncpy, edx, [app_path], 1024
  990. @@:
  991.            mov    ebx,[slot]
  992.            mov    eax,ebx
  993.            shl    ebx,5
  994.            lea    ecx,[draw_data+ebx]  ;ecx - pointer to draw data
  995.  
  996. ; set window state to 'normal' (non-minimized/maximized/rolled-up) state
  997.            mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
  998.            mov     [ebx+window_data+WDATA.fl_redraw], 1
  999.            add    ebx,CURRENT_TASK            ;ebx - pointer to information about process
  1000.            mov    [ebx+TASKDATA.wnd_number],al;set window number on screen = process slot
  1001.  
  1002.            mov    [ebx+TASKDATA.event_mask],dword 1+2+4 ;set default event flags (see 40 function)
  1003.  
  1004.            inc    dword [process_number]
  1005.            mov    eax,[process_number]
  1006.            mov    [ebx+4],eax           ;set PID
  1007.  
  1008. ;set draw data to full screen
  1009.  
  1010.            mov    [ecx+0],dword 0
  1011.            mov    [ecx+4],dword 0
  1012.            mov    eax,[ScreenWidth]
  1013.            mov    [ecx+8],eax
  1014.            mov    eax,[ScreenHeight]
  1015.            mov    [ecx+12],eax
  1016.  
  1017.            mov ebx, [pl0_stack]
  1018.            mov esi,[params]
  1019.            lea ecx, [ebx+REG_EIP]
  1020.            xor eax, eax
  1021.  
  1022.            mov [ebx+REG_RET], dword irq0.return
  1023.            mov [ebx+REG_EDI], eax
  1024.            mov [ebx+REG_ESI], eax
  1025.            mov [ebx+REG_EBP], eax
  1026.            mov [ebx+REG_ESP], ecx   ;ebx+REG_EIP
  1027.            mov [ebx+REG_EBX], eax
  1028.            mov [ebx+REG_EDX], eax
  1029.            mov [ebx+REG_ECX], eax
  1030.            mov [ebx+REG_EAX], eax
  1031.  
  1032.            mov eax, [esi+0x08]       ;app_eip
  1033.            mov [ebx+REG_EIP],  eax   ;app_entry
  1034.            mov [ebx+REG_CS], dword app_code
  1035.            mov [ebx+REG_EFLAGS], dword EFL_IOPL1+EFL_IF
  1036.  
  1037.            mov eax, [esi+0x0C]       ;app_esp
  1038.            mov [ebx+REG_APP_ESP], eax    ;app_stack
  1039.            mov [ebx+REG_SS], dword app_data
  1040.  
  1041.            lea ecx, [ebx+REG_RET]
  1042.            mov ebx, [slot]
  1043.            shl ebx, 8
  1044.            mov [ebx+SLOT_BASE+APPDATA.saved_esp], ecx
  1045.  
  1046. ;flush keyboard and buttons queue
  1047.            mov    [KEY_COUNT],byte 0
  1048.            mov    [BTN_COUNT],byte 0
  1049.  
  1050.            mov    edi,[slot]
  1051.            shl    edi,5
  1052.            add    edi,window_data
  1053.            mov    ebx,[slot]
  1054.            movzx  esi,word [WIN_STACK+ebx*2]
  1055.            lea    esi,[WIN_POS+esi*2]
  1056.            call   windowactivate        ;gui initialization
  1057.  
  1058.            mov    ebx,[slot]
  1059.            shl    ebx,5
  1060.            mov    [CURRENT_TASK+ebx+0xa],byte 0 ;set process state - running
  1061. ; set if debuggee
  1062.            mov eax, [flags]
  1063.            test byte [flags], 1
  1064.            jz   .no_debug
  1065.            mov  [CURRENT_TASK+ebx+0xa],byte 1 ;set process state - suspended
  1066.            mov  eax,[CURRENT_TASK]
  1067.            mov  [SLOT_BASE+ebx*8+0xac],eax ;set debugger PID - current
  1068. .no_debug:
  1069.            ;mov    esi,new_process_running
  1070.            ;call   sys_msg_board_str     ;output information about succefull startup
  1071.            DEBUGF 1,"%s",new_process_running
  1072.            ret
  1073. endp
  1074.  
  1075. include "debug.inc"
  1076.  
  1077.