Subversion Repositories Kolibri OS

Rev

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

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