Subversion Repositories Kolibri OS

Rev

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

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